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
amba_cs_uci_id_match(const struct amba_id * table,struct amba_device * dev)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 *
amba_lookup(const struct amba_id * table,struct amba_device * dev)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
amba_get_enable_pclk(struct amba_device * pcdev)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
amba_put_disable_pclk(struct amba_device * pcdev)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
driver_override_show(struct device * _dev,struct device_attribute * attr,char * buf)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
driver_override_store(struct device * _dev,struct device_attribute * attr,const char * buf,size_t count)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
amba_match(struct device * dev,struct device_driver * drv)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
amba_uevent(struct device * dev,struct kobj_uevent_env * env)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
of_amba_device_decode_irq(struct amba_device * dev)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 */
amba_probe(struct device * dev)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
amba_remove(struct device * dev)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
amba_shutdown(struct device * dev)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
amba_dma_configure(struct device * dev)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
amba_dma_cleanup(struct device * dev)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 */
amba_pm_runtime_suspend(struct device * dev)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
amba_pm_runtime_resume(struct device * dev)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
amba_init(void)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 */
amba_driver_register(struct amba_driver * drv)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 */
amba_driver_unregister(struct amba_driver * drv)398 void amba_driver_unregister(struct amba_driver *drv)
399 {
400 driver_unregister(&drv->drv);
401 }
402 EXPORT_SYMBOL(amba_driver_unregister);
403
amba_device_release(struct device * dev)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
amba_read_periphid(struct amba_device * dev)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
amba_device_try_add(struct amba_device * dev,struct resource * parent)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 goto err_release;
498
499 skip_probe:
500 ret = device_add(&dev->dev);
501 err_release:
502 if (ret)
503 release_resource(&dev->res);
504 err_out:
505 return ret;
506 }
507
508 /*
509 * Registration of AMBA device require reading its pid and cid registers.
510 * To do this, the device must be turned on (if it is a part of power domain)
511 * and have clocks enabled. However in some cases those resources might not be
512 * yet available. Returning EPROBE_DEFER is not a solution in such case,
513 * because callers don't handle this special error code. Instead such devices
514 * are added to the special list and their registration is retried from
515 * periodic worker, until all resources are available and registration succeeds.
516 */
517 struct deferred_device {
518 struct amba_device *dev;
519 struct resource *parent;
520 struct list_head node;
521 };
522
523 static LIST_HEAD(deferred_devices);
524 static DEFINE_MUTEX(deferred_devices_lock);
525
526 static void amba_deferred_retry_func(struct work_struct *dummy);
527 static DECLARE_DELAYED_WORK(deferred_retry_work, amba_deferred_retry_func);
528
529 #define DEFERRED_DEVICE_TIMEOUT (msecs_to_jiffies(5 * 1000))
530
amba_deferred_retry(void)531 static int amba_deferred_retry(void)
532 {
533 struct deferred_device *ddev, *tmp;
534
535 mutex_lock(&deferred_devices_lock);
536
537 list_for_each_entry_safe(ddev, tmp, &deferred_devices, node) {
538 int ret = amba_device_try_add(ddev->dev, ddev->parent);
539
540 if (ret == -EPROBE_DEFER)
541 continue;
542
543 list_del_init(&ddev->node);
544 amba_device_put(ddev->dev);
545 kfree(ddev);
546 }
547
548 mutex_unlock(&deferred_devices_lock);
549
550 return 0;
551 }
552 late_initcall(amba_deferred_retry);
553
amba_deferred_retry_func(struct work_struct * dummy)554 static void amba_deferred_retry_func(struct work_struct *dummy)
555 {
556 amba_deferred_retry();
557
558 if (!list_empty(&deferred_devices))
559 schedule_delayed_work(&deferred_retry_work,
560 DEFERRED_DEVICE_TIMEOUT);
561 }
562
563 /**
564 * amba_device_add - add a previously allocated AMBA device structure
565 * @dev: AMBA device allocated by amba_device_alloc
566 * @parent: resource parent for this devices resources
567 *
568 * Claim the resource, and read the device cell ID if not already
569 * initialized. Register the AMBA device with the Linux device
570 * manager.
571 */
amba_device_add(struct amba_device * dev,struct resource * parent)572 int amba_device_add(struct amba_device *dev, struct resource *parent)
573 {
574 int ret = amba_device_try_add(dev, parent);
575
576 if (ret == -EPROBE_DEFER) {
577 struct deferred_device *ddev;
578
579 ddev = kmalloc(sizeof(*ddev), GFP_KERNEL);
580 if (!ddev)
581 return -ENOMEM;
582
583 ddev->dev = dev;
584 ddev->parent = parent;
585 ret = 0;
586
587 mutex_lock(&deferred_devices_lock);
588
589 if (list_empty(&deferred_devices))
590 schedule_delayed_work(&deferred_retry_work,
591 DEFERRED_DEVICE_TIMEOUT);
592 list_add_tail(&ddev->node, &deferred_devices);
593
594 mutex_unlock(&deferred_devices_lock);
595 }
596 return ret;
597 }
598 EXPORT_SYMBOL_GPL(amba_device_add);
599
amba_device_initialize(struct amba_device * dev,const char * name)600 static void amba_device_initialize(struct amba_device *dev, const char *name)
601 {
602 device_initialize(&dev->dev);
603 if (name)
604 dev_set_name(&dev->dev, "%s", name);
605 dev->dev.release = amba_device_release;
606 dev->dev.bus = &amba_bustype;
607 dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
608 dev->dev.dma_parms = &dev->dma_parms;
609 dev->res.name = dev_name(&dev->dev);
610 }
611
612 /**
613 * amba_device_alloc - allocate an AMBA device
614 * @name: sysfs name of the AMBA device
615 * @base: base of AMBA device
616 * @size: size of AMBA device
617 *
618 * Allocate and initialize an AMBA device structure. Returns %NULL
619 * on failure.
620 */
amba_device_alloc(const char * name,resource_size_t base,size_t size)621 struct amba_device *amba_device_alloc(const char *name, resource_size_t base,
622 size_t size)
623 {
624 struct amba_device *dev;
625
626 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
627 if (dev) {
628 amba_device_initialize(dev, name);
629 dev->res.start = base;
630 dev->res.end = base + size - 1;
631 dev->res.flags = IORESOURCE_MEM;
632 }
633
634 return dev;
635 }
636 EXPORT_SYMBOL_GPL(amba_device_alloc);
637
638 /**
639 * amba_device_register - register an AMBA device
640 * @dev: AMBA device to register
641 * @parent: parent memory resource
642 *
643 * Setup the AMBA device, reading the cell ID if present.
644 * Claim the resource, and register the AMBA device with
645 * the Linux device manager.
646 */
amba_device_register(struct amba_device * dev,struct resource * parent)647 int amba_device_register(struct amba_device *dev, struct resource *parent)
648 {
649 amba_device_initialize(dev, dev->dev.init_name);
650 dev->dev.init_name = NULL;
651
652 return amba_device_add(dev, parent);
653 }
654 EXPORT_SYMBOL(amba_device_register);
655
656 /**
657 * amba_device_put - put an AMBA device
658 * @dev: AMBA device to put
659 */
amba_device_put(struct amba_device * dev)660 void amba_device_put(struct amba_device *dev)
661 {
662 put_device(&dev->dev);
663 }
664 EXPORT_SYMBOL_GPL(amba_device_put);
665
666 /**
667 * amba_device_unregister - unregister an AMBA device
668 * @dev: AMBA device to remove
669 *
670 * Remove the specified AMBA device from the Linux device
671 * manager. All files associated with this object will be
672 * destroyed, and device drivers notified that the device has
673 * been removed. The AMBA device's resources including
674 * the amba_device structure will be freed once all
675 * references to it have been dropped.
676 */
amba_device_unregister(struct amba_device * dev)677 void amba_device_unregister(struct amba_device *dev)
678 {
679 device_unregister(&dev->dev);
680 }
681 EXPORT_SYMBOL(amba_device_unregister);
682
683 /**
684 * amba_request_regions - request all mem regions associated with device
685 * @dev: amba_device structure for device
686 * @name: name, or NULL to use driver name
687 */
amba_request_regions(struct amba_device * dev,const char * name)688 int amba_request_regions(struct amba_device *dev, const char *name)
689 {
690 int ret = 0;
691 u32 size;
692
693 if (!name)
694 name = dev->dev.driver->name;
695
696 size = resource_size(&dev->res);
697
698 if (!request_mem_region(dev->res.start, size, name))
699 ret = -EBUSY;
700
701 return ret;
702 }
703 EXPORT_SYMBOL(amba_request_regions);
704
705 /**
706 * amba_release_regions - release mem regions associated with device
707 * @dev: amba_device structure for device
708 *
709 * Release regions claimed by a successful call to amba_request_regions.
710 */
amba_release_regions(struct amba_device * dev)711 void amba_release_regions(struct amba_device *dev)
712 {
713 u32 size;
714
715 size = resource_size(&dev->res);
716 release_mem_region(dev->res.start, size);
717 }
718 EXPORT_SYMBOL(amba_release_regions);
719