Merge tag 'nand/for-5.19' into mtd/next
[linux-2.6-microblaze.git] / drivers / mtd / nand / ecc-mtk.c
1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /*
3  * MTK ECC controller driver.
4  * Copyright (C) 2016  MediaTek Inc.
5  * Authors:     Xiaolei Li              <xiaolei.li@mediatek.com>
6  *              Jorge Ramirez-Ortiz     <jorge.ramirez-ortiz@linaro.org>
7  */
8
9 #include <linux/platform_device.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/interrupt.h>
12 #include <linux/clk.h>
13 #include <linux/module.h>
14 #include <linux/iopoll.h>
15 #include <linux/of.h>
16 #include <linux/of_platform.h>
17 #include <linux/mutex.h>
18 #include <linux/mtd/nand-ecc-mtk.h>
19
20 #define ECC_IDLE_MASK           BIT(0)
21 #define ECC_IRQ_EN              BIT(0)
22 #define ECC_PG_IRQ_SEL          BIT(1)
23 #define ECC_OP_ENABLE           (1)
24 #define ECC_OP_DISABLE          (0)
25
26 #define ECC_ENCCON              (0x00)
27 #define ECC_ENCCNFG             (0x04)
28 #define         ECC_MS_SHIFT            (16)
29 #define ECC_ENCDIADDR           (0x08)
30 #define ECC_ENCIDLE             (0x0C)
31 #define ECC_DECCON              (0x100)
32 #define ECC_DECCNFG             (0x104)
33 #define         DEC_EMPTY_EN            BIT(31)
34 #define         DEC_CNFG_CORRECT        (0x3 << 12)
35 #define ECC_DECIDLE             (0x10C)
36 #define ECC_DECENUM0            (0x114)
37
38 #define ECC_TIMEOUT             (500000)
39
40 #define ECC_IDLE_REG(op)        ((op) == ECC_ENCODE ? ECC_ENCIDLE : ECC_DECIDLE)
41 #define ECC_CTL_REG(op)         ((op) == ECC_ENCODE ? ECC_ENCCON : ECC_DECCON)
42
43 struct mtk_ecc_caps {
44         u32 err_mask;
45         const u8 *ecc_strength;
46         const u32 *ecc_regs;
47         u8 num_ecc_strength;
48         u8 ecc_mode_shift;
49         u32 parity_bits;
50         int pg_irq_sel;
51 };
52
53 struct mtk_ecc {
54         struct device *dev;
55         const struct mtk_ecc_caps *caps;
56         void __iomem *regs;
57         struct clk *clk;
58
59         struct completion done;
60         struct mutex lock;
61         u32 sectors;
62
63         u8 *eccdata;
64 };
65
66 /* ecc strength that each IP supports */
67 static const u8 ecc_strength_mt2701[] = {
68         4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 28, 32, 36,
69         40, 44, 48, 52, 56, 60
70 };
71
72 static const u8 ecc_strength_mt2712[] = {
73         4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 28, 32, 36,
74         40, 44, 48, 52, 56, 60, 68, 72, 80
75 };
76
77 static const u8 ecc_strength_mt7622[] = {
78         4, 6, 8, 10, 12, 14, 16
79 };
80
81 enum mtk_ecc_regs {
82         ECC_ENCPAR00,
83         ECC_ENCIRQ_EN,
84         ECC_ENCIRQ_STA,
85         ECC_DECDONE,
86         ECC_DECIRQ_EN,
87         ECC_DECIRQ_STA,
88 };
89
90 static int mt2701_ecc_regs[] = {
91         [ECC_ENCPAR00] =        0x10,
92         [ECC_ENCIRQ_EN] =       0x80,
93         [ECC_ENCIRQ_STA] =      0x84,
94         [ECC_DECDONE] =         0x124,
95         [ECC_DECIRQ_EN] =       0x200,
96         [ECC_DECIRQ_STA] =      0x204,
97 };
98
99 static int mt2712_ecc_regs[] = {
100         [ECC_ENCPAR00] =        0x300,
101         [ECC_ENCIRQ_EN] =       0x80,
102         [ECC_ENCIRQ_STA] =      0x84,
103         [ECC_DECDONE] =         0x124,
104         [ECC_DECIRQ_EN] =       0x200,
105         [ECC_DECIRQ_STA] =      0x204,
106 };
107
108 static int mt7622_ecc_regs[] = {
109         [ECC_ENCPAR00] =        0x10,
110         [ECC_ENCIRQ_EN] =       0x30,
111         [ECC_ENCIRQ_STA] =      0x34,
112         [ECC_DECDONE] =         0x11c,
113         [ECC_DECIRQ_EN] =       0x140,
114         [ECC_DECIRQ_STA] =      0x144,
115 };
116
117 static inline void mtk_ecc_wait_idle(struct mtk_ecc *ecc,
118                                      enum mtk_ecc_operation op)
119 {
120         struct device *dev = ecc->dev;
121         u32 val;
122         int ret;
123
124         ret = readl_poll_timeout_atomic(ecc->regs + ECC_IDLE_REG(op), val,
125                                         val & ECC_IDLE_MASK,
126                                         10, ECC_TIMEOUT);
127         if (ret)
128                 dev_warn(dev, "%s NOT idle\n",
129                          op == ECC_ENCODE ? "encoder" : "decoder");
130 }
131
132 static irqreturn_t mtk_ecc_irq(int irq, void *id)
133 {
134         struct mtk_ecc *ecc = id;
135         u32 dec, enc;
136
137         dec = readw(ecc->regs + ecc->caps->ecc_regs[ECC_DECIRQ_STA])
138                     & ECC_IRQ_EN;
139         if (dec) {
140                 dec = readw(ecc->regs + ecc->caps->ecc_regs[ECC_DECDONE]);
141                 if (dec & ecc->sectors) {
142                         /*
143                          * Clear decode IRQ status once again to ensure that
144                          * there will be no extra IRQ.
145                          */
146                         readw(ecc->regs + ecc->caps->ecc_regs[ECC_DECIRQ_STA]);
147                         ecc->sectors = 0;
148                         complete(&ecc->done);
149                 } else {
150                         return IRQ_HANDLED;
151                 }
152         } else {
153                 enc = readl(ecc->regs + ecc->caps->ecc_regs[ECC_ENCIRQ_STA])
154                       & ECC_IRQ_EN;
155                 if (enc)
156                         complete(&ecc->done);
157                 else
158                         return IRQ_NONE;
159         }
160
161         return IRQ_HANDLED;
162 }
163
164 static int mtk_ecc_config(struct mtk_ecc *ecc, struct mtk_ecc_config *config)
165 {
166         u32 ecc_bit, dec_sz, enc_sz;
167         u32 reg, i;
168
169         for (i = 0; i < ecc->caps->num_ecc_strength; i++) {
170                 if (ecc->caps->ecc_strength[i] == config->strength)
171                         break;
172         }
173
174         if (i == ecc->caps->num_ecc_strength) {
175                 dev_err(ecc->dev, "invalid ecc strength %d\n",
176                         config->strength);
177                 return -EINVAL;
178         }
179
180         ecc_bit = i;
181
182         if (config->op == ECC_ENCODE) {
183                 /* configure ECC encoder (in bits) */
184                 enc_sz = config->len << 3;
185
186                 reg = ecc_bit | (config->mode << ecc->caps->ecc_mode_shift);
187                 reg |= (enc_sz << ECC_MS_SHIFT);
188                 writel(reg, ecc->regs + ECC_ENCCNFG);
189
190                 if (config->mode != ECC_NFI_MODE)
191                         writel(lower_32_bits(config->addr),
192                                ecc->regs + ECC_ENCDIADDR);
193
194         } else {
195                 /* configure ECC decoder (in bits) */
196                 dec_sz = (config->len << 3) +
197                          config->strength * ecc->caps->parity_bits;
198
199                 reg = ecc_bit | (config->mode << ecc->caps->ecc_mode_shift);
200                 reg |= (dec_sz << ECC_MS_SHIFT) | DEC_CNFG_CORRECT;
201                 reg |= DEC_EMPTY_EN;
202                 writel(reg, ecc->regs + ECC_DECCNFG);
203
204                 if (config->sectors)
205                         ecc->sectors = 1 << (config->sectors - 1);
206         }
207
208         return 0;
209 }
210
211 void mtk_ecc_get_stats(struct mtk_ecc *ecc, struct mtk_ecc_stats *stats,
212                        int sectors)
213 {
214         u32 offset, i, err;
215         u32 bitflips = 0;
216
217         stats->corrected = 0;
218         stats->failed = 0;
219
220         for (i = 0; i < sectors; i++) {
221                 offset = (i >> 2) << 2;
222                 err = readl(ecc->regs + ECC_DECENUM0 + offset);
223                 err = err >> ((i % 4) * 8);
224                 err &= ecc->caps->err_mask;
225                 if (err == ecc->caps->err_mask) {
226                         /* uncorrectable errors */
227                         stats->failed++;
228                         continue;
229                 }
230
231                 stats->corrected += err;
232                 bitflips = max_t(u32, bitflips, err);
233         }
234
235         stats->bitflips = bitflips;
236 }
237 EXPORT_SYMBOL(mtk_ecc_get_stats);
238
239 void mtk_ecc_release(struct mtk_ecc *ecc)
240 {
241         clk_disable_unprepare(ecc->clk);
242         put_device(ecc->dev);
243 }
244 EXPORT_SYMBOL(mtk_ecc_release);
245
246 static void mtk_ecc_hw_init(struct mtk_ecc *ecc)
247 {
248         mtk_ecc_wait_idle(ecc, ECC_ENCODE);
249         writew(ECC_OP_DISABLE, ecc->regs + ECC_ENCCON);
250
251         mtk_ecc_wait_idle(ecc, ECC_DECODE);
252         writel(ECC_OP_DISABLE, ecc->regs + ECC_DECCON);
253 }
254
255 static struct mtk_ecc *mtk_ecc_get(struct device_node *np)
256 {
257         struct platform_device *pdev;
258         struct mtk_ecc *ecc;
259
260         pdev = of_find_device_by_node(np);
261         if (!pdev)
262                 return ERR_PTR(-EPROBE_DEFER);
263
264         ecc = platform_get_drvdata(pdev);
265         if (!ecc) {
266                 put_device(&pdev->dev);
267                 return ERR_PTR(-EPROBE_DEFER);
268         }
269
270         clk_prepare_enable(ecc->clk);
271         mtk_ecc_hw_init(ecc);
272
273         return ecc;
274 }
275
276 struct mtk_ecc *of_mtk_ecc_get(struct device_node *of_node)
277 {
278         struct mtk_ecc *ecc = NULL;
279         struct device_node *np;
280
281         np = of_parse_phandle(of_node, "nand-ecc-engine", 0);
282         /* for backward compatibility */
283         if (!np)
284                 np = of_parse_phandle(of_node, "ecc-engine", 0);
285         if (np) {
286                 ecc = mtk_ecc_get(np);
287                 of_node_put(np);
288         }
289
290         return ecc;
291 }
292 EXPORT_SYMBOL(of_mtk_ecc_get);
293
294 int mtk_ecc_enable(struct mtk_ecc *ecc, struct mtk_ecc_config *config)
295 {
296         enum mtk_ecc_operation op = config->op;
297         u16 reg_val;
298         int ret;
299
300         ret = mutex_lock_interruptible(&ecc->lock);
301         if (ret) {
302                 dev_err(ecc->dev, "interrupted when attempting to lock\n");
303                 return ret;
304         }
305
306         mtk_ecc_wait_idle(ecc, op);
307
308         ret = mtk_ecc_config(ecc, config);
309         if (ret) {
310                 mutex_unlock(&ecc->lock);
311                 return ret;
312         }
313
314         if (config->mode != ECC_NFI_MODE || op != ECC_ENCODE) {
315                 init_completion(&ecc->done);
316                 reg_val = ECC_IRQ_EN;
317                 /*
318                  * For ECC_NFI_MODE, if ecc->caps->pg_irq_sel is 1, then it
319                  * means this chip can only generate one ecc irq during page
320                  * read / write. If is 0, generate one ecc irq each ecc step.
321                  */
322                 if (ecc->caps->pg_irq_sel && config->mode == ECC_NFI_MODE)
323                         reg_val |= ECC_PG_IRQ_SEL;
324                 if (op == ECC_ENCODE)
325                         writew(reg_val, ecc->regs +
326                                ecc->caps->ecc_regs[ECC_ENCIRQ_EN]);
327                 else
328                         writew(reg_val, ecc->regs +
329                                ecc->caps->ecc_regs[ECC_DECIRQ_EN]);
330         }
331
332         writew(ECC_OP_ENABLE, ecc->regs + ECC_CTL_REG(op));
333
334         return 0;
335 }
336 EXPORT_SYMBOL(mtk_ecc_enable);
337
338 void mtk_ecc_disable(struct mtk_ecc *ecc)
339 {
340         enum mtk_ecc_operation op = ECC_ENCODE;
341
342         /* find out the running operation */
343         if (readw(ecc->regs + ECC_CTL_REG(op)) != ECC_OP_ENABLE)
344                 op = ECC_DECODE;
345
346         /* disable it */
347         mtk_ecc_wait_idle(ecc, op);
348         if (op == ECC_DECODE) {
349                 /*
350                  * Clear decode IRQ status in case there is a timeout to wait
351                  * decode IRQ.
352                  */
353                 readw(ecc->regs + ecc->caps->ecc_regs[ECC_DECDONE]);
354                 writew(0, ecc->regs + ecc->caps->ecc_regs[ECC_DECIRQ_EN]);
355         } else {
356                 writew(0, ecc->regs + ecc->caps->ecc_regs[ECC_ENCIRQ_EN]);
357         }
358
359         writew(ECC_OP_DISABLE, ecc->regs + ECC_CTL_REG(op));
360
361         mutex_unlock(&ecc->lock);
362 }
363 EXPORT_SYMBOL(mtk_ecc_disable);
364
365 int mtk_ecc_wait_done(struct mtk_ecc *ecc, enum mtk_ecc_operation op)
366 {
367         int ret;
368
369         ret = wait_for_completion_timeout(&ecc->done, msecs_to_jiffies(500));
370         if (!ret) {
371                 dev_err(ecc->dev, "%s timeout - interrupt did not arrive)\n",
372                         (op == ECC_ENCODE) ? "encoder" : "decoder");
373                 return -ETIMEDOUT;
374         }
375
376         return 0;
377 }
378 EXPORT_SYMBOL(mtk_ecc_wait_done);
379
380 int mtk_ecc_encode(struct mtk_ecc *ecc, struct mtk_ecc_config *config,
381                    u8 *data, u32 bytes)
382 {
383         dma_addr_t addr;
384         u32 len;
385         int ret;
386
387         addr = dma_map_single(ecc->dev, data, bytes, DMA_TO_DEVICE);
388         ret = dma_mapping_error(ecc->dev, addr);
389         if (ret) {
390                 dev_err(ecc->dev, "dma mapping error\n");
391                 return -EINVAL;
392         }
393
394         config->op = ECC_ENCODE;
395         config->addr = addr;
396         ret = mtk_ecc_enable(ecc, config);
397         if (ret) {
398                 dma_unmap_single(ecc->dev, addr, bytes, DMA_TO_DEVICE);
399                 return ret;
400         }
401
402         ret = mtk_ecc_wait_done(ecc, ECC_ENCODE);
403         if (ret)
404                 goto timeout;
405
406         mtk_ecc_wait_idle(ecc, ECC_ENCODE);
407
408         /* Program ECC bytes to OOB: per sector oob = FDM + ECC + SPARE */
409         len = (config->strength * ecc->caps->parity_bits + 7) >> 3;
410
411         /* write the parity bytes generated by the ECC back to temp buffer */
412         __ioread32_copy(ecc->eccdata,
413                         ecc->regs + ecc->caps->ecc_regs[ECC_ENCPAR00],
414                         round_up(len, 4));
415
416         /* copy into possibly unaligned OOB region with actual length */
417         memcpy(data + bytes, ecc->eccdata, len);
418 timeout:
419
420         dma_unmap_single(ecc->dev, addr, bytes, DMA_TO_DEVICE);
421         mtk_ecc_disable(ecc);
422
423         return ret;
424 }
425 EXPORT_SYMBOL(mtk_ecc_encode);
426
427 void mtk_ecc_adjust_strength(struct mtk_ecc *ecc, u32 *p)
428 {
429         const u8 *ecc_strength = ecc->caps->ecc_strength;
430         int i;
431
432         for (i = 0; i < ecc->caps->num_ecc_strength; i++) {
433                 if (*p <= ecc_strength[i]) {
434                         if (!i)
435                                 *p = ecc_strength[i];
436                         else if (*p != ecc_strength[i])
437                                 *p = ecc_strength[i - 1];
438                         return;
439                 }
440         }
441
442         *p = ecc_strength[ecc->caps->num_ecc_strength - 1];
443 }
444 EXPORT_SYMBOL(mtk_ecc_adjust_strength);
445
446 unsigned int mtk_ecc_get_parity_bits(struct mtk_ecc *ecc)
447 {
448         return ecc->caps->parity_bits;
449 }
450 EXPORT_SYMBOL(mtk_ecc_get_parity_bits);
451
452 static const struct mtk_ecc_caps mtk_ecc_caps_mt2701 = {
453         .err_mask = 0x3f,
454         .ecc_strength = ecc_strength_mt2701,
455         .ecc_regs = mt2701_ecc_regs,
456         .num_ecc_strength = 20,
457         .ecc_mode_shift = 5,
458         .parity_bits = 14,
459         .pg_irq_sel = 0,
460 };
461
462 static const struct mtk_ecc_caps mtk_ecc_caps_mt2712 = {
463         .err_mask = 0x7f,
464         .ecc_strength = ecc_strength_mt2712,
465         .ecc_regs = mt2712_ecc_regs,
466         .num_ecc_strength = 23,
467         .ecc_mode_shift = 5,
468         .parity_bits = 14,
469         .pg_irq_sel = 1,
470 };
471
472 static const struct mtk_ecc_caps mtk_ecc_caps_mt7622 = {
473         .err_mask = 0x3f,
474         .ecc_strength = ecc_strength_mt7622,
475         .ecc_regs = mt7622_ecc_regs,
476         .num_ecc_strength = 7,
477         .ecc_mode_shift = 4,
478         .parity_bits = 13,
479         .pg_irq_sel = 0,
480 };
481
482 static const struct of_device_id mtk_ecc_dt_match[] = {
483         {
484                 .compatible = "mediatek,mt2701-ecc",
485                 .data = &mtk_ecc_caps_mt2701,
486         }, {
487                 .compatible = "mediatek,mt2712-ecc",
488                 .data = &mtk_ecc_caps_mt2712,
489         }, {
490                 .compatible = "mediatek,mt7622-ecc",
491                 .data = &mtk_ecc_caps_mt7622,
492         },
493         {},
494 };
495
496 static int mtk_ecc_probe(struct platform_device *pdev)
497 {
498         struct device *dev = &pdev->dev;
499         struct mtk_ecc *ecc;
500         u32 max_eccdata_size;
501         int irq, ret;
502
503         ecc = devm_kzalloc(dev, sizeof(*ecc), GFP_KERNEL);
504         if (!ecc)
505                 return -ENOMEM;
506
507         ecc->caps = of_device_get_match_data(dev);
508
509         max_eccdata_size = ecc->caps->num_ecc_strength - 1;
510         max_eccdata_size = ecc->caps->ecc_strength[max_eccdata_size];
511         max_eccdata_size = (max_eccdata_size * ecc->caps->parity_bits + 7) >> 3;
512         max_eccdata_size = round_up(max_eccdata_size, 4);
513         ecc->eccdata = devm_kzalloc(dev, max_eccdata_size, GFP_KERNEL);
514         if (!ecc->eccdata)
515                 return -ENOMEM;
516
517         ecc->regs = devm_platform_ioremap_resource(pdev, 0);
518         if (IS_ERR(ecc->regs))
519                 return PTR_ERR(ecc->regs);
520
521         ecc->clk = devm_clk_get(dev, NULL);
522         if (IS_ERR(ecc->clk)) {
523                 dev_err(dev, "failed to get clock: %ld\n", PTR_ERR(ecc->clk));
524                 return PTR_ERR(ecc->clk);
525         }
526
527         irq = platform_get_irq(pdev, 0);
528         if (irq < 0)
529                 return irq;
530
531         ret = dma_set_mask(dev, DMA_BIT_MASK(32));
532         if (ret) {
533                 dev_err(dev, "failed to set DMA mask\n");
534                 return ret;
535         }
536
537         ret = devm_request_irq(dev, irq, mtk_ecc_irq, 0x0, "mtk-ecc", ecc);
538         if (ret) {
539                 dev_err(dev, "failed to request irq\n");
540                 return -EINVAL;
541         }
542
543         ecc->dev = dev;
544         mutex_init(&ecc->lock);
545         platform_set_drvdata(pdev, ecc);
546         dev_info(dev, "probed\n");
547
548         return 0;
549 }
550
551 #ifdef CONFIG_PM_SLEEP
552 static int mtk_ecc_suspend(struct device *dev)
553 {
554         struct mtk_ecc *ecc = dev_get_drvdata(dev);
555
556         clk_disable_unprepare(ecc->clk);
557
558         return 0;
559 }
560
561 static int mtk_ecc_resume(struct device *dev)
562 {
563         struct mtk_ecc *ecc = dev_get_drvdata(dev);
564         int ret;
565
566         ret = clk_prepare_enable(ecc->clk);
567         if (ret) {
568                 dev_err(dev, "failed to enable clk\n");
569                 return ret;
570         }
571
572         return 0;
573 }
574
575 static SIMPLE_DEV_PM_OPS(mtk_ecc_pm_ops, mtk_ecc_suspend, mtk_ecc_resume);
576 #endif
577
578 MODULE_DEVICE_TABLE(of, mtk_ecc_dt_match);
579
580 static struct platform_driver mtk_ecc_driver = {
581         .probe  = mtk_ecc_probe,
582         .driver = {
583                 .name  = "mtk-ecc",
584                 .of_match_table = mtk_ecc_dt_match,
585 #ifdef CONFIG_PM_SLEEP
586                 .pm = &mtk_ecc_pm_ops,
587 #endif
588         },
589 };
590
591 module_platform_driver(mtk_ecc_driver);
592
593 MODULE_AUTHOR("Xiaolei Li <xiaolei.li@mediatek.com>");
594 MODULE_DESCRIPTION("MTK Nand ECC Driver");
595 MODULE_LICENSE("Dual MIT/GPL");