1 /*-
2 * SPDX-License-Identifier: ISC
3 *
4 * Copyright (c) 2020 Dr Robert Harvey Crowston <crowston@protonmail.com>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *
18 *
19 * $FreeBSD$
20 *
21 */
22
23 /*
24 * VIA VL805 controller on the Raspberry Pi 4.
25 * The VL805 is a generic xhci controller. However, in the newer hardware
26 * revisions of the Raspberry Pi 4, it is incapable of loading its own firmware.
27 * Instead, the VideoCore GPU must load the firmware into the controller at the
28 * appropriate time. This driver is a shim that pre-loads the firmware before
29 * handing control to the xhci generic driver.
30 */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include <sys/stdint.h>
36 #include <sys/stddef.h>
37 #include <sys/param.h>
38 #include <sys/queue.h>
39 #include <sys/types.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/bus.h>
43 #include <sys/module.h>
44 #include <sys/lock.h>
45 #include <sys/mutex.h>
46 #include <sys/condvar.h>
47 #include <sys/sysctl.h>
48 #include <sys/sx.h>
49 #include <sys/unistd.h>
50 #include <sys/callout.h>
51 #include <sys/malloc.h>
52 #include <sys/priv.h>
53
54 #include <dev/usb/usb.h>
55 #include <dev/usb/usbdi.h>
56
57 #include <dev/usb/usb_core.h>
58 #include <dev/usb/usb_busdma.h>
59 #include <dev/usb/usb_process.h>
60 #include <dev/usb/usb_util.h>
61
62 #include <dev/usb/usb_controller.h>
63 #include <dev/usb/usb_bus.h>
64 #include <dev/usb/usb_pci.h>
65 #include <dev/usb/controller/xhci.h>
66
67 #include <dev/ofw/openfirm.h>
68 #include <dev/ofw/ofw_bus.h>
69 #include <dev/ofw/ofw_bus_subr.h>
70
71 #include <arm/broadcom/bcm2835/bcm2835_mbox_prop.h>
72
73 #define VL805_FIRMWARE_REG 0x50
74 #define PCIE_BUS_SHIFT 20
75 #define PCIE_SLOT_SHIFT 15
76 #define PCIE_FUNC_SHIFT 12
77
78 static int
79 bcm_xhci_probe(device_t dev)
80 {
81 phandle_t root;
82 uint32_t device_id;
83
84 device_id = pci_get_devid(dev);
85 if (device_id != 0x34831106) /* VIA VL805 USB 3.0 controller. */
86 return (ENXIO);
87
88 /*
89 * The VIA chip is not unique to the Pi, but we only want to use this
90 * driver if the SoC is a Raspberry Pi 4. Walk the device tree to
91 * discover if the system is a Pi 4.
92 */
93 root = OF_finddevice("/");
94 if (root == -1)
95 return (ENXIO);
96 if (!ofw_bus_node_is_compatible(root, "raspberrypi,4-model-b"))
97 return (ENXIO);
98
99 /*
100 * On the Pi 4, the VIA chip with the firmware-loading limitation is
101 * soldered-on to a particular bus/slot/function. But, it's possible a
102 * user could desolder the VIA chip, replace it with a pci-pci bridge,
103 * then plug in a commodity VIA PCI-e card on the new bridge. In that
104 * case we don't want to try to load the firmware to a commodity
105 * expansion card.
106 */
107 if (pci_get_bus(dev) != 1 || pci_get_slot(dev) != 0 ||
108 pci_get_function(dev) != 0 )
109 return (ENXIO);
110
111 device_set_desc(dev,
112 "VL805 USB 3.0 controller (on the Raspberry Pi 4b)");
113
114 return (BUS_PROBE_SPECIFIC);
115 }
116
117 static uint32_t
118 bcm_xhci_check_firmware(device_t dev, bool expect_loaded)
119 {
120 uint32_t revision;
121 bool loaded;
122
123 revision = pci_read_config(dev, VL805_FIRMWARE_REG, 4);
124 loaded = !(revision == 0 || revision == 0xffffffff);
125
126 if (expect_loaded && !loaded)
127 device_printf(dev, "warning: xhci firmware not found.\n");
128 else if (bootverbose && !loaded)
129 device_printf(dev, "note: xhci firmware not found.\n");
130 else if (bootverbose)
131 device_printf(dev,
132 "note: xhci firmware detected; firmware is revision %x.\n",
133 revision);
134
135 if (!loaded)
136 return 0;
137
138 return (revision);
139 }
140
141 static void
142 bcm_xhci_install_xhci_firmware(device_t dev)
143 {
144 uint32_t revision, dev_addr;
145 int error;
146
147 revision = bcm_xhci_check_firmware(dev, false);
148 if (revision > 0) {
149 /*
150 * With the pre-June 2020 boot firmware, it does not seem
151 * possible to reload already-installed xhci firmware.
152 */
153 return;
154 }
155
156 /*
157 * Notify the VideoCore gpu processor that it needs to reload the xhci
158 * firmware into the xhci controller. This needs to happen after the pci
159 * bridge topology is registered with the controller.
160 */
161 if (bootverbose)
162 device_printf(dev, "note: installing xhci firmware.\n");
163
164 dev_addr =
165 pci_get_bus(dev) << PCIE_BUS_SHIFT |
166 pci_get_slot(dev) << PCIE_SLOT_SHIFT |
167 pci_get_function(dev) << PCIE_FUNC_SHIFT;
168
169 error = bcm2835_mbox_notify_xhci_reset(dev_addr);
170 if (error)
171 device_printf(dev,
172 "warning: xhci firmware install failed (error %d).\n",
173 error);
174
175 DELAY(1000);
176 bcm_xhci_check_firmware(dev, true);
177
178 return;
179 }
180
181 static int
182 bcm_xhci_attach(device_t dev)
183 {
184 struct xhci_softc *sc;
185 int error;
186
187 sc = device_get_softc(dev);
188
189 bcm_xhci_install_xhci_firmware(dev);
190
191 error = xhci_pci_attach(dev);
192 if (error)
193 return (error);
194
195 /* 32 bit DMA is a limitation of the PCI-e controller, not the VL805. */
196 sc->sc_bus.dma_bits = 32;
197 if (bootverbose)
198 device_printf(dev, "note: switched to 32-bit DMA.\n");
199
200 return (0);
201 }
202
203 /*
204 * Device method table.
205 */
206 static device_method_t bcm_xhci_methods[] = {
207 /* Device interface. */
208 DEVMETHOD(device_probe, bcm_xhci_probe),
209 DEVMETHOD(device_attach, bcm_xhci_attach),
210
211 DEVMETHOD_END,
212 };
213
214 DEFINE_CLASS_1(bcm_xhci, bcm_xhci_driver, bcm_xhci_methods,
215 sizeof(struct xhci_softc), xhci_pci_driver);
216
217 DRIVER_MODULE(bcm_xhci, pci, bcm_xhci_driver, 0, 0);
218 MODULE_DEPEND(bcm_xhci, usb, 1, 1, 1);
Cache object: 06d8a609fe333f9727e9aa38f9bdaaac
|