1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* drivers/mtd/maps/plat-ram.c
4 * (c) 2004-2005 Simtec Electronics
5 * http://www.simtec.co.uk/products/SWLINUX/
6 * Ben Dooks <ben@simtec.co.uk>
8 * Generic platform device based RAM map
11 #include <linux/module.h>
12 #include <linux/types.h>
13 #include <linux/kernel.h>
14 #include <linux/string.h>
15 #include <linux/ioport.h>
16 #include <linux/device.h>
17 #include <linux/slab.h>
18 #include <linux/platform_device.h>
20 #include <linux/mtd/mtd.h>
21 #include <linux/mtd/map.h>
22 #include <linux/mtd/partitions.h>
23 #include <linux/mtd/plat-ram.h>
27 /* private structure for each mtd platform ram device created */
33 struct platdata_mtd_ram
*pdata
;
38 * device private data to struct platram_info conversion
41 static inline struct platram_info
*to_platram_info(struct platform_device
*dev
)
43 return platform_get_drvdata(dev
);
48 * call the platform device's set rw/ro control
54 static inline void platram_setrw(struct platram_info
*info
, int to
)
56 if (info
->pdata
== NULL
)
59 if (info
->pdata
->set_rw
!= NULL
)
60 (info
->pdata
->set_rw
)(info
->dev
, to
);
65 * called to remove the device from the driver's control
68 static void platram_remove(struct platform_device
*pdev
)
70 struct platram_info
*info
= to_platram_info(pdev
);
72 dev_dbg(&pdev
->dev
, "removing device\n");
78 mtd_device_unregister(info
->mtd
);
79 map_destroy(info
->mtd
);
82 /* ensure ram is left read-only */
84 platram_setrw(info
, PLATRAM_RO
);
91 * called from device drive system when a device matching our
95 static int platram_probe(struct platform_device
*pdev
)
97 struct platdata_mtd_ram
*pdata
;
98 struct platram_info
*info
;
102 dev_dbg(&pdev
->dev
, "probe entered\n");
104 if (dev_get_platdata(&pdev
->dev
) == NULL
) {
105 dev_err(&pdev
->dev
, "no platform data supplied\n");
110 pdata
= dev_get_platdata(&pdev
->dev
);
112 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
118 platform_set_drvdata(pdev
, info
);
120 info
->dev
= &pdev
->dev
;
123 /* get the resource for the memory mapping */
124 info
->map
.virt
= devm_platform_get_and_ioremap_resource(pdev
, 0, &res
);
125 if (IS_ERR(info
->map
.virt
)) {
126 err
= PTR_ERR(info
->map
.virt
);
130 dev_dbg(&pdev
->dev
, "got platform resource %p (0x%llx)\n", res
,
131 (unsigned long long)res
->start
);
133 /* setup map parameters */
135 info
->map
.phys
= res
->start
;
136 info
->map
.size
= resource_size(res
);
137 info
->map
.name
= pdata
->mapname
!= NULL
?
138 (char *)pdata
->mapname
: (char *)pdev
->name
;
139 info
->map
.bankwidth
= pdata
->bankwidth
;
141 dev_dbg(&pdev
->dev
, "virt %p, %lu bytes\n", info
->map
.virt
, info
->map
.size
);
143 simple_map_init(&info
->map
);
145 dev_dbg(&pdev
->dev
, "initialised map, probing for mtd\n");
147 /* probe for the right mtd map driver
148 * supplied by the platform_data struct */
150 if (pdata
->map_probes
) {
151 const char * const *map_probes
= pdata
->map_probes
;
153 for ( ; !info
->mtd
&& *map_probes
; map_probes
++)
154 info
->mtd
= do_map_probe(*map_probes
, &info
->map
);
156 /* fallback to map_ram */
158 info
->mtd
= do_map_probe("map_ram", &info
->map
);
160 if (info
->mtd
== NULL
) {
161 dev_err(&pdev
->dev
, "failed to probe for map_ram\n");
166 info
->mtd
->dev
.parent
= &pdev
->dev
;
168 platram_setrw(info
, PLATRAM_RW
);
170 /* check to see if there are any available partitions, or whether
171 * to add this device whole */
173 err
= mtd_device_parse_register(info
->mtd
, pdata
->probes
, NULL
,
175 pdata
->nr_partitions
);
177 dev_err(&pdev
->dev
, "failed to register mtd device\n");
181 dev_info(&pdev
->dev
, "registered mtd device\n");
183 if (pdata
->nr_partitions
) {
184 /* add the whole device. */
185 err
= mtd_device_register(info
->mtd
, NULL
, 0);
188 "failed to register the entire device\n");
196 platram_remove(pdev
);
201 /* device driver info */
203 /* work with hotplug and coldplug */
204 MODULE_ALIAS("platform:mtd-ram");
206 static struct platform_driver platram_driver
= {
207 .probe
= platram_probe
,
208 .remove
= platram_remove
,
214 module_platform_driver(platram_driver
);
216 MODULE_LICENSE("GPL");
217 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
218 MODULE_DESCRIPTION("MTD platform RAM map driver");