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 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/list.h>
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/slab.h>
24 #include <linux/mtd/mtd.h>
26 #define ERROR(fmt, args...) printk(KERN_ERR "phram: " fmt , ## args)
28 struct phram_mtd_list {
30 struct list_head list;
33 static LIST_HEAD(phram_list);
36 static int phram_erase(struct mtd_info *mtd, struct erase_info *instr)
38 u_char *start = mtd->priv;
40 if (instr->addr + instr->len > mtd->size)
43 memset(start + instr->addr, 0xff, instr->len);
45 /* This'll catch a few races. Free the thing before returning :)
46 * I don't feel at all ashamed. This kind of thing is possible anyway
47 * with flash, but unlikely.
50 instr->state = MTD_ERASE_DONE;
52 mtd_erase_callback(instr);
57 static int phram_point(struct mtd_info *mtd, loff_t from, size_t len,
58 size_t *retlen, void **virt, resource_size_t *phys)
60 if (from + len > mtd->size)
63 /* can we return a physical address with this driver? */
67 *virt = mtd->priv + from;
72 static void phram_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
76 static int phram_read(struct mtd_info *mtd, loff_t from, size_t len,
77 size_t *retlen, u_char *buf)
79 u_char *start = mtd->priv;
81 if (from >= mtd->size)
84 if (len > mtd->size - from)
85 len = mtd->size - from;
87 memcpy(buf, start + from, len);
93 static int phram_write(struct mtd_info *mtd, loff_t to, size_t len,
94 size_t *retlen, const u_char *buf)
96 u_char *start = mtd->priv;
101 if (len > mtd->size - to)
102 len = mtd->size - to;
104 memcpy(start + to, buf, len);
112 static void unregister_devices(void)
114 struct phram_mtd_list *this, *safe;
116 list_for_each_entry_safe(this, safe, &phram_list, list) {
117 del_mtd_device(&this->mtd);
118 iounmap(this->mtd.priv);
123 static int register_device(char *name, unsigned long start, unsigned long len)
125 struct phram_mtd_list *new;
128 new = kzalloc(sizeof(*new), GFP_KERNEL);
133 new->mtd.priv = ioremap(start, len);
134 if (!new->mtd.priv) {
135 ERROR("ioremap failed\n");
140 new->mtd.name = name;
142 new->mtd.flags = MTD_CAP_RAM;
143 new->mtd.erase = phram_erase;
144 new->mtd.point = phram_point;
145 new->mtd.unpoint = phram_unpoint;
146 new->mtd.read = phram_read;
147 new->mtd.write = phram_write;
148 new->mtd.owner = THIS_MODULE;
149 new->mtd.type = MTD_RAM;
150 new->mtd.erasesize = PAGE_SIZE;
151 new->mtd.writesize = 1;
154 if (add_mtd_device(&new->mtd)) {
155 ERROR("Failed to register new device\n");
159 list_add_tail(&new->list, &phram_list);
163 iounmap(new->mtd.priv);
170 static int ustrtoul(const char *cp, char **endp, unsigned int base)
172 unsigned long result = simple_strtoul(cp, endp, base);
181 /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
182 if ((*endp)[1] == 'i')
188 static int parse_num32(uint32_t *num32, const char *token)
193 n = ustrtoul(token, &endp, 0);
201 static int parse_name(char **pname, const char *token)
206 len = strlen(token) + 1;
210 name = kmalloc(len, GFP_KERNEL);
221 static inline void kill_final_newline(char *str)
223 char *newline = strrchr(str, '\n');
224 if (newline && !newline[1])
229 #define parse_err(fmt, args...) do { \
230 ERROR(fmt , ## args); \
234 static int phram_setup(const char *val, struct kernel_param *kp)
236 char buf[64+12+12], *str = buf;
243 if (strnlen(val, sizeof(buf)) >= sizeof(buf))
244 parse_err("parameter too long\n");
247 kill_final_newline(str);
250 token[i] = strsep(&str, ",");
253 parse_err("too many arguments\n");
256 parse_err("not enough arguments\n");
258 ret = parse_name(&name, token[0]);
260 parse_err("out of memory\n");
262 parse_err("name too long\n");
266 ret = parse_num32(&start, token[1]);
269 parse_err("illegal start address\n");
272 ret = parse_num32(&len, token[2]);
275 parse_err("illegal device length\n");
278 register_device(name, start, len);
283 module_param_call(phram, phram_setup, NULL, NULL, 000);
284 MODULE_PARM_DESC(phram, "Memory region to map. \"phram=<name>,<start>,<length>\"");
287 static int __init init_phram(void)
292 static void __exit cleanup_phram(void)
294 unregister_devices();
297 module_init(init_phram);
298 module_exit(cleanup_phram);
300 MODULE_LICENSE("GPL");
301 MODULE_AUTHOR("Joern Engel <joern@wh.fh-wedel.de>");
302 MODULE_DESCRIPTION("MTD driver for physical RAM");