cb86db746f28c91f552462696b10d5c99f23c07d
[pandora-kernel.git] / drivers / mtd / devices / slram.c
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 *, void **,
80                 resource_size_t *);
81 static void slram_unpoint(struct mtd_info *, loff_t, size_t);
82 static int slram_read(struct mtd_info *, loff_t, size_t, size_t *, u_char *);
83 static int slram_write(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
84
85 static int slram_erase(struct mtd_info *mtd, struct erase_info *instr)
86 {
87         slram_priv_t *priv = mtd->priv;
88
89         if (instr->addr + instr->len > mtd->size) {
90                 return(-EINVAL);
91         }
92
93         memset(priv->start + instr->addr, 0xff, instr->len);
94
95         /* This'll catch a few races. Free the thing before returning :)
96          * I don't feel at all ashamed. This kind of thing is possible anyway
97          * with flash, but unlikely.
98          */
99
100         instr->state = MTD_ERASE_DONE;
101
102         mtd_erase_callback(instr);
103
104         return(0);
105 }
106
107 static int slram_point(struct mtd_info *mtd, loff_t from, size_t len,
108                 size_t *retlen, void **virt, resource_size_t *phys)
109 {
110         slram_priv_t *priv = mtd->priv;
111
112         /* can we return a physical address with this driver? */
113         if (phys)
114                 return -EINVAL;
115
116         if (from + len > mtd->size)
117                 return -EINVAL;
118
119         *virt = priv->start + from;
120         *retlen = len;
121         return(0);
122 }
123
124 static void slram_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
125 {
126 }
127
128 static int slram_read(struct mtd_info *mtd, loff_t from, size_t len,
129                 size_t *retlen, u_char *buf)
130 {
131         slram_priv_t *priv = mtd->priv;
132
133         if (from > mtd->size)
134                 return -EINVAL;
135
136         if (from + len > mtd->size)
137                 len = mtd->size - from;
138
139         memcpy(buf, priv->start + from, len);
140
141         *retlen = len;
142         return(0);
143 }
144
145 static int slram_write(struct mtd_info *mtd, loff_t to, size_t len,
146                 size_t *retlen, const u_char *buf)
147 {
148         slram_priv_t *priv = mtd->priv;
149
150         if (to + len > mtd->size)
151                 return -EINVAL;
152
153         memcpy(priv->start + to, buf, len);
154
155         *retlen = len;
156         return(0);
157 }
158
159 /*====================================================================*/
160
161 static int register_device(char *name, unsigned long start, unsigned long length)
162 {
163         slram_mtd_list_t **curmtd;
164
165         curmtd = &slram_mtdlist;
166         while (*curmtd) {
167                 curmtd = &(*curmtd)->next;
168         }
169
170         *curmtd = kmalloc(sizeof(slram_mtd_list_t), GFP_KERNEL);
171         if (!(*curmtd)) {
172                 E("slram: Cannot allocate new MTD device.\n");
173                 return(-ENOMEM);
174         }
175         (*curmtd)->mtdinfo = kzalloc(sizeof(struct mtd_info), GFP_KERNEL);
176         (*curmtd)->next = NULL;
177
178         if ((*curmtd)->mtdinfo) {
179                 (*curmtd)->mtdinfo->priv =
180                         kzalloc(sizeof(slram_priv_t), GFP_KERNEL);
181
182                 if (!(*curmtd)->mtdinfo->priv) {
183                         kfree((*curmtd)->mtdinfo);
184                         (*curmtd)->mtdinfo = NULL;
185                 }
186         }
187
188         if (!(*curmtd)->mtdinfo) {
189                 E("slram: Cannot allocate new MTD device.\n");
190                 return(-ENOMEM);
191         }
192
193         if (!(((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start =
194                                 ioremap(start, length))) {
195                 E("slram: ioremap failed\n");
196                 return -EIO;
197         }
198         ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->end =
199                 ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start + length;
200
201
202         (*curmtd)->mtdinfo->name = name;
203         (*curmtd)->mtdinfo->size = length;
204         (*curmtd)->mtdinfo->flags = MTD_CAP_RAM;
205         (*curmtd)->mtdinfo->erase = slram_erase;
206         (*curmtd)->mtdinfo->point = slram_point;
207         (*curmtd)->mtdinfo->unpoint = slram_unpoint;
208         (*curmtd)->mtdinfo->read = slram_read;
209         (*curmtd)->mtdinfo->write = slram_write;
210         (*curmtd)->mtdinfo->owner = THIS_MODULE;
211         (*curmtd)->mtdinfo->type = MTD_RAM;
212         (*curmtd)->mtdinfo->erasesize = SLRAM_BLK_SZ;
213         (*curmtd)->mtdinfo->writesize = 1;
214
215         if (add_mtd_device((*curmtd)->mtdinfo)) {
216                 E("slram: Failed to register new device\n");
217                 iounmap(((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start);
218                 kfree((*curmtd)->mtdinfo->priv);
219                 kfree((*curmtd)->mtdinfo);
220                 return(-EAGAIN);
221         }
222         T("slram: Registered device %s from %luKiB to %luKiB\n", name,
223                         (start / 1024), ((start + length) / 1024));
224         T("slram: Mapped from 0x%p to 0x%p\n",
225                         ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start,
226                         ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->end);
227         return(0);
228 }
229
230 static void unregister_devices(void)
231 {
232         slram_mtd_list_t *nextitem;
233
234         while (slram_mtdlist) {
235                 nextitem = slram_mtdlist->next;
236                 del_mtd_device(slram_mtdlist->mtdinfo);
237                 iounmap(((slram_priv_t *)slram_mtdlist->mtdinfo->priv)->start);
238                 kfree(slram_mtdlist->mtdinfo->priv);
239                 kfree(slram_mtdlist->mtdinfo);
240                 kfree(slram_mtdlist);
241                 slram_mtdlist = nextitem;
242         }
243 }
244
245 static unsigned long handle_unit(unsigned long value, char *unit)
246 {
247         if ((*unit == 'M') || (*unit == 'm')) {
248                 return(value * 1024 * 1024);
249         } else if ((*unit == 'K') || (*unit == 'k')) {
250                 return(value * 1024);
251         }
252         return(value);
253 }
254
255 static int parse_cmdline(char *devname, char *szstart, char *szlength)
256 {
257         char *buffer;
258         unsigned long devstart;
259         unsigned long devlength;
260
261         if ((!devname) || (!szstart) || (!szlength)) {
262                 unregister_devices();
263                 return(-EINVAL);
264         }
265
266         devstart = simple_strtoul(szstart, &buffer, 0);
267         devstart = handle_unit(devstart, buffer);
268
269         if (*(szlength) != '+') {
270                 devlength = simple_strtoul(szlength, &buffer, 0);
271                 devlength = handle_unit(devlength, buffer) - devstart;
272         } else {
273                 devlength = simple_strtoul(szlength + 1, &buffer, 0);
274                 devlength = handle_unit(devlength, buffer);
275         }
276         T("slram: devname=%s, devstart=0x%lx, devlength=0x%lx\n",
277                         devname, devstart, devlength);
278         if ((devstart < 0) || (devlength < 0) || (devlength % SLRAM_BLK_SZ != 0)) {
279                 E("slram: Illegal start / length parameter.\n");
280                 return(-EINVAL);
281         }
282
283         if ((devstart = register_device(devname, devstart, devlength))){
284                 unregister_devices();
285                 return((int)devstart);
286         }
287         return(0);
288 }
289
290 #ifndef MODULE
291
292 static int __init mtd_slram_setup(char *str)
293 {
294         map = str;
295         return(1);
296 }
297
298 __setup("slram=", mtd_slram_setup);
299
300 #endif
301
302 static int init_slram(void)
303 {
304         char *devname;
305         int i;
306
307 #ifndef MODULE
308         char *devstart;
309         char *devlength;
310
311         i = 0;
312
313         if (!map) {
314                 E("slram: not enough parameters.\n");
315                 return(-EINVAL);
316         }
317         while (map) {
318                 devname = devstart = devlength = NULL;
319
320                 if (!(devname = strsep(&map, ","))) {
321                         E("slram: No devicename specified.\n");
322                         break;
323                 }
324                 T("slram: devname = %s\n", devname);
325                 if ((!map) || (!(devstart = strsep(&map, ",")))) {
326                         E("slram: No devicestart specified.\n");
327                 }
328                 T("slram: devstart = %s\n", devstart);
329                 if ((!map) || (!(devlength = strsep(&map, ",")))) {
330                         E("slram: No devicelength / -end specified.\n");
331                 }
332                 T("slram: devlength = %s\n", devlength);
333                 if (parse_cmdline(devname, devstart, devlength) != 0) {
334                         return(-EINVAL);
335                 }
336         }
337 #else
338         int count;
339
340         for (count = 0; (map[count]) && (count < SLRAM_MAX_DEVICES_PARAMS);
341                         count++) {
342         }
343
344         if ((count % 3 != 0) || (count == 0)) {
345                 E("slram: not enough parameters.\n");
346                 return(-EINVAL);
347         }
348         for (i = 0; i < (count / 3); i++) {
349                 devname = map[i * 3];
350
351                 if (parse_cmdline(devname, map[i * 3 + 1], map[i * 3 + 2])!=0) {
352                         return(-EINVAL);
353                 }
354
355         }
356 #endif /* !MODULE */
357
358         return(0);
359 }
360
361 static void __exit cleanup_slram(void)
362 {
363         unregister_devices();
364 }
365
366 module_init(init_slram);
367 module_exit(cleanup_slram);
368
369 MODULE_LICENSE("GPL");
370 MODULE_AUTHOR("Jochen Schaeuble <psionic@psionic.de>");
371 MODULE_DESCRIPTION("MTD driver for uncached system RAM");