1 /*======================================================================
3 $Id: slram.c,v 1.33 2005/01/05 18:05:13 dwmw2 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).
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
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>
45 #include <linux/ioctl.h>
46 #include <linux/init.h>
48 #include <asm/system.h>
50 #include <linux/mtd/mtd.h>
52 #define SLRAM_MAX_DEVICES_PARAMS 6 /* 3 parameters / device */
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
{
62 typedef struct slram_mtd_list
{
63 struct mtd_info
*mtdinfo
;
64 struct slram_mtd_list
*next
;
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>\"");
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
) {
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
);
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 *mtdbuf
= priv
->start
+ from
;
116 static void slram_unpoint(struct mtd_info
*mtd
, u_char
*addr
, loff_t from
, size_t len
)
120 static int slram_read(struct mtd_info
*mtd
, loff_t from
, size_t len
,
121 size_t *retlen
, u_char
*buf
)
123 slram_priv_t
*priv
= mtd
->priv
;
125 memcpy(buf
, priv
->start
+ from
, len
);
131 static int slram_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
132 size_t *retlen
, const u_char
*buf
)
134 slram_priv_t
*priv
= mtd
->priv
;
136 memcpy(priv
->start
+ to
, buf
, len
);
142 /*====================================================================*/
144 static int register_device(char *name
, unsigned long start
, unsigned long length
)
146 slram_mtd_list_t
**curmtd
;
148 curmtd
= &slram_mtdlist
;
150 curmtd
= &(*curmtd
)->next
;
153 *curmtd
= kmalloc(sizeof(slram_mtd_list_t
), GFP_KERNEL
);
155 E("slram: Cannot allocate new MTD device.\n");
158 (*curmtd
)->mtdinfo
= kmalloc(sizeof(struct mtd_info
), GFP_KERNEL
);
159 (*curmtd
)->next
= NULL
;
161 if ((*curmtd
)->mtdinfo
) {
162 memset((char *)(*curmtd
)->mtdinfo
, 0, sizeof(struct mtd_info
));
163 (*curmtd
)->mtdinfo
->priv
=
164 kmalloc(sizeof(slram_priv_t
), GFP_KERNEL
);
166 if (!(*curmtd
)->mtdinfo
->priv
) {
167 kfree((*curmtd
)->mtdinfo
);
168 (*curmtd
)->mtdinfo
= NULL
;
170 memset((*curmtd
)->mtdinfo
->priv
,0,sizeof(slram_priv_t
));
174 if (!(*curmtd
)->mtdinfo
) {
175 E("slram: Cannot allocate new MTD device.\n");
179 if (!(((slram_priv_t
*)(*curmtd
)->mtdinfo
->priv
)->start
=
180 ioremap(start
, length
))) {
181 E("slram: ioremap failed\n");
184 ((slram_priv_t
*)(*curmtd
)->mtdinfo
->priv
)->end
=
185 ((slram_priv_t
*)(*curmtd
)->mtdinfo
->priv
)->start
+ length
;
188 (*curmtd
)->mtdinfo
->name
= name
;
189 (*curmtd
)->mtdinfo
->size
= length
;
190 (*curmtd
)->mtdinfo
->flags
= MTD_CLEAR_BITS
| MTD_SET_BITS
|
191 MTD_WRITEB_WRITEABLE
| MTD_VOLATILE
;
192 (*curmtd
)->mtdinfo
->erase
= slram_erase
;
193 (*curmtd
)->mtdinfo
->point
= slram_point
;
194 (*curmtd
)->mtdinfo
->unpoint
= slram_unpoint
;
195 (*curmtd
)->mtdinfo
->read
= slram_read
;
196 (*curmtd
)->mtdinfo
->write
= slram_write
;
197 (*curmtd
)->mtdinfo
->owner
= THIS_MODULE
;
198 (*curmtd
)->mtdinfo
->type
= MTD_RAM
;
199 (*curmtd
)->mtdinfo
->erasesize
= 0x0;
201 if (add_mtd_device((*curmtd
)->mtdinfo
)) {
202 E("slram: Failed to register new device\n");
203 iounmap(((slram_priv_t
*)(*curmtd
)->mtdinfo
->priv
)->start
);
204 kfree((*curmtd
)->mtdinfo
->priv
);
205 kfree((*curmtd
)->mtdinfo
);
208 T("slram: Registered device %s from %luKiB to %luKiB\n", name
,
209 (start
/ 1024), ((start
+ length
) / 1024));
210 T("slram: Mapped from 0x%p to 0x%p\n",
211 ((slram_priv_t
*)(*curmtd
)->mtdinfo
->priv
)->start
,
212 ((slram_priv_t
*)(*curmtd
)->mtdinfo
->priv
)->end
);
216 static void unregister_devices(void)
218 slram_mtd_list_t
*nextitem
;
220 while (slram_mtdlist
) {
221 nextitem
= slram_mtdlist
->next
;
222 del_mtd_device(slram_mtdlist
->mtdinfo
);
223 iounmap(((slram_priv_t
*)slram_mtdlist
->mtdinfo
->priv
)->start
);
224 kfree(slram_mtdlist
->mtdinfo
->priv
);
225 kfree(slram_mtdlist
->mtdinfo
);
226 kfree(slram_mtdlist
);
227 slram_mtdlist
= nextitem
;
231 static unsigned long handle_unit(unsigned long value
, char *unit
)
233 if ((*unit
== 'M') || (*unit
== 'm')) {
234 return(value
* 1024 * 1024);
235 } else if ((*unit
== 'K') || (*unit
== 'k')) {
236 return(value
* 1024);
241 static int parse_cmdline(char *devname
, char *szstart
, char *szlength
)
244 unsigned long devstart
;
245 unsigned long devlength
;
247 if ((!devname
) || (!szstart
) || (!szlength
)) {
248 unregister_devices();
252 devstart
= simple_strtoul(szstart
, &buffer
, 0);
253 devstart
= handle_unit(devstart
, buffer
);
255 if (*(szlength
) != '+') {
256 devlength
= simple_strtoul(szlength
, &buffer
, 0);
257 devlength
= handle_unit(devlength
, buffer
) - devstart
;
259 devlength
= simple_strtoul(szlength
+ 1, &buffer
, 0);
260 devlength
= handle_unit(devlength
, buffer
);
262 T("slram: devname=%s, devstart=0x%lx, devlength=0x%lx\n",
263 devname
, devstart
, devlength
);
264 if ((devstart
< 0) || (devlength
< 0)) {
265 E("slram: Illegal start / length parameter.\n");
269 if ((devstart
= register_device(devname
, devstart
, devlength
))){
270 unregister_devices();
271 return((int)devstart
);
278 static int __init
mtd_slram_setup(char *str
)
284 __setup("slram=", mtd_slram_setup
);
288 static int init_slram(void)
300 E("slram: not enough parameters.\n");
304 devname
= devstart
= devlength
= NULL
;
306 if (!(devname
= strsep(&map
, ","))) {
307 E("slram: No devicename specified.\n");
310 T("slram: devname = %s\n", devname
);
311 if ((!map
) || (!(devstart
= strsep(&map
, ",")))) {
312 E("slram: No devicestart specified.\n");
314 T("slram: devstart = %s\n", devstart
);
315 if ((!map
) || (!(devlength
= strsep(&map
, ",")))) {
316 E("slram: No devicelength / -end specified.\n");
318 T("slram: devlength = %s\n", devlength
);
319 if (parse_cmdline(devname
, devstart
, devlength
) != 0) {
326 for (count
= 0; (map
[count
]) && (count
< SLRAM_MAX_DEVICES_PARAMS
);
330 if ((count
% 3 != 0) || (count
== 0)) {
331 E("slram: not enough parameters.\n");
334 for (i
= 0; i
< (count
/ 3); i
++) {
335 devname
= map
[i
* 3];
337 if (parse_cmdline(devname
, map
[i
* 3 + 1], map
[i
* 3 + 2])!=0) {
347 static void __exit
cleanup_slram(void)
349 unregister_devices();
352 module_init(init_slram
);
353 module_exit(cleanup_slram
);
355 MODULE_LICENSE("GPL");
356 MODULE_AUTHOR("Jochen Schaeuble <psionic@psionic.de>");
357 MODULE_DESCRIPTION("MTD driver for uncached system RAM");