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