blob: 892ad6ac63f2936a12ecd52c256b07c5dbd395e1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Flash memory access on SA11x0 based devices
Thomas Gleixner69f34c92005-11-07 11:15:40 +00003 *
Nicolas Pitre2f82af02009-09-14 03:25:28 -04004 * (C) 2000 Nicolas Pitre <nico@fluxnic.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#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>
Russell Kingd052d1b2005-10-29 19:07:23 +010013#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/err.h>
Russell King99730222009-03-25 10:21:35 +000015#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
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
Russell Kinga09e64f2008-08-05 16:14:15 +010022#include <mach/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/sizes.h>
24#include <asm/mach/flash.h>
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026struct sa_subdev_info {
27 char name[16];
28 struct map_info map;
29 struct mtd_info *mtd;
Russell King57725f02005-10-29 15:51:14 +010030 struct flash_platform_data *plat;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031};
32
33struct sa_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 struct mtd_info *mtd;
35 int num_subdev;
36 struct sa_subdev_info subdev[0];
37};
38
Paul Parsonsee478af2012-03-07 14:14:31 +000039static DEFINE_SPINLOCK(sa1100_vpp_lock);
40static int sa1100_vpp_refcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041static 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);
Paul Parsonsee478af2012-03-07 14:14:31 +000044 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
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
Russell King57725f02005-10-29 15:51:14 +010098 if (subdev->plat->set_vpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 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 */
Russell King57725f02005-10-29 15:51:14 +0100115 subdev->mtd = do_map_probe(subdev->plat->map_name, &subdev->map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 if (subdev->mtd == NULL) {
117 ret = -ENXIO;
118 goto err;
119 }
120 subdev->mtd->owner = THIS_MODULE;
121
Russell King794d5792009-09-27 23:51:04 +0100122 printk(KERN_INFO "SA1100 flash: CFI device at 0x%08lx, %uMiB, %d-bit\n",
123 phys, (unsigned)(subdev->mtd->size >> 20),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 subdev->map.bankwidth * 8);
125
126 return 0;
127
128 err:
129 sa1100_destroy_subdev(subdev);
130 out:
131 return ret;
132}
133
Russell King0d2ef7d2005-10-29 15:57:20 +0100134static void sa1100_destroy(struct sa_info *info, struct flash_platform_data *plat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
136 int i;
137
138 if (info->mtd) {
Jamie Iles2fe2e242011-05-23 10:23:09 +0100139 mtd_device_unregister(info->mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 if (info->mtd != info->subdev[0].mtd)
141 mtd_concat_destroy(info->mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 }
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 for (i = info->num_subdev - 1; i >= 0; i--)
145 sa1100_destroy_subdev(&info->subdev[i]);
146 kfree(info);
Russell King0d2ef7d2005-10-29 15:57:20 +0100147
148 if (plat->exit)
149 plat->exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
Artem Bityutskiy7bf350b72012-11-22 12:16:28 +0200152static struct sa_info *sa1100_setup_mtd(struct platform_device *pdev,
153 struct flash_platform_data *plat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
155 struct sa_info *info;
156 int nr, size, i, ret = 0;
157
158 /*
159 * Count number of devices.
160 */
161 for (nr = 0; ; nr++)
162 if (!platform_get_resource(pdev, IORESOURCE_MEM, nr))
163 break;
164
165 if (nr == 0) {
166 ret = -ENODEV;
167 goto out;
168 }
169
170 size = sizeof(struct sa_info) + sizeof(struct sa_subdev_info) * nr;
171
172 /*
173 * Allocate the map_info structs in one go.
174 */
Burman Yan95b93a02006-11-15 21:10:29 +0200175 info = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if (!info) {
177 ret = -ENOMEM;
178 goto out;
179 }
180
Russell King0d2ef7d2005-10-29 15:57:20 +0100181 if (plat->init) {
182 ret = plat->init();
183 if (ret)
184 goto err;
185 }
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 /*
188 * Claim and then map the memory regions.
189 */
190 for (i = 0; i < nr; i++) {
191 struct sa_subdev_info *subdev = &info->subdev[i];
192 struct resource *res;
193
194 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
195 if (!res)
196 break;
197
198 subdev->map.name = subdev->name;
Russell King14e66f72005-10-29 16:08:31 +0100199 sprintf(subdev->name, "%s-%d", plat->name, i);
Russell King57725f02005-10-29 15:51:14 +0100200 subdev->plat = plat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 ret = sa1100_probe_subdev(subdev, res);
203 if (ret)
204 break;
205 }
206
207 info->num_subdev = i;
208
209 /*
210 * ENXIO is special. It means we didn't find a chip when we probed.
211 */
212 if (ret != 0 && !(ret == -ENXIO && info->num_subdev > 0))
213 goto err;
214
215 /*
216 * If we found one device, don't bother with concat support. If
217 * we found multiple devices, use concat if we have it available,
218 * otherwise fail. Either way, it'll be called "sa1100".
219 */
220 if (info->num_subdev == 1) {
Russell King14e66f72005-10-29 16:08:31 +0100221 strcpy(info->subdev[0].name, plat->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 info->mtd = info->subdev[0].mtd;
223 ret = 0;
224 } else if (info->num_subdev > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 struct mtd_info *cdev[nr];
226 /*
227 * We detected multiple devices. Concatenate them together.
228 */
229 for (i = 0; i < info->num_subdev; i++)
230 cdev[i] = info->subdev[i].mtd;
231
232 info->mtd = mtd_concat_create(cdev, info->num_subdev,
Russell King14e66f72005-10-29 16:08:31 +0100233 plat->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 if (info->mtd == NULL)
235 ret = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
237
238 if (ret == 0)
239 return info;
240
241 err:
Russell King0d2ef7d2005-10-29 15:57:20 +0100242 sa1100_destroy(info, plat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 out:
244 return ERR_PTR(ret);
245}
246
Artem Bityutskiy0984c892013-03-12 10:46:37 +0200247static const char * const part_probes[] = { "cmdlinepart", "RedBoot", NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Bill Pemberton06f25512012-11-19 13:23:07 -0500249static int sa1100_mtd_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Jingoo Hand20d5a52013-07-30 17:18:06 +0900251 struct flash_platform_data *plat = dev_get_platdata(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 struct sa_info *info;
Dmitry Eremin-Solenikov769dc432011-06-02 18:00:06 +0400253 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Russell King57725f02005-10-29 15:51:14 +0100255 if (!plat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return -ENODEV;
257
Russell King57725f02005-10-29 15:51:14 +0100258 info = sa1100_setup_mtd(pdev, plat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (IS_ERR(info)) {
260 err = PTR_ERR(info);
261 goto out;
262 }
263
264 /*
265 * Partition selection stuff.
266 */
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +0200267 mtd_device_parse_register(info->mtd, part_probes, NULL, plat->parts,
268 plat->nr_parts);
Russell King822e5e72005-10-29 16:03:24 +0100269
Russell King3ae5eae2005-11-09 22:32:44 +0000270 platform_set_drvdata(pdev, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 err = 0;
272
273 out:
274 return err;
275}
276
Dmitry Torokhov271afb42015-03-09 11:10:41 -0700277static int sa1100_mtd_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Russell King3ae5eae2005-11-09 22:32:44 +0000279 struct sa_info *info = platform_get_drvdata(pdev);
Jingoo Hand20d5a52013-07-30 17:18:06 +0900280 struct flash_platform_data *plat = dev_get_platdata(&pdev->dev);
Russell King0d2ef7d2005-10-29 15:57:20 +0100281
Russell King0d2ef7d2005-10-29 15:57:20 +0100282 sa1100_destroy(info, plat);
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return 0;
285}
286
Russell King3ae5eae2005-11-09 22:32:44 +0000287static struct platform_driver sa1100_mtd_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 .probe = sa1100_mtd_probe,
Dmitry Torokhov271afb42015-03-09 11:10:41 -0700289 .remove = sa1100_mtd_remove,
Russell King3ae5eae2005-11-09 22:32:44 +0000290 .driver = {
Uwe Kleine-Königbcc8f3e2009-01-31 01:21:58 +0100291 .name = "sa1100-mtd",
Russell King3ae5eae2005-11-09 22:32:44 +0000292 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293};
294
Axel Linf99640d2011-11-27 20:45:03 +0800295module_platform_driver(sa1100_mtd_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297MODULE_AUTHOR("Nicolas Pitre");
298MODULE_DESCRIPTION("SA1100 CFI map driver");
299MODULE_LICENSE("GPL");
Uwe Kleine-Königbcc8f3e2009-01-31 01:21:58 +0100300MODULE_ALIAS("platform:sa1100-mtd");