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