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