2 * drivers/mtd/nand/sharpsl.c
4 * Copyright (C) 2004 Richard Purdie
5 * Copyright (C) 2008 Dmitry Baryshkov
7 * Based on Sharp's NAND driver sharp_sl.c
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
15 #include <linux/genhd.h>
16 #include <linux/slab.h>
17 #include <linux/module.h>
18 #include <linux/delay.h>
19 #include <linux/mtd/mtd.h>
20 #include <linux/mtd/rawnand.h>
21 #include <linux/mtd/nand_ecc.h>
22 #include <linux/mtd/partitions.h>
23 #include <linux/mtd/sharpsl.h>
24 #include <linux/interrupt.h>
25 #include <linux/platform_device.h>
28 #include <mach/hardware.h>
29 #include <asm/mach-types.h>
32 struct nand_chip chip;
37 static inline struct sharpsl_nand *mtd_to_sharpsl(struct mtd_info *mtd)
39 return container_of(mtd_to_nand(mtd), struct sharpsl_nand, chip);
43 #define ECCLPLB 0x00 /* line parity 7 - 0 bit */
44 #define ECCLPUB 0x04 /* line parity 15 - 8 bit */
45 #define ECCCP 0x08 /* column parity 5 - 0 bit */
46 #define ECCCNTR 0x0C /* ECC byte counter */
47 #define ECCCLRR 0x10 /* cleare ECC */
48 #define FLASHIO 0x14 /* Flash I/O */
49 #define FLASHCTL 0x18 /* Flash Control */
51 /* Flash control bit */
52 #define FLRYBY (1 << 5)
53 #define FLCE1 (1 << 4)
55 #define FLALE (1 << 2)
56 #define FLCLE (1 << 1)
57 #define FLCE0 (1 << 0)
60 * hardware specific access to control-lines
62 * NAND_CNE: bit 0 -> ! bit 0 & 4
63 * NAND_CLE: bit 1 -> bit 1
64 * NAND_ALE: bit 2 -> bit 2
67 static void sharpsl_nand_hwcontrol(struct mtd_info *mtd, int cmd,
70 struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
71 struct nand_chip *chip = mtd_to_nand(mtd);
73 if (ctrl & NAND_CTRL_CHANGE) {
74 unsigned char bits = ctrl & 0x07;
76 bits |= (ctrl & 0x01) << 4;
80 writeb((readb(sharpsl->io + FLASHCTL) & ~0x17) | bits, sharpsl->io + FLASHCTL);
83 if (cmd != NAND_CMD_NONE)
84 writeb(cmd, chip->IO_ADDR_W);
87 static int sharpsl_nand_dev_ready(struct mtd_info *mtd)
89 struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
90 return !((readb(sharpsl->io + FLASHCTL) & FLRYBY) == 0);
93 static void sharpsl_nand_enable_hwecc(struct mtd_info *mtd, int mode)
95 struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
96 writeb(0, sharpsl->io + ECCCLRR);
99 static int sharpsl_nand_calculate_ecc(struct mtd_info *mtd, const u_char * dat, u_char * ecc_code)
101 struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
102 ecc_code[0] = ~readb(sharpsl->io + ECCLPUB);
103 ecc_code[1] = ~readb(sharpsl->io + ECCLPLB);
104 ecc_code[2] = (~readb(sharpsl->io + ECCCP) << 2) | 0x03;
105 return readb(sharpsl->io + ECCCNTR) != 0;
109 * Main initialization routine
111 static int sharpsl_nand_probe(struct platform_device *pdev)
113 struct nand_chip *this;
114 struct mtd_info *mtd;
117 struct sharpsl_nand *sharpsl;
118 struct sharpsl_nand_platform_data *data = dev_get_platdata(&pdev->dev);
121 dev_err(&pdev->dev, "no platform data!\n");
125 /* Allocate memory for MTD device structure and private data */
126 sharpsl = kzalloc(sizeof(struct sharpsl_nand), GFP_KERNEL);
130 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
132 dev_err(&pdev->dev, "no io memory resource defined!\n");
137 /* map physical address */
138 sharpsl->io = ioremap(r->start, resource_size(r));
140 dev_err(&pdev->dev, "ioremap to access Sharp SL NAND chip failed\n");
145 /* Get pointer to private data */
146 this = (struct nand_chip *)(&sharpsl->chip);
148 /* Link the private data with the MTD structure */
149 mtd = nand_to_mtd(this);
150 mtd->dev.parent = &pdev->dev;
151 mtd_set_ooblayout(mtd, data->ecc_layout);
153 platform_set_drvdata(pdev, sharpsl);
158 writeb(readb(sharpsl->io + FLASHCTL) | FLWP, sharpsl->io + FLASHCTL);
160 /* Set address of NAND IO lines */
161 this->IO_ADDR_R = sharpsl->io + FLASHIO;
162 this->IO_ADDR_W = sharpsl->io + FLASHIO;
163 /* Set address of hardware control function */
164 this->cmd_ctrl = sharpsl_nand_hwcontrol;
165 this->dev_ready = sharpsl_nand_dev_ready;
166 /* 15 us command delay time */
167 this->chip_delay = 15;
168 /* set eccmode using hardware ECC */
169 this->ecc.mode = NAND_ECC_HW;
170 this->ecc.size = 256;
172 this->ecc.strength = 1;
173 this->badblock_pattern = data->badblock_pattern;
174 this->ecc.hwctl = sharpsl_nand_enable_hwecc;
175 this->ecc.calculate = sharpsl_nand_calculate_ecc;
176 this->ecc.correct = nand_correct_data;
178 /* Scan to find existence of the device */
179 err = nand_scan(mtd, 1);
183 /* Register the partitions */
184 mtd->name = "sharpsl-nand";
186 err = mtd_device_parse_register(mtd, NULL, NULL,
187 data->partitions, data->nr_partitions);
198 iounmap(sharpsl->io);
208 static int sharpsl_nand_remove(struct platform_device *pdev)
210 struct sharpsl_nand *sharpsl = platform_get_drvdata(pdev);
212 /* Release resources, unregister device */
213 nand_release(nand_to_mtd(&sharpsl->chip));
215 iounmap(sharpsl->io);
217 /* Free the MTD device structure */
223 static struct platform_driver sharpsl_nand_driver = {
225 .name = "sharpsl-nand",
227 .probe = sharpsl_nand_probe,
228 .remove = sharpsl_nand_remove,
231 module_platform_driver(sharpsl_nand_driver);
233 MODULE_LICENSE("GPL");
234 MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
235 MODULE_DESCRIPTION("Device specific logic for NAND flash on Sharp SL-C7xx Series");