xref: /linux/drivers/acpi/arm64/amba.c (revision ca5a75df36dd54fd7ca470a74581ef1d27edaaab)
1fc001b36SSudeep Holla // SPDX-License-Identifier: GPL-2.0-only
2fc001b36SSudeep Holla 
3fc001b36SSudeep Holla /*
4fc001b36SSudeep Holla  * ACPI support for platform bus type.
5fc001b36SSudeep Holla  *
6fc001b36SSudeep Holla  * Copyright (C) 2015, Linaro Ltd
7fc001b36SSudeep Holla  * Author: Graeme Gregory <graeme.gregory@linaro.org>
8fc001b36SSudeep Holla  */
9fc001b36SSudeep Holla 
10fc001b36SSudeep Holla #include <linux/acpi.h>
11fc001b36SSudeep Holla #include <linux/amba/bus.h>
12fc001b36SSudeep Holla #include <linux/clkdev.h>
13fc001b36SSudeep Holla #include <linux/clk-provider.h>
14fc001b36SSudeep Holla #include <linux/device.h>
15fc001b36SSudeep Holla #include <linux/err.h>
16fc001b36SSudeep Holla #include <linux/ioport.h>
17fc001b36SSudeep Holla #include <linux/kernel.h>
18fc001b36SSudeep Holla #include <linux/module.h>
19fc001b36SSudeep Holla 
20fc001b36SSudeep Holla #include "init.h"
21fc001b36SSudeep Holla 
22fc001b36SSudeep Holla static const struct acpi_device_id amba_id_list[] = {
23fc001b36SSudeep Holla 	{"ARMH0061", 0}, /* PL061 GPIO Device */
24fc001b36SSudeep Holla 	{"ARMH0330", 0}, /* ARM DMA Controller DMA-330 */
25fc001b36SSudeep Holla 	{"ARMHC501", 0}, /* ARM CoreSight ETR */
26fc001b36SSudeep Holla 	{"ARMHC502", 0}, /* ARM CoreSight STM */
27fc001b36SSudeep Holla 	{"ARMHC503", 0}, /* ARM CoreSight Debug */
28fc001b36SSudeep Holla 	{"ARMHC979", 0}, /* ARM CoreSight TPIU */
29fc001b36SSudeep Holla 	{"ARMHC97C", 0}, /* ARM CoreSight SoC-400 TMC, SoC-600 ETF/ETB */
30fc001b36SSudeep Holla 	{"ARMHC98D", 0}, /* ARM CoreSight Dynamic Replicator */
31fc001b36SSudeep Holla 	{"ARMHC9CA", 0}, /* ARM CoreSight CATU */
32fc001b36SSudeep Holla 	{"ARMHC9FF", 0}, /* ARM CoreSight Dynamic Funnel */
33fc001b36SSudeep Holla 	{"", 0},
34fc001b36SSudeep Holla };
35fc001b36SSudeep Holla 
36fc001b36SSudeep Holla static void amba_register_dummy_clk(void)
37fc001b36SSudeep Holla {
38fc001b36SSudeep Holla 	static struct clk *amba_dummy_clk;
39fc001b36SSudeep Holla 
40fc001b36SSudeep Holla 	/* If clock already registered */
41fc001b36SSudeep Holla 	if (amba_dummy_clk)
42fc001b36SSudeep Holla 		return;
43fc001b36SSudeep Holla 
44fc001b36SSudeep Holla 	amba_dummy_clk = clk_register_fixed_rate(NULL, "apb_pclk", NULL, 0, 0);
45fc001b36SSudeep Holla 	clk_register_clkdev(amba_dummy_clk, "apb_pclk", NULL);
46fc001b36SSudeep Holla }
47fc001b36SSudeep Holla 
48fc001b36SSudeep Holla static int amba_handler_attach(struct acpi_device *adev,
49fc001b36SSudeep Holla 				const struct acpi_device_id *id)
50fc001b36SSudeep Holla {
51fc001b36SSudeep Holla 	struct acpi_device *parent = acpi_dev_parent(adev);
52fc001b36SSudeep Holla 	struct amba_device *dev;
53fc001b36SSudeep Holla 	struct resource_entry *rentry;
54fc001b36SSudeep Holla 	struct list_head resource_list;
55fc001b36SSudeep Holla 	bool address_found = false;
56fc001b36SSudeep Holla 	int irq_no = 0;
57fc001b36SSudeep Holla 	int ret;
58fc001b36SSudeep Holla 
59fc001b36SSudeep Holla 	/* If the ACPI node already has a physical device attached, skip it. */
60fc001b36SSudeep Holla 	if (adev->physical_node_count)
61fc001b36SSudeep Holla 		return 0;
62fc001b36SSudeep Holla 
63fc001b36SSudeep Holla 	dev = amba_device_alloc(dev_name(&adev->dev), 0, 0);
64fc001b36SSudeep Holla 	if (!dev) {
65fc001b36SSudeep Holla 		dev_err(&adev->dev, "%s(): amba_device_alloc() failed\n",
66fc001b36SSudeep Holla 			__func__);
67fc001b36SSudeep Holla 		return -ENOMEM;
68fc001b36SSudeep Holla 	}
69fc001b36SSudeep Holla 
70fc001b36SSudeep Holla 	INIT_LIST_HEAD(&resource_list);
71fc001b36SSudeep Holla 	ret = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
72fc001b36SSudeep Holla 	if (ret < 0)
73fc001b36SSudeep Holla 		goto err_free;
74fc001b36SSudeep Holla 
75fc001b36SSudeep Holla 	list_for_each_entry(rentry, &resource_list, node) {
76fc001b36SSudeep Holla 		switch (resource_type(rentry->res)) {
77fc001b36SSudeep Holla 		case IORESOURCE_MEM:
78fc001b36SSudeep Holla 			if (!address_found) {
79fc001b36SSudeep Holla 				dev->res = *rentry->res;
80fc001b36SSudeep Holla 				dev->res.name = dev_name(&dev->dev);
81fc001b36SSudeep Holla 				address_found = true;
82fc001b36SSudeep Holla 			}
83fc001b36SSudeep Holla 			break;
84fc001b36SSudeep Holla 		case IORESOURCE_IRQ:
85fc001b36SSudeep Holla 			if (irq_no < AMBA_NR_IRQS)
86fc001b36SSudeep Holla 				dev->irq[irq_no++] = rentry->res->start;
87fc001b36SSudeep Holla 			break;
88fc001b36SSudeep Holla 		default:
89fc001b36SSudeep Holla 			dev_warn(&adev->dev, "Invalid resource\n");
90fc001b36SSudeep Holla 			break;
91fc001b36SSudeep Holla 		}
92fc001b36SSudeep Holla 	}
93fc001b36SSudeep Holla 
94fc001b36SSudeep Holla 	acpi_dev_free_resource_list(&resource_list);
95fc001b36SSudeep Holla 
96fc001b36SSudeep Holla 	/*
97fc001b36SSudeep Holla 	 * If the ACPI node has a parent and that parent has a physical device
98fc001b36SSudeep Holla 	 * attached to it, that physical device should be the parent of
99fc001b36SSudeep Holla 	 * the amba device we are about to create.
100fc001b36SSudeep Holla 	 */
101fc001b36SSudeep Holla 	if (parent)
102fc001b36SSudeep Holla 		dev->dev.parent = acpi_get_first_physical_node(parent);
103fc001b36SSudeep Holla 
104*ca5a75dfSAndy Shevchenko 	device_set_node(&dev->dev, acpi_fwnode_handle(adev));
105fc001b36SSudeep Holla 
106fc001b36SSudeep Holla 	ret = amba_device_add(dev, &iomem_resource);
107fc001b36SSudeep Holla 	if (ret) {
108fc001b36SSudeep Holla 		dev_err(&adev->dev, "%s(): amba_device_add() failed (%d)\n",
109fc001b36SSudeep Holla 		       __func__, ret);
110fc001b36SSudeep Holla 		goto err_free;
111fc001b36SSudeep Holla 	}
112fc001b36SSudeep Holla 
113fc001b36SSudeep Holla 	return 1;
114fc001b36SSudeep Holla 
115fc001b36SSudeep Holla err_free:
116fc001b36SSudeep Holla 	amba_device_put(dev);
117fc001b36SSudeep Holla 	return ret;
118fc001b36SSudeep Holla }
119fc001b36SSudeep Holla 
120fc001b36SSudeep Holla static struct acpi_scan_handler amba_handler = {
121fc001b36SSudeep Holla 	.ids = amba_id_list,
122fc001b36SSudeep Holla 	.attach = amba_handler_attach,
123fc001b36SSudeep Holla };
124fc001b36SSudeep Holla 
125fc001b36SSudeep Holla void __init acpi_amba_init(void)
126fc001b36SSudeep Holla {
127fc001b36SSudeep Holla 	amba_register_dummy_clk();
128fc001b36SSudeep Holla 	acpi_scan_add_handler(&amba_handler);
129fc001b36SSudeep Holla }
130