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>
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
);
23 static unsigned long mapram_unmapped_area(struct mtd_info
*, unsigned long,
24 unsigned long, unsigned long);
27 static struct mtd_chip_driver mapram_chipdrv
= {
28 .probe
= map_ram_probe
,
33 static struct mtd_info
*map_ram_probe(struct map_info
*map
)
37 /* Check the first byte is RAM */
39 map_write8(map
, 0x55, 0);
40 if (map_read8(map
, 0) != 0x55)
43 map_write8(map
, 0xAA, 0);
44 if (map_read8(map
, 0) != 0xAA)
47 /* Check the last byte is RAM */
48 map_write8(map
, 0x55, map
->size
-1);
49 if (map_read8(map
, map
->size
-1) != 0x55)
52 map_write8(map
, 0xAA, map
->size
-1);
53 if (map_read8(map
, map
->size
-1) != 0xAA)
56 /* OK. It seems to be RAM. */
58 mtd
= kzalloc(sizeof(*mtd
), GFP_KERNEL
);
62 map
->fldrv
= &mapram_chipdrv
;
64 mtd
->name
= map
->name
;
66 mtd
->size
= map
->size
;
67 mtd
->_erase
= mapram_erase
;
68 mtd
->_get_unmapped_area
= mapram_unmapped_area
;
69 mtd
->_read
= mapram_read
;
70 mtd
->_write
= mapram_write
;
71 mtd
->_panic_write
= mapram_write
;
72 mtd
->_sync
= mapram_nop
;
73 mtd
->flags
= MTD_CAP_RAM
;
76 mtd
->erasesize
= PAGE_SIZE
;
77 while(mtd
->size
& (mtd
->erasesize
- 1))
80 __module_get(THIS_MODULE
);
86 * Allow NOMMU mmap() to directly map the device (if not NULL)
87 * - return the address to which the offset maps
88 * - return -ENOSYS to indicate refusal to do the mapping
90 static unsigned long mapram_unmapped_area(struct mtd_info
*mtd
,
95 struct map_info
*map
= mtd
->priv
;
96 return (unsigned long) map
->virt
+ offset
;
99 static int mapram_read (struct mtd_info
*mtd
, loff_t from
, size_t len
, size_t *retlen
, u_char
*buf
)
101 struct map_info
*map
= mtd
->priv
;
103 map_copy_from(map
, buf
, from
, len
);
108 static int mapram_write (struct mtd_info
*mtd
, loff_t to
, size_t len
, size_t *retlen
, const u_char
*buf
)
110 struct map_info
*map
= mtd
->priv
;
112 map_copy_to(map
, to
, buf
, len
);
117 static int mapram_erase (struct mtd_info
*mtd
, struct erase_info
*instr
)
119 /* Yeah, it's inefficient. Who cares? It's faster than a _real_
121 struct map_info
*map
= mtd
->priv
;
125 allff
= map_word_ff(map
);
126 for (i
=0; i
<instr
->len
; i
+= map_bankwidth(map
))
127 map_write(map
, allff
, instr
->addr
+ i
);
128 instr
->state
= MTD_ERASE_DONE
;
129 mtd_erase_callback(instr
);
133 static void mapram_nop(struct mtd_info
*mtd
)
135 /* Nothing to see here */
138 static int __init
map_ram_init(void)
140 register_mtd_chip_driver(&mapram_chipdrv
);
144 static void __exit
map_ram_exit(void)
146 unregister_mtd_chip_driver(&mapram_chipdrv
);
149 module_init(map_ram_init
);
150 module_exit(map_ram_exit
);
152 MODULE_LICENSE("GPL");
153 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
154 MODULE_DESCRIPTION("MTD chip driver for RAM chips");