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/gpio/consumer.h> 42 43 #include "physmap-gemini.h" 44 #include "physmap-ixp4xx.h" 45 #include "physmap-versatile.h" 46 47 struct physmap_flash_info { 48 unsigned int nmaps; 49 struct mtd_info **mtds; 50 struct mtd_info *cmtd; 51 struct map_info *maps; 52 spinlock_t vpp_lock; 53 int vpp_refcnt; 54 const char *probe_type; 55 const char * const *part_types; 56 unsigned int nparts; 57 const struct mtd_partition *parts; 58 struct gpio_descs *gpios; 59 unsigned int gpio_values; 60 unsigned int win_order; 61 }; 62 63 static int physmap_flash_remove(struct platform_device *dev) 64 { 65 struct physmap_flash_info *info; 66 struct physmap_flash_data *physmap_data; 67 int i, err; 68 69 info = platform_get_drvdata(dev); 70 if (!info) 71 return 0; 72 73 if (info->cmtd) { 74 err = mtd_device_unregister(info->cmtd); 75 if (err) 76 return err; 77 78 if (info->cmtd != info->mtds[0]) 79 mtd_concat_destroy(info->cmtd); 80 } 81 82 for (i = 0; i < info->nmaps; i++) { 83 if (info->mtds[i]) 84 map_destroy(info->mtds[i]); 85 } 86 87 physmap_data = dev_get_platdata(&dev->dev); 88 if (physmap_data && physmap_data->exit) 89 physmap_data->exit(dev); 90 91 return 0; 92 } 93 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; 98 struct physmap_flash_info *info; 99 unsigned long flags; 100 101 pdev = (struct platform_device *)map->map_priv_1; 102 physmap_data = dev_get_platdata(&pdev->dev); 103 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); 118 } 119 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 } 136 137 info->gpio_values = ofs; 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 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); 303 probe_type = match->data; 304 if (probe_type) 305 return probe_type; 306 307 dev_warn(&dev->dev, 308 "Device tree uses obsolete \"direct-mapped\" flash binding\n"); 309 310 of_property_read_string(dp, "probe-type", &probe_type); 311 if (!probe_type) 312 return NULL; 313 314 if (!strcmp(probe_type, "CFI")) { 315 probe_type = "cfi_probe"; 316 } else if (!strcmp(probe_type, "JEDEC")) { 317 probe_type = "jedec_probe"; 318 } else if (!strcmp(probe_type, "ROM")) { 319 probe_type = "map_rom"; 320 } else { 321 dev_warn(&dev->dev, 322 "obsolete_probe: don't know probe type '%s', mapping as rom\n", 323 probe_type); 324 probe_type = "map_rom"; 325 } 326 327 return probe_type; 328 } 329 330 static int physmap_flash_of_init(struct platform_device *dev) 331 { 332 struct physmap_flash_info *info = platform_get_drvdata(dev); 333 struct device_node *dp = dev->dev.of_node; 334 const char *mtd_name = NULL; 335 int err, swap = 0; 336 bool map_indirect; 337 unsigned int i; 338 u32 bankwidth; 339 340 if (!dp) 341 return -EINVAL; 342 343 info->probe_type = of_select_probe_type(dev); 344 345 info->part_types = of_get_part_probes(dev); 346 if (!info->part_types) 347 return -ENOMEM; 348 349 of_property_read_string(dp, "linux,mtd-name", &mtd_name); 350 351 map_indirect = of_property_read_bool(dp, "no-unaligned-direct-access"); 352 353 err = of_property_read_u32(dp, "bank-width", &bankwidth); 354 if (err) { 355 dev_err(&dev->dev, "Can't get bank width from device tree\n"); 356 return err; 357 } 358 359 if (of_property_read_bool(dp, "big-endian")) 360 swap = CFI_BIG_ENDIAN; 361 else if (of_property_read_bool(dp, "little-endian")) 362 swap = CFI_LITTLE_ENDIAN; 363 364 for (i = 0; i < info->nmaps; i++) { 365 info->maps[i].name = mtd_name; 366 info->maps[i].swap = swap; 367 info->maps[i].bankwidth = bankwidth; 368 info->maps[i].device_node = dp; 369 370 err = of_flash_probe_gemini(dev, dp, &info->maps[i]); 371 if (err) 372 return err; 373 374 err = of_flash_probe_ixp4xx(dev, dp, &info->maps[i]); 375 if (err) 376 return err; 377 378 err = of_flash_probe_versatile(dev, dp, &info->maps[i]); 379 if (err) 380 return err; 381 382 /* 383 * On some platforms (e.g. MPC5200) a direct 1:1 mapping 384 * may cause problems with JFFS2 usage, as the local bus (LPB) 385 * doesn't support unaligned accesses as implemented in the 386 * JFFS2 code via memcpy(). By setting NO_XIP, the 387 * flash will not be exposed directly to the MTD users 388 * (e.g. JFFS2) any more. 389 */ 390 if (map_indirect) 391 info->maps[i].phys = NO_XIP; 392 } 393 394 return 0; 395 } 396 #else /* IS_ENABLED(CONFIG_MTD_PHYSMAP_OF) */ 397 #define of_flash_match NULL 398 399 static int physmap_flash_of_init(struct platform_device *dev) 400 { 401 return -ENOTSUPP; 402 } 403 #endif /* IS_ENABLED(CONFIG_MTD_PHYSMAP_OF) */ 404 405 static const char * const rom_probe_types[] = { 406 "cfi_probe", "jedec_probe", "qinfo_probe", "map_rom", 407 }; 408 409 static const char * const part_probe_types[] = { 410 "cmdlinepart", "RedBoot", "afs", NULL 411 }; 412 413 static int physmap_flash_pdata_init(struct platform_device *dev) 414 { 415 struct physmap_flash_info *info = platform_get_drvdata(dev); 416 struct physmap_flash_data *physmap_data; 417 unsigned int i; 418 int err; 419 420 physmap_data = dev_get_platdata(&dev->dev); 421 if (!physmap_data) 422 return -EINVAL; 423 424 info->probe_type = physmap_data->probe_type; 425 info->part_types = physmap_data->part_probe_types ? : part_probe_types; 426 info->parts = physmap_data->parts; 427 info->nparts = physmap_data->nr_parts; 428 429 if (physmap_data->init) { 430 err = physmap_data->init(dev); 431 if (err) 432 return err; 433 } 434 435 for (i = 0; i < info->nmaps; i++) { 436 info->maps[i].bankwidth = physmap_data->width; 437 info->maps[i].pfow_base = physmap_data->pfow_base; 438 info->maps[i].set_vpp = physmap_set_vpp; 439 } 440 441 return 0; 442 } 443 444 static int physmap_flash_probe(struct platform_device *dev) 445 { 446 struct physmap_flash_info *info; 447 int err = 0; 448 int i; 449 450 if (!dev->dev.of_node && !dev_get_platdata(&dev->dev)) 451 return -EINVAL; 452 453 info = devm_kzalloc(&dev->dev, sizeof(*info), GFP_KERNEL); 454 if (!info) 455 return -ENOMEM; 456 457 while (platform_get_resource(dev, IORESOURCE_MEM, info->nmaps)) 458 info->nmaps++; 459 460 if (!info->nmaps) 461 return -ENODEV; 462 463 info->maps = devm_kzalloc(&dev->dev, 464 sizeof(*info->maps) * info->nmaps, 465 GFP_KERNEL); 466 if (!info->maps) 467 return -ENOMEM; 468 469 info->mtds = devm_kzalloc(&dev->dev, 470 sizeof(*info->mtds) * info->nmaps, 471 GFP_KERNEL); 472 if (!info->mtds) 473 return -ENOMEM; 474 475 platform_set_drvdata(dev, info); 476 477 info->gpios = devm_gpiod_get_array_optional(&dev->dev, "addr", 478 GPIOD_OUT_LOW); 479 if (IS_ERR(info->gpios)) 480 return PTR_ERR(info->gpios); 481 482 if (info->gpios && info->nmaps > 1) { 483 dev_err(&dev->dev, "addr-gpios only supported for nmaps == 1\n"); 484 return -EINVAL; 485 } 486 487 if (dev->dev.of_node) 488 err = physmap_flash_of_init(dev); 489 else 490 err = physmap_flash_pdata_init(dev); 491 492 if (err) 493 return err; 494 495 for (i = 0; i < info->nmaps; i++) { 496 struct resource *res; 497 498 res = platform_get_resource(dev, IORESOURCE_MEM, i); 499 info->maps[i].virt = devm_ioremap_resource(&dev->dev, res); 500 if (IS_ERR(info->maps[i].virt)) { 501 err = PTR_ERR(info->maps[i].virt); 502 goto err_out; 503 } 504 505 dev_notice(&dev->dev, "physmap platform flash device: %pR\n", 506 res); 507 508 info->maps[i].name = dev_name(&dev->dev); 509 510 if (!info->maps[i].phys) 511 info->maps[i].phys = res->start; 512 513 info->win_order = get_bitmask_order(resource_size(res)) - 1; 514 info->maps[i].size = BIT(info->win_order + 515 (info->gpios ? 516 info->gpios->ndescs : 0)); 517 518 info->maps[i].map_priv_1 = (unsigned long)dev; 519 520 if (info->gpios) { 521 err = physmap_addr_gpios_map_init(&info->maps[i]); 522 if (err) 523 goto err_out; 524 } 525 526 #ifdef CONFIG_MTD_COMPLEX_MAPPINGS 527 /* 528 * Only use the simple_map implementation if map hooks are not 529 * implemented. Since map->read() is mandatory checking for its 530 * presence is enough. 531 */ 532 if (!info->maps[i].read) 533 simple_map_init(&info->maps[i]); 534 #else 535 simple_map_init(&info->maps[i]); 536 #endif 537 538 if (info->probe_type) { 539 info->mtds[i] = do_map_probe(info->probe_type, 540 &info->maps[i]); 541 } else { 542 int j; 543 544 for (j = 0; j < ARRAY_SIZE(rom_probe_types); j++) { 545 info->mtds[i] = do_map_probe(rom_probe_types[j], 546 &info->maps[i]); 547 if (info->mtds[i]) 548 break; 549 } 550 } 551 552 if (!info->mtds[i]) { 553 dev_err(&dev->dev, "map_probe failed\n"); 554 err = -ENXIO; 555 goto err_out; 556 } 557 info->mtds[i]->dev.parent = &dev->dev; 558 } 559 560 if (info->nmaps == 1) { 561 info->cmtd = info->mtds[0]; 562 } else { 563 /* 564 * We detected multiple devices. Concatenate them together. 565 */ 566 info->cmtd = mtd_concat_create(info->mtds, info->nmaps, 567 dev_name(&dev->dev)); 568 if (!info->cmtd) 569 err = -ENXIO; 570 } 571 if (err) 572 goto err_out; 573 574 spin_lock_init(&info->vpp_lock); 575 576 mtd_set_of_node(info->cmtd, dev->dev.of_node); 577 err = mtd_device_parse_register(info->cmtd, info->part_types, NULL, 578 info->parts, info->nparts); 579 if (err) 580 goto err_out; 581 582 return 0; 583 584 err_out: 585 physmap_flash_remove(dev); 586 return err; 587 } 588 589 #ifdef CONFIG_PM 590 static void physmap_flash_shutdown(struct platform_device *dev) 591 { 592 struct physmap_flash_info *info = platform_get_drvdata(dev); 593 int i; 594 595 for (i = 0; i < info->nmaps && info->mtds[i]; i++) 596 if (mtd_suspend(info->mtds[i]) == 0) 597 mtd_resume(info->mtds[i]); 598 } 599 #else 600 #define physmap_flash_shutdown NULL 601 #endif 602 603 static struct platform_driver physmap_flash_driver = { 604 .probe = physmap_flash_probe, 605 .remove = physmap_flash_remove, 606 .shutdown = physmap_flash_shutdown, 607 .driver = { 608 .name = "physmap-flash", 609 .of_match_table = of_flash_match, 610 }, 611 }; 612 613 #ifdef CONFIG_MTD_PHYSMAP_COMPAT 614 static struct physmap_flash_data physmap_flash_data = { 615 .width = CONFIG_MTD_PHYSMAP_BANKWIDTH, 616 }; 617 618 static struct resource physmap_flash_resource = { 619 .start = CONFIG_MTD_PHYSMAP_START, 620 .end = CONFIG_MTD_PHYSMAP_START + CONFIG_MTD_PHYSMAP_LEN - 1, 621 .flags = IORESOURCE_MEM, 622 }; 623 624 static struct platform_device physmap_flash = { 625 .name = "physmap-flash", 626 .id = 0, 627 .dev = { 628 .platform_data = &physmap_flash_data, 629 }, 630 .num_resources = 1, 631 .resource = &physmap_flash_resource, 632 }; 633 #endif 634 635 static int __init physmap_init(void) 636 { 637 int err; 638 639 err = platform_driver_register(&physmap_flash_driver); 640 #ifdef CONFIG_MTD_PHYSMAP_COMPAT 641 if (err == 0) { 642 err = platform_device_register(&physmap_flash); 643 if (err) 644 platform_driver_unregister(&physmap_flash_driver); 645 } 646 #endif 647 648 return err; 649 } 650 651 static void __exit physmap_exit(void) 652 { 653 #ifdef CONFIG_MTD_PHYSMAP_COMPAT 654 platform_device_unregister(&physmap_flash); 655 #endif 656 platform_driver_unregister(&physmap_flash_driver); 657 } 658 659 module_init(physmap_init); 660 module_exit(physmap_exit); 661 662 MODULE_LICENSE("GPL"); 663 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); 664 MODULE_AUTHOR("Vitaly Wool <vwool@ru.mvista.com>"); 665 MODULE_AUTHOR("Mike Frysinger <vapier@gentoo.org>"); 666 MODULE_DESCRIPTION("Generic configurable MTD map driver"); 667 668 /* legacy platform drivers can't hotplug or coldplg */ 669 #ifndef CONFIG_MTD_PHYSMAP_COMPAT 670 /* work with hotplug and coldplug */ 671 MODULE_ALIAS("platform:physmap-flash"); 672 #endif 673