1 /*======================================================================
3 This driver provides a method to access memory not used by the kernel
4 itself (i.e. if the kernel commandline mem=xxx is used). To actually
5 use slram at least mtdblock or mtdchar is required (for block or
6 character device access).
10 if compiled as loadable module:
11 modprobe slram map=<name>,<start>,<end/offset>
12 if statically linked into the kernel use the following kernel cmd.line
13 slram=<name>,<start>,<end/offset>
15 <name>: name of the device that will be listed in /proc/mtd
16 <start>: start of the memory region, decimal or hex (0xabcdef)
17 <end/offset>: end of the memory region. It's possible to use +0x1234
18 to specify the offset instead of the absolute address
21 With slram it's only possible to map a contiguous memory region. Therefore
22 if there's a device mapped somewhere in the region specified slram will
23 fail to load (see kernel log if modprobe fails).
27 Jochen Schaeuble <psionic@psionic.de>
29 ======================================================================*/
32 #include <linux/module.h>
33 #include <asm/uaccess.h>
34 #include <linux/types.h>
35 #include <linux/kernel.h>
36 #include <linux/ptrace.h>
37 #include <linux/slab.h>
38 #include <linux/string.h>
39 #include <linux/timer.h>
40 #include <linux/major.h>
42 #include <linux/ioctl.h>
43 #include <linux/init.h>
45 #include <asm/system.h>
47 #include <linux/mtd/mtd.h>
49 #define SLRAM_MAX_DEVICES_PARAMS 6 /* 3 parameters / device */
50 #define SLRAM_BLK_SZ 0x4000
52 #define T(fmt, args...) printk(KERN_DEBUG fmt, ## args)
53 #define E(fmt, args...) printk(KERN_NOTICE fmt, ## args)
55 typedef struct slram_priv {
60 typedef struct slram_mtd_list {
61 struct mtd_info *mtdinfo;
62 struct slram_mtd_list *next;
66 static char *map[SLRAM_MAX_DEVICES_PARAMS];
68 module_param_array(map, charp, NULL, 0);
69 MODULE_PARM_DESC(map, "List of memory regions to map. \"map=<name>, <start>, <length / end>\"");
74 static slram_mtd_list_t *slram_mtdlist = NULL;
76 static int slram_erase(struct mtd_info *, struct erase_info *);
77 static int slram_point(struct mtd_info *, loff_t, size_t, size_t *, void **,
79 static int slram_unpoint(struct mtd_info *, loff_t, size_t);
80 static int slram_read(struct mtd_info *, loff_t, size_t, size_t *, u_char *);
81 static int slram_write(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
83 static int slram_erase(struct mtd_info *mtd, struct erase_info *instr)
85 slram_priv_t *priv = mtd->priv;
87 memset(priv->start + instr->addr, 0xff, instr->len);
88 /* This'll catch a few races. Free the thing before returning :)
89 * I don't feel at all ashamed. This kind of thing is possible anyway
90 * with flash, but unlikely.
92 instr->state = MTD_ERASE_DONE;
93 mtd_erase_callback(instr);
97 static int slram_point(struct mtd_info *mtd, loff_t from, size_t len,
98 size_t *retlen, void **virt, resource_size_t *phys)
100 slram_priv_t *priv = mtd->priv;
102 /* can we return a physical address with this driver? */
105 *virt = priv->start + from;
110 static int slram_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
115 static int slram_read(struct mtd_info *mtd, loff_t from, size_t len,
116 size_t *retlen, u_char *buf)
118 slram_priv_t *priv = mtd->priv;
120 memcpy(buf, priv->start + from, len);
125 static int slram_write(struct mtd_info *mtd, loff_t to, size_t len,
126 size_t *retlen, const u_char *buf)
128 slram_priv_t *priv = mtd->priv;
130 memcpy(priv->start + to, buf, len);
135 /*====================================================================*/
137 static int register_device(char *name, unsigned long start, unsigned long length)
139 slram_mtd_list_t **curmtd;
141 curmtd = &slram_mtdlist;
143 curmtd = &(*curmtd)->next;
146 *curmtd = kmalloc(sizeof(slram_mtd_list_t), GFP_KERNEL);
148 E("slram: Cannot allocate new MTD device.\n");
151 (*curmtd)->mtdinfo = kzalloc(sizeof(struct mtd_info), GFP_KERNEL);
152 (*curmtd)->next = NULL;
154 if ((*curmtd)->mtdinfo) {
155 (*curmtd)->mtdinfo->priv =
156 kzalloc(sizeof(slram_priv_t), GFP_KERNEL);
158 if (!(*curmtd)->mtdinfo->priv) {
159 kfree((*curmtd)->mtdinfo);
160 (*curmtd)->mtdinfo = NULL;
164 if (!(*curmtd)->mtdinfo) {
165 E("slram: Cannot allocate new MTD device.\n");
169 if (!(((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start =
170 ioremap(start, length))) {
171 E("slram: ioremap failed\n");
174 ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->end =
175 ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start + length;
178 (*curmtd)->mtdinfo->name = name;
179 (*curmtd)->mtdinfo->size = length;
180 (*curmtd)->mtdinfo->flags = MTD_CAP_RAM;
181 (*curmtd)->mtdinfo->_erase = slram_erase;
182 (*curmtd)->mtdinfo->_point = slram_point;
183 (*curmtd)->mtdinfo->_unpoint = slram_unpoint;
184 (*curmtd)->mtdinfo->_read = slram_read;
185 (*curmtd)->mtdinfo->_write = slram_write;
186 (*curmtd)->mtdinfo->owner = THIS_MODULE;
187 (*curmtd)->mtdinfo->type = MTD_RAM;
188 (*curmtd)->mtdinfo->erasesize = SLRAM_BLK_SZ;
189 (*curmtd)->mtdinfo->writesize = 1;
191 if (mtd_device_register((*curmtd)->mtdinfo, NULL, 0)) {
192 E("slram: Failed to register new device\n");
193 iounmap(((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start);
194 kfree((*curmtd)->mtdinfo->priv);
195 kfree((*curmtd)->mtdinfo);
198 T("slram: Registered device %s from %luKiB to %luKiB\n", name,
199 (start / 1024), ((start + length) / 1024));
200 T("slram: Mapped from 0x%p to 0x%p\n",
201 ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start,
202 ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->end);
206 static void unregister_devices(void)
208 slram_mtd_list_t *nextitem;
210 while (slram_mtdlist) {
211 nextitem = slram_mtdlist->next;
212 mtd_device_unregister(slram_mtdlist->mtdinfo);
213 iounmap(((slram_priv_t *)slram_mtdlist->mtdinfo->priv)->start);
214 kfree(slram_mtdlist->mtdinfo->priv);
215 kfree(slram_mtdlist->mtdinfo);
216 kfree(slram_mtdlist);
217 slram_mtdlist = nextitem;
221 static unsigned long handle_unit(unsigned long value, char *unit)
223 if ((*unit == 'M') || (*unit == 'm')) {
224 return(value * 1024 * 1024);
225 } else if ((*unit == 'K') || (*unit == 'k')) {
226 return(value * 1024);
231 static int parse_cmdline(char *devname, char *szstart, char *szlength)
234 unsigned long devstart;
235 unsigned long devlength;
237 if ((!devname) || (!szstart) || (!szlength)) {
238 unregister_devices();
242 devstart = simple_strtoul(szstart, &buffer, 0);
243 devstart = handle_unit(devstart, buffer);
245 if (*(szlength) != '+') {
246 devlength = simple_strtoul(szlength, &buffer, 0);
247 devlength = handle_unit(devlength, buffer) - devstart;
248 if (devlength < devstart)
251 devlength -= devstart;
253 devlength = simple_strtoul(szlength + 1, &buffer, 0);
254 devlength = handle_unit(devlength, buffer);
256 T("slram: devname=%s, devstart=0x%lx, devlength=0x%lx\n",
257 devname, devstart, devlength);
258 if (devlength % SLRAM_BLK_SZ != 0)
261 if ((devstart = register_device(devname, devstart, devlength))){
262 unregister_devices();
263 return((int)devstart);
268 E("slram: Illegal length parameter.\n");
274 static int __init mtd_slram_setup(char *str)
280 __setup("slram=", mtd_slram_setup);
284 static int __init init_slram(void)
296 E("slram: not enough parameters.\n");
300 devname = devstart = devlength = NULL;
302 if (!(devname = strsep(&map, ","))) {
303 E("slram: No devicename specified.\n");
306 T("slram: devname = %s\n", devname);
307 if ((!map) || (!(devstart = strsep(&map, ",")))) {
308 E("slram: No devicestart specified.\n");
310 T("slram: devstart = %s\n", devstart);
311 if ((!map) || (!(devlength = strsep(&map, ",")))) {
312 E("slram: No devicelength / -end specified.\n");
314 T("slram: devlength = %s\n", devlength);
315 if (parse_cmdline(devname, devstart, devlength) != 0) {
322 for (count = 0; count < SLRAM_MAX_DEVICES_PARAMS && map[count];
326 if ((count % 3 != 0) || (count == 0)) {
327 E("slram: not enough parameters.\n");
330 for (i = 0; i < (count / 3); i++) {
331 devname = map[i * 3];
333 if (parse_cmdline(devname, map[i * 3 + 1], map[i * 3 + 2])!=0) {
343 static void __exit cleanup_slram(void)
345 unregister_devices();
348 module_init(init_slram);
349 module_exit(cleanup_slram);
351 MODULE_LICENSE("GPL");
352 MODULE_AUTHOR("Jochen Schaeuble <psionic@psionic.de>");
353 MODULE_DESCRIPTION("MTD driver for uncached system RAM");