]> Pileus Git - ~andy/linux/blob - drivers/mtd/nand/sharpsl.c
[MTD] sharpsl-nand: cleanup partitions support
[~andy/linux] / drivers / mtd / nand / sharpsl.c
1 /*
2  * drivers/mtd/nand/sharpsl.c
3  *
4  *  Copyright (C) 2004 Richard Purdie
5  *  Copyright (C) 2008 Dmitry Baryshkov
6  *
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>
23 #include <linux/interrupt.h>
24 #include <linux/platform_device.h>
25
26 #include <asm/io.h>
27 #include <mach/hardware.h>
28 #include <asm/mach-types.h>
29
30 struct sharpsl_nand {
31         struct mtd_info         mtd;
32         struct nand_chip        chip;
33
34         void __iomem            *io;
35 };
36
37 #define mtd_to_sharpsl(_mtd)    container_of(_mtd, struct sharpsl_nand, mtd)
38
39 /* register offset */
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 */
47
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
56 #ifdef CONFIG_MTD_PARTITIONS
57 /*
58  * Define partitions for flash device
59  */
60 #define DEFAULT_NUM_PARTITIONS 3
61
62 static struct mtd_partition sharpsl_nand_default_partition_info[] = {
63         {
64          .name = "System Area",
65          .offset = 0,
66          .size = 7 * 1024 * 1024,
67          },
68         {
69          .name = "Root Filesystem",
70          .offset = 7 * 1024 * 1024,
71          .size = 30 * 1024 * 1024,
72          },
73         {
74          .name = "Home Filesystem",
75          .offset = MTDPART_OFS_APPEND,
76          .size = MTDPART_SIZ_FULL,
77          },
78 };
79 #endif
80
81 /*
82  *      hardware specific access to control-lines
83  *      ctrl:
84  *      NAND_CNE: bit 0 -> ! bit 0 & 4
85  *      NAND_CLE: bit 1 -> bit 1
86  *      NAND_ALE: bit 2 -> bit 2
87  *
88  */
89 static void sharpsl_nand_hwcontrol(struct mtd_info *mtd, int cmd,
90                                    unsigned int ctrl)
91 {
92         struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
93         struct nand_chip *chip = mtd->priv;
94
95         if (ctrl & NAND_CTRL_CHANGE) {
96                 unsigned char bits = ctrl & 0x07;
97
98                 bits |= (ctrl & 0x01) << 4;
99
100                 bits ^= 0x11;
101
102                 writeb((readb(sharpsl->io + FLASHCTL) & ~0x17) | bits, sharpsl->io + FLASHCTL);
103         }
104
105         if (cmd != NAND_CMD_NONE)
106                 writeb(cmd, chip->IO_ADDR_W);
107 }
108
109 static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
110
111 static struct nand_bbt_descr sharpsl_bbt = {
112         .options = 0,
113         .offs = 4,
114         .len = 2,
115         .pattern = scan_ff_pattern
116 };
117
118 static struct nand_bbt_descr sharpsl_akita_bbt = {
119         .options = 0,
120         .offs = 4,
121         .len = 1,
122         .pattern = scan_ff_pattern
123 };
124
125 static struct nand_ecclayout akita_oobinfo = {
126         .eccbytes = 24,
127         .eccpos = {
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}}
132 };
133
134 static int sharpsl_nand_dev_ready(struct mtd_info *mtd)
135 {
136         struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
137         return !((readb(sharpsl->io + FLASHCTL) & FLRYBY) == 0);
138 }
139
140 static void sharpsl_nand_enable_hwecc(struct mtd_info *mtd, int mode)
141 {
142         struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
143         writeb(0, sharpsl->io + ECCCLRR);
144 }
145
146 static int sharpsl_nand_calculate_ecc(struct mtd_info *mtd, const u_char * dat, u_char * ecc_code)
147 {
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;
153 }
154
155 #ifdef CONFIG_MTD_PARTITIONS
156 static const char *part_probes[] = { "cmdlinepart", NULL };
157 #endif
158
159 /*
160  * Main initialization routine
161  */
162 static int __devinit sharpsl_nand_probe(struct platform_device *pdev)
163 {
164         struct nand_chip *this;
165 #ifdef CONFIG_MTD_PARTITIONS
166         struct mtd_partition *sharpsl_partition_info;
167         int nr_partitions;
168 #endif
169         struct resource *r;
170         int err = 0;
171         struct sharpsl_nand *sharpsl;
172
173         /* Allocate memory for MTD device structure and private data */
174         sharpsl = kzalloc(sizeof(struct sharpsl_nand), GFP_KERNEL);
175         if (!sharpsl) {
176                 printk("Unable to allocate SharpSL NAND MTD device structure.\n");
177                 return -ENOMEM;
178         }
179
180         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
187         /* map physical address */
188         sharpsl->io = ioremap(r->start, resource_size(r));
189         if (!sharpsl->io) {
190                 printk("ioremap to access Sharp SL NAND chip failed\n");
191                 err = -EIO;
192                 goto err_ioremap;
193         }
194
195         /* Get pointer to private data */
196         this = (struct nand_chip *)(&sharpsl->chip);
197
198         /* Link the private data with the MTD structure */
199         sharpsl->mtd.priv = this;
200         sharpsl->mtd.owner = THIS_MODULE;
201
202         platform_set_drvdata(pdev, sharpsl);
203
204         /*
205          * PXA initialize
206          */
207         writeb(readb(sharpsl->io + FLASHCTL) | FLWP, sharpsl->io + FLASHCTL);
208
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;
220         this->ecc.bytes = 3;
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;
225         }
226         this->ecc.hwctl = sharpsl_nand_enable_hwecc;
227         this->ecc.calculate = sharpsl_nand_calculate_ecc;
228         this->ecc.correct = nand_correct_data;
229
230         /* Scan to find existence of the device */
231         err = nand_scan(&sharpsl->mtd, 1);
232         if (err)
233                 goto err_scan;
234
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);
239
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;
255                 }
256         }
257
258         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;
264
265         /* Return happy */
266         return 0;
267
268 err_add:
269         nand_release(&sharpsl->mtd);
270
271 err_scan:
272         platform_set_drvdata(pdev, NULL);
273         iounmap(sharpsl->io);
274 err_ioremap:
275 err_get_res:
276         kfree(sharpsl);
277         return err;
278 }
279
280 /*
281  * Clean up routine
282  */
283 static int __devexit sharpsl_nand_remove(struct platform_device *pdev)
284 {
285         struct sharpsl_nand *sharpsl = platform_get_drvdata(pdev);
286
287         /* Release resources, unregister device */
288         nand_release(&sharpsl->mtd);
289
290         platform_set_drvdata(pdev, NULL);
291
292         iounmap(sharpsl->io);
293
294         /* Free the MTD device structure */
295         kfree(sharpsl);
296
297         return 0;
298 }
299
300 static 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
309 static struct resource sharpsl_nand_resources[] = {
310         {
311                 .start  = 0x0C000000,
312                 .end    = 0x0C000FFF,
313                 .flags  = IORESOURCE_MEM,
314         },
315 };
316
317 static 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
324 static int __init sharpsl_nand_init(void)
325 {
326         platform_device_register(&sharpsl_nand_device);
327         return platform_driver_register(&sharpsl_nand_driver);
328 }
329 module_init(sharpsl_nand_init);
330
331 static void __exit sharpsl_nand_exit(void)
332 {
333         platform_driver_unregister(&sharpsl_nand_driver);
334         platform_device_unregister(&sharpsl_nand_device);
335 }
336 module_exit(sharpsl_nand_exit);
337
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");