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