1 /***************************************************************************
2 * Copyright (C) 2016 - 2019 by Andreas Bolsch *
3 * andreas.bolsch@mni.thm.de *
5 * Copyright (C) 2010 by Antonio Borneo *
6 * borneo.antonio@gmail.com *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
20 ***************************************************************************/
22 /* STM QuadSPI (QSPI) and OctoSPI (OCTOSPI) controller are SPI bus controllers
23 * specifically designed for SPI memories.
24 * Two working modes are available:
25 * - indirect mode: the SPI is controlled by SW. Any custom commands can be sent
27 * - memory mapped mode: the SPI is under QSPI/OCTOSPI control. Memory content
28 * is directly accessible in CPU memory space. CPU can read and execute from
29 * memory (but not write to) */
32 * To have flash mapped in CPU memory space, the QSPI/OCTOSPI controller
33 * has to be in "memory mapped mode". This requires following constraints:
34 * 1) The command "reset init" has to initialize QSPI/OCTOSPI controller and put
35 * it in memory mapped mode;
36 * 2) every command in this file has to return to prompt in memory mapped mode. */
43 #include <helper/binarybuffer.h>
44 #include <helper/bits.h>
45 #include <helper/time_support.h>
46 #include <target/algorithm.h>
47 #include <target/armv7m.h>
48 #include <target/image.h>
54 #undef SPIFLASH_PAGE_PROGRAM
56 /* saved mode settings */
57 #define QSPI_MODE (stmqspi_info->saved_ccr & \
58 (0xF0000000U | QSPI_DCYC_MASK | QSPI_4LINE_MODE | QSPI_ALTB_MODE | QSPI_ADDR4))
60 /* saved read mode settings but indirect read instead of memory mapped
61 * in particular, use the dummy cycle setting from this saved setting */
62 #define QSPI_CCR_READ (QSPI_READ_MODE | (stmqspi_info->saved_ccr & \
63 (0xF0000000U | QSPI_DCYC_MASK | QSPI_4LINE_MODE | QSPI_ALTB_MODE | QSPI_ADDR4 | 0xFF)))
65 /* QSPI_CCR for various other commands, these never use dummy cycles nor alternate bytes */
66 #define QSPI_CCR_READ_STATUS \
67 ((QSPI_MODE & ~QSPI_DCYC_MASK & QSPI_NO_ADDR & QSPI_NO_ALTB) | \
68 (QSPI_READ_MODE | SPIFLASH_READ_STATUS))
70 #define QSPI_CCR_READ_ID \
71 ((QSPI_MODE & ~QSPI_DCYC_MASK & QSPI_NO_ADDR & QSPI_NO_ALTB) | \
72 (QSPI_READ_MODE | SPIFLASH_READ_ID))
74 #define QSPI_CCR_READ_MID \
75 ((QSPI_MODE & ~QSPI_DCYC_MASK & QSPI_NO_ADDR & QSPI_NO_ALTB) | \
76 (QSPI_READ_MODE | SPIFLASH_READ_MID))
78 /* always use 3-byte addresses for read SFDP */
79 #define QSPI_CCR_READ_SFDP \
80 ((QSPI_MODE & ~QSPI_DCYC_MASK & ~QSPI_ADDR4 & QSPI_NO_ALTB) | \
81 (QSPI_READ_MODE | QSPI_ADDR3 | SPIFLASH_READ_SFDP))
83 #define QSPI_CCR_WRITE_ENABLE \
84 ((QSPI_MODE & ~QSPI_DCYC_MASK & QSPI_NO_ADDR & QSPI_NO_ALTB & QSPI_NO_DATA) | \
85 (QSPI_WRITE_MODE | SPIFLASH_WRITE_ENABLE))
87 #define QSPI_CCR_SECTOR_ERASE \
88 ((QSPI_MODE & ~QSPI_DCYC_MASK & QSPI_NO_ALTB & QSPI_NO_DATA) | \
89 (QSPI_WRITE_MODE | stmqspi_info->dev.erase_cmd))
91 #define QSPI_CCR_MASS_ERASE \
92 ((QSPI_MODE & ~QSPI_DCYC_MASK & QSPI_NO_ADDR & QSPI_NO_ALTB & QSPI_NO_DATA) | \
93 (QSPI_WRITE_MODE | stmqspi_info->dev.chip_erase_cmd))
95 #define QSPI_CCR_PAGE_PROG \
96 ((QSPI_MODE & ~QSPI_DCYC_MASK & QSPI_NO_ALTB) | \
97 (QSPI_WRITE_MODE | stmqspi_info->dev.pprog_cmd))
99 /* saved mode settings */
100 #define OCTOSPI_MODE (stmqspi_info->saved_cr & 0xCFFFFFFF)
102 #define OPI_MODE ((stmqspi_info->saved_ccr & OCTOSPI_ISIZE_MASK) != 0)
104 #define OCTOSPI_MODE_CCR (stmqspi_info->saved_ccr & \
105 (0xF0000000U | OCTOSPI_8LINE_MODE | OCTOSPI_ALTB_MODE | OCTOSPI_ADDR4))
107 /* use saved ccr for read */
108 #define OCTOSPI_CCR_READ OCTOSPI_MODE_CCR
110 /* OCTOSPI_CCR for various other commands, these never use alternate bytes *
111 * for READ_STATUS and READ_ID, 4-byte address 0 *
112 * 4 dummy cycles must sent in OPI mode when DQS is disabled. However, when *
113 * DQS is enabled, some STM32 devices need at least 6 dummy cycles for *
114 * proper operation, but otherwise the actual number has no effect! *
115 * E.g. RM0432 Rev. 7 is incorrect regarding this: L4R9 works well with 4 *
116 * dummy clocks whereas L4P5 not at all. *
119 ((stmqspi_info->saved_ccr & OCTOSPI_DQSEN) ? 6U : 4U)
121 #define OCTOSPI_CCR_READ_STATUS \
122 ((OCTOSPI_MODE_CCR & OCTOSPI_NO_DDTR & \
123 (OPI_MODE ? ~0U : OCTOSPI_NO_ADDR) & OCTOSPI_NO_ALTB))
125 #define OCTOSPI_CCR_READ_ID \
126 ((OCTOSPI_MODE_CCR & OCTOSPI_NO_DDTR & \
127 (OPI_MODE ? ~0U : OCTOSPI_NO_ADDR) & OCTOSPI_NO_ALTB))
129 #define OCTOSPI_CCR_READ_MID OCTOSPI_CCR_READ_ID
131 /* 4-byte address in octo mode, else 3-byte address for read SFDP */
132 #define OCTOSPI_CCR_READ_SFDP(len) \
133 ((OCTOSPI_MODE_CCR & OCTOSPI_NO_DDTR & ~OCTOSPI_ADDR4 & OCTOSPI_NO_ALTB) | \
134 (((len) < 4) ? OCTOSPI_ADDR3 : OCTOSPI_ADDR4))
136 #define OCTOSPI_CCR_WRITE_ENABLE \
137 ((OCTOSPI_MODE_CCR & OCTOSPI_NO_ADDR & OCTOSPI_NO_ALTB & OCTOSPI_NO_DATA))
139 #define OCTOSPI_CCR_SECTOR_ERASE \
140 ((OCTOSPI_MODE_CCR & OCTOSPI_NO_ALTB & OCTOSPI_NO_DATA))
142 #define OCTOSPI_CCR_MASS_ERASE \
143 ((OCTOSPI_MODE_CCR & OCTOSPI_NO_ADDR & OCTOSPI_NO_ALTB & OCTOSPI_NO_DATA))
145 #define OCTOSPI_CCR_PAGE_PROG \
146 ((OCTOSPI_MODE_CCR & QSPI_NO_ALTB))
148 #define SPI_ADSIZE (((stmqspi_info->saved_ccr >> SPI_ADSIZE_POS) & 0x3) + 1)
150 #define OPI_CMD(cmd) ((OPI_MODE ? ((((uint16_t)(cmd)) << 8) | (~(cmd) & 0xFFU)) : (cmd)))
152 /* convert uint32_t into 4 uint8_t in little endian byte order */
153 static inline uint32_t h_to_le_32(uint32_t val)
157 h_u32_to_le((uint8_t *)&result, val);
162 #define SPI_CMD_TIMEOUT (100)
163 #define SPI_PROBE_TIMEOUT (100)
164 #define SPI_MAX_TIMEOUT (2000)
165 #define SPI_MASS_ERASE_TIMEOUT (400000)
173 struct stmqspi_flash_bank {
177 struct flash_device dev;
179 uint32_t saved_cr; /* in particular FSEL, DFM bit mask in QUADSPI_CR *AND* OCTOSPI_CR */
180 uint32_t saved_ccr; /* different meaning for QUADSPI and OCTOSPI */
181 uint32_t saved_tcr; /* only for OCTOSPI */
182 uint32_t saved_ir; /* only for OCTOSPI */
183 unsigned int sfdp_dummy1; /* number of dummy bytes for SFDP read for flash1 and octo */
184 unsigned int sfdp_dummy2; /* number of dummy bytes for SFDP read for flash2 */
187 static inline int octospi_cmd(struct flash_bank *bank, uint32_t mode,
188 uint32_t ccr, uint32_t ir)
190 struct target *target = bank->target;
191 const struct stmqspi_flash_bank *stmqspi_info = bank->driver_priv;
192 const uint32_t io_base = stmqspi_info->io_base;
194 int retval = target_write_u32(target, io_base + OCTOSPI_CR,
195 OCTOSPI_MODE | mode);
197 if (retval != ERROR_OK)
200 retval = target_write_u32(target, io_base + OCTOSPI_TCR,
201 (stmqspi_info->saved_tcr & ~OCTOSPI_DCYC_MASK) |
202 ((OPI_MODE && (mode == OCTOSPI_READ_MODE)) ?
203 (OPI_DUMMY << OCTOSPI_DCYC_POS) : 0));
205 if (retval != ERROR_OK)
208 retval = target_write_u32(target, io_base + OCTOSPI_CCR, ccr);
210 if (retval != ERROR_OK)
213 return target_write_u32(target, io_base + OCTOSPI_IR, OPI_CMD(ir));
216 FLASH_BANK_COMMAND_HANDLER(stmqspi_flash_bank_command)
218 struct stmqspi_flash_bank *stmqspi_info;
221 LOG_DEBUG("%s", __func__);
224 return ERROR_COMMAND_SYNTAX_ERROR;
226 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[6], io_base);
228 stmqspi_info = malloc(sizeof(struct stmqspi_flash_bank));
230 LOG_ERROR("not enough memory");
234 bank->driver_priv = stmqspi_info;
235 stmqspi_info->sfdp_dummy1 = 0;
236 stmqspi_info->sfdp_dummy2 = 0;
237 stmqspi_info->probed = false;
238 stmqspi_info->io_base = io_base;
245 static int poll_busy(struct flash_bank *bank, int timeout)
247 struct target *target = bank->target;
248 struct stmqspi_flash_bank *stmqspi_info = bank->driver_priv;
249 uint32_t io_base = stmqspi_info->io_base;
252 endtime = timeval_ms() + timeout;
255 int retval = target_read_u32(target, io_base + SPI_SR, &spi_sr);
257 if (retval != ERROR_OK)
260 if ((spi_sr & BIT(SPI_BUSY)) == 0) {
261 /* Clear transmit finished flag */
262 return target_write_u32(target, io_base + SPI_FCR, BIT(SPI_TCF));
264 LOG_DEBUG("busy: 0x%08X", spi_sr);
266 } while (timeval_ms() < endtime);
268 LOG_ERROR("Timeout while polling BUSY");
269 return ERROR_FLASH_OPERATION_FAILED;
272 static int stmqspi_abort(struct flash_bank *bank)
274 struct target *target = bank->target;
275 const struct stmqspi_flash_bank *stmqspi_info = bank->driver_priv;
276 const uint32_t io_base = stmqspi_info->io_base;
279 int retval = target_read_u32(target, io_base + SPI_CR, &cr);
281 if (retval != ERROR_OK)
284 return target_write_u32(target, io_base + SPI_CR, cr | BIT(SPI_ABORT));
287 /* Set to memory-mapped mode, e.g. after an error */
288 static int set_mm_mode(struct flash_bank *bank)
290 struct target *target = bank->target;
291 struct stmqspi_flash_bank *stmqspi_info = bank->driver_priv;
292 uint32_t io_base = stmqspi_info->io_base;
295 /* Reset Address register bits 0 and 1, see various errata sheets */
296 retval = target_write_u32(target, io_base + SPI_AR, 0x0);
297 if (retval != ERROR_OK)
300 /* Abort any previous operation */
301 retval = stmqspi_abort(bank);
302 if (retval != ERROR_OK)
305 /* Wait for busy to be cleared */
306 retval = poll_busy(bank, SPI_PROBE_TIMEOUT);
307 if (retval != ERROR_OK)
310 /* Finally switch to memory mapped mode */
312 retval = target_write_u32(target, io_base + OCTOSPI_CR,
313 OCTOSPI_MODE | OCTOSPI_MM_MODE);
314 if (retval == ERROR_OK)
315 retval = target_write_u32(target, io_base + OCTOSPI_CCR,
316 stmqspi_info->saved_ccr);
317 if (retval == ERROR_OK)
318 retval = target_write_u32(target, io_base + OCTOSPI_TCR,
319 stmqspi_info->saved_tcr);
320 if (retval == ERROR_OK)
321 retval = target_write_u32(target, io_base + OCTOSPI_IR,
322 stmqspi_info->saved_ir);
324 retval = target_write_u32(target, io_base + QSPI_CR,
325 stmqspi_info->saved_cr);
326 if (retval == ERROR_OK)
327 retval = target_write_u32(target, io_base + QSPI_CCR,
328 stmqspi_info->saved_ccr);
333 /* Read the status register of the external SPI flash chip(s). */
334 static int read_status_reg(struct flash_bank *bank, uint16_t *status)
336 struct target *target = bank->target;
337 struct stmqspi_flash_bank *stmqspi_info = bank->driver_priv;
338 uint32_t io_base = stmqspi_info->io_base;
342 /* Abort any previous operation */
343 retval = stmqspi_abort(bank);
344 if (retval != ERROR_OK)
347 /* Wait for busy to be cleared */
348 retval = poll_busy(bank, SPI_PROBE_TIMEOUT);
349 if (retval != ERROR_OK)
352 /* Read always two (for DTR mode) bytes per chip */
354 retval = target_write_u32(target, io_base + SPI_DLR,
355 ((stmqspi_info->saved_cr & BIT(SPI_DUAL_FLASH)) ? 2 * count : count) - 1);
356 if (retval != ERROR_OK)
361 retval = octospi_cmd(bank, OCTOSPI_READ_MODE, OCTOSPI_CCR_READ_STATUS,
362 SPIFLASH_READ_STATUS);
364 /* Dummy address 0, only required for 8-line mode */
365 retval = target_write_u32(target, io_base + SPI_AR, 0);
366 if (retval != ERROR_OK)
370 retval = target_write_u32(target, io_base + QSPI_CCR, QSPI_CCR_READ_STATUS);
371 if (retval != ERROR_OK)
376 /* for debugging only */
378 (void)target_read_u32(target, io_base + SPI_SR, &dummy);
380 for ( ; count > 0; --count) {
381 if ((stmqspi_info->saved_cr & (BIT(SPI_DUAL_FLASH) | BIT(SPI_FSEL_FLASH)))
382 != BIT(SPI_FSEL_FLASH)) {
383 /* get status of flash 1 in dual mode or flash 1 only mode */
384 retval = target_read_u8(target, io_base + SPI_DR, &data);
385 if (retval != ERROR_OK)
390 if ((stmqspi_info->saved_cr & (BIT(SPI_DUAL_FLASH) | BIT(SPI_FSEL_FLASH))) != 0) {
391 /* get status of flash 2 in dual mode or flash 2 only mode */
392 retval = target_read_u8(target, io_base + SPI_DR, &data);
393 if (retval != ERROR_OK)
395 *status |= ((uint16_t)data) << 8;
399 LOG_DEBUG("flash status regs: 0x%04" PRIx16, *status);
405 /* check for WIP (write in progress) bit(s) in status register(s) */
407 static int wait_till_ready(struct flash_bank *bank, int timeout)
413 endtime = timeval_ms() + timeout;
415 /* Read flash status register(s) */
416 retval = read_status_reg(bank, &status);
417 if (retval != ERROR_OK)
420 if ((status & ((SPIFLASH_BSY_BIT << 8) | SPIFLASH_BSY_BIT)) == 0)
423 } while (timeval_ms() < endtime);
425 LOG_ERROR("timeout");
426 return ERROR_FLASH_OPERATION_FAILED;
429 /* Send "write enable" command to SPI flash chip(s). */
430 static int qspi_write_enable(struct flash_bank *bank)
432 struct target *target = bank->target;
433 struct stmqspi_flash_bank *stmqspi_info = bank->driver_priv;
434 uint32_t io_base = stmqspi_info->io_base;
438 /* Abort any previous operation */
439 retval = stmqspi_abort(bank);
440 if (retval != ERROR_OK)
443 /* Wait for busy to be cleared */
444 retval = poll_busy(bank, SPI_PROBE_TIMEOUT);
445 if (retval != ERROR_OK)
448 /* Send write enable command */
450 retval = octospi_cmd(bank, OCTOSPI_WRITE_MODE, OCTOSPI_CCR_WRITE_ENABLE,
451 SPIFLASH_WRITE_ENABLE);
453 /* Dummy address 0, only required for 8-line mode */
454 retval = target_write_u32(target, io_base + SPI_AR, 0);
455 if (retval != ERROR_OK)
459 retval = target_write_u32(target, io_base + QSPI_CCR, QSPI_CCR_WRITE_ENABLE);
460 if (retval != ERROR_OK)
464 /* Wait for transmit of command completed */
465 poll_busy(bank, SPI_CMD_TIMEOUT);
466 if (retval != ERROR_OK)
469 /* Read flash status register */
470 retval = read_status_reg(bank, &status);
471 if (retval != ERROR_OK)
474 /* Check write enabled for flash 1 */
475 if ((stmqspi_info->saved_cr & (BIT(SPI_DUAL_FLASH) | BIT(SPI_FSEL_FLASH)))
476 != BIT(SPI_FSEL_FLASH))
477 if ((status & (SPIFLASH_WE_BIT | SPIFLASH_BSY_BIT)) != SPIFLASH_WE_BIT) {
478 LOG_ERROR("Cannot write enable flash1. Status=0x%02x",
480 return ERROR_FLASH_OPERATION_FAILED;
483 /* Check write enabled for flash 2 */
485 if ((stmqspi_info->saved_cr & (BIT(SPI_DUAL_FLASH) | BIT(SPI_FSEL_FLASH))) != 0)
486 if ((status & (SPIFLASH_WE_BIT | SPIFLASH_BSY_BIT)) != SPIFLASH_WE_BIT) {
487 LOG_ERROR("Cannot write enable flash2. Status=0x%02x",
489 return ERROR_FLASH_OPERATION_FAILED;
496 COMMAND_HANDLER(stmqspi_handle_mass_erase_command)
498 struct target *target = NULL;
499 struct flash_bank *bank;
500 struct stmqspi_flash_bank *stmqspi_info;
501 struct duration bench;
507 LOG_DEBUG("%s", __func__);
510 return ERROR_COMMAND_SYNTAX_ERROR;
512 retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
513 if (retval != ERROR_OK)
516 stmqspi_info = bank->driver_priv;
517 target = bank->target;
519 if (target->state != TARGET_HALTED) {
520 LOG_ERROR("Target not halted");
521 return ERROR_TARGET_NOT_HALTED;
524 if (!(stmqspi_info->probed)) {
525 LOG_ERROR("Flash bank not probed");
526 return ERROR_FLASH_BANK_NOT_PROBED;
529 if (stmqspi_info->dev.chip_erase_cmd == 0x00) {
530 LOG_ERROR("Mass erase not available for this device");
531 return ERROR_FLASH_OPER_UNSUPPORTED;
534 for (sector = 0; sector < bank->num_sectors; sector++) {
535 if (bank->sectors[sector].is_protected) {
536 LOG_ERROR("Flash sector %u protected", sector);
537 return ERROR_FLASH_PROTECTED;
541 io_base = stmqspi_info->io_base;
542 duration_start(&bench);
544 retval = qspi_write_enable(bank);
545 if (retval != ERROR_OK)
548 /* Send Mass Erase command */
550 retval = octospi_cmd(bank, OCTOSPI_WRITE_MODE, OCTOSPI_CCR_MASS_ERASE,
551 stmqspi_info->dev.chip_erase_cmd);
553 retval = target_write_u32(target, io_base + QSPI_CCR, QSPI_CCR_MASS_ERASE);
554 if (retval != ERROR_OK)
557 /* Wait for transmit of command completed */
558 poll_busy(bank, SPI_CMD_TIMEOUT);
559 if (retval != ERROR_OK)
562 /* Read flash status register(s) */
563 retval = read_status_reg(bank, &status);
564 if (retval != ERROR_OK)
567 /* Check for command in progress for flash 1 */
568 if (((stmqspi_info->saved_cr & (BIT(SPI_DUAL_FLASH) | BIT(SPI_FSEL_FLASH)))
569 != BIT(SPI_FSEL_FLASH)) && ((status & SPIFLASH_BSY_BIT) == 0) &&
570 ((status & SPIFLASH_WE_BIT) != 0)) {
571 LOG_ERROR("Mass erase command not accepted by flash1. Status=0x%02x",
573 retval = ERROR_FLASH_OPERATION_FAILED;
577 /* Check for command in progress for flash 2 */
579 if (((stmqspi_info->saved_cr & (BIT(SPI_DUAL_FLASH) | BIT(SPI_FSEL_FLASH))) != 0) &&
580 ((status & SPIFLASH_BSY_BIT) == 0) &&
581 ((status & SPIFLASH_WE_BIT) != 0)) {
582 LOG_ERROR("Mass erase command not accepted by flash2. Status=0x%02x",
584 retval = ERROR_FLASH_OPERATION_FAILED;
588 /* Poll WIP for end of self timed Sector Erase cycle */
589 retval = wait_till_ready(bank, SPI_MASS_ERASE_TIMEOUT);
591 duration_measure(&bench);
592 if (retval == ERROR_OK)
593 command_print(CMD, "stmqspi mass erase completed in %fs (%0.3f KiB/s)",
594 duration_elapsed(&bench),
595 duration_kbps(&bench, bank->size));
597 command_print(CMD, "stmqspi mass erase not completed even after %fs",
598 duration_elapsed(&bench));
601 /* Switch to memory mapped mode before return to prompt */
607 static int log2u(uint32_t word)
611 for (result = 0; (unsigned int) result < sizeof(uint32_t) * CHAR_BIT; result++)
612 if (word == BIT(result))
618 COMMAND_HANDLER(stmqspi_handle_set)
620 struct flash_bank *bank = NULL;
621 struct target *target = NULL;
622 struct stmqspi_flash_bank *stmqspi_info = NULL;
623 struct flash_sector *sectors = NULL;
625 unsigned int index = 0, dual, fsize;
628 LOG_DEBUG("%s", __func__);
630 dual = (stmqspi_info->saved_cr & BIT(SPI_DUAL_FLASH)) ? 1 : 0;
632 /* chip_erase_cmd, sectorsize and erase_cmd are optional */
633 if ((CMD_ARGC < 7) || (CMD_ARGC > 10))
634 return ERROR_COMMAND_SYNTAX_ERROR;
636 retval = CALL_COMMAND_HANDLER(flash_command_get_bank, index++, &bank);
637 if (retval != ERROR_OK)
640 target = bank->target;
641 stmqspi_info = bank->driver_priv;
643 /* invalidate all old info */
644 if (stmqspi_info->probed)
647 bank->num_sectors = 0;
648 bank->sectors = NULL;
649 stmqspi_info->sfdp_dummy1 = 0;
650 stmqspi_info->sfdp_dummy2 = 0;
651 stmqspi_info->probed = false;
652 memset(&stmqspi_info->dev, 0, sizeof(stmqspi_info->dev));
653 stmqspi_info->dev.name = "unknown";
655 strncpy(stmqspi_info->devname, CMD_ARGV[index++], sizeof(stmqspi_info->devname) - 1);
656 stmqspi_info->devname[sizeof(stmqspi_info->devname) - 1] = '\0';
658 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[index++], stmqspi_info->dev.size_in_bytes);
659 if (log2u(stmqspi_info->dev.size_in_bytes) < 8) {
660 command_print(CMD, "stmqspi: device size must be 2^n with n >= 8");
661 return ERROR_COMMAND_SYNTAX_ERROR;
664 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[index++], stmqspi_info->dev.pagesize);
665 if (stmqspi_info->dev.pagesize > stmqspi_info->dev.size_in_bytes ||
666 (log2u(stmqspi_info->dev.pagesize) < 0)) {
667 command_print(CMD, "stmqspi: page size must be 2^n and <= device size");
668 return ERROR_COMMAND_SYNTAX_ERROR;
671 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[index++], stmqspi_info->dev.read_cmd);
672 if ((stmqspi_info->dev.read_cmd != 0x03) &&
673 (stmqspi_info->dev.read_cmd != 0x13)) {
674 command_print(CMD, "stmqspi: only 0x03/0x13 READ cmd allowed");
675 return ERROR_COMMAND_SYNTAX_ERROR;
678 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[index++], stmqspi_info->dev.qread_cmd);
679 if ((stmqspi_info->dev.qread_cmd != 0x00) &&
680 (stmqspi_info->dev.qread_cmd != 0x0B) &&
681 (stmqspi_info->dev.qread_cmd != 0x0C) &&
682 (stmqspi_info->dev.qread_cmd != 0x3B) &&
683 (stmqspi_info->dev.qread_cmd != 0x3C) &&
684 (stmqspi_info->dev.qread_cmd != 0x6B) &&
685 (stmqspi_info->dev.qread_cmd != 0x6C) &&
686 (stmqspi_info->dev.qread_cmd != 0xBB) &&
687 (stmqspi_info->dev.qread_cmd != 0xBC) &&
688 (stmqspi_info->dev.qread_cmd != 0xEB) &&
689 (stmqspi_info->dev.qread_cmd != 0xEC) &&
690 (stmqspi_info->dev.qread_cmd != 0xEE)) {
691 command_print(CMD, "stmqspi: only 0x0B/0x0C/0x3B/0x3C/"
692 "0x6B/0x6C/0xBB/0xBC/0xEB/0xEC/0xEE QREAD allowed");
693 return ERROR_COMMAND_SYNTAX_ERROR;
696 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[index++], stmqspi_info->dev.pprog_cmd);
697 if ((stmqspi_info->dev.pprog_cmd != 0x02) &&
698 (stmqspi_info->dev.pprog_cmd != 0x12) &&
699 (stmqspi_info->dev.pprog_cmd != 0x32)) {
700 command_print(CMD, "stmqspi: only 0x02/0x12/0x32 PPRG cmd allowed");
701 return ERROR_COMMAND_SYNTAX_ERROR;
704 if (index < CMD_ARGC)
705 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[index++], stmqspi_info->dev.chip_erase_cmd);
707 stmqspi_info->dev.chip_erase_cmd = 0x00;
709 if (index < CMD_ARGC) {
710 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[index++], stmqspi_info->dev.sectorsize);
711 if ((stmqspi_info->dev.sectorsize > stmqspi_info->dev.size_in_bytes) ||
712 (stmqspi_info->dev.sectorsize < stmqspi_info->dev.pagesize) ||
713 (log2u(stmqspi_info->dev.sectorsize) < 0)) {
714 command_print(CMD, "stmqspi: sector size must be 2^n and <= device size");
715 return ERROR_COMMAND_SYNTAX_ERROR;
718 if (index < CMD_ARGC)
719 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[index++], stmqspi_info->dev.erase_cmd);
721 return ERROR_COMMAND_SYNTAX_ERROR;
723 /* no sector size / sector erase cmd given, treat whole bank as a single sector */
724 stmqspi_info->dev.erase_cmd = 0x00;
725 stmqspi_info->dev.sectorsize = stmqspi_info->dev.size_in_bytes;
728 /* set correct size value */
729 bank->size = stmqspi_info->dev.size_in_bytes << dual;
731 io_base = stmqspi_info->io_base;
734 retval = target_read_u32(target, io_base + SPI_DCR, &dcr);
736 if (retval != ERROR_OK)
739 fsize = (dcr >> SPI_FSIZE_POS) & (BIT(SPI_FSIZE_LEN) - 1);
741 LOG_DEBUG("FSIZE = 0x%04x", fsize);
742 if (bank->size == BIT(fsize + 1))
743 LOG_DEBUG("FSIZE in DCR(1) matches actual capacity. Beware of silicon bug in H7, L4+, MP1.");
744 else if (bank->size == BIT(fsize + 0))
745 LOG_DEBUG("FSIZE in DCR(1) is off by one regarding actual capacity. Fix for silicon bug?");
747 LOG_ERROR("FSIZE in DCR(1) doesn't match actual capacity.");
749 /* create and fill sectors array */
751 stmqspi_info->dev.size_in_bytes / stmqspi_info->dev.sectorsize;
752 sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
754 LOG_ERROR("not enough memory");
758 for (unsigned int sector = 0; sector < bank->num_sectors; sector++) {
759 sectors[sector].offset = sector * (stmqspi_info->dev.sectorsize << dual);
760 sectors[sector].size = (stmqspi_info->dev.sectorsize << dual);
761 sectors[sector].is_erased = -1;
762 sectors[sector].is_protected = 0;
765 bank->sectors = sectors;
766 stmqspi_info->dev.name = stmqspi_info->devname;
767 if (stmqspi_info->dev.size_in_bytes / 4096)
768 LOG_INFO("flash \'%s\' id = unknown\nchip size = %" PRIu32 "kbytes,"
769 " bank size = %" PRIu32 "kbytes", stmqspi_info->dev.name,
770 stmqspi_info->dev.size_in_bytes / 1024,
771 (stmqspi_info->dev.size_in_bytes / 1024) << dual);
773 LOG_INFO("flash \'%s\' id = unknown\nchip size = %" PRIu32 "bytes,"
774 " bank size = %" PRIu32 "bytes", stmqspi_info->dev.name,
775 stmqspi_info->dev.size_in_bytes,
776 stmqspi_info->dev.size_in_bytes << dual);
778 stmqspi_info->probed = true;
783 COMMAND_HANDLER(stmqspi_handle_cmd)
785 struct target *target = NULL;
786 struct flash_bank *bank;
787 struct stmqspi_flash_bank *stmqspi_info = NULL;
788 uint32_t io_base, addr;
789 uint8_t num_write, num_read, cmd_byte, data;
792 char temp[4], output[(2 + max + 256) * 3 + 8];
795 LOG_DEBUG("%s", __func__);
798 return ERROR_COMMAND_SYNTAX_ERROR;
800 num_write = CMD_ARGC - 2;
801 if (num_write > max) {
802 LOG_ERROR("at most %d bytes may be sent", max);
803 return ERROR_COMMAND_SYNTAX_ERROR;
806 retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
807 if (retval != ERROR_OK)
810 target = bank->target;
811 stmqspi_info = bank->driver_priv;
812 io_base = stmqspi_info->io_base;
814 if (target->state != TARGET_HALTED) {
815 LOG_ERROR("Target not halted");
816 return ERROR_TARGET_NOT_HALTED;
819 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[1], num_read);
820 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[2], cmd_byte);
823 /* nothing to read, then one command byte and for dual flash
824 * an *even* number of data bytes to follow */
825 if (stmqspi_info->saved_cr & BIT(SPI_DUAL_FLASH)) {
826 if ((num_write & 1) == 0) {
827 LOG_ERROR("number of data bytes to write must be even in dual mode");
828 return ERROR_COMMAND_SYNTAX_ERROR;
832 /* read mode, one command byte and up to four following address bytes */
833 if (stmqspi_info->saved_cr & BIT(SPI_DUAL_FLASH)) {
834 if ((num_read & 1) != 0) {
835 LOG_ERROR("number of bytes to read must be even in dual mode");
836 return ERROR_COMMAND_SYNTAX_ERROR;
839 if ((num_write < 1) || (num_write > 5)) {
840 LOG_ERROR("one cmd and up to four addr bytes must be send when reading");
841 return ERROR_COMMAND_SYNTAX_ERROR;
845 /* Abort any previous operation */
846 retval = stmqspi_abort(bank);
847 if (retval != ERROR_OK)
850 /* Wait for busy to be cleared */
851 retval = poll_busy(bank, SPI_PROBE_TIMEOUT);
852 if (retval != ERROR_OK)
855 /* send command byte */
856 snprintf(output, sizeof(output), "spi: %02x ", cmd_byte);
858 /* write, send cmd byte */
859 retval = target_write_u32(target, io_base + SPI_DLR, ((uint32_t)num_write) - 2);
860 if (retval != ERROR_OK)
864 retval = octospi_cmd(bank, OCTOSPI_WRITE_MODE,
865 (OCTOSPI_MODE_CCR & OCTOSPI_NO_ALTB & OCTOSPI_NO_ADDR &
866 ((num_write == 1) ? OCTOSPI_NO_DATA : ~0U)), cmd_byte);
868 retval = target_write_u32(target, io_base + QSPI_CCR,
869 (QSPI_MODE & ~QSPI_DCYC_MASK & QSPI_NO_ALTB & QSPI_NO_ADDR &
870 ((num_write == 1) ? QSPI_NO_DATA : ~0U)) |
871 (QSPI_WRITE_MODE | cmd_byte));
872 if (retval != ERROR_OK)
875 /* send additional data bytes */
876 for (count = 3; count < CMD_ARGC; count++) {
877 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[count], data);
878 snprintf(temp, sizeof(temp), "%02" PRIx8 " ", data);
879 retval = target_write_u8(target, io_base + SPI_DR, data);
880 if (retval != ERROR_OK)
882 strncat(output, temp, sizeof(output) - strlen(output) - 1);
884 strncat(output, "-> ", sizeof(output) - strlen(output) - 1);
886 /* read, pack additional bytes into address */
888 for (count = 3; count < CMD_ARGC; count++) {
889 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[count], data);
890 snprintf(temp, sizeof(temp), "%02" PRIx8 " ", data);
891 addr = (addr << 8) | data;
892 strncat(output, temp, sizeof(output) - strlen(output) - 1);
894 strncat(output, "-> ", sizeof(output) - strlen(output) - 1);
896 /* send cmd byte, if ADMODE indicates no address, this already triggers command */
897 retval = target_write_u32(target, io_base + SPI_DLR, ((uint32_t)num_read) - 1);
898 if (retval != ERROR_OK)
901 retval = octospi_cmd(bank, OCTOSPI_READ_MODE,
902 (OCTOSPI_MODE_CCR & OCTOSPI_NO_DDTR & OCTOSPI_NO_ALTB & ~OCTOSPI_ADDR4 &
903 ((num_write == 1) ? OCTOSPI_NO_ADDR : ~0U)) |
904 (((num_write - 2) & 0x3U) << SPI_ADSIZE_POS), cmd_byte);
906 retval = target_write_u32(target, io_base + QSPI_CCR,
907 (QSPI_MODE & ~QSPI_DCYC_MASK & QSPI_NO_ALTB & ~QSPI_ADDR4 &
908 ((num_write == 1) ? QSPI_NO_ADDR : ~0U)) |
909 ((QSPI_READ_MODE | (((num_write - 2) & 0x3U) << SPI_ADSIZE_POS) | cmd_byte)));
910 if (retval != ERROR_OK)
914 /* if ADMODE indicates address required, only the write to AR triggers command */
915 retval = target_write_u32(target, io_base + SPI_AR, addr);
916 if (retval != ERROR_OK)
920 /* read response bytes */
921 for ( ; num_read > 0; num_read--) {
922 retval = target_read_u8(target, io_base + SPI_DR, &data);
923 if (retval != ERROR_OK)
925 snprintf(temp, sizeof(temp), "%02" PRIx8 " ", data);
926 strncat(output, temp, sizeof(output) - strlen(output) - 1);
929 command_print(CMD, "%s", output);
932 /* Switch to memory mapped mode before return to prompt */
938 static int qspi_erase_sector(struct flash_bank *bank, unsigned int sector)
940 struct target *target = bank->target;
941 struct stmqspi_flash_bank *stmqspi_info = bank->driver_priv;
942 uint32_t io_base = stmqspi_info->io_base;
946 retval = qspi_write_enable(bank);
947 if (retval != ERROR_OK)
950 /* Send Sector Erase command */
952 retval = octospi_cmd(bank, OCTOSPI_WRITE_MODE, OCTOSPI_CCR_SECTOR_ERASE,
953 stmqspi_info->dev.erase_cmd);
955 retval = target_write_u32(target, io_base + QSPI_CCR, QSPI_CCR_SECTOR_ERASE);
956 if (retval != ERROR_OK)
959 /* Address is sector offset, this write initiates command transmission */
960 retval = target_write_u32(target, io_base + SPI_AR, bank->sectors[sector].offset);
961 if (retval != ERROR_OK)
964 /* Wait for transmit of command completed */
965 poll_busy(bank, SPI_CMD_TIMEOUT);
966 if (retval != ERROR_OK)
969 /* Read flash status register(s) */
970 retval = read_status_reg(bank, &status);
971 if (retval != ERROR_OK)
974 LOG_DEBUG("erase status regs: 0x%04" PRIx16, status);
976 /* Check for command in progress for flash 1 */
977 /* If BSY and WE are already cleared the erase did probably complete already */
978 if (((stmqspi_info->saved_cr & (BIT(SPI_DUAL_FLASH) | BIT(SPI_FSEL_FLASH)))
979 != BIT(SPI_FSEL_FLASH)) && ((status & SPIFLASH_BSY_BIT) == 0) &&
980 ((status & SPIFLASH_WE_BIT) != 0)) {
981 LOG_ERROR("Sector erase command not accepted by flash1. Status=0x%02x",
983 retval = ERROR_FLASH_OPERATION_FAILED;
987 /* Check for command in progress for flash 2 */
988 /* If BSY and WE are already cleared the erase did probably complete already */
990 if (((stmqspi_info->saved_cr & (BIT(SPI_DUAL_FLASH) | BIT(SPI_FSEL_FLASH))) != 0) &&
991 ((status & SPIFLASH_BSY_BIT) == 0) &&
992 ((status & SPIFLASH_WE_BIT) != 0)) {
993 LOG_ERROR("Sector erase command not accepted by flash2. Status=0x%02x",
995 retval = ERROR_FLASH_OPERATION_FAILED;
999 /* Erase takes a long time, so some sort of progress message is a good idea */
1000 LOG_DEBUG("erasing sector %4u", sector);
1002 /* Poll WIP for end of self timed Sector Erase cycle */
1003 retval = wait_till_ready(bank, SPI_MAX_TIMEOUT);
1009 static int stmqspi_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
1011 struct target *target = bank->target;
1012 struct stmqspi_flash_bank *stmqspi_info = bank->driver_priv;
1013 unsigned int sector;
1014 int retval = ERROR_OK;
1016 LOG_DEBUG("%s: from sector %u to sector %u", __func__, first, last);
1018 if (target->state != TARGET_HALTED) {
1019 LOG_ERROR("Target not halted");
1020 return ERROR_TARGET_NOT_HALTED;
1023 if (!(stmqspi_info->probed)) {
1024 LOG_ERROR("Flash bank not probed");
1025 return ERROR_FLASH_BANK_NOT_PROBED;
1028 if (stmqspi_info->dev.erase_cmd == 0x00) {
1029 LOG_ERROR("Sector erase not available for this device");
1030 return ERROR_FLASH_OPER_UNSUPPORTED;
1033 if ((last < first) || (last >= bank->num_sectors)) {
1034 LOG_ERROR("Flash sector invalid");
1035 return ERROR_FLASH_SECTOR_INVALID;
1038 for (sector = first; sector <= last; sector++) {
1039 if (bank->sectors[sector].is_protected) {
1040 LOG_ERROR("Flash sector %u protected", sector);
1041 return ERROR_FLASH_PROTECTED;
1045 for (sector = first; sector <= last; sector++) {
1046 retval = qspi_erase_sector(bank, sector);
1047 if (retval != ERROR_OK)
1053 if (retval != ERROR_OK)
1054 LOG_ERROR("Flash sector_erase failed on sector %u", sector);
1056 /* Switch to memory mapped mode before return to prompt */
1062 static int stmqspi_protect(struct flash_bank *bank, int set,
1063 unsigned int first, unsigned int last)
1065 unsigned int sector;
1067 for (sector = first; sector <= last; sector++)
1068 bank->sectors[sector].is_protected = set;
1071 LOG_WARNING("setting soft protection only, not related to flash's hardware write protection");
1076 /* Check whether flash is blank */
1077 static int stmqspi_blank_check(struct flash_bank *bank)
1079 struct target *target = bank->target;
1080 struct stmqspi_flash_bank *stmqspi_info = bank->driver_priv;
1081 struct duration bench;
1082 struct reg_param reg_params[2];
1083 struct armv7m_algorithm armv7m_info;
1084 struct working_area *algorithm;
1085 const uint8_t *code;
1086 struct sector_info erase_check_info;
1087 uint32_t codesize, maxsize, result, exit_point;
1088 unsigned int count, index, num_sectors, sector;
1090 const uint32_t erased = 0x00FF;
1092 if (target->state != TARGET_HALTED) {
1093 LOG_ERROR("Target not halted");
1094 return ERROR_TARGET_NOT_HALTED;
1097 if (!(stmqspi_info->probed)) {
1098 LOG_ERROR("Flash bank not probed");
1099 return ERROR_FLASH_BANK_NOT_PROBED;
1102 /* Abort any previous operation */
1103 retval = stmqspi_abort(bank);
1104 if (retval != ERROR_OK)
1107 /* Wait for busy to be cleared */
1108 retval = poll_busy(bank, SPI_PROBE_TIMEOUT);
1109 if (retval != ERROR_OK)
1112 /* see contrib/loaders/flash/stmqspi/stmqspi_erase_check.S for src */
1113 static const uint8_t stmqspi_erase_check_code[] = {
1114 #include "../../../contrib/loaders/flash/stmqspi/stmqspi_erase_check.inc"
1117 /* see contrib/loaders/flash/stmqspi/stmoctospi_erase_check.S for src */
1118 static const uint8_t stmoctospi_erase_check_code[] = {
1119 #include "../../../contrib/loaders/flash/stmqspi/stmoctospi_erase_check.inc"
1123 code = stmoctospi_erase_check_code;
1124 codesize = sizeof(stmoctospi_erase_check_code);
1126 code = stmqspi_erase_check_code;
1127 codesize = sizeof(stmqspi_erase_check_code);
1130 /* This will overlay the last 4 words of stmqspi/stmoctospi_erase_check_code in target */
1131 /* for read use the saved settings (memory mapped mode) but indirect read mode */
1132 uint32_t ccr_buffer[][4] = {
1133 /* cr (not used for QSPI) *
1134 * ccr (for both QSPI and OCTOSPI) *
1135 * tcr (not used for QSPI) *
1136 * ir (not used for QSPI) */
1138 h_to_le_32(OCTOSPI_MODE | OCTOSPI_READ_MODE),
1139 h_to_le_32(IS_OCTOSPI ? OCTOSPI_CCR_READ : QSPI_CCR_READ),
1140 h_to_le_32(stmqspi_info->saved_tcr),
1141 h_to_le_32(stmqspi_info->saved_ir),
1145 maxsize = target_get_working_area_avail(target);
1146 if (maxsize < codesize + sizeof(erase_check_info)) {
1147 LOG_ERROR("Not enough working area, can't do QSPI blank check");
1148 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1151 num_sectors = (maxsize - codesize) / sizeof(erase_check_info);
1152 num_sectors = (bank->num_sectors < num_sectors) ? bank->num_sectors : num_sectors;
1154 if (target_alloc_working_area_try(target,
1155 codesize + num_sectors * sizeof(erase_check_info), &algorithm) != ERROR_OK) {
1156 LOG_ERROR("allocating working area failed");
1157 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1160 /* prepare blank check code, excluding ccr_buffer */
1161 retval = target_write_buffer(target, algorithm->address,
1162 codesize - sizeof(ccr_buffer), code);
1163 if (retval != ERROR_OK)
1166 /* prepare QSPI/OCTOSPI_CCR register values */
1167 retval = target_write_buffer(target, algorithm->address
1168 + codesize - sizeof(ccr_buffer),
1169 sizeof(ccr_buffer), (uint8_t *)ccr_buffer);
1170 if (retval != ERROR_OK)
1173 duration_start(&bench);
1175 /* after breakpoint instruction (halfword), one nop (halfword) and
1176 * port_buffer till end of code */
1177 exit_point = algorithm->address + codesize - sizeof(uint32_t) - sizeof(ccr_buffer);
1179 init_reg_param(®_params[0], "r0", 32, PARAM_OUT); /* sector count */
1180 init_reg_param(®_params[1], "r1", 32, PARAM_OUT); /* QSPI/OCTOSPI io_base */
1183 while (sector < bank->num_sectors) {
1184 /* at most num_sectors sectors to handle in one run */
1185 count = bank->num_sectors - sector;
1186 if (count > num_sectors)
1187 count = num_sectors;
1189 for (index = 0; index < count; index++) {
1190 erase_check_info.offset = h_to_le_32(bank->sectors[sector + index].offset);
1191 erase_check_info.size = h_to_le_32(bank->sectors[sector + index].size);
1192 erase_check_info.result = h_to_le_32(erased);
1194 retval = target_write_buffer(target, algorithm->address
1195 + codesize + index * sizeof(erase_check_info),
1196 sizeof(erase_check_info), (uint8_t *)&erase_check_info);
1197 if (retval != ERROR_OK)
1201 buf_set_u32(reg_params[0].value, 0, 32, count);
1202 buf_set_u32(reg_params[1].value, 0, 32, stmqspi_info->io_base);
1204 armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
1205 armv7m_info.core_mode = ARM_MODE_THREAD;
1207 LOG_DEBUG("checking sectors %u to %u", sector, sector + count - 1);
1208 /* check a block of sectors */
1209 retval = target_run_algorithm(target,
1211 ARRAY_SIZE(reg_params), reg_params,
1212 algorithm->address, exit_point,
1213 count * ((bank->sectors[sector].size >> 6) + 1) + 1000,
1215 if (retval != ERROR_OK)
1218 for (index = 0; index < count; index++) {
1219 retval = target_read_buffer(target, algorithm->address
1220 + codesize + index * sizeof(erase_check_info),
1221 sizeof(erase_check_info), (uint8_t *)&erase_check_info);
1222 if (retval != ERROR_OK)
1225 if ((erase_check_info.offset != h_to_le_32(bank->sectors[sector + index].offset)) ||
1226 (erase_check_info.size != 0)) {
1227 LOG_ERROR("corrupted blank check info");
1231 /* we need le_32_to_h, but that's the same as h_to_le_32 */
1232 result = h_to_le_32(erase_check_info.result);
1233 bank->sectors[sector + index].is_erased = ((result & 0xFF) == 0xFF);
1234 LOG_DEBUG("Flash sector %u checked: 0x%04x", sector + index, result & 0xFFFFU);
1240 destroy_reg_param(®_params[0]);
1241 destroy_reg_param(®_params[1]);
1243 duration_measure(&bench);
1244 LOG_INFO("stmqspi blank checked in %fs (%0.3f KiB/s)", duration_elapsed(&bench),
1245 duration_kbps(&bench, bank->size));
1248 target_free_working_area(target, algorithm);
1250 /* Switch to memory mapped mode before return to prompt */
1256 /* Verify checksum */
1257 static int qspi_verify(struct flash_bank *bank, uint8_t *buffer,
1258 uint32_t offset, uint32_t count)
1260 struct target *target = bank->target;
1261 struct stmqspi_flash_bank *stmqspi_info = bank->driver_priv;
1262 struct reg_param reg_params[4];
1263 struct armv7m_algorithm armv7m_info;
1264 struct working_area *algorithm;
1265 const uint8_t *code;
1266 uint32_t pagesize, codesize, crc32, result, exit_point;
1269 /* see contrib/loaders/flash/stmqspi/stmqspi_crc32.S for src */
1270 static const uint8_t stmqspi_crc32_code[] = {
1271 #include "../../../contrib/loaders/flash/stmqspi/stmqspi_crc32.inc"
1274 /* see contrib/loaders/flash/stmqspi/stmoctospi_crc32.S for src */
1275 static const uint8_t stmoctospi_crc32_code[] = {
1276 #include "../../../contrib/loaders/flash/stmqspi/stmoctospi_crc32.inc"
1280 code = stmoctospi_crc32_code;
1281 codesize = sizeof(stmoctospi_crc32_code);
1283 code = stmqspi_crc32_code;
1284 codesize = sizeof(stmqspi_crc32_code);
1287 /* block size doesn't matter that much here */
1288 pagesize = stmqspi_info->dev.sectorsize;
1290 pagesize = stmqspi_info->dev.pagesize;
1292 pagesize = SPIFLASH_DEF_PAGESIZE;
1294 /* adjust size according to dual flash mode */
1295 pagesize = (stmqspi_info->saved_cr & BIT(SPI_DUAL_FLASH)) ? pagesize << 1 : pagesize;
1297 /* This will overlay the last 4 words of stmqspi/stmoctospi_crc32_code in target */
1298 /* for read use the saved settings (memory mapped mode) but indirect read mode */
1299 uint32_t ccr_buffer[][4] = {
1300 /* cr (not used for QSPI) *
1301 * ccr (for both QSPI and OCTOSPI) *
1302 * tcr (not used for QSPI) *
1303 * ir (not used for QSPI) */
1305 h_to_le_32(OCTOSPI_MODE | OCTOSPI_READ_MODE),
1306 h_to_le_32(IS_OCTOSPI ? OCTOSPI_CCR_READ : QSPI_CCR_READ),
1307 h_to_le_32(stmqspi_info->saved_tcr),
1308 h_to_le_32(stmqspi_info->saved_ir),
1312 if (target_alloc_working_area_try(target, codesize, &algorithm) != ERROR_OK) {
1313 LOG_ERROR("Not enough working area, can't do QSPI verify");
1314 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1317 /* prepare verify code, excluding ccr_buffer */
1318 retval = target_write_buffer(target, algorithm->address,
1319 codesize - sizeof(ccr_buffer), code);
1320 if (retval != ERROR_OK)
1323 /* prepare QSPI/OCTOSPI_CCR register values */
1324 retval = target_write_buffer(target, algorithm->address
1325 + codesize - sizeof(ccr_buffer),
1326 sizeof(ccr_buffer), (uint8_t *)ccr_buffer);
1327 if (retval != ERROR_OK)
1330 /* after breakpoint instruction (halfword), one nop (halfword) and
1331 * port_buffer till end of code */
1332 exit_point = algorithm->address + codesize - sizeof(uint32_t) - sizeof(ccr_buffer);
1334 init_reg_param(®_params[0], "r0", 32, PARAM_IN_OUT); /* count (in), crc32 (out) */
1335 init_reg_param(®_params[1], "r1", 32, PARAM_OUT); /* pagesize */
1336 init_reg_param(®_params[2], "r2", 32, PARAM_OUT); /* offset into flash address */
1337 init_reg_param(®_params[3], "r3", 32, PARAM_OUT); /* QSPI/OCTOSPI io_base */
1339 buf_set_u32(reg_params[0].value, 0, 32, count);
1340 buf_set_u32(reg_params[1].value, 0, 32, pagesize);
1341 buf_set_u32(reg_params[2].value, 0, 32, offset);
1342 buf_set_u32(reg_params[3].value, 0, 32, stmqspi_info->io_base);
1345 armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
1346 armv7m_info.core_mode = ARM_MODE_THREAD;
1348 retval = target_run_algorithm(target,
1350 ARRAY_SIZE(reg_params), reg_params,
1351 algorithm->address, exit_point,
1352 (count >> 5) + 1000,
1356 image_calculate_checksum(buffer, count, &crc32);
1358 if (retval == ERROR_OK) {
1359 result = buf_get_u32(reg_params[0].value, 0, 32);
1360 LOG_DEBUG("addr " TARGET_ADDR_FMT ", len 0x%08" PRIx32 ", crc 0x%08" PRIx32 " 0x%08" PRIx32,
1361 offset + bank->base, count, ~crc32, result);
1362 if (~crc32 != result)
1363 retval = ERROR_FAIL;
1366 destroy_reg_param(®_params[0]);
1367 destroy_reg_param(®_params[1]);
1368 destroy_reg_param(®_params[2]);
1369 destroy_reg_param(®_params[3]);
1372 target_free_working_area(target, algorithm);
1374 /* Switch to memory mapped mode before return to prompt */
1380 static int qspi_read_write_block(struct flash_bank *bank, uint8_t *buffer,
1381 uint32_t offset, uint32_t count, bool write)
1383 struct target *target = bank->target;
1384 struct stmqspi_flash_bank *stmqspi_info = bank->driver_priv;
1385 uint32_t io_base = stmqspi_info->io_base;
1386 struct reg_param reg_params[6];
1387 struct armv7m_algorithm armv7m_info;
1388 struct working_area *algorithm;
1389 uint32_t pagesize, fifo_start, fifosize, remaining;
1390 uint32_t maxsize, codesize, exit_point;
1391 const uint8_t *code = NULL;
1395 LOG_DEBUG("%s: offset=0x%08" PRIx32 " len=0x%08" PRIx32,
1396 __func__, offset, count);
1398 dual = (stmqspi_info->saved_cr & BIT(SPI_DUAL_FLASH)) ? 1 : 0;
1400 /* see contrib/loaders/flash/stmqspi/stmqspi_read.S for src */
1401 static const uint8_t stmqspi_read_code[] = {
1402 #include "../../../contrib/loaders/flash/stmqspi/stmqspi_read.inc"
1405 /* see contrib/loaders/flash/stmqspi/stmoctospi_read.S for src */
1406 static const uint8_t stmoctospi_read_code[] = {
1407 #include "../../../contrib/loaders/flash/stmqspi/stmoctospi_read.inc"
1410 /* see contrib/loaders/flash/stmqspi/stmqspi_write.S for src */
1411 static const uint8_t stmqspi_write_code[] = {
1412 #include "../../../contrib/loaders/flash/stmqspi/stmqspi_write.inc"
1415 /* see contrib/loaders/flash/stmqspi/stmoctospi_write.S for src */
1416 static const uint8_t stmoctospi_write_code[] = {
1417 #include "../../../contrib/loaders/flash/stmqspi/stmoctospi_write.inc"
1420 /* This will overlay the last 12 words of stmqspi/stmoctospi_read/write_code in target */
1421 /* for read use the saved settings (memory mapped mode) but indirect read mode */
1422 uint32_t ccr_buffer[][4] = {
1423 /* cr (not used for QSPI) *
1424 * ccr (for both QSPI and OCTOSPI) *
1425 * tcr (not used for QSPI) *
1426 * ir (not used for QSPI) */
1428 h_to_le_32(OCTOSPI_MODE | OCTOSPI_READ_MODE),
1429 h_to_le_32(IS_OCTOSPI ? OCTOSPI_CCR_READ_STATUS : QSPI_CCR_READ_STATUS),
1430 h_to_le_32((stmqspi_info->saved_tcr & ~OCTOSPI_DCYC_MASK) |
1431 (OPI_MODE ? (OPI_DUMMY << OCTOSPI_DCYC_POS) : 0)),
1432 h_to_le_32(OPI_CMD(SPIFLASH_READ_STATUS)),
1435 h_to_le_32(OCTOSPI_MODE | OCTOSPI_WRITE_MODE),
1436 h_to_le_32(IS_OCTOSPI ? OCTOSPI_CCR_WRITE_ENABLE : QSPI_CCR_WRITE_ENABLE),
1437 h_to_le_32(stmqspi_info->saved_tcr & ~OCTOSPI_DCYC_MASK),
1438 h_to_le_32(OPI_CMD(SPIFLASH_WRITE_ENABLE)),
1441 h_to_le_32(OCTOSPI_MODE | (write ? OCTOSPI_WRITE_MODE : OCTOSPI_READ_MODE)),
1442 h_to_le_32(write ? (IS_OCTOSPI ? OCTOSPI_CCR_PAGE_PROG : QSPI_CCR_PAGE_PROG) :
1443 (IS_OCTOSPI ? OCTOSPI_CCR_READ : QSPI_CCR_READ)),
1444 h_to_le_32(write ? (stmqspi_info->saved_tcr & ~OCTOSPI_DCYC_MASK) :
1445 stmqspi_info->saved_tcr),
1446 h_to_le_32(write ? OPI_CMD(stmqspi_info->dev.pprog_cmd) : stmqspi_info->saved_ir),
1450 /* force reasonable defaults */
1451 fifosize = stmqspi_info->dev.sectorsize ?
1452 stmqspi_info->dev.sectorsize : stmqspi_info->dev.size_in_bytes;
1456 code = stmoctospi_write_code;
1457 codesize = sizeof(stmoctospi_write_code);
1459 code = stmqspi_write_code;
1460 codesize = sizeof(stmqspi_write_code);
1464 code = stmoctospi_read_code;
1465 codesize = sizeof(stmoctospi_read_code);
1467 code = stmqspi_read_code;
1468 codesize = sizeof(stmqspi_read_code);
1472 /* for write, pagesize must be taken into account */
1473 /* for read, the page size doesn't matter that much */
1474 pagesize = stmqspi_info->dev.pagesize;
1476 pagesize = (fifosize <= SPIFLASH_DEF_PAGESIZE) ?
1477 fifosize : SPIFLASH_DEF_PAGESIZE;
1479 /* adjust sizes according to dual flash mode */
1483 /* memory buffer, we assume sectorsize to be a power of 2 times pagesize */
1484 maxsize = target_get_working_area_avail(target);
1485 if (maxsize < codesize + 2 * sizeof(uint32_t) + pagesize) {
1486 LOG_ERROR("not enough working area, can't do QSPI page reads/writes");
1487 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1490 /* fifo size at most sector size, and multiple of page size */
1491 maxsize -= (codesize + 2 * sizeof(uint32_t));
1492 fifosize = ((maxsize < fifosize) ? maxsize : fifosize) & ~(pagesize - 1);
1494 if (target_alloc_working_area_try(target,
1495 codesize + 2 * sizeof(uint32_t) + fifosize, &algorithm) != ERROR_OK) {
1496 LOG_ERROR("allocating working area failed");
1497 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1500 /* prepare flash write code, excluding ccr_buffer */
1501 retval = target_write_buffer(target, algorithm->address,
1502 codesize - sizeof(ccr_buffer), code);
1503 if (retval != ERROR_OK)
1506 /* prepare QSPI/OCTOSPI_CCR register values */
1507 retval = target_write_buffer(target, algorithm->address
1508 + codesize - sizeof(ccr_buffer),
1509 sizeof(ccr_buffer), (uint8_t *)ccr_buffer);
1510 if (retval != ERROR_OK)
1513 /* target buffer starts right after flash_write_code, i.e.
1514 * wp and rp are implicitly included in buffer!!! */
1515 fifo_start = algorithm->address + codesize + 2 * sizeof(uint32_t);
1517 init_reg_param(®_params[0], "r0", 32, PARAM_IN_OUT); /* count (in), status (out) */
1518 init_reg_param(®_params[1], "r1", 32, PARAM_OUT); /* pagesize */
1519 init_reg_param(®_params[2], "r2", 32, PARAM_IN_OUT); /* offset into flash address */
1520 init_reg_param(®_params[3], "r3", 32, PARAM_OUT); /* QSPI/OCTOSPI io_base */
1521 init_reg_param(®_params[4], "r8", 32, PARAM_OUT); /* fifo start */
1522 init_reg_param(®_params[5], "r9", 32, PARAM_OUT); /* fifo end + 1 */
1524 buf_set_u32(reg_params[0].value, 0, 32, count);
1525 buf_set_u32(reg_params[1].value, 0, 32, pagesize);
1526 buf_set_u32(reg_params[2].value, 0, 32, offset);
1527 buf_set_u32(reg_params[3].value, 0, 32, io_base);
1528 buf_set_u32(reg_params[4].value, 0, 32, fifo_start);
1529 buf_set_u32(reg_params[5].value, 0, 32, fifo_start + fifosize);
1531 armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
1532 armv7m_info.core_mode = ARM_MODE_THREAD;
1534 /* after breakpoint instruction (halfword), one nop (halfword) and
1535 * ccr_buffer follow till end of code */
1536 exit_point = algorithm->address + codesize
1537 - (sizeof(ccr_buffer) + sizeof(uint32_t));
1540 retval = target_run_flash_async_algorithm(target, buffer, count, 1,
1542 ARRAY_SIZE(reg_params), reg_params,
1543 algorithm->address + codesize,
1544 fifosize + 2 * sizeof(uint32_t),
1545 algorithm->address, exit_point,
1548 retval = target_run_read_async_algorithm(target, buffer, count, 1,
1550 ARRAY_SIZE(reg_params), reg_params,
1551 algorithm->address + codesize,
1552 fifosize + 2 * sizeof(uint32_t),
1553 algorithm->address, exit_point,
1557 remaining = buf_get_u32(reg_params[0].value, 0, 32);
1558 if ((retval == ERROR_OK) && remaining)
1559 retval = ERROR_FLASH_OPERATION_FAILED;
1561 if (retval != ERROR_OK) {
1562 offset = buf_get_u32(reg_params[2].value, 0, 32);
1563 LOG_ERROR("flash %s failed at address 0x%" PRIx32 ", remaining 0x%" PRIx32,
1564 write ? "write" : "read", offset, remaining);
1567 destroy_reg_param(®_params[0]);
1568 destroy_reg_param(®_params[1]);
1569 destroy_reg_param(®_params[2]);
1570 destroy_reg_param(®_params[3]);
1571 destroy_reg_param(®_params[4]);
1572 destroy_reg_param(®_params[5]);
1575 target_free_working_area(target, algorithm);
1577 /* Switch to memory mapped mode before return to prompt */
1583 static int stmqspi_read(struct flash_bank *bank, uint8_t *buffer,
1584 uint32_t offset, uint32_t count)
1586 struct target *target = bank->target;
1587 struct stmqspi_flash_bank *stmqspi_info = bank->driver_priv;
1590 LOG_DEBUG("%s: offset=0x%08" PRIx32 " count=0x%08" PRIx32,
1591 __func__, offset, count);
1593 if (target->state != TARGET_HALTED) {
1594 LOG_ERROR("Target not halted");
1595 return ERROR_TARGET_NOT_HALTED;
1598 if (!(stmqspi_info->probed)) {
1599 LOG_ERROR("Flash bank not probed");
1600 return ERROR_FLASH_BANK_NOT_PROBED;
1603 if (offset + count > bank->size) {
1604 LOG_WARNING("Read beyond end of flash. Extra data to be ignored.");
1605 count = bank->size - offset;
1608 /* Abort any previous operation */
1609 retval = stmqspi_abort(bank);
1610 if (retval != ERROR_OK)
1613 /* Wait for busy to be cleared */
1614 retval = poll_busy(bank, SPI_PROBE_TIMEOUT);
1615 if (retval != ERROR_OK)
1618 return qspi_read_write_block(bank, buffer, offset, count, false);
1621 static int stmqspi_write(struct flash_bank *bank, const uint8_t *buffer,
1622 uint32_t offset, uint32_t count)
1624 struct target *target = bank->target;
1625 struct stmqspi_flash_bank *stmqspi_info = bank->driver_priv;
1626 unsigned int dual, sector;
1630 LOG_DEBUG("%s: offset=0x%08" PRIx32 " count=0x%08" PRIx32,
1631 __func__, offset, count);
1633 dual = (stmqspi_info->saved_cr & BIT(SPI_DUAL_FLASH)) ? 1 : 0;
1634 octal_dtr = IS_OCTOSPI && (stmqspi_info->saved_ccr & BIT(OCTOSPI_DDTR));
1636 if (target->state != TARGET_HALTED) {
1637 LOG_ERROR("Target not halted");
1638 return ERROR_TARGET_NOT_HALTED;
1641 if (!(stmqspi_info->probed)) {
1642 LOG_ERROR("Flash bank not probed");
1643 return ERROR_FLASH_BANK_NOT_PROBED;
1646 if (offset + count > bank->size) {
1647 LOG_WARNING("Write beyond end of flash. Extra data discarded.");
1648 count = bank->size - offset;
1651 /* Check sector protection */
1652 for (sector = 0; sector < bank->num_sectors; sector++) {
1653 /* Start offset in or before this sector? */
1654 /* End offset in or behind this sector? */
1655 if ((offset < (bank->sectors[sector].offset + bank->sectors[sector].size)) &&
1656 ((offset + count - 1) >= bank->sectors[sector].offset) &&
1657 bank->sectors[sector].is_protected) {
1658 LOG_ERROR("Flash sector %u protected", sector);
1659 return ERROR_FLASH_PROTECTED;
1663 if ((dual || octal_dtr) && ((offset & 1) != 0 || (count & 1) != 0)) {
1664 LOG_ERROR("In dual-QSPI and octal-DTR modes writes must be two byte aligned: "
1665 "%s: address=0x%08" PRIx32 " len=0x%08" PRIx32, __func__, offset, count);
1666 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
1669 /* Abort any previous operation */
1670 retval = stmqspi_abort(bank);
1671 if (retval != ERROR_OK)
1674 /* Wait for busy to be cleared */
1675 retval = poll_busy(bank, SPI_PROBE_TIMEOUT);
1676 if (retval != ERROR_OK)
1679 return qspi_read_write_block(bank, (uint8_t *)buffer, offset, count, true);
1682 static int stmqspi_verify(struct flash_bank *bank, const uint8_t *buffer,
1683 uint32_t offset, uint32_t count)
1685 struct target *target = bank->target;
1686 struct stmqspi_flash_bank *stmqspi_info = bank->driver_priv;
1691 LOG_DEBUG("%s: offset=0x%08" PRIx32 " count=0x%08" PRIx32,
1692 __func__, offset, count);
1694 dual = (stmqspi_info->saved_cr & BIT(SPI_DUAL_FLASH)) ? 1 : 0;
1695 octal_dtr = IS_OCTOSPI && (stmqspi_info->saved_ccr & BIT(OCTOSPI_DDTR));
1697 if (target->state != TARGET_HALTED) {
1698 LOG_ERROR("Target not halted");
1699 return ERROR_TARGET_NOT_HALTED;
1702 if (!(stmqspi_info->probed)) {
1703 LOG_ERROR("Flash bank not probed");
1704 return ERROR_FLASH_BANK_NOT_PROBED;
1707 if (offset + count > bank->size) {
1708 LOG_WARNING("Verify beyond end of flash. Extra data ignored.");
1709 count = bank->size - offset;
1712 if ((dual || octal_dtr) && ((offset & 1) != 0 || (count & 1) != 0)) {
1713 LOG_ERROR("In dual-QSPI and octal-DTR modes reads must be two byte aligned: "
1714 "%s: address=0x%08" PRIx32 " len=0x%08" PRIx32, __func__, offset, count);
1715 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
1718 /* Abort any previous operation */
1719 retval = stmqspi_abort(bank);
1720 if (retval != ERROR_OK)
1723 /* Wait for busy to be cleared */
1724 retval = poll_busy(bank, SPI_PROBE_TIMEOUT);
1725 if (retval != ERROR_OK)
1728 return qspi_verify(bank, (uint8_t *)buffer, offset, count);
1731 /* Find appropriate dummy setting, in particular octo mode */
1732 static int find_sfdp_dummy(struct flash_bank *bank, int len)
1734 struct target *target = bank->target;
1735 struct stmqspi_flash_bank *stmqspi_info = bank->driver_priv;
1736 uint32_t io_base = stmqspi_info->io_base;
1738 unsigned int dual, count;
1739 bool flash1 = !(stmqspi_info->saved_cr & BIT(SPI_FSEL_FLASH));
1741 const unsigned int max_bytes = 64;
1743 dual = (stmqspi_info->saved_cr & BIT(SPI_DUAL_FLASH)) ? 1 : 0;
1745 LOG_DEBUG("%s: len=%d, dual=%u, flash1=%d",
1746 __func__, len, dual, flash1);
1748 /* Abort any previous operation */
1749 retval = target_write_u32(target, io_base + SPI_CR,
1750 stmqspi_info->saved_cr | BIT(SPI_ABORT));
1751 if (retval != ERROR_OK)
1754 /* Wait for busy to be cleared */
1755 retval = poll_busy(bank, SPI_PROBE_TIMEOUT);
1756 if (retval != ERROR_OK)
1759 /* Switch to saved_cr (had to be set accordingly before this call) */
1760 retval = target_write_u32(target, io_base + SPI_CR, stmqspi_info->saved_cr);
1761 if (retval != ERROR_OK)
1764 /* Read at most that many bytes */
1765 retval = target_write_u32(target, io_base + SPI_DLR, (max_bytes << dual) - 1);
1766 if (retval != ERROR_OK)
1769 /* Read SFDP block */
1771 retval = octospi_cmd(bank, OCTOSPI_READ_MODE,
1772 OCTOSPI_CCR_READ_SFDP(len), SPIFLASH_READ_SFDP);
1774 retval = target_write_u32(target, io_base + QSPI_CCR, QSPI_CCR_READ_SFDP);
1775 if (retval != ERROR_OK)
1778 /* Read from start of sfdp block */
1779 retval = target_write_u32(target, io_base + SPI_AR, 0);
1780 if (retval != ERROR_OK)
1783 for (count = 0 ; count < max_bytes; count++) {
1784 if ((dual != 0) && !flash1) {
1785 /* discard even byte in dual flash-mode if flash2 */
1786 retval = target_read_u8(target, io_base + SPI_DR, &data);
1787 if (retval != ERROR_OK)
1791 retval = target_read_u8(target, io_base + SPI_DR, &data);
1792 if (retval != ERROR_OK)
1796 LOG_DEBUG("start of SFDP header for flash%c after %u dummy bytes",
1797 flash1 ? '1' : '2', count);
1799 stmqspi_info->sfdp_dummy1 = count;
1801 stmqspi_info->sfdp_dummy2 = count;
1805 if ((dual != 0) && flash1) {
1806 /* discard odd byte in dual flash-mode if flash1 */
1807 retval = target_read_u8(target, io_base + SPI_DR, &data);
1808 if (retval != ERROR_OK)
1813 retval = ERROR_FAIL;
1814 LOG_DEBUG("no start of SFDP header even after %u dummy bytes", count);
1817 /* Abort operation */
1818 retval = stmqspi_abort(bank);
1823 /* Read SFDP parameter block */
1824 static int read_sfdp_block(struct flash_bank *bank, uint32_t addr,
1825 uint32_t words, uint32_t *buffer)
1827 struct target *target = bank->target;
1828 struct stmqspi_flash_bank *stmqspi_info = bank->driver_priv;
1829 uint32_t io_base = stmqspi_info->io_base;
1830 bool flash1 = !(stmqspi_info->saved_cr & BIT(SPI_FSEL_FLASH));
1831 unsigned int dual, count, len, *dummy;
1834 dual = (stmqspi_info->saved_cr & BIT(SPI_DUAL_FLASH)) ? 1 : 0;
1836 if (IS_OCTOSPI && (((stmqspi_info->saved_ccr >> SPI_DMODE_POS) & 0x7) > 3)) {
1837 /* in OCTO mode 4-byte address and (yet) unknown number of dummy clocks */
1840 /* in octo mode, use sfdp_dummy1 only */
1841 dummy = &stmqspi_info->sfdp_dummy1;
1843 retval = find_sfdp_dummy(bank, len);
1844 if (retval != ERROR_OK)
1848 /* in all other modes 3-byte-address and 8(?) dummy clocks */
1851 /* use sfdp_dummy1/2 according to currently selected flash */
1852 dummy = (stmqspi_info->saved_cr & BIT(SPI_FSEL_FLASH)) ?
1853 &stmqspi_info->sfdp_dummy2 : &stmqspi_info->sfdp_dummy1;
1855 /* according to SFDP standard, there should always be 8 dummy *CLOCKS*
1856 * giving 1, 2 or 4 dummy *BYTES*, however, this is apparently not
1857 * always implemented correctly, so determine the number of dummy bytes
1860 retval = find_sfdp_dummy(bank, len);
1861 if (retval != ERROR_OK)
1866 LOG_DEBUG("%s: addr=0x%08" PRIx32 " words=0x%08" PRIx32 " dummy=%u",
1867 __func__, addr, words, *dummy);
1869 /* Abort any previous operation */
1870 retval = target_write_u32(target, io_base + SPI_CR,
1871 stmqspi_info->saved_cr | BIT(SPI_ABORT));
1872 if (retval != ERROR_OK)
1875 /* Wait for busy to be cleared */
1876 retval = poll_busy(bank, SPI_PROBE_TIMEOUT);
1877 if (retval != ERROR_OK)
1880 /* Switch to one flash only */
1881 retval = target_write_u32(target, io_base + SPI_CR, stmqspi_info->saved_cr);
1882 if (retval != ERROR_OK)
1885 /* Read that many words plus dummy bytes */
1886 retval = target_write_u32(target, io_base + SPI_DLR,
1887 ((*dummy + words * sizeof(uint32_t)) << dual) - 1);
1888 if (retval != ERROR_OK)
1891 /* Read SFDP block */
1893 retval = octospi_cmd(bank, OCTOSPI_READ_MODE,
1894 OCTOSPI_CCR_READ_SFDP(len), SPIFLASH_READ_SFDP);
1896 retval = target_write_u32(target, io_base + QSPI_CCR, QSPI_CCR_READ_SFDP);
1897 if (retval != ERROR_OK)
1900 retval = target_write_u32(target, io_base + SPI_AR, addr << dual);
1901 if (retval != ERROR_OK)
1905 for (count = *dummy << dual; count > 0; --count) {
1906 retval = target_read_u8(target, io_base + SPI_DR, (uint8_t *)buffer);
1907 if (retval != ERROR_OK)
1911 for ( ; words > 0; words--) {
1913 uint32_t word1, word2;
1915 retval = target_read_u32(target, io_base + SPI_DR, &word1);
1916 if (retval != ERROR_OK)
1918 retval = target_read_u32(target, io_base + SPI_DR, &word2);
1919 if (retval != ERROR_OK)
1923 /* shift odd numbered bytes into even numbered ones */
1928 /* pack even numbered bytes into one word */
1929 *buffer = (word1 & 0xFFU) | ((word1 & 0xFF0000U) >> 8) |
1930 ((word2 & 0xFFU) << 16) | ((word2 & 0xFF0000U) << 8);
1934 retval = target_read_u32(target, io_base + SPI_DR, buffer);
1935 if (retval != ERROR_OK)
1938 LOG_DEBUG("raw SFDP data 0x%08" PRIx32, *buffer);
1940 /* endian correction, sfdp data is always le uint32_t based */
1941 *buffer = le_to_h_u32((uint8_t *)buffer);
1949 /* Return ID of flash device(s) */
1950 /* On exit, indirect mode is kept */
1951 static int read_flash_id(struct flash_bank *bank, uint32_t *id1, uint32_t *id2)
1953 struct target *target = bank->target;
1954 struct stmqspi_flash_bank *stmqspi_info = bank->driver_priv;
1955 uint32_t io_base = stmqspi_info->io_base;
1957 unsigned int type, count, len1, len2;
1958 int retval = ERROR_OK;
1960 /* invalidate both ids */
1964 if (target->state != TARGET_HALTED) {
1965 LOG_ERROR("Target not halted");
1966 return ERROR_TARGET_NOT_HALTED;
1969 /* SPIFLASH_READ_MID causes device in octal mode to go berserk, so don't use in this case */
1970 for (type = (IS_OCTOSPI && OPI_MODE) ? 1 : 0; type < 2 ; type++) {
1971 /* Abort any previous operation */
1972 retval = stmqspi_abort(bank);
1973 if (retval != ERROR_OK)
1977 retval = wait_till_ready(bank, SPI_PROBE_TIMEOUT);
1978 if (retval != ERROR_OK)
1981 /* Wait for busy to be cleared */
1982 retval = poll_busy(bank, SPI_PROBE_TIMEOUT);
1983 if (retval != ERROR_OK)
1986 /* Read at most 16 bytes per chip */
1988 retval = target_write_u32(target, io_base + SPI_DLR,
1989 (stmqspi_info->saved_cr & BIT(SPI_DUAL_FLASH) ? count * 2 : count) - 1);
1990 if (retval != ERROR_OK)
1993 /* Read id: one particular flash chip (N25Q128) switches back to SPI mode when receiving
1994 * SPI_FLASH_READ_ID in QPI mode, hence try SPIFLASH_READ_MID first */
1998 retval = octospi_cmd(bank, OCTOSPI_READ_MODE,
1999 OCTOSPI_CCR_READ_MID, SPIFLASH_READ_MID);
2001 retval = target_write_u32(target, io_base + QSPI_CCR, QSPI_CCR_READ_MID);
2006 retval = octospi_cmd(bank, OCTOSPI_READ_MODE,
2007 OCTOSPI_CCR_READ_ID, SPIFLASH_READ_ID);
2009 retval = target_write_u32(target, io_base + QSPI_CCR, QSPI_CCR_READ_ID);
2016 if (retval != ERROR_OK)
2019 /* Dummy address 0, only required for 8-line mode */
2020 if (IS_OCTOSPI && OPI_MODE) {
2021 retval = target_write_u32(target, io_base + SPI_AR, 0);
2022 if (retval != ERROR_OK)
2026 /* for debugging only */
2028 (void)target_read_u32(target, io_base + SPI_SR, &dummy);
2030 /* Read ID from Data Register */
2031 for (len1 = 0, len2 = 0; count > 0; --count) {
2032 if ((stmqspi_info->saved_cr & (BIT(SPI_DUAL_FLASH) |
2033 BIT(SPI_FSEL_FLASH))) != BIT(SPI_FSEL_FLASH)) {
2034 retval = target_read_u8(target, io_base + SPI_DR, &byte);
2035 if (retval != ERROR_OK)
2037 /* collect 3 bytes without continuation codes */
2038 if ((byte != 0x7F) && (len1 < 3)) {
2039 *id1 = (*id1 >> 8) | ((uint32_t)byte) << 16;
2043 if ((stmqspi_info->saved_cr & (BIT(SPI_DUAL_FLASH) |
2044 BIT(SPI_FSEL_FLASH))) != 0) {
2045 retval = target_read_u8(target, io_base + SPI_DR, &byte);
2046 if (retval != ERROR_OK)
2048 /* collect 3 bytes without continuation codes */
2049 if ((byte != 0x7F) && (len2 < 3)) {
2050 *id2 = (*id2 >> 8) | ((uint32_t)byte) << 16;
2056 if (((*id1 != 0x000000) && (*id1 != 0xFFFFFF)) ||
2057 ((*id2 != 0x000000) && (*id2 != 0xFFFFFF)))
2061 if ((stmqspi_info->saved_cr & (BIT(SPI_DUAL_FLASH) |
2062 BIT(SPI_FSEL_FLASH))) != BIT(SPI_FSEL_FLASH)) {
2063 if ((*id1 == 0x000000) || (*id1 == 0xFFFFFF)) {
2064 /* no id retrieved, so id must be set manually */
2065 LOG_INFO("No id from flash1");
2066 retval = ERROR_FLASH_BANK_NOT_PROBED;
2070 if ((stmqspi_info->saved_cr & (BIT(SPI_DUAL_FLASH) | BIT(SPI_FSEL_FLASH))) != 0) {
2071 if ((*id2 == 0x000000) || (*id2 == 0xFFFFFF)) {
2072 /* no id retrieved, so id must be set manually */
2073 LOG_INFO("No id from flash2");
2074 retval = ERROR_FLASH_BANK_NOT_PROBED;
2082 static int stmqspi_probe(struct flash_bank *bank)
2084 struct target *target = bank->target;
2085 struct stmqspi_flash_bank *stmqspi_info = bank->driver_priv;
2086 struct flash_sector *sectors = NULL;
2087 uint32_t io_base = stmqspi_info->io_base;
2088 uint32_t id1 = 0, id2 = 0, data = 0;
2089 const struct flash_device *p;
2090 const uint32_t magic = 0xAEF1510E;
2091 unsigned int dual, fsize;
2095 if (stmqspi_info->probed) {
2097 bank->num_sectors = 0;
2098 free(bank->sectors);
2099 bank->sectors = NULL;
2100 memset(&stmqspi_info->dev, 0, sizeof(stmqspi_info->dev));
2101 stmqspi_info->sfdp_dummy1 = 0;
2102 stmqspi_info->sfdp_dummy2 = 0;
2103 stmqspi_info->probed = false;
2106 /* Abort any previous operation */
2107 retval = stmqspi_abort(bank);
2108 if (retval != ERROR_OK)
2111 /* Wait for busy to be cleared */
2112 retval = poll_busy(bank, SPI_PROBE_TIMEOUT);
2113 if (retval != ERROR_OK)
2116 /* check whether QSPI_ABR is writeable and readback returns the value written */
2117 retval = target_write_u32(target, io_base + QSPI_ABR, magic);
2118 if (retval == ERROR_OK) {
2119 retval = target_read_u32(target, io_base + QSPI_ABR, &data);
2120 retval = target_write_u32(target, io_base + QSPI_ABR, 0);
2123 if (data == magic) {
2124 LOG_DEBUG("QSPI_ABR register present");
2125 stmqspi_info->octo = false;
2129 retval = target_read_u32(target, io_base + OCTOSPI_MAGIC, &magic_id);
2131 if (retval == ERROR_OK && magic_id == OCTO_MAGIC_ID) {
2132 LOG_DEBUG("OCTOSPI_MAGIC present");
2133 stmqspi_info->octo = true;
2135 LOG_ERROR("No QSPI, no OCTOSPI at 0x%08" PRIx32, io_base);
2136 stmqspi_info->probed = false;
2137 stmqspi_info->dev.name = "none";
2142 /* save current FSEL and DFM bits in QSPI/OCTOSPI_CR, current QSPI/OCTOSPI_CCR value */
2143 retval = target_read_u32(target, io_base + SPI_CR, &stmqspi_info->saved_cr);
2144 if (retval == ERROR_OK)
2145 retval = target_read_u32(target, io_base + SPI_CCR, &stmqspi_info->saved_ccr);
2150 retval = target_read_u32(target, io_base + OCTOSPI_DCR1, &dcr1);
2152 if (retval == ERROR_OK)
2153 retval = target_read_u32(target, io_base + OCTOSPI_TCR,
2154 &stmqspi_info->saved_tcr);
2156 if (retval == ERROR_OK)
2157 retval = target_read_u32(target, io_base + OCTOSPI_IR,
2158 &stmqspi_info->saved_ir);
2160 if (retval != ERROR_OK) {
2161 LOG_ERROR("No OCTOSPI at io_base 0x%08" PRIx32, io_base);
2162 stmqspi_info->probed = false;
2163 stmqspi_info->dev.name = "none";
2167 const uint32_t mtyp = (dcr1 & OCTOSPI_MTYP_MASK) >> OCTOSPI_MTYP_POS;
2169 if ((mtyp != 0x0) && (mtyp != 0x1)) {
2170 LOG_ERROR("Only regular SPI protocol supported in OCTOSPI");
2171 stmqspi_info->probed = false;
2172 stmqspi_info->dev.name = "none";
2176 LOG_DEBUG("OCTOSPI at 0x%08" PRIx64 ", io_base at 0x%08" PRIx32 ", OCTOSPI_CR 0x%08"
2177 PRIx32 ", OCTOSPI_CCR 0x%08" PRIx32 ", %d-byte addr", bank->base, io_base,
2178 stmqspi_info->saved_cr, stmqspi_info->saved_ccr, SPI_ADSIZE);
2180 if (retval == ERROR_OK) {
2181 LOG_DEBUG("QSPI at 0x%08" PRIx64 ", io_base at 0x%08" PRIx32 ", QSPI_CR 0x%08"
2182 PRIx32 ", QSPI_CCR 0x%08" PRIx32 ", %d-byte addr", bank->base, io_base,
2183 stmqspi_info->saved_cr, stmqspi_info->saved_ccr, SPI_ADSIZE);
2184 if (stmqspi_info->saved_ccr & (1U << QSPI_DDRM))
2185 LOG_WARNING("DDR mode is untested and suffers from some silicon bugs");
2187 LOG_ERROR("No QSPI at io_base 0x%08" PRIx32, io_base);
2188 stmqspi_info->probed = false;
2189 stmqspi_info->dev.name = "none";
2194 dual = (stmqspi_info->saved_cr & BIT(SPI_DUAL_FLASH)) ? 1 : 0;
2195 octal_dtr = IS_OCTOSPI && (stmqspi_info->saved_ccr & BIT(OCTOSPI_DDTR));
2196 if (dual || octal_dtr)
2197 bank->write_start_alignment = bank->write_end_alignment = 2;
2199 bank->write_start_alignment = bank->write_end_alignment = 1;
2201 /* read and decode flash ID; returns in indirect mode */
2202 retval = read_flash_id(bank, &id1, &id2);
2203 LOG_DEBUG("id1 0x%06" PRIx32 ", id2 0x%06" PRIx32, id1, id2);
2204 if (retval == ERROR_FLASH_BANK_NOT_PROBED) {
2205 /* no id retrieved, so id must be set manually */
2206 LOG_INFO("No id - set flash parameters manually");
2211 if (retval != ERROR_OK)
2214 /* identify flash1 */
2215 for (p = flash_devices; id1 && p->name ; p++) {
2216 if (p->device_id == id1) {
2217 memcpy(&stmqspi_info->dev, p, sizeof(stmqspi_info->dev));
2218 if (p->size_in_bytes / 4096)
2219 LOG_INFO("flash1 \'%s\' id = 0x%06" PRIx32 " size = %" PRIu32
2220 "kbytes", p->name, id1, p->size_in_bytes / 1024);
2222 LOG_INFO("flash1 \'%s\' id = 0x%06" PRIx32 " size = %" PRIu32
2223 "bytes", p->name, id1, p->size_in_bytes);
2228 if (id1 && !p->name) {
2229 /* chip not been identified by id, then try SFDP */
2230 struct flash_device temp;
2231 uint32_t saved_cr = stmqspi_info->saved_cr;
2234 stmqspi_info->saved_cr = stmqspi_info->saved_cr & ~BIT(SPI_FSEL_FLASH);
2235 retval = spi_sfdp(bank, &temp, &read_sfdp_block);
2237 /* restore saved_cr */
2238 stmqspi_info->saved_cr = saved_cr;
2240 if (retval == ERROR_OK) {
2241 LOG_INFO("flash1 \'%s\' id = 0x%06" PRIx32 " size = %" PRIu32
2242 "kbytes", temp.name, id1, temp.size_in_bytes / 1024);
2243 /* save info and retrieved *good* id as spi_sfdp clears all info */
2244 memcpy(&stmqspi_info->dev, &temp, sizeof(stmqspi_info->dev));
2245 stmqspi_info->dev.device_id = id1;
2247 /* even not identified by SFDP, then give up */
2248 LOG_WARNING("Unknown flash1 device id = 0x%06" PRIx32
2249 " - set flash parameters manually", id1);
2255 /* identify flash2 */
2256 for (p = flash_devices; id2 && p->name ; p++) {
2257 if (p->device_id == id2) {
2258 if (p->size_in_bytes / 4096)
2259 LOG_INFO("flash2 \'%s\' id = 0x%06" PRIx32 " size = %" PRIu32
2260 "kbytes", p->name, id2, p->size_in_bytes / 1024);
2262 LOG_INFO("flash2 \'%s\' id = 0x%06" PRIx32 " size = %" PRIu32
2263 "bytes", p->name, id2, p->size_in_bytes);
2266 memcpy(&stmqspi_info->dev, p, sizeof(stmqspi_info->dev));
2268 if ((stmqspi_info->dev.read_cmd != p->read_cmd) ||
2269 (stmqspi_info->dev.qread_cmd != p->qread_cmd) ||
2270 (stmqspi_info->dev.pprog_cmd != p->pprog_cmd) ||
2271 (stmqspi_info->dev.erase_cmd != p->erase_cmd) ||
2272 (stmqspi_info->dev.chip_erase_cmd != p->chip_erase_cmd) ||
2273 (stmqspi_info->dev.sectorsize != p->sectorsize) ||
2274 (stmqspi_info->dev.size_in_bytes != p->size_in_bytes)) {
2275 LOG_ERROR("Incompatible flash1/flash2 devices");
2278 /* page size is optional in SFDP, so accept smallest value */
2279 if (p->pagesize < stmqspi_info->dev.pagesize)
2280 stmqspi_info->dev.pagesize = p->pagesize;
2286 if (id2 && !p->name) {
2287 /* chip not been identified by id, then try SFDP */
2288 struct flash_device temp;
2289 uint32_t saved_cr = stmqspi_info->saved_cr;
2292 stmqspi_info->saved_cr = stmqspi_info->saved_cr | BIT(SPI_FSEL_FLASH);
2293 retval = spi_sfdp(bank, &temp, &read_sfdp_block);
2295 /* restore saved_cr */
2296 stmqspi_info->saved_cr = saved_cr;
2298 if (retval == ERROR_OK)
2299 LOG_INFO("flash2 \'%s\' id = 0x%06" PRIx32 " size = %" PRIu32
2300 "kbytes", temp.name, id2, temp.size_in_bytes / 1024);
2302 /* even not identified by SFDP, then give up */
2303 LOG_WARNING("Unknown flash2 device id = 0x%06" PRIx32
2304 " - set flash parameters manually", id2);
2310 memcpy(&stmqspi_info->dev, &temp, sizeof(stmqspi_info->dev));
2312 if ((stmqspi_info->dev.read_cmd != temp.read_cmd) ||
2313 (stmqspi_info->dev.qread_cmd != temp.qread_cmd) ||
2314 (stmqspi_info->dev.pprog_cmd != temp.pprog_cmd) ||
2315 (stmqspi_info->dev.erase_cmd != temp.erase_cmd) ||
2316 (stmqspi_info->dev.chip_erase_cmd != temp.chip_erase_cmd) ||
2317 (stmqspi_info->dev.sectorsize != temp.sectorsize) ||
2318 (stmqspi_info->dev.size_in_bytes != temp.size_in_bytes)) {
2319 LOG_ERROR("Incompatible flash1/flash2 devices");
2322 /* page size is optional in SFDP, so accept smallest value */
2323 if (temp.pagesize < stmqspi_info->dev.pagesize)
2324 stmqspi_info->dev.pagesize = temp.pagesize;
2328 /* Set correct size value */
2329 bank->size = stmqspi_info->dev.size_in_bytes << dual;
2332 retval = target_read_u32(target, io_base + SPI_DCR, &dcr);
2334 if (retval != ERROR_OK)
2337 fsize = (dcr >> SPI_FSIZE_POS) & (BIT(SPI_FSIZE_LEN) - 1);
2339 LOG_DEBUG("FSIZE = 0x%04x", fsize);
2340 if (bank->size == BIT((fsize + 1)))
2341 LOG_DEBUG("FSIZE in DCR(1) matches actual capacity. Beware of silicon bug in H7, L4+, MP1.");
2342 else if (bank->size == BIT((fsize + 0)))
2343 LOG_DEBUG("FSIZE in DCR(1) is off by one regarding actual capacity. Fix for silicon bug?");
2345 LOG_ERROR("FSIZE in DCR(1) doesn't match actual capacity.");
2347 /* if no sectors, then treat whole flash as single sector */
2348 if (stmqspi_info->dev.sectorsize == 0)
2349 stmqspi_info->dev.sectorsize = stmqspi_info->dev.size_in_bytes;
2350 /* if no page_size, then use sectorsize as page_size */
2351 if (stmqspi_info->dev.pagesize == 0)
2352 stmqspi_info->dev.pagesize = stmqspi_info->dev.sectorsize;
2354 /* create and fill sectors array */
2355 bank->num_sectors = stmqspi_info->dev.size_in_bytes / stmqspi_info->dev.sectorsize;
2356 sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
2358 LOG_ERROR("not enough memory");
2359 retval = ERROR_FAIL;
2363 for (unsigned int sector = 0; sector < bank->num_sectors; sector++) {
2364 sectors[sector].offset = sector * (stmqspi_info->dev.sectorsize << dual);
2365 sectors[sector].size = (stmqspi_info->dev.sectorsize << dual);
2366 sectors[sector].is_erased = -1;
2367 sectors[sector].is_protected = 0;
2370 bank->sectors = sectors;
2371 stmqspi_info->probed = true;
2374 /* Switch to memory mapped mode before return to prompt */
2380 static int stmqspi_auto_probe(struct flash_bank *bank)
2382 struct stmqspi_flash_bank *stmqspi_info = bank->driver_priv;
2384 if (stmqspi_info->probed)
2386 stmqspi_probe(bank);
2390 static int stmqspi_protect_check(struct flash_bank *bank)
2392 /* Nothing to do. Protection is only handled in SW. */
2396 static int get_stmqspi_info(struct flash_bank *bank, struct command_invocation *cmd)
2398 struct stmqspi_flash_bank *stmqspi_info = bank->driver_priv;
2400 if (!(stmqspi_info->probed)) {
2401 command_print_sameline(cmd, "\nQSPI flash bank not probed yet\n");
2402 return ERROR_FLASH_BANK_NOT_PROBED;
2405 command_print_sameline(cmd, "flash%s%s \'%s\', device id = 0x%06" PRIx32
2406 ", flash size = %" PRIu32 "%sbytes\n(page size = %" PRIu32
2407 ", read = 0x%02" PRIx8 ", qread = 0x%02" PRIx8
2408 ", pprog = 0x%02" PRIx8 ", mass_erase = 0x%02" PRIx8
2409 ", sector size = %" PRIu32 "%sbytes, sector_erase = 0x%02" PRIx8 ")",
2410 ((stmqspi_info->saved_cr & (BIT(SPI_DUAL_FLASH) |
2411 BIT(SPI_FSEL_FLASH))) != BIT(SPI_FSEL_FLASH)) ? "1" : "",
2412 ((stmqspi_info->saved_cr & (BIT(SPI_DUAL_FLASH) |
2413 BIT(SPI_FSEL_FLASH))) != 0) ? "2" : "",
2414 stmqspi_info->dev.name, stmqspi_info->dev.device_id,
2415 bank->size / 4096 ? bank->size / 1024 : bank->size,
2416 bank->size / 4096 ? "k" : "", stmqspi_info->dev.pagesize,
2417 stmqspi_info->dev.read_cmd, stmqspi_info->dev.qread_cmd,
2418 stmqspi_info->dev.pprog_cmd, stmqspi_info->dev.chip_erase_cmd,
2419 stmqspi_info->dev.sectorsize / 4096 ?
2420 stmqspi_info->dev.sectorsize / 1024 : stmqspi_info->dev.sectorsize,
2421 stmqspi_info->dev.sectorsize / 4096 ? "k" : "",
2422 stmqspi_info->dev.erase_cmd);
2427 static const struct command_registration stmqspi_exec_command_handlers[] = {
2429 .name = "mass_erase",
2430 .handler = stmqspi_handle_mass_erase_command,
2431 .mode = COMMAND_EXEC,
2433 .help = "Mass erase entire flash device.",
2437 .handler = stmqspi_handle_set,
2438 .mode = COMMAND_EXEC,
2439 .usage = "bank_id name chip_size page_size read_cmd qread_cmd pprg_cmd "
2440 "[ mass_erase_cmd ] [ sector_size sector_erase_cmd ]",
2441 .help = "Set params of single flash chip",
2445 .handler = stmqspi_handle_cmd,
2446 .mode = COMMAND_EXEC,
2447 .usage = "bank_id num_resp cmd_byte ...",
2448 .help = "Send low-level command cmd_byte and following bytes or read num_resp.",
2450 COMMAND_REGISTRATION_DONE
2453 static const struct command_registration stmqspi_command_handlers[] = {
2456 .mode = COMMAND_ANY,
2457 .help = "stmqspi flash command group",
2459 .chain = stmqspi_exec_command_handlers,
2461 COMMAND_REGISTRATION_DONE
2464 struct flash_driver stmqspi_flash = {
2466 .commands = stmqspi_command_handlers,
2467 .flash_bank_command = stmqspi_flash_bank_command,
2468 .erase = stmqspi_erase,
2469 .protect = stmqspi_protect,
2470 .write = stmqspi_write,
2471 .read = stmqspi_read,
2472 .verify = stmqspi_verify,
2473 .probe = stmqspi_probe,
2474 .auto_probe = stmqspi_auto_probe,
2475 .erase_check = stmqspi_blank_check,
2476 .protect_check = stmqspi_protect_check,
2477 .info = get_stmqspi_info,
2478 .free_driver_priv = default_flash_free_driver_priv,