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