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