blob: 542a0c009006d4b682d30c03fa437a4f0b62a2aa [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*======================================================================
2
Thomas Gleixnere5580fb2005-11-07 11:15:40 +00003 $Id: slram.c,v 1.36 2005/11/07 11:14:25 gleixner Exp $
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
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).
9
10 Usage:
11
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>
16
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
Thomas Gleixnere5580fb2005-11-07 11:15:40 +000021
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 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).
26
27 -
Thomas Gleixnere5580fb2005-11-07 11:15:40 +000028
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 Jochen Schaeuble <psionic@psionic.de>
30
31======================================================================*/
32
33
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>
49
50#include <linux/mtd/mtd.h>
51
52#define SLRAM_MAX_DEVICES_PARAMS 6 /* 3 parameters / device */
Josh Boyer14f83512005-01-06 21:16:45 +000053#define SLRAM_BLK_SZ 0x4000
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55#define T(fmt, args...) printk(KERN_DEBUG fmt, ## args)
56#define E(fmt, args...) printk(KERN_NOTICE fmt, ## args)
57
58typedef struct slram_priv {
59 u_char *start;
60 u_char *end;
61} slram_priv_t;
62
63typedef struct slram_mtd_list {
64 struct mtd_info *mtdinfo;
65 struct slram_mtd_list *next;
66} slram_mtd_list_t;
67
68#ifdef MODULE
69static char *map[SLRAM_MAX_DEVICES_PARAMS];
70
71module_param_array(map, charp, NULL, 0);
72MODULE_PARM_DESC(map, "List of memory regions to map. \"map=<name>, <start>, <length / end>\"");
73#else
74static char *map;
75#endif
76
77static slram_mtd_list_t *slram_mtdlist = NULL;
78
79static int slram_erase(struct mtd_info *, struct erase_info *);
80static int slram_point(struct mtd_info *, loff_t, size_t, size_t *, u_char **);
81static void slram_unpoint(struct mtd_info *, u_char *, loff_t, size_t);
82static int slram_read(struct mtd_info *, loff_t, size_t, size_t *, u_char *);
83static int slram_write(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
84
85static int slram_erase(struct mtd_info *mtd, struct erase_info *instr)
86{
87 slram_priv_t *priv = mtd->priv;
88
89 if (instr->addr + instr->len > mtd->size) {
90 return(-EINVAL);
91 }
Thomas Gleixnere5580fb2005-11-07 11:15:40 +000092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 memset(priv->start + instr->addr, 0xff, instr->len);
94
Thomas Gleixnere5580fb2005-11-07 11:15:40 +000095 /* This'll catch a few races. Free the thing before returning :)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 * I don't feel at all ashamed. This kind of thing is possible anyway
97 * with flash, but unlikely.
98 */
99
100 instr->state = MTD_ERASE_DONE;
101
102 mtd_erase_callback(instr);
103
104 return(0);
105}
106
107static int slram_point(struct mtd_info *mtd, loff_t from, size_t len,
108 size_t *retlen, u_char **mtdbuf)
109{
110 slram_priv_t *priv = mtd->priv;
111
Josh Boyer14f83512005-01-06 21:16:45 +0000112 if (from + len > mtd->size)
113 return -EINVAL;
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 *mtdbuf = priv->start + from;
116 *retlen = len;
117 return(0);
118}
119
120static void slram_unpoint(struct mtd_info *mtd, u_char *addr, loff_t from, size_t len)
121{
122}
123
124static int slram_read(struct mtd_info *mtd, loff_t from, size_t len,
125 size_t *retlen, u_char *buf)
126{
127 slram_priv_t *priv = mtd->priv;
Josh Boyer14f83512005-01-06 21:16:45 +0000128
129 if (from > mtd->size)
130 return -EINVAL;
131
132 if (from + len > mtd->size)
133 len = mtd->size - from;
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 memcpy(buf, priv->start + from, len);
136
137 *retlen = len;
138 return(0);
139}
140
141static int slram_write(struct mtd_info *mtd, loff_t to, size_t len,
142 size_t *retlen, const u_char *buf)
143{
144 slram_priv_t *priv = mtd->priv;
145
Josh Boyer14f83512005-01-06 21:16:45 +0000146 if (to + len > mtd->size)
147 return -EINVAL;
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 memcpy(priv->start + to, buf, len);
150
151 *retlen = len;
152 return(0);
153}
154
155/*====================================================================*/
156
157static int register_device(char *name, unsigned long start, unsigned long length)
158{
159 slram_mtd_list_t **curmtd;
160
161 curmtd = &slram_mtdlist;
162 while (*curmtd) {
163 curmtd = &(*curmtd)->next;
164 }
165
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);
170 }
171 (*curmtd)->mtdinfo = kmalloc(sizeof(struct mtd_info), GFP_KERNEL);
172 (*curmtd)->next = NULL;
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 if ((*curmtd)->mtdinfo) {
175 memset((char *)(*curmtd)->mtdinfo, 0, sizeof(struct mtd_info));
176 (*curmtd)->mtdinfo->priv =
177 kmalloc(sizeof(slram_priv_t), GFP_KERNEL);
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 if (!(*curmtd)->mtdinfo->priv) {
180 kfree((*curmtd)->mtdinfo);
181 (*curmtd)->mtdinfo = NULL;
182 } else {
183 memset((*curmtd)->mtdinfo->priv,0,sizeof(slram_priv_t));
184 }
185 }
186
187 if (!(*curmtd)->mtdinfo) {
188 E("slram: Cannot allocate new MTD device.\n");
189 return(-ENOMEM);
190 }
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (!(((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start =
193 ioremap(start, length))) {
194 E("slram: ioremap failed\n");
195 return -EIO;
196 }
197 ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->end =
198 ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start + length;
199
200
201 (*curmtd)->mtdinfo->name = name;
202 (*curmtd)->mtdinfo->size = length;
Jörn Engela6c591e2006-04-13 18:54:34 +0200203 (*curmtd)->mtdinfo->flags = MTD_CAP_RAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 (*curmtd)->mtdinfo->erase = slram_erase;
205 (*curmtd)->mtdinfo->point = slram_point;
206 (*curmtd)->mtdinfo->unpoint = slram_unpoint;
207 (*curmtd)->mtdinfo->read = slram_read;
208 (*curmtd)->mtdinfo->write = slram_write;
209 (*curmtd)->mtdinfo->owner = THIS_MODULE;
David Woodhouse21c8db92006-06-14 21:39:48 +0100210 (*curmtd)->mtdinfo->type = MTD_RAM;
Josh Boyer14f83512005-01-06 21:16:45 +0000211 (*curmtd)->mtdinfo->erasesize = SLRAM_BLK_SZ;
Artem B. Bityutskiy17ffc7b2006-06-22 18:15:48 +0400212 (*curmtd)->mtdinfo->writesize = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 if (add_mtd_device((*curmtd)->mtdinfo)) {
215 E("slram: Failed to register new device\n");
216 iounmap(((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start);
217 kfree((*curmtd)->mtdinfo->priv);
218 kfree((*curmtd)->mtdinfo);
219 return(-EAGAIN);
220 }
221 T("slram: Registered device %s from %luKiB to %luKiB\n", name,
222 (start / 1024), ((start + length) / 1024));
223 T("slram: Mapped from 0x%p to 0x%p\n",
224 ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start,
225 ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->end);
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000226 return(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228
229static void unregister_devices(void)
230{
231 slram_mtd_list_t *nextitem;
232
233 while (slram_mtdlist) {
234 nextitem = slram_mtdlist->next;
235 del_mtd_device(slram_mtdlist->mtdinfo);
236 iounmap(((slram_priv_t *)slram_mtdlist->mtdinfo->priv)->start);
237 kfree(slram_mtdlist->mtdinfo->priv);
238 kfree(slram_mtdlist->mtdinfo);
239 kfree(slram_mtdlist);
240 slram_mtdlist = nextitem;
241 }
242}
243
244static unsigned long handle_unit(unsigned long value, char *unit)
245{
246 if ((*unit == 'M') || (*unit == 'm')) {
247 return(value * 1024 * 1024);
248 } else if ((*unit == 'K') || (*unit == 'k')) {
249 return(value * 1024);
250 }
251 return(value);
252}
253
254static int parse_cmdline(char *devname, char *szstart, char *szlength)
255{
256 char *buffer;
257 unsigned long devstart;
258 unsigned long devlength;
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if ((!devname) || (!szstart) || (!szlength)) {
261 unregister_devices();
262 return(-EINVAL);
263 }
264
265 devstart = simple_strtoul(szstart, &buffer, 0);
266 devstart = handle_unit(devstart, buffer);
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (*(szlength) != '+') {
269 devlength = simple_strtoul(szlength, &buffer, 0);
270 devlength = handle_unit(devlength, buffer) - devstart;
271 } else {
272 devlength = simple_strtoul(szlength + 1, &buffer, 0);
273 devlength = handle_unit(devlength, buffer);
274 }
275 T("slram: devname=%s, devstart=0x%lx, devlength=0x%lx\n",
276 devname, devstart, devlength);
Josh Boyer14f83512005-01-06 21:16:45 +0000277 if ((devstart < 0) || (devlength < 0) || (devlength % SLRAM_BLK_SZ != 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 E("slram: Illegal start / length parameter.\n");
279 return(-EINVAL);
280 }
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if ((devstart = register_device(devname, devstart, devlength))){
283 unregister_devices();
284 return((int)devstart);
285 }
286 return(0);
287}
288
289#ifndef MODULE
290
291static int __init mtd_slram_setup(char *str)
292{
293 map = str;
294 return(1);
295}
296
297__setup("slram=", mtd_slram_setup);
298
299#endif
300
301static int init_slram(void)
302{
303 char *devname;
304 int i;
305
306#ifndef MODULE
307 char *devstart;
308 char *devlength;
309
310 i = 0;
311
312 if (!map) {
313 E("slram: not enough parameters.\n");
314 return(-EINVAL);
315 }
316 while (map) {
317 devname = devstart = devlength = NULL;
318
319 if (!(devname = strsep(&map, ","))) {
320 E("slram: No devicename specified.\n");
321 break;
322 }
323 T("slram: devname = %s\n", devname);
324 if ((!map) || (!(devstart = strsep(&map, ",")))) {
325 E("slram: No devicestart specified.\n");
326 }
327 T("slram: devstart = %s\n", devstart);
328 if ((!map) || (!(devlength = strsep(&map, ",")))) {
329 E("slram: No devicelength / -end specified.\n");
330 }
331 T("slram: devlength = %s\n", devlength);
332 if (parse_cmdline(devname, devstart, devlength) != 0) {
333 return(-EINVAL);
334 }
335 }
336#else
337 int count;
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 for (count = 0; (map[count]) && (count < SLRAM_MAX_DEVICES_PARAMS);
340 count++) {
341 }
342
343 if ((count % 3 != 0) || (count == 0)) {
344 E("slram: not enough parameters.\n");
345 return(-EINVAL);
346 }
347 for (i = 0; i < (count / 3); i++) {
348 devname = map[i * 3];
349
350 if (parse_cmdline(devname, map[i * 3 + 1], map[i * 3 + 2])!=0) {
351 return(-EINVAL);
352 }
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
355#endif /* !MODULE */
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return(0);
358}
359
360static void __exit cleanup_slram(void)
361{
362 unregister_devices();
363}
364
365module_init(init_slram);
366module_exit(cleanup_slram);
367
368MODULE_LICENSE("GPL");
369MODULE_AUTHOR("Jochen Schaeuble <psionic@psionic.de>");
370MODULE_DESCRIPTION("MTD driver for uncached system RAM");