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