Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Flash memory access on SA11x0 based devices |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 3 | * |
Nicolas Pitre | 2f82af0 | 2009-09-14 03:25:28 -0400 | [diff] [blame] | 4 | * (C) 2000 Nicolas Pitre <nico@fluxnic.net> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 13 | #include <linux/platform_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/err.h> |
Russell King | 9973022 | 2009-03-25 10:21:35 +0000 | [diff] [blame] | 15 | #include <linux/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 22 | #include <mach/hardware.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <asm/sizes.h> |
| 24 | #include <asm/mach/flash.h> |
| 25 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | struct sa_subdev_info { |
| 27 | char name[16]; |
| 28 | struct map_info map; |
| 29 | struct mtd_info *mtd; |
Russell King | 57725f0 | 2005-10-29 15:51:14 +0100 | [diff] [blame] | 30 | struct flash_platform_data *plat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | struct sa_info { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | struct mtd_info *mtd; |
| 35 | int num_subdev; |
| 36 | struct sa_subdev_info subdev[0]; |
| 37 | }; |
| 38 | |
Paul Parsons | ee478af | 2012-03-07 14:14:31 +0000 | [diff] [blame] | 39 | static DEFINE_SPINLOCK(sa1100_vpp_lock); |
| 40 | static int sa1100_vpp_refcnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | static 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 Parsons | ee478af | 2012-03-07 14:14:31 +0000 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | static 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 | |
| 66 | static 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 King | 57725f0 | 2005-10-29 15:51:14 +0100 | [diff] [blame] | 98 | if (subdev->plat->set_vpp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Russell King | 57725f0 | 2005-10-29 15:51:14 +0100 | [diff] [blame] | 115 | subdev->mtd = do_map_probe(subdev->plat->map_name, &subdev->map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | if (subdev->mtd == NULL) { |
| 117 | ret = -ENXIO; |
| 118 | goto err; |
| 119 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
Russell King | 794d579 | 2009-09-27 23:51:04 +0100 | [diff] [blame] | 121 | printk(KERN_INFO "SA1100 flash: CFI device at 0x%08lx, %uMiB, %d-bit\n", |
| 122 | phys, (unsigned)(subdev->mtd->size >> 20), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | subdev->map.bankwidth * 8); |
| 124 | |
| 125 | return 0; |
| 126 | |
| 127 | err: |
| 128 | sa1100_destroy_subdev(subdev); |
| 129 | out: |
| 130 | return ret; |
| 131 | } |
| 132 | |
Russell King | 0d2ef7d | 2005-10-29 15:57:20 +0100 | [diff] [blame] | 133 | static void sa1100_destroy(struct sa_info *info, struct flash_platform_data *plat) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | { |
| 135 | int i; |
| 136 | |
| 137 | if (info->mtd) { |
Jamie Iles | 2fe2e24 | 2011-05-23 10:23:09 +0100 | [diff] [blame] | 138 | mtd_device_unregister(info->mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | if (info->mtd != info->subdev[0].mtd) |
| 140 | mtd_concat_destroy(info->mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | for (i = info->num_subdev - 1; i >= 0; i--) |
| 144 | sa1100_destroy_subdev(&info->subdev[i]); |
| 145 | kfree(info); |
Russell King | 0d2ef7d | 2005-10-29 15:57:20 +0100 | [diff] [blame] | 146 | |
| 147 | if (plat->exit) |
| 148 | plat->exit(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Artem Bityutskiy | 7bf350b7 | 2012-11-22 12:16:28 +0200 | [diff] [blame] | 151 | static struct sa_info *sa1100_setup_mtd(struct platform_device *pdev, |
| 152 | struct flash_platform_data *plat) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Burman Yan | 95b93a0 | 2006-11-15 21:10:29 +0200 | [diff] [blame] | 174 | info = kzalloc(size, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | if (!info) { |
| 176 | ret = -ENOMEM; |
| 177 | goto out; |
| 178 | } |
| 179 | |
Russell King | 0d2ef7d | 2005-10-29 15:57:20 +0100 | [diff] [blame] | 180 | if (plat->init) { |
| 181 | ret = plat->init(); |
| 182 | if (ret) |
| 183 | goto err; |
| 184 | } |
| 185 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Russell King | 14e66f7 | 2005-10-29 16:08:31 +0100 | [diff] [blame] | 198 | sprintf(subdev->name, "%s-%d", plat->name, i); |
Russell King | 57725f0 | 2005-10-29 15:51:14 +0100 | [diff] [blame] | 199 | subdev->plat = plat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
Russell King | 14e66f7 | 2005-10-29 16:08:31 +0100 | [diff] [blame] | 220 | strcpy(info->subdev[0].name, plat->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | info->mtd = info->subdev[0].mtd; |
| 222 | ret = 0; |
| 223 | } else if (info->num_subdev > 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, |
Russell King | 14e66f7 | 2005-10-29 16:08:31 +0100 | [diff] [blame] | 232 | plat->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | if (info->mtd == NULL) |
| 234 | ret = -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | } |
Frans Klaver | 7216975 | 2015-06-10 22:38:35 +0200 | [diff] [blame] | 236 | info->mtd->dev.parent = &pdev->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | |
| 238 | if (ret == 0) |
| 239 | return info; |
| 240 | |
| 241 | err: |
Russell King | 0d2ef7d | 2005-10-29 15:57:20 +0100 | [diff] [blame] | 242 | sa1100_destroy(info, plat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | out: |
| 244 | return ERR_PTR(ret); |
| 245 | } |
| 246 | |
Artem Bityutskiy | 0984c89 | 2013-03-12 10:46:37 +0200 | [diff] [blame] | 247 | static const char * const part_probes[] = { "cmdlinepart", "RedBoot", NULL }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |
Bill Pemberton | 06f2551 | 2012-11-19 13:23:07 -0500 | [diff] [blame] | 249 | static int sa1100_mtd_probe(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | { |
Jingoo Han | d20d5a5 | 2013-07-30 17:18:06 +0900 | [diff] [blame] | 251 | struct flash_platform_data *plat = dev_get_platdata(&pdev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | struct sa_info *info; |
Dmitry Eremin-Solenikov | 769dc43 | 2011-06-02 18:00:06 +0400 | [diff] [blame] | 253 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | |
Russell King | 57725f0 | 2005-10-29 15:51:14 +0100 | [diff] [blame] | 255 | if (!plat) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | return -ENODEV; |
| 257 | |
Russell King | 57725f0 | 2005-10-29 15:51:14 +0100 | [diff] [blame] | 258 | info = sa1100_setup_mtd(pdev, plat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | if (IS_ERR(info)) { |
| 260 | err = PTR_ERR(info); |
| 261 | goto out; |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | * Partition selection stuff. |
| 266 | */ |
Artem Bityutskiy | 42d7fbe | 2012-03-09 19:24:26 +0200 | [diff] [blame] | 267 | mtd_device_parse_register(info->mtd, part_probes, NULL, plat->parts, |
| 268 | plat->nr_parts); |
Russell King | 822e5e7 | 2005-10-29 16:03:24 +0100 | [diff] [blame] | 269 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 270 | platform_set_drvdata(pdev, info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | err = 0; |
| 272 | |
| 273 | out: |
| 274 | return err; |
| 275 | } |
| 276 | |
Dmitry Torokhov | 271afb4 | 2015-03-09 11:10:41 -0700 | [diff] [blame] | 277 | static int sa1100_mtd_remove(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 279 | struct sa_info *info = platform_get_drvdata(pdev); |
Jingoo Han | d20d5a5 | 2013-07-30 17:18:06 +0900 | [diff] [blame] | 280 | struct flash_platform_data *plat = dev_get_platdata(&pdev->dev); |
Russell King | 0d2ef7d | 2005-10-29 15:57:20 +0100 | [diff] [blame] | 281 | |
Russell King | 0d2ef7d | 2005-10-29 15:57:20 +0100 | [diff] [blame] | 282 | sa1100_destroy(info, plat); |
| 283 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | return 0; |
| 285 | } |
| 286 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 287 | static struct platform_driver sa1100_mtd_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | .probe = sa1100_mtd_probe, |
Dmitry Torokhov | 271afb4 | 2015-03-09 11:10:41 -0700 | [diff] [blame] | 289 | .remove = sa1100_mtd_remove, |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 290 | .driver = { |
Uwe Kleine-König | bcc8f3e | 2009-01-31 01:21:58 +0100 | [diff] [blame] | 291 | .name = "sa1100-mtd", |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 292 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | }; |
| 294 | |
Axel Lin | f99640d | 2011-11-27 20:45:03 +0800 | [diff] [blame] | 295 | module_platform_driver(sa1100_mtd_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | |
| 297 | MODULE_AUTHOR("Nicolas Pitre"); |
| 298 | MODULE_DESCRIPTION("SA1100 CFI map driver"); |
| 299 | MODULE_LICENSE("GPL"); |
Uwe Kleine-König | bcc8f3e | 2009-01-31 01:21:58 +0100 | [diff] [blame] | 300 | MODULE_ALIAS("platform:sa1100-mtd"); |