at v2.6.25 142 lines 3.2 kB view raw
1/* 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.22 2005/01/05 18:05:12 dwmw2 Exp $ 5 */ 6 7#include <linux/module.h> 8#include <linux/types.h> 9#include <linux/kernel.h> 10#include <asm/io.h> 11#include <asm/byteorder.h> 12#include <linux/errno.h> 13#include <linux/slab.h> 14#include <linux/init.h> 15#include <linux/mtd/mtd.h> 16#include <linux/mtd/map.h> 17#include <linux/mtd/compatmac.h> 18 19 20static int mapram_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *); 21static int mapram_write (struct mtd_info *, loff_t, size_t, size_t *, const u_char *); 22static int mapram_erase (struct mtd_info *, struct erase_info *); 23static void mapram_nop (struct mtd_info *); 24static struct mtd_info *map_ram_probe(struct map_info *map); 25 26 27static struct mtd_chip_driver mapram_chipdrv = { 28 .probe = map_ram_probe, 29 .name = "map_ram", 30 .module = THIS_MODULE 31}; 32 33static struct mtd_info *map_ram_probe(struct map_info *map) 34{ 35 struct mtd_info *mtd; 36 37 /* Check the first byte is RAM */ 38#if 0 39 map_write8(map, 0x55, 0); 40 if (map_read8(map, 0) != 0x55) 41 return NULL; 42 43 map_write8(map, 0xAA, 0); 44 if (map_read8(map, 0) != 0xAA) 45 return NULL; 46 47 /* Check the last byte is RAM */ 48 map_write8(map, 0x55, map->size-1); 49 if (map_read8(map, map->size-1) != 0x55) 50 return NULL; 51 52 map_write8(map, 0xAA, map->size-1); 53 if (map_read8(map, map->size-1) != 0xAA) 54 return NULL; 55#endif 56 /* OK. It seems to be RAM. */ 57 58 mtd = kzalloc(sizeof(*mtd), GFP_KERNEL); 59 if (!mtd) 60 return NULL; 61 62 map->fldrv = &mapram_chipdrv; 63 mtd->priv = map; 64 mtd->name = map->name; 65 mtd->type = MTD_RAM; 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; 72 mtd->writesize = 1; 73 74 mtd->erasesize = PAGE_SIZE; 75 while(mtd->size & (mtd->erasesize - 1)) 76 mtd->erasesize >>= 1; 77 78 __module_get(THIS_MODULE); 79 return mtd; 80} 81 82 83static int mapram_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf) 84{ 85 struct map_info *map = mtd->priv; 86 87 map_copy_from(map, buf, from, len); 88 *retlen = len; 89 return 0; 90} 91 92static int mapram_write (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf) 93{ 94 struct map_info *map = mtd->priv; 95 96 map_copy_to(map, to, buf, len); 97 *retlen = len; 98 return 0; 99} 100 101static int mapram_erase (struct mtd_info *mtd, struct erase_info *instr) 102{ 103 /* Yeah, it's inefficient. Who cares? It's faster than a _real_ 104 flash erase. */ 105 struct map_info *map = mtd->priv; 106 map_word allff; 107 unsigned long i; 108 109 allff = map_word_ff(map); 110 111 for (i=0; i<instr->len; i += map_bankwidth(map)) 112 map_write(map, allff, instr->addr + i); 113 114 instr->state = MTD_ERASE_DONE; 115 116 mtd_erase_callback(instr); 117 118 return 0; 119} 120 121static void mapram_nop(struct mtd_info *mtd) 122{ 123 /* Nothing to see here */ 124} 125 126static int __init map_ram_init(void) 127{ 128 register_mtd_chip_driver(&mapram_chipdrv); 129 return 0; 130} 131 132static void __exit map_ram_exit(void) 133{ 134 unregister_mtd_chip_driver(&mapram_chipdrv); 135} 136 137module_init(map_ram_init); 138module_exit(map_ram_exit); 139 140MODULE_LICENSE("GPL"); 141MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); 142MODULE_DESCRIPTION("MTD chip driver for RAM chips");