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/nand.h>
21 #include <linux/mtd/nand_ecc.h>
22 #include <linux/mtd/partitions.h>
23 #include <linux/interrupt.h>
24 #include <linux/platform_device.h>
27 #include <mach/hardware.h>
28 #include <asm/mach-types.h>
32 struct nand_chip chip;
37 #define mtd_to_sharpsl(_mtd) container_of(_mtd, struct sharpsl_nand, mtd)
40 #define ECCLPLB 0x00 /* line parity 7 - 0 bit */
41 #define ECCLPUB 0x04 /* line parity 15 - 8 bit */
42 #define ECCCP 0x08 /* column parity 5 - 0 bit */
43 #define ECCCNTR 0x0C /* ECC byte counter */
44 #define ECCCLRR 0x10 /* cleare ECC */
45 #define FLASHIO 0x14 /* Flash I/O */
46 #define FLASHCTL 0x18 /* Flash Control */
48 /* Flash control bit */
49 #define FLRYBY (1 << 5)
50 #define FLCE1 (1 << 4)
52 #define FLALE (1 << 2)
53 #define FLCLE (1 << 1)
54 #define FLCE0 (1 << 0)
56 #ifdef CONFIG_MTD_PARTITIONS
58 * Define partitions for flash device
60 #define DEFAULT_NUM_PARTITIONS 3
62 static struct mtd_partition sharpsl_nand_default_partition_info[] = {
64 .name = "System Area",
66 .size = 7 * 1024 * 1024,
69 .name = "Root Filesystem",
70 .offset = 7 * 1024 * 1024,
71 .size = 30 * 1024 * 1024,
74 .name = "Home Filesystem",
75 .offset = MTDPART_OFS_APPEND,
76 .size = MTDPART_SIZ_FULL,
82 * hardware specific access to control-lines
84 * NAND_CNE: bit 0 -> ! bit 0 & 4
85 * NAND_CLE: bit 1 -> bit 1
86 * NAND_ALE: bit 2 -> bit 2
89 static void sharpsl_nand_hwcontrol(struct mtd_info *mtd, int cmd,
92 struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
93 struct nand_chip *chip = mtd->priv;
95 if (ctrl & NAND_CTRL_CHANGE) {
96 unsigned char bits = ctrl & 0x07;
98 bits |= (ctrl & 0x01) << 4;
102 writeb((readb(sharpsl->io + FLASHCTL) & ~0x17) | bits, sharpsl->io + FLASHCTL);
105 if (cmd != NAND_CMD_NONE)
106 writeb(cmd, chip->IO_ADDR_W);
109 static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
111 static struct nand_bbt_descr sharpsl_bbt = {
115 .pattern = scan_ff_pattern
118 static struct nand_bbt_descr sharpsl_akita_bbt = {
122 .pattern = scan_ff_pattern
125 static struct nand_ecclayout akita_oobinfo = {
128 0x5, 0x1, 0x2, 0x3, 0x6, 0x7, 0x15, 0x11,
129 0x12, 0x13, 0x16, 0x17, 0x25, 0x21, 0x22, 0x23,
130 0x26, 0x27, 0x35, 0x31, 0x32, 0x33, 0x36, 0x37},
131 .oobfree = {{0x08, 0x09}}
134 static int sharpsl_nand_dev_ready(struct mtd_info *mtd)
136 struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
137 return !((readb(sharpsl->io + FLASHCTL) & FLRYBY) == 0);
140 static void sharpsl_nand_enable_hwecc(struct mtd_info *mtd, int mode)
142 struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
143 writeb(0, sharpsl->io + ECCCLRR);
146 static int sharpsl_nand_calculate_ecc(struct mtd_info *mtd, const u_char * dat, u_char * ecc_code)
148 struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
149 ecc_code[0] = ~readb(sharpsl->io + ECCLPUB);
150 ecc_code[1] = ~readb(sharpsl->io + ECCLPLB);
151 ecc_code[2] = (~readb(sharpsl->io + ECCCP) << 2) | 0x03;
152 return readb(sharpsl->io + ECCCNTR) != 0;
155 #ifdef CONFIG_MTD_PARTITIONS
156 static const char *part_probes[] = { "cmdlinepart", NULL };
160 * Main initialization routine
162 static int __devinit sharpsl_nand_probe(struct platform_device *pdev)
164 struct nand_chip *this;
165 #ifdef CONFIG_MTD_PARTITIONS
166 struct mtd_partition *sharpsl_partition_info;
171 struct sharpsl_nand *sharpsl;
173 /* Allocate memory for MTD device structure and private data */
174 sharpsl = kzalloc(sizeof(struct sharpsl_nand), GFP_KERNEL);
176 printk("Unable to allocate SharpSL NAND MTD device structure.\n");
180 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
182 dev_err(&pdev->dev, "no io memory resource defined!\n");
187 /* map physical address */
188 sharpsl->io = ioremap(r->start, resource_size(r));
190 printk("ioremap to access Sharp SL NAND chip failed\n");
195 /* Get pointer to private data */
196 this = (struct nand_chip *)(&sharpsl->chip);
198 /* Link the private data with the MTD structure */
199 sharpsl->mtd.priv = this;
200 sharpsl->mtd.owner = THIS_MODULE;
202 platform_set_drvdata(pdev, sharpsl);
207 writeb(readb(sharpsl->io + FLASHCTL) | FLWP, sharpsl->io + FLASHCTL);
209 /* Set address of NAND IO lines */
210 this->IO_ADDR_R = sharpsl->io + FLASHIO;
211 this->IO_ADDR_W = sharpsl->io + FLASHIO;
212 /* Set address of hardware control function */
213 this->cmd_ctrl = sharpsl_nand_hwcontrol;
214 this->dev_ready = sharpsl_nand_dev_ready;
215 /* 15 us command delay time */
216 this->chip_delay = 15;
217 /* set eccmode using hardware ECC */
218 this->ecc.mode = NAND_ECC_HW;
219 this->ecc.size = 256;
221 this->badblock_pattern = &sharpsl_bbt;
222 if (machine_is_akita() || machine_is_borzoi()) {
223 this->badblock_pattern = &sharpsl_akita_bbt;
224 this->ecc.layout = &akita_oobinfo;
226 this->ecc.hwctl = sharpsl_nand_enable_hwecc;
227 this->ecc.calculate = sharpsl_nand_calculate_ecc;
228 this->ecc.correct = nand_correct_data;
230 /* Scan to find existence of the device */
231 err = nand_scan(&sharpsl->mtd, 1);
235 /* Register the partitions */
236 sharpsl->mtd.name = "sharpsl-nand";
237 #ifdef CONFIG_MTD_PARTITIONS
238 nr_partitions = parse_mtd_partitions(&sharpsl->mtd, part_probes, &sharpsl_partition_info, 0);
240 if (nr_partitions <= 0) {
241 nr_partitions = ARRAY_SIZE(sharpsl_nand_default_partition_info);
242 sharpsl_partition_info = sharpsl_nand_default_partition_info;
243 if (machine_is_poodle()) {
244 sharpsl_partition_info[1].size = 22 * 1024 * 1024;
245 } else if (machine_is_corgi() || machine_is_shepherd()) {
246 sharpsl_partition_info[1].size = 25 * 1024 * 1024;
247 } else if (machine_is_husky()) {
248 sharpsl_partition_info[1].size = 53 * 1024 * 1024;
249 } else if (machine_is_spitz()) {
250 sharpsl_partition_info[1].size = 5 * 1024 * 1024;
251 } else if (machine_is_akita()) {
252 sharpsl_partition_info[1].size = 58 * 1024 * 1024;
253 } else if (machine_is_borzoi()) {
254 sharpsl_partition_info[1].size = 32 * 1024 * 1024;
258 err = add_mtd_partitions(&sharpsl->mtd, sharpsl_partition_info, nr_partitions);
260 err = add_mtd_device(&sharpsl->mtd);
269 nand_release(&sharpsl->mtd);
272 platform_set_drvdata(pdev, NULL);
273 iounmap(sharpsl->io);
283 static int __devexit sharpsl_nand_remove(struct platform_device *pdev)
285 struct sharpsl_nand *sharpsl = platform_get_drvdata(pdev);
287 /* Release resources, unregister device */
288 nand_release(&sharpsl->mtd);
290 platform_set_drvdata(pdev, NULL);
292 iounmap(sharpsl->io);
294 /* Free the MTD device structure */
300 static struct platform_driver sharpsl_nand_driver = {
302 .name = "sharpsl-nand",
303 .owner = THIS_MODULE,
305 .probe = sharpsl_nand_probe,
306 .remove = __devexit_p(sharpsl_nand_remove),
309 static struct resource sharpsl_nand_resources[] = {
313 .flags = IORESOURCE_MEM,
317 static struct platform_device sharpsl_nand_device = {
318 .name = "sharpsl-nand",
320 .resource = sharpsl_nand_resources,
321 .num_resources = ARRAY_SIZE(sharpsl_nand_resources),
324 static int __init sharpsl_nand_init(void)
326 platform_device_register(&sharpsl_nand_device);
327 return platform_driver_register(&sharpsl_nand_driver);
329 module_init(sharpsl_nand_init);
331 static void __exit sharpsl_nand_exit(void)
333 platform_driver_unregister(&sharpsl_nand_driver);
334 platform_device_unregister(&sharpsl_nand_device);
336 module_exit(sharpsl_nand_exit);
338 MODULE_LICENSE("GPL");
339 MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
340 MODULE_DESCRIPTION("Device specific logic for NAND flash on Sharp SL-C7xx Series");