xref: /linux-6.15/drivers/mtd/chips/map_rom.c (revision 55100cfa)
1 /*
2  * Common code to handle map devices which are simple ROM
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/of.h>
15 #include <linux/mtd/mtd.h>
16 #include <linux/mtd/map.h>
17 
18 static int maprom_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
19 static int maprom_write (struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
20 static void maprom_nop (struct mtd_info *);
21 static struct mtd_info *map_rom_probe(struct map_info *map);
22 static int maprom_erase (struct mtd_info *mtd, struct erase_info *info);
23 static unsigned long maprom_unmapped_area(struct mtd_info *, unsigned long,
24 					  unsigned long, unsigned long);
25 static int maprom_point (struct mtd_info *mtd, loff_t from, size_t len,
26 			 size_t *retlen, void **virt, resource_size_t *phys);
27 static int maprom_unpoint(struct mtd_info *mtd, loff_t from, size_t len);
28 
29 
30 static struct mtd_chip_driver maprom_chipdrv = {
31 	.probe	= map_rom_probe,
32 	.name	= "map_rom",
33 	.module	= THIS_MODULE
34 };
35 
36 static unsigned int default_erasesize(struct map_info *map)
37 {
38 	const __be32 *erase_size = NULL;
39 
40 	erase_size = of_get_property(map->device_node, "erase-size", NULL);
41 
42 	return !erase_size ? map->size : be32_to_cpu(*erase_size);
43 }
44 
45 static struct mtd_info *map_rom_probe(struct map_info *map)
46 {
47 	struct mtd_info *mtd;
48 
49 	mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
50 	if (!mtd)
51 		return NULL;
52 
53 	map->fldrv = &maprom_chipdrv;
54 	mtd->priv = map;
55 	mtd->name = map->name;
56 	mtd->type = MTD_ROM;
57 	mtd->size = map->size;
58 	mtd->_get_unmapped_area = maprom_unmapped_area;
59 	mtd->_point = maprom_point;
60 	mtd->_unpoint = maprom_unpoint;
61 	mtd->_read = maprom_read;
62 	mtd->_write = maprom_write;
63 	mtd->_sync = maprom_nop;
64 	mtd->_erase = maprom_erase;
65 	mtd->flags = MTD_CAP_ROM;
66 	mtd->erasesize = default_erasesize(map);
67 	mtd->writesize = 1;
68 	mtd->writebufsize = 1;
69 
70 	__module_get(THIS_MODULE);
71 	return mtd;
72 }
73 
74 
75 /*
76  * Allow NOMMU mmap() to directly map the device (if not NULL)
77  * - return the address to which the offset maps
78  * - return -ENOSYS to indicate refusal to do the mapping
79  */
80 static unsigned long maprom_unmapped_area(struct mtd_info *mtd,
81 					  unsigned long len,
82 					  unsigned long offset,
83 					  unsigned long flags)
84 {
85 	struct map_info *map = mtd->priv;
86 	return (unsigned long) map->virt + offset;
87 }
88 
89 static int maprom_point(struct mtd_info *mtd, loff_t from, size_t len,
90 			size_t *retlen, void **virt, resource_size_t *phys)
91 {
92 	struct map_info *map = mtd->priv;
93 
94 	if (!map->virt)
95 		return -EINVAL;
96 	*virt = map->virt + from;
97 	if (phys)
98 		*phys = map->phys + from;
99 	*retlen = len;
100 	return 0;
101 }
102 
103 static int maprom_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
104 {
105 	return 0;
106 }
107 
108 static int maprom_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
109 {
110 	struct map_info *map = mtd->priv;
111 
112 	map_copy_from(map, buf, from, len);
113 	*retlen = len;
114 	return 0;
115 }
116 
117 static void maprom_nop(struct mtd_info *mtd)
118 {
119 	/* Nothing to see here */
120 }
121 
122 static int maprom_write (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf)
123 {
124 	return -EROFS;
125 }
126 
127 static int maprom_erase (struct mtd_info *mtd, struct erase_info *info)
128 {
129 	/* We do our best 8) */
130 	return -EROFS;
131 }
132 
133 static int __init map_rom_init(void)
134 {
135 	register_mtd_chip_driver(&maprom_chipdrv);
136 	return 0;
137 }
138 
139 static void __exit map_rom_exit(void)
140 {
141 	unregister_mtd_chip_driver(&maprom_chipdrv);
142 }
143 
144 module_init(map_rom_init);
145 module_exit(map_rom_exit);
146 
147 MODULE_LICENSE("GPL");
148 MODULE_AUTHOR("David Woodhouse <[email protected]>");
149 MODULE_DESCRIPTION("MTD chip driver for ROM chips");
150