Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  linux/arch/arm/common/amba.c
0004  *
0005  *  Copyright (C) 2003 Deep Blue Solutions Ltd, All Rights Reserved.
0006  */
0007 #include <linux/module.h>
0008 #include <linux/init.h>
0009 #include <linux/device.h>
0010 #include <linux/string.h>
0011 #include <linux/slab.h>
0012 #include <linux/io.h>
0013 #include <linux/pm.h>
0014 #include <linux/pm_runtime.h>
0015 #include <linux/pm_domain.h>
0016 #include <linux/amba/bus.h>
0017 #include <linux/sizes.h>
0018 #include <linux/limits.h>
0019 #include <linux/clk/clk-conf.h>
0020 #include <linux/platform_device.h>
0021 #include <linux/reset.h>
0022 #include <linux/of_irq.h>
0023 #include <linux/of_device.h>
0024 #include <linux/acpi.h>
0025 #include <linux/iommu.h>
0026 #include <linux/dma-map-ops.h>
0027 
0028 #define to_amba_driver(d)   container_of(d, struct amba_driver, drv)
0029 
0030 /* called on periphid match and class 0x9 coresight device. */
0031 static int
0032 amba_cs_uci_id_match(const struct amba_id *table, struct amba_device *dev)
0033 {
0034     int ret = 0;
0035     struct amba_cs_uci_id *uci;
0036 
0037     uci = table->data;
0038 
0039     /* no table data or zero mask - return match on periphid */
0040     if (!uci || (uci->devarch_mask == 0))
0041         return 1;
0042 
0043     /* test against read devtype and masked devarch value */
0044     ret = (dev->uci.devtype == uci->devtype) &&
0045         ((dev->uci.devarch & uci->devarch_mask) == uci->devarch);
0046     return ret;
0047 }
0048 
0049 static const struct amba_id *
0050 amba_lookup(const struct amba_id *table, struct amba_device *dev)
0051 {
0052     while (table->mask) {
0053         if (((dev->periphid & table->mask) == table->id) &&
0054             ((dev->cid != CORESIGHT_CID) ||
0055              (amba_cs_uci_id_match(table, dev))))
0056             return table;
0057         table++;
0058     }
0059     return NULL;
0060 }
0061 
0062 static int amba_get_enable_pclk(struct amba_device *pcdev)
0063 {
0064     int ret;
0065 
0066     pcdev->pclk = clk_get(&pcdev->dev, "apb_pclk");
0067     if (IS_ERR(pcdev->pclk))
0068         return PTR_ERR(pcdev->pclk);
0069 
0070     ret = clk_prepare_enable(pcdev->pclk);
0071     if (ret)
0072         clk_put(pcdev->pclk);
0073 
0074     return ret;
0075 }
0076 
0077 static void amba_put_disable_pclk(struct amba_device *pcdev)
0078 {
0079     clk_disable_unprepare(pcdev->pclk);
0080     clk_put(pcdev->pclk);
0081 }
0082 
0083 
0084 static ssize_t driver_override_show(struct device *_dev,
0085                     struct device_attribute *attr, char *buf)
0086 {
0087     struct amba_device *dev = to_amba_device(_dev);
0088     ssize_t len;
0089 
0090     device_lock(_dev);
0091     len = sprintf(buf, "%s\n", dev->driver_override);
0092     device_unlock(_dev);
0093     return len;
0094 }
0095 
0096 static ssize_t driver_override_store(struct device *_dev,
0097                      struct device_attribute *attr,
0098                      const char *buf, size_t count)
0099 {
0100     struct amba_device *dev = to_amba_device(_dev);
0101     int ret;
0102 
0103     ret = driver_set_override(_dev, &dev->driver_override, buf, count);
0104     if (ret)
0105         return ret;
0106 
0107     return count;
0108 }
0109 static DEVICE_ATTR_RW(driver_override);
0110 
0111 #define amba_attr_func(name,fmt,arg...)                 \
0112 static ssize_t name##_show(struct device *_dev,             \
0113                struct device_attribute *attr, char *buf)    \
0114 {                                   \
0115     struct amba_device *dev = to_amba_device(_dev);         \
0116     return sprintf(buf, fmt, arg);                  \
0117 }                                   \
0118 static DEVICE_ATTR_RO(name)
0119 
0120 amba_attr_func(id, "%08x\n", dev->periphid);
0121 amba_attr_func(resource, "\t%016llx\t%016llx\t%016lx\n",
0122      (unsigned long long)dev->res.start, (unsigned long long)dev->res.end,
0123      dev->res.flags);
0124 
0125 static struct attribute *amba_dev_attrs[] = {
0126     &dev_attr_id.attr,
0127     &dev_attr_resource.attr,
0128     &dev_attr_driver_override.attr,
0129     NULL,
0130 };
0131 ATTRIBUTE_GROUPS(amba_dev);
0132 
0133 static int amba_read_periphid(struct amba_device *dev)
0134 {
0135     struct reset_control *rstc;
0136     u32 size, pid, cid;
0137     void __iomem *tmp;
0138     int i, ret;
0139 
0140     ret = dev_pm_domain_attach(&dev->dev, true);
0141     if (ret) {
0142         dev_dbg(&dev->dev, "can't get PM domain: %d\n", ret);
0143         goto err_out;
0144     }
0145 
0146     ret = amba_get_enable_pclk(dev);
0147     if (ret) {
0148         dev_dbg(&dev->dev, "can't get pclk: %d\n", ret);
0149         goto err_pm;
0150     }
0151 
0152     /*
0153      * Find reset control(s) of the amba bus and de-assert them.
0154      */
0155     rstc = of_reset_control_array_get_optional_shared(dev->dev.of_node);
0156     if (IS_ERR(rstc)) {
0157         ret = PTR_ERR(rstc);
0158         if (ret != -EPROBE_DEFER)
0159             dev_err(&dev->dev, "can't get reset: %d\n", ret);
0160         goto err_clk;
0161     }
0162     reset_control_deassert(rstc);
0163     reset_control_put(rstc);
0164 
0165     size = resource_size(&dev->res);
0166     tmp = ioremap(dev->res.start, size);
0167     if (!tmp) {
0168         ret = -ENOMEM;
0169         goto err_clk;
0170     }
0171 
0172     /*
0173      * Read pid and cid based on size of resource
0174      * they are located at end of region
0175      */
0176     for (pid = 0, i = 0; i < 4; i++)
0177         pid |= (readl(tmp + size - 0x20 + 4 * i) & 255) << (i * 8);
0178     for (cid = 0, i = 0; i < 4; i++)
0179         cid |= (readl(tmp + size - 0x10 + 4 * i) & 255) << (i * 8);
0180 
0181     if (cid == CORESIGHT_CID) {
0182         /* set the base to the start of the last 4k block */
0183         void __iomem *csbase = tmp + size - 4096;
0184 
0185         dev->uci.devarch = readl(csbase + UCI_REG_DEVARCH_OFFSET);
0186         dev->uci.devtype = readl(csbase + UCI_REG_DEVTYPE_OFFSET) & 0xff;
0187     }
0188 
0189     if (cid == AMBA_CID || cid == CORESIGHT_CID) {
0190         dev->periphid = pid;
0191         dev->cid = cid;
0192     }
0193 
0194     if (!dev->periphid)
0195         ret = -ENODEV;
0196 
0197     iounmap(tmp);
0198 
0199 err_clk:
0200     amba_put_disable_pclk(dev);
0201 err_pm:
0202     dev_pm_domain_detach(&dev->dev, true);
0203 err_out:
0204     return ret;
0205 }
0206 
0207 static int amba_match(struct device *dev, struct device_driver *drv)
0208 {
0209     struct amba_device *pcdev = to_amba_device(dev);
0210     struct amba_driver *pcdrv = to_amba_driver(drv);
0211 
0212     mutex_lock(&pcdev->periphid_lock);
0213     if (!pcdev->periphid) {
0214         int ret = amba_read_periphid(pcdev);
0215 
0216         /*
0217          * Returning any error other than -EPROBE_DEFER from bus match
0218          * can cause driver registration failure. So, if there's a
0219          * permanent failure in reading pid and cid, simply map it to
0220          * -EPROBE_DEFER.
0221          */
0222         if (ret) {
0223             mutex_unlock(&pcdev->periphid_lock);
0224             return -EPROBE_DEFER;
0225         }
0226         dev_set_uevent_suppress(dev, false);
0227         kobject_uevent(&dev->kobj, KOBJ_ADD);
0228     }
0229     mutex_unlock(&pcdev->periphid_lock);
0230 
0231     /* When driver_override is set, only bind to the matching driver */
0232     if (pcdev->driver_override)
0233         return !strcmp(pcdev->driver_override, drv->name);
0234 
0235     return amba_lookup(pcdrv->id_table, pcdev) != NULL;
0236 }
0237 
0238 static int amba_uevent(struct device *dev, struct kobj_uevent_env *env)
0239 {
0240     struct amba_device *pcdev = to_amba_device(dev);
0241     int retval = 0;
0242 
0243     retval = add_uevent_var(env, "AMBA_ID=%08x", pcdev->periphid);
0244     if (retval)
0245         return retval;
0246 
0247     retval = add_uevent_var(env, "MODALIAS=amba:d%08X", pcdev->periphid);
0248     return retval;
0249 }
0250 
0251 static int of_amba_device_decode_irq(struct amba_device *dev)
0252 {
0253     struct device_node *node = dev->dev.of_node;
0254     int i, irq = 0;
0255 
0256     if (IS_ENABLED(CONFIG_OF_IRQ) && node) {
0257         /* Decode the IRQs and address ranges */
0258         for (i = 0; i < AMBA_NR_IRQS; i++) {
0259             irq = of_irq_get(node, i);
0260             if (irq < 0) {
0261                 if (irq == -EPROBE_DEFER)
0262                     return irq;
0263                 irq = 0;
0264             }
0265 
0266             dev->irq[i] = irq;
0267         }
0268     }
0269 
0270     return 0;
0271 }
0272 
0273 /*
0274  * These are the device model conversion veneers; they convert the
0275  * device model structures to our more specific structures.
0276  */
0277 static int amba_probe(struct device *dev)
0278 {
0279     struct amba_device *pcdev = to_amba_device(dev);
0280     struct amba_driver *pcdrv = to_amba_driver(dev->driver);
0281     const struct amba_id *id = amba_lookup(pcdrv->id_table, pcdev);
0282     int ret;
0283 
0284     do {
0285         ret = of_amba_device_decode_irq(pcdev);
0286         if (ret)
0287             break;
0288 
0289         ret = of_clk_set_defaults(dev->of_node, false);
0290         if (ret < 0)
0291             break;
0292 
0293         ret = dev_pm_domain_attach(dev, true);
0294         if (ret)
0295             break;
0296 
0297         ret = amba_get_enable_pclk(pcdev);
0298         if (ret) {
0299             dev_pm_domain_detach(dev, true);
0300             break;
0301         }
0302 
0303         pm_runtime_get_noresume(dev);
0304         pm_runtime_set_active(dev);
0305         pm_runtime_enable(dev);
0306 
0307         ret = pcdrv->probe(pcdev, id);
0308         if (ret == 0)
0309             break;
0310 
0311         pm_runtime_disable(dev);
0312         pm_runtime_set_suspended(dev);
0313         pm_runtime_put_noidle(dev);
0314 
0315         amba_put_disable_pclk(pcdev);
0316         dev_pm_domain_detach(dev, true);
0317     } while (0);
0318 
0319     return ret;
0320 }
0321 
0322 static void amba_remove(struct device *dev)
0323 {
0324     struct amba_device *pcdev = to_amba_device(dev);
0325     struct amba_driver *drv = to_amba_driver(dev->driver);
0326 
0327     pm_runtime_get_sync(dev);
0328     if (drv->remove)
0329         drv->remove(pcdev);
0330     pm_runtime_put_noidle(dev);
0331 
0332     /* Undo the runtime PM settings in amba_probe() */
0333     pm_runtime_disable(dev);
0334     pm_runtime_set_suspended(dev);
0335     pm_runtime_put_noidle(dev);
0336 
0337     amba_put_disable_pclk(pcdev);
0338     dev_pm_domain_detach(dev, true);
0339 }
0340 
0341 static void amba_shutdown(struct device *dev)
0342 {
0343     struct amba_driver *drv;
0344 
0345     if (!dev->driver)
0346         return;
0347 
0348     drv = to_amba_driver(dev->driver);
0349     if (drv->shutdown)
0350         drv->shutdown(to_amba_device(dev));
0351 }
0352 
0353 static int amba_dma_configure(struct device *dev)
0354 {
0355     struct amba_driver *drv = to_amba_driver(dev->driver);
0356     enum dev_dma_attr attr;
0357     int ret = 0;
0358 
0359     if (dev->of_node) {
0360         ret = of_dma_configure(dev, dev->of_node, true);
0361     } else if (has_acpi_companion(dev)) {
0362         attr = acpi_get_dma_attr(to_acpi_device_node(dev->fwnode));
0363         ret = acpi_dma_configure(dev, attr);
0364     }
0365 
0366     if (!ret && !drv->driver_managed_dma) {
0367         ret = iommu_device_use_default_domain(dev);
0368         if (ret)
0369             arch_teardown_dma_ops(dev);
0370     }
0371 
0372     return ret;
0373 }
0374 
0375 static void amba_dma_cleanup(struct device *dev)
0376 {
0377     struct amba_driver *drv = to_amba_driver(dev->driver);
0378 
0379     if (!drv->driver_managed_dma)
0380         iommu_device_unuse_default_domain(dev);
0381 }
0382 
0383 #ifdef CONFIG_PM
0384 /*
0385  * Hooks to provide runtime PM of the pclk (bus clock).  It is safe to
0386  * enable/disable the bus clock at runtime PM suspend/resume as this
0387  * does not result in loss of context.
0388  */
0389 static int amba_pm_runtime_suspend(struct device *dev)
0390 {
0391     struct amba_device *pcdev = to_amba_device(dev);
0392     int ret = pm_generic_runtime_suspend(dev);
0393 
0394     if (ret == 0 && dev->driver) {
0395         if (pm_runtime_is_irq_safe(dev))
0396             clk_disable(pcdev->pclk);
0397         else
0398             clk_disable_unprepare(pcdev->pclk);
0399     }
0400 
0401     return ret;
0402 }
0403 
0404 static int amba_pm_runtime_resume(struct device *dev)
0405 {
0406     struct amba_device *pcdev = to_amba_device(dev);
0407     int ret;
0408 
0409     if (dev->driver) {
0410         if (pm_runtime_is_irq_safe(dev))
0411             ret = clk_enable(pcdev->pclk);
0412         else
0413             ret = clk_prepare_enable(pcdev->pclk);
0414         /* Failure is probably fatal to the system, but... */
0415         if (ret)
0416             return ret;
0417     }
0418 
0419     return pm_generic_runtime_resume(dev);
0420 }
0421 #endif /* CONFIG_PM */
0422 
0423 static const struct dev_pm_ops amba_pm = {
0424     .suspend    = pm_generic_suspend,
0425     .resume     = pm_generic_resume,
0426     .freeze     = pm_generic_freeze,
0427     .thaw       = pm_generic_thaw,
0428     .poweroff   = pm_generic_poweroff,
0429     .restore    = pm_generic_restore,
0430     SET_RUNTIME_PM_OPS(
0431         amba_pm_runtime_suspend,
0432         amba_pm_runtime_resume,
0433         NULL
0434     )
0435 };
0436 
0437 /*
0438  * Primecells are part of the Advanced Microcontroller Bus Architecture,
0439  * so we call the bus "amba".
0440  * DMA configuration for platform and AMBA bus is same. So here we reuse
0441  * platform's DMA config routine.
0442  */
0443 struct bus_type amba_bustype = {
0444     .name       = "amba",
0445     .dev_groups = amba_dev_groups,
0446     .match      = amba_match,
0447     .uevent     = amba_uevent,
0448     .probe      = amba_probe,
0449     .remove     = amba_remove,
0450     .shutdown   = amba_shutdown,
0451     .dma_configure  = amba_dma_configure,
0452     .dma_cleanup    = amba_dma_cleanup,
0453     .pm     = &amba_pm,
0454 };
0455 EXPORT_SYMBOL_GPL(amba_bustype);
0456 
0457 static int __init amba_init(void)
0458 {
0459     return bus_register(&amba_bustype);
0460 }
0461 
0462 postcore_initcall(amba_init);
0463 
0464 static int amba_proxy_probe(struct amba_device *adev,
0465                 const struct amba_id *id)
0466 {
0467     WARN(1, "Stub driver should never match any device.\n");
0468     return -ENODEV;
0469 }
0470 
0471 static const struct amba_id amba_stub_drv_ids[] = {
0472     { 0, 0 },
0473 };
0474 
0475 static struct amba_driver amba_proxy_drv = {
0476     .drv = {
0477         .name = "amba-proxy",
0478     },
0479     .probe = amba_proxy_probe,
0480     .id_table = amba_stub_drv_ids,
0481 };
0482 
0483 static int __init amba_stub_drv_init(void)
0484 {
0485     if (!IS_ENABLED(CONFIG_MODULES))
0486         return 0;
0487 
0488     /*
0489      * The amba_match() function will get called only if there is at least
0490      * one amba driver registered. If all amba drivers are modules and are
0491      * only loaded based on uevents, then we'll hit a chicken-and-egg
0492      * situation where amba_match() is waiting on drivers and drivers are
0493      * waiting on amba_match(). So, register a stub driver to make sure
0494      * amba_match() is called even if no amba driver has been registered.
0495      */
0496     return amba_driver_register(&amba_proxy_drv);
0497 }
0498 late_initcall_sync(amba_stub_drv_init);
0499 
0500 /**
0501  *  amba_driver_register - register an AMBA device driver
0502  *  @drv: amba device driver structure
0503  *
0504  *  Register an AMBA device driver with the Linux device model
0505  *  core.  If devices pre-exist, the drivers probe function will
0506  *  be called.
0507  */
0508 int amba_driver_register(struct amba_driver *drv)
0509 {
0510     if (!drv->probe)
0511         return -EINVAL;
0512 
0513     drv->drv.bus = &amba_bustype;
0514 
0515     return driver_register(&drv->drv);
0516 }
0517 EXPORT_SYMBOL(amba_driver_register);
0518 
0519 /**
0520  *  amba_driver_unregister - remove an AMBA device driver
0521  *  @drv: AMBA device driver structure to remove
0522  *
0523  *  Unregister an AMBA device driver from the Linux device
0524  *  model.  The device model will call the drivers remove function
0525  *  for each device the device driver is currently handling.
0526  */
0527 void amba_driver_unregister(struct amba_driver *drv)
0528 {
0529     driver_unregister(&drv->drv);
0530 }
0531 EXPORT_SYMBOL(amba_driver_unregister);
0532 
0533 static void amba_device_release(struct device *dev)
0534 {
0535     struct amba_device *d = to_amba_device(dev);
0536 
0537     if (d->res.parent)
0538         release_resource(&d->res);
0539     mutex_destroy(&d->periphid_lock);
0540     kfree(d);
0541 }
0542 
0543 /**
0544  *  amba_device_add - add a previously allocated AMBA device structure
0545  *  @dev: AMBA device allocated by amba_device_alloc
0546  *  @parent: resource parent for this devices resources
0547  *
0548  *  Claim the resource, and read the device cell ID if not already
0549  *  initialized.  Register the AMBA device with the Linux device
0550  *  manager.
0551  */
0552 int amba_device_add(struct amba_device *dev, struct resource *parent)
0553 {
0554     int ret;
0555 
0556     ret = request_resource(parent, &dev->res);
0557     if (ret)
0558         return ret;
0559 
0560     /* If primecell ID isn't hard-coded, figure it out */
0561     if (!dev->periphid) {
0562         /*
0563          * AMBA device uevents require reading its pid and cid
0564          * registers.  To do this, the device must be on, clocked and
0565          * out of reset.  However in some cases those resources might
0566          * not yet be available.  If that's the case, we suppress the
0567          * generation of uevents until we can read the pid and cid
0568          * registers.  See also amba_match().
0569          */
0570         if (amba_read_periphid(dev))
0571             dev_set_uevent_suppress(&dev->dev, true);
0572     }
0573 
0574     ret = device_add(&dev->dev);
0575     if (ret)
0576         release_resource(&dev->res);
0577 
0578     return ret;
0579 }
0580 EXPORT_SYMBOL_GPL(amba_device_add);
0581 
0582 static void amba_device_initialize(struct amba_device *dev, const char *name)
0583 {
0584     device_initialize(&dev->dev);
0585     if (name)
0586         dev_set_name(&dev->dev, "%s", name);
0587     dev->dev.release = amba_device_release;
0588     dev->dev.bus = &amba_bustype;
0589     dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
0590     dev->dev.dma_parms = &dev->dma_parms;
0591     dev->res.name = dev_name(&dev->dev);
0592     mutex_init(&dev->periphid_lock);
0593 }
0594 
0595 /**
0596  *  amba_device_alloc - allocate an AMBA device
0597  *  @name: sysfs name of the AMBA device
0598  *  @base: base of AMBA device
0599  *  @size: size of AMBA device
0600  *
0601  *  Allocate and initialize an AMBA device structure.  Returns %NULL
0602  *  on failure.
0603  */
0604 struct amba_device *amba_device_alloc(const char *name, resource_size_t base,
0605     size_t size)
0606 {
0607     struct amba_device *dev;
0608 
0609     dev = kzalloc(sizeof(*dev), GFP_KERNEL);
0610     if (dev) {
0611         amba_device_initialize(dev, name);
0612         dev->res.start = base;
0613         dev->res.end = base + size - 1;
0614         dev->res.flags = IORESOURCE_MEM;
0615     }
0616 
0617     return dev;
0618 }
0619 EXPORT_SYMBOL_GPL(amba_device_alloc);
0620 
0621 /**
0622  *  amba_device_register - register an AMBA device
0623  *  @dev: AMBA device to register
0624  *  @parent: parent memory resource
0625  *
0626  *  Setup the AMBA device, reading the cell ID if present.
0627  *  Claim the resource, and register the AMBA device with
0628  *  the Linux device manager.
0629  */
0630 int amba_device_register(struct amba_device *dev, struct resource *parent)
0631 {
0632     amba_device_initialize(dev, dev->dev.init_name);
0633     dev->dev.init_name = NULL;
0634 
0635     return amba_device_add(dev, parent);
0636 }
0637 EXPORT_SYMBOL(amba_device_register);
0638 
0639 /**
0640  *  amba_device_put - put an AMBA device
0641  *  @dev: AMBA device to put
0642  */
0643 void amba_device_put(struct amba_device *dev)
0644 {
0645     put_device(&dev->dev);
0646 }
0647 EXPORT_SYMBOL_GPL(amba_device_put);
0648 
0649 /**
0650  *  amba_device_unregister - unregister an AMBA device
0651  *  @dev: AMBA device to remove
0652  *
0653  *  Remove the specified AMBA device from the Linux device
0654  *  manager.  All files associated with this object will be
0655  *  destroyed, and device drivers notified that the device has
0656  *  been removed.  The AMBA device's resources including
0657  *  the amba_device structure will be freed once all
0658  *  references to it have been dropped.
0659  */
0660 void amba_device_unregister(struct amba_device *dev)
0661 {
0662     device_unregister(&dev->dev);
0663 }
0664 EXPORT_SYMBOL(amba_device_unregister);
0665 
0666 /**
0667  *  amba_request_regions - request all mem regions associated with device
0668  *  @dev: amba_device structure for device
0669  *  @name: name, or NULL to use driver name
0670  */
0671 int amba_request_regions(struct amba_device *dev, const char *name)
0672 {
0673     int ret = 0;
0674     u32 size;
0675 
0676     if (!name)
0677         name = dev->dev.driver->name;
0678 
0679     size = resource_size(&dev->res);
0680 
0681     if (!request_mem_region(dev->res.start, size, name))
0682         ret = -EBUSY;
0683 
0684     return ret;
0685 }
0686 EXPORT_SYMBOL(amba_request_regions);
0687 
0688 /**
0689  *  amba_release_regions - release mem regions associated with device
0690  *  @dev: amba_device structure for device
0691  *
0692  *  Release regions claimed by a successful call to amba_request_regions.
0693  */
0694 void amba_release_regions(struct amba_device *dev)
0695 {
0696     u32 size;
0697 
0698     size = resource_size(&dev->res);
0699     release_mem_region(dev->res.start, size);
0700 }
0701 EXPORT_SYMBOL(amba_release_regions);