2 * Copyright (c) ???? Jochen Schäuble <psionic@psionic.de>
3 * Copyright (c) 2003-2004 Joern Engel <joern@wh.fh-wedel.de>
7 * one commend line parameter per device, each in the form:
8 * phram=<name>,<start>,<len>
9 * <name> may be up to 63 characters.
10 * <start> and <len> can be octal, decimal or hexadecimal. If followed
11 * by "ki", "Mi" or "Gi", the numbers will be interpreted as kilo, mega or
15 * phram=swap,64Mi,128Mi phram=test,900Mi,1Mi
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/init.h>
22 #include <linux/kernel.h>
23 #include <linux/list.h>
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/slab.h>
27 #include <linux/mtd/mtd.h>
29 struct phram_mtd_list {
31 struct list_head list;
34 static LIST_HEAD(phram_list);
37 static int phram_erase(struct mtd_info *mtd, struct erase_info *instr)
39 u_char *start = mtd->priv;
41 memset(start + instr->addr, 0xff, instr->len);
43 /* This'll catch a few races. Free the thing before returning :)
44 * I don't feel at all ashamed. This kind of thing is possible anyway
45 * with flash, but unlikely.
47 instr->state = MTD_ERASE_DONE;
48 mtd_erase_callback(instr);
52 static int phram_point(struct mtd_info *mtd, loff_t from, size_t len,
53 size_t *retlen, void **virt, resource_size_t *phys)
55 /* can we return a physical address with this driver? */
59 *virt = mtd->priv + from;
64 static int phram_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
69 static int phram_read(struct mtd_info *mtd, loff_t from, size_t len,
70 size_t *retlen, u_char *buf)
72 u_char *start = mtd->priv;
74 memcpy(buf, start + from, len);
79 static int phram_write(struct mtd_info *mtd, loff_t to, size_t len,
80 size_t *retlen, const u_char *buf)
82 u_char *start = mtd->priv;
84 memcpy(start + to, buf, len);
91 static void unregister_devices(void)
93 struct phram_mtd_list *this, *safe;
95 list_for_each_entry_safe(this, safe, &phram_list, list) {
96 mtd_device_unregister(&this->mtd);
97 iounmap(this->mtd.priv);
98 kfree(this->mtd.name);
103 static int register_device(char *name, unsigned long start, unsigned long len)
105 struct phram_mtd_list *new;
108 new = kzalloc(sizeof(*new), GFP_KERNEL);
113 new->mtd.priv = ioremap(start, len);
114 if (!new->mtd.priv) {
115 pr_err("ioremap failed\n");
120 new->mtd.name = name;
122 new->mtd.flags = MTD_CAP_RAM;
123 new->mtd._erase = phram_erase;
124 new->mtd._point = phram_point;
125 new->mtd._unpoint = phram_unpoint;
126 new->mtd._read = phram_read;
127 new->mtd._write = phram_write;
128 new->mtd.owner = THIS_MODULE;
129 new->mtd.type = MTD_RAM;
130 new->mtd.erasesize = PAGE_SIZE;
131 new->mtd.writesize = 1;
134 if (mtd_device_register(&new->mtd, NULL, 0)) {
135 pr_err("Failed to register new device\n");
139 list_add_tail(&new->list, &phram_list);
143 iounmap(new->mtd.priv);
150 static int ustrtoul(const char *cp, char **endp, unsigned int base)
152 unsigned long result = simple_strtoul(cp, endp, base);
161 /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
162 if ((*endp)[1] == 'i')
168 static int parse_num32(uint32_t *num32, const char *token)
173 n = ustrtoul(token, &endp, 0);
181 static int parse_name(char **pname, const char *token)
186 len = strlen(token) + 1;
190 name = kmalloc(len, GFP_KERNEL);
201 static inline void kill_final_newline(char *str)
203 char *newline = strrchr(str, '\n');
204 if (newline && !newline[1])
209 #define parse_err(fmt, args...) do { \
210 pr_err(fmt , ## args); \
214 static int phram_setup(const char *val, struct kernel_param *kp)
216 char buf[64+12+12], *str = buf;
223 if (strnlen(val, sizeof(buf)) >= sizeof(buf))
224 parse_err("parameter too long\n");
227 kill_final_newline(str);
230 token[i] = strsep(&str, ",");
233 parse_err("too many arguments\n");
236 parse_err("not enough arguments\n");
238 ret = parse_name(&name, token[0]);
242 ret = parse_num32(&start, token[1]);
245 parse_err("illegal start address\n");
248 ret = parse_num32(&len, token[2]);
251 parse_err("illegal device length\n");
254 ret = register_device(name, start, len);
256 pr_info("%s device: %#x at %#x\n", name, len, start);
263 module_param_call(phram, phram_setup, NULL, NULL, 000);
264 MODULE_PARM_DESC(phram, "Memory region to map. \"phram=<name>,<start>,<length>\"");
267 static int __init init_phram(void)
272 static void __exit cleanup_phram(void)
274 unregister_devices();
277 module_init(init_phram);
278 module_exit(cleanup_phram);
280 MODULE_LICENSE("GPL");
281 MODULE_AUTHOR("Joern Engel <joern@wh.fh-wedel.de>");
282 MODULE_DESCRIPTION("MTD driver for physical RAM");