blob: 17625c0a8f61dab0e676e43f812ef783e7f2eea7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/mtd/nand/sharpsl.c
3 *
4 * Copyright (C) 2004 Richard Purdie
Dmitry Baryshkovc176d0c2008-10-16 18:22:28 +04005 * Copyright (C) 2008 Dmitry Baryshkov
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Based on Sharp's NAND driver sharp_sl.c
8 *
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.
12 *
13 */
14
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>
Dmitry Baryshkov26615242008-10-16 12:08:14 +040024#include <linux/platform_device.h>
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/io.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010027#include <mach/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/mach-types.h>
29
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +040030struct sharpsl_nand {
31 struct mtd_info mtd;
32 struct nand_chip chip;
Dmitry Baryshkova4e4f292008-10-16 17:58:18 +040033
34 void __iomem *io;
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +040035};
36
Dmitry Baryshkova4e4f292008-10-16 17:58:18 +040037#define mtd_to_sharpsl(_mtd) container_of(_mtd, struct sharpsl_nand, mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39/* register offset */
Dmitry Baryshkova4e4f292008-10-16 17:58:18 +040040#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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48/* Flash control bit */
49#define FLRYBY (1 << 5)
50#define FLCE1 (1 << 4)
51#define FLWP (1 << 3)
52#define FLALE (1 << 2)
53#define FLCLE (1 << 1)
54#define FLCE0 (1 << 0)
55
Dmitry Baryshkovc176d0c2008-10-16 18:22:28 +040056#ifdef CONFIG_MTD_PARTITIONS
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 * Define partitions for flash device
59 */
60#define DEFAULT_NUM_PARTITIONS 3
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static struct mtd_partition sharpsl_nand_default_partition_info[] = {
63 {
David Woodhousee0c7d762006-05-13 18:07:53 +010064 .name = "System Area",
65 .offset = 0,
66 .size = 7 * 1024 * 1024,
67 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 {
David Woodhousee0c7d762006-05-13 18:07:53 +010069 .name = "Root Filesystem",
70 .offset = 7 * 1024 * 1024,
71 .size = 30 * 1024 * 1024,
72 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 {
David Woodhousee0c7d762006-05-13 18:07:53 +010074 .name = "Home Filesystem",
75 .offset = MTDPART_OFS_APPEND,
76 .size = MTDPART_SIZ_FULL,
77 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070078};
Dmitry Baryshkovc176d0c2008-10-16 18:22:28 +040079#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000081/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * hardware specific access to control-lines
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +020083 * ctrl:
Richard Purdie6a5a297c2006-07-15 13:05:24 +010084 * NAND_CNE: bit 0 -> ! bit 0 & 4
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +020085 * NAND_CLE: bit 1 -> bit 1
86 * NAND_ALE: bit 2 -> bit 2
87 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +020089static void sharpsl_nand_hwcontrol(struct mtd_info *mtd, int cmd,
90 unsigned int ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Dmitry Baryshkova4e4f292008-10-16 17:58:18 +040092 struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +020093 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +020095 if (ctrl & NAND_CTRL_CHANGE) {
96 unsigned char bits = ctrl & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +020098 bits |= (ctrl & 0x01) << 4;
Richard Purdie6a5a297c2006-07-15 13:05:24 +010099
100 bits ^= 0x11;
101
Dmitry Baryshkova4e4f292008-10-16 17:58:18 +0400102 writeb((readb(sharpsl->io + FLASHCTL) & ~0x17) | bits, sharpsl->io + FLASHCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200104
105 if (cmd != NAND_CMD_NONE)
106 writeb(cmd, chip->IO_ADDR_W);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
109static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
110
111static struct nand_bbt_descr sharpsl_bbt = {
112 .options = 0,
113 .offs = 4,
114 .len = 2,
115 .pattern = scan_ff_pattern
116};
117
Richard Purdie87c146d2005-11-03 11:36:45 +0000118static struct nand_bbt_descr sharpsl_akita_bbt = {
119 .options = 0,
120 .offs = 4,
121 .len = 1,
122 .pattern = scan_ff_pattern
123};
124
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200125static struct nand_ecclayout akita_oobinfo = {
Richard Purdie87c146d2005-11-03 11:36:45 +0000126 .eccbytes = 24,
127 .eccpos = {
David Woodhousee0c7d762006-05-13 18:07:53 +0100128 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}}
Richard Purdie87c146d2005-11-03 11:36:45 +0000132};
133
David Woodhousee0c7d762006-05-13 18:07:53 +0100134static int sharpsl_nand_dev_ready(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Dmitry Baryshkova4e4f292008-10-16 17:58:18 +0400136 struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
137 return !((readb(sharpsl->io + FLASHCTL) & FLRYBY) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
David Woodhousee0c7d762006-05-13 18:07:53 +0100140static void sharpsl_nand_enable_hwecc(struct mtd_info *mtd, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Dmitry Baryshkova4e4f292008-10-16 17:58:18 +0400142 struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
143 writeb(0, sharpsl->io + ECCCLRR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
David Woodhousee0c7d762006-05-13 18:07:53 +0100146static int sharpsl_nand_calculate_ecc(struct mtd_info *mtd, const u_char * dat, u_char * ecc_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Dmitry Baryshkova4e4f292008-10-16 17:58:18 +0400148 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155#ifdef CONFIG_MTD_PARTITIONS
Dmitry Baryshkovc176d0c2008-10-16 18:22:28 +0400156static const char *part_probes[] = { "cmdlinepart", NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157#endif
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159/*
160 * Main initialization routine
161 */
Dmitry Baryshkov26615242008-10-16 12:08:14 +0400162static int __devinit sharpsl_nand_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
164 struct nand_chip *this;
Dmitry Baryshkovc176d0c2008-10-16 18:22:28 +0400165#ifdef CONFIG_MTD_PARTITIONS
David Woodhousee0c7d762006-05-13 18:07:53 +0100166 struct mtd_partition *sharpsl_partition_info;
Dmitry Baryshkovc176d0c2008-10-16 18:22:28 +0400167 int nr_partitions;
168#endif
Dmitry Baryshkov26615242008-10-16 12:08:14 +0400169 struct resource *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 int err = 0;
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +0400171 struct sharpsl_nand *sharpsl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 /* Allocate memory for MTD device structure and private data */
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +0400174 sharpsl = kzalloc(sizeof(struct sharpsl_nand), GFP_KERNEL);
175 if (!sharpsl) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100176 printk("Unable to allocate SharpSL NAND MTD device structure.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 return -ENOMEM;
178 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000179
Dmitry Baryshkov26615242008-10-16 12:08:14 +0400180 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
181 if (!r) {
182 dev_err(&pdev->dev, "no io memory resource defined!\n");
183 err = -ENODEV;
184 goto err_get_res;
185 }
186
Joe Perches8e87d782008-02-03 17:22:34 +0200187 /* map physical address */
Dmitry Baryshkova4e4f292008-10-16 17:58:18 +0400188 sharpsl->io = ioremap(r->start, resource_size(r));
189 if (!sharpsl->io) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 printk("ioremap to access Sharp SL NAND chip failed\n");
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +0400191 err = -EIO;
192 goto err_ioremap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 /* Get pointer to private data */
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +0400196 this = (struct nand_chip *)(&sharpsl->chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 /* Link the private data with the MTD structure */
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +0400199 sharpsl->mtd.priv = this;
200 sharpsl->mtd.owner = THIS_MODULE;
201
202 platform_set_drvdata(pdev, sharpsl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 /*
205 * PXA initialize
206 */
Dmitry Baryshkova4e4f292008-10-16 17:58:18 +0400207 writeb(readb(sharpsl->io + FLASHCTL) | FLWP, sharpsl->io + FLASHCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 /* Set address of NAND IO lines */
Dmitry Baryshkova4e4f292008-10-16 17:58:18 +0400210 this->IO_ADDR_R = sharpsl->io + FLASHIO;
211 this->IO_ADDR_W = sharpsl->io + FLASHIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 /* Set address of hardware control function */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200213 this->cmd_ctrl = sharpsl_nand_hwcontrol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 this->dev_ready = sharpsl_nand_dev_ready;
215 /* 15 us command delay time */
216 this->chip_delay = 15;
217 /* set eccmode using hardware ECC */
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +0200218 this->ecc.mode = NAND_ECC_HW;
219 this->ecc.size = 256;
220 this->ecc.bytes = 3;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000221 this->badblock_pattern = &sharpsl_bbt;
Richard Purdie87c146d2005-11-03 11:36:45 +0000222 if (machine_is_akita() || machine_is_borzoi()) {
223 this->badblock_pattern = &sharpsl_akita_bbt;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200224 this->ecc.layout = &akita_oobinfo;
Richard Purdie87c146d2005-11-03 11:36:45 +0000225 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +0200226 this->ecc.hwctl = sharpsl_nand_enable_hwecc;
227 this->ecc.calculate = sharpsl_nand_calculate_ecc;
228 this->ecc.correct = nand_correct_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 /* Scan to find existence of the device */
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +0400231 err = nand_scan(&sharpsl->mtd, 1);
Dmitry Baryshkova4e4f292008-10-16 17:58:18 +0400232 if (err)
233 goto err_scan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 /* Register the partitions */
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +0400236 sharpsl->mtd.name = "sharpsl-nand";
Dmitry Baryshkovc176d0c2008-10-16 18:22:28 +0400237#ifdef CONFIG_MTD_PARTITIONS
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +0400238 nr_partitions = parse_mtd_partitions(&sharpsl->mtd, part_probes, &sharpsl_partition_info, 0);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (nr_partitions <= 0) {
Dmitry Baryshkovc176d0c2008-10-16 18:22:28 +0400241 nr_partitions = ARRAY_SIZE(sharpsl_nand_default_partition_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 sharpsl_partition_info = sharpsl_nand_default_partition_info;
243 if (machine_is_poodle()) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100244 sharpsl_partition_info[1].size = 22 * 1024 * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 } else if (machine_is_corgi() || machine_is_shepherd()) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100246 sharpsl_partition_info[1].size = 25 * 1024 * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 } else if (machine_is_husky()) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100248 sharpsl_partition_info[1].size = 53 * 1024 * 1024;
Richard Purdie62052d42005-09-16 19:27:31 -0700249 } else if (machine_is_spitz()) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100250 sharpsl_partition_info[1].size = 5 * 1024 * 1024;
Richard Purdie62052d42005-09-16 19:27:31 -0700251 } else if (machine_is_akita()) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100252 sharpsl_partition_info[1].size = 58 * 1024 * 1024;
Richard Purdie62052d42005-09-16 19:27:31 -0700253 } else if (machine_is_borzoi()) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100254 sharpsl_partition_info[1].size = 32 * 1024 * 1024;
Richard Purdie62052d42005-09-16 19:27:31 -0700255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
257
Dmitry Baryshkovc176d0c2008-10-16 18:22:28 +0400258 err = add_mtd_partitions(&sharpsl->mtd, sharpsl_partition_info, nr_partitions);
259#else
260 err = add_mtd_device(&sharpsl->mtd);
261#endif
262 if (err)
263 goto err_add;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 /* Return happy */
266 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +0100267
Dmitry Baryshkovc176d0c2008-10-16 18:22:28 +0400268err_add:
269 nand_release(&sharpsl->mtd);
270
Dmitry Baryshkova4e4f292008-10-16 17:58:18 +0400271err_scan:
272 platform_set_drvdata(pdev, NULL);
273 iounmap(sharpsl->io);
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +0400274err_ioremap:
Dmitry Baryshkov26615242008-10-16 12:08:14 +0400275err_get_res:
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +0400276 kfree(sharpsl);
Dmitry Baryshkov26615242008-10-16 12:08:14 +0400277 return err;
278}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280/*
281 * Clean up routine
282 */
Dmitry Baryshkov26615242008-10-16 12:08:14 +0400283static int __devexit sharpsl_nand_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +0400285 struct sharpsl_nand *sharpsl = platform_get_drvdata(pdev);
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 /* Release resources, unregister device */
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +0400288 nand_release(&sharpsl->mtd);
289
290 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Dmitry Baryshkova4e4f292008-10-16 17:58:18 +0400292 iounmap(sharpsl->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 /* Free the MTD device structure */
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +0400295 kfree(sharpsl);
Dmitry Baryshkov26615242008-10-16 12:08:14 +0400296
297 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
David Woodhousee0c7d762006-05-13 18:07:53 +0100299
Dmitry Baryshkov26615242008-10-16 12:08:14 +0400300static struct platform_driver sharpsl_nand_driver = {
301 .driver = {
302 .name = "sharpsl-nand",
303 .owner = THIS_MODULE,
304 },
305 .probe = sharpsl_nand_probe,
306 .remove = __devexit_p(sharpsl_nand_remove),
307};
308
309static struct resource sharpsl_nand_resources[] = {
310 {
311 .start = 0x0C000000,
312 .end = 0x0C000FFF,
313 .flags = IORESOURCE_MEM,
314 },
315};
316
317static struct platform_device sharpsl_nand_device = {
318 .name = "sharpsl-nand",
319 .id = -1,
320 .resource = sharpsl_nand_resources,
321 .num_resources = ARRAY_SIZE(sharpsl_nand_resources),
322};
323
324static int __init sharpsl_nand_init(void)
325{
326 platform_device_register(&sharpsl_nand_device);
327 return platform_driver_register(&sharpsl_nand_driver);
328}
329module_init(sharpsl_nand_init);
330
331static void __exit sharpsl_nand_exit(void)
332{
333 platform_driver_unregister(&sharpsl_nand_driver);
334 platform_device_unregister(&sharpsl_nand_device);
335}
336module_exit(sharpsl_nand_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338MODULE_LICENSE("GPL");
339MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
340MODULE_DESCRIPTION("Device specific logic for NAND flash on Sharp SL-C7xx Series");