blob: dfbf3f270cea9441a7261b69ca3c9f8aa9c87770 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Normal mappings of chips in physical memory
3 *
4 * Copyright (C) 2003 MontaVista Software Inc.
5 * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
6 *
7 * 031022 - [jsun] add run-time configure and partition setup
8 */
9
10#include <linux/module.h>
11#include <linux/types.h>
12#include <linux/kernel.h>
13#include <linux/init.h>
14#include <linux/slab.h>
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010015#include <linux/device.h>
16#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/mtd/mtd.h>
18#include <linux/mtd/map.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/mtd/partitions.h>
Adrian Bunk2b9175c2005-11-29 14:49:38 +000020#include <linux/mtd/physmap.h>
Stefan Roesedf66e712008-02-01 15:26:54 +010021#include <linux/mtd/concat.h>
Atsushi Nemoto3136e902008-11-26 10:26:29 +000022#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Stefan Roesedf66e712008-02-01 15:26:54 +010024#define MAX_RESOURCES 4
25
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010026struct physmap_flash_info {
Stefan Roesedf66e712008-02-01 15:26:54 +010027 struct mtd_info *mtd[MAX_RESOURCES];
28 struct mtd_info *cmtd;
29 struct map_info map[MAX_RESOURCES];
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010030#ifdef CONFIG_MTD_PARTITIONS
31 int nr_parts;
32 struct mtd_partition *parts;
33#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070034};
35
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010036static int physmap_flash_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010038 struct physmap_flash_info *info;
39 struct physmap_flash_data *physmap_data;
Stefan Roesedf66e712008-02-01 15:26:54 +010040 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010042 info = platform_get_drvdata(dev);
43 if (info == NULL)
44 return 0;
45 platform_set_drvdata(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010047 physmap_data = dev->dev.platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Stefan Roesedf66e712008-02-01 15:26:54 +010049#ifdef CONFIG_MTD_CONCAT
50 if (info->cmtd != info->mtd[0]) {
51 del_mtd_device(info->cmtd);
52 mtd_concat_destroy(info->cmtd);
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010053 }
Stefan Roesedf66e712008-02-01 15:26:54 +010054#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Stefan Roesedf66e712008-02-01 15:26:54 +010056 for (i = 0; i < MAX_RESOURCES; i++) {
57 if (info->mtd[i] != NULL) {
58#ifdef CONFIG_MTD_PARTITIONS
59 if (info->nr_parts) {
60 del_mtd_partitions(info->mtd[i]);
61 kfree(info->parts);
62 } else if (physmap_data->nr_parts) {
63 del_mtd_partitions(info->mtd[i]);
64 } else {
65 del_mtd_device(info->mtd[i]);
66 }
67#else
68 del_mtd_device(info->mtd[i]);
69#endif
70 map_destroy(info->mtd[i]);
71 }
Stefan Roesedf66e712008-02-01 15:26:54 +010072 }
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010073 return 0;
74}
75
76static const char *rom_probe_types[] = { "cfi_probe", "jedec_probe", "map_rom", NULL };
77#ifdef CONFIG_MTD_PARTITIONS
78static const char *part_probe_types[] = { "cmdlinepart", "RedBoot", NULL };
79#endif
80
81static int physmap_flash_probe(struct platform_device *dev)
82{
83 struct physmap_flash_data *physmap_data;
84 struct physmap_flash_info *info;
85 const char **probe_type;
Stefan Roesedf66e712008-02-01 15:26:54 +010086 int err = 0;
87 int i;
88 int devices_found = 0;
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010089
90 physmap_data = dev->dev.platform_data;
91 if (physmap_data == NULL)
92 return -ENODEV;
93
Atsushi Nemoto3136e902008-11-26 10:26:29 +000094 info = devm_kzalloc(&dev->dev, sizeof(struct physmap_flash_info),
95 GFP_KERNEL);
Lennert Buytenhek73566ed2006-05-07 17:16:36 +010096 if (info == NULL) {
97 err = -ENOMEM;
98 goto err_out;
99 }
Lennert Buytenhek73566ed2006-05-07 17:16:36 +0100100
101 platform_set_drvdata(dev, info);
102
Stefan Roesedf66e712008-02-01 15:26:54 +0100103 for (i = 0; i < dev->num_resources; i++) {
104 printk(KERN_NOTICE "physmap platform flash device: %.8llx at %.8llx\n",
105 (unsigned long long)(dev->resource[i].end - dev->resource[i].start + 1),
106 (unsigned long long)dev->resource[i].start);
107
Atsushi Nemoto3136e902008-11-26 10:26:29 +0000108 if (!devm_request_mem_region(&dev->dev,
109 dev->resource[i].start,
110 dev->resource[i].end - dev->resource[i].start + 1,
111 dev->dev.bus_id)) {
Stefan Roesedf66e712008-02-01 15:26:54 +0100112 dev_err(&dev->dev, "Could not reserve memory region\n");
113 err = -ENOMEM;
114 goto err_out;
115 }
116
117 info->map[i].name = dev->dev.bus_id;
118 info->map[i].phys = dev->resource[i].start;
119 info->map[i].size = dev->resource[i].end - dev->resource[i].start + 1;
120 info->map[i].bankwidth = physmap_data->width;
121 info->map[i].set_vpp = physmap_data->set_vpp;
122
Atsushi Nemoto3136e902008-11-26 10:26:29 +0000123 info->map[i].virt = devm_ioremap(&dev->dev, info->map[i].phys,
124 info->map[i].size);
Stefan Roesedf66e712008-02-01 15:26:54 +0100125 if (info->map[i].virt == NULL) {
126 dev_err(&dev->dev, "Failed to ioremap flash region\n");
127 err = EIO;
128 goto err_out;
129 }
130
131 simple_map_init(&info->map[i]);
132
133 probe_type = rom_probe_types;
134 for (; info->mtd[i] == NULL && *probe_type != NULL; probe_type++)
135 info->mtd[i] = do_map_probe(*probe_type, &info->map[i]);
136 if (info->mtd[i] == NULL) {
137 dev_err(&dev->dev, "map_probe failed\n");
138 err = -ENXIO;
139 goto err_out;
140 } else {
141 devices_found++;
142 }
143 info->mtd[i]->owner = THIS_MODULE;
Lennert Buytenhek73566ed2006-05-07 17:16:36 +0100144 }
145
Stefan Roesedf66e712008-02-01 15:26:54 +0100146 if (devices_found == 1) {
147 info->cmtd = info->mtd[0];
148 } else if (devices_found > 1) {
149 /*
150 * We detected multiple devices. Concatenate them together.
151 */
152#ifdef CONFIG_MTD_CONCAT
153 info->cmtd = mtd_concat_create(info->mtd, devices_found, dev->dev.bus_id);
154 if (info->cmtd == NULL)
155 err = -ENXIO;
156#else
157 printk(KERN_ERR "physmap-flash: multiple devices "
158 "found but MTD concat support disabled.\n");
Lennert Buytenhek73566ed2006-05-07 17:16:36 +0100159 err = -ENXIO;
Stefan Roesedf66e712008-02-01 15:26:54 +0100160#endif
Lennert Buytenhek73566ed2006-05-07 17:16:36 +0100161 }
Stefan Roesedf66e712008-02-01 15:26:54 +0100162 if (err)
163 goto err_out;
Lennert Buytenhek73566ed2006-05-07 17:16:36 +0100164
165#ifdef CONFIG_MTD_PARTITIONS
Stefan Roesedf66e712008-02-01 15:26:54 +0100166 err = parse_mtd_partitions(info->cmtd, part_probe_types, &info->parts, 0);
Lennert Buytenhek73566ed2006-05-07 17:16:36 +0100167 if (err > 0) {
Stefan Roesedf66e712008-02-01 15:26:54 +0100168 add_mtd_partitions(info->cmtd, info->parts, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return 0;
170 }
171
Lennert Buytenhek73566ed2006-05-07 17:16:36 +0100172 if (physmap_data->nr_parts) {
173 printk(KERN_NOTICE "Using physmap partition information\n");
Stefan Roesedf66e712008-02-01 15:26:54 +0100174 add_mtd_partitions(info->cmtd, physmap_data->parts,
175 physmap_data->nr_parts);
Lennert Buytenhek73566ed2006-05-07 17:16:36 +0100176 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Stefan Roesedf66e712008-02-01 15:26:54 +0100180 add_mtd_device(info->cmtd);
Lennert Buytenhek73566ed2006-05-07 17:16:36 +0100181 return 0;
182
183err_out:
184 physmap_flash_remove(dev);
185 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
Lennert Buytenhek17c2dae2006-09-21 23:16:48 +0200188#ifdef CONFIG_PM
189static int physmap_flash_suspend(struct platform_device *dev, pm_message_t state)
190{
191 struct physmap_flash_info *info = platform_get_drvdata(dev);
192 int ret = 0;
Stefan Roesedf66e712008-02-01 15:26:54 +0100193 int i;
Lennert Buytenhek17c2dae2006-09-21 23:16:48 +0200194
Anton Vorontsov4a5691c2008-03-28 14:16:09 -0700195 for (i = 0; i < MAX_RESOURCES && info->mtd[i]; i++)
Uwe Kleine-König4b5e33a2008-07-22 09:39:01 +0200196 if (info->mtd[i]->suspend) {
197 ret = info->mtd[i]->suspend(info->mtd[i]);
198 if (ret)
199 goto fail;
200 }
201
202 return 0;
203fail:
204 for (--i; i >= 0; --i)
205 if (info->mtd[i]->suspend) {
206 BUG_ON(!info->mtd[i]->resume);
207 info->mtd[i]->resume(info->mtd[i]);
208 }
Lennert Buytenhek17c2dae2006-09-21 23:16:48 +0200209
210 return ret;
211}
212
213static int physmap_flash_resume(struct platform_device *dev)
214{
215 struct physmap_flash_info *info = platform_get_drvdata(dev);
Stefan Roesedf66e712008-02-01 15:26:54 +0100216 int i;
217
Anton Vorontsov4a5691c2008-03-28 14:16:09 -0700218 for (i = 0; i < MAX_RESOURCES && info->mtd[i]; i++)
Robert Jarzmik7b249192008-07-22 09:39:00 +0200219 if (info->mtd[i]->resume)
220 info->mtd[i]->resume(info->mtd[i]);
Anton Vorontsov4a5691c2008-03-28 14:16:09 -0700221
Lennert Buytenhek17c2dae2006-09-21 23:16:48 +0200222 return 0;
223}
224
225static void physmap_flash_shutdown(struct platform_device *dev)
226{
227 struct physmap_flash_info *info = platform_get_drvdata(dev);
Stefan Roesedf66e712008-02-01 15:26:54 +0100228 int i;
229
Anton Vorontsov4a5691c2008-03-28 14:16:09 -0700230 for (i = 0; i < MAX_RESOURCES && info->mtd[i]; i++)
Robert Jarzmik7b249192008-07-22 09:39:00 +0200231 if (info->mtd[i]->suspend && info->mtd[i]->resume)
232 if (info->mtd[i]->suspend(info->mtd[i]) == 0)
233 info->mtd[i]->resume(info->mtd[i]);
Lennert Buytenhek17c2dae2006-09-21 23:16:48 +0200234}
akpm@linux-foundation.orgd5476682008-02-03 12:56:03 -0800235#else
236#define physmap_flash_suspend NULL
237#define physmap_flash_resume NULL
238#define physmap_flash_shutdown NULL
Lennert Buytenhek17c2dae2006-09-21 23:16:48 +0200239#endif
240
Lennert Buytenhek73566ed2006-05-07 17:16:36 +0100241static struct platform_driver physmap_flash_driver = {
242 .probe = physmap_flash_probe,
243 .remove = physmap_flash_remove,
Lennert Buytenhek17c2dae2006-09-21 23:16:48 +0200244 .suspend = physmap_flash_suspend,
245 .resume = physmap_flash_resume,
246 .shutdown = physmap_flash_shutdown,
Lennert Buytenhek73566ed2006-05-07 17:16:36 +0100247 .driver = {
248 .name = "physmap-flash",
Kay Sievers41d867c2008-04-18 13:44:26 -0700249 .owner = THIS_MODULE,
Lennert Buytenhek73566ed2006-05-07 17:16:36 +0100250 },
251};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253
Lennert Buytenhek73566ed2006-05-07 17:16:36 +0100254#ifdef CONFIG_MTD_PHYSMAP_LEN
255#if CONFIG_MTD_PHYSMAP_LEN != 0
256#warning using PHYSMAP compat code
257#define PHYSMAP_COMPAT
258#endif
259#endif
260
261#ifdef PHYSMAP_COMPAT
262static struct physmap_flash_data physmap_flash_data = {
263 .width = CONFIG_MTD_PHYSMAP_BANKWIDTH,
264};
265
266static struct resource physmap_flash_resource = {
267 .start = CONFIG_MTD_PHYSMAP_START,
Sascha Hauer6d4f8222006-06-27 14:38:15 +0100268 .end = CONFIG_MTD_PHYSMAP_START + CONFIG_MTD_PHYSMAP_LEN - 1,
Lennert Buytenhek73566ed2006-05-07 17:16:36 +0100269 .flags = IORESOURCE_MEM,
270};
271
272static struct platform_device physmap_flash = {
273 .name = "physmap-flash",
274 .id = 0,
275 .dev = {
276 .platform_data = &physmap_flash_data,
277 },
278 .num_resources = 1,
279 .resource = &physmap_flash_resource,
280};
281
282void physmap_configure(unsigned long addr, unsigned long size,
283 int bankwidth, void (*set_vpp)(struct map_info *, int))
284{
285 physmap_flash_resource.start = addr;
286 physmap_flash_resource.end = addr + size - 1;
287 physmap_flash_data.width = bankwidth;
288 physmap_flash_data.set_vpp = set_vpp;
289}
290
291#ifdef CONFIG_MTD_PARTITIONS
292void physmap_set_partitions(struct mtd_partition *parts, int num_parts)
293{
294 physmap_flash_data.nr_parts = num_parts;
295 physmap_flash_data.parts = parts;
296}
297#endif
298#endif
299
300static int __init physmap_init(void)
301{
302 int err;
303
304 err = platform_driver_register(&physmap_flash_driver);
305#ifdef PHYSMAP_COMPAT
306 if (err == 0)
307 platform_device_register(&physmap_flash);
308#endif
309
310 return err;
311}
312
313static void __exit physmap_exit(void)
314{
315#ifdef PHYSMAP_COMPAT
316 platform_device_unregister(&physmap_flash);
317#endif
318 platform_driver_unregister(&physmap_flash_driver);
319}
320
321module_init(physmap_init);
322module_exit(physmap_exit);
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324MODULE_LICENSE("GPL");
325MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
326MODULE_DESCRIPTION("Generic configurable MTD map driver");
Kay Sievers41d867c2008-04-18 13:44:26 -0700327
328/* legacy platform drivers can't hotplug or coldplg */
329#ifndef PHYSMAP_COMPAT
330/* work with hotplug and coldplug */
331MODULE_ALIAS("platform:physmap-flash");
332#endif
333