mtd: s3c2410.c: use mtd_device_parse_register
[deliverable/linux.git] / drivers / mtd / nand / sharpsl.c
CommitLineData
1da177e4
LT
1/*
2 * drivers/mtd/nand/sharpsl.c
3 *
4 * Copyright (C) 2004 Richard Purdie
c176d0ca 5 * Copyright (C) 2008 Dmitry Baryshkov
1da177e4 6 *
1da177e4
LT
7 * 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>
a20c7ab5 23#include <linux/mtd/sharpsl.h>
1da177e4 24#include <linux/interrupt.h>
26615249
DB
25#include <linux/platform_device.h>
26
1da177e4 27#include <asm/io.h>
a09e64fb 28#include <mach/hardware.h>
1da177e4
LT
29#include <asm/mach-types.h>
30
2206ef1c
DB
31struct sharpsl_nand {
32 struct mtd_info mtd;
33 struct nand_chip chip;
a4e4f29c
DB
34
35 void __iomem *io;
2206ef1c
DB
36};
37
a4e4f29c 38#define mtd_to_sharpsl(_mtd) container_of(_mtd, struct sharpsl_nand, mtd)
1da177e4
LT
39
40/* register offset */
a4e4f29c
DB
41#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 */
1da177e4
LT
48
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
61b03bd7 57/*
1da177e4 58 * hardware specific access to control-lines
7abd3ef9 59 * ctrl:
6a5a297c 60 * NAND_CNE: bit 0 -> ! bit 0 & 4
7abd3ef9
TG
61 * NAND_CLE: bit 1 -> bit 1
62 * NAND_ALE: bit 2 -> bit 2
63 *
1da177e4 64 */
7abd3ef9
TG
65static void sharpsl_nand_hwcontrol(struct mtd_info *mtd, int cmd,
66 unsigned int ctrl)
1da177e4 67{
a4e4f29c 68 struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
7abd3ef9
TG
69 struct nand_chip *chip = mtd->priv;
70
71 if (ctrl & NAND_CTRL_CHANGE) {
72 unsigned char bits = ctrl & 0x07;
73
74 bits |= (ctrl & 0x01) << 4;
6a5a297c
RP
75
76 bits ^= 0x11;
77
a4e4f29c 78 writeb((readb(sharpsl->io + FLASHCTL) & ~0x17) | bits, sharpsl->io + FLASHCTL);
1da177e4 79 }
7abd3ef9
TG
80
81 if (cmd != NAND_CMD_NONE)
82 writeb(cmd, chip->IO_ADDR_W);
1da177e4
LT
83}
84
e0c7d767 85static int sharpsl_nand_dev_ready(struct mtd_info *mtd)
1da177e4 86{
a4e4f29c
DB
87 struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
88 return !((readb(sharpsl->io + FLASHCTL) & FLRYBY) == 0);
1da177e4
LT
89}
90
e0c7d767 91static void sharpsl_nand_enable_hwecc(struct mtd_info *mtd, int mode)
1da177e4 92{
a4e4f29c
DB
93 struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
94 writeb(0, sharpsl->io + ECCCLRR);
1da177e4
LT
95}
96
e0c7d767 97static int sharpsl_nand_calculate_ecc(struct mtd_info *mtd, const u_char * dat, u_char * ecc_code)
1da177e4 98{
a4e4f29c
DB
99 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;
1da177e4
LT
104}
105
1da177e4
LT
106/*
107 * Main initialization routine
108 */
26615249 109static int __devinit sharpsl_nand_probe(struct platform_device *pdev)
1da177e4
LT
110{
111 struct nand_chip *this;
e0c7d767 112 struct mtd_partition *sharpsl_partition_info;
c176d0ca 113 int nr_partitions;
26615249 114 struct resource *r;
1da177e4 115 int err = 0;
2206ef1c 116 struct sharpsl_nand *sharpsl;
a20c7ab5
DB
117 struct sharpsl_nand_platform_data *data = pdev->dev.platform_data;
118
119 if (!data) {
120 dev_err(&pdev->dev, "no platform data!\n");
121 return -EINVAL;
122 }
1da177e4
LT
123
124 /* Allocate memory for MTD device structure and private data */
2206ef1c
DB
125 sharpsl = kzalloc(sizeof(struct sharpsl_nand), GFP_KERNEL);
126 if (!sharpsl) {
e0c7d767 127 printk("Unable to allocate SharpSL NAND MTD device structure.\n");
1da177e4
LT
128 return -ENOMEM;
129 }
61b03bd7 130
26615249
DB
131 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
132 if (!r) {
133 dev_err(&pdev->dev, "no io memory resource defined!\n");
134 err = -ENODEV;
135 goto err_get_res;
136 }
137
8e87d782 138 /* map physical address */
a4e4f29c
DB
139 sharpsl->io = ioremap(r->start, resource_size(r));
140 if (!sharpsl->io) {
1da177e4 141 printk("ioremap to access Sharp SL NAND chip failed\n");
2206ef1c
DB
142 err = -EIO;
143 goto err_ioremap;
1da177e4 144 }
61b03bd7 145
1da177e4 146 /* Get pointer to private data */
2206ef1c 147 this = (struct nand_chip *)(&sharpsl->chip);
1da177e4
LT
148
149 /* Link the private data with the MTD structure */
2206ef1c
DB
150 sharpsl->mtd.priv = this;
151 sharpsl->mtd.owner = THIS_MODULE;
152
153 platform_set_drvdata(pdev, sharpsl);
1da177e4
LT
154
155 /*
156 * PXA initialize
157 */
a4e4f29c 158 writeb(readb(sharpsl->io + FLASHCTL) | FLWP, sharpsl->io + FLASHCTL);
1da177e4
LT
159
160 /* Set address of NAND IO lines */
a4e4f29c
DB
161 this->IO_ADDR_R = sharpsl->io + FLASHIO;
162 this->IO_ADDR_W = sharpsl->io + FLASHIO;
1da177e4 163 /* Set address of hardware control function */
7abd3ef9 164 this->cmd_ctrl = sharpsl_nand_hwcontrol;
1da177e4
LT
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 */
6dfc6d25
TG
169 this->ecc.mode = NAND_ECC_HW;
170 this->ecc.size = 256;
171 this->ecc.bytes = 3;
a20c7ab5
DB
172 this->badblock_pattern = data->badblock_pattern;
173 this->ecc.layout = data->ecc_layout;
6dfc6d25
TG
174 this->ecc.hwctl = sharpsl_nand_enable_hwecc;
175 this->ecc.calculate = sharpsl_nand_calculate_ecc;
176 this->ecc.correct = nand_correct_data;
1da177e4
LT
177
178 /* Scan to find existence of the device */
2206ef1c 179 err = nand_scan(&sharpsl->mtd, 1);
a4e4f29c
DB
180 if (err)
181 goto err_scan;
1da177e4
LT
182
183 /* Register the partitions */
2206ef1c 184 sharpsl->mtd.name = "sharpsl-nand";
a4c93614 185 nr_partitions = parse_mtd_partitions(&sharpsl->mtd, NULL, &sharpsl_partition_info, 0);
1da177e4 186 if (nr_partitions <= 0) {
a20c7ab5
DB
187 nr_partitions = data->nr_partitions;
188 sharpsl_partition_info = data->partitions;
1da177e4
LT
189 }
190
6baed700
JI
191 err = mtd_device_register(&sharpsl->mtd, sharpsl_partition_info,
192 nr_partitions);
c176d0ca
DB
193 if (err)
194 goto err_add;
1da177e4
LT
195
196 /* Return happy */
197 return 0;
e0c7d767 198
c176d0ca
DB
199err_add:
200 nand_release(&sharpsl->mtd);
201
a4e4f29c
DB
202err_scan:
203 platform_set_drvdata(pdev, NULL);
204 iounmap(sharpsl->io);
2206ef1c 205err_ioremap:
26615249 206err_get_res:
2206ef1c 207 kfree(sharpsl);
26615249
DB
208 return err;
209}
1da177e4
LT
210
211/*
212 * Clean up routine
213 */
26615249 214static int __devexit sharpsl_nand_remove(struct platform_device *pdev)
1da177e4 215{
2206ef1c
DB
216 struct sharpsl_nand *sharpsl = platform_get_drvdata(pdev);
217
1da177e4 218 /* Release resources, unregister device */
2206ef1c
DB
219 nand_release(&sharpsl->mtd);
220
221 platform_set_drvdata(pdev, NULL);
1da177e4 222
a4e4f29c 223 iounmap(sharpsl->io);
1da177e4
LT
224
225 /* Free the MTD device structure */
2206ef1c 226 kfree(sharpsl);
26615249
DB
227
228 return 0;
229}
230
231static struct platform_driver sharpsl_nand_driver = {
232 .driver = {
233 .name = "sharpsl-nand",
234 .owner = THIS_MODULE,
235 },
236 .probe = sharpsl_nand_probe,
237 .remove = __devexit_p(sharpsl_nand_remove),
238};
239
26615249
DB
240static int __init sharpsl_nand_init(void)
241{
26615249 242 return platform_driver_register(&sharpsl_nand_driver);
1da177e4 243}
26615249 244module_init(sharpsl_nand_init);
e0c7d767 245
26615249
DB
246static void __exit sharpsl_nand_exit(void)
247{
248 platform_driver_unregister(&sharpsl_nand_driver);
26615249
DB
249}
250module_exit(sharpsl_nand_exit);
1da177e4
LT
251
252MODULE_LICENSE("GPL");
253MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
254MODULE_DESCRIPTION("Device specific logic for NAND flash on Sharp SL-C7xx Series");
This page took 0.516714 seconds and 5 git commands to generate.