blob: 2b6504ecbbd16a362331000f501ee5371f5ed862 [file] [log] [blame]
Ben Dooks99f2a8a2005-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 *
7 * Generic platfrom device based RAM map
8 *
Thomas Gleixner69f34c92005-11-07 11:15:40 +00009 * $Id: plat-ram.c,v 1.7 2005/11/07 11:14:28 gleixner Exp $
Ben Dooks99f2a8a2005-01-24 00:37:04 +000010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24*/
25
Ben Dooks99f2a8a2005-01-24 00:37:04 +000026#include <linux/module.h>
27#include <linux/types.h>
28#include <linux/init.h>
29#include <linux/kernel.h>
30#include <linux/string.h>
31#include <linux/ioport.h>
32#include <linux/device.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080033#include <linux/slab.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010034#include <linux/platform_device.h>
Ben Dooks99f2a8a2005-01-24 00:37:04 +000035
36#include <linux/mtd/mtd.h>
37#include <linux/mtd/map.h>
38#include <linux/mtd/partitions.h>
39#include <linux/mtd/plat-ram.h>
40
41#include <asm/io.h>
42
43/* private structure for each mtd platform ram device created */
44
45struct platram_info {
46 struct device *dev;
47 struct mtd_info *mtd;
48 struct map_info map;
49 struct mtd_partition *partitions;
50 struct resource *area;
51 struct platdata_mtd_ram *pdata;
52};
53
54/* to_platram_info()
55 *
56 * device private data to struct platram_info conversion
57*/
58
Russell King3ae5eae2005-11-09 22:32:44 +000059static inline struct platram_info *to_platram_info(struct platform_device *dev)
Ben Dooks99f2a8a2005-01-24 00:37:04 +000060{
Russell King3ae5eae2005-11-09 22:32:44 +000061 return (struct platram_info *)platform_get_drvdata(dev);
Ben Dooks99f2a8a2005-01-24 00:37:04 +000062}
63
64/* platram_setrw
65 *
66 * call the platform device's set rw/ro control
67 *
68 * to = 0 => read-only
69 * = 1 => read-write
70*/
71
72static inline void platram_setrw(struct platram_info *info, int to)
73{
74 if (info->pdata == NULL)
75 return;
76
77 if (info->pdata->set_rw != NULL)
78 (info->pdata->set_rw)(info->dev, to);
79}
80
81/* platram_remove
82 *
83 * called to remove the device from the driver's control
84*/
85
Russell King3ae5eae2005-11-09 22:32:44 +000086static int platram_remove(struct platform_device *pdev)
Ben Dooks99f2a8a2005-01-24 00:37:04 +000087{
Russell King3ae5eae2005-11-09 22:32:44 +000088 struct platram_info *info = to_platram_info(pdev);
Ben Dooks99f2a8a2005-01-24 00:37:04 +000089
Russell King3ae5eae2005-11-09 22:32:44 +000090 platform_set_drvdata(pdev, NULL);
Ben Dooks99f2a8a2005-01-24 00:37:04 +000091
Russell King3ae5eae2005-11-09 22:32:44 +000092 dev_dbg(&pdev->dev, "removing device\n");
Ben Dooks99f2a8a2005-01-24 00:37:04 +000093
Thomas Gleixner69f34c92005-11-07 11:15:40 +000094 if (info == NULL)
Ben Dooks99f2a8a2005-01-24 00:37:04 +000095 return 0;
96
97 if (info->mtd) {
98#ifdef CONFIG_MTD_PARTITIONS
99 if (info->partitions) {
100 del_mtd_partitions(info->mtd);
101 kfree(info->partitions);
102 }
103#endif
104 del_mtd_device(info->mtd);
105 map_destroy(info->mtd);
106 }
107
108 /* ensure ram is left read-only */
109
110 platram_setrw(info, PLATRAM_RO);
111
112 /* release resources */
113
114 if (info->area) {
115 release_resource(info->area);
116 kfree(info->area);
117 }
118
119 if (info->map.virt != NULL)
120 iounmap(info->map.virt);
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000121
Ben Dooks99f2a8a2005-01-24 00:37:04 +0000122 kfree(info);
123
124 return 0;
125}
126
127/* platram_probe
128 *
129 * called from device drive system when a device matching our
130 * driver is found.
131*/
132
Russell King3ae5eae2005-11-09 22:32:44 +0000133static int platram_probe(struct platform_device *pdev)
Ben Dooks99f2a8a2005-01-24 00:37:04 +0000134{
Ben Dooks99f2a8a2005-01-24 00:37:04 +0000135 struct platdata_mtd_ram *pdata;
136 struct platram_info *info;
137 struct resource *res;
138 int err = 0;
139
Russell King3ae5eae2005-11-09 22:32:44 +0000140 dev_dbg(&pdev->dev, "probe entered\n");
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000141
Russell King3ae5eae2005-11-09 22:32:44 +0000142 if (pdev->dev.platform_data == NULL) {
143 dev_err(&pdev->dev, "no platform data supplied\n");
Ben Dooks99f2a8a2005-01-24 00:37:04 +0000144 err = -ENOENT;
145 goto exit_error;
146 }
147
Russell King3ae5eae2005-11-09 22:32:44 +0000148 pdata = pdev->dev.platform_data;
Ben Dooks99f2a8a2005-01-24 00:37:04 +0000149
Burman Yan95b93a02006-11-15 21:10:29 +0200150 info = kzalloc(sizeof(*info), GFP_KERNEL);
Ben Dooks99f2a8a2005-01-24 00:37:04 +0000151 if (info == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000152 dev_err(&pdev->dev, "no memory for flash info\n");
Ben Dooks99f2a8a2005-01-24 00:37:04 +0000153 err = -ENOMEM;
154 goto exit_error;
155 }
156
Russell King3ae5eae2005-11-09 22:32:44 +0000157 platform_set_drvdata(pdev, info);
Ben Dooks99f2a8a2005-01-24 00:37:04 +0000158
Russell King3ae5eae2005-11-09 22:32:44 +0000159 info->dev = &pdev->dev;
Ben Dooks99f2a8a2005-01-24 00:37:04 +0000160 info->pdata = pdata;
161
162 /* get the resource for the memory mapping */
163
Russell King3ae5eae2005-11-09 22:32:44 +0000164 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ben Dooks99f2a8a2005-01-24 00:37:04 +0000165
166 if (res == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000167 dev_err(&pdev->dev, "no memory resource specified\n");
Ben Dooks99f2a8a2005-01-24 00:37:04 +0000168 err = -ENOENT;
169 goto exit_free;
170 }
171
Russell King3ae5eae2005-11-09 22:32:44 +0000172 dev_dbg(&pdev->dev, "got platform resource %p (0x%lx)\n", res, res->start);
Ben Dooks99f2a8a2005-01-24 00:37:04 +0000173
174 /* setup map parameters */
175
176 info->map.phys = res->start;
177 info->map.size = (res->end - res->start) + 1;
Russell King3ae5eae2005-11-09 22:32:44 +0000178 info->map.name = pdata->mapname != NULL ? pdata->mapname : (char *)pdev->name;
Ben Dooks99f2a8a2005-01-24 00:37:04 +0000179 info->map.bankwidth = pdata->bankwidth;
180
181 /* register our usage of the memory area */
182
Russell King3ae5eae2005-11-09 22:32:44 +0000183 info->area = request_mem_region(res->start, info->map.size, pdev->name);
Ben Dooks99f2a8a2005-01-24 00:37:04 +0000184 if (info->area == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000185 dev_err(&pdev->dev, "failed to request memory region\n");
Ben Dooks99f2a8a2005-01-24 00:37:04 +0000186 err = -EIO;
187 goto exit_free;
188 }
189
190 /* remap the memory area */
191
192 info->map.virt = ioremap(res->start, info->map.size);
Russell King3ae5eae2005-11-09 22:32:44 +0000193 dev_dbg(&pdev->dev, "virt %p, %lu bytes\n", info->map.virt, info->map.size);
Ben Dooks99f2a8a2005-01-24 00:37:04 +0000194
195 if (info->map.virt == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000196 dev_err(&pdev->dev, "failed to ioremap() region\n");
Ben Dooks99f2a8a2005-01-24 00:37:04 +0000197 err = -EIO;
198 goto exit_free;
199 }
200
Ben Dooks99f2a8a2005-01-24 00:37:04 +0000201 simple_map_init(&info->map);
202
Russell King3ae5eae2005-11-09 22:32:44 +0000203 dev_dbg(&pdev->dev, "initialised map, probing for mtd\n");
Ben Dooks99f2a8a2005-01-24 00:37:04 +0000204
205 /* probe for the right mtd map driver */
206
207 info->mtd = do_map_probe("map_ram" , &info->map);
208 if (info->mtd == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000209 dev_err(&pdev->dev, "failed to probe for map_ram\n");
Ben Dooks99f2a8a2005-01-24 00:37:04 +0000210 err = -ENOMEM;
211 goto exit_free;
212 }
213
214 info->mtd->owner = THIS_MODULE;
215
216 platram_setrw(info, PLATRAM_RW);
217
218 /* check to see if there are any available partitions, or wether
219 * to add this device whole */
220
221#ifdef CONFIG_MTD_PARTITIONS
222 if (pdata->nr_partitions > 0) {
223 const char **probes = { NULL };
224
225 if (pdata->probes)
226 probes = (const char **)pdata->probes;
227
228 err = parse_mtd_partitions(info->mtd, probes,
229 &info->partitions, 0);
230 if (err > 0) {
231 err = add_mtd_partitions(info->mtd, info->partitions,
232 err);
233 }
234 }
235#endif /* CONFIG_MTD_PARTITIONS */
236
237 if (add_mtd_device(info->mtd)) {
Russell King3ae5eae2005-11-09 22:32:44 +0000238 dev_err(&pdev->dev, "add_mtd_device() failed\n");
Ben Dooks99f2a8a2005-01-24 00:37:04 +0000239 err = -ENOMEM;
240 }
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000241
Russell King3ae5eae2005-11-09 22:32:44 +0000242 dev_info(&pdev->dev, "registered mtd device\n");
Ben Dooks99f2a8a2005-01-24 00:37:04 +0000243 return err;
244
245 exit_free:
Russell King3ae5eae2005-11-09 22:32:44 +0000246 platram_remove(pdev);
Ben Dooks99f2a8a2005-01-24 00:37:04 +0000247 exit_error:
248 return err;
249}
250
251/* device driver info */
252
Russell King3ae5eae2005-11-09 22:32:44 +0000253static struct platform_driver platram_driver = {
Ben Dooks99f2a8a2005-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 Dooks99f2a8a2005-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 Dooks99f2a8a2005-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 Dooks99f2a8a2005-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");