Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /*====================================================================== |
| 2 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 19 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | NOTE: |
| 21 | With slram it's only possible to map a contigous memory region. Therfore |
| 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 | - |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | Jochen Schaeuble <psionic@psionic.de> |
| 28 | |
| 29 | ======================================================================*/ |
| 30 | |
| 31 | |
| 32 | #include <linux/module.h> |
| 33 | #include <asm/uaccess.h> |
| 34 | #include <linux/types.h> |
| 35 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 <asm/io.h> |
| 45 | #include <asm/system.h> |
| 46 | |
| 47 | #include <linux/mtd/mtd.h> |
| 48 | |
| 49 | #define SLRAM_MAX_DEVICES_PARAMS 6 /* 3 parameters / device */ |
Josh Boyer | 14f8351 | 2005-01-06 21:16:45 +0000 | [diff] [blame] | 50 | #define SLRAM_BLK_SZ 0x4000 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
| 52 | #define T(fmt, args...) printk(KERN_DEBUG fmt, ## args) |
| 53 | #define E(fmt, args...) printk(KERN_NOTICE fmt, ## args) |
| 54 | |
| 55 | typedef struct slram_priv { |
| 56 | u_char *start; |
| 57 | u_char *end; |
| 58 | } slram_priv_t; |
| 59 | |
| 60 | typedef struct slram_mtd_list { |
| 61 | struct mtd_info *mtdinfo; |
| 62 | struct slram_mtd_list *next; |
| 63 | } slram_mtd_list_t; |
| 64 | |
| 65 | #ifdef MODULE |
| 66 | static char *map[SLRAM_MAX_DEVICES_PARAMS]; |
| 67 | |
| 68 | module_param_array(map, charp, NULL, 0); |
| 69 | MODULE_PARM_DESC(map, "List of memory regions to map. \"map=<name>, <start>, <length / end>\""); |
| 70 | #else |
| 71 | static char *map; |
| 72 | #endif |
| 73 | |
| 74 | static slram_mtd_list_t *slram_mtdlist = NULL; |
| 75 | |
| 76 | static int slram_erase(struct mtd_info *, struct erase_info *); |
Jared Hulbert | a98889f | 2008-04-29 23:26:49 -0700 | [diff] [blame] | 77 | static int slram_point(struct mtd_info *, loff_t, size_t, size_t *, void **, |
| 78 | resource_size_t *); |
| 79 | static void slram_unpoint(struct mtd_info *, loff_t, size_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | static int slram_read(struct mtd_info *, loff_t, size_t, size_t *, u_char *); |
| 81 | static int slram_write(struct mtd_info *, loff_t, size_t, size_t *, const u_char *); |
| 82 | |
| 83 | static int slram_erase(struct mtd_info *mtd, struct erase_info *instr) |
| 84 | { |
| 85 | slram_priv_t *priv = mtd->priv; |
| 86 | |
| 87 | if (instr->addr + instr->len > mtd->size) { |
| 88 | return(-EINVAL); |
| 89 | } |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 90 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | memset(priv->start + instr->addr, 0xff, instr->len); |
| 92 | |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 93 | /* This'll catch a few races. Free the thing before returning :) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | * I don't feel at all ashamed. This kind of thing is possible anyway |
| 95 | * with flash, but unlikely. |
| 96 | */ |
| 97 | |
| 98 | instr->state = MTD_ERASE_DONE; |
| 99 | |
| 100 | mtd_erase_callback(instr); |
| 101 | |
| 102 | return(0); |
| 103 | } |
| 104 | |
| 105 | static int slram_point(struct mtd_info *mtd, loff_t from, size_t len, |
Jared Hulbert | a98889f | 2008-04-29 23:26:49 -0700 | [diff] [blame] | 106 | size_t *retlen, void **virt, resource_size_t *phys) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | { |
| 108 | slram_priv_t *priv = mtd->priv; |
| 109 | |
Jared Hulbert | a98889f | 2008-04-29 23:26:49 -0700 | [diff] [blame] | 110 | /* can we return a physical address with this driver? */ |
| 111 | if (phys) |
| 112 | return -EINVAL; |
| 113 | |
Josh Boyer | 14f8351 | 2005-01-06 21:16:45 +0000 | [diff] [blame] | 114 | if (from + len > mtd->size) |
| 115 | return -EINVAL; |
| 116 | |
Jared Hulbert | a98889f | 2008-04-29 23:26:49 -0700 | [diff] [blame] | 117 | *virt = priv->start + from; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | *retlen = len; |
| 119 | return(0); |
| 120 | } |
| 121 | |
Jared Hulbert | a98889f | 2008-04-29 23:26:49 -0700 | [diff] [blame] | 122 | static void slram_unpoint(struct mtd_info *mtd, loff_t from, size_t len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | { |
| 124 | } |
| 125 | |
| 126 | static int slram_read(struct mtd_info *mtd, loff_t from, size_t len, |
| 127 | size_t *retlen, u_char *buf) |
| 128 | { |
| 129 | slram_priv_t *priv = mtd->priv; |
Josh Boyer | 14f8351 | 2005-01-06 21:16:45 +0000 | [diff] [blame] | 130 | |
| 131 | if (from > mtd->size) |
| 132 | return -EINVAL; |
| 133 | |
| 134 | if (from + len > mtd->size) |
| 135 | len = mtd->size - from; |
| 136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | memcpy(buf, priv->start + from, len); |
| 138 | |
| 139 | *retlen = len; |
| 140 | return(0); |
| 141 | } |
| 142 | |
| 143 | static int slram_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 144 | size_t *retlen, const u_char *buf) |
| 145 | { |
| 146 | slram_priv_t *priv = mtd->priv; |
| 147 | |
Josh Boyer | 14f8351 | 2005-01-06 21:16:45 +0000 | [diff] [blame] | 148 | if (to + len > mtd->size) |
| 149 | return -EINVAL; |
| 150 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | memcpy(priv->start + to, buf, len); |
| 152 | |
| 153 | *retlen = len; |
| 154 | return(0); |
| 155 | } |
| 156 | |
| 157 | /*====================================================================*/ |
| 158 | |
| 159 | static int register_device(char *name, unsigned long start, unsigned long length) |
| 160 | { |
| 161 | slram_mtd_list_t **curmtd; |
| 162 | |
| 163 | curmtd = &slram_mtdlist; |
| 164 | while (*curmtd) { |
| 165 | curmtd = &(*curmtd)->next; |
| 166 | } |
| 167 | |
| 168 | *curmtd = kmalloc(sizeof(slram_mtd_list_t), GFP_KERNEL); |
| 169 | if (!(*curmtd)) { |
| 170 | E("slram: Cannot allocate new MTD device.\n"); |
| 171 | return(-ENOMEM); |
| 172 | } |
Burman Yan | 95b93a0 | 2006-11-15 21:10:29 +0200 | [diff] [blame] | 173 | (*curmtd)->mtdinfo = kzalloc(sizeof(struct mtd_info), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | (*curmtd)->next = NULL; |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 175 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | if ((*curmtd)->mtdinfo) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | (*curmtd)->mtdinfo->priv = |
Burman Yan | 95b93a0 | 2006-11-15 21:10:29 +0200 | [diff] [blame] | 178 | kzalloc(sizeof(slram_priv_t), GFP_KERNEL); |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | if (!(*curmtd)->mtdinfo->priv) { |
| 181 | kfree((*curmtd)->mtdinfo); |
| 182 | (*curmtd)->mtdinfo = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
| 186 | if (!(*curmtd)->mtdinfo) { |
| 187 | E("slram: Cannot allocate new MTD device.\n"); |
| 188 | return(-ENOMEM); |
| 189 | } |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | if (!(((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start = |
| 192 | ioremap(start, length))) { |
| 193 | E("slram: ioremap failed\n"); |
| 194 | return -EIO; |
| 195 | } |
| 196 | ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->end = |
| 197 | ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start + length; |
| 198 | |
| 199 | |
| 200 | (*curmtd)->mtdinfo->name = name; |
| 201 | (*curmtd)->mtdinfo->size = length; |
Jörn Engel | a6c591e | 2006-04-13 18:54:34 +0200 | [diff] [blame] | 202 | (*curmtd)->mtdinfo->flags = MTD_CAP_RAM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | (*curmtd)->mtdinfo->erase = slram_erase; |
| 204 | (*curmtd)->mtdinfo->point = slram_point; |
| 205 | (*curmtd)->mtdinfo->unpoint = slram_unpoint; |
| 206 | (*curmtd)->mtdinfo->read = slram_read; |
| 207 | (*curmtd)->mtdinfo->write = slram_write; |
| 208 | (*curmtd)->mtdinfo->owner = THIS_MODULE; |
David Woodhouse | 21c8db9 | 2006-06-14 21:39:48 +0100 | [diff] [blame] | 209 | (*curmtd)->mtdinfo->type = MTD_RAM; |
Josh Boyer | 14f8351 | 2005-01-06 21:16:45 +0000 | [diff] [blame] | 210 | (*curmtd)->mtdinfo->erasesize = SLRAM_BLK_SZ; |
Artem B. Bityutskiy | 17ffc7b | 2006-06-22 18:15:48 +0400 | [diff] [blame] | 211 | (*curmtd)->mtdinfo->writesize = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
| 213 | if (add_mtd_device((*curmtd)->mtdinfo)) { |
| 214 | E("slram: Failed to register new device\n"); |
| 215 | iounmap(((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start); |
| 216 | kfree((*curmtd)->mtdinfo->priv); |
| 217 | kfree((*curmtd)->mtdinfo); |
| 218 | return(-EAGAIN); |
| 219 | } |
| 220 | T("slram: Registered device %s from %luKiB to %luKiB\n", name, |
| 221 | (start / 1024), ((start + length) / 1024)); |
| 222 | T("slram: Mapped from 0x%p to 0x%p\n", |
| 223 | ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->start, |
| 224 | ((slram_priv_t *)(*curmtd)->mtdinfo->priv)->end); |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 225 | return(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | static void unregister_devices(void) |
| 229 | { |
| 230 | slram_mtd_list_t *nextitem; |
| 231 | |
| 232 | while (slram_mtdlist) { |
| 233 | nextitem = slram_mtdlist->next; |
| 234 | del_mtd_device(slram_mtdlist->mtdinfo); |
| 235 | iounmap(((slram_priv_t *)slram_mtdlist->mtdinfo->priv)->start); |
| 236 | kfree(slram_mtdlist->mtdinfo->priv); |
| 237 | kfree(slram_mtdlist->mtdinfo); |
| 238 | kfree(slram_mtdlist); |
| 239 | slram_mtdlist = nextitem; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | static unsigned long handle_unit(unsigned long value, char *unit) |
| 244 | { |
| 245 | if ((*unit == 'M') || (*unit == 'm')) { |
| 246 | return(value * 1024 * 1024); |
| 247 | } else if ((*unit == 'K') || (*unit == 'k')) { |
| 248 | return(value * 1024); |
| 249 | } |
| 250 | return(value); |
| 251 | } |
| 252 | |
| 253 | static int parse_cmdline(char *devname, char *szstart, char *szlength) |
| 254 | { |
| 255 | char *buffer; |
| 256 | unsigned long devstart; |
| 257 | unsigned long devlength; |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 258 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | if ((!devname) || (!szstart) || (!szlength)) { |
| 260 | unregister_devices(); |
| 261 | return(-EINVAL); |
| 262 | } |
| 263 | |
| 264 | devstart = simple_strtoul(szstart, &buffer, 0); |
| 265 | devstart = handle_unit(devstart, buffer); |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 266 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | if (*(szlength) != '+') { |
| 268 | devlength = simple_strtoul(szlength, &buffer, 0); |
| 269 | devlength = handle_unit(devlength, buffer) - devstart; |
Roel Kluin | 3afd522 | 2009-01-19 00:15:13 +0100 | [diff] [blame] | 270 | if (devlength < devstart) |
| 271 | goto err_out; |
| 272 | |
| 273 | devlength -= devstart; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | } else { |
| 275 | devlength = simple_strtoul(szlength + 1, &buffer, 0); |
| 276 | devlength = handle_unit(devlength, buffer); |
| 277 | } |
| 278 | T("slram: devname=%s, devstart=0x%lx, devlength=0x%lx\n", |
| 279 | devname, devstart, devlength); |
Roel Kluin | 3afd522 | 2009-01-19 00:15:13 +0100 | [diff] [blame] | 280 | if (devlength % SLRAM_BLK_SZ != 0) |
| 281 | goto err_out; |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | if ((devstart = register_device(devname, devstart, devlength))){ |
| 284 | unregister_devices(); |
| 285 | return((int)devstart); |
| 286 | } |
| 287 | return(0); |
Roel Kluin | 3afd522 | 2009-01-19 00:15:13 +0100 | [diff] [blame] | 288 | |
| 289 | err_out: |
| 290 | E("slram: Illegal length parameter.\n"); |
| 291 | return(-EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | #ifndef MODULE |
| 295 | |
| 296 | static int __init mtd_slram_setup(char *str) |
| 297 | { |
| 298 | map = str; |
| 299 | return(1); |
| 300 | } |
| 301 | |
| 302 | __setup("slram=", mtd_slram_setup); |
| 303 | |
| 304 | #endif |
| 305 | |
| 306 | static int init_slram(void) |
| 307 | { |
| 308 | char *devname; |
| 309 | int i; |
| 310 | |
| 311 | #ifndef MODULE |
| 312 | char *devstart; |
| 313 | char *devlength; |
| 314 | |
| 315 | i = 0; |
| 316 | |
| 317 | if (!map) { |
| 318 | E("slram: not enough parameters.\n"); |
| 319 | return(-EINVAL); |
| 320 | } |
| 321 | while (map) { |
| 322 | devname = devstart = devlength = NULL; |
| 323 | |
| 324 | if (!(devname = strsep(&map, ","))) { |
| 325 | E("slram: No devicename specified.\n"); |
| 326 | break; |
| 327 | } |
| 328 | T("slram: devname = %s\n", devname); |
| 329 | if ((!map) || (!(devstart = strsep(&map, ",")))) { |
| 330 | E("slram: No devicestart specified.\n"); |
| 331 | } |
| 332 | T("slram: devstart = %s\n", devstart); |
| 333 | if ((!map) || (!(devlength = strsep(&map, ",")))) { |
| 334 | E("slram: No devicelength / -end specified.\n"); |
| 335 | } |
| 336 | T("slram: devlength = %s\n", devlength); |
| 337 | if (parse_cmdline(devname, devstart, devlength) != 0) { |
| 338 | return(-EINVAL); |
| 339 | } |
| 340 | } |
| 341 | #else |
| 342 | int count; |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 343 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | for (count = 0; (map[count]) && (count < SLRAM_MAX_DEVICES_PARAMS); |
| 345 | count++) { |
| 346 | } |
| 347 | |
| 348 | if ((count % 3 != 0) || (count == 0)) { |
| 349 | E("slram: not enough parameters.\n"); |
| 350 | return(-EINVAL); |
| 351 | } |
| 352 | for (i = 0; i < (count / 3); i++) { |
| 353 | devname = map[i * 3]; |
| 354 | |
| 355 | if (parse_cmdline(devname, map[i * 3 + 1], map[i * 3 + 2])!=0) { |
| 356 | return(-EINVAL); |
| 357 | } |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 358 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | } |
| 360 | #endif /* !MODULE */ |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 361 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | return(0); |
| 363 | } |
| 364 | |
| 365 | static void __exit cleanup_slram(void) |
| 366 | { |
| 367 | unregister_devices(); |
| 368 | } |
| 369 | |
| 370 | module_init(init_slram); |
| 371 | module_exit(cleanup_slram); |
| 372 | |
| 373 | MODULE_LICENSE("GPL"); |
| 374 | MODULE_AUTHOR("Jochen Schaeuble <psionic@psionic.de>"); |
| 375 | MODULE_DESCRIPTION("MTD driver for uncached system RAM"); |