blob: 19e24ed089ea9dd05d1bc9b4ea25dc406d5ede90 [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>
Dmitry Baryshkova20c7ab2008-10-16 18:43:48 +040023#include <linux/mtd/sharpsl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/interrupt.h>
Dmitry Baryshkov26615242008-10-16 12:08:14 +040025#include <linux/platform_device.h>
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/io.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010028#include <mach/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/mach-types.h>
30
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +040031struct sharpsl_nand {
32 struct mtd_info mtd;
33 struct nand_chip chip;
Dmitry Baryshkova4e4f292008-10-16 17:58:18 +040034
35 void __iomem *io;
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +040036};
37
Dmitry Baryshkova4e4f292008-10-16 17:58:18 +040038#define mtd_to_sharpsl(_mtd) container_of(_mtd, struct sharpsl_nand, mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40/* register offset */
Dmitry Baryshkova4e4f292008-10-16 17:58:18 +040041#define ECCLPLB 0x00 /* line parity 7 - 0 bit */
42#define ECCLPUB 0x04 /* line parity 15 - 8 bit */
43#define ECCCP 0x08 /* column parity 5 - 0 bit */
44#define ECCCNTR 0x0C /* ECC byte counter */
45#define ECCCLRR 0x10 /* cleare ECC */
46#define FLASHIO 0x14 /* Flash I/O */
47#define FLASHCTL 0x18 /* Flash Control */
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49/* Flash control bit */
50#define FLRYBY (1 << 5)
51#define FLCE1 (1 << 4)
52#define FLWP (1 << 3)
53#define FLALE (1 << 2)
54#define FLCLE (1 << 1)
55#define FLCE0 (1 << 0)
56
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000057/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 * hardware specific access to control-lines
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +020059 * ctrl:
Richard Purdie6a5a297c2006-07-15 13:05:24 +010060 * NAND_CNE: bit 0 -> ! bit 0 & 4
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +020061 * NAND_CLE: bit 1 -> bit 1
62 * NAND_ALE: bit 2 -> bit 2
63 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +020065static void sharpsl_nand_hwcontrol(struct mtd_info *mtd, int cmd,
66 unsigned int ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
Dmitry Baryshkova4e4f292008-10-16 17:58:18 +040068 struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +020069 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +020071 if (ctrl & NAND_CTRL_CHANGE) {
72 unsigned char bits = ctrl & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +020074 bits |= (ctrl & 0x01) << 4;
Richard Purdie6a5a297c2006-07-15 13:05:24 +010075
76 bits ^= 0x11;
77
Dmitry Baryshkova4e4f292008-10-16 17:58:18 +040078 writeb((readb(sharpsl->io + FLASHCTL) & ~0x17) | bits, sharpsl->io + FLASHCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +020080
81 if (cmd != NAND_CMD_NONE)
82 writeb(cmd, chip->IO_ADDR_W);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
David Woodhousee0c7d762006-05-13 18:07:53 +010085static int sharpsl_nand_dev_ready(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Dmitry Baryshkova4e4f292008-10-16 17:58:18 +040087 struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
88 return !((readb(sharpsl->io + FLASHCTL) & FLRYBY) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
David Woodhousee0c7d762006-05-13 18:07:53 +010091static void sharpsl_nand_enable_hwecc(struct mtd_info *mtd, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Dmitry Baryshkova4e4f292008-10-16 17:58:18 +040093 struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
94 writeb(0, sharpsl->io + ECCCLRR);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
David Woodhousee0c7d762006-05-13 18:07:53 +010097static int sharpsl_nand_calculate_ecc(struct mtd_info *mtd, const u_char * dat, u_char * ecc_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Dmitry Baryshkova4e4f292008-10-16 17:58:18 +040099 struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
100 ecc_code[0] = ~readb(sharpsl->io + ECCLPUB);
101 ecc_code[1] = ~readb(sharpsl->io + ECCLPLB);
102 ecc_code[2] = (~readb(sharpsl->io + ECCCP) << 2) | 0x03;
103 return readb(sharpsl->io + ECCCNTR) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
Dmitry Baryshkovc176d0c2008-10-16 18:22:28 +0400106static const char *part_probes[] = { "cmdlinepart", NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108/*
109 * Main initialization routine
110 */
Dmitry Baryshkov26615242008-10-16 12:08:14 +0400111static int __devinit sharpsl_nand_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
113 struct nand_chip *this;
David Woodhousee0c7d762006-05-13 18:07:53 +0100114 struct mtd_partition *sharpsl_partition_info;
Dmitry Baryshkovc176d0c2008-10-16 18:22:28 +0400115 int nr_partitions;
Dmitry Baryshkov26615242008-10-16 12:08:14 +0400116 struct resource *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 int err = 0;
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +0400118 struct sharpsl_nand *sharpsl;
Dmitry Baryshkova20c7ab2008-10-16 18:43:48 +0400119 struct sharpsl_nand_platform_data *data = pdev->dev.platform_data;
120
121 if (!data) {
122 dev_err(&pdev->dev, "no platform data!\n");
123 return -EINVAL;
124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126 /* Allocate memory for MTD device structure and private data */
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +0400127 sharpsl = kzalloc(sizeof(struct sharpsl_nand), GFP_KERNEL);
128 if (!sharpsl) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100129 printk("Unable to allocate SharpSL NAND MTD device structure.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 return -ENOMEM;
131 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000132
Dmitry Baryshkov26615242008-10-16 12:08:14 +0400133 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
134 if (!r) {
135 dev_err(&pdev->dev, "no io memory resource defined!\n");
136 err = -ENODEV;
137 goto err_get_res;
138 }
139
Joe Perches8e87d782008-02-03 17:22:34 +0200140 /* map physical address */
Dmitry Baryshkova4e4f292008-10-16 17:58:18 +0400141 sharpsl->io = ioremap(r->start, resource_size(r));
142 if (!sharpsl->io) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 printk("ioremap to access Sharp SL NAND chip failed\n");
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +0400144 err = -EIO;
145 goto err_ioremap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 /* Get pointer to private data */
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +0400149 this = (struct nand_chip *)(&sharpsl->chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 /* Link the private data with the MTD structure */
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +0400152 sharpsl->mtd.priv = this;
153 sharpsl->mtd.owner = THIS_MODULE;
154
155 platform_set_drvdata(pdev, sharpsl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 /*
158 * PXA initialize
159 */
Dmitry Baryshkova4e4f292008-10-16 17:58:18 +0400160 writeb(readb(sharpsl->io + FLASHCTL) | FLWP, sharpsl->io + FLASHCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 /* Set address of NAND IO lines */
Dmitry Baryshkova4e4f292008-10-16 17:58:18 +0400163 this->IO_ADDR_R = sharpsl->io + FLASHIO;
164 this->IO_ADDR_W = sharpsl->io + FLASHIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 /* Set address of hardware control function */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200166 this->cmd_ctrl = sharpsl_nand_hwcontrol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 this->dev_ready = sharpsl_nand_dev_ready;
168 /* 15 us command delay time */
169 this->chip_delay = 15;
170 /* set eccmode using hardware ECC */
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +0200171 this->ecc.mode = NAND_ECC_HW;
172 this->ecc.size = 256;
173 this->ecc.bytes = 3;
Dmitry Baryshkova20c7ab2008-10-16 18:43:48 +0400174 this->badblock_pattern = data->badblock_pattern;
175 this->ecc.layout = data->ecc_layout;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +0200176 this->ecc.hwctl = sharpsl_nand_enable_hwecc;
177 this->ecc.calculate = sharpsl_nand_calculate_ecc;
178 this->ecc.correct = nand_correct_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 /* Scan to find existence of the device */
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +0400181 err = nand_scan(&sharpsl->mtd, 1);
Dmitry Baryshkova4e4f292008-10-16 17:58:18 +0400182 if (err)
183 goto err_scan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 /* Register the partitions */
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +0400186 sharpsl->mtd.name = "sharpsl-nand";
187 nr_partitions = parse_mtd_partitions(&sharpsl->mtd, part_probes, &sharpsl_partition_info, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 if (nr_partitions <= 0) {
Dmitry Baryshkova20c7ab2008-10-16 18:43:48 +0400189 nr_partitions = data->nr_partitions;
190 sharpsl_partition_info = data->partitions;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 }
192
Jamie Iles6baed702011-05-23 10:23:34 +0100193 err = mtd_device_register(&sharpsl->mtd, sharpsl_partition_info,
194 nr_partitions);
Dmitry Baryshkovc176d0c2008-10-16 18:22:28 +0400195 if (err)
196 goto err_add;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 /* Return happy */
199 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +0100200
Dmitry Baryshkovc176d0c2008-10-16 18:22:28 +0400201err_add:
202 nand_release(&sharpsl->mtd);
203
Dmitry Baryshkova4e4f292008-10-16 17:58:18 +0400204err_scan:
205 platform_set_drvdata(pdev, NULL);
206 iounmap(sharpsl->io);
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +0400207err_ioremap:
Dmitry Baryshkov26615242008-10-16 12:08:14 +0400208err_get_res:
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +0400209 kfree(sharpsl);
Dmitry Baryshkov26615242008-10-16 12:08:14 +0400210 return err;
211}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213/*
214 * Clean up routine
215 */
Dmitry Baryshkov26615242008-10-16 12:08:14 +0400216static int __devexit sharpsl_nand_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +0400218 struct sharpsl_nand *sharpsl = platform_get_drvdata(pdev);
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 /* Release resources, unregister device */
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +0400221 nand_release(&sharpsl->mtd);
222
223 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Dmitry Baryshkova4e4f292008-10-16 17:58:18 +0400225 iounmap(sharpsl->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 /* Free the MTD device structure */
Dmitry Baryshkov2206ef12008-10-16 17:49:06 +0400228 kfree(sharpsl);
Dmitry Baryshkov26615242008-10-16 12:08:14 +0400229
230 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
David Woodhousee0c7d762006-05-13 18:07:53 +0100232
Dmitry Baryshkov26615242008-10-16 12:08:14 +0400233static struct platform_driver sharpsl_nand_driver = {
234 .driver = {
235 .name = "sharpsl-nand",
236 .owner = THIS_MODULE,
237 },
238 .probe = sharpsl_nand_probe,
239 .remove = __devexit_p(sharpsl_nand_remove),
240};
241
Dmitry Baryshkov26615242008-10-16 12:08:14 +0400242static int __init sharpsl_nand_init(void)
243{
Dmitry Baryshkov26615242008-10-16 12:08:14 +0400244 return platform_driver_register(&sharpsl_nand_driver);
245}
246module_init(sharpsl_nand_init);
247
248static void __exit sharpsl_nand_exit(void)
249{
250 platform_driver_unregister(&sharpsl_nand_driver);
Dmitry Baryshkov26615242008-10-16 12:08:14 +0400251}
252module_exit(sharpsl_nand_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254MODULE_LICENSE("GPL");
255MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
256MODULE_DESCRIPTION("Device specific logic for NAND flash on Sharp SL-C7xx Series");