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/bits.h>
44 #include <helper/time_support.h>
45 #include <target/algorithm.h>
46 #include <target/armv7m.h>
47 #include <target/image.h>
53 #undef SPIFLASH_PAGE_PROGRAM
59 retval = target_read_u32(target, io_base + (a), &_result); \
60 (retval == ERROR_OK) ? _result : 0x0; \
63 /* saved mode settings */
64 #define QSPI_MODE (stmqspi_info->saved_ccr & \
65 (0xF0000000U | QSPI_DCYC_MASK | QSPI_4LINE_MODE | QSPI_ALTB_MODE | QSPI_ADDR4))
67 /* saved read mode settings but indirect read instead of memory mapped
68 * in particular, use the dummy cycle setting from this saved setting */
69 #define QSPI_CCR_READ (QSPI_READ_MODE | (stmqspi_info->saved_ccr & \
70 (0xF0000000U | QSPI_DCYC_MASK | QSPI_4LINE_MODE | QSPI_ALTB_MODE | QSPI_ADDR4 | 0xFF)))
72 /* QSPI_CCR for various other commands, these never use dummy cycles nor alternate bytes */
73 #define QSPI_CCR_READ_STATUS \
74 ((QSPI_MODE & ~QSPI_DCYC_MASK & QSPI_NO_ADDR & QSPI_NO_ALTB) | \
75 (QSPI_READ_MODE | SPIFLASH_READ_STATUS))
77 #define QSPI_CCR_READ_ID \
78 ((QSPI_MODE & ~QSPI_DCYC_MASK & QSPI_NO_ADDR & QSPI_NO_ALTB) | \
79 (QSPI_READ_MODE | SPIFLASH_READ_ID))
81 #define QSPI_CCR_READ_MID \
82 ((QSPI_MODE & ~QSPI_DCYC_MASK & QSPI_NO_ADDR & QSPI_NO_ALTB) | \
83 (QSPI_READ_MODE | SPIFLASH_READ_MID))
85 /* always use 3-byte addresses for read SFDP */
86 #define QSPI_CCR_READ_SFDP \
87 ((QSPI_MODE & ~QSPI_DCYC_MASK & ~QSPI_ADDR4 & QSPI_NO_ALTB) | \
88 (QSPI_READ_MODE | QSPI_ADDR3 | SPIFLASH_READ_SFDP))
90 #define QSPI_CCR_WRITE_ENABLE \
91 ((QSPI_MODE & ~QSPI_DCYC_MASK & QSPI_NO_ADDR & QSPI_NO_ALTB & QSPI_NO_DATA) | \
92 (QSPI_WRITE_MODE | SPIFLASH_WRITE_ENABLE))
94 #define QSPI_CCR_SECTOR_ERASE \
95 ((QSPI_MODE & ~QSPI_DCYC_MASK & QSPI_NO_ALTB & QSPI_NO_DATA) | \
96 (QSPI_WRITE_MODE | stmqspi_info->dev.erase_cmd))
98 #define QSPI_CCR_MASS_ERASE \
99 ((QSPI_MODE & ~QSPI_DCYC_MASK & QSPI_NO_ADDR & QSPI_NO_ALTB & QSPI_NO_DATA) | \
100 (QSPI_WRITE_MODE | stmqspi_info->dev.chip_erase_cmd))
102 #define QSPI_CCR_PAGE_PROG \
103 ((QSPI_MODE & ~QSPI_DCYC_MASK & QSPI_NO_ALTB) | \
104 (QSPI_WRITE_MODE | stmqspi_info->dev.pprog_cmd))
106 /* saved mode settings */
107 #define OCTOSPI_MODE (stmqspi_info->saved_cr & 0xCFFFFFFF)
109 #define OPI_MODE ((stmqspi_info->saved_ccr & OCTOSPI_ISIZE_MASK) != 0)
111 #define OCTOSPI_MODE_CCR (stmqspi_info->saved_ccr & \
112 (0xF0000000U | OCTOSPI_8LINE_MODE | OCTOSPI_ALTB_MODE | OCTOSPI_ADDR4))
114 /* use saved ccr for read */
115 #define OCTOSPI_CCR_READ OCTOSPI_MODE_CCR
117 /* OCTOSPI_CCR for various other commands, these never use alternate bytes *
118 * for READ_STATUS and READ_ID, 4-byte address 0 *
119 * 4 dummy cycles must sent in OPI mode when DQS is disabled. However, when *
120 * DQS is enabled, some STM32 devices need at least 6 dummy cycles for *
121 * proper operation, but otherwise the actual number has no effect! *
122 * E.g. RM0432 Rev. 7 is incorrect regarding this: L4R9 works well with 4 *
123 * dummy clocks whereas L4P5 not at all. *
126 ((stmqspi_info->saved_ccr & OCTOSPI_DQSEN) ? 6U : 4U)
128 #define OCTOSPI_CCR_READ_STATUS \
129 ((OCTOSPI_MODE_CCR & OCTOSPI_NO_DDTR & \
130 (OPI_MODE ? ~0U : OCTOSPI_NO_ADDR) & OCTOSPI_NO_ALTB))
132 #define OCTOSPI_CCR_READ_ID \
133 ((OCTOSPI_MODE_CCR & OCTOSPI_NO_DDTR & \
134 (OPI_MODE ? ~0U : OCTOSPI_NO_ADDR) & OCTOSPI_NO_ALTB))
136 #define OCTOSPI_CCR_READ_MID OCTOSPI_CCR_READ_ID
138 /* 4-byte address in octo mode, else 3-byte address for read SFDP */
139 #define OCTOSPI_CCR_READ_SFDP(len) \
140 ((OCTOSPI_MODE_CCR & OCTOSPI_NO_DDTR & ~OCTOSPI_ADDR4 & OCTOSPI_NO_ALTB) | \
141 (((len) < 4) ? OCTOSPI_ADDR3 : OCTOSPI_ADDR4))
143 #define OCTOSPI_CCR_WRITE_ENABLE \
144 ((OCTOSPI_MODE_CCR & OCTOSPI_NO_ADDR & OCTOSPI_NO_ALTB & OCTOSPI_NO_DATA))
146 #define OCTOSPI_CCR_SECTOR_ERASE \
147 ((OCTOSPI_MODE_CCR & OCTOSPI_NO_ALTB & OCTOSPI_NO_DATA))
149 #define OCTOSPI_CCR_MASS_ERASE \
150 ((OCTOSPI_MODE_CCR & OCTOSPI_NO_ADDR & OCTOSPI_NO_ALTB & OCTOSPI_NO_DATA))
152 #define OCTOSPI_CCR_PAGE_PROG \
153 ((OCTOSPI_MODE_CCR & QSPI_NO_ALTB))
155 #define SPI_ADSIZE (((stmqspi_info->saved_ccr >> SPI_ADSIZE_POS) & 0x3) + 1)
157 #define OPI_CMD(cmd) ((OPI_MODE ? ((((uint16_t)(cmd)) << 8) | (~(cmd) & 0xFFU)) : (cmd)))
159 #define OCTOSPI_CMD(mode, ccr, ir) \
161 retval = target_write_u32(target, io_base + OCTOSPI_CR, \
162 OCTOSPI_MODE | (mode)); \
163 if (retval == ERROR_OK) \
164 retval = target_write_u32(target, io_base + OCTOSPI_TCR, \
165 (stmqspi_info->saved_tcr & ~OCTOSPI_DCYC_MASK) | \
166 ((OPI_MODE && ((mode) == OCTOSPI_READ_MODE)) ? \
167 (OPI_DUMMY << OCTOSPI_DCYC_POS) : 0)); \
168 if (retval == ERROR_OK) \
169 retval = target_write_u32(target, io_base + OCTOSPI_CCR, ccr); \
170 if (retval == ERROR_OK) \
171 retval = target_write_u32(target, io_base + OCTOSPI_IR, \
176 /* convert uint32_t into 4 uint8_t in little endian byte order */
177 static inline uint32_t h_to_le_32(uint32_t val)
181 h_u32_to_le((uint8_t *)&result, val);
186 #define SPI_CMD_TIMEOUT (100)
187 #define SPI_PROBE_TIMEOUT (100)
188 #define SPI_MAX_TIMEOUT (2000)
189 #define SPI_MASS_ERASE_TIMEOUT (400000)
197 struct stmqspi_flash_bank {
201 struct flash_device dev;
203 uint32_t saved_cr; /* in particalar FSEL, DFM bit mask in QUADSPI_CR *AND* OCTOSPI_CR */
204 uint32_t saved_ccr; /* different meaning for QUADSPI and OCTOSPI */
205 uint32_t saved_tcr; /* only for OCTOSPI */
206 uint32_t saved_ir; /* only for OCTOSPI */
207 unsigned int sfdp_dummy1; /* number of dummy bytes for SFDP read for flash1 and octo */
208 unsigned int sfdp_dummy2; /* number of dummy bytes for SFDP read for flash2 */
211 FLASH_BANK_COMMAND_HANDLER(stmqspi_flash_bank_command)
213 struct stmqspi_flash_bank *stmqspi_info;
216 LOG_DEBUG("%s", __func__);
219 return ERROR_COMMAND_SYNTAX_ERROR;
221 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[6], io_base);
223 stmqspi_info = malloc(sizeof(struct stmqspi_flash_bank));
224 if (stmqspi_info == NULL) {
225 LOG_ERROR("not enough memory");
229 bank->driver_priv = stmqspi_info;
230 stmqspi_info->sfdp_dummy1 = 0;
231 stmqspi_info->sfdp_dummy2 = 0;
232 stmqspi_info->probed = false;
233 stmqspi_info->io_base = io_base;
240 static int poll_busy(struct flash_bank *bank, int timeout)
242 struct target *target = bank->target;
243 struct stmqspi_flash_bank *stmqspi_info = bank->driver_priv;
244 uint32_t io_base = stmqspi_info->io_base;
249 endtime = timeval_ms() + timeout;
251 spi_sr = READ_REG(SPI_SR);
252 if ((spi_sr & BIT(SPI_BUSY)) == 0) {
253 if (retval == ERROR_OK) {
254 /* Clear transmit finished flag */
255 retval = target_write_u32(target, io_base + SPI_FCR, BIT(SPI_TCF));
259 LOG_DEBUG("busy: 0x%08X", spi_sr);
261 } while (timeval_ms() < endtime);
263 LOG_ERROR("Timeout while polling BUSY");
264 return ERROR_FLASH_OPERATION_FAILED;
267 /* Set to memory-mapped mode, e.g. after an error */
268 static int set_mm_mode(struct flash_bank *bank)
270 struct target *target = bank->target;
271 struct stmqspi_flash_bank *stmqspi_info = bank->driver_priv;
272 uint32_t io_base = stmqspi_info->io_base;
275 /* Reset Address register bits 0 and 1, see various errata sheets */
276 retval = target_write_u32(target, io_base + SPI_AR, 0x0);
277 if (retval != ERROR_OK)
280 /* Abort any previous operation */
281 retval = target_write_u32(target, io_base + SPI_CR,
282 READ_REG(SPI_CR) | BIT(SPI_ABORT));
283 if (retval != ERROR_OK)
286 /* Wait for busy to be cleared */
287 retval = poll_busy(bank, SPI_PROBE_TIMEOUT);
288 if (retval != ERROR_OK)
291 /* Finally switch to memory mapped mode */
293 retval = target_write_u32(target, io_base + OCTOSPI_CR,
294 OCTOSPI_MODE | OCTOSPI_MM_MODE);
295 if (retval == ERROR_OK)
296 retval = target_write_u32(target, io_base + OCTOSPI_CCR,
297 stmqspi_info->saved_ccr);
298 if (retval == ERROR_OK)
299 retval = target_write_u32(target, io_base + OCTOSPI_TCR,
300 stmqspi_info->saved_tcr);
301 if (retval == ERROR_OK)
302 retval = target_write_u32(target, io_base + OCTOSPI_IR,
303 stmqspi_info->saved_ir);
305 retval = target_write_u32(target, io_base + QSPI_CR,
306 stmqspi_info->saved_cr);
307 if (retval == ERROR_OK)
308 retval = target_write_u32(target, io_base + QSPI_CCR,
309 stmqspi_info->saved_ccr);
314 /* Read the status register of the external SPI flash chip(s). */
315 static int read_status_reg(struct flash_bank *bank, uint16_t *status)
317 struct target *target = bank->target;
318 struct stmqspi_flash_bank *stmqspi_info = bank->driver_priv;
319 uint32_t io_base = stmqspi_info->io_base;
323 /* Abort any previous operation */
324 retval = target_write_u32(target, io_base + SPI_CR,
325 READ_REG(SPI_CR) | BIT(SPI_ABORT));
326 if (retval != ERROR_OK)
329 /* Wait for busy to be cleared */
330 retval = poll_busy(bank, SPI_PROBE_TIMEOUT);
331 if (retval != ERROR_OK)
334 /* Read always two (for DTR mode) bytes per chip */
336 retval = target_write_u32(target, io_base + SPI_DLR,
337 ((stmqspi_info->saved_cr & BIT(SPI_DUAL_FLASH)) ? 2 * count : count) - 1);
338 if (retval != ERROR_OK)
343 retval = OCTOSPI_CMD(OCTOSPI_READ_MODE, OCTOSPI_CCR_READ_STATUS, SPIFLASH_READ_STATUS);
345 /* Dummy address 0, only required for 8-line mode */
346 retval = target_write_u32(target, io_base + SPI_AR, 0);
347 if (retval != ERROR_OK)
351 retval = target_write_u32(target, io_base + QSPI_CCR, QSPI_CCR_READ_STATUS);
352 if (retval != ERROR_OK)
357 /* for debugging only */
358 (void)READ_REG(SPI_SR);
360 for ( ; count > 0; --count) {
361 if ((stmqspi_info->saved_cr & (BIT(SPI_DUAL_FLASH) | BIT(SPI_FSEL_FLASH)))
362 != BIT(SPI_FSEL_FLASH)) {
363 /* get status of flash 1 in dual mode or flash 1 only mode */
364 retval = target_read_u8(target, io_base + SPI_DR, &data);
365 if (retval != ERROR_OK)
370 if ((stmqspi_info->saved_cr & (BIT(SPI_DUAL_FLASH) | BIT(SPI_FSEL_FLASH))) != 0) {
371 /* get status of flash 2 in dual mode or flash 2 only mode */
372 retval = target_read_u8(target, io_base + SPI_DR, &data);
373 if (retval != ERROR_OK)
375 *status |= ((uint16_t)data) << 8;
379 LOG_DEBUG("flash status regs: 0x%04" PRIx16, *status);
385 /* check for WIP (write in progress) bit(s) in status register(s) */
387 static int wait_till_ready(struct flash_bank *bank, int timeout)
393 endtime = timeval_ms() + timeout;
395 /* Read flash status register(s) */
396 retval = read_status_reg(bank, &status);
397 if (retval != ERROR_OK)
400 if ((status & ((SPIFLASH_BSY_BIT << 8) | SPIFLASH_BSY_BIT)) == 0)
403 } while (timeval_ms() < endtime);
405 LOG_ERROR("timeout");
406 return ERROR_FLASH_OPERATION_FAILED;
409 /* Send "write enable" command to SPI flash chip(s). */
410 static int qspi_write_enable(struct flash_bank *bank)
412 struct target *target = bank->target;
413 struct stmqspi_flash_bank *stmqspi_info = bank->driver_priv;
414 uint32_t io_base = stmqspi_info->io_base;
418 /* Abort any previous operation */
419 retval = target_write_u32(target, io_base + SPI_CR,
420 READ_REG(SPI_CR) | BIT(SPI_ABORT));
421 if (retval != ERROR_OK)
424 /* Wait for busy to be cleared */
425 retval = poll_busy(bank, SPI_PROBE_TIMEOUT);
426 if (retval != ERROR_OK)
429 /* Send write enable command */
431 retval = OCTOSPI_CMD(OCTOSPI_WRITE_MODE, OCTOSPI_CCR_WRITE_ENABLE, SPIFLASH_WRITE_ENABLE);
433 /* Dummy address 0, only required for 8-line mode */
434 retval = target_write_u32(target, io_base + SPI_AR, 0);
435 if (retval != ERROR_OK)
439 retval = target_write_u32(target, io_base + QSPI_CCR, QSPI_CCR_WRITE_ENABLE);
440 if (retval != ERROR_OK)
444 /* Wait for transmit of command completed */
445 poll_busy(bank, SPI_CMD_TIMEOUT);
446 if (retval != ERROR_OK)
449 /* Read flash status register */
450 retval = read_status_reg(bank, &status);
451 if (retval != ERROR_OK)
454 /* Check write enabled for flash 1 */
455 if ((stmqspi_info->saved_cr & (BIT(SPI_DUAL_FLASH) | BIT(SPI_FSEL_FLASH)))
456 != BIT(SPI_FSEL_FLASH))
457 if ((status & (SPIFLASH_WE_BIT | SPIFLASH_BSY_BIT)) != SPIFLASH_WE_BIT) {
458 LOG_ERROR("Cannot write enable flash1. Status=0x%02x",
460 return ERROR_FLASH_OPERATION_FAILED;
463 /* Check write enabled for flash 2 */
465 if ((stmqspi_info->saved_cr & (BIT(SPI_DUAL_FLASH) | BIT(SPI_FSEL_FLASH))) != 0)
466 if ((status & (SPIFLASH_WE_BIT | SPIFLASH_BSY_BIT)) != SPIFLASH_WE_BIT) {
467 LOG_ERROR("Cannot write enable flash2. Status=0x%02x",
469 return ERROR_FLASH_OPERATION_FAILED;
476 COMMAND_HANDLER(stmqspi_handle_mass_erase_command)
478 struct target *target = NULL;
479 struct flash_bank *bank;
480 struct stmqspi_flash_bank *stmqspi_info;
481 struct duration bench;
487 LOG_DEBUG("%s", __func__);
490 return ERROR_COMMAND_SYNTAX_ERROR;
492 retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
493 if (ERROR_OK != retval)
496 stmqspi_info = bank->driver_priv;
497 target = bank->target;
499 if (target->state != TARGET_HALTED) {
500 LOG_ERROR("Target not halted");
501 return ERROR_TARGET_NOT_HALTED;
504 if (!(stmqspi_info->probed)) {
505 LOG_ERROR("Flash bank not probed");
506 return ERROR_FLASH_BANK_NOT_PROBED;
509 if (stmqspi_info->dev.chip_erase_cmd == 0x00) {
510 LOG_ERROR("Mass erase not available for this device");
511 return ERROR_FLASH_OPER_UNSUPPORTED;
514 for (sector = 0; sector < bank->num_sectors; sector++) {
515 if (bank->sectors[sector].is_protected) {
516 LOG_ERROR("Flash sector %u protected", sector);
517 return ERROR_FLASH_PROTECTED;
521 io_base = stmqspi_info->io_base;
522 duration_start(&bench);
524 retval = qspi_write_enable(bank);
525 if (retval != ERROR_OK)
528 /* Send Mass Erase command */
530 retval = OCTOSPI_CMD(OCTOSPI_WRITE_MODE, OCTOSPI_CCR_MASS_ERASE,
531 stmqspi_info->dev.chip_erase_cmd);
533 retval = target_write_u32(target, io_base + QSPI_CCR, QSPI_CCR_MASS_ERASE);
534 if (retval != ERROR_OK)
537 /* Wait for transmit of command completed */
538 poll_busy(bank, SPI_CMD_TIMEOUT);
539 if (retval != ERROR_OK)
542 /* Read flash status register(s) */
543 retval = read_status_reg(bank, &status);
544 if (retval != ERROR_OK)
547 /* Check for command in progress for flash 1 */
548 if (((stmqspi_info->saved_cr & (BIT(SPI_DUAL_FLASH) | BIT(SPI_FSEL_FLASH)))
549 != BIT(SPI_FSEL_FLASH)) && ((status & SPIFLASH_BSY_BIT) == 0) &&
550 ((status & SPIFLASH_WE_BIT) != 0)) {
551 LOG_ERROR("Mass erase command not accepted by flash1. Status=0x%02x",
553 retval = ERROR_FLASH_OPERATION_FAILED;
557 /* Check for command in progress for flash 2 */
559 if (((stmqspi_info->saved_cr & (BIT(SPI_DUAL_FLASH) | BIT(SPI_FSEL_FLASH))) != 0) &&
560 ((status & SPIFLASH_BSY_BIT) == 0) &&
561 ((status & SPIFLASH_WE_BIT) != 0)) {
562 LOG_ERROR("Mass erase command not accepted by flash2. Status=0x%02x",
564 retval = ERROR_FLASH_OPERATION_FAILED;
568 /* Poll WIP for end of self timed Sector Erase cycle */
569 retval = wait_till_ready(bank, SPI_MASS_ERASE_TIMEOUT);
571 duration_measure(&bench);
572 if (retval == ERROR_OK) {
573 /* set all sectors as erased */
574 for (sector = 0; sector < bank->num_sectors; sector++)
575 bank->sectors[sector].is_erased = 1;
577 command_print(CMD, "stmqspi mass erase completed in %fs (%0.3f KiB/s)",
578 duration_elapsed(&bench),
579 duration_kbps(&bench, bank->size));
581 command_print(CMD, "stmqspi mass erase not completed even after %fs",
582 duration_elapsed(&bench));
586 /* Switch to memory mapped mode before return to prompt */
592 static int log2u(uint32_t word)
596 for (result = 0; (unsigned int) result < sizeof(uint32_t) * CHAR_BIT; result++)
597 if (word == BIT(result))
603 COMMAND_HANDLER(stmqspi_handle_set)
605 struct flash_bank *bank = NULL;
606 struct target *target = NULL;
607 struct stmqspi_flash_bank *stmqspi_info = NULL;
608 struct flash_sector *sectors = NULL;
610 unsigned int index = 0, dual, fsize;
613 LOG_DEBUG("%s", __func__);
615 dual = (stmqspi_info->saved_cr & BIT(SPI_DUAL_FLASH)) ? 1 : 0;
617 /* chip_erase_cmd, sectorsize and erase_cmd are optional */
618 if ((CMD_ARGC < 7) || (CMD_ARGC > 10))
619 return ERROR_COMMAND_SYNTAX_ERROR;
621 retval = CALL_COMMAND_HANDLER(flash_command_get_bank, index++, &bank);
622 if (ERROR_OK != retval)
625 target = bank->target;
626 stmqspi_info = bank->driver_priv;
628 /* invalidate all old info */
629 if (stmqspi_info->probed)
632 bank->num_sectors = 0;
633 bank->sectors = NULL;
634 stmqspi_info->sfdp_dummy1 = 0;
635 stmqspi_info->sfdp_dummy2 = 0;
636 stmqspi_info->probed = false;
637 memset(&stmqspi_info->dev, 0, sizeof(stmqspi_info->dev));
638 stmqspi_info->dev.name = "unknown";
640 strncpy(stmqspi_info->devname, CMD_ARGV[index++], sizeof(stmqspi_info->devname) - 1);
641 stmqspi_info->devname[sizeof(stmqspi_info->devname) - 1] = '\0';
643 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[index++], stmqspi_info->dev.size_in_bytes);
644 if (log2u(stmqspi_info->dev.size_in_bytes) < 8) {
645 command_print(CMD, "stmqspi: device size must be 2^n with n >= 8");
646 return ERROR_COMMAND_SYNTAX_ERROR;
649 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[index++], stmqspi_info->dev.pagesize);
650 if (stmqspi_info->dev.pagesize > stmqspi_info->dev.size_in_bytes ||
651 (log2u(stmqspi_info->dev.pagesize) < 0)) {
652 command_print(CMD, "stmqspi: page size must be 2^n and <= device size");
653 return ERROR_COMMAND_SYNTAX_ERROR;
656 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[index++], stmqspi_info->dev.read_cmd);
657 if ((stmqspi_info->dev.read_cmd != 0x03) &&
658 (stmqspi_info->dev.read_cmd != 0x13)) {
659 command_print(CMD, "stmqspi: only 0x03/0x13 READ cmd allowed");
660 return ERROR_COMMAND_SYNTAX_ERROR;
663 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[index++], stmqspi_info->dev.qread_cmd);
664 if ((stmqspi_info->dev.qread_cmd != 0x00) &&
665 (stmqspi_info->dev.qread_cmd != 0x0B) &&
666 (stmqspi_info->dev.qread_cmd != 0x0C) &&
667 (stmqspi_info->dev.qread_cmd != 0x3B) &&
668 (stmqspi_info->dev.qread_cmd != 0x3C) &&
669 (stmqspi_info->dev.qread_cmd != 0x6B) &&
670 (stmqspi_info->dev.qread_cmd != 0x6C) &&
671 (stmqspi_info->dev.qread_cmd != 0xBB) &&
672 (stmqspi_info->dev.qread_cmd != 0xBC) &&
673 (stmqspi_info->dev.qread_cmd != 0xEB) &&
674 (stmqspi_info->dev.qread_cmd != 0xEC) &&
675 (stmqspi_info->dev.qread_cmd != 0xEE)) {
676 command_print(CMD, "stmqspi: only 0x0B/0x0C/0x3B/0x3C/"
677 "0x6B/0x6C/0xBB/0xBC/0xEB/0xEC/0xEE QREAD allowed");
678 return ERROR_COMMAND_SYNTAX_ERROR;
681 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[index++], stmqspi_info->dev.pprog_cmd);
682 if ((stmqspi_info->dev.pprog_cmd != 0x02) &&
683 (stmqspi_info->dev.pprog_cmd != 0x12) &&
684 (stmqspi_info->dev.pprog_cmd != 0x32)) {
685 command_print(CMD, "stmqspi: only 0x02/0x12/0x32 PPRG cmd allowed");
686 return ERROR_COMMAND_SYNTAX_ERROR;
689 if (index < CMD_ARGC)
690 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[index++], stmqspi_info->dev.chip_erase_cmd);
692 stmqspi_info->dev.chip_erase_cmd = 0x00;
694 if (index < CMD_ARGC) {
695 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[index++], stmqspi_info->dev.sectorsize);
696 if ((stmqspi_info->dev.sectorsize > stmqspi_info->dev.size_in_bytes) ||
697 (stmqspi_info->dev.sectorsize < stmqspi_info->dev.pagesize) ||
698 (log2u(stmqspi_info->dev.sectorsize) < 0)) {
699 command_print(CMD, "stmqspi: sector size must be 2^n and <= device size");
700 return ERROR_COMMAND_SYNTAX_ERROR;
703 if (index < CMD_ARGC)
704 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[index++], stmqspi_info->dev.erase_cmd);
706 return ERROR_COMMAND_SYNTAX_ERROR;
708 /* no sector size / sector erase cmd given, treat whole bank as a single sector */
709 stmqspi_info->dev.erase_cmd = 0x00;
710 stmqspi_info->dev.sectorsize = stmqspi_info->dev.size_in_bytes;
713 /* set correct size value */
714 bank->size = stmqspi_info->dev.size_in_bytes << dual;
716 io_base = stmqspi_info->io_base;
717 fsize = (READ_REG(SPI_DCR) >> SPI_FSIZE_POS) & (BIT(SPI_FSIZE_LEN) - 1);
718 if (retval != ERROR_OK)
721 LOG_DEBUG("FSIZE = 0x%04x", fsize);
722 if (bank->size == BIT(fsize + 1))
723 LOG_DEBUG("FSIZE in DCR(1) matches actual capacity. Beware of silicon bug in H7, L4+, MP1.");
724 else if (bank->size == BIT(fsize + 0))
725 LOG_DEBUG("FSIZE in DCR(1) is off by one regarding actual capacity. Fix for silicon bug?");
727 LOG_ERROR("FSIZE in DCR(1) doesn't match actual capacity.");
729 /* create and fill sectors array */
731 stmqspi_info->dev.size_in_bytes / stmqspi_info->dev.sectorsize;
732 sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
733 if (sectors == NULL) {
734 LOG_ERROR("not enough memory");
738 for (unsigned int sector = 0; sector < bank->num_sectors; sector++) {
739 sectors[sector].offset = sector * (stmqspi_info->dev.sectorsize << dual);
740 sectors[sector].size = (stmqspi_info->dev.sectorsize << dual);
741 sectors[sector].is_erased = -1;
742 sectors[sector].is_protected = 0;
745 bank->sectors = sectors;
746 stmqspi_info->dev.name = stmqspi_info->devname;
747 if (stmqspi_info->dev.size_in_bytes / 4096)
748 LOG_INFO("flash \'%s\' id = unknown\nchip size = %" PRIu32 "kbytes,"
749 " bank size = %" PRIu32 "kbytes", stmqspi_info->dev.name,
750 stmqspi_info->dev.size_in_bytes / 1024,
751 (stmqspi_info->dev.size_in_bytes / 1024) << dual);
753 LOG_INFO("flash \'%s\' id = unknown\nchip size = %" PRIu32 "bytes,"
754 " bank size = %" PRIu32 "bytes", stmqspi_info->dev.name,
755 stmqspi_info->dev.size_in_bytes,
756 stmqspi_info->dev.size_in_bytes << dual);
758 stmqspi_info->probed = true;
763 COMMAND_HANDLER(stmqspi_handle_cmd)
765 struct target *target = NULL;
766 struct flash_bank *bank;
767 struct stmqspi_flash_bank *stmqspi_info = NULL;
768 uint32_t io_base, addr;
769 uint8_t num_write, num_read, cmd_byte, data;
772 char temp[4], output[(2 + max + 256) * 3 + 8];
775 LOG_DEBUG("%s", __func__);
778 return ERROR_COMMAND_SYNTAX_ERROR;
780 num_write = CMD_ARGC - 2;
781 if (num_write > max) {
782 LOG_ERROR("at most %d bytes may be sent", max);
783 return ERROR_COMMAND_SYNTAX_ERROR;
786 retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
787 if (ERROR_OK != retval)
790 target = bank->target;
791 stmqspi_info = bank->driver_priv;
792 io_base = stmqspi_info->io_base;
794 if (target->state != TARGET_HALTED) {
795 LOG_ERROR("Target not halted");
796 return ERROR_TARGET_NOT_HALTED;
799 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[1], num_read);
800 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[2], cmd_byte);
803 /* nothing to read, then one command byte and for dual flash
804 * an *even* number of data bytes to follow */
805 if (stmqspi_info->saved_cr & BIT(SPI_DUAL_FLASH)) {
806 if ((num_write & 1) == 0) {
807 LOG_ERROR("number of data bytes to write must be even in dual mode");
808 return ERROR_COMMAND_SYNTAX_ERROR;
812 /* read mode, one command byte and up to four following address bytes */
813 if (stmqspi_info->saved_cr & BIT(SPI_DUAL_FLASH)) {
814 if ((num_read & 1) != 0) {
815 LOG_ERROR("number of bytes to read must be even in dual mode");
816 return ERROR_COMMAND_SYNTAX_ERROR;
819 if ((num_write < 1) || (num_write > 5)) {
820 LOG_ERROR("one cmd and up to four addr bytes must be send when reading");
821 return ERROR_COMMAND_SYNTAX_ERROR;
825 /* Abort any previous operation */
826 retval = target_write_u32(target, io_base + SPI_CR,
827 READ_REG(SPI_CR) | BIT(SPI_ABORT));
828 if (retval != ERROR_OK)
831 /* Wait for busy to be cleared */
832 retval = poll_busy(bank, SPI_PROBE_TIMEOUT);
833 if (retval != ERROR_OK)
836 /* send command byte */
837 snprintf(output, sizeof(output), "spi: %02x ", cmd_byte);
839 /* write, send cmd byte */
840 retval = target_write_u32(target, io_base + SPI_DLR, ((uint32_t)num_write) - 2);
841 if (retval != ERROR_OK)
845 retval = OCTOSPI_CMD(OCTOSPI_WRITE_MODE,
846 (OCTOSPI_MODE_CCR & OCTOSPI_NO_ALTB & OCTOSPI_NO_ADDR &
847 ((num_write == 1) ? OCTOSPI_NO_DATA : ~0U)), cmd_byte);
849 retval = target_write_u32(target, io_base + QSPI_CCR,
850 (QSPI_MODE & ~QSPI_DCYC_MASK & QSPI_NO_ALTB & QSPI_NO_ADDR &
851 ((num_write == 1) ? QSPI_NO_DATA : ~0U)) |
852 (QSPI_WRITE_MODE | cmd_byte));
853 if (retval != ERROR_OK)
856 /* send additional data bytes */
857 for (count = 3; count < CMD_ARGC; count++) {
858 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[count], data);
859 snprintf(temp, sizeof(temp), "%02" PRIx8 " ", data);
860 retval = target_write_u8(target, io_base + SPI_DR, data);
861 if (retval != ERROR_OK)
863 strncat(output, temp, sizeof(output) - strlen(output) - 1);
865 strncat(output, "-> ", sizeof(output) - strlen(output) - 1);
867 /* read, pack additional bytes into address */
869 for (count = 3; count < CMD_ARGC; count++) {
870 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[count], data);
871 snprintf(temp, sizeof(temp), "%02" PRIx8 " ", data);
872 addr = (addr << 8) | data;
873 strncat(output, temp, sizeof(output) - strlen(output) - 1);
875 strncat(output, "-> ", sizeof(output) - strlen(output) - 1);
877 /* send cmd byte, if ADMODE indicates no address, this already triggers command */
878 retval = target_write_u32(target, io_base + SPI_DLR, ((uint32_t)num_read) - 1);
879 if (retval != ERROR_OK)
882 retval = OCTOSPI_CMD(OCTOSPI_READ_MODE,
883 (OCTOSPI_MODE_CCR & OCTOSPI_NO_DDTR & OCTOSPI_NO_ALTB & ~OCTOSPI_ADDR4 &
884 ((num_write == 1) ? OCTOSPI_NO_ADDR : ~0U)) |
885 (((num_write - 2) & 0x3U) << SPI_ADSIZE_POS), cmd_byte);
887 retval = target_write_u32(target, io_base + QSPI_CCR,
888 (QSPI_MODE & ~QSPI_DCYC_MASK & QSPI_NO_ALTB & ~QSPI_ADDR4 &
889 ((num_write == 1) ? QSPI_NO_ADDR : ~0U)) |
890 ((QSPI_READ_MODE | (((num_write - 2) & 0x3U) << SPI_ADSIZE_POS) | cmd_byte)));
891 if (retval != ERROR_OK)
895 /* if ADMODE indicates address required, only the write to AR triggers command */
896 retval = target_write_u32(target, io_base + SPI_AR, addr);
897 if (retval != ERROR_OK)
901 /* read response bytes */
902 for ( ; num_read > 0; num_read--) {
903 retval = target_read_u8(target, io_base + SPI_DR, &data);
904 if (retval != ERROR_OK)
906 snprintf(temp, sizeof(temp), "%02" PRIx8 " ", data);
907 strncat(output, temp, sizeof(output) - strlen(output) - 1);
910 command_print(CMD, "%s", output);
913 /* Switch to memory mapped mode before return to prompt */
919 static int qspi_erase_sector(struct flash_bank *bank, unsigned int sector)
921 struct target *target = bank->target;
922 struct stmqspi_flash_bank *stmqspi_info = bank->driver_priv;
923 uint32_t io_base = stmqspi_info->io_base;
927 retval = qspi_write_enable(bank);
928 if (retval != ERROR_OK)
931 /* Send Sector Erase command */
933 retval = OCTOSPI_CMD(OCTOSPI_WRITE_MODE, OCTOSPI_CCR_SECTOR_ERASE,
934 stmqspi_info->dev.erase_cmd);
936 retval = target_write_u32(target, io_base + QSPI_CCR, QSPI_CCR_SECTOR_ERASE);
937 if (retval != ERROR_OK)
940 /* Address is sector offset, this write initiates command transmission */
941 retval = target_write_u32(target, io_base + SPI_AR, bank->sectors[sector].offset);
942 if (retval != ERROR_OK)
945 /* Wait for transmit of command completed */
946 poll_busy(bank, SPI_CMD_TIMEOUT);
947 if (retval != ERROR_OK)
950 /* Read flash status register(s) */
951 retval = read_status_reg(bank, &status);
952 if (retval != ERROR_OK)
955 LOG_DEBUG("erase status regs: 0x%04" PRIx16, status);
957 /* Check for command in progress for flash 1 */
958 /* If BSY and WE are already cleared the erase did probably complete already */
959 if (((stmqspi_info->saved_cr & (BIT(SPI_DUAL_FLASH) | BIT(SPI_FSEL_FLASH)))
960 != BIT(SPI_FSEL_FLASH)) && ((status & SPIFLASH_BSY_BIT) == 0) &&
961 ((status & SPIFLASH_WE_BIT) != 0)) {
962 LOG_ERROR("Sector erase command not accepted by flash1. Status=0x%02x",
964 retval = ERROR_FLASH_OPERATION_FAILED;
968 /* Check for command in progress for flash 2 */
969 /* If BSY and WE are already cleared the erase did probably complete already */
971 if (((stmqspi_info->saved_cr & (BIT(SPI_DUAL_FLASH) | BIT(SPI_FSEL_FLASH))) != 0) &&
972 ((status & SPIFLASH_BSY_BIT) == 0) &&
973 ((status & SPIFLASH_WE_BIT) != 0)) {
974 LOG_ERROR("Sector erase command not accepted by flash2. Status=0x%02x",
976 retval = ERROR_FLASH_OPERATION_FAILED;
980 /* Erase takes a long time, so some sort of progress message is a good idea */
981 LOG_DEBUG("erasing sector %4u", sector);
983 /* Poll WIP for end of self timed Sector Erase cycle */
984 retval = wait_till_ready(bank, SPI_MAX_TIMEOUT);
990 static int stmqspi_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
992 struct target *target = bank->target;
993 struct stmqspi_flash_bank *stmqspi_info = bank->driver_priv;
995 int retval = ERROR_OK;
997 LOG_DEBUG("%s: from sector %u to sector %u", __func__, first, last);
999 if (target->state != TARGET_HALTED) {
1000 LOG_ERROR("Target not halted");
1001 return ERROR_TARGET_NOT_HALTED;
1004 if (!(stmqspi_info->probed)) {
1005 LOG_ERROR("Flash bank not probed");
1006 return ERROR_FLASH_BANK_NOT_PROBED;
1009 if (stmqspi_info->dev.erase_cmd == 0x00) {
1010 LOG_ERROR("Sector erase not available for this device");
1011 return ERROR_FLASH_OPER_UNSUPPORTED;
1014 if ((last < first) || (last >= bank->num_sectors)) {
1015 LOG_ERROR("Flash sector invalid");
1016 return ERROR_FLASH_SECTOR_INVALID;
1019 for (sector = first; sector <= last; sector++) {
1020 if (bank->sectors[sector].is_protected) {
1021 LOG_ERROR("Flash sector %u protected", sector);
1022 return ERROR_FLASH_PROTECTED;
1026 for (sector = first; sector <= last; sector++) {
1027 retval = qspi_erase_sector(bank, sector);
1028 if (retval != ERROR_OK)
1034 if (retval != ERROR_OK)
1035 LOG_ERROR("Flash sector_erase failed on sector %u", sector);
1037 /* Switch to memory mapped mode before return to prompt */
1043 static int stmqspi_protect(struct flash_bank *bank, int set,
1044 unsigned int first, unsigned int last)
1046 unsigned int sector;
1048 for (sector = first; sector <= last; sector++)
1049 bank->sectors[sector].is_protected = set;
1052 LOG_WARNING("setting soft protection only, not related to flash's hardware write protection");
1057 /* Check whether flash is blank */
1058 static int stmqspi_blank_check(struct flash_bank *bank)
1060 struct target *target = bank->target;
1061 struct stmqspi_flash_bank *stmqspi_info = bank->driver_priv;
1062 uint32_t io_base = stmqspi_info->io_base;
1063 struct duration bench;
1064 struct reg_param reg_params[2];
1065 struct armv7m_algorithm armv7m_info;
1066 struct working_area *algorithm;
1067 const uint8_t *code;
1068 struct sector_info erase_check_info;
1069 uint32_t codesize, maxsize, result, exit_point;
1070 unsigned int count, index, num_sectors, sector;
1072 const uint32_t erased = 0x00FF;
1074 if (target->state != TARGET_HALTED) {
1075 LOG_ERROR("Target not halted");
1076 return ERROR_TARGET_NOT_HALTED;
1079 if (!(stmqspi_info->probed)) {
1080 LOG_ERROR("Flash bank not probed");
1081 return ERROR_FLASH_BANK_NOT_PROBED;
1084 /* Abort any previous operation */
1085 retval = target_write_u32(target, io_base + SPI_CR,
1086 READ_REG(SPI_CR) | BIT(SPI_ABORT));
1087 if (retval != ERROR_OK)
1090 /* Wait for busy to be cleared */
1091 retval = poll_busy(bank, SPI_PROBE_TIMEOUT);
1092 if (retval != ERROR_OK)
1095 /* see contrib/loaders/flash/stmqspi/stmqspi_erase_check.S for src */
1096 static const uint8_t stmqspi_erase_check_code[] = {
1097 #include "../../../contrib/loaders/flash/stmqspi/stmqspi_erase_check.inc"
1100 /* see contrib/loaders/flash/stmqspi/stmoctospi_erase_check.S for src */
1101 static const uint8_t stmoctospi_erase_check_code[] = {
1102 #include "../../../contrib/loaders/flash/stmqspi/stmoctospi_erase_check.inc"
1106 code = stmoctospi_erase_check_code;
1107 codesize = sizeof(stmoctospi_erase_check_code);
1109 code = stmqspi_erase_check_code;
1110 codesize = sizeof(stmqspi_erase_check_code);
1113 /* This will overlay the last 4 words of stmqspi/stmoctospi_erase_check_code in target */
1114 /* for read use the saved settings (memory mapped mode) but indirect read mode */
1115 uint32_t ccr_buffer[][4] = {
1116 /* cr (not used for QSPI) *
1117 * ccr (for both QSPI and OCTOSPI) *
1118 * tcr (not used for QSPI) *
1119 * ir (not used for QSPI) */
1121 h_to_le_32(OCTOSPI_MODE | OCTOSPI_READ_MODE),
1122 h_to_le_32(IS_OCTOSPI ? OCTOSPI_CCR_READ : QSPI_CCR_READ),
1123 h_to_le_32(stmqspi_info->saved_tcr),
1124 h_to_le_32(stmqspi_info->saved_ir),
1128 maxsize = target_get_working_area_avail(target);
1129 if (maxsize < codesize + sizeof(erase_check_info)) {
1130 LOG_ERROR("Not enough working area, can't do QSPI blank check");
1131 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1134 num_sectors = (maxsize - codesize) / sizeof(erase_check_info);
1135 num_sectors = (bank->num_sectors < num_sectors) ? bank->num_sectors : num_sectors;
1137 if (target_alloc_working_area_try(target,
1138 codesize + num_sectors * sizeof(erase_check_info), &algorithm) != ERROR_OK) {
1139 LOG_ERROR("allocating working area failed");
1140 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1143 /* prepare blank check code, excluding ccr_buffer */
1144 retval = target_write_buffer(target, algorithm->address,
1145 codesize - sizeof(ccr_buffer), code);
1146 if (retval != ERROR_OK)
1149 /* prepare QSPI/OCTOSPI_CCR register values */
1150 retval = target_write_buffer(target, algorithm->address
1151 + codesize - sizeof(ccr_buffer),
1152 sizeof(ccr_buffer), (uint8_t *)ccr_buffer);
1153 if (retval != ERROR_OK)
1156 duration_start(&bench);
1158 /* after breakpoint instruction (halfword), one nop (halfword) and
1159 * port_buffer till end of code */
1160 exit_point = algorithm->address + codesize - sizeof(uint32_t) - sizeof(ccr_buffer);
1162 init_reg_param(®_params[0], "r0", 32, PARAM_OUT); /* sector count */
1163 init_reg_param(®_params[1], "r1", 32, PARAM_OUT); /* QSPI/OCTOSPI io_base */
1166 while (sector < bank->num_sectors) {
1167 /* at most num_sectors sectors to handle in one run */
1168 count = bank->num_sectors - sector;
1169 if (count > num_sectors)
1170 count = num_sectors;
1172 for (index = 0; index < count; index++) {
1173 erase_check_info.offset = h_to_le_32(bank->sectors[sector + index].offset);
1174 erase_check_info.size = h_to_le_32(bank->sectors[sector + index].size);
1175 erase_check_info.result = h_to_le_32(erased);
1177 retval = target_write_buffer(target, algorithm->address
1178 + codesize + index * sizeof(erase_check_info),
1179 sizeof(erase_check_info), (uint8_t *)&erase_check_info);
1180 if (retval != ERROR_OK)
1184 buf_set_u32(reg_params[0].value, 0, 32, count);
1185 buf_set_u32(reg_params[1].value, 0, 32, stmqspi_info->io_base);
1187 armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
1188 armv7m_info.core_mode = ARM_MODE_THREAD;
1190 LOG_DEBUG("checking sectors %u to %u", sector, sector + count - 1);
1191 /* check a block of sectors */
1192 retval = target_run_algorithm(target,
1194 ARRAY_SIZE(reg_params), reg_params,
1195 algorithm->address, exit_point,
1196 count * ((bank->sectors[sector].size >> 6) + 1) + 1000,
1198 if (retval != ERROR_OK)
1201 for (index = 0; index < count; index++) {
1202 retval = target_read_buffer(target, algorithm->address
1203 + codesize + index * sizeof(erase_check_info),
1204 sizeof(erase_check_info), (uint8_t *)&erase_check_info);
1205 if (retval != ERROR_OK)
1208 if ((erase_check_info.offset != h_to_le_32(bank->sectors[sector + index].offset)) ||
1209 (erase_check_info.size != 0)) {
1210 LOG_ERROR("corrupted blank check info");
1214 /* we need le_32_to_h, but that's the same as h_to_le_32 */
1215 result = h_to_le_32(erase_check_info.result);
1216 bank->sectors[sector + index].is_erased = ((result & 0xFF) == 0xFF);
1217 LOG_DEBUG("Flash sector %u checked: 0x%04x", sector + index, result & 0xFFFFU);
1223 destroy_reg_param(®_params[0]);
1224 destroy_reg_param(®_params[1]);
1226 duration_measure(&bench);
1227 LOG_INFO("stmqspi blank checked in %fs (%0.3f KiB/s)", duration_elapsed(&bench),
1228 duration_kbps(&bench, bank->size));
1231 target_free_working_area(target, algorithm);
1233 /* Switch to memory mapped mode before return to prompt */
1239 /* Verify checksum */
1240 static int qspi_verify(struct flash_bank *bank, uint8_t *buffer,
1241 uint32_t offset, uint32_t count)
1243 struct target *target = bank->target;
1244 struct stmqspi_flash_bank *stmqspi_info = bank->driver_priv;
1245 struct reg_param reg_params[4];
1246 struct armv7m_algorithm armv7m_info;
1247 struct working_area *algorithm;
1248 const uint8_t *code;
1249 uint32_t pagesize, codesize, crc32, result, exit_point;
1252 /* see contrib/loaders/flash/stmqspi/stmqspi_crc32.S for src */
1253 static const uint8_t stmqspi_crc32_code[] = {
1254 #include "../../../contrib/loaders/flash/stmqspi/stmqspi_crc32.inc"
1257 /* see contrib/loaders/flash/stmqspi/stmoctospi_crc32.S for src */
1258 static const uint8_t stmoctospi_crc32_code[] = {
1259 #include "../../../contrib/loaders/flash/stmqspi/stmoctospi_crc32.inc"
1263 code = stmoctospi_crc32_code;
1264 codesize = sizeof(stmoctospi_crc32_code);
1266 code = stmqspi_crc32_code;
1267 codesize = sizeof(stmqspi_crc32_code);
1270 /* block size doesn't matter that much here */
1271 pagesize = stmqspi_info->dev.sectorsize;
1273 pagesize = stmqspi_info->dev.pagesize;
1275 pagesize = SPIFLASH_DEF_PAGESIZE;
1277 /* adjust size according to dual flash mode */
1278 pagesize = (stmqspi_info->saved_cr & BIT(SPI_DUAL_FLASH)) ? pagesize << 1 : pagesize;
1280 /* This will overlay the last 4 words of stmqspi/stmoctospi_crc32_code in target */
1281 /* for read use the saved settings (memory mapped mode) but indirect read mode */
1282 uint32_t ccr_buffer[][4] = {
1283 /* cr (not used for QSPI) *
1284 * ccr (for both QSPI and OCTOSPI) *
1285 * tcr (not used for QSPI) *
1286 * ir (not used for QSPI) */
1288 h_to_le_32(OCTOSPI_MODE | OCTOSPI_READ_MODE),
1289 h_to_le_32(IS_OCTOSPI ? OCTOSPI_CCR_READ : QSPI_CCR_READ),
1290 h_to_le_32(stmqspi_info->saved_tcr),
1291 h_to_le_32(stmqspi_info->saved_ir),
1295 if (target_alloc_working_area_try(target, codesize, &algorithm) != ERROR_OK) {
1296 LOG_ERROR("Not enough working area, can't do QSPI verify");
1297 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1300 /* prepare verify code, excluding ccr_buffer */
1301 retval = target_write_buffer(target, algorithm->address,
1302 codesize - sizeof(ccr_buffer), code);
1303 if (retval != ERROR_OK)
1306 /* prepare QSPI/OCTOSPI_CCR register values */
1307 retval = target_write_buffer(target, algorithm->address
1308 + codesize - sizeof(ccr_buffer),
1309 sizeof(ccr_buffer), (uint8_t *)ccr_buffer);
1310 if (retval != ERROR_OK)
1313 /* after breakpoint instruction (halfword), one nop (halfword) and
1314 * port_buffer till end of code */
1315 exit_point = algorithm->address + codesize - sizeof(uint32_t) - sizeof(ccr_buffer);
1317 init_reg_param(®_params[0], "r0", 32, PARAM_IN_OUT); /* count (in), crc32 (out) */
1318 init_reg_param(®_params[1], "r1", 32, PARAM_OUT); /* pagesize */
1319 init_reg_param(®_params[2], "r2", 32, PARAM_OUT); /* offset into flash address */
1320 init_reg_param(®_params[3], "r3", 32, PARAM_OUT); /* QSPI/OCTOSPI io_base */
1322 buf_set_u32(reg_params[0].value, 0, 32, count);
1323 buf_set_u32(reg_params[1].value, 0, 32, pagesize);
1324 buf_set_u32(reg_params[2].value, 0, 32, offset);
1325 buf_set_u32(reg_params[3].value, 0, 32, stmqspi_info->io_base);
1328 armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
1329 armv7m_info.core_mode = ARM_MODE_THREAD;
1331 retval = target_run_algorithm(target,
1333 ARRAY_SIZE(reg_params), reg_params,
1334 algorithm->address, exit_point,
1335 (count >> 5) + 1000,
1339 image_calculate_checksum(buffer, count, &crc32);
1341 if (retval == ERROR_OK) {
1342 result = buf_get_u32(reg_params[0].value, 0, 32);
1343 LOG_DEBUG("addr " TARGET_ADDR_FMT ", len 0x%08" PRIx32 ", crc 0x%08" PRIx32 " 0x%08" PRIx32,
1344 offset + bank->base, count, ~crc32, result);
1345 if (~crc32 != result)
1346 retval = ERROR_FAIL;
1349 destroy_reg_param(®_params[0]);
1350 destroy_reg_param(®_params[1]);
1351 destroy_reg_param(®_params[2]);
1352 destroy_reg_param(®_params[3]);
1355 target_free_working_area(target, algorithm);
1357 /* Switch to memory mapped mode before return to prompt */
1363 static int qspi_read_write_block(struct flash_bank *bank, uint8_t *buffer,
1364 uint32_t offset, uint32_t count, bool write)
1366 struct target *target = bank->target;
1367 struct stmqspi_flash_bank *stmqspi_info = bank->driver_priv;
1368 uint32_t io_base = stmqspi_info->io_base;
1369 struct reg_param reg_params[6];
1370 struct armv7m_algorithm armv7m_info;
1371 struct working_area *algorithm;
1372 uint32_t pagesize, fifo_start, fifosize, remaining;
1373 uint32_t maxsize, codesize, exit_point;
1374 const uint8_t *code = NULL;
1378 LOG_DEBUG("%s: offset=0x%08" PRIx32 " len=0x%08" PRIx32,
1379 __func__, offset, count);
1381 dual = (stmqspi_info->saved_cr & BIT(SPI_DUAL_FLASH)) ? 1 : 0;
1383 /* see contrib/loaders/flash/stmqspi/stmqspi_read.S for src */
1384 static const uint8_t stmqspi_read_code[] = {
1385 #include "../../../contrib/loaders/flash/stmqspi/stmqspi_read.inc"
1388 /* see contrib/loaders/flash/stmqspi/stmoctospi_read.S for src */
1389 static const uint8_t stmoctospi_read_code[] = {
1390 #include "../../../contrib/loaders/flash/stmqspi/stmoctospi_read.inc"
1393 /* see contrib/loaders/flash/stmqspi/stmqspi_write.S for src */
1394 static const uint8_t stmqspi_write_code[] = {
1395 #include "../../../contrib/loaders/flash/stmqspi/stmqspi_write.inc"
1398 /* see contrib/loaders/flash/stmqspi/stmoctospi_write.S for src */
1399 static const uint8_t stmoctospi_write_code[] = {
1400 #include "../../../contrib/loaders/flash/stmqspi/stmoctospi_write.inc"
1403 /* This will overlay the last 12 words of stmqspi/stmoctospi_read/write_code in target */
1404 /* for read use the saved settings (memory mapped mode) but indirect read mode */
1405 uint32_t ccr_buffer[][4] = {
1406 /* cr (not used for QSPI) *
1407 * ccr (for both QSPI and OCTOSPI) *
1408 * tcr (not used for QSPI) *
1409 * ir (not used for QSPI) */
1411 h_to_le_32(OCTOSPI_MODE | OCTOSPI_READ_MODE),
1412 h_to_le_32(IS_OCTOSPI ? OCTOSPI_CCR_READ_STATUS : QSPI_CCR_READ_STATUS),
1413 h_to_le_32((stmqspi_info->saved_tcr & ~OCTOSPI_DCYC_MASK) |
1414 (OPI_MODE ? (OPI_DUMMY << OCTOSPI_DCYC_POS) : 0)),
1415 h_to_le_32(OPI_CMD(SPIFLASH_READ_STATUS)),
1418 h_to_le_32(OCTOSPI_MODE | OCTOSPI_WRITE_MODE),
1419 h_to_le_32(IS_OCTOSPI ? OCTOSPI_CCR_WRITE_ENABLE : QSPI_CCR_WRITE_ENABLE),
1420 h_to_le_32(stmqspi_info->saved_tcr & ~OCTOSPI_DCYC_MASK),
1421 h_to_le_32(OPI_CMD(SPIFLASH_WRITE_ENABLE)),
1424 h_to_le_32(OCTOSPI_MODE | (write ? OCTOSPI_WRITE_MODE : OCTOSPI_READ_MODE)),
1425 h_to_le_32(write ? (IS_OCTOSPI ? OCTOSPI_CCR_PAGE_PROG : QSPI_CCR_PAGE_PROG) :
1426 (IS_OCTOSPI ? OCTOSPI_CCR_READ : QSPI_CCR_READ)),
1427 h_to_le_32(write ? (stmqspi_info->saved_tcr & ~OCTOSPI_DCYC_MASK) :
1428 stmqspi_info->saved_tcr),
1429 h_to_le_32(write ? OPI_CMD(stmqspi_info->dev.pprog_cmd) : stmqspi_info->saved_ir),
1433 /* force reasonable defaults */
1434 fifosize = stmqspi_info->dev.sectorsize ?
1435 stmqspi_info->dev.sectorsize : stmqspi_info->dev.size_in_bytes;
1439 code = stmoctospi_write_code;
1440 codesize = sizeof(stmoctospi_write_code);
1442 code = stmqspi_write_code;
1443 codesize = sizeof(stmqspi_write_code);
1447 code = stmoctospi_read_code;
1448 codesize = sizeof(stmoctospi_read_code);
1450 code = stmqspi_read_code;
1451 codesize = sizeof(stmqspi_read_code);
1455 /* for write, pagesize must be taken into account */
1456 /* for read, the page size doesn't matter that much */
1457 pagesize = stmqspi_info->dev.pagesize;
1459 pagesize = (fifosize <= SPIFLASH_DEF_PAGESIZE) ?
1460 fifosize : SPIFLASH_DEF_PAGESIZE;
1462 /* adjust sizes according to dual flash mode */
1466 /* memory buffer, we assume sectorsize to be a power of 2 times pagesize */
1467 maxsize = target_get_working_area_avail(target);
1468 if (maxsize < codesize + 2 * sizeof(uint32_t) + pagesize) {
1469 LOG_ERROR("not enough working area, can't do QSPI page reads/writes");
1470 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1473 /* fifo size at most sector size, and multiple of page size */
1474 maxsize -= (codesize + 2 * sizeof(uint32_t));
1475 fifosize = ((maxsize < fifosize) ? maxsize : fifosize) & ~(pagesize - 1);
1477 if (target_alloc_working_area_try(target,
1478 codesize + 2 * sizeof(uint32_t) + fifosize, &algorithm) != ERROR_OK) {
1479 LOG_ERROR("allocating working area failed");
1480 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1483 /* prepare flash write code, excluding ccr_buffer */
1484 retval = target_write_buffer(target, algorithm->address,
1485 codesize - sizeof(ccr_buffer), code);
1486 if (retval != ERROR_OK)
1489 /* prepare QSPI/OCTOSPI_CCR register values */
1490 retval = target_write_buffer(target, algorithm->address
1491 + codesize - sizeof(ccr_buffer),
1492 sizeof(ccr_buffer), (uint8_t *)ccr_buffer);
1493 if (retval != ERROR_OK)
1496 /* target buffer starts right after flash_write_code, i.e.
1497 * wp and rp are implicitly included in buffer!!! */
1498 fifo_start = algorithm->address + codesize + 2 * sizeof(uint32_t);
1500 init_reg_param(®_params[0], "r0", 32, PARAM_IN_OUT); /* count (in), status (out) */
1501 init_reg_param(®_params[1], "r1", 32, PARAM_OUT); /* pagesize */
1502 init_reg_param(®_params[2], "r2", 32, PARAM_IN_OUT); /* offset into flash address */
1503 init_reg_param(®_params[3], "r3", 32, PARAM_OUT); /* QSPI/OCTOSPI io_base */
1504 init_reg_param(®_params[4], "r8", 32, PARAM_OUT); /* fifo start */
1505 init_reg_param(®_params[5], "r9", 32, PARAM_OUT); /* fifo end + 1 */
1507 buf_set_u32(reg_params[0].value, 0, 32, count);
1508 buf_set_u32(reg_params[1].value, 0, 32, pagesize);
1509 buf_set_u32(reg_params[2].value, 0, 32, offset);
1510 buf_set_u32(reg_params[3].value, 0, 32, io_base);
1511 buf_set_u32(reg_params[4].value, 0, 32, fifo_start);
1512 buf_set_u32(reg_params[5].value, 0, 32, fifo_start + fifosize);
1514 armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
1515 armv7m_info.core_mode = ARM_MODE_THREAD;
1517 /* after breakpoint instruction (halfword), one nop (halfword) and
1518 * ccr_buffer follow till end of code */
1519 exit_point = algorithm->address + codesize
1520 - (sizeof(ccr_buffer) + sizeof(uint32_t));
1523 retval = target_run_flash_async_algorithm(target, buffer, count, 1,
1525 ARRAY_SIZE(reg_params), reg_params,
1526 algorithm->address + codesize,
1527 fifosize + 2 * sizeof(uint32_t),
1528 algorithm->address, exit_point,
1531 retval = target_run_read_async_algorithm(target, buffer, count, 1,
1533 ARRAY_SIZE(reg_params), reg_params,
1534 algorithm->address + codesize,
1535 fifosize + 2 * sizeof(uint32_t),
1536 algorithm->address, exit_point,
1540 remaining = buf_get_u32(reg_params[0].value, 0, 32);
1541 if ((retval == ERROR_OK) && remaining)
1542 retval = ERROR_FLASH_OPERATION_FAILED;
1544 if (retval != ERROR_OK) {
1545 offset = buf_get_u32(reg_params[2].value, 0, 32);
1546 LOG_ERROR("flash %s failed at address 0x%" PRIx32 ", remaining 0x%" PRIx32,
1547 write ? "write" : "read", offset, remaining);
1550 destroy_reg_param(®_params[0]);
1551 destroy_reg_param(®_params[1]);
1552 destroy_reg_param(®_params[2]);
1553 destroy_reg_param(®_params[3]);
1554 destroy_reg_param(®_params[4]);
1555 destroy_reg_param(®_params[5]);
1558 target_free_working_area(target, algorithm);
1560 /* Switch to memory mapped mode before return to prompt */
1566 static int stmqspi_read(struct flash_bank *bank, uint8_t *buffer,
1567 uint32_t offset, uint32_t count)
1569 struct target *target = bank->target;
1570 struct stmqspi_flash_bank *stmqspi_info = bank->driver_priv;
1571 uint32_t io_base = stmqspi_info->io_base;
1574 LOG_DEBUG("%s: offset=0x%08" PRIx32 " count=0x%08" PRIx32,
1575 __func__, offset, count);
1577 if (target->state != TARGET_HALTED) {
1578 LOG_ERROR("Target not halted");
1579 return ERROR_TARGET_NOT_HALTED;
1582 if (!(stmqspi_info->probed)) {
1583 LOG_ERROR("Flash bank not probed");
1584 return ERROR_FLASH_BANK_NOT_PROBED;
1587 if (offset + count > bank->size) {
1588 LOG_WARNING("Read beyond end of flash. Extra data to be ignored.");
1589 count = bank->size - offset;
1592 /* Abort any previous operation */
1593 retval = target_write_u32(target, io_base + SPI_CR,
1594 READ_REG(SPI_CR) | BIT(SPI_ABORT));
1595 if (retval != ERROR_OK)
1598 /* Wait for busy to be cleared */
1599 retval = poll_busy(bank, SPI_PROBE_TIMEOUT);
1600 if (retval != ERROR_OK)
1603 return qspi_read_write_block(bank, buffer, offset, count, false);
1606 static int stmqspi_write(struct flash_bank *bank, const uint8_t *buffer,
1607 uint32_t offset, uint32_t count)
1609 struct target *target = bank->target;
1610 struct stmqspi_flash_bank *stmqspi_info = bank->driver_priv;
1611 uint32_t io_base = stmqspi_info->io_base;
1612 unsigned int dual, sector;
1616 LOG_DEBUG("%s: offset=0x%08" PRIx32 " count=0x%08" PRIx32,
1617 __func__, offset, count);
1619 dual = (stmqspi_info->saved_cr & BIT(SPI_DUAL_FLASH)) ? 1 : 0;
1620 octal_dtr = IS_OCTOSPI && (stmqspi_info->saved_ccr & BIT(OCTOSPI_DDTR));
1622 if (target->state != TARGET_HALTED) {
1623 LOG_ERROR("Target not halted");
1624 return ERROR_TARGET_NOT_HALTED;
1627 if (!(stmqspi_info->probed)) {
1628 LOG_ERROR("Flash bank not probed");
1629 return ERROR_FLASH_BANK_NOT_PROBED;
1632 if (offset + count > bank->size) {
1633 LOG_WARNING("Write beyond end of flash. Extra data discarded.");
1634 count = bank->size - offset;
1637 /* Check sector protection */
1638 for (sector = 0; sector < bank->num_sectors; sector++) {
1639 /* Start offset in or before this sector? */
1640 /* End offset in or behind this sector? */
1641 if ((offset < (bank->sectors[sector].offset + bank->sectors[sector].size)) &&
1642 ((offset + count - 1) >= bank->sectors[sector].offset) &&
1643 bank->sectors[sector].is_protected) {
1644 LOG_ERROR("Flash sector %u protected", sector);
1645 return ERROR_FLASH_PROTECTED;
1649 if ((dual || octal_dtr) && ((offset & 1) != 0 || (count & 1) != 0)) {
1650 LOG_ERROR("In dual-QSPI and octal-DTR modes writes must be two byte aligned: "
1651 "%s: address=0x%08" PRIx32 " len=0x%08" PRIx32, __func__, offset, count);
1652 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
1655 /* Abort any previous operation */
1656 retval = target_write_u32(target, io_base + SPI_CR,
1657 READ_REG(SPI_CR) | BIT(SPI_ABORT));
1658 if (retval != ERROR_OK)
1661 /* Wait for busy to be cleared */
1662 retval = poll_busy(bank, SPI_PROBE_TIMEOUT);
1663 if (retval != ERROR_OK)
1666 return qspi_read_write_block(bank, (uint8_t *)buffer, offset, count, true);
1669 static int stmqspi_verify(struct flash_bank *bank, const uint8_t *buffer,
1670 uint32_t offset, uint32_t count)
1672 struct target *target = bank->target;
1673 struct stmqspi_flash_bank *stmqspi_info = bank->driver_priv;
1674 uint32_t io_base = stmqspi_info->io_base;
1679 LOG_DEBUG("%s: offset=0x%08" PRIx32 " count=0x%08" PRIx32,
1680 __func__, offset, count);
1682 dual = (stmqspi_info->saved_cr & BIT(SPI_DUAL_FLASH)) ? 1 : 0;
1683 octal_dtr = IS_OCTOSPI && (stmqspi_info->saved_ccr & BIT(OCTOSPI_DDTR));
1685 if (target->state != TARGET_HALTED) {
1686 LOG_ERROR("Target not halted");
1687 return ERROR_TARGET_NOT_HALTED;
1690 if (!(stmqspi_info->probed)) {
1691 LOG_ERROR("Flash bank not probed");
1692 return ERROR_FLASH_BANK_NOT_PROBED;
1695 if (offset + count > bank->size) {
1696 LOG_WARNING("Verify beyond end of flash. Extra data ignored.");
1697 count = bank->size - offset;
1700 if ((dual || octal_dtr) && ((offset & 1) != 0 || (count & 1) != 0)) {
1701 LOG_ERROR("In dual-QSPI and octal-DTR modes reads must be two byte aligned: "
1702 "%s: address=0x%08" PRIx32 " len=0x%08" PRIx32, __func__, offset, count);
1703 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
1706 /* Abort any previous operation */
1707 retval = target_write_u32(target, io_base + SPI_CR,
1708 READ_REG(SPI_CR) | BIT(SPI_ABORT));
1709 if (retval != ERROR_OK)
1712 /* Wait for busy to be cleared */
1713 retval = poll_busy(bank, SPI_PROBE_TIMEOUT);
1714 if (retval != ERROR_OK)
1717 return qspi_verify(bank, (uint8_t *)buffer, offset, count);
1720 /* Find appropriate dummy setting, in particular octo mode */
1721 static int find_sfdp_dummy(struct flash_bank *bank, int len)
1723 struct target *target = bank->target;
1724 struct stmqspi_flash_bank *stmqspi_info = bank->driver_priv;
1725 uint32_t io_base = stmqspi_info->io_base;
1727 unsigned int dual, count;
1728 bool flash1 = !(stmqspi_info->saved_cr & BIT(SPI_FSEL_FLASH));
1730 const unsigned int max_bytes = 64;
1732 dual = (stmqspi_info->saved_cr & BIT(SPI_DUAL_FLASH)) ? 1 : 0;
1734 LOG_DEBUG("%s: len=%d, dual=%u, flash1=%d",
1735 __func__, len, dual, flash1);
1737 /* Abort any previous operation */
1738 retval = target_write_u32(target, io_base + SPI_CR,
1739 stmqspi_info->saved_cr | BIT(SPI_ABORT));
1740 if (retval != ERROR_OK)
1743 /* Wait for busy to be cleared */
1744 retval = poll_busy(bank, SPI_PROBE_TIMEOUT);
1745 if (retval != ERROR_OK)
1748 /* Switch to saved_cr (had to be set accordingly before this call) */
1749 retval = target_write_u32(target, io_base + SPI_CR, stmqspi_info->saved_cr);
1750 if (retval != ERROR_OK)
1753 /* Read at most that many bytes */
1754 retval = target_write_u32(target, io_base + SPI_DLR, (max_bytes << dual) - 1);
1755 if (retval != ERROR_OK)
1758 /* Read SFDP block */
1760 retval = OCTOSPI_CMD(OCTOSPI_READ_MODE, OCTOSPI_CCR_READ_SFDP(len),
1761 SPIFLASH_READ_SFDP);
1763 retval = target_write_u32(target, io_base + QSPI_CCR, QSPI_CCR_READ_SFDP);
1764 if (retval != ERROR_OK)
1767 /* Read from start of sfdp block */
1768 retval = target_write_u32(target, io_base + SPI_AR, 0);
1769 if (retval != ERROR_OK)
1772 for (count = 0 ; count < max_bytes; count++) {
1773 if ((dual != 0) && !flash1) {
1774 /* discard even byte in dual flash-mode if flash2 */
1775 retval = target_read_u8(target, io_base + SPI_DR, &data);
1776 if (retval != ERROR_OK)
1780 retval = target_read_u8(target, io_base + SPI_DR, &data);
1781 if (retval != ERROR_OK)
1785 LOG_DEBUG("start of SFDP header for flash%c after %u dummy bytes",
1786 flash1 ? '1' : '2', count);
1788 stmqspi_info->sfdp_dummy1 = count;
1790 stmqspi_info->sfdp_dummy2 = count;
1794 if ((dual != 0) && flash1) {
1795 /* discard odd byte in dual flash-mode if flash1 */
1796 retval = target_read_u8(target, io_base + SPI_DR, &data);
1797 if (retval != ERROR_OK)
1802 retval = ERROR_FAIL;
1803 LOG_DEBUG("no start of SFDP header even after %u dummy bytes", count);
1806 /* Abort operation */
1807 retval = target_write_u32(target, io_base + SPI_CR,
1808 READ_REG(SPI_CR) | BIT(SPI_ABORT));
1813 /* Read SFDP parameter block */
1814 static int read_sfdp_block(struct flash_bank *bank, uint32_t addr,
1815 uint32_t words, uint32_t *buffer)
1817 struct target *target = bank->target;
1818 struct stmqspi_flash_bank *stmqspi_info = bank->driver_priv;
1819 uint32_t io_base = stmqspi_info->io_base;
1820 bool flash1 = !(stmqspi_info->saved_cr & BIT(SPI_FSEL_FLASH));
1821 unsigned int dual, count, len, *dummy;
1824 dual = (stmqspi_info->saved_cr & BIT(SPI_DUAL_FLASH)) ? 1 : 0;
1826 if (IS_OCTOSPI && (((stmqspi_info->saved_ccr >> SPI_DMODE_POS) & 0x7) > 3)) {
1827 /* in OCTO mode 4-byte address and (yet) unknown number of dummy clocks */
1830 /* in octo mode, use sfdp_dummy1 only */
1831 dummy = &stmqspi_info->sfdp_dummy1;
1833 retval = find_sfdp_dummy(bank, len);
1834 if (retval != ERROR_OK)
1838 /* in all other modes 3-byte-address and 8(?) dummy clocks */
1841 /* use sfdp_dummy1/2 according to currently selected flash */
1842 dummy = (stmqspi_info->saved_cr & BIT(SPI_FSEL_FLASH)) ?
1843 &stmqspi_info->sfdp_dummy2 : &stmqspi_info->sfdp_dummy1;
1845 /* according to SFDP standard, there should always be 8 dummy *CLOCKS*
1846 * giving 1, 2 or 4 dummy *BYTES*, however, this is apparently not
1847 * always implemented correctly, so determine the number of dummy bytes
1850 retval = find_sfdp_dummy(bank, len);
1851 if (retval != ERROR_OK)
1856 LOG_DEBUG("%s: addr=0x%08" PRIx32 " words=0x%08" PRIx32 " dummy=%u",
1857 __func__, addr, words, *dummy);
1859 /* Abort any previous operation */
1860 retval = target_write_u32(target, io_base + SPI_CR,
1861 stmqspi_info->saved_cr | BIT(SPI_ABORT));
1862 if (retval != ERROR_OK)
1865 /* Wait for busy to be cleared */
1866 retval = poll_busy(bank, SPI_PROBE_TIMEOUT);
1867 if (retval != ERROR_OK)
1870 /* Switch to one flash only */
1871 retval = target_write_u32(target, io_base + SPI_CR, stmqspi_info->saved_cr);
1872 if (retval != ERROR_OK)
1875 /* Read that many words plus dummy bytes */
1876 retval = target_write_u32(target, io_base + SPI_DLR,
1877 ((*dummy + words * sizeof(uint32_t)) << dual) - 1);
1878 if (retval != ERROR_OK)
1881 /* Read SFDP block */
1883 retval = OCTOSPI_CMD(OCTOSPI_READ_MODE, OCTOSPI_CCR_READ_SFDP(len),
1884 SPIFLASH_READ_SFDP);
1886 retval = target_write_u32(target, io_base + QSPI_CCR, QSPI_CCR_READ_SFDP);
1887 if (retval != ERROR_OK)
1890 retval = target_write_u32(target, io_base + SPI_AR, addr << dual);
1891 if (retval != ERROR_OK)
1895 for (count = *dummy << dual; count > 0; --count) {
1896 retval = target_read_u8(target, io_base + SPI_DR, (uint8_t *)buffer);
1897 if (retval != ERROR_OK)
1901 for ( ; words > 0; words--) {
1903 uint32_t word1, word2;
1905 retval = target_read_u32(target, io_base + SPI_DR, &word1);
1906 if (retval != ERROR_OK)
1908 retval = target_read_u32(target, io_base + SPI_DR, &word2);
1909 if (retval != ERROR_OK)
1913 /* shift odd numbered bytes into even numbered ones */
1918 /* pack even numbered bytes into one word */
1919 *buffer = (word1 & 0xFFU) | ((word1 & 0xFF0000U) >> 8) |
1920 ((word2 & 0xFFU) << 16) | ((word2 & 0xFF0000U) << 8);
1924 retval = target_read_u32(target, io_base + SPI_DR, buffer);
1925 if (retval != ERROR_OK)
1928 LOG_DEBUG("raw SFDP data 0x%08" PRIx32, *buffer);
1930 /* endian correction, sfdp data is always le uint32_t based */
1931 *buffer = le_to_h_u32((uint8_t *)buffer);
1939 /* Return ID of flash device(s) */
1940 /* On exit, indirect mode is kept */
1941 static int read_flash_id(struct flash_bank *bank, uint32_t *id1, uint32_t *id2)
1943 struct target *target = bank->target;
1944 struct stmqspi_flash_bank *stmqspi_info = bank->driver_priv;
1945 uint32_t io_base = stmqspi_info->io_base;
1947 unsigned int type, count, len1, len2;
1950 /* invalidate both ids */
1954 if (target->state != TARGET_HALTED) {
1955 LOG_ERROR("Target not halted");
1956 return ERROR_TARGET_NOT_HALTED;
1959 /* SPIFLASH_READ_MID causes device in octal mode to go berserk, so don't use in this case */
1960 for (type = (IS_OCTOSPI && OPI_MODE) ? 1 : 0; type < 2 ; type++) {
1961 /* Abort any previous operation */
1962 retval = target_write_u32(target, io_base + SPI_CR,
1963 READ_REG(SPI_CR) | BIT(SPI_ABORT));
1964 if (retval != ERROR_OK)
1968 retval = wait_till_ready(bank, SPI_PROBE_TIMEOUT);
1969 if (retval != ERROR_OK)
1972 /* Wait for busy to be cleared */
1973 retval = poll_busy(bank, SPI_PROBE_TIMEOUT);
1974 if (retval != ERROR_OK)
1977 /* Read at most 16 bytes per chip */
1979 retval = target_write_u32(target, io_base + SPI_DLR,
1980 (stmqspi_info->saved_cr & BIT(SPI_DUAL_FLASH) ? count * 2 : count) - 1);
1981 if (retval != ERROR_OK)
1984 /* Read id: one particular flash chip (N25Q128) switches back to SPI mode when receiving
1985 * SPI_FLASH_READ_ID in QPI mode, hence try SPIFLASH_READ_MID first */
1989 retval = OCTOSPI_CMD(OCTOSPI_READ_MODE, OCTOSPI_CCR_READ_MID, SPIFLASH_READ_MID);
1991 retval = target_write_u32(target, io_base + QSPI_CCR, QSPI_CCR_READ_MID);
1996 retval = OCTOSPI_CMD(OCTOSPI_READ_MODE, OCTOSPI_CCR_READ_ID, SPIFLASH_READ_ID);
1998 retval = target_write_u32(target, io_base + QSPI_CCR, QSPI_CCR_READ_ID);
2005 if (retval != ERROR_OK)
2008 /* Dummy address 0, only required for 8-line mode */
2009 if (IS_OCTOSPI && OPI_MODE) {
2010 retval = target_write_u32(target, io_base + SPI_AR, 0);
2011 if (retval != ERROR_OK)
2015 /* for debugging only */
2016 (void)READ_REG(SPI_SR);
2018 /* Read ID from Data Register */
2019 for (len1 = 0, len2 = 0; count > 0; --count) {
2020 if ((stmqspi_info->saved_cr & (BIT(SPI_DUAL_FLASH) |
2021 BIT(SPI_FSEL_FLASH))) != BIT(SPI_FSEL_FLASH)) {
2022 retval = target_read_u8(target, io_base + SPI_DR, &byte);
2023 if (retval != ERROR_OK)
2025 /* collect 3 bytes without continuation codes */
2026 if ((byte != 0x7F) && (len1 < 3)) {
2027 *id1 = (*id1 >> 8) | ((uint32_t)byte) << 16;
2031 if ((stmqspi_info->saved_cr & (BIT(SPI_DUAL_FLASH) |
2032 BIT(SPI_FSEL_FLASH))) != 0) {
2033 retval = target_read_u8(target, io_base + SPI_DR, &byte);
2034 if (retval != ERROR_OK)
2036 /* collect 3 bytes without continuation codes */
2037 if ((byte != 0x7F) && (len2 < 3)) {
2038 *id2 = (*id2 >> 8) | ((uint32_t)byte) << 16;
2044 if (((*id1 != 0x000000) && (*id1 != 0xFFFFFF)) ||
2045 ((*id2 != 0x000000) && (*id2 != 0xFFFFFF)))
2049 if ((stmqspi_info->saved_cr & (BIT(SPI_DUAL_FLASH) |
2050 BIT(SPI_FSEL_FLASH))) != BIT(SPI_FSEL_FLASH)) {
2051 if ((*id1 == 0x000000) || (*id1 == 0xFFFFFF)) {
2052 /* no id retrieved, so id must be set manually */
2053 LOG_INFO("No id from flash1");
2054 retval = ERROR_FLASH_BANK_NOT_PROBED;
2058 if ((stmqspi_info->saved_cr & (BIT(SPI_DUAL_FLASH) | BIT(SPI_FSEL_FLASH))) != 0) {
2059 if ((*id2 == 0x000000) || (*id2 == 0xFFFFFF)) {
2060 /* no id retrieved, so id must be set manually */
2061 LOG_INFO("No id from flash2");
2062 retval = ERROR_FLASH_BANK_NOT_PROBED;
2070 static int stmqspi_probe(struct flash_bank *bank)
2072 struct target *target = bank->target;
2073 struct stmqspi_flash_bank *stmqspi_info = bank->driver_priv;
2074 struct flash_sector *sectors = NULL;
2075 uint32_t io_base = stmqspi_info->io_base;
2076 uint32_t id1 = 0, id2 = 0, data = 0;
2077 const struct flash_device *p;
2078 const uint32_t magic = 0xAEF1510E;
2079 unsigned int dual, fsize;
2083 if (stmqspi_info->probed) {
2085 bank->num_sectors = 0;
2086 free(bank->sectors);
2087 bank->sectors = NULL;
2088 memset(&stmqspi_info->dev, 0, sizeof(stmqspi_info->dev));
2089 stmqspi_info->sfdp_dummy1 = 0;
2090 stmqspi_info->sfdp_dummy2 = 0;
2091 stmqspi_info->probed = false;
2094 /* Abort any previous operation */
2095 retval = target_write_u32(target, io_base + SPI_CR,
2096 READ_REG(SPI_CR) | BIT(SPI_ABORT));
2097 if (retval != ERROR_OK)
2100 /* Wait for busy to be cleared */
2101 retval = poll_busy(bank, SPI_PROBE_TIMEOUT);
2102 if (retval != ERROR_OK)
2105 /* check whether QSPI_ABR is writeable and readback returns the value written */
2106 retval = target_write_u32(target, io_base + QSPI_ABR, magic);
2107 if (retval == ERROR_OK) {
2108 retval = target_read_u32(target, io_base + QSPI_ABR, &data);
2109 retval = target_write_u32(target, io_base + QSPI_ABR, 0);
2112 if (data == magic) {
2113 LOG_DEBUG("QSPI_ABR register present");
2114 stmqspi_info->octo = false;
2115 } else if (READ_REG(OCTOSPI_MAGIC) == OCTO_MAGIC_ID) {
2116 LOG_DEBUG("OCTOSPI_MAGIC present");
2117 stmqspi_info->octo = true;
2119 LOG_ERROR("No QSPI, no OCTOSPI at 0x%08" PRIx32, io_base);
2120 stmqspi_info->probed = false;
2121 stmqspi_info->dev.name = "none";
2125 /* save current FSEL and DFM bits in QSPI/OCTOSPI_CR, current QSPI/OCTOSPI_CCR value */
2126 stmqspi_info->saved_cr = READ_REG(SPI_CR);
2127 if (retval == ERROR_OK)
2128 stmqspi_info->saved_ccr = READ_REG(SPI_CCR);
2133 mtyp = ((READ_REG(OCTOSPI_DCR1) & OCTOSPI_MTYP_MASK)) >> OCTOSPI_MTYP_POS;
2134 if (retval == ERROR_OK)
2135 stmqspi_info->saved_tcr = READ_REG(OCTOSPI_TCR);
2136 if (retval == ERROR_OK)
2137 stmqspi_info->saved_ir = READ_REG(OCTOSPI_IR);
2138 if ((mtyp != 0x0) && (mtyp != 0x1)) {
2139 retval = ERROR_FAIL;
2140 LOG_ERROR("Only regular SPI protocol supported in OCTOSPI");
2142 if (retval == ERROR_OK) {
2143 LOG_DEBUG("OCTOSPI at 0x%08" PRIx64 ", io_base at 0x%08" PRIx32 ", OCTOSPI_CR 0x%08"
2144 PRIx32 ", OCTOSPI_CCR 0x%08" PRIx32 ", %d-byte addr", bank->base, io_base,
2145 stmqspi_info->saved_cr, stmqspi_info->saved_ccr, SPI_ADSIZE);
2147 LOG_ERROR("No OCTOSPI at io_base 0x%08" PRIx32, io_base);
2148 stmqspi_info->probed = false;
2149 stmqspi_info->dev.name = "none";
2153 if (retval == ERROR_OK) {
2154 LOG_DEBUG("QSPI at 0x%08" PRIx64 ", io_base at 0x%08" PRIx32 ", QSPI_CR 0x%08"
2155 PRIx32 ", QSPI_CCR 0x%08" PRIx32 ", %d-byte addr", bank->base, io_base,
2156 stmqspi_info->saved_cr, stmqspi_info->saved_ccr, SPI_ADSIZE);
2157 if (stmqspi_info->saved_ccr & (1U << QSPI_DDRM))
2158 LOG_WARNING("DDR mode is untested and suffers from some silicon bugs");
2160 LOG_ERROR("No QSPI at io_base 0x%08" PRIx32, io_base);
2161 stmqspi_info->probed = false;
2162 stmqspi_info->dev.name = "none";
2167 dual = (stmqspi_info->saved_cr & BIT(SPI_DUAL_FLASH)) ? 1 : 0;
2168 octal_dtr = IS_OCTOSPI && (stmqspi_info->saved_ccr & BIT(OCTOSPI_DDTR));
2169 if (dual || octal_dtr)
2170 bank->write_start_alignment = bank->write_end_alignment = 2;
2172 bank->write_start_alignment = bank->write_end_alignment = 1;
2174 /* read and decode flash ID; returns in indirect mode */
2175 retval = read_flash_id(bank, &id1, &id2);
2176 LOG_DEBUG("id1 0x%06" PRIx32 ", id2 0x%06" PRIx32, id1, id2);
2177 if (retval == ERROR_FLASH_BANK_NOT_PROBED) {
2178 /* no id retrieved, so id must be set manually */
2179 LOG_INFO("No id - set flash parameters manually");
2184 if (retval != ERROR_OK)
2187 /* identify flash1 */
2188 for (p = flash_devices; id1 && p->name ; p++) {
2189 if (p->device_id == id1) {
2190 memcpy(&stmqspi_info->dev, p, sizeof(stmqspi_info->dev));
2191 if (p->size_in_bytes / 4096)
2192 LOG_INFO("flash1 \'%s\' id = 0x%06" PRIx32 " size = %" PRIu32
2193 "kbytes", p->name, id1, p->size_in_bytes / 1024);
2195 LOG_INFO("flash1 \'%s\' id = 0x%06" PRIx32 " size = %" PRIu32
2196 "bytes", p->name, id1, p->size_in_bytes);
2201 if (id1 && !p->name) {
2202 /* chip not been identified by id, then try SFDP */
2203 struct flash_device temp;
2204 uint32_t saved_cr = stmqspi_info->saved_cr;
2207 stmqspi_info->saved_cr = stmqspi_info->saved_cr & ~BIT(SPI_FSEL_FLASH);
2208 retval = spi_sfdp(bank, &temp, &read_sfdp_block);
2210 /* restore saved_cr */
2211 stmqspi_info->saved_cr = saved_cr;
2213 if (retval == ERROR_OK) {
2214 LOG_INFO("flash1 \'%s\' id = 0x%06" PRIx32 " size = %" PRIu32
2215 "kbytes", temp.name, id1, temp.size_in_bytes / 1024);
2216 /* save info and retrieved *good* id as spi_sfdp clears all info */
2217 memcpy(&stmqspi_info->dev, &temp, sizeof(stmqspi_info->dev));
2218 stmqspi_info->dev.device_id = id1;
2220 /* even not identified by SFDP, then give up */
2221 LOG_WARNING("Unknown flash1 device id = 0x%06" PRIx32
2222 " - set flash parameters manually", id1);
2228 /* identify flash2 */
2229 for (p = flash_devices; id2 && p->name ; p++) {
2230 if (p->device_id == id2) {
2231 if (p->size_in_bytes / 4096)
2232 LOG_INFO("flash2 \'%s\' id = 0x%06" PRIx32 " size = %" PRIu32
2233 "kbytes", p->name, id2, p->size_in_bytes / 1024);
2235 LOG_INFO("flash2 \'%s\' id = 0x%06" PRIx32 " size = %" PRIu32
2236 "bytes", p->name, id2, p->size_in_bytes);
2239 memcpy(&stmqspi_info->dev, p, sizeof(stmqspi_info->dev));
2241 if ((stmqspi_info->dev.read_cmd != p->read_cmd) ||
2242 (stmqspi_info->dev.qread_cmd != p->qread_cmd) ||
2243 (stmqspi_info->dev.pprog_cmd != p->pprog_cmd) ||
2244 (stmqspi_info->dev.erase_cmd != p->erase_cmd) ||
2245 (stmqspi_info->dev.chip_erase_cmd != p->chip_erase_cmd) ||
2246 (stmqspi_info->dev.sectorsize != p->sectorsize) ||
2247 (stmqspi_info->dev.size_in_bytes != p->size_in_bytes)) {
2248 LOG_ERROR("Incompatible flash1/flash2 devices");
2251 /* page size is optional in SFDP, so accept smallest value */
2252 if (p->pagesize < stmqspi_info->dev.pagesize)
2253 stmqspi_info->dev.pagesize = p->pagesize;
2259 if (id2 && !p->name) {
2260 /* chip not been identified by id, then try SFDP */
2261 struct flash_device temp;
2262 uint32_t saved_cr = stmqspi_info->saved_cr;
2265 stmqspi_info->saved_cr = stmqspi_info->saved_cr | BIT(SPI_FSEL_FLASH);
2266 retval = spi_sfdp(bank, &temp, &read_sfdp_block);
2268 /* restore saved_cr */
2269 stmqspi_info->saved_cr = saved_cr;
2271 if (retval == ERROR_OK)
2272 LOG_INFO("flash2 \'%s\' id = 0x%06" PRIx32 " size = %" PRIu32
2273 "kbytes", temp.name, id2, temp.size_in_bytes / 1024);
2275 /* even not identified by SFDP, then give up */
2276 LOG_WARNING("Unknown flash2 device id = 0x%06" PRIx32
2277 " - set flash parameters manually", id2);
2283 memcpy(&stmqspi_info->dev, &temp, sizeof(stmqspi_info->dev));
2285 if ((stmqspi_info->dev.read_cmd != temp.read_cmd) ||
2286 (stmqspi_info->dev.qread_cmd != temp.qread_cmd) ||
2287 (stmqspi_info->dev.pprog_cmd != temp.pprog_cmd) ||
2288 (stmqspi_info->dev.erase_cmd != temp.erase_cmd) ||
2289 (stmqspi_info->dev.chip_erase_cmd != temp.chip_erase_cmd) ||
2290 (stmqspi_info->dev.sectorsize != temp.sectorsize) ||
2291 (stmqspi_info->dev.size_in_bytes != temp.size_in_bytes)) {
2292 LOG_ERROR("Incompatible flash1/flash2 devices");
2295 /* page size is optional in SFDP, so accept smallest value */
2296 if (temp.pagesize < stmqspi_info->dev.pagesize)
2297 stmqspi_info->dev.pagesize = temp.pagesize;
2301 /* Set correct size value */
2302 bank->size = stmqspi_info->dev.size_in_bytes << dual;
2304 fsize = ((READ_REG(SPI_DCR) >> SPI_FSIZE_POS) & (BIT(SPI_FSIZE_LEN) - 1));
2305 if (retval != ERROR_OK)
2308 LOG_DEBUG("FSIZE = 0x%04x", fsize);
2309 if (bank->size == BIT((fsize + 1)))
2310 LOG_DEBUG("FSIZE in DCR(1) matches actual capacity. Beware of silicon bug in H7, L4+, MP1.");
2311 else if (bank->size == BIT((fsize + 0)))
2312 LOG_DEBUG("FSIZE in DCR(1) is off by one regarding actual capacity. Fix for silicon bug?");
2314 LOG_ERROR("FSIZE in DCR(1) doesn't match actual capacity.");
2316 /* if no sectors, then treat whole flash as single sector */
2317 if (stmqspi_info->dev.sectorsize == 0)
2318 stmqspi_info->dev.sectorsize = stmqspi_info->dev.size_in_bytes;
2319 /* if no page_size, then use sectorsize as page_size */
2320 if (stmqspi_info->dev.pagesize == 0)
2321 stmqspi_info->dev.pagesize = stmqspi_info->dev.sectorsize;
2323 /* create and fill sectors array */
2324 bank->num_sectors = stmqspi_info->dev.size_in_bytes / stmqspi_info->dev.sectorsize;
2325 sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
2326 if (sectors == NULL) {
2327 LOG_ERROR("not enough memory");
2328 retval = ERROR_FAIL;
2332 for (unsigned int sector = 0; sector < bank->num_sectors; sector++) {
2333 sectors[sector].offset = sector * (stmqspi_info->dev.sectorsize << dual);
2334 sectors[sector].size = (stmqspi_info->dev.sectorsize << dual);
2335 sectors[sector].is_erased = -1;
2336 sectors[sector].is_protected = 0;
2339 bank->sectors = sectors;
2340 stmqspi_info->probed = true;
2343 /* Switch to memory mapped mode before return to prompt */
2349 static int stmqspi_auto_probe(struct flash_bank *bank)
2351 struct stmqspi_flash_bank *stmqspi_info = bank->driver_priv;
2353 if (stmqspi_info->probed)
2355 stmqspi_probe(bank);
2359 static int stmqspi_protect_check(struct flash_bank *bank)
2361 /* Nothing to do. Protection is only handled in SW. */
2365 static int get_stmqspi_info(struct flash_bank *bank, char *buf, int buf_size)
2367 struct stmqspi_flash_bank *stmqspi_info = bank->driver_priv;
2369 if (!(stmqspi_info->probed)) {
2370 snprintf(buf, buf_size,
2371 "\nQSPI flash bank not probed yet\n");
2372 return ERROR_FLASH_BANK_NOT_PROBED;
2375 snprintf(buf, buf_size, "flash%s%s \'%s\', device id = 0x%06" PRIx32
2376 ", flash size = %" PRIu32 "%sbytes\n(page size = %" PRIu32
2377 ", read = 0x%02" PRIx8 ", qread = 0x%02" PRIx8
2378 ", pprog = 0x%02" PRIx8 ", mass_erase = 0x%02" PRIx8
2379 ", sector size = %" PRIu32 "%sbytes, sector_erase = 0x%02" PRIx8 ")",
2380 ((stmqspi_info->saved_cr & (BIT(SPI_DUAL_FLASH) |
2381 BIT(SPI_FSEL_FLASH))) != BIT(SPI_FSEL_FLASH)) ? "1" : "",
2382 ((stmqspi_info->saved_cr & (BIT(SPI_DUAL_FLASH) |
2383 BIT(SPI_FSEL_FLASH))) != 0) ? "2" : "",
2384 stmqspi_info->dev.name, stmqspi_info->dev.device_id,
2385 bank->size / 4096 ? bank->size / 1024 : bank->size,
2386 bank->size / 4096 ? "k" : "", stmqspi_info->dev.pagesize,
2387 stmqspi_info->dev.read_cmd, stmqspi_info->dev.qread_cmd,
2388 stmqspi_info->dev.pprog_cmd, stmqspi_info->dev.chip_erase_cmd,
2389 stmqspi_info->dev.sectorsize / 4096 ?
2390 stmqspi_info->dev.sectorsize / 1024 : stmqspi_info->dev.sectorsize,
2391 stmqspi_info->dev.sectorsize / 4096 ? "k" : "",
2392 stmqspi_info->dev.erase_cmd);
2397 static const struct command_registration stmqspi_exec_command_handlers[] = {
2399 .name = "mass_erase",
2400 .handler = stmqspi_handle_mass_erase_command,
2401 .mode = COMMAND_EXEC,
2403 .help = "Mass erase entire flash device.",
2407 .handler = stmqspi_handle_set,
2408 .mode = COMMAND_EXEC,
2409 .usage = "bank_id name chip_size page_size read_cmd qread_cmd pprg_cmd "
2410 "[ mass_erase_cmd ] [ sector_size sector_erase_cmd ]",
2411 .help = "Set params of single flash chip",
2415 .handler = stmqspi_handle_cmd,
2416 .mode = COMMAND_EXEC,
2417 .usage = "bank_id num_resp cmd_byte ...",
2418 .help = "Send low-level command cmd_byte and following bytes or read num_resp.",
2420 COMMAND_REGISTRATION_DONE
2423 static const struct command_registration stmqspi_command_handlers[] = {
2426 .mode = COMMAND_ANY,
2427 .help = "stmqspi flash command group",
2429 .chain = stmqspi_exec_command_handlers,
2431 COMMAND_REGISTRATION_DONE
2434 struct flash_driver stmqspi_flash = {
2436 .commands = stmqspi_command_handlers,
2437 .flash_bank_command = stmqspi_flash_bank_command,
2438 .erase = stmqspi_erase,
2439 .protect = stmqspi_protect,
2440 .write = stmqspi_write,
2441 .read = stmqspi_read,
2442 .verify = stmqspi_verify,
2443 .probe = stmqspi_probe,
2444 .auto_probe = stmqspi_auto_probe,
2445 .erase_check = stmqspi_blank_check,
2446 .protect_check = stmqspi_protect_check,
2447 .info = get_stmqspi_info,
2448 .free_driver_priv = default_flash_free_driver_priv,