1 // SPDX-License-Identifier: GPL-2.0 OR MIT
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>
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>
16 #include <linux/of_platform.h>
17 #include <linux/mutex.h>
18 #include <linux/mtd/nand-ecc-mtk.h>
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)
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)
38 #define ECC_TIMEOUT (500000)
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)
43 #define ECC_ERRMASK_MT7622 GENMASK(4, 0)
44 #define ECC_ERRMASK_MT2701 GENMASK(5, 0)
45 #define ECC_ERRMASK_MT2712 GENMASK(6, 0)
50 const u8
*ecc_strength
;
60 const struct mtk_ecc_caps
*caps
;
64 struct completion done
;
71 /* ecc strength that each IP supports */
72 static const u8 ecc_strength_mt2701
[] = {
73 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 28, 32, 36,
74 40, 44, 48, 52, 56, 60
77 static const u8 ecc_strength_mt2712
[] = {
78 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 28, 32, 36,
79 40, 44, 48, 52, 56, 60, 68, 72, 80
82 static const u8 ecc_strength_mt7622
[] = {
86 static const u8 ecc_strength_mt7986
[] = {
87 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24
99 static int mt2701_ecc_regs
[] = {
100 [ECC_ENCPAR00
] = 0x10,
101 [ECC_ENCIRQ_EN
] = 0x80,
102 [ECC_ENCIRQ_STA
] = 0x84,
103 [ECC_DECDONE
] = 0x124,
104 [ECC_DECIRQ_EN
] = 0x200,
105 [ECC_DECIRQ_STA
] = 0x204,
108 static int mt2712_ecc_regs
[] = {
109 [ECC_ENCPAR00
] = 0x300,
110 [ECC_ENCIRQ_EN
] = 0x80,
111 [ECC_ENCIRQ_STA
] = 0x84,
112 [ECC_DECDONE
] = 0x124,
113 [ECC_DECIRQ_EN
] = 0x200,
114 [ECC_DECIRQ_STA
] = 0x204,
117 static int mt7622_ecc_regs
[] = {
118 [ECC_ENCPAR00
] = 0x10,
119 [ECC_ENCIRQ_EN
] = 0x30,
120 [ECC_ENCIRQ_STA
] = 0x34,
121 [ECC_DECDONE
] = 0x11c,
122 [ECC_DECIRQ_EN
] = 0x140,
123 [ECC_DECIRQ_STA
] = 0x144,
126 static inline void mtk_ecc_wait_idle(struct mtk_ecc
*ecc
,
127 enum mtk_ecc_operation op
)
129 struct device
*dev
= ecc
->dev
;
133 ret
= readl_poll_timeout_atomic(ecc
->regs
+ ECC_IDLE_REG(op
), val
,
137 dev_warn(dev
, "%s NOT idle\n",
138 op
== ECC_ENCODE
? "encoder" : "decoder");
141 static irqreturn_t
mtk_ecc_irq(int irq
, void *id
)
143 struct mtk_ecc
*ecc
= id
;
146 dec
= readw(ecc
->regs
+ ecc
->caps
->ecc_regs
[ECC_DECIRQ_STA
])
149 dec
= readw(ecc
->regs
+ ecc
->caps
->ecc_regs
[ECC_DECDONE
]);
150 if (dec
& ecc
->sectors
) {
152 * Clear decode IRQ status once again to ensure that
153 * there will be no extra IRQ.
155 readw(ecc
->regs
+ ecc
->caps
->ecc_regs
[ECC_DECIRQ_STA
]);
157 complete(&ecc
->done
);
162 enc
= readl(ecc
->regs
+ ecc
->caps
->ecc_regs
[ECC_ENCIRQ_STA
])
165 complete(&ecc
->done
);
173 static int mtk_ecc_config(struct mtk_ecc
*ecc
, struct mtk_ecc_config
*config
)
175 u32 ecc_bit
, dec_sz
, enc_sz
;
178 for (i
= 0; i
< ecc
->caps
->num_ecc_strength
; i
++) {
179 if (ecc
->caps
->ecc_strength
[i
] == config
->strength
)
183 if (i
== ecc
->caps
->num_ecc_strength
) {
184 dev_err(ecc
->dev
, "invalid ecc strength %d\n",
191 if (config
->op
== ECC_ENCODE
) {
192 /* configure ECC encoder (in bits) */
193 enc_sz
= config
->len
<< 3;
195 reg
= ecc_bit
| (config
->mode
<< ecc
->caps
->ecc_mode_shift
);
196 reg
|= (enc_sz
<< ECC_MS_SHIFT
);
197 writel(reg
, ecc
->regs
+ ECC_ENCCNFG
);
199 if (config
->mode
!= ECC_NFI_MODE
)
200 writel(lower_32_bits(config
->addr
),
201 ecc
->regs
+ ECC_ENCDIADDR
);
204 /* configure ECC decoder (in bits) */
205 dec_sz
= (config
->len
<< 3) +
206 config
->strength
* ecc
->caps
->parity_bits
;
208 reg
= ecc_bit
| (config
->mode
<< ecc
->caps
->ecc_mode_shift
);
209 reg
|= (dec_sz
<< ECC_MS_SHIFT
) | DEC_CNFG_CORRECT
;
211 writel(reg
, ecc
->regs
+ ECC_DECCNFG
);
214 ecc
->sectors
= 1 << (config
->sectors
- 1);
220 void mtk_ecc_get_stats(struct mtk_ecc
*ecc
, struct mtk_ecc_stats
*stats
,
226 stats
->corrected
= 0;
229 for (i
= 0; i
< sectors
; i
++) {
230 offset
= (i
>> 2) << 2;
231 err
= readl(ecc
->regs
+ ECC_DECENUM0
+ offset
);
232 err
= err
>> ((i
% 4) * ecc
->caps
->err_shift
);
233 err
&= ecc
->caps
->err_mask
;
234 if (err
== ecc
->caps
->err_mask
) {
235 /* uncorrectable errors */
240 stats
->corrected
+= err
;
241 bitflips
= max_t(u32
, bitflips
, err
);
244 stats
->bitflips
= bitflips
;
246 EXPORT_SYMBOL(mtk_ecc_get_stats
);
248 void mtk_ecc_release(struct mtk_ecc
*ecc
)
250 clk_disable_unprepare(ecc
->clk
);
251 put_device(ecc
->dev
);
253 EXPORT_SYMBOL(mtk_ecc_release
);
255 static void mtk_ecc_hw_init(struct mtk_ecc
*ecc
)
257 mtk_ecc_wait_idle(ecc
, ECC_ENCODE
);
258 writew(ECC_OP_DISABLE
, ecc
->regs
+ ECC_ENCCON
);
260 mtk_ecc_wait_idle(ecc
, ECC_DECODE
);
261 writel(ECC_OP_DISABLE
, ecc
->regs
+ ECC_DECCON
);
264 static struct mtk_ecc
*mtk_ecc_get(struct device_node
*np
)
266 struct platform_device
*pdev
;
269 pdev
= of_find_device_by_node(np
);
271 return ERR_PTR(-EPROBE_DEFER
);
273 ecc
= platform_get_drvdata(pdev
);
275 put_device(&pdev
->dev
);
276 return ERR_PTR(-EPROBE_DEFER
);
279 clk_prepare_enable(ecc
->clk
);
280 mtk_ecc_hw_init(ecc
);
285 struct mtk_ecc
*of_mtk_ecc_get(struct device_node
*of_node
)
287 struct mtk_ecc
*ecc
= NULL
;
288 struct device_node
*np
;
290 np
= of_parse_phandle(of_node
, "nand-ecc-engine", 0);
291 /* for backward compatibility */
293 np
= of_parse_phandle(of_node
, "ecc-engine", 0);
295 ecc
= mtk_ecc_get(np
);
301 EXPORT_SYMBOL(of_mtk_ecc_get
);
303 int mtk_ecc_enable(struct mtk_ecc
*ecc
, struct mtk_ecc_config
*config
)
305 enum mtk_ecc_operation op
= config
->op
;
309 ret
= mutex_lock_interruptible(&ecc
->lock
);
311 dev_err(ecc
->dev
, "interrupted when attempting to lock\n");
315 mtk_ecc_wait_idle(ecc
, op
);
317 ret
= mtk_ecc_config(ecc
, config
);
319 mutex_unlock(&ecc
->lock
);
323 if (config
->mode
!= ECC_NFI_MODE
|| op
!= ECC_ENCODE
) {
324 init_completion(&ecc
->done
);
325 reg_val
= ECC_IRQ_EN
;
327 * For ECC_NFI_MODE, if ecc->caps->pg_irq_sel is 1, then it
328 * means this chip can only generate one ecc irq during page
329 * read / write. If is 0, generate one ecc irq each ecc step.
331 if (ecc
->caps
->pg_irq_sel
&& config
->mode
== ECC_NFI_MODE
)
332 reg_val
|= ECC_PG_IRQ_SEL
;
333 if (op
== ECC_ENCODE
)
334 writew(reg_val
, ecc
->regs
+
335 ecc
->caps
->ecc_regs
[ECC_ENCIRQ_EN
]);
337 writew(reg_val
, ecc
->regs
+
338 ecc
->caps
->ecc_regs
[ECC_DECIRQ_EN
]);
341 writew(ECC_OP_ENABLE
, ecc
->regs
+ ECC_CTL_REG(op
));
345 EXPORT_SYMBOL(mtk_ecc_enable
);
347 void mtk_ecc_disable(struct mtk_ecc
*ecc
)
349 enum mtk_ecc_operation op
= ECC_ENCODE
;
351 /* find out the running operation */
352 if (readw(ecc
->regs
+ ECC_CTL_REG(op
)) != ECC_OP_ENABLE
)
356 mtk_ecc_wait_idle(ecc
, op
);
357 if (op
== ECC_DECODE
) {
359 * Clear decode IRQ status in case there is a timeout to wait
362 readw(ecc
->regs
+ ecc
->caps
->ecc_regs
[ECC_DECDONE
]);
363 writew(0, ecc
->regs
+ ecc
->caps
->ecc_regs
[ECC_DECIRQ_EN
]);
365 writew(0, ecc
->regs
+ ecc
->caps
->ecc_regs
[ECC_ENCIRQ_EN
]);
368 writew(ECC_OP_DISABLE
, ecc
->regs
+ ECC_CTL_REG(op
));
370 mutex_unlock(&ecc
->lock
);
372 EXPORT_SYMBOL(mtk_ecc_disable
);
374 int mtk_ecc_wait_done(struct mtk_ecc
*ecc
, enum mtk_ecc_operation op
)
378 ret
= wait_for_completion_timeout(&ecc
->done
, msecs_to_jiffies(500));
380 dev_err(ecc
->dev
, "%s timeout - interrupt did not arrive)\n",
381 (op
== ECC_ENCODE
) ? "encoder" : "decoder");
387 EXPORT_SYMBOL(mtk_ecc_wait_done
);
389 int mtk_ecc_encode(struct mtk_ecc
*ecc
, struct mtk_ecc_config
*config
,
396 addr
= dma_map_single(ecc
->dev
, data
, bytes
, DMA_TO_DEVICE
);
397 ret
= dma_mapping_error(ecc
->dev
, addr
);
399 dev_err(ecc
->dev
, "dma mapping error\n");
403 config
->op
= ECC_ENCODE
;
405 ret
= mtk_ecc_enable(ecc
, config
);
407 dma_unmap_single(ecc
->dev
, addr
, bytes
, DMA_TO_DEVICE
);
411 ret
= mtk_ecc_wait_done(ecc
, ECC_ENCODE
);
415 mtk_ecc_wait_idle(ecc
, ECC_ENCODE
);
417 /* Program ECC bytes to OOB: per sector oob = FDM + ECC + SPARE */
418 len
= (config
->strength
* ecc
->caps
->parity_bits
+ 7) >> 3;
420 /* write the parity bytes generated by the ECC back to temp buffer */
421 __ioread32_copy(ecc
->eccdata
,
422 ecc
->regs
+ ecc
->caps
->ecc_regs
[ECC_ENCPAR00
],
425 /* copy into possibly unaligned OOB region with actual length */
426 memcpy(data
+ bytes
, ecc
->eccdata
, len
);
429 dma_unmap_single(ecc
->dev
, addr
, bytes
, DMA_TO_DEVICE
);
430 mtk_ecc_disable(ecc
);
434 EXPORT_SYMBOL(mtk_ecc_encode
);
436 void mtk_ecc_adjust_strength(struct mtk_ecc
*ecc
, u32
*p
)
438 const u8
*ecc_strength
= ecc
->caps
->ecc_strength
;
441 for (i
= 0; i
< ecc
->caps
->num_ecc_strength
; i
++) {
442 if (*p
<= ecc_strength
[i
]) {
444 *p
= ecc_strength
[i
];
445 else if (*p
!= ecc_strength
[i
])
446 *p
= ecc_strength
[i
- 1];
451 *p
= ecc_strength
[ecc
->caps
->num_ecc_strength
- 1];
453 EXPORT_SYMBOL(mtk_ecc_adjust_strength
);
455 unsigned int mtk_ecc_get_parity_bits(struct mtk_ecc
*ecc
)
457 return ecc
->caps
->parity_bits
;
459 EXPORT_SYMBOL(mtk_ecc_get_parity_bits
);
461 static const struct mtk_ecc_caps mtk_ecc_caps_mt2701
= {
462 .err_mask
= ECC_ERRMASK_MT2701
,
464 .ecc_strength
= ecc_strength_mt2701
,
465 .ecc_regs
= mt2701_ecc_regs
,
466 .num_ecc_strength
= 20,
472 static const struct mtk_ecc_caps mtk_ecc_caps_mt2712
= {
473 .err_mask
= ECC_ERRMASK_MT2712
,
475 .ecc_strength
= ecc_strength_mt2712
,
476 .ecc_regs
= mt2712_ecc_regs
,
477 .num_ecc_strength
= 23,
483 static const struct mtk_ecc_caps mtk_ecc_caps_mt7622
= {
484 .err_mask
= ECC_ERRMASK_MT7622
,
486 .ecc_strength
= ecc_strength_mt7622
,
487 .ecc_regs
= mt7622_ecc_regs
,
488 .num_ecc_strength
= 5,
494 static const struct mtk_ecc_caps mtk_ecc_caps_mt7986
= {
495 .err_mask
= ECC_ERRMASK_MT7622
,
497 .ecc_strength
= ecc_strength_mt7986
,
498 .ecc_regs
= mt2712_ecc_regs
,
499 .num_ecc_strength
= 11,
505 static const struct of_device_id mtk_ecc_dt_match
[] = {
507 .compatible
= "mediatek,mt2701-ecc",
508 .data
= &mtk_ecc_caps_mt2701
,
510 .compatible
= "mediatek,mt2712-ecc",
511 .data
= &mtk_ecc_caps_mt2712
,
513 .compatible
= "mediatek,mt7622-ecc",
514 .data
= &mtk_ecc_caps_mt7622
,
516 .compatible
= "mediatek,mt7986-ecc",
517 .data
= &mtk_ecc_caps_mt7986
,
522 static int mtk_ecc_probe(struct platform_device
*pdev
)
524 struct device
*dev
= &pdev
->dev
;
526 u32 max_eccdata_size
;
529 ecc
= devm_kzalloc(dev
, sizeof(*ecc
), GFP_KERNEL
);
533 ecc
->caps
= of_device_get_match_data(dev
);
535 max_eccdata_size
= ecc
->caps
->num_ecc_strength
- 1;
536 max_eccdata_size
= ecc
->caps
->ecc_strength
[max_eccdata_size
];
537 max_eccdata_size
= (max_eccdata_size
* ecc
->caps
->parity_bits
+ 7) >> 3;
538 max_eccdata_size
= round_up(max_eccdata_size
, 4);
539 ecc
->eccdata
= devm_kzalloc(dev
, max_eccdata_size
, GFP_KERNEL
);
543 ecc
->regs
= devm_platform_ioremap_resource(pdev
, 0);
544 if (IS_ERR(ecc
->regs
))
545 return PTR_ERR(ecc
->regs
);
547 ecc
->clk
= devm_clk_get(dev
, NULL
);
548 if (IS_ERR(ecc
->clk
)) {
549 dev_err(dev
, "failed to get clock: %ld\n", PTR_ERR(ecc
->clk
));
550 return PTR_ERR(ecc
->clk
);
553 irq
= platform_get_irq(pdev
, 0);
557 ret
= dma_set_mask(dev
, DMA_BIT_MASK(32));
559 dev_err(dev
, "failed to set DMA mask\n");
563 ret
= devm_request_irq(dev
, irq
, mtk_ecc_irq
, 0x0, "mtk-ecc", ecc
);
565 dev_err(dev
, "failed to request irq\n");
570 mutex_init(&ecc
->lock
);
571 platform_set_drvdata(pdev
, ecc
);
572 dev_info(dev
, "probed\n");
577 #ifdef CONFIG_PM_SLEEP
578 static int mtk_ecc_suspend(struct device
*dev
)
580 struct mtk_ecc
*ecc
= dev_get_drvdata(dev
);
582 clk_disable_unprepare(ecc
->clk
);
587 static int mtk_ecc_resume(struct device
*dev
)
589 struct mtk_ecc
*ecc
= dev_get_drvdata(dev
);
592 ret
= clk_prepare_enable(ecc
->clk
);
594 dev_err(dev
, "failed to enable clk\n");
601 static SIMPLE_DEV_PM_OPS(mtk_ecc_pm_ops
, mtk_ecc_suspend
, mtk_ecc_resume
);
604 MODULE_DEVICE_TABLE(of
, mtk_ecc_dt_match
);
606 static struct platform_driver mtk_ecc_driver
= {
607 .probe
= mtk_ecc_probe
,
610 .of_match_table
= mtk_ecc_dt_match
,
611 #ifdef CONFIG_PM_SLEEP
612 .pm
= &mtk_ecc_pm_ops
,
617 module_platform_driver(mtk_ecc_driver
);
619 MODULE_AUTHOR("Xiaolei Li <xiaolei.li@mediatek.com>");
620 MODULE_DESCRIPTION("MTK Nand ECC Driver");
621 MODULE_LICENSE("Dual MIT/GPL");