Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Common code to handle map devices which are simple RAM
0003  * (C) 2000 Red Hat. GPL'd.
0004  */
0005 
0006 #include <linux/module.h>
0007 #include <linux/types.h>
0008 #include <linux/kernel.h>
0009 #include <asm/io.h>
0010 #include <asm/byteorder.h>
0011 #include <linux/errno.h>
0012 #include <linux/slab.h>
0013 #include <linux/init.h>
0014 #include <linux/mtd/mtd.h>
0015 #include <linux/mtd/map.h>
0016 
0017 
0018 static int mapram_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
0019 static int mapram_write (struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
0020 static int mapram_erase (struct mtd_info *, struct erase_info *);
0021 static void mapram_nop (struct mtd_info *);
0022 static struct mtd_info *map_ram_probe(struct map_info *map);
0023 static int mapram_point (struct mtd_info *mtd, loff_t from, size_t len,
0024              size_t *retlen, void **virt, resource_size_t *phys);
0025 static int mapram_unpoint(struct mtd_info *mtd, loff_t from, size_t len);
0026 
0027 
0028 static struct mtd_chip_driver mapram_chipdrv = {
0029     .probe  = map_ram_probe,
0030     .name   = "map_ram",
0031     .module = THIS_MODULE
0032 };
0033 
0034 static struct mtd_info *map_ram_probe(struct map_info *map)
0035 {
0036     struct mtd_info *mtd;
0037 
0038     /* Check the first byte is RAM */
0039 #if 0
0040     map_write8(map, 0x55, 0);
0041     if (map_read8(map, 0) != 0x55)
0042         return NULL;
0043 
0044     map_write8(map, 0xAA, 0);
0045     if (map_read8(map, 0) != 0xAA)
0046         return NULL;
0047 
0048     /* Check the last byte is RAM */
0049     map_write8(map, 0x55, map->size-1);
0050     if (map_read8(map, map->size-1) != 0x55)
0051         return NULL;
0052 
0053     map_write8(map, 0xAA, map->size-1);
0054     if (map_read8(map, map->size-1) != 0xAA)
0055         return NULL;
0056 #endif
0057     /* OK. It seems to be RAM. */
0058 
0059     mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
0060     if (!mtd)
0061         return NULL;
0062 
0063     map->fldrv = &mapram_chipdrv;
0064     mtd->priv = map;
0065     mtd->name = map->name;
0066     mtd->type = MTD_RAM;
0067     mtd->size = map->size;
0068     mtd->_erase = mapram_erase;
0069     mtd->_read = mapram_read;
0070     mtd->_write = mapram_write;
0071     mtd->_panic_write = mapram_write;
0072     mtd->_point = mapram_point;
0073     mtd->_sync = mapram_nop;
0074     mtd->_unpoint = mapram_unpoint;
0075     mtd->flags = MTD_CAP_RAM;
0076     mtd->writesize = 1;
0077 
0078     mtd->erasesize = PAGE_SIZE;
0079     while(mtd->size & (mtd->erasesize - 1))
0080         mtd->erasesize >>= 1;
0081 
0082     __module_get(THIS_MODULE);
0083     return mtd;
0084 }
0085 
0086 static int mapram_point(struct mtd_info *mtd, loff_t from, size_t len,
0087             size_t *retlen, void **virt, resource_size_t *phys)
0088 {
0089     struct map_info *map = mtd->priv;
0090 
0091     if (!map->virt)
0092         return -EINVAL;
0093     *virt = map->virt + from;
0094     if (phys)
0095         *phys = map->phys + from;
0096     *retlen = len;
0097     return 0;
0098 }
0099 
0100 static int mapram_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
0101 {
0102     return 0;
0103 }
0104 
0105 static int mapram_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
0106 {
0107     struct map_info *map = mtd->priv;
0108 
0109     map_copy_from(map, buf, from, len);
0110     *retlen = len;
0111     return 0;
0112 }
0113 
0114 static int mapram_write (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf)
0115 {
0116     struct map_info *map = mtd->priv;
0117 
0118     map_copy_to(map, to, buf, len);
0119     *retlen = len;
0120     return 0;
0121 }
0122 
0123 static int mapram_erase (struct mtd_info *mtd, struct erase_info *instr)
0124 {
0125     /* Yeah, it's inefficient. Who cares? It's faster than a _real_
0126        flash erase. */
0127     struct map_info *map = mtd->priv;
0128     map_word allff;
0129     unsigned long i;
0130 
0131     allff = map_word_ff(map);
0132     for (i=0; i<instr->len; i += map_bankwidth(map))
0133         map_write(map, allff, instr->addr + i);
0134     return 0;
0135 }
0136 
0137 static void mapram_nop(struct mtd_info *mtd)
0138 {
0139     /* Nothing to see here */
0140 }
0141 
0142 static int __init map_ram_init(void)
0143 {
0144     register_mtd_chip_driver(&mapram_chipdrv);
0145     return 0;
0146 }
0147 
0148 static void __exit map_ram_exit(void)
0149 {
0150     unregister_mtd_chip_driver(&mapram_chipdrv);
0151 }
0152 
0153 module_init(map_ram_init);
0154 module_exit(map_ram_exit);
0155 
0156 MODULE_LICENSE("GPL");
0157 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
0158 MODULE_DESCRIPTION("MTD chip driver for RAM chips");