2 * Copyright (C) 2009, Lars-Peter Clausen <lars@metafoo.de>
3 * JZ4720/JZ4740 SoC NAND controller driver
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
10 * You should have received a copy of the GNU General Public License along
11 * with this program; if not, write to the Free Software Foundation, Inc.,
12 * 675 Mass Ave, Cambridge, MA 02139, USA.
16 #include <linux/ioport.h>
17 #include <linux/platform_device.h>
19 #include <linux/mtd/mtd.h>
20 #include <linux/mtd/nand.h>
21 #include <linux/mtd/partitions.h>
23 #include <linux/mtd/jz4740_nand.h>
24 #include <linux/gpio.h>
26 #define JZ_REG_NAND_CTRL 0x50
27 #define JZ_REG_NAND_ECC_CTRL 0x100
28 #define JZ_REG_NAND_DATA 0x104
29 #define JZ_REG_NAND_PAR0 0x108
30 #define JZ_REG_NAND_PAR1 0x10C
31 #define JZ_REG_NAND_PAR2 0x110
32 #define JZ_REG_NAND_IRQ_STAT 0x114
33 #define JZ_REG_NAND_IRQ_CTRL 0x118
34 #define JZ_REG_NAND_ERR(x) (0x11C + (x << 2))
36 #define JZ_NAND_ECC_CTRL_PAR_READY BIT(4)
37 #define JZ_NAND_ECC_CTRL_ENCODING BIT(3)
38 #define JZ_NAND_ECC_CTRL_RS BIT(2)
39 #define JZ_NAND_ECC_CTRL_RESET BIT(1)
40 #define JZ_NAND_ECC_CTRL_ENABLE BIT(0)
42 #define JZ_NAND_STATUS_ERR_COUNT (BIT(31) | BIT(30) | BIT(29))
43 #define JZ_NAND_STATUS_PAD_FINISH BIT(4)
44 #define JZ_NAND_STATUS_DEC_FINISH BIT(3)
45 #define JZ_NAND_STATUS_ENC_FINISH BIT(2)
46 #define JZ_NAND_STATUS_UNCOR_ERROR BIT(1)
47 #define JZ_NAND_STATUS_ERROR BIT(0)
49 #define JZ_NAND_CTRL_ENABLE_CHIP(x) BIT(x << 1)
50 #define JZ_NAND_CTRL_ASSERT_CHIP(x) BIT((x << 1) + 1)
52 #define JZ_NAND_DATA_ADDR ((void __iomem *)0xB8000000)
53 #define JZ_NAND_CMD_ADDR (JZ_NAND_DATA_ADDR + 0x8000)
54 #define JZ_NAND_ADDR_ADDR (JZ_NAND_DATA_ADDR + 0x10000)
58 struct nand_chip chip;
62 struct jz_nand_platform_data *pdata;
65 static inline struct jz_nand *mtd_to_jz_nand(struct mtd_info *mtd)
67 return container_of(mtd, struct jz_nand, mtd);
70 static void jz_nand_cmd_ctrl(struct mtd_info *mtd, int dat, unsigned int ctrl)
72 struct jz_nand *nand = mtd_to_jz_nand(mtd);
73 struct nand_chip *chip = mtd->priv;
76 if (ctrl & NAND_CTRL_CHANGE) {
77 BUG_ON((ctrl & NAND_ALE) && (ctrl & NAND_CLE));
79 chip->IO_ADDR_W = JZ_NAND_ADDR_ADDR;
80 else if (ctrl & NAND_CLE)
81 chip->IO_ADDR_W = JZ_NAND_CMD_ADDR;
83 chip->IO_ADDR_W = JZ_NAND_DATA_ADDR;
85 reg = readl(nand->base + JZ_REG_NAND_CTRL);
86 if ( ctrl & NAND_NCE )
87 reg |= JZ_NAND_CTRL_ASSERT_CHIP(0);
89 reg &= ~JZ_NAND_CTRL_ASSERT_CHIP(0);
90 writel(reg, nand->base + JZ_REG_NAND_CTRL);
92 if (dat != NAND_CMD_NONE)
93 writeb(dat, chip->IO_ADDR_W);
96 static int jz_nand_dev_ready(struct mtd_info *mtd)
98 struct jz_nand *nand = mtd_to_jz_nand(mtd);
99 return gpio_get_value_cansleep(nand->pdata->busy_gpio);
102 static void jz_nand_hwctl(struct mtd_info *mtd, int mode)
104 struct jz_nand *nand = mtd_to_jz_nand(mtd);
108 writel(0, nand->base + JZ_REG_NAND_IRQ_STAT);
109 reg = readl(nand->base + JZ_REG_NAND_ECC_CTRL);
111 reg |= JZ_NAND_ECC_CTRL_RESET;
112 reg |= JZ_NAND_ECC_CTRL_ENABLE;
113 reg |= JZ_NAND_ECC_CTRL_RS;
117 reg &= ~JZ_NAND_ECC_CTRL_ENCODING;
120 reg |= JZ_NAND_ECC_CTRL_ENCODING;
126 writel(reg, nand->base + JZ_REG_NAND_ECC_CTRL);
129 static int jz_nand_calculate_ecc_rs(struct mtd_info* mtd, const uint8_t* dat,
132 struct jz_nand *nand = mtd_to_jz_nand(mtd);
133 uint32_t reg, status;
137 status = readl(nand->base + JZ_REG_NAND_IRQ_STAT);
138 } while(!(status & JZ_NAND_STATUS_ENC_FINISH));
140 reg = readl(nand->base + JZ_REG_NAND_ECC_CTRL);
141 reg &= ~JZ_NAND_ECC_CTRL_ENABLE;
142 writel(reg, nand->base + JZ_REG_NAND_ECC_CTRL);
144 for (i = 0; i < 9; ++i) {
145 ecc_code[i] = readb(nand->base + JZ_REG_NAND_PAR0 + i);
151 static void correct_data(uint8_t *dat, int index, int mask)
153 int offset = index & 0x7;
157 index += (index >> 3);
160 data |= dat[index+1] << 8;
162 printk("0x%x -> ", data);
164 mask ^= (data >> offset) & 0x1ff;
165 data &= ~(0x1ff << offset);
166 data |= (mask << offset);
168 printk("0x%x\n", data);
170 dat[index] = data & 0xff;
171 dat[index+1] = (data >> 8) & 0xff;
174 static int jz_nand_correct_ecc_rs(struct mtd_info* mtd, uint8_t *dat,
175 uint8_t *read_ecc, uint8_t *calc_ecc)
177 struct jz_nand *nand = mtd_to_jz_nand(mtd);
178 int i, error_count, index;
179 uint32_t reg, status, error;
181 for(i = 0; i < 9; ++i) {
182 if (read_ecc[i] != 0xff)
186 for (i = 0; i < nand->chip.ecc.size; ++i) {
190 if (i == nand->chip.ecc.size)
194 for(i = 0; i < 9; ++i)
195 writeb(read_ecc[i], nand->base + JZ_REG_NAND_PAR0 + i);
197 reg = readl(nand->base + JZ_REG_NAND_ECC_CTRL);
198 reg |= JZ_NAND_ECC_CTRL_PAR_READY;
199 writel(reg, nand->base + JZ_REG_NAND_ECC_CTRL);
202 status = readl(nand->base + JZ_REG_NAND_IRQ_STAT);
203 } while (!(status & JZ_NAND_STATUS_DEC_FINISH));
205 reg = readl(nand->base + JZ_REG_NAND_ECC_CTRL);
206 reg &= ~JZ_NAND_ECC_CTRL_ENABLE;
207 writel(reg, nand->base + JZ_REG_NAND_ECC_CTRL);
209 if (status & JZ_NAND_STATUS_ERROR) {
210 if (status & JZ_NAND_STATUS_UNCOR_ERROR) {
211 printk("uncorrectable ecc:");
212 for(i = 0; i < 9; ++i)
213 printk(" 0x%x", read_ecc[i]);
215 printk("uncorrectable data:");
216 for(i = 0; i < 32; ++i)
217 printk(" 0x%x", dat[i]);
222 error_count = (status & JZ_NAND_STATUS_ERR_COUNT) >> 29;
224 printk("error_count: %d %x\n", error_count, status);
226 for(i = 0; i < error_count; ++i) {
227 error = readl(nand->base + JZ_REG_NAND_ERR(i));
228 index = ((error >> 16) & 0x1ff) - 1;
229 if (index >= 0 && index < 512) {
230 correct_data(dat, index, error & 0x1ff);
242 #ifdef CONFIG_MTD_CMDLINE_PARTS
243 static const char *part_probes[] = {"cmdline", NULL};
246 static int __devinit jz_nand_probe(struct platform_device *pdev)
249 struct jz_nand *nand;
250 struct nand_chip *chip;
251 struct mtd_info *mtd;
252 struct jz_nand_platform_data *pdata = pdev->dev.platform_data;
253 #ifdef CONFIG_MTD_PARTITIONS
254 struct mtd_partition *partition_info;
255 int num_partitions = 0;
258 nand = kzalloc(sizeof(*nand), GFP_KERNEL);
260 dev_err(&pdev->dev, "Failed to allocate device structure.\n");
264 nand->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
266 dev_err(&pdev->dev, "Failed to get platform mmio memory\n");
271 nand->mem = request_mem_region(nand->mem->start, resource_size(nand->mem),
275 dev_err(&pdev->dev, "Failed to request mmio memory region\n");
280 nand->base = ioremap(nand->mem->start, resource_size(nand->mem));
283 dev_err(&pdev->dev, "Faild to ioremap mmio memory region\n");
285 goto err_release_mem;
288 if (pdata && gpio_is_valid(pdata->busy_gpio)) {
289 ret = gpio_request(pdata->busy_gpio, "jz nand busy line");
291 dev_err(&pdev->dev, "Failed to request busy gpio %d: %d\n",
292 pdata->busy_gpio, ret);
300 mtd->owner = THIS_MODULE;
301 mtd->name = "jz4740-nand";
303 chip->ecc.hwctl = jz_nand_hwctl;
305 chip->ecc.calculate = jz_nand_calculate_ecc_rs;
306 chip->ecc.correct = jz_nand_correct_ecc_rs;
307 chip->ecc.mode = NAND_ECC_HW;
308 chip->ecc.size = 512;
311 chip->ecc.layout = pdata->ecc_layout;
313 chip->chip_delay = 50;
314 chip->cmd_ctrl = jz_nand_cmd_ctrl;
316 if (pdata && gpio_is_valid(pdata->busy_gpio))
317 chip->dev_ready = jz_nand_dev_ready;
319 chip->IO_ADDR_R = JZ_NAND_DATA_ADDR;
320 chip->IO_ADDR_W = JZ_NAND_DATA_ADDR;
323 platform_set_drvdata(pdev, nand);
325 ret = nand_scan_ident(mtd, 1);
327 dev_err(&pdev->dev, "Failed to scan nand\n");
331 if (pdata && pdata->ident_callback) {
332 pdata->ident_callback(pdev, chip, &pdata->partitions, &pdata->num_partitions);
335 ret = nand_scan_tail(mtd);
337 dev_err(&pdev->dev, "Failed to scan nand\n");
341 #ifdef CONFIG_MTD_PARTITIONS
342 #ifdef CONFIG_MTD_CMDLINE_PARTS
343 num_partitions = parse_mtd_partitions(mtd, part_probes,
346 if (num_partitions <= 0 && pdata) {
347 num_partitions = pdata->num_partitions;
348 partition_info = pdata->partitions;
351 if (num_partitions > 0)
352 ret = add_mtd_partitions(mtd, partition_info, num_partitions);
355 ret = add_mtd_device(mtd);
358 dev_err(&pdev->dev, "Failed to add mtd device\n");
359 goto err_nand_release;
362 dev_info(&pdev->dev, "Successfully registered JZ4740 NAND driver\n");
366 nand_release(&nand->mtd);
368 platform_set_drvdata(pdev, NULL);
369 gpio_free(pdata->busy_gpio);
373 release_mem_region(nand->mem->start, resource_size(nand->mem));
379 static void __devexit jz_nand_remove(struct platform_device *pdev)
381 struct jz_nand *nand = platform_get_drvdata(pdev);
383 nand_release(&nand->mtd);
387 release_mem_region(nand->mem->start, resource_size(nand->mem));
389 platform_set_drvdata(pdev, NULL);
393 struct platform_driver jz_nand_driver = {
394 .probe = jz_nand_probe,
395 .remove = __devexit_p(jz_nand_probe),
397 .name = "jz4740-nand",
398 .owner = THIS_MODULE,
402 static int __init jz_nand_init(void)
404 return platform_driver_register(&jz_nand_driver);
406 module_init(jz_nand_init);
408 static void __exit jz_nand_exit(void)
410 platform_driver_unregister(&jz_nand_driver);
412 module_exit(jz_nand_exit);
414 MODULE_LICENSE("GPL");
415 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
416 MODULE_DESCRIPTION("NAND controller driver for JZ4720/JZ4740 SoC");
417 MODULE_ALIAS("platform:jz4740-nand");
418 MODULE_ALIAS("platform:jz4720-nand");