Linux 2.6.26-rc5
[linux-2.6/openmoko-kernel/knife-kernel.git] / drivers / mtd / devices / slram.c
blobcb86db746f28c91f552462696b10d5c99f23c07d
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/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>
49 #include <linux/mtd/mtd.h>
51 #define SLRAM_MAX_DEVICES_PARAMS 6 /* 3 parameters / device */
52 #define SLRAM_BLK_SZ 0x4000
54 #define T(fmt, args...) printk(KERN_DEBUG fmt, ## args)
55 #define E(fmt, args...) printk(KERN_NOTICE fmt, ## args)
57 typedef struct slram_priv {
58 u_char *start;
59 u_char *end;
60 } slram_priv_t;
62 typedef struct slram_mtd_list {
63 struct mtd_info *mtdinfo;
64 struct slram_mtd_list *next;
65 } slram_mtd_list_t;
67 #ifdef MODULE
68 static char *map[SLRAM_MAX_DEVICES_PARAMS];
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
76 static slram_mtd_list_t *slram_mtdlist = NULL;
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 *);
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, void **virt, resource_size_t *phys)
110 slram_priv_t *priv = mtd->priv;
112 /* can we return a physical address with this driver? */
113 if (phys)
114 return -EINVAL;
116 if (from + len > mtd->size)
117 return -EINVAL;
119 *virt = priv->start + from;
120 *retlen = len;
121 return(0);
124 static void slram_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
128 static int slram_read(struct mtd_info *mtd, loff_t from, size_t len,
129 size_t *retlen, u_char *buf)
131 slram_priv_t *priv = mtd->priv;
133 if (from > mtd->size)
134 return -EINVAL;
136 if (from + len > mtd->size)
137 len = mtd->size - from;
139 memcpy(buf, priv->start + from, len);
141 *retlen = len;
142 return(0);
145 static int slram_write(struct mtd_info *mtd, loff_t to, size_t len,
146 size_t *retlen, const u_char *buf)
148 slram_priv_t *priv = mtd->priv;
150 if (to + len > mtd->size)
151 return -EINVAL;
153 memcpy(priv->start + to, buf, len);
155 *retlen = len;
156 return(0);
159 /*====================================================================*/
161 static int register_device(char *name, unsigned long start, unsigned long length)
163 slram_mtd_list_t **curmtd;
165 curmtd = &slram_mtdlist;
166 while (*curmtd) {
167 curmtd = &(*curmtd)->next;
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);
175 (*curmtd)->mtdinfo = kzalloc(sizeof(struct mtd_info), GFP_KERNEL);
176 (*curmtd)->next = NULL;
178 if ((*curmtd)->mtdinfo) {
179 (*curmtd)->mtdinfo->priv =
180 kzalloc(sizeof(slram_priv_t), GFP_KERNEL);
182 if (!(*curmtd)->mtdinfo->priv) {
183 kfree((*curmtd)->mtdinfo);
184 (*curmtd)->mtdinfo = NULL;
188 if (!(*curmtd)->mtdinfo) {
189 E("slram: Cannot allocate new MTD device.\n");
190 return(-ENOMEM);
193 if (!(((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start =
194 ioremap(start, length))) {
195 E("slram: ioremap failed\n");
196 return -EIO;
198 ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->end =
199 ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start + length;
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;
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);
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);
230 static void unregister_devices(void)
232 slram_mtd_list_t *nextitem;
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;
245 static unsigned long handle_unit(unsigned long value, char *unit)
247 if ((*unit == 'M') || (*unit == 'm')) {
248 return(value * 1024 * 1024);
249 } else if ((*unit == 'K') || (*unit == 'k')) {
250 return(value * 1024);
252 return(value);
255 static int parse_cmdline(char *devname, char *szstart, char *szlength)
257 char *buffer;
258 unsigned long devstart;
259 unsigned long devlength;
261 if ((!devname) || (!szstart) || (!szlength)) {
262 unregister_devices();
263 return(-EINVAL);
266 devstart = simple_strtoul(szstart, &buffer, 0);
267 devstart = handle_unit(devstart, buffer);
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);
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);
283 if ((devstart = register_device(devname, devstart, devlength))){
284 unregister_devices();
285 return((int)devstart);
287 return(0);
290 #ifndef MODULE
292 static int __init mtd_slram_setup(char *str)
294 map = str;
295 return(1);
298 __setup("slram=", mtd_slram_setup);
300 #endif
302 static int init_slram(void)
304 char *devname;
305 int i;
307 #ifndef MODULE
308 char *devstart;
309 char *devlength;
311 i = 0;
313 if (!map) {
314 E("slram: not enough parameters.\n");
315 return(-EINVAL);
317 while (map) {
318 devname = devstart = devlength = NULL;
320 if (!(devname = strsep(&map, ","))) {
321 E("slram: No devicename specified.\n");
322 break;
324 T("slram: devname = %s\n", devname);
325 if ((!map) || (!(devstart = strsep(&map, ",")))) {
326 E("slram: No devicestart specified.\n");
328 T("slram: devstart = %s\n", devstart);
329 if ((!map) || (!(devlength = strsep(&map, ",")))) {
330 E("slram: No devicelength / -end specified.\n");
332 T("slram: devlength = %s\n", devlength);
333 if (parse_cmdline(devname, devstart, devlength) != 0) {
334 return(-EINVAL);
337 #else
338 int count;
340 for (count = 0; (map[count]) && (count < SLRAM_MAX_DEVICES_PARAMS);
341 count++) {
344 if ((count % 3 != 0) || (count == 0)) {
345 E("slram: not enough parameters.\n");
346 return(-EINVAL);
348 for (i = 0; i < (count / 3); i++) {
349 devname = map[i * 3];
351 if (parse_cmdline(devname, map[i * 3 + 1], map[i * 3 + 2])!=0) {
352 return(-EINVAL);
356 #endif /* !MODULE */
358 return(0);
361 static void __exit cleanup_slram(void)
363 unregister_devices();
366 module_init(init_slram);
367 module_exit(cleanup_slram);
369 MODULE_LICENSE("GPL");
370 MODULE_AUTHOR("Jochen Schaeuble <psionic@psionic.de>");
371 MODULE_DESCRIPTION("MTD driver for uncached system RAM");