Angel Pons | 0612b27 | 2020-04-05 15:46:56 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Subrata Banik | a554b0c | 2017-02-16 16:08:49 +0530 | [diff] [blame] | 2 | |
Furquan Shaikh | 76cedd2 | 2020-05-02 10:24:23 -0700 | [diff] [blame] | 3 | #include <acpi/acpi_device.h> |
Karthikeyan Ramasubramanian | ef0c226 | 2019-06-06 15:35:11 -0600 | [diff] [blame] | 4 | #include <console/console.h> |
Subrata Banik | a554b0c | 2017-02-16 16:08:49 +0530 | [diff] [blame] | 5 | #include <device/device.h> |
| 6 | #include <device/pci.h> |
| 7 | #include <device/pci_ids.h> |
Karthikeyan Ramasubramanian | ef0c226 | 2019-06-06 15:35:11 -0600 | [diff] [blame] | 8 | #include <drivers/usb/acpi/chip.h> |
Duncan Laurie | bf713b0 | 2018-05-07 15:33:18 -0700 | [diff] [blame] | 9 | #include <intelblocks/acpi.h> |
Subrata Banik | a554b0c | 2017-02-16 16:08:49 +0530 | [diff] [blame] | 10 | #include <intelblocks/xhci.h> |
Karthikeyan Ramasubramanian | ef0c226 | 2019-06-06 15:35:11 -0600 | [diff] [blame] | 11 | #include <soc/pci_devs.h> |
| 12 | |
| 13 | #define XHCI_USB2 2 |
| 14 | #define XHCI_USB3 3 |
| 15 | |
Subrata Banik | e3a4a13 | 2022-04-11 17:45:33 +0530 | [diff] [blame] | 16 | #define XHCI_USBCMD 0x80 |
| 17 | #define USBCMD_HCRST (1 << 1) |
| 18 | |
Karthikeyan Ramasubramanian | ef0c226 | 2019-06-06 15:35:11 -0600 | [diff] [blame] | 19 | /* Current Connect Status */ |
| 20 | #define XHCI_STATUS_CCS (1 << 0) |
| 21 | |
Subrata Banik | e3a4a13 | 2022-04-11 17:45:33 +0530 | [diff] [blame] | 22 | static uint8_t *xhci_mem_base(void) |
| 23 | { |
| 24 | uint32_t mem_base = pci_read_config32(PCH_DEV_XHCI, PCI_BASE_ADDRESS_0); |
| 25 | |
| 26 | /* Check if the controller is disabled or not present */ |
| 27 | if (mem_base == 0 || mem_base == 0xffffffff) |
| 28 | return 0; |
| 29 | |
| 30 | return (uint8_t *)(mem_base & ~PCI_BASE_ADDRESS_MEM_ATTR_MASK); |
| 31 | } |
| 32 | |
| 33 | void xhci_host_reset(void) |
| 34 | { |
| 35 | uint8_t *xhci_base = xhci_mem_base(); |
| 36 | if (!xhci_base) |
| 37 | return; |
| 38 | |
| 39 | setbits8(xhci_base + XHCI_USBCMD, USBCMD_HCRST); |
| 40 | } |
| 41 | |
| 42 | #if ENV_RAMSTAGE |
Karthikeyan Ramasubramanian | ef0c226 | 2019-06-06 15:35:11 -0600 | [diff] [blame] | 43 | static bool is_usb_port_connected(const struct xhci_usb_info *info, |
| 44 | unsigned int port_type, unsigned int port_id) |
| 45 | { |
| 46 | uintptr_t port_sts_reg; |
| 47 | uint32_t port_status; |
| 48 | const struct resource *res; |
| 49 | |
| 50 | /* Support only USB2 or USB3 ports */ |
| 51 | if (!(port_type == XHCI_USB2 || port_type == XHCI_USB3)) |
| 52 | return false; |
| 53 | |
| 54 | /* Mark out of bound port id as not connected */ |
| 55 | if ((port_type == XHCI_USB2 && port_id >= info->num_usb2_ports) || |
| 56 | (port_type == XHCI_USB3 && port_id >= info->num_usb3_ports)) |
| 57 | return false; |
| 58 | |
| 59 | /* Calculate port status register address and read the status */ |
Angel Pons | c1bfbe0 | 2021-11-03 13:18:53 +0100 | [diff] [blame] | 60 | res = probe_resource(PCH_DEV_XHCI, PCI_BASE_ADDRESS_0); |
Karthikeyan Ramasubramanian | ef0c226 | 2019-06-06 15:35:11 -0600 | [diff] [blame] | 61 | /* If the memory BAR is not allocated for XHCI, leave the devices enabled */ |
| 62 | if (!res) |
| 63 | return true; |
| 64 | |
| 65 | if (port_type == XHCI_USB2) |
| 66 | port_sts_reg = (uintptr_t)res->base + |
| 67 | info->usb2_port_status_reg + port_id * 0x10; |
| 68 | else |
| 69 | port_sts_reg = (uintptr_t)res->base + |
| 70 | info->usb3_port_status_reg + port_id * 0x10; |
| 71 | port_status = read32((void *)port_sts_reg); |
| 72 | |
| 73 | /* Ensure that the status is not all 1s */ |
| 74 | if (port_status == 0xffffffff) |
| 75 | return false; |
| 76 | |
| 77 | return !!(port_status & XHCI_STATUS_CCS); |
| 78 | } |
| 79 | |
| 80 | void usb_xhci_disable_unused(bool (*ext_usb_xhci_en_cb)(unsigned int port_type, |
| 81 | unsigned int port_id)) |
| 82 | { |
| 83 | struct device *xhci, *hub = NULL, *port = NULL; |
Tim Wawrzynczak | 56fcfb5 | 2020-11-10 13:39:37 -0700 | [diff] [blame] | 84 | const struct xhci_usb_info *info = soc_get_xhci_usb_info(PCH_DEVFN_XHCI); |
Karthikeyan Ramasubramanian | ef0c226 | 2019-06-06 15:35:11 -0600 | [diff] [blame] | 85 | struct drivers_usb_acpi_config *config; |
| 86 | bool enable; |
| 87 | |
| 88 | xhci = pcidev_path_on_root(PCH_DEVFN_XHCI); |
| 89 | if (!xhci) { |
| 90 | printk(BIOS_ERR, "%s: Could not locate XHCI device in DT\n", __func__); |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | while ((hub = dev_bus_each_child(xhci->link_list, hub)) != NULL) { |
| 95 | while ((port = dev_bus_each_child(hub->link_list, port)) != NULL) { |
| 96 | enable = true; |
| 97 | config = config_of(port); |
| 98 | if (config->type == UPC_TYPE_INTERNAL) { |
| 99 | /* Probe the connect status of internal ports */ |
| 100 | enable = is_usb_port_connected(info, port->path.usb.port_type, |
| 101 | port->path.usb.port_id); |
| 102 | } else if (ext_usb_xhci_en_cb) { |
| 103 | /* Check the mainboard for the status of external ports */ |
| 104 | enable = ext_usb_xhci_en_cb(port->path.usb.port_type, |
| 105 | port->path.usb.port_id); |
| 106 | } |
| 107 | |
| 108 | if (!enable) { |
| 109 | printk(BIOS_INFO, "%s: Disabling USB Type%d Id%d\n", |
| 110 | __func__, port->path.usb.port_type, |
| 111 | port->path.usb.port_id); |
| 112 | port->enabled = 0; |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | } |
Subrata Banik | a554b0c | 2017-02-16 16:08:49 +0530 | [diff] [blame] | 117 | |
Aaron Durbin | 6403167 | 2018-04-21 14:45:32 -0600 | [diff] [blame] | 118 | __weak void soc_xhci_init(struct device *dev) { /* no-op */ } |
Subrata Banik | a554b0c | 2017-02-16 16:08:49 +0530 | [diff] [blame] | 119 | |
| 120 | static struct device_operations usb_xhci_ops = { |
Elyes HAOUAS | 1d19127 | 2018-11-27 12:23:48 +0100 | [diff] [blame] | 121 | .read_resources = pci_dev_read_resources, |
| 122 | .set_resources = pci_dev_set_resources, |
| 123 | .enable_resources = pci_dev_enable_resources, |
Subrata Banik | a554b0c | 2017-02-16 16:08:49 +0530 | [diff] [blame] | 124 | .init = soc_xhci_init, |
Subrata Banik | 6bbc91a | 2017-12-07 14:55:51 +0530 | [diff] [blame] | 125 | .ops_pci = &pci_dev_ops_pci, |
Nico Huber | a89c82e | 2017-09-14 15:40:28 +0200 | [diff] [blame] | 126 | .scan_bus = scan_static_bus, |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 127 | #if CONFIG(HAVE_ACPI_TABLES) |
Elyes HAOUAS | 1d19127 | 2018-11-27 12:23:48 +0100 | [diff] [blame] | 128 | .acpi_name = soc_acpi_name, |
Subrata Banik | 98376b8 | 2018-05-22 16:18:16 +0530 | [diff] [blame] | 129 | #endif |
Subrata Banik | a554b0c | 2017-02-16 16:08:49 +0530 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | static const unsigned short pci_device_ids[] = { |
Wonkyu Kim | 9f40107 | 2020-11-13 15:16:32 -0800 | [diff] [blame] | 133 | PCI_DID_INTEL_MTL_XHCI, |
Felix Singer | 43b7f41 | 2022-03-07 04:34:52 +0100 | [diff] [blame] | 134 | PCI_DID_INTEL_APL_XHCI, |
| 135 | PCI_DID_INTEL_CNL_LP_XHCI, |
| 136 | PCI_DID_INTEL_GLK_XHCI, |
| 137 | PCI_DID_INTEL_SPT_LP_XHCI, |
| 138 | PCI_DID_INTEL_SPT_H_XHCI, |
| 139 | PCI_DID_INTEL_LWB_XHCI, |
| 140 | PCI_DID_INTEL_LWB_XHCI_SUPER, |
| 141 | PCI_DID_INTEL_UPT_H_XHCI, |
| 142 | PCI_DID_INTEL_CNP_H_XHCI, |
| 143 | PCI_DID_INTEL_ICP_LP_XHCI, |
| 144 | PCI_DID_INTEL_CMP_LP_XHCI, |
| 145 | PCI_DID_INTEL_CMP_H_XHCI, |
| 146 | PCI_DID_INTEL_TGP_LP_XHCI, |
| 147 | PCI_DID_INTEL_TGP_H_XHCI, |
| 148 | PCI_DID_INTEL_MCC_XHCI, |
| 149 | PCI_DID_INTEL_JSP_XHCI, |
| 150 | PCI_DID_INTEL_ADP_P_XHCI, |
| 151 | PCI_DID_INTEL_ADP_S_XHCI, |
| 152 | PCI_DID_INTEL_ADP_M_XHCI, |
Subrata Banik | a554b0c | 2017-02-16 16:08:49 +0530 | [diff] [blame] | 153 | 0 |
| 154 | }; |
| 155 | |
| 156 | static const struct pci_driver pch_usb_xhci __pci_driver = { |
| 157 | .ops = &usb_xhci_ops, |
Felix Singer | 43b7f41 | 2022-03-07 04:34:52 +0100 | [diff] [blame] | 158 | .vendor = PCI_VID_INTEL, |
Subrata Banik | a554b0c | 2017-02-16 16:08:49 +0530 | [diff] [blame] | 159 | .devices = pci_device_ids, |
| 160 | }; |
Subrata Banik | e3a4a13 | 2022-04-11 17:45:33 +0530 | [diff] [blame] | 161 | #endif |