dm: treewide: Rename auto_alloc_size members to be shorter
[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         struct nand_page_io_req req = {
659                 .pos = *pos,
660                 .ooblen = 2,
661                 .ooboffs = 0,
662                 .oobbuf.in = spinand->oobbuf,
663                 .mode = MTD_OPS_RAW,
664         };
665         int ret;
666
667         memset(spinand->oobbuf, 0, 2);
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 (spinand->oobbuf[0] != 0xff || spinand->oobbuf[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         struct nand_page_io_req req = {
706                 .pos = *pos,
707                 .ooboffs = 0,
708                 .ooblen = 2,
709                 .oobbuf.out = spinand->oobbuf,
710         };
711         int ret;
712
713         /* Erase block before marking it bad. */
714         ret = spinand_select_target(spinand, pos->target);
715         if (ret)
716                 return ret;
717
718         ret = spinand_write_enable_op(spinand);
719         if (ret)
720                 return ret;
721
722         ret = spinand_erase_op(spinand, pos);
723         if (ret)
724                 return ret;
725
726         memset(spinand->oobbuf, 0, 2);
727         return spinand_write_page(spinand, &req);
728 }
729
730 static int spinand_mtd_block_markbad(struct mtd_info *mtd, loff_t offs)
731 {
732         struct nand_device *nand = mtd_to_nanddev(mtd);
733 #ifndef __UBOOT__
734         struct spinand_device *spinand = nand_to_spinand(nand);
735 #endif
736         struct nand_pos pos;
737         int ret;
738
739         nanddev_offs_to_pos(nand, offs, &pos);
740 #ifndef __UBOOT__
741         mutex_lock(&spinand->lock);
742 #endif
743         ret = nanddev_markbad(nand, &pos);
744 #ifndef __UBOOT__
745         mutex_unlock(&spinand->lock);
746 #endif
747         return ret;
748 }
749
750 static int spinand_erase(struct nand_device *nand, const struct nand_pos *pos)
751 {
752         struct spinand_device *spinand = nand_to_spinand(nand);
753         u8 status;
754         int ret;
755
756         ret = spinand_select_target(spinand, pos->target);
757         if (ret)
758                 return ret;
759
760         ret = spinand_write_enable_op(spinand);
761         if (ret)
762                 return ret;
763
764         ret = spinand_erase_op(spinand, pos);
765         if (ret)
766                 return ret;
767
768         ret = spinand_wait(spinand, &status);
769         if (!ret && (status & STATUS_ERASE_FAILED))
770                 ret = -EIO;
771
772         return ret;
773 }
774
775 static int spinand_mtd_erase(struct mtd_info *mtd,
776                              struct erase_info *einfo)
777 {
778 #ifndef __UBOOT__
779         struct spinand_device *spinand = mtd_to_spinand(mtd);
780 #endif
781         int ret;
782
783 #ifndef __UBOOT__
784         mutex_lock(&spinand->lock);
785 #endif
786         ret = nanddev_mtd_erase(mtd, einfo);
787 #ifndef __UBOOT__
788         mutex_unlock(&spinand->lock);
789 #endif
790
791         return ret;
792 }
793
794 static int spinand_mtd_block_isreserved(struct mtd_info *mtd, loff_t offs)
795 {
796 #ifndef __UBOOT__
797         struct spinand_device *spinand = mtd_to_spinand(mtd);
798 #endif
799         struct nand_device *nand = mtd_to_nanddev(mtd);
800         struct nand_pos pos;
801         int ret;
802
803         nanddev_offs_to_pos(nand, offs, &pos);
804 #ifndef __UBOOT__
805         mutex_lock(&spinand->lock);
806 #endif
807         ret = nanddev_isreserved(nand, &pos);
808 #ifndef __UBOOT__
809         mutex_unlock(&spinand->lock);
810 #endif
811
812         return ret;
813 }
814
815 const struct spi_mem_op *
816 spinand_find_supported_op(struct spinand_device *spinand,
817                           const struct spi_mem_op *ops,
818                           unsigned int nops)
819 {
820         unsigned int i;
821
822         for (i = 0; i < nops; i++) {
823                 if (spi_mem_supports_op(spinand->slave, &ops[i]))
824                         return &ops[i];
825         }
826
827         return NULL;
828 }
829
830 static const struct nand_ops spinand_ops = {
831         .erase = spinand_erase,
832         .markbad = spinand_markbad,
833         .isbad = spinand_isbad,
834 };
835
836 static const struct spinand_manufacturer *spinand_manufacturers[] = {
837         &gigadevice_spinand_manufacturer,
838         &macronix_spinand_manufacturer,
839         &micron_spinand_manufacturer,
840         &toshiba_spinand_manufacturer,
841         &winbond_spinand_manufacturer,
842 };
843
844 static int spinand_manufacturer_detect(struct spinand_device *spinand)
845 {
846         unsigned int i;
847         int ret;
848
849         for (i = 0; i < ARRAY_SIZE(spinand_manufacturers); i++) {
850                 ret = spinand_manufacturers[i]->ops->detect(spinand);
851                 if (ret > 0) {
852                         spinand->manufacturer = spinand_manufacturers[i];
853                         return 0;
854                 } else if (ret < 0) {
855                         return ret;
856                 }
857         }
858
859         return -ENOTSUPP;
860 }
861
862 static int spinand_manufacturer_init(struct spinand_device *spinand)
863 {
864         if (spinand->manufacturer->ops->init)
865                 return spinand->manufacturer->ops->init(spinand);
866
867         return 0;
868 }
869
870 static void spinand_manufacturer_cleanup(struct spinand_device *spinand)
871 {
872         /* Release manufacturer private data */
873         if (spinand->manufacturer->ops->cleanup)
874                 return spinand->manufacturer->ops->cleanup(spinand);
875 }
876
877 static const struct spi_mem_op *
878 spinand_select_op_variant(struct spinand_device *spinand,
879                           const struct spinand_op_variants *variants)
880 {
881         struct nand_device *nand = spinand_to_nand(spinand);
882         unsigned int i;
883
884         for (i = 0; i < variants->nops; i++) {
885                 struct spi_mem_op op = variants->ops[i];
886                 unsigned int nbytes;
887                 int ret;
888
889                 nbytes = nanddev_per_page_oobsize(nand) +
890                          nanddev_page_size(nand);
891
892                 while (nbytes) {
893                         op.data.nbytes = nbytes;
894                         ret = spi_mem_adjust_op_size(spinand->slave, &op);
895                         if (ret)
896                                 break;
897
898                         if (!spi_mem_supports_op(spinand->slave, &op))
899                                 break;
900
901                         nbytes -= op.data.nbytes;
902                 }
903
904                 if (!nbytes)
905                         return &variants->ops[i];
906         }
907
908         return NULL;
909 }
910
911 /**
912  * spinand_match_and_init() - Try to find a match between a device ID and an
913  *                            entry in a spinand_info table
914  * @spinand: SPI NAND object
915  * @table: SPI NAND device description table
916  * @table_size: size of the device description table
917  *
918  * Should be used by SPI NAND manufacturer drivers when they want to find a
919  * match between a device ID retrieved through the READ_ID command and an
920  * entry in the SPI NAND description table. If a match is found, the spinand
921  * object will be initialized with information provided by the matching
922  * spinand_info entry.
923  *
924  * Return: 0 on success, a negative error code otherwise.
925  */
926 int spinand_match_and_init(struct spinand_device *spinand,
927                            const struct spinand_info *table,
928                            unsigned int table_size, u8 devid)
929 {
930         struct nand_device *nand = spinand_to_nand(spinand);
931         unsigned int i;
932
933         for (i = 0; i < table_size; i++) {
934                 const struct spinand_info *info = &table[i];
935                 const struct spi_mem_op *op;
936
937                 if (devid != info->devid)
938                         continue;
939
940                 nand->memorg = table[i].memorg;
941                 nand->eccreq = table[i].eccreq;
942                 spinand->eccinfo = table[i].eccinfo;
943                 spinand->flags = table[i].flags;
944                 spinand->select_target = table[i].select_target;
945
946                 op = spinand_select_op_variant(spinand,
947                                                info->op_variants.read_cache);
948                 if (!op)
949                         return -ENOTSUPP;
950
951                 spinand->op_templates.read_cache = op;
952
953                 op = spinand_select_op_variant(spinand,
954                                                info->op_variants.write_cache);
955                 if (!op)
956                         return -ENOTSUPP;
957
958                 spinand->op_templates.write_cache = op;
959
960                 op = spinand_select_op_variant(spinand,
961                                                info->op_variants.update_cache);
962                 spinand->op_templates.update_cache = op;
963
964                 return 0;
965         }
966
967         return -ENOTSUPP;
968 }
969
970 static int spinand_detect(struct spinand_device *spinand)
971 {
972         struct nand_device *nand = spinand_to_nand(spinand);
973         int ret;
974
975         ret = spinand_reset_op(spinand);
976         if (ret)
977                 return ret;
978
979         ret = spinand_read_id_op(spinand, spinand->id.data);
980         if (ret)
981                 return ret;
982
983         spinand->id.len = SPINAND_MAX_ID_LEN;
984
985         ret = spinand_manufacturer_detect(spinand);
986         if (ret) {
987                 dev_err(spinand->slave->dev, "unknown raw ID %*phN\n",
988                         SPINAND_MAX_ID_LEN, spinand->id.data);
989                 return ret;
990         }
991
992         if (nand->memorg.ntargets > 1 && !spinand->select_target) {
993                 dev_err(spinand->slave->dev,
994                         "SPI NANDs with more than one die must implement ->select_target()\n");
995                 return -EINVAL;
996         }
997
998         dev_info(spinand->slave->dev,
999                  "%s SPI NAND was found.\n", spinand->manufacturer->name);
1000         dev_info(spinand->slave->dev,
1001                  "%llu MiB, block size: %zu KiB, page size: %zu, OOB size: %u\n",
1002                  nanddev_size(nand) >> 20, nanddev_eraseblock_size(nand) >> 10,
1003                  nanddev_page_size(nand), nanddev_per_page_oobsize(nand));
1004
1005         return 0;
1006 }
1007
1008 static int spinand_noecc_ooblayout_ecc(struct mtd_info *mtd, int section,
1009                                        struct mtd_oob_region *region)
1010 {
1011         return -ERANGE;
1012 }
1013
1014 static int spinand_noecc_ooblayout_free(struct mtd_info *mtd, int section,
1015                                         struct mtd_oob_region *region)
1016 {
1017         if (section)
1018                 return -ERANGE;
1019
1020         /* Reserve 2 bytes for the BBM. */
1021         region->offset = 2;
1022         region->length = 62;
1023
1024         return 0;
1025 }
1026
1027 static const struct mtd_ooblayout_ops spinand_noecc_ooblayout = {
1028         .ecc = spinand_noecc_ooblayout_ecc,
1029         .rfree = spinand_noecc_ooblayout_free,
1030 };
1031
1032 static int spinand_init(struct spinand_device *spinand)
1033 {
1034         struct mtd_info *mtd = spinand_to_mtd(spinand);
1035         struct nand_device *nand = mtd_to_nanddev(mtd);
1036         int ret, i;
1037
1038         /*
1039          * We need a scratch buffer because the spi_mem interface requires that
1040          * buf passed in spi_mem_op->data.buf be DMA-able.
1041          */
1042         spinand->scratchbuf = kzalloc(SPINAND_MAX_ID_LEN, GFP_KERNEL);
1043         if (!spinand->scratchbuf)
1044                 return -ENOMEM;
1045
1046         ret = spinand_detect(spinand);
1047         if (ret)
1048                 goto err_free_bufs;
1049
1050         /*
1051          * Use kzalloc() instead of devm_kzalloc() here, because some drivers
1052          * may use this buffer for DMA access.
1053          * Memory allocated by devm_ does not guarantee DMA-safe alignment.
1054          */
1055         spinand->databuf = kzalloc(nanddev_page_size(nand) +
1056                                nanddev_per_page_oobsize(nand),
1057                                GFP_KERNEL);
1058         if (!spinand->databuf) {
1059                 ret = -ENOMEM;
1060                 goto err_free_bufs;
1061         }
1062
1063         spinand->oobbuf = spinand->databuf + nanddev_page_size(nand);
1064
1065         ret = spinand_init_cfg_cache(spinand);
1066         if (ret)
1067                 goto err_free_bufs;
1068
1069         ret = spinand_init_quad_enable(spinand);
1070         if (ret)
1071                 goto err_free_bufs;
1072
1073         ret = spinand_upd_cfg(spinand, CFG_OTP_ENABLE, 0);
1074         if (ret)
1075                 goto err_free_bufs;
1076
1077         ret = spinand_manufacturer_init(spinand);
1078         if (ret) {
1079                 dev_err(spinand->slave->dev,
1080                         "Failed to initialize the SPI NAND chip (err = %d)\n",
1081                         ret);
1082                 goto err_free_bufs;
1083         }
1084
1085         /* After power up, all blocks are locked, so unlock them here. */
1086         for (i = 0; i < nand->memorg.ntargets; i++) {
1087                 ret = spinand_select_target(spinand, i);
1088                 if (ret)
1089                         goto err_free_bufs;
1090
1091                 ret = spinand_lock_block(spinand, BL_ALL_UNLOCKED);
1092                 if (ret)
1093                         goto err_free_bufs;
1094         }
1095
1096         ret = nanddev_init(nand, &spinand_ops, THIS_MODULE);
1097         if (ret)
1098                 goto err_manuf_cleanup;
1099
1100         /*
1101          * Right now, we don't support ECC, so let the whole oob
1102          * area is available for user.
1103          */
1104         mtd->_read_oob = spinand_mtd_read;
1105         mtd->_write_oob = spinand_mtd_write;
1106         mtd->_block_isbad = spinand_mtd_block_isbad;
1107         mtd->_block_markbad = spinand_mtd_block_markbad;
1108         mtd->_block_isreserved = spinand_mtd_block_isreserved;
1109         mtd->_erase = spinand_mtd_erase;
1110
1111         if (spinand->eccinfo.ooblayout)
1112                 mtd_set_ooblayout(mtd, spinand->eccinfo.ooblayout);
1113         else
1114                 mtd_set_ooblayout(mtd, &spinand_noecc_ooblayout);
1115
1116         ret = mtd_ooblayout_count_freebytes(mtd);
1117         if (ret < 0)
1118                 goto err_cleanup_nanddev;
1119
1120         mtd->oobavail = ret;
1121
1122         return 0;
1123
1124 err_cleanup_nanddev:
1125         nanddev_cleanup(nand);
1126
1127 err_manuf_cleanup:
1128         spinand_manufacturer_cleanup(spinand);
1129
1130 err_free_bufs:
1131         kfree(spinand->databuf);
1132         kfree(spinand->scratchbuf);
1133         return ret;
1134 }
1135
1136 static void spinand_cleanup(struct spinand_device *spinand)
1137 {
1138         struct nand_device *nand = spinand_to_nand(spinand);
1139
1140         nanddev_cleanup(nand);
1141         spinand_manufacturer_cleanup(spinand);
1142         kfree(spinand->databuf);
1143         kfree(spinand->scratchbuf);
1144 }
1145
1146 static int spinand_probe(struct udevice *dev)
1147 {
1148         struct spinand_device *spinand = dev_get_priv(dev);
1149         struct spi_slave *slave = dev_get_parent_priv(dev);
1150         struct mtd_info *mtd = dev_get_uclass_priv(dev);
1151         struct nand_device *nand = spinand_to_nand(spinand);
1152         int ret;
1153
1154 #ifndef __UBOOT__
1155         spinand = devm_kzalloc(&mem->spi->dev, sizeof(*spinand),
1156                                GFP_KERNEL);
1157         if (!spinand)
1158                 return -ENOMEM;
1159
1160         spinand->spimem = mem;
1161         spi_mem_set_drvdata(mem, spinand);
1162         spinand_set_of_node(spinand, mem->spi->dev.of_node);
1163         mutex_init(&spinand->lock);
1164
1165         mtd = spinand_to_mtd(spinand);
1166         mtd->dev.parent = &mem->spi->dev;
1167 #else
1168         nand->mtd = mtd;
1169         mtd->priv = nand;
1170         mtd->dev = dev;
1171         mtd->name = malloc(20);
1172         if (!mtd->name)
1173                 return -ENOMEM;
1174         sprintf(mtd->name, "spi-nand%d", spi_nand_idx++);
1175         spinand->slave = slave;
1176         spinand_set_of_node(spinand, dev->node.np);
1177 #endif
1178
1179         ret = spinand_init(spinand);
1180         if (ret)
1181                 return ret;
1182
1183 #ifndef __UBOOT__
1184         ret = mtd_device_register(mtd, NULL, 0);
1185 #else
1186         ret = add_mtd_device(mtd);
1187 #endif
1188         if (ret)
1189                 goto err_spinand_cleanup;
1190
1191         return 0;
1192
1193 err_spinand_cleanup:
1194         spinand_cleanup(spinand);
1195
1196         return ret;
1197 }
1198
1199 #ifndef __UBOOT__
1200 static int spinand_remove(struct udevice *slave)
1201 {
1202         struct spinand_device *spinand;
1203         struct mtd_info *mtd;
1204         int ret;
1205
1206         spinand = spi_mem_get_drvdata(slave);
1207         mtd = spinand_to_mtd(spinand);
1208         free(mtd->name);
1209
1210         ret = mtd_device_unregister(mtd);
1211         if (ret)
1212                 return ret;
1213
1214         spinand_cleanup(spinand);
1215
1216         return 0;
1217 }
1218
1219 static const struct spi_device_id spinand_ids[] = {
1220         { .name = "spi-nand" },
1221         { /* sentinel */ },
1222 };
1223
1224 #ifdef CONFIG_OF
1225 static const struct of_device_id spinand_of_ids[] = {
1226         { .compatible = "spi-nand" },
1227         { /* sentinel */ },
1228 };
1229 #endif
1230
1231 static struct spi_mem_driver spinand_drv = {
1232         .spidrv = {
1233                 .id_table = spinand_ids,
1234                 .driver = {
1235                         .name = "spi-nand",
1236                         .of_match_table = of_match_ptr(spinand_of_ids),
1237                 },
1238         },
1239         .probe = spinand_probe,
1240         .remove = spinand_remove,
1241 };
1242 module_spi_mem_driver(spinand_drv);
1243
1244 MODULE_DESCRIPTION("SPI NAND framework");
1245 MODULE_AUTHOR("Peter Pan<peterpandong@micron.com>");
1246 MODULE_LICENSE("GPL v2");
1247 #endif /* __UBOOT__ */
1248
1249 static const struct udevice_id spinand_ids[] = {
1250         { .compatible = "spi-nand" },
1251         { /* sentinel */ },
1252 };
1253
1254 U_BOOT_DRIVER(spinand) = {
1255         .name = "spi_nand",
1256         .id = UCLASS_MTD,
1257         .of_match = spinand_ids,
1258         .priv_auto      = sizeof(struct spinand_device),
1259         .probe = spinand_probe,
1260 };