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