]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/mtd/maps/sa1100-flash.c
mtd: remove use of __devexit
[mirror_ubuntu-bionic-kernel.git] / drivers / mtd / maps / sa1100-flash.c
CommitLineData
1da177e4
LT
1/*
2 * Flash memory access on SA11x0 based devices
69f34c98 3 *
2f82af08 4 * (C) 2000 Nicolas Pitre <nico@fluxnic.net>
1da177e4 5 */
1da177e4
LT
6#include <linux/module.h>
7#include <linux/types.h>
8#include <linux/ioport.h>
9#include <linux/kernel.h>
10#include <linux/init.h>
11#include <linux/errno.h>
12#include <linux/slab.h>
d052d1be 13#include <linux/platform_device.h>
1da177e4 14#include <linux/err.h>
99730225 15#include <linux/io.h>
1da177e4
LT
16
17#include <linux/mtd/mtd.h>
18#include <linux/mtd/map.h>
19#include <linux/mtd/partitions.h>
20#include <linux/mtd/concat.h>
21
a09e64fb 22#include <mach/hardware.h>
1da177e4
LT
23#include <asm/sizes.h>
24#include <asm/mach/flash.h>
25
1da177e4
LT
26struct sa_subdev_info {
27 char name[16];
28 struct map_info map;
29 struct mtd_info *mtd;
57725f0a 30 struct flash_platform_data *plat;
1da177e4
LT
31};
32
33struct sa_info {
1da177e4
LT
34 struct mtd_info *mtd;
35 int num_subdev;
36 struct sa_subdev_info subdev[0];
37};
38
ee478af8
PP
39static DEFINE_SPINLOCK(sa1100_vpp_lock);
40static int sa1100_vpp_refcnt;
1da177e4
LT
41static void sa1100_set_vpp(struct map_info *map, int on)
42{
43 struct sa_subdev_info *subdev = container_of(map, struct sa_subdev_info, map);
ee478af8
PP
44 unsigned long flags;
45
46 spin_lock_irqsave(&sa1100_vpp_lock, flags);
47 if (on) {
48 if (++sa1100_vpp_refcnt == 1) /* first nested 'on' */
49 subdev->plat->set_vpp(1);
50 } else {
51 if (--sa1100_vpp_refcnt == 0) /* last nested 'off' */
52 subdev->plat->set_vpp(0);
53 }
54 spin_unlock_irqrestore(&sa1100_vpp_lock, flags);
1da177e4
LT
55}
56
57static void sa1100_destroy_subdev(struct sa_subdev_info *subdev)
58{
59 if (subdev->mtd)
60 map_destroy(subdev->mtd);
61 if (subdev->map.virt)
62 iounmap(subdev->map.virt);
63 release_mem_region(subdev->map.phys, subdev->map.size);
64}
65
66static int sa1100_probe_subdev(struct sa_subdev_info *subdev, struct resource *res)
67{
68 unsigned long phys;
69 unsigned int size;
70 int ret;
71
72 phys = res->start;
73 size = res->end - phys + 1;
74
75 /*
76 * Retrieve the bankwidth from the MSC registers.
77 * We currently only implement CS0 and CS1 here.
78 */
79 switch (phys) {
80 default:
81 printk(KERN_WARNING "SA1100 flash: unknown base address "
82 "0x%08lx, assuming CS0\n", phys);
83
84 case SA1100_CS0_PHYS:
85 subdev->map.bankwidth = (MSC0 & MSC_RBW) ? 2 : 4;
86 break;
87
88 case SA1100_CS1_PHYS:
89 subdev->map.bankwidth = ((MSC0 >> 16) & MSC_RBW) ? 2 : 4;
90 break;
91 }
92
93 if (!request_mem_region(phys, size, subdev->name)) {
94 ret = -EBUSY;
95 goto out;
96 }
97
57725f0a 98 if (subdev->plat->set_vpp)
1da177e4
LT
99 subdev->map.set_vpp = sa1100_set_vpp;
100
101 subdev->map.phys = phys;
102 subdev->map.size = size;
103 subdev->map.virt = ioremap(phys, size);
104 if (!subdev->map.virt) {
105 ret = -ENOMEM;
106 goto err;
107 }
108
109 simple_map_init(&subdev->map);
110
111 /*
112 * Now let's probe for the actual flash. Do it here since
113 * specific machine settings might have been set above.
114 */
57725f0a 115 subdev->mtd = do_map_probe(subdev->plat->map_name, &subdev->map);
1da177e4
LT
116 if (subdev->mtd == NULL) {
117 ret = -ENXIO;
118 goto err;
119 }
120 subdev->mtd->owner = THIS_MODULE;
121
794d579a
RK
122 printk(KERN_INFO "SA1100 flash: CFI device at 0x%08lx, %uMiB, %d-bit\n",
123 phys, (unsigned)(subdev->mtd->size >> 20),
1da177e4
LT
124 subdev->map.bankwidth * 8);
125
126 return 0;
127
128 err:
129 sa1100_destroy_subdev(subdev);
130 out:
131 return ret;
132}
133
0d2ef7d7 134static void sa1100_destroy(struct sa_info *info, struct flash_platform_data *plat)
1da177e4
LT
135{
136 int i;
137
138 if (info->mtd) {
2fe2e24e 139 mtd_device_unregister(info->mtd);
1da177e4
LT
140 if (info->mtd != info->subdev[0].mtd)
141 mtd_concat_destroy(info->mtd);
1da177e4
LT
142 }
143
1da177e4
LT
144 for (i = info->num_subdev - 1; i >= 0; i--)
145 sa1100_destroy_subdev(&info->subdev[i]);
146 kfree(info);
0d2ef7d7
RK
147
148 if (plat->exit)
149 plat->exit();
1da177e4
LT
150}
151
06f25510 152static struct sa_info *sa1100_setup_mtd(struct platform_device *pdev, struct flash_platform_data *plat)
1da177e4
LT
153{
154 struct sa_info *info;
155 int nr, size, i, ret = 0;
156
157 /*
158 * Count number of devices.
159 */
160 for (nr = 0; ; nr++)
161 if (!platform_get_resource(pdev, IORESOURCE_MEM, nr))
162 break;
163
164 if (nr == 0) {
165 ret = -ENODEV;
166 goto out;
167 }
168
169 size = sizeof(struct sa_info) + sizeof(struct sa_subdev_info) * nr;
170
171 /*
172 * Allocate the map_info structs in one go.
173 */
95b93a0c 174 info = kzalloc(size, GFP_KERNEL);
1da177e4
LT
175 if (!info) {
176 ret = -ENOMEM;
177 goto out;
178 }
179
0d2ef7d7
RK
180 if (plat->init) {
181 ret = plat->init();
182 if (ret)
183 goto err;
184 }
185
1da177e4
LT
186 /*
187 * Claim and then map the memory regions.
188 */
189 for (i = 0; i < nr; i++) {
190 struct sa_subdev_info *subdev = &info->subdev[i];
191 struct resource *res;
192
193 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
194 if (!res)
195 break;
196
197 subdev->map.name = subdev->name;
14e66f76 198 sprintf(subdev->name, "%s-%d", plat->name, i);
57725f0a 199 subdev->plat = plat;
1da177e4
LT
200
201 ret = sa1100_probe_subdev(subdev, res);
202 if (ret)
203 break;
204 }
205
206 info->num_subdev = i;
207
208 /*
209 * ENXIO is special. It means we didn't find a chip when we probed.
210 */
211 if (ret != 0 && !(ret == -ENXIO && info->num_subdev > 0))
212 goto err;
213
214 /*
215 * If we found one device, don't bother with concat support. If
216 * we found multiple devices, use concat if we have it available,
217 * otherwise fail. Either way, it'll be called "sa1100".
218 */
219 if (info->num_subdev == 1) {
14e66f76 220 strcpy(info->subdev[0].name, plat->name);
1da177e4
LT
221 info->mtd = info->subdev[0].mtd;
222 ret = 0;
223 } else if (info->num_subdev > 1) {
1da177e4
LT
224 struct mtd_info *cdev[nr];
225 /*
226 * We detected multiple devices. Concatenate them together.
227 */
228 for (i = 0; i < info->num_subdev; i++)
229 cdev[i] = info->subdev[i].mtd;
230
231 info->mtd = mtd_concat_create(cdev, info->num_subdev,
14e66f76 232 plat->name);
1da177e4
LT
233 if (info->mtd == NULL)
234 ret = -ENXIO;
1da177e4
LT
235 }
236
237 if (ret == 0)
238 return info;
239
240 err:
0d2ef7d7 241 sa1100_destroy(info, plat);
1da177e4
LT
242 out:
243 return ERR_PTR(ret);
244}
245
246static const char *part_probes[] = { "cmdlinepart", "RedBoot", NULL };
247
06f25510 248static int sa1100_mtd_probe(struct platform_device *pdev)
1da177e4 249{
57725f0a 250 struct flash_platform_data *plat = pdev->dev.platform_data;
1da177e4 251 struct sa_info *info;
769dc431 252 int err;
1da177e4 253
57725f0a 254 if (!plat)
1da177e4
LT
255 return -ENODEV;
256
57725f0a 257 info = sa1100_setup_mtd(pdev, plat);
1da177e4
LT
258 if (IS_ERR(info)) {
259 err = PTR_ERR(info);
260 goto out;
261 }
262
263 /*
264 * Partition selection stuff.
265 */
42d7fbe2
AB
266 mtd_device_parse_register(info->mtd, part_probes, NULL, plat->parts,
267 plat->nr_parts);
822e5e72 268
3ae5eaec 269 platform_set_drvdata(pdev, info);
1da177e4
LT
270 err = 0;
271
272 out:
273 return err;
274}
275
3ae5eaec 276static int __exit sa1100_mtd_remove(struct platform_device *pdev)
1da177e4 277{
3ae5eaec
RK
278 struct sa_info *info = platform_get_drvdata(pdev);
279 struct flash_platform_data *plat = pdev->dev.platform_data;
0d2ef7d7 280
3ae5eaec 281 platform_set_drvdata(pdev, NULL);
0d2ef7d7
RK
282 sa1100_destroy(info, plat);
283
1da177e4
LT
284 return 0;
285}
286
3ae5eaec 287static struct platform_driver sa1100_mtd_driver = {
1da177e4
LT
288 .probe = sa1100_mtd_probe,
289 .remove = __exit_p(sa1100_mtd_remove),
3ae5eaec 290 .driver = {
bcc8f3e0 291 .name = "sa1100-mtd",
41d867c9 292 .owner = THIS_MODULE,
3ae5eaec 293 },
1da177e4
LT
294};
295
f99640de 296module_platform_driver(sa1100_mtd_driver);
1da177e4
LT
297
298MODULE_AUTHOR("Nicolas Pitre");
299MODULE_DESCRIPTION("SA1100 CFI map driver");
300MODULE_LICENSE("GPL");
bcc8f3e0 301MODULE_ALIAS("platform:sa1100-mtd");