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