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