1 // SPDX-License-Identifier: GPL-2.0
3 * Common code to handle map devices which are simple RAM
7 #include <linux/module.h>
8 #include <linux/types.h>
9 #include <linux/kernel.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>
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
);
29 static struct mtd_chip_driver mapram_chipdrv
= {
30 .probe
= map_ram_probe
,
35 static struct mtd_info
*map_ram_probe(struct map_info
*map
)
39 /* Check the first byte is RAM */
41 map_write8(map
, 0x55, 0);
42 if (map_read8(map
, 0) != 0x55)
45 map_write8(map
, 0xAA, 0);
46 if (map_read8(map
, 0) != 0xAA)
49 /* Check the last byte is RAM */
50 map_write8(map
, 0x55, map
->size
-1);
51 if (map_read8(map
, map
->size
-1) != 0x55)
54 map_write8(map
, 0xAA, map
->size
-1);
55 if (map_read8(map
, map
->size
-1) != 0xAA)
58 /* OK. It seems to be RAM. */
60 mtd
= kzalloc(sizeof(*mtd
), GFP_KERNEL
);
64 map
->fldrv
= &mapram_chipdrv
;
66 mtd
->name
= map
->name
;
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
->_sync
= mapram_nop
;
74 mtd
->flags
= MTD_CAP_RAM
;
77 /* Disable direct access when NO_XIP is set */
78 if (map
->phys
!= NO_XIP
) {
79 mtd
->_point
= mapram_point
;
80 mtd
->_unpoint
= mapram_unpoint
;
83 mtd
->erasesize
= PAGE_SIZE
;
84 while(mtd
->size
& (mtd
->erasesize
- 1))
87 __module_get(THIS_MODULE
);
91 static int mapram_point(struct mtd_info
*mtd
, loff_t from
, size_t len
,
92 size_t *retlen
, void **virt
, resource_size_t
*phys
)
94 struct map_info
*map
= mtd
->priv
;
98 *virt
= map
->virt
+ from
;
100 *phys
= map
->phys
+ from
;
105 static int mapram_unpoint(struct mtd_info
*mtd
, loff_t from
, size_t len
)
110 static int mapram_read (struct mtd_info
*mtd
, loff_t from
, size_t len
, size_t *retlen
, u_char
*buf
)
112 struct map_info
*map
= mtd
->priv
;
114 map_copy_from(map
, buf
, from
, len
);
119 static int mapram_write (struct mtd_info
*mtd
, loff_t to
, size_t len
, size_t *retlen
, const u_char
*buf
)
121 struct map_info
*map
= mtd
->priv
;
123 map_copy_to(map
, to
, buf
, len
);
128 static int mapram_erase (struct mtd_info
*mtd
, struct erase_info
*instr
)
130 /* Yeah, it's inefficient. Who cares? It's faster than a _real_
132 struct map_info
*map
= mtd
->priv
;
136 allff
= map_word_ff(map
);
137 for (i
=0; i
<instr
->len
; i
+= map_bankwidth(map
))
138 map_write(map
, allff
, instr
->addr
+ i
);
142 static void mapram_nop(struct mtd_info
*mtd
)
144 /* Nothing to see here */
147 static int __init
map_ram_init(void)
149 register_mtd_chip_driver(&mapram_chipdrv
);
153 static void __exit
map_ram_exit(void)
155 unregister_mtd_chip_driver(&mapram_chipdrv
);
158 module_init(map_ram_init
);
159 module_exit(map_ram_exit
);
161 MODULE_LICENSE("GPL");
162 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
163 MODULE_DESCRIPTION("MTD chip driver for RAM chips");