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