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