2 * Flash memory access on SA11x0 based devices
4 * (C) 2000 Nicolas Pitre <nico@cam.org>
6 * $Id: sa1100-flash.c,v 1.51 2005/11/07 11:14:28 gleixner Exp $
8 #include <linux/config.h>
9 #include <linux/module.h>
10 #include <linux/types.h>
11 #include <linux/ioport.h>
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/errno.h>
15 #include <linux/slab.h>
16 #include <linux/platform_device.h>
17 #include <linux/err.h>
19 #include <linux/mtd/mtd.h>
20 #include <linux/mtd/map.h>
21 #include <linux/mtd/partitions.h>
22 #include <linux/mtd/concat.h>
24 #include <asm/hardware.h>
26 #include <asm/sizes.h>
27 #include <asm/mach/flash.h>
31 * This is here for documentation purposes only - until these people
32 * submit their machine types. It will be gone January 2005.
34 static struct mtd_partition consus_partitions[] = {
36 .name = "Consus boot firmware",
39 .mask_flags = MTD_WRITABLE, /* force read-only */
41 .name = "Consus kernel",
46 .name = "Consus disk",
48 /* The rest (up to 16M) for jffs. We could put 0 and
49 make it find the size automatically, but right now
50 i have 32 megs. jffs will use all 32 megs if given
51 the chance, and this leads to horrible problems
52 when you try to re-flash the image because blob
53 won't erase the whole partition. */
54 .size = 0x01000000 - 0x00140000,
57 /* this disk is a secondary disk, which can be used as
58 needed, for simplicity, make it the size of the other
59 consus partition, although realistically it could be
60 the remainder of the disk (depending on the file
62 .name = "Consus disk2",
64 .size = 0x01000000 - 0x00140000,
69 /* Frodo has 2 x 16M 28F128J3A flash chips in bank 0: */
70 static struct mtd_partition frodo_partitions[] =
76 .mask_flags = MTD_WRITEABLE
78 .name = "bootloader params",
80 .offset = MTDPART_OFS_APPEND,
81 .mask_flags = MTD_WRITEABLE
85 .offset = MTDPART_OFS_APPEND,
86 .mask_flags = MTD_WRITEABLE
90 .offset = MTDPART_OFS_APPEND,
91 .mask_flags = MTD_WRITEABLE
93 .name = "file system",
94 .size = MTDPART_SIZ_FULL,
95 .offset = MTDPART_OFS_APPEND
99 static struct mtd_partition jornada56x_partitions[] = {
104 .mask_flags = MTD_WRITEABLE,
107 .size = MTDPART_SIZ_FULL,
108 .offset = MTDPART_OFS_APPEND,
112 static void jornada56x_set_vpp(int vpp)
122 * Machine Phys Size set_vpp
123 * Consus : SA1100_CS0_PHYS SZ_32M
124 * Frodo : SA1100_CS0_PHYS SZ_32M
125 * Jornada56x: SA1100_CS0_PHYS SZ_32M jornada56x_set_vpp
129 struct sa_subdev_info {
132 struct mtd_info *mtd;
133 struct flash_platform_data *plat;
137 struct mtd_partition *parts;
138 struct mtd_info *mtd;
140 unsigned int nr_parts;
141 struct sa_subdev_info subdev[0];
144 static void sa1100_set_vpp(struct map_info *map, int on)
146 struct sa_subdev_info *subdev = container_of(map, struct sa_subdev_info, map);
147 subdev->plat->set_vpp(on);
150 static void sa1100_destroy_subdev(struct sa_subdev_info *subdev)
153 map_destroy(subdev->mtd);
154 if (subdev->map.virt)
155 iounmap(subdev->map.virt);
156 release_mem_region(subdev->map.phys, subdev->map.size);
159 static int sa1100_probe_subdev(struct sa_subdev_info *subdev, struct resource *res)
166 size = res->end - phys + 1;
169 * Retrieve the bankwidth from the MSC registers.
170 * We currently only implement CS0 and CS1 here.
174 printk(KERN_WARNING "SA1100 flash: unknown base address "
175 "0x%08lx, assuming CS0\n", phys);
177 case SA1100_CS0_PHYS:
178 subdev->map.bankwidth = (MSC0 & MSC_RBW) ? 2 : 4;
181 case SA1100_CS1_PHYS:
182 subdev->map.bankwidth = ((MSC0 >> 16) & MSC_RBW) ? 2 : 4;
186 if (!request_mem_region(phys, size, subdev->name)) {
191 if (subdev->plat->set_vpp)
192 subdev->map.set_vpp = sa1100_set_vpp;
194 subdev->map.phys = phys;
195 subdev->map.size = size;
196 subdev->map.virt = ioremap(phys, size);
197 if (!subdev->map.virt) {
202 simple_map_init(&subdev->map);
205 * Now let's probe for the actual flash. Do it here since
206 * specific machine settings might have been set above.
208 subdev->mtd = do_map_probe(subdev->plat->map_name, &subdev->map);
209 if (subdev->mtd == NULL) {
213 subdev->mtd->owner = THIS_MODULE;
215 printk(KERN_INFO "SA1100 flash: CFI device at 0x%08lx, %dMiB, "
216 "%d-bit\n", phys, subdev->mtd->size >> 20,
217 subdev->map.bankwidth * 8);
222 sa1100_destroy_subdev(subdev);
227 static void sa1100_destroy(struct sa_info *info, struct flash_platform_data *plat)
232 if (info->nr_parts == 0)
233 del_mtd_device(info->mtd);
234 #ifdef CONFIG_MTD_PARTITIONS
236 del_mtd_partitions(info->mtd);
238 #ifdef CONFIG_MTD_CONCAT
239 if (info->mtd != info->subdev[0].mtd)
240 mtd_concat_destroy(info->mtd);
246 for (i = info->num_subdev - 1; i >= 0; i--)
247 sa1100_destroy_subdev(&info->subdev[i]);
254 static struct sa_info *__init
255 sa1100_setup_mtd(struct platform_device *pdev, struct flash_platform_data *plat)
257 struct sa_info *info;
258 int nr, size, i, ret = 0;
261 * Count number of devices.
264 if (!platform_get_resource(pdev, IORESOURCE_MEM, nr))
272 size = sizeof(struct sa_info) + sizeof(struct sa_subdev_info) * nr;
275 * Allocate the map_info structs in one go.
277 info = kmalloc(size, GFP_KERNEL);
283 memset(info, 0, size);
292 * Claim and then map the memory regions.
294 for (i = 0; i < nr; i++) {
295 struct sa_subdev_info *subdev = &info->subdev[i];
296 struct resource *res;
298 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
302 subdev->map.name = subdev->name;
303 sprintf(subdev->name, "%s-%d", plat->name, i);
306 ret = sa1100_probe_subdev(subdev, res);
311 info->num_subdev = i;
314 * ENXIO is special. It means we didn't find a chip when we probed.
316 if (ret != 0 && !(ret == -ENXIO && info->num_subdev > 0))
320 * If we found one device, don't bother with concat support. If
321 * we found multiple devices, use concat if we have it available,
322 * otherwise fail. Either way, it'll be called "sa1100".
324 if (info->num_subdev == 1) {
325 strcpy(info->subdev[0].name, plat->name);
326 info->mtd = info->subdev[0].mtd;
328 } else if (info->num_subdev > 1) {
329 #ifdef CONFIG_MTD_CONCAT
330 struct mtd_info *cdev[nr];
332 * We detected multiple devices. Concatenate them together.
334 for (i = 0; i < info->num_subdev; i++)
335 cdev[i] = info->subdev[i].mtd;
337 info->mtd = mtd_concat_create(cdev, info->num_subdev,
339 if (info->mtd == NULL)
342 printk(KERN_ERR "SA1100 flash: multiple devices "
343 "found but MTD concat support disabled.\n");
352 sa1100_destroy(info, plat);
357 static const char *part_probes[] = { "cmdlinepart", "RedBoot", NULL };
359 static int __init sa1100_mtd_probe(struct platform_device *pdev)
361 struct flash_platform_data *plat = pdev->dev.platform_data;
362 struct mtd_partition *parts;
363 const char *part_type = NULL;
364 struct sa_info *info;
365 int err, nr_parts = 0;
370 info = sa1100_setup_mtd(pdev, plat);
377 * Partition selection stuff.
379 #ifdef CONFIG_MTD_PARTITIONS
380 nr_parts = parse_mtd_partitions(info->mtd, part_probes, &parts, 0);
383 part_type = "dynamic";
388 nr_parts = plat->nr_parts;
389 part_type = "static";
393 printk(KERN_NOTICE "SA1100 flash: no partition info "
394 "available, registering whole flash\n");
395 add_mtd_device(info->mtd);
397 printk(KERN_NOTICE "SA1100 flash: using %s partition "
398 "definition\n", part_type);
399 add_mtd_partitions(info->mtd, parts, nr_parts);
402 info->nr_parts = nr_parts;
404 platform_set_drvdata(pdev, info);
411 static int __exit sa1100_mtd_remove(struct platform_device *pdev)
413 struct sa_info *info = platform_get_drvdata(pdev);
414 struct flash_platform_data *plat = pdev->dev.platform_data;
416 platform_set_drvdata(pdev, NULL);
417 sa1100_destroy(info, plat);
423 static int sa1100_mtd_suspend(struct platform_device *dev, pm_message_t state)
425 struct sa_info *info = platform_get_drvdata(dev);
429 ret = info->mtd->suspend(info->mtd);
434 static int sa1100_mtd_resume(struct platform_device *dev)
436 struct sa_info *info = platform_get_drvdata(dev);
438 info->mtd->resume(info->mtd);
442 static void sa1100_mtd_shutdown(struct platform_device *dev)
444 struct sa_info *info = platform_get_drvdata(dev);
445 if (info && info->mtd->suspend(info->mtd) == 0)
446 info->mtd->resume(info->mtd);
449 #define sa1100_mtd_suspend NULL
450 #define sa1100_mtd_resume NULL
451 #define sa1100_mtd_shutdown NULL
454 static struct platform_driver sa1100_mtd_driver = {
455 .probe = sa1100_mtd_probe,
456 .remove = __exit_p(sa1100_mtd_remove),
457 .suspend = sa1100_mtd_suspend,
458 .resume = sa1100_mtd_resume,
459 .shutdown = sa1100_mtd_shutdown,
465 static int __init sa1100_mtd_init(void)
467 return platform_driver_register(&sa1100_mtd_driver);
470 static void __exit sa1100_mtd_exit(void)
472 platform_driver_unregister(&sa1100_mtd_driver);
475 module_init(sa1100_mtd_init);
476 module_exit(sa1100_mtd_exit);
478 MODULE_AUTHOR("Nicolas Pitre");
479 MODULE_DESCRIPTION("SA1100 CFI map driver");
480 MODULE_LICENSE("GPL");