xref: /openbmc/u-boot/drivers/mtd/spi/spi-nor-core.c (revision 838fb1e6fdc6aac376ff9df9d82d7d7830d34adc)
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 <linux/err.h>
14 #include <linux/errno.h>
15 #include <linux/log2.h>
16 #include <linux/math64.h>
17 #include <linux/sizes.h>
18 
19 #include <linux/mtd/mtd.h>
20 #include <linux/mtd/spi-nor.h>
21 #include <spi-mem.h>
22 #include <spi.h>
23 
24 #include "sf_internal.h"
25 
26 /* Define max times to check status register before we give up. */
27 
28 /*
29  * For everything but full-chip erase; probably could be much smaller, but kept
30  * around for safety for now
31  */
32 
33 #define HZ					CONFIG_SYS_HZ
34 
35 #define DEFAULT_READY_WAIT_JIFFIES		(40UL * HZ)
36 
37 /**
38  * spi_nor_setup_op() - Set up common properties of a spi-mem op.
39  * @nor:		pointer to a 'struct spi_nor'
40  * @op:			pointer to the 'struct spi_mem_op' whose properties
41  *			need to be initialized.
42  * @proto:		the protocol from which the properties need to be set.
43  */
44 void spi_nor_setup_op(const struct spi_nor *nor,
45 		      struct spi_mem_op *op,
46 		      const enum spi_nor_protocol proto)
47 {
48 	op->cmd.buswidth = spi_nor_get_protocol_inst_nbits(proto);
49 
50 	if (op->addr.nbytes)
51 		op->addr.buswidth = spi_nor_get_protocol_addr_nbits(proto);
52 
53 	if (op->dummy.nbytes)
54 		op->dummy.buswidth = spi_nor_get_protocol_addr_nbits(proto);
55 
56 	if (op->data.nbytes)
57 		op->data.buswidth = spi_nor_get_protocol_data_nbits(proto);
58 }
59 
60 static int spi_nor_read_write_reg(struct spi_nor *nor, struct spi_mem_op
61 		*op, void *buf)
62 {
63 	if (op->data.dir == SPI_MEM_DATA_IN)
64 		op->data.buf.in = buf;
65 	else
66 		op->data.buf.out = buf;
67 	return spi_mem_exec_op(nor->spi, op);
68 }
69 
70 static int spi_nor_read_reg(struct spi_nor *nor, u8 code, u8 *val, int len)
71 {
72 	struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(code, 1),
73 					  SPI_MEM_OP_NO_ADDR,
74 					  SPI_MEM_OP_NO_DUMMY,
75 					  SPI_MEM_OP_DATA_IN(len, NULL, 1));
76 	int ret;
77 
78 	ret = spi_nor_read_write_reg(nor, &op, val);
79 	if (ret < 0)
80 		dev_dbg(&flash->spimem->spi->dev, "error %d reading %x\n", ret,
81 			code);
82 
83 	return ret;
84 }
85 
86 static int spi_nor_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
87 {
88 	struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(opcode, 1),
89 					  SPI_MEM_OP_NO_ADDR,
90 					  SPI_MEM_OP_NO_DUMMY,
91 					  SPI_MEM_OP_DATA_OUT(len, NULL, 1));
92 
93 	return spi_nor_read_write_reg(nor, &op, buf);
94 }
95 
96 #ifdef CONFIG_SPI_FLASH_SPANSION
97 static int spansion_read_any_reg(struct spi_nor *nor, u32 addr, u8 dummy,
98 				 u8 *val)
99 {
100 	int ret;
101 	u8 dummy_ori;
102 	struct spi_mem_op op =
103 			SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDAR, 1),
104 				   SPI_MEM_OP_ADDR(nor->addr_width, addr, 1),
105 				   SPI_MEM_OP_DUMMY(dummy / 8, 1),
106 				   SPI_MEM_OP_DATA_IN(1, NULL, 1));
107 
108 	dummy_ori = nor->read_dummy;
109 	nor->read_dummy = dummy;
110 
111 	ret = spi_nor_read_write_reg(nor, &op, val);
112 
113 	nor->read_dummy = dummy_ori;
114 
115 	return ret;
116 }
117 
118 static int spansion_write_any_reg(struct spi_nor *nor, u32 addr, u8 val)
119 {
120 	struct spi_mem_op op =
121 			SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WRAR, 1),
122 				   SPI_MEM_OP_ADDR(nor->addr_width, addr, 1),
123 				   SPI_MEM_OP_NO_DUMMY,
124 				   SPI_MEM_OP_DATA_OUT(1, NULL, 1));
125 
126 	return spi_nor_read_write_reg(nor, &op, &val);
127 }
128 #endif
129 
130 static ssize_t spi_nor_read_data(struct spi_nor *nor, loff_t from, size_t len,
131 				 u_char *buf)
132 {
133 	struct spi_mem_op op =
134 			SPI_MEM_OP(SPI_MEM_OP_CMD(nor->read_opcode, 1),
135 				   SPI_MEM_OP_ADDR(nor->addr_width, from, 1),
136 				   SPI_MEM_OP_DUMMY(nor->read_dummy, 1),
137 				   SPI_MEM_OP_DATA_IN(len, buf, 1));
138 	size_t remaining = len;
139 	int ret;
140 
141 	/* get transfer protocols. */
142 	op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->read_proto);
143 	op.addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->read_proto);
144 	op.dummy.buswidth = op.addr.buswidth;
145 	op.data.buswidth = spi_nor_get_protocol_data_nbits(nor->read_proto);
146 
147 	/* convert the dummy cycles to the number of bytes */
148 	op.dummy.nbytes = (nor->read_dummy * op.dummy.buswidth) / 8;
149 
150 	while (remaining) {
151 		op.data.nbytes = remaining < UINT_MAX ? remaining : UINT_MAX;
152 		ret = spi_mem_adjust_op_size(nor->spi, &op);
153 		if (ret)
154 			return ret;
155 
156 		ret = spi_mem_exec_op(nor->spi, &op);
157 		if (ret)
158 			return ret;
159 
160 		op.addr.val += op.data.nbytes;
161 		remaining -= op.data.nbytes;
162 		op.data.buf.in += op.data.nbytes;
163 	}
164 
165 	return len;
166 }
167 
168 static ssize_t spi_nor_write_data(struct spi_nor *nor, loff_t to, size_t len,
169 				  const u_char *buf)
170 {
171 	struct spi_mem_op op =
172 			SPI_MEM_OP(SPI_MEM_OP_CMD(nor->program_opcode, 1),
173 				   SPI_MEM_OP_ADDR(nor->addr_width, to, 1),
174 				   SPI_MEM_OP_NO_DUMMY,
175 				   SPI_MEM_OP_DATA_OUT(len, buf, 1));
176 	size_t remaining = len;
177 	int ret;
178 
179 	/* get transfer protocols. */
180 	op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->write_proto);
181 	op.addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->write_proto);
182 	op.data.buswidth = spi_nor_get_protocol_data_nbits(nor->write_proto);
183 
184 	if (nor->program_opcode == SPINOR_OP_AAI_WP && nor->sst_write_second)
185 		op.addr.nbytes = 0;
186 
187 	while (remaining) {
188 		op.data.nbytes = remaining < UINT_MAX ? remaining : UINT_MAX;
189 		ret = spi_mem_adjust_op_size(nor->spi, &op);
190 		if (ret)
191 			return ret;
192 
193 		ret = spi_mem_exec_op(nor->spi, &op);
194 		if (ret)
195 			return ret;
196 
197 		op.addr.val += op.data.nbytes;
198 		remaining -= op.data.nbytes;
199 		op.data.buf.out += op.data.nbytes;
200 	}
201 
202 	return len;
203 }
204 
205 /*
206  * Read the status register, returning its value in the location
207  * Return the status register value.
208  * Returns negative if error occurred.
209  */
210 static int read_sr(struct spi_nor *nor)
211 {
212 	int ret;
213 	u8 val;
214 
215 	ret = nor->read_reg(nor, SPINOR_OP_RDSR, &val, 1);
216 	if (ret < 0) {
217 		pr_debug("error %d reading SR\n", (int)ret);
218 		return ret;
219 	}
220 
221 	return val;
222 }
223 
224 static int read_winbond_sr2(struct spi_nor *nor)
225 {
226 	int ret;
227 	u8 val;
228 
229 	ret = nor->read_reg(nor, SPINOR_OP_WINBOND_RDSR2, &val, 1);
230 	if (ret < 0) {
231 		pr_debug("error %d reading SR2\n", (int)ret);
232 		return ret;
233 	}
234 
235 	return val;
236 }
237 
238 /*
239  * Read the flag status register, returning its value in the location
240  * Return the status register value.
241  * Returns negative if error occurred.
242  */
243 static int read_fsr(struct spi_nor *nor)
244 {
245 	int ret;
246 	u8 val;
247 
248 	ret = nor->read_reg(nor, SPINOR_OP_RDFSR, &val, 1);
249 	if (ret < 0) {
250 		pr_debug("error %d reading FSR\n", ret);
251 		return ret;
252 	}
253 
254 	return val;
255 }
256 
257 /*
258  * Read configuration register, returning its value in the
259  * location. Return the configuration register value.
260  * Returns negative if error occurred.
261  */
262 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
263 static int read_cr(struct spi_nor *nor)
264 {
265 	int ret;
266 	u8 val;
267 
268 	ret = nor->read_reg(nor, SPINOR_OP_RDCR, &val, 1);
269 	if (ret < 0) {
270 		dev_dbg(nor->dev, "error %d reading CR\n", ret);
271 		return ret;
272 	}
273 
274 	return val;
275 }
276 #endif
277 
278 /*
279  * Write status register 1 byte
280  * Returns negative if error occurred.
281  */
282 static int write_sr(struct spi_nor *nor, u8 val)
283 {
284 	nor->cmd_buf[0] = val;
285 	return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 1);
286 }
287 
288 static int write_winbond_sr2(struct spi_nor *nor, u8 val)
289 {
290 	nor->cmd_buf[0] = val;
291 	return nor->write_reg(nor, SPINOR_OP_WINBOND_WRSR2, nor->cmd_buf, 1);
292 }
293 
294 /*
295  * Set write enable latch with Write Enable command.
296  * Returns negative if error occurred.
297  */
298 static int write_enable(struct spi_nor *nor)
299 {
300 	return nor->write_reg(nor, SPINOR_OP_WREN, NULL, 0);
301 }
302 
303 /*
304  * Send write disable instruction to the chip.
305  */
306 static int write_disable(struct spi_nor *nor)
307 {
308 	return nor->write_reg(nor, SPINOR_OP_WRDI, NULL, 0);
309 }
310 
311 static struct spi_nor *mtd_to_spi_nor(struct mtd_info *mtd)
312 {
313 	return mtd->priv;
314 }
315 
316 #ifndef CONFIG_SPI_FLASH_BAR
317 static u8 spi_nor_convert_opcode(u8 opcode, const u8 table[][2], size_t size)
318 {
319 	size_t i;
320 
321 	for (i = 0; i < size; i++)
322 		if (table[i][0] == opcode)
323 			return table[i][1];
324 
325 	/* No conversion found, keep input op code. */
326 	return opcode;
327 }
328 
329 static u8 spi_nor_convert_3to4_read(u8 opcode)
330 {
331 	static const u8 spi_nor_3to4_read[][2] = {
332 		{ SPINOR_OP_READ,	SPINOR_OP_READ_4B },
333 		{ SPINOR_OP_READ_FAST,	SPINOR_OP_READ_FAST_4B },
334 		{ SPINOR_OP_READ_1_1_2,	SPINOR_OP_READ_1_1_2_4B },
335 		{ SPINOR_OP_READ_1_2_2,	SPINOR_OP_READ_1_2_2_4B },
336 		{ SPINOR_OP_READ_1_1_4,	SPINOR_OP_READ_1_1_4_4B },
337 		{ SPINOR_OP_READ_1_4_4,	SPINOR_OP_READ_1_4_4_4B },
338 
339 		{ SPINOR_OP_READ_1_1_1_DTR,	SPINOR_OP_READ_1_1_1_DTR_4B },
340 		{ SPINOR_OP_READ_1_2_2_DTR,	SPINOR_OP_READ_1_2_2_DTR_4B },
341 		{ SPINOR_OP_READ_1_4_4_DTR,	SPINOR_OP_READ_1_4_4_DTR_4B },
342 	};
343 
344 	return spi_nor_convert_opcode(opcode, spi_nor_3to4_read,
345 				      ARRAY_SIZE(spi_nor_3to4_read));
346 }
347 
348 static u8 spi_nor_convert_3to4_program(u8 opcode)
349 {
350 	static const u8 spi_nor_3to4_program[][2] = {
351 		{ SPINOR_OP_PP,		SPINOR_OP_PP_4B },
352 		{ SPINOR_OP_PP_1_1_4,	SPINOR_OP_PP_1_1_4_4B },
353 		{ SPINOR_OP_PP_1_4_4,	SPINOR_OP_PP_1_4_4_4B },
354 	};
355 
356 	return spi_nor_convert_opcode(opcode, spi_nor_3to4_program,
357 				      ARRAY_SIZE(spi_nor_3to4_program));
358 }
359 
360 static u8 spi_nor_convert_3to4_erase(u8 opcode)
361 {
362 	static const u8 spi_nor_3to4_erase[][2] = {
363 		{ SPINOR_OP_BE_4K,	SPINOR_OP_BE_4K_4B },
364 		{ SPINOR_OP_BE_32K,	SPINOR_OP_BE_32K_4B },
365 		{ SPINOR_OP_SE,		SPINOR_OP_SE_4B },
366 	};
367 
368 	return spi_nor_convert_opcode(opcode, spi_nor_3to4_erase,
369 				      ARRAY_SIZE(spi_nor_3to4_erase));
370 }
371 
372 static void spi_nor_set_4byte_opcodes(struct spi_nor *nor,
373 				      const struct flash_info *info)
374 {
375 	/* Do some manufacturer fixups first */
376 	switch (JEDEC_MFR(info)) {
377 	case SNOR_MFR_SPANSION:
378 	case SNOR_MFR_CYPRESS:
379 		/* No small sector erase for 4-byte command set */
380 		nor->erase_opcode = SPINOR_OP_SE;
381 		nor->mtd.erasesize = info->sector_size;
382 		break;
383 
384 	default:
385 		break;
386 	}
387 
388 	nor->read_opcode = spi_nor_convert_3to4_read(nor->read_opcode);
389 	nor->program_opcode = spi_nor_convert_3to4_program(nor->program_opcode);
390 	nor->erase_opcode = spi_nor_convert_3to4_erase(nor->erase_opcode);
391 }
392 #endif /* !CONFIG_SPI_FLASH_BAR */
393 
394 /* Enable/disable 4-byte addressing mode. */
395 static int set_4byte(struct spi_nor *nor, const struct flash_info *info,
396 		     int enable)
397 {
398 	int status;
399 	bool need_wren = false;
400 	u8 cmd;
401 
402 	switch (JEDEC_MFR(info)) {
403 	case SNOR_MFR_ST:
404 	case SNOR_MFR_MICRON:
405 		/* Some Micron need WREN command; all will accept it */
406 		need_wren = true;
407 	case SNOR_MFR_MACRONIX:
408 	case SNOR_MFR_WINBOND:
409 	case SNOR_MFR_GIGADEVICE:
410 	case SNOR_MFR_ISSI:
411 	case SNOR_MFR_CYPRESS:
412 		if (need_wren)
413 			write_enable(nor);
414 
415 		cmd = enable ? SPINOR_OP_EN4B : SPINOR_OP_EX4B;
416 		status = nor->write_reg(nor, cmd, NULL, 0);
417 		if (need_wren)
418 			write_disable(nor);
419 
420 		if (!status && !enable &&
421 		    JEDEC_MFR(info) == SNOR_MFR_WINBOND) {
422 			/*
423 			 * On Winbond W25Q256FV, leaving 4byte mode causes
424 			 * the Extended Address Register to be set to 1, so all
425 			 * 3-byte-address reads come from the second 16M.
426 			 * We must clear the register to enable normal behavior.
427 			 */
428 			write_enable(nor);
429 			nor->cmd_buf[0] = 0;
430 			nor->write_reg(nor, SPINOR_OP_WREAR, nor->cmd_buf, 1);
431 			write_disable(nor);
432 		}
433 
434 		return status;
435 	default:
436 		/* Spansion style */
437 		nor->cmd_buf[0] = enable << 7;
438 		return nor->write_reg(nor, SPINOR_OP_BRWR, nor->cmd_buf, 1);
439 	}
440 }
441 
442 static int spi_nor_sr_ready(struct spi_nor *nor)
443 {
444 	int sr = read_sr(nor);
445 
446 	if (sr < 0)
447 		return sr;
448 
449 	if (nor->flags & SNOR_F_USE_CLSR && sr & (SR_E_ERR | SR_P_ERR)) {
450 		if (sr & SR_E_ERR)
451 			dev_dbg(nor->dev, "Erase Error occurred\n");
452 		else
453 			dev_dbg(nor->dev, "Programming Error occurred\n");
454 
455 		nor->write_reg(nor, SPINOR_OP_CLSR, NULL, 0);
456 		return -EIO;
457 	}
458 
459 	return !(sr & SR_WIP);
460 }
461 
462 static int spi_nor_fsr_ready(struct spi_nor *nor)
463 {
464 	int fsr = read_fsr(nor);
465 
466 	if (fsr < 0)
467 		return fsr;
468 
469 	if (fsr & (FSR_E_ERR | FSR_P_ERR)) {
470 		if (fsr & FSR_E_ERR)
471 			dev_dbg(nor->dev, "Erase operation failed.\n");
472 		else
473 			dev_dbg(nor->dev, "Program operation failed.\n");
474 
475 		if (fsr & FSR_PT_ERR)
476 			dev_dbg(nor->dev,
477 				"Attempted to modify a protected sector.\n");
478 
479 		nor->write_reg(nor, SPINOR_OP_CLFSR, NULL, 0);
480 		return -EIO;
481 	}
482 
483 	return fsr & FSR_READY;
484 }
485 
486 static int spi_nor_ready(struct spi_nor *nor)
487 {
488 	int sr, fsr;
489 
490 	sr = spi_nor_sr_ready(nor);
491 	if (sr < 0)
492 		return sr;
493 	fsr = nor->flags & SNOR_F_USE_FSR ? spi_nor_fsr_ready(nor) : 1;
494 	if (fsr < 0)
495 		return fsr;
496 	return sr && fsr;
497 }
498 
499 /*
500  * Service routine to read status register until ready, or timeout occurs.
501  * Returns non-zero if error.
502  */
503 static int spi_nor_wait_till_ready_with_timeout(struct spi_nor *nor,
504 						unsigned long timeout)
505 {
506 	unsigned long timebase;
507 	int ret;
508 
509 	timebase = get_timer(0);
510 
511 	while (get_timer(timebase) < timeout) {
512 		ret = spi_nor_ready(nor);
513 		if (ret < 0)
514 			return ret;
515 		if (ret)
516 			return 0;
517 	}
518 
519 	dev_err(nor->dev, "flash operation timed out\n");
520 
521 	return -ETIMEDOUT;
522 }
523 
524 static int spi_nor_wait_till_ready(struct spi_nor *nor)
525 {
526 	return spi_nor_wait_till_ready_with_timeout(nor,
527 						    DEFAULT_READY_WAIT_JIFFIES);
528 }
529 
530 #ifdef CONFIG_SPI_FLASH_BAR
531 /*
532  * This "clean_bar" is necessary in a situation when one was accessing
533  * spi flash memory > 16 MiB by using Bank Address Register's BA24 bit.
534  *
535  * After it the BA24 bit shall be cleared to allow access to correct
536  * memory region after SW reset (by calling "reset" command).
537  *
538  * Otherwise, the BA24 bit may be left set and then after reset, the
539  * ROM would read/write/erase SPL from 16 MiB * bank_sel address.
540  */
541 static int clean_bar(struct spi_nor *nor)
542 {
543 	u8 cmd, bank_sel = 0;
544 
545 	if (nor->bank_curr == 0)
546 		return 0;
547 	cmd = nor->bank_write_cmd;
548 	nor->bank_curr = 0;
549 	write_enable(nor);
550 
551 	return nor->write_reg(nor, cmd, &bank_sel, 1);
552 }
553 
554 static int write_bar(struct spi_nor *nor, u32 offset)
555 {
556 	u8 cmd, bank_sel;
557 	int ret;
558 
559 	bank_sel = offset / SZ_16M;
560 	if (bank_sel == nor->bank_curr)
561 		goto bar_end;
562 
563 	cmd = nor->bank_write_cmd;
564 	write_enable(nor);
565 	ret = nor->write_reg(nor, cmd, &bank_sel, 1);
566 	if (ret < 0) {
567 		debug("SF: fail to write bank register\n");
568 		return ret;
569 	}
570 
571 bar_end:
572 	nor->bank_curr = bank_sel;
573 	return nor->bank_curr;
574 }
575 
576 static int read_bar(struct spi_nor *nor, const struct flash_info *info)
577 {
578 	u8 curr_bank = 0;
579 	int ret;
580 
581 	switch (JEDEC_MFR(info)) {
582 	case SNOR_MFR_SPANSION:
583 		nor->bank_read_cmd = SPINOR_OP_BRRD;
584 		nor->bank_write_cmd = SPINOR_OP_BRWR;
585 		break;
586 	default:
587 		nor->bank_read_cmd = SPINOR_OP_RDEAR;
588 		nor->bank_write_cmd = SPINOR_OP_WREAR;
589 	}
590 
591 	ret = nor->read_reg(nor, nor->bank_read_cmd,
592 				    &curr_bank, 1);
593 	if (ret) {
594 		debug("SF: fail to read bank addr register\n");
595 		return ret;
596 	}
597 	nor->bank_curr = curr_bank;
598 
599 	return 0;
600 }
601 #endif
602 
603 /*
604  * Initiate the erasure of a single sector
605  */
606 static int spi_nor_erase_sector(struct spi_nor *nor, u32 addr)
607 {
608 	u8 buf[SPI_NOR_MAX_ADDR_WIDTH];
609 	int i;
610 	int ret = 0;
611 
612 	if (nor->erase)
613 		return nor->erase(nor, addr);
614 
615 	/*
616 	 * Default implementation, if driver doesn't have a specialized HW
617 	 * control
618 	 */
619 	for (i = nor->addr_width - 1; i >= 0; i--) {
620 		buf[i] = addr & 0xff;
621 		addr >>= 8;
622 	}
623 
624 	ret = nor->write_reg(nor, nor->erase_opcode, buf, nor->addr_width);
625 	if (ret)
626 		return ret;
627 
628 	return nor->mtd.erasesize;
629 }
630 
631 /*
632  * Erase an address range on the nor chip.  The address range may extend
633  * one or more erase sectors.  Return an error is there is a problem erasing.
634  */
635 static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
636 {
637 	struct spi_nor *nor = mtd_to_spi_nor(mtd);
638 	u32 addr, len, rem;
639 	int ret;
640 
641 	dev_dbg(nor->dev, "at 0x%llx, len %lld\n", (long long)instr->addr,
642 		(long long)instr->len);
643 
644 	div_u64_rem(instr->len, mtd->erasesize, &rem);
645 	if (rem)
646 		return -EINVAL;
647 
648 	addr = instr->addr;
649 	len = instr->len;
650 
651 	while (len) {
652 #ifdef CONFIG_SPI_FLASH_BAR
653 		ret = write_bar(nor, addr);
654 		if (ret < 0)
655 			return ret;
656 #endif
657 		write_enable(nor);
658 
659 		ret = spi_nor_erase_sector(nor, addr);
660 		if (ret < 0)
661 			goto erase_err;
662 
663 		addr += ret;
664 		len -= ret;
665 
666 		ret = spi_nor_wait_till_ready(nor);
667 		if (ret)
668 			goto erase_err;
669 	}
670 
671 erase_err:
672 #ifdef CONFIG_SPI_FLASH_BAR
673 	ret = clean_bar(nor);
674 #endif
675 	write_disable(nor);
676 
677 	return ret;
678 }
679 
680 #ifdef CONFIG_SPI_FLASH_SPANSION
681 /**
682  * spansion_erase_non_uniform() - erase non-uniform sectors for Spansion/Cypress
683  *                                chips
684  * @nor:	pointer to a 'struct spi_nor'
685  * @addr:	address of the sector to erase
686  * @opcode_4k:	opcode for 4K sector erase
687  * @ovlsz_top:	size of overlaid portion at the top address
688  * @ovlsz_btm:	size of overlaid portion at the bottom address
689  *
690  * Erase an address range on the nor chip that can contain 4KB sectors overlaid
691  * on top and/or bottom. The appropriate erase opcode and size are chosen by
692  * address to erase and size of overlaid portion.
693  *
694  * Return: number of bytes erased on success, -errno otherwise.
695  */
696 static int spansion_erase_non_uniform(struct spi_nor *nor, u32 addr,
697 				      u8 opcode_4k, u32 ovlsz_top,
698 				      u32 ovlsz_btm)
699 {
700 	struct spi_mem_op op =
701 		SPI_MEM_OP(SPI_MEM_OP_CMD(nor->erase_opcode, 0),
702 			   SPI_MEM_OP_ADDR(nor->addr_width, addr, 0),
703 			   SPI_MEM_OP_NO_DUMMY,
704 			   SPI_MEM_OP_NO_DATA);
705 	struct mtd_info *mtd = &nor->mtd;
706 	u32 erasesize;
707 	int ret;
708 
709 	/* 4KB sectors */
710 	if (op.addr.val < ovlsz_btm ||
711 	    op.addr.val >= mtd->size - ovlsz_top) {
712 		op.cmd.opcode = opcode_4k;
713 		erasesize = SZ_4K;
714 
715 	/* Non-overlaid portion in the normal sector at the bottom */
716 	} else if (op.addr.val == ovlsz_btm) {
717 		op.cmd.opcode = nor->erase_opcode;
718 		erasesize = mtd->erasesize - ovlsz_btm;
719 
720 	/* Non-overlaid portion in the normal sector at the top */
721 	} else if (op.addr.val == mtd->size - mtd->erasesize) {
722 		op.cmd.opcode = nor->erase_opcode;
723 		erasesize = mtd->erasesize - ovlsz_top;
724 
725 	/* Normal sectors */
726 	} else {
727 		op.cmd.opcode = nor->erase_opcode;
728 		erasesize = mtd->erasesize;
729 	}
730 
731 	spi_nor_setup_op(nor, &op, nor->write_proto);
732 
733 	ret = spi_mem_exec_op(nor->spi, &op);
734 	if (ret)
735 		return ret;
736 
737 	return erasesize;
738 }
739 
740 static int s25hx_t_erase_non_uniform(struct spi_nor *nor, loff_t addr)
741 {
742 	/* Support 32 x 4KB sectors at bottom */
743 	return spansion_erase_non_uniform(nor, addr, SPINOR_OP_BE_4K_4B, 0,
744 					  SZ_128K);
745 }
746 
747 static int s25hx_t_setup(struct spi_nor *nor, const struct flash_info *info)
748 {
749 	int ret;
750 	u8 cfr3v;
751 
752 #ifdef CONFIG_SPI_FLASH_BAR
753 	return -ENOTSUPP; /* Bank Address Register is not supported */
754 #endif
755 	/*
756 	 * Read CFR3V to check if uniform sector is selected. If not, assign an
757 	 * erase hook that supports non-uniform erase.
758 	 */
759 	ret = spansion_read_any_reg(nor, SPINOR_REG_ADDR_CFR3V, 0, &cfr3v);
760 	if (ret)
761 		return ret;
762 
763 	if (!(cfr3v & CFR3V_UNHYSA))
764 		nor->erase = s25hx_t_erase_non_uniform;
765 
766 	return 0;
767 }
768 #endif
769 
770 static int micron_read_nvcr(struct spi_nor *nor)
771 {
772 	int ret;
773 	int val;
774 
775 	ret = nor->read_reg(nor, SPINOR_OP_MICRON_RDNVCR, (u8 *)&val, 2);
776 	if (ret < 0) {
777 		dev_err(nor->dev, "[Micron] error %d reading NVCR\n", ret);
778 		return ret;
779 	}
780 
781 	return val;
782 }
783 
784 static int micron_write_nvcr(struct spi_nor *nor, int val)
785 {
786 	int ret;
787 
788 	write_enable(nor);
789 
790 	nor->cmd_buf[0] = val & 0xff;
791 	nor->cmd_buf[1] = (val >> 8) & 0xff;
792 
793 	ret = nor->write_reg(nor, SPINOR_OP_MICRON_WRNVCR, nor->cmd_buf, 2);
794 	if (ret < 0) {
795 		dev_err(nor->dev,
796 			"[Micron] error while writing configuration register\n");
797 		return -EINVAL;
798 	}
799 
800 	ret = spi_nor_wait_till_ready(nor);
801 	if (ret) {
802 		dev_err(nor->dev,
803 			"[Micron] timeout while writing configuration register\n");
804 		return ret;
805 	}
806 
807 	return 0;
808 }
809 
810 static int micron_read_cr_quad_enable(struct spi_nor *nor)
811 {
812 	int ret;
813 
814 	/* Check current Quad Enable bit value. */
815 	ret = micron_read_nvcr(nor);
816 	if (ret < 0) {
817 		dev_err(dev, "[Micron] error while reading nonvolatile configuration register\n");
818 		return -EINVAL;
819 	}
820 
821 	if ((ret & MICRON_RST_HOLD_CTRL) == 0)
822 		return 0;
823 
824 	ret &= ~MICRON_RST_HOLD_CTRL;
825 
826 	/* Keep the current value of the Status Register. */
827 	ret = micron_write_nvcr(nor, ret);
828 	if (ret < 0) {
829 		dev_err(dev, "[Micron] error while writing nonvolatile configuration register\n");
830 		return -EINVAL;
831 	}
832 
833 	ret = micron_read_nvcr(nor);
834 	if (ret > 0 && (ret & MICRON_RST_HOLD_CTRL)) {
835 		dev_err(nor->dev, "[Micron] Quad bit not set\n");
836 		return -EINVAL;
837 	}
838 
839 	return 0;
840 }
841 
842 static bool cypress_s25hx_t(const struct flash_info *info)
843 {
844 	if (JEDEC_MFR(info) == SNOR_MFR_CYPRESS) {
845 		switch (info->id[1]) {
846 		case 0x2a: /* S25HL (QSPI, 3.3V) */
847 		case 0x2b: /* S25HS (QSPI, 1.8V) */
848 			return true;
849 			break;
850 
851 		default:
852 			break;
853 		}
854 	}
855 
856 	return false;
857 }
858 
859 #if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST)
860 /* Write status register and ensure bits in mask match written values */
861 static int write_sr_and_check(struct spi_nor *nor, u8 status_new, u8 mask)
862 {
863 	int ret;
864 
865 	write_enable(nor);
866 	ret = write_sr(nor, status_new);
867 	if (ret)
868 		return ret;
869 
870 	ret = spi_nor_wait_till_ready(nor);
871 	if (ret)
872 		return ret;
873 
874 	ret = read_sr(nor);
875 	if (ret < 0)
876 		return ret;
877 
878 	return ((ret & mask) != (status_new & mask)) ? -EIO : 0;
879 }
880 
881 static void stm_get_locked_range(struct spi_nor *nor, u8 sr, loff_t *ofs,
882 				 uint64_t *len)
883 {
884 	struct mtd_info *mtd = &nor->mtd;
885 	u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
886 	int shift = ffs(mask) - 1;
887 	int pow;
888 
889 	if (!(sr & mask)) {
890 		/* No protection */
891 		*ofs = 0;
892 		*len = 0;
893 	} else {
894 		pow = ((sr & mask) ^ mask) >> shift;
895 		*len = mtd->size >> pow;
896 		if (nor->flags & SNOR_F_HAS_SR_TB && sr & SR_TB)
897 			*ofs = 0;
898 		else
899 			*ofs = mtd->size - *len;
900 	}
901 }
902 
903 /*
904  * Return 1 if the entire region is locked (if @locked is true) or unlocked (if
905  * @locked is false); 0 otherwise
906  */
907 static int stm_check_lock_status_sr(struct spi_nor *nor, loff_t ofs, u64 len,
908 				    u8 sr, bool locked)
909 {
910 	loff_t lock_offs;
911 	uint64_t lock_len;
912 
913 	if (!len)
914 		return 1;
915 
916 	stm_get_locked_range(nor, sr, &lock_offs, &lock_len);
917 
918 	if (locked)
919 		/* Requested range is a sub-range of locked range */
920 		return (ofs + len <= lock_offs + lock_len) && (ofs >= lock_offs);
921 	else
922 		/* Requested range does not overlap with locked range */
923 		return (ofs >= lock_offs + lock_len) || (ofs + len <= lock_offs);
924 }
925 
926 static int stm_is_locked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
927 			    u8 sr)
928 {
929 	return stm_check_lock_status_sr(nor, ofs, len, sr, true);
930 }
931 
932 static int stm_is_unlocked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
933 			      u8 sr)
934 {
935 	return stm_check_lock_status_sr(nor, ofs, len, sr, false);
936 }
937 
938 /*
939  * Lock a region of the flash. Compatible with ST Micro and similar flash.
940  * Supports the block protection bits BP{0,1,2} in the status register
941  * (SR). Does not support these features found in newer SR bitfields:
942  *   - SEC: sector/block protect - only handle SEC=0 (block protect)
943  *   - CMP: complement protect - only support CMP=0 (range is not complemented)
944  *
945  * Support for the following is provided conditionally for some flash:
946  *   - TB: top/bottom protect
947  *
948  * Sample table portion for 8MB flash (Winbond w25q64fw):
949  *
950  *   SEC  |  TB   |  BP2  |  BP1  |  BP0  |  Prot Length  | Protected Portion
951  *  --------------------------------------------------------------------------
952  *    X   |   X   |   0   |   0   |   0   |  NONE         | NONE
953  *    0   |   0   |   0   |   0   |   1   |  128 KB       | Upper 1/64
954  *    0   |   0   |   0   |   1   |   0   |  256 KB       | Upper 1/32
955  *    0   |   0   |   0   |   1   |   1   |  512 KB       | Upper 1/16
956  *    0   |   0   |   1   |   0   |   0   |  1 MB         | Upper 1/8
957  *    0   |   0   |   1   |   0   |   1   |  2 MB         | Upper 1/4
958  *    0   |   0   |   1   |   1   |   0   |  4 MB         | Upper 1/2
959  *    X   |   X   |   1   |   1   |   1   |  8 MB         | ALL
960  *  ------|-------|-------|-------|-------|---------------|-------------------
961  *    0   |   1   |   0   |   0   |   1   |  128 KB       | Lower 1/64
962  *    0   |   1   |   0   |   1   |   0   |  256 KB       | Lower 1/32
963  *    0   |   1   |   0   |   1   |   1   |  512 KB       | Lower 1/16
964  *    0   |   1   |   1   |   0   |   0   |  1 MB         | Lower 1/8
965  *    0   |   1   |   1   |   0   |   1   |  2 MB         | Lower 1/4
966  *    0   |   1   |   1   |   1   |   0   |  4 MB         | Lower 1/2
967  *
968  * Returns negative on errors, 0 on success.
969  */
970 static int stm_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
971 {
972 	struct mtd_info *mtd = &nor->mtd;
973 	int status_old, status_new;
974 	u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
975 	u8 shift = ffs(mask) - 1, pow, val;
976 	loff_t lock_len;
977 	bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
978 	bool use_top;
979 
980 	status_old = read_sr(nor);
981 	if (status_old < 0)
982 		return status_old;
983 
984 	/* If nothing in our range is unlocked, we don't need to do anything */
985 	if (stm_is_locked_sr(nor, ofs, len, status_old))
986 		return 0;
987 
988 	/* If anything below us is unlocked, we can't use 'bottom' protection */
989 	if (!stm_is_locked_sr(nor, 0, ofs, status_old))
990 		can_be_bottom = false;
991 
992 	/* If anything above us is unlocked, we can't use 'top' protection */
993 	if (!stm_is_locked_sr(nor, ofs + len, mtd->size - (ofs + len),
994 			      status_old))
995 		can_be_top = false;
996 
997 	if (!can_be_bottom && !can_be_top)
998 		return -EINVAL;
999 
1000 	/* Prefer top, if both are valid */
1001 	use_top = can_be_top;
1002 
1003 	/* lock_len: length of region that should end up locked */
1004 	if (use_top)
1005 		lock_len = mtd->size - ofs;
1006 	else
1007 		lock_len = ofs + len;
1008 
1009 	/*
1010 	 * Need smallest pow such that:
1011 	 *
1012 	 *   1 / (2^pow) <= (len / size)
1013 	 *
1014 	 * so (assuming power-of-2 size) we do:
1015 	 *
1016 	 *   pow = ceil(log2(size / len)) = log2(size) - floor(log2(len))
1017 	 */
1018 	pow = ilog2(mtd->size) - ilog2(lock_len);
1019 	val = mask - (pow << shift);
1020 	if (val & ~mask)
1021 		return -EINVAL;
1022 	/* Don't "lock" with no region! */
1023 	if (!(val & mask))
1024 		return -EINVAL;
1025 
1026 	status_new = (status_old & ~mask & ~SR_TB) | val;
1027 
1028 	/* Disallow further writes if WP pin is asserted */
1029 	status_new |= SR_SRWD;
1030 
1031 	if (!use_top)
1032 		status_new |= SR_TB;
1033 
1034 	/* Don't bother if they're the same */
1035 	if (status_new == status_old)
1036 		return 0;
1037 
1038 	/* Only modify protection if it will not unlock other areas */
1039 	if ((status_new & mask) < (status_old & mask))
1040 		return -EINVAL;
1041 
1042 	return write_sr_and_check(nor, status_new, mask);
1043 }
1044 
1045 /*
1046  * Unlock a region of the flash. See stm_lock() for more info
1047  *
1048  * Returns negative on errors, 0 on success.
1049  */
1050 static int stm_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
1051 {
1052 	struct mtd_info *mtd = &nor->mtd;
1053 	int status_old, status_new;
1054 	u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
1055 	u8 shift = ffs(mask) - 1, pow, val;
1056 	loff_t lock_len;
1057 	bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
1058 	bool use_top;
1059 
1060 	status_old = read_sr(nor);
1061 	if (status_old < 0)
1062 		return status_old;
1063 
1064 	/* If nothing in our range is locked, we don't need to do anything */
1065 	if (stm_is_unlocked_sr(nor, ofs, len, status_old))
1066 		return 0;
1067 
1068 	/* If anything below us is locked, we can't use 'top' protection */
1069 	if (!stm_is_unlocked_sr(nor, 0, ofs, status_old))
1070 		can_be_top = false;
1071 
1072 	/* If anything above us is locked, we can't use 'bottom' protection */
1073 	if (!stm_is_unlocked_sr(nor, ofs + len, mtd->size - (ofs + len),
1074 				status_old))
1075 		can_be_bottom = false;
1076 
1077 	if (!can_be_bottom && !can_be_top)
1078 		return -EINVAL;
1079 
1080 	/* Prefer top, if both are valid */
1081 	use_top = can_be_top;
1082 
1083 	/* lock_len: length of region that should remain locked */
1084 	if (use_top)
1085 		lock_len = mtd->size - (ofs + len);
1086 	else
1087 		lock_len = ofs;
1088 
1089 	/*
1090 	 * Need largest pow such that:
1091 	 *
1092 	 *   1 / (2^pow) >= (len / size)
1093 	 *
1094 	 * so (assuming power-of-2 size) we do:
1095 	 *
1096 	 *   pow = floor(log2(size / len)) = log2(size) - ceil(log2(len))
1097 	 */
1098 	pow = ilog2(mtd->size) - order_base_2(lock_len);
1099 	if (lock_len == 0) {
1100 		val = 0; /* fully unlocked */
1101 	} else {
1102 		val = mask - (pow << shift);
1103 		/* Some power-of-two sizes are not supported */
1104 		if (val & ~mask)
1105 			return -EINVAL;
1106 	}
1107 
1108 	status_new = (status_old & ~mask & ~SR_TB) | val;
1109 
1110 	/* Don't protect status register if we're fully unlocked */
1111 	if (lock_len == 0)
1112 		status_new &= ~SR_SRWD;
1113 
1114 	if (!use_top)
1115 		status_new |= SR_TB;
1116 
1117 	/* Don't bother if they're the same */
1118 	if (status_new == status_old)
1119 		return 0;
1120 
1121 	/* Only modify protection if it will not lock other areas */
1122 	if ((status_new & mask) > (status_old & mask))
1123 		return -EINVAL;
1124 
1125 	return write_sr_and_check(nor, status_new, mask);
1126 }
1127 
1128 /*
1129  * Check if a region of the flash is (completely) locked. See stm_lock() for
1130  * more info.
1131  *
1132  * Returns 1 if entire region is locked, 0 if any portion is unlocked, and
1133  * negative on errors.
1134  */
1135 static int stm_is_locked(struct spi_nor *nor, loff_t ofs, uint64_t len)
1136 {
1137 	int status;
1138 
1139 	status = read_sr(nor);
1140 	if (status < 0)
1141 		return status;
1142 
1143 	return stm_is_locked_sr(nor, ofs, len, status);
1144 }
1145 #endif /* CONFIG_SPI_FLASH_STMICRO */
1146 
1147 static const struct flash_info *spi_nor_read_id(struct spi_nor *nor)
1148 {
1149 	int			tmp;
1150 	u8			id[SPI_NOR_MAX_ID_LEN];
1151 	const struct flash_info	*info;
1152 
1153 	tmp = nor->read_reg(nor, SPINOR_OP_RDID, id, SPI_NOR_MAX_ID_LEN);
1154 	if (tmp < 0) {
1155 		dev_dbg(nor->dev, "error %d reading JEDEC ID\n", tmp);
1156 		return ERR_PTR(tmp);
1157 	}
1158 
1159 	info = spi_nor_ids;
1160 	for (; info->name; info++) {
1161 		if (info->id_len) {
1162 			if (!memcmp(info->id, id, info->id_len))
1163 				return info;
1164 		}
1165 	}
1166 
1167 	dev_err(nor->dev, "unrecognized JEDEC id bytes: %02x, %02x, %02x\n",
1168 		id[0], id[1], id[2]);
1169 	return ERR_PTR(-ENODEV);
1170 }
1171 
1172 static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
1173 			size_t *retlen, u_char *buf)
1174 {
1175 	struct spi_nor *nor = mtd_to_spi_nor(mtd);
1176 	int ret;
1177 
1178 	dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len);
1179 
1180 	while (len) {
1181 		loff_t addr = from;
1182 		size_t read_len = len;
1183 
1184 #ifdef CONFIG_SPI_FLASH_BAR
1185 		u32 remain_len;
1186 
1187 		ret = write_bar(nor, addr);
1188 		if (ret < 0)
1189 			return log_ret(ret);
1190 		remain_len = (SZ_16M * (nor->bank_curr + 1)) - addr;
1191 
1192 		if (len < remain_len)
1193 			read_len = len;
1194 		else
1195 			read_len = remain_len;
1196 #endif
1197 
1198 		ret = nor->read(nor, addr, read_len, buf);
1199 		if (ret == 0) {
1200 			/* We shouldn't see 0-length reads */
1201 			ret = -EIO;
1202 			goto read_err;
1203 		}
1204 		if (ret < 0)
1205 			goto read_err;
1206 
1207 		*retlen += ret;
1208 		buf += ret;
1209 		from += ret;
1210 		len -= ret;
1211 	}
1212 	ret = 0;
1213 
1214 read_err:
1215 #ifdef CONFIG_SPI_FLASH_BAR
1216 	ret = clean_bar(nor);
1217 #endif
1218 	return ret;
1219 }
1220 
1221 #if CONFIG_IS_ENABLED(SPI_FLASH_SFDP_SUPPORT)
1222 static int sst_read_nvcr(struct spi_nor *nor)
1223 {
1224 	int ret;
1225 	int val;
1226 
1227 	ret = nor->read_reg(nor, SPINOR_OP_SST_RDNVCR, (u8 *)&val, 2);
1228 	if (ret < 0) {
1229 		dev_err(nor->dev, "SST error %d while reading CR\n", ret);
1230 		return ret;
1231 	}
1232 
1233 	return val;
1234 }
1235 
1236 static int sst_write_nvcr(struct spi_nor *nor, int val)
1237 {
1238 	int ret;
1239 
1240 	write_enable(nor);
1241 
1242 	nor->cmd_buf[0] = val & 0xff;
1243 	nor->cmd_buf[1] = (val >> 8) & 0xff;
1244 
1245 	ret = nor->write_reg(nor, SPINOR_OP_SST_WRNVCR, nor->cmd_buf, 2);
1246 	if (ret < 0) {
1247 		dev_err(nor->dev,
1248 			"SST error while writing configuration register\n");
1249 		return -EINVAL;
1250 	}
1251 
1252 	ret = spi_nor_wait_till_ready(nor);
1253 	if (ret) {
1254 		dev_err(nor->dev,
1255 			"SST timeout while writing configuration register\n");
1256 		return ret;
1257 	}
1258 
1259 	return 0;
1260 }
1261 
1262 static int sst_cr_quad_enable(struct spi_nor *nor)
1263 {
1264 	int ret;
1265 
1266 	/* Check current Quad Enable bit value. */
1267 	ret = sst_read_nvcr(nor);
1268 	if (ret < 0) {
1269 		dev_dbg(nor->dev, "SST error while reading nonvolatile configuration register\n");
1270 		return -EINVAL;
1271 	}
1272 
1273 	if ((ret & SPINOR_SST_RST_HOLD_CTRL) == 0)
1274 		return 0;
1275 
1276 	/* Nonvolatile Configuration Register bit 4 */
1277 	ret &= ~SPINOR_SST_RST_HOLD_CTRL;
1278 
1279 	/* Keep the current value of the Status Register. */
1280 	ret = sst_write_nvcr(nor, ret);
1281 	if (ret < 0) {
1282 		dev_err(nor->dev, "SST error while writing nonvolatile configuration register\n");
1283 		return -EINVAL;
1284 	}
1285 
1286 	ret = sst_read_nvcr(nor);
1287 	if (ret > 0 && (ret & SPINOR_SST_RST_HOLD_CTRL)) {
1288 		dev_err(nor->dev, "SST Quad bit not set\n");
1289 		return -EINVAL;
1290 	}
1291 
1292 	return 0;
1293 }
1294 #endif
1295 
1296 #ifdef CONFIG_SPI_FLASH_SST
1297 static int sst_write_byteprogram(struct spi_nor *nor, loff_t to, size_t len,
1298 				 size_t *retlen, const u_char *buf)
1299 {
1300 	size_t actual;
1301 	int ret = 0;
1302 
1303 	for (actual = 0; actual < len; actual++) {
1304 		nor->program_opcode = SPINOR_OP_BP;
1305 
1306 		write_enable(nor);
1307 		/* write one byte. */
1308 		ret = nor->write(nor, to, 1, buf + actual);
1309 		if (ret < 0)
1310 			goto sst_write_err;
1311 		ret = spi_nor_wait_till_ready(nor);
1312 		if (ret)
1313 			goto sst_write_err;
1314 		to++;
1315 	}
1316 
1317 sst_write_err:
1318 	write_disable(nor);
1319 	return ret;
1320 }
1321 
1322 static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
1323 		     size_t *retlen, const u_char *buf)
1324 {
1325 	struct spi_nor *nor = mtd_to_spi_nor(mtd);
1326 	struct spi_slave *spi = nor->spi;
1327 	size_t actual;
1328 	int ret;
1329 
1330 	dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
1331 	if (spi->mode & SPI_TX_BYTE)
1332 		return sst_write_byteprogram(nor, to, len, retlen, buf);
1333 
1334 	write_enable(nor);
1335 
1336 	nor->sst_write_second = false;
1337 
1338 	actual = to % 2;
1339 	/* Start write from odd address. */
1340 	if (actual) {
1341 		nor->program_opcode = SPINOR_OP_BP;
1342 
1343 		/* write one byte. */
1344 		ret = nor->write(nor, to, 1, buf);
1345 		if (ret < 0)
1346 			goto sst_write_err;
1347 		ret = spi_nor_wait_till_ready(nor);
1348 		if (ret)
1349 			goto sst_write_err;
1350 	}
1351 	to += actual;
1352 
1353 	/* Write out most of the data here. */
1354 	for (; actual < len - 1; actual += 2) {
1355 		nor->program_opcode = SPINOR_OP_AAI_WP;
1356 
1357 		/* write two bytes. */
1358 		ret = nor->write(nor, to, 2, buf + actual);
1359 		if (ret < 0)
1360 			goto sst_write_err;
1361 		ret = spi_nor_wait_till_ready(nor);
1362 		if (ret)
1363 			goto sst_write_err;
1364 		to += 2;
1365 		nor->sst_write_second = true;
1366 	}
1367 	nor->sst_write_second = false;
1368 
1369 	write_disable(nor);
1370 	ret = spi_nor_wait_till_ready(nor);
1371 	if (ret)
1372 		goto sst_write_err;
1373 
1374 	/* Write out trailing byte if it exists. */
1375 	if (actual != len) {
1376 		write_enable(nor);
1377 
1378 		nor->program_opcode = SPINOR_OP_BP;
1379 		ret = nor->write(nor, to, 1, buf + actual);
1380 		if (ret < 0)
1381 			goto sst_write_err;
1382 		ret = spi_nor_wait_till_ready(nor);
1383 		if (ret)
1384 			goto sst_write_err;
1385 		write_disable(nor);
1386 		actual += 1;
1387 	}
1388 sst_write_err:
1389 	*retlen += actual;
1390 	return ret;
1391 }
1392 #endif
1393 /*
1394  * Write an address range to the nor chip.  Data must be written in
1395  * FLASH_PAGESIZE chunks.  The address range may be any size provided
1396  * it is within the physical boundaries.
1397  */
1398 static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
1399 	size_t *retlen, const u_char *buf)
1400 {
1401 	struct spi_nor *nor = mtd_to_spi_nor(mtd);
1402 	size_t page_offset, page_remain, i;
1403 	ssize_t ret;
1404 
1405 	dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
1406 
1407 	for (i = 0; i < len; ) {
1408 		ssize_t written;
1409 		loff_t addr = to + i;
1410 
1411 		/*
1412 		 * If page_size is a power of two, the offset can be quickly
1413 		 * calculated with an AND operation. On the other cases we
1414 		 * need to do a modulus operation (more expensive).
1415 		 * Power of two numbers have only one bit set and we can use
1416 		 * the instruction hweight32 to detect if we need to do a
1417 		 * modulus (do_div()) or not.
1418 		 */
1419 		if (hweight32(nor->page_size) == 1) {
1420 			page_offset = addr & (nor->page_size - 1);
1421 		} else {
1422 			u64 aux = addr;
1423 
1424 			page_offset = do_div(aux, nor->page_size);
1425 		}
1426 		/* the size of data remaining on the first page */
1427 		page_remain = min_t(size_t,
1428 				    nor->page_size - page_offset, len - i);
1429 
1430 #ifdef CONFIG_SPI_FLASH_BAR
1431 		ret = write_bar(nor, addr);
1432 		if (ret < 0)
1433 			return ret;
1434 #endif
1435 		write_enable(nor);
1436 		ret = nor->write(nor, addr, page_remain, buf + i);
1437 		if (ret < 0)
1438 			goto write_err;
1439 		written = ret;
1440 
1441 		ret = spi_nor_wait_till_ready(nor);
1442 		if (ret)
1443 			goto write_err;
1444 		*retlen += written;
1445 		i += written;
1446 		if (written != page_remain) {
1447 			ret = -EIO;
1448 			goto write_err;
1449 		}
1450 	}
1451 
1452 write_err:
1453 #ifdef CONFIG_SPI_FLASH_BAR
1454 	ret = clean_bar(nor);
1455 #endif
1456 	return ret;
1457 }
1458 
1459 #ifdef CONFIG_SPI_FLASH_MACRONIX
1460 /**
1461  * macronix_quad_enable() - set QE bit in Status Register.
1462  * @nor:	pointer to a 'struct spi_nor'
1463  *
1464  * Set the Quad Enable (QE) bit in the Status Register.
1465  *
1466  * bit 6 of the Status Register is the QE bit for Macronix like QSPI memories.
1467  *
1468  * Return: 0 on success, -errno otherwise.
1469  */
1470 static int macronix_quad_enable(struct spi_nor *nor)
1471 {
1472 	int ret, val;
1473 
1474 	val = read_sr(nor);
1475 	if (val < 0)
1476 		return val;
1477 	if (val & SR_QUAD_EN_MX)
1478 		return 0;
1479 
1480 	write_enable(nor);
1481 
1482 	write_sr(nor, val | SR_QUAD_EN_MX);
1483 
1484 	ret = spi_nor_wait_till_ready(nor);
1485 	if (ret)
1486 		return ret;
1487 
1488 	ret = read_sr(nor);
1489 	if (!(ret > 0 && (ret & SR_QUAD_EN_MX))) {
1490 		dev_err(nor->dev, "Macronix Quad bit not set\n");
1491 		return -EINVAL;
1492 	}
1493 
1494 	return 0;
1495 }
1496 #endif
1497 
1498 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
1499 /*
1500  * Write status Register and configuration register with 2 bytes
1501  * The first byte will be written to the status register, while the
1502  * second byte will be written to the configuration register.
1503  * Return negative if error occurred.
1504  */
1505 static int write_sr_cr(struct spi_nor *nor, u8 *sr_cr)
1506 {
1507 	int ret;
1508 
1509 	write_enable(nor);
1510 
1511 	ret = nor->write_reg(nor, SPINOR_OP_WRSR, sr_cr, 2);
1512 	if (ret < 0) {
1513 		dev_dbg(nor->dev,
1514 			"error while writing configuration register\n");
1515 		return -EINVAL;
1516 	}
1517 
1518 	ret = spi_nor_wait_till_ready(nor);
1519 	if (ret) {
1520 		dev_dbg(nor->dev,
1521 			"timeout while writing configuration register\n");
1522 		return ret;
1523 	}
1524 
1525 	return 0;
1526 }
1527 
1528 /**
1529  * spansion_read_cr_quad_enable() - set QE bit in Configuration Register.
1530  * @nor:	pointer to a 'struct spi_nor'
1531  *
1532  * Set the Quad Enable (QE) bit in the Configuration Register.
1533  * This function should be used with QSPI memories supporting the Read
1534  * Configuration Register (35h) instruction.
1535  *
1536  * bit 1 of the Configuration Register is the QE bit for Spansion like QSPI
1537  * memories.
1538  *
1539  * Return: 0 on success, -errno otherwise.
1540  */
1541 static int spansion_read_cr_quad_enable(struct spi_nor *nor)
1542 {
1543 	u8 sr_cr[2];
1544 	int ret;
1545 
1546 	/* Check current Quad Enable bit value. */
1547 	ret = read_cr(nor);
1548 	if (ret < 0) {
1549 		dev_dbg(nor->dev, "error while reading configuration register\n");
1550 		return -EINVAL;
1551 	}
1552 
1553 	if (ret & CR_QUAD_EN_SPAN)
1554 		return 0;
1555 
1556 	sr_cr[1] = ret | CR_QUAD_EN_SPAN;
1557 
1558 	/* Keep the current value of the Status Register. */
1559 	ret = read_sr(nor);
1560 	if (ret < 0) {
1561 		dev_dbg(nor->dev, "error while reading status register\n");
1562 		return -EINVAL;
1563 	}
1564 	sr_cr[0] = ret;
1565 
1566 	ret = write_sr_cr(nor, sr_cr);
1567 	if (ret)
1568 		return ret;
1569 
1570 	/* Read back and check it. */
1571 	ret = read_cr(nor);
1572 	if (!(ret > 0 && (ret & CR_QUAD_EN_SPAN))) {
1573 		dev_dbg(nor->dev, "Spansion Quad bit not set\n");
1574 		return -EINVAL;
1575 	}
1576 
1577 	return 0;
1578 }
1579 
1580 /**
1581  * sr2_bit1_quad_enable() - set QE bit in Status Register 2.
1582  * @nor:	pointer to a 'struct spi_nor'
1583  *
1584  * Set the Quad Enable (QE) bit in the Status Register 2.
1585  *
1586  * Return: 0 on success, -errno otherwise.
1587  */
1588 static int winbond_sr2_bit1_quad_enable(struct spi_nor *nor)
1589 {
1590 	u8 sr2 = 0;
1591 	int ret;
1592 
1593 	/* Check current Quad Enable bit value. */
1594 	ret = read_winbond_sr2(nor);
1595 	if (ret < 0) {
1596 		dev_err(nor->dev, "error while reading status register 2\n");
1597 		return -EINVAL;
1598 	}
1599 
1600 	if (ret & SR2_QUAD_EN_BIT1)
1601 		return 0;
1602 
1603 	/* Update the Quad Enable bit. */
1604 	sr2 = (u8)(ret | SR2_QUAD_EN_BIT1);
1605 
1606 	write_enable(nor);
1607 
1608 	ret = write_winbond_sr2(nor, sr2);
1609 	if (ret < 0) {
1610 		dev_err(nor->dev, "error while writing status register 2\n");
1611 		return -EINVAL;
1612 	}
1613 
1614 	ret = spi_nor_wait_till_ready(nor);
1615 	if (ret < 0) {
1616 		dev_err(nor->dev, "timeout while writing status register 2\n");
1617 		return ret;
1618 	}
1619 
1620 	/* Read back and check it. */
1621 	ret = read_winbond_sr2(nor);
1622 	if (ret < 0 || !(ret & SR2_QUAD_EN_BIT1)) {
1623 		dev_err(nor->dev, "SR2 Quad bit not set\n");
1624 		return -EINVAL;
1625 	}
1626 
1627 	return 0;
1628 }
1629 
1630 #if CONFIG_IS_ENABLED(SPI_FLASH_SFDP_SUPPORT)
1631 /**
1632  * spansion_no_read_cr_quad_enable() - set QE bit in Configuration Register.
1633  * @nor:	pointer to a 'struct spi_nor'
1634  *
1635  * Set the Quad Enable (QE) bit in the Configuration Register.
1636  * This function should be used with QSPI memories not supporting the Read
1637  * Configuration Register (35h) instruction.
1638  *
1639  * bit 1 of the Configuration Register is the QE bit for Spansion like QSPI
1640  * memories.
1641  *
1642  * Return: 0 on success, -errno otherwise.
1643  */
1644 static int spansion_no_read_cr_quad_enable(struct spi_nor *nor)
1645 {
1646 	u8 sr_cr[2];
1647 	int ret;
1648 
1649 	/* Keep the current value of the Status Register. */
1650 	ret = read_sr(nor);
1651 	if (ret < 0) {
1652 		dev_dbg(nor->dev, "error while reading status register\n");
1653 		return -EINVAL;
1654 	}
1655 	sr_cr[0] = ret;
1656 	sr_cr[1] = CR_QUAD_EN_SPAN;
1657 
1658 	return write_sr_cr(nor, sr_cr);
1659 }
1660 
1661 #endif /* CONFIG_SPI_FLASH_SFDP_SUPPORT */
1662 #endif /* CONFIG_SPI_FLASH_SPANSION */
1663 
1664 struct spi_nor_read_command {
1665 	u8			num_mode_clocks;
1666 	u8			num_wait_states;
1667 	u8			opcode;
1668 	enum spi_nor_protocol	proto;
1669 };
1670 
1671 struct spi_nor_pp_command {
1672 	u8			opcode;
1673 	enum spi_nor_protocol	proto;
1674 };
1675 
1676 enum spi_nor_read_command_index {
1677 	SNOR_CMD_READ,
1678 	SNOR_CMD_READ_FAST,
1679 	SNOR_CMD_READ_1_1_1_DTR,
1680 
1681 	/* Dual SPI */
1682 	SNOR_CMD_READ_1_1_2,
1683 	SNOR_CMD_READ_1_2_2,
1684 	SNOR_CMD_READ_2_2_2,
1685 	SNOR_CMD_READ_1_2_2_DTR,
1686 
1687 	/* Quad SPI */
1688 	SNOR_CMD_READ_1_1_4,
1689 	SNOR_CMD_READ_1_4_4,
1690 	SNOR_CMD_READ_4_4_4,
1691 	SNOR_CMD_READ_1_4_4_DTR,
1692 
1693 	/* Octo SPI */
1694 	SNOR_CMD_READ_1_1_8,
1695 	SNOR_CMD_READ_1_8_8,
1696 	SNOR_CMD_READ_8_8_8,
1697 	SNOR_CMD_READ_1_8_8_DTR,
1698 
1699 	SNOR_CMD_READ_MAX
1700 };
1701 
1702 enum spi_nor_pp_command_index {
1703 	SNOR_CMD_PP,
1704 
1705 	/* Quad SPI */
1706 	SNOR_CMD_PP_1_1_4,
1707 	SNOR_CMD_PP_1_4_4,
1708 	SNOR_CMD_PP_4_4_4,
1709 
1710 	/* Octo SPI */
1711 	SNOR_CMD_PP_1_1_8,
1712 	SNOR_CMD_PP_1_8_8,
1713 	SNOR_CMD_PP_8_8_8,
1714 
1715 	SNOR_CMD_PP_MAX
1716 };
1717 
1718 struct spi_nor_flash_parameter {
1719 	u64				size;
1720 	u32				page_size;
1721 
1722 	struct spi_nor_hwcaps		hwcaps;
1723 	struct spi_nor_read_command	reads[SNOR_CMD_READ_MAX];
1724 	struct spi_nor_pp_command	page_programs[SNOR_CMD_PP_MAX];
1725 
1726 	int (*quad_enable)(struct spi_nor *nor);
1727 };
1728 
1729 #ifdef CONFIG_SPI_FLASH_SPANSION
1730 /**
1731  * spansion_quad_enable_volatile() - enable Quad I/O mode in volatile register.
1732  * @nor:	pointer to a 'struct spi_nor'
1733  *
1734  * It is recommended to update volatile registers in the field application due
1735  * to a risk of the non-volatile registers corruption by power interrupt. This
1736  * function sets Quad Enable bit in CFR1 volatile.
1737  *
1738  * Return: 0 on success, -errno otherwise.
1739  */
1740 static int spansion_quad_enable_volatile(struct spi_nor *nor)
1741 {
1742 	u32 addr = SPINOR_REG_ADDR_CFR1V;
1743 
1744 	u8 cr;
1745 	int ret;
1746 
1747 	/* Check current Quad Enable bit value. */
1748 	ret = spansion_read_any_reg(nor, addr, 0, &cr);
1749 	if (ret < 0) {
1750 		dev_dbg(nor->dev,
1751 			"error while reading configuration register\n");
1752 		return -EINVAL;
1753 	}
1754 
1755 	if (cr & CR_QUAD_EN_SPAN)
1756 		return 0;
1757 
1758 	cr |= CR_QUAD_EN_SPAN;
1759 
1760 	write_enable(nor);
1761 
1762 	ret = spansion_write_any_reg(nor, addr, cr);
1763 
1764 	if (ret < 0) {
1765 		dev_dbg(nor->dev,
1766 			"error while writing configuration register\n");
1767 		return -EINVAL;
1768 	}
1769 
1770 	/* Read back and check it. */
1771 	ret = spansion_read_any_reg(nor, addr, 0, &cr);
1772 	if (ret || !(cr & CR_QUAD_EN_SPAN)) {
1773 		dev_dbg(nor->dev, "Spansion Quad bit not set\n");
1774 		return -EINVAL;
1775 	}
1776 
1777 	return 0;
1778 }
1779 #endif
1780 
1781 static void
1782 spi_nor_set_read_settings(struct spi_nor_read_command *read,
1783 			  u8 num_mode_clocks,
1784 			  u8 num_wait_states,
1785 			  u8 opcode,
1786 			  enum spi_nor_protocol proto)
1787 {
1788 	read->num_mode_clocks = num_mode_clocks;
1789 	read->num_wait_states = num_wait_states;
1790 	read->opcode = opcode;
1791 	read->proto = proto;
1792 }
1793 
1794 static void
1795 spi_nor_set_pp_settings(struct spi_nor_pp_command *pp,
1796 			u8 opcode,
1797 			enum spi_nor_protocol proto)
1798 {
1799 	pp->opcode = opcode;
1800 	pp->proto = proto;
1801 }
1802 
1803 #if CONFIG_IS_ENABLED(SPI_FLASH_SFDP_SUPPORT)
1804 /*
1805  * Serial Flash Discoverable Parameters (SFDP) parsing.
1806  */
1807 
1808 /**
1809  * spi_nor_read_sfdp() - read Serial Flash Discoverable Parameters.
1810  * @nor:	pointer to a 'struct spi_nor'
1811  * @addr:	offset in the SFDP area to start reading data from
1812  * @len:	number of bytes to read
1813  * @buf:	buffer where the SFDP data are copied into (dma-safe memory)
1814  *
1815  * Whatever the actual numbers of bytes for address and dummy cycles are
1816  * for (Fast) Read commands, the Read SFDP (5Ah) instruction is always
1817  * followed by a 3-byte address and 8 dummy clock cycles.
1818  *
1819  * Return: 0 on success, -errno otherwise.
1820  */
1821 static int spi_nor_read_sfdp(struct spi_nor *nor, u32 addr,
1822 			     size_t len, void *buf)
1823 {
1824 	u8 addr_width, read_opcode, read_dummy;
1825 	enum spi_nor_protocol	read_proto;
1826 	int ret;
1827 
1828 	read_opcode = nor->read_opcode;
1829 	addr_width = nor->addr_width;
1830 	read_dummy = nor->read_dummy;
1831 	read_proto = nor->read_proto;
1832 
1833 	nor->read_opcode = SPINOR_OP_RDSFDP;
1834 	nor->read_proto = SNOR_PROTO_1_1_1;
1835 	nor->addr_width = 3;
1836 	nor->read_dummy = 8;
1837 
1838 	while (len) {
1839 		ret = nor->read(nor, addr, len, (u8 *)buf);
1840 		if (!ret || ret > len) {
1841 			ret = -EIO;
1842 			goto read_err;
1843 		}
1844 		if (ret < 0)
1845 			goto read_err;
1846 
1847 		buf += ret;
1848 		addr += ret;
1849 		len -= ret;
1850 	}
1851 	ret = 0;
1852 
1853 read_err:
1854 	nor->read_opcode = read_opcode;
1855 	nor->addr_width = addr_width;
1856 	nor->read_dummy = read_dummy;
1857 	nor->read_proto = read_proto;
1858 
1859 	return ret;
1860 }
1861 
1862 struct sfdp_parameter_header {
1863 	u8		id_lsb;
1864 	u8		minor;
1865 	u8		major;
1866 	u8		length; /* in double words */
1867 	u8		parameter_table_pointer[3]; /* byte address */
1868 	u8		id_msb;
1869 };
1870 
1871 #define SFDP_PARAM_HEADER_ID(p)	(((p)->id_msb << 8) | (p)->id_lsb)
1872 #define SFDP_PARAM_HEADER_PTP(p) \
1873 	(((p)->parameter_table_pointer[2] << 16) | \
1874 	 ((p)->parameter_table_pointer[1] <<  8) | \
1875 	 ((p)->parameter_table_pointer[0] <<  0))
1876 
1877 #define SFDP_BFPT_ID		0xff00	/* Basic Flash Parameter Table */
1878 #define SFDP_SECTOR_MAP_ID	0xff81	/* Sector Map Table */
1879 
1880 #define SFDP_SIGNATURE		0x50444653U
1881 #define SFDP_JESD216_MAJOR	1
1882 #define SFDP_JESD216_MINOR	0
1883 #define SFDP_JESD216A_MINOR	5
1884 #define SFDP_JESD216B_MINOR	6
1885 
1886 struct sfdp_header {
1887 	u32		signature; /* Ox50444653U <=> "SFDP" */
1888 	u8		minor;
1889 	u8		major;
1890 	u8		nph; /* 0-base number of parameter headers */
1891 	u8		unused;
1892 
1893 	/* Basic Flash Parameter Table. */
1894 	struct sfdp_parameter_header	bfpt_header;
1895 };
1896 
1897 /* Basic Flash Parameter Table */
1898 
1899 /*
1900  * JESD216 rev B defines a Basic Flash Parameter Table of 16 DWORDs.
1901  * They are indexed from 1 but C arrays are indexed from 0.
1902  */
1903 #define BFPT_DWORD(i)		((i) - 1)
1904 #define BFPT_DWORD_MAX		16
1905 
1906 /* The first version of JESB216 defined only 9 DWORDs. */
1907 #define BFPT_DWORD_MAX_JESD216			9
1908 
1909 /* 1st DWORD. */
1910 #define BFPT_DWORD1_FAST_READ_1_1_2		BIT(16)
1911 #define BFPT_DWORD1_ADDRESS_BYTES_MASK		GENMASK(18, 17)
1912 #define BFPT_DWORD1_ADDRESS_BYTES_3_ONLY	(0x0UL << 17)
1913 #define BFPT_DWORD1_ADDRESS_BYTES_3_OR_4	(0x1UL << 17)
1914 #define BFPT_DWORD1_ADDRESS_BYTES_4_ONLY	(0x2UL << 17)
1915 #define BFPT_DWORD1_DTR				BIT(19)
1916 #define BFPT_DWORD1_FAST_READ_1_2_2		BIT(20)
1917 #define BFPT_DWORD1_FAST_READ_1_4_4		BIT(21)
1918 #define BFPT_DWORD1_FAST_READ_1_1_4		BIT(22)
1919 
1920 /* 5th DWORD. */
1921 #define BFPT_DWORD5_FAST_READ_2_2_2		BIT(0)
1922 #define BFPT_DWORD5_FAST_READ_4_4_4		BIT(4)
1923 
1924 /* 11th DWORD. */
1925 #define BFPT_DWORD11_PAGE_SIZE_SHIFT		4
1926 #define BFPT_DWORD11_PAGE_SIZE_MASK		GENMASK(7, 4)
1927 
1928 /* 15th DWORD. */
1929 
1930 /*
1931  * (from JESD216 rev B)
1932  * Quad Enable Requirements (QER):
1933  * - 000b: Device does not have a QE bit. Device detects 1-1-4 and 1-4-4
1934  *         reads based on instruction. DQ3/HOLD# functions are hold during
1935  *         instruction phase.
1936  * - 001b: QE is bit 1 of status register 2. It is set via Write Status with
1937  *         two data bytes where bit 1 of the second byte is one.
1938  *         [...]
1939  *         Writing only one byte to the status register has the side-effect of
1940  *         clearing status register 2, including the QE bit. The 100b code is
1941  *         used if writing one byte to the status register does not modify
1942  *         status register 2.
1943  * - 010b: QE is bit 6 of status register 1. It is set via Write Status with
1944  *         one data byte where bit 6 is one.
1945  *         [...]
1946  * - 011b: QE is bit 7 of status register 2. It is set via Write status
1947  *         register 2 instruction 3Eh with one data byte where bit 7 is one.
1948  *         [...]
1949  *         The status register 2 is read using instruction 3Fh.
1950  * - 100b: QE is bit 1 of status register 2. It is set via Write Status with
1951  *         two data bytes where bit 1 of the second byte is one.
1952  *         [...]
1953  *         In contrast to the 001b code, writing one byte to the status
1954  *         register does not modify status register 2.
1955  * - 101b: QE is bit 1 of status register 2. Status register 1 is read using
1956  *         Read Status instruction 05h. Status register2 is read using
1957  *         instruction 35h. QE is set via Writ Status instruction 01h with
1958  *         two data bytes where bit 1 of the second byte is one.
1959  *         [...]
1960  */
1961 #define BFPT_DWORD15_QER_MASK			GENMASK(22, 20)
1962 #define BFPT_DWORD15_QER_NONE			(0x0UL << 20) /* Micron */
1963 #define BFPT_DWORD15_QER_SR2_BIT1_BUGGY		(0x1UL << 20)
1964 #define BFPT_DWORD15_QER_SR1_BIT6		(0x2UL << 20) /* Macronix */
1965 #define BFPT_DWORD15_QER_SR2_BIT7		(0x3UL << 20)
1966 #define BFPT_DWORD15_QER_SR2_BIT1_NO_RD		(0x4UL << 20)
1967 #define BFPT_DWORD15_QER_SR2_BIT1		(0x5UL << 20) /* Spansion */
1968 #define BFPT_DWORD15_QER_NONE_111		(0x7UL << 20) /* Gigadevice */
1969 
1970 struct sfdp_bfpt {
1971 	u32	dwords[BFPT_DWORD_MAX];
1972 };
1973 
1974 /* Fast Read settings. */
1975 
1976 static void
1977 spi_nor_set_read_settings_from_bfpt(struct spi_nor_read_command *read,
1978 				    u16 half,
1979 				    enum spi_nor_protocol proto)
1980 {
1981 	read->num_mode_clocks = (half >> 5) & 0x07;
1982 	read->num_wait_states = (half >> 0) & 0x1f;
1983 	read->opcode = (half >> 8) & 0xff;
1984 	read->proto = proto;
1985 }
1986 
1987 struct sfdp_bfpt_read {
1988 	/* The Fast Read x-y-z hardware capability in params->hwcaps.mask. */
1989 	u32			hwcaps;
1990 
1991 	/*
1992 	 * The <supported_bit> bit in <supported_dword> BFPT DWORD tells us
1993 	 * whether the Fast Read x-y-z command is supported.
1994 	 */
1995 	u32			supported_dword;
1996 	u32			supported_bit;
1997 
1998 	/*
1999 	 * The half-word at offset <setting_shift> in <setting_dword> BFPT DWORD
2000 	 * encodes the op code, the number of mode clocks and the number of wait
2001 	 * states to be used by Fast Read x-y-z command.
2002 	 */
2003 	u32			settings_dword;
2004 	u32			settings_shift;
2005 
2006 	/* The SPI protocol for this Fast Read x-y-z command. */
2007 	enum spi_nor_protocol	proto;
2008 };
2009 
2010 static const struct sfdp_bfpt_read sfdp_bfpt_reads[] = {
2011 	/* Fast Read 1-1-2 */
2012 	{
2013 		SNOR_HWCAPS_READ_1_1_2,
2014 		BFPT_DWORD(1), BIT(16),	/* Supported bit */
2015 		BFPT_DWORD(4), 0,	/* Settings */
2016 		SNOR_PROTO_1_1_2,
2017 	},
2018 
2019 	/* Fast Read 1-2-2 */
2020 	{
2021 		SNOR_HWCAPS_READ_1_2_2,
2022 		BFPT_DWORD(1), BIT(20),	/* Supported bit */
2023 		BFPT_DWORD(4), 16,	/* Settings */
2024 		SNOR_PROTO_1_2_2,
2025 	},
2026 
2027 	/* Fast Read 2-2-2 */
2028 	{
2029 		SNOR_HWCAPS_READ_2_2_2,
2030 		BFPT_DWORD(5),  BIT(0),	/* Supported bit */
2031 		BFPT_DWORD(6), 16,	/* Settings */
2032 		SNOR_PROTO_2_2_2,
2033 	},
2034 
2035 	/* Fast Read 1-1-4 */
2036 	{
2037 		SNOR_HWCAPS_READ_1_1_4,
2038 		BFPT_DWORD(1), BIT(22),	/* Supported bit */
2039 		BFPT_DWORD(3), 16,	/* Settings */
2040 		SNOR_PROTO_1_1_4,
2041 	},
2042 
2043 	/* Fast Read 1-4-4 */
2044 	{
2045 		SNOR_HWCAPS_READ_1_4_4,
2046 		BFPT_DWORD(1), BIT(21),	/* Supported bit */
2047 		BFPT_DWORD(3), 0,	/* Settings */
2048 		SNOR_PROTO_1_4_4,
2049 	},
2050 
2051 	/* Fast Read 4-4-4 */
2052 	{
2053 		SNOR_HWCAPS_READ_4_4_4,
2054 		BFPT_DWORD(5), BIT(4),	/* Supported bit */
2055 		BFPT_DWORD(7), 16,	/* Settings */
2056 		SNOR_PROTO_4_4_4,
2057 	},
2058 };
2059 
2060 struct sfdp_bfpt_erase {
2061 	/*
2062 	 * The half-word at offset <shift> in DWORD <dwoard> encodes the
2063 	 * op code and erase sector size to be used by Sector Erase commands.
2064 	 */
2065 	u32			dword;
2066 	u32			shift;
2067 };
2068 
2069 static const struct sfdp_bfpt_erase sfdp_bfpt_erases[] = {
2070 	/* Erase Type 1 in DWORD8 bits[15:0] */
2071 	{BFPT_DWORD(8), 0},
2072 
2073 	/* Erase Type 2 in DWORD8 bits[31:16] */
2074 	{BFPT_DWORD(8), 16},
2075 
2076 	/* Erase Type 3 in DWORD9 bits[15:0] */
2077 	{BFPT_DWORD(9), 0},
2078 
2079 	/* Erase Type 4 in DWORD9 bits[31:16] */
2080 	{BFPT_DWORD(9), 16},
2081 };
2082 
2083 static int spi_nor_hwcaps_read2cmd(u32 hwcaps);
2084 
2085 /**
2086  * spi_nor_parse_bfpt() - read and parse the Basic Flash Parameter Table.
2087  * @nor:		pointer to a 'struct spi_nor'
2088  * @bfpt_header:	pointer to the 'struct sfdp_parameter_header' describing
2089  *			the Basic Flash Parameter Table length and version
2090  * @params:		pointer to the 'struct spi_nor_flash_parameter' to be
2091  *			filled
2092  *
2093  * The Basic Flash Parameter Table is the main and only mandatory table as
2094  * defined by the SFDP (JESD216) specification.
2095  * It provides us with the total size (memory density) of the data array and
2096  * the number of address bytes for Fast Read, Page Program and Sector Erase
2097  * commands.
2098  * For Fast READ commands, it also gives the number of mode clock cycles and
2099  * wait states (regrouped in the number of dummy clock cycles) for each
2100  * supported instruction op code.
2101  * For Page Program, the page size is now available since JESD216 rev A, however
2102  * the supported instruction op codes are still not provided.
2103  * For Sector Erase commands, this table stores the supported instruction op
2104  * codes and the associated sector sizes.
2105  * Finally, the Quad Enable Requirements (QER) are also available since JESD216
2106  * rev A. The QER bits encode the manufacturer dependent procedure to be
2107  * executed to set the Quad Enable (QE) bit in some internal register of the
2108  * Quad SPI memory. Indeed the QE bit, when it exists, must be set before
2109  * sending any Quad SPI command to the memory. Actually, setting the QE bit
2110  * tells the memory to reassign its WP# and HOLD#/RESET# pins to functions IO2
2111  * and IO3 hence enabling 4 (Quad) I/O lines.
2112  *
2113  * Return: 0 on success, -errno otherwise.
2114  */
2115 static int spi_nor_parse_bfpt(struct spi_nor *nor,
2116 			      const struct sfdp_parameter_header *bfpt_header,
2117 			      struct spi_nor_flash_parameter *params)
2118 {
2119 	struct mtd_info *mtd = &nor->mtd;
2120 	struct sfdp_bfpt bfpt;
2121 	size_t len;
2122 	int i, cmd, err;
2123 	u32 addr;
2124 	u16 half;
2125 
2126 	/* JESD216 Basic Flash Parameter Table length is at least 9 DWORDs. */
2127 	if (bfpt_header->length < BFPT_DWORD_MAX_JESD216)
2128 		return -EINVAL;
2129 
2130 	/* Read the Basic Flash Parameter Table. */
2131 	len = min_t(size_t, sizeof(bfpt),
2132 		    bfpt_header->length * sizeof(u32));
2133 	addr = SFDP_PARAM_HEADER_PTP(bfpt_header);
2134 	memset(&bfpt, 0, sizeof(bfpt));
2135 	err = spi_nor_read_sfdp(nor,  addr, len, &bfpt);
2136 	if (err < 0)
2137 		return err;
2138 
2139 	/* Fix endianness of the BFPT DWORDs. */
2140 	for (i = 0; i < BFPT_DWORD_MAX; i++)
2141 		bfpt.dwords[i] = le32_to_cpu(bfpt.dwords[i]);
2142 
2143 	/* Number of address bytes. */
2144 	switch (bfpt.dwords[BFPT_DWORD(1)] & BFPT_DWORD1_ADDRESS_BYTES_MASK) {
2145 	case BFPT_DWORD1_ADDRESS_BYTES_3_ONLY:
2146 		nor->addr_width = 3;
2147 		break;
2148 
2149 	case BFPT_DWORD1_ADDRESS_BYTES_4_ONLY:
2150 		nor->addr_width = 4;
2151 		break;
2152 
2153 	default:
2154 		break;
2155 	}
2156 
2157 	/* Flash Memory Density (in bits). */
2158 	params->size = bfpt.dwords[BFPT_DWORD(2)];
2159 	if (params->size & BIT(31)) {
2160 		params->size &= ~BIT(31);
2161 
2162 		/*
2163 		 * Prevent overflows on params->size. Anyway, a NOR of 2^64
2164 		 * bits is unlikely to exist so this error probably means
2165 		 * the BFPT we are reading is corrupted/wrong.
2166 		 */
2167 		if (params->size > 63)
2168 			return -EINVAL;
2169 
2170 		params->size = 1ULL << params->size;
2171 	} else {
2172 		params->size++;
2173 	}
2174 	params->size >>= 3; /* Convert to bytes. */
2175 
2176 	/* Fast Read settings. */
2177 	for (i = 0; i < ARRAY_SIZE(sfdp_bfpt_reads); i++) {
2178 		const struct sfdp_bfpt_read *rd = &sfdp_bfpt_reads[i];
2179 		struct spi_nor_read_command *read;
2180 
2181 		if (!(bfpt.dwords[rd->supported_dword] & rd->supported_bit)) {
2182 			params->hwcaps.mask &= ~rd->hwcaps;
2183 			continue;
2184 		}
2185 
2186 		params->hwcaps.mask |= rd->hwcaps;
2187 		cmd = spi_nor_hwcaps_read2cmd(rd->hwcaps);
2188 		read = &params->reads[cmd];
2189 		half = bfpt.dwords[rd->settings_dword] >> rd->settings_shift;
2190 		spi_nor_set_read_settings_from_bfpt(read, half, rd->proto);
2191 	}
2192 
2193 	/* Sector Erase settings. */
2194 	for (i = 0; i < ARRAY_SIZE(sfdp_bfpt_erases); i++) {
2195 		const struct sfdp_bfpt_erase *er = &sfdp_bfpt_erases[i];
2196 		u32 erasesize;
2197 		u8 opcode;
2198 
2199 		half = bfpt.dwords[er->dword] >> er->shift;
2200 		erasesize = half & 0xff;
2201 
2202 		/* erasesize == 0 means this Erase Type is not supported. */
2203 		if (!erasesize)
2204 			continue;
2205 
2206 		erasesize = 1U << erasesize;
2207 		opcode = (half >> 8) & 0xff;
2208 #ifdef CONFIG_MTD_SPI_NOR_USE_4K_SECTORS
2209 		if (erasesize == SZ_4K) {
2210 			nor->erase_opcode = opcode;
2211 			mtd->erasesize = erasesize;
2212 			break;
2213 		}
2214 #endif
2215 		if (!mtd->erasesize || mtd->erasesize < erasesize) {
2216 			nor->erase_opcode = opcode;
2217 			mtd->erasesize = erasesize;
2218 		}
2219 	}
2220 
2221 	/* Stop here if not JESD216 rev A or later. */
2222 	if (bfpt_header->length < BFPT_DWORD_MAX)
2223 		return 0;
2224 
2225 	/* Page size: this field specifies 'N' so the page size = 2^N bytes. */
2226 	params->page_size = bfpt.dwords[BFPT_DWORD(11)];
2227 	params->page_size &= BFPT_DWORD11_PAGE_SIZE_MASK;
2228 	params->page_size >>= BFPT_DWORD11_PAGE_SIZE_SHIFT;
2229 	params->page_size = 1U << params->page_size;
2230 
2231 	/* Quad Enable Requirements. */
2232 	switch (bfpt.dwords[BFPT_DWORD(15)] & BFPT_DWORD15_QER_MASK) {
2233 	case BFPT_DWORD15_QER_NONE:
2234 		params->quad_enable = sst_cr_quad_enable;
2235 		break;
2236 	case BFPT_DWORD15_QER_NONE_111:
2237 		params->quad_enable = NULL;
2238 		break;
2239 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
2240 	case BFPT_DWORD15_QER_SR2_BIT1_BUGGY:
2241 	case BFPT_DWORD15_QER_SR2_BIT1_NO_RD:
2242 		params->quad_enable = spansion_no_read_cr_quad_enable;
2243 		break;
2244 #endif
2245 #ifdef CONFIG_SPI_FLASH_MACRONIX
2246 	case BFPT_DWORD15_QER_SR1_BIT6:
2247 		params->quad_enable = macronix_quad_enable;
2248 		break;
2249 #endif
2250 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
2251 	case BFPT_DWORD15_QER_SR2_BIT1:
2252 		params->quad_enable = spansion_read_cr_quad_enable;
2253 		break;
2254 #endif
2255 	default:
2256 		return -EINVAL;
2257 	}
2258 
2259 	return 0;
2260 }
2261 
2262 /**
2263  * spi_nor_parse_sfdp() - parse the Serial Flash Discoverable Parameters.
2264  * @nor:		pointer to a 'struct spi_nor'
2265  * @params:		pointer to the 'struct spi_nor_flash_parameter' to be
2266  *			filled
2267  *
2268  * The Serial Flash Discoverable Parameters are described by the JEDEC JESD216
2269  * specification. This is a standard which tends to supported by almost all
2270  * (Q)SPI memory manufacturers. Those hard-coded tables allow us to learn at
2271  * runtime the main parameters needed to perform basic SPI flash operations such
2272  * as Fast Read, Page Program or Sector Erase commands.
2273  *
2274  * Return: 0 on success, -errno otherwise.
2275  */
2276 static int spi_nor_parse_sfdp(struct spi_nor *nor,
2277 			      struct spi_nor_flash_parameter *params)
2278 {
2279 	const struct sfdp_parameter_header *param_header, *bfpt_header;
2280 	struct sfdp_parameter_header *param_headers = NULL;
2281 	struct sfdp_header header;
2282 	size_t psize;
2283 	int i, err;
2284 
2285 	/* Get the SFDP header. */
2286 	err = spi_nor_read_sfdp(nor, 0, sizeof(header), &header);
2287 	if (err < 0)
2288 		return err;
2289 
2290 	/* Check the SFDP header version. */
2291 	if (le32_to_cpu(header.signature) != SFDP_SIGNATURE ||
2292 	    header.major != SFDP_JESD216_MAJOR)
2293 		return -EINVAL;
2294 
2295 	/*
2296 	 * Verify that the first and only mandatory parameter header is a
2297 	 * Basic Flash Parameter Table header as specified in JESD216.
2298 	 */
2299 	bfpt_header = &header.bfpt_header;
2300 	if (SFDP_PARAM_HEADER_ID(bfpt_header) != SFDP_BFPT_ID ||
2301 	    bfpt_header->major != SFDP_JESD216_MAJOR)
2302 		return -EINVAL;
2303 
2304 	/*
2305 	 * Allocate memory then read all parameter headers with a single
2306 	 * Read SFDP command. These parameter headers will actually be parsed
2307 	 * twice: a first time to get the latest revision of the basic flash
2308 	 * parameter table, then a second time to handle the supported optional
2309 	 * tables.
2310 	 * Hence we read the parameter headers once for all to reduce the
2311 	 * processing time. Also we use kmalloc() instead of devm_kmalloc()
2312 	 * because we don't need to keep these parameter headers: the allocated
2313 	 * memory is always released with kfree() before exiting this function.
2314 	 */
2315 	if (header.nph) {
2316 		psize = header.nph * sizeof(*param_headers);
2317 
2318 		param_headers = kmalloc(psize, GFP_KERNEL);
2319 		if (!param_headers)
2320 			return -ENOMEM;
2321 
2322 		err = spi_nor_read_sfdp(nor, sizeof(header),
2323 					psize, param_headers);
2324 		if (err < 0) {
2325 			dev_err(dev, "failed to read SFDP parameter headers\n");
2326 			goto exit;
2327 		}
2328 	}
2329 
2330 	/*
2331 	 * Check other parameter headers to get the latest revision of
2332 	 * the basic flash parameter table.
2333 	 */
2334 	for (i = 0; i < header.nph; i++) {
2335 		param_header = &param_headers[i];
2336 
2337 		if (SFDP_PARAM_HEADER_ID(param_header) == SFDP_BFPT_ID &&
2338 		    param_header->major == SFDP_JESD216_MAJOR &&
2339 		    (param_header->minor > bfpt_header->minor ||
2340 		     (param_header->minor == bfpt_header->minor &&
2341 		      param_header->length > bfpt_header->length)))
2342 			bfpt_header = param_header;
2343 	}
2344 
2345 	err = spi_nor_parse_bfpt(nor, bfpt_header, params);
2346 	if (err)
2347 		goto exit;
2348 
2349 	/* Parse other parameter headers. */
2350 	for (i = 0; i < header.nph; i++) {
2351 		param_header = &param_headers[i];
2352 
2353 		switch (SFDP_PARAM_HEADER_ID(param_header)) {
2354 		case SFDP_SECTOR_MAP_ID:
2355 			dev_info(dev, "non-uniform erase sector maps are not supported yet.\n");
2356 			break;
2357 
2358 		default:
2359 			break;
2360 		}
2361 
2362 		if (err)
2363 			goto exit;
2364 	}
2365 
2366 exit:
2367 	kfree(param_headers);
2368 	return err;
2369 }
2370 #else
2371 static int spi_nor_parse_sfdp(struct spi_nor *nor,
2372 			      struct spi_nor_flash_parameter *params)
2373 {
2374 	return -EINVAL;
2375 }
2376 #endif /* SPI_FLASH_SFDP_SUPPORT */
2377 
2378 static int spi_nor_init_params(struct spi_nor *nor,
2379 			       const struct flash_info *info,
2380 			       struct spi_nor_flash_parameter *params)
2381 {
2382 	int ret;
2383 	/* Set legacy flash parameters as default. */
2384 	memset(params, 0, sizeof(*params));
2385 
2386 	/* Set SPI NOR sizes. */
2387 	params->size = info->sector_size * info->n_sectors;
2388 	params->page_size = info->page_size;
2389 
2390 	/* (Fast) Read settings. */
2391 	params->hwcaps.mask |= SNOR_HWCAPS_READ;
2392 	spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ],
2393 				  0, 0, SPINOR_OP_READ,
2394 				  SNOR_PROTO_1_1_1);
2395 
2396 	if (!(info->flags & SPI_NOR_NO_FR)) {
2397 		params->hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
2398 		spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_FAST],
2399 					  0, 8, SPINOR_OP_READ_FAST,
2400 					  SNOR_PROTO_1_1_1);
2401 #ifdef CONFIG_SPI_FLASH_SPANSION
2402 		if (cypress_s25hx_t(info))
2403 			params->reads[SNOR_CMD_READ_FAST].num_mode_clocks = 8;
2404 #endif
2405 	}
2406 
2407 	if (info->flags & SPI_NOR_DUAL_READ) {
2408 		params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_2;
2409 		spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_1_1_2],
2410 					  0, 8, SPINOR_OP_READ_1_1_2,
2411 					  SNOR_PROTO_1_1_2);
2412 	}
2413 
2414 	if (info->flags & SPI_NOR_QUAD_READ) {
2415 		params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4;
2416 		spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_1_1_4],
2417 					  0, 8, SPINOR_OP_READ_1_1_4,
2418 					  SNOR_PROTO_1_1_4);
2419 	}
2420 
2421 	/* Page Program settings. */
2422 	params->hwcaps.mask |= SNOR_HWCAPS_PP;
2423 	spi_nor_set_pp_settings(&params->page_programs[SNOR_CMD_PP],
2424 				SPINOR_OP_PP, SNOR_PROTO_1_1_1);
2425 
2426 	if (info->flags & SPI_NOR_QUAD_READ) {
2427 		params->hwcaps.mask |= SNOR_HWCAPS_PP_1_1_4;
2428 		spi_nor_set_pp_settings(&params->page_programs[SNOR_CMD_PP_1_1_4],
2429 					SPINOR_OP_PP_1_1_4, SNOR_PROTO_1_1_4);
2430 	}
2431 
2432 	/* Select the procedure to set the Quad Enable bit. */
2433 	if (params->hwcaps.mask & (SNOR_HWCAPS_READ_QUAD |
2434 				   SNOR_HWCAPS_PP_QUAD)) {
2435 		switch (JEDEC_MFR(info)) {
2436 #ifdef CONFIG_SPI_FLASH_MACRONIX
2437 		case SNOR_MFR_MACRONIX:
2438 			params->quad_enable = macronix_quad_enable;
2439 			break;
2440 #endif
2441 		case SNOR_MFR_ST:
2442 		case SNOR_MFR_MICRON:
2443 		case SNOR_MFR_ISSI:
2444 			break;
2445 #ifdef CONFIG_SPI_FLASH_SPANSION
2446 		case SNOR_MFR_CYPRESS:
2447 			if (info->id[1] == 0x2a || info->id[1] == 0x2b) {
2448 				params->quad_enable = spansion_quad_enable_volatile;
2449 			}
2450 			break;
2451 #endif
2452 
2453 		default:
2454 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
2455 			/* Kept only for backward compatibility purpose. */
2456 			params->quad_enable = spansion_read_cr_quad_enable;
2457 #endif
2458 			break;
2459 		}
2460 	}
2461 
2462 	/* Override the parameters with data read from SFDP tables. */
2463 	nor->addr_width = 0;
2464 	nor->mtd.erasesize = 0;
2465 	if ((info->flags & (SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ)) &&
2466 	    !(info->flags & SPI_NOR_SKIP_SFDP)) {
2467 		struct spi_nor_flash_parameter sfdp_params;
2468 
2469 		memcpy(&sfdp_params, params, sizeof(sfdp_params));
2470 		if (spi_nor_parse_sfdp(nor, &sfdp_params)) {
2471 			nor->addr_width = 0;
2472 			nor->mtd.erasesize = 0;
2473 		} else {
2474 			memcpy(params, &sfdp_params, sizeof(*params));
2475 #ifdef CONFIG_SPI_FLASH_SPANSION
2476 			if (cypress_s25hx_t(info)) {
2477 				/* BFPT fixup */
2478 				nor->erase_opcode = SPINOR_OP_SE_4B;
2479 				nor->mtd.erasesize = info->sector_size;
2480 				ret = set_4byte(nor, info, 1);
2481 				if (ret)
2482 					return ret;
2483 
2484 				nor->addr_width = 4;
2485 
2486 				/* SFDP fixup */
2487 				/* Default page size is 256-byte, but BFPT reports 512-byte */
2488 				params->page_size = 256;
2489 				/* READ_FAST_4B (0Ch) requires mode cycles*/
2490 				params->reads[SNOR_CMD_READ_FAST].opcode = SPINOR_OP_READ_FAST_4B;
2491 				params->reads[SNOR_CMD_READ_FAST].num_mode_clocks = 8;
2492 				/* PP_1_1_4 is not supported */
2493 				params->hwcaps.mask &= ~SNOR_HWCAPS_PP_1_1_4;
2494 				/* Use volatile register to enable quad */
2495 				params->quad_enable = spansion_quad_enable_volatile;
2496 			}
2497 #endif
2498 		}
2499 
2500 		/* need to disable hold/reset pin feature */
2501 		if (JEDEC_MFR(info) == SNOR_MFR_ST)
2502 			params->quad_enable = micron_read_cr_quad_enable;
2503 
2504 		if (JEDEC_MFR(info) == SNOR_MFR_GIGADEVICE)
2505 			params->quad_enable = winbond_sr2_bit1_quad_enable;
2506 	}
2507 
2508 	return 0;
2509 }
2510 
2511 static int spi_nor_hwcaps2cmd(u32 hwcaps, const int table[][2], size_t size)
2512 {
2513 	size_t i;
2514 
2515 	for (i = 0; i < size; i++)
2516 		if (table[i][0] == (int)hwcaps)
2517 			return table[i][1];
2518 
2519 	return -EINVAL;
2520 }
2521 
2522 static int spi_nor_hwcaps_read2cmd(u32 hwcaps)
2523 {
2524 	static const int hwcaps_read2cmd[][2] = {
2525 		{ SNOR_HWCAPS_READ,		SNOR_CMD_READ },
2526 		{ SNOR_HWCAPS_READ_FAST,	SNOR_CMD_READ_FAST },
2527 		{ SNOR_HWCAPS_READ_1_1_1_DTR,	SNOR_CMD_READ_1_1_1_DTR },
2528 		{ SNOR_HWCAPS_READ_1_1_2,	SNOR_CMD_READ_1_1_2 },
2529 		{ SNOR_HWCAPS_READ_1_2_2,	SNOR_CMD_READ_1_2_2 },
2530 		{ SNOR_HWCAPS_READ_2_2_2,	SNOR_CMD_READ_2_2_2 },
2531 		{ SNOR_HWCAPS_READ_1_2_2_DTR,	SNOR_CMD_READ_1_2_2_DTR },
2532 		{ SNOR_HWCAPS_READ_1_1_4,	SNOR_CMD_READ_1_1_4 },
2533 		{ SNOR_HWCAPS_READ_1_4_4,	SNOR_CMD_READ_1_4_4 },
2534 		{ SNOR_HWCAPS_READ_4_4_4,	SNOR_CMD_READ_4_4_4 },
2535 		{ SNOR_HWCAPS_READ_1_4_4_DTR,	SNOR_CMD_READ_1_4_4_DTR },
2536 		{ SNOR_HWCAPS_READ_1_1_8,	SNOR_CMD_READ_1_1_8 },
2537 		{ SNOR_HWCAPS_READ_1_8_8,	SNOR_CMD_READ_1_8_8 },
2538 		{ SNOR_HWCAPS_READ_8_8_8,	SNOR_CMD_READ_8_8_8 },
2539 		{ SNOR_HWCAPS_READ_1_8_8_DTR,	SNOR_CMD_READ_1_8_8_DTR },
2540 	};
2541 
2542 	return spi_nor_hwcaps2cmd(hwcaps, hwcaps_read2cmd,
2543 				  ARRAY_SIZE(hwcaps_read2cmd));
2544 }
2545 
2546 static int spi_nor_hwcaps_pp2cmd(u32 hwcaps)
2547 {
2548 	static const int hwcaps_pp2cmd[][2] = {
2549 		{ SNOR_HWCAPS_PP,		SNOR_CMD_PP },
2550 		{ SNOR_HWCAPS_PP_1_1_4,		SNOR_CMD_PP_1_1_4 },
2551 		{ SNOR_HWCAPS_PP_1_4_4,		SNOR_CMD_PP_1_4_4 },
2552 		{ SNOR_HWCAPS_PP_4_4_4,		SNOR_CMD_PP_4_4_4 },
2553 		{ SNOR_HWCAPS_PP_1_1_8,		SNOR_CMD_PP_1_1_8 },
2554 		{ SNOR_HWCAPS_PP_1_8_8,		SNOR_CMD_PP_1_8_8 },
2555 		{ SNOR_HWCAPS_PP_8_8_8,		SNOR_CMD_PP_8_8_8 },
2556 	};
2557 
2558 	return spi_nor_hwcaps2cmd(hwcaps, hwcaps_pp2cmd,
2559 				  ARRAY_SIZE(hwcaps_pp2cmd));
2560 }
2561 
2562 static int spi_nor_select_read(struct spi_nor *nor,
2563 			       const struct spi_nor_flash_parameter *params,
2564 			       u32 shared_hwcaps)
2565 {
2566 	int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_READ_MASK) - 1;
2567 	const struct spi_nor_read_command *read;
2568 
2569 	if (best_match < 0)
2570 		return -EINVAL;
2571 
2572 	cmd = spi_nor_hwcaps_read2cmd(BIT(best_match));
2573 	if (cmd < 0)
2574 		return -EINVAL;
2575 
2576 	read = &params->reads[cmd];
2577 	nor->read_opcode = read->opcode;
2578 	nor->read_proto = read->proto;
2579 
2580 	/*
2581 	 * In the spi-nor framework, we don't need to make the difference
2582 	 * between mode clock cycles and wait state clock cycles.
2583 	 * Indeed, the value of the mode clock cycles is used by a QSPI
2584 	 * flash memory to know whether it should enter or leave its 0-4-4
2585 	 * (Continuous Read / XIP) mode.
2586 	 * eXecution In Place is out of the scope of the mtd sub-system.
2587 	 * Hence we choose to merge both mode and wait state clock cycles
2588 	 * into the so called dummy clock cycles.
2589 	 */
2590 	nor->read_dummy = read->num_mode_clocks + read->num_wait_states;
2591 	return 0;
2592 }
2593 
2594 static int spi_nor_select_pp(struct spi_nor *nor,
2595 			     const struct spi_nor_flash_parameter *params,
2596 			     u32 shared_hwcaps)
2597 {
2598 	int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_PP_MASK) - 1;
2599 	const struct spi_nor_pp_command *pp;
2600 
2601 	if (best_match < 0)
2602 		return -EINVAL;
2603 
2604 	cmd = spi_nor_hwcaps_pp2cmd(BIT(best_match));
2605 	if (cmd < 0)
2606 		return -EINVAL;
2607 
2608 	pp = &params->page_programs[cmd];
2609 	nor->program_opcode = pp->opcode;
2610 	nor->write_proto = pp->proto;
2611 	return 0;
2612 }
2613 
2614 static int spi_nor_select_erase(struct spi_nor *nor,
2615 				const struct flash_info *info)
2616 {
2617 	struct mtd_info *mtd = &nor->mtd;
2618 
2619 	/* Do nothing if already configured from SFDP. */
2620 	if (mtd->erasesize)
2621 		return 0;
2622 
2623 #ifdef CONFIG_SPI_FLASH_USE_4K_SECTORS
2624 	/* prefer "small sector" erase if possible */
2625 	if (info->flags & SECT_4K) {
2626 		nor->erase_opcode = SPINOR_OP_BE_4K;
2627 		mtd->erasesize = 4096;
2628 	} else if (info->flags & SECT_4K_PMC) {
2629 		nor->erase_opcode = SPINOR_OP_BE_4K_PMC;
2630 		mtd->erasesize = 4096;
2631 	} else
2632 #endif
2633 	{
2634 		nor->erase_opcode = SPINOR_OP_SE;
2635 		mtd->erasesize = info->sector_size;
2636 	}
2637 	return 0;
2638 }
2639 
2640 static int spi_nor_setup(struct spi_nor *nor, const struct flash_info *info,
2641 			 const struct spi_nor_flash_parameter *params,
2642 			 const struct spi_nor_hwcaps *hwcaps)
2643 {
2644 	u32 ignored_mask, shared_mask;
2645 	bool enable_quad_io;
2646 	int err;
2647 
2648 	/*
2649 	 * Keep only the hardware capabilities supported by both the SPI
2650 	 * controller and the SPI flash memory.
2651 	 */
2652 	shared_mask = hwcaps->mask & params->hwcaps.mask;
2653 
2654 	/* SPI n-n-n protocols are not supported yet. */
2655 	ignored_mask = (SNOR_HWCAPS_READ_1_1_1_DTR |
2656 			SNOR_HWCAPS_READ_1_2_2 |
2657 			SNOR_HWCAPS_READ_1_2_2_DTR |
2658 			SNOR_HWCAPS_READ_2_2_2 |
2659 			SNOR_HWCAPS_READ_1_4_4 |
2660 			SNOR_HWCAPS_READ_1_4_4_DTR |
2661 			SNOR_HWCAPS_READ_4_4_4 |
2662 			SNOR_HWCAPS_READ_8_8_8 |
2663 			SNOR_HWCAPS_PP_1_4_4 |
2664 			SNOR_HWCAPS_PP_4_4_4 |
2665 			SNOR_HWCAPS_PP_8_8_8);
2666 	if (shared_mask & ignored_mask) {
2667 		dev_dbg(nor->dev,
2668 			"SPI n-n-n protocols are not supported yet.\n");
2669 		shared_mask &= ~ignored_mask;
2670 	}
2671 
2672 	/* Select the (Fast) Read command. */
2673 	err = spi_nor_select_read(nor, params, shared_mask);
2674 	if (err) {
2675 		dev_dbg(nor->dev,
2676 			"can't select read settings supported by both the SPI controller and memory.\n");
2677 		return err;
2678 	}
2679 
2680 	/* Select the Page Program command. */
2681 	err = spi_nor_select_pp(nor, params, shared_mask);
2682 	if (err) {
2683 		dev_dbg(nor->dev,
2684 			"can't select write settings supported by both the SPI controller and memory.\n");
2685 		return err;
2686 	}
2687 
2688 	/* Select the Sector Erase command. */
2689 	err = spi_nor_select_erase(nor, info);
2690 	if (err) {
2691 		dev_dbg(nor->dev,
2692 			"can't select erase settings supported by both the SPI controller and memory.\n");
2693 		return err;
2694 	}
2695 
2696 	/* Enable Quad I/O if needed. */
2697 	enable_quad_io = (spi_nor_get_protocol_width(nor->read_proto) == 4 ||
2698 			  spi_nor_get_protocol_width(nor->write_proto) == 4);
2699 	if (enable_quad_io && params->quad_enable)
2700 		nor->quad_enable = params->quad_enable;
2701 	else
2702 		nor->quad_enable = NULL;
2703 
2704 	return 0;
2705 }
2706 
2707 static int spi_nor_init(struct spi_nor *nor)
2708 {
2709 	int err;
2710 
2711 	/*
2712 	 * Atmel, SST, Intel/Numonyx, and others serial NOR tend to power up
2713 	 * with the software protection bits set
2714 	 */
2715 	if (JEDEC_MFR(nor->info) == SNOR_MFR_ATMEL ||
2716 	    JEDEC_MFR(nor->info) == SNOR_MFR_INTEL ||
2717 	    JEDEC_MFR(nor->info) == SNOR_MFR_SST ||
2718 	    nor->info->flags & SPI_NOR_HAS_LOCK) {
2719 		write_enable(nor);
2720 		write_sr(nor, 0);
2721 		spi_nor_wait_till_ready(nor);
2722 	}
2723 
2724 	if (nor->quad_enable) {
2725 		err = nor->quad_enable(nor);
2726 		if (err) {
2727 			dev_dbg(nor->dev, "quad mode not supported\n");
2728 			return err;
2729 		}
2730 	}
2731 
2732 	if (nor->addr_width == 4 &&
2733 	    (JEDEC_MFR(nor->info) != SNOR_MFR_SPANSION)) {
2734 
2735 		/*
2736 		 * If the RESET# pin isn't hooked up properly, or the system
2737 		 * otherwise doesn't perform a reset command in the boot
2738 		 * sequence, it's impossible to 100% protect against unexpected
2739 		 * reboots (e.g., crashes). Warn the user (or hopefully, system
2740 		 * designer) that this is bad.
2741 		 */
2742 		if (nor->flags & SNOR_F_BROKEN_RESET)
2743 			printf("enabling reset hack; may not recover from unexpected reboots\n");
2744 		set_4byte(nor, nor->info, 1);
2745 	}
2746 
2747 	return 0;
2748 }
2749 
2750 int spi_nor_scan(struct spi_nor *nor)
2751 {
2752 	struct spi_nor_flash_parameter params;
2753 	const struct flash_info *info = NULL;
2754 	struct mtd_info *mtd = &nor->mtd;
2755 	struct spi_nor_hwcaps hwcaps = {
2756 		.mask = SNOR_HWCAPS_READ |
2757 			SNOR_HWCAPS_READ_FAST |
2758 			SNOR_HWCAPS_PP,
2759 	};
2760 	struct spi_slave *spi = nor->spi;
2761 	int ret;
2762 
2763 	/* Reset SPI protocol for all commands. */
2764 	nor->reg_proto = SNOR_PROTO_1_1_1;
2765 	nor->read_proto = SNOR_PROTO_1_1_1;
2766 	nor->write_proto = SNOR_PROTO_1_1_1;
2767 	nor->read = spi_nor_read_data;
2768 	nor->write = spi_nor_write_data;
2769 	nor->read_reg = spi_nor_read_reg;
2770 	nor->write_reg = spi_nor_write_reg;
2771 
2772 	if (spi->mode & SPI_RX_QUAD) {
2773 		hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4;
2774 
2775 		if (spi->mode & SPI_TX_QUAD)
2776 			hwcaps.mask |= (SNOR_HWCAPS_READ_1_4_4 |
2777 					SNOR_HWCAPS_PP_1_1_4 |
2778 					SNOR_HWCAPS_PP_1_4_4);
2779 	} else if (spi->mode & SPI_RX_DUAL) {
2780 		hwcaps.mask |= SNOR_HWCAPS_READ_1_1_2;
2781 
2782 		if (spi->mode & SPI_TX_DUAL)
2783 			hwcaps.mask |= SNOR_HWCAPS_READ_1_2_2;
2784 	}
2785 
2786 	info = spi_nor_read_id(nor);
2787 	if (IS_ERR_OR_NULL(info))
2788 		return -ENOENT;
2789 	/* Parse the Serial Flash Discoverable Parameters table. */
2790 	ret = spi_nor_init_params(nor, info, &params);
2791 	if (ret)
2792 		return ret;
2793 
2794 	if (!mtd->name)
2795 		mtd->name = info->name;
2796 	mtd->priv = nor;
2797 	mtd->type = MTD_NORFLASH;
2798 	mtd->writesize = 1;
2799 	mtd->flags = MTD_CAP_NORFLASH;
2800 	mtd->size = params.size;
2801 	mtd->_erase = spi_nor_erase;
2802 	mtd->_read = spi_nor_read;
2803 
2804 #if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST)
2805 	/* NOR protection support for STmicro/Micron chips and similar */
2806 	if (JEDEC_MFR(info) == SNOR_MFR_ST ||
2807 	    JEDEC_MFR(info) == SNOR_MFR_MICRON ||
2808 	    JEDEC_MFR(info) == SNOR_MFR_SST ||
2809 			info->flags & SPI_NOR_HAS_LOCK) {
2810 		nor->flash_lock = stm_lock;
2811 		nor->flash_unlock = stm_unlock;
2812 		nor->flash_is_locked = stm_is_locked;
2813 	}
2814 #endif
2815 
2816 #ifdef CONFIG_SPI_FLASH_SST
2817 	/* sst nor chips use AAI word program */
2818 	if (info->flags & SST_WRITE)
2819 		mtd->_write = sst_write;
2820 	else
2821 #endif
2822 		mtd->_write = spi_nor_write;
2823 
2824 	if (info->flags & USE_FSR)
2825 		nor->flags |= SNOR_F_USE_FSR;
2826 	if (info->flags & SPI_NOR_HAS_TB)
2827 		nor->flags |= SNOR_F_HAS_SR_TB;
2828 	if (info->flags & NO_CHIP_ERASE)
2829 		nor->flags |= SNOR_F_NO_OP_CHIP_ERASE;
2830 	if (info->flags & USE_CLSR)
2831 		nor->flags |= SNOR_F_USE_CLSR;
2832 
2833 	if (info->flags & SPI_NOR_NO_ERASE)
2834 		mtd->flags |= MTD_NO_ERASE;
2835 
2836 	nor->page_size = params.page_size;
2837 	mtd->writebufsize = nor->page_size;
2838 
2839 #ifdef CONFIG_SPI_FLASH_SPANSION
2840 	if (cypress_s25hx_t(info)) {
2841 		/*
2842 		 * The Cypress Semper family has transparent ECC. To preserve
2843 		 * ECC enabled, multi-pass programming within the same 16-byte
2844 		 * ECC data unit needs to be avoided. Set writesize to the page
2845 		 * size and remove the MTD_BIT_WRITEABLE flag in mtd_info to
2846 		 * prevent multi-pass programming.
2847 		 */
2848 		nor->mtd.writesize = params.page_size;
2849 		nor->mtd.flags &= ~MTD_BIT_WRITEABLE;
2850 		ret = s25hx_t_setup(nor, info);
2851 		if (ret) {
2852 			dev_err(nor->dev, "fail to setup s25hx_t flash\n");
2853 			return ret;
2854 		}
2855 	}
2856 #endif
2857 
2858 	/* Some devices cannot do fast-read, no matter what DT tells us */
2859 	if ((info->flags & SPI_NOR_NO_FR) || (spi->mode & SPI_RX_SLOW))
2860 		params.hwcaps.mask &= ~SNOR_HWCAPS_READ_FAST;
2861 
2862 	/*
2863 	 * Configure the SPI memory:
2864 	 * - select op codes for (Fast) Read, Page Program and Sector Erase.
2865 	 * - set the number of dummy cycles (mode cycles + wait states).
2866 	 * - set the SPI protocols for register and memory accesses.
2867 	 * - set the Quad Enable bit if needed (required by SPI x-y-4 protos).
2868 	 */
2869 	ret = spi_nor_setup(nor, info, &params, &hwcaps);
2870 	if (ret)
2871 		return ret;
2872 
2873 	if (nor->addr_width) {
2874 		/* already configured from SFDP */
2875 	} else if (info->addr_width) {
2876 		nor->addr_width = info->addr_width;
2877 	} else if (mtd->size > SZ_16M) {
2878 #ifndef CONFIG_SPI_FLASH_BAR
2879 		/* enable 4-byte addressing if the device exceeds 16MiB */
2880 		nor->addr_width = 4;
2881 		if (JEDEC_MFR(info) == SNOR_MFR_SPANSION ||
2882 		    info->flags & SPI_NOR_4B_OPCODES)
2883 			spi_nor_set_4byte_opcodes(nor, info);
2884 #else
2885 		/* Configure the BAR - discover bank cmds and read current bank */
2886 		nor->addr_width = 3;
2887 		ret = read_bar(nor, info);
2888 		if (ret < 0)
2889 			return ret;
2890 #endif
2891 	} else {
2892 		nor->addr_width = 3;
2893 	}
2894 
2895 	if (nor->addr_width > SPI_NOR_MAX_ADDR_WIDTH) {
2896 		dev_dbg(dev, "address width is too large: %u\n",
2897 			nor->addr_width);
2898 		return -EINVAL;
2899 	}
2900 
2901 	/* Send all the required SPI flash commands to initialize device */
2902 	nor->info = info;
2903 	ret = spi_nor_init(nor);
2904 	if (ret)
2905 		return ret;
2906 
2907 	nor->name = mtd->name;
2908 	nor->size = mtd->size;
2909 	nor->erase_size = mtd->erasesize;
2910 	nor->sector_size = mtd->erasesize;
2911 
2912 #ifndef CONFIG_SPL_BUILD
2913 	printf("SF: Detected %s with page size ", nor->name);
2914 	print_size(nor->page_size, ", erase size ");
2915 	print_size(nor->erase_size, ", total ");
2916 	print_size(nor->size, "");
2917 	puts("\n");
2918 #endif
2919 
2920 	return 0;
2921 }
2922 
2923 /* U-Boot specific functions, need to extend MTD to support these */
2924 int spi_flash_cmd_get_sw_write_prot(struct spi_nor *nor)
2925 {
2926 	int sr = read_sr(nor);
2927 
2928 	if (sr < 0)
2929 		return sr;
2930 
2931 	return (sr >> 2) & 7;
2932 }
2933