09bfde66853def948cab1f59b3a769468aff4663
[platform/kernel/u-boot.git] / drivers / mtd / nand / spi / core.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2016-2017 Micron Technology, Inc.
4  *
5  * Authors:
6  *      Peter Pan <peterpandong@micron.com>
7  *      Boris Brezillon <boris.brezillon@bootlin.com>
8  */
9
10 #define pr_fmt(fmt)     "spi-nand: " fmt
11
12 #ifndef __UBOOT__
13 #include <linux/device.h>
14 #include <linux/jiffies.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/mtd/spinand.h>
18 #include <linux/of.h>
19 #include <linux/slab.h>
20 #include <linux/spi/spi.h>
21 #include <linux/spi/spi-mem.h>
22 #else
23 #include <common.h>
24 #include <errno.h>
25 #include <spi.h>
26 #include <spi-mem.h>
27 #include <dm/device_compat.h>
28 #include <dm/devres.h>
29 #include <linux/bitops.h>
30 #include <linux/bug.h>
31 #include <linux/mtd/spinand.h>
32 #endif
33
34 /* SPI NAND index visible in MTD names */
35 static int spi_nand_idx;
36
37 static void spinand_cache_op_adjust_colum(struct spinand_device *spinand,
38                                           const struct nand_page_io_req *req,
39                                           u16 *column)
40 {
41         struct nand_device *nand = spinand_to_nand(spinand);
42         unsigned int shift;
43
44         if (nand->memorg.planes_per_lun < 2)
45                 return;
46
47         /* The plane number is passed in MSB just above the column address */
48         shift = fls(nand->memorg.pagesize);
49         *column |= req->pos.plane << shift;
50 }
51
52 static int spinand_read_reg_op(struct spinand_device *spinand, u8 reg, u8 *val)
53 {
54         struct spi_mem_op op = SPINAND_GET_FEATURE_OP(reg,
55                                                       spinand->scratchbuf);
56         int ret;
57
58         ret = spi_mem_exec_op(spinand->slave, &op);
59         if (ret)
60                 return ret;
61
62         *val = *spinand->scratchbuf;
63         return 0;
64 }
65
66 static int spinand_write_reg_op(struct spinand_device *spinand, u8 reg, u8 val)
67 {
68         struct spi_mem_op op = SPINAND_SET_FEATURE_OP(reg,
69                                                       spinand->scratchbuf);
70
71         *spinand->scratchbuf = val;
72         return spi_mem_exec_op(spinand->slave, &op);
73 }
74
75 static int spinand_read_status(struct spinand_device *spinand, u8 *status)
76 {
77         return spinand_read_reg_op(spinand, REG_STATUS, status);
78 }
79
80 static int spinand_get_cfg(struct spinand_device *spinand, u8 *cfg)
81 {
82         struct nand_device *nand = spinand_to_nand(spinand);
83
84         if (WARN_ON(spinand->cur_target < 0 ||
85                     spinand->cur_target >= nand->memorg.ntargets))
86                 return -EINVAL;
87
88         *cfg = spinand->cfg_cache[spinand->cur_target];
89         return 0;
90 }
91
92 static int spinand_set_cfg(struct spinand_device *spinand, u8 cfg)
93 {
94         struct nand_device *nand = spinand_to_nand(spinand);
95         int ret;
96
97         if (WARN_ON(spinand->cur_target < 0 ||
98                     spinand->cur_target >= nand->memorg.ntargets))
99                 return -EINVAL;
100
101         if (spinand->cfg_cache[spinand->cur_target] == cfg)
102                 return 0;
103
104         ret = spinand_write_reg_op(spinand, REG_CFG, cfg);
105         if (ret)
106                 return ret;
107
108         spinand->cfg_cache[spinand->cur_target] = cfg;
109         return 0;
110 }
111
112 /**
113  * spinand_upd_cfg() - Update the configuration register
114  * @spinand: the spinand device
115  * @mask: the mask encoding the bits to update in the config reg
116  * @val: the new value to apply
117  *
118  * Update the configuration register.
119  *
120  * Return: 0 on success, a negative error code otherwise.
121  */
122 int spinand_upd_cfg(struct spinand_device *spinand, u8 mask, u8 val)
123 {
124         int ret;
125         u8 cfg;
126
127         ret = spinand_get_cfg(spinand, &cfg);
128         if (ret)
129                 return ret;
130
131         cfg &= ~mask;
132         cfg |= val;
133
134         return spinand_set_cfg(spinand, cfg);
135 }
136
137 /**
138  * spinand_select_target() - Select a specific NAND target/die
139  * @spinand: the spinand device
140  * @target: the target/die to select
141  *
142  * Select a new target/die. If chip only has one die, this function is a NOOP.
143  *
144  * Return: 0 on success, a negative error code otherwise.
145  */
146 int spinand_select_target(struct spinand_device *spinand, unsigned int target)
147 {
148         struct nand_device *nand = spinand_to_nand(spinand);
149         int ret;
150
151         if (WARN_ON(target >= nand->memorg.ntargets))
152                 return -EINVAL;
153
154         if (spinand->cur_target == target)
155                 return 0;
156
157         if (nand->memorg.ntargets == 1) {
158                 spinand->cur_target = target;
159                 return 0;
160         }
161
162         ret = spinand->select_target(spinand, target);
163         if (ret)
164                 return ret;
165
166         spinand->cur_target = target;
167         return 0;
168 }
169
170 static int spinand_init_cfg_cache(struct spinand_device *spinand)
171 {
172         struct nand_device *nand = spinand_to_nand(spinand);
173         struct udevice *dev = spinand->slave->dev;
174         unsigned int target;
175         int ret;
176
177         spinand->cfg_cache = devm_kzalloc(dev,
178                                           sizeof(*spinand->cfg_cache) *
179                                           nand->memorg.ntargets,
180                                           GFP_KERNEL);
181         if (!spinand->cfg_cache)
182                 return -ENOMEM;
183
184         for (target = 0; target < nand->memorg.ntargets; target++) {
185                 ret = spinand_select_target(spinand, target);
186                 if (ret)
187                         return ret;
188
189                 /*
190                  * We use spinand_read_reg_op() instead of spinand_get_cfg()
191                  * here to bypass the config cache.
192                  */
193                 ret = spinand_read_reg_op(spinand, REG_CFG,
194                                           &spinand->cfg_cache[target]);
195                 if (ret)
196                         return ret;
197         }
198
199         return 0;
200 }
201
202 static int spinand_init_quad_enable(struct spinand_device *spinand)
203 {
204         bool enable = false;
205
206         if (!(spinand->flags & SPINAND_HAS_QE_BIT))
207                 return 0;
208
209         if (spinand->op_templates.read_cache->data.buswidth == 4 ||
210             spinand->op_templates.write_cache->data.buswidth == 4 ||
211             spinand->op_templates.update_cache->data.buswidth == 4)
212                 enable = true;
213
214         return spinand_upd_cfg(spinand, CFG_QUAD_ENABLE,
215                                enable ? CFG_QUAD_ENABLE : 0);
216 }
217
218 static int spinand_ecc_enable(struct spinand_device *spinand,
219                               bool enable)
220 {
221         return spinand_upd_cfg(spinand, CFG_ECC_ENABLE,
222                                enable ? CFG_ECC_ENABLE : 0);
223 }
224
225 static int spinand_write_enable_op(struct spinand_device *spinand)
226 {
227         struct spi_mem_op op = SPINAND_WR_EN_DIS_OP(true);
228
229         return spi_mem_exec_op(spinand->slave, &op);
230 }
231
232 static int spinand_load_page_op(struct spinand_device *spinand,
233                                 const struct nand_page_io_req *req)
234 {
235         struct nand_device *nand = spinand_to_nand(spinand);
236         unsigned int row = nanddev_pos_to_row(nand, &req->pos);
237         struct spi_mem_op op = SPINAND_PAGE_READ_OP(row);
238
239         return spi_mem_exec_op(spinand->slave, &op);
240 }
241
242 static int spinand_read_from_cache_op(struct spinand_device *spinand,
243                                       const struct nand_page_io_req *req)
244 {
245         struct spi_mem_op op = *spinand->op_templates.read_cache;
246         struct nand_device *nand = spinand_to_nand(spinand);
247         struct mtd_info *mtd = nanddev_to_mtd(nand);
248         struct nand_page_io_req adjreq = *req;
249         unsigned int nbytes = 0;
250         void *buf = NULL;
251         u16 column = 0;
252         int ret;
253
254         if (req->datalen) {
255                 adjreq.datalen = nanddev_page_size(nand);
256                 adjreq.dataoffs = 0;
257                 adjreq.databuf.in = spinand->databuf;
258                 buf = spinand->databuf;
259                 nbytes = adjreq.datalen;
260         }
261
262         if (req->ooblen) {
263                 adjreq.ooblen = nanddev_per_page_oobsize(nand);
264                 adjreq.ooboffs = 0;
265                 adjreq.oobbuf.in = spinand->oobbuf;
266                 nbytes += nanddev_per_page_oobsize(nand);
267                 if (!buf) {
268                         buf = spinand->oobbuf;
269                         column = nanddev_page_size(nand);
270                 }
271         }
272
273         spinand_cache_op_adjust_colum(spinand, &adjreq, &column);
274         op.addr.val = column;
275
276         /*
277          * Some controllers are limited in term of max RX data size. In this
278          * case, just repeat the READ_CACHE operation after updating the
279          * column.
280          */
281         while (nbytes) {
282                 op.data.buf.in = buf;
283                 op.data.nbytes = nbytes;
284                 ret = spi_mem_adjust_op_size(spinand->slave, &op);
285                 if (ret)
286                         return ret;
287
288                 ret = spi_mem_exec_op(spinand->slave, &op);
289                 if (ret)
290                         return ret;
291
292                 buf += op.data.nbytes;
293                 nbytes -= op.data.nbytes;
294                 op.addr.val += op.data.nbytes;
295         }
296
297         if (req->datalen)
298                 memcpy(req->databuf.in, spinand->databuf + req->dataoffs,
299                        req->datalen);
300
301         if (req->ooblen) {
302                 if (req->mode == MTD_OPS_AUTO_OOB)
303                         mtd_ooblayout_get_databytes(mtd, req->oobbuf.in,
304                                                     spinand->oobbuf,
305                                                     req->ooboffs,
306                                                     req->ooblen);
307                 else
308                         memcpy(req->oobbuf.in, spinand->oobbuf + req->ooboffs,
309                                req->ooblen);
310         }
311
312         return 0;
313 }
314
315 static int spinand_write_to_cache_op(struct spinand_device *spinand,
316                                      const struct nand_page_io_req *req)
317 {
318         struct spi_mem_op op = *spinand->op_templates.write_cache;
319         struct nand_device *nand = spinand_to_nand(spinand);
320         struct mtd_info *mtd = nanddev_to_mtd(nand);
321         struct nand_page_io_req adjreq = *req;
322         unsigned int nbytes = 0;
323         void *buf = NULL;
324         u16 column = 0;
325         int ret;
326
327         memset(spinand->databuf, 0xff,
328                nanddev_page_size(nand) +
329                nanddev_per_page_oobsize(nand));
330
331         if (req->datalen) {
332                 memcpy(spinand->databuf + req->dataoffs, req->databuf.out,
333                        req->datalen);
334                 adjreq.dataoffs = 0;
335                 adjreq.datalen = nanddev_page_size(nand);
336                 adjreq.databuf.out = spinand->databuf;
337                 nbytes = adjreq.datalen;
338                 buf = spinand->databuf;
339         }
340
341         if (req->ooblen) {
342                 if (req->mode == MTD_OPS_AUTO_OOB)
343                         mtd_ooblayout_set_databytes(mtd, req->oobbuf.out,
344                                                     spinand->oobbuf,
345                                                     req->ooboffs,
346                                                     req->ooblen);
347                 else
348                         memcpy(spinand->oobbuf + req->ooboffs, req->oobbuf.out,
349                                req->ooblen);
350
351                 adjreq.ooblen = nanddev_per_page_oobsize(nand);
352                 adjreq.ooboffs = 0;
353                 nbytes += nanddev_per_page_oobsize(nand);
354                 if (!buf) {
355                         buf = spinand->oobbuf;
356                         column = nanddev_page_size(nand);
357                 }
358         }
359
360         spinand_cache_op_adjust_colum(spinand, &adjreq, &column);
361
362         op = *spinand->op_templates.write_cache;
363         op.addr.val = column;
364
365         /*
366          * Some controllers are limited in term of max TX data size. In this
367          * case, split the operation into one LOAD CACHE and one or more
368          * LOAD RANDOM CACHE.
369          */
370         while (nbytes) {
371                 op.data.buf.out = buf;
372                 op.data.nbytes = nbytes;
373
374                 ret = spi_mem_adjust_op_size(spinand->slave, &op);
375                 if (ret)
376                         return ret;
377
378                 ret = spi_mem_exec_op(spinand->slave, &op);
379                 if (ret)
380                         return ret;
381
382                 buf += op.data.nbytes;
383                 nbytes -= op.data.nbytes;
384                 op.addr.val += op.data.nbytes;
385
386                 /*
387                  * We need to use the RANDOM LOAD CACHE operation if there's
388                  * more than one iteration, because the LOAD operation resets
389                  * the cache to 0xff.
390                  */
391                 if (nbytes) {
392                         column = op.addr.val;
393                         op = *spinand->op_templates.update_cache;
394                         op.addr.val = column;
395                 }
396         }
397
398         return 0;
399 }
400
401 static int spinand_program_op(struct spinand_device *spinand,
402                               const struct nand_page_io_req *req)
403 {
404         struct nand_device *nand = spinand_to_nand(spinand);
405         unsigned int row = nanddev_pos_to_row(nand, &req->pos);
406         struct spi_mem_op op = SPINAND_PROG_EXEC_OP(row);
407
408         return spi_mem_exec_op(spinand->slave, &op);
409 }
410
411 static int spinand_erase_op(struct spinand_device *spinand,
412                             const struct nand_pos *pos)
413 {
414         struct nand_device *nand = &spinand->base;
415         unsigned int row = nanddev_pos_to_row(nand, pos);
416         struct spi_mem_op op = SPINAND_BLK_ERASE_OP(row);
417
418         return spi_mem_exec_op(spinand->slave, &op);
419 }
420
421 static int spinand_wait(struct spinand_device *spinand, u8 *s)
422 {
423         unsigned long start, stop;
424         u8 status;
425         int ret;
426
427         start = get_timer(0);
428         stop = 400;
429         do {
430                 ret = spinand_read_status(spinand, &status);
431                 if (ret)
432                         return ret;
433
434                 if (!(status & STATUS_BUSY))
435                         goto out;
436         } while (get_timer(start) < stop);
437
438         /*
439          * Extra read, just in case the STATUS_READY bit has changed
440          * since our last check
441          */
442         ret = spinand_read_status(spinand, &status);
443         if (ret)
444                 return ret;
445
446 out:
447         if (s)
448                 *s = status;
449
450         return status & STATUS_BUSY ? -ETIMEDOUT : 0;
451 }
452
453 static int spinand_read_id_op(struct spinand_device *spinand, u8 *buf)
454 {
455         struct spi_mem_op op = SPINAND_READID_OP(0, spinand->scratchbuf,
456                                                  SPINAND_MAX_ID_LEN);
457         int ret;
458
459         ret = spi_mem_exec_op(spinand->slave, &op);
460         if (!ret)
461                 memcpy(buf, spinand->scratchbuf, SPINAND_MAX_ID_LEN);
462
463         return ret;
464 }
465
466 static int spinand_reset_op(struct spinand_device *spinand)
467 {
468         struct spi_mem_op op = SPINAND_RESET_OP;
469         int ret;
470
471         ret = spi_mem_exec_op(spinand->slave, &op);
472         if (ret)
473                 return ret;
474
475         return spinand_wait(spinand, NULL);
476 }
477
478 static int spinand_lock_block(struct spinand_device *spinand, u8 lock)
479 {
480         return spinand_write_reg_op(spinand, REG_BLOCK_LOCK, lock);
481 }
482
483 static int spinand_check_ecc_status(struct spinand_device *spinand, u8 status)
484 {
485         struct nand_device *nand = spinand_to_nand(spinand);
486
487         if (spinand->eccinfo.get_status)
488                 return spinand->eccinfo.get_status(spinand, status);
489
490         switch (status & STATUS_ECC_MASK) {
491         case STATUS_ECC_NO_BITFLIPS:
492                 return 0;
493
494         case STATUS_ECC_HAS_BITFLIPS:
495                 /*
496                  * We have no way to know exactly how many bitflips have been
497                  * fixed, so let's return the maximum possible value so that
498                  * wear-leveling layers move the data immediately.
499                  */
500                 return nand->eccreq.strength;
501
502         case STATUS_ECC_UNCOR_ERROR:
503                 return -EBADMSG;
504
505         default:
506                 break;
507         }
508
509         return -EINVAL;
510 }
511
512 static int spinand_read_page(struct spinand_device *spinand,
513                              const struct nand_page_io_req *req,
514                              bool ecc_enabled)
515 {
516         u8 status;
517         int ret;
518
519         ret = spinand_load_page_op(spinand, req);
520         if (ret)
521                 return ret;
522
523         ret = spinand_wait(spinand, &status);
524         if (ret < 0)
525                 return ret;
526
527         ret = spinand_read_from_cache_op(spinand, req);
528         if (ret)
529                 return ret;
530
531         if (!ecc_enabled)
532                 return 0;
533
534         return spinand_check_ecc_status(spinand, status);
535 }
536
537 static int spinand_write_page(struct spinand_device *spinand,
538                               const struct nand_page_io_req *req)
539 {
540         u8 status;
541         int ret;
542
543         ret = spinand_write_enable_op(spinand);
544         if (ret)
545                 return ret;
546
547         ret = spinand_write_to_cache_op(spinand, req);
548         if (ret)
549                 return ret;
550
551         ret = spinand_program_op(spinand, req);
552         if (ret)
553                 return ret;
554
555         ret = spinand_wait(spinand, &status);
556         if (!ret && (status & STATUS_PROG_FAILED))
557                 ret = -EIO;
558
559         return ret;
560 }
561
562 static int spinand_mtd_read(struct mtd_info *mtd, loff_t from,
563                             struct mtd_oob_ops *ops)
564 {
565         struct spinand_device *spinand = mtd_to_spinand(mtd);
566         struct nand_device *nand = mtd_to_nanddev(mtd);
567         unsigned int max_bitflips = 0;
568         struct nand_io_iter iter;
569         bool enable_ecc = false;
570         bool ecc_failed = false;
571         int ret = 0;
572
573         if (ops->mode != MTD_OPS_RAW && spinand->eccinfo.ooblayout)
574                 enable_ecc = true;
575
576 #ifndef __UBOOT__
577         mutex_lock(&spinand->lock);
578 #endif
579
580         nanddev_io_for_each_page(nand, from, ops, &iter) {
581                 ret = spinand_select_target(spinand, iter.req.pos.target);
582                 if (ret)
583                         break;
584
585                 ret = spinand_ecc_enable(spinand, enable_ecc);
586                 if (ret)
587                         break;
588
589                 ret = spinand_read_page(spinand, &iter.req, enable_ecc);
590                 if (ret < 0 && ret != -EBADMSG)
591                         break;
592
593                 if (ret == -EBADMSG) {
594                         ecc_failed = true;
595                         mtd->ecc_stats.failed++;
596                         ret = 0;
597                 } else {
598                         mtd->ecc_stats.corrected += ret;
599                         max_bitflips = max_t(unsigned int, max_bitflips, ret);
600                 }
601
602                 ops->retlen += iter.req.datalen;
603                 ops->oobretlen += iter.req.ooblen;
604         }
605
606 #ifndef __UBOOT__
607         mutex_unlock(&spinand->lock);
608 #endif
609         if (ecc_failed && !ret)
610                 ret = -EBADMSG;
611
612         return ret ? ret : max_bitflips;
613 }
614
615 static int spinand_mtd_write(struct mtd_info *mtd, loff_t to,
616                              struct mtd_oob_ops *ops)
617 {
618         struct spinand_device *spinand = mtd_to_spinand(mtd);
619         struct nand_device *nand = mtd_to_nanddev(mtd);
620         struct nand_io_iter iter;
621         bool enable_ecc = false;
622         int ret = 0;
623
624         if (ops->mode != MTD_OPS_RAW && mtd->ooblayout)
625                 enable_ecc = true;
626
627 #ifndef __UBOOT__
628         mutex_lock(&spinand->lock);
629 #endif
630
631         nanddev_io_for_each_page(nand, to, ops, &iter) {
632                 ret = spinand_select_target(spinand, iter.req.pos.target);
633                 if (ret)
634                         break;
635
636                 ret = spinand_ecc_enable(spinand, enable_ecc);
637                 if (ret)
638                         break;
639
640                 ret = spinand_write_page(spinand, &iter.req);
641                 if (ret)
642                         break;
643
644                 ops->retlen += iter.req.datalen;
645                 ops->oobretlen += iter.req.ooblen;
646         }
647
648 #ifndef __UBOOT__
649         mutex_unlock(&spinand->lock);
650 #endif
651
652         return ret;
653 }
654
655 static bool spinand_isbad(struct nand_device *nand, const struct nand_pos *pos)
656 {
657         struct spinand_device *spinand = nand_to_spinand(nand);
658         u8 marker[2] = { };
659         struct nand_page_io_req req = {
660                 .pos = *pos,
661                 .ooblen = sizeof(marker),
662                 .ooboffs = 0,
663                 .oobbuf.in = marker,
664                 .mode = MTD_OPS_RAW,
665         };
666         int ret;
667
668         ret = spinand_select_target(spinand, pos->target);
669         if (ret)
670                 return ret;
671
672         ret = spinand_read_page(spinand, &req, false);
673         if (ret)
674                 return ret;
675
676         if (marker[0] != 0xff || marker[1] != 0xff)
677                 return true;
678
679         return false;
680 }
681
682 static int spinand_mtd_block_isbad(struct mtd_info *mtd, loff_t offs)
683 {
684         struct nand_device *nand = mtd_to_nanddev(mtd);
685 #ifndef __UBOOT__
686         struct spinand_device *spinand = nand_to_spinand(nand);
687 #endif
688         struct nand_pos pos;
689         int ret;
690
691         nanddev_offs_to_pos(nand, offs, &pos);
692 #ifndef __UBOOT__
693         mutex_lock(&spinand->lock);
694 #endif
695         ret = nanddev_isbad(nand, &pos);
696 #ifndef __UBOOT__
697         mutex_unlock(&spinand->lock);
698 #endif
699         return ret;
700 }
701
702 static int spinand_markbad(struct nand_device *nand, const struct nand_pos *pos)
703 {
704         struct spinand_device *spinand = nand_to_spinand(nand);
705         u8 marker[2] = { };
706         struct nand_page_io_req req = {
707                 .pos = *pos,
708                 .ooboffs = 0,
709                 .ooblen = sizeof(marker),
710                 .oobbuf.out = marker,
711                 .mode = MTD_OPS_RAW,
712         };
713         int ret;
714
715         ret = spinand_select_target(spinand, pos->target);
716         if (ret)
717                 return ret;
718
719         return spinand_write_page(spinand, &req);
720 }
721
722 static int spinand_mtd_block_markbad(struct mtd_info *mtd, loff_t offs)
723 {
724         struct nand_device *nand = mtd_to_nanddev(mtd);
725 #ifndef __UBOOT__
726         struct spinand_device *spinand = nand_to_spinand(nand);
727 #endif
728         struct nand_pos pos;
729         int ret;
730
731         nanddev_offs_to_pos(nand, offs, &pos);
732 #ifndef __UBOOT__
733         mutex_lock(&spinand->lock);
734 #endif
735         ret = nanddev_markbad(nand, &pos);
736 #ifndef __UBOOT__
737         mutex_unlock(&spinand->lock);
738 #endif
739         return ret;
740 }
741
742 static int spinand_erase(struct nand_device *nand, const struct nand_pos *pos)
743 {
744         struct spinand_device *spinand = nand_to_spinand(nand);
745         u8 status;
746         int ret;
747
748         ret = spinand_select_target(spinand, pos->target);
749         if (ret)
750                 return ret;
751
752         ret = spinand_write_enable_op(spinand);
753         if (ret)
754                 return ret;
755
756         ret = spinand_erase_op(spinand, pos);
757         if (ret)
758                 return ret;
759
760         ret = spinand_wait(spinand, &status);
761         if (!ret && (status & STATUS_ERASE_FAILED))
762                 ret = -EIO;
763
764         return ret;
765 }
766
767 static int spinand_mtd_erase(struct mtd_info *mtd,
768                              struct erase_info *einfo)
769 {
770 #ifndef __UBOOT__
771         struct spinand_device *spinand = mtd_to_spinand(mtd);
772 #endif
773         int ret;
774
775 #ifndef __UBOOT__
776         mutex_lock(&spinand->lock);
777 #endif
778         ret = nanddev_mtd_erase(mtd, einfo);
779 #ifndef __UBOOT__
780         mutex_unlock(&spinand->lock);
781 #endif
782
783         return ret;
784 }
785
786 static int spinand_mtd_block_isreserved(struct mtd_info *mtd, loff_t offs)
787 {
788 #ifndef __UBOOT__
789         struct spinand_device *spinand = mtd_to_spinand(mtd);
790 #endif
791         struct nand_device *nand = mtd_to_nanddev(mtd);
792         struct nand_pos pos;
793         int ret;
794
795         nanddev_offs_to_pos(nand, offs, &pos);
796 #ifndef __UBOOT__
797         mutex_lock(&spinand->lock);
798 #endif
799         ret = nanddev_isreserved(nand, &pos);
800 #ifndef __UBOOT__
801         mutex_unlock(&spinand->lock);
802 #endif
803
804         return ret;
805 }
806
807 const struct spi_mem_op *
808 spinand_find_supported_op(struct spinand_device *spinand,
809                           const struct spi_mem_op *ops,
810                           unsigned int nops)
811 {
812         unsigned int i;
813
814         for (i = 0; i < nops; i++) {
815                 if (spi_mem_supports_op(spinand->slave, &ops[i]))
816                         return &ops[i];
817         }
818
819         return NULL;
820 }
821
822 static const struct nand_ops spinand_ops = {
823         .erase = spinand_erase,
824         .markbad = spinand_markbad,
825         .isbad = spinand_isbad,
826 };
827
828 static const struct spinand_manufacturer *spinand_manufacturers[] = {
829         &gigadevice_spinand_manufacturer,
830         &macronix_spinand_manufacturer,
831         &micron_spinand_manufacturer,
832         &toshiba_spinand_manufacturer,
833         &winbond_spinand_manufacturer,
834 };
835
836 static int spinand_manufacturer_detect(struct spinand_device *spinand)
837 {
838         unsigned int i;
839         int ret;
840
841         for (i = 0; i < ARRAY_SIZE(spinand_manufacturers); i++) {
842                 ret = spinand_manufacturers[i]->ops->detect(spinand);
843                 if (ret > 0) {
844                         spinand->manufacturer = spinand_manufacturers[i];
845                         return 0;
846                 } else if (ret < 0) {
847                         return ret;
848                 }
849         }
850
851         return -ENOTSUPP;
852 }
853
854 static int spinand_manufacturer_init(struct spinand_device *spinand)
855 {
856         if (spinand->manufacturer->ops->init)
857                 return spinand->manufacturer->ops->init(spinand);
858
859         return 0;
860 }
861
862 static void spinand_manufacturer_cleanup(struct spinand_device *spinand)
863 {
864         /* Release manufacturer private data */
865         if (spinand->manufacturer->ops->cleanup)
866                 return spinand->manufacturer->ops->cleanup(spinand);
867 }
868
869 static const struct spi_mem_op *
870 spinand_select_op_variant(struct spinand_device *spinand,
871                           const struct spinand_op_variants *variants)
872 {
873         struct nand_device *nand = spinand_to_nand(spinand);
874         unsigned int i;
875
876         for (i = 0; i < variants->nops; i++) {
877                 struct spi_mem_op op = variants->ops[i];
878                 unsigned int nbytes;
879                 int ret;
880
881                 nbytes = nanddev_per_page_oobsize(nand) +
882                          nanddev_page_size(nand);
883
884                 while (nbytes) {
885                         op.data.nbytes = nbytes;
886                         ret = spi_mem_adjust_op_size(spinand->slave, &op);
887                         if (ret)
888                                 break;
889
890                         if (!spi_mem_supports_op(spinand->slave, &op))
891                                 break;
892
893                         nbytes -= op.data.nbytes;
894                 }
895
896                 if (!nbytes)
897                         return &variants->ops[i];
898         }
899
900         return NULL;
901 }
902
903 /**
904  * spinand_match_and_init() - Try to find a match between a device ID and an
905  *                            entry in a spinand_info table
906  * @spinand: SPI NAND object
907  * @table: SPI NAND device description table
908  * @table_size: size of the device description table
909  *
910  * Should be used by SPI NAND manufacturer drivers when they want to find a
911  * match between a device ID retrieved through the READ_ID command and an
912  * entry in the SPI NAND description table. If a match is found, the spinand
913  * object will be initialized with information provided by the matching
914  * spinand_info entry.
915  *
916  * Return: 0 on success, a negative error code otherwise.
917  */
918 int spinand_match_and_init(struct spinand_device *spinand,
919                            const struct spinand_info *table,
920                            unsigned int table_size, u8 devid)
921 {
922         struct nand_device *nand = spinand_to_nand(spinand);
923         unsigned int i;
924
925         for (i = 0; i < table_size; i++) {
926                 const struct spinand_info *info = &table[i];
927                 const struct spi_mem_op *op;
928
929                 if (devid != info->devid)
930                         continue;
931
932                 nand->memorg = table[i].memorg;
933                 nand->eccreq = table[i].eccreq;
934                 spinand->eccinfo = table[i].eccinfo;
935                 spinand->flags = table[i].flags;
936                 spinand->select_target = table[i].select_target;
937
938                 op = spinand_select_op_variant(spinand,
939                                                info->op_variants.read_cache);
940                 if (!op)
941                         return -ENOTSUPP;
942
943                 spinand->op_templates.read_cache = op;
944
945                 op = spinand_select_op_variant(spinand,
946                                                info->op_variants.write_cache);
947                 if (!op)
948                         return -ENOTSUPP;
949
950                 spinand->op_templates.write_cache = op;
951
952                 op = spinand_select_op_variant(spinand,
953                                                info->op_variants.update_cache);
954                 spinand->op_templates.update_cache = op;
955
956                 return 0;
957         }
958
959         return -ENOTSUPP;
960 }
961
962 static int spinand_detect(struct spinand_device *spinand)
963 {
964         struct nand_device *nand = spinand_to_nand(spinand);
965         int ret;
966
967         ret = spinand_reset_op(spinand);
968         if (ret)
969                 return ret;
970
971         ret = spinand_read_id_op(spinand, spinand->id.data);
972         if (ret)
973                 return ret;
974
975         spinand->id.len = SPINAND_MAX_ID_LEN;
976
977         ret = spinand_manufacturer_detect(spinand);
978         if (ret) {
979                 dev_err(spinand->slave->dev, "unknown raw ID %*phN\n",
980                         SPINAND_MAX_ID_LEN, spinand->id.data);
981                 return ret;
982         }
983
984         if (nand->memorg.ntargets > 1 && !spinand->select_target) {
985                 dev_err(spinand->slave->dev,
986                         "SPI NANDs with more than one die must implement ->select_target()\n");
987                 return -EINVAL;
988         }
989
990         dev_info(spinand->slave->dev,
991                  "%s SPI NAND was found.\n", spinand->manufacturer->name);
992         dev_info(spinand->slave->dev,
993                  "%llu MiB, block size: %zu KiB, page size: %zu, OOB size: %u\n",
994                  nanddev_size(nand) >> 20, nanddev_eraseblock_size(nand) >> 10,
995                  nanddev_page_size(nand), nanddev_per_page_oobsize(nand));
996
997         return 0;
998 }
999
1000 static int spinand_noecc_ooblayout_ecc(struct mtd_info *mtd, int section,
1001                                        struct mtd_oob_region *region)
1002 {
1003         return -ERANGE;
1004 }
1005
1006 static int spinand_noecc_ooblayout_free(struct mtd_info *mtd, int section,
1007                                         struct mtd_oob_region *region)
1008 {
1009         if (section)
1010                 return -ERANGE;
1011
1012         /* Reserve 2 bytes for the BBM. */
1013         region->offset = 2;
1014         region->length = 62;
1015
1016         return 0;
1017 }
1018
1019 static const struct mtd_ooblayout_ops spinand_noecc_ooblayout = {
1020         .ecc = spinand_noecc_ooblayout_ecc,
1021         .rfree = spinand_noecc_ooblayout_free,
1022 };
1023
1024 static int spinand_init(struct spinand_device *spinand)
1025 {
1026         struct mtd_info *mtd = spinand_to_mtd(spinand);
1027         struct nand_device *nand = mtd_to_nanddev(mtd);
1028         int ret, i;
1029
1030         /*
1031          * We need a scratch buffer because the spi_mem interface requires that
1032          * buf passed in spi_mem_op->data.buf be DMA-able.
1033          */
1034         spinand->scratchbuf = kzalloc(SPINAND_MAX_ID_LEN, GFP_KERNEL);
1035         if (!spinand->scratchbuf)
1036                 return -ENOMEM;
1037
1038         ret = spinand_detect(spinand);
1039         if (ret)
1040                 goto err_free_bufs;
1041
1042         /*
1043          * Use kzalloc() instead of devm_kzalloc() here, because some drivers
1044          * may use this buffer for DMA access.
1045          * Memory allocated by devm_ does not guarantee DMA-safe alignment.
1046          */
1047         spinand->databuf = kzalloc(nanddev_page_size(nand) +
1048                                nanddev_per_page_oobsize(nand),
1049                                GFP_KERNEL);
1050         if (!spinand->databuf) {
1051                 ret = -ENOMEM;
1052                 goto err_free_bufs;
1053         }
1054
1055         spinand->oobbuf = spinand->databuf + nanddev_page_size(nand);
1056
1057         ret = spinand_init_cfg_cache(spinand);
1058         if (ret)
1059                 goto err_free_bufs;
1060
1061         ret = spinand_init_quad_enable(spinand);
1062         if (ret)
1063                 goto err_free_bufs;
1064
1065         ret = spinand_upd_cfg(spinand, CFG_OTP_ENABLE, 0);
1066         if (ret)
1067                 goto err_free_bufs;
1068
1069         ret = spinand_manufacturer_init(spinand);
1070         if (ret) {
1071                 dev_err(spinand->slave->dev,
1072                         "Failed to initialize the SPI NAND chip (err = %d)\n",
1073                         ret);
1074                 goto err_free_bufs;
1075         }
1076
1077         /* After power up, all blocks are locked, so unlock them here. */
1078         for (i = 0; i < nand->memorg.ntargets; i++) {
1079                 ret = spinand_select_target(spinand, i);
1080                 if (ret)
1081                         goto err_free_bufs;
1082
1083                 ret = spinand_lock_block(spinand, BL_ALL_UNLOCKED);
1084                 if (ret)
1085                         goto err_free_bufs;
1086         }
1087
1088         ret = nanddev_init(nand, &spinand_ops, THIS_MODULE);
1089         if (ret)
1090                 goto err_manuf_cleanup;
1091
1092         /*
1093          * Right now, we don't support ECC, so let the whole oob
1094          * area is available for user.
1095          */
1096         mtd->_read_oob = spinand_mtd_read;
1097         mtd->_write_oob = spinand_mtd_write;
1098         mtd->_block_isbad = spinand_mtd_block_isbad;
1099         mtd->_block_markbad = spinand_mtd_block_markbad;
1100         mtd->_block_isreserved = spinand_mtd_block_isreserved;
1101         mtd->_erase = spinand_mtd_erase;
1102
1103         if (spinand->eccinfo.ooblayout)
1104                 mtd_set_ooblayout(mtd, spinand->eccinfo.ooblayout);
1105         else
1106                 mtd_set_ooblayout(mtd, &spinand_noecc_ooblayout);
1107
1108         ret = mtd_ooblayout_count_freebytes(mtd);
1109         if (ret < 0)
1110                 goto err_cleanup_nanddev;
1111
1112         mtd->oobavail = ret;
1113
1114         return 0;
1115
1116 err_cleanup_nanddev:
1117         nanddev_cleanup(nand);
1118
1119 err_manuf_cleanup:
1120         spinand_manufacturer_cleanup(spinand);
1121
1122 err_free_bufs:
1123         kfree(spinand->databuf);
1124         kfree(spinand->scratchbuf);
1125         return ret;
1126 }
1127
1128 static void spinand_cleanup(struct spinand_device *spinand)
1129 {
1130         struct nand_device *nand = spinand_to_nand(spinand);
1131
1132         nanddev_cleanup(nand);
1133         spinand_manufacturer_cleanup(spinand);
1134         kfree(spinand->databuf);
1135         kfree(spinand->scratchbuf);
1136 }
1137
1138 static int spinand_probe(struct udevice *dev)
1139 {
1140         struct spinand_device *spinand = dev_get_priv(dev);
1141         struct spi_slave *slave = dev_get_parent_priv(dev);
1142         struct mtd_info *mtd = dev_get_uclass_priv(dev);
1143         struct nand_device *nand = spinand_to_nand(spinand);
1144         int ret;
1145
1146 #ifndef __UBOOT__
1147         spinand = devm_kzalloc(&mem->spi->dev, sizeof(*spinand),
1148                                GFP_KERNEL);
1149         if (!spinand)
1150                 return -ENOMEM;
1151
1152         spinand->spimem = mem;
1153         spi_mem_set_drvdata(mem, spinand);
1154         spinand_set_of_node(spinand, mem->spi->dev.of_node);
1155         mutex_init(&spinand->lock);
1156
1157         mtd = spinand_to_mtd(spinand);
1158         mtd->dev.parent = &mem->spi->dev;
1159 #else
1160         nand->mtd = mtd;
1161         mtd->priv = nand;
1162         mtd->dev = dev;
1163         mtd->name = malloc(20);
1164         if (!mtd->name)
1165                 return -ENOMEM;
1166         sprintf(mtd->name, "spi-nand%d", spi_nand_idx++);
1167         spinand->slave = slave;
1168         spinand_set_of_node(spinand, dev->node.np);
1169 #endif
1170
1171         ret = spinand_init(spinand);
1172         if (ret)
1173                 return ret;
1174
1175 #ifndef __UBOOT__
1176         ret = mtd_device_register(mtd, NULL, 0);
1177 #else
1178         ret = add_mtd_device(mtd);
1179 #endif
1180         if (ret)
1181                 goto err_spinand_cleanup;
1182
1183         return 0;
1184
1185 err_spinand_cleanup:
1186         spinand_cleanup(spinand);
1187
1188         return ret;
1189 }
1190
1191 #ifndef __UBOOT__
1192 static int spinand_remove(struct udevice *slave)
1193 {
1194         struct spinand_device *spinand;
1195         struct mtd_info *mtd;
1196         int ret;
1197
1198         spinand = spi_mem_get_drvdata(slave);
1199         mtd = spinand_to_mtd(spinand);
1200         free(mtd->name);
1201
1202         ret = mtd_device_unregister(mtd);
1203         if (ret)
1204                 return ret;
1205
1206         spinand_cleanup(spinand);
1207
1208         return 0;
1209 }
1210
1211 static const struct spi_device_id spinand_ids[] = {
1212         { .name = "spi-nand" },
1213         { /* sentinel */ },
1214 };
1215
1216 #ifdef CONFIG_OF
1217 static const struct of_device_id spinand_of_ids[] = {
1218         { .compatible = "spi-nand" },
1219         { /* sentinel */ },
1220 };
1221 #endif
1222
1223 static struct spi_mem_driver spinand_drv = {
1224         .spidrv = {
1225                 .id_table = spinand_ids,
1226                 .driver = {
1227                         .name = "spi-nand",
1228                         .of_match_table = of_match_ptr(spinand_of_ids),
1229                 },
1230         },
1231         .probe = spinand_probe,
1232         .remove = spinand_remove,
1233 };
1234 module_spi_mem_driver(spinand_drv);
1235
1236 MODULE_DESCRIPTION("SPI NAND framework");
1237 MODULE_AUTHOR("Peter Pan<peterpandong@micron.com>");
1238 MODULE_LICENSE("GPL v2");
1239 #endif /* __UBOOT__ */
1240
1241 static const struct udevice_id spinand_ids[] = {
1242         { .compatible = "spi-nand" },
1243         { /* sentinel */ },
1244 };
1245
1246 U_BOOT_DRIVER(spinand) = {
1247         .name = "spi_nand",
1248         .id = UCLASS_MTD,
1249         .of_match = spinand_ids,
1250         .priv_auto      = sizeof(struct spinand_device),
1251         .probe = spinand_probe,
1252 };