Merge branch 'rfc' of git://git.kernel.org/pub/scm/linux/kernel/git/paulus/perfcounte...
[linux-2.6] / drivers / mtd / nand / excite_nandflash.c
1 /*
2 *  Copyright (C) 2005 - 2007 by Basler Vision Technologies AG
3 *  Author: Thomas Koeller <thomas.koeller.qbaslerweb.com>
4 *  Original code by Thies Moeller <thies.moeller@baslerweb.com>
5 *
6 *  This program is free software; you can redistribute it and/or modify
7 *  it under the terms of the GNU General Public License as published by
8 *  the Free Software Foundation; either version 2 of the License, or
9 *  (at your option) any later version.
10 *
11 *  This program is distributed in the hope that it will be useful,
12 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 *  GNU General Public License for more details.
15 *
16 *  You should have received a copy of the GNU General Public License
17 *  along with this program; if not, write to the Free Software
18 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include <linux/module.h>
22 #include <linux/types.h>
23 #include <linux/init.h>
24 #include <linux/kernel.h>
25 #include <linux/string.h>
26 #include <linux/ioport.h>
27 #include <linux/platform_device.h>
28 #include <linux/delay.h>
29 #include <linux/err.h>
30
31 #include <linux/mtd/mtd.h>
32 #include <linux/mtd/nand.h>
33 #include <linux/mtd/nand_ecc.h>
34 #include <linux/mtd/partitions.h>
35
36 #include <asm/io.h>
37 #include <asm/rm9k-ocd.h>
38
39 #include <excite_nandflash.h>
40
41 #define EXCITE_NANDFLASH_VERSION "0.1"
42
43 /* I/O register offsets */
44 #define EXCITE_NANDFLASH_DATA_BYTE   0x00
45 #define EXCITE_NANDFLASH_STATUS_BYTE 0x0c
46 #define EXCITE_NANDFLASH_ADDR_BYTE   0x10
47 #define EXCITE_NANDFLASH_CMD_BYTE    0x14
48
49 /* prefix for debug output */
50 static const char module_id[] = "excite_nandflash";
51
52 /*
53  * partition definition
54  */
55 static const struct mtd_partition partition_info[] = {
56         {
57                 .name = "eXcite RootFS",
58                 .offset = 0,
59                 .size = MTDPART_SIZ_FULL
60         }
61 };
62
63 static inline const struct resource *
64 excite_nand_get_resource(struct platform_device *d, unsigned long flags,
65                          const char *basename)
66 {
67         char buf[80];
68
69         if (snprintf(buf, sizeof buf, "%s_%u", basename, d->id) >= sizeof buf)
70                 return NULL;
71         return platform_get_resource_byname(d, flags, buf);
72 }
73
74 static inline void __iomem *
75 excite_nand_map_regs(struct platform_device *d, const char *basename)
76 {
77         void *result = NULL;
78         const struct resource *const r =
79             excite_nand_get_resource(d, IORESOURCE_MEM, basename);
80
81         if (r)
82                 result = ioremap_nocache(r->start, r->end + 1 - r->start);
83         return result;
84 }
85
86 /* controller and mtd information */
87 struct excite_nand_drvdata {
88         struct mtd_info board_mtd;
89         struct nand_chip board_chip;
90         void __iomem *regs;
91         void __iomem *tgt;
92 };
93
94 /* Control function */
95 static void excite_nand_control(struct mtd_info *mtd, int cmd,
96                                        unsigned int ctrl)
97 {
98         struct excite_nand_drvdata * const d =
99             container_of(mtd, struct excite_nand_drvdata, board_mtd);
100
101         switch (ctrl) {
102         case NAND_CTRL_CHANGE | NAND_CTRL_CLE:
103                 d->tgt = d->regs + EXCITE_NANDFLASH_CMD_BYTE;
104                 break;
105         case NAND_CTRL_CHANGE | NAND_CTRL_ALE:
106                 d->tgt = d->regs + EXCITE_NANDFLASH_ADDR_BYTE;
107                 break;
108         case NAND_CTRL_CHANGE | NAND_NCE:
109                 d->tgt = d->regs + EXCITE_NANDFLASH_DATA_BYTE;
110                 break;
111         }
112
113         if (cmd != NAND_CMD_NONE)
114                 __raw_writeb(cmd, d->tgt);
115 }
116
117 /* Return 0 if flash is busy, 1 if ready */
118 static int excite_nand_devready(struct mtd_info *mtd)
119 {
120         struct excite_nand_drvdata * const drvdata =
121             container_of(mtd, struct excite_nand_drvdata, board_mtd);
122
123         return __raw_readb(drvdata->regs + EXCITE_NANDFLASH_STATUS_BYTE);
124 }
125
126 /*
127  * Called by device layer to remove the driver.
128  * The binding to the mtd and all allocated
129  * resources are released.
130  */
131 static int __exit excite_nand_remove(struct device *dev)
132 {
133         struct excite_nand_drvdata * const this = dev_get_drvdata(dev);
134
135         dev_set_drvdata(dev, NULL);
136
137         if (unlikely(!this)) {
138                 printk(KERN_ERR "%s: called %s without private data!!",
139                        module_id, __func__);
140                 return -EINVAL;
141         }
142
143         /* first thing we need to do is release our mtd
144          * then go through freeing the resource used
145          */
146         nand_release(&this->board_mtd);
147
148         /* free the common resources */
149         iounmap(this->regs);
150         kfree(this);
151
152         DEBUG(MTD_DEBUG_LEVEL1, "%s: removed\n", module_id);
153         return 0;
154 }
155
156 /*
157  * Called by device layer when it finds a device matching
158  * one our driver can handle. This code checks to see if
159  * it can allocate all necessary resources then calls the
160  * nand layer to look for devices.
161 */
162 static int __init excite_nand_probe(struct device *dev)
163 {
164         struct platform_device * const pdev = to_platform_device(dev);
165         struct excite_nand_drvdata *drvdata;    /* private driver data */
166         struct nand_chip *board_chip;   /* private flash chip data */
167         struct mtd_info *board_mtd;     /* mtd info for this board */
168         int scan_res;
169
170         drvdata = kzalloc(sizeof(*drvdata), GFP_KERNEL);
171         if (unlikely(!drvdata)) {
172                 printk(KERN_ERR "%s: no memory for drvdata\n",
173                        module_id);
174                 return -ENOMEM;
175         }
176
177         /* bind private data into driver */
178         dev_set_drvdata(dev, drvdata);
179
180         /* allocate and map the resource */
181         drvdata->regs =
182                 excite_nand_map_regs(pdev, EXCITE_NANDFLASH_RESOURCE_REGS);
183
184         if (unlikely(!drvdata->regs)) {
185                 printk(KERN_ERR "%s: cannot reserve register region\n",
186                        module_id);
187                 kfree(drvdata);
188                 return -ENXIO;
189         }
190         
191         drvdata->tgt = drvdata->regs + EXCITE_NANDFLASH_DATA_BYTE;
192
193         /* initialise our chip */
194         board_chip = &drvdata->board_chip;
195         board_chip->IO_ADDR_R = board_chip->IO_ADDR_W =
196                 drvdata->regs + EXCITE_NANDFLASH_DATA_BYTE;
197         board_chip->cmd_ctrl = excite_nand_control;
198         board_chip->dev_ready = excite_nand_devready;
199         board_chip->chip_delay = 25;
200         board_chip->ecc.mode = NAND_ECC_SOFT;
201
202         /* link chip to mtd */
203         board_mtd = &drvdata->board_mtd;
204         board_mtd->priv = board_chip;
205
206         DEBUG(MTD_DEBUG_LEVEL2, "%s: device scan\n", module_id);
207         scan_res = nand_scan(&drvdata->board_mtd, 1);
208
209         if (likely(!scan_res)) {
210                 DEBUG(MTD_DEBUG_LEVEL2, "%s: register partitions\n", module_id);
211                 add_mtd_partitions(&drvdata->board_mtd, partition_info,
212                                    ARRAY_SIZE(partition_info));
213         } else {
214                 iounmap(drvdata->regs);
215                 kfree(drvdata);
216                 printk(KERN_ERR "%s: device scan failed\n", module_id);
217                 return -EIO;
218         }
219         return 0;
220 }
221
222 static struct device_driver excite_nand_driver = {
223         .name = "excite_nand",
224         .bus = &platform_bus_type,
225         .probe = excite_nand_probe,
226         .remove = __exit_p(excite_nand_remove)
227 };
228
229 static int __init excite_nand_init(void)
230 {
231         pr_info("Basler eXcite nand flash driver Version "
232                 EXCITE_NANDFLASH_VERSION "\n");
233         return driver_register(&excite_nand_driver);
234 }
235
236 static void __exit excite_nand_exit(void)
237 {
238         driver_unregister(&excite_nand_driver);
239 }
240
241 module_init(excite_nand_init);
242 module_exit(excite_nand_exit);
243
244 MODULE_AUTHOR("Thomas Koeller <thomas.koeller@baslerweb.com>");
245 MODULE_DESCRIPTION("Basler eXcite NAND-Flash driver");
246 MODULE_LICENSE("GPL");
247 MODULE_VERSION(EXCITE_NANDFLASH_VERSION)