1 /*====================================================================== 2 3 This driver provides a method to access memory not used by the kernel 4 itself (i.e. if the kernel commandline mem=xxx is used). To actually 5 use slram at least mtdblock or mtdchar is required (for block or 6 character device access). 7 8 Usage: 9 10 if compiled as loadable module: 11 modprobe slram map=<name>,<start>,<end/offset> 12 if statically linked into the kernel use the following kernel cmd.line 13 slram=<name>,<start>,<end/offset> 14 15 <name>: name of the device that will be listed in /proc/mtd 16 <start>: start of the memory region, decimal or hex (0xabcdef) 17 <end/offset>: end of the memory region. It's possible to use +0x1234 18 to specify the offset instead of the absolute address 19 20 NOTE: 21 With slram it's only possible to map a contiguous memory region. Therefore 22 if there's a device mapped somewhere in the region specified slram will 23 fail to load (see kernel log if modprobe fails). 24 25 - 26 27 Jochen Schaeuble <psionic@psionic.de> 28 29 ======================================================================*/ 30 31 32 #include <linux/module.h> 33 #include <linux/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> 39 #include <linux/timer.h> 40 #include <linux/major.h> 41 #include <linux/fs.h> 42 #include <linux/ioctl.h> 43 #include <linux/init.h> 44 #include <linux/io.h> 45 46 #include <linux/mtd/mtd.h> 47 48 #define SLRAM_MAX_DEVICES_PARAMS 6 /* 3 parameters / device */ 49 #define SLRAM_BLK_SZ 0x4000 50 51 #define T(fmt, args...) printk(KERN_DEBUG fmt, ## args) 52 #define E(fmt, args...) printk(KERN_NOTICE fmt, ## args) 53 54 typedef struct slram_priv { 55 u_char *start; 56 u_char *end; 57 } slram_priv_t; 58 59 typedef struct slram_mtd_list { 60 struct mtd_info *mtdinfo; 61 struct slram_mtd_list *next; 62 } slram_mtd_list_t; 63 64 #ifdef MODULE 65 static char *map[SLRAM_MAX_DEVICES_PARAMS]; 66 67 module_param_array(map, charp, NULL, 0); 68 MODULE_PARM_DESC(map, "List of memory regions to map. \"map=<name>, <start>, <length / end>\""); 69 #else 70 static char *map; 71 #endif 72 73 static slram_mtd_list_t *slram_mtdlist = NULL; 74 75 static int slram_erase(struct mtd_info *, struct erase_info *); 76 static int slram_point(struct mtd_info *, loff_t, size_t, size_t *, void **, 77 resource_size_t *); 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 *); 81 82 static int slram_erase(struct mtd_info *mtd, struct erase_info *instr) 83 { 84 slram_priv_t *priv = mtd->priv; 85 86 memset(priv->start + instr->addr, 0xff, instr->len); 87 88 return(0); 89 } 90 91 static int slram_point(struct mtd_info *mtd, loff_t from, size_t len, 92 size_t *retlen, void **virt, resource_size_t *phys) 93 { 94 slram_priv_t *priv = mtd->priv; 95 96 *virt = priv->start + from; 97 *retlen = len; 98 return(0); 99 } 100 101 static int slram_unpoint(struct mtd_info *mtd, loff_t from, size_t len) 102 { 103 return 0; 104 } 105 106 static int slram_read(struct mtd_info *mtd, loff_t from, size_t len, 107 size_t *retlen, u_char *buf) 108 { 109 slram_priv_t *priv = mtd->priv; 110 111 memcpy(buf, priv->start + from, len); 112 *retlen = len; 113 return(0); 114 } 115 116 static int slram_write(struct mtd_info *mtd, loff_t to, size_t len, 117 size_t *retlen, const u_char *buf) 118 { 119 slram_priv_t *priv = mtd->priv; 120 121 memcpy(priv->start + to, buf, len); 122 *retlen = len; 123 return(0); 124 } 125 126 /*====================================================================*/ 127 128 static int register_device(char *name, unsigned long start, unsigned long length) 129 { 130 slram_mtd_list_t **curmtd; 131 132 curmtd = &slram_mtdlist; 133 while (*curmtd) { 134 curmtd = &(*curmtd)->next; 135 } 136 137 *curmtd = kmalloc(sizeof(slram_mtd_list_t), GFP_KERNEL); 138 if (!(*curmtd)) { 139 E("slram: Cannot allocate new MTD device.\n"); 140 return(-ENOMEM); 141 } 142 (*curmtd)->mtdinfo = kzalloc(sizeof(struct mtd_info), GFP_KERNEL); 143 (*curmtd)->next = NULL; 144 145 if ((*curmtd)->mtdinfo) { 146 (*curmtd)->mtdinfo->priv = 147 kzalloc(sizeof(slram_priv_t), GFP_KERNEL); 148 149 if (!(*curmtd)->mtdinfo->priv) { 150 kfree((*curmtd)->mtdinfo); 151 (*curmtd)->mtdinfo = NULL; 152 } 153 } 154 155 if (!(*curmtd)->mtdinfo) { 156 E("slram: Cannot allocate new MTD device.\n"); 157 return(-ENOMEM); 158 } 159 160 if (!(((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start = 161 memremap(start, length, 162 MEMREMAP_WB | MEMREMAP_WT | MEMREMAP_WC))) { 163 E("slram: memremap failed\n"); 164 return -EIO; 165 } 166 ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->end = 167 ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start + length; 168 169 170 (*curmtd)->mtdinfo->name = name; 171 (*curmtd)->mtdinfo->size = length; 172 (*curmtd)->mtdinfo->flags = MTD_CAP_RAM; 173 (*curmtd)->mtdinfo->_erase = slram_erase; 174 (*curmtd)->mtdinfo->_point = slram_point; 175 (*curmtd)->mtdinfo->_unpoint = slram_unpoint; 176 (*curmtd)->mtdinfo->_read = slram_read; 177 (*curmtd)->mtdinfo->_write = slram_write; 178 (*curmtd)->mtdinfo->owner = THIS_MODULE; 179 (*curmtd)->mtdinfo->type = MTD_RAM; 180 (*curmtd)->mtdinfo->erasesize = SLRAM_BLK_SZ; 181 (*curmtd)->mtdinfo->writesize = 1; 182 183 if (mtd_device_register((*curmtd)->mtdinfo, NULL, 0)) { 184 E("slram: Failed to register new device\n"); 185 memunmap(((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start); 186 kfree((*curmtd)->mtdinfo->priv); 187 kfree((*curmtd)->mtdinfo); 188 return(-EAGAIN); 189 } 190 T("slram: Registered device %s from %luKiB to %luKiB\n", name, 191 (start / 1024), ((start + length) / 1024)); 192 T("slram: Mapped from 0x%p to 0x%p\n", 193 ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start, 194 ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->end); 195 return(0); 196 } 197 198 static void unregister_devices(void) 199 { 200 slram_mtd_list_t *nextitem; 201 202 while (slram_mtdlist) { 203 nextitem = slram_mtdlist->next; 204 mtd_device_unregister(slram_mtdlist->mtdinfo); 205 memunmap(((slram_priv_t *)slram_mtdlist->mtdinfo->priv)->start); 206 kfree(slram_mtdlist->mtdinfo->priv); 207 kfree(slram_mtdlist->mtdinfo); 208 kfree(slram_mtdlist); 209 slram_mtdlist = nextitem; 210 } 211 } 212 213 static unsigned long handle_unit(unsigned long value, char *unit) 214 { 215 if ((*unit == 'M') || (*unit == 'm')) { 216 return(value * 1024 * 1024); 217 } else if ((*unit == 'K') || (*unit == 'k')) { 218 return(value * 1024); 219 } 220 return(value); 221 } 222 223 static int parse_cmdline(char *devname, char *szstart, char *szlength) 224 { 225 char *buffer; 226 unsigned long devstart; 227 unsigned long devlength; 228 229 if ((!devname) || (!szstart) || (!szlength)) { 230 unregister_devices(); 231 return(-EINVAL); 232 } 233 234 devstart = simple_strtoul(szstart, &buffer, 0); 235 devstart = handle_unit(devstart, buffer); 236 237 if (*(szlength) != '+') { 238 devlength = simple_strtoul(szlength, &buffer, 0); 239 devlength = handle_unit(devlength, buffer); 240 if (devlength < devstart) 241 goto err_out; 242 243 devlength -= devstart; 244 } else { 245 devlength = simple_strtoul(szlength + 1, &buffer, 0); 246 devlength = handle_unit(devlength, buffer); 247 } 248 T("slram: devname=%s, devstart=0x%lx, devlength=0x%lx\n", 249 devname, devstart, devlength); 250 if (devlength % SLRAM_BLK_SZ != 0) 251 goto err_out; 252 253 if ((devstart = register_device(devname, devstart, devlength))){ 254 unregister_devices(); 255 return((int)devstart); 256 } 257 return(0); 258 259 err_out: 260 E("slram: Illegal length parameter.\n"); 261 return(-EINVAL); 262 } 263 264 #ifndef MODULE 265 266 static int __init mtd_slram_setup(char *str) 267 { 268 map = str; 269 return(1); 270 } 271 272 __setup("slram=", mtd_slram_setup); 273 274 #endif 275 276 static int __init init_slram(void) 277 { 278 char *devname; 279 280 #ifndef MODULE 281 char *devstart; 282 char *devlength; 283 284 if (!map) { 285 E("slram: not enough parameters.\n"); 286 return(-EINVAL); 287 } 288 while (map) { 289 devname = devstart = devlength = NULL; 290 291 if (!(devname = strsep(&map, ","))) { 292 E("slram: No devicename specified.\n"); 293 break; 294 } 295 T("slram: devname = %s\n", devname); 296 if ((!map) || (!(devstart = strsep(&map, ",")))) { 297 E("slram: No devicestart specified.\n"); 298 } 299 T("slram: devstart = %s\n", devstart); 300 if ((!map) || (!(devlength = strsep(&map, ",")))) { 301 E("slram: No devicelength / -end specified.\n"); 302 } 303 T("slram: devlength = %s\n", devlength); 304 if (parse_cmdline(devname, devstart, devlength) != 0) { 305 return(-EINVAL); 306 } 307 } 308 #else 309 int count; 310 int i; 311 312 for (count = 0; count < SLRAM_MAX_DEVICES_PARAMS && map[count]; 313 count++) { 314 } 315 316 if ((count % 3 != 0) || (count == 0)) { 317 E("slram: not enough parameters.\n"); 318 return(-EINVAL); 319 } 320 for (i = 0; i < (count / 3); i++) { 321 devname = map[i * 3]; 322 323 if (parse_cmdline(devname, map[i * 3 + 1], map[i * 3 + 2])!=0) { 324 return(-EINVAL); 325 } 326 327 } 328 #endif /* !MODULE */ 329 330 return(0); 331 } 332 333 static void __exit cleanup_slram(void) 334 { 335 unregister_devices(); 336 } 337 338 module_init(init_slram); 339 module_exit(cleanup_slram); 340 341 MODULE_LICENSE("GPL"); 342 MODULE_AUTHOR("Jochen Schaeuble <psionic@psionic.de>"); 343 MODULE_DESCRIPTION("MTD driver for uncached system RAM"); 344