mtd: do not use mtd->lock, unlock and is_locked directly
[deliverable/linux.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];
1da177e4
LT
30};
31
73566edf
LB
32static int physmap_flash_remove(struct platform_device *dev)
33{
34 struct physmap_flash_info *info;
35 struct physmap_flash_data *physmap_data;
df66e716 36 int i;
73566edf
LB
37
38 info = platform_get_drvdata(dev);
39 if (info == NULL)
40 return 0;
41 platform_set_drvdata(dev, NULL);
42
43 physmap_data = dev->dev.platform_data;
44
8ce110ac 45 if (info->cmtd) {
984e6d8e 46 mtd_device_unregister(info->cmtd);
8ce110ac
HS
47 if (info->cmtd != info->mtd[0])
48 mtd_concat_destroy(info->cmtd);
8ce110ac 49 }
df66e716
SR
50
51 for (i = 0; i < MAX_RESOURCES; i++) {
e480814f 52 if (info->mtd[i] != NULL)
df66e716 53 map_destroy(info->mtd[i]);
73566edf 54 }
b7281ca2
MZ
55
56 if (physmap_data->exit)
57 physmap_data->exit(dev);
58
73566edf 59 return 0;
1da177e4 60}
1da177e4 61
667f390b
MZ
62static void physmap_set_vpp(struct map_info *map, int state)
63{
64 struct platform_device *pdev;
65 struct physmap_flash_data *physmap_data;
66
67 pdev = (struct platform_device *)map->map_priv_1;
68 physmap_data = pdev->dev.platform_data;
69
70 if (physmap_data->set_vpp)
71 physmap_data->set_vpp(pdev, state);
72}
73
d8140830
AK
74static const char *rom_probe_types[] = {
75 "cfi_probe",
76 "jedec_probe",
77 "qinfo_probe",
78 "map_rom",
79 NULL };
667f390b 80static const char *part_probe_types[] = { "cmdlinepart", "RedBoot", "afs",
b7281ca2 81 NULL };
73566edf
LB
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;
529688fe 88 const char **part_types;
df66e716
SR
89 int err = 0;
90 int i;
91 int devices_found = 0;
73566edf
LB
92
93 physmap_data = dev->dev.platform_data;
94 if (physmap_data == NULL)
95 return -ENODEV;
96
3136e903
AN
97 info = devm_kzalloc(&dev->dev, sizeof(struct physmap_flash_info),
98 GFP_KERNEL);
73566edf
LB
99 if (info == NULL) {
100 err = -ENOMEM;
101 goto err_out;
102 }
1da177e4 103
b7281ca2
MZ
104 if (physmap_data->init) {
105 err = physmap_data->init(dev);
106 if (err)
107 goto err_out;
108 }
109
73566edf 110 platform_set_drvdata(dev, info);
1da177e4 111
df66e716
SR
112 for (i = 0; i < dev->num_resources; i++) {
113 printk(KERN_NOTICE "physmap platform flash device: %.8llx at %.8llx\n",
1d90d2c3 114 (unsigned long long)resource_size(&dev->resource[i]),
df66e716
SR
115 (unsigned long long)dev->resource[i].start);
116
3136e903
AN
117 if (!devm_request_mem_region(&dev->dev,
118 dev->resource[i].start,
1d90d2c3 119 resource_size(&dev->resource[i]),
160bbab3 120 dev_name(&dev->dev))) {
df66e716
SR
121 dev_err(&dev->dev, "Could not reserve memory region\n");
122 err = -ENOMEM;
123 goto err_out;
124 }
1da177e4 125
160bbab3 126 info->map[i].name = dev_name(&dev->dev);
df66e716 127 info->map[i].phys = dev->resource[i].start;
1d90d2c3 128 info->map[i].size = resource_size(&dev->resource[i]);
df66e716 129 info->map[i].bankwidth = physmap_data->width;
667f390b 130 info->map[i].set_vpp = physmap_set_vpp;
d8140830 131 info->map[i].pfow_base = physmap_data->pfow_base;
667f390b 132 info->map[i].map_priv_1 = (unsigned long)dev;
df66e716 133
3136e903
AN
134 info->map[i].virt = devm_ioremap(&dev->dev, info->map[i].phys,
135 info->map[i].size);
df66e716
SR
136 if (info->map[i].virt == NULL) {
137 dev_err(&dev->dev, "Failed to ioremap flash region\n");
895fb494 138 err = -EIO;
df66e716
SR
139 goto err_out;
140 }
73566edf 141
df66e716 142 simple_map_init(&info->map[i]);
1da177e4 143
df66e716 144 probe_type = rom_probe_types;
78ef7fab
BS
145 if (physmap_data->probe_type == NULL) {
146 for (; info->mtd[i] == NULL && *probe_type != NULL; probe_type++)
147 info->mtd[i] = do_map_probe(*probe_type, &info->map[i]);
148 } else
149 info->mtd[i] = do_map_probe(physmap_data->probe_type, &info->map[i]);
150
df66e716
SR
151 if (info->mtd[i] == NULL) {
152 dev_err(&dev->dev, "map_probe failed\n");
153 err = -ENXIO;
154 goto err_out;
155 } else {
156 devices_found++;
157 }
158 info->mtd[i]->owner = THIS_MODULE;
87f39f04 159 info->mtd[i]->dev.parent = &dev->dev;
df66e716 160 }
73566edf 161
df66e716
SR
162 if (devices_found == 1) {
163 info->cmtd = info->mtd[0];
164 } else if (devices_found > 1) {
165 /*
166 * We detected multiple devices. Concatenate them together.
167 */
160bbab3 168 info->cmtd = mtd_concat_create(info->mtd, devices_found, dev_name(&dev->dev));
df66e716
SR
169 if (info->cmtd == NULL)
170 err = -ENXIO;
1da177e4 171 }
df66e716
SR
172 if (err)
173 goto err_out;
1da177e4 174
529688fe
JG
175 part_types = physmap_data->part_probe_types ? : part_probe_types;
176
177 mtd_device_parse_register(info->cmtd, part_types, 0,
d45fd121 178 physmap_data->parts, physmap_data->nr_parts);
73566edf
LB
179 return 0;
180
181err_out:
182 physmap_flash_remove(dev);
183 return err;
184}
185
17c2dae3 186#ifdef CONFIG_PM
17c2dae3
LB
187static void physmap_flash_shutdown(struct platform_device *dev)
188{
189 struct physmap_flash_info *info = platform_get_drvdata(dev);
df66e716
SR
190 int i;
191
4a5691c0 192 for (i = 0; i < MAX_RESOURCES && info->mtd[i]; i++)
7b249191 193 if (info->mtd[i]->suspend && info->mtd[i]->resume)
3fe4bae8 194 if (mtd_suspend(info->mtd[i]) == 0)
ead995f8 195 mtd_resume(info->mtd[i]);
17c2dae3 196}
d5476689 197#else
d5476689 198#define physmap_flash_shutdown NULL
17c2dae3
LB
199#endif
200
73566edf
LB
201static struct platform_driver physmap_flash_driver = {
202 .probe = physmap_flash_probe,
203 .remove = physmap_flash_remove,
17c2dae3 204 .shutdown = physmap_flash_shutdown,
73566edf
LB
205 .driver = {
206 .name = "physmap-flash",
41d867c9 207 .owner = THIS_MODULE,
73566edf
LB
208 },
209};
1da177e4 210
1da177e4 211
dcb3e137 212#ifdef CONFIG_MTD_PHYSMAP_COMPAT
73566edf
LB
213static struct physmap_flash_data physmap_flash_data = {
214 .width = CONFIG_MTD_PHYSMAP_BANKWIDTH,
215};
1da177e4 216
73566edf
LB
217static struct resource physmap_flash_resource = {
218 .start = CONFIG_MTD_PHYSMAP_START,
6d4f8224 219 .end = CONFIG_MTD_PHYSMAP_START + CONFIG_MTD_PHYSMAP_LEN - 1,
73566edf
LB
220 .flags = IORESOURCE_MEM,
221};
1da177e4 222
73566edf
LB
223static struct platform_device physmap_flash = {
224 .name = "physmap-flash",
225 .id = 0,
226 .dev = {
227 .platform_data = &physmap_flash_data,
228 },
229 .num_resources = 1,
230 .resource = &physmap_flash_resource,
231};
73566edf 232#endif
1da177e4 233
73566edf
LB
234static int __init physmap_init(void)
235{
236 int err;
237
238 err = platform_driver_register(&physmap_flash_driver);
dcb3e137 239#ifdef CONFIG_MTD_PHYSMAP_COMPAT
1ca5d2f0
HS
240 if (err == 0) {
241 err = platform_device_register(&physmap_flash);
242 if (err)
243 platform_driver_unregister(&physmap_flash_driver);
244 }
73566edf
LB
245#endif
246
247 return err;
1da177e4
LT
248}
249
73566edf
LB
250static void __exit physmap_exit(void)
251{
dcb3e137 252#ifdef CONFIG_MTD_PHYSMAP_COMPAT
73566edf
LB
253 platform_device_unregister(&physmap_flash);
254#endif
255 platform_driver_unregister(&physmap_flash_driver);
256}
1da177e4 257
73566edf
LB
258module_init(physmap_init);
259module_exit(physmap_exit);
1da177e4
LT
260
261MODULE_LICENSE("GPL");
262MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
263MODULE_DESCRIPTION("Generic configurable MTD map driver");
41d867c9
KS
264
265/* legacy platform drivers can't hotplug or coldplg */
dcb3e137 266#ifndef CONFIG_MTD_PHYSMAP_COMPAT
41d867c9
KS
267/* work with hotplug and coldplug */
268MODULE_ALIAS("platform:physmap-flash");
269#endif
This page took 0.424003 seconds and 5 git commands to generate.