blob: 891558de3ec19d1ef67ca69751be8f06e21e480f [file] [log] [blame]
Ben Dooks99f2a8aea2005-01-24 00:37:04 +00001/* drivers/mtd/maps/plat-ram.c
2 *
3 * (c) 2004-2005 Simtec Electronics
4 * http://www.simtec.co.uk/products/SWLINUX/
5 * Ben Dooks <ben@simtec.co.uk>
6 *
Stefan Weil947af292010-01-07 00:03:52 +01007 * Generic platform device based RAM map
Ben Dooks99f2a8aea2005-01-24 00:37:04 +00008 *
Ben Dooks99f2a8aea2005-01-24 00:37:04 +00009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22*/
23
Ben Dooks99f2a8aea2005-01-24 00:37:04 +000024#include <linux/module.h>
25#include <linux/types.h>
26#include <linux/init.h>
27#include <linux/kernel.h>
28#include <linux/string.h>
29#include <linux/ioport.h>
30#include <linux/device.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080031#include <linux/slab.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010032#include <linux/platform_device.h>
Ben Dooks99f2a8aea2005-01-24 00:37:04 +000033
34#include <linux/mtd/mtd.h>
35#include <linux/mtd/map.h>
36#include <linux/mtd/partitions.h>
37#include <linux/mtd/plat-ram.h>
38
39#include <asm/io.h>
40
41/* private structure for each mtd platform ram device created */
42
43struct platram_info {
44 struct device *dev;
45 struct mtd_info *mtd;
46 struct map_info map;
Ben Dooks99f2a8aea2005-01-24 00:37:04 +000047 struct resource *area;
48 struct platdata_mtd_ram *pdata;
49};
50
51/* to_platram_info()
52 *
53 * device private data to struct platram_info conversion
54*/
55
Russell King3ae5eae2005-11-09 22:32:44 +000056static inline struct platram_info *to_platram_info(struct platform_device *dev)
Ben Dooks99f2a8aea2005-01-24 00:37:04 +000057{
Russell King3ae5eae2005-11-09 22:32:44 +000058 return (struct platram_info *)platform_get_drvdata(dev);
Ben Dooks99f2a8aea2005-01-24 00:37:04 +000059}
60
61/* platram_setrw
62 *
63 * call the platform device's set rw/ro control
64 *
65 * to = 0 => read-only
66 * = 1 => read-write
67*/
68
69static inline void platram_setrw(struct platram_info *info, int to)
70{
71 if (info->pdata == NULL)
72 return;
73
74 if (info->pdata->set_rw != NULL)
75 (info->pdata->set_rw)(info->dev, to);
76}
77
78/* platram_remove
79 *
80 * called to remove the device from the driver's control
81*/
82
Russell King3ae5eae2005-11-09 22:32:44 +000083static int platram_remove(struct platform_device *pdev)
Ben Dooks99f2a8aea2005-01-24 00:37:04 +000084{
Russell King3ae5eae2005-11-09 22:32:44 +000085 struct platram_info *info = to_platram_info(pdev);
Ben Dooks99f2a8aea2005-01-24 00:37:04 +000086
Russell King3ae5eae2005-11-09 22:32:44 +000087 platform_set_drvdata(pdev, NULL);
Ben Dooks99f2a8aea2005-01-24 00:37:04 +000088
Russell King3ae5eae2005-11-09 22:32:44 +000089 dev_dbg(&pdev->dev, "removing device\n");
Ben Dooks99f2a8aea2005-01-24 00:37:04 +000090
Thomas Gleixner69f34c92005-11-07 11:15:40 +000091 if (info == NULL)
Ben Dooks99f2a8aea2005-01-24 00:37:04 +000092 return 0;
93
94 if (info->mtd) {
Jamie Iles095dd502011-05-23 10:23:07 +010095 mtd_device_unregister(info->mtd);
Ben Dooks99f2a8aea2005-01-24 00:37:04 +000096 map_destroy(info->mtd);
97 }
98
99 /* ensure ram is left read-only */
100
101 platram_setrw(info, PLATRAM_RO);
102
103 /* release resources */
104
105 if (info->area) {
106 release_resource(info->area);
107 kfree(info->area);
108 }
109
110 if (info->map.virt != NULL)
111 iounmap(info->map.virt);
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000112
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000113 kfree(info);
114
115 return 0;
116}
117
118/* platram_probe
119 *
120 * called from device drive system when a device matching our
121 * driver is found.
122*/
123
Russell King3ae5eae2005-11-09 22:32:44 +0000124static int platram_probe(struct platform_device *pdev)
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000125{
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000126 struct platdata_mtd_ram *pdata;
127 struct platram_info *info;
128 struct resource *res;
129 int err = 0;
130
Russell King3ae5eae2005-11-09 22:32:44 +0000131 dev_dbg(&pdev->dev, "probe entered\n");
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000132
Russell King3ae5eae2005-11-09 22:32:44 +0000133 if (pdev->dev.platform_data == NULL) {
134 dev_err(&pdev->dev, "no platform data supplied\n");
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000135 err = -ENOENT;
136 goto exit_error;
137 }
138
Russell King3ae5eae2005-11-09 22:32:44 +0000139 pdata = pdev->dev.platform_data;
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000140
Burman Yan95b93a02006-11-15 21:10:29 +0200141 info = kzalloc(sizeof(*info), GFP_KERNEL);
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000142 if (info == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000143 dev_err(&pdev->dev, "no memory for flash info\n");
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000144 err = -ENOMEM;
145 goto exit_error;
146 }
147
Russell King3ae5eae2005-11-09 22:32:44 +0000148 platform_set_drvdata(pdev, info);
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000149
Russell King3ae5eae2005-11-09 22:32:44 +0000150 info->dev = &pdev->dev;
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000151 info->pdata = pdata;
152
153 /* get the resource for the memory mapping */
154
Russell King3ae5eae2005-11-09 22:32:44 +0000155 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000156
157 if (res == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000158 dev_err(&pdev->dev, "no memory resource specified\n");
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000159 err = -ENOENT;
160 goto exit_free;
161 }
162
Randy Dunlap06d63cc2007-04-25 22:41:34 -0700163 dev_dbg(&pdev->dev, "got platform resource %p (0x%llx)\n", res,
164 (unsigned long long)res->start);
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000165
166 /* setup map parameters */
167
168 info->map.phys = res->start;
Wolfram Sang76d6a472009-07-17 17:47:37 +0200169 info->map.size = resource_size(res);
Florian Fainelli757570062008-03-03 18:30:24 +0100170 info->map.name = pdata->mapname != NULL ?
171 (char *)pdata->mapname : (char *)pdev->name;
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000172 info->map.bankwidth = pdata->bankwidth;
173
174 /* register our usage of the memory area */
175
Russell King3ae5eae2005-11-09 22:32:44 +0000176 info->area = request_mem_region(res->start, info->map.size, pdev->name);
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000177 if (info->area == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000178 dev_err(&pdev->dev, "failed to request memory region\n");
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000179 err = -EIO;
180 goto exit_free;
181 }
182
183 /* remap the memory area */
184
185 info->map.virt = ioremap(res->start, info->map.size);
Russell King3ae5eae2005-11-09 22:32:44 +0000186 dev_dbg(&pdev->dev, "virt %p, %lu bytes\n", info->map.virt, info->map.size);
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000187
188 if (info->map.virt == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000189 dev_err(&pdev->dev, "failed to ioremap() region\n");
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000190 err = -EIO;
191 goto exit_free;
192 }
193
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000194 simple_map_init(&info->map);
195
Russell King3ae5eae2005-11-09 22:32:44 +0000196 dev_dbg(&pdev->dev, "initialised map, probing for mtd\n");
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000197
Florian Fainelli757570062008-03-03 18:30:24 +0100198 /* probe for the right mtd map driver
199 * supplied by the platform_data struct */
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000200
Harvey Harrisona01e0352008-04-28 16:50:04 -0700201 if (pdata->map_probes) {
Florian Fainelli757570062008-03-03 18:30:24 +0100202 const char **map_probes = pdata->map_probes;
203
204 for ( ; !info->mtd && *map_probes; map_probes++)
205 info->mtd = do_map_probe(*map_probes , &info->map);
206 }
207 /* fallback to map_ram */
208 else
209 info->mtd = do_map_probe("map_ram", &info->map);
210
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000211 if (info->mtd == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000212 dev_err(&pdev->dev, "failed to probe for map_ram\n");
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000213 err = -ENOMEM;
214 goto exit_free;
215 }
216
217 info->mtd->owner = THIS_MODULE;
David Brownell87f39f02009-03-26 00:42:50 -0700218 info->mtd->dev.parent = &pdev->dev;
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000219
220 platram_setrw(info, PLATRAM_RW);
221
222 /* check to see if there are any available partitions, or wether
223 * to add this device whole */
224
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +0200225 err = mtd_device_parse_register(info->mtd, pdata->probes, NULL,
226 pdata->partitions,
227 pdata->nr_partitions);
Florian Fainelli757570062008-03-03 18:30:24 +0100228 if (!err)
229 dev_info(&pdev->dev, "registered mtd device\n");
230
Ilya Yanokc3298792011-12-13 00:37:56 +0100231 if (pdata->nr_partitions) {
232 /* add the whole device. */
233 err = mtd_device_register(info->mtd, NULL, 0);
234 if (err) {
235 dev_err(&pdev->dev,
236 "failed to register the entire device\n");
237 }
238 }
Jamie Iles095dd502011-05-23 10:23:07 +0100239
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000240 return err;
241
242 exit_free:
Russell King3ae5eae2005-11-09 22:32:44 +0000243 platram_remove(pdev);
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000244 exit_error:
245 return err;
246}
247
248/* device driver info */
249
Kay Sievers41d867c2008-04-18 13:44:26 -0700250/* work with hotplug and coldplug */
251MODULE_ALIAS("platform:mtd-ram");
252
Russell King3ae5eae2005-11-09 22:32:44 +0000253static struct platform_driver platram_driver = {
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000254 .probe = platram_probe,
255 .remove = platram_remove,
Russell King3ae5eae2005-11-09 22:32:44 +0000256 .driver = {
257 .name = "mtd-ram",
258 .owner = THIS_MODULE,
259 },
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000260};
261
262/* module init/exit */
263
264static int __init platram_init(void)
265{
266 printk("Generic platform RAM MTD, (c) 2004 Simtec Electronics\n");
Russell King3ae5eae2005-11-09 22:32:44 +0000267 return platform_driver_register(&platram_driver);
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000268}
269
270static void __exit platram_exit(void)
271{
Russell King3ae5eae2005-11-09 22:32:44 +0000272 platform_driver_unregister(&platram_driver);
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000273}
274
275module_init(platram_init);
276module_exit(platram_exit);
277
278MODULE_LICENSE("GPL");
279MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
280MODULE_DESCRIPTION("MTD platform RAM map driver");