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