1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * linux/arch/arm/common/amba.c 4 * 5 * Copyright (C) 2003 Deep Blue Solutions Ltd, All Rights Reserved. 6 */ 7 #include <linux/module.h> 8 #include <linux/init.h> 9 #include <linux/device.h> 10 #include <linux/string.h> 11 #include <linux/slab.h> 12 #include <linux/io.h> 13 #include <linux/pm.h> 14 #include <linux/pm_runtime.h> 15 #include <linux/pm_domain.h> 16 #include <linux/amba/bus.h> 17 #include <linux/sizes.h> 18 #include <linux/limits.h> 19 #include <linux/clk/clk-conf.h> 20 #include <linux/platform_device.h> 21 #include <linux/reset.h> 22 #include <linux/of_irq.h> 23 #include <linux/of_device.h> 24 #include <linux/acpi.h> 25 #include <linux/iommu.h> 26 #include <linux/dma-map-ops.h> 27 28 #define to_amba_driver(d) container_of(d, struct amba_driver, drv) 29 30 /* called on periphid match and class 0x9 coresight device. */ 31 static int 32 amba_cs_uci_id_match(const struct amba_id *table, struct amba_device *dev) 33 { 34 int ret = 0; 35 struct amba_cs_uci_id *uci; 36 37 uci = table->data; 38 39 /* no table data or zero mask - return match on periphid */ 40 if (!uci || (uci->devarch_mask == 0)) 41 return 1; 42 43 /* test against read devtype and masked devarch value */ 44 ret = (dev->uci.devtype == uci->devtype) && 45 ((dev->uci.devarch & uci->devarch_mask) == uci->devarch); 46 return ret; 47 } 48 49 static const struct amba_id * 50 amba_lookup(const struct amba_id *table, struct amba_device *dev) 51 { 52 while (table->mask) { 53 if (((dev->periphid & table->mask) == table->id) && 54 ((dev->cid != CORESIGHT_CID) || 55 (amba_cs_uci_id_match(table, dev)))) 56 return table; 57 table++; 58 } 59 return NULL; 60 } 61 62 static int amba_get_enable_pclk(struct amba_device *pcdev) 63 { 64 int ret; 65 66 pcdev->pclk = clk_get(&pcdev->dev, "apb_pclk"); 67 if (IS_ERR(pcdev->pclk)) 68 return PTR_ERR(pcdev->pclk); 69 70 ret = clk_prepare_enable(pcdev->pclk); 71 if (ret) 72 clk_put(pcdev->pclk); 73 74 return ret; 75 } 76 77 static void amba_put_disable_pclk(struct amba_device *pcdev) 78 { 79 clk_disable_unprepare(pcdev->pclk); 80 clk_put(pcdev->pclk); 81 } 82 83 84 static ssize_t driver_override_show(struct device *_dev, 85 struct device_attribute *attr, char *buf) 86 { 87 struct amba_device *dev = to_amba_device(_dev); 88 ssize_t len; 89 90 device_lock(_dev); 91 len = sprintf(buf, "%s\n", dev->driver_override); 92 device_unlock(_dev); 93 return len; 94 } 95 96 static ssize_t driver_override_store(struct device *_dev, 97 struct device_attribute *attr, 98 const char *buf, size_t count) 99 { 100 struct amba_device *dev = to_amba_device(_dev); 101 int ret; 102 103 ret = driver_set_override(_dev, &dev->driver_override, buf, count); 104 if (ret) 105 return ret; 106 107 return count; 108 } 109 static DEVICE_ATTR_RW(driver_override); 110 111 #define amba_attr_func(name,fmt,arg...) \ 112 static ssize_t name##_show(struct device *_dev, \ 113 struct device_attribute *attr, char *buf) \ 114 { \ 115 struct amba_device *dev = to_amba_device(_dev); \ 116 return sprintf(buf, fmt, arg); \ 117 } \ 118 static DEVICE_ATTR_RO(name) 119 120 amba_attr_func(id, "%08x\n", dev->periphid); 121 amba_attr_func(resource, "\t%016llx\t%016llx\t%016lx\n", 122 (unsigned long long)dev->res.start, (unsigned long long)dev->res.end, 123 dev->res.flags); 124 125 static struct attribute *amba_dev_attrs[] = { 126 &dev_attr_id.attr, 127 &dev_attr_resource.attr, 128 &dev_attr_driver_override.attr, 129 NULL, 130 }; 131 ATTRIBUTE_GROUPS(amba_dev); 132 133 static int amba_match(struct device *dev, struct device_driver *drv) 134 { 135 struct amba_device *pcdev = to_amba_device(dev); 136 struct amba_driver *pcdrv = to_amba_driver(drv); 137 138 /* When driver_override is set, only bind to the matching driver */ 139 if (pcdev->driver_override) 140 return !strcmp(pcdev->driver_override, drv->name); 141 142 return amba_lookup(pcdrv->id_table, pcdev) != NULL; 143 } 144 145 static int amba_uevent(struct device *dev, struct kobj_uevent_env *env) 146 { 147 struct amba_device *pcdev = to_amba_device(dev); 148 int retval = 0; 149 150 retval = add_uevent_var(env, "AMBA_ID=%08x", pcdev->periphid); 151 if (retval) 152 return retval; 153 154 retval = add_uevent_var(env, "MODALIAS=amba:d%08X", pcdev->periphid); 155 return retval; 156 } 157 158 static int of_amba_device_decode_irq(struct amba_device *dev) 159 { 160 struct device_node *node = dev->dev.of_node; 161 int i, irq = 0; 162 163 if (IS_ENABLED(CONFIG_OF_IRQ) && node) { 164 /* Decode the IRQs and address ranges */ 165 for (i = 0; i < AMBA_NR_IRQS; i++) { 166 irq = of_irq_get(node, i); 167 if (irq < 0) { 168 if (irq == -EPROBE_DEFER) 169 return irq; 170 irq = 0; 171 } 172 173 dev->irq[i] = irq; 174 } 175 } 176 177 return 0; 178 } 179 180 /* 181 * These are the device model conversion veneers; they convert the 182 * device model structures to our more specific structures. 183 */ 184 static int amba_probe(struct device *dev) 185 { 186 struct amba_device *pcdev = to_amba_device(dev); 187 struct amba_driver *pcdrv = to_amba_driver(dev->driver); 188 const struct amba_id *id = amba_lookup(pcdrv->id_table, pcdev); 189 int ret; 190 191 do { 192 ret = of_amba_device_decode_irq(pcdev); 193 if (ret) 194 break; 195 196 ret = of_clk_set_defaults(dev->of_node, false); 197 if (ret < 0) 198 break; 199 200 ret = dev_pm_domain_attach(dev, true); 201 if (ret) 202 break; 203 204 ret = amba_get_enable_pclk(pcdev); 205 if (ret) { 206 dev_pm_domain_detach(dev, true); 207 break; 208 } 209 210 pm_runtime_get_noresume(dev); 211 pm_runtime_set_active(dev); 212 pm_runtime_enable(dev); 213 214 ret = pcdrv->probe(pcdev, id); 215 if (ret == 0) 216 break; 217 218 pm_runtime_disable(dev); 219 pm_runtime_set_suspended(dev); 220 pm_runtime_put_noidle(dev); 221 222 amba_put_disable_pclk(pcdev); 223 dev_pm_domain_detach(dev, true); 224 } while (0); 225 226 return ret; 227 } 228 229 static void amba_remove(struct device *dev) 230 { 231 struct amba_device *pcdev = to_amba_device(dev); 232 struct amba_driver *drv = to_amba_driver(dev->driver); 233 234 pm_runtime_get_sync(dev); 235 if (drv->remove) 236 drv->remove(pcdev); 237 pm_runtime_put_noidle(dev); 238 239 /* Undo the runtime PM settings in amba_probe() */ 240 pm_runtime_disable(dev); 241 pm_runtime_set_suspended(dev); 242 pm_runtime_put_noidle(dev); 243 244 amba_put_disable_pclk(pcdev); 245 dev_pm_domain_detach(dev, true); 246 } 247 248 static void amba_shutdown(struct device *dev) 249 { 250 struct amba_driver *drv; 251 252 if (!dev->driver) 253 return; 254 255 drv = to_amba_driver(dev->driver); 256 if (drv->shutdown) 257 drv->shutdown(to_amba_device(dev)); 258 } 259 260 static int amba_dma_configure(struct device *dev) 261 { 262 struct amba_driver *drv = to_amba_driver(dev->driver); 263 enum dev_dma_attr attr; 264 int ret = 0; 265 266 if (dev->of_node) { 267 ret = of_dma_configure(dev, dev->of_node, true); 268 } else if (has_acpi_companion(dev)) { 269 attr = acpi_get_dma_attr(to_acpi_device_node(dev->fwnode)); 270 ret = acpi_dma_configure(dev, attr); 271 } 272 273 if (!ret && !drv->driver_managed_dma) { 274 ret = iommu_device_use_default_domain(dev); 275 if (ret) 276 arch_teardown_dma_ops(dev); 277 } 278 279 return ret; 280 } 281 282 static void amba_dma_cleanup(struct device *dev) 283 { 284 struct amba_driver *drv = to_amba_driver(dev->driver); 285 286 if (!drv->driver_managed_dma) 287 iommu_device_unuse_default_domain(dev); 288 } 289 290 #ifdef CONFIG_PM 291 /* 292 * Hooks to provide runtime PM of the pclk (bus clock). It is safe to 293 * enable/disable the bus clock at runtime PM suspend/resume as this 294 * does not result in loss of context. 295 */ 296 static int amba_pm_runtime_suspend(struct device *dev) 297 { 298 struct amba_device *pcdev = to_amba_device(dev); 299 int ret = pm_generic_runtime_suspend(dev); 300 301 if (ret == 0 && dev->driver) { 302 if (pm_runtime_is_irq_safe(dev)) 303 clk_disable(pcdev->pclk); 304 else 305 clk_disable_unprepare(pcdev->pclk); 306 } 307 308 return ret; 309 } 310 311 static int amba_pm_runtime_resume(struct device *dev) 312 { 313 struct amba_device *pcdev = to_amba_device(dev); 314 int ret; 315 316 if (dev->driver) { 317 if (pm_runtime_is_irq_safe(dev)) 318 ret = clk_enable(pcdev->pclk); 319 else 320 ret = clk_prepare_enable(pcdev->pclk); 321 /* Failure is probably fatal to the system, but... */ 322 if (ret) 323 return ret; 324 } 325 326 return pm_generic_runtime_resume(dev); 327 } 328 #endif /* CONFIG_PM */ 329 330 static const struct dev_pm_ops amba_pm = { 331 .suspend = pm_generic_suspend, 332 .resume = pm_generic_resume, 333 .freeze = pm_generic_freeze, 334 .thaw = pm_generic_thaw, 335 .poweroff = pm_generic_poweroff, 336 .restore = pm_generic_restore, 337 SET_RUNTIME_PM_OPS( 338 amba_pm_runtime_suspend, 339 amba_pm_runtime_resume, 340 NULL 341 ) 342 }; 343 344 /* 345 * Primecells are part of the Advanced Microcontroller Bus Architecture, 346 * so we call the bus "amba". 347 * DMA configuration for platform and AMBA bus is same. So here we reuse 348 * platform's DMA config routine. 349 */ 350 struct bus_type amba_bustype = { 351 .name = "amba", 352 .dev_groups = amba_dev_groups, 353 .match = amba_match, 354 .uevent = amba_uevent, 355 .probe = amba_probe, 356 .remove = amba_remove, 357 .shutdown = amba_shutdown, 358 .dma_configure = amba_dma_configure, 359 .dma_cleanup = amba_dma_cleanup, 360 .pm = &amba_pm, 361 }; 362 EXPORT_SYMBOL_GPL(amba_bustype); 363 364 static int __init amba_init(void) 365 { 366 return bus_register(&amba_bustype); 367 } 368 369 postcore_initcall(amba_init); 370 371 /** 372 * amba_driver_register - register an AMBA device driver 373 * @drv: amba device driver structure 374 * 375 * Register an AMBA device driver with the Linux device model 376 * core. If devices pre-exist, the drivers probe function will 377 * be called. 378 */ 379 int amba_driver_register(struct amba_driver *drv) 380 { 381 if (!drv->probe) 382 return -EINVAL; 383 384 drv->drv.bus = &amba_bustype; 385 386 return driver_register(&drv->drv); 387 } 388 EXPORT_SYMBOL(amba_driver_register); 389 390 /** 391 * amba_driver_unregister - remove an AMBA device driver 392 * @drv: AMBA device driver structure to remove 393 * 394 * Unregister an AMBA device driver from the Linux device 395 * model. The device model will call the drivers remove function 396 * for each device the device driver is currently handling. 397 */ 398 void amba_driver_unregister(struct amba_driver *drv) 399 { 400 driver_unregister(&drv->drv); 401 } 402 EXPORT_SYMBOL(amba_driver_unregister); 403 404 static void amba_device_release(struct device *dev) 405 { 406 struct amba_device *d = to_amba_device(dev); 407 408 if (d->res.parent) 409 release_resource(&d->res); 410 kfree(d); 411 } 412 413 static int amba_read_periphid(struct amba_device *dev) 414 { 415 struct reset_control *rstc; 416 u32 size, pid, cid; 417 void __iomem *tmp; 418 int i, ret; 419 420 ret = dev_pm_domain_attach(&dev->dev, true); 421 if (ret) 422 goto err_out; 423 424 ret = amba_get_enable_pclk(dev); 425 if (ret) 426 goto err_pm; 427 428 /* 429 * Find reset control(s) of the amba bus and de-assert them. 430 */ 431 rstc = of_reset_control_array_get_optional_shared(dev->dev.of_node); 432 if (IS_ERR(rstc)) { 433 ret = PTR_ERR(rstc); 434 if (ret != -EPROBE_DEFER) 435 dev_err(&dev->dev, "can't get reset: %d\n", ret); 436 goto err_clk; 437 } 438 reset_control_deassert(rstc); 439 reset_control_put(rstc); 440 441 size = resource_size(&dev->res); 442 tmp = ioremap(dev->res.start, size); 443 if (!tmp) { 444 ret = -ENOMEM; 445 goto err_clk; 446 } 447 448 /* 449 * Read pid and cid based on size of resource 450 * they are located at end of region 451 */ 452 for (pid = 0, i = 0; i < 4; i++) 453 pid |= (readl(tmp + size - 0x20 + 4 * i) & 255) << (i * 8); 454 for (cid = 0, i = 0; i < 4; i++) 455 cid |= (readl(tmp + size - 0x10 + 4 * i) & 255) << (i * 8); 456 457 if (cid == CORESIGHT_CID) { 458 /* set the base to the start of the last 4k block */ 459 void __iomem *csbase = tmp + size - 4096; 460 461 dev->uci.devarch = readl(csbase + UCI_REG_DEVARCH_OFFSET); 462 dev->uci.devtype = readl(csbase + UCI_REG_DEVTYPE_OFFSET) & 0xff; 463 } 464 465 if (cid == AMBA_CID || cid == CORESIGHT_CID) { 466 dev->periphid = pid; 467 dev->cid = cid; 468 } 469 470 if (!dev->periphid) 471 ret = -ENODEV; 472 473 iounmap(tmp); 474 475 err_clk: 476 amba_put_disable_pclk(dev); 477 err_pm: 478 dev_pm_domain_detach(&dev->dev, true); 479 err_out: 480 return ret; 481 } 482 483 static int amba_device_try_add(struct amba_device *dev, struct resource *parent) 484 { 485 int ret; 486 487 ret = request_resource(parent, &dev->res); 488 if (ret) 489 goto err_out; 490 491 /* Hard-coded primecell ID instead of plug-n-play */ 492 if (dev->periphid != 0) 493 goto skip_probe; 494 495 ret = amba_read_periphid(dev); 496 if (ret) { 497 if (ret != -EPROBE_DEFER) { 498 amba_device_put(dev); 499 goto err_out; 500 } 501 goto err_release; 502 } 503 504 skip_probe: 505 ret = device_add(&dev->dev); 506 err_release: 507 if (ret) 508 release_resource(&dev->res); 509 err_out: 510 return ret; 511 } 512 513 /* 514 * Registration of AMBA device require reading its pid and cid registers. 515 * To do this, the device must be turned on (if it is a part of power domain) 516 * and have clocks enabled. However in some cases those resources might not be 517 * yet available. Returning EPROBE_DEFER is not a solution in such case, 518 * because callers don't handle this special error code. Instead such devices 519 * are added to the special list and their registration is retried from 520 * periodic worker, until all resources are available and registration succeeds. 521 */ 522 struct deferred_device { 523 struct amba_device *dev; 524 struct resource *parent; 525 struct list_head node; 526 }; 527 528 static LIST_HEAD(deferred_devices); 529 static DEFINE_MUTEX(deferred_devices_lock); 530 531 static void amba_deferred_retry_func(struct work_struct *dummy); 532 static DECLARE_DELAYED_WORK(deferred_retry_work, amba_deferred_retry_func); 533 534 #define DEFERRED_DEVICE_TIMEOUT (msecs_to_jiffies(5 * 1000)) 535 536 static int amba_deferred_retry(void) 537 { 538 struct deferred_device *ddev, *tmp; 539 540 mutex_lock(&deferred_devices_lock); 541 542 list_for_each_entry_safe(ddev, tmp, &deferred_devices, node) { 543 int ret = amba_device_try_add(ddev->dev, ddev->parent); 544 545 if (ret == -EPROBE_DEFER) 546 continue; 547 548 list_del_init(&ddev->node); 549 kfree(ddev); 550 } 551 552 mutex_unlock(&deferred_devices_lock); 553 554 return 0; 555 } 556 late_initcall(amba_deferred_retry); 557 558 static void amba_deferred_retry_func(struct work_struct *dummy) 559 { 560 amba_deferred_retry(); 561 562 if (!list_empty(&deferred_devices)) 563 schedule_delayed_work(&deferred_retry_work, 564 DEFERRED_DEVICE_TIMEOUT); 565 } 566 567 /** 568 * amba_device_add - add a previously allocated AMBA device structure 569 * @dev: AMBA device allocated by amba_device_alloc 570 * @parent: resource parent for this devices resources 571 * 572 * Claim the resource, and read the device cell ID if not already 573 * initialized. Register the AMBA device with the Linux device 574 * manager. 575 */ 576 int amba_device_add(struct amba_device *dev, struct resource *parent) 577 { 578 int ret = amba_device_try_add(dev, parent); 579 580 if (ret == -EPROBE_DEFER) { 581 struct deferred_device *ddev; 582 583 ddev = kmalloc(sizeof(*ddev), GFP_KERNEL); 584 if (!ddev) 585 return -ENOMEM; 586 587 ddev->dev = dev; 588 ddev->parent = parent; 589 ret = 0; 590 591 mutex_lock(&deferred_devices_lock); 592 593 if (list_empty(&deferred_devices)) 594 schedule_delayed_work(&deferred_retry_work, 595 DEFERRED_DEVICE_TIMEOUT); 596 list_add_tail(&ddev->node, &deferred_devices); 597 598 mutex_unlock(&deferred_devices_lock); 599 } 600 return ret; 601 } 602 EXPORT_SYMBOL_GPL(amba_device_add); 603 604 static void amba_device_initialize(struct amba_device *dev, const char *name) 605 { 606 device_initialize(&dev->dev); 607 if (name) 608 dev_set_name(&dev->dev, "%s", name); 609 dev->dev.release = amba_device_release; 610 dev->dev.bus = &amba_bustype; 611 dev->dev.dma_mask = &dev->dev.coherent_dma_mask; 612 dev->dev.dma_parms = &dev->dma_parms; 613 dev->res.name = dev_name(&dev->dev); 614 } 615 616 /** 617 * amba_device_alloc - allocate an AMBA device 618 * @name: sysfs name of the AMBA device 619 * @base: base of AMBA device 620 * @size: size of AMBA device 621 * 622 * Allocate and initialize an AMBA device structure. Returns %NULL 623 * on failure. 624 */ 625 struct amba_device *amba_device_alloc(const char *name, resource_size_t base, 626 size_t size) 627 { 628 struct amba_device *dev; 629 630 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 631 if (dev) { 632 amba_device_initialize(dev, name); 633 dev->res.start = base; 634 dev->res.end = base + size - 1; 635 dev->res.flags = IORESOURCE_MEM; 636 } 637 638 return dev; 639 } 640 EXPORT_SYMBOL_GPL(amba_device_alloc); 641 642 /** 643 * amba_device_register - register an AMBA device 644 * @dev: AMBA device to register 645 * @parent: parent memory resource 646 * 647 * Setup the AMBA device, reading the cell ID if present. 648 * Claim the resource, and register the AMBA device with 649 * the Linux device manager. 650 */ 651 int amba_device_register(struct amba_device *dev, struct resource *parent) 652 { 653 amba_device_initialize(dev, dev->dev.init_name); 654 dev->dev.init_name = NULL; 655 656 return amba_device_add(dev, parent); 657 } 658 EXPORT_SYMBOL(amba_device_register); 659 660 /** 661 * amba_device_put - put an AMBA device 662 * @dev: AMBA device to put 663 */ 664 void amba_device_put(struct amba_device *dev) 665 { 666 put_device(&dev->dev); 667 } 668 EXPORT_SYMBOL_GPL(amba_device_put); 669 670 /** 671 * amba_device_unregister - unregister an AMBA device 672 * @dev: AMBA device to remove 673 * 674 * Remove the specified AMBA device from the Linux device 675 * manager. All files associated with this object will be 676 * destroyed, and device drivers notified that the device has 677 * been removed. The AMBA device's resources including 678 * the amba_device structure will be freed once all 679 * references to it have been dropped. 680 */ 681 void amba_device_unregister(struct amba_device *dev) 682 { 683 device_unregister(&dev->dev); 684 } 685 EXPORT_SYMBOL(amba_device_unregister); 686 687 /** 688 * amba_request_regions - request all mem regions associated with device 689 * @dev: amba_device structure for device 690 * @name: name, or NULL to use driver name 691 */ 692 int amba_request_regions(struct amba_device *dev, const char *name) 693 { 694 int ret = 0; 695 u32 size; 696 697 if (!name) 698 name = dev->dev.driver->name; 699 700 size = resource_size(&dev->res); 701 702 if (!request_mem_region(dev->res.start, size, name)) 703 ret = -EBUSY; 704 705 return ret; 706 } 707 EXPORT_SYMBOL(amba_request_regions); 708 709 /** 710 * amba_release_regions - release mem regions associated with device 711 * @dev: amba_device structure for device 712 * 713 * Release regions claimed by a successful call to amba_request_regions. 714 */ 715 void amba_release_regions(struct amba_device *dev) 716 { 717 u32 size; 718 719 size = resource_size(&dev->res); 720 release_mem_region(dev->res.start, size); 721 } 722 EXPORT_SYMBOL(amba_release_regions); 723