m68k: use KBUILD_DEFCONFIG
[linux-2.6/verdex.git] / drivers / mtd / devices / slram.c
blobd293add1857cfba358a033ff0247e7c755ef42c1
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 *, 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 *);
84 static int slram_erase(struct mtd_info *mtd, struct erase_info *instr)
86 slram_priv_t *priv = mtd->priv;
88 if (instr->addr + instr->len > mtd->size) {
89 return(-EINVAL);
92 memset(priv->start + instr->addr, 0xff, instr->len);
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.
99 instr->state = MTD_ERASE_DONE;
101 mtd_erase_callback(instr);
103 return(0);
106 static int slram_point(struct mtd_info *mtd, loff_t from, size_t len,
107 size_t *retlen, u_char **mtdbuf)
109 slram_priv_t *priv = mtd->priv;
111 if (from + len > mtd->size)
112 return -EINVAL;
114 *mtdbuf = priv->start + from;
115 *retlen = len;
116 return(0);
119 static void slram_unpoint(struct mtd_info *mtd, u_char *addr, loff_t from, size_t len)
123 static int slram_read(struct mtd_info *mtd, loff_t from, size_t len,
124 size_t *retlen, u_char *buf)
126 slram_priv_t *priv = mtd->priv;
128 if (from > mtd->size)
129 return -EINVAL;
131 if (from + len > mtd->size)
132 len = mtd->size - from;
134 memcpy(buf, priv->start + from, len);
136 *retlen = len;
137 return(0);
140 static int slram_write(struct mtd_info *mtd, loff_t to, size_t len,
141 size_t *retlen, const u_char *buf)
143 slram_priv_t *priv = mtd->priv;
145 if (to + len > mtd->size)
146 return -EINVAL;
148 memcpy(priv->start + to, buf, len);
150 *retlen = len;
151 return(0);
154 /*====================================================================*/
156 static int register_device(char *name, unsigned long start, unsigned long length)
158 slram_mtd_list_t **curmtd;
160 curmtd = &slram_mtdlist;
161 while (*curmtd) {
162 curmtd = &(*curmtd)->next;
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);
170 (*curmtd)->mtdinfo = kzalloc(sizeof(struct mtd_info), GFP_KERNEL);
171 (*curmtd)->next = NULL;
173 if ((*curmtd)->mtdinfo) {
174 (*curmtd)->mtdinfo->priv =
175 kzalloc(sizeof(slram_priv_t), GFP_KERNEL);
177 if (!(*curmtd)->mtdinfo->priv) {
178 kfree((*curmtd)->mtdinfo);
179 (*curmtd)->mtdinfo = NULL;
183 if (!(*curmtd)->mtdinfo) {
184 E("slram: Cannot allocate new MTD device.\n");
185 return(-ENOMEM);
188 if (!(((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start =
189 ioremap(start, length))) {
190 E("slram: ioremap failed\n");
191 return -EIO;
193 ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->end =
194 ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start + length;
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;
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);
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);
225 static void unregister_devices(void)
227 slram_mtd_list_t *nextitem;
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;
240 static unsigned long handle_unit(unsigned long value, char *unit)
242 if ((*unit == 'M') || (*unit == 'm')) {
243 return(value * 1024 * 1024);
244 } else if ((*unit == 'K') || (*unit == 'k')) {
245 return(value * 1024);
247 return(value);
250 static int parse_cmdline(char *devname, char *szstart, char *szlength)
252 char *buffer;
253 unsigned long devstart;
254 unsigned long devlength;
256 if ((!devname) || (!szstart) || (!szlength)) {
257 unregister_devices();
258 return(-EINVAL);
261 devstart = simple_strtoul(szstart, &buffer, 0);
262 devstart = handle_unit(devstart, buffer);
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);
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);
278 if ((devstart = register_device(devname, devstart, devlength))){
279 unregister_devices();
280 return((int)devstart);
282 return(0);
285 #ifndef MODULE
287 static int __init mtd_slram_setup(char *str)
289 map = str;
290 return(1);
293 __setup("slram=", mtd_slram_setup);
295 #endif
297 static int init_slram(void)
299 char *devname;
300 int i;
302 #ifndef MODULE
303 char *devstart;
304 char *devlength;
306 i = 0;
308 if (!map) {
309 E("slram: not enough parameters.\n");
310 return(-EINVAL);
312 while (map) {
313 devname = devstart = devlength = NULL;
315 if (!(devname = strsep(&map, ","))) {
316 E("slram: No devicename specified.\n");
317 break;
319 T("slram: devname = %s\n", devname);
320 if ((!map) || (!(devstart = strsep(&map, ",")))) {
321 E("slram: No devicestart specified.\n");
323 T("slram: devstart = %s\n", devstart);
324 if ((!map) || (!(devlength = strsep(&map, ",")))) {
325 E("slram: No devicelength / -end specified.\n");
327 T("slram: devlength = %s\n", devlength);
328 if (parse_cmdline(devname, devstart, devlength) != 0) {
329 return(-EINVAL);
332 #else
333 int count;
335 for (count = 0; (map[count]) && (count < SLRAM_MAX_DEVICES_PARAMS);
336 count++) {
339 if ((count % 3 != 0) || (count == 0)) {
340 E("slram: not enough parameters.\n");
341 return(-EINVAL);
343 for (i = 0; i < (count / 3); i++) {
344 devname = map[i * 3];
346 if (parse_cmdline(devname, map[i * 3 + 1], map[i * 3 + 2])!=0) {
347 return(-EINVAL);
351 #endif /* !MODULE */
353 return(0);
356 static void __exit cleanup_slram(void)
358 unregister_devices();
361 module_init(init_slram);
362 module_exit(cleanup_slram);
364 MODULE_LICENSE("GPL");
365 MODULE_AUTHOR("Jochen Schaeuble <psionic@psionic.de>");
366 MODULE_DESCRIPTION("MTD driver for uncached system RAM");