blob: 47602af4ee34940a3f5fd381aca7dcf7fd7c1a48 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Flash memory access on SA11x0 based devices
Thomas Gleixner69f34c92005-11-07 11:15:40 +00004 *
Nicolas Pitre2f82af02009-09-14 03:25:28 -04005 * (C) 2000 Nicolas Pitre <nico@fluxnic.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/module.h>
8#include <linux/types.h>
9#include <linux/ioport.h>
10#include <linux/kernel.h>
11#include <linux/init.h>
12#include <linux/errno.h>
13#include <linux/slab.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010014#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/err.h>
Russell King99730222009-03-25 10:21:35 +000016#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#include <linux/mtd/mtd.h>
19#include <linux/mtd/map.h>
20#include <linux/mtd/partitions.h>
21#include <linux/mtd/concat.h>
22
Russell Kinga09e64f2008-08-05 16:14:15 +010023#include <mach/hardware.h>
Masahiro Yamada87dfb312019-05-14 15:46:51 -070024#include <linux/sizes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/mach/flash.h>
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027struct sa_subdev_info {
28 char name[16];
29 struct map_info map;
30 struct mtd_info *mtd;
Russell King57725f02005-10-29 15:51:14 +010031 struct flash_platform_data *plat;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032};
33
34struct sa_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 struct mtd_info *mtd;
36 int num_subdev;
37 struct sa_subdev_info subdev[0];
38};
39
Paul Parsonsee478af2012-03-07 14:14:31 +000040static DEFINE_SPINLOCK(sa1100_vpp_lock);
41static int sa1100_vpp_refcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static void sa1100_set_vpp(struct map_info *map, int on)
43{
44 struct sa_subdev_info *subdev = container_of(map, struct sa_subdev_info, map);
Paul Parsonsee478af2012-03-07 14:14:31 +000045 unsigned long flags;
46
47 spin_lock_irqsave(&sa1100_vpp_lock, flags);
48 if (on) {
49 if (++sa1100_vpp_refcnt == 1) /* first nested 'on' */
50 subdev->plat->set_vpp(1);
51 } else {
52 if (--sa1100_vpp_refcnt == 0) /* last nested 'off' */
53 subdev->plat->set_vpp(0);
54 }
55 spin_unlock_irqrestore(&sa1100_vpp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056}
57
58static void sa1100_destroy_subdev(struct sa_subdev_info *subdev)
59{
60 if (subdev->mtd)
61 map_destroy(subdev->mtd);
62 if (subdev->map.virt)
63 iounmap(subdev->map.virt);
64 release_mem_region(subdev->map.phys, subdev->map.size);
65}
66
67static int sa1100_probe_subdev(struct sa_subdev_info *subdev, struct resource *res)
68{
69 unsigned long phys;
70 unsigned int size;
71 int ret;
72
73 phys = res->start;
74 size = res->end - phys + 1;
75
76 /*
77 * Retrieve the bankwidth from the MSC registers.
78 * We currently only implement CS0 and CS1 here.
79 */
80 switch (phys) {
81 default:
82 printk(KERN_WARNING "SA1100 flash: unknown base address "
83 "0x%08lx, assuming CS0\n", phys);
Gustavo A. R. Silva3f0289c2019-08-20 12:54:32 -050084 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 case SA1100_CS0_PHYS:
87 subdev->map.bankwidth = (MSC0 & MSC_RBW) ? 2 : 4;
88 break;
89
90 case SA1100_CS1_PHYS:
91 subdev->map.bankwidth = ((MSC0 >> 16) & MSC_RBW) ? 2 : 4;
92 break;
93 }
94
95 if (!request_mem_region(phys, size, subdev->name)) {
96 ret = -EBUSY;
97 goto out;
98 }
99
Russell King57725f02005-10-29 15:51:14 +0100100 if (subdev->plat->set_vpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 subdev->map.set_vpp = sa1100_set_vpp;
102
103 subdev->map.phys = phys;
104 subdev->map.size = size;
105 subdev->map.virt = ioremap(phys, size);
106 if (!subdev->map.virt) {
107 ret = -ENOMEM;
108 goto err;
109 }
110
111 simple_map_init(&subdev->map);
112
113 /*
114 * Now let's probe for the actual flash. Do it here since
115 * specific machine settings might have been set above.
116 */
Russell King57725f02005-10-29 15:51:14 +0100117 subdev->mtd = do_map_probe(subdev->plat->map_name, &subdev->map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 if (subdev->mtd == NULL) {
119 ret = -ENXIO;
120 goto err;
121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Russell King794d5792009-09-27 23:51:04 +0100123 printk(KERN_INFO "SA1100 flash: CFI device at 0x%08lx, %uMiB, %d-bit\n",
124 phys, (unsigned)(subdev->mtd->size >> 20),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 subdev->map.bankwidth * 8);
126
127 return 0;
128
129 err:
130 sa1100_destroy_subdev(subdev);
131 out:
132 return ret;
133}
134
Russell King0d2ef7d2005-10-29 15:57:20 +0100135static void sa1100_destroy(struct sa_info *info, struct flash_platform_data *plat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 int i;
138
139 if (info->mtd) {
Jamie Iles2fe2e242011-05-23 10:23:09 +0100140 mtd_device_unregister(info->mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 if (info->mtd != info->subdev[0].mtd)
142 mtd_concat_destroy(info->mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 for (i = info->num_subdev - 1; i >= 0; i--)
146 sa1100_destroy_subdev(&info->subdev[i]);
147 kfree(info);
Russell King0d2ef7d2005-10-29 15:57:20 +0100148
149 if (plat->exit)
150 plat->exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
Artem Bityutskiy7bf350b72012-11-22 12:16:28 +0200153static struct sa_info *sa1100_setup_mtd(struct platform_device *pdev,
154 struct flash_platform_data *plat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
156 struct sa_info *info;
157 int nr, size, i, ret = 0;
158
159 /*
160 * Count number of devices.
161 */
162 for (nr = 0; ; nr++)
163 if (!platform_get_resource(pdev, IORESOURCE_MEM, nr))
164 break;
165
166 if (nr == 0) {
167 ret = -ENODEV;
168 goto out;
169 }
170
171 size = sizeof(struct sa_info) + sizeof(struct sa_subdev_info) * nr;
172
173 /*
174 * Allocate the map_info structs in one go.
175 */
Burman Yan95b93a02006-11-15 21:10:29 +0200176 info = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 if (!info) {
178 ret = -ENOMEM;
179 goto out;
180 }
181
Russell King0d2ef7d2005-10-29 15:57:20 +0100182 if (plat->init) {
183 ret = plat->init();
184 if (ret)
185 goto err;
186 }
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 /*
189 * Claim and then map the memory regions.
190 */
191 for (i = 0; i < nr; i++) {
192 struct sa_subdev_info *subdev = &info->subdev[i];
193 struct resource *res;
194
195 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
196 if (!res)
197 break;
198
199 subdev->map.name = subdev->name;
Russell King14e66f72005-10-29 16:08:31 +0100200 sprintf(subdev->name, "%s-%d", plat->name, i);
Russell King57725f02005-10-29 15:51:14 +0100201 subdev->plat = plat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 ret = sa1100_probe_subdev(subdev, res);
204 if (ret)
205 break;
206 }
207
208 info->num_subdev = i;
209
210 /*
211 * ENXIO is special. It means we didn't find a chip when we probed.
212 */
213 if (ret != 0 && !(ret == -ENXIO && info->num_subdev > 0))
214 goto err;
215
216 /*
217 * If we found one device, don't bother with concat support. If
218 * we found multiple devices, use concat if we have it available,
219 * otherwise fail. Either way, it'll be called "sa1100".
220 */
221 if (info->num_subdev == 1) {
Russell King14e66f72005-10-29 16:08:31 +0100222 strcpy(info->subdev[0].name, plat->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 info->mtd = info->subdev[0].mtd;
224 ret = 0;
225 } else if (info->num_subdev > 1) {
Boris Brezillonba26cd72018-11-05 08:58:35 +0100226 struct mtd_info **cdev;
227
228 cdev = kmalloc_array(nr, sizeof(*cdev), GFP_KERNEL);
229 if (!cdev) {
230 ret = -ENOMEM;
231 goto err;
232 }
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 /*
235 * We detected multiple devices. Concatenate them together.
236 */
237 for (i = 0; i < info->num_subdev; i++)
238 cdev[i] = info->subdev[i].mtd;
239
240 info->mtd = mtd_concat_create(cdev, info->num_subdev,
Russell King14e66f72005-10-29 16:08:31 +0100241 plat->name);
Boris Brezillonba26cd72018-11-05 08:58:35 +0100242 kfree(cdev);
Dan Carpenterdc01a282016-07-15 14:06:30 +0300243 if (info->mtd == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 ret = -ENXIO;
Dan Carpenterdc01a282016-07-15 14:06:30 +0300245 goto err;
246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
Frans Klaver72169752015-06-10 22:38:35 +0200248 info->mtd->dev.parent = &pdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 if (ret == 0)
251 return info;
252
253 err:
Russell King0d2ef7d2005-10-29 15:57:20 +0100254 sa1100_destroy(info, plat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 out:
256 return ERR_PTR(ret);
257}
258
Artem Bityutskiy0984c892013-03-12 10:46:37 +0200259static const char * const part_probes[] = { "cmdlinepart", "RedBoot", NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Bill Pemberton06f25512012-11-19 13:23:07 -0500261static int sa1100_mtd_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Jingoo Hand20d5a52013-07-30 17:18:06 +0900263 struct flash_platform_data *plat = dev_get_platdata(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 struct sa_info *info;
Dmitry Eremin-Solenikov769dc432011-06-02 18:00:06 +0400265 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Russell King57725f02005-10-29 15:51:14 +0100267 if (!plat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return -ENODEV;
269
Russell King57725f02005-10-29 15:51:14 +0100270 info = sa1100_setup_mtd(pdev, plat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if (IS_ERR(info)) {
272 err = PTR_ERR(info);
273 goto out;
274 }
275
276 /*
277 * Partition selection stuff.
278 */
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +0200279 mtd_device_parse_register(info->mtd, part_probes, NULL, plat->parts,
280 plat->nr_parts);
Russell King822e5e72005-10-29 16:03:24 +0100281
Russell King3ae5eae2005-11-09 22:32:44 +0000282 platform_set_drvdata(pdev, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 err = 0;
284
285 out:
286 return err;
287}
288
Dmitry Torokhov271afb42015-03-09 11:10:41 -0700289static int sa1100_mtd_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Russell King3ae5eae2005-11-09 22:32:44 +0000291 struct sa_info *info = platform_get_drvdata(pdev);
Jingoo Hand20d5a52013-07-30 17:18:06 +0900292 struct flash_platform_data *plat = dev_get_platdata(&pdev->dev);
Russell King0d2ef7d2005-10-29 15:57:20 +0100293
Russell King0d2ef7d2005-10-29 15:57:20 +0100294 sa1100_destroy(info, plat);
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 return 0;
297}
298
Russell King3ae5eae2005-11-09 22:32:44 +0000299static struct platform_driver sa1100_mtd_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 .probe = sa1100_mtd_probe,
Dmitry Torokhov271afb42015-03-09 11:10:41 -0700301 .remove = sa1100_mtd_remove,
Russell King3ae5eae2005-11-09 22:32:44 +0000302 .driver = {
Uwe Kleine-Königbcc8f3e2009-01-31 01:21:58 +0100303 .name = "sa1100-mtd",
Russell King3ae5eae2005-11-09 22:32:44 +0000304 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305};
306
Axel Linf99640d2011-11-27 20:45:03 +0800307module_platform_driver(sa1100_mtd_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309MODULE_AUTHOR("Nicolas Pitre");
310MODULE_DESCRIPTION("SA1100 CFI map driver");
311MODULE_LICENSE("GPL");
Uwe Kleine-Königbcc8f3e2009-01-31 01:21:58 +0100312MODULE_ALIAS("platform:sa1100-mtd");