1 /** 2 * Copyright (c) ???? Jochen Schäuble <psionic@psionic.de> 3 * Copyright (c) 2003-2004 Joern Engel <joern@wh.fh-wedel.de> 4 * 5 * Usage: 6 * 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 12 * gigabytes. 13 * 14 * Example: 15 * phram=swap,64Mi,128Mi phram=test,900Mi,1Mi 16 */ 17 18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 19 20 #include <asm/io.h> 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> 28 29 struct phram_mtd_list { 30 struct mtd_info mtd; 31 struct list_head list; 32 }; 33 34 static LIST_HEAD(phram_list); 35 36 static int phram_erase(struct mtd_info *mtd, struct erase_info *instr) 37 { 38 u_char *start = mtd->priv; 39 40 memset(start + instr->addr, 0xff, instr->len); 41 42 /* This'll catch a few races. Free the thing before returning :) 43 * I don't feel at all ashamed. This kind of thing is possible anyway 44 * with flash, but unlikely. 45 */ 46 instr->state = MTD_ERASE_DONE; 47 mtd_erase_callback(instr); 48 return 0; 49 } 50 51 static int phram_point(struct mtd_info *mtd, loff_t from, size_t len, 52 size_t *retlen, void **virt, resource_size_t *phys) 53 { 54 /* can we return a physical address with this driver? */ 55 if (phys) 56 return -EINVAL; 57 58 *virt = mtd->priv + from; 59 *retlen = len; 60 return 0; 61 } 62 63 static int phram_unpoint(struct mtd_info *mtd, loff_t from, size_t len) 64 { 65 return 0; 66 } 67 68 static int phram_read(struct mtd_info *mtd, loff_t from, size_t len, 69 size_t *retlen, u_char *buf) 70 { 71 u_char *start = mtd->priv; 72 73 memcpy(buf, start + from, len); 74 *retlen = len; 75 return 0; 76 } 77 78 static int phram_write(struct mtd_info *mtd, loff_t to, size_t len, 79 size_t *retlen, const u_char *buf) 80 { 81 u_char *start = mtd->priv; 82 83 memcpy(start + to, buf, len); 84 *retlen = len; 85 return 0; 86 } 87 88 89 90 static void unregister_devices(void) 91 { 92 struct phram_mtd_list *this, *safe; 93 94 list_for_each_entry_safe(this, safe, &phram_list, list) { 95 mtd_device_unregister(&this->mtd); 96 iounmap(this->mtd.priv); 97 kfree(this->mtd.name); 98 kfree(this); 99 } 100 } 101 102 static int register_device(char *name, unsigned long start, unsigned long len) 103 { 104 struct phram_mtd_list *new; 105 int ret = -ENOMEM; 106 107 new = kzalloc(sizeof(*new), GFP_KERNEL); 108 if (!new) 109 goto out0; 110 111 ret = -EIO; 112 new->mtd.priv = ioremap(start, len); 113 if (!new->mtd.priv) { 114 pr_err("ioremap failed\n"); 115 goto out1; 116 } 117 118 119 new->mtd.name = name; 120 new->mtd.size = len; 121 new->mtd.flags = MTD_CAP_RAM; 122 new->mtd._erase = phram_erase; 123 new->mtd._point = phram_point; 124 new->mtd._unpoint = phram_unpoint; 125 new->mtd._read = phram_read; 126 new->mtd._write = phram_write; 127 new->mtd.owner = THIS_MODULE; 128 new->mtd.type = MTD_RAM; 129 new->mtd.erasesize = PAGE_SIZE; 130 new->mtd.writesize = 1; 131 132 ret = -EAGAIN; 133 if (mtd_device_register(&new->mtd, NULL, 0)) { 134 pr_err("Failed to register new device\n"); 135 goto out2; 136 } 137 138 list_add_tail(&new->list, &phram_list); 139 return 0; 140 141 out2: 142 iounmap(new->mtd.priv); 143 out1: 144 kfree(new); 145 out0: 146 return ret; 147 } 148 149 static int ustrtoul(const char *cp, char **endp, unsigned int base) 150 { 151 unsigned long result = simple_strtoul(cp, endp, base); 152 153 switch (**endp) { 154 case 'G': 155 result *= 1024; 156 case 'M': 157 result *= 1024; 158 case 'k': 159 result *= 1024; 160 /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */ 161 if ((*endp)[1] == 'i') 162 (*endp) += 2; 163 } 164 return result; 165 } 166 167 static int parse_num32(uint32_t *num32, const char *token) 168 { 169 char *endp; 170 unsigned long n; 171 172 n = ustrtoul(token, &endp, 0); 173 if (*endp) 174 return -EINVAL; 175 176 *num32 = n; 177 return 0; 178 } 179 180 static int parse_name(char **pname, const char *token) 181 { 182 size_t len; 183 char *name; 184 185 len = strlen(token) + 1; 186 if (len > 64) 187 return -ENOSPC; 188 189 name = kmalloc(len, GFP_KERNEL); 190 if (!name) 191 return -ENOMEM; 192 193 strcpy(name, token); 194 195 *pname = name; 196 return 0; 197 } 198 199 200 static inline void kill_final_newline(char *str) 201 { 202 char *newline = strrchr(str, '\n'); 203 if (newline && !newline[1]) 204 *newline = 0; 205 } 206 207 208 #define parse_err(fmt, args...) do { \ 209 pr_err(fmt , ## args); \ 210 return 1; \ 211 } while (0) 212 213 static int phram_setup(const char *val, struct kernel_param *kp) 214 { 215 char buf[64+12+12], *str = buf; 216 char *token[3]; 217 char *name; 218 uint32_t start; 219 uint32_t len; 220 int i, ret; 221 222 if (strnlen(val, sizeof(buf)) >= sizeof(buf)) 223 parse_err("parameter too long\n"); 224 225 strcpy(str, val); 226 kill_final_newline(str); 227 228 for (i=0; i<3; i++) 229 token[i] = strsep(&str, ","); 230 231 if (str) 232 parse_err("too many arguments\n"); 233 234 if (!token[2]) 235 parse_err("not enough arguments\n"); 236 237 ret = parse_name(&name, token[0]); 238 if (ret) 239 return ret; 240 241 ret = parse_num32(&start, token[1]); 242 if (ret) { 243 kfree(name); 244 parse_err("illegal start address\n"); 245 } 246 247 ret = parse_num32(&len, token[2]); 248 if (ret) { 249 kfree(name); 250 parse_err("illegal device length\n"); 251 } 252 253 ret = register_device(name, start, len); 254 if (!ret) 255 pr_info("%s device: %#x at %#x\n", name, len, start); 256 else 257 kfree(name); 258 259 return ret; 260 } 261 262 module_param_call(phram, phram_setup, NULL, NULL, 000); 263 MODULE_PARM_DESC(phram, "Memory region to map. \"phram=<name>,<start>,<length>\""); 264 265 266 static int __init init_phram(void) 267 { 268 return 0; 269 } 270 271 static void __exit cleanup_phram(void) 272 { 273 unregister_devices(); 274 } 275 276 module_init(init_phram); 277 module_exit(cleanup_phram); 278 279 MODULE_LICENSE("GPL"); 280 MODULE_AUTHOR("Joern Engel <joern@wh.fh-wedel.de>"); 281 MODULE_DESCRIPTION("MTD driver for physical RAM"); 282