xref: /linux/drivers/acpi/arm64/amba.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
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 	{"", 0},
26fc001b36SSudeep Holla };
27fc001b36SSudeep Holla 
amba_register_dummy_clk(void)28fc001b36SSudeep Holla static void amba_register_dummy_clk(void)
29fc001b36SSudeep Holla {
30*bfe3f0dfSYouwan Wang 	struct clk *amba_dummy_clk;
31fc001b36SSudeep Holla 
32fc001b36SSudeep Holla 	amba_dummy_clk = clk_register_fixed_rate(NULL, "apb_pclk", NULL, 0, 0);
33fc001b36SSudeep Holla 	clk_register_clkdev(amba_dummy_clk, "apb_pclk", NULL);
34fc001b36SSudeep Holla }
35fc001b36SSudeep Holla 
amba_handler_attach(struct acpi_device * adev,const struct acpi_device_id * id)36fc001b36SSudeep Holla static int amba_handler_attach(struct acpi_device *adev,
37fc001b36SSudeep Holla 				const struct acpi_device_id *id)
38fc001b36SSudeep Holla {
39fc001b36SSudeep Holla 	struct acpi_device *parent = acpi_dev_parent(adev);
40fc001b36SSudeep Holla 	struct amba_device *dev;
41fc001b36SSudeep Holla 	struct resource_entry *rentry;
42fc001b36SSudeep Holla 	struct list_head resource_list;
43fc001b36SSudeep Holla 	bool address_found = false;
44fc001b36SSudeep Holla 	int irq_no = 0;
45fc001b36SSudeep Holla 	int ret;
46fc001b36SSudeep Holla 
47fc001b36SSudeep Holla 	/* If the ACPI node already has a physical device attached, skip it. */
48fc001b36SSudeep Holla 	if (adev->physical_node_count)
49fc001b36SSudeep Holla 		return 0;
50fc001b36SSudeep Holla 
51fc001b36SSudeep Holla 	dev = amba_device_alloc(dev_name(&adev->dev), 0, 0);
52fc001b36SSudeep Holla 	if (!dev) {
53fc001b36SSudeep Holla 		dev_err(&adev->dev, "%s(): amba_device_alloc() failed\n",
54fc001b36SSudeep Holla 			__func__);
55fc001b36SSudeep Holla 		return -ENOMEM;
56fc001b36SSudeep Holla 	}
57fc001b36SSudeep Holla 
58fc001b36SSudeep Holla 	INIT_LIST_HEAD(&resource_list);
59fc001b36SSudeep Holla 	ret = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
60fc001b36SSudeep Holla 	if (ret < 0)
61fc001b36SSudeep Holla 		goto err_free;
62fc001b36SSudeep Holla 
63fc001b36SSudeep Holla 	list_for_each_entry(rentry, &resource_list, node) {
64fc001b36SSudeep Holla 		switch (resource_type(rentry->res)) {
65fc001b36SSudeep Holla 		case IORESOURCE_MEM:
66fc001b36SSudeep Holla 			if (!address_found) {
67fc001b36SSudeep Holla 				dev->res = *rentry->res;
68fc001b36SSudeep Holla 				dev->res.name = dev_name(&dev->dev);
69fc001b36SSudeep Holla 				address_found = true;
70fc001b36SSudeep Holla 			}
71fc001b36SSudeep Holla 			break;
72fc001b36SSudeep Holla 		case IORESOURCE_IRQ:
73fc001b36SSudeep Holla 			if (irq_no < AMBA_NR_IRQS)
74fc001b36SSudeep Holla 				dev->irq[irq_no++] = rentry->res->start;
75fc001b36SSudeep Holla 			break;
76fc001b36SSudeep Holla 		default:
77fc001b36SSudeep Holla 			dev_warn(&adev->dev, "Invalid resource\n");
78fc001b36SSudeep Holla 			break;
79fc001b36SSudeep Holla 		}
80fc001b36SSudeep Holla 	}
81fc001b36SSudeep Holla 
82fc001b36SSudeep Holla 	acpi_dev_free_resource_list(&resource_list);
83fc001b36SSudeep Holla 
84fc001b36SSudeep Holla 	/*
85fc001b36SSudeep Holla 	 * If the ACPI node has a parent and that parent has a physical device
86fc001b36SSudeep Holla 	 * attached to it, that physical device should be the parent of
87fc001b36SSudeep Holla 	 * the amba device we are about to create.
88fc001b36SSudeep Holla 	 */
89fc001b36SSudeep Holla 	if (parent)
90fc001b36SSudeep Holla 		dev->dev.parent = acpi_get_first_physical_node(parent);
91fc001b36SSudeep Holla 
92ca5a75dfSAndy Shevchenko 	device_set_node(&dev->dev, acpi_fwnode_handle(adev));
93fc001b36SSudeep Holla 
94fc001b36SSudeep Holla 	ret = amba_device_add(dev, &iomem_resource);
95fc001b36SSudeep Holla 	if (ret) {
96fc001b36SSudeep Holla 		dev_err(&adev->dev, "%s(): amba_device_add() failed (%d)\n",
97fc001b36SSudeep Holla 		       __func__, ret);
98fc001b36SSudeep Holla 		goto err_free;
99fc001b36SSudeep Holla 	}
100fc001b36SSudeep Holla 
101fc001b36SSudeep Holla 	return 1;
102fc001b36SSudeep Holla 
103fc001b36SSudeep Holla err_free:
104fc001b36SSudeep Holla 	amba_device_put(dev);
105fc001b36SSudeep Holla 	return ret;
106fc001b36SSudeep Holla }
107fc001b36SSudeep Holla 
108fc001b36SSudeep Holla static struct acpi_scan_handler amba_handler = {
109fc001b36SSudeep Holla 	.ids = amba_id_list,
110fc001b36SSudeep Holla 	.attach = amba_handler_attach,
111fc001b36SSudeep Holla };
112fc001b36SSudeep Holla 
acpi_amba_init(void)113fc001b36SSudeep Holla void __init acpi_amba_init(void)
114fc001b36SSudeep Holla {
115fc001b36SSudeep Holla 	amba_register_dummy_clk();
116fc001b36SSudeep Holla 	acpi_scan_add_handler(&amba_handler);
117fc001b36SSudeep Holla }
118