linux-old/drivers/mtd/devices/mtdram.c
<<
>>
Prefs
   1/*
   2 * mtdram - a test mtd device
   3 * $Id: mtdram.c,v 1.29 2002/10/21 13:40:06 jocke Exp $
   4 * Author: Alexander Larsson <alex@cendio.se>
   5 *
   6 * Copyright (c) 1999 Alexander Larsson <alex@cendio.se>
   7 *
   8 * This code is GPL
   9 *
  10 */
  11
  12#include <linux/config.h>
  13#include <linux/module.h>
  14#include <linux/slab.h>
  15#include <linux/ioport.h>
  16#include <linux/mtd/compatmac.h>
  17#include <linux/mtd/mtd.h>
  18
  19#ifndef CONFIG_MTDRAM_ABS_POS
  20  #define CONFIG_MTDRAM_ABS_POS 0
  21#endif
  22
  23#if CONFIG_MTDRAM_ABS_POS > 0
  24  #include <asm/io.h>
  25#endif
  26
  27#ifdef MODULE
  28static unsigned long total_size = CONFIG_MTDRAM_TOTAL_SIZE;
  29static unsigned long erase_size = CONFIG_MTDRAM_ERASE_SIZE;
  30MODULE_PARM(total_size,"l");
  31MODULE_PARM_DESC(total_size, "Total device size in KiB");
  32MODULE_PARM(erase_size,"l");
  33MODULE_PARM_DESC(erase_size, "Device erase block size in KiB");
  34#define MTDRAM_TOTAL_SIZE (total_size * 1024)
  35#define MTDRAM_ERASE_SIZE (erase_size * 1024)
  36#else
  37#define MTDRAM_TOTAL_SIZE (CONFIG_MTDRAM_TOTAL_SIZE * 1024)
  38#define MTDRAM_ERASE_SIZE (CONFIG_MTDRAM_ERASE_SIZE * 1024)
  39#endif
  40
  41
  42// We could store these in the mtd structure, but we only support 1 device..
  43static struct mtd_info *mtd_info;
  44
  45
  46static int
  47ram_erase(struct mtd_info *mtd, struct erase_info *instr)
  48{
  49  DEBUG(MTD_DEBUG_LEVEL2, "ram_erase(pos:%ld, len:%ld)\n", (long)instr->addr, (long)instr->len);
  50  if (instr->addr + instr->len > mtd->size) {
  51    DEBUG(MTD_DEBUG_LEVEL1, "ram_erase() out of bounds (%ld > %ld)\n", (long)(instr->addr + instr->len), (long)mtd->size);
  52    return -EINVAL;
  53  }
  54        
  55  memset((char *)mtd->priv + instr->addr, 0xff, instr->len);
  56        
  57  instr->state = MTD_ERASE_DONE;
  58
  59  if (instr->callback)
  60    (*(instr->callback))(instr);
  61  return 0;
  62}
  63
  64static int ram_point (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char **mtdbuf)
  65{
  66  if (from + len > mtd->size)
  67    return -EINVAL;
  68        
  69  *mtdbuf = mtd->priv + from;
  70  *retlen = len;
  71  return 0;
  72}
  73
  74static void ram_unpoint (struct mtd_info *mtd, u_char *addr, loff_t from,
  75                         size_t len)
  76{
  77  DEBUG(MTD_DEBUG_LEVEL2, "ram_unpoint\n");
  78}
  79
  80static int ram_read(struct mtd_info *mtd, loff_t from, size_t len,
  81             size_t *retlen, u_char *buf)
  82{
  83  DEBUG(MTD_DEBUG_LEVEL2, "ram_read(pos:%ld, len:%ld)\n", (long)from, (long)len);
  84  if (from + len > mtd->size) {
  85    DEBUG(MTD_DEBUG_LEVEL1, "ram_read() out of bounds (%ld > %ld)\n", (long)(from + len), (long)mtd->size);
  86    return -EINVAL;
  87  }
  88
  89  memcpy(buf, mtd->priv + from, len);
  90
  91  *retlen=len;
  92  return 0;
  93}
  94
  95static int ram_write(struct mtd_info *mtd, loff_t to, size_t len,
  96              size_t *retlen, const u_char *buf)
  97{
  98  DEBUG(MTD_DEBUG_LEVEL2, "ram_write(pos:%ld, len:%ld)\n", (long)to, (long)len);
  99  if (to + len > mtd->size) {
 100    DEBUG(MTD_DEBUG_LEVEL1, "ram_write() out of bounds (%ld > %ld)\n", (long)(to + len), (long)mtd->size);
 101    return -EINVAL;
 102  }
 103
 104  memcpy ((char *)mtd->priv + to, buf, len);
 105
 106  *retlen=len;
 107  return 0;
 108}
 109
 110static void __exit cleanup_mtdram(void)
 111{
 112  if (mtd_info) {
 113    del_mtd_device(mtd_info);
 114#if CONFIG_MTDRAM_TOTAL_SIZE > 0
 115    if (mtd_info->priv)
 116#if CONFIG_MTDRAM_ABS_POS > 0
 117      iounmap(mtd_info->priv);
 118#else
 119      vfree(mtd_info->priv);
 120#endif  
 121#endif
 122    kfree(mtd_info);
 123  }
 124}
 125
 126int mtdram_init_device(struct mtd_info *mtd, void *mapped_address, 
 127                       unsigned long size, char *name)
 128{
 129   memset(mtd, 0, sizeof(*mtd));
 130
 131   /* Setup the MTD structure */
 132   mtd->name = name;
 133   mtd->type = MTD_RAM;
 134   mtd->flags = MTD_CAP_RAM;
 135   mtd->size = size;
 136   mtd->erasesize = MTDRAM_ERASE_SIZE;
 137   mtd->priv = mapped_address;
 138
 139   mtd->module = THIS_MODULE;
 140   mtd->erase = ram_erase;
 141   mtd->point = ram_point;
 142   mtd->unpoint = ram_unpoint;
 143   mtd->read = ram_read;
 144   mtd->write = ram_write;
 145
 146   if (add_mtd_device(mtd)) {
 147     return -EIO;
 148   }
 149   
 150   return 0;
 151}
 152
 153#if CONFIG_MTDRAM_TOTAL_SIZE > 0
 154#if CONFIG_MTDRAM_ABS_POS > 0
 155int __init init_mtdram(void)
 156{
 157  void *addr;
 158  int err;
 159  /* Allocate some memory */
 160   mtd_info = (struct mtd_info *)kmalloc(sizeof(struct mtd_info), GFP_KERNEL);
 161   if (!mtd_info)
 162     return -ENOMEM;
 163   
 164  addr = ioremap(CONFIG_MTDRAM_ABS_POS, MTDRAM_TOTAL_SIZE);
 165  if (!addr) {
 166    DEBUG(MTD_DEBUG_LEVEL1, 
 167          "Failed to ioremap) memory region of size %ld at ABS_POS:%ld\n", 
 168          (long)MTDRAM_TOTAL_SIZE, (long)CONFIG_MTDRAM_ABS_POS);
 169    kfree(mtd_info);
 170    mtd_info = NULL;
 171    return -ENOMEM;
 172  }
 173  err = mtdram_init_device(mtd_info, addr, 
 174                           MTDRAM_TOTAL_SIZE, "mtdram test device");
 175  if (err) 
 176  {
 177    iounmap(addr);
 178    kfree(mtd_info);
 179    mtd_info = NULL;
 180    return err;
 181  }
 182  memset(mtd_info->priv, 0xff, MTDRAM_TOTAL_SIZE);
 183  return err;
 184}
 185
 186#else /* CONFIG_MTDRAM_ABS_POS > 0 */
 187
 188int __init init_mtdram(void)
 189{
 190  void *addr;
 191  int err;
 192  /* Allocate some memory */
 193   mtd_info = (struct mtd_info *)kmalloc(sizeof(struct mtd_info), GFP_KERNEL);
 194   if (!mtd_info)
 195     return -ENOMEM;
 196
 197  addr = vmalloc(MTDRAM_TOTAL_SIZE);
 198  if (!addr) {
 199    DEBUG(MTD_DEBUG_LEVEL1, 
 200          "Failed to vmalloc memory region of size %ld\n", 
 201          (long)MTDRAM_TOTAL_SIZE);
 202    kfree(mtd_info);
 203    mtd_info = NULL;
 204    return -ENOMEM;
 205  }
 206  err = mtdram_init_device(mtd_info, addr, 
 207                           MTDRAM_TOTAL_SIZE, "mtdram test device");
 208  if (err) 
 209  {
 210    vfree(addr);
 211    kfree(mtd_info);
 212    mtd_info = NULL;
 213    return err;
 214  }
 215  memset(mtd_info->priv, 0xff, MTDRAM_TOTAL_SIZE);
 216  return err;
 217}
 218#endif /* !(CONFIG_MTDRAM_ABS_POS > 0) */
 219
 220#else /* CONFIG_MTDRAM_TOTAL_SIZE > 0 */
 221
 222int __init init_mtdram(void)
 223{
 224  return 0;
 225}
 226#endif /* !(CONFIG_MTDRAM_TOTAL_SIZE > 0) */
 227
 228module_init(init_mtdram);
 229module_exit(cleanup_mtdram);
 230
 231MODULE_LICENSE("GPL");
 232MODULE_AUTHOR("Alexander Larsson <alexl@redhat.com>");
 233MODULE_DESCRIPTION("Simulated MTD driver for testing");
 234
 235