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