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