xref: /openbmc/linux/drivers/mtd/spi-nor/core.c (revision e8254a8e)
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 
10 #include <linux/err.h>
11 #include <linux/errno.h>
12 #include <linux/module.h>
13 #include <linux/device.h>
14 #include <linux/mutex.h>
15 #include <linux/math64.h>
16 #include <linux/sizes.h>
17 #include <linux/slab.h>
18 
19 #include <linux/mtd/mtd.h>
20 #include <linux/of_platform.h>
21 #include <linux/sched/task_stack.h>
22 #include <linux/spi/flash.h>
23 #include <linux/mtd/spi-nor.h>
24 
25 #include "core.h"
26 
27 /* Define max times to check status register before we give up. */
28 
29 /*
30  * For everything but full-chip erase; probably could be much smaller, but kept
31  * around for safety for now
32  */
33 #define DEFAULT_READY_WAIT_JIFFIES		(40UL * HZ)
34 
35 /*
36  * For full-chip erase, calibrated to a 2MB flash (M25P16); should be scaled up
37  * for larger flash
38  */
39 #define CHIP_ERASE_2MB_READY_WAIT_JIFFIES	(40UL * HZ)
40 
41 #define SPI_NOR_MAX_ADDR_NBYTES	4
42 
43 #define SPI_NOR_SRST_SLEEP_MIN 200
44 #define SPI_NOR_SRST_SLEEP_MAX 400
45 
46 /**
47  * spi_nor_get_cmd_ext() - Get the command opcode extension based on the
48  *			   extension type.
49  * @nor:		pointer to a 'struct spi_nor'
50  * @op:			pointer to the 'struct spi_mem_op' whose properties
51  *			need to be initialized.
52  *
53  * Right now, only "repeat" and "invert" are supported.
54  *
55  * Return: The opcode extension.
56  */
57 static u8 spi_nor_get_cmd_ext(const struct spi_nor *nor,
58 			      const struct spi_mem_op *op)
59 {
60 	switch (nor->cmd_ext_type) {
61 	case SPI_NOR_EXT_INVERT:
62 		return ~op->cmd.opcode;
63 
64 	case SPI_NOR_EXT_REPEAT:
65 		return op->cmd.opcode;
66 
67 	default:
68 		dev_err(nor->dev, "Unknown command extension type\n");
69 		return 0;
70 	}
71 }
72 
73 /**
74  * spi_nor_spimem_setup_op() - Set up common properties of a spi-mem op.
75  * @nor:		pointer to a 'struct spi_nor'
76  * @op:			pointer to the 'struct spi_mem_op' whose properties
77  *			need to be initialized.
78  * @proto:		the protocol from which the properties need to be set.
79  */
80 void spi_nor_spimem_setup_op(const struct spi_nor *nor,
81 			     struct spi_mem_op *op,
82 			     const enum spi_nor_protocol proto)
83 {
84 	u8 ext;
85 
86 	op->cmd.buswidth = spi_nor_get_protocol_inst_nbits(proto);
87 
88 	if (op->addr.nbytes)
89 		op->addr.buswidth = spi_nor_get_protocol_addr_nbits(proto);
90 
91 	if (op->dummy.nbytes)
92 		op->dummy.buswidth = spi_nor_get_protocol_addr_nbits(proto);
93 
94 	if (op->data.nbytes)
95 		op->data.buswidth = spi_nor_get_protocol_data_nbits(proto);
96 
97 	if (spi_nor_protocol_is_dtr(proto)) {
98 		/*
99 		 * SPIMEM supports mixed DTR modes, but right now we can only
100 		 * have all phases either DTR or STR. IOW, SPIMEM can have
101 		 * something like 4S-4D-4D, but SPI NOR can't. So, set all 4
102 		 * phases to either DTR or STR.
103 		 */
104 		op->cmd.dtr = true;
105 		op->addr.dtr = true;
106 		op->dummy.dtr = true;
107 		op->data.dtr = true;
108 
109 		/* 2 bytes per clock cycle in DTR mode. */
110 		op->dummy.nbytes *= 2;
111 
112 		ext = spi_nor_get_cmd_ext(nor, op);
113 		op->cmd.opcode = (op->cmd.opcode << 8) | ext;
114 		op->cmd.nbytes = 2;
115 	}
116 }
117 
118 /**
119  * spi_nor_spimem_bounce() - check if a bounce buffer is needed for the data
120  *                           transfer
121  * @nor:        pointer to 'struct spi_nor'
122  * @op:         pointer to 'struct spi_mem_op' template for transfer
123  *
124  * If we have to use the bounce buffer, the data field in @op will be updated.
125  *
126  * Return: true if the bounce buffer is needed, false if not
127  */
128 static bool spi_nor_spimem_bounce(struct spi_nor *nor, struct spi_mem_op *op)
129 {
130 	/* op->data.buf.in occupies the same memory as op->data.buf.out */
131 	if (object_is_on_stack(op->data.buf.in) ||
132 	    !virt_addr_valid(op->data.buf.in)) {
133 		if (op->data.nbytes > nor->bouncebuf_size)
134 			op->data.nbytes = nor->bouncebuf_size;
135 		op->data.buf.in = nor->bouncebuf;
136 		return true;
137 	}
138 
139 	return false;
140 }
141 
142 /**
143  * spi_nor_spimem_exec_op() - execute a memory operation
144  * @nor:        pointer to 'struct spi_nor'
145  * @op:         pointer to 'struct spi_mem_op' template for transfer
146  *
147  * Return: 0 on success, -error otherwise.
148  */
149 static int spi_nor_spimem_exec_op(struct spi_nor *nor, struct spi_mem_op *op)
150 {
151 	int error;
152 
153 	error = spi_mem_adjust_op_size(nor->spimem, op);
154 	if (error)
155 		return error;
156 
157 	return spi_mem_exec_op(nor->spimem, op);
158 }
159 
160 int spi_nor_controller_ops_read_reg(struct spi_nor *nor, u8 opcode,
161 				    u8 *buf, size_t len)
162 {
163 	if (spi_nor_protocol_is_dtr(nor->reg_proto))
164 		return -EOPNOTSUPP;
165 
166 	return nor->controller_ops->read_reg(nor, opcode, buf, len);
167 }
168 
169 int spi_nor_controller_ops_write_reg(struct spi_nor *nor, u8 opcode,
170 				     const u8 *buf, size_t len)
171 {
172 	if (spi_nor_protocol_is_dtr(nor->reg_proto))
173 		return -EOPNOTSUPP;
174 
175 	return nor->controller_ops->write_reg(nor, opcode, buf, len);
176 }
177 
178 static int spi_nor_controller_ops_erase(struct spi_nor *nor, loff_t offs)
179 {
180 	if (spi_nor_protocol_is_dtr(nor->reg_proto))
181 		return -EOPNOTSUPP;
182 
183 	return nor->controller_ops->erase(nor, offs);
184 }
185 
186 /**
187  * spi_nor_spimem_read_data() - read data from flash's memory region via
188  *                              spi-mem
189  * @nor:        pointer to 'struct spi_nor'
190  * @from:       offset to read from
191  * @len:        number of bytes to read
192  * @buf:        pointer to dst buffer
193  *
194  * Return: number of bytes read successfully, -errno otherwise
195  */
196 static ssize_t spi_nor_spimem_read_data(struct spi_nor *nor, loff_t from,
197 					size_t len, u8 *buf)
198 {
199 	struct spi_mem_op op =
200 		SPI_MEM_OP(SPI_MEM_OP_CMD(nor->read_opcode, 0),
201 			   SPI_MEM_OP_ADDR(nor->addr_nbytes, from, 0),
202 			   SPI_MEM_OP_DUMMY(nor->read_dummy, 0),
203 			   SPI_MEM_OP_DATA_IN(len, buf, 0));
204 	bool usebouncebuf;
205 	ssize_t nbytes;
206 	int error;
207 
208 	spi_nor_spimem_setup_op(nor, &op, nor->read_proto);
209 
210 	/* convert the dummy cycles to the number of bytes */
211 	op.dummy.nbytes = (nor->read_dummy * op.dummy.buswidth) / 8;
212 	if (spi_nor_protocol_is_dtr(nor->read_proto))
213 		op.dummy.nbytes *= 2;
214 
215 	usebouncebuf = spi_nor_spimem_bounce(nor, &op);
216 
217 	if (nor->dirmap.rdesc) {
218 		nbytes = spi_mem_dirmap_read(nor->dirmap.rdesc, op.addr.val,
219 					     op.data.nbytes, op.data.buf.in);
220 	} else {
221 		error = spi_nor_spimem_exec_op(nor, &op);
222 		if (error)
223 			return error;
224 		nbytes = op.data.nbytes;
225 	}
226 
227 	if (usebouncebuf && nbytes > 0)
228 		memcpy(buf, op.data.buf.in, nbytes);
229 
230 	return nbytes;
231 }
232 
233 /**
234  * spi_nor_read_data() - read data from flash memory
235  * @nor:        pointer to 'struct spi_nor'
236  * @from:       offset to read from
237  * @len:        number of bytes to read
238  * @buf:        pointer to dst buffer
239  *
240  * Return: number of bytes read successfully, -errno otherwise
241  */
242 ssize_t spi_nor_read_data(struct spi_nor *nor, loff_t from, size_t len, u8 *buf)
243 {
244 	if (nor->spimem)
245 		return spi_nor_spimem_read_data(nor, from, len, buf);
246 
247 	return nor->controller_ops->read(nor, from, len, buf);
248 }
249 
250 /**
251  * spi_nor_spimem_write_data() - write data to flash memory via
252  *                               spi-mem
253  * @nor:        pointer to 'struct spi_nor'
254  * @to:         offset to write to
255  * @len:        number of bytes to write
256  * @buf:        pointer to src buffer
257  *
258  * Return: number of bytes written successfully, -errno otherwise
259  */
260 static ssize_t spi_nor_spimem_write_data(struct spi_nor *nor, loff_t to,
261 					 size_t len, const u8 *buf)
262 {
263 	struct spi_mem_op op =
264 		SPI_MEM_OP(SPI_MEM_OP_CMD(nor->program_opcode, 0),
265 			   SPI_MEM_OP_ADDR(nor->addr_nbytes, to, 0),
266 			   SPI_MEM_OP_NO_DUMMY,
267 			   SPI_MEM_OP_DATA_OUT(len, buf, 0));
268 	ssize_t nbytes;
269 	int error;
270 
271 	if (nor->program_opcode == SPINOR_OP_AAI_WP && nor->sst_write_second)
272 		op.addr.nbytes = 0;
273 
274 	spi_nor_spimem_setup_op(nor, &op, nor->write_proto);
275 
276 	if (spi_nor_spimem_bounce(nor, &op))
277 		memcpy(nor->bouncebuf, buf, op.data.nbytes);
278 
279 	if (nor->dirmap.wdesc) {
280 		nbytes = spi_mem_dirmap_write(nor->dirmap.wdesc, op.addr.val,
281 					      op.data.nbytes, op.data.buf.out);
282 	} else {
283 		error = spi_nor_spimem_exec_op(nor, &op);
284 		if (error)
285 			return error;
286 		nbytes = op.data.nbytes;
287 	}
288 
289 	return nbytes;
290 }
291 
292 /**
293  * spi_nor_write_data() - write data to flash memory
294  * @nor:        pointer to 'struct spi_nor'
295  * @to:         offset to write to
296  * @len:        number of bytes to write
297  * @buf:        pointer to src buffer
298  *
299  * Return: number of bytes written successfully, -errno otherwise
300  */
301 ssize_t spi_nor_write_data(struct spi_nor *nor, loff_t to, size_t len,
302 			   const u8 *buf)
303 {
304 	if (nor->spimem)
305 		return spi_nor_spimem_write_data(nor, to, len, buf);
306 
307 	return nor->controller_ops->write(nor, to, len, buf);
308 }
309 
310 /**
311  * spi_nor_read_any_reg() - read any register from flash memory, nonvolatile or
312  * volatile.
313  * @nor:        pointer to 'struct spi_nor'.
314  * @op:		SPI memory operation. op->data.buf must be DMA-able.
315  * @proto:	SPI protocol to use for the register operation.
316  *
317  * Return: zero on success, -errno otherwise
318  */
319 int spi_nor_read_any_reg(struct spi_nor *nor, struct spi_mem_op *op,
320 			 enum spi_nor_protocol proto)
321 {
322 	if (!nor->spimem)
323 		return -EOPNOTSUPP;
324 
325 	spi_nor_spimem_setup_op(nor, op, proto);
326 	return spi_nor_spimem_exec_op(nor, op);
327 }
328 
329 /**
330  * spi_nor_write_any_volatile_reg() - write any volatile register to flash
331  * memory.
332  * @nor:        pointer to 'struct spi_nor'
333  * @op:		SPI memory operation. op->data.buf must be DMA-able.
334  * @proto:	SPI protocol to use for the register operation.
335  *
336  * Writing volatile registers are instant according to some manufacturers
337  * (Cypress, Micron) and do not need any status polling.
338  *
339  * Return: zero on success, -errno otherwise
340  */
341 int spi_nor_write_any_volatile_reg(struct spi_nor *nor, struct spi_mem_op *op,
342 				   enum spi_nor_protocol proto)
343 {
344 	int ret;
345 
346 	if (!nor->spimem)
347 		return -EOPNOTSUPP;
348 
349 	ret = spi_nor_write_enable(nor);
350 	if (ret)
351 		return ret;
352 	spi_nor_spimem_setup_op(nor, op, proto);
353 	return spi_nor_spimem_exec_op(nor, op);
354 }
355 
356 /**
357  * spi_nor_write_enable() - Set write enable latch with Write Enable command.
358  * @nor:	pointer to 'struct spi_nor'.
359  *
360  * Return: 0 on success, -errno otherwise.
361  */
362 int spi_nor_write_enable(struct spi_nor *nor)
363 {
364 	int ret;
365 
366 	if (nor->spimem) {
367 		struct spi_mem_op op = SPI_NOR_WREN_OP;
368 
369 		spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
370 
371 		ret = spi_mem_exec_op(nor->spimem, &op);
372 	} else {
373 		ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_WREN,
374 						       NULL, 0);
375 	}
376 
377 	if (ret)
378 		dev_dbg(nor->dev, "error %d on Write Enable\n", ret);
379 
380 	return ret;
381 }
382 
383 /**
384  * spi_nor_write_disable() - Send Write Disable instruction to the chip.
385  * @nor:	pointer to 'struct spi_nor'.
386  *
387  * Return: 0 on success, -errno otherwise.
388  */
389 int spi_nor_write_disable(struct spi_nor *nor)
390 {
391 	int ret;
392 
393 	if (nor->spimem) {
394 		struct spi_mem_op op = SPI_NOR_WRDI_OP;
395 
396 		spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
397 
398 		ret = spi_mem_exec_op(nor->spimem, &op);
399 	} else {
400 		ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_WRDI,
401 						       NULL, 0);
402 	}
403 
404 	if (ret)
405 		dev_dbg(nor->dev, "error %d on Write Disable\n", ret);
406 
407 	return ret;
408 }
409 
410 /**
411  * spi_nor_read_id() - Read the JEDEC ID.
412  * @nor:	pointer to 'struct spi_nor'.
413  * @naddr:	number of address bytes to send. Can be zero if the operation
414  *		does not need to send an address.
415  * @ndummy:	number of dummy bytes to send after an opcode or address. Can
416  *		be zero if the operation does not require dummy bytes.
417  * @id:		pointer to a DMA-able buffer where the value of the JEDEC ID
418  *		will be written.
419  * @proto:	the SPI protocol for register operation.
420  *
421  * Return: 0 on success, -errno otherwise.
422  */
423 int spi_nor_read_id(struct spi_nor *nor, u8 naddr, u8 ndummy, u8 *id,
424 		    enum spi_nor_protocol proto)
425 {
426 	int ret;
427 
428 	if (nor->spimem) {
429 		struct spi_mem_op op =
430 			SPI_NOR_READID_OP(naddr, ndummy, id, SPI_NOR_MAX_ID_LEN);
431 
432 		spi_nor_spimem_setup_op(nor, &op, proto);
433 		ret = spi_mem_exec_op(nor->spimem, &op);
434 	} else {
435 		ret = nor->controller_ops->read_reg(nor, SPINOR_OP_RDID, id,
436 						    SPI_NOR_MAX_ID_LEN);
437 	}
438 	return ret;
439 }
440 
441 /**
442  * spi_nor_read_sr() - Read the Status Register.
443  * @nor:	pointer to 'struct spi_nor'.
444  * @sr:		pointer to a DMA-able buffer where the value of the
445  *              Status Register will be written. Should be at least 2 bytes.
446  *
447  * Return: 0 on success, -errno otherwise.
448  */
449 int spi_nor_read_sr(struct spi_nor *nor, u8 *sr)
450 {
451 	int ret;
452 
453 	if (nor->spimem) {
454 		struct spi_mem_op op = SPI_NOR_RDSR_OP(sr);
455 
456 		if (nor->reg_proto == SNOR_PROTO_8_8_8_DTR) {
457 			op.addr.nbytes = nor->params->rdsr_addr_nbytes;
458 			op.dummy.nbytes = nor->params->rdsr_dummy;
459 			/*
460 			 * We don't want to read only one byte in DTR mode. So,
461 			 * read 2 and then discard the second byte.
462 			 */
463 			op.data.nbytes = 2;
464 		}
465 
466 		spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
467 
468 		ret = spi_mem_exec_op(nor->spimem, &op);
469 	} else {
470 		ret = spi_nor_controller_ops_read_reg(nor, SPINOR_OP_RDSR, sr,
471 						      1);
472 	}
473 
474 	if (ret)
475 		dev_dbg(nor->dev, "error %d reading SR\n", ret);
476 
477 	return ret;
478 }
479 
480 /**
481  * spi_nor_read_cr() - Read the Configuration Register using the
482  * SPINOR_OP_RDCR (35h) command.
483  * @nor:	pointer to 'struct spi_nor'
484  * @cr:		pointer to a DMA-able buffer where the value of the
485  *              Configuration Register will be written.
486  *
487  * Return: 0 on success, -errno otherwise.
488  */
489 int spi_nor_read_cr(struct spi_nor *nor, u8 *cr)
490 {
491 	int ret;
492 
493 	if (nor->spimem) {
494 		struct spi_mem_op op = SPI_NOR_RDCR_OP(cr);
495 
496 		spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
497 
498 		ret = spi_mem_exec_op(nor->spimem, &op);
499 	} else {
500 		ret = spi_nor_controller_ops_read_reg(nor, SPINOR_OP_RDCR, cr,
501 						      1);
502 	}
503 
504 	if (ret)
505 		dev_dbg(nor->dev, "error %d reading CR\n", ret);
506 
507 	return ret;
508 }
509 
510 /**
511  * spi_nor_set_4byte_addr_mode() - Enter/Exit 4-byte address mode.
512  * @nor:	pointer to 'struct spi_nor'.
513  * @enable:	true to enter the 4-byte address mode, false to exit the 4-byte
514  *		address mode.
515  *
516  * Return: 0 on success, -errno otherwise.
517  */
518 int spi_nor_set_4byte_addr_mode(struct spi_nor *nor, bool enable)
519 {
520 	int ret;
521 
522 	if (nor->spimem) {
523 		struct spi_mem_op op = SPI_NOR_EN4B_EX4B_OP(enable);
524 
525 		spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
526 
527 		ret = spi_mem_exec_op(nor->spimem, &op);
528 	} else {
529 		ret = spi_nor_controller_ops_write_reg(nor,
530 						       enable ? SPINOR_OP_EN4B :
531 								SPINOR_OP_EX4B,
532 						       NULL, 0);
533 	}
534 
535 	if (ret)
536 		dev_dbg(nor->dev, "error %d setting 4-byte mode\n", ret);
537 
538 	return ret;
539 }
540 
541 /**
542  * spansion_set_4byte_addr_mode() - Set 4-byte address mode for Spansion
543  * flashes.
544  * @nor:	pointer to 'struct spi_nor'.
545  * @enable:	true to enter the 4-byte address mode, false to exit the 4-byte
546  *		address mode.
547  *
548  * Return: 0 on success, -errno otherwise.
549  */
550 static int spansion_set_4byte_addr_mode(struct spi_nor *nor, bool enable)
551 {
552 	int ret;
553 
554 	nor->bouncebuf[0] = enable << 7;
555 
556 	if (nor->spimem) {
557 		struct spi_mem_op op = SPI_NOR_BRWR_OP(nor->bouncebuf);
558 
559 		spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
560 
561 		ret = spi_mem_exec_op(nor->spimem, &op);
562 	} else {
563 		ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_BRWR,
564 						       nor->bouncebuf, 1);
565 	}
566 
567 	if (ret)
568 		dev_dbg(nor->dev, "error %d setting 4-byte mode\n", ret);
569 
570 	return ret;
571 }
572 
573 /**
574  * spi_nor_sr_ready() - Query the Status Register to see if the flash is ready
575  * for new commands.
576  * @nor:	pointer to 'struct spi_nor'.
577  *
578  * Return: 1 if ready, 0 if not ready, -errno on errors.
579  */
580 int spi_nor_sr_ready(struct spi_nor *nor)
581 {
582 	int ret;
583 
584 	ret = spi_nor_read_sr(nor, nor->bouncebuf);
585 	if (ret)
586 		return ret;
587 
588 	return !(nor->bouncebuf[0] & SR_WIP);
589 }
590 
591 /**
592  * spi_nor_ready() - Query the flash to see if it is ready for new commands.
593  * @nor:	pointer to 'struct spi_nor'.
594  *
595  * Return: 1 if ready, 0 if not ready, -errno on errors.
596  */
597 static int spi_nor_ready(struct spi_nor *nor)
598 {
599 	/* Flashes might override the standard routine. */
600 	if (nor->params->ready)
601 		return nor->params->ready(nor);
602 
603 	return spi_nor_sr_ready(nor);
604 }
605 
606 /**
607  * spi_nor_wait_till_ready_with_timeout() - Service routine to read the
608  * Status Register until ready, or timeout occurs.
609  * @nor:		pointer to "struct spi_nor".
610  * @timeout_jiffies:	jiffies to wait until timeout.
611  *
612  * Return: 0 on success, -errno otherwise.
613  */
614 static int spi_nor_wait_till_ready_with_timeout(struct spi_nor *nor,
615 						unsigned long timeout_jiffies)
616 {
617 	unsigned long deadline;
618 	int timeout = 0, ret;
619 
620 	deadline = jiffies + timeout_jiffies;
621 
622 	while (!timeout) {
623 		if (time_after_eq(jiffies, deadline))
624 			timeout = 1;
625 
626 		ret = spi_nor_ready(nor);
627 		if (ret < 0)
628 			return ret;
629 		if (ret)
630 			return 0;
631 
632 		cond_resched();
633 	}
634 
635 	dev_dbg(nor->dev, "flash operation timed out\n");
636 
637 	return -ETIMEDOUT;
638 }
639 
640 /**
641  * spi_nor_wait_till_ready() - Wait for a predefined amount of time for the
642  * flash to be ready, or timeout occurs.
643  * @nor:	pointer to "struct spi_nor".
644  *
645  * Return: 0 on success, -errno otherwise.
646  */
647 int spi_nor_wait_till_ready(struct spi_nor *nor)
648 {
649 	return spi_nor_wait_till_ready_with_timeout(nor,
650 						    DEFAULT_READY_WAIT_JIFFIES);
651 }
652 
653 /**
654  * spi_nor_global_block_unlock() - Unlock Global Block Protection.
655  * @nor:	pointer to 'struct spi_nor'.
656  *
657  * Return: 0 on success, -errno otherwise.
658  */
659 int spi_nor_global_block_unlock(struct spi_nor *nor)
660 {
661 	int ret;
662 
663 	ret = spi_nor_write_enable(nor);
664 	if (ret)
665 		return ret;
666 
667 	if (nor->spimem) {
668 		struct spi_mem_op op = SPI_NOR_GBULK_OP;
669 
670 		spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
671 
672 		ret = spi_mem_exec_op(nor->spimem, &op);
673 	} else {
674 		ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_GBULK,
675 						       NULL, 0);
676 	}
677 
678 	if (ret) {
679 		dev_dbg(nor->dev, "error %d on Global Block Unlock\n", ret);
680 		return ret;
681 	}
682 
683 	return spi_nor_wait_till_ready(nor);
684 }
685 
686 /**
687  * spi_nor_write_sr() - Write the Status Register.
688  * @nor:	pointer to 'struct spi_nor'.
689  * @sr:		pointer to DMA-able buffer to write to the Status Register.
690  * @len:	number of bytes to write to the Status Register.
691  *
692  * Return: 0 on success, -errno otherwise.
693  */
694 int spi_nor_write_sr(struct spi_nor *nor, const u8 *sr, size_t len)
695 {
696 	int ret;
697 
698 	ret = spi_nor_write_enable(nor);
699 	if (ret)
700 		return ret;
701 
702 	if (nor->spimem) {
703 		struct spi_mem_op op = SPI_NOR_WRSR_OP(sr, len);
704 
705 		spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
706 
707 		ret = spi_mem_exec_op(nor->spimem, &op);
708 	} else {
709 		ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_WRSR, sr,
710 						       len);
711 	}
712 
713 	if (ret) {
714 		dev_dbg(nor->dev, "error %d writing SR\n", ret);
715 		return ret;
716 	}
717 
718 	return spi_nor_wait_till_ready(nor);
719 }
720 
721 /**
722  * spi_nor_write_sr1_and_check() - Write one byte to the Status Register 1 and
723  * ensure that the byte written match the received value.
724  * @nor:	pointer to a 'struct spi_nor'.
725  * @sr1:	byte value to be written to the Status Register.
726  *
727  * Return: 0 on success, -errno otherwise.
728  */
729 static int spi_nor_write_sr1_and_check(struct spi_nor *nor, u8 sr1)
730 {
731 	int ret;
732 
733 	nor->bouncebuf[0] = sr1;
734 
735 	ret = spi_nor_write_sr(nor, nor->bouncebuf, 1);
736 	if (ret)
737 		return ret;
738 
739 	ret = spi_nor_read_sr(nor, nor->bouncebuf);
740 	if (ret)
741 		return ret;
742 
743 	if (nor->bouncebuf[0] != sr1) {
744 		dev_dbg(nor->dev, "SR1: read back test failed\n");
745 		return -EIO;
746 	}
747 
748 	return 0;
749 }
750 
751 /**
752  * spi_nor_write_16bit_sr_and_check() - Write the Status Register 1 and the
753  * Status Register 2 in one shot. Ensure that the byte written in the Status
754  * Register 1 match the received value, and that the 16-bit Write did not
755  * affect what was already in the Status Register 2.
756  * @nor:	pointer to a 'struct spi_nor'.
757  * @sr1:	byte value to be written to the Status Register 1.
758  *
759  * Return: 0 on success, -errno otherwise.
760  */
761 static int spi_nor_write_16bit_sr_and_check(struct spi_nor *nor, u8 sr1)
762 {
763 	int ret;
764 	u8 *sr_cr = nor->bouncebuf;
765 	u8 cr_written;
766 
767 	/* Make sure we don't overwrite the contents of Status Register 2. */
768 	if (!(nor->flags & SNOR_F_NO_READ_CR)) {
769 		ret = spi_nor_read_cr(nor, &sr_cr[1]);
770 		if (ret)
771 			return ret;
772 	} else if (nor->params->quad_enable) {
773 		/*
774 		 * If the Status Register 2 Read command (35h) is not
775 		 * supported, we should at least be sure we don't
776 		 * change the value of the SR2 Quad Enable bit.
777 		 *
778 		 * We can safely assume that when the Quad Enable method is
779 		 * set, the value of the QE bit is one, as a consequence of the
780 		 * nor->params->quad_enable() call.
781 		 *
782 		 * We can safely assume that the Quad Enable bit is present in
783 		 * the Status Register 2 at BIT(1). According to the JESD216
784 		 * revB standard, BFPT DWORDS[15], bits 22:20, the 16-bit
785 		 * Write Status (01h) command is available just for the cases
786 		 * in which the QE bit is described in SR2 at BIT(1).
787 		 */
788 		sr_cr[1] = SR2_QUAD_EN_BIT1;
789 	} else {
790 		sr_cr[1] = 0;
791 	}
792 
793 	sr_cr[0] = sr1;
794 
795 	ret = spi_nor_write_sr(nor, sr_cr, 2);
796 	if (ret)
797 		return ret;
798 
799 	ret = spi_nor_read_sr(nor, sr_cr);
800 	if (ret)
801 		return ret;
802 
803 	if (sr1 != sr_cr[0]) {
804 		dev_dbg(nor->dev, "SR: Read back test failed\n");
805 		return -EIO;
806 	}
807 
808 	if (nor->flags & SNOR_F_NO_READ_CR)
809 		return 0;
810 
811 	cr_written = sr_cr[1];
812 
813 	ret = spi_nor_read_cr(nor, &sr_cr[1]);
814 	if (ret)
815 		return ret;
816 
817 	if (cr_written != sr_cr[1]) {
818 		dev_dbg(nor->dev, "CR: read back test failed\n");
819 		return -EIO;
820 	}
821 
822 	return 0;
823 }
824 
825 /**
826  * spi_nor_write_16bit_cr_and_check() - Write the Status Register 1 and the
827  * Configuration Register in one shot. Ensure that the byte written in the
828  * Configuration Register match the received value, and that the 16-bit Write
829  * did not affect what was already in the Status Register 1.
830  * @nor:	pointer to a 'struct spi_nor'.
831  * @cr:		byte value to be written to the Configuration Register.
832  *
833  * Return: 0 on success, -errno otherwise.
834  */
835 int spi_nor_write_16bit_cr_and_check(struct spi_nor *nor, u8 cr)
836 {
837 	int ret;
838 	u8 *sr_cr = nor->bouncebuf;
839 	u8 sr_written;
840 
841 	/* Keep the current value of the Status Register 1. */
842 	ret = spi_nor_read_sr(nor, sr_cr);
843 	if (ret)
844 		return ret;
845 
846 	sr_cr[1] = cr;
847 
848 	ret = spi_nor_write_sr(nor, sr_cr, 2);
849 	if (ret)
850 		return ret;
851 
852 	sr_written = sr_cr[0];
853 
854 	ret = spi_nor_read_sr(nor, sr_cr);
855 	if (ret)
856 		return ret;
857 
858 	if (sr_written != sr_cr[0]) {
859 		dev_dbg(nor->dev, "SR: Read back test failed\n");
860 		return -EIO;
861 	}
862 
863 	if (nor->flags & SNOR_F_NO_READ_CR)
864 		return 0;
865 
866 	ret = spi_nor_read_cr(nor, &sr_cr[1]);
867 	if (ret)
868 		return ret;
869 
870 	if (cr != sr_cr[1]) {
871 		dev_dbg(nor->dev, "CR: read back test failed\n");
872 		return -EIO;
873 	}
874 
875 	return 0;
876 }
877 
878 /**
879  * spi_nor_write_sr_and_check() - Write the Status Register 1 and ensure that
880  * the byte written match the received value without affecting other bits in the
881  * Status Register 1 and 2.
882  * @nor:	pointer to a 'struct spi_nor'.
883  * @sr1:	byte value to be written to the Status Register.
884  *
885  * Return: 0 on success, -errno otherwise.
886  */
887 int spi_nor_write_sr_and_check(struct spi_nor *nor, u8 sr1)
888 {
889 	if (nor->flags & SNOR_F_HAS_16BIT_SR)
890 		return spi_nor_write_16bit_sr_and_check(nor, sr1);
891 
892 	return spi_nor_write_sr1_and_check(nor, sr1);
893 }
894 
895 /**
896  * spi_nor_write_sr2() - Write the Status Register 2 using the
897  * SPINOR_OP_WRSR2 (3eh) command.
898  * @nor:	pointer to 'struct spi_nor'.
899  * @sr2:	pointer to DMA-able buffer to write to the Status Register 2.
900  *
901  * Return: 0 on success, -errno otherwise.
902  */
903 static int spi_nor_write_sr2(struct spi_nor *nor, const u8 *sr2)
904 {
905 	int ret;
906 
907 	ret = spi_nor_write_enable(nor);
908 	if (ret)
909 		return ret;
910 
911 	if (nor->spimem) {
912 		struct spi_mem_op op = SPI_NOR_WRSR2_OP(sr2);
913 
914 		spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
915 
916 		ret = spi_mem_exec_op(nor->spimem, &op);
917 	} else {
918 		ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_WRSR2,
919 						       sr2, 1);
920 	}
921 
922 	if (ret) {
923 		dev_dbg(nor->dev, "error %d writing SR2\n", ret);
924 		return ret;
925 	}
926 
927 	return spi_nor_wait_till_ready(nor);
928 }
929 
930 /**
931  * spi_nor_read_sr2() - Read the Status Register 2 using the
932  * SPINOR_OP_RDSR2 (3fh) command.
933  * @nor:	pointer to 'struct spi_nor'.
934  * @sr2:	pointer to DMA-able buffer where the value of the
935  *		Status Register 2 will be written.
936  *
937  * Return: 0 on success, -errno otherwise.
938  */
939 static int spi_nor_read_sr2(struct spi_nor *nor, u8 *sr2)
940 {
941 	int ret;
942 
943 	if (nor->spimem) {
944 		struct spi_mem_op op = SPI_NOR_RDSR2_OP(sr2);
945 
946 		spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
947 
948 		ret = spi_mem_exec_op(nor->spimem, &op);
949 	} else {
950 		ret = spi_nor_controller_ops_read_reg(nor, SPINOR_OP_RDSR2, sr2,
951 						      1);
952 	}
953 
954 	if (ret)
955 		dev_dbg(nor->dev, "error %d reading SR2\n", ret);
956 
957 	return ret;
958 }
959 
960 /**
961  * spi_nor_erase_chip() - Erase the entire flash memory.
962  * @nor:	pointer to 'struct spi_nor'.
963  *
964  * Return: 0 on success, -errno otherwise.
965  */
966 static int spi_nor_erase_chip(struct spi_nor *nor)
967 {
968 	int ret;
969 
970 	dev_dbg(nor->dev, " %lldKiB\n", (long long)(nor->mtd.size >> 10));
971 
972 	if (nor->spimem) {
973 		struct spi_mem_op op = SPI_NOR_CHIP_ERASE_OP;
974 
975 		spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
976 
977 		ret = spi_mem_exec_op(nor->spimem, &op);
978 	} else {
979 		ret = spi_nor_controller_ops_write_reg(nor,
980 						       SPINOR_OP_CHIP_ERASE,
981 						       NULL, 0);
982 	}
983 
984 	if (ret)
985 		dev_dbg(nor->dev, "error %d erasing chip\n", ret);
986 
987 	return ret;
988 }
989 
990 static u8 spi_nor_convert_opcode(u8 opcode, const u8 table[][2], size_t size)
991 {
992 	size_t i;
993 
994 	for (i = 0; i < size; i++)
995 		if (table[i][0] == opcode)
996 			return table[i][1];
997 
998 	/* No conversion found, keep input op code. */
999 	return opcode;
1000 }
1001 
1002 u8 spi_nor_convert_3to4_read(u8 opcode)
1003 {
1004 	static const u8 spi_nor_3to4_read[][2] = {
1005 		{ SPINOR_OP_READ,	SPINOR_OP_READ_4B },
1006 		{ SPINOR_OP_READ_FAST,	SPINOR_OP_READ_FAST_4B },
1007 		{ SPINOR_OP_READ_1_1_2,	SPINOR_OP_READ_1_1_2_4B },
1008 		{ SPINOR_OP_READ_1_2_2,	SPINOR_OP_READ_1_2_2_4B },
1009 		{ SPINOR_OP_READ_1_1_4,	SPINOR_OP_READ_1_1_4_4B },
1010 		{ SPINOR_OP_READ_1_4_4,	SPINOR_OP_READ_1_4_4_4B },
1011 		{ SPINOR_OP_READ_1_1_8,	SPINOR_OP_READ_1_1_8_4B },
1012 		{ SPINOR_OP_READ_1_8_8,	SPINOR_OP_READ_1_8_8_4B },
1013 
1014 		{ SPINOR_OP_READ_1_1_1_DTR,	SPINOR_OP_READ_1_1_1_DTR_4B },
1015 		{ SPINOR_OP_READ_1_2_2_DTR,	SPINOR_OP_READ_1_2_2_DTR_4B },
1016 		{ SPINOR_OP_READ_1_4_4_DTR,	SPINOR_OP_READ_1_4_4_DTR_4B },
1017 	};
1018 
1019 	return spi_nor_convert_opcode(opcode, spi_nor_3to4_read,
1020 				      ARRAY_SIZE(spi_nor_3to4_read));
1021 }
1022 
1023 static u8 spi_nor_convert_3to4_program(u8 opcode)
1024 {
1025 	static const u8 spi_nor_3to4_program[][2] = {
1026 		{ SPINOR_OP_PP,		SPINOR_OP_PP_4B },
1027 		{ SPINOR_OP_PP_1_1_4,	SPINOR_OP_PP_1_1_4_4B },
1028 		{ SPINOR_OP_PP_1_4_4,	SPINOR_OP_PP_1_4_4_4B },
1029 		{ SPINOR_OP_PP_1_1_8,	SPINOR_OP_PP_1_1_8_4B },
1030 		{ SPINOR_OP_PP_1_8_8,	SPINOR_OP_PP_1_8_8_4B },
1031 	};
1032 
1033 	return spi_nor_convert_opcode(opcode, spi_nor_3to4_program,
1034 				      ARRAY_SIZE(spi_nor_3to4_program));
1035 }
1036 
1037 static u8 spi_nor_convert_3to4_erase(u8 opcode)
1038 {
1039 	static const u8 spi_nor_3to4_erase[][2] = {
1040 		{ SPINOR_OP_BE_4K,	SPINOR_OP_BE_4K_4B },
1041 		{ SPINOR_OP_BE_32K,	SPINOR_OP_BE_32K_4B },
1042 		{ SPINOR_OP_SE,		SPINOR_OP_SE_4B },
1043 	};
1044 
1045 	return spi_nor_convert_opcode(opcode, spi_nor_3to4_erase,
1046 				      ARRAY_SIZE(spi_nor_3to4_erase));
1047 }
1048 
1049 static bool spi_nor_has_uniform_erase(const struct spi_nor *nor)
1050 {
1051 	return !!nor->params->erase_map.uniform_erase_type;
1052 }
1053 
1054 static void spi_nor_set_4byte_opcodes(struct spi_nor *nor)
1055 {
1056 	nor->read_opcode = spi_nor_convert_3to4_read(nor->read_opcode);
1057 	nor->program_opcode = spi_nor_convert_3to4_program(nor->program_opcode);
1058 	nor->erase_opcode = spi_nor_convert_3to4_erase(nor->erase_opcode);
1059 
1060 	if (!spi_nor_has_uniform_erase(nor)) {
1061 		struct spi_nor_erase_map *map = &nor->params->erase_map;
1062 		struct spi_nor_erase_type *erase;
1063 		int i;
1064 
1065 		for (i = 0; i < SNOR_ERASE_TYPE_MAX; i++) {
1066 			erase = &map->erase_type[i];
1067 			erase->opcode =
1068 				spi_nor_convert_3to4_erase(erase->opcode);
1069 		}
1070 	}
1071 }
1072 
1073 int spi_nor_lock_and_prep(struct spi_nor *nor)
1074 {
1075 	int ret = 0;
1076 
1077 	mutex_lock(&nor->lock);
1078 
1079 	if (nor->controller_ops &&  nor->controller_ops->prepare) {
1080 		ret = nor->controller_ops->prepare(nor);
1081 		if (ret) {
1082 			mutex_unlock(&nor->lock);
1083 			return ret;
1084 		}
1085 	}
1086 	return ret;
1087 }
1088 
1089 void spi_nor_unlock_and_unprep(struct spi_nor *nor)
1090 {
1091 	if (nor->controller_ops && nor->controller_ops->unprepare)
1092 		nor->controller_ops->unprepare(nor);
1093 	mutex_unlock(&nor->lock);
1094 }
1095 
1096 static u32 spi_nor_convert_addr(struct spi_nor *nor, loff_t addr)
1097 {
1098 	if (!nor->params->convert_addr)
1099 		return addr;
1100 
1101 	return nor->params->convert_addr(nor, addr);
1102 }
1103 
1104 /*
1105  * Initiate the erasure of a single sector
1106  */
1107 int spi_nor_erase_sector(struct spi_nor *nor, u32 addr)
1108 {
1109 	int i;
1110 
1111 	addr = spi_nor_convert_addr(nor, addr);
1112 
1113 	if (nor->spimem) {
1114 		struct spi_mem_op op =
1115 			SPI_NOR_SECTOR_ERASE_OP(nor->erase_opcode,
1116 						nor->addr_nbytes, addr);
1117 
1118 		spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
1119 
1120 		return spi_mem_exec_op(nor->spimem, &op);
1121 	} else if (nor->controller_ops->erase) {
1122 		return spi_nor_controller_ops_erase(nor, addr);
1123 	}
1124 
1125 	/*
1126 	 * Default implementation, if driver doesn't have a specialized HW
1127 	 * control
1128 	 */
1129 	for (i = nor->addr_nbytes - 1; i >= 0; i--) {
1130 		nor->bouncebuf[i] = addr & 0xff;
1131 		addr >>= 8;
1132 	}
1133 
1134 	return spi_nor_controller_ops_write_reg(nor, nor->erase_opcode,
1135 						nor->bouncebuf, nor->addr_nbytes);
1136 }
1137 
1138 /**
1139  * spi_nor_div_by_erase_size() - calculate remainder and update new dividend
1140  * @erase:	pointer to a structure that describes a SPI NOR erase type
1141  * @dividend:	dividend value
1142  * @remainder:	pointer to u32 remainder (will be updated)
1143  *
1144  * Return: the result of the division
1145  */
1146 static u64 spi_nor_div_by_erase_size(const struct spi_nor_erase_type *erase,
1147 				     u64 dividend, u32 *remainder)
1148 {
1149 	/* JEDEC JESD216B Standard imposes erase sizes to be power of 2. */
1150 	*remainder = (u32)dividend & erase->size_mask;
1151 	return dividend >> erase->size_shift;
1152 }
1153 
1154 /**
1155  * spi_nor_find_best_erase_type() - find the best erase type for the given
1156  *				    offset in the serial flash memory and the
1157  *				    number of bytes to erase. The region in
1158  *				    which the address fits is expected to be
1159  *				    provided.
1160  * @map:	the erase map of the SPI NOR
1161  * @region:	pointer to a structure that describes a SPI NOR erase region
1162  * @addr:	offset in the serial flash memory
1163  * @len:	number of bytes to erase
1164  *
1165  * Return: a pointer to the best fitted erase type, NULL otherwise.
1166  */
1167 static const struct spi_nor_erase_type *
1168 spi_nor_find_best_erase_type(const struct spi_nor_erase_map *map,
1169 			     const struct spi_nor_erase_region *region,
1170 			     u64 addr, u32 len)
1171 {
1172 	const struct spi_nor_erase_type *erase;
1173 	u32 rem;
1174 	int i;
1175 	u8 erase_mask = region->offset & SNOR_ERASE_TYPE_MASK;
1176 
1177 	/*
1178 	 * Erase types are ordered by size, with the smallest erase type at
1179 	 * index 0.
1180 	 */
1181 	for (i = SNOR_ERASE_TYPE_MAX - 1; i >= 0; i--) {
1182 		/* Does the erase region support the tested erase type? */
1183 		if (!(erase_mask & BIT(i)))
1184 			continue;
1185 
1186 		erase = &map->erase_type[i];
1187 		if (!erase->size)
1188 			continue;
1189 
1190 		/* Alignment is not mandatory for overlaid regions */
1191 		if (region->offset & SNOR_OVERLAID_REGION &&
1192 		    region->size <= len)
1193 			return erase;
1194 
1195 		/* Don't erase more than what the user has asked for. */
1196 		if (erase->size > len)
1197 			continue;
1198 
1199 		spi_nor_div_by_erase_size(erase, addr, &rem);
1200 		if (!rem)
1201 			return erase;
1202 	}
1203 
1204 	return NULL;
1205 }
1206 
1207 static u64 spi_nor_region_is_last(const struct spi_nor_erase_region *region)
1208 {
1209 	return region->offset & SNOR_LAST_REGION;
1210 }
1211 
1212 static u64 spi_nor_region_end(const struct spi_nor_erase_region *region)
1213 {
1214 	return (region->offset & ~SNOR_ERASE_FLAGS_MASK) + region->size;
1215 }
1216 
1217 /**
1218  * spi_nor_region_next() - get the next spi nor region
1219  * @region:	pointer to a structure that describes a SPI NOR erase region
1220  *
1221  * Return: the next spi nor region or NULL if last region.
1222  */
1223 struct spi_nor_erase_region *
1224 spi_nor_region_next(struct spi_nor_erase_region *region)
1225 {
1226 	if (spi_nor_region_is_last(region))
1227 		return NULL;
1228 	region++;
1229 	return region;
1230 }
1231 
1232 /**
1233  * spi_nor_find_erase_region() - find the region of the serial flash memory in
1234  *				 which the offset fits
1235  * @map:	the erase map of the SPI NOR
1236  * @addr:	offset in the serial flash memory
1237  *
1238  * Return: a pointer to the spi_nor_erase_region struct, ERR_PTR(-errno)
1239  *	   otherwise.
1240  */
1241 static struct spi_nor_erase_region *
1242 spi_nor_find_erase_region(const struct spi_nor_erase_map *map, u64 addr)
1243 {
1244 	struct spi_nor_erase_region *region = map->regions;
1245 	u64 region_start = region->offset & ~SNOR_ERASE_FLAGS_MASK;
1246 	u64 region_end = region_start + region->size;
1247 
1248 	while (addr < region_start || addr >= region_end) {
1249 		region = spi_nor_region_next(region);
1250 		if (!region)
1251 			return ERR_PTR(-EINVAL);
1252 
1253 		region_start = region->offset & ~SNOR_ERASE_FLAGS_MASK;
1254 		region_end = region_start + region->size;
1255 	}
1256 
1257 	return region;
1258 }
1259 
1260 /**
1261  * spi_nor_init_erase_cmd() - initialize an erase command
1262  * @region:	pointer to a structure that describes a SPI NOR erase region
1263  * @erase:	pointer to a structure that describes a SPI NOR erase type
1264  *
1265  * Return: the pointer to the allocated erase command, ERR_PTR(-errno)
1266  *	   otherwise.
1267  */
1268 static struct spi_nor_erase_command *
1269 spi_nor_init_erase_cmd(const struct spi_nor_erase_region *region,
1270 		       const struct spi_nor_erase_type *erase)
1271 {
1272 	struct spi_nor_erase_command *cmd;
1273 
1274 	cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
1275 	if (!cmd)
1276 		return ERR_PTR(-ENOMEM);
1277 
1278 	INIT_LIST_HEAD(&cmd->list);
1279 	cmd->opcode = erase->opcode;
1280 	cmd->count = 1;
1281 
1282 	if (region->offset & SNOR_OVERLAID_REGION)
1283 		cmd->size = region->size;
1284 	else
1285 		cmd->size = erase->size;
1286 
1287 	return cmd;
1288 }
1289 
1290 /**
1291  * spi_nor_destroy_erase_cmd_list() - destroy erase command list
1292  * @erase_list:	list of erase commands
1293  */
1294 static void spi_nor_destroy_erase_cmd_list(struct list_head *erase_list)
1295 {
1296 	struct spi_nor_erase_command *cmd, *next;
1297 
1298 	list_for_each_entry_safe(cmd, next, erase_list, list) {
1299 		list_del(&cmd->list);
1300 		kfree(cmd);
1301 	}
1302 }
1303 
1304 /**
1305  * spi_nor_init_erase_cmd_list() - initialize erase command list
1306  * @nor:	pointer to a 'struct spi_nor'
1307  * @erase_list:	list of erase commands to be executed once we validate that the
1308  *		erase can be performed
1309  * @addr:	offset in the serial flash memory
1310  * @len:	number of bytes to erase
1311  *
1312  * Builds the list of best fitted erase commands and verifies if the erase can
1313  * be performed.
1314  *
1315  * Return: 0 on success, -errno otherwise.
1316  */
1317 static int spi_nor_init_erase_cmd_list(struct spi_nor *nor,
1318 				       struct list_head *erase_list,
1319 				       u64 addr, u32 len)
1320 {
1321 	const struct spi_nor_erase_map *map = &nor->params->erase_map;
1322 	const struct spi_nor_erase_type *erase, *prev_erase = NULL;
1323 	struct spi_nor_erase_region *region;
1324 	struct spi_nor_erase_command *cmd = NULL;
1325 	u64 region_end;
1326 	int ret = -EINVAL;
1327 
1328 	region = spi_nor_find_erase_region(map, addr);
1329 	if (IS_ERR(region))
1330 		return PTR_ERR(region);
1331 
1332 	region_end = spi_nor_region_end(region);
1333 
1334 	while (len) {
1335 		erase = spi_nor_find_best_erase_type(map, region, addr, len);
1336 		if (!erase)
1337 			goto destroy_erase_cmd_list;
1338 
1339 		if (prev_erase != erase ||
1340 		    erase->size != cmd->size ||
1341 		    region->offset & SNOR_OVERLAID_REGION) {
1342 			cmd = spi_nor_init_erase_cmd(region, erase);
1343 			if (IS_ERR(cmd)) {
1344 				ret = PTR_ERR(cmd);
1345 				goto destroy_erase_cmd_list;
1346 			}
1347 
1348 			list_add_tail(&cmd->list, erase_list);
1349 		} else {
1350 			cmd->count++;
1351 		}
1352 
1353 		addr += cmd->size;
1354 		len -= cmd->size;
1355 
1356 		if (len && addr >= region_end) {
1357 			region = spi_nor_region_next(region);
1358 			if (!region)
1359 				goto destroy_erase_cmd_list;
1360 			region_end = spi_nor_region_end(region);
1361 		}
1362 
1363 		prev_erase = erase;
1364 	}
1365 
1366 	return 0;
1367 
1368 destroy_erase_cmd_list:
1369 	spi_nor_destroy_erase_cmd_list(erase_list);
1370 	return ret;
1371 }
1372 
1373 /**
1374  * spi_nor_erase_multi_sectors() - perform a non-uniform erase
1375  * @nor:	pointer to a 'struct spi_nor'
1376  * @addr:	offset in the serial flash memory
1377  * @len:	number of bytes to erase
1378  *
1379  * Build a list of best fitted erase commands and execute it once we validate
1380  * that the erase can be performed.
1381  *
1382  * Return: 0 on success, -errno otherwise.
1383  */
1384 static int spi_nor_erase_multi_sectors(struct spi_nor *nor, u64 addr, u32 len)
1385 {
1386 	LIST_HEAD(erase_list);
1387 	struct spi_nor_erase_command *cmd, *next;
1388 	int ret;
1389 
1390 	ret = spi_nor_init_erase_cmd_list(nor, &erase_list, addr, len);
1391 	if (ret)
1392 		return ret;
1393 
1394 	list_for_each_entry_safe(cmd, next, &erase_list, list) {
1395 		nor->erase_opcode = cmd->opcode;
1396 		while (cmd->count) {
1397 			dev_vdbg(nor->dev, "erase_cmd->size = 0x%08x, erase_cmd->opcode = 0x%02x, erase_cmd->count = %u\n",
1398 				 cmd->size, cmd->opcode, cmd->count);
1399 
1400 			ret = spi_nor_write_enable(nor);
1401 			if (ret)
1402 				goto destroy_erase_cmd_list;
1403 
1404 			ret = spi_nor_erase_sector(nor, addr);
1405 			if (ret)
1406 				goto destroy_erase_cmd_list;
1407 
1408 			ret = spi_nor_wait_till_ready(nor);
1409 			if (ret)
1410 				goto destroy_erase_cmd_list;
1411 
1412 			addr += cmd->size;
1413 			cmd->count--;
1414 		}
1415 		list_del(&cmd->list);
1416 		kfree(cmd);
1417 	}
1418 
1419 	return 0;
1420 
1421 destroy_erase_cmd_list:
1422 	spi_nor_destroy_erase_cmd_list(&erase_list);
1423 	return ret;
1424 }
1425 
1426 /*
1427  * Erase an address range on the nor chip.  The address range may extend
1428  * one or more erase sectors. Return an error if there is a problem erasing.
1429  */
1430 static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
1431 {
1432 	struct spi_nor *nor = mtd_to_spi_nor(mtd);
1433 	u32 addr, len;
1434 	uint32_t rem;
1435 	int ret;
1436 
1437 	dev_dbg(nor->dev, "at 0x%llx, len %lld\n", (long long)instr->addr,
1438 			(long long)instr->len);
1439 
1440 	if (spi_nor_has_uniform_erase(nor)) {
1441 		div_u64_rem(instr->len, mtd->erasesize, &rem);
1442 		if (rem)
1443 			return -EINVAL;
1444 	}
1445 
1446 	addr = instr->addr;
1447 	len = instr->len;
1448 
1449 	ret = spi_nor_lock_and_prep(nor);
1450 	if (ret)
1451 		return ret;
1452 
1453 	/* whole-chip erase? */
1454 	if (len == mtd->size && !(nor->flags & SNOR_F_NO_OP_CHIP_ERASE)) {
1455 		unsigned long timeout;
1456 
1457 		ret = spi_nor_write_enable(nor);
1458 		if (ret)
1459 			goto erase_err;
1460 
1461 		ret = spi_nor_erase_chip(nor);
1462 		if (ret)
1463 			goto erase_err;
1464 
1465 		/*
1466 		 * Scale the timeout linearly with the size of the flash, with
1467 		 * a minimum calibrated to an old 2MB flash. We could try to
1468 		 * pull these from CFI/SFDP, but these values should be good
1469 		 * enough for now.
1470 		 */
1471 		timeout = max(CHIP_ERASE_2MB_READY_WAIT_JIFFIES,
1472 			      CHIP_ERASE_2MB_READY_WAIT_JIFFIES *
1473 			      (unsigned long)(mtd->size / SZ_2M));
1474 		ret = spi_nor_wait_till_ready_with_timeout(nor, timeout);
1475 		if (ret)
1476 			goto erase_err;
1477 
1478 	/* REVISIT in some cases we could speed up erasing large regions
1479 	 * by using SPINOR_OP_SE instead of SPINOR_OP_BE_4K.  We may have set up
1480 	 * to use "small sector erase", but that's not always optimal.
1481 	 */
1482 
1483 	/* "sector"-at-a-time erase */
1484 	} else if (spi_nor_has_uniform_erase(nor)) {
1485 		while (len) {
1486 			ret = spi_nor_write_enable(nor);
1487 			if (ret)
1488 				goto erase_err;
1489 
1490 			ret = spi_nor_erase_sector(nor, addr);
1491 			if (ret)
1492 				goto erase_err;
1493 
1494 			ret = spi_nor_wait_till_ready(nor);
1495 			if (ret)
1496 				goto erase_err;
1497 
1498 			addr += mtd->erasesize;
1499 			len -= mtd->erasesize;
1500 		}
1501 
1502 	/* erase multiple sectors */
1503 	} else {
1504 		ret = spi_nor_erase_multi_sectors(nor, addr, len);
1505 		if (ret)
1506 			goto erase_err;
1507 	}
1508 
1509 	ret = spi_nor_write_disable(nor);
1510 
1511 erase_err:
1512 	spi_nor_unlock_and_unprep(nor);
1513 
1514 	return ret;
1515 }
1516 
1517 /**
1518  * spi_nor_sr1_bit6_quad_enable() - Set the Quad Enable BIT(6) in the Status
1519  * Register 1.
1520  * @nor:	pointer to a 'struct spi_nor'
1521  *
1522  * Bit 6 of the Status Register 1 is the QE bit for Macronix like QSPI memories.
1523  *
1524  * Return: 0 on success, -errno otherwise.
1525  */
1526 int spi_nor_sr1_bit6_quad_enable(struct spi_nor *nor)
1527 {
1528 	int ret;
1529 
1530 	ret = spi_nor_read_sr(nor, nor->bouncebuf);
1531 	if (ret)
1532 		return ret;
1533 
1534 	if (nor->bouncebuf[0] & SR1_QUAD_EN_BIT6)
1535 		return 0;
1536 
1537 	nor->bouncebuf[0] |= SR1_QUAD_EN_BIT6;
1538 
1539 	return spi_nor_write_sr1_and_check(nor, nor->bouncebuf[0]);
1540 }
1541 
1542 /**
1543  * spi_nor_sr2_bit1_quad_enable() - set the Quad Enable BIT(1) in the Status
1544  * Register 2.
1545  * @nor:       pointer to a 'struct spi_nor'.
1546  *
1547  * Bit 1 of the Status Register 2 is the QE bit for Spansion like QSPI memories.
1548  *
1549  * Return: 0 on success, -errno otherwise.
1550  */
1551 int spi_nor_sr2_bit1_quad_enable(struct spi_nor *nor)
1552 {
1553 	int ret;
1554 
1555 	if (nor->flags & SNOR_F_NO_READ_CR)
1556 		return spi_nor_write_16bit_cr_and_check(nor, SR2_QUAD_EN_BIT1);
1557 
1558 	ret = spi_nor_read_cr(nor, nor->bouncebuf);
1559 	if (ret)
1560 		return ret;
1561 
1562 	if (nor->bouncebuf[0] & SR2_QUAD_EN_BIT1)
1563 		return 0;
1564 
1565 	nor->bouncebuf[0] |= SR2_QUAD_EN_BIT1;
1566 
1567 	return spi_nor_write_16bit_cr_and_check(nor, nor->bouncebuf[0]);
1568 }
1569 
1570 /**
1571  * spi_nor_sr2_bit7_quad_enable() - set QE bit in Status Register 2.
1572  * @nor:	pointer to a 'struct spi_nor'
1573  *
1574  * Set the Quad Enable (QE) bit in the Status Register 2.
1575  *
1576  * This is one of the procedures to set the QE bit described in the SFDP
1577  * (JESD216 rev B) specification but no manufacturer using this procedure has
1578  * been identified yet, hence the name of the function.
1579  *
1580  * Return: 0 on success, -errno otherwise.
1581  */
1582 int spi_nor_sr2_bit7_quad_enable(struct spi_nor *nor)
1583 {
1584 	u8 *sr2 = nor->bouncebuf;
1585 	int ret;
1586 	u8 sr2_written;
1587 
1588 	/* Check current Quad Enable bit value. */
1589 	ret = spi_nor_read_sr2(nor, sr2);
1590 	if (ret)
1591 		return ret;
1592 	if (*sr2 & SR2_QUAD_EN_BIT7)
1593 		return 0;
1594 
1595 	/* Update the Quad Enable bit. */
1596 	*sr2 |= SR2_QUAD_EN_BIT7;
1597 
1598 	ret = spi_nor_write_sr2(nor, sr2);
1599 	if (ret)
1600 		return ret;
1601 
1602 	sr2_written = *sr2;
1603 
1604 	/* Read back and check it. */
1605 	ret = spi_nor_read_sr2(nor, sr2);
1606 	if (ret)
1607 		return ret;
1608 
1609 	if (*sr2 != sr2_written) {
1610 		dev_dbg(nor->dev, "SR2: Read back test failed\n");
1611 		return -EIO;
1612 	}
1613 
1614 	return 0;
1615 }
1616 
1617 static const struct spi_nor_manufacturer *manufacturers[] = {
1618 	&spi_nor_atmel,
1619 	&spi_nor_catalyst,
1620 	&spi_nor_eon,
1621 	&spi_nor_esmt,
1622 	&spi_nor_everspin,
1623 	&spi_nor_fujitsu,
1624 	&spi_nor_gigadevice,
1625 	&spi_nor_intel,
1626 	&spi_nor_issi,
1627 	&spi_nor_macronix,
1628 	&spi_nor_micron,
1629 	&spi_nor_st,
1630 	&spi_nor_spansion,
1631 	&spi_nor_sst,
1632 	&spi_nor_winbond,
1633 	&spi_nor_xilinx,
1634 	&spi_nor_xmc,
1635 };
1636 
1637 static const struct flash_info spi_nor_generic_flash = {
1638 	.name = "spi-nor-generic",
1639 	/*
1640 	 * JESD216 rev A doesn't specify the page size, therefore we need a
1641 	 * sane default.
1642 	 */
1643 	.page_size = 256,
1644 	.parse_sfdp = true,
1645 };
1646 
1647 static const struct flash_info *spi_nor_match_id(struct spi_nor *nor,
1648 						 const u8 *id)
1649 {
1650 	const struct flash_info *part;
1651 	unsigned int i, j;
1652 
1653 	for (i = 0; i < ARRAY_SIZE(manufacturers); i++) {
1654 		for (j = 0; j < manufacturers[i]->nparts; j++) {
1655 			part = &manufacturers[i]->parts[j];
1656 			if (part->id_len &&
1657 			    !memcmp(part->id, id, part->id_len)) {
1658 				nor->manufacturer = manufacturers[i];
1659 				return part;
1660 			}
1661 		}
1662 	}
1663 
1664 	return NULL;
1665 }
1666 
1667 static const struct flash_info *spi_nor_detect(struct spi_nor *nor)
1668 {
1669 	const struct flash_info *info;
1670 	u8 *id = nor->bouncebuf;
1671 	int ret;
1672 
1673 	ret = spi_nor_read_id(nor, 0, 0, id, nor->reg_proto);
1674 	if (ret) {
1675 		dev_dbg(nor->dev, "error %d reading JEDEC ID\n", ret);
1676 		return ERR_PTR(ret);
1677 	}
1678 
1679 	/* Cache the complete flash ID. */
1680 	nor->id = devm_kmemdup(nor->dev, id, SPI_NOR_MAX_ID_LEN, GFP_KERNEL);
1681 	if (!nor->id)
1682 		return ERR_PTR(-ENOMEM);
1683 
1684 	info = spi_nor_match_id(nor, id);
1685 
1686 	/* Fallback to a generic flash described only by its SFDP data. */
1687 	if (!info) {
1688 		ret = spi_nor_check_sfdp_signature(nor);
1689 		if (!ret)
1690 			info = &spi_nor_generic_flash;
1691 	}
1692 
1693 	if (!info) {
1694 		dev_err(nor->dev, "unrecognized JEDEC id bytes: %*ph\n",
1695 			SPI_NOR_MAX_ID_LEN, id);
1696 		return ERR_PTR(-ENODEV);
1697 	}
1698 	return info;
1699 }
1700 
1701 static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
1702 			size_t *retlen, u_char *buf)
1703 {
1704 	struct spi_nor *nor = mtd_to_spi_nor(mtd);
1705 	ssize_t ret;
1706 
1707 	dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len);
1708 
1709 	ret = spi_nor_lock_and_prep(nor);
1710 	if (ret)
1711 		return ret;
1712 
1713 	while (len) {
1714 		loff_t addr = from;
1715 
1716 		addr = spi_nor_convert_addr(nor, addr);
1717 
1718 		ret = spi_nor_read_data(nor, addr, len, buf);
1719 		if (ret == 0) {
1720 			/* We shouldn't see 0-length reads */
1721 			ret = -EIO;
1722 			goto read_err;
1723 		}
1724 		if (ret < 0)
1725 			goto read_err;
1726 
1727 		WARN_ON(ret > len);
1728 		*retlen += ret;
1729 		buf += ret;
1730 		from += ret;
1731 		len -= ret;
1732 	}
1733 	ret = 0;
1734 
1735 read_err:
1736 	spi_nor_unlock_and_unprep(nor);
1737 	return ret;
1738 }
1739 
1740 /*
1741  * Write an address range to the nor chip.  Data must be written in
1742  * FLASH_PAGESIZE chunks.  The address range may be any size provided
1743  * it is within the physical boundaries.
1744  */
1745 static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
1746 	size_t *retlen, const u_char *buf)
1747 {
1748 	struct spi_nor *nor = mtd_to_spi_nor(mtd);
1749 	size_t page_offset, page_remain, i;
1750 	ssize_t ret;
1751 	u32 page_size = nor->params->page_size;
1752 
1753 	dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
1754 
1755 	ret = spi_nor_lock_and_prep(nor);
1756 	if (ret)
1757 		return ret;
1758 
1759 	for (i = 0; i < len; ) {
1760 		ssize_t written;
1761 		loff_t addr = to + i;
1762 
1763 		/*
1764 		 * If page_size is a power of two, the offset can be quickly
1765 		 * calculated with an AND operation. On the other cases we
1766 		 * need to do a modulus operation (more expensive).
1767 		 */
1768 		if (is_power_of_2(page_size)) {
1769 			page_offset = addr & (page_size - 1);
1770 		} else {
1771 			uint64_t aux = addr;
1772 
1773 			page_offset = do_div(aux, page_size);
1774 		}
1775 		/* the size of data remaining on the first page */
1776 		page_remain = min_t(size_t, page_size - page_offset, len - i);
1777 
1778 		addr = spi_nor_convert_addr(nor, addr);
1779 
1780 		ret = spi_nor_write_enable(nor);
1781 		if (ret)
1782 			goto write_err;
1783 
1784 		ret = spi_nor_write_data(nor, addr, page_remain, buf + i);
1785 		if (ret < 0)
1786 			goto write_err;
1787 		written = ret;
1788 
1789 		ret = spi_nor_wait_till_ready(nor);
1790 		if (ret)
1791 			goto write_err;
1792 		*retlen += written;
1793 		i += written;
1794 	}
1795 
1796 write_err:
1797 	spi_nor_unlock_and_unprep(nor);
1798 	return ret;
1799 }
1800 
1801 static int spi_nor_check(struct spi_nor *nor)
1802 {
1803 	if (!nor->dev ||
1804 	    (!nor->spimem && !nor->controller_ops) ||
1805 	    (!nor->spimem && nor->controller_ops &&
1806 	    (!nor->controller_ops->read ||
1807 	     !nor->controller_ops->write ||
1808 	     !nor->controller_ops->read_reg ||
1809 	     !nor->controller_ops->write_reg))) {
1810 		pr_err("spi-nor: please fill all the necessary fields!\n");
1811 		return -EINVAL;
1812 	}
1813 
1814 	if (nor->spimem && nor->controller_ops) {
1815 		dev_err(nor->dev, "nor->spimem and nor->controller_ops are mutually exclusive, please set just one of them.\n");
1816 		return -EINVAL;
1817 	}
1818 
1819 	return 0;
1820 }
1821 
1822 void
1823 spi_nor_set_read_settings(struct spi_nor_read_command *read,
1824 			  u8 num_mode_clocks,
1825 			  u8 num_wait_states,
1826 			  u8 opcode,
1827 			  enum spi_nor_protocol proto)
1828 {
1829 	read->num_mode_clocks = num_mode_clocks;
1830 	read->num_wait_states = num_wait_states;
1831 	read->opcode = opcode;
1832 	read->proto = proto;
1833 }
1834 
1835 void spi_nor_set_pp_settings(struct spi_nor_pp_command *pp, u8 opcode,
1836 			     enum spi_nor_protocol proto)
1837 {
1838 	pp->opcode = opcode;
1839 	pp->proto = proto;
1840 }
1841 
1842 static int spi_nor_hwcaps2cmd(u32 hwcaps, const int table[][2], size_t size)
1843 {
1844 	size_t i;
1845 
1846 	for (i = 0; i < size; i++)
1847 		if (table[i][0] == (int)hwcaps)
1848 			return table[i][1];
1849 
1850 	return -EINVAL;
1851 }
1852 
1853 int spi_nor_hwcaps_read2cmd(u32 hwcaps)
1854 {
1855 	static const int hwcaps_read2cmd[][2] = {
1856 		{ SNOR_HWCAPS_READ,		SNOR_CMD_READ },
1857 		{ SNOR_HWCAPS_READ_FAST,	SNOR_CMD_READ_FAST },
1858 		{ SNOR_HWCAPS_READ_1_1_1_DTR,	SNOR_CMD_READ_1_1_1_DTR },
1859 		{ SNOR_HWCAPS_READ_1_1_2,	SNOR_CMD_READ_1_1_2 },
1860 		{ SNOR_HWCAPS_READ_1_2_2,	SNOR_CMD_READ_1_2_2 },
1861 		{ SNOR_HWCAPS_READ_2_2_2,	SNOR_CMD_READ_2_2_2 },
1862 		{ SNOR_HWCAPS_READ_1_2_2_DTR,	SNOR_CMD_READ_1_2_2_DTR },
1863 		{ SNOR_HWCAPS_READ_1_1_4,	SNOR_CMD_READ_1_1_4 },
1864 		{ SNOR_HWCAPS_READ_1_4_4,	SNOR_CMD_READ_1_4_4 },
1865 		{ SNOR_HWCAPS_READ_4_4_4,	SNOR_CMD_READ_4_4_4 },
1866 		{ SNOR_HWCAPS_READ_1_4_4_DTR,	SNOR_CMD_READ_1_4_4_DTR },
1867 		{ SNOR_HWCAPS_READ_1_1_8,	SNOR_CMD_READ_1_1_8 },
1868 		{ SNOR_HWCAPS_READ_1_8_8,	SNOR_CMD_READ_1_8_8 },
1869 		{ SNOR_HWCAPS_READ_8_8_8,	SNOR_CMD_READ_8_8_8 },
1870 		{ SNOR_HWCAPS_READ_1_8_8_DTR,	SNOR_CMD_READ_1_8_8_DTR },
1871 		{ SNOR_HWCAPS_READ_8_8_8_DTR,	SNOR_CMD_READ_8_8_8_DTR },
1872 	};
1873 
1874 	return spi_nor_hwcaps2cmd(hwcaps, hwcaps_read2cmd,
1875 				  ARRAY_SIZE(hwcaps_read2cmd));
1876 }
1877 
1878 int spi_nor_hwcaps_pp2cmd(u32 hwcaps)
1879 {
1880 	static const int hwcaps_pp2cmd[][2] = {
1881 		{ SNOR_HWCAPS_PP,		SNOR_CMD_PP },
1882 		{ SNOR_HWCAPS_PP_1_1_4,		SNOR_CMD_PP_1_1_4 },
1883 		{ SNOR_HWCAPS_PP_1_4_4,		SNOR_CMD_PP_1_4_4 },
1884 		{ SNOR_HWCAPS_PP_4_4_4,		SNOR_CMD_PP_4_4_4 },
1885 		{ SNOR_HWCAPS_PP_1_1_8,		SNOR_CMD_PP_1_1_8 },
1886 		{ SNOR_HWCAPS_PP_1_8_8,		SNOR_CMD_PP_1_8_8 },
1887 		{ SNOR_HWCAPS_PP_8_8_8,		SNOR_CMD_PP_8_8_8 },
1888 		{ SNOR_HWCAPS_PP_8_8_8_DTR,	SNOR_CMD_PP_8_8_8_DTR },
1889 	};
1890 
1891 	return spi_nor_hwcaps2cmd(hwcaps, hwcaps_pp2cmd,
1892 				  ARRAY_SIZE(hwcaps_pp2cmd));
1893 }
1894 
1895 /**
1896  * spi_nor_spimem_check_op - check if the operation is supported
1897  *                           by controller
1898  *@nor:        pointer to a 'struct spi_nor'
1899  *@op:         pointer to op template to be checked
1900  *
1901  * Returns 0 if operation is supported, -EOPNOTSUPP otherwise.
1902  */
1903 static int spi_nor_spimem_check_op(struct spi_nor *nor,
1904 				   struct spi_mem_op *op)
1905 {
1906 	/*
1907 	 * First test with 4 address bytes. The opcode itself might
1908 	 * be a 3B addressing opcode but we don't care, because
1909 	 * SPI controller implementation should not check the opcode,
1910 	 * but just the sequence.
1911 	 */
1912 	op->addr.nbytes = 4;
1913 	if (!spi_mem_supports_op(nor->spimem, op)) {
1914 		if (nor->params->size > SZ_16M)
1915 			return -EOPNOTSUPP;
1916 
1917 		/* If flash size <= 16MB, 3 address bytes are sufficient */
1918 		op->addr.nbytes = 3;
1919 		if (!spi_mem_supports_op(nor->spimem, op))
1920 			return -EOPNOTSUPP;
1921 	}
1922 
1923 	return 0;
1924 }
1925 
1926 /**
1927  * spi_nor_spimem_check_readop - check if the read op is supported
1928  *                               by controller
1929  *@nor:         pointer to a 'struct spi_nor'
1930  *@read:        pointer to op template to be checked
1931  *
1932  * Returns 0 if operation is supported, -EOPNOTSUPP otherwise.
1933  */
1934 static int spi_nor_spimem_check_readop(struct spi_nor *nor,
1935 				       const struct spi_nor_read_command *read)
1936 {
1937 	struct spi_mem_op op = SPI_NOR_READ_OP(read->opcode);
1938 
1939 	spi_nor_spimem_setup_op(nor, &op, read->proto);
1940 
1941 	/* convert the dummy cycles to the number of bytes */
1942 	op.dummy.nbytes = (read->num_mode_clocks + read->num_wait_states) *
1943 			  op.dummy.buswidth / 8;
1944 	if (spi_nor_protocol_is_dtr(nor->read_proto))
1945 		op.dummy.nbytes *= 2;
1946 
1947 	return spi_nor_spimem_check_op(nor, &op);
1948 }
1949 
1950 /**
1951  * spi_nor_spimem_check_pp - check if the page program op is supported
1952  *                           by controller
1953  *@nor:         pointer to a 'struct spi_nor'
1954  *@pp:          pointer to op template to be checked
1955  *
1956  * Returns 0 if operation is supported, -EOPNOTSUPP otherwise.
1957  */
1958 static int spi_nor_spimem_check_pp(struct spi_nor *nor,
1959 				   const struct spi_nor_pp_command *pp)
1960 {
1961 	struct spi_mem_op op = SPI_NOR_PP_OP(pp->opcode);
1962 
1963 	spi_nor_spimem_setup_op(nor, &op, pp->proto);
1964 
1965 	return spi_nor_spimem_check_op(nor, &op);
1966 }
1967 
1968 /**
1969  * spi_nor_spimem_adjust_hwcaps - Find optimal Read/Write protocol
1970  *                                based on SPI controller capabilities
1971  * @nor:        pointer to a 'struct spi_nor'
1972  * @hwcaps:     pointer to resulting capabilities after adjusting
1973  *              according to controller and flash's capability
1974  */
1975 static void
1976 spi_nor_spimem_adjust_hwcaps(struct spi_nor *nor, u32 *hwcaps)
1977 {
1978 	struct spi_nor_flash_parameter *params = nor->params;
1979 	unsigned int cap;
1980 
1981 	/* X-X-X modes are not supported yet, mask them all. */
1982 	*hwcaps &= ~SNOR_HWCAPS_X_X_X;
1983 
1984 	/*
1985 	 * If the reset line is broken, we do not want to enter a stateful
1986 	 * mode.
1987 	 */
1988 	if (nor->flags & SNOR_F_BROKEN_RESET)
1989 		*hwcaps &= ~(SNOR_HWCAPS_X_X_X | SNOR_HWCAPS_X_X_X_DTR);
1990 
1991 	for (cap = 0; cap < sizeof(*hwcaps) * BITS_PER_BYTE; cap++) {
1992 		int rdidx, ppidx;
1993 
1994 		if (!(*hwcaps & BIT(cap)))
1995 			continue;
1996 
1997 		rdidx = spi_nor_hwcaps_read2cmd(BIT(cap));
1998 		if (rdidx >= 0 &&
1999 		    spi_nor_spimem_check_readop(nor, &params->reads[rdidx]))
2000 			*hwcaps &= ~BIT(cap);
2001 
2002 		ppidx = spi_nor_hwcaps_pp2cmd(BIT(cap));
2003 		if (ppidx < 0)
2004 			continue;
2005 
2006 		if (spi_nor_spimem_check_pp(nor,
2007 					    &params->page_programs[ppidx]))
2008 			*hwcaps &= ~BIT(cap);
2009 	}
2010 }
2011 
2012 /**
2013  * spi_nor_set_erase_type() - set a SPI NOR erase type
2014  * @erase:	pointer to a structure that describes a SPI NOR erase type
2015  * @size:	the size of the sector/block erased by the erase type
2016  * @opcode:	the SPI command op code to erase the sector/block
2017  */
2018 void spi_nor_set_erase_type(struct spi_nor_erase_type *erase, u32 size,
2019 			    u8 opcode)
2020 {
2021 	erase->size = size;
2022 	erase->opcode = opcode;
2023 	/* JEDEC JESD216B Standard imposes erase sizes to be power of 2. */
2024 	erase->size_shift = ffs(erase->size) - 1;
2025 	erase->size_mask = (1 << erase->size_shift) - 1;
2026 }
2027 
2028 /**
2029  * spi_nor_init_uniform_erase_map() - Initialize uniform erase map
2030  * @map:		the erase map of the SPI NOR
2031  * @erase_mask:		bitmask encoding erase types that can erase the entire
2032  *			flash memory
2033  * @flash_size:		the spi nor flash memory size
2034  */
2035 void spi_nor_init_uniform_erase_map(struct spi_nor_erase_map *map,
2036 				    u8 erase_mask, u64 flash_size)
2037 {
2038 	/* Offset 0 with erase_mask and SNOR_LAST_REGION bit set */
2039 	map->uniform_region.offset = (erase_mask & SNOR_ERASE_TYPE_MASK) |
2040 				     SNOR_LAST_REGION;
2041 	map->uniform_region.size = flash_size;
2042 	map->regions = &map->uniform_region;
2043 	map->uniform_erase_type = erase_mask;
2044 }
2045 
2046 int spi_nor_post_bfpt_fixups(struct spi_nor *nor,
2047 			     const struct sfdp_parameter_header *bfpt_header,
2048 			     const struct sfdp_bfpt *bfpt)
2049 {
2050 	int ret;
2051 
2052 	if (nor->manufacturer && nor->manufacturer->fixups &&
2053 	    nor->manufacturer->fixups->post_bfpt) {
2054 		ret = nor->manufacturer->fixups->post_bfpt(nor, bfpt_header,
2055 							   bfpt);
2056 		if (ret)
2057 			return ret;
2058 	}
2059 
2060 	if (nor->info->fixups && nor->info->fixups->post_bfpt)
2061 		return nor->info->fixups->post_bfpt(nor, bfpt_header, bfpt);
2062 
2063 	return 0;
2064 }
2065 
2066 static int spi_nor_select_read(struct spi_nor *nor,
2067 			       u32 shared_hwcaps)
2068 {
2069 	int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_READ_MASK) - 1;
2070 	const struct spi_nor_read_command *read;
2071 
2072 	if (best_match < 0)
2073 		return -EINVAL;
2074 
2075 	cmd = spi_nor_hwcaps_read2cmd(BIT(best_match));
2076 	if (cmd < 0)
2077 		return -EINVAL;
2078 
2079 	read = &nor->params->reads[cmd];
2080 	nor->read_opcode = read->opcode;
2081 	nor->read_proto = read->proto;
2082 
2083 	/*
2084 	 * In the SPI NOR framework, we don't need to make the difference
2085 	 * between mode clock cycles and wait state clock cycles.
2086 	 * Indeed, the value of the mode clock cycles is used by a QSPI
2087 	 * flash memory to know whether it should enter or leave its 0-4-4
2088 	 * (Continuous Read / XIP) mode.
2089 	 * eXecution In Place is out of the scope of the mtd sub-system.
2090 	 * Hence we choose to merge both mode and wait state clock cycles
2091 	 * into the so called dummy clock cycles.
2092 	 */
2093 	nor->read_dummy = read->num_mode_clocks + read->num_wait_states;
2094 	return 0;
2095 }
2096 
2097 static int spi_nor_select_pp(struct spi_nor *nor,
2098 			     u32 shared_hwcaps)
2099 {
2100 	int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_PP_MASK) - 1;
2101 	const struct spi_nor_pp_command *pp;
2102 
2103 	if (best_match < 0)
2104 		return -EINVAL;
2105 
2106 	cmd = spi_nor_hwcaps_pp2cmd(BIT(best_match));
2107 	if (cmd < 0)
2108 		return -EINVAL;
2109 
2110 	pp = &nor->params->page_programs[cmd];
2111 	nor->program_opcode = pp->opcode;
2112 	nor->write_proto = pp->proto;
2113 	return 0;
2114 }
2115 
2116 /**
2117  * spi_nor_select_uniform_erase() - select optimum uniform erase type
2118  * @map:		the erase map of the SPI NOR
2119  * @wanted_size:	the erase type size to search for. Contains the value of
2120  *			info->sector_size, the "small sector" size in case
2121  *			CONFIG_MTD_SPI_NOR_USE_4K_SECTORS is defined or 0 if
2122  *			there is no information about the sector size. The
2123  *			latter is the case if the flash parameters are parsed
2124  *			solely by SFDP, then the largest supported erase type
2125  *			is selected.
2126  *
2127  * Once the optimum uniform sector erase command is found, disable all the
2128  * other.
2129  *
2130  * Return: pointer to erase type on success, NULL otherwise.
2131  */
2132 static const struct spi_nor_erase_type *
2133 spi_nor_select_uniform_erase(struct spi_nor_erase_map *map,
2134 			     const u32 wanted_size)
2135 {
2136 	const struct spi_nor_erase_type *tested_erase, *erase = NULL;
2137 	int i;
2138 	u8 uniform_erase_type = map->uniform_erase_type;
2139 
2140 	for (i = SNOR_ERASE_TYPE_MAX - 1; i >= 0; i--) {
2141 		if (!(uniform_erase_type & BIT(i)))
2142 			continue;
2143 
2144 		tested_erase = &map->erase_type[i];
2145 
2146 		/* Skip masked erase types. */
2147 		if (!tested_erase->size)
2148 			continue;
2149 
2150 		/*
2151 		 * If the current erase size is the one, stop here:
2152 		 * we have found the right uniform Sector Erase command.
2153 		 */
2154 		if (tested_erase->size == wanted_size) {
2155 			erase = tested_erase;
2156 			break;
2157 		}
2158 
2159 		/*
2160 		 * Otherwise, the current erase size is still a valid candidate.
2161 		 * Select the biggest valid candidate.
2162 		 */
2163 		if (!erase && tested_erase->size)
2164 			erase = tested_erase;
2165 			/* keep iterating to find the wanted_size */
2166 	}
2167 
2168 	if (!erase)
2169 		return NULL;
2170 
2171 	/* Disable all other Sector Erase commands. */
2172 	map->uniform_erase_type &= ~SNOR_ERASE_TYPE_MASK;
2173 	map->uniform_erase_type |= BIT(erase - map->erase_type);
2174 	return erase;
2175 }
2176 
2177 static int spi_nor_select_erase(struct spi_nor *nor)
2178 {
2179 	struct spi_nor_erase_map *map = &nor->params->erase_map;
2180 	const struct spi_nor_erase_type *erase = NULL;
2181 	struct mtd_info *mtd = &nor->mtd;
2182 	u32 wanted_size = nor->info->sector_size;
2183 	int i;
2184 
2185 	/*
2186 	 * The previous implementation handling Sector Erase commands assumed
2187 	 * that the SPI flash memory has an uniform layout then used only one
2188 	 * of the supported erase sizes for all Sector Erase commands.
2189 	 * So to be backward compatible, the new implementation also tries to
2190 	 * manage the SPI flash memory as uniform with a single erase sector
2191 	 * size, when possible.
2192 	 */
2193 #ifdef CONFIG_MTD_SPI_NOR_USE_4K_SECTORS
2194 	/* prefer "small sector" erase if possible */
2195 	wanted_size = 4096u;
2196 #endif
2197 
2198 	if (spi_nor_has_uniform_erase(nor)) {
2199 		erase = spi_nor_select_uniform_erase(map, wanted_size);
2200 		if (!erase)
2201 			return -EINVAL;
2202 		nor->erase_opcode = erase->opcode;
2203 		mtd->erasesize = erase->size;
2204 		return 0;
2205 	}
2206 
2207 	/*
2208 	 * For non-uniform SPI flash memory, set mtd->erasesize to the
2209 	 * maximum erase sector size. No need to set nor->erase_opcode.
2210 	 */
2211 	for (i = SNOR_ERASE_TYPE_MAX - 1; i >= 0; i--) {
2212 		if (map->erase_type[i].size) {
2213 			erase = &map->erase_type[i];
2214 			break;
2215 		}
2216 	}
2217 
2218 	if (!erase)
2219 		return -EINVAL;
2220 
2221 	mtd->erasesize = erase->size;
2222 	return 0;
2223 }
2224 
2225 static int spi_nor_default_setup(struct spi_nor *nor,
2226 				 const struct spi_nor_hwcaps *hwcaps)
2227 {
2228 	struct spi_nor_flash_parameter *params = nor->params;
2229 	u32 ignored_mask, shared_mask;
2230 	int err;
2231 
2232 	/*
2233 	 * Keep only the hardware capabilities supported by both the SPI
2234 	 * controller and the SPI flash memory.
2235 	 */
2236 	shared_mask = hwcaps->mask & params->hwcaps.mask;
2237 
2238 	if (nor->spimem) {
2239 		/*
2240 		 * When called from spi_nor_probe(), all caps are set and we
2241 		 * need to discard some of them based on what the SPI
2242 		 * controller actually supports (using spi_mem_supports_op()).
2243 		 */
2244 		spi_nor_spimem_adjust_hwcaps(nor, &shared_mask);
2245 	} else {
2246 		/*
2247 		 * SPI n-n-n protocols are not supported when the SPI
2248 		 * controller directly implements the spi_nor interface.
2249 		 * Yet another reason to switch to spi-mem.
2250 		 */
2251 		ignored_mask = SNOR_HWCAPS_X_X_X | SNOR_HWCAPS_X_X_X_DTR;
2252 		if (shared_mask & ignored_mask) {
2253 			dev_dbg(nor->dev,
2254 				"SPI n-n-n protocols are not supported.\n");
2255 			shared_mask &= ~ignored_mask;
2256 		}
2257 	}
2258 
2259 	/* Select the (Fast) Read command. */
2260 	err = spi_nor_select_read(nor, shared_mask);
2261 	if (err) {
2262 		dev_dbg(nor->dev,
2263 			"can't select read settings supported by both the SPI controller and memory.\n");
2264 		return err;
2265 	}
2266 
2267 	/* Select the Page Program command. */
2268 	err = spi_nor_select_pp(nor, shared_mask);
2269 	if (err) {
2270 		dev_dbg(nor->dev,
2271 			"can't select write settings supported by both the SPI controller and memory.\n");
2272 		return err;
2273 	}
2274 
2275 	/* Select the Sector Erase command. */
2276 	err = spi_nor_select_erase(nor);
2277 	if (err) {
2278 		dev_dbg(nor->dev,
2279 			"can't select erase settings supported by both the SPI controller and memory.\n");
2280 		return err;
2281 	}
2282 
2283 	return 0;
2284 }
2285 
2286 static int spi_nor_set_addr_nbytes(struct spi_nor *nor)
2287 {
2288 	if (nor->params->addr_nbytes) {
2289 		nor->addr_nbytes = nor->params->addr_nbytes;
2290 	} else if (nor->read_proto == SNOR_PROTO_8_8_8_DTR) {
2291 		/*
2292 		 * In 8D-8D-8D mode, one byte takes half a cycle to transfer. So
2293 		 * in this protocol an odd addr_nbytes cannot be used because
2294 		 * then the address phase would only span a cycle and a half.
2295 		 * Half a cycle would be left over. We would then have to start
2296 		 * the dummy phase in the middle of a cycle and so too the data
2297 		 * phase, and we will end the transaction with half a cycle left
2298 		 * over.
2299 		 *
2300 		 * Force all 8D-8D-8D flashes to use an addr_nbytes of 4 to
2301 		 * avoid this situation.
2302 		 */
2303 		nor->addr_nbytes = 4;
2304 	} else if (nor->info->addr_nbytes) {
2305 		nor->addr_nbytes = nor->info->addr_nbytes;
2306 	} else {
2307 		nor->addr_nbytes = 3;
2308 	}
2309 
2310 	if (nor->addr_nbytes == 3 && nor->params->size > 0x1000000) {
2311 		/* enable 4-byte addressing if the device exceeds 16MiB */
2312 		nor->addr_nbytes = 4;
2313 	}
2314 
2315 	if (nor->addr_nbytes > SPI_NOR_MAX_ADDR_NBYTES) {
2316 		dev_dbg(nor->dev, "The number of address bytes is too large: %u\n",
2317 			nor->addr_nbytes);
2318 		return -EINVAL;
2319 	}
2320 
2321 	/* Set 4byte opcodes when possible. */
2322 	if (nor->addr_nbytes == 4 && nor->flags & SNOR_F_4B_OPCODES &&
2323 	    !(nor->flags & SNOR_F_HAS_4BAIT))
2324 		spi_nor_set_4byte_opcodes(nor);
2325 
2326 	return 0;
2327 }
2328 
2329 static int spi_nor_setup(struct spi_nor *nor,
2330 			 const struct spi_nor_hwcaps *hwcaps)
2331 {
2332 	int ret;
2333 
2334 	if (nor->params->setup)
2335 		ret = nor->params->setup(nor, hwcaps);
2336 	else
2337 		ret = spi_nor_default_setup(nor, hwcaps);
2338 	if (ret)
2339 		return ret;
2340 
2341 	return spi_nor_set_addr_nbytes(nor);
2342 }
2343 
2344 /**
2345  * spi_nor_manufacturer_init_params() - Initialize the flash's parameters and
2346  * settings based on MFR register and ->default_init() hook.
2347  * @nor:	pointer to a 'struct spi_nor'.
2348  */
2349 static void spi_nor_manufacturer_init_params(struct spi_nor *nor)
2350 {
2351 	if (nor->manufacturer && nor->manufacturer->fixups &&
2352 	    nor->manufacturer->fixups->default_init)
2353 		nor->manufacturer->fixups->default_init(nor);
2354 
2355 	if (nor->info->fixups && nor->info->fixups->default_init)
2356 		nor->info->fixups->default_init(nor);
2357 }
2358 
2359 /**
2360  * spi_nor_no_sfdp_init_params() - Initialize the flash's parameters and
2361  * settings based on nor->info->sfdp_flags. This method should be called only by
2362  * flashes that do not define SFDP tables. If the flash supports SFDP but the
2363  * information is wrong and the settings from this function can not be retrieved
2364  * by parsing SFDP, one should instead use the fixup hooks and update the wrong
2365  * bits.
2366  * @nor:	pointer to a 'struct spi_nor'.
2367  */
2368 static void spi_nor_no_sfdp_init_params(struct spi_nor *nor)
2369 {
2370 	struct spi_nor_flash_parameter *params = nor->params;
2371 	struct spi_nor_erase_map *map = &params->erase_map;
2372 	const u8 no_sfdp_flags = nor->info->no_sfdp_flags;
2373 	u8 i, erase_mask;
2374 
2375 	if (no_sfdp_flags & SPI_NOR_DUAL_READ) {
2376 		params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_2;
2377 		spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_1_1_2],
2378 					  0, 8, SPINOR_OP_READ_1_1_2,
2379 					  SNOR_PROTO_1_1_2);
2380 	}
2381 
2382 	if (no_sfdp_flags & SPI_NOR_QUAD_READ) {
2383 		params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4;
2384 		spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_1_1_4],
2385 					  0, 8, SPINOR_OP_READ_1_1_4,
2386 					  SNOR_PROTO_1_1_4);
2387 	}
2388 
2389 	if (no_sfdp_flags & SPI_NOR_OCTAL_READ) {
2390 		params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_8;
2391 		spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_1_1_8],
2392 					  0, 8, SPINOR_OP_READ_1_1_8,
2393 					  SNOR_PROTO_1_1_8);
2394 	}
2395 
2396 	if (no_sfdp_flags & SPI_NOR_OCTAL_DTR_READ) {
2397 		params->hwcaps.mask |= SNOR_HWCAPS_READ_8_8_8_DTR;
2398 		spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_8_8_8_DTR],
2399 					  0, 20, SPINOR_OP_READ_FAST,
2400 					  SNOR_PROTO_8_8_8_DTR);
2401 	}
2402 
2403 	if (no_sfdp_flags & SPI_NOR_OCTAL_DTR_PP) {
2404 		params->hwcaps.mask |= SNOR_HWCAPS_PP_8_8_8_DTR;
2405 		/*
2406 		 * Since xSPI Page Program opcode is backward compatible with
2407 		 * Legacy SPI, use Legacy SPI opcode there as well.
2408 		 */
2409 		spi_nor_set_pp_settings(&params->page_programs[SNOR_CMD_PP_8_8_8_DTR],
2410 					SPINOR_OP_PP, SNOR_PROTO_8_8_8_DTR);
2411 	}
2412 
2413 	/*
2414 	 * Sector Erase settings. Sort Erase Types in ascending order, with the
2415 	 * smallest erase size starting at BIT(0).
2416 	 */
2417 	erase_mask = 0;
2418 	i = 0;
2419 	if (no_sfdp_flags & SECT_4K) {
2420 		erase_mask |= BIT(i);
2421 		spi_nor_set_erase_type(&map->erase_type[i], 4096u,
2422 				       SPINOR_OP_BE_4K);
2423 		i++;
2424 	}
2425 	erase_mask |= BIT(i);
2426 	spi_nor_set_erase_type(&map->erase_type[i], nor->info->sector_size,
2427 			       SPINOR_OP_SE);
2428 	spi_nor_init_uniform_erase_map(map, erase_mask, params->size);
2429 }
2430 
2431 /**
2432  * spi_nor_init_flags() - Initialize NOR flags for settings that are not defined
2433  * in the JESD216 SFDP standard, thus can not be retrieved when parsing SFDP.
2434  * @nor:	pointer to a 'struct spi_nor'
2435  */
2436 static void spi_nor_init_flags(struct spi_nor *nor)
2437 {
2438 	struct device_node *np = spi_nor_get_flash_node(nor);
2439 	const u16 flags = nor->info->flags;
2440 
2441 	if (of_property_read_bool(np, "broken-flash-reset"))
2442 		nor->flags |= SNOR_F_BROKEN_RESET;
2443 
2444 	if (flags & SPI_NOR_SWP_IS_VOLATILE)
2445 		nor->flags |= SNOR_F_SWP_IS_VOLATILE;
2446 
2447 	if (flags & SPI_NOR_HAS_LOCK)
2448 		nor->flags |= SNOR_F_HAS_LOCK;
2449 
2450 	if (flags & SPI_NOR_HAS_TB) {
2451 		nor->flags |= SNOR_F_HAS_SR_TB;
2452 		if (flags & SPI_NOR_TB_SR_BIT6)
2453 			nor->flags |= SNOR_F_HAS_SR_TB_BIT6;
2454 	}
2455 
2456 	if (flags & SPI_NOR_4BIT_BP) {
2457 		nor->flags |= SNOR_F_HAS_4BIT_BP;
2458 		if (flags & SPI_NOR_BP3_SR_BIT6)
2459 			nor->flags |= SNOR_F_HAS_SR_BP3_BIT6;
2460 	}
2461 
2462 	if (flags & NO_CHIP_ERASE)
2463 		nor->flags |= SNOR_F_NO_OP_CHIP_ERASE;
2464 }
2465 
2466 /**
2467  * spi_nor_init_fixup_flags() - Initialize NOR flags for settings that can not
2468  * be discovered by SFDP for this particular flash because the SFDP table that
2469  * indicates this support is not defined in the flash. In case the table for
2470  * this support is defined but has wrong values, one should instead use a
2471  * post_sfdp() hook to set the SNOR_F equivalent flag.
2472  * @nor:       pointer to a 'struct spi_nor'
2473  */
2474 static void spi_nor_init_fixup_flags(struct spi_nor *nor)
2475 {
2476 	const u8 fixup_flags = nor->info->fixup_flags;
2477 
2478 	if (fixup_flags & SPI_NOR_4B_OPCODES)
2479 		nor->flags |= SNOR_F_4B_OPCODES;
2480 
2481 	if (fixup_flags & SPI_NOR_IO_MODE_EN_VOLATILE)
2482 		nor->flags |= SNOR_F_IO_MODE_EN_VOLATILE;
2483 }
2484 
2485 /**
2486  * spi_nor_late_init_params() - Late initialization of default flash parameters.
2487  * @nor:	pointer to a 'struct spi_nor'
2488  *
2489  * Used to initialize flash parameters that are not declared in the JESD216
2490  * SFDP standard, or where SFDP tables are not defined at all.
2491  * Will replace the spi_nor_manufacturer_init_params() method.
2492  */
2493 static void spi_nor_late_init_params(struct spi_nor *nor)
2494 {
2495 	if (nor->manufacturer && nor->manufacturer->fixups &&
2496 	    nor->manufacturer->fixups->late_init)
2497 		nor->manufacturer->fixups->late_init(nor);
2498 
2499 	if (nor->info->fixups && nor->info->fixups->late_init)
2500 		nor->info->fixups->late_init(nor);
2501 
2502 	spi_nor_init_flags(nor);
2503 	spi_nor_init_fixup_flags(nor);
2504 
2505 	/*
2506 	 * NOR protection support. When locking_ops are not provided, we pick
2507 	 * the default ones.
2508 	 */
2509 	if (nor->flags & SNOR_F_HAS_LOCK && !nor->params->locking_ops)
2510 		spi_nor_init_default_locking_ops(nor);
2511 }
2512 
2513 /**
2514  * spi_nor_sfdp_init_params_deprecated() - Deprecated way of initializing flash
2515  * parameters and settings based on JESD216 SFDP standard.
2516  * @nor:	pointer to a 'struct spi_nor'.
2517  *
2518  * The method has a roll-back mechanism: in case the SFDP parsing fails, the
2519  * legacy flash parameters and settings will be restored.
2520  */
2521 static void spi_nor_sfdp_init_params_deprecated(struct spi_nor *nor)
2522 {
2523 	struct spi_nor_flash_parameter sfdp_params;
2524 
2525 	memcpy(&sfdp_params, nor->params, sizeof(sfdp_params));
2526 
2527 	if (spi_nor_parse_sfdp(nor)) {
2528 		memcpy(nor->params, &sfdp_params, sizeof(*nor->params));
2529 		nor->flags &= ~SNOR_F_4B_OPCODES;
2530 	}
2531 }
2532 
2533 /**
2534  * spi_nor_init_params_deprecated() - Deprecated way of initializing flash
2535  * parameters and settings.
2536  * @nor:	pointer to a 'struct spi_nor'.
2537  *
2538  * The method assumes that flash doesn't support SFDP so it initializes flash
2539  * parameters in spi_nor_no_sfdp_init_params() which later on can be overwritten
2540  * when parsing SFDP, if supported.
2541  */
2542 static void spi_nor_init_params_deprecated(struct spi_nor *nor)
2543 {
2544 	spi_nor_no_sfdp_init_params(nor);
2545 
2546 	spi_nor_manufacturer_init_params(nor);
2547 
2548 	if (nor->info->no_sfdp_flags & (SPI_NOR_DUAL_READ |
2549 					SPI_NOR_QUAD_READ |
2550 					SPI_NOR_OCTAL_READ |
2551 					SPI_NOR_OCTAL_DTR_READ))
2552 		spi_nor_sfdp_init_params_deprecated(nor);
2553 }
2554 
2555 /**
2556  * spi_nor_init_default_params() - Default initialization of flash parameters
2557  * and settings. Done for all flashes, regardless is they define SFDP tables
2558  * or not.
2559  * @nor:	pointer to a 'struct spi_nor'.
2560  */
2561 static void spi_nor_init_default_params(struct spi_nor *nor)
2562 {
2563 	struct spi_nor_flash_parameter *params = nor->params;
2564 	const struct flash_info *info = nor->info;
2565 	struct device_node *np = spi_nor_get_flash_node(nor);
2566 
2567 	params->quad_enable = spi_nor_sr2_bit1_quad_enable;
2568 	params->set_4byte_addr_mode = spansion_set_4byte_addr_mode;
2569 	params->otp.org = &info->otp_org;
2570 
2571 	/* Default to 16-bit Write Status (01h) Command */
2572 	nor->flags |= SNOR_F_HAS_16BIT_SR;
2573 
2574 	/* Set SPI NOR sizes. */
2575 	params->writesize = 1;
2576 	params->size = (u64)info->sector_size * info->n_sectors;
2577 	params->page_size = info->page_size;
2578 
2579 	if (!(info->flags & SPI_NOR_NO_FR)) {
2580 		/* Default to Fast Read for DT and non-DT platform devices. */
2581 		params->hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
2582 
2583 		/* Mask out Fast Read if not requested at DT instantiation. */
2584 		if (np && !of_property_read_bool(np, "m25p,fast-read"))
2585 			params->hwcaps.mask &= ~SNOR_HWCAPS_READ_FAST;
2586 	}
2587 
2588 	/* (Fast) Read settings. */
2589 	params->hwcaps.mask |= SNOR_HWCAPS_READ;
2590 	spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ],
2591 				  0, 0, SPINOR_OP_READ,
2592 				  SNOR_PROTO_1_1_1);
2593 
2594 	if (params->hwcaps.mask & SNOR_HWCAPS_READ_FAST)
2595 		spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_FAST],
2596 					  0, 8, SPINOR_OP_READ_FAST,
2597 					  SNOR_PROTO_1_1_1);
2598 	/* Page Program settings. */
2599 	params->hwcaps.mask |= SNOR_HWCAPS_PP;
2600 	spi_nor_set_pp_settings(&params->page_programs[SNOR_CMD_PP],
2601 				SPINOR_OP_PP, SNOR_PROTO_1_1_1);
2602 
2603 	if (info->flags & SPI_NOR_QUAD_PP) {
2604 		params->hwcaps.mask |= SNOR_HWCAPS_PP_1_1_4;
2605 		spi_nor_set_pp_settings(&params->page_programs[SNOR_CMD_PP_1_1_4],
2606 					SPINOR_OP_PP_1_1_4, SNOR_PROTO_1_1_4);
2607 	}
2608 }
2609 
2610 /**
2611  * spi_nor_init_params() - Initialize the flash's parameters and settings.
2612  * @nor:	pointer to a 'struct spi_nor'.
2613  *
2614  * The flash parameters and settings are initialized based on a sequence of
2615  * calls that are ordered by priority:
2616  *
2617  * 1/ Default flash parameters initialization. The initializations are done
2618  *    based on nor->info data:
2619  *		spi_nor_info_init_params()
2620  *
2621  * which can be overwritten by:
2622  * 2/ Manufacturer flash parameters initialization. The initializations are
2623  *    done based on MFR register, or when the decisions can not be done solely
2624  *    based on MFR, by using specific flash_info tweeks, ->default_init():
2625  *		spi_nor_manufacturer_init_params()
2626  *
2627  * which can be overwritten by:
2628  * 3/ SFDP flash parameters initialization. JESD216 SFDP is a standard and
2629  *    should be more accurate that the above.
2630  *		spi_nor_parse_sfdp() or spi_nor_no_sfdp_init_params()
2631  *
2632  *    Please note that there is a ->post_bfpt() fixup hook that can overwrite
2633  *    the flash parameters and settings immediately after parsing the Basic
2634  *    Flash Parameter Table.
2635  *    spi_nor_post_sfdp_fixups() is called after the SFDP tables are parsed.
2636  *    It is used to tweak various flash parameters when information provided
2637  *    by the SFDP tables are wrong.
2638  *
2639  * which can be overwritten by:
2640  * 4/ Late flash parameters initialization, used to initialize flash
2641  * parameters that are not declared in the JESD216 SFDP standard, or where SFDP
2642  * tables are not defined at all.
2643  *		spi_nor_late_init_params()
2644  *
2645  * Return: 0 on success, -errno otherwise.
2646  */
2647 static int spi_nor_init_params(struct spi_nor *nor)
2648 {
2649 	int ret;
2650 
2651 	nor->params = devm_kzalloc(nor->dev, sizeof(*nor->params), GFP_KERNEL);
2652 	if (!nor->params)
2653 		return -ENOMEM;
2654 
2655 	spi_nor_init_default_params(nor);
2656 
2657 	if (nor->info->parse_sfdp) {
2658 		ret = spi_nor_parse_sfdp(nor);
2659 		if (ret) {
2660 			dev_err(nor->dev, "BFPT parsing failed. Please consider using SPI_NOR_SKIP_SFDP when declaring the flash\n");
2661 			return ret;
2662 		}
2663 	} else if (nor->info->no_sfdp_flags & SPI_NOR_SKIP_SFDP) {
2664 		spi_nor_no_sfdp_init_params(nor);
2665 	} else {
2666 		spi_nor_init_params_deprecated(nor);
2667 	}
2668 
2669 	spi_nor_late_init_params(nor);
2670 
2671 	return 0;
2672 }
2673 
2674 /** spi_nor_octal_dtr_enable() - enable Octal DTR I/O if needed
2675  * @nor:                 pointer to a 'struct spi_nor'
2676  * @enable:              whether to enable or disable Octal DTR
2677  *
2678  * Return: 0 on success, -errno otherwise.
2679  */
2680 static int spi_nor_octal_dtr_enable(struct spi_nor *nor, bool enable)
2681 {
2682 	int ret;
2683 
2684 	if (!nor->params->octal_dtr_enable)
2685 		return 0;
2686 
2687 	if (!(nor->read_proto == SNOR_PROTO_8_8_8_DTR &&
2688 	      nor->write_proto == SNOR_PROTO_8_8_8_DTR))
2689 		return 0;
2690 
2691 	if (!(nor->flags & SNOR_F_IO_MODE_EN_VOLATILE))
2692 		return 0;
2693 
2694 	ret = nor->params->octal_dtr_enable(nor, enable);
2695 	if (ret)
2696 		return ret;
2697 
2698 	if (enable)
2699 		nor->reg_proto = SNOR_PROTO_8_8_8_DTR;
2700 	else
2701 		nor->reg_proto = SNOR_PROTO_1_1_1;
2702 
2703 	return 0;
2704 }
2705 
2706 /**
2707  * spi_nor_quad_enable() - enable Quad I/O if needed.
2708  * @nor:                pointer to a 'struct spi_nor'
2709  *
2710  * Return: 0 on success, -errno otherwise.
2711  */
2712 static int spi_nor_quad_enable(struct spi_nor *nor)
2713 {
2714 	if (!nor->params->quad_enable)
2715 		return 0;
2716 
2717 	if (!(spi_nor_get_protocol_width(nor->read_proto) == 4 ||
2718 	      spi_nor_get_protocol_width(nor->write_proto) == 4))
2719 		return 0;
2720 
2721 	return nor->params->quad_enable(nor);
2722 }
2723 
2724 static int spi_nor_init(struct spi_nor *nor)
2725 {
2726 	int err;
2727 
2728 	err = spi_nor_octal_dtr_enable(nor, true);
2729 	if (err) {
2730 		dev_dbg(nor->dev, "octal mode not supported\n");
2731 		return err;
2732 	}
2733 
2734 	err = spi_nor_quad_enable(nor);
2735 	if (err) {
2736 		dev_dbg(nor->dev, "quad mode not supported\n");
2737 		return err;
2738 	}
2739 
2740 	/*
2741 	 * Some SPI NOR flashes are write protected by default after a power-on
2742 	 * reset cycle, in order to avoid inadvertent writes during power-up.
2743 	 * Backward compatibility imposes to unlock the entire flash memory
2744 	 * array at power-up by default. Depending on the kernel configuration
2745 	 * (1) do nothing, (2) always unlock the entire flash array or (3)
2746 	 * unlock the entire flash array only when the software write
2747 	 * protection bits are volatile. The latter is indicated by
2748 	 * SNOR_F_SWP_IS_VOLATILE.
2749 	 */
2750 	if (IS_ENABLED(CONFIG_MTD_SPI_NOR_SWP_DISABLE) ||
2751 	    (IS_ENABLED(CONFIG_MTD_SPI_NOR_SWP_DISABLE_ON_VOLATILE) &&
2752 	     nor->flags & SNOR_F_SWP_IS_VOLATILE))
2753 		spi_nor_try_unlock_all(nor);
2754 
2755 	if (nor->addr_nbytes == 4 &&
2756 	    nor->read_proto != SNOR_PROTO_8_8_8_DTR &&
2757 	    !(nor->flags & SNOR_F_4B_OPCODES)) {
2758 		/*
2759 		 * If the RESET# pin isn't hooked up properly, or the system
2760 		 * otherwise doesn't perform a reset command in the boot
2761 		 * sequence, it's impossible to 100% protect against unexpected
2762 		 * reboots (e.g., crashes). Warn the user (or hopefully, system
2763 		 * designer) that this is bad.
2764 		 */
2765 		WARN_ONCE(nor->flags & SNOR_F_BROKEN_RESET,
2766 			  "enabling reset hack; may not recover from unexpected reboots\n");
2767 		err = nor->params->set_4byte_addr_mode(nor, true);
2768 		if (err && err != -ENOTSUPP)
2769 			return err;
2770 	}
2771 
2772 	return 0;
2773 }
2774 
2775 /**
2776  * spi_nor_soft_reset() - Perform a software reset
2777  * @nor:	pointer to 'struct spi_nor'
2778  *
2779  * Performs a "Soft Reset and Enter Default Protocol Mode" sequence which resets
2780  * the device to its power-on-reset state. This is useful when the software has
2781  * made some changes to device (volatile) registers and needs to reset it before
2782  * shutting down, for example.
2783  *
2784  * Not every flash supports this sequence. The same set of opcodes might be used
2785  * for some other operation on a flash that does not support this. Support for
2786  * this sequence can be discovered via SFDP in the BFPT table.
2787  *
2788  * Return: 0 on success, -errno otherwise.
2789  */
2790 static void spi_nor_soft_reset(struct spi_nor *nor)
2791 {
2792 	struct spi_mem_op op;
2793 	int ret;
2794 
2795 	op = (struct spi_mem_op)SPINOR_SRSTEN_OP;
2796 
2797 	spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
2798 
2799 	ret = spi_mem_exec_op(nor->spimem, &op);
2800 	if (ret) {
2801 		dev_warn(nor->dev, "Software reset failed: %d\n", ret);
2802 		return;
2803 	}
2804 
2805 	op = (struct spi_mem_op)SPINOR_SRST_OP;
2806 
2807 	spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
2808 
2809 	ret = spi_mem_exec_op(nor->spimem, &op);
2810 	if (ret) {
2811 		dev_warn(nor->dev, "Software reset failed: %d\n", ret);
2812 		return;
2813 	}
2814 
2815 	/*
2816 	 * Software Reset is not instant, and the delay varies from flash to
2817 	 * flash. Looking at a few flashes, most range somewhere below 100
2818 	 * microseconds. So, sleep for a range of 200-400 us.
2819 	 */
2820 	usleep_range(SPI_NOR_SRST_SLEEP_MIN, SPI_NOR_SRST_SLEEP_MAX);
2821 }
2822 
2823 /* mtd suspend handler */
2824 static int spi_nor_suspend(struct mtd_info *mtd)
2825 {
2826 	struct spi_nor *nor = mtd_to_spi_nor(mtd);
2827 	int ret;
2828 
2829 	/* Disable octal DTR mode if we enabled it. */
2830 	ret = spi_nor_octal_dtr_enable(nor, false);
2831 	if (ret)
2832 		dev_err(nor->dev, "suspend() failed\n");
2833 
2834 	return ret;
2835 }
2836 
2837 /* mtd resume handler */
2838 static void spi_nor_resume(struct mtd_info *mtd)
2839 {
2840 	struct spi_nor *nor = mtd_to_spi_nor(mtd);
2841 	struct device *dev = nor->dev;
2842 	int ret;
2843 
2844 	/* re-initialize the nor chip */
2845 	ret = spi_nor_init(nor);
2846 	if (ret)
2847 		dev_err(dev, "resume() failed\n");
2848 }
2849 
2850 static int spi_nor_get_device(struct mtd_info *mtd)
2851 {
2852 	struct mtd_info *master = mtd_get_master(mtd);
2853 	struct spi_nor *nor = mtd_to_spi_nor(master);
2854 	struct device *dev;
2855 
2856 	if (nor->spimem)
2857 		dev = nor->spimem->spi->controller->dev.parent;
2858 	else
2859 		dev = nor->dev;
2860 
2861 	if (!try_module_get(dev->driver->owner))
2862 		return -ENODEV;
2863 
2864 	return 0;
2865 }
2866 
2867 static void spi_nor_put_device(struct mtd_info *mtd)
2868 {
2869 	struct mtd_info *master = mtd_get_master(mtd);
2870 	struct spi_nor *nor = mtd_to_spi_nor(master);
2871 	struct device *dev;
2872 
2873 	if (nor->spimem)
2874 		dev = nor->spimem->spi->controller->dev.parent;
2875 	else
2876 		dev = nor->dev;
2877 
2878 	module_put(dev->driver->owner);
2879 }
2880 
2881 void spi_nor_restore(struct spi_nor *nor)
2882 {
2883 	int ret;
2884 
2885 	/* restore the addressing mode */
2886 	if (nor->addr_nbytes == 4 && !(nor->flags & SNOR_F_4B_OPCODES) &&
2887 	    nor->flags & SNOR_F_BROKEN_RESET) {
2888 		ret = nor->params->set_4byte_addr_mode(nor, false);
2889 		if (ret)
2890 			/*
2891 			 * Do not stop the execution in the hope that the flash
2892 			 * will default to the 3-byte address mode after the
2893 			 * software reset.
2894 			 */
2895 			dev_err(nor->dev, "Failed to exit 4-byte address mode, err = %d\n", ret);
2896 	}
2897 
2898 	if (nor->flags & SNOR_F_SOFT_RESET)
2899 		spi_nor_soft_reset(nor);
2900 }
2901 EXPORT_SYMBOL_GPL(spi_nor_restore);
2902 
2903 static const struct flash_info *spi_nor_match_name(struct spi_nor *nor,
2904 						   const char *name)
2905 {
2906 	unsigned int i, j;
2907 
2908 	for (i = 0; i < ARRAY_SIZE(manufacturers); i++) {
2909 		for (j = 0; j < manufacturers[i]->nparts; j++) {
2910 			if (!strcmp(name, manufacturers[i]->parts[j].name)) {
2911 				nor->manufacturer = manufacturers[i];
2912 				return &manufacturers[i]->parts[j];
2913 			}
2914 		}
2915 	}
2916 
2917 	return NULL;
2918 }
2919 
2920 static const struct flash_info *spi_nor_get_flash_info(struct spi_nor *nor,
2921 						       const char *name)
2922 {
2923 	const struct flash_info *info = NULL;
2924 
2925 	if (name)
2926 		info = spi_nor_match_name(nor, name);
2927 	/* Try to auto-detect if chip name wasn't specified or not found */
2928 	if (!info)
2929 		return spi_nor_detect(nor);
2930 
2931 	/*
2932 	 * If caller has specified name of flash model that can normally be
2933 	 * detected using JEDEC, let's verify it.
2934 	 */
2935 	if (name && info->id_len) {
2936 		const struct flash_info *jinfo;
2937 
2938 		jinfo = spi_nor_detect(nor);
2939 		if (IS_ERR(jinfo)) {
2940 			return jinfo;
2941 		} else if (jinfo != info) {
2942 			/*
2943 			 * JEDEC knows better, so overwrite platform ID. We
2944 			 * can't trust partitions any longer, but we'll let
2945 			 * mtd apply them anyway, since some partitions may be
2946 			 * marked read-only, and we don't want to lose that
2947 			 * information, even if it's not 100% accurate.
2948 			 */
2949 			dev_warn(nor->dev, "found %s, expected %s\n",
2950 				 jinfo->name, info->name);
2951 			info = jinfo;
2952 		}
2953 	}
2954 
2955 	return info;
2956 }
2957 
2958 static void spi_nor_set_mtd_info(struct spi_nor *nor)
2959 {
2960 	struct mtd_info *mtd = &nor->mtd;
2961 	struct device *dev = nor->dev;
2962 
2963 	spi_nor_set_mtd_locking_ops(nor);
2964 	spi_nor_set_mtd_otp_ops(nor);
2965 
2966 	mtd->dev.parent = dev;
2967 	if (!mtd->name)
2968 		mtd->name = dev_name(dev);
2969 	mtd->type = MTD_NORFLASH;
2970 	mtd->flags = MTD_CAP_NORFLASH;
2971 	if (nor->info->flags & SPI_NOR_NO_ERASE)
2972 		mtd->flags |= MTD_NO_ERASE;
2973 	else
2974 		mtd->_erase = spi_nor_erase;
2975 	mtd->writesize = nor->params->writesize;
2976 	mtd->writebufsize = nor->params->page_size;
2977 	mtd->size = nor->params->size;
2978 	mtd->_read = spi_nor_read;
2979 	/* Might be already set by some SST flashes. */
2980 	if (!mtd->_write)
2981 		mtd->_write = spi_nor_write;
2982 	mtd->_suspend = spi_nor_suspend;
2983 	mtd->_resume = spi_nor_resume;
2984 	mtd->_get_device = spi_nor_get_device;
2985 	mtd->_put_device = spi_nor_put_device;
2986 }
2987 
2988 static int spi_nor_hw_reset(struct spi_nor *nor)
2989 {
2990 	struct gpio_desc *reset;
2991 
2992 	reset = devm_gpiod_get_optional(nor->dev, "reset", GPIOD_OUT_LOW);
2993 	if (IS_ERR_OR_NULL(reset))
2994 		return PTR_ERR_OR_ZERO(reset);
2995 
2996 	/*
2997 	 * Experimental delay values by looking at different flash device
2998 	 * vendors datasheets.
2999 	 */
3000 	usleep_range(1, 5);
3001 	gpiod_set_value_cansleep(reset, 1);
3002 	usleep_range(100, 150);
3003 	gpiod_set_value_cansleep(reset, 0);
3004 	usleep_range(1000, 1200);
3005 
3006 	return 0;
3007 }
3008 
3009 int spi_nor_scan(struct spi_nor *nor, const char *name,
3010 		 const struct spi_nor_hwcaps *hwcaps)
3011 {
3012 	const struct flash_info *info;
3013 	struct device *dev = nor->dev;
3014 	struct mtd_info *mtd = &nor->mtd;
3015 	int ret;
3016 	int i;
3017 
3018 	ret = spi_nor_check(nor);
3019 	if (ret)
3020 		return ret;
3021 
3022 	/* Reset SPI protocol for all commands. */
3023 	nor->reg_proto = SNOR_PROTO_1_1_1;
3024 	nor->read_proto = SNOR_PROTO_1_1_1;
3025 	nor->write_proto = SNOR_PROTO_1_1_1;
3026 
3027 	/*
3028 	 * We need the bounce buffer early to read/write registers when going
3029 	 * through the spi-mem layer (buffers have to be DMA-able).
3030 	 * For spi-mem drivers, we'll reallocate a new buffer if
3031 	 * nor->params->page_size turns out to be greater than PAGE_SIZE (which
3032 	 * shouldn't happen before long since NOR pages are usually less
3033 	 * than 1KB) after spi_nor_scan() returns.
3034 	 */
3035 	nor->bouncebuf_size = PAGE_SIZE;
3036 	nor->bouncebuf = devm_kmalloc(dev, nor->bouncebuf_size,
3037 				      GFP_KERNEL);
3038 	if (!nor->bouncebuf)
3039 		return -ENOMEM;
3040 
3041 	ret = spi_nor_hw_reset(nor);
3042 	if (ret)
3043 		return ret;
3044 
3045 	info = spi_nor_get_flash_info(nor, name);
3046 	if (IS_ERR(info))
3047 		return PTR_ERR(info);
3048 
3049 	nor->info = info;
3050 
3051 	mutex_init(&nor->lock);
3052 
3053 	/* Init flash parameters based on flash_info struct and SFDP */
3054 	ret = spi_nor_init_params(nor);
3055 	if (ret)
3056 		return ret;
3057 
3058 	/*
3059 	 * Configure the SPI memory:
3060 	 * - select op codes for (Fast) Read, Page Program and Sector Erase.
3061 	 * - set the number of dummy cycles (mode cycles + wait states).
3062 	 * - set the SPI protocols for register and memory accesses.
3063 	 * - set the number of address bytes.
3064 	 */
3065 	ret = spi_nor_setup(nor, hwcaps);
3066 	if (ret)
3067 		return ret;
3068 
3069 	/* Send all the required SPI flash commands to initialize device */
3070 	ret = spi_nor_init(nor);
3071 	if (ret)
3072 		return ret;
3073 
3074 	/* No mtd_info fields should be used up to this point. */
3075 	spi_nor_set_mtd_info(nor);
3076 
3077 	dev_info(dev, "%s (%lld Kbytes)\n", info->name,
3078 			(long long)mtd->size >> 10);
3079 
3080 	dev_dbg(dev,
3081 		"mtd .name = %s, .size = 0x%llx (%lldMiB), "
3082 		".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
3083 		mtd->name, (long long)mtd->size, (long long)(mtd->size >> 20),
3084 		mtd->erasesize, mtd->erasesize / 1024, mtd->numeraseregions);
3085 
3086 	if (mtd->numeraseregions)
3087 		for (i = 0; i < mtd->numeraseregions; i++)
3088 			dev_dbg(dev,
3089 				"mtd.eraseregions[%d] = { .offset = 0x%llx, "
3090 				".erasesize = 0x%.8x (%uKiB), "
3091 				".numblocks = %d }\n",
3092 				i, (long long)mtd->eraseregions[i].offset,
3093 				mtd->eraseregions[i].erasesize,
3094 				mtd->eraseregions[i].erasesize / 1024,
3095 				mtd->eraseregions[i].numblocks);
3096 	return 0;
3097 }
3098 EXPORT_SYMBOL_GPL(spi_nor_scan);
3099 
3100 static int spi_nor_create_read_dirmap(struct spi_nor *nor)
3101 {
3102 	struct spi_mem_dirmap_info info = {
3103 		.op_tmpl = SPI_MEM_OP(SPI_MEM_OP_CMD(nor->read_opcode, 0),
3104 				      SPI_MEM_OP_ADDR(nor->addr_nbytes, 0, 0),
3105 				      SPI_MEM_OP_DUMMY(nor->read_dummy, 0),
3106 				      SPI_MEM_OP_DATA_IN(0, NULL, 0)),
3107 		.offset = 0,
3108 		.length = nor->params->size,
3109 	};
3110 	struct spi_mem_op *op = &info.op_tmpl;
3111 
3112 	spi_nor_spimem_setup_op(nor, op, nor->read_proto);
3113 
3114 	/* convert the dummy cycles to the number of bytes */
3115 	op->dummy.nbytes = (nor->read_dummy * op->dummy.buswidth) / 8;
3116 	if (spi_nor_protocol_is_dtr(nor->read_proto))
3117 		op->dummy.nbytes *= 2;
3118 
3119 	/*
3120 	 * Since spi_nor_spimem_setup_op() only sets buswidth when the number
3121 	 * of data bytes is non-zero, the data buswidth won't be set here. So,
3122 	 * do it explicitly.
3123 	 */
3124 	op->data.buswidth = spi_nor_get_protocol_data_nbits(nor->read_proto);
3125 
3126 	nor->dirmap.rdesc = devm_spi_mem_dirmap_create(nor->dev, nor->spimem,
3127 						       &info);
3128 	return PTR_ERR_OR_ZERO(nor->dirmap.rdesc);
3129 }
3130 
3131 static int spi_nor_create_write_dirmap(struct spi_nor *nor)
3132 {
3133 	struct spi_mem_dirmap_info info = {
3134 		.op_tmpl = SPI_MEM_OP(SPI_MEM_OP_CMD(nor->program_opcode, 0),
3135 				      SPI_MEM_OP_ADDR(nor->addr_nbytes, 0, 0),
3136 				      SPI_MEM_OP_NO_DUMMY,
3137 				      SPI_MEM_OP_DATA_OUT(0, NULL, 0)),
3138 		.offset = 0,
3139 		.length = nor->params->size,
3140 	};
3141 	struct spi_mem_op *op = &info.op_tmpl;
3142 
3143 	if (nor->program_opcode == SPINOR_OP_AAI_WP && nor->sst_write_second)
3144 		op->addr.nbytes = 0;
3145 
3146 	spi_nor_spimem_setup_op(nor, op, nor->write_proto);
3147 
3148 	/*
3149 	 * Since spi_nor_spimem_setup_op() only sets buswidth when the number
3150 	 * of data bytes is non-zero, the data buswidth won't be set here. So,
3151 	 * do it explicitly.
3152 	 */
3153 	op->data.buswidth = spi_nor_get_protocol_data_nbits(nor->write_proto);
3154 
3155 	nor->dirmap.wdesc = devm_spi_mem_dirmap_create(nor->dev, nor->spimem,
3156 						       &info);
3157 	return PTR_ERR_OR_ZERO(nor->dirmap.wdesc);
3158 }
3159 
3160 static int spi_nor_probe(struct spi_mem *spimem)
3161 {
3162 	struct spi_device *spi = spimem->spi;
3163 	struct flash_platform_data *data = dev_get_platdata(&spi->dev);
3164 	struct spi_nor *nor;
3165 	/*
3166 	 * Enable all caps by default. The core will mask them after
3167 	 * checking what's really supported using spi_mem_supports_op().
3168 	 */
3169 	const struct spi_nor_hwcaps hwcaps = { .mask = SNOR_HWCAPS_ALL };
3170 	char *flash_name;
3171 	int ret;
3172 
3173 	nor = devm_kzalloc(&spi->dev, sizeof(*nor), GFP_KERNEL);
3174 	if (!nor)
3175 		return -ENOMEM;
3176 
3177 	nor->spimem = spimem;
3178 	nor->dev = &spi->dev;
3179 	spi_nor_set_flash_node(nor, spi->dev.of_node);
3180 
3181 	spi_mem_set_drvdata(spimem, nor);
3182 
3183 	if (data && data->name)
3184 		nor->mtd.name = data->name;
3185 
3186 	if (!nor->mtd.name)
3187 		nor->mtd.name = spi_mem_get_name(spimem);
3188 
3189 	/*
3190 	 * For some (historical?) reason many platforms provide two different
3191 	 * names in flash_platform_data: "name" and "type". Quite often name is
3192 	 * set to "m25p80" and then "type" provides a real chip name.
3193 	 * If that's the case, respect "type" and ignore a "name".
3194 	 */
3195 	if (data && data->type)
3196 		flash_name = data->type;
3197 	else if (!strcmp(spi->modalias, "spi-nor"))
3198 		flash_name = NULL; /* auto-detect */
3199 	else
3200 		flash_name = spi->modalias;
3201 
3202 	ret = spi_nor_scan(nor, flash_name, &hwcaps);
3203 	if (ret)
3204 		return ret;
3205 
3206 	spi_nor_debugfs_register(nor);
3207 
3208 	/*
3209 	 * None of the existing parts have > 512B pages, but let's play safe
3210 	 * and add this logic so that if anyone ever adds support for such
3211 	 * a NOR we don't end up with buffer overflows.
3212 	 */
3213 	if (nor->params->page_size > PAGE_SIZE) {
3214 		nor->bouncebuf_size = nor->params->page_size;
3215 		devm_kfree(nor->dev, nor->bouncebuf);
3216 		nor->bouncebuf = devm_kmalloc(nor->dev,
3217 					      nor->bouncebuf_size,
3218 					      GFP_KERNEL);
3219 		if (!nor->bouncebuf)
3220 			return -ENOMEM;
3221 	}
3222 
3223 	ret = spi_nor_create_read_dirmap(nor);
3224 	if (ret)
3225 		return ret;
3226 
3227 	ret = spi_nor_create_write_dirmap(nor);
3228 	if (ret)
3229 		return ret;
3230 
3231 	return mtd_device_register(&nor->mtd, data ? data->parts : NULL,
3232 				   data ? data->nr_parts : 0);
3233 }
3234 
3235 static int spi_nor_remove(struct spi_mem *spimem)
3236 {
3237 	struct spi_nor *nor = spi_mem_get_drvdata(spimem);
3238 
3239 	spi_nor_restore(nor);
3240 
3241 	/* Clean up MTD stuff. */
3242 	return mtd_device_unregister(&nor->mtd);
3243 }
3244 
3245 static void spi_nor_shutdown(struct spi_mem *spimem)
3246 {
3247 	struct spi_nor *nor = spi_mem_get_drvdata(spimem);
3248 
3249 	spi_nor_restore(nor);
3250 }
3251 
3252 /*
3253  * Do NOT add to this array without reading the following:
3254  *
3255  * Historically, many flash devices are bound to this driver by their name. But
3256  * since most of these flash are compatible to some extent, and their
3257  * differences can often be differentiated by the JEDEC read-ID command, we
3258  * encourage new users to add support to the spi-nor library, and simply bind
3259  * against a generic string here (e.g., "jedec,spi-nor").
3260  *
3261  * Many flash names are kept here in this list to keep them available
3262  * as module aliases for existing platforms.
3263  */
3264 static const struct spi_device_id spi_nor_dev_ids[] = {
3265 	/*
3266 	 * Allow non-DT platform devices to bind to the "spi-nor" modalias, and
3267 	 * hack around the fact that the SPI core does not provide uevent
3268 	 * matching for .of_match_table
3269 	 */
3270 	{"spi-nor"},
3271 
3272 	/*
3273 	 * Entries not used in DTs that should be safe to drop after replacing
3274 	 * them with "spi-nor" in platform data.
3275 	 */
3276 	{"s25sl064a"},	{"w25x16"},	{"m25p10"},	{"m25px64"},
3277 
3278 	/*
3279 	 * Entries that were used in DTs without "jedec,spi-nor" fallback and
3280 	 * should be kept for backward compatibility.
3281 	 */
3282 	{"at25df321a"},	{"at25df641"},	{"at26df081a"},
3283 	{"mx25l4005a"},	{"mx25l1606e"},	{"mx25l6405d"},	{"mx25l12805d"},
3284 	{"mx25l25635e"},{"mx66l51235l"},
3285 	{"n25q064"},	{"n25q128a11"},	{"n25q128a13"},	{"n25q512a"},
3286 	{"s25fl256s1"},	{"s25fl512s"},	{"s25sl12801"},	{"s25fl008k"},
3287 	{"s25fl064k"},
3288 	{"sst25vf040b"},{"sst25vf016b"},{"sst25vf032b"},{"sst25wf040"},
3289 	{"m25p40"},	{"m25p80"},	{"m25p16"},	{"m25p32"},
3290 	{"m25p64"},	{"m25p128"},
3291 	{"w25x80"},	{"w25x32"},	{"w25q32"},	{"w25q32dw"},
3292 	{"w25q80bl"},	{"w25q128"},	{"w25q256"},
3293 
3294 	/* Flashes that can't be detected using JEDEC */
3295 	{"m25p05-nonjedec"},	{"m25p10-nonjedec"},	{"m25p20-nonjedec"},
3296 	{"m25p40-nonjedec"},	{"m25p80-nonjedec"},	{"m25p16-nonjedec"},
3297 	{"m25p32-nonjedec"},	{"m25p64-nonjedec"},	{"m25p128-nonjedec"},
3298 
3299 	/* Everspin MRAMs (non-JEDEC) */
3300 	{ "mr25h128" }, /* 128 Kib, 40 MHz */
3301 	{ "mr25h256" }, /* 256 Kib, 40 MHz */
3302 	{ "mr25h10" },  /*   1 Mib, 40 MHz */
3303 	{ "mr25h40" },  /*   4 Mib, 40 MHz */
3304 
3305 	{ },
3306 };
3307 MODULE_DEVICE_TABLE(spi, spi_nor_dev_ids);
3308 
3309 static const struct of_device_id spi_nor_of_table[] = {
3310 	/*
3311 	 * Generic compatibility for SPI NOR that can be identified by the
3312 	 * JEDEC READ ID opcode (0x9F). Use this, if possible.
3313 	 */
3314 	{ .compatible = "jedec,spi-nor" },
3315 	{ /* sentinel */ },
3316 };
3317 MODULE_DEVICE_TABLE(of, spi_nor_of_table);
3318 
3319 /*
3320  * REVISIT: many of these chips have deep power-down modes, which
3321  * should clearly be entered on suspend() to minimize power use.
3322  * And also when they're otherwise idle...
3323  */
3324 static struct spi_mem_driver spi_nor_driver = {
3325 	.spidrv = {
3326 		.driver = {
3327 			.name = "spi-nor",
3328 			.of_match_table = spi_nor_of_table,
3329 			.dev_groups = spi_nor_sysfs_groups,
3330 		},
3331 		.id_table = spi_nor_dev_ids,
3332 	},
3333 	.probe = spi_nor_probe,
3334 	.remove = spi_nor_remove,
3335 	.shutdown = spi_nor_shutdown,
3336 };
3337 module_spi_mem_driver(spi_nor_driver);
3338 
3339 MODULE_LICENSE("GPL v2");
3340 MODULE_AUTHOR("Huang Shijie <shijie8@gmail.com>");
3341 MODULE_AUTHOR("Mike Lavender");
3342 MODULE_DESCRIPTION("framework for SPI NOR");
3343