Path: blob/main/sys/arm/broadcom/bcm2835/bcm2838_xhci.c
39566 views
/*-1* SPDX-License-Identifier: ISC2*3* Copyright (c) 2020 Dr Robert Harvey Crowston <[email protected]>4*5* Permission to use, copy, modify, and distribute this software for any6* purpose with or without fee is hereby granted, provided that the above7* copyright notice and this permission notice appear in all copies.8*9* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES10* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF11* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR12* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES13* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN14* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF15* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.16*17*18*/1920/*21* VIA VL805 controller on the Raspberry Pi 4.22* The VL805 is a generic xhci controller. However, in the newer hardware23* revisions of the Raspberry Pi 4, it is incapable of loading its own firmware.24* Instead, the VideoCore GPU must load the firmware into the controller at the25* appropriate time. This driver is a shim that pre-loads the firmware before26* handing control to the xhci generic driver.27*/2829#include <sys/cdefs.h>30#include <sys/stdint.h>31#include <sys/stddef.h>32#include <sys/param.h>33#include <sys/queue.h>34#include <sys/types.h>35#include <sys/systm.h>36#include <sys/kernel.h>37#include <sys/bus.h>38#include <sys/module.h>39#include <sys/lock.h>40#include <sys/mutex.h>41#include <sys/condvar.h>42#include <sys/sysctl.h>43#include <sys/sx.h>44#include <sys/unistd.h>45#include <sys/callout.h>46#include <sys/malloc.h>47#include <sys/priv.h>4849#include <dev/usb/usb.h>50#include <dev/usb/usbdi.h>5152#include <dev/usb/usb_core.h>53#include <dev/usb/usb_busdma.h>54#include <dev/usb/usb_process.h>55#include <dev/usb/usb_util.h>5657#include <dev/usb/usb_controller.h>58#include <dev/usb/usb_bus.h>59#include <dev/usb/usb_pci.h>60#include <dev/usb/controller/xhci.h>6162#include <dev/ofw/openfirm.h>63#include <dev/ofw/ofw_bus.h>64#include <dev/ofw/ofw_bus_subr.h>6566#include <arm/broadcom/bcm2835/bcm2835_mbox_prop.h>6768#define VL805_FIRMWARE_REG 0x5069#define PCIE_BUS_SHIFT 2070#define PCIE_SLOT_SHIFT 1571#define PCIE_FUNC_SHIFT 127273static int74bcm_xhci_probe(device_t dev)75{76phandle_t root;77uint32_t device_id;7879device_id = pci_get_devid(dev);80if (device_id != 0x34831106) /* VIA VL805 USB 3.0 controller. */81return (ENXIO);8283/*84* The VIA chip is not unique to the Pi, but we only want to use this85* driver if the SoC is a Raspberry Pi 4. Walk the device tree to86* discover if the system is a Pi 4.87*/88root = OF_finddevice("/");89if (root == -1)90return (ENXIO);91if (!ofw_bus_node_is_compatible(root, "raspberrypi,4-model-b"))92return (ENXIO);9394/*95* On the Pi 4, the VIA chip with the firmware-loading limitation is96* soldered-on to a particular bus/slot/function. But, it's possible a97* user could desolder the VIA chip, replace it with a pci-pci bridge,98* then plug in a commodity VIA PCI-e card on the new bridge. In that99* case we don't want to try to load the firmware to a commodity100* expansion card.101*/102if (pci_get_bus(dev) != 1 || pci_get_slot(dev) != 0 ||103pci_get_function(dev) != 0 )104return (ENXIO);105106device_set_desc(dev,107"VL805 USB 3.0 controller (on the Raspberry Pi 4b)");108109return (BUS_PROBE_SPECIFIC);110}111112static uint32_t113bcm_xhci_check_firmware(device_t dev, bool expect_loaded)114{115uint32_t revision;116bool loaded;117118revision = pci_read_config(dev, VL805_FIRMWARE_REG, 4);119loaded = !(revision == 0 || revision == 0xffffffff);120121if (expect_loaded && !loaded)122device_printf(dev, "warning: xhci firmware not found.\n");123else if (bootverbose && !loaded)124device_printf(dev, "note: xhci firmware not found.\n");125else if (bootverbose)126device_printf(dev,127"note: xhci firmware detected; firmware is revision %x.\n",128revision);129130if (!loaded)131return 0;132133return (revision);134}135136static void137bcm_xhci_install_xhci_firmware(device_t dev)138{139uint32_t revision, dev_addr;140int error;141142revision = bcm_xhci_check_firmware(dev, false);143if (revision > 0) {144/*145* With the pre-June 2020 boot firmware, it does not seem146* possible to reload already-installed xhci firmware.147*/148return;149}150151/*152* Notify the VideoCore gpu processor that it needs to reload the xhci153* firmware into the xhci controller. This needs to happen after the pci154* bridge topology is registered with the controller.155*/156if (bootverbose)157device_printf(dev, "note: installing xhci firmware.\n");158159dev_addr =160pci_get_bus(dev) << PCIE_BUS_SHIFT |161pci_get_slot(dev) << PCIE_SLOT_SHIFT |162pci_get_function(dev) << PCIE_FUNC_SHIFT;163164error = bcm2835_mbox_notify_xhci_reset(dev_addr);165if (error)166device_printf(dev,167"warning: xhci firmware install failed (error %d).\n",168error);169170DELAY(1000);171bcm_xhci_check_firmware(dev, true);172173return;174}175176static int177bcm_xhci_attach(device_t dev)178{179struct xhci_softc *sc;180int error;181182sc = device_get_softc(dev);183184bcm_xhci_install_xhci_firmware(dev);185186error = xhci_pci_attach(dev);187if (error)188return (error);189190/* 32 bit DMA is a limitation of the PCI-e controller, not the VL805. */191sc->sc_bus.dma_bits = 32;192if (bootverbose)193device_printf(dev, "note: switched to 32-bit DMA.\n");194195return (0);196}197198/*199* Device method table.200*/201static device_method_t bcm_xhci_methods[] = {202/* Device interface. */203DEVMETHOD(device_probe, bcm_xhci_probe),204DEVMETHOD(device_attach, bcm_xhci_attach),205206DEVMETHOD_END,207};208209DEFINE_CLASS_1(bcm_xhci, bcm_xhci_driver, bcm_xhci_methods,210sizeof(struct xhci_softc), xhci_pci_driver);211212DRIVER_MODULE(bcm_xhci, pci, bcm_xhci_driver, 0, 0);213MODULE_DEPEND(bcm_xhci, usb, 1, 1, 1);214MODULE_DEPEND(bcm_xhci, pci, 1, 1, 1);215MODULE_DEPEND(bcm_xhci, xhci, 1, 1, 1);216217218