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>
48 #define SLRAM_MAX_DEVICES_PARAMS 6
49 #define SLRAM_BLK_SZ 0x4000
51 #define T(fmt, args...) printk(KERN_DEBUG fmt, ## args)
52 #define E(fmt, args...) printk(KERN_NOTICE fmt, ## args)
68 MODULE_PARM_DESC(
map,
"List of memory regions to map. \"map=<name>, <start>, <length / end>\"");
76 static int slram_point(
struct mtd_info *, loff_t,
size_t,
size_t *,
void **,
78 static int slram_unpoint(
struct mtd_info *, loff_t,
size_t);
79 static int slram_read(
struct mtd_info *, loff_t,
size_t,
size_t *,
u_char *);
80 static int slram_write(
struct mtd_info *, loff_t,
size_t,
size_t *,
const u_char *);
96 static int slram_point(
struct mtd_info *mtd, loff_t
from,
size_t len,
106 static int slram_unpoint(
struct mtd_info *mtd, loff_t from,
size_t len)
111 static int slram_read(
struct mtd_info *mtd, loff_t from,
size_t len,
121 static int slram_write(
struct mtd_info *mtd, loff_t to,
size_t len,
122 size_t *retlen,
const u_char *buf)
133 static int register_device(
char *
name,
unsigned long start,
unsigned long length)
137 curmtd = &slram_mtdlist;
139 curmtd = &(*curmtd)->
next;
144 E(
"slram: Cannot allocate new MTD device.\n");
148 (*curmtd)->next =
NULL;
150 if ((*curmtd)->mtdinfo) {
151 (*curmtd)->mtdinfo->priv =
154 if (!(*curmtd)->mtdinfo->priv) {
155 kfree((*curmtd)->mtdinfo);
156 (*curmtd)->mtdinfo =
NULL;
160 if (!(*curmtd)->mtdinfo) {
161 E(
"slram: Cannot allocate new MTD device.\n");
165 if (!(((
slram_priv_t *)(*curmtd)->mtdinfo->priv)->start =
167 E(
"slram: ioremap failed\n");
174 (*curmtd)->mtdinfo->name =
name;
175 (*curmtd)->mtdinfo->size =
length;
177 (*curmtd)->mtdinfo->_erase = slram_erase;
178 (*curmtd)->mtdinfo->_point = slram_point;
179 (*curmtd)->mtdinfo->_unpoint = slram_unpoint;
180 (*curmtd)->mtdinfo->_read = slram_read;
181 (*curmtd)->mtdinfo->_write = slram_write;
183 (*curmtd)->mtdinfo->type =
MTD_RAM;
185 (*curmtd)->mtdinfo->writesize = 1;
188 E(
"slram: Failed to register new device\n");
190 kfree((*curmtd)->mtdinfo->priv);
191 kfree((*curmtd)->mtdinfo);
194 T(
"slram: Registered device %s from %luKiB to %luKiB\n", name,
195 (start / 1024), ((start + length) / 1024));
196 T(
"slram: Mapped from 0x%p to 0x%p\n",
202 static void unregister_devices(
void)
206 while (slram_mtdlist) {
207 nextitem = slram_mtdlist->
next;
212 kfree(slram_mtdlist);
213 slram_mtdlist = nextitem;
217 static unsigned long handle_unit(
unsigned long value,
char *
unit)
219 if ((*unit ==
'M') || (*unit ==
'm')) {
220 return(value * 1024 * 1024);
221 }
else if ((*unit ==
'K') || (*unit ==
'k')) {
222 return(value * 1024);
227 static int parse_cmdline(
char *devname,
char *szstart,
char *szlength)
230 unsigned long devstart;
231 unsigned long devlength;
233 if ((!devname) || (!szstart) || (!szlength)) {
234 unregister_devices();
239 devstart = handle_unit(devstart, buffer);
241 if (*(szlength) !=
'+') {
243 devlength = handle_unit(devlength, buffer);
244 if (devlength < devstart)
247 devlength -= devstart;
250 devlength = handle_unit(devlength, buffer);
252 T(
"slram: devname=%s, devstart=0x%lx, devlength=0x%lx\n",
253 devname, devstart, devlength);
257 if ((devstart = register_device(devname, devstart, devlength))){
258 unregister_devices();
259 return((
int)devstart);
264 E(
"slram: Illegal length parameter.\n");
270 static int __init mtd_slram_setup(
char *
str)
276 __setup(
"slram=", mtd_slram_setup);
280 static int __init init_slram(
void)
292 E(
"slram: not enough parameters.\n");
296 devname = devstart = devlength =
NULL;
299 E(
"slram: No devicename specified.\n");
302 T(
"slram: devname = %s\n", devname);
304 E(
"slram: No devicestart specified.\n");
306 T(
"slram: devstart = %s\n", devstart);
308 E(
"slram: No devicelength / -end specified.\n");
310 T(
"slram: devlength = %s\n", devlength);
311 if (parse_cmdline(devname, devstart, devlength) != 0) {
322 if ((count % 3 != 0) || (count == 0)) {
323 E(
"slram: not enough parameters.\n");
326 for (i = 0; i < (count / 3); i++) {
327 devname = map[i * 3];
329 if (parse_cmdline(devname, map[i * 3 + 1], map[i * 3 + 2])!=0) {
339 static void __exit cleanup_slram(
void)
341 unregister_devices();