xref: /openbmc/linux/drivers/mtd/maps/plat-ram.c (revision 8bd1369b)
1 /* 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 platform device based RAM map
8  *
9  * 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 
24 #include <linux/module.h>
25 #include <linux/types.h>
26 #include <linux/kernel.h>
27 #include <linux/string.h>
28 #include <linux/ioport.h>
29 #include <linux/device.h>
30 #include <linux/slab.h>
31 #include <linux/platform_device.h>
32 
33 #include <linux/mtd/mtd.h>
34 #include <linux/mtd/map.h>
35 #include <linux/mtd/partitions.h>
36 #include <linux/mtd/plat-ram.h>
37 
38 #include <asm/io.h>
39 
40 /* private structure for each mtd platform ram device created */
41 
42 struct platram_info {
43 	struct device		*dev;
44 	struct mtd_info		*mtd;
45 	struct map_info		 map;
46 	struct platdata_mtd_ram	*pdata;
47 };
48 
49 /* to_platram_info()
50  *
51  * device private data to struct platram_info conversion
52 */
53 
54 static inline struct platram_info *to_platram_info(struct platform_device *dev)
55 {
56 	return platform_get_drvdata(dev);
57 }
58 
59 /* platram_setrw
60  *
61  * call the platform device's set rw/ro control
62  *
63  * to = 0 => read-only
64  *    = 1 => read-write
65 */
66 
67 static inline void platram_setrw(struct platram_info *info, int to)
68 {
69 	if (info->pdata == NULL)
70 		return;
71 
72 	if (info->pdata->set_rw != NULL)
73 		(info->pdata->set_rw)(info->dev, to);
74 }
75 
76 /* platram_remove
77  *
78  * called to remove the device from the driver's control
79 */
80 
81 static int platram_remove(struct platform_device *pdev)
82 {
83 	struct platram_info *info = to_platram_info(pdev);
84 
85 	dev_dbg(&pdev->dev, "removing device\n");
86 
87 	if (info == NULL)
88 		return 0;
89 
90 	if (info->mtd) {
91 		mtd_device_unregister(info->mtd);
92 		map_destroy(info->mtd);
93 	}
94 
95 	/* ensure ram is left read-only */
96 
97 	platram_setrw(info, PLATRAM_RO);
98 
99 	kfree(info);
100 
101 	return 0;
102 }
103 
104 /* platram_probe
105  *
106  * called from device drive system when a device matching our
107  * driver is found.
108 */
109 
110 static int platram_probe(struct platform_device *pdev)
111 {
112 	struct platdata_mtd_ram	*pdata;
113 	struct platram_info *info;
114 	struct resource *res;
115 	int err = 0;
116 
117 	dev_dbg(&pdev->dev, "probe entered\n");
118 
119 	if (dev_get_platdata(&pdev->dev) == NULL) {
120 		dev_err(&pdev->dev, "no platform data supplied\n");
121 		err = -ENOENT;
122 		goto exit_error;
123 	}
124 
125 	pdata = dev_get_platdata(&pdev->dev);
126 
127 	info = kzalloc(sizeof(*info), GFP_KERNEL);
128 	if (info == NULL) {
129 		err = -ENOMEM;
130 		goto exit_error;
131 	}
132 
133 	platform_set_drvdata(pdev, info);
134 
135 	info->dev = &pdev->dev;
136 	info->pdata = pdata;
137 
138 	/* get the resource for the memory mapping */
139 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
140 	info->map.virt = devm_ioremap_resource(&pdev->dev, res);
141 	if (IS_ERR(info->map.virt)) {
142 		err = PTR_ERR(info->map.virt);
143 		dev_err(&pdev->dev, "failed to ioremap() region\n");
144 		goto exit_free;
145 	}
146 
147 	dev_dbg(&pdev->dev, "got platform resource %p (0x%llx)\n", res,
148 		(unsigned long long)res->start);
149 
150 	/* setup map parameters */
151 
152 	info->map.phys = res->start;
153 	info->map.size = resource_size(res);
154 	info->map.name = pdata->mapname != NULL ?
155 			(char *)pdata->mapname : (char *)pdev->name;
156 	info->map.bankwidth = pdata->bankwidth;
157 
158 	dev_dbg(&pdev->dev, "virt %p, %lu bytes\n", info->map.virt, info->map.size);
159 
160 	simple_map_init(&info->map);
161 
162 	dev_dbg(&pdev->dev, "initialised map, probing for mtd\n");
163 
164 	/* probe for the right mtd map driver
165 	 * supplied by the platform_data struct */
166 
167 	if (pdata->map_probes) {
168 		const char * const *map_probes = pdata->map_probes;
169 
170 		for ( ; !info->mtd && *map_probes; map_probes++)
171 			info->mtd = do_map_probe(*map_probes , &info->map);
172 	}
173 	/* fallback to map_ram */
174 	else
175 		info->mtd = do_map_probe("map_ram", &info->map);
176 
177 	if (info->mtd == NULL) {
178 		dev_err(&pdev->dev, "failed to probe for map_ram\n");
179 		err = -ENOMEM;
180 		goto exit_free;
181 	}
182 
183 	info->mtd->dev.parent = &pdev->dev;
184 
185 	platram_setrw(info, PLATRAM_RW);
186 
187 	/* check to see if there are any available partitions, or whether
188 	 * to add this device whole */
189 
190 	err = mtd_device_parse_register(info->mtd, pdata->probes, NULL,
191 					pdata->partitions,
192 					pdata->nr_partitions);
193 	if (!err)
194 		dev_info(&pdev->dev, "registered mtd device\n");
195 
196 	if (pdata->nr_partitions) {
197 		/* add the whole device. */
198 		err = mtd_device_register(info->mtd, NULL, 0);
199 		if (err) {
200 			dev_err(&pdev->dev,
201 				"failed to register the entire device\n");
202 		}
203 	}
204 
205 	return err;
206 
207  exit_free:
208 	platram_remove(pdev);
209  exit_error:
210 	return err;
211 }
212 
213 /* device driver info */
214 
215 /* work with hotplug and coldplug */
216 MODULE_ALIAS("platform:mtd-ram");
217 
218 static struct platform_driver platram_driver = {
219 	.probe		= platram_probe,
220 	.remove		= platram_remove,
221 	.driver		= {
222 		.name	= "mtd-ram",
223 	},
224 };
225 
226 module_platform_driver(platram_driver);
227 
228 MODULE_LICENSE("GPL");
229 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
230 MODULE_DESCRIPTION("MTD platform RAM map driver");
231