19ec105cbb2d8dafec80ede9cd2d0841b2e3abc3
[sfrench/cifs-2.6.git] / drivers / mtd / maps / physmap.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Normal mappings of chips in physical memory
4  *
5  * Copyright (C) 2003 MontaVista Software Inc.
6  * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
7  *
8  * 031022 - [jsun] add run-time configure and partition setup
9  */
10
11 #include <linux/module.h>
12 #include <linux/types.h>
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/device.h>
17 #include <linux/platform_device.h>
18 #include <linux/mtd/mtd.h>
19 #include <linux/mtd/map.h>
20 #include <linux/mtd/partitions.h>
21 #include <linux/mtd/physmap.h>
22 #include <linux/mtd/concat.h>
23 #include <linux/io.h>
24
25 #define MAX_RESOURCES           4
26
27 struct physmap_flash_info {
28         struct mtd_info         *mtd[MAX_RESOURCES];
29         struct mtd_info         *cmtd;
30         struct map_info         map[MAX_RESOURCES];
31         spinlock_t              vpp_lock;
32         int                     vpp_refcnt;
33 };
34
35 static int physmap_flash_remove(struct platform_device *dev)
36 {
37         struct physmap_flash_info *info;
38         struct physmap_flash_data *physmap_data;
39         int i;
40
41         info = platform_get_drvdata(dev);
42         if (info == NULL)
43                 return 0;
44
45         physmap_data = dev_get_platdata(&dev->dev);
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
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 = dev_get_platdata(&pdev->dev);
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
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_get_platdata(&dev->dev);
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]->dev.parent = &dev->dev;
172         }
173
174         if (devices_found == 1) {
175                 info->cmtd = info->mtd[0];
176         } else if (devices_found > 1) {
177                 /*
178                  * We detected multiple devices. Concatenate them together.
179                  */
180                 info->cmtd = mtd_concat_create(info->mtd, devices_found, dev_name(&dev->dev));
181                 if (info->cmtd == NULL)
182                         err = -ENXIO;
183         }
184         if (err)
185                 goto err_out;
186
187         spin_lock_init(&info->vpp_lock);
188
189         part_types = physmap_data->part_probe_types ? : part_probe_types;
190
191         mtd_device_parse_register(info->cmtd, part_types, NULL,
192                                   physmap_data->parts, physmap_data->nr_parts);
193         return 0;
194
195 err_out:
196         physmap_flash_remove(dev);
197         return err;
198 }
199
200 #ifdef CONFIG_PM
201 static void physmap_flash_shutdown(struct platform_device *dev)
202 {
203         struct physmap_flash_info *info = platform_get_drvdata(dev);
204         int i;
205
206         for (i = 0; i < MAX_RESOURCES && info->mtd[i]; i++)
207                 if (mtd_suspend(info->mtd[i]) == 0)
208                         mtd_resume(info->mtd[i]);
209 }
210 #else
211 #define physmap_flash_shutdown NULL
212 #endif
213
214 static struct platform_driver physmap_flash_driver = {
215         .probe          = physmap_flash_probe,
216         .remove         = physmap_flash_remove,
217         .shutdown       = physmap_flash_shutdown,
218         .driver         = {
219                 .name   = "physmap-flash",
220         },
221 };
222
223
224 #ifdef CONFIG_MTD_PHYSMAP_COMPAT
225 static struct physmap_flash_data physmap_flash_data = {
226         .width          = CONFIG_MTD_PHYSMAP_BANKWIDTH,
227 };
228
229 static struct resource physmap_flash_resource = {
230         .start          = CONFIG_MTD_PHYSMAP_START,
231         .end            = CONFIG_MTD_PHYSMAP_START + CONFIG_MTD_PHYSMAP_LEN - 1,
232         .flags          = IORESOURCE_MEM,
233 };
234
235 static struct platform_device physmap_flash = {
236         .name           = "physmap-flash",
237         .id             = 0,
238         .dev            = {
239                 .platform_data  = &physmap_flash_data,
240         },
241         .num_resources  = 1,
242         .resource       = &physmap_flash_resource,
243 };
244 #endif
245
246 static int __init physmap_init(void)
247 {
248         int err;
249
250         err = platform_driver_register(&physmap_flash_driver);
251 #ifdef CONFIG_MTD_PHYSMAP_COMPAT
252         if (err == 0) {
253                 err = platform_device_register(&physmap_flash);
254                 if (err)
255                         platform_driver_unregister(&physmap_flash_driver);
256         }
257 #endif
258
259         return err;
260 }
261
262 static void __exit physmap_exit(void)
263 {
264 #ifdef CONFIG_MTD_PHYSMAP_COMPAT
265         platform_device_unregister(&physmap_flash);
266 #endif
267         platform_driver_unregister(&physmap_flash_driver);
268 }
269
270 module_init(physmap_init);
271 module_exit(physmap_exit);
272
273 MODULE_LICENSE("GPL");
274 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
275 MODULE_DESCRIPTION("Generic configurable MTD map driver");
276
277 /* legacy platform drivers can't hotplug or coldplg */
278 #ifndef CONFIG_MTD_PHYSMAP_COMPAT
279 /* work with hotplug and coldplug */
280 MODULE_ALIAS("platform:physmap-flash");
281 #endif