18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22 #include <linux/kernel.h>
23 #include <linux/list.h>
24 #include <linux/module.h>
26 #include <linux/slab.h>
52 static int phram_point(
struct mtd_info *mtd, loff_t
from,
size_t len,
60 static int phram_unpoint(
struct mtd_info *mtd, loff_t
from,
size_t len)
65 static int phram_read(
struct mtd_info *mtd, loff_t
from,
size_t len,
70 memcpy(buf, start + from, len);
75 static int phram_write(
struct mtd_info *mtd, loff_t to,
size_t len,
80 memcpy(start + to, buf, len);
85 static void unregister_devices(
void)
97 static int register_device(
char *
name,
unsigned long start,
unsigned long len)
107 new->mtd.priv =
ioremap(start, len);
108 if (!new->mtd.priv) {
109 pr_err(
"ioremap failed\n");
114 new->mtd.name =
name;
117 new->mtd._erase = phram_erase;
118 new->mtd._point = phram_point;
119 new->mtd._unpoint = phram_unpoint;
120 new->mtd._read = phram_read;
121 new->mtd._write = phram_write;
125 new->mtd.writesize = 1;
129 pr_err(
"Failed to register new device\n");
144 static int ustrtoul(
const char *
cp,
char **endp,
unsigned int base)
156 if ((*endp)[1] ==
'i')
167 n = ustrtoul(token, &endp, 0);
175 static int parse_name(
char **pname,
const char *token)
195 static inline void kill_final_newline(
char *
str)
197 char *newline =
strrchr(str,
'\n');
198 if (newline && !newline[1])
203 #define parse_err(fmt, args...) do { \
204 pr_err(fmt , ## args); \
216 static __initdata char phram_paramline[64+12+12];
218 static int __init phram_setup(
const char *
val)
220 char buf[64+12+12], *str =
buf;
227 if (
strnlen(val,
sizeof(buf)) >=
sizeof(buf))
231 kill_final_newline(str);
234 token[i] =
strsep(&str,
",");
242 ret = parse_name(&name, token[0]);
246 ret = parse_num32(&start, token[1]);
252 ret = parse_num32(&len, token[2]);
258 ret = register_device(name, start, len);
260 pr_info(
"%s device: %#x at %#x\n", name, len, start);
273 if (
strlen(val) >=
sizeof(phram_paramline))
275 strcpy(phram_paramline, val);
281 MODULE_PARM_DESC(phram,
"Memory region to map. \"phram=<name>,<start>,<length>\"");
284 static int __init init_phram(
void)
286 if (phram_paramline[0])
287 return phram_setup(phram_paramline);
292 static void __exit cleanup_phram(
void)
294 unregister_devices();