Boris Brezillon | 18d54e5 | 2018-10-19 09:48:54 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Normal mappings of chips in physical memory |
| 4 | * |
| 5 | * Copyright (C) 2003 MontaVista Software Inc. |
| 6 | * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net |
| 7 | * |
| 8 | * 031022 - [jsun] add run-time configure and partition setup |
Boris Brezillon | 642b1e8 | 2018-10-19 09:49:04 +0200 | [diff] [blame] | 9 | * |
| 10 | * Device tree support: |
| 11 | * Copyright (C) 2006 MontaVista Software Inc. |
| 12 | * Author: Vitaly Wool <vwool@ru.mvista.com> |
| 13 | * |
| 14 | * Revised to handle newer style flash binding by: |
| 15 | * Copyright (C) 2007 David Gibson, IBM Corporation. |
Boris Brezillon | ba32ce9 | 2018-10-19 09:49:07 +0200 | [diff] [blame] | 16 | * |
| 17 | * GPIO address extension: |
| 18 | * Handle the case where a flash device is mostly addressed using physical |
| 19 | * line and supplemented by GPIOs. This way you can hook up say a 8MiB flash |
| 20 | * to a 2MiB memory range and use the GPIOs to select a particular range. |
| 21 | * |
| 22 | * Copyright © 2000 Nicolas Pitre <nico@cam.org> |
| 23 | * Copyright © 2005-2009 Analog Devices Inc. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | */ |
| 25 | |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/types.h> |
| 28 | #include <linux/kernel.h> |
| 29 | #include <linux/init.h> |
| 30 | #include <linux/slab.h> |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 31 | #include <linux/device.h> |
| 32 | #include <linux/platform_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/mtd/mtd.h> |
| 34 | #include <linux/mtd/map.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/mtd/partitions.h> |
Adrian Bunk | 2b9175c | 2005-11-29 14:49:38 +0000 | [diff] [blame] | 36 | #include <linux/mtd/physmap.h> |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 37 | #include <linux/mtd/concat.h> |
Boris Brezillon | 642b1e8 | 2018-10-19 09:49:04 +0200 | [diff] [blame] | 38 | #include <linux/mtd/cfi_endian.h> |
Atsushi Nemoto | 3136e90 | 2008-11-26 10:26:29 +0000 | [diff] [blame] | 39 | #include <linux/io.h> |
Boris Brezillon | 642b1e8 | 2018-10-19 09:49:04 +0200 | [diff] [blame] | 40 | #include <linux/of_device.h> |
Geert Uytterhoeven | 0bc448b | 2019-12-09 14:48:23 +0100 | [diff] [blame] | 41 | #include <linux/pm_runtime.h> |
Boris Brezillon | ba32ce9 | 2018-10-19 09:49:07 +0200 | [diff] [blame] | 42 | #include <linux/gpio/consumer.h> |
Boris Brezillon | 642b1e8 | 2018-10-19 09:49:04 +0200 | [diff] [blame] | 43 | |
Serge Semin | b3e79e7 | 2020-09-20 14:14:44 +0300 | [diff] [blame] | 44 | #include "physmap-bt1-rom.h" |
Boris Brezillon | 6ca15cf | 2018-10-19 09:49:05 +0200 | [diff] [blame] | 45 | #include "physmap-gemini.h" |
Linus Walleij | 2aba2f2 | 2019-10-21 01:00:42 +0200 | [diff] [blame] | 46 | #include "physmap-ixp4xx.h" |
Boris Brezillon | 6ca15cf | 2018-10-19 09:49:05 +0200 | [diff] [blame] | 47 | #include "physmap-versatile.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 49 | struct physmap_flash_info { |
Boris Brezillon | 51b436a | 2018-10-19 09:48:59 +0200 | [diff] [blame] | 50 | unsigned int nmaps; |
| 51 | struct mtd_info **mtds; |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 52 | struct mtd_info *cmtd; |
Boris Brezillon | 51b436a | 2018-10-19 09:48:59 +0200 | [diff] [blame] | 53 | struct map_info *maps; |
Paul Parsons | 876fe76 | 2012-03-07 14:12:08 +0000 | [diff] [blame] | 54 | spinlock_t vpp_lock; |
| 55 | int vpp_refcnt; |
Boris Brezillon | 642b1e8 | 2018-10-19 09:49:04 +0200 | [diff] [blame] | 56 | const char *probe_type; |
| 57 | const char * const *part_types; |
| 58 | unsigned int nparts; |
| 59 | const struct mtd_partition *parts; |
Boris Brezillon | ba32ce9 | 2018-10-19 09:49:07 +0200 | [diff] [blame] | 60 | struct gpio_descs *gpios; |
| 61 | unsigned int gpio_values; |
| 62 | unsigned int win_order; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 65 | static int physmap_flash_remove(struct platform_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | { |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 67 | struct physmap_flash_info *info; |
| 68 | struct physmap_flash_data *physmap_data; |
Uwe Kleine-König | bb82365 | 2022-06-03 23:07:57 +0200 | [diff] [blame] | 69 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 71 | info = platform_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
H Hartley Sweeten | 8ce110a | 2009-10-20 12:23:33 -0400 | [diff] [blame] | 73 | if (info->cmtd) { |
Uwe Kleine-König | bb82365 | 2022-06-03 23:07:57 +0200 | [diff] [blame] | 74 | WARN_ON(mtd_device_unregister(info->cmtd)); |
Boris Brezillon | 1e4f420 | 2018-10-19 09:49:00 +0200 | [diff] [blame] | 75 | |
Boris Brezillon | 00142d6 | 2018-10-19 09:48:55 +0200 | [diff] [blame] | 76 | if (info->cmtd != info->mtds[0]) |
H Hartley Sweeten | 8ce110a | 2009-10-20 12:23:33 -0400 | [diff] [blame] | 77 | mtd_concat_destroy(info->cmtd); |
H Hartley Sweeten | 8ce110a | 2009-10-20 12:23:33 -0400 | [diff] [blame] | 78 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
Boris Brezillon | 51b436a | 2018-10-19 09:48:59 +0200 | [diff] [blame] | 80 | for (i = 0; i < info->nmaps; i++) { |
Boris Brezillon | cb946bf | 2018-10-19 09:49:02 +0200 | [diff] [blame] | 81 | if (info->mtds[i]) |
Boris Brezillon | 00142d6 | 2018-10-19 09:48:55 +0200 | [diff] [blame] | 82 | map_destroy(info->mtds[i]); |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 83 | } |
Marc Zyngier | b7281ca | 2011-05-18 10:51:48 +0100 | [diff] [blame] | 84 | |
Boris Brezillon | 642b1e8 | 2018-10-19 09:49:04 +0200 | [diff] [blame] | 85 | physmap_data = dev_get_platdata(&dev->dev); |
| 86 | if (physmap_data && physmap_data->exit) |
Marc Zyngier | b7281ca | 2011-05-18 10:51:48 +0100 | [diff] [blame] | 87 | physmap_data->exit(dev); |
| 88 | |
Geert Uytterhoeven | 0bc448b | 2019-12-09 14:48:23 +0100 | [diff] [blame] | 89 | pm_runtime_put(&dev->dev); |
| 90 | pm_runtime_disable(&dev->dev); |
Uwe Kleine-König | bb82365 | 2022-06-03 23:07:57 +0200 | [diff] [blame] | 91 | return 0; |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 92 | } |
| 93 | |
Marc Zyngier | 667f390 | 2011-05-18 10:51:55 +0100 | [diff] [blame] | 94 | static void physmap_set_vpp(struct map_info *map, int state) |
| 95 | { |
| 96 | struct platform_device *pdev; |
| 97 | struct physmap_flash_data *physmap_data; |
Paul Parsons | 876fe76 | 2012-03-07 14:12:08 +0000 | [diff] [blame] | 98 | struct physmap_flash_info *info; |
| 99 | unsigned long flags; |
Marc Zyngier | 667f390 | 2011-05-18 10:51:55 +0100 | [diff] [blame] | 100 | |
| 101 | pdev = (struct platform_device *)map->map_priv_1; |
Jingoo Han | d20d5a5 | 2013-07-30 17:18:06 +0900 | [diff] [blame] | 102 | physmap_data = dev_get_platdata(&pdev->dev); |
Marc Zyngier | 667f390 | 2011-05-18 10:51:55 +0100 | [diff] [blame] | 103 | |
Paul Parsons | 876fe76 | 2012-03-07 14:12:08 +0000 | [diff] [blame] | 104 | if (!physmap_data->set_vpp) |
| 105 | return; |
| 106 | |
| 107 | info = platform_get_drvdata(pdev); |
| 108 | |
| 109 | spin_lock_irqsave(&info->vpp_lock, flags); |
| 110 | if (state) { |
| 111 | if (++info->vpp_refcnt == 1) /* first nested 'on' */ |
| 112 | physmap_data->set_vpp(pdev, 1); |
| 113 | } else { |
| 114 | if (--info->vpp_refcnt == 0) /* last nested 'off' */ |
| 115 | physmap_data->set_vpp(pdev, 0); |
| 116 | } |
| 117 | spin_unlock_irqrestore(&info->vpp_lock, flags); |
Marc Zyngier | 667f390 | 2011-05-18 10:51:55 +0100 | [diff] [blame] | 118 | } |
| 119 | |
Boris Brezillon | ba32ce9 | 2018-10-19 09:49:07 +0200 | [diff] [blame] | 120 | #if IS_ENABLED(CONFIG_MTD_PHYSMAP_GPIO_ADDR) |
| 121 | static void physmap_set_addr_gpios(struct physmap_flash_info *info, |
| 122 | unsigned long ofs) |
| 123 | { |
| 124 | unsigned int i; |
| 125 | |
| 126 | ofs >>= info->win_order; |
| 127 | if (info->gpio_values == ofs) |
| 128 | return; |
| 129 | |
| 130 | for (i = 0; i < info->gpios->ndescs; i++) { |
| 131 | if ((BIT(i) & ofs) == (BIT(i) & info->gpio_values)) |
| 132 | continue; |
| 133 | |
| 134 | gpiod_set_value(info->gpios->desc[i], !!(BIT(i) & ofs)); |
| 135 | } |
Chris Packham | 64d14c6 | 2019-03-29 15:13:21 +1300 | [diff] [blame] | 136 | |
| 137 | info->gpio_values = ofs; |
Boris Brezillon | ba32ce9 | 2018-10-19 09:49:07 +0200 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | #define win_mask(order) (BIT(order) - 1) |
| 141 | |
| 142 | static map_word physmap_addr_gpios_read(struct map_info *map, |
| 143 | unsigned long ofs) |
| 144 | { |
| 145 | struct platform_device *pdev; |
| 146 | struct physmap_flash_info *info; |
| 147 | map_word mw; |
| 148 | u16 word; |
| 149 | |
| 150 | pdev = (struct platform_device *)map->map_priv_1; |
| 151 | info = platform_get_drvdata(pdev); |
| 152 | physmap_set_addr_gpios(info, ofs); |
| 153 | |
| 154 | word = readw(map->virt + (ofs & win_mask(info->win_order))); |
| 155 | mw.x[0] = word; |
| 156 | return mw; |
| 157 | } |
| 158 | |
| 159 | static void physmap_addr_gpios_copy_from(struct map_info *map, void *buf, |
| 160 | unsigned long ofs, ssize_t len) |
| 161 | { |
| 162 | struct platform_device *pdev; |
| 163 | struct physmap_flash_info *info; |
| 164 | |
| 165 | pdev = (struct platform_device *)map->map_priv_1; |
| 166 | info = platform_get_drvdata(pdev); |
| 167 | |
| 168 | while (len) { |
| 169 | unsigned int winofs = ofs & win_mask(info->win_order); |
| 170 | unsigned int chunklen = min_t(unsigned int, len, |
| 171 | BIT(info->win_order) - winofs); |
| 172 | |
| 173 | physmap_set_addr_gpios(info, ofs); |
| 174 | memcpy_fromio(buf, map->virt + winofs, chunklen); |
| 175 | len -= chunklen; |
| 176 | buf += chunklen; |
| 177 | ofs += chunklen; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | static void physmap_addr_gpios_write(struct map_info *map, map_word mw, |
| 182 | unsigned long ofs) |
| 183 | { |
| 184 | struct platform_device *pdev; |
| 185 | struct physmap_flash_info *info; |
| 186 | u16 word; |
| 187 | |
| 188 | pdev = (struct platform_device *)map->map_priv_1; |
| 189 | info = platform_get_drvdata(pdev); |
| 190 | physmap_set_addr_gpios(info, ofs); |
| 191 | |
| 192 | word = mw.x[0]; |
| 193 | writew(word, map->virt + (ofs & win_mask(info->win_order))); |
| 194 | } |
| 195 | |
| 196 | static void physmap_addr_gpios_copy_to(struct map_info *map, unsigned long ofs, |
| 197 | const void *buf, ssize_t len) |
| 198 | { |
| 199 | struct platform_device *pdev; |
| 200 | struct physmap_flash_info *info; |
| 201 | |
| 202 | pdev = (struct platform_device *)map->map_priv_1; |
| 203 | info = platform_get_drvdata(pdev); |
| 204 | |
| 205 | while (len) { |
| 206 | unsigned int winofs = ofs & win_mask(info->win_order); |
| 207 | unsigned int chunklen = min_t(unsigned int, len, |
| 208 | BIT(info->win_order) - winofs); |
| 209 | |
| 210 | physmap_set_addr_gpios(info, ofs); |
| 211 | memcpy_toio(map->virt + winofs, buf, chunklen); |
| 212 | len -= chunklen; |
| 213 | buf += chunklen; |
| 214 | ofs += chunklen; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | static int physmap_addr_gpios_map_init(struct map_info *map) |
| 219 | { |
| 220 | map->phys = NO_XIP; |
| 221 | map->read = physmap_addr_gpios_read; |
| 222 | map->copy_from = physmap_addr_gpios_copy_from; |
| 223 | map->write = physmap_addr_gpios_write; |
| 224 | map->copy_to = physmap_addr_gpios_copy_to; |
| 225 | |
| 226 | return 0; |
| 227 | } |
| 228 | #else |
| 229 | static int physmap_addr_gpios_map_init(struct map_info *map) |
| 230 | { |
| 231 | return -ENOTSUPP; |
| 232 | } |
| 233 | #endif |
| 234 | |
Boris Brezillon | 642b1e8 | 2018-10-19 09:49:04 +0200 | [diff] [blame] | 235 | #if IS_ENABLED(CONFIG_MTD_PHYSMAP_OF) |
| 236 | static const struct of_device_id of_flash_match[] = { |
| 237 | { |
| 238 | .compatible = "cfi-flash", |
| 239 | .data = "cfi_probe", |
| 240 | }, |
| 241 | { |
| 242 | /* |
| 243 | * FIXME: JEDEC chips can't be safely and reliably |
| 244 | * probed, although the mtd code gets it right in |
| 245 | * practice most of the time. We should use the |
| 246 | * vendor and device ids specified by the binding to |
| 247 | * bypass the heuristic probe code, but the mtd layer |
| 248 | * provides, at present, no interface for doing so |
| 249 | * :(. |
| 250 | */ |
| 251 | .compatible = "jedec-flash", |
| 252 | .data = "jedec_probe", |
| 253 | }, |
| 254 | { |
| 255 | .compatible = "mtd-ram", |
| 256 | .data = "map_ram", |
| 257 | }, |
| 258 | { |
| 259 | .compatible = "mtd-rom", |
| 260 | .data = "map_rom", |
| 261 | }, |
| 262 | { |
| 263 | .type = "rom", |
| 264 | .compatible = "direct-mapped" |
| 265 | }, |
| 266 | { /* sentinel */ }, |
| 267 | }; |
| 268 | MODULE_DEVICE_TABLE(of, of_flash_match); |
| 269 | |
| 270 | static const char * const of_default_part_probes[] = { |
| 271 | "cmdlinepart", "RedBoot", "ofpart", "ofoldpart", NULL |
| 272 | }; |
| 273 | |
| 274 | static const char * const *of_get_part_probes(struct platform_device *dev) |
| 275 | { |
| 276 | struct device_node *dp = dev->dev.of_node; |
| 277 | const char **res; |
| 278 | int count; |
| 279 | |
| 280 | count = of_property_count_strings(dp, "linux,part-probe"); |
| 281 | if (count < 0) |
| 282 | return of_default_part_probes; |
| 283 | |
| 284 | res = devm_kcalloc(&dev->dev, count + 1, sizeof(*res), GFP_KERNEL); |
| 285 | if (!res) |
| 286 | return NULL; |
| 287 | |
| 288 | count = of_property_read_string_array(dp, "linux,part-probe", res, |
| 289 | count); |
| 290 | if (count < 0) |
| 291 | return NULL; |
| 292 | |
| 293 | return res; |
| 294 | } |
| 295 | |
| 296 | static const char *of_select_probe_type(struct platform_device *dev) |
| 297 | { |
| 298 | struct device_node *dp = dev->dev.of_node; |
| 299 | const struct of_device_id *match; |
| 300 | const char *probe_type; |
| 301 | |
| 302 | match = of_match_device(of_flash_match, &dev->dev); |
Zeng Jingxiang | 8b740c0 | 2022-07-27 14:03:02 +0800 | [diff] [blame] | 303 | if (!match) |
| 304 | return NULL; |
| 305 | |
Boris Brezillon | 642b1e8 | 2018-10-19 09:49:04 +0200 | [diff] [blame] | 306 | probe_type = match->data; |
| 307 | if (probe_type) |
| 308 | return probe_type; |
| 309 | |
| 310 | dev_warn(&dev->dev, |
| 311 | "Device tree uses obsolete \"direct-mapped\" flash binding\n"); |
| 312 | |
| 313 | of_property_read_string(dp, "probe-type", &probe_type); |
| 314 | if (!probe_type) |
| 315 | return NULL; |
| 316 | |
| 317 | if (!strcmp(probe_type, "CFI")) { |
| 318 | probe_type = "cfi_probe"; |
| 319 | } else if (!strcmp(probe_type, "JEDEC")) { |
| 320 | probe_type = "jedec_probe"; |
| 321 | } else if (!strcmp(probe_type, "ROM")) { |
| 322 | probe_type = "map_rom"; |
| 323 | } else { |
| 324 | dev_warn(&dev->dev, |
| 325 | "obsolete_probe: don't know probe type '%s', mapping as rom\n", |
| 326 | probe_type); |
| 327 | probe_type = "map_rom"; |
| 328 | } |
| 329 | |
| 330 | return probe_type; |
| 331 | } |
| 332 | |
| 333 | static int physmap_flash_of_init(struct platform_device *dev) |
| 334 | { |
| 335 | struct physmap_flash_info *info = platform_get_drvdata(dev); |
| 336 | struct device_node *dp = dev->dev.of_node; |
| 337 | const char *mtd_name = NULL; |
| 338 | int err, swap = 0; |
| 339 | bool map_indirect; |
| 340 | unsigned int i; |
| 341 | u32 bankwidth; |
| 342 | |
| 343 | if (!dp) |
| 344 | return -EINVAL; |
| 345 | |
| 346 | info->probe_type = of_select_probe_type(dev); |
| 347 | |
| 348 | info->part_types = of_get_part_probes(dev); |
| 349 | if (!info->part_types) |
| 350 | return -ENOMEM; |
| 351 | |
| 352 | of_property_read_string(dp, "linux,mtd-name", &mtd_name); |
| 353 | |
| 354 | map_indirect = of_property_read_bool(dp, "no-unaligned-direct-access"); |
| 355 | |
| 356 | err = of_property_read_u32(dp, "bank-width", &bankwidth); |
| 357 | if (err) { |
| 358 | dev_err(&dev->dev, "Can't get bank width from device tree\n"); |
| 359 | return err; |
| 360 | } |
| 361 | |
| 362 | if (of_property_read_bool(dp, "big-endian")) |
| 363 | swap = CFI_BIG_ENDIAN; |
| 364 | else if (of_property_read_bool(dp, "little-endian")) |
| 365 | swap = CFI_LITTLE_ENDIAN; |
| 366 | |
| 367 | for (i = 0; i < info->nmaps; i++) { |
| 368 | info->maps[i].name = mtd_name; |
| 369 | info->maps[i].swap = swap; |
| 370 | info->maps[i].bankwidth = bankwidth; |
| 371 | info->maps[i].device_node = dp; |
| 372 | |
Serge Semin | b3e79e7 | 2020-09-20 14:14:44 +0300 | [diff] [blame] | 373 | err = of_flash_probe_bt1_rom(dev, dp, &info->maps[i]); |
| 374 | if (err) |
| 375 | return err; |
| 376 | |
Boris Brezillon | 642b1e8 | 2018-10-19 09:49:04 +0200 | [diff] [blame] | 377 | err = of_flash_probe_gemini(dev, dp, &info->maps[i]); |
| 378 | if (err) |
| 379 | return err; |
| 380 | |
Linus Walleij | 2aba2f2 | 2019-10-21 01:00:42 +0200 | [diff] [blame] | 381 | err = of_flash_probe_ixp4xx(dev, dp, &info->maps[i]); |
| 382 | if (err) |
| 383 | return err; |
| 384 | |
Boris Brezillon | 642b1e8 | 2018-10-19 09:49:04 +0200 | [diff] [blame] | 385 | err = of_flash_probe_versatile(dev, dp, &info->maps[i]); |
| 386 | if (err) |
| 387 | return err; |
| 388 | |
| 389 | /* |
| 390 | * On some platforms (e.g. MPC5200) a direct 1:1 mapping |
| 391 | * may cause problems with JFFS2 usage, as the local bus (LPB) |
| 392 | * doesn't support unaligned accesses as implemented in the |
| 393 | * JFFS2 code via memcpy(). By setting NO_XIP, the |
| 394 | * flash will not be exposed directly to the MTD users |
| 395 | * (e.g. JFFS2) any more. |
| 396 | */ |
| 397 | if (map_indirect) |
| 398 | info->maps[i].phys = NO_XIP; |
| 399 | } |
| 400 | |
| 401 | return 0; |
| 402 | } |
| 403 | #else /* IS_ENABLED(CONFIG_MTD_PHYSMAP_OF) */ |
| 404 | #define of_flash_match NULL |
| 405 | |
| 406 | static int physmap_flash_of_init(struct platform_device *dev) |
| 407 | { |
| 408 | return -ENOTSUPP; |
| 409 | } |
| 410 | #endif /* IS_ENABLED(CONFIG_MTD_PHYSMAP_OF) */ |
| 411 | |
Artem Bityutskiy | f39cf6c | 2013-03-12 10:32:52 +0200 | [diff] [blame] | 412 | static const char * const rom_probe_types[] = { |
Ricardo Ribalda Delgado | 99f732b | 2018-10-19 09:49:08 +0200 | [diff] [blame] | 413 | "cfi_probe", "jedec_probe", "qinfo_probe", "map_rom", |
Boris Brezillon | cb946bf | 2018-10-19 09:49:02 +0200 | [diff] [blame] | 414 | }; |
Artem Bityutskiy | f39cf6c | 2013-03-12 10:32:52 +0200 | [diff] [blame] | 415 | |
| 416 | static const char * const part_probe_types[] = { |
Boris Brezillon | cb946bf | 2018-10-19 09:49:02 +0200 | [diff] [blame] | 417 | "cmdlinepart", "RedBoot", "afs", NULL |
| 418 | }; |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 419 | |
Boris Brezillon | 642b1e8 | 2018-10-19 09:49:04 +0200 | [diff] [blame] | 420 | static int physmap_flash_pdata_init(struct platform_device *dev) |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 421 | { |
Boris Brezillon | 642b1e8 | 2018-10-19 09:49:04 +0200 | [diff] [blame] | 422 | struct physmap_flash_info *info = platform_get_drvdata(dev); |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 423 | struct physmap_flash_data *physmap_data; |
Boris Brezillon | 642b1e8 | 2018-10-19 09:49:04 +0200 | [diff] [blame] | 424 | unsigned int i; |
| 425 | int err; |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 426 | |
Jingoo Han | d20d5a5 | 2013-07-30 17:18:06 +0900 | [diff] [blame] | 427 | physmap_data = dev_get_platdata(&dev->dev); |
Boris Brezillon | cb946bf | 2018-10-19 09:49:02 +0200 | [diff] [blame] | 428 | if (!physmap_data) |
Boris Brezillon | 642b1e8 | 2018-10-19 09:49:04 +0200 | [diff] [blame] | 429 | return -EINVAL; |
| 430 | |
| 431 | info->probe_type = physmap_data->probe_type; |
| 432 | info->part_types = physmap_data->part_probe_types ? : part_probe_types; |
| 433 | info->parts = physmap_data->parts; |
| 434 | info->nparts = physmap_data->nr_parts; |
| 435 | |
| 436 | if (physmap_data->init) { |
| 437 | err = physmap_data->init(dev); |
| 438 | if (err) |
| 439 | return err; |
| 440 | } |
| 441 | |
| 442 | for (i = 0; i < info->nmaps; i++) { |
| 443 | info->maps[i].bankwidth = physmap_data->width; |
| 444 | info->maps[i].pfow_base = physmap_data->pfow_base; |
| 445 | info->maps[i].set_vpp = physmap_set_vpp; |
| 446 | } |
| 447 | |
| 448 | return 0; |
| 449 | } |
| 450 | |
| 451 | static int physmap_flash_probe(struct platform_device *dev) |
| 452 | { |
| 453 | struct physmap_flash_info *info; |
Boris Brezillon | 642b1e8 | 2018-10-19 09:49:04 +0200 | [diff] [blame] | 454 | int err = 0; |
| 455 | int i; |
| 456 | |
| 457 | if (!dev->dev.of_node && !dev_get_platdata(&dev->dev)) |
| 458 | return -EINVAL; |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 459 | |
Boris Brezillon | cb946bf | 2018-10-19 09:49:02 +0200 | [diff] [blame] | 460 | info = devm_kzalloc(&dev->dev, sizeof(*info), GFP_KERNEL); |
Boris Brezillon | 7abe530 | 2018-10-19 09:49:01 +0200 | [diff] [blame] | 461 | if (!info) |
| 462 | return -ENOMEM; |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 463 | |
Boris Brezillon | 51b436a | 2018-10-19 09:48:59 +0200 | [diff] [blame] | 464 | while (platform_get_resource(dev, IORESOURCE_MEM, info->nmaps)) |
| 465 | info->nmaps++; |
| 466 | |
| 467 | if (!info->nmaps) |
| 468 | return -ENODEV; |
| 469 | |
| 470 | info->maps = devm_kzalloc(&dev->dev, |
| 471 | sizeof(*info->maps) * info->nmaps, |
| 472 | GFP_KERNEL); |
| 473 | if (!info->maps) |
| 474 | return -ENOMEM; |
| 475 | |
| 476 | info->mtds = devm_kzalloc(&dev->dev, |
| 477 | sizeof(*info->mtds) * info->nmaps, |
| 478 | GFP_KERNEL); |
| 479 | if (!info->mtds) |
| 480 | return -ENOMEM; |
| 481 | |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 482 | platform_set_drvdata(dev, info); |
| 483 | |
Boris Brezillon | ba32ce9 | 2018-10-19 09:49:07 +0200 | [diff] [blame] | 484 | info->gpios = devm_gpiod_get_array_optional(&dev->dev, "addr", |
| 485 | GPIOD_OUT_LOW); |
| 486 | if (IS_ERR(info->gpios)) |
| 487 | return PTR_ERR(info->gpios); |
| 488 | |
| 489 | if (info->gpios && info->nmaps > 1) { |
| 490 | dev_err(&dev->dev, "addr-gpios only supported for nmaps == 1\n"); |
| 491 | return -EINVAL; |
| 492 | } |
| 493 | |
Geert Uytterhoeven | 0bc448b | 2019-12-09 14:48:23 +0100 | [diff] [blame] | 494 | pm_runtime_enable(&dev->dev); |
| 495 | pm_runtime_get_sync(&dev->dev); |
| 496 | |
Boris Brezillon | 642b1e8 | 2018-10-19 09:49:04 +0200 | [diff] [blame] | 497 | if (dev->dev.of_node) |
| 498 | err = physmap_flash_of_init(dev); |
| 499 | else |
| 500 | err = physmap_flash_pdata_init(dev); |
| 501 | |
Geert Uytterhoeven | 0bc448b | 2019-12-09 14:48:23 +0100 | [diff] [blame] | 502 | if (err) { |
| 503 | pm_runtime_put(&dev->dev); |
| 504 | pm_runtime_disable(&dev->dev); |
Boris Brezillon | 642b1e8 | 2018-10-19 09:49:04 +0200 | [diff] [blame] | 505 | return err; |
Geert Uytterhoeven | 0bc448b | 2019-12-09 14:48:23 +0100 | [diff] [blame] | 506 | } |
Boris Brezillon | 642b1e8 | 2018-10-19 09:49:04 +0200 | [diff] [blame] | 507 | |
Boris Brezillon | 51b436a | 2018-10-19 09:48:59 +0200 | [diff] [blame] | 508 | for (i = 0; i < info->nmaps; i++) { |
Boris Brezillon | c7f6dc6 | 2018-10-19 09:48:56 +0200 | [diff] [blame] | 509 | struct resource *res; |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 510 | |
Yangtao Li | e1666cf | 2023-07-07 12:06:22 +0800 | [diff] [blame] | 511 | info->maps[i].virt = devm_platform_get_and_ioremap_resource(dev, i, &res); |
Boris Brezillon | ea5bc54 | 2018-10-19 09:48:58 +0200 | [diff] [blame] | 512 | if (IS_ERR(info->maps[i].virt)) { |
| 513 | err = PTR_ERR(info->maps[i].virt); |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 514 | goto err_out; |
| 515 | } |
| 516 | |
Boris Brezillon | ea5bc54 | 2018-10-19 09:48:58 +0200 | [diff] [blame] | 517 | dev_notice(&dev->dev, "physmap platform flash device: %pR\n", |
| 518 | res); |
| 519 | |
Chris Packham | 9b21084 | 2020-08-24 14:57:44 +1200 | [diff] [blame] | 520 | if (!info->maps[i].name) |
| 521 | info->maps[i].name = dev_name(&dev->dev); |
Boris Brezillon | 642b1e8 | 2018-10-19 09:49:04 +0200 | [diff] [blame] | 522 | |
| 523 | if (!info->maps[i].phys) |
| 524 | info->maps[i].phys = res->start; |
| 525 | |
Boris Brezillon | ba32ce9 | 2018-10-19 09:49:07 +0200 | [diff] [blame] | 526 | info->win_order = get_bitmask_order(resource_size(res)) - 1; |
| 527 | info->maps[i].size = BIT(info->win_order + |
| 528 | (info->gpios ? |
| 529 | info->gpios->ndescs : 0)); |
| 530 | |
Boris Brezillon | 00142d6 | 2018-10-19 09:48:55 +0200 | [diff] [blame] | 531 | info->maps[i].map_priv_1 = (unsigned long)dev; |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 532 | |
Boris Brezillon | ba32ce9 | 2018-10-19 09:49:07 +0200 | [diff] [blame] | 533 | if (info->gpios) { |
| 534 | err = physmap_addr_gpios_map_init(&info->maps[i]); |
| 535 | if (err) |
| 536 | goto err_out; |
Boris Brezillon | ba32ce9 | 2018-10-19 09:49:07 +0200 | [diff] [blame] | 537 | } |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 538 | |
Linus Walleij | b3dd930 | 2018-11-27 21:53:57 +0100 | [diff] [blame] | 539 | #ifdef CONFIG_MTD_COMPLEX_MAPPINGS |
| 540 | /* |
| 541 | * Only use the simple_map implementation if map hooks are not |
| 542 | * implemented. Since map->read() is mandatory checking for its |
| 543 | * presence is enough. |
| 544 | */ |
| 545 | if (!info->maps[i].read) |
| 546 | simple_map_init(&info->maps[i]); |
| 547 | #else |
| 548 | simple_map_init(&info->maps[i]); |
| 549 | #endif |
| 550 | |
Ricardo Ribalda Delgado | 99f732b | 2018-10-19 09:49:08 +0200 | [diff] [blame] | 551 | if (info->probe_type) { |
Boris Brezillon | 642b1e8 | 2018-10-19 09:49:04 +0200 | [diff] [blame] | 552 | info->mtds[i] = do_map_probe(info->probe_type, |
Boris Brezillon | cb946bf | 2018-10-19 09:49:02 +0200 | [diff] [blame] | 553 | &info->maps[i]); |
Ricardo Ribalda Delgado | 99f732b | 2018-10-19 09:49:08 +0200 | [diff] [blame] | 554 | } else { |
| 555 | int j; |
| 556 | |
| 557 | for (j = 0; j < ARRAY_SIZE(rom_probe_types); j++) { |
| 558 | info->mtds[i] = do_map_probe(rom_probe_types[j], |
| 559 | &info->maps[i]); |
| 560 | if (info->mtds[i]) |
| 561 | break; |
| 562 | } |
Boris Brezillon | cb946bf | 2018-10-19 09:49:02 +0200 | [diff] [blame] | 563 | } |
Barry Song | 78ef7fa | 2010-01-15 15:50:14 +0800 | [diff] [blame] | 564 | |
Boris Brezillon | cb946bf | 2018-10-19 09:49:02 +0200 | [diff] [blame] | 565 | if (!info->mtds[i]) { |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 566 | dev_err(&dev->dev, "map_probe failed\n"); |
| 567 | err = -ENXIO; |
| 568 | goto err_out; |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 569 | } |
Boris Brezillon | 00142d6 | 2018-10-19 09:48:55 +0200 | [diff] [blame] | 570 | info->mtds[i]->dev.parent = &dev->dev; |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 571 | } |
| 572 | |
Boris Brezillon | 51b436a | 2018-10-19 09:48:59 +0200 | [diff] [blame] | 573 | if (info->nmaps == 1) { |
Boris Brezillon | 00142d6 | 2018-10-19 09:48:55 +0200 | [diff] [blame] | 574 | info->cmtd = info->mtds[0]; |
Boris Brezillon | c7f6dc6 | 2018-10-19 09:48:56 +0200 | [diff] [blame] | 575 | } else { |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 576 | /* |
| 577 | * We detected multiple devices. Concatenate them together. |
| 578 | */ |
Boris Brezillon | 51b436a | 2018-10-19 09:48:59 +0200 | [diff] [blame] | 579 | info->cmtd = mtd_concat_create(info->mtds, info->nmaps, |
| 580 | dev_name(&dev->dev)); |
Boris Brezillon | cb946bf | 2018-10-19 09:49:02 +0200 | [diff] [blame] | 581 | if (!info->cmtd) |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 582 | err = -ENXIO; |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 583 | } |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 584 | if (err) |
| 585 | goto err_out; |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 586 | |
Paul Parsons | 876fe76 | 2012-03-07 14:12:08 +0000 | [diff] [blame] | 587 | spin_lock_init(&info->vpp_lock); |
| 588 | |
Boris Brezillon | 642b1e8 | 2018-10-19 09:49:04 +0200 | [diff] [blame] | 589 | mtd_set_of_node(info->cmtd, dev->dev.of_node); |
| 590 | err = mtd_device_parse_register(info->cmtd, info->part_types, NULL, |
| 591 | info->parts, info->nparts); |
Boris Brezillon | 1e4f420 | 2018-10-19 09:49:00 +0200 | [diff] [blame] | 592 | if (err) |
| 593 | goto err_out; |
| 594 | |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 595 | return 0; |
| 596 | |
| 597 | err_out: |
| 598 | physmap_flash_remove(dev); |
| 599 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | } |
| 601 | |
Lennert Buytenhek | 17c2dae | 2006-09-21 23:16:48 +0200 | [diff] [blame] | 602 | #ifdef CONFIG_PM |
Lennert Buytenhek | 17c2dae | 2006-09-21 23:16:48 +0200 | [diff] [blame] | 603 | static void physmap_flash_shutdown(struct platform_device *dev) |
| 604 | { |
| 605 | struct physmap_flash_info *info = platform_get_drvdata(dev); |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 606 | int i; |
| 607 | |
Boris Brezillon | 51b436a | 2018-10-19 09:48:59 +0200 | [diff] [blame] | 608 | for (i = 0; i < info->nmaps && info->mtds[i]; i++) |
Boris Brezillon | 00142d6 | 2018-10-19 09:48:55 +0200 | [diff] [blame] | 609 | if (mtd_suspend(info->mtds[i]) == 0) |
| 610 | mtd_resume(info->mtds[i]); |
Lennert Buytenhek | 17c2dae | 2006-09-21 23:16:48 +0200 | [diff] [blame] | 611 | } |
akpm@linux-foundation.org | d547668 | 2008-02-03 12:56:03 -0800 | [diff] [blame] | 612 | #else |
akpm@linux-foundation.org | d547668 | 2008-02-03 12:56:03 -0800 | [diff] [blame] | 613 | #define physmap_flash_shutdown NULL |
Lennert Buytenhek | 17c2dae | 2006-09-21 23:16:48 +0200 | [diff] [blame] | 614 | #endif |
| 615 | |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 616 | static struct platform_driver physmap_flash_driver = { |
| 617 | .probe = physmap_flash_probe, |
| 618 | .remove = physmap_flash_remove, |
Lennert Buytenhek | 17c2dae | 2006-09-21 23:16:48 +0200 | [diff] [blame] | 619 | .shutdown = physmap_flash_shutdown, |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 620 | .driver = { |
| 621 | .name = "physmap-flash", |
Boris Brezillon | 642b1e8 | 2018-10-19 09:49:04 +0200 | [diff] [blame] | 622 | .of_match_table = of_flash_match, |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 623 | }, |
| 624 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | |
Mike Frysinger | dcb3e13 | 2008-12-01 14:23:40 -0800 | [diff] [blame] | 626 | #ifdef CONFIG_MTD_PHYSMAP_COMPAT |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 627 | static struct physmap_flash_data physmap_flash_data = { |
| 628 | .width = CONFIG_MTD_PHYSMAP_BANKWIDTH, |
| 629 | }; |
| 630 | |
| 631 | static struct resource physmap_flash_resource = { |
| 632 | .start = CONFIG_MTD_PHYSMAP_START, |
Sascha Hauer | 6d4f822 | 2006-06-27 14:38:15 +0100 | [diff] [blame] | 633 | .end = CONFIG_MTD_PHYSMAP_START + CONFIG_MTD_PHYSMAP_LEN - 1, |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 634 | .flags = IORESOURCE_MEM, |
| 635 | }; |
| 636 | |
| 637 | static struct platform_device physmap_flash = { |
| 638 | .name = "physmap-flash", |
| 639 | .id = 0, |
| 640 | .dev = { |
| 641 | .platform_data = &physmap_flash_data, |
| 642 | }, |
| 643 | .num_resources = 1, |
| 644 | .resource = &physmap_flash_resource, |
| 645 | }; |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 646 | #endif |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 647 | |
| 648 | static int __init physmap_init(void) |
| 649 | { |
| 650 | int err; |
| 651 | |
| 652 | err = platform_driver_register(&physmap_flash_driver); |
Mike Frysinger | dcb3e13 | 2008-12-01 14:23:40 -0800 | [diff] [blame] | 653 | #ifdef CONFIG_MTD_PHYSMAP_COMPAT |
H Hartley Sweeten | 1ca5d2f | 2010-04-02 17:46:30 -0500 | [diff] [blame] | 654 | if (err == 0) { |
| 655 | err = platform_device_register(&physmap_flash); |
| 656 | if (err) |
| 657 | platform_driver_unregister(&physmap_flash_driver); |
| 658 | } |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 659 | #endif |
| 660 | |
| 661 | return err; |
| 662 | } |
| 663 | |
| 664 | static void __exit physmap_exit(void) |
| 665 | { |
Mike Frysinger | dcb3e13 | 2008-12-01 14:23:40 -0800 | [diff] [blame] | 666 | #ifdef CONFIG_MTD_PHYSMAP_COMPAT |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 667 | platform_device_unregister(&physmap_flash); |
| 668 | #endif |
| 669 | platform_driver_unregister(&physmap_flash_driver); |
| 670 | } |
| 671 | |
| 672 | module_init(physmap_init); |
| 673 | module_exit(physmap_exit); |
| 674 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | MODULE_LICENSE("GPL"); |
| 676 | MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); |
Boris Brezillon | 642b1e8 | 2018-10-19 09:49:04 +0200 | [diff] [blame] | 677 | MODULE_AUTHOR("Vitaly Wool <vwool@ru.mvista.com>"); |
Boris Brezillon | ba32ce9 | 2018-10-19 09:49:07 +0200 | [diff] [blame] | 678 | MODULE_AUTHOR("Mike Frysinger <vapier@gentoo.org>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | MODULE_DESCRIPTION("Generic configurable MTD map driver"); |
Kay Sievers | 41d867c | 2008-04-18 13:44:26 -0700 | [diff] [blame] | 680 | |
| 681 | /* legacy platform drivers can't hotplug or coldplg */ |
Mike Frysinger | dcb3e13 | 2008-12-01 14:23:40 -0800 | [diff] [blame] | 682 | #ifndef CONFIG_MTD_PHYSMAP_COMPAT |
Kay Sievers | 41d867c | 2008-04-18 13:44:26 -0700 | [diff] [blame] | 683 | /* work with hotplug and coldplug */ |
| 684 | MODULE_ALIAS("platform:physmap-flash"); |
| 685 | #endif |