blob: b08a798ee254be0ed501758f0300df314c18078b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*======================================================================
2
3 drivers/mtd/maps/integrator-flash.c: ARM Integrator flash map driver
Thomas Gleixner69f34c92005-11-07 11:15:40 +00004
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 Copyright (C) 2000 ARM Limited
6 Copyright (C) 2003 Deep Blue Solutions Ltd.
Thomas Gleixner69f34c92005-11-07 11:15:40 +00007
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
Thomas Gleixner69f34c92005-11-07 11:15:40 +000012
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
Thomas Gleixner69f34c92005-11-07 11:15:40 +000017
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Thomas Gleixner69f34c92005-11-07 11:15:40 +000021
22 This is access code for flashes using ARM's flash partitioning
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 standards.
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025======================================================================*/
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/module.h>
28#include <linux/types.h>
29#include <linux/kernel.h>
30#include <linux/slab.h>
31#include <linux/ioport.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010032#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/init.h>
Russell King99730222009-03-25 10:21:35 +000034#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#include <linux/mtd/mtd.h>
37#include <linux/mtd/map.h>
38#include <linux/mtd/partitions.h>
Catalin Marinas9fd1e8f2009-04-23 17:41:10 +010039#include <linux/mtd/concat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include <asm/mach/flash.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010042#include <mach/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/system.h>
44
Catalin Marinas9fd1e8f2009-04-23 17:41:10 +010045#define SUBDEV_NAME_SIZE (BUS_ID_SIZE + 2)
46
47struct armflash_subdev_info {
48 char name[SUBDEV_NAME_SIZE];
49 struct mtd_info *mtd;
50 struct map_info map;
51 struct flash_platform_data *plat;
52};
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54struct armflash_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 struct resource *res;
56 struct mtd_partition *parts;
57 struct mtd_info *mtd;
Catalin Marinas9fd1e8f2009-04-23 17:41:10 +010058 int nr_subdev;
59 struct armflash_subdev_info subdev[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -070060};
61
62static void armflash_set_vpp(struct map_info *map, int on)
63{
Catalin Marinas9fd1e8f2009-04-23 17:41:10 +010064 struct armflash_subdev_info *info =
65 container_of(map, struct armflash_subdev_info, map);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67 if (info->plat && info->plat->set_vpp)
68 info->plat->set_vpp(on);
69}
70
71static const char *probes[] = { "cmdlinepart", "RedBoot", "afs", NULL };
72
Catalin Marinas9fd1e8f2009-04-23 17:41:10 +010073static int armflash_subdev_probe(struct armflash_subdev_info *subdev,
74 struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Catalin Marinas9fd1e8f2009-04-23 17:41:10 +010076 struct flash_platform_data *plat = subdev->plat;
77 resource_size_t size = res->end - res->start + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 void __iomem *base;
Catalin Marinas9fd1e8f2009-04-23 17:41:10 +010079 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Catalin Marinas9fd1e8f2009-04-23 17:41:10 +010081 if (!request_mem_region(res->start, size, subdev->name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 err = -EBUSY;
Catalin Marinas9fd1e8f2009-04-23 17:41:10 +010083 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
85
86 base = ioremap(res->start, size);
87 if (!base) {
88 err = -ENOMEM;
89 goto no_mem;
90 }
91
92 /*
93 * look for CFI based flash parts fitted to this board
94 */
Catalin Marinas9fd1e8f2009-04-23 17:41:10 +010095 subdev->map.size = size;
96 subdev->map.bankwidth = plat->width;
97 subdev->map.phys = res->start;
98 subdev->map.virt = base;
99 subdev->map.name = subdev->name;
100 subdev->map.set_vpp = armflash_set_vpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Catalin Marinas9fd1e8f2009-04-23 17:41:10 +0100102 simple_map_init(&subdev->map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 /*
105 * Also, the CFI layer automatically works out what size
106 * of chips we have, and does the necessary identification
107 * for us automatically.
108 */
Catalin Marinas9fd1e8f2009-04-23 17:41:10 +0100109 subdev->mtd = do_map_probe(plat->map_name, &subdev->map);
110 if (!subdev->mtd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 err = -ENXIO;
112 goto no_device;
113 }
114
Catalin Marinas9fd1e8f2009-04-23 17:41:10 +0100115 subdev->mtd->owner = THIS_MODULE;
116
117 /* Successful? */
118 if (err == 0)
119 return err;
120
121 if (subdev->mtd)
122 map_destroy(subdev->mtd);
123 no_device:
124 iounmap(base);
125 no_mem:
126 release_mem_region(res->start, size);
127 out:
128 return err;
129}
130
131static void armflash_subdev_remove(struct armflash_subdev_info *subdev)
132{
133 if (subdev->mtd)
134 map_destroy(subdev->mtd);
135 if (subdev->map.virt)
136 iounmap(subdev->map.virt);
137 release_mem_region(subdev->map.phys, subdev->map.size);
138}
139
140static int armflash_probe(struct platform_device *dev)
141{
142 struct flash_platform_data *plat = dev->dev.platform_data;
143 unsigned int size;
144 struct armflash_info *info;
145 int i, nr, err;
146
147 /* Count the number of devices */
148 for (nr = 0; ; nr++)
149 if (!platform_get_resource(dev, IORESOURCE_MEM, nr))
150 break;
151 if (nr == 0) {
152 err = -ENODEV;
153 goto out;
154 }
155
156 size = sizeof(struct armflash_info) +
157 sizeof(struct armflash_subdev_info) * nr;
158 info = kzalloc(size, GFP_KERNEL);
159 if (!info) {
160 err = -ENOMEM;
161 goto out;
162 }
163
164 if (plat && plat->init) {
165 err = plat->init();
166 if (err)
167 goto no_resource;
168 }
169
170 for (i = 0; i < nr; i++) {
171 struct armflash_subdev_info *subdev = &info->subdev[i];
172 struct resource *res;
173
174 res = platform_get_resource(dev, IORESOURCE_MEM, i);
175 if (!res)
176 break;
177
178 if (nr == 1)
179 /* No MTD concatenation, just use the default name */
180 snprintf(subdev->name, SUBDEV_NAME_SIZE, "%s",
181 dev_name(&dev->dev));
182 else
183 snprintf(subdev->name, SUBDEV_NAME_SIZE, "%s-%d",
184 dev_name(&dev->dev), i);
185 subdev->plat = plat;
186
187 err = armflash_subdev_probe(subdev, res);
188 if (err)
189 break;
190 }
191 info->nr_subdev = i;
192
193 if (err)
194 goto subdev_err;
195
196 if (info->nr_subdev == 1)
197 info->mtd = info->subdev[0].mtd;
198 else if (info->nr_subdev > 1) {
199#ifdef CONFIG_MTD_CONCAT
200 struct mtd_info *cdev[info->nr_subdev];
201
202 /*
203 * We detected multiple devices. Concatenate them together.
204 */
205 for (i = 0; i < info->nr_subdev; i++)
206 cdev[i] = info->subdev[i].mtd;
207
208 info->mtd = mtd_concat_create(cdev, info->nr_subdev,
209 dev_name(&dev->dev));
210 if (info->mtd == NULL)
211 err = -ENXIO;
212#else
213 printk(KERN_ERR "armflash: multiple devices found but "
214 "MTD concat support disabled.\n");
215 err = -ENXIO;
216#endif
217 }
218
219 if (err < 0)
220 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 err = parse_mtd_partitions(info->mtd, probes, &info->parts, 0);
223 if (err > 0) {
224 err = add_mtd_partitions(info->mtd, info->parts, err);
225 if (err)
226 printk(KERN_ERR
227 "mtd partition registration failed: %d\n", err);
228 }
229
Catalin Marinas9fd1e8f2009-04-23 17:41:10 +0100230 if (err == 0) {
Russell King3ae5eae2005-11-09 22:32:44 +0000231 platform_set_drvdata(dev, info);
Catalin Marinas9fd1e8f2009-04-23 17:41:10 +0100232 return err;
233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 /*
Catalin Marinas9fd1e8f2009-04-23 17:41:10 +0100236 * We got an error, free all resources.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 */
Catalin Marinas9fd1e8f2009-04-23 17:41:10 +0100238 cleanup:
239 if (info->mtd) {
240 del_mtd_partitions(info->mtd);
241#ifdef CONFIG_MTD_CONCAT
242 if (info->mtd != info->subdev[0].mtd)
243 mtd_concat_destroy(info->mtd);
244#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
Catalin Marinas9fd1e8f2009-04-23 17:41:10 +0100246 kfree(info->parts);
247 subdev_err:
248 for (i = info->nr_subdev - 1; i >= 0; i--)
249 armflash_subdev_remove(&info->subdev[i]);
250 no_resource:
251 if (plat && plat->exit)
252 plat->exit();
253 kfree(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 out:
255 return err;
256}
257
Russell King3ae5eae2005-11-09 22:32:44 +0000258static int armflash_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
Russell King3ae5eae2005-11-09 22:32:44 +0000260 struct armflash_info *info = platform_get_drvdata(dev);
Catalin Marinas9fd1e8f2009-04-23 17:41:10 +0100261 struct flash_platform_data *plat = dev->dev.platform_data;
262 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Russell King3ae5eae2005-11-09 22:32:44 +0000264 platform_set_drvdata(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 if (info) {
267 if (info->mtd) {
268 del_mtd_partitions(info->mtd);
Catalin Marinas9fd1e8f2009-04-23 17:41:10 +0100269#ifdef CONFIG_MTD_CONCAT
270 if (info->mtd != info->subdev[0].mtd)
271 mtd_concat_destroy(info->mtd);
272#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
Jesper Juhlfa671642005-11-07 01:01:27 -0800274 kfree(info->parts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Catalin Marinas9fd1e8f2009-04-23 17:41:10 +0100276 for (i = info->nr_subdev - 1; i >= 0; i--)
277 armflash_subdev_remove(&info->subdev[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Catalin Marinas9fd1e8f2009-04-23 17:41:10 +0100279 if (plat && plat->exit)
280 plat->exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 kfree(info);
283 }
284
285 return 0;
286}
287
Russell King3ae5eae2005-11-09 22:32:44 +0000288static struct platform_driver armflash_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 .probe = armflash_probe,
290 .remove = armflash_remove,
Russell King3ae5eae2005-11-09 22:32:44 +0000291 .driver = {
292 .name = "armflash",
Kay Sievers41d867c2008-04-18 13:44:26 -0700293 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +0000294 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295};
296
297static int __init armflash_init(void)
298{
Russell King3ae5eae2005-11-09 22:32:44 +0000299 return platform_driver_register(&armflash_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
302static void __exit armflash_exit(void)
303{
Russell King3ae5eae2005-11-09 22:32:44 +0000304 platform_driver_unregister(&armflash_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
307module_init(armflash_init);
308module_exit(armflash_exit);
309
310MODULE_AUTHOR("ARM Ltd");
311MODULE_DESCRIPTION("ARM Integrator CFI map driver");
312MODULE_LICENSE("GPL");
Kay Sievers41d867c2008-04-18 13:44:26 -0700313MODULE_ALIAS("platform:armflash");