Linux 2.6.20.7
[linux/fpc-iii.git] / drivers / mtd / devices / slram.c
blob5f49248a48564398a82239c569bf860b5d431da0
1 /*======================================================================
3 $Id: slram.c,v 1.36 2005/11/07 11:14:25 gleixner Exp $
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).
10 Usage:
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>
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
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).
29 Jochen Schaeuble <psionic@psionic.de>
31 ======================================================================*/
34 #include <linux/module.h>
35 #include <asm/uaccess.h>
36 #include <linux/types.h>
37 #include <linux/kernel.h>
38 #include <linux/sched.h>
39 #include <linux/ptrace.h>
40 #include <linux/slab.h>
41 #include <linux/string.h>
42 #include <linux/timer.h>
43 #include <linux/major.h>
44 #include <linux/fs.h>
45 #include <linux/ioctl.h>
46 #include <linux/init.h>
47 #include <asm/io.h>
48 #include <asm/system.h>
50 #include <linux/mtd/mtd.h>
52 #define SLRAM_MAX_DEVICES_PARAMS 6 /* 3 parameters / device */
53 #define SLRAM_BLK_SZ 0x4000
55 #define T(fmt, args...) printk(KERN_DEBUG fmt, ## args)
56 #define E(fmt, args...) printk(KERN_NOTICE fmt, ## args)
58 typedef struct slram_priv {
59 u_char *start;
60 u_char *end;
61 } slram_priv_t;
63 typedef struct slram_mtd_list {
64 struct mtd_info *mtdinfo;
65 struct slram_mtd_list *next;
66 } slram_mtd_list_t;
68 #ifdef MODULE
69 static char *map[SLRAM_MAX_DEVICES_PARAMS];
71 module_param_array(map, charp, NULL, 0);
72 MODULE_PARM_DESC(map, "List of memory regions to map. \"map=<name>, <start>, <length / end>\"");
73 #else
74 static char *map;
75 #endif
77 static slram_mtd_list_t *slram_mtdlist = NULL;
79 static int slram_erase(struct mtd_info *, struct erase_info *);
80 static int slram_point(struct mtd_info *, loff_t, size_t, size_t *, u_char **);
81 static void slram_unpoint(struct mtd_info *, u_char *, 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 *);
85 static int slram_erase(struct mtd_info *mtd, struct erase_info *instr)
87 slram_priv_t *priv = mtd->priv;
89 if (instr->addr + instr->len > mtd->size) {
90 return(-EINVAL);
93 memset(priv->start + instr->addr, 0xff, instr->len);
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.
100 instr->state = MTD_ERASE_DONE;
102 mtd_erase_callback(instr);
104 return(0);
107 static int slram_point(struct mtd_info *mtd, loff_t from, size_t len,
108 size_t *retlen, u_char **mtdbuf)
110 slram_priv_t *priv = mtd->priv;
112 if (from + len > mtd->size)
113 return -EINVAL;
115 *mtdbuf = priv->start + from;
116 *retlen = len;
117 return(0);
120 static void slram_unpoint(struct mtd_info *mtd, u_char *addr, loff_t from, size_t len)
124 static int slram_read(struct mtd_info *mtd, loff_t from, size_t len,
125 size_t *retlen, u_char *buf)
127 slram_priv_t *priv = mtd->priv;
129 if (from > mtd->size)
130 return -EINVAL;
132 if (from + len > mtd->size)
133 len = mtd->size - from;
135 memcpy(buf, priv->start + from, len);
137 *retlen = len;
138 return(0);
141 static int slram_write(struct mtd_info *mtd, loff_t to, size_t len,
142 size_t *retlen, const u_char *buf)
144 slram_priv_t *priv = mtd->priv;
146 if (to + len > mtd->size)
147 return -EINVAL;
149 memcpy(priv->start + to, buf, len);
151 *retlen = len;
152 return(0);
155 /*====================================================================*/
157 static int register_device(char *name, unsigned long start, unsigned long length)
159 slram_mtd_list_t **curmtd;
161 curmtd = &slram_mtdlist;
162 while (*curmtd) {
163 curmtd = &(*curmtd)->next;
166 *curmtd = kmalloc(sizeof(slram_mtd_list_t), GFP_KERNEL);
167 if (!(*curmtd)) {
168 E("slram: Cannot allocate new MTD device.\n");
169 return(-ENOMEM);
171 (*curmtd)->mtdinfo = kzalloc(sizeof(struct mtd_info), GFP_KERNEL);
172 (*curmtd)->next = NULL;
174 if ((*curmtd)->mtdinfo) {
175 (*curmtd)->mtdinfo->priv =
176 kzalloc(sizeof(slram_priv_t), GFP_KERNEL);
178 if (!(*curmtd)->mtdinfo->priv) {
179 kfree((*curmtd)->mtdinfo);
180 (*curmtd)->mtdinfo = NULL;
184 if (!(*curmtd)->mtdinfo) {
185 E("slram: Cannot allocate new MTD device.\n");
186 return(-ENOMEM);
189 if (!(((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start =
190 ioremap(start, length))) {
191 E("slram: ioremap failed\n");
192 return -EIO;
194 ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->end =
195 ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start + length;
198 (*curmtd)->mtdinfo->name = name;
199 (*curmtd)->mtdinfo->size = length;
200 (*curmtd)->mtdinfo->flags = MTD_CAP_RAM;
201 (*curmtd)->mtdinfo->erase = slram_erase;
202 (*curmtd)->mtdinfo->point = slram_point;
203 (*curmtd)->mtdinfo->unpoint = slram_unpoint;
204 (*curmtd)->mtdinfo->read = slram_read;
205 (*curmtd)->mtdinfo->write = slram_write;
206 (*curmtd)->mtdinfo->owner = THIS_MODULE;
207 (*curmtd)->mtdinfo->type = MTD_RAM;
208 (*curmtd)->mtdinfo->erasesize = SLRAM_BLK_SZ;
209 (*curmtd)->mtdinfo->writesize = 1;
211 if (add_mtd_device((*curmtd)->mtdinfo)) {
212 E("slram: Failed to register new device\n");
213 iounmap(((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start);
214 kfree((*curmtd)->mtdinfo->priv);
215 kfree((*curmtd)->mtdinfo);
216 return(-EAGAIN);
218 T("slram: Registered device %s from %luKiB to %luKiB\n", name,
219 (start / 1024), ((start + length) / 1024));
220 T("slram: Mapped from 0x%p to 0x%p\n",
221 ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start,
222 ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->end);
223 return(0);
226 static void unregister_devices(void)
228 slram_mtd_list_t *nextitem;
230 while (slram_mtdlist) {
231 nextitem = slram_mtdlist->next;
232 del_mtd_device(slram_mtdlist->mtdinfo);
233 iounmap(((slram_priv_t *)slram_mtdlist->mtdinfo->priv)->start);
234 kfree(slram_mtdlist->mtdinfo->priv);
235 kfree(slram_mtdlist->mtdinfo);
236 kfree(slram_mtdlist);
237 slram_mtdlist = nextitem;
241 static unsigned long handle_unit(unsigned long value, char *unit)
243 if ((*unit == 'M') || (*unit == 'm')) {
244 return(value * 1024 * 1024);
245 } else if ((*unit == 'K') || (*unit == 'k')) {
246 return(value * 1024);
248 return(value);
251 static int parse_cmdline(char *devname, char *szstart, char *szlength)
253 char *buffer;
254 unsigned long devstart;
255 unsigned long devlength;
257 if ((!devname) || (!szstart) || (!szlength)) {
258 unregister_devices();
259 return(-EINVAL);
262 devstart = simple_strtoul(szstart, &buffer, 0);
263 devstart = handle_unit(devstart, buffer);
265 if (*(szlength) != '+') {
266 devlength = simple_strtoul(szlength, &buffer, 0);
267 devlength = handle_unit(devlength, buffer) - devstart;
268 } else {
269 devlength = simple_strtoul(szlength + 1, &buffer, 0);
270 devlength = handle_unit(devlength, buffer);
272 T("slram: devname=%s, devstart=0x%lx, devlength=0x%lx\n",
273 devname, devstart, devlength);
274 if ((devstart < 0) || (devlength < 0) || (devlength % SLRAM_BLK_SZ != 0)) {
275 E("slram: Illegal start / length parameter.\n");
276 return(-EINVAL);
279 if ((devstart = register_device(devname, devstart, devlength))){
280 unregister_devices();
281 return((int)devstart);
283 return(0);
286 #ifndef MODULE
288 static int __init mtd_slram_setup(char *str)
290 map = str;
291 return(1);
294 __setup("slram=", mtd_slram_setup);
296 #endif
298 static int init_slram(void)
300 char *devname;
301 int i;
303 #ifndef MODULE
304 char *devstart;
305 char *devlength;
307 i = 0;
309 if (!map) {
310 E("slram: not enough parameters.\n");
311 return(-EINVAL);
313 while (map) {
314 devname = devstart = devlength = NULL;
316 if (!(devname = strsep(&map, ","))) {
317 E("slram: No devicename specified.\n");
318 break;
320 T("slram: devname = %s\n", devname);
321 if ((!map) || (!(devstart = strsep(&map, ",")))) {
322 E("slram: No devicestart specified.\n");
324 T("slram: devstart = %s\n", devstart);
325 if ((!map) || (!(devlength = strsep(&map, ",")))) {
326 E("slram: No devicelength / -end specified.\n");
328 T("slram: devlength = %s\n", devlength);
329 if (parse_cmdline(devname, devstart, devlength) != 0) {
330 return(-EINVAL);
333 #else
334 int count;
336 for (count = 0; (map[count]) && (count < SLRAM_MAX_DEVICES_PARAMS);
337 count++) {
340 if ((count % 3 != 0) || (count == 0)) {
341 E("slram: not enough parameters.\n");
342 return(-EINVAL);
344 for (i = 0; i < (count / 3); i++) {
345 devname = map[i * 3];
347 if (parse_cmdline(devname, map[i * 3 + 1], map[i * 3 + 2])!=0) {
348 return(-EINVAL);
352 #endif /* !MODULE */
354 return(0);
357 static void __exit cleanup_slram(void)
359 unregister_devices();
362 module_init(init_slram);
363 module_exit(cleanup_slram);
365 MODULE_LICENSE("GPL");
366 MODULE_AUTHOR("Jochen Schaeuble <psionic@psionic.de>");
367 MODULE_DESCRIPTION("MTD driver for uncached system RAM");