1 /*======================================================================
2
3 $Id: slram.c,v 1.36 2005/11/07 11:14:25 gleixner Exp $
4
5 This driver provides a method to access memory not used by the kernel
6 itself (i.e. if the kernel commandline mem=xxx is used). To actually
7 use slram at least mtdblock or mtdchar is required (for block or
8 character device access).
9
10 Usage:
11
12 if compiled as loadable module:
13 modprobe slram map=<name>,<start>,<end/offset>
14 if statically linked into the kernel use the following kernel cmd.line
15 slram=<name>,<start>,<end/offset>
16
17 <name>: name of the device that will be listed in /proc/mtd
18 <start>: start of the memory region, decimal or hex (0xabcdef)
19 <end/offset>: end of the memory region. It's possible to use +0x1234
20 to specify the offset instead of the absolute address
21
22 NOTE:
23 With slram it's only possible to map a contigous memory region. Therfore
24 if there's a device mapped somewhere in the region specified slram will
25 fail to load (see kernel log if modprobe fails).
26
27 -
28
29 Jochen Schaeuble <psionic@psionic.de>
30
31 ======================================================================*/
32
33
34 #include <linux/module.h>
35 #include <asm/uaccess.h>
36 #include <linux/types.h>
37 #include <linux/kernel.h>
38 #include <linux/ptrace.h>
39 #include <linux/slab.h>
40 #include <linux/string.h>
41 #include <linux/timer.h>
42 #include <linux/major.h>
43 #include <linux/fs.h>
44 #include <linux/ioctl.h>
45 #include <linux/init.h>
46 #include <asm/io.h>
47 #include <asm/system.h>
48
49 #include <linux/mtd/mtd.h>
50
51 #define SLRAM_MAX_DEVICES_PARAMS 6 /* 3 parameters / device */
52 #define SLRAM_BLK_SZ 0x4000
53
54 #define T(fmt, args...) printk(KERN_DEBUG fmt, ## args)
55 #define E(fmt, args...) printk(KERN_NOTICE fmt, ## args)
56
57 typedef struct slram_priv {
58 u_char *start;
59 u_char *end;
60 } slram_priv_t;
61
62 typedef struct slram_mtd_list {
63 struct mtd_info *mtdinfo;
64 struct slram_mtd_list *next;
65 } slram_mtd_list_t;
66
67 #ifdef MODULE
68 static char *map[SLRAM_MAX_DEVICES_PARAMS];
69
70 module_param_array(map, charp, NULL, 0);
71 MODULE_PARM_DESC(map, "List of memory regions to map. \"map=<name>, <start>, <length / end>\"");
72 #else
73 static char *map;
74 #endif
75
76 static slram_mtd_list_t *slram_mtdlist = NULL;
77
78 static int slram_erase(struct mtd_info *, struct erase_info *);
79 static int slram_point(struct mtd_info *, loff_t, size_t, size_t *, u_char **);
80 static void slram_unpoint(struct mtd_info *, u_char *, loff_t, size_t);
81 static int slram_read(struct mtd_info *, loff_t, size_t, size_t *, u_char *);
82 static int slram_write(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
83
84 static int slram_erase(struct mtd_info *mtd, struct erase_info *instr)
85 {
86 slram_priv_t *priv = mtd->priv;
87
88 if (instr->addr + instr->len > mtd->size) {
89 return(-EINVAL);
90 }
91
92 memset(priv->start + instr->addr, 0xff, instr->len);
93
94 /* This'll catch a few races. Free the thing before returning :)
95 * I don't feel at all ashamed. This kind of thing is possible anyway
96 * with flash, but unlikely.
97 */
98
99 instr->state = MTD_ERASE_DONE;
100
101 mtd_erase_callback(instr);
102
103 return(0);
104 }
105
106 static int slram_point(struct mtd_info *mtd, loff_t from, size_t len,
107 size_t *retlen, u_char **mtdbuf)
108 {
109 slram_priv_t *priv = mtd->priv;
110
111 if (from + len > mtd->size)
112 return -EINVAL;
113
114 *mtdbuf = priv->start + from;
115 *retlen = len;
116 return(0);
117 }
118
119 static void slram_unpoint(struct mtd_info *mtd, u_char *addr, loff_t from, size_t len)
120 {
121 }
122
123 static int slram_read(struct mtd_info *mtd, loff_t from, size_t len,
124 size_t *retlen, u_char *buf)
125 {
126 slram_priv_t *priv = mtd->priv;
127
128 if (from > mtd->size)
129 return -EINVAL;
130
131 if (from + len > mtd->size)
132 len = mtd->size - from;
133
134 memcpy(buf, priv->start + from, len);
135
136 *retlen = len;
137 return(0);
138 }
139
140 static int slram_write(struct mtd_info *mtd, loff_t to, size_t len,
141 size_t *retlen, const u_char *buf)
142 {
143 slram_priv_t *priv = mtd->priv;
144
145 if (to + len > mtd->size)
146 return -EINVAL;
147
148 memcpy(priv->start + to, buf, len);
149
150 *retlen = len;
151 return(0);
152 }
153
154 /*====================================================================*/
155
156 static int register_device(char *name, unsigned long start, unsigned long length)
157 {
158 slram_mtd_list_t **curmtd;
159
160 curmtd = &slram_mtdlist;
161 while (*curmtd) {
162 curmtd = &(*curmtd)->next;
163 }
164
165 *curmtd = kmalloc(sizeof(slram_mtd_list_t), GFP_KERNEL);
166 if (!(*curmtd)) {
167 E("slram: Cannot allocate new MTD device.\n");
168 return(-ENOMEM);
169 }
170 (*curmtd)->mtdinfo = kzalloc(sizeof(struct mtd_info), GFP_KERNEL);
171 (*curmtd)->next = NULL;
172
173 if ((*curmtd)->mtdinfo) {
174 (*curmtd)->mtdinfo->priv =
175 kzalloc(sizeof(slram_priv_t), GFP_KERNEL);
176
177 if (!(*curmtd)->mtdinfo->priv) {
178 kfree((*curmtd)->mtdinfo);
179 (*curmtd)->mtdinfo = NULL;
180 }
181 }
182
183 if (!(*curmtd)->mtdinfo) {
184 E("slram: Cannot allocate new MTD device.\n");
185 return(-ENOMEM);
186 }
187
188 if (!(((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start =
189 ioremap(start, length))) {
190 E("slram: ioremap failed\n");
191 return -EIO;
192 }
193 ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->end =
194 ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start + length;
195
196
197 (*curmtd)->mtdinfo->name = name;
198 (*curmtd)->mtdinfo->size = length;
199 (*curmtd)->mtdinfo->flags = MTD_CAP_RAM;
200 (*curmtd)->mtdinfo->erase = slram_erase;
201 (*curmtd)->mtdinfo->point = slram_point;
202 (*curmtd)->mtdinfo->unpoint = slram_unpoint;
203 (*curmtd)->mtdinfo->read = slram_read;
204 (*curmtd)->mtdinfo->write = slram_write;
205 (*curmtd)->mtdinfo->owner = THIS_MODULE;
206 (*curmtd)->mtdinfo->type = MTD_RAM;
207 (*curmtd)->mtdinfo->erasesize = SLRAM_BLK_SZ;
208 (*curmtd)->mtdinfo->writesize = 1;
209
210 if (add_mtd_device((*curmtd)->mtdinfo)) {
211 E("slram: Failed to register new device\n");
212 iounmap(((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start);
213 kfree((*curmtd)->mtdinfo->priv);
214 kfree((*curmtd)->mtdinfo);
215 return(-EAGAIN);
216 }
217 T("slram: Registered device %s from %luKiB to %luKiB\n", name,
218 (start / 1024), ((start + length) / 1024));
219 T("slram: Mapped from 0x%p to 0x%p\n",
220 ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start,
221 ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->end);
222 return(0);
223 }
224
225 static void unregister_devices(void)
226 {
227 slram_mtd_list_t *nextitem;
228
229 while (slram_mtdlist) {
230 nextitem = slram_mtdlist->next;
231 del_mtd_device(slram_mtdlist->mtdinfo);
232 iounmap(((slram_priv_t *)slram_mtdlist->mtdinfo->priv)->start);
233 kfree(slram_mtdlist->mtdinfo->priv);
234 kfree(slram_mtdlist->mtdinfo);
235 kfree(slram_mtdlist);
236 slram_mtdlist = nextitem;
237 }
238 }
239
240 static unsigned long handle_unit(unsigned long value, char *unit)
241 {
242 if ((*unit == 'M') || (*unit == 'm')) {
243 return(value * 1024 * 1024);
244 } else if ((*unit == 'K') || (*unit == 'k')) {
245 return(value * 1024);
246 }
247 return(value);
248 }
249
250 static int parse_cmdline(char *devname, char *szstart, char *szlength)
251 {
252 char *buffer;
253 unsigned long devstart;
254 unsigned long devlength;
255
256 if ((!devname) || (!szstart) || (!szlength)) {
257 unregister_devices();
258 return(-EINVAL);
259 }
260
261 devstart = simple_strtoul(szstart, &buffer, 0);
262 devstart = handle_unit(devstart, buffer);
263
264 if (*(szlength) != '+') {
265 devlength = simple_strtoul(szlength, &buffer, 0);
266 devlength = handle_unit(devlength, buffer) - devstart;
267 } else {
268 devlength = simple_strtoul(szlength + 1, &buffer, 0);
269 devlength = handle_unit(devlength, buffer);
270 }
271 T("slram: devname=%s, devstart=0x%lx, devlength=0x%lx\n",
272 devname, devstart, devlength);
273 if ((devstart < 0) || (devlength < 0) || (devlength % SLRAM_BLK_SZ != 0)) {
274 E("slram: Illegal start / length parameter.\n");
275 return(-EINVAL);
276 }
277
278 if ((devstart = register_device(devname, devstart, devlength))){
279 unregister_devices();
280 return((int)devstart);
281 }
282 return(0);
283 }
284
285 #ifndef MODULE
286
287 static int __init mtd_slram_setup(char *str)
288 {
289 map = str;
290 return(1);
291 }
292
293 __setup("slram=", mtd_slram_setup);
294
295 #endif
296
297 static int init_slram(void)
298 {
299 char *devname;
300 int i;
301
302 #ifndef MODULE
303 char *devstart;
304 char *devlength;
305
306 i = 0;
307
308 if (!map) {
309 E("slram: not enough parameters.\n");
310 return(-EINVAL);
311 }
312 while (map) {
313 devname = devstart = devlength = NULL;
314
315 if (!(devname = strsep(&map, ","))) {
316 E("slram: No devicename specified.\n");
317 break;
318 }
319 T("slram: devname = %s\n", devname);
320 if ((!map) || (!(devstart = strsep(&map, ",")))) {
321 E("slram: No devicestart specified.\n");
322 }
323 T("slram: devstart = %s\n", devstart);
324 if ((!map) || (!(devlength = strsep(&map, ",")))) {
325 E("slram: No devicelength / -end specified.\n");
326 }
327 T("slram: devlength = %s\n", devlength);
328 if (parse_cmdline(devname, devstart, devlength) != 0) {
329 return(-EINVAL);
330 }
331 }
332 #else
333 int count;
334
335 for (count = 0; (map[count]) && (count < SLRAM_MAX_DEVICES_PARAMS);
336 count++) {
337 }
338
339 if ((count % 3 != 0) || (count == 0)) {
340 E("slram: not enough parameters.\n");
341 return(-EINVAL);
342 }
343 for (i = 0; i < (count / 3); i++) {
344 devname = map[i * 3];
345
346 if (parse_cmdline(devname, map[i * 3 + 1], map[i * 3 + 2])!=0) {
347 return(-EINVAL);
348 }
349
350 }
351 #endif /* !MODULE */
352
353 return(0);
354 }
355
356 static void __exit cleanup_slram(void)
357 {
358 unregister_devices();
359 }
360
361 module_init(init_slram);
362 module_exit(cleanup_slram);
363
364 MODULE_LICENSE("GPL");
365 MODULE_AUTHOR("Jochen Schaeuble <psionic@psionic.de>");
366 MODULE_DESCRIPTION("MTD driver for uncached system RAM");
367
|
This page was automatically generated by the
LXR engine.
|