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