xref: /openbmc/linux/drivers/mtd/chips/map_ram.c (revision 402d3265)
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 static unsigned long mapram_unmapped_area(struct mtd_info *, unsigned long,
25 					  unsigned long, unsigned long);
26 
27 
28 static struct mtd_chip_driver mapram_chipdrv = {
29 	.probe	= map_ram_probe,
30 	.name	= "map_ram",
31 	.module	= THIS_MODULE
32 };
33 
34 static struct mtd_info *map_ram_probe(struct map_info *map)
35 {
36 	struct mtd_info *mtd;
37 
38 	/* Check the first byte is RAM */
39 #if 0
40 	map_write8(map, 0x55, 0);
41 	if (map_read8(map, 0) != 0x55)
42 		return NULL;
43 
44 	map_write8(map, 0xAA, 0);
45 	if (map_read8(map, 0) != 0xAA)
46 		return NULL;
47 
48 	/* Check the last byte is RAM */
49 	map_write8(map, 0x55, map->size-1);
50 	if (map_read8(map, map->size-1) != 0x55)
51 		return NULL;
52 
53 	map_write8(map, 0xAA, map->size-1);
54 	if (map_read8(map, map->size-1) != 0xAA)
55 		return NULL;
56 #endif
57 	/* OK. It seems to be RAM. */
58 
59 	mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
60 	if (!mtd)
61 		return NULL;
62 
63 	map->fldrv = &mapram_chipdrv;
64 	mtd->priv = map;
65 	mtd->name = map->name;
66 	mtd->type = MTD_RAM;
67 	mtd->size = map->size;
68 	mtd->erase = mapram_erase;
69 	mtd->get_unmapped_area = mapram_unmapped_area;
70 	mtd->read = mapram_read;
71 	mtd->write = mapram_write;
72 	mtd->sync = mapram_nop;
73 	mtd->flags = MTD_CAP_RAM;
74 	mtd->writesize = 1;
75 
76 	mtd->erasesize = PAGE_SIZE;
77  	while(mtd->size & (mtd->erasesize - 1))
78 		mtd->erasesize >>= 1;
79 
80 	__module_get(THIS_MODULE);
81 	return mtd;
82 }
83 
84 
85 /*
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
89  */
90 static unsigned long mapram_unmapped_area(struct mtd_info *mtd,
91 					  unsigned long len,
92 					  unsigned long offset,
93 					  unsigned long flags)
94 {
95 	struct map_info *map = mtd->priv;
96 	return (unsigned long) map->virt + offset;
97 }
98 
99 static int mapram_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
100 {
101 	struct map_info *map = mtd->priv;
102 
103 	map_copy_from(map, buf, from, len);
104 	*retlen = len;
105 	return 0;
106 }
107 
108 static int mapram_write (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf)
109 {
110 	struct map_info *map = mtd->priv;
111 
112 	map_copy_to(map, to, buf, len);
113 	*retlen = len;
114 	return 0;
115 }
116 
117 static int mapram_erase (struct mtd_info *mtd, struct erase_info *instr)
118 {
119 	/* Yeah, it's inefficient. Who cares? It's faster than a _real_
120 	   flash erase. */
121 	struct map_info *map = mtd->priv;
122 	map_word allff;
123 	unsigned long i;
124 
125 	allff = map_word_ff(map);
126 
127 	for (i=0; i<instr->len; i += map_bankwidth(map))
128 		map_write(map, allff, instr->addr + i);
129 
130 	instr->state = MTD_ERASE_DONE;
131 
132 	mtd_erase_callback(instr);
133 
134 	return 0;
135 }
136 
137 static void mapram_nop(struct mtd_info *mtd)
138 {
139 	/* Nothing to see here */
140 }
141 
142 static int __init map_ram_init(void)
143 {
144 	register_mtd_chip_driver(&mapram_chipdrv);
145 	return 0;
146 }
147 
148 static void __exit map_ram_exit(void)
149 {
150 	unregister_mtd_chip_driver(&mapram_chipdrv);
151 }
152 
153 module_init(map_ram_init);
154 module_exit(map_ram_exit);
155 
156 MODULE_LICENSE("GPL");
157 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
158 MODULE_DESCRIPTION("MTD chip driver for RAM chips");
159