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