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