blob: 0719fe845ca96f6812bc57f6c913b5a097da304b [file] [log] [blame]
Vignesh R3f5fb8b2019-02-05 11:29:25 +05301// SPDX-License-Identifier: GPL-2.0
2/*
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
5 *
6 * Copyright (C) 2005, Intec Automation Inc.
7 * Copyright (C) 2014, Freescale Semiconductor, Inc.
8 *
9 * Synced from Linux v4.19
10 */
11
12#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070014#include <dm/device_compat.h>
Vignesh R3f5fb8b2019-02-05 11:29:25 +053015#include <linux/err.h>
16#include <linux/errno.h>
17#include <linux/log2.h>
18#include <linux/math64.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060019#include <linux/printk.h>
Vignesh R3f5fb8b2019-02-05 11:29:25 +053020#include <linux/sizes.h>
21
22#include <linux/mtd/mtd.h>
23#include <linux/mtd/spi-nor.h>
24#include <spi-mem.h>
25#include <spi.h>
26
27#include "sf_internal.h"
28
29/* Define max times to check status register before we give up. */
30
31/*
32 * For everything but full-chip erase; probably could be much smaller, but kept
33 * around for safety for now
34 */
35
36#define HZ CONFIG_SYS_HZ
37
38#define DEFAULT_READY_WAIT_JIFFIES (40UL * HZ)
39
40static int spi_nor_read_write_reg(struct spi_nor *nor, struct spi_mem_op
41 *op, void *buf)
42{
43 if (op->data.dir == SPI_MEM_DATA_IN)
44 op->data.buf.in = buf;
45 else
46 op->data.buf.out = buf;
47 return spi_mem_exec_op(nor->spi, op);
48}
49
50static int spi_nor_read_reg(struct spi_nor *nor, u8 code, u8 *val, int len)
51{
52 struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(code, 1),
53 SPI_MEM_OP_NO_ADDR,
54 SPI_MEM_OP_NO_DUMMY,
55 SPI_MEM_OP_DATA_IN(len, NULL, 1));
56 int ret;
57
58 ret = spi_nor_read_write_reg(nor, &op, val);
Sean Anderson9bdd34b2020-09-15 10:44:43 -040059 if (ret < 0) {
60 /*
61 * spi_slave does not have a struct udevice member without DM,
62 * so use the bus and cs instead.
63 */
64#if CONFIG_IS_ENABLED(DM_SPI)
65 dev_dbg(nor->spi->dev, "error %d reading %x\n", ret,
Vignesh R3f5fb8b2019-02-05 11:29:25 +053066 code);
Sean Anderson9bdd34b2020-09-15 10:44:43 -040067#else
68 log_debug("spi%u.%u: error %d reading %x\n",
69 nor->spi->bus, nor->spi->cs, ret, code);
70#endif
71 }
Vignesh R3f5fb8b2019-02-05 11:29:25 +053072
73 return ret;
74}
75
76static int spi_nor_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
77{
78 struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(opcode, 1),
79 SPI_MEM_OP_NO_ADDR,
80 SPI_MEM_OP_NO_DUMMY,
81 SPI_MEM_OP_DATA_OUT(len, NULL, 1));
82
83 return spi_nor_read_write_reg(nor, &op, buf);
84}
85
86static ssize_t spi_nor_read_data(struct spi_nor *nor, loff_t from, size_t len,
87 u_char *buf)
88{
89 struct spi_mem_op op =
90 SPI_MEM_OP(SPI_MEM_OP_CMD(nor->read_opcode, 1),
91 SPI_MEM_OP_ADDR(nor->addr_width, from, 1),
92 SPI_MEM_OP_DUMMY(nor->read_dummy, 1),
93 SPI_MEM_OP_DATA_IN(len, buf, 1));
94 size_t remaining = len;
95 int ret;
96
97 /* get transfer protocols. */
98 op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->read_proto);
99 op.addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->read_proto);
100 op.dummy.buswidth = op.addr.buswidth;
101 op.data.buswidth = spi_nor_get_protocol_data_nbits(nor->read_proto);
102
103 /* convert the dummy cycles to the number of bytes */
104 op.dummy.nbytes = (nor->read_dummy * op.dummy.buswidth) / 8;
105
106 while (remaining) {
107 op.data.nbytes = remaining < UINT_MAX ? remaining : UINT_MAX;
108 ret = spi_mem_adjust_op_size(nor->spi, &op);
109 if (ret)
110 return ret;
111
112 ret = spi_mem_exec_op(nor->spi, &op);
113 if (ret)
114 return ret;
115
116 op.addr.val += op.data.nbytes;
117 remaining -= op.data.nbytes;
118 op.data.buf.in += op.data.nbytes;
119 }
120
121 return len;
122}
123
124#if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
125/*
126 * Read configuration register, returning its value in the
127 * location. Return the configuration register value.
128 * Returns negative if error occurred.
129 */
130static int read_cr(struct spi_nor *nor)
131{
132 int ret;
133 u8 val;
134
135 ret = spi_nor_read_reg(nor, SPINOR_OP_RDCR, &val, 1);
136 if (ret < 0) {
137 dev_dbg(nor->dev, "error %d reading CR\n", ret);
138 return ret;
139 }
140
141 return val;
142}
143#endif
144
145/*
146 * Write status register 1 byte
147 * Returns negative if error occurred.
148 */
149static inline int write_sr(struct spi_nor *nor, u8 val)
150{
151 nor->cmd_buf[0] = val;
152 return spi_nor_write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 1);
153}
154
155/*
156 * Set write enable latch with Write Enable command.
157 * Returns negative if error occurred.
158 */
159static inline int write_enable(struct spi_nor *nor)
160{
161 return spi_nor_write_reg(nor, SPINOR_OP_WREN, NULL, 0);
162}
163
164/*
165 * Send write disable instruction to the chip.
166 */
167static inline int write_disable(struct spi_nor *nor)
168{
169 return spi_nor_write_reg(nor, SPINOR_OP_WRDI, NULL, 0);
170}
171
172static inline struct spi_nor *mtd_to_spi_nor(struct mtd_info *mtd)
173{
174 return mtd->priv;
175}
176
177static u8 spi_nor_convert_opcode(u8 opcode, const u8 table[][2], size_t size)
178{
179 size_t i;
180
181 for (i = 0; i < size; i++)
182 if (table[i][0] == opcode)
183 return table[i][1];
184
185 /* No conversion found, keep input op code. */
186 return opcode;
187}
188
189static inline u8 spi_nor_convert_3to4_read(u8 opcode)
190{
191 static const u8 spi_nor_3to4_read[][2] = {
192 { SPINOR_OP_READ, SPINOR_OP_READ_4B },
193 { SPINOR_OP_READ_FAST, SPINOR_OP_READ_FAST_4B },
194 { SPINOR_OP_READ_1_1_2, SPINOR_OP_READ_1_1_2_4B },
195 { SPINOR_OP_READ_1_2_2, SPINOR_OP_READ_1_2_2_4B },
196 { SPINOR_OP_READ_1_1_4, SPINOR_OP_READ_1_1_4_4B },
197 { SPINOR_OP_READ_1_4_4, SPINOR_OP_READ_1_4_4_4B },
198 };
199
200 return spi_nor_convert_opcode(opcode, spi_nor_3to4_read,
201 ARRAY_SIZE(spi_nor_3to4_read));
202}
203
204static void spi_nor_set_4byte_opcodes(struct spi_nor *nor,
205 const struct flash_info *info)
206{
207 nor->read_opcode = spi_nor_convert_3to4_read(nor->read_opcode);
208}
209
210/* Enable/disable 4-byte addressing mode. */
211static inline int set_4byte(struct spi_nor *nor, const struct flash_info *info,
212 int enable)
213{
214 int status;
215 bool need_wren = false;
216 u8 cmd;
217
218 switch (JEDEC_MFR(info)) {
219 case SNOR_MFR_ST:
220 case SNOR_MFR_MICRON:
221 /* Some Micron need WREN command; all will accept it */
222 need_wren = true;
223 case SNOR_MFR_MACRONIX:
224 case SNOR_MFR_WINBOND:
225 if (need_wren)
226 write_enable(nor);
227
228 cmd = enable ? SPINOR_OP_EN4B : SPINOR_OP_EX4B;
229 status = spi_nor_write_reg(nor, cmd, NULL, 0);
230 if (need_wren)
231 write_disable(nor);
232
233 if (!status && !enable &&
234 JEDEC_MFR(info) == SNOR_MFR_WINBOND) {
235 /*
236 * On Winbond W25Q256FV, leaving 4byte mode causes
237 * the Extended Address Register to be set to 1, so all
238 * 3-byte-address reads come from the second 16M.
239 * We must clear the register to enable normal behavior.
240 */
241 write_enable(nor);
242 nor->cmd_buf[0] = 0;
243 spi_nor_write_reg(nor, SPINOR_OP_WREAR,
244 nor->cmd_buf, 1);
245 write_disable(nor);
246 }
247
248 return status;
249 default:
250 /* Spansion style */
251 nor->cmd_buf[0] = enable << 7;
252 return spi_nor_write_reg(nor, SPINOR_OP_BRWR, nor->cmd_buf, 1);
253 }
254}
255
256#if defined(CONFIG_SPI_FLASH_SPANSION) || \
257 defined(CONFIG_SPI_FLASH_WINBOND) || \
258 defined(CONFIG_SPI_FLASH_MACRONIX)
259/*
260 * Read the status register, returning its value in the location
261 * Return the status register value.
262 * Returns negative if error occurred.
263 */
264static int read_sr(struct spi_nor *nor)
265{
266 int ret;
267 u8 val;
268
269 ret = spi_nor_read_reg(nor, SPINOR_OP_RDSR, &val, 1);
270 if (ret < 0) {
271 pr_debug("error %d reading SR\n", (int)ret);
272 return ret;
273 }
274
275 return val;
276}
277
278/*
279 * Read the flag status register, returning its value in the location
280 * Return the status register value.
281 * Returns negative if error occurred.
282 */
283static int read_fsr(struct spi_nor *nor)
284{
285 int ret;
286 u8 val;
287
288 ret = spi_nor_read_reg(nor, SPINOR_OP_RDFSR, &val, 1);
289 if (ret < 0) {
290 pr_debug("error %d reading FSR\n", ret);
291 return ret;
292 }
293
294 return val;
295}
296
297static int spi_nor_sr_ready(struct spi_nor *nor)
298{
299 int sr = read_sr(nor);
300
301 if (sr < 0)
302 return sr;
303
304 return !(sr & SR_WIP);
305}
306
307static int spi_nor_fsr_ready(struct spi_nor *nor)
308{
309 int fsr = read_fsr(nor);
310
311 if (fsr < 0)
312 return fsr;
313 return fsr & FSR_READY;
314}
315
316static int spi_nor_ready(struct spi_nor *nor)
317{
318 int sr, fsr;
319
320 sr = spi_nor_sr_ready(nor);
321 if (sr < 0)
322 return sr;
323 fsr = nor->flags & SNOR_F_USE_FSR ? spi_nor_fsr_ready(nor) : 1;
324 if (fsr < 0)
325 return fsr;
326 return sr && fsr;
327}
328
329/*
330 * Service routine to read status register until ready, or timeout occurs.
331 * Returns non-zero if error.
332 */
333static int spi_nor_wait_till_ready_with_timeout(struct spi_nor *nor,
334 unsigned long timeout)
335{
336 unsigned long timebase;
337 int ret;
338
339 timebase = get_timer(0);
340
341 while (get_timer(timebase) < timeout) {
342 ret = spi_nor_ready(nor);
343 if (ret < 0)
344 return ret;
345 if (ret)
346 return 0;
347 }
348
349 dev_err(nor->dev, "flash operation timed out\n");
350
351 return -ETIMEDOUT;
352}
353
354static int spi_nor_wait_till_ready(struct spi_nor *nor)
355{
356 return spi_nor_wait_till_ready_with_timeout(nor,
357 DEFAULT_READY_WAIT_JIFFIES);
358}
359#endif /* CONFIG_SPI_FLASH_SPANSION */
360
361/*
362 * Erase an address range on the nor chip. The address range may extend
363 * one or more erase sectors. Return an error is there is a problem erasing.
364 */
Simon Glassdf97e142023-05-04 16:50:48 -0600365static int spi_nor_erase_tiny(struct mtd_info *mtd, struct erase_info *instr)
Vignesh R3f5fb8b2019-02-05 11:29:25 +0530366{
367 return -ENOTSUPP;
368}
369
370static const struct flash_info *spi_nor_read_id(struct spi_nor *nor)
371{
372 int tmp;
373 u8 id[SPI_NOR_MAX_ID_LEN];
374 const struct flash_info *info;
375
376 tmp = spi_nor_read_reg(nor, SPINOR_OP_RDID, id, SPI_NOR_MAX_ID_LEN);
377 if (tmp < 0) {
378 dev_dbg(nor->dev, "error %d reading JEDEC ID\n", tmp);
379 return ERR_PTR(tmp);
380 }
381
382 info = spi_nor_ids;
383 for (; info->sector_size != 0; info++) {
384 if (info->id_len) {
385 if (!memcmp(info->id, id, info->id_len))
386 return info;
387 }
388 }
389 dev_dbg(nor->dev, "unrecognized JEDEC id bytes: %02x, %02x, %02x\n",
390 id[0], id[1], id[2]);
Simon Glass03f31012020-07-19 10:15:32 -0600391 return ERR_PTR(-EMEDIUMTYPE);
Vignesh R3f5fb8b2019-02-05 11:29:25 +0530392}
393
Simon Glassdf97e142023-05-04 16:50:48 -0600394static int spi_nor_read_tiny(struct mtd_info *mtd, loff_t from, size_t len,
395 size_t *retlen, u_char *buf)
Vignesh R3f5fb8b2019-02-05 11:29:25 +0530396{
397 struct spi_nor *nor = mtd_to_spi_nor(mtd);
398 int ret;
399
400 dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len);
401
402 while (len) {
403 loff_t addr = from;
404
405 ret = spi_nor_read_data(nor, addr, len, buf);
406 if (ret == 0) {
407 /* We shouldn't see 0-length reads */
408 ret = -EIO;
409 goto read_err;
410 }
411 if (ret < 0)
412 goto read_err;
413
414 *retlen += ret;
415 buf += ret;
416 from += ret;
417 len -= ret;
418 }
419 ret = 0;
420
421read_err:
422 return ret;
423}
424
425/*
426 * Write an address range to the nor chip. Data must be written in
427 * FLASH_PAGESIZE chunks. The address range may be any size provided
428 * it is within the physical boundaries.
429 */
Simon Glassdf97e142023-05-04 16:50:48 -0600430static int spi_nor_write_tiny(struct mtd_info *mtd, loff_t to, size_t len,
431 size_t *retlen, const u_char *buf)
Vignesh R3f5fb8b2019-02-05 11:29:25 +0530432{
433 return -ENOTSUPP;
434}
435
436#ifdef CONFIG_SPI_FLASH_MACRONIX
437/**
438 * macronix_quad_enable() - set QE bit in Status Register.
439 * @nor: pointer to a 'struct spi_nor'
440 *
441 * Set the Quad Enable (QE) bit in the Status Register.
442 *
443 * bit 6 of the Status Register is the QE bit for Macronix like QSPI memories.
444 *
445 * Return: 0 on success, -errno otherwise.
446 */
447static int macronix_quad_enable(struct spi_nor *nor)
448{
449 int ret, val;
450
451 val = read_sr(nor);
452 if (val < 0)
453 return val;
454 if (val & SR_QUAD_EN_MX)
455 return 0;
456
457 write_enable(nor);
458
459 write_sr(nor, val | SR_QUAD_EN_MX);
460
461 ret = spi_nor_wait_till_ready(nor);
462 if (ret)
463 return ret;
464
465 ret = read_sr(nor);
466 if (!(ret > 0 && (ret & SR_QUAD_EN_MX))) {
467 dev_err(nor->dev, "Macronix Quad bit not set\n");
468 return -EINVAL;
469 }
470
471 return 0;
472}
473#endif
474
475#if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
476/*
477 * Write status Register and configuration register with 2 bytes
478 * The first byte will be written to the status register, while the
479 * second byte will be written to the configuration register.
480 * Return negative if error occurred.
481 */
482static int write_sr_cr(struct spi_nor *nor, u8 *sr_cr)
483{
484 int ret;
485
486 write_enable(nor);
487
488 ret = spi_nor_write_reg(nor, SPINOR_OP_WRSR, sr_cr, 2);
489 if (ret < 0) {
490 dev_dbg(nor->dev,
491 "error while writing configuration register\n");
492 return -EINVAL;
493 }
494
495 ret = spi_nor_wait_till_ready(nor);
496 if (ret) {
497 dev_dbg(nor->dev,
498 "timeout while writing configuration register\n");
499 return ret;
500 }
501
502 return 0;
503}
504
505/**
506 * spansion_read_cr_quad_enable() - set QE bit in Configuration Register.
507 * @nor: pointer to a 'struct spi_nor'
508 *
509 * Set the Quad Enable (QE) bit in the Configuration Register.
510 * This function should be used with QSPI memories supporting the Read
511 * Configuration Register (35h) instruction.
512 *
513 * bit 1 of the Configuration Register is the QE bit for Spansion like QSPI
514 * memories.
515 *
516 * Return: 0 on success, -errno otherwise.
517 */
518static int spansion_read_cr_quad_enable(struct spi_nor *nor)
519{
520 u8 sr_cr[2];
521 int ret;
522
523 /* Check current Quad Enable bit value. */
524 ret = read_cr(nor);
525 if (ret < 0) {
Sean Anderson9bdd34b2020-09-15 10:44:43 -0400526 dev_dbg(nor->dev,
527 "error while reading configuration register\n");
Vignesh R3f5fb8b2019-02-05 11:29:25 +0530528 return -EINVAL;
529 }
530
531 if (ret & CR_QUAD_EN_SPAN)
532 return 0;
533
534 sr_cr[1] = ret | CR_QUAD_EN_SPAN;
535
536 /* Keep the current value of the Status Register. */
537 ret = read_sr(nor);
538 if (ret < 0) {
Sean Anderson9bdd34b2020-09-15 10:44:43 -0400539 dev_dbg(nor->dev, "error while reading status register\n");
Vignesh R3f5fb8b2019-02-05 11:29:25 +0530540 return -EINVAL;
541 }
542 sr_cr[0] = ret;
543
544 ret = write_sr_cr(nor, sr_cr);
545 if (ret)
546 return ret;
547
548 /* Read back and check it. */
549 ret = read_cr(nor);
550 if (!(ret > 0 && (ret & CR_QUAD_EN_SPAN))) {
551 dev_dbg(nor->dev, "Spansion Quad bit not set\n");
552 return -EINVAL;
553 }
554
555 return 0;
556}
557#endif /* CONFIG_SPI_FLASH_SPANSION */
558
Vignesh R3f5fb8b2019-02-05 11:29:25 +0530559static void
560spi_nor_set_read_settings(struct spi_nor_read_command *read,
561 u8 num_mode_clocks,
562 u8 num_wait_states,
563 u8 opcode,
564 enum spi_nor_protocol proto)
565{
566 read->num_mode_clocks = num_mode_clocks;
567 read->num_wait_states = num_wait_states;
568 read->opcode = opcode;
569 read->proto = proto;
570}
571
572static int spi_nor_init_params(struct spi_nor *nor,
573 const struct flash_info *info,
574 struct spi_nor_flash_parameter *params)
575{
576 /* (Fast) Read settings. */
577 params->hwcaps.mask = SNOR_HWCAPS_READ;
578 spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ],
579 0, 0, SPINOR_OP_READ,
580 SNOR_PROTO_1_1_1);
581
582 if (!(info->flags & SPI_NOR_NO_FR)) {
583 params->hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
584 spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_FAST],
585 0, 8, SPINOR_OP_READ_FAST,
586 SNOR_PROTO_1_1_1);
Takahiro Kuwano712e52c2021-06-29 15:01:04 +0900587#ifdef CONFIG_SPI_FLASH_SPANSION
588 if (JEDEC_MFR(info) == SNOR_MFR_CYPRESS &&
589 (info->id[1] == 0x2a || info->id[1] == 0x2b))
590 /* 0x2a: S25HL (QSPI, 3.3V), 0x2b: S25HS (QSPI, 1.8V) */
591 params->reads[SNOR_CMD_READ_FAST].num_mode_clocks = 8;
592#endif
Vignesh R3f5fb8b2019-02-05 11:29:25 +0530593 }
594
595 if (info->flags & SPI_NOR_QUAD_READ) {
596 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4;
597 spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_1_1_4],
598 0, 8, SPINOR_OP_READ_1_1_4,
599 SNOR_PROTO_1_1_4);
600 }
601
602 return 0;
603}
604
605static int spi_nor_select_read(struct spi_nor *nor,
606 const struct spi_nor_flash_parameter *params,
607 u32 shared_hwcaps)
608{
609 int best_match = shared_hwcaps & SNOR_HWCAPS_READ_MASK;
610 int cmd;
611 const struct spi_nor_read_command *read;
612
613 if (best_match < 0)
614 return -EINVAL;
615
616 if (best_match & SNOR_HWCAPS_READ_1_1_4)
617 cmd = SNOR_CMD_READ_1_1_4;
618 else if (best_match & SNOR_HWCAPS_READ_FAST)
619 cmd = SNOR_CMD_READ_FAST;
620 else
621 cmd = SNOR_CMD_READ;
622
623 read = &params->reads[cmd];
624 nor->read_opcode = read->opcode;
625 nor->read_proto = read->proto;
626
627 /*
628 * In the spi-nor framework, we don't need to make the difference
629 * between mode clock cycles and wait state clock cycles.
630 * Indeed, the value of the mode clock cycles is used by a QSPI
631 * flash memory to know whether it should enter or leave its 0-4-4
632 * (Continuous Read / XIP) mode.
633 * eXecution In Place is out of the scope of the mtd sub-system.
634 * Hence we choose to merge both mode and wait state clock cycles
635 * into the so called dummy clock cycles.
636 */
637 nor->read_dummy = read->num_mode_clocks + read->num_wait_states;
638 return 0;
639}
640
641static int spi_nor_setup(struct spi_nor *nor, const struct flash_info *info,
642 const struct spi_nor_flash_parameter *params,
643 const struct spi_nor_hwcaps *hwcaps)
644{
645 u32 shared_mask;
646 int err;
647
648 /*
649 * Keep only the hardware capabilities supported by both the SPI
650 * controller and the SPI flash memory.
651 */
652 shared_mask = hwcaps->mask & params->hwcaps.mask;
653
654 /* Select the (Fast) Read command. */
655 err = spi_nor_select_read(nor, params, shared_mask);
656 if (err) {
657 dev_dbg(nor->dev,
658 "can't select read settings supported by both the SPI controller and memory.\n");
659 return err;
660 }
661
662 /* Enable Quad I/O if needed. */
663 if (spi_nor_get_protocol_width(nor->read_proto) == 4) {
664 switch (JEDEC_MFR(info)) {
665#ifdef CONFIG_SPI_FLASH_MACRONIX
666 case SNOR_MFR_MACRONIX:
667 err = macronix_quad_enable(nor);
668 break;
669#endif
670 case SNOR_MFR_ST:
671 case SNOR_MFR_MICRON:
672 break;
673
674 default:
675#if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
676 /* Kept only for backward compatibility purpose. */
677 err = spansion_read_cr_quad_enable(nor);
678#endif
679 break;
680 }
681 }
682 if (err) {
683 dev_dbg(nor->dev, "quad mode not supported\n");
684 return err;
685 }
686
687 return 0;
688}
689
690static int spi_nor_init(struct spi_nor *nor)
691{
692 if (nor->addr_width == 4 &&
693 (JEDEC_MFR(nor->info) != SNOR_MFR_SPANSION) &&
694 !(nor->info->flags & SPI_NOR_4B_OPCODES)) {
695 /*
696 * If the RESET# pin isn't hooked up properly, or the system
697 * otherwise doesn't perform a reset command in the boot
698 * sequence, it's impossible to 100% protect against unexpected
699 * reboots (e.g., crashes). Warn the user (or hopefully, system
700 * designer) that this is bad.
701 */
702 if (nor->flags & SNOR_F_BROKEN_RESET)
703 printf("enabling reset hack; may not recover from unexpected reboots\n");
704 set_4byte(nor, nor->info, 1);
705 }
706
707 return 0;
708}
709
710int spi_nor_scan(struct spi_nor *nor)
711{
712 struct spi_nor_flash_parameter params;
713 const struct flash_info *info = NULL;
714 struct mtd_info *mtd = &nor->mtd;
715 struct spi_nor_hwcaps hwcaps = {
716 .mask = SNOR_HWCAPS_READ |
717 SNOR_HWCAPS_READ_FAST
718 };
719 struct spi_slave *spi = nor->spi;
720 int ret;
721
722 /* Reset SPI protocol for all commands. */
723 nor->reg_proto = SNOR_PROTO_1_1_1;
724 nor->read_proto = SNOR_PROTO_1_1_1;
725 nor->write_proto = SNOR_PROTO_1_1_1;
726
727 if (spi->mode & SPI_RX_QUAD)
728 hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4;
729
730 info = spi_nor_read_id(nor);
731 if (IS_ERR_OR_NULL(info))
Simon Glass03f31012020-07-19 10:15:32 -0600732 return PTR_ERR(info);
Vignesh R3f5fb8b2019-02-05 11:29:25 +0530733 /* Parse the Serial Flash Discoverable Parameters table. */
734 ret = spi_nor_init_params(nor, info, &params);
735 if (ret)
736 return ret;
737
738 mtd->name = "spi-flash";
Marek BehĂșnd37eb232021-05-26 14:08:21 +0200739 mtd->dev = nor->dev;
Vignesh R3f5fb8b2019-02-05 11:29:25 +0530740 mtd->priv = nor;
741 mtd->type = MTD_NORFLASH;
742 mtd->writesize = 1;
743 mtd->flags = MTD_CAP_NORFLASH;
744 mtd->size = info->sector_size * info->n_sectors;
Simon Glassdf97e142023-05-04 16:50:48 -0600745 mtd->_erase = spi_nor_erase_tiny;
746 mtd->_read = spi_nor_read_tiny;
747 mtd->_write = spi_nor_write_tiny;
Vignesh R3f5fb8b2019-02-05 11:29:25 +0530748
749 nor->size = mtd->size;
750
751 if (info->flags & USE_FSR)
752 nor->flags |= SNOR_F_USE_FSR;
753 if (info->flags & USE_CLSR)
754 nor->flags |= SNOR_F_USE_CLSR;
755
756 if (info->flags & SPI_NOR_NO_FR)
757 params.hwcaps.mask &= ~SNOR_HWCAPS_READ_FAST;
758
759 /*
760 * Configure the SPI memory:
761 * - select op codes for (Fast) Read, Page Program and Sector Erase.
762 * - set the number of dummy cycles (mode cycles + wait states).
763 * - set the SPI protocols for register and memory accesses.
764 * - set the Quad Enable bit if needed (required by SPI x-y-4 protos).
765 */
766 ret = spi_nor_setup(nor, info, &params, &hwcaps);
767 if (ret)
768 return ret;
769
770 if (nor->addr_width) {
771 /* already configured from SFDP */
772 } else if (info->addr_width) {
773 nor->addr_width = info->addr_width;
774 } else if (mtd->size > 0x1000000) {
775 /* enable 4-byte addressing if the device exceeds 16MiB */
776 nor->addr_width = 4;
777 if (JEDEC_MFR(info) == SNOR_MFR_SPANSION ||
778 info->flags & SPI_NOR_4B_OPCODES)
779 spi_nor_set_4byte_opcodes(nor, info);
780 } else {
781 nor->addr_width = 3;
782 }
783
784 if (nor->addr_width > SPI_NOR_MAX_ADDR_WIDTH) {
Sean Anderson9bdd34b2020-09-15 10:44:43 -0400785 dev_dbg(nor->dev, "address width is too large: %u\n",
Vignesh R3f5fb8b2019-02-05 11:29:25 +0530786 nor->addr_width);
787 return -EINVAL;
788 }
789
790 /* Send all the required SPI flash commands to initialize device */
791 nor->info = info;
792 ret = spi_nor_init(nor);
793 if (ret)
794 return ret;
795
796 return 0;
797}
Simon Glass61d04642021-03-15 18:11:17 +1300798
799/* U-Boot specific functions, need to extend MTD to support these */
800int spi_flash_cmd_get_sw_write_prot(struct spi_nor *nor)
801{
802 return -ENOTSUPP;
803}