Merge tag 'pci-v6.16-fixes-3' of git://git.kernel.org/pub/scm/linux/kernel/git/pci/pci
[linux-2.6-block.git] / drivers / mtd / maps / plat-ram.c
CommitLineData
1a59d1b8 1// SPDX-License-Identifier: GPL-2.0-or-later
99f2a8ae
BD
2/* drivers/mtd/maps/plat-ram.c
3 *
4 * (c) 2004-2005 Simtec Electronics
5 * http://www.simtec.co.uk/products/SWLINUX/
6 * Ben Dooks <ben@simtec.co.uk>
7 *
947af294 8 * Generic platform device based RAM map
99f2a8ae
BD
9*/
10
99f2a8ae
BD
11#include <linux/module.h>
12#include <linux/types.h>
99f2a8ae
BD
13#include <linux/kernel.h>
14#include <linux/string.h>
15#include <linux/ioport.h>
16#include <linux/device.h>
4e57b681 17#include <linux/slab.h>
d052d1be 18#include <linux/platform_device.h>
99f2a8ae
BD
19
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>
24
25#include <asm/io.h>
26
27/* private structure for each mtd platform ram device created */
28
29struct platram_info {
30 struct device *dev;
31 struct mtd_info *mtd;
32 struct map_info map;
99f2a8ae
BD
33 struct platdata_mtd_ram *pdata;
34};
35
36/* to_platram_info()
37 *
38 * device private data to struct platram_info conversion
39*/
40
3ae5eaec 41static inline struct platram_info *to_platram_info(struct platform_device *dev)
99f2a8ae 42{
14ac0785 43 return platform_get_drvdata(dev);
99f2a8ae
BD
44}
45
46/* platram_setrw
47 *
48 * call the platform device's set rw/ro control
49 *
50 * to = 0 => read-only
51 * = 1 => read-write
52*/
53
54static inline void platram_setrw(struct platram_info *info, int to)
55{
56 if (info->pdata == NULL)
57 return;
58
59 if (info->pdata->set_rw != NULL)
60 (info->pdata->set_rw)(info->dev, to);
61}
62
63/* platram_remove
64 *
65 * called to remove the device from the driver's control
66*/
67
677edf77 68static void platram_remove(struct platform_device *pdev)
99f2a8ae 69{
3ae5eaec 70 struct platram_info *info = to_platram_info(pdev);
99f2a8ae 71
3ae5eaec 72 dev_dbg(&pdev->dev, "removing device\n");
99f2a8ae 73
69f34c98 74 if (info == NULL)
677edf77 75 return;
99f2a8ae
BD
76
77 if (info->mtd) {
095dd500 78 mtd_device_unregister(info->mtd);
99f2a8ae
BD
79 map_destroy(info->mtd);
80 }
81
82 /* ensure ram is left read-only */
83
84 platram_setrw(info, PLATRAM_RO);
85
99f2a8ae 86 kfree(info);
99f2a8ae
BD
87}
88
89/* platram_probe
90 *
91 * called from device drive system when a device matching our
92 * driver is found.
93*/
94
3ae5eaec 95static int platram_probe(struct platform_device *pdev)
99f2a8ae 96{
99f2a8ae
BD
97 struct platdata_mtd_ram *pdata;
98 struct platram_info *info;
99 struct resource *res;
100 int err = 0;
101
3ae5eaec 102 dev_dbg(&pdev->dev, "probe entered\n");
69f34c98 103
d20d5a57 104 if (dev_get_platdata(&pdev->dev) == NULL) {
3ae5eaec 105 dev_err(&pdev->dev, "no platform data supplied\n");
99f2a8ae
BD
106 err = -ENOENT;
107 goto exit_error;
108 }
109
d20d5a57 110 pdata = dev_get_platdata(&pdev->dev);
99f2a8ae 111
95b93a0c 112 info = kzalloc(sizeof(*info), GFP_KERNEL);
99f2a8ae 113 if (info == NULL) {
99f2a8ae
BD
114 err = -ENOMEM;
115 goto exit_error;
116 }
117
3ae5eaec 118 platform_set_drvdata(pdev, info);
99f2a8ae 119
3ae5eaec 120 info->dev = &pdev->dev;
99f2a8ae
BD
121 info->pdata = pdata;
122
123 /* get the resource for the memory mapping */
0e0d59f2 124 info->map.virt = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
2e442aeb
AV
125 if (IS_ERR(info->map.virt)) {
126 err = PTR_ERR(info->map.virt);
99f2a8ae
BD
127 goto exit_free;
128 }
129
06d63cc5
RD
130 dev_dbg(&pdev->dev, "got platform resource %p (0x%llx)\n", res,
131 (unsigned long long)res->start);
99f2a8ae
BD
132
133 /* setup map parameters */
134
135 info->map.phys = res->start;
76d6a479 136 info->map.size = resource_size(res);
75757006
FF
137 info->map.name = pdata->mapname != NULL ?
138 (char *)pdata->mapname : (char *)pdev->name;
99f2a8ae
BD
139 info->map.bankwidth = pdata->bankwidth;
140
3ae5eaec 141 dev_dbg(&pdev->dev, "virt %p, %lu bytes\n", info->map.virt, info->map.size);
99f2a8ae 142
99f2a8ae
BD
143 simple_map_init(&info->map);
144
3ae5eaec 145 dev_dbg(&pdev->dev, "initialised map, probing for mtd\n");
99f2a8ae 146
75757006
FF
147 /* probe for the right mtd map driver
148 * supplied by the platform_data struct */
149
a01e035e 150 if (pdata->map_probes) {
d50dcb1d 151 const char * const *map_probes = pdata->map_probes;
75757006
FF
152
153 for ( ; !info->mtd && *map_probes; map_probes++)
154 info->mtd = do_map_probe(*map_probes , &info->map);
155 }
156 /* fallback to map_ram */
157 else
158 info->mtd = do_map_probe("map_ram", &info->map);
99f2a8ae 159
99f2a8ae 160 if (info->mtd == NULL) {
3ae5eaec 161 dev_err(&pdev->dev, "failed to probe for map_ram\n");
99f2a8ae
BD
162 err = -ENOMEM;
163 goto exit_free;
164 }
165
87f39f04 166 info->mtd->dev.parent = &pdev->dev;
99f2a8ae
BD
167
168 platram_setrw(info, PLATRAM_RW);
169
48fc7f7e 170 /* check to see if there are any available partitions, or whether
99f2a8ae
BD
171 * to add this device whole */
172
42d7fbe2
AB
173 err = mtd_device_parse_register(info->mtd, pdata->probes, NULL,
174 pdata->partitions,
175 pdata->nr_partitions);
8c293f54
BE
176 if (err) {
177 dev_err(&pdev->dev, "failed to register mtd device\n");
178 goto exit_free;
179 }
180
181 dev_info(&pdev->dev, "registered mtd device\n");
75757006 182
c3298791
IY
183 if (pdata->nr_partitions) {
184 /* add the whole device. */
185 err = mtd_device_register(info->mtd, NULL, 0);
186 if (err) {
187 dev_err(&pdev->dev,
188 "failed to register the entire device\n");
8c293f54 189 goto exit_free;
c3298791
IY
190 }
191 }
095dd500 192
8c293f54 193 return 0;
99f2a8ae
BD
194
195 exit_free:
3ae5eaec 196 platram_remove(pdev);
99f2a8ae
BD
197 exit_error:
198 return err;
199}
200
201/* device driver info */
202
41d867c9
KS
203/* work with hotplug and coldplug */
204MODULE_ALIAS("platform:mtd-ram");
205
3ae5eaec 206static struct platform_driver platram_driver = {
99f2a8ae 207 .probe = platram_probe,
f8470006 208 .remove = platram_remove,
3ae5eaec
RK
209 .driver = {
210 .name = "mtd-ram",
3ae5eaec 211 },
99f2a8ae
BD
212};
213
98a7c747 214module_platform_driver(platram_driver);
99f2a8ae
BD
215
216MODULE_LICENSE("GPL");
217MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
218MODULE_DESCRIPTION("MTD platform RAM map driver");