1a0ac778eSPaul Cercueil // SPDX-License-Identifier: GPL-2.0
2a0ac778eSPaul Cercueil /*
3a0ac778eSPaul Cercueil * JZ4740 ECC controller driver
4a0ac778eSPaul Cercueil *
5a0ac778eSPaul Cercueil * Copyright (c) 2019 Paul Cercueil <paul@crapouillou.net>
6a0ac778eSPaul Cercueil *
7a0ac778eSPaul Cercueil * based on jz4740-nand.c
8a0ac778eSPaul Cercueil */
9a0ac778eSPaul Cercueil
10a0ac778eSPaul Cercueil #include <linux/bitops.h>
11a0ac778eSPaul Cercueil #include <linux/device.h>
12a0ac778eSPaul Cercueil #include <linux/io.h>
13a0ac778eSPaul Cercueil #include <linux/module.h>
14a0ac778eSPaul Cercueil #include <linux/of_platform.h>
15a0ac778eSPaul Cercueil #include <linux/platform_device.h>
16a0ac778eSPaul Cercueil
17a0ac778eSPaul Cercueil #include "ingenic_ecc.h"
18a0ac778eSPaul Cercueil
19a0ac778eSPaul Cercueil #define JZ_REG_NAND_ECC_CTRL 0x00
20a0ac778eSPaul Cercueil #define JZ_REG_NAND_DATA 0x04
21a0ac778eSPaul Cercueil #define JZ_REG_NAND_PAR0 0x08
22a0ac778eSPaul Cercueil #define JZ_REG_NAND_PAR1 0x0C
23a0ac778eSPaul Cercueil #define JZ_REG_NAND_PAR2 0x10
24a0ac778eSPaul Cercueil #define JZ_REG_NAND_IRQ_STAT 0x14
25a0ac778eSPaul Cercueil #define JZ_REG_NAND_IRQ_CTRL 0x18
26a0ac778eSPaul Cercueil #define JZ_REG_NAND_ERR(x) (0x1C + ((x) << 2))
27a0ac778eSPaul Cercueil
28a0ac778eSPaul Cercueil #define JZ_NAND_ECC_CTRL_PAR_READY BIT(4)
29a0ac778eSPaul Cercueil #define JZ_NAND_ECC_CTRL_ENCODING BIT(3)
30a0ac778eSPaul Cercueil #define JZ_NAND_ECC_CTRL_RS BIT(2)
31a0ac778eSPaul Cercueil #define JZ_NAND_ECC_CTRL_RESET BIT(1)
32a0ac778eSPaul Cercueil #define JZ_NAND_ECC_CTRL_ENABLE BIT(0)
33a0ac778eSPaul Cercueil
34a0ac778eSPaul Cercueil #define JZ_NAND_STATUS_ERR_COUNT (BIT(31) | BIT(30) | BIT(29))
35a0ac778eSPaul Cercueil #define JZ_NAND_STATUS_PAD_FINISH BIT(4)
36a0ac778eSPaul Cercueil #define JZ_NAND_STATUS_DEC_FINISH BIT(3)
37a0ac778eSPaul Cercueil #define JZ_NAND_STATUS_ENC_FINISH BIT(2)
38a0ac778eSPaul Cercueil #define JZ_NAND_STATUS_UNCOR_ERROR BIT(1)
39a0ac778eSPaul Cercueil #define JZ_NAND_STATUS_ERROR BIT(0)
40a0ac778eSPaul Cercueil
41a0ac778eSPaul Cercueil static const uint8_t empty_block_ecc[] = {
42a0ac778eSPaul Cercueil 0xcd, 0x9d, 0x90, 0x58, 0xf4, 0x8b, 0xff, 0xb7, 0x6f
43a0ac778eSPaul Cercueil };
44a0ac778eSPaul Cercueil
jz4740_ecc_reset(struct ingenic_ecc * ecc,bool calc_ecc)45a0ac778eSPaul Cercueil static void jz4740_ecc_reset(struct ingenic_ecc *ecc, bool calc_ecc)
46a0ac778eSPaul Cercueil {
47a0ac778eSPaul Cercueil uint32_t reg;
48a0ac778eSPaul Cercueil
49a0ac778eSPaul Cercueil /* Clear interrupt status */
50a0ac778eSPaul Cercueil writel(0, ecc->base + JZ_REG_NAND_IRQ_STAT);
51a0ac778eSPaul Cercueil
52a0ac778eSPaul Cercueil /* Initialize and enable ECC hardware */
53a0ac778eSPaul Cercueil reg = readl(ecc->base + JZ_REG_NAND_ECC_CTRL);
54a0ac778eSPaul Cercueil reg |= JZ_NAND_ECC_CTRL_RESET;
55a0ac778eSPaul Cercueil reg |= JZ_NAND_ECC_CTRL_ENABLE;
56a0ac778eSPaul Cercueil reg |= JZ_NAND_ECC_CTRL_RS;
57a0ac778eSPaul Cercueil if (calc_ecc) /* calculate ECC from data */
58a0ac778eSPaul Cercueil reg |= JZ_NAND_ECC_CTRL_ENCODING;
59a0ac778eSPaul Cercueil else /* correct data from ECC */
60a0ac778eSPaul Cercueil reg &= ~JZ_NAND_ECC_CTRL_ENCODING;
61a0ac778eSPaul Cercueil
62a0ac778eSPaul Cercueil writel(reg, ecc->base + JZ_REG_NAND_ECC_CTRL);
63a0ac778eSPaul Cercueil }
64a0ac778eSPaul Cercueil
jz4740_ecc_calculate(struct ingenic_ecc * ecc,struct ingenic_ecc_params * params,const u8 * buf,u8 * ecc_code)65a0ac778eSPaul Cercueil static int jz4740_ecc_calculate(struct ingenic_ecc *ecc,
66a0ac778eSPaul Cercueil struct ingenic_ecc_params *params,
67a0ac778eSPaul Cercueil const u8 *buf, u8 *ecc_code)
68a0ac778eSPaul Cercueil {
69a0ac778eSPaul Cercueil uint32_t reg, status;
70a0ac778eSPaul Cercueil unsigned int timeout = 1000;
71a0ac778eSPaul Cercueil int i;
72a0ac778eSPaul Cercueil
73a0ac778eSPaul Cercueil jz4740_ecc_reset(ecc, true);
74a0ac778eSPaul Cercueil
75a0ac778eSPaul Cercueil do {
76a0ac778eSPaul Cercueil status = readl(ecc->base + JZ_REG_NAND_IRQ_STAT);
77a0ac778eSPaul Cercueil } while (!(status & JZ_NAND_STATUS_ENC_FINISH) && --timeout);
78a0ac778eSPaul Cercueil
79a0ac778eSPaul Cercueil if (timeout == 0)
80a0ac778eSPaul Cercueil return -ETIMEDOUT;
81a0ac778eSPaul Cercueil
82a0ac778eSPaul Cercueil reg = readl(ecc->base + JZ_REG_NAND_ECC_CTRL);
83a0ac778eSPaul Cercueil reg &= ~JZ_NAND_ECC_CTRL_ENABLE;
84a0ac778eSPaul Cercueil writel(reg, ecc->base + JZ_REG_NAND_ECC_CTRL);
85a0ac778eSPaul Cercueil
86a0ac778eSPaul Cercueil for (i = 0; i < params->bytes; ++i)
87a0ac778eSPaul Cercueil ecc_code[i] = readb(ecc->base + JZ_REG_NAND_PAR0 + i);
88a0ac778eSPaul Cercueil
89a0ac778eSPaul Cercueil /*
90a0ac778eSPaul Cercueil * If the written data is completely 0xff, we also want to write 0xff as
91a0ac778eSPaul Cercueil * ECC, otherwise we will get in trouble when doing subpage writes.
92a0ac778eSPaul Cercueil */
93*d2e639d6SDan Carpenter if (memcmp(ecc_code, empty_block_ecc, sizeof(empty_block_ecc)) == 0)
94*d2e639d6SDan Carpenter memset(ecc_code, 0xff, sizeof(empty_block_ecc));
95a0ac778eSPaul Cercueil
96a0ac778eSPaul Cercueil return 0;
97a0ac778eSPaul Cercueil }
98a0ac778eSPaul Cercueil
jz_nand_correct_data(uint8_t * buf,int index,int mask)99a0ac778eSPaul Cercueil static void jz_nand_correct_data(uint8_t *buf, int index, int mask)
100a0ac778eSPaul Cercueil {
101a0ac778eSPaul Cercueil int offset = index & 0x7;
102a0ac778eSPaul Cercueil uint16_t data;
103a0ac778eSPaul Cercueil
104a0ac778eSPaul Cercueil index += (index >> 3);
105a0ac778eSPaul Cercueil
106a0ac778eSPaul Cercueil data = buf[index];
107a0ac778eSPaul Cercueil data |= buf[index + 1] << 8;
108a0ac778eSPaul Cercueil
109a0ac778eSPaul Cercueil mask ^= (data >> offset) & 0x1ff;
110a0ac778eSPaul Cercueil data &= ~(0x1ff << offset);
111a0ac778eSPaul Cercueil data |= (mask << offset);
112a0ac778eSPaul Cercueil
113a0ac778eSPaul Cercueil buf[index] = data & 0xff;
114a0ac778eSPaul Cercueil buf[index + 1] = (data >> 8) & 0xff;
115a0ac778eSPaul Cercueil }
116a0ac778eSPaul Cercueil
jz4740_ecc_correct(struct ingenic_ecc * ecc,struct ingenic_ecc_params * params,u8 * buf,u8 * ecc_code)117a0ac778eSPaul Cercueil static int jz4740_ecc_correct(struct ingenic_ecc *ecc,
118a0ac778eSPaul Cercueil struct ingenic_ecc_params *params,
119a0ac778eSPaul Cercueil u8 *buf, u8 *ecc_code)
120a0ac778eSPaul Cercueil {
121a0ac778eSPaul Cercueil int i, error_count, index;
122a0ac778eSPaul Cercueil uint32_t reg, status, error;
123a0ac778eSPaul Cercueil unsigned int timeout = 1000;
124a0ac778eSPaul Cercueil
125a0ac778eSPaul Cercueil jz4740_ecc_reset(ecc, false);
126a0ac778eSPaul Cercueil
127a0ac778eSPaul Cercueil for (i = 0; i < params->bytes; ++i)
128a0ac778eSPaul Cercueil writeb(ecc_code[i], ecc->base + JZ_REG_NAND_PAR0 + i);
129a0ac778eSPaul Cercueil
130a0ac778eSPaul Cercueil reg = readl(ecc->base + JZ_REG_NAND_ECC_CTRL);
131a0ac778eSPaul Cercueil reg |= JZ_NAND_ECC_CTRL_PAR_READY;
132a0ac778eSPaul Cercueil writel(reg, ecc->base + JZ_REG_NAND_ECC_CTRL);
133a0ac778eSPaul Cercueil
134a0ac778eSPaul Cercueil do {
135a0ac778eSPaul Cercueil status = readl(ecc->base + JZ_REG_NAND_IRQ_STAT);
136a0ac778eSPaul Cercueil } while (!(status & JZ_NAND_STATUS_DEC_FINISH) && --timeout);
137a0ac778eSPaul Cercueil
138a0ac778eSPaul Cercueil if (timeout == 0)
139a0ac778eSPaul Cercueil return -ETIMEDOUT;
140a0ac778eSPaul Cercueil
141a0ac778eSPaul Cercueil reg = readl(ecc->base + JZ_REG_NAND_ECC_CTRL);
142a0ac778eSPaul Cercueil reg &= ~JZ_NAND_ECC_CTRL_ENABLE;
143a0ac778eSPaul Cercueil writel(reg, ecc->base + JZ_REG_NAND_ECC_CTRL);
144a0ac778eSPaul Cercueil
145a0ac778eSPaul Cercueil if (status & JZ_NAND_STATUS_ERROR) {
146a0ac778eSPaul Cercueil if (status & JZ_NAND_STATUS_UNCOR_ERROR)
147a0ac778eSPaul Cercueil return -EBADMSG;
148a0ac778eSPaul Cercueil
149a0ac778eSPaul Cercueil error_count = (status & JZ_NAND_STATUS_ERR_COUNT) >> 29;
150a0ac778eSPaul Cercueil
151a0ac778eSPaul Cercueil for (i = 0; i < error_count; ++i) {
152a0ac778eSPaul Cercueil error = readl(ecc->base + JZ_REG_NAND_ERR(i));
153a0ac778eSPaul Cercueil index = ((error >> 16) & 0x1ff) - 1;
154a0ac778eSPaul Cercueil if (index >= 0 && index < params->size)
155a0ac778eSPaul Cercueil jz_nand_correct_data(buf, index, error & 0x1ff);
156a0ac778eSPaul Cercueil }
157a0ac778eSPaul Cercueil
158a0ac778eSPaul Cercueil return error_count;
159a0ac778eSPaul Cercueil }
160a0ac778eSPaul Cercueil
161a0ac778eSPaul Cercueil return 0;
162a0ac778eSPaul Cercueil }
163a0ac778eSPaul Cercueil
jz4740_ecc_disable(struct ingenic_ecc * ecc)164a0ac778eSPaul Cercueil static void jz4740_ecc_disable(struct ingenic_ecc *ecc)
165a0ac778eSPaul Cercueil {
166a0ac778eSPaul Cercueil u32 reg;
167a0ac778eSPaul Cercueil
168a0ac778eSPaul Cercueil writel(0, ecc->base + JZ_REG_NAND_IRQ_STAT);
169a0ac778eSPaul Cercueil reg = readl(ecc->base + JZ_REG_NAND_ECC_CTRL);
170a0ac778eSPaul Cercueil reg &= ~JZ_NAND_ECC_CTRL_ENABLE;
171a0ac778eSPaul Cercueil writel(reg, ecc->base + JZ_REG_NAND_ECC_CTRL);
172a0ac778eSPaul Cercueil }
173a0ac778eSPaul Cercueil
174a0ac778eSPaul Cercueil static const struct ingenic_ecc_ops jz4740_ecc_ops = {
175a0ac778eSPaul Cercueil .disable = jz4740_ecc_disable,
176a0ac778eSPaul Cercueil .calculate = jz4740_ecc_calculate,
177a0ac778eSPaul Cercueil .correct = jz4740_ecc_correct,
178a0ac778eSPaul Cercueil };
179a0ac778eSPaul Cercueil
180a0ac778eSPaul Cercueil static const struct of_device_id jz4740_ecc_dt_match[] = {
181a0ac778eSPaul Cercueil { .compatible = "ingenic,jz4740-ecc", .data = &jz4740_ecc_ops },
182a0ac778eSPaul Cercueil {},
183a0ac778eSPaul Cercueil };
184a0ac778eSPaul Cercueil MODULE_DEVICE_TABLE(of, jz4740_ecc_dt_match);
185a0ac778eSPaul Cercueil
186a0ac778eSPaul Cercueil static struct platform_driver jz4740_ecc_driver = {
187a0ac778eSPaul Cercueil .probe = ingenic_ecc_probe,
188a0ac778eSPaul Cercueil .driver = {
189a0ac778eSPaul Cercueil .name = "jz4740-ecc",
190a0ac778eSPaul Cercueil .of_match_table = jz4740_ecc_dt_match,
191a0ac778eSPaul Cercueil },
192a0ac778eSPaul Cercueil };
193a0ac778eSPaul Cercueil module_platform_driver(jz4740_ecc_driver);
194a0ac778eSPaul Cercueil
195a0ac778eSPaul Cercueil MODULE_AUTHOR("Paul Cercueil <paul@crapouillou.net>");
196a0ac778eSPaul Cercueil MODULE_DESCRIPTION("Ingenic JZ4740 ECC controller driver");
197a0ac778eSPaul Cercueil MODULE_LICENSE("GPL v2");
198