blob: cedd8ef9a6bf5a2096df73744e1f6e9eed8df629 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Ben Dooks99f2a8aea2005-01-24 00:37:04 +00002/* 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 *
Stefan Weil947af292010-01-07 00:03:52 +01008 * Generic platform device based RAM map
Ben Dooks99f2a8aea2005-01-24 00:37:04 +00009*/
10
Ben Dooks99f2a8aea2005-01-24 00:37:04 +000011#include <linux/module.h>
12#include <linux/types.h>
Ben Dooks99f2a8aea2005-01-24 00:37:04 +000013#include <linux/kernel.h>
14#include <linux/string.h>
15#include <linux/ioport.h>
16#include <linux/device.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080017#include <linux/slab.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010018#include <linux/platform_device.h>
Ben Dooks99f2a8aea2005-01-24 00:37:04 +000019
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;
Ben Dooks99f2a8aea2005-01-24 00:37:04 +000033 struct platdata_mtd_ram *pdata;
34};
35
36/* to_platram_info()
37 *
38 * device private data to struct platram_info conversion
39*/
40
Russell King3ae5eae2005-11-09 22:32:44 +000041static inline struct platram_info *to_platram_info(struct platform_device *dev)
Ben Dooks99f2a8aea2005-01-24 00:37:04 +000042{
Jingoo Han14ac0782013-09-09 14:42:52 +090043 return platform_get_drvdata(dev);
Ben Dooks99f2a8aea2005-01-24 00:37:04 +000044}
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
Russell King3ae5eae2005-11-09 22:32:44 +000068static int platram_remove(struct platform_device *pdev)
Ben Dooks99f2a8aea2005-01-24 00:37:04 +000069{
Russell King3ae5eae2005-11-09 22:32:44 +000070 struct platram_info *info = to_platram_info(pdev);
Ben Dooks99f2a8aea2005-01-24 00:37:04 +000071
Russell King3ae5eae2005-11-09 22:32:44 +000072 dev_dbg(&pdev->dev, "removing device\n");
Ben Dooks99f2a8aea2005-01-24 00:37:04 +000073
Thomas Gleixner69f34c92005-11-07 11:15:40 +000074 if (info == NULL)
Ben Dooks99f2a8aea2005-01-24 00:37:04 +000075 return 0;
76
77 if (info->mtd) {
Jamie Iles095dd502011-05-23 10:23:07 +010078 mtd_device_unregister(info->mtd);
Ben Dooks99f2a8aea2005-01-24 00:37:04 +000079 map_destroy(info->mtd);
80 }
81
82 /* ensure ram is left read-only */
83
84 platram_setrw(info, PLATRAM_RO);
85
Ben Dooks99f2a8aea2005-01-24 00:37:04 +000086 kfree(info);
87
88 return 0;
89}
90
91/* platram_probe
92 *
93 * called from device drive system when a device matching our
94 * driver is found.
95*/
96
Russell King3ae5eae2005-11-09 22:32:44 +000097static int platram_probe(struct platform_device *pdev)
Ben Dooks99f2a8aea2005-01-24 00:37:04 +000098{
Ben Dooks99f2a8aea2005-01-24 00:37:04 +000099 struct platdata_mtd_ram *pdata;
100 struct platram_info *info;
101 struct resource *res;
102 int err = 0;
103
Russell King3ae5eae2005-11-09 22:32:44 +0000104 dev_dbg(&pdev->dev, "probe entered\n");
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000105
Jingoo Hand20d5a52013-07-30 17:18:06 +0900106 if (dev_get_platdata(&pdev->dev) == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000107 dev_err(&pdev->dev, "no platform data supplied\n");
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000108 err = -ENOENT;
109 goto exit_error;
110 }
111
Jingoo Hand20d5a52013-07-30 17:18:06 +0900112 pdata = dev_get_platdata(&pdev->dev);
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000113
Burman Yan95b93a02006-11-15 21:10:29 +0200114 info = kzalloc(sizeof(*info), GFP_KERNEL);
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000115 if (info == NULL) {
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000116 err = -ENOMEM;
117 goto exit_error;
118 }
119
Russell King3ae5eae2005-11-09 22:32:44 +0000120 platform_set_drvdata(pdev, info);
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000121
Russell King3ae5eae2005-11-09 22:32:44 +0000122 info->dev = &pdev->dev;
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000123 info->pdata = pdata;
124
125 /* get the resource for the memory mapping */
Russell King3ae5eae2005-11-09 22:32:44 +0000126 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Anton Vasilyev2e442ae2017-08-22 16:24:11 +0300127 info->map.virt = devm_ioremap_resource(&pdev->dev, res);
128 if (IS_ERR(info->map.virt)) {
129 err = PTR_ERR(info->map.virt);
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000130 goto exit_free;
131 }
132
Randy Dunlap06d63cc2007-04-25 22:41:34 -0700133 dev_dbg(&pdev->dev, "got platform resource %p (0x%llx)\n", res,
134 (unsigned long long)res->start);
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000135
136 /* setup map parameters */
137
138 info->map.phys = res->start;
Wolfram Sang76d6a472009-07-17 17:47:37 +0200139 info->map.size = resource_size(res);
Florian Fainelli757570062008-03-03 18:30:24 +0100140 info->map.name = pdata->mapname != NULL ?
141 (char *)pdata->mapname : (char *)pdev->name;
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000142 info->map.bankwidth = pdata->bankwidth;
143
Russell King3ae5eae2005-11-09 22:32:44 +0000144 dev_dbg(&pdev->dev, "virt %p, %lu bytes\n", info->map.virt, info->map.size);
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000145
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000146 simple_map_init(&info->map);
147
Russell King3ae5eae2005-11-09 22:32:44 +0000148 dev_dbg(&pdev->dev, "initialised map, probing for mtd\n");
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000149
Florian Fainelli757570062008-03-03 18:30:24 +0100150 /* probe for the right mtd map driver
151 * supplied by the platform_data struct */
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000152
Harvey Harrisona01e0352008-04-28 16:50:04 -0700153 if (pdata->map_probes) {
Artem Bityutskiyd50dcb12013-03-12 10:28:31 +0200154 const char * const *map_probes = pdata->map_probes;
Florian Fainelli757570062008-03-03 18:30:24 +0100155
156 for ( ; !info->mtd && *map_probes; map_probes++)
157 info->mtd = do_map_probe(*map_probes , &info->map);
158 }
159 /* fallback to map_ram */
160 else
161 info->mtd = do_map_probe("map_ram", &info->map);
162
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000163 if (info->mtd == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000164 dev_err(&pdev->dev, "failed to probe for map_ram\n");
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000165 err = -ENOMEM;
166 goto exit_free;
167 }
168
David Brownell87f39f02009-03-26 00:42:50 -0700169 info->mtd->dev.parent = &pdev->dev;
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000170
171 platram_setrw(info, PLATRAM_RW);
172
Adam Buchbinder48fc7f72012-09-19 21:48:00 -0400173 /* check to see if there are any available partitions, or whether
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000174 * to add this device whole */
175
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +0200176 err = mtd_device_parse_register(info->mtd, pdata->probes, NULL,
177 pdata->partitions,
178 pdata->nr_partitions);
Baskov Evgeiny8c293f52020-11-13 19:05:37 +0300179 if (err) {
180 dev_err(&pdev->dev, "failed to register mtd device\n");
181 goto exit_free;
182 }
183
184 dev_info(&pdev->dev, "registered mtd device\n");
Florian Fainelli757570062008-03-03 18:30:24 +0100185
Ilya Yanokc3298792011-12-13 00:37:56 +0100186 if (pdata->nr_partitions) {
187 /* add the whole device. */
188 err = mtd_device_register(info->mtd, NULL, 0);
189 if (err) {
190 dev_err(&pdev->dev,
191 "failed to register the entire device\n");
Baskov Evgeiny8c293f52020-11-13 19:05:37 +0300192 goto exit_free;
Ilya Yanokc3298792011-12-13 00:37:56 +0100193 }
194 }
Jamie Iles095dd502011-05-23 10:23:07 +0100195
Baskov Evgeiny8c293f52020-11-13 19:05:37 +0300196 return 0;
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000197
198 exit_free:
Russell King3ae5eae2005-11-09 22:32:44 +0000199 platram_remove(pdev);
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000200 exit_error:
201 return err;
202}
203
204/* device driver info */
205
Kay Sievers41d867c2008-04-18 13:44:26 -0700206/* work with hotplug and coldplug */
207MODULE_ALIAS("platform:mtd-ram");
208
Russell King3ae5eae2005-11-09 22:32:44 +0000209static struct platform_driver platram_driver = {
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000210 .probe = platram_probe,
211 .remove = platram_remove,
Russell King3ae5eae2005-11-09 22:32:44 +0000212 .driver = {
213 .name = "mtd-ram",
Russell King3ae5eae2005-11-09 22:32:44 +0000214 },
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000215};
216
Sachin Kamat98a7c742013-10-11 10:11:23 +0530217module_platform_driver(platram_driver);
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000218
219MODULE_LICENSE("GPL");
220MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
221MODULE_DESCRIPTION("MTD platform RAM map driver");