Merge https://gitlab.denx.de/u-boot/custodians/u-boot-spi
[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         &toshiba_spinand_manufacturer,
839         &winbond_spinand_manufacturer,
840 };
841
842 static int spinand_manufacturer_detect(struct spinand_device *spinand)
843 {
844         unsigned int i;
845         int ret;
846
847         for (i = 0; i < ARRAY_SIZE(spinand_manufacturers); i++) {
848                 ret = spinand_manufacturers[i]->ops->detect(spinand);
849                 if (ret > 0) {
850                         spinand->manufacturer = spinand_manufacturers[i];
851                         return 0;
852                 } else if (ret < 0) {
853                         return ret;
854                 }
855         }
856
857         return -ENOTSUPP;
858 }
859
860 static int spinand_manufacturer_init(struct spinand_device *spinand)
861 {
862         if (spinand->manufacturer->ops->init)
863                 return spinand->manufacturer->ops->init(spinand);
864
865         return 0;
866 }
867
868 static void spinand_manufacturer_cleanup(struct spinand_device *spinand)
869 {
870         /* Release manufacturer private data */
871         if (spinand->manufacturer->ops->cleanup)
872                 return spinand->manufacturer->ops->cleanup(spinand);
873 }
874
875 static const struct spi_mem_op *
876 spinand_select_op_variant(struct spinand_device *spinand,
877                           const struct spinand_op_variants *variants)
878 {
879         struct nand_device *nand = spinand_to_nand(spinand);
880         unsigned int i;
881
882         for (i = 0; i < variants->nops; i++) {
883                 struct spi_mem_op op = variants->ops[i];
884                 unsigned int nbytes;
885                 int ret;
886
887                 nbytes = nanddev_per_page_oobsize(nand) +
888                          nanddev_page_size(nand);
889
890                 while (nbytes) {
891                         op.data.nbytes = nbytes;
892                         ret = spi_mem_adjust_op_size(spinand->slave, &op);
893                         if (ret)
894                                 break;
895
896                         if (!spi_mem_supports_op(spinand->slave, &op))
897                                 break;
898
899                         nbytes -= op.data.nbytes;
900                 }
901
902                 if (!nbytes)
903                         return &variants->ops[i];
904         }
905
906         return NULL;
907 }
908
909 /**
910  * spinand_match_and_init() - Try to find a match between a device ID and an
911  *                            entry in a spinand_info table
912  * @spinand: SPI NAND object
913  * @table: SPI NAND device description table
914  * @table_size: size of the device description table
915  *
916  * Should be used by SPI NAND manufacturer drivers when they want to find a
917  * match between a device ID retrieved through the READ_ID command and an
918  * entry in the SPI NAND description table. If a match is found, the spinand
919  * object will be initialized with information provided by the matching
920  * spinand_info entry.
921  *
922  * Return: 0 on success, a negative error code otherwise.
923  */
924 int spinand_match_and_init(struct spinand_device *spinand,
925                            const struct spinand_info *table,
926                            unsigned int table_size, u8 devid)
927 {
928         struct nand_device *nand = spinand_to_nand(spinand);
929         unsigned int i;
930
931         for (i = 0; i < table_size; i++) {
932                 const struct spinand_info *info = &table[i];
933                 const struct spi_mem_op *op;
934
935                 if (devid != info->devid)
936                         continue;
937
938                 nand->memorg = table[i].memorg;
939                 nand->eccreq = table[i].eccreq;
940                 spinand->eccinfo = table[i].eccinfo;
941                 spinand->flags = table[i].flags;
942                 spinand->select_target = table[i].select_target;
943
944                 op = spinand_select_op_variant(spinand,
945                                                info->op_variants.read_cache);
946                 if (!op)
947                         return -ENOTSUPP;
948
949                 spinand->op_templates.read_cache = op;
950
951                 op = spinand_select_op_variant(spinand,
952                                                info->op_variants.write_cache);
953                 if (!op)
954                         return -ENOTSUPP;
955
956                 spinand->op_templates.write_cache = op;
957
958                 op = spinand_select_op_variant(spinand,
959                                                info->op_variants.update_cache);
960                 spinand->op_templates.update_cache = op;
961
962                 return 0;
963         }
964
965         return -ENOTSUPP;
966 }
967
968 static int spinand_detect(struct spinand_device *spinand)
969 {
970         struct nand_device *nand = spinand_to_nand(spinand);
971         int ret;
972
973         ret = spinand_reset_op(spinand);
974         if (ret)
975                 return ret;
976
977         ret = spinand_read_id_op(spinand, spinand->id.data);
978         if (ret)
979                 return ret;
980
981         spinand->id.len = SPINAND_MAX_ID_LEN;
982
983         ret = spinand_manufacturer_detect(spinand);
984         if (ret) {
985                 dev_err(dev, "unknown raw ID %*phN\n", SPINAND_MAX_ID_LEN,
986                         spinand->id.data);
987                 return ret;
988         }
989
990         if (nand->memorg.ntargets > 1 && !spinand->select_target) {
991                 dev_err(dev,
992                         "SPI NANDs with more than one die must implement ->select_target()\n");
993                 return -EINVAL;
994         }
995
996         dev_info(spinand->slave->dev,
997                  "%s SPI NAND was found.\n", spinand->manufacturer->name);
998         dev_info(spinand->slave->dev,
999                  "%llu MiB, block size: %zu KiB, page size: %zu, OOB size: %u\n",
1000                  nanddev_size(nand) >> 20, nanddev_eraseblock_size(nand) >> 10,
1001                  nanddev_page_size(nand), nanddev_per_page_oobsize(nand));
1002
1003         return 0;
1004 }
1005
1006 static int spinand_noecc_ooblayout_ecc(struct mtd_info *mtd, int section,
1007                                        struct mtd_oob_region *region)
1008 {
1009         return -ERANGE;
1010 }
1011
1012 static int spinand_noecc_ooblayout_free(struct mtd_info *mtd, int section,
1013                                         struct mtd_oob_region *region)
1014 {
1015         if (section)
1016                 return -ERANGE;
1017
1018         /* Reserve 2 bytes for the BBM. */
1019         region->offset = 2;
1020         region->length = 62;
1021
1022         return 0;
1023 }
1024
1025 static const struct mtd_ooblayout_ops spinand_noecc_ooblayout = {
1026         .ecc = spinand_noecc_ooblayout_ecc,
1027         .rfree = spinand_noecc_ooblayout_free,
1028 };
1029
1030 static int spinand_init(struct spinand_device *spinand)
1031 {
1032         struct mtd_info *mtd = spinand_to_mtd(spinand);
1033         struct nand_device *nand = mtd_to_nanddev(mtd);
1034         int ret, i;
1035
1036         /*
1037          * We need a scratch buffer because the spi_mem interface requires that
1038          * buf passed in spi_mem_op->data.buf be DMA-able.
1039          */
1040         spinand->scratchbuf = kzalloc(SPINAND_MAX_ID_LEN, GFP_KERNEL);
1041         if (!spinand->scratchbuf)
1042                 return -ENOMEM;
1043
1044         ret = spinand_detect(spinand);
1045         if (ret)
1046                 goto err_free_bufs;
1047
1048         /*
1049          * Use kzalloc() instead of devm_kzalloc() here, because some drivers
1050          * may use this buffer for DMA access.
1051          * Memory allocated by devm_ does not guarantee DMA-safe alignment.
1052          */
1053         spinand->databuf = kzalloc(nanddev_page_size(nand) +
1054                                nanddev_per_page_oobsize(nand),
1055                                GFP_KERNEL);
1056         if (!spinand->databuf) {
1057                 ret = -ENOMEM;
1058                 goto err_free_bufs;
1059         }
1060
1061         spinand->oobbuf = spinand->databuf + nanddev_page_size(nand);
1062
1063         ret = spinand_init_cfg_cache(spinand);
1064         if (ret)
1065                 goto err_free_bufs;
1066
1067         ret = spinand_init_quad_enable(spinand);
1068         if (ret)
1069                 goto err_free_bufs;
1070
1071         ret = spinand_upd_cfg(spinand, CFG_OTP_ENABLE, 0);
1072         if (ret)
1073                 goto err_free_bufs;
1074
1075         ret = spinand_manufacturer_init(spinand);
1076         if (ret) {
1077                 dev_err(dev,
1078                         "Failed to initialize the SPI NAND chip (err = %d)\n",
1079                         ret);
1080                 goto err_free_bufs;
1081         }
1082
1083         /* After power up, all blocks are locked, so unlock them here. */
1084         for (i = 0; i < nand->memorg.ntargets; i++) {
1085                 ret = spinand_select_target(spinand, i);
1086                 if (ret)
1087                         goto err_free_bufs;
1088
1089                 ret = spinand_lock_block(spinand, BL_ALL_UNLOCKED);
1090                 if (ret)
1091                         goto err_free_bufs;
1092         }
1093
1094         ret = nanddev_init(nand, &spinand_ops, THIS_MODULE);
1095         if (ret)
1096                 goto err_manuf_cleanup;
1097
1098         /*
1099          * Right now, we don't support ECC, so let the whole oob
1100          * area is available for user.
1101          */
1102         mtd->_read_oob = spinand_mtd_read;
1103         mtd->_write_oob = spinand_mtd_write;
1104         mtd->_block_isbad = spinand_mtd_block_isbad;
1105         mtd->_block_markbad = spinand_mtd_block_markbad;
1106         mtd->_block_isreserved = spinand_mtd_block_isreserved;
1107         mtd->_erase = spinand_mtd_erase;
1108
1109         if (spinand->eccinfo.ooblayout)
1110                 mtd_set_ooblayout(mtd, spinand->eccinfo.ooblayout);
1111         else
1112                 mtd_set_ooblayout(mtd, &spinand_noecc_ooblayout);
1113
1114         ret = mtd_ooblayout_count_freebytes(mtd);
1115         if (ret < 0)
1116                 goto err_cleanup_nanddev;
1117
1118         mtd->oobavail = ret;
1119
1120         return 0;
1121
1122 err_cleanup_nanddev:
1123         nanddev_cleanup(nand);
1124
1125 err_manuf_cleanup:
1126         spinand_manufacturer_cleanup(spinand);
1127
1128 err_free_bufs:
1129         kfree(spinand->databuf);
1130         kfree(spinand->scratchbuf);
1131         return ret;
1132 }
1133
1134 static void spinand_cleanup(struct spinand_device *spinand)
1135 {
1136         struct nand_device *nand = spinand_to_nand(spinand);
1137
1138         nanddev_cleanup(nand);
1139         spinand_manufacturer_cleanup(spinand);
1140         kfree(spinand->databuf);
1141         kfree(spinand->scratchbuf);
1142 }
1143
1144 static int spinand_probe(struct udevice *dev)
1145 {
1146         struct spinand_device *spinand = dev_get_priv(dev);
1147         struct spi_slave *slave = dev_get_parent_priv(dev);
1148         struct mtd_info *mtd = dev_get_uclass_priv(dev);
1149         struct nand_device *nand = spinand_to_nand(spinand);
1150         int ret;
1151
1152 #ifndef __UBOOT__
1153         spinand = devm_kzalloc(&mem->spi->dev, sizeof(*spinand),
1154                                GFP_KERNEL);
1155         if (!spinand)
1156                 return -ENOMEM;
1157
1158         spinand->spimem = mem;
1159         spi_mem_set_drvdata(mem, spinand);
1160         spinand_set_of_node(spinand, mem->spi->dev.of_node);
1161         mutex_init(&spinand->lock);
1162
1163         mtd = spinand_to_mtd(spinand);
1164         mtd->dev.parent = &mem->spi->dev;
1165 #else
1166         nand->mtd = mtd;
1167         mtd->priv = nand;
1168         mtd->dev = dev;
1169         mtd->name = malloc(20);
1170         if (!mtd->name)
1171                 return -ENOMEM;
1172         sprintf(mtd->name, "spi-nand%d", spi_nand_idx++);
1173         spinand->slave = slave;
1174         spinand_set_of_node(spinand, dev->node.np);
1175 #endif
1176
1177         ret = spinand_init(spinand);
1178         if (ret)
1179                 return ret;
1180
1181 #ifndef __UBOOT__
1182         ret = mtd_device_register(mtd, NULL, 0);
1183 #else
1184         ret = add_mtd_device(mtd);
1185 #endif
1186         if (ret)
1187                 goto err_spinand_cleanup;
1188
1189         return 0;
1190
1191 err_spinand_cleanup:
1192         spinand_cleanup(spinand);
1193
1194         return ret;
1195 }
1196
1197 #ifndef __UBOOT__
1198 static int spinand_remove(struct udevice *slave)
1199 {
1200         struct spinand_device *spinand;
1201         struct mtd_info *mtd;
1202         int ret;
1203
1204         spinand = spi_mem_get_drvdata(slave);
1205         mtd = spinand_to_mtd(spinand);
1206         free(mtd->name);
1207
1208         ret = mtd_device_unregister(mtd);
1209         if (ret)
1210                 return ret;
1211
1212         spinand_cleanup(spinand);
1213
1214         return 0;
1215 }
1216
1217 static const struct spi_device_id spinand_ids[] = {
1218         { .name = "spi-nand" },
1219         { /* sentinel */ },
1220 };
1221
1222 #ifdef CONFIG_OF
1223 static const struct of_device_id spinand_of_ids[] = {
1224         { .compatible = "spi-nand" },
1225         { /* sentinel */ },
1226 };
1227 #endif
1228
1229 static struct spi_mem_driver spinand_drv = {
1230         .spidrv = {
1231                 .id_table = spinand_ids,
1232                 .driver = {
1233                         .name = "spi-nand",
1234                         .of_match_table = of_match_ptr(spinand_of_ids),
1235                 },
1236         },
1237         .probe = spinand_probe,
1238         .remove = spinand_remove,
1239 };
1240 module_spi_mem_driver(spinand_drv);
1241
1242 MODULE_DESCRIPTION("SPI NAND framework");
1243 MODULE_AUTHOR("Peter Pan<peterpandong@micron.com>");
1244 MODULE_LICENSE("GPL v2");
1245 #endif /* __UBOOT__ */
1246
1247 static const struct udevice_id spinand_ids[] = {
1248         { .compatible = "spi-nand" },
1249         { /* sentinel */ },
1250 };
1251
1252 U_BOOT_DRIVER(spinand) = {
1253         .name = "spi_nand",
1254         .id = UCLASS_MTD,
1255         .of_match = spinand_ids,
1256         .priv_auto_alloc_size = sizeof(struct spinand_device),
1257         .probe = spinand_probe,
1258 };