1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <linux/module.h>
25#include <linux/types.h>
26#include <linux/kernel.h>
27#include <linux/string.h>
28#include <linux/ioport.h>
29#include <linux/device.h>
30#include <linux/slab.h>
31#include <linux/platform_device.h>
32
33#include <linux/mtd/mtd.h>
34#include <linux/mtd/map.h>
35#include <linux/mtd/partitions.h>
36#include <linux/mtd/plat-ram.h>
37
38#include <asm/io.h>
39
40
41
42struct platram_info {
43 struct device *dev;
44 struct mtd_info *mtd;
45 struct map_info map;
46 struct resource *area;
47 struct platdata_mtd_ram *pdata;
48};
49
50
51
52
53
54
55static inline struct platram_info *to_platram_info(struct platform_device *dev)
56{
57 return platform_get_drvdata(dev);
58}
59
60
61
62
63
64
65
66
67
68static inline void platram_setrw(struct platram_info *info, int to)
69{
70 if (info->pdata == NULL)
71 return;
72
73 if (info->pdata->set_rw != NULL)
74 (info->pdata->set_rw)(info->dev, to);
75}
76
77
78
79
80
81
82static int platram_remove(struct platform_device *pdev)
83{
84 struct platram_info *info = to_platram_info(pdev);
85
86 dev_dbg(&pdev->dev, "removing device\n");
87
88 if (info == NULL)
89 return 0;
90
91 if (info->mtd) {
92 mtd_device_unregister(info->mtd);
93 map_destroy(info->mtd);
94 }
95
96
97
98 platram_setrw(info, PLATRAM_RO);
99
100
101
102 if (info->area) {
103 release_resource(info->area);
104 kfree(info->area);
105 }
106
107 if (info->map.virt != NULL)
108 iounmap(info->map.virt);
109
110 kfree(info);
111
112 return 0;
113}
114
115
116
117
118
119
120
121static int platram_probe(struct platform_device *pdev)
122{
123 struct platdata_mtd_ram *pdata;
124 struct platram_info *info;
125 struct resource *res;
126 int err = 0;
127
128 dev_dbg(&pdev->dev, "probe entered\n");
129
130 if (dev_get_platdata(&pdev->dev) == NULL) {
131 dev_err(&pdev->dev, "no platform data supplied\n");
132 err = -ENOENT;
133 goto exit_error;
134 }
135
136 pdata = dev_get_platdata(&pdev->dev);
137
138 info = kzalloc(sizeof(*info), GFP_KERNEL);
139 if (info == NULL) {
140 err = -ENOMEM;
141 goto exit_error;
142 }
143
144 platform_set_drvdata(pdev, info);
145
146 info->dev = &pdev->dev;
147 info->pdata = pdata;
148
149
150
151 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
152
153 if (res == NULL) {
154 dev_err(&pdev->dev, "no memory resource specified\n");
155 err = -ENOENT;
156 goto exit_free;
157 }
158
159 dev_dbg(&pdev->dev, "got platform resource %p (0x%llx)\n", res,
160 (unsigned long long)res->start);
161
162
163
164 info->map.phys = res->start;
165 info->map.size = resource_size(res);
166 info->map.name = pdata->mapname != NULL ?
167 (char *)pdata->mapname : (char *)pdev->name;
168 info->map.bankwidth = pdata->bankwidth;
169
170
171
172 info->area = request_mem_region(res->start, info->map.size, pdev->name);
173 if (info->area == NULL) {
174 dev_err(&pdev->dev, "failed to request memory region\n");
175 err = -EIO;
176 goto exit_free;
177 }
178
179
180
181 info->map.virt = ioremap(res->start, info->map.size);
182 dev_dbg(&pdev->dev, "virt %p, %lu bytes\n", info->map.virt, info->map.size);
183
184 if (info->map.virt == NULL) {
185 dev_err(&pdev->dev, "failed to ioremap() region\n");
186 err = -EIO;
187 goto exit_free;
188 }
189
190 simple_map_init(&info->map);
191
192 dev_dbg(&pdev->dev, "initialised map, probing for mtd\n");
193
194
195
196
197 if (pdata->map_probes) {
198 const char * const *map_probes = pdata->map_probes;
199
200 for ( ; !info->mtd && *map_probes; map_probes++)
201 info->mtd = do_map_probe(*map_probes , &info->map);
202 }
203
204 else
205 info->mtd = do_map_probe("map_ram", &info->map);
206
207 if (info->mtd == NULL) {
208 dev_err(&pdev->dev, "failed to probe for map_ram\n");
209 err = -ENOMEM;
210 goto exit_free;
211 }
212
213 info->mtd->owner = THIS_MODULE;
214 info->mtd->dev.parent = &pdev->dev;
215
216 platram_setrw(info, PLATRAM_RW);
217
218
219
220
221 err = mtd_device_parse_register(info->mtd, pdata->probes, NULL,
222 pdata->partitions,
223 pdata->nr_partitions);
224 if (!err)
225 dev_info(&pdev->dev, "registered mtd device\n");
226
227 if (pdata->nr_partitions) {
228
229 err = mtd_device_register(info->mtd, NULL, 0);
230 if (err) {
231 dev_err(&pdev->dev,
232 "failed to register the entire device\n");
233 }
234 }
235
236 return err;
237
238 exit_free:
239 platram_remove(pdev);
240 exit_error:
241 return err;
242}
243
244
245
246
247MODULE_ALIAS("platform:mtd-ram");
248
249static struct platform_driver platram_driver = {
250 .probe = platram_probe,
251 .remove = platram_remove,
252 .driver = {
253 .name = "mtd-ram",
254 .owner = THIS_MODULE,
255 },
256};
257
258module_platform_driver(platram_driver);
259
260MODULE_LICENSE("GPL");
261MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
262MODULE_DESCRIPTION("MTD platform RAM map driver");
263