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