mtd: harmonize mtd_point interface implementation
[linux-2.6.git] / drivers / mtd / devices / slram.c
blobccd39ff509b168895e34d4b0235eb79544f69ae1
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).
8 Usage:
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
20 NOTE:
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>
41 #include <linux/fs.h>
42 #include <linux/ioctl.h>
43 #include <linux/init.h>
44 #include <asm/io.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 {
56 u_char *start;
57 u_char *end;
58 } slram_priv_t;
60 typedef struct slram_mtd_list {
61 struct mtd_info *mtdinfo;
62 struct slram_mtd_list *next;
63 } slram_mtd_list_t;
65 #ifdef MODULE
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>\"");
70 #else
71 static char *map;
72 #endif
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 **,
78 resource_size_t *);
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);
94 return(0);
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 *virt = priv->start + from;
103 *retlen = len;
104 return(0);
107 static int slram_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
109 return 0;
112 static int slram_read(struct mtd_info *mtd, loff_t from, size_t len,
113 size_t *retlen, u_char *buf)
115 slram_priv_t *priv = mtd->priv;
117 memcpy(buf, priv->start + from, len);
118 *retlen = len;
119 return(0);
122 static int slram_write(struct mtd_info *mtd, loff_t to, size_t len,
123 size_t *retlen, const u_char *buf)
125 slram_priv_t *priv = mtd->priv;
127 memcpy(priv->start + to, buf, len);
128 *retlen = len;
129 return(0);
132 /*====================================================================*/
134 static int register_device(char *name, unsigned long start, unsigned long length)
136 slram_mtd_list_t **curmtd;
138 curmtd = &slram_mtdlist;
139 while (*curmtd) {
140 curmtd = &(*curmtd)->next;
143 *curmtd = kmalloc(sizeof(slram_mtd_list_t), GFP_KERNEL);
144 if (!(*curmtd)) {
145 E("slram: Cannot allocate new MTD device.\n");
146 return(-ENOMEM);
148 (*curmtd)->mtdinfo = kzalloc(sizeof(struct mtd_info), GFP_KERNEL);
149 (*curmtd)->next = NULL;
151 if ((*curmtd)->mtdinfo) {
152 (*curmtd)->mtdinfo->priv =
153 kzalloc(sizeof(slram_priv_t), GFP_KERNEL);
155 if (!(*curmtd)->mtdinfo->priv) {
156 kfree((*curmtd)->mtdinfo);
157 (*curmtd)->mtdinfo = NULL;
161 if (!(*curmtd)->mtdinfo) {
162 E("slram: Cannot allocate new MTD device.\n");
163 return(-ENOMEM);
166 if (!(((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start =
167 ioremap(start, length))) {
168 E("slram: ioremap failed\n");
169 return -EIO;
171 ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->end =
172 ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start + length;
175 (*curmtd)->mtdinfo->name = name;
176 (*curmtd)->mtdinfo->size = length;
177 (*curmtd)->mtdinfo->flags = MTD_CAP_RAM;
178 (*curmtd)->mtdinfo->_erase = slram_erase;
179 (*curmtd)->mtdinfo->_point = slram_point;
180 (*curmtd)->mtdinfo->_unpoint = slram_unpoint;
181 (*curmtd)->mtdinfo->_read = slram_read;
182 (*curmtd)->mtdinfo->_write = slram_write;
183 (*curmtd)->mtdinfo->owner = THIS_MODULE;
184 (*curmtd)->mtdinfo->type = MTD_RAM;
185 (*curmtd)->mtdinfo->erasesize = SLRAM_BLK_SZ;
186 (*curmtd)->mtdinfo->writesize = 1;
188 if (mtd_device_register((*curmtd)->mtdinfo, NULL, 0)) {
189 E("slram: Failed to register new device\n");
190 iounmap(((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start);
191 kfree((*curmtd)->mtdinfo->priv);
192 kfree((*curmtd)->mtdinfo);
193 return(-EAGAIN);
195 T("slram: Registered device %s from %luKiB to %luKiB\n", name,
196 (start / 1024), ((start + length) / 1024));
197 T("slram: Mapped from 0x%p to 0x%p\n",
198 ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start,
199 ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->end);
200 return(0);
203 static void unregister_devices(void)
205 slram_mtd_list_t *nextitem;
207 while (slram_mtdlist) {
208 nextitem = slram_mtdlist->next;
209 mtd_device_unregister(slram_mtdlist->mtdinfo);
210 iounmap(((slram_priv_t *)slram_mtdlist->mtdinfo->priv)->start);
211 kfree(slram_mtdlist->mtdinfo->priv);
212 kfree(slram_mtdlist->mtdinfo);
213 kfree(slram_mtdlist);
214 slram_mtdlist = nextitem;
218 static unsigned long handle_unit(unsigned long value, char *unit)
220 if ((*unit == 'M') || (*unit == 'm')) {
221 return(value * 1024 * 1024);
222 } else if ((*unit == 'K') || (*unit == 'k')) {
223 return(value * 1024);
225 return(value);
228 static int parse_cmdline(char *devname, char *szstart, char *szlength)
230 char *buffer;
231 unsigned long devstart;
232 unsigned long devlength;
234 if ((!devname) || (!szstart) || (!szlength)) {
235 unregister_devices();
236 return(-EINVAL);
239 devstart = simple_strtoul(szstart, &buffer, 0);
240 devstart = handle_unit(devstart, buffer);
242 if (*(szlength) != '+') {
243 devlength = simple_strtoul(szlength, &buffer, 0);
244 devlength = handle_unit(devlength, buffer) - devstart;
245 if (devlength < devstart)
246 goto err_out;
248 devlength -= devstart;
249 } else {
250 devlength = simple_strtoul(szlength + 1, &buffer, 0);
251 devlength = handle_unit(devlength, buffer);
253 T("slram: devname=%s, devstart=0x%lx, devlength=0x%lx\n",
254 devname, devstart, devlength);
255 if (devlength % SLRAM_BLK_SZ != 0)
256 goto err_out;
258 if ((devstart = register_device(devname, devstart, devlength))){
259 unregister_devices();
260 return((int)devstart);
262 return(0);
264 err_out:
265 E("slram: Illegal length parameter.\n");
266 return(-EINVAL);
269 #ifndef MODULE
271 static int __init mtd_slram_setup(char *str)
273 map = str;
274 return(1);
277 __setup("slram=", mtd_slram_setup);
279 #endif
281 static int __init init_slram(void)
283 char *devname;
284 int i;
286 #ifndef MODULE
287 char *devstart;
288 char *devlength;
290 i = 0;
292 if (!map) {
293 E("slram: not enough parameters.\n");
294 return(-EINVAL);
296 while (map) {
297 devname = devstart = devlength = NULL;
299 if (!(devname = strsep(&map, ","))) {
300 E("slram: No devicename specified.\n");
301 break;
303 T("slram: devname = %s\n", devname);
304 if ((!map) || (!(devstart = strsep(&map, ",")))) {
305 E("slram: No devicestart specified.\n");
307 T("slram: devstart = %s\n", devstart);
308 if ((!map) || (!(devlength = strsep(&map, ",")))) {
309 E("slram: No devicelength / -end specified.\n");
311 T("slram: devlength = %s\n", devlength);
312 if (parse_cmdline(devname, devstart, devlength) != 0) {
313 return(-EINVAL);
316 #else
317 int count;
319 for (count = 0; count < SLRAM_MAX_DEVICES_PARAMS && map[count];
320 count++) {
323 if ((count % 3 != 0) || (count == 0)) {
324 E("slram: not enough parameters.\n");
325 return(-EINVAL);
327 for (i = 0; i < (count / 3); i++) {
328 devname = map[i * 3];
330 if (parse_cmdline(devname, map[i * 3 + 1], map[i * 3 + 2])!=0) {
331 return(-EINVAL);
335 #endif /* !MODULE */
337 return(0);
340 static void __exit cleanup_slram(void)
342 unregister_devices();
345 module_init(init_slram);
346 module_exit(cleanup_slram);
348 MODULE_LICENSE("GPL");
349 MODULE_AUTHOR("Jochen Schaeuble <psionic@psionic.de>");
350 MODULE_DESCRIPTION("MTD driver for uncached system RAM");