1 From c09d9002953c1182843050df1d4c639dea4af7f6 Mon Sep 17 00:00:00 2001
2 From: Lars-Peter Clausen <lars@metafoo.de>
3 Date: Sat, 17 Jul 2010 11:15:29 +0000
4 Subject: [PATCH] MTD: Nand: Add JZ4740 NAND driver
6 Add support for the NAND controller on JZ4740 SoCs.
8 Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
9 Cc: David Woodhouse <dwmw2@infradead.org>
10 Cc: linux-mtd@lists.infradead.org
11 Cc: linux-mips@linux-mips.org
12 Cc: linux-kernel@vger.kernel.org
13 Patchwork: https://patchwork.linux-mips.org/patch/1470/
14 Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
16 arch/mips/include/asm/mach-jz4740/jz4740_nand.h | 34 ++
17 drivers/mtd/nand/Kconfig | 6 +
18 drivers/mtd/nand/Makefile | 1 +
19 drivers/mtd/nand/jz4740_nand.c | 516 +++++++++++++++++++++++
20 4 files changed, 557 insertions(+), 0 deletions(-)
21 create mode 100644 arch/mips/include/asm/mach-jz4740/jz4740_nand.h
22 create mode 100644 drivers/mtd/nand/jz4740_nand.c
25 +++ b/arch/mips/include/asm/mach-jz4740/jz4740_nand.h
28 + * Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
29 + * JZ4740 SoC NAND controller driver
31 + * This program is free software; you can redistribute it and/or modify it
32 + * under the terms of the GNU General Public License as published by the
33 + * Free Software Foundation; either version 2 of the License, or (at your
34 + * option) any later version.
36 + * You should have received a copy of the GNU General Public License along
37 + * with this program; if not, write to the Free Software Foundation, Inc.,
38 + * 675 Mass Ave, Cambridge, MA 02139, USA.
42 +#ifndef __ASM_MACH_JZ4740_JZ4740_NAND_H__
43 +#define __ASM_MACH_JZ4740_JZ4740_NAND_H__
45 +#include <linux/mtd/nand.h>
46 +#include <linux/mtd/partitions.h>
48 +struct jz_nand_platform_data {
50 + struct mtd_partition *partitions;
52 + struct nand_ecclayout *ecc_layout;
54 + unsigned int busy_gpio;
56 + void (*ident_callback)(struct platform_device *, struct nand_chip *,
57 + struct mtd_partition **, int *num_partitions);
61 --- a/drivers/mtd/nand/Kconfig
62 +++ b/drivers/mtd/nand/Kconfig
63 @@ -526,4 +526,10 @@ config MTD_NAND_NUC900
64 This enables the driver for the NAND Flash on evaluation board based
67 +config MTD_NAND_JZ4740
68 + tristate "Support for JZ4740 SoC NAND controller"
69 + depends on MACH_JZ4740
71 + Enables support for NAND Flash on JZ4740 SoC based boards.
74 --- a/drivers/mtd/nand/Makefile
75 +++ b/drivers/mtd/nand/Makefile
76 @@ -46,5 +46,6 @@ obj-$(CONFIG_MTD_NAND_NOMADIK) += nomad
77 obj-$(CONFIG_MTD_NAND_BCM_UMI) += bcm_umi_nand.o nand_bcm_umi.o
78 obj-$(CONFIG_MTD_NAND_MPC5121_NFC) += mpc5121_nfc.o
79 obj-$(CONFIG_MTD_NAND_RICOH) += r852.o
80 +obj-$(CONFIG_MTD_NAND_JZ4740) += jz4740_nand.o
82 nand-objs := nand_base.o nand_bbt.o
84 +++ b/drivers/mtd/nand/jz4740_nand.c
87 + * Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
88 + * JZ4740 SoC NAND controller driver
90 + * This program is free software; you can redistribute it and/or modify it
91 + * under the terms of the GNU General Public License as published by the
92 + * Free Software Foundation; either version 2 of the License, or (at your
93 + * option) any later version.
95 + * You should have received a copy of the GNU General Public License along
96 + * with this program; if not, write to the Free Software Foundation, Inc.,
97 + * 675 Mass Ave, Cambridge, MA 02139, USA.
101 +#include <linux/ioport.h>
102 +#include <linux/kernel.h>
103 +#include <linux/module.h>
104 +#include <linux/platform_device.h>
105 +#include <linux/slab.h>
107 +#include <linux/mtd/mtd.h>
108 +#include <linux/mtd/nand.h>
109 +#include <linux/mtd/partitions.h>
111 +#include <linux/gpio.h>
113 +#include <asm/mach-jz4740/jz4740_nand.h>
115 +#define JZ_REG_NAND_CTRL 0x50
116 +#define JZ_REG_NAND_ECC_CTRL 0x100
117 +#define JZ_REG_NAND_DATA 0x104
118 +#define JZ_REG_NAND_PAR0 0x108
119 +#define JZ_REG_NAND_PAR1 0x10C
120 +#define JZ_REG_NAND_PAR2 0x110
121 +#define JZ_REG_NAND_IRQ_STAT 0x114
122 +#define JZ_REG_NAND_IRQ_CTRL 0x118
123 +#define JZ_REG_NAND_ERR(x) (0x11C + ((x) << 2))
125 +#define JZ_NAND_ECC_CTRL_PAR_READY BIT(4)
126 +#define JZ_NAND_ECC_CTRL_ENCODING BIT(3)
127 +#define JZ_NAND_ECC_CTRL_RS BIT(2)
128 +#define JZ_NAND_ECC_CTRL_RESET BIT(1)
129 +#define JZ_NAND_ECC_CTRL_ENABLE BIT(0)
131 +#define JZ_NAND_STATUS_ERR_COUNT (BIT(31) | BIT(30) | BIT(29))
132 +#define JZ_NAND_STATUS_PAD_FINISH BIT(4)
133 +#define JZ_NAND_STATUS_DEC_FINISH BIT(3)
134 +#define JZ_NAND_STATUS_ENC_FINISH BIT(2)
135 +#define JZ_NAND_STATUS_UNCOR_ERROR BIT(1)
136 +#define JZ_NAND_STATUS_ERROR BIT(0)
138 +#define JZ_NAND_CTRL_ENABLE_CHIP(x) BIT((x) << 1)
139 +#define JZ_NAND_CTRL_ASSERT_CHIP(x) BIT(((x) << 1) + 1)
141 +#define JZ_NAND_MEM_ADDR_OFFSET 0x10000
142 +#define JZ_NAND_MEM_CMD_OFFSET 0x08000
145 + struct mtd_info mtd;
146 + struct nand_chip chip;
147 + void __iomem *base;
148 + struct resource *mem;
150 + void __iomem *bank_base;
151 + struct resource *bank_mem;
153 + struct jz_nand_platform_data *pdata;
157 +static inline struct jz_nand *mtd_to_jz_nand(struct mtd_info *mtd)
159 + return container_of(mtd, struct jz_nand, mtd);
162 +static void jz_nand_cmd_ctrl(struct mtd_info *mtd, int dat, unsigned int ctrl)
164 + struct jz_nand *nand = mtd_to_jz_nand(mtd);
165 + struct nand_chip *chip = mtd->priv;
168 + if (ctrl & NAND_CTRL_CHANGE) {
169 + BUG_ON((ctrl & NAND_ALE) && (ctrl & NAND_CLE));
170 + if (ctrl & NAND_ALE)
171 + chip->IO_ADDR_W = nand->bank_base + JZ_NAND_MEM_ADDR_OFFSET;
172 + else if (ctrl & NAND_CLE)
173 + chip->IO_ADDR_W = nand->bank_base + JZ_NAND_MEM_CMD_OFFSET;
175 + chip->IO_ADDR_W = nand->bank_base;
177 + reg = readl(nand->base + JZ_REG_NAND_CTRL);
178 + if (ctrl & NAND_NCE)
179 + reg |= JZ_NAND_CTRL_ASSERT_CHIP(0);
181 + reg &= ~JZ_NAND_CTRL_ASSERT_CHIP(0);
182 + writel(reg, nand->base + JZ_REG_NAND_CTRL);
184 + if (dat != NAND_CMD_NONE)
185 + writeb(dat, chip->IO_ADDR_W);
188 +static int jz_nand_dev_ready(struct mtd_info *mtd)
190 + struct jz_nand *nand = mtd_to_jz_nand(mtd);
191 + return gpio_get_value_cansleep(nand->pdata->busy_gpio);
194 +static void jz_nand_hwctl(struct mtd_info *mtd, int mode)
196 + struct jz_nand *nand = mtd_to_jz_nand(mtd);
199 + writel(0, nand->base + JZ_REG_NAND_IRQ_STAT);
200 + reg = readl(nand->base + JZ_REG_NAND_ECC_CTRL);
202 + reg |= JZ_NAND_ECC_CTRL_RESET;
203 + reg |= JZ_NAND_ECC_CTRL_ENABLE;
204 + reg |= JZ_NAND_ECC_CTRL_RS;
207 + case NAND_ECC_READ:
208 + reg &= ~JZ_NAND_ECC_CTRL_ENCODING;
209 + nand->is_reading = true;
211 + case NAND_ECC_WRITE:
212 + reg |= JZ_NAND_ECC_CTRL_ENCODING;
213 + nand->is_reading = false;
219 + writel(reg, nand->base + JZ_REG_NAND_ECC_CTRL);
222 +static int jz_nand_calculate_ecc_rs(struct mtd_info *mtd, const uint8_t *dat,
225 + struct jz_nand *nand = mtd_to_jz_nand(mtd);
226 + uint32_t reg, status;
228 + unsigned int timeout = 1000;
229 + static uint8_t empty_block_ecc[] = {0xcd, 0x9d, 0x90, 0x58, 0xf4,
230 + 0x8b, 0xff, 0xb7, 0x6f};
232 + if (nand->is_reading)
236 + status = readl(nand->base + JZ_REG_NAND_IRQ_STAT);
237 + } while (!(status & JZ_NAND_STATUS_ENC_FINISH) && --timeout);
242 + reg = readl(nand->base + JZ_REG_NAND_ECC_CTRL);
243 + reg &= ~JZ_NAND_ECC_CTRL_ENABLE;
244 + writel(reg, nand->base + JZ_REG_NAND_ECC_CTRL);
246 + for (i = 0; i < 9; ++i)
247 + ecc_code[i] = readb(nand->base + JZ_REG_NAND_PAR0 + i);
249 + /* If the written data is completly 0xff, we also want to write 0xff as
250 + * ecc, otherwise we will get in trouble when doing subpage writes. */
251 + if (memcmp(ecc_code, empty_block_ecc, 9) == 0)
252 + memset(ecc_code, 0xff, 9);
257 +static void jz_nand_correct_data(uint8_t *dat, int index, int mask)
259 + int offset = index & 0x7;
262 + index += (index >> 3);
265 + data |= dat[index+1] << 8;
267 + mask ^= (data >> offset) & 0x1ff;
268 + data &= ~(0x1ff << offset);
269 + data |= (mask << offset);
271 + dat[index] = data & 0xff;
272 + dat[index+1] = (data >> 8) & 0xff;
275 +static int jz_nand_correct_ecc_rs(struct mtd_info *mtd, uint8_t *dat,
276 + uint8_t *read_ecc, uint8_t *calc_ecc)
278 + struct jz_nand *nand = mtd_to_jz_nand(mtd);
279 + int i, error_count, index;
280 + uint32_t reg, status, error;
282 + unsigned int timeout = 1000;
287 + for (i = 1; i < 9; ++i)
291 + t &= dat[nand->chip.ecc.size / 2];
292 + t &= dat[nand->chip.ecc.size - 1];
295 + for (i = 1; i < nand->chip.ecc.size - 1; ++i)
302 + for (i = 0; i < 9; ++i)
303 + writeb(read_ecc[i], nand->base + JZ_REG_NAND_PAR0 + i);
305 + reg = readl(nand->base + JZ_REG_NAND_ECC_CTRL);
306 + reg |= JZ_NAND_ECC_CTRL_PAR_READY;
307 + writel(reg, nand->base + JZ_REG_NAND_ECC_CTRL);
310 + status = readl(nand->base + JZ_REG_NAND_IRQ_STAT);
311 + } while (!(status & JZ_NAND_STATUS_DEC_FINISH) && --timeout);
316 + reg = readl(nand->base + JZ_REG_NAND_ECC_CTRL);
317 + reg &= ~JZ_NAND_ECC_CTRL_ENABLE;
318 + writel(reg, nand->base + JZ_REG_NAND_ECC_CTRL);
320 + if (status & JZ_NAND_STATUS_ERROR) {
321 + if (status & JZ_NAND_STATUS_UNCOR_ERROR)
324 + error_count = (status & JZ_NAND_STATUS_ERR_COUNT) >> 29;
326 + for (i = 0; i < error_count; ++i) {
327 + error = readl(nand->base + JZ_REG_NAND_ERR(i));
328 + index = ((error >> 16) & 0x1ff) - 1;
329 + if (index >= 0 && index < 512)
330 + jz_nand_correct_data(dat, index, error & 0x1ff);
333 + return error_count;
340 +/* Copy paste of nand_read_page_hwecc_oob_first except for different eccpos
341 + * handling. The ecc area is for 4k chips 72 bytes long and thus does not fit
342 + * into the eccpos array. */
343 +static int jz_nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
344 + struct nand_chip *chip, uint8_t *buf, int page)
346 + int i, eccsize = chip->ecc.size;
347 + int eccbytes = chip->ecc.bytes;
348 + int eccsteps = chip->ecc.steps;
350 + unsigned int ecc_offset = chip->page_shift;
352 + /* Read the OOB area first */
353 + chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
354 + chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
355 + chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
357 + for (i = ecc_offset; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
360 + chip->ecc.hwctl(mtd, NAND_ECC_READ);
361 + chip->read_buf(mtd, p, eccsize);
363 + stat = chip->ecc.correct(mtd, p, &chip->oob_poi[i], NULL);
365 + mtd->ecc_stats.failed++;
367 + mtd->ecc_stats.corrected += stat;
372 +/* Copy-and-paste of nand_write_page_hwecc with different eccpos handling. */
373 +static void jz_nand_write_page_hwecc(struct mtd_info *mtd,
374 + struct nand_chip *chip, const uint8_t *buf)
376 + int i, eccsize = chip->ecc.size;
377 + int eccbytes = chip->ecc.bytes;
378 + int eccsteps = chip->ecc.steps;
379 + const uint8_t *p = buf;
380 + unsigned int ecc_offset = chip->page_shift;
382 + for (i = ecc_offset; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
383 + chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
384 + chip->write_buf(mtd, p, eccsize);
385 + chip->ecc.calculate(mtd, p, &chip->oob_poi[i]);
388 + chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
391 +#ifdef CONFIG_MTD_CMDLINE_PARTS
392 +static const char *part_probes[] = {"cmdline", NULL};
395 +static int jz_nand_ioremap_resource(struct platform_device *pdev,
396 + const char *name, struct resource **res, void __iomem **base)
400 + *res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
402 + dev_err(&pdev->dev, "Failed to get platform %s memory\n", name);
407 + *res = request_mem_region((*res)->start, resource_size(*res),
410 + dev_err(&pdev->dev, "Failed to request %s memory region\n", name);
415 + *base = ioremap((*res)->start, resource_size(*res));
417 + dev_err(&pdev->dev, "Failed to ioremap %s memory region\n", name);
419 + goto err_release_mem;
425 + release_mem_region((*res)->start, resource_size(*res));
432 +static int __devinit jz_nand_probe(struct platform_device *pdev)
435 + struct jz_nand *nand;
436 + struct nand_chip *chip;
437 + struct mtd_info *mtd;
438 + struct jz_nand_platform_data *pdata = pdev->dev.platform_data;
439 +#ifdef CONFIG_MTD_PARTITIONS
440 + struct mtd_partition *partition_info;
441 + int num_partitions = 0;
444 + nand = kzalloc(sizeof(*nand), GFP_KERNEL);
446 + dev_err(&pdev->dev, "Failed to allocate device structure.\n");
450 + ret = jz_nand_ioremap_resource(pdev, "mmio", &nand->mem, &nand->base);
453 + ret = jz_nand_ioremap_resource(pdev, "bank", &nand->bank_mem,
456 + goto err_iounmap_mmio;
458 + if (pdata && gpio_is_valid(pdata->busy_gpio)) {
459 + ret = gpio_request(pdata->busy_gpio, "NAND busy pin");
461 + dev_err(&pdev->dev,
462 + "Failed to request busy gpio %d: %d\n",
463 + pdata->busy_gpio, ret);
464 + goto err_iounmap_mem;
469 + chip = &nand->chip;
471 + mtd->owner = THIS_MODULE;
472 + mtd->name = "jz4740-nand";
474 + chip->ecc.hwctl = jz_nand_hwctl;
475 + chip->ecc.calculate = jz_nand_calculate_ecc_rs;
476 + chip->ecc.correct = jz_nand_correct_ecc_rs;
477 + chip->ecc.mode = NAND_ECC_HW_OOB_FIRST;
478 + chip->ecc.size = 512;
479 + chip->ecc.bytes = 9;
481 + chip->ecc.read_page = jz_nand_read_page_hwecc_oob_first;
482 + chip->ecc.write_page = jz_nand_write_page_hwecc;
485 + chip->ecc.layout = pdata->ecc_layout;
487 + chip->chip_delay = 50;
488 + chip->cmd_ctrl = jz_nand_cmd_ctrl;
490 + if (pdata && gpio_is_valid(pdata->busy_gpio))
491 + chip->dev_ready = jz_nand_dev_ready;
493 + chip->IO_ADDR_R = nand->bank_base;
494 + chip->IO_ADDR_W = nand->bank_base;
496 + nand->pdata = pdata;
497 + platform_set_drvdata(pdev, nand);
499 + writel(JZ_NAND_CTRL_ENABLE_CHIP(0), nand->base + JZ_REG_NAND_CTRL);
501 + ret = nand_scan_ident(mtd, 1, NULL);
503 + dev_err(&pdev->dev, "Failed to scan nand\n");
504 + goto err_gpio_free;
507 + if (pdata && pdata->ident_callback) {
508 + pdata->ident_callback(pdev, chip, &pdata->partitions,
509 + &pdata->num_partitions);
512 + ret = nand_scan_tail(mtd);
514 + dev_err(&pdev->dev, "Failed to scan nand\n");
515 + goto err_gpio_free;
518 +#ifdef CONFIG_MTD_PARTITIONS
519 +#ifdef CONFIG_MTD_CMDLINE_PARTS
520 + num_partitions = parse_mtd_partitions(mtd, part_probes,
521 + &partition_info, 0);
523 + if (num_partitions <= 0 && pdata) {
524 + num_partitions = pdata->num_partitions;
525 + partition_info = pdata->partitions;
528 + if (num_partitions > 0)
529 + ret = add_mtd_partitions(mtd, partition_info, num_partitions);
532 + ret = add_mtd_device(mtd);
535 + dev_err(&pdev->dev, "Failed to add mtd device\n");
536 + goto err_nand_release;
539 + dev_info(&pdev->dev, "Successfully registered JZ4740 NAND driver\n");
544 + nand_release(&nand->mtd);
546 + platform_set_drvdata(pdev, NULL);
547 + gpio_free(pdata->busy_gpio);
549 + iounmap(nand->bank_base);
551 + iounmap(nand->base);
557 +static int __devexit jz_nand_remove(struct platform_device *pdev)
559 + struct jz_nand *nand = platform_get_drvdata(pdev);
561 + nand_release(&nand->mtd);
563 + /* Deassert and disable all chips */
564 + writel(0, nand->base + JZ_REG_NAND_CTRL);
566 + iounmap(nand->bank_base);
567 + release_mem_region(nand->bank_mem->start, resource_size(nand->bank_mem));
568 + iounmap(nand->base);
569 + release_mem_region(nand->mem->start, resource_size(nand->mem));
571 + platform_set_drvdata(pdev, NULL);
577 +struct platform_driver jz_nand_driver = {
578 + .probe = jz_nand_probe,
579 + .remove = __devexit_p(jz_nand_remove),
581 + .name = "jz4740-nand",
582 + .owner = THIS_MODULE,
586 +static int __init jz_nand_init(void)
588 + return platform_driver_register(&jz_nand_driver);
590 +module_init(jz_nand_init);
592 +static void __exit jz_nand_exit(void)
594 + platform_driver_unregister(&jz_nand_driver);
596 +module_exit(jz_nand_exit);
598 +MODULE_LICENSE("GPL");
599 +MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
600 +MODULE_DESCRIPTION("NAND controller driver for JZ4740 SoC");
601 +MODULE_ALIAS("platform:jz4740-nand");