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