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