blob: 9ca1eccba4bc2e70b0d5e7ea6c3be5d014f6da5e [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;
47 struct mtd_partition *partitions;
Florian Fainelli757570062008-03-03 18:30:24 +010048 bool free_partitions;
Ben Dooks99f2a8aea2005-01-24 00:37:04 +000049 struct resource *area;
50 struct platdata_mtd_ram *pdata;
51};
52
53/* to_platram_info()
54 *
55 * device private data to struct platram_info conversion
56*/
57
Russell King3ae5eae2005-11-09 22:32:44 +000058static inline struct platram_info *to_platram_info(struct platform_device *dev)
Ben Dooks99f2a8aea2005-01-24 00:37:04 +000059{
Russell King3ae5eae2005-11-09 22:32:44 +000060 return (struct platram_info *)platform_get_drvdata(dev);
Ben Dooks99f2a8aea2005-01-24 00:37:04 +000061}
62
63/* platram_setrw
64 *
65 * call the platform device's set rw/ro control
66 *
67 * to = 0 => read-only
68 * = 1 => read-write
69*/
70
71static inline void platram_setrw(struct platram_info *info, int to)
72{
73 if (info->pdata == NULL)
74 return;
75
76 if (info->pdata->set_rw != NULL)
77 (info->pdata->set_rw)(info->dev, to);
78}
79
80/* platram_remove
81 *
82 * called to remove the device from the driver's control
83*/
84
Russell King3ae5eae2005-11-09 22:32:44 +000085static int platram_remove(struct platform_device *pdev)
Ben Dooks99f2a8aea2005-01-24 00:37:04 +000086{
Russell King3ae5eae2005-11-09 22:32:44 +000087 struct platram_info *info = to_platram_info(pdev);
Ben Dooks99f2a8aea2005-01-24 00:37:04 +000088
Russell King3ae5eae2005-11-09 22:32:44 +000089 platform_set_drvdata(pdev, NULL);
Ben Dooks99f2a8aea2005-01-24 00:37:04 +000090
Russell King3ae5eae2005-11-09 22:32:44 +000091 dev_dbg(&pdev->dev, "removing device\n");
Ben Dooks99f2a8aea2005-01-24 00:37:04 +000092
Thomas Gleixner69f34c92005-11-07 11:15:40 +000093 if (info == NULL)
Ben Dooks99f2a8aea2005-01-24 00:37:04 +000094 return 0;
95
96 if (info->mtd) {
Jamie Iles095dd502011-05-23 10:23:07 +010097 mtd_device_unregister(info->mtd);
Ben Dooks99f2a8aea2005-01-24 00:37:04 +000098 if (info->partitions) {
Florian Fainelli757570062008-03-03 18:30:24 +010099 if (info->free_partitions)
100 kfree(info->partitions);
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000101 }
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000102 map_destroy(info->mtd);
103 }
104
105 /* ensure ram is left read-only */
106
107 platram_setrw(info, PLATRAM_RO);
108
109 /* release resources */
110
111 if (info->area) {
112 release_resource(info->area);
113 kfree(info->area);
114 }
115
116 if (info->map.virt != NULL)
117 iounmap(info->map.virt);
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000118
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000119 kfree(info);
120
121 return 0;
122}
123
124/* platram_probe
125 *
126 * called from device drive system when a device matching our
127 * driver is found.
128*/
129
Russell King3ae5eae2005-11-09 22:32:44 +0000130static int platram_probe(struct platform_device *pdev)
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000131{
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000132 struct platdata_mtd_ram *pdata;
133 struct platram_info *info;
134 struct resource *res;
135 int err = 0;
136
Russell King3ae5eae2005-11-09 22:32:44 +0000137 dev_dbg(&pdev->dev, "probe entered\n");
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000138
Russell King3ae5eae2005-11-09 22:32:44 +0000139 if (pdev->dev.platform_data == NULL) {
140 dev_err(&pdev->dev, "no platform data supplied\n");
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000141 err = -ENOENT;
142 goto exit_error;
143 }
144
Russell King3ae5eae2005-11-09 22:32:44 +0000145 pdata = pdev->dev.platform_data;
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000146
Burman Yan95b93a02006-11-15 21:10:29 +0200147 info = kzalloc(sizeof(*info), GFP_KERNEL);
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000148 if (info == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000149 dev_err(&pdev->dev, "no memory for flash info\n");
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000150 err = -ENOMEM;
151 goto exit_error;
152 }
153
Russell King3ae5eae2005-11-09 22:32:44 +0000154 platform_set_drvdata(pdev, info);
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000155
Russell King3ae5eae2005-11-09 22:32:44 +0000156 info->dev = &pdev->dev;
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000157 info->pdata = pdata;
158
159 /* get the resource for the memory mapping */
160
Russell King3ae5eae2005-11-09 22:32:44 +0000161 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000162
163 if (res == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000164 dev_err(&pdev->dev, "no memory resource specified\n");
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000165 err = -ENOENT;
166 goto exit_free;
167 }
168
Randy Dunlap06d63cc2007-04-25 22:41:34 -0700169 dev_dbg(&pdev->dev, "got platform resource %p (0x%llx)\n", res,
170 (unsigned long long)res->start);
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000171
172 /* setup map parameters */
173
174 info->map.phys = res->start;
Wolfram Sang76d6a472009-07-17 17:47:37 +0200175 info->map.size = resource_size(res);
Florian Fainelli757570062008-03-03 18:30:24 +0100176 info->map.name = pdata->mapname != NULL ?
177 (char *)pdata->mapname : (char *)pdev->name;
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000178 info->map.bankwidth = pdata->bankwidth;
179
180 /* register our usage of the memory area */
181
Russell King3ae5eae2005-11-09 22:32:44 +0000182 info->area = request_mem_region(res->start, info->map.size, pdev->name);
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000183 if (info->area == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000184 dev_err(&pdev->dev, "failed to request memory region\n");
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000185 err = -EIO;
186 goto exit_free;
187 }
188
189 /* remap the memory area */
190
191 info->map.virt = ioremap(res->start, info->map.size);
Russell King3ae5eae2005-11-09 22:32:44 +0000192 dev_dbg(&pdev->dev, "virt %p, %lu bytes\n", info->map.virt, info->map.size);
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000193
194 if (info->map.virt == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000195 dev_err(&pdev->dev, "failed to ioremap() region\n");
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000196 err = -EIO;
197 goto exit_free;
198 }
199
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000200 simple_map_init(&info->map);
201
Russell King3ae5eae2005-11-09 22:32:44 +0000202 dev_dbg(&pdev->dev, "initialised map, probing for mtd\n");
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000203
Florian Fainelli757570062008-03-03 18:30:24 +0100204 /* probe for the right mtd map driver
205 * supplied by the platform_data struct */
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000206
Harvey Harrisona01e0352008-04-28 16:50:04 -0700207 if (pdata->map_probes) {
Florian Fainelli757570062008-03-03 18:30:24 +0100208 const char **map_probes = pdata->map_probes;
209
210 for ( ; !info->mtd && *map_probes; map_probes++)
211 info->mtd = do_map_probe(*map_probes , &info->map);
212 }
213 /* fallback to map_ram */
214 else
215 info->mtd = do_map_probe("map_ram", &info->map);
216
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000217 if (info->mtd == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000218 dev_err(&pdev->dev, "failed to probe for map_ram\n");
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000219 err = -ENOMEM;
220 goto exit_free;
221 }
222
223 info->mtd->owner = THIS_MODULE;
David Brownell87f39f02009-03-26 00:42:50 -0700224 info->mtd->dev.parent = &pdev->dev;
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000225
226 platram_setrw(info, PLATRAM_RW);
227
228 /* check to see if there are any available partitions, or wether
229 * to add this device whole */
230
Florian Fainelli757570062008-03-03 18:30:24 +0100231 if (!pdata->nr_partitions) {
232 /* try to probe using the supplied probe type */
233 if (pdata->probes) {
234 err = parse_mtd_partitions(info->mtd, pdata->probes,
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000235 &info->partitions, 0);
Florian Fainelli757570062008-03-03 18:30:24 +0100236 info->free_partitions = 1;
237 if (err > 0)
Jamie Iles095dd502011-05-23 10:23:07 +0100238 err = mtd_device_register(info->mtd,
Florian Fainelli757570062008-03-03 18:30:24 +0100239 info->partitions, err);
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000240 }
241 }
Florian Fainelli757570062008-03-03 18:30:24 +0100242 /* use the static mapping */
243 else
Jamie Iles095dd502011-05-23 10:23:07 +0100244 err = mtd_device_register(info->mtd, pdata->partitions,
245 pdata->nr_partitions);
Florian Fainelli757570062008-03-03 18:30:24 +0100246 if (!err)
247 dev_info(&pdev->dev, "registered mtd device\n");
248
Jamie Iles095dd502011-05-23 10:23:07 +0100249 /* add the whole device. */
250 err = mtd_device_register(info->mtd, NULL, 0);
251 if (err)
252 dev_err(&pdev->dev, "failed to register the entire device\n");
253
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000254 return err;
255
256 exit_free:
Russell King3ae5eae2005-11-09 22:32:44 +0000257 platram_remove(pdev);
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000258 exit_error:
259 return err;
260}
261
262/* device driver info */
263
Kay Sievers41d867c2008-04-18 13:44:26 -0700264/* work with hotplug and coldplug */
265MODULE_ALIAS("platform:mtd-ram");
266
Russell King3ae5eae2005-11-09 22:32:44 +0000267static struct platform_driver platram_driver = {
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000268 .probe = platram_probe,
269 .remove = platram_remove,
Russell King3ae5eae2005-11-09 22:32:44 +0000270 .driver = {
271 .name = "mtd-ram",
272 .owner = THIS_MODULE,
273 },
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000274};
275
276/* module init/exit */
277
278static int __init platram_init(void)
279{
280 printk("Generic platform RAM MTD, (c) 2004 Simtec Electronics\n");
Russell King3ae5eae2005-11-09 22:32:44 +0000281 return platform_driver_register(&platram_driver);
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000282}
283
284static void __exit platram_exit(void)
285{
Russell King3ae5eae2005-11-09 22:32:44 +0000286 platform_driver_unregister(&platram_driver);
Ben Dooks99f2a8aea2005-01-24 00:37:04 +0000287}
288
289module_init(platram_init);
290module_exit(platram_exit);
291
292MODULE_LICENSE("GPL");
293MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
294MODULE_DESCRIPTION("MTD platform RAM map driver");