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;
66 static inline struct jz_nand *mtd_to_jz_nand(struct mtd_info *mtd)
68 return container_of(mtd, struct jz_nand, mtd);
71 static void jz_nand_cmd_ctrl(struct mtd_info *mtd, int dat, unsigned int ctrl)
73 struct jz_nand *nand = mtd_to_jz_nand(mtd);
74 struct nand_chip *chip = mtd->priv;
77 if (ctrl & NAND_CTRL_CHANGE) {
78 BUG_ON((ctrl & NAND_ALE) && (ctrl & NAND_CLE));
80 chip->IO_ADDR_W = JZ_NAND_ADDR_ADDR;
81 else if (ctrl & NAND_CLE)
82 chip->IO_ADDR_W = JZ_NAND_CMD_ADDR;
84 chip->IO_ADDR_W = JZ_NAND_DATA_ADDR;
86 reg = readl(nand->base + JZ_REG_NAND_CTRL);
87 if ( ctrl & NAND_NCE )
88 reg |= JZ_NAND_CTRL_ASSERT_CHIP(0);
90 reg &= ~JZ_NAND_CTRL_ASSERT_CHIP(0);
91 writel(reg, nand->base + JZ_REG_NAND_CTRL);
93 if (dat != NAND_CMD_NONE)
94 writeb(dat, chip->IO_ADDR_W);
97 static int jz_nand_dev_ready(struct mtd_info *mtd)
99 struct jz_nand *nand = mtd_to_jz_nand(mtd);
100 return gpio_get_value_cansleep(nand->pdata->busy_gpio);
103 static void jz_nand_hwctl(struct mtd_info *mtd, int mode)
105 struct jz_nand *nand = mtd_to_jz_nand(mtd);
109 writel(0, nand->base + JZ_REG_NAND_IRQ_STAT);
110 reg = readl(nand->base + JZ_REG_NAND_ECC_CTRL);
112 reg |= JZ_NAND_ECC_CTRL_RESET;
113 reg |= JZ_NAND_ECC_CTRL_ENABLE;
114 reg |= JZ_NAND_ECC_CTRL_RS;
118 reg &= ~JZ_NAND_ECC_CTRL_ENCODING;
119 nand->is_reading = true;
122 reg |= JZ_NAND_ECC_CTRL_ENCODING;
123 nand->is_reading = false;
129 writel(reg, nand->base + JZ_REG_NAND_ECC_CTRL);
133 static int jz_nand_calculate_ecc_rs(struct mtd_info* mtd, const uint8_t* dat,
136 struct jz_nand *nand = mtd_to_jz_nand(mtd);
137 uint32_t reg, status;
139 static uint8_t all_ff_ecc[] = {0xcd, 0x9d, 0x90, 0x58, 0xf4, 0x8b, 0xff, 0xb7, 0x6f};
141 if (nand->is_reading)
145 status = readl(nand->base + JZ_REG_NAND_IRQ_STAT);
146 } while(!(status & JZ_NAND_STATUS_ENC_FINISH));
148 reg = readl(nand->base + JZ_REG_NAND_ECC_CTRL);
149 reg &= ~JZ_NAND_ECC_CTRL_ENABLE;
150 writel(reg, nand->base + JZ_REG_NAND_ECC_CTRL);
152 for (i = 0; i < 9; ++i) {
153 ecc_code[i] = readb(nand->base + JZ_REG_NAND_PAR0 + i);
156 /* If the written data is completly 0xff, we also want to write 0xff as
157 * ecc, otherwise we will get in trouble when doing subpage writes. */
158 if (memcmp(ecc_code, all_ff_ecc, 9) == 0) {
159 memset(ecc_code, 0xff, 9);
165 /*#define printkd printk*/
168 static void correct_data(uint8_t *dat, int index, int mask)
170 int offset = index & 0x7;
172 printkd("correct: ");
174 index += (index >> 3);
177 data |= dat[index+1] << 8;
179 printkd("0x%x -> ", data);
181 mask ^= (data >> offset) & 0x1ff;
182 data &= ~(0x1ff << offset);
183 data |= (mask << offset);
185 printkd("0x%x\n", data);
187 dat[index] = data & 0xff;
188 dat[index+1] = (data >> 8) & 0xff;
191 static int jz_nand_correct_ecc_rs(struct mtd_info* mtd, uint8_t *dat,
192 uint8_t *read_ecc, uint8_t *calc_ecc)
194 struct jz_nand *nand = mtd_to_jz_nand(mtd);
195 int i, error_count, index;
196 uint32_t reg, status, error;
202 for (i = 1; i < 9; ++i)
206 t &= dat[nand->chip.ecc.size / 2];
207 t &= dat[nand->chip.ecc.size - 1];
210 for (i = 1; i < nand->chip.ecc.size - 1; ++i)
217 for(i = 0; i < 9; ++i)
218 writeb(read_ecc[i], nand->base + JZ_REG_NAND_PAR0 + i);
220 reg = readl(nand->base + JZ_REG_NAND_ECC_CTRL);
221 reg |= JZ_NAND_ECC_CTRL_PAR_READY;
222 writel(reg, nand->base + JZ_REG_NAND_ECC_CTRL);
225 status = readl(nand->base + JZ_REG_NAND_IRQ_STAT);
226 } while (!(status & JZ_NAND_STATUS_DEC_FINISH));
228 reg = readl(nand->base + JZ_REG_NAND_ECC_CTRL);
229 reg &= ~JZ_NAND_ECC_CTRL_ENABLE;
230 writel(reg, nand->base + JZ_REG_NAND_ECC_CTRL);
232 if (status & JZ_NAND_STATUS_ERROR) {
233 if (status & JZ_NAND_STATUS_UNCOR_ERROR) {
234 printkd("uncorrectable ecc:");
235 for(i = 0; i < 9; ++i)
236 printkd(" 0x%x", read_ecc[i]);
238 printkd("uncorrectable data:");
239 for(i = 0; i < 32; ++i)
240 printkd(" 0x%x", dat[i]);
245 error_count = (status & JZ_NAND_STATUS_ERR_COUNT) >> 29;
247 printkd("error_count: %d %x\n", error_count, status);
249 for(i = 0; i < error_count; ++i) {
250 error = readl(nand->base + JZ_REG_NAND_ERR(i));
251 index = ((error >> 16) & 0x1ff) - 1;
252 if (index >= 0 && index < 512) {
253 correct_data(dat, index, error & 0x1ff);
265 #ifdef CONFIG_MTD_CMDLINE_PARTS
266 static const char *part_probes[] = {"cmdline", NULL};
269 static int __devinit jz_nand_probe(struct platform_device *pdev)
272 struct jz_nand *nand;
273 struct nand_chip *chip;
274 struct mtd_info *mtd;
275 struct jz_nand_platform_data *pdata = pdev->dev.platform_data;
276 #ifdef CONFIG_MTD_PARTITIONS
277 struct mtd_partition *partition_info;
278 int num_partitions = 0;
281 nand = kzalloc(sizeof(*nand), GFP_KERNEL);
283 dev_err(&pdev->dev, "Failed to allocate device structure.\n");
287 nand->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
289 dev_err(&pdev->dev, "Failed to get platform mmio memory\n");
294 nand->mem = request_mem_region(nand->mem->start, resource_size(nand->mem),
298 dev_err(&pdev->dev, "Failed to request mmio memory region\n");
303 nand->base = ioremap(nand->mem->start, resource_size(nand->mem));
306 dev_err(&pdev->dev, "Faild to ioremap mmio memory region\n");
308 goto err_release_mem;
311 if (pdata && gpio_is_valid(pdata->busy_gpio)) {
312 ret = gpio_request(pdata->busy_gpio, "jz nand busy line");
314 dev_err(&pdev->dev, "Failed to request busy gpio %d: %d\n",
315 pdata->busy_gpio, ret);
323 mtd->owner = THIS_MODULE;
324 mtd->name = "jz4740-nand";
326 chip->ecc.hwctl = jz_nand_hwctl;
328 chip->ecc.calculate = jz_nand_calculate_ecc_rs;
329 chip->ecc.correct = jz_nand_correct_ecc_rs;
330 chip->ecc.mode = NAND_ECC_HW_OOB_FIRST;
331 chip->ecc.size = 512;
334 chip->ecc.layout = pdata->ecc_layout;
336 chip->chip_delay = 50;
337 chip->cmd_ctrl = jz_nand_cmd_ctrl;
339 if (pdata && gpio_is_valid(pdata->busy_gpio))
340 chip->dev_ready = jz_nand_dev_ready;
342 chip->IO_ADDR_R = JZ_NAND_DATA_ADDR;
343 chip->IO_ADDR_W = JZ_NAND_DATA_ADDR;
346 platform_set_drvdata(pdev, nand);
348 ret = nand_scan_ident(mtd, 1);
350 dev_err(&pdev->dev, "Failed to scan nand\n");
354 if (pdata && pdata->ident_callback) {
355 pdata->ident_callback(pdev, chip, &pdata->partitions, &pdata->num_partitions);
358 ret = nand_scan_tail(mtd);
360 dev_err(&pdev->dev, "Failed to scan nand\n");
364 #ifdef CONFIG_MTD_PARTITIONS
365 #ifdef CONFIG_MTD_CMDLINE_PARTS
366 num_partitions = parse_mtd_partitions(mtd, part_probes,
369 if (num_partitions <= 0 && pdata) {
370 num_partitions = pdata->num_partitions;
371 partition_info = pdata->partitions;
374 if (num_partitions > 0)
375 ret = add_mtd_partitions(mtd, partition_info, num_partitions);
378 ret = add_mtd_device(mtd);
381 dev_err(&pdev->dev, "Failed to add mtd device\n");
382 goto err_nand_release;
385 dev_info(&pdev->dev, "Successfully registered JZ4740 NAND driver\n");
389 nand_release(&nand->mtd);
391 platform_set_drvdata(pdev, NULL);
392 gpio_free(pdata->busy_gpio);
396 release_mem_region(nand->mem->start, resource_size(nand->mem));
402 static void __devexit jz_nand_remove(struct platform_device *pdev)
404 struct jz_nand *nand = platform_get_drvdata(pdev);
406 nand_release(&nand->mtd);
410 release_mem_region(nand->mem->start, resource_size(nand->mem));
412 platform_set_drvdata(pdev, NULL);
416 struct platform_driver jz_nand_driver = {
417 .probe = jz_nand_probe,
418 .remove = __devexit_p(jz_nand_probe),
420 .name = "jz4740-nand",
421 .owner = THIS_MODULE,
425 static int __init jz_nand_init(void)
427 return platform_driver_register(&jz_nand_driver);
429 module_init(jz_nand_init);
431 static void __exit jz_nand_exit(void)
433 platform_driver_unregister(&jz_nand_driver);
435 module_exit(jz_nand_exit);
437 MODULE_LICENSE("GPL");
438 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
439 MODULE_DESCRIPTION("NAND controller driver for JZ4720/JZ4740 SoC");
440 MODULE_ALIAS("platform:jz4740-nand");
441 MODULE_ALIAS("platform:jz4720-nand");