2 * Common code to handle map devices which are simple RAM
3 * (C) 2000 Red Hat. GPL'd.
4 * $Id: map_ram.c,v 1.1.1.1 2005/04/11 02:50:25 jack Exp $
7 #include <linux/module.h>
8 #include <linux/types.h>
9 #include <linux/kernel.h>
11 #include <asm/byteorder.h>
12 #include <linux/errno.h>
13 #include <linux/slab.h>
15 #include <linux/mtd/map.h>
18 static int mapram_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
19 static int mapram_write (struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
20 static int mapram_erase (struct mtd_info *, struct erase_info *);
21 static void mapram_nop (struct mtd_info *);
22 static struct mtd_info *map_ram_probe(struct map_info *map);
25 static struct mtd_chip_driver mapram_chipdrv = {
31 static struct mtd_info *map_ram_probe(struct map_info *map)
35 /* Check the first byte is RAM */
37 map->write8(map, 0x55, 0);
38 if (map->read8(map, 0) != 0x55)
41 map->write8(map, 0xAA, 0);
42 if (map->read8(map, 0) != 0xAA)
45 /* Check the last byte is RAM */
46 map->write8(map, 0x55, map->size-1);
47 if (map->read8(map, map->size-1) != 0x55)
50 map->write8(map, 0xAA, map->size-1);
51 if (map->read8(map, map->size-1) != 0xAA)
54 /* OK. It seems to be RAM. */
56 mtd = kmalloc(sizeof(*mtd), GFP_KERNEL);
60 memset(mtd, 0, sizeof(*mtd));
62 map->fldrv = &mapram_chipdrv;
64 mtd->name = map->name;
66 mtd->size = map->size;
67 mtd->erase = mapram_erase;
68 mtd->read = mapram_read;
69 mtd->write = mapram_write;
70 mtd->sync = mapram_nop;
71 mtd->flags = MTD_CAP_RAM | MTD_VOLATILE;
73 mtd->erasesize = PAGE_SIZE;
74 while(mtd->size & (mtd->erasesize - 1))
82 static int mapram_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
84 struct map_info *map = (struct map_info *)mtd->priv;
86 map->copy_from(map, buf, from, len);
91 static int mapram_write (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf)
93 struct map_info *map = (struct map_info *)mtd->priv;
95 map->copy_to(map, to, buf, len);
100 static int mapram_erase (struct mtd_info *mtd, struct erase_info *instr)
102 /* Yeah, it's inefficient. Who cares? It's faster than a _real_
104 struct map_info *map = (struct map_info *)mtd->priv;
107 for (i=0; i<instr->len; i++)
108 map->write8(map, 0xFF, instr->addr + i);
111 instr->callback(instr);
116 static void mapram_nop(struct mtd_info *mtd)
118 /* Nothing to see here */
121 int __init map_ram_init(void)
123 register_mtd_chip_driver(&mapram_chipdrv);
127 static void __exit map_ram_exit(void)
129 unregister_mtd_chip_driver(&mapram_chipdrv);
132 module_init(map_ram_init);
133 module_exit(map_ram_exit);
135 MODULE_LICENSE("GPL");
136 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
137 MODULE_DESCRIPTION("MTD chip driver for RAM chips");