]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/mtd/maps/physmap.c
Merge branch 'for-linus' of master.kernel.org:/home/rmk/linux-2.6-arm
[mirror_ubuntu-artful-kernel.git] / drivers / mtd / maps / physmap.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 * 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>
73566edf
LB
15#include <linux/device.h>
16#include <linux/platform_device.h>
1da177e4
LT
17#include <linux/mtd/mtd.h>
18#include <linux/mtd/map.h>
1da177e4 19#include <linux/mtd/partitions.h>
2b9175c1 20#include <linux/mtd/physmap.h>
df66e716 21#include <linux/mtd/concat.h>
3136e903 22#include <linux/io.h>
1da177e4 23
df66e716
SR
24#define MAX_RESOURCES 4
25
73566edf 26struct physmap_flash_info {
df66e716
SR
27 struct mtd_info *mtd[MAX_RESOURCES];
28 struct mtd_info *cmtd;
29 struct map_info map[MAX_RESOURCES];
73566edf
LB
30#ifdef CONFIG_MTD_PARTITIONS
31 int nr_parts;
e480814f 32 struct mtd_partition *parts;
73566edf 33#endif
1da177e4
LT
34};
35
73566edf
LB
36static int physmap_flash_remove(struct platform_device *dev)
37{
38 struct physmap_flash_info *info;
39 struct physmap_flash_data *physmap_data;
df66e716 40 int i;
73566edf
LB
41
42 info = platform_get_drvdata(dev);
43 if (info == NULL)
44 return 0;
45 platform_set_drvdata(dev, NULL);
46
47 physmap_data = dev->dev.platform_data;
48
8ce110ac
HS
49 if (info->cmtd) {
50#ifdef CONFIG_MTD_PARTITIONS
4b56ffca 51 if (info->nr_parts || physmap_data->nr_parts) {
d58ab5cf 52 del_mtd_partitions(info->cmtd);
4b56ffca
HS
53
54 if (info->nr_parts)
55 kfree(info->parts);
56 } else {
d58ab5cf 57 del_mtd_device(info->cmtd);
4b56ffca 58 }
8ce110ac 59#else
d58ab5cf 60 del_mtd_device(info->cmtd);
8ce110ac 61#endif
8ce110ac
HS
62 if (info->cmtd != info->mtd[0])
63 mtd_concat_destroy(info->cmtd);
8ce110ac 64 }
df66e716
SR
65
66 for (i = 0; i < MAX_RESOURCES; i++) {
e480814f 67 if (info->mtd[i] != NULL)
df66e716 68 map_destroy(info->mtd[i]);
73566edf 69 }
73566edf 70 return 0;
1da177e4 71}
1da177e4 72
d8140830
AK
73static const char *rom_probe_types[] = {
74 "cfi_probe",
75 "jedec_probe",
76 "qinfo_probe",
77 "map_rom",
78 NULL };
73566edf
LB
79#ifdef CONFIG_MTD_PARTITIONS
80static const char *part_probe_types[] = { "cmdlinepart", "RedBoot", NULL };
81#endif
82
83static int physmap_flash_probe(struct platform_device *dev)
1da177e4 84{
73566edf
LB
85 struct physmap_flash_data *physmap_data;
86 struct physmap_flash_info *info;
87 const char **probe_type;
df66e716
SR
88 int err = 0;
89 int i;
90 int devices_found = 0;
73566edf
LB
91
92 physmap_data = dev->dev.platform_data;
93 if (physmap_data == NULL)
94 return -ENODEV;
95
3136e903
AN
96 info = devm_kzalloc(&dev->dev, sizeof(struct physmap_flash_info),
97 GFP_KERNEL);
73566edf
LB
98 if (info == NULL) {
99 err = -ENOMEM;
100 goto err_out;
101 }
1da177e4 102
73566edf 103 platform_set_drvdata(dev, info);
1da177e4 104
df66e716
SR
105 for (i = 0; i < dev->num_resources; i++) {
106 printk(KERN_NOTICE "physmap platform flash device: %.8llx at %.8llx\n",
1d90d2c3 107 (unsigned long long)resource_size(&dev->resource[i]),
df66e716
SR
108 (unsigned long long)dev->resource[i].start);
109
3136e903
AN
110 if (!devm_request_mem_region(&dev->dev,
111 dev->resource[i].start,
1d90d2c3 112 resource_size(&dev->resource[i]),
160bbab3 113 dev_name(&dev->dev))) {
df66e716
SR
114 dev_err(&dev->dev, "Could not reserve memory region\n");
115 err = -ENOMEM;
116 goto err_out;
117 }
1da177e4 118
160bbab3 119 info->map[i].name = dev_name(&dev->dev);
df66e716 120 info->map[i].phys = dev->resource[i].start;
1d90d2c3 121 info->map[i].size = resource_size(&dev->resource[i]);
df66e716
SR
122 info->map[i].bankwidth = physmap_data->width;
123 info->map[i].set_vpp = physmap_data->set_vpp;
d8140830 124 info->map[i].pfow_base = physmap_data->pfow_base;
df66e716 125
3136e903
AN
126 info->map[i].virt = devm_ioremap(&dev->dev, info->map[i].phys,
127 info->map[i].size);
df66e716
SR
128 if (info->map[i].virt == NULL) {
129 dev_err(&dev->dev, "Failed to ioremap flash region\n");
895fb494 130 err = -EIO;
df66e716
SR
131 goto err_out;
132 }
73566edf 133
df66e716 134 simple_map_init(&info->map[i]);
1da177e4 135
df66e716 136 probe_type = rom_probe_types;
78ef7fab
BS
137 if (physmap_data->probe_type == NULL) {
138 for (; info->mtd[i] == NULL && *probe_type != NULL; probe_type++)
139 info->mtd[i] = do_map_probe(*probe_type, &info->map[i]);
140 } else
141 info->mtd[i] = do_map_probe(physmap_data->probe_type, &info->map[i]);
142
df66e716
SR
143 if (info->mtd[i] == NULL) {
144 dev_err(&dev->dev, "map_probe failed\n");
145 err = -ENXIO;
146 goto err_out;
147 } else {
148 devices_found++;
149 }
150 info->mtd[i]->owner = THIS_MODULE;
87f39f04 151 info->mtd[i]->dev.parent = &dev->dev;
df66e716 152 }
73566edf 153
df66e716
SR
154 if (devices_found == 1) {
155 info->cmtd = info->mtd[0];
156 } else if (devices_found > 1) {
157 /*
158 * We detected multiple devices. Concatenate them together.
159 */
160bbab3 160 info->cmtd = mtd_concat_create(info->mtd, devices_found, dev_name(&dev->dev));
df66e716
SR
161 if (info->cmtd == NULL)
162 err = -ENXIO;
1da177e4 163 }
df66e716
SR
164 if (err)
165 goto err_out;
1da177e4 166
8ce110ac
HS
167#ifdef CONFIG_MTD_PARTITIONS
168 err = parse_mtd_partitions(info->cmtd, part_probe_types,
169 &info->parts, 0);
170 if (err > 0) {
171 add_mtd_partitions(info->cmtd, info->parts, err);
172 info->nr_parts = err;
173 return 0;
174 }
1da177e4 175
8ce110ac
HS
176 if (physmap_data->nr_parts) {
177 printk(KERN_NOTICE "Using physmap partition information\n");
178 add_mtd_partitions(info->cmtd, physmap_data->parts,
179 physmap_data->nr_parts);
180 return 0;
73566edf 181 }
8ce110ac 182#endif
73566edf 183
df66e716 184 add_mtd_device(info->cmtd);
73566edf
LB
185 return 0;
186
187err_out:
188 physmap_flash_remove(dev);
189 return err;
190}
191
17c2dae3 192#ifdef CONFIG_PM
17c2dae3
LB
193static void physmap_flash_shutdown(struct platform_device *dev)
194{
195 struct physmap_flash_info *info = platform_get_drvdata(dev);
df66e716
SR
196 int i;
197
4a5691c0 198 for (i = 0; i < MAX_RESOURCES && info->mtd[i]; i++)
7b249191
RJ
199 if (info->mtd[i]->suspend && info->mtd[i]->resume)
200 if (info->mtd[i]->suspend(info->mtd[i]) == 0)
201 info->mtd[i]->resume(info->mtd[i]);
17c2dae3 202}
d5476689 203#else
d5476689 204#define physmap_flash_shutdown NULL
17c2dae3
LB
205#endif
206
73566edf
LB
207static struct platform_driver physmap_flash_driver = {
208 .probe = physmap_flash_probe,
209 .remove = physmap_flash_remove,
17c2dae3 210 .shutdown = physmap_flash_shutdown,
73566edf
LB
211 .driver = {
212 .name = "physmap-flash",
41d867c9 213 .owner = THIS_MODULE,
73566edf
LB
214 },
215};
1da177e4 216
1da177e4 217
dcb3e137 218#ifdef CONFIG_MTD_PHYSMAP_COMPAT
73566edf
LB
219static struct physmap_flash_data physmap_flash_data = {
220 .width = CONFIG_MTD_PHYSMAP_BANKWIDTH,
221};
1da177e4 222
73566edf
LB
223static struct resource physmap_flash_resource = {
224 .start = CONFIG_MTD_PHYSMAP_START,
6d4f8224 225 .end = CONFIG_MTD_PHYSMAP_START + CONFIG_MTD_PHYSMAP_LEN - 1,
73566edf
LB
226 .flags = IORESOURCE_MEM,
227};
1da177e4 228
73566edf
LB
229static struct platform_device physmap_flash = {
230 .name = "physmap-flash",
231 .id = 0,
232 .dev = {
233 .platform_data = &physmap_flash_data,
234 },
235 .num_resources = 1,
236 .resource = &physmap_flash_resource,
237};
238
239void physmap_configure(unsigned long addr, unsigned long size,
240 int bankwidth, void (*set_vpp)(struct map_info *, int))
1da177e4 241{
73566edf
LB
242 physmap_flash_resource.start = addr;
243 physmap_flash_resource.end = addr + size - 1;
244 physmap_flash_data.width = bankwidth;
245 physmap_flash_data.set_vpp = set_vpp;
246}
247
1da177e4 248#ifdef CONFIG_MTD_PARTITIONS
73566edf
LB
249void physmap_set_partitions(struct mtd_partition *parts, int num_parts)
250{
251 physmap_flash_data.nr_parts = num_parts;
252 physmap_flash_data.parts = parts;
253}
254#endif
1da177e4 255#endif
1da177e4 256
73566edf
LB
257static int __init physmap_init(void)
258{
259 int err;
260
261 err = platform_driver_register(&physmap_flash_driver);
dcb3e137 262#ifdef CONFIG_MTD_PHYSMAP_COMPAT
1ca5d2f0
HS
263 if (err == 0) {
264 err = platform_device_register(&physmap_flash);
265 if (err)
266 platform_driver_unregister(&physmap_flash_driver);
267 }
73566edf
LB
268#endif
269
270 return err;
1da177e4
LT
271}
272
73566edf
LB
273static void __exit physmap_exit(void)
274{
dcb3e137 275#ifdef CONFIG_MTD_PHYSMAP_COMPAT
73566edf
LB
276 platform_device_unregister(&physmap_flash);
277#endif
278 platform_driver_unregister(&physmap_flash_driver);
279}
1da177e4 280
73566edf
LB
281module_init(physmap_init);
282module_exit(physmap_exit);
1da177e4
LT
283
284MODULE_LICENSE("GPL");
285MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
286MODULE_DESCRIPTION("Generic configurable MTD map driver");
41d867c9
KS
287
288/* legacy platform drivers can't hotplug or coldplg */
dcb3e137 289#ifndef CONFIG_MTD_PHYSMAP_COMPAT
41d867c9
KS
290/* work with hotplug and coldplug */
291MODULE_ALIAS("platform:physmap-flash");
292#endif