2 * Common code to handle map devices which are simple RAM
3 * (C) 2000 Red Hat. GPL'd.
6 #include <linux/module.h>
7 #include <linux/types.h>
8 #include <linux/kernel.h>
10 #include <asm/byteorder.h>
11 #include <linux/errno.h>
12 #include <linux/slab.h>
13 #include <linux/init.h>
14 #include <linux/mtd/mtd.h>
15 #include <linux/mtd/map.h>
16 #include <linux/mtd/compatmac.h>
19 static int mapram_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
20 static int mapram_write (struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
21 static int mapram_erase (struct mtd_info *, struct erase_info *);
22 static void mapram_nop (struct mtd_info *);
23 static struct mtd_info *map_ram_probe(struct map_info *map);
26 static struct mtd_chip_driver mapram_chipdrv = {
27 .probe = map_ram_probe,
32 static struct mtd_info *map_ram_probe(struct map_info *map)
36 /* Check the first byte is RAM */
38 map_write8(map, 0x55, 0);
39 if (map_read8(map, 0) != 0x55)
42 map_write8(map, 0xAA, 0);
43 if (map_read8(map, 0) != 0xAA)
46 /* Check the last byte is RAM */
47 map_write8(map, 0x55, map->size-1);
48 if (map_read8(map, map->size-1) != 0x55)
51 map_write8(map, 0xAA, map->size-1);
52 if (map_read8(map, map->size-1) != 0xAA)
55 /* OK. It seems to be RAM. */
57 mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
61 map->fldrv = &mapram_chipdrv;
63 mtd->name = map->name;
65 mtd->size = map->size;
66 mtd->erase = mapram_erase;
67 mtd->read = mapram_read;
68 mtd->write = mapram_write;
69 mtd->sync = mapram_nop;
70 mtd->flags = MTD_CAP_RAM;
73 mtd->erasesize = PAGE_SIZE;
74 while(mtd->size & (mtd->erasesize - 1))
77 __module_get(THIS_MODULE);
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 = 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 = 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 = mtd->priv;
108 allff = map_word_ff(map);
110 for (i=0; i<instr->len; i += map_bankwidth(map))
111 map_write(map, allff, instr->addr + i);
113 instr->state = MTD_ERASE_DONE;
115 mtd_erase_callback(instr);
120 static void mapram_nop(struct mtd_info *mtd)
122 /* Nothing to see here */
125 static int __init map_ram_init(void)
127 register_mtd_chip_driver(&mapram_chipdrv);
131 static void __exit map_ram_exit(void)
133 unregister_mtd_chip_driver(&mapram_chipdrv);
136 module_init(map_ram_init);
137 module_exit(map_ram_exit);
139 MODULE_LICENSE("GPL");
140 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
141 MODULE_DESCRIPTION("MTD chip driver for RAM chips");