blob: f7c5958a6c7189298a58f503b09be31fcb1a2650 [file] [log] [blame]
Raul E Rangela5b7ddf2020-05-29 17:16:20 -06001/* SPDX-License-Identifier: GPL-2.0-or-later */
2
Raul E Rangela5b7ddf2020-05-29 17:16:20 -06003#include <console/console.h>
Elyes Haouas8823ba12022-12-05 08:48:50 +01004#include <device/mmio.h>
Raul E Rangela5b7ddf2020-05-29 17:16:20 -06005#include <device/pci_def.h>
Raul E Rangel2c952d62020-07-10 13:58:48 -06006#include <device/xhci.h>
7#include <string.h>
Raul E Rangela5b7ddf2020-05-29 17:16:20 -06008
9union xhci_ext_caps_header {
10 uint32_t val;
11 struct {
12 uint32_t cap_id : 8;
13 uint32_t next_ptr : 8;
14 uint32_t reserved : 16;
15 };
16};
17
18enum cb_err xhci_for_each_ext_cap(const struct device *device, void *context,
19 void (*callback)(void *context,
20 const struct xhci_ext_cap *cap))
21{
22 struct resource *res;
23 uint32_t *ext_cap_ptr;
24 uint32_t ext_caps_word_offset;
25 union xhci_ext_caps_header header;
26 struct xhci_ext_cap cap;
27
28 if (!device || !callback)
29 return CB_ERR_ARG;
30
31 res = probe_resource(device, PCI_BASE_ADDRESS_0);
32 if (!res) {
33 printk(BIOS_ERR, "%s: Unable to find BAR resource for %s\n", __func__,
34 dev_path(device));
35 return CB_ERR;
36 }
37
38 if (!(res->flags & IORESOURCE_ASSIGNED)) {
Elyes HAOUAScb0584e2021-01-16 14:44:07 +010039 printk(BIOS_ERR, "%s: BAR is not assigned\n", __func__);
Raul E Rangela5b7ddf2020-05-29 17:16:20 -060040 return CB_ERR;
41 }
42
43 if (res->limit > 0xFFFFFFFF) {
44 printk(BIOS_ERR, "%s: 64-bit BAR is not supported\n", __func__);
45 return CB_ERR;
46 }
47
48 ext_caps_word_offset = read16(res2mmio(res, XHCI_HCCPARAMS1_XECP, 0));
49
50 if (!ext_caps_word_offset) {
51 printk(BIOS_ERR, "%s: No extended capabilities defined\n", __func__);
52 return CB_ERR;
53 }
54
55 ext_cap_ptr = res2mmio(res, ext_caps_word_offset << 2, 0);
56
57 while ((uintptr_t)ext_cap_ptr < (uintptr_t)res->limit) {
58 header.val = read32(ext_cap_ptr);
59
60 cap.cap_id = header.cap_id;
61
62 if (header.cap_id == XHCI_ECP_CAP_ID_SUPP) {
63 cap.supported_protocol.reg0 = header.val;
64 cap.supported_protocol.reg1 = read32(ext_cap_ptr + 1);
65 cap.supported_protocol.reg2 = read32(ext_cap_ptr + 2);
66 }
67
68 callback(context, &cap);
69
70 if (!header.next_ptr)
71 break;
72
73 ext_cap_ptr += header.next_ptr;
74 }
75
76 return CB_SUCCESS;
77}
78
Raul E Rangel2c952d62020-07-10 13:58:48 -060079struct supported_usb_cap_context {
80 void *context;
81 void (*callback)(void *context, const struct xhci_supported_protocol *data);
82};
83
84static void xhci_supported_usb_cap_handler(void *context, const struct xhci_ext_cap *cap)
85{
86 const struct xhci_supported_protocol *data;
87 struct supported_usb_cap_context *internal_context = context;
88
89 if (cap->cap_id != XHCI_ECP_CAP_ID_SUPP)
90 return;
91
92 data = &cap->supported_protocol;
93
94 if (memcmp(data->name, "USB ", 4)) {
95 printk(BIOS_DEBUG, "%s: Unknown Protocol: %.*s\n", __func__,
96 (int)sizeof(data->name), data->name);
97 return;
98 }
99
100 internal_context->callback(internal_context->context, data);
101}
102
103enum cb_err xhci_for_each_supported_usb_cap(
104 const struct device *device, void *context,
105 void (*callback)(void *context, const struct xhci_supported_protocol *data))
106{
107 struct supported_usb_cap_context internal_context = {
108 .context = context,
109 .callback = callback,
110 };
111
112 return xhci_for_each_ext_cap(device, &internal_context, xhci_supported_usb_cap_handler);
113}
114
Raul E Rangela5b7ddf2020-05-29 17:16:20 -0600115void xhci_print_supported_protocol(const struct xhci_supported_protocol *supported_protocol)
116{
117 printk(BIOS_DEBUG, "xHCI Supported Protocol:\n");
118 printk(BIOS_DEBUG, " Major: %#x, Minor: %#x, Protocol: '%.*s'\n",
119 supported_protocol->major_rev, supported_protocol->minor_rev,
120 (int)sizeof(supported_protocol->name), supported_protocol->name);
121 printk(BIOS_DEBUG, " Port Offset: %d, Port Count: %d\n",
122 supported_protocol->port_offset, supported_protocol->port_count);
123}