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>
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.
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.
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>
24 #include <linux/of_platform.h>
25 #include <linux/mutex.h>
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)
35 #define ECC_ENCCON (0x00)
36 #define ECC_ENCCNFG (0x04)
37 #define ECC_MODE_SHIFT (5)
38 #define ECC_MS_SHIFT (16)
39 #define ECC_ENCDIADDR (0x08)
40 #define ECC_ENCIDLE (0x0C)
41 #define ECC_ENCIRQ_EN (0x80)
42 #define ECC_ENCIRQ_STA (0x84)
43 #define ECC_DECCON (0x100)
44 #define ECC_DECCNFG (0x104)
45 #define DEC_EMPTY_EN BIT(31)
46 #define DEC_CNFG_CORRECT (0x3 << 12)
47 #define ECC_DECIDLE (0x10C)
48 #define ECC_DECENUM0 (0x114)
49 #define ECC_DECDONE (0x124)
50 #define ECC_DECIRQ_EN (0x200)
51 #define ECC_DECIRQ_STA (0x204)
53 #define ECC_TIMEOUT (500000)
55 #define ECC_IDLE_REG(op) ((op) == ECC_ENCODE ? ECC_ENCIDLE : ECC_DECIDLE)
56 #define ECC_CTL_REG(op) ((op) == ECC_ENCODE ? ECC_ENCCON : ECC_DECCON)
57 #define ECC_IRQ_REG(op) ((op) == ECC_ENCODE ? \
58 ECC_ENCIRQ_EN : ECC_DECIRQ_EN)
62 const u8
*ecc_strength
;
64 u32 encode_parity_reg0
;
70 const struct mtk_ecc_caps
*caps
;
74 struct completion done
;
81 /* ecc strength that each IP supports */
82 static const u8 ecc_strength_mt2701
[] = {
83 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 28, 32, 36,
84 40, 44, 48, 52, 56, 60
87 static const u8 ecc_strength_mt2712
[] = {
88 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 28, 32, 36,
89 40, 44, 48, 52, 56, 60, 68, 72, 80
92 static inline void mtk_ecc_wait_idle(struct mtk_ecc
*ecc
,
93 enum mtk_ecc_operation op
)
95 struct device
*dev
= ecc
->dev
;
99 ret
= readl_poll_timeout_atomic(ecc
->regs
+ ECC_IDLE_REG(op
), val
,
103 dev_warn(dev
, "%s NOT idle\n",
104 op
== ECC_ENCODE
? "encoder" : "decoder");
107 static irqreturn_t
mtk_ecc_irq(int irq
, void *id
)
109 struct mtk_ecc
*ecc
= id
;
110 enum mtk_ecc_operation op
;
113 dec
= readw(ecc
->regs
+ ECC_DECIRQ_STA
) & ECC_IRQ_EN
;
116 dec
= readw(ecc
->regs
+ ECC_DECDONE
);
117 if (dec
& ecc
->sectors
) {
119 complete(&ecc
->done
);
124 enc
= readl(ecc
->regs
+ ECC_ENCIRQ_STA
) & ECC_IRQ_EN
;
127 complete(&ecc
->done
);
133 writel(0, ecc
->regs
+ ECC_IRQ_REG(op
));
138 static int mtk_ecc_config(struct mtk_ecc
*ecc
, struct mtk_ecc_config
*config
)
140 u32 ecc_bit
, dec_sz
, enc_sz
;
143 for (i
= 0; i
< ecc
->caps
->num_ecc_strength
; i
++) {
144 if (ecc
->caps
->ecc_strength
[i
] == config
->strength
)
148 if (i
== ecc
->caps
->num_ecc_strength
) {
149 dev_err(ecc
->dev
, "invalid ecc strength %d\n",
156 if (config
->op
== ECC_ENCODE
) {
157 /* configure ECC encoder (in bits) */
158 enc_sz
= config
->len
<< 3;
160 reg
= ecc_bit
| (config
->mode
<< ECC_MODE_SHIFT
);
161 reg
|= (enc_sz
<< ECC_MS_SHIFT
);
162 writel(reg
, ecc
->regs
+ ECC_ENCCNFG
);
164 if (config
->mode
!= ECC_NFI_MODE
)
165 writel(lower_32_bits(config
->addr
),
166 ecc
->regs
+ ECC_ENCDIADDR
);
169 /* configure ECC decoder (in bits) */
170 dec_sz
= (config
->len
<< 3) +
171 config
->strength
* ECC_PARITY_BITS
;
173 reg
= ecc_bit
| (config
->mode
<< ECC_MODE_SHIFT
);
174 reg
|= (dec_sz
<< ECC_MS_SHIFT
) | DEC_CNFG_CORRECT
;
176 writel(reg
, ecc
->regs
+ ECC_DECCNFG
);
179 ecc
->sectors
= 1 << (config
->sectors
- 1);
185 void mtk_ecc_get_stats(struct mtk_ecc
*ecc
, struct mtk_ecc_stats
*stats
,
191 stats
->corrected
= 0;
194 for (i
= 0; i
< sectors
; i
++) {
195 offset
= (i
>> 2) << 2;
196 err
= readl(ecc
->regs
+ ECC_DECENUM0
+ offset
);
197 err
= err
>> ((i
% 4) * 8);
198 err
&= ecc
->caps
->err_mask
;
199 if (err
== ecc
->caps
->err_mask
) {
200 /* uncorrectable errors */
205 stats
->corrected
+= err
;
206 bitflips
= max_t(u32
, bitflips
, err
);
209 stats
->bitflips
= bitflips
;
211 EXPORT_SYMBOL(mtk_ecc_get_stats
);
213 void mtk_ecc_release(struct mtk_ecc
*ecc
)
215 clk_disable_unprepare(ecc
->clk
);
216 put_device(ecc
->dev
);
218 EXPORT_SYMBOL(mtk_ecc_release
);
220 static void mtk_ecc_hw_init(struct mtk_ecc
*ecc
)
222 mtk_ecc_wait_idle(ecc
, ECC_ENCODE
);
223 writew(ECC_OP_DISABLE
, ecc
->regs
+ ECC_ENCCON
);
225 mtk_ecc_wait_idle(ecc
, ECC_DECODE
);
226 writel(ECC_OP_DISABLE
, ecc
->regs
+ ECC_DECCON
);
229 static struct mtk_ecc
*mtk_ecc_get(struct device_node
*np
)
231 struct platform_device
*pdev
;
234 pdev
= of_find_device_by_node(np
);
235 if (!pdev
|| !platform_get_drvdata(pdev
))
236 return ERR_PTR(-EPROBE_DEFER
);
238 get_device(&pdev
->dev
);
239 ecc
= platform_get_drvdata(pdev
);
240 clk_prepare_enable(ecc
->clk
);
241 mtk_ecc_hw_init(ecc
);
246 struct mtk_ecc
*of_mtk_ecc_get(struct device_node
*of_node
)
248 struct mtk_ecc
*ecc
= NULL
;
249 struct device_node
*np
;
251 np
= of_parse_phandle(of_node
, "ecc-engine", 0);
253 ecc
= mtk_ecc_get(np
);
259 EXPORT_SYMBOL(of_mtk_ecc_get
);
261 int mtk_ecc_enable(struct mtk_ecc
*ecc
, struct mtk_ecc_config
*config
)
263 enum mtk_ecc_operation op
= config
->op
;
267 ret
= mutex_lock_interruptible(&ecc
->lock
);
269 dev_err(ecc
->dev
, "interrupted when attempting to lock\n");
273 mtk_ecc_wait_idle(ecc
, op
);
275 ret
= mtk_ecc_config(ecc
, config
);
277 mutex_unlock(&ecc
->lock
);
281 if (config
->mode
!= ECC_NFI_MODE
|| op
!= ECC_ENCODE
) {
282 init_completion(&ecc
->done
);
283 reg_val
= ECC_IRQ_EN
;
285 * For ECC_NFI_MODE, if ecc->caps->pg_irq_sel is 1, then it
286 * means this chip can only generate one ecc irq during page
287 * read / write. If is 0, generate one ecc irq each ecc step.
289 if (ecc
->caps
->pg_irq_sel
&& config
->mode
== ECC_NFI_MODE
)
290 reg_val
|= ECC_PG_IRQ_SEL
;
291 writew(reg_val
, ecc
->regs
+ ECC_IRQ_REG(op
));
294 writew(ECC_OP_ENABLE
, ecc
->regs
+ ECC_CTL_REG(op
));
298 EXPORT_SYMBOL(mtk_ecc_enable
);
300 void mtk_ecc_disable(struct mtk_ecc
*ecc
)
302 enum mtk_ecc_operation op
= ECC_ENCODE
;
304 /* find out the running operation */
305 if (readw(ecc
->regs
+ ECC_CTL_REG(op
)) != ECC_OP_ENABLE
)
309 mtk_ecc_wait_idle(ecc
, op
);
310 writew(0, ecc
->regs
+ ECC_IRQ_REG(op
));
311 writew(ECC_OP_DISABLE
, ecc
->regs
+ ECC_CTL_REG(op
));
313 mutex_unlock(&ecc
->lock
);
315 EXPORT_SYMBOL(mtk_ecc_disable
);
317 int mtk_ecc_wait_done(struct mtk_ecc
*ecc
, enum mtk_ecc_operation op
)
321 ret
= wait_for_completion_timeout(&ecc
->done
, msecs_to_jiffies(500));
323 dev_err(ecc
->dev
, "%s timeout - interrupt did not arrive)\n",
324 (op
== ECC_ENCODE
) ? "encoder" : "decoder");
330 EXPORT_SYMBOL(mtk_ecc_wait_done
);
332 int mtk_ecc_encode(struct mtk_ecc
*ecc
, struct mtk_ecc_config
*config
,
339 addr
= dma_map_single(ecc
->dev
, data
, bytes
, DMA_TO_DEVICE
);
340 ret
= dma_mapping_error(ecc
->dev
, addr
);
342 dev_err(ecc
->dev
, "dma mapping error\n");
346 config
->op
= ECC_ENCODE
;
348 ret
= mtk_ecc_enable(ecc
, config
);
350 dma_unmap_single(ecc
->dev
, addr
, bytes
, DMA_TO_DEVICE
);
354 ret
= mtk_ecc_wait_done(ecc
, ECC_ENCODE
);
358 mtk_ecc_wait_idle(ecc
, ECC_ENCODE
);
360 /* Program ECC bytes to OOB: per sector oob = FDM + ECC + SPARE */
361 len
= (config
->strength
* ECC_PARITY_BITS
+ 7) >> 3;
363 /* write the parity bytes generated by the ECC back to temp buffer */
364 __ioread32_copy(ecc
->eccdata
,
365 ecc
->regs
+ ecc
->caps
->encode_parity_reg0
,
368 /* copy into possibly unaligned OOB region with actual length */
369 memcpy(data
+ bytes
, ecc
->eccdata
, len
);
372 dma_unmap_single(ecc
->dev
, addr
, bytes
, DMA_TO_DEVICE
);
373 mtk_ecc_disable(ecc
);
377 EXPORT_SYMBOL(mtk_ecc_encode
);
379 void mtk_ecc_adjust_strength(struct mtk_ecc
*ecc
, u32
*p
)
381 const u8
*ecc_strength
= ecc
->caps
->ecc_strength
;
384 for (i
= 0; i
< ecc
->caps
->num_ecc_strength
; i
++) {
385 if (*p
<= ecc_strength
[i
]) {
387 *p
= ecc_strength
[i
];
388 else if (*p
!= ecc_strength
[i
])
389 *p
= ecc_strength
[i
- 1];
394 *p
= ecc_strength
[ecc
->caps
->num_ecc_strength
- 1];
396 EXPORT_SYMBOL(mtk_ecc_adjust_strength
);
398 static const struct mtk_ecc_caps mtk_ecc_caps_mt2701
= {
400 .ecc_strength
= ecc_strength_mt2701
,
401 .num_ecc_strength
= 20,
402 .encode_parity_reg0
= 0x10,
406 static const struct mtk_ecc_caps mtk_ecc_caps_mt2712
= {
408 .ecc_strength
= ecc_strength_mt2712
,
409 .num_ecc_strength
= 23,
410 .encode_parity_reg0
= 0x300,
414 static const struct of_device_id mtk_ecc_dt_match
[] = {
416 .compatible
= "mediatek,mt2701-ecc",
417 .data
= &mtk_ecc_caps_mt2701
,
419 .compatible
= "mediatek,mt2712-ecc",
420 .data
= &mtk_ecc_caps_mt2712
,
425 static int mtk_ecc_probe(struct platform_device
*pdev
)
427 struct device
*dev
= &pdev
->dev
;
429 struct resource
*res
;
430 const struct of_device_id
*of_ecc_id
= NULL
;
431 u32 max_eccdata_size
;
434 ecc
= devm_kzalloc(dev
, sizeof(*ecc
), GFP_KERNEL
);
438 of_ecc_id
= of_match_device(mtk_ecc_dt_match
, &pdev
->dev
);
442 ecc
->caps
= of_ecc_id
->data
;
444 max_eccdata_size
= ecc
->caps
->num_ecc_strength
- 1;
445 max_eccdata_size
= ecc
->caps
->ecc_strength
[max_eccdata_size
];
446 max_eccdata_size
= (max_eccdata_size
* ECC_PARITY_BITS
+ 7) >> 3;
447 max_eccdata_size
= round_up(max_eccdata_size
, 4);
448 ecc
->eccdata
= devm_kzalloc(dev
, max_eccdata_size
, GFP_KERNEL
);
452 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
453 ecc
->regs
= devm_ioremap_resource(dev
, res
);
454 if (IS_ERR(ecc
->regs
)) {
455 dev_err(dev
, "failed to map regs: %ld\n", PTR_ERR(ecc
->regs
));
456 return PTR_ERR(ecc
->regs
);
459 ecc
->clk
= devm_clk_get(dev
, NULL
);
460 if (IS_ERR(ecc
->clk
)) {
461 dev_err(dev
, "failed to get clock: %ld\n", PTR_ERR(ecc
->clk
));
462 return PTR_ERR(ecc
->clk
);
465 irq
= platform_get_irq(pdev
, 0);
467 dev_err(dev
, "failed to get irq: %d\n", irq
);
471 ret
= dma_set_mask(dev
, DMA_BIT_MASK(32));
473 dev_err(dev
, "failed to set DMA mask\n");
477 ret
= devm_request_irq(dev
, irq
, mtk_ecc_irq
, 0x0, "mtk-ecc", ecc
);
479 dev_err(dev
, "failed to request irq\n");
484 mutex_init(&ecc
->lock
);
485 platform_set_drvdata(pdev
, ecc
);
486 dev_info(dev
, "probed\n");
491 #ifdef CONFIG_PM_SLEEP
492 static int mtk_ecc_suspend(struct device
*dev
)
494 struct mtk_ecc
*ecc
= dev_get_drvdata(dev
);
496 clk_disable_unprepare(ecc
->clk
);
501 static int mtk_ecc_resume(struct device
*dev
)
503 struct mtk_ecc
*ecc
= dev_get_drvdata(dev
);
506 ret
= clk_prepare_enable(ecc
->clk
);
508 dev_err(dev
, "failed to enable clk\n");
515 static SIMPLE_DEV_PM_OPS(mtk_ecc_pm_ops
, mtk_ecc_suspend
, mtk_ecc_resume
);
518 MODULE_DEVICE_TABLE(of
, mtk_ecc_dt_match
);
520 static struct platform_driver mtk_ecc_driver
= {
521 .probe
= mtk_ecc_probe
,
524 .of_match_table
= of_match_ptr(mtk_ecc_dt_match
),
525 #ifdef CONFIG_PM_SLEEP
526 .pm
= &mtk_ecc_pm_ops
,
531 module_platform_driver(mtk_ecc_driver
);
533 MODULE_AUTHOR("Xiaolei Li <xiaolei.li@mediatek.com>");
534 MODULE_DESCRIPTION("MTK Nand ECC Driver");
535 MODULE_LICENSE("GPL");