blob: d293add1857cfba358a033ff0247e7c755ef42c1 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#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>
48
49#include <linux/mtd/mtd.h>
50
51#define SLRAM_MAX_DEVICES_PARAMS 6 /* 3 parameters / device */
Josh Boyer14f83512005-01-06 21:16:45 +000052#define SLRAM_BLK_SZ 0x4000
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#define T(fmt, args...) printk(KERN_DEBUG fmt, ## args)
55#define E(fmt, args...) printk(KERN_NOTICE fmt, ## args)
56
57typedef struct slram_priv {
58 u_char *start;
59 u_char *end;
60} slram_priv_t;
61
62typedef struct slram_mtd_list {
63 struct mtd_info *mtdinfo;
64 struct slram_mtd_list *next;
65} slram_mtd_list_t;
66
67#ifdef MODULE
68static char *map[SLRAM_MAX_DEVICES_PARAMS];
69
70module_param_array(map, charp, NULL, 0);
71MODULE_PARM_DESC(map, "List of memory regions to map. \"map=<name>, <start>, <length / end>\"");
72#else
73static char *map;
74#endif
75
76static slram_mtd_list_t *slram_mtdlist = NULL;
77
78static int slram_erase(struct mtd_info *, struct erase_info *);
79static int slram_point(struct mtd_info *, loff_t, size_t, size_t *, u_char **);
80static void slram_unpoint(struct mtd_info *, u_char *, loff_t, size_t);
81static int slram_read(struct mtd_info *, loff_t, size_t, size_t *, u_char *);
82static int slram_write(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
83
84static int slram_erase(struct mtd_info *mtd, struct erase_info *instr)
85{
86 slram_priv_t *priv = mtd->priv;
87
88 if (instr->addr + instr->len > mtd->size) {
89 return(-EINVAL);
90 }
Thomas Gleixnere5580fb2005-11-07 11:15:40 +000091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 memset(priv->start + instr->addr, 0xff, instr->len);
93
Thomas Gleixnere5580fb2005-11-07 11:15:40 +000094 /* This'll catch a few races. Free the thing before returning :)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 * I don't feel at all ashamed. This kind of thing is possible anyway
96 * with flash, but unlikely.
97 */
98
99 instr->state = MTD_ERASE_DONE;
100
101 mtd_erase_callback(instr);
102
103 return(0);
104}
105
106static int slram_point(struct mtd_info *mtd, loff_t from, size_t len,
107 size_t *retlen, u_char **mtdbuf)
108{
109 slram_priv_t *priv = mtd->priv;
110
Josh Boyer14f83512005-01-06 21:16:45 +0000111 if (from + len > mtd->size)
112 return -EINVAL;
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 *mtdbuf = priv->start + from;
115 *retlen = len;
116 return(0);
117}
118
119static void slram_unpoint(struct mtd_info *mtd, u_char *addr, loff_t from, size_t len)
120{
121}
122
123static int slram_read(struct mtd_info *mtd, loff_t from, size_t len,
124 size_t *retlen, u_char *buf)
125{
126 slram_priv_t *priv = mtd->priv;
Josh Boyer14f83512005-01-06 21:16:45 +0000127
128 if (from > mtd->size)
129 return -EINVAL;
130
131 if (from + len > mtd->size)
132 len = mtd->size - from;
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 memcpy(buf, priv->start + from, len);
135
136 *retlen = len;
137 return(0);
138}
139
140static int slram_write(struct mtd_info *mtd, loff_t to, size_t len,
141 size_t *retlen, const u_char *buf)
142{
143 slram_priv_t *priv = mtd->priv;
144
Josh Boyer14f83512005-01-06 21:16:45 +0000145 if (to + len > mtd->size)
146 return -EINVAL;
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 memcpy(priv->start + to, buf, len);
149
150 *retlen = len;
151 return(0);
152}
153
154/*====================================================================*/
155
156static int register_device(char *name, unsigned long start, unsigned long length)
157{
158 slram_mtd_list_t **curmtd;
159
160 curmtd = &slram_mtdlist;
161 while (*curmtd) {
162 curmtd = &(*curmtd)->next;
163 }
164
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);
169 }
Burman Yan95b93a02006-11-15 21:10:29 +0200170 (*curmtd)->mtdinfo = kzalloc(sizeof(struct mtd_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 (*curmtd)->next = NULL;
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 if ((*curmtd)->mtdinfo) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 (*curmtd)->mtdinfo->priv =
Burman Yan95b93a02006-11-15 21:10:29 +0200175 kzalloc(sizeof(slram_priv_t), GFP_KERNEL);
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 if (!(*curmtd)->mtdinfo->priv) {
178 kfree((*curmtd)->mtdinfo);
179 (*curmtd)->mtdinfo = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 }
181 }
182
183 if (!(*curmtd)->mtdinfo) {
184 E("slram: Cannot allocate new MTD device.\n");
185 return(-ENOMEM);
186 }
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 if (!(((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start =
189 ioremap(start, length))) {
190 E("slram: ioremap failed\n");
191 return -EIO;
192 }
193 ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->end =
194 ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start + length;
195
196
197 (*curmtd)->mtdinfo->name = name;
198 (*curmtd)->mtdinfo->size = length;
Jörn Engela6c591e2006-04-13 18:54:34 +0200199 (*curmtd)->mtdinfo->flags = MTD_CAP_RAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 (*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;
David Woodhouse21c8db92006-06-14 21:39:48 +0100206 (*curmtd)->mtdinfo->type = MTD_RAM;
Josh Boyer14f83512005-01-06 21:16:45 +0000207 (*curmtd)->mtdinfo->erasesize = SLRAM_BLK_SZ;
Artem B. Bityutskiy17ffc7b2006-06-22 18:15:48 +0400208 (*curmtd)->mtdinfo->writesize = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
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);
216 }
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);
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000222 return(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
225static void unregister_devices(void)
226{
227 slram_mtd_list_t *nextitem;
228
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;
237 }
238}
239
240static unsigned long handle_unit(unsigned long value, char *unit)
241{
242 if ((*unit == 'M') || (*unit == 'm')) {
243 return(value * 1024 * 1024);
244 } else if ((*unit == 'K') || (*unit == 'k')) {
245 return(value * 1024);
246 }
247 return(value);
248}
249
250static int parse_cmdline(char *devname, char *szstart, char *szlength)
251{
252 char *buffer;
253 unsigned long devstart;
254 unsigned long devlength;
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if ((!devname) || (!szstart) || (!szlength)) {
257 unregister_devices();
258 return(-EINVAL);
259 }
260
261 devstart = simple_strtoul(szstart, &buffer, 0);
262 devstart = handle_unit(devstart, buffer);
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 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);
270 }
271 T("slram: devname=%s, devstart=0x%lx, devlength=0x%lx\n",
272 devname, devstart, devlength);
Josh Boyer14f83512005-01-06 21:16:45 +0000273 if ((devstart < 0) || (devlength < 0) || (devlength % SLRAM_BLK_SZ != 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 E("slram: Illegal start / length parameter.\n");
275 return(-EINVAL);
276 }
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if ((devstart = register_device(devname, devstart, devlength))){
279 unregister_devices();
280 return((int)devstart);
281 }
282 return(0);
283}
284
285#ifndef MODULE
286
287static int __init mtd_slram_setup(char *str)
288{
289 map = str;
290 return(1);
291}
292
293__setup("slram=", mtd_slram_setup);
294
295#endif
296
297static int init_slram(void)
298{
299 char *devname;
300 int i;
301
302#ifndef MODULE
303 char *devstart;
304 char *devlength;
305
306 i = 0;
307
308 if (!map) {
309 E("slram: not enough parameters.\n");
310 return(-EINVAL);
311 }
312 while (map) {
313 devname = devstart = devlength = NULL;
314
315 if (!(devname = strsep(&map, ","))) {
316 E("slram: No devicename specified.\n");
317 break;
318 }
319 T("slram: devname = %s\n", devname);
320 if ((!map) || (!(devstart = strsep(&map, ",")))) {
321 E("slram: No devicestart specified.\n");
322 }
323 T("slram: devstart = %s\n", devstart);
324 if ((!map) || (!(devlength = strsep(&map, ",")))) {
325 E("slram: No devicelength / -end specified.\n");
326 }
327 T("slram: devlength = %s\n", devlength);
328 if (parse_cmdline(devname, devstart, devlength) != 0) {
329 return(-EINVAL);
330 }
331 }
332#else
333 int count;
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 for (count = 0; (map[count]) && (count < SLRAM_MAX_DEVICES_PARAMS);
336 count++) {
337 }
338
339 if ((count % 3 != 0) || (count == 0)) {
340 E("slram: not enough parameters.\n");
341 return(-EINVAL);
342 }
343 for (i = 0; i < (count / 3); i++) {
344 devname = map[i * 3];
345
346 if (parse_cmdline(devname, map[i * 3 + 1], map[i * 3 + 2])!=0) {
347 return(-EINVAL);
348 }
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
351#endif /* !MODULE */
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return(0);
354}
355
356static void __exit cleanup_slram(void)
357{
358 unregister_devices();
359}
360
361module_init(init_slram);
362module_exit(cleanup_slram);
363
364MODULE_LICENSE("GPL");
365MODULE_AUTHOR("Jochen Schaeuble <psionic@psionic.de>");
366MODULE_DESCRIPTION("MTD driver for uncached system RAM");