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