blob: 6851806b70158beb9501b977ef3778d954c99001 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/mtd/nand/sharpsl.c
3 *
4 * Copyright (C) 2004 Richard Purdie
5 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Based on Sharp's NAND driver sharp_sl.c
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 */
13
14#include <linux/genhd.h>
15#include <linux/slab.h>
16#include <linux/module.h>
17#include <linux/delay.h>
18#include <linux/mtd/mtd.h>
19#include <linux/mtd/nand.h>
20#include <linux/mtd/nand_ecc.h>
21#include <linux/mtd/partitions.h>
22#include <linux/interrupt.h>
Dmitry Baryshkov26615242008-10-16 12:08:14 +040023#include <linux/platform_device.h>
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/io.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010026#include <mach/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/mach-types.h>
28
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +040029struct sharpsl_nand {
30 struct mtd_info mtd;
31 struct nand_chip chip;
32};
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034static void __iomem *sharpsl_io_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36/* register offset */
37#define ECCLPLB sharpsl_io_base+0x00 /* line parity 7 - 0 bit */
38#define ECCLPUB sharpsl_io_base+0x04 /* line parity 15 - 8 bit */
39#define ECCCP sharpsl_io_base+0x08 /* column parity 5 - 0 bit */
40#define ECCCNTR sharpsl_io_base+0x0C /* ECC byte counter */
41#define ECCCLRR sharpsl_io_base+0x10 /* cleare ECC */
42#define FLASHIO sharpsl_io_base+0x14 /* Flash I/O */
43#define FLASHCTL sharpsl_io_base+0x18 /* Flash Control */
44
45/* Flash control bit */
46#define FLRYBY (1 << 5)
47#define FLCE1 (1 << 4)
48#define FLWP (1 << 3)
49#define FLALE (1 << 2)
50#define FLCLE (1 << 1)
51#define FLCE0 (1 << 0)
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * Define partitions for flash device
55 */
56#define DEFAULT_NUM_PARTITIONS 3
57
58static int nr_partitions;
59static struct mtd_partition sharpsl_nand_default_partition_info[] = {
60 {
David Woodhousee0c7d762006-05-13 18:07:53 +010061 .name = "System Area",
62 .offset = 0,
63 .size = 7 * 1024 * 1024,
64 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 {
David Woodhousee0c7d762006-05-13 18:07:53 +010066 .name = "Root Filesystem",
67 .offset = 7 * 1024 * 1024,
68 .size = 30 * 1024 * 1024,
69 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 {
David Woodhousee0c7d762006-05-13 18:07:53 +010071 .name = "Home Filesystem",
72 .offset = MTDPART_OFS_APPEND,
73 .size = MTDPART_SIZ_FULL,
74 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070075};
76
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000077/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 * hardware specific access to control-lines
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +020079 * ctrl:
Richard Purdie6a5a297c2006-07-15 13:05:24 +010080 * NAND_CNE: bit 0 -> ! bit 0 & 4
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +020081 * NAND_CLE: bit 1 -> bit 1
82 * NAND_ALE: bit 2 -> bit 2
83 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +020085static void sharpsl_nand_hwcontrol(struct mtd_info *mtd, int cmd,
86 unsigned int ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +020088 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +020090 if (ctrl & NAND_CTRL_CHANGE) {
91 unsigned char bits = ctrl & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +020093 bits |= (ctrl & 0x01) << 4;
Richard Purdie6a5a297c2006-07-15 13:05:24 +010094
95 bits ^= 0x11;
96
97 writeb((readb(FLASHCTL) & ~0x17) | bits, FLASHCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +020099
100 if (cmd != NAND_CMD_NONE)
101 writeb(cmd, chip->IO_ADDR_W);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
104static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
105
106static struct nand_bbt_descr sharpsl_bbt = {
107 .options = 0,
108 .offs = 4,
109 .len = 2,
110 .pattern = scan_ff_pattern
111};
112
Richard Purdie87c146d2005-11-03 11:36:45 +0000113static struct nand_bbt_descr sharpsl_akita_bbt = {
114 .options = 0,
115 .offs = 4,
116 .len = 1,
117 .pattern = scan_ff_pattern
118};
119
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200120static struct nand_ecclayout akita_oobinfo = {
Richard Purdie87c146d2005-11-03 11:36:45 +0000121 .eccbytes = 24,
122 .eccpos = {
David Woodhousee0c7d762006-05-13 18:07:53 +0100123 0x5, 0x1, 0x2, 0x3, 0x6, 0x7, 0x15, 0x11,
124 0x12, 0x13, 0x16, 0x17, 0x25, 0x21, 0x22, 0x23,
125 0x26, 0x27, 0x35, 0x31, 0x32, 0x33, 0x36, 0x37},
126 .oobfree = {{0x08, 0x09}}
Richard Purdie87c146d2005-11-03 11:36:45 +0000127};
128
David Woodhousee0c7d762006-05-13 18:07:53 +0100129static int sharpsl_nand_dev_ready(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 return !((readb(FLASHCTL) & FLRYBY) == 0);
132}
133
David Woodhousee0c7d762006-05-13 18:07:53 +0100134static void sharpsl_nand_enable_hwecc(struct mtd_info *mtd, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
David Woodhousee0c7d762006-05-13 18:07:53 +0100136 writeb(0, ECCCLRR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
David Woodhousee0c7d762006-05-13 18:07:53 +0100139static int sharpsl_nand_calculate_ecc(struct mtd_info *mtd, const u_char * dat, u_char * ecc_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 ecc_code[0] = ~readb(ECCLPUB);
142 ecc_code[1] = ~readb(ECCLPLB);
143 ecc_code[2] = (~readb(ECCCP) << 2) | 0x03;
144 return readb(ECCCNTR) != 0;
145}
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147#ifdef CONFIG_MTD_PARTITIONS
148const char *part_probes[] = { "cmdlinepart", NULL };
149#endif
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151/*
152 * Main initialization routine
153 */
Dmitry Baryshkov26615242008-10-16 12:08:14 +0400154static int __devinit sharpsl_nand_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
156 struct nand_chip *this;
David Woodhousee0c7d762006-05-13 18:07:53 +0100157 struct mtd_partition *sharpsl_partition_info;
Dmitry Baryshkov26615242008-10-16 12:08:14 +0400158 struct resource *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 int err = 0;
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +0400160 struct sharpsl_nand *sharpsl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 /* Allocate memory for MTD device structure and private data */
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +0400163 sharpsl = kzalloc(sizeof(struct sharpsl_nand), GFP_KERNEL);
164 if (!sharpsl) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100165 printk("Unable to allocate SharpSL NAND MTD device structure.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 return -ENOMEM;
167 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000168
Dmitry Baryshkov26615242008-10-16 12:08:14 +0400169 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
170 if (!r) {
171 dev_err(&pdev->dev, "no io memory resource defined!\n");
172 err = -ENODEV;
173 goto err_get_res;
174 }
175
Joe Perches8e87d782008-02-03 17:22:34 +0200176 /* map physical address */
Dmitry Baryshkov26615242008-10-16 12:08:14 +0400177 sharpsl_io_base = ioremap(r->start, resource_size(r));
David Woodhousee0c7d762006-05-13 18:07:53 +0100178 if (!sharpsl_io_base) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 printk("ioremap to access Sharp SL NAND chip failed\n");
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +0400180 err = -EIO;
181 goto err_ioremap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 /* Get pointer to private data */
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +0400185 this = (struct nand_chip *)(&sharpsl->chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 /* Link the private data with the MTD structure */
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +0400188 sharpsl->mtd.priv = this;
189 sharpsl->mtd.owner = THIS_MODULE;
190
191 platform_set_drvdata(pdev, sharpsl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 /*
194 * PXA initialize
195 */
196 writeb(readb(FLASHCTL) | FLWP, FLASHCTL);
197
198 /* Set address of NAND IO lines */
199 this->IO_ADDR_R = FLASHIO;
200 this->IO_ADDR_W = FLASHIO;
201 /* Set address of hardware control function */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200202 this->cmd_ctrl = sharpsl_nand_hwcontrol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 this->dev_ready = sharpsl_nand_dev_ready;
204 /* 15 us command delay time */
205 this->chip_delay = 15;
206 /* set eccmode using hardware ECC */
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +0200207 this->ecc.mode = NAND_ECC_HW;
208 this->ecc.size = 256;
209 this->ecc.bytes = 3;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000210 this->badblock_pattern = &sharpsl_bbt;
Richard Purdie87c146d2005-11-03 11:36:45 +0000211 if (machine_is_akita() || machine_is_borzoi()) {
212 this->badblock_pattern = &sharpsl_akita_bbt;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200213 this->ecc.layout = &akita_oobinfo;
Richard Purdie87c146d2005-11-03 11:36:45 +0000214 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +0200215 this->ecc.hwctl = sharpsl_nand_enable_hwecc;
216 this->ecc.calculate = sharpsl_nand_calculate_ecc;
217 this->ecc.correct = nand_correct_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 /* Scan to find existence of the device */
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +0400220 err = nand_scan(&sharpsl->mtd, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 if (err) {
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +0400222 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 iounmap(sharpsl_io_base);
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +0400224 kfree(sharpsl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 return err;
226 }
227
228 /* Register the partitions */
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +0400229 sharpsl->mtd.name = "sharpsl-nand";
230 nr_partitions = parse_mtd_partitions(&sharpsl->mtd, part_probes, &sharpsl_partition_info, 0);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (nr_partitions <= 0) {
233 nr_partitions = DEFAULT_NUM_PARTITIONS;
234 sharpsl_partition_info = sharpsl_nand_default_partition_info;
235 if (machine_is_poodle()) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100236 sharpsl_partition_info[1].size = 22 * 1024 * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 } else if (machine_is_corgi() || machine_is_shepherd()) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100238 sharpsl_partition_info[1].size = 25 * 1024 * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 } else if (machine_is_husky()) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100240 sharpsl_partition_info[1].size = 53 * 1024 * 1024;
Richard Purdie62052d42005-09-16 19:27:31 -0700241 } else if (machine_is_spitz()) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100242 sharpsl_partition_info[1].size = 5 * 1024 * 1024;
Richard Purdie62052d42005-09-16 19:27:31 -0700243 } else if (machine_is_akita()) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100244 sharpsl_partition_info[1].size = 58 * 1024 * 1024;
Richard Purdie62052d42005-09-16 19:27:31 -0700245 } else if (machine_is_borzoi()) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100246 sharpsl_partition_info[1].size = 32 * 1024 * 1024;
Richard Purdie62052d42005-09-16 19:27:31 -0700247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
249
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +0400250 add_mtd_partitions(&sharpsl->mtd, sharpsl_partition_info, nr_partitions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 /* Return happy */
253 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +0100254
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +0400255err_ioremap:
Dmitry Baryshkov26615242008-10-16 12:08:14 +0400256err_get_res:
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +0400257 kfree(sharpsl);
Dmitry Baryshkov26615242008-10-16 12:08:14 +0400258 return err;
259}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261/*
262 * Clean up routine
263 */
Dmitry Baryshkov26615242008-10-16 12:08:14 +0400264static int __devexit sharpsl_nand_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +0400266 struct sharpsl_nand *sharpsl = platform_get_drvdata(pdev);
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 /* Release resources, unregister device */
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +0400269 nand_release(&sharpsl->mtd);
270
271 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 iounmap(sharpsl_io_base);
274
275 /* Free the MTD device structure */
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +0400276 kfree(sharpsl);
Dmitry Baryshkov26615242008-10-16 12:08:14 +0400277
278 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
David Woodhousee0c7d762006-05-13 18:07:53 +0100280
Dmitry Baryshkov26615242008-10-16 12:08:14 +0400281static struct platform_driver sharpsl_nand_driver = {
282 .driver = {
283 .name = "sharpsl-nand",
284 .owner = THIS_MODULE,
285 },
286 .probe = sharpsl_nand_probe,
287 .remove = __devexit_p(sharpsl_nand_remove),
288};
289
290static struct resource sharpsl_nand_resources[] = {
291 {
292 .start = 0x0C000000,
293 .end = 0x0C000FFF,
294 .flags = IORESOURCE_MEM,
295 },
296};
297
298static struct platform_device sharpsl_nand_device = {
299 .name = "sharpsl-nand",
300 .id = -1,
301 .resource = sharpsl_nand_resources,
302 .num_resources = ARRAY_SIZE(sharpsl_nand_resources),
303};
304
305static int __init sharpsl_nand_init(void)
306{
307 platform_device_register(&sharpsl_nand_device);
308 return platform_driver_register(&sharpsl_nand_driver);
309}
310module_init(sharpsl_nand_init);
311
312static void __exit sharpsl_nand_exit(void)
313{
314 platform_driver_unregister(&sharpsl_nand_driver);
315 platform_device_unregister(&sharpsl_nand_device);
316}
317module_exit(sharpsl_nand_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319MODULE_LICENSE("GPL");
320MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
321MODULE_DESCRIPTION("Device specific logic for NAND flash on Sharp SL-C7xx Series");