1 // SPDX-License-Identifier: GPL-2.0
3 * Based on m25p80.c, by Mike Lavender (mike@steroidmicros.com), with
4 * influence from lart.c (Abraham Van Der Merwe) and mtd_dataflash.c
6 * Copyright (C) 2005, Intec Automation Inc.
7 * Copyright (C) 2014, Freescale Semiconductor, Inc.
9 * Synced from Linux v4.19
14 #include <dm/device_compat.h>
15 #include <linux/err.h>
16 #include <linux/errno.h>
17 #include <linux/log2.h>
18 #include <linux/math64.h>
19 #include <linux/sizes.h>
21 #include <linux/mtd/mtd.h>
22 #include <linux/mtd/spi-nor.h>
26 #include "sf_internal.h"
28 /* Define max times to check status register before we give up. */
31 * For everything but full-chip erase; probably could be much smaller, but kept
32 * around for safety for now
35 #define HZ CONFIG_SYS_HZ
37 #define DEFAULT_READY_WAIT_JIFFIES (40UL * HZ)
39 static int spi_nor_read_write_reg(struct spi_nor *nor, struct spi_mem_op
42 if (op->data.dir == SPI_MEM_DATA_IN)
43 op->data.buf.in = buf;
45 op->data.buf.out = buf;
46 return spi_mem_exec_op(nor->spi, op);
49 static int spi_nor_read_reg(struct spi_nor *nor, u8 code, u8 *val, int len)
51 struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(code, 1),
54 SPI_MEM_OP_DATA_IN(len, NULL, 1));
57 ret = spi_nor_read_write_reg(nor, &op, val);
60 * spi_slave does not have a struct udevice member without DM,
61 * so use the bus and cs instead.
63 #if CONFIG_IS_ENABLED(DM_SPI)
64 dev_dbg(nor->spi->dev, "error %d reading %x\n", ret,
67 log_debug("spi%u.%u: error %d reading %x\n",
68 nor->spi->bus, nor->spi->cs, ret, code);
75 static int spi_nor_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
77 struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(opcode, 1),
80 SPI_MEM_OP_DATA_OUT(len, NULL, 1));
82 return spi_nor_read_write_reg(nor, &op, buf);
85 static ssize_t spi_nor_read_data(struct spi_nor *nor, loff_t from, size_t len,
88 struct spi_mem_op op =
89 SPI_MEM_OP(SPI_MEM_OP_CMD(nor->read_opcode, 1),
90 SPI_MEM_OP_ADDR(nor->addr_width, from, 1),
91 SPI_MEM_OP_DUMMY(nor->read_dummy, 1),
92 SPI_MEM_OP_DATA_IN(len, buf, 1));
93 size_t remaining = len;
96 /* get transfer protocols. */
97 op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->read_proto);
98 op.addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->read_proto);
99 op.dummy.buswidth = op.addr.buswidth;
100 op.data.buswidth = spi_nor_get_protocol_data_nbits(nor->read_proto);
102 /* convert the dummy cycles to the number of bytes */
103 op.dummy.nbytes = (nor->read_dummy * op.dummy.buswidth) / 8;
106 op.data.nbytes = remaining < UINT_MAX ? remaining : UINT_MAX;
107 ret = spi_mem_adjust_op_size(nor->spi, &op);
111 ret = spi_mem_exec_op(nor->spi, &op);
115 op.addr.val += op.data.nbytes;
116 remaining -= op.data.nbytes;
117 op.data.buf.in += op.data.nbytes;
123 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
125 * Read configuration register, returning its value in the
126 * location. Return the configuration register value.
127 * Returns negative if error occurred.
129 static int read_cr(struct spi_nor *nor)
134 ret = spi_nor_read_reg(nor, SPINOR_OP_RDCR, &val, 1);
136 dev_dbg(nor->dev, "error %d reading CR\n", ret);
145 * Write status register 1 byte
146 * Returns negative if error occurred.
148 static inline int write_sr(struct spi_nor *nor, u8 val)
150 nor->cmd_buf[0] = val;
151 return spi_nor_write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 1);
155 * Set write enable latch with Write Enable command.
156 * Returns negative if error occurred.
158 static inline int write_enable(struct spi_nor *nor)
160 return spi_nor_write_reg(nor, SPINOR_OP_WREN, NULL, 0);
164 * Send write disable instruction to the chip.
166 static inline int write_disable(struct spi_nor *nor)
168 return spi_nor_write_reg(nor, SPINOR_OP_WRDI, NULL, 0);
171 static inline struct spi_nor *mtd_to_spi_nor(struct mtd_info *mtd)
176 static u8 spi_nor_convert_opcode(u8 opcode, const u8 table[][2], size_t size)
180 for (i = 0; i < size; i++)
181 if (table[i][0] == opcode)
184 /* No conversion found, keep input op code. */
188 static inline u8 spi_nor_convert_3to4_read(u8 opcode)
190 static const u8 spi_nor_3to4_read[][2] = {
191 { SPINOR_OP_READ, SPINOR_OP_READ_4B },
192 { SPINOR_OP_READ_FAST, SPINOR_OP_READ_FAST_4B },
193 { SPINOR_OP_READ_1_1_2, SPINOR_OP_READ_1_1_2_4B },
194 { SPINOR_OP_READ_1_2_2, SPINOR_OP_READ_1_2_2_4B },
195 { SPINOR_OP_READ_1_1_4, SPINOR_OP_READ_1_1_4_4B },
196 { SPINOR_OP_READ_1_4_4, SPINOR_OP_READ_1_4_4_4B },
199 return spi_nor_convert_opcode(opcode, spi_nor_3to4_read,
200 ARRAY_SIZE(spi_nor_3to4_read));
203 static void spi_nor_set_4byte_opcodes(struct spi_nor *nor,
204 const struct flash_info *info)
206 nor->read_opcode = spi_nor_convert_3to4_read(nor->read_opcode);
209 /* Enable/disable 4-byte addressing mode. */
210 static inline int set_4byte(struct spi_nor *nor, const struct flash_info *info,
214 bool need_wren = false;
217 switch (JEDEC_MFR(info)) {
219 case SNOR_MFR_MICRON:
220 /* Some Micron need WREN command; all will accept it */
222 case SNOR_MFR_MACRONIX:
223 case SNOR_MFR_WINBOND:
227 cmd = enable ? SPINOR_OP_EN4B : SPINOR_OP_EX4B;
228 status = spi_nor_write_reg(nor, cmd, NULL, 0);
232 if (!status && !enable &&
233 JEDEC_MFR(info) == SNOR_MFR_WINBOND) {
235 * On Winbond W25Q256FV, leaving 4byte mode causes
236 * the Extended Address Register to be set to 1, so all
237 * 3-byte-address reads come from the second 16M.
238 * We must clear the register to enable normal behavior.
242 spi_nor_write_reg(nor, SPINOR_OP_WREAR,
250 nor->cmd_buf[0] = enable << 7;
251 return spi_nor_write_reg(nor, SPINOR_OP_BRWR, nor->cmd_buf, 1);
255 #if defined(CONFIG_SPI_FLASH_SPANSION) || \
256 defined(CONFIG_SPI_FLASH_WINBOND) || \
257 defined(CONFIG_SPI_FLASH_MACRONIX)
259 * Read the status register, returning its value in the location
260 * Return the status register value.
261 * Returns negative if error occurred.
263 static int read_sr(struct spi_nor *nor)
268 ret = spi_nor_read_reg(nor, SPINOR_OP_RDSR, &val, 1);
270 pr_debug("error %d reading SR\n", (int)ret);
278 * Read the flag status register, returning its value in the location
279 * Return the status register value.
280 * Returns negative if error occurred.
282 static int read_fsr(struct spi_nor *nor)
287 ret = spi_nor_read_reg(nor, SPINOR_OP_RDFSR, &val, 1);
289 pr_debug("error %d reading FSR\n", ret);
296 static int spi_nor_sr_ready(struct spi_nor *nor)
298 int sr = read_sr(nor);
303 return !(sr & SR_WIP);
306 static int spi_nor_fsr_ready(struct spi_nor *nor)
308 int fsr = read_fsr(nor);
312 return fsr & FSR_READY;
315 static int spi_nor_ready(struct spi_nor *nor)
319 sr = spi_nor_sr_ready(nor);
322 fsr = nor->flags & SNOR_F_USE_FSR ? spi_nor_fsr_ready(nor) : 1;
329 * Service routine to read status register until ready, or timeout occurs.
330 * Returns non-zero if error.
332 static int spi_nor_wait_till_ready_with_timeout(struct spi_nor *nor,
333 unsigned long timeout)
335 unsigned long timebase;
338 timebase = get_timer(0);
340 while (get_timer(timebase) < timeout) {
341 ret = spi_nor_ready(nor);
348 dev_err(nor->dev, "flash operation timed out\n");
353 static int spi_nor_wait_till_ready(struct spi_nor *nor)
355 return spi_nor_wait_till_ready_with_timeout(nor,
356 DEFAULT_READY_WAIT_JIFFIES);
358 #endif /* CONFIG_SPI_FLASH_SPANSION */
361 * Erase an address range on the nor chip. The address range may extend
362 * one or more erase sectors. Return an error is there is a problem erasing.
364 static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
369 static const struct flash_info *spi_nor_read_id(struct spi_nor *nor)
372 u8 id[SPI_NOR_MAX_ID_LEN];
373 const struct flash_info *info;
375 tmp = spi_nor_read_reg(nor, SPINOR_OP_RDID, id, SPI_NOR_MAX_ID_LEN);
377 dev_dbg(nor->dev, "error %d reading JEDEC ID\n", tmp);
382 for (; info->sector_size != 0; info++) {
384 if (!memcmp(info->id, id, info->id_len))
388 dev_dbg(nor->dev, "unrecognized JEDEC id bytes: %02x, %02x, %02x\n",
389 id[0], id[1], id[2]);
390 return ERR_PTR(-EMEDIUMTYPE);
393 static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
394 size_t *retlen, u_char *buf)
396 struct spi_nor *nor = mtd_to_spi_nor(mtd);
399 dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len);
404 ret = spi_nor_read_data(nor, addr, len, buf);
406 /* We shouldn't see 0-length reads */
425 * Write an address range to the nor chip. Data must be written in
426 * FLASH_PAGESIZE chunks. The address range may be any size provided
427 * it is within the physical boundaries.
429 static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
430 size_t *retlen, const u_char *buf)
435 #ifdef CONFIG_SPI_FLASH_MACRONIX
437 * macronix_quad_enable() - set QE bit in Status Register.
438 * @nor: pointer to a 'struct spi_nor'
440 * Set the Quad Enable (QE) bit in the Status Register.
442 * bit 6 of the Status Register is the QE bit for Macronix like QSPI memories.
444 * Return: 0 on success, -errno otherwise.
446 static int macronix_quad_enable(struct spi_nor *nor)
453 if (val & SR_QUAD_EN_MX)
458 write_sr(nor, val | SR_QUAD_EN_MX);
460 ret = spi_nor_wait_till_ready(nor);
465 if (!(ret > 0 && (ret & SR_QUAD_EN_MX))) {
466 dev_err(nor->dev, "Macronix Quad bit not set\n");
474 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
476 * Write status Register and configuration register with 2 bytes
477 * The first byte will be written to the status register, while the
478 * second byte will be written to the configuration register.
479 * Return negative if error occurred.
481 static int write_sr_cr(struct spi_nor *nor, u8 *sr_cr)
487 ret = spi_nor_write_reg(nor, SPINOR_OP_WRSR, sr_cr, 2);
490 "error while writing configuration register\n");
494 ret = spi_nor_wait_till_ready(nor);
497 "timeout while writing configuration register\n");
505 * spansion_read_cr_quad_enable() - set QE bit in Configuration Register.
506 * @nor: pointer to a 'struct spi_nor'
508 * Set the Quad Enable (QE) bit in the Configuration Register.
509 * This function should be used with QSPI memories supporting the Read
510 * Configuration Register (35h) instruction.
512 * bit 1 of the Configuration Register is the QE bit for Spansion like QSPI
515 * Return: 0 on success, -errno otherwise.
517 static int spansion_read_cr_quad_enable(struct spi_nor *nor)
522 /* Check current Quad Enable bit value. */
526 "error while reading configuration register\n");
530 if (ret & CR_QUAD_EN_SPAN)
533 sr_cr[1] = ret | CR_QUAD_EN_SPAN;
535 /* Keep the current value of the Status Register. */
538 dev_dbg(nor->dev, "error while reading status register\n");
543 ret = write_sr_cr(nor, sr_cr);
547 /* Read back and check it. */
549 if (!(ret > 0 && (ret & CR_QUAD_EN_SPAN))) {
550 dev_dbg(nor->dev, "Spansion Quad bit not set\n");
556 #endif /* CONFIG_SPI_FLASH_SPANSION */
559 spi_nor_set_read_settings(struct spi_nor_read_command *read,
563 enum spi_nor_protocol proto)
565 read->num_mode_clocks = num_mode_clocks;
566 read->num_wait_states = num_wait_states;
567 read->opcode = opcode;
571 static int spi_nor_init_params(struct spi_nor *nor,
572 const struct flash_info *info,
573 struct spi_nor_flash_parameter *params)
575 /* (Fast) Read settings. */
576 params->hwcaps.mask = SNOR_HWCAPS_READ;
577 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ],
578 0, 0, SPINOR_OP_READ,
581 if (!(info->flags & SPI_NOR_NO_FR)) {
582 params->hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
583 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_FAST],
584 0, 8, SPINOR_OP_READ_FAST,
586 #ifdef CONFIG_SPI_FLASH_SPANSION
587 if (JEDEC_MFR(info) == SNOR_MFR_CYPRESS &&
588 (info->id[1] == 0x2a || info->id[1] == 0x2b))
589 /* 0x2a: S25HL (QSPI, 3.3V), 0x2b: S25HS (QSPI, 1.8V) */
590 params->reads[SNOR_CMD_READ_FAST].num_mode_clocks = 8;
594 if (info->flags & SPI_NOR_QUAD_READ) {
595 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4;
596 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_1_1_4],
597 0, 8, SPINOR_OP_READ_1_1_4,
604 static int spi_nor_select_read(struct spi_nor *nor,
605 const struct spi_nor_flash_parameter *params,
608 int best_match = shared_hwcaps & SNOR_HWCAPS_READ_MASK;
610 const struct spi_nor_read_command *read;
615 if (best_match & SNOR_HWCAPS_READ_1_1_4)
616 cmd = SNOR_CMD_READ_1_1_4;
617 else if (best_match & SNOR_HWCAPS_READ_FAST)
618 cmd = SNOR_CMD_READ_FAST;
622 read = ¶ms->reads[cmd];
623 nor->read_opcode = read->opcode;
624 nor->read_proto = read->proto;
627 * In the spi-nor framework, we don't need to make the difference
628 * between mode clock cycles and wait state clock cycles.
629 * Indeed, the value of the mode clock cycles is used by a QSPI
630 * flash memory to know whether it should enter or leave its 0-4-4
631 * (Continuous Read / XIP) mode.
632 * eXecution In Place is out of the scope of the mtd sub-system.
633 * Hence we choose to merge both mode and wait state clock cycles
634 * into the so called dummy clock cycles.
636 nor->read_dummy = read->num_mode_clocks + read->num_wait_states;
640 static int spi_nor_setup(struct spi_nor *nor, const struct flash_info *info,
641 const struct spi_nor_flash_parameter *params,
642 const struct spi_nor_hwcaps *hwcaps)
648 * Keep only the hardware capabilities supported by both the SPI
649 * controller and the SPI flash memory.
651 shared_mask = hwcaps->mask & params->hwcaps.mask;
653 /* Select the (Fast) Read command. */
654 err = spi_nor_select_read(nor, params, shared_mask);
657 "can't select read settings supported by both the SPI controller and memory.\n");
661 /* Enable Quad I/O if needed. */
662 if (spi_nor_get_protocol_width(nor->read_proto) == 4) {
663 switch (JEDEC_MFR(info)) {
664 #ifdef CONFIG_SPI_FLASH_MACRONIX
665 case SNOR_MFR_MACRONIX:
666 err = macronix_quad_enable(nor);
670 case SNOR_MFR_MICRON:
674 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
675 /* Kept only for backward compatibility purpose. */
676 err = spansion_read_cr_quad_enable(nor);
682 dev_dbg(nor->dev, "quad mode not supported\n");
689 static int spi_nor_init(struct spi_nor *nor)
691 if (nor->addr_width == 4 &&
692 (JEDEC_MFR(nor->info) != SNOR_MFR_SPANSION) &&
693 !(nor->info->flags & SPI_NOR_4B_OPCODES)) {
695 * If the RESET# pin isn't hooked up properly, or the system
696 * otherwise doesn't perform a reset command in the boot
697 * sequence, it's impossible to 100% protect against unexpected
698 * reboots (e.g., crashes). Warn the user (or hopefully, system
699 * designer) that this is bad.
701 if (nor->flags & SNOR_F_BROKEN_RESET)
702 printf("enabling reset hack; may not recover from unexpected reboots\n");
703 set_4byte(nor, nor->info, 1);
709 int spi_nor_scan(struct spi_nor *nor)
711 struct spi_nor_flash_parameter params;
712 const struct flash_info *info = NULL;
713 struct mtd_info *mtd = &nor->mtd;
714 struct spi_nor_hwcaps hwcaps = {
715 .mask = SNOR_HWCAPS_READ |
716 SNOR_HWCAPS_READ_FAST
718 struct spi_slave *spi = nor->spi;
721 /* Reset SPI protocol for all commands. */
722 nor->reg_proto = SNOR_PROTO_1_1_1;
723 nor->read_proto = SNOR_PROTO_1_1_1;
724 nor->write_proto = SNOR_PROTO_1_1_1;
726 if (spi->mode & SPI_RX_QUAD)
727 hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4;
729 info = spi_nor_read_id(nor);
730 if (IS_ERR_OR_NULL(info))
731 return PTR_ERR(info);
732 /* Parse the Serial Flash Discoverable Parameters table. */
733 ret = spi_nor_init_params(nor, info, ¶ms);
737 mtd->name = "spi-flash";
740 mtd->type = MTD_NORFLASH;
742 mtd->flags = MTD_CAP_NORFLASH;
743 mtd->size = info->sector_size * info->n_sectors;
744 mtd->_erase = spi_nor_erase;
745 mtd->_read = spi_nor_read;
746 mtd->_write = spi_nor_write;
748 nor->size = mtd->size;
750 if (info->flags & USE_FSR)
751 nor->flags |= SNOR_F_USE_FSR;
752 if (info->flags & USE_CLSR)
753 nor->flags |= SNOR_F_USE_CLSR;
755 if (info->flags & SPI_NOR_NO_FR)
756 params.hwcaps.mask &= ~SNOR_HWCAPS_READ_FAST;
759 * Configure the SPI memory:
760 * - select op codes for (Fast) Read, Page Program and Sector Erase.
761 * - set the number of dummy cycles (mode cycles + wait states).
762 * - set the SPI protocols for register and memory accesses.
763 * - set the Quad Enable bit if needed (required by SPI x-y-4 protos).
765 ret = spi_nor_setup(nor, info, ¶ms, &hwcaps);
769 if (nor->addr_width) {
770 /* already configured from SFDP */
771 } else if (info->addr_width) {
772 nor->addr_width = info->addr_width;
773 } else if (mtd->size > 0x1000000) {
774 /* enable 4-byte addressing if the device exceeds 16MiB */
776 if (JEDEC_MFR(info) == SNOR_MFR_SPANSION ||
777 info->flags & SPI_NOR_4B_OPCODES)
778 spi_nor_set_4byte_opcodes(nor, info);
783 if (nor->addr_width > SPI_NOR_MAX_ADDR_WIDTH) {
784 dev_dbg(nor->dev, "address width is too large: %u\n",
789 /* Send all the required SPI flash commands to initialize device */
791 ret = spi_nor_init(nor);
798 /* U-Boot specific functions, need to extend MTD to support these */
799 int spi_flash_cmd_get_sw_write_prot(struct spi_nor *nor)