tegra_ehci.c revision 308335
1266077Sdes/*- 2266077Sdes * Copyright (c) 2016 Michal Meloun <mmel@FreeBSD.org> 3266077Sdes * All rights reserved. 4266077Sdes * 5266077Sdes * Redistribution and use in source and binary forms, with or without 6266077Sdes * modification, are permitted provided that the following conditions 7266077Sdes * are met: 8266077Sdes * 1. Redistributions of source code must retain the above copyright 9266077Sdes * notice, this list of conditions and the following disclaimer. 10266077Sdes * 2. Redistributions in binary form must reproduce the above copyright 11266077Sdes * notice, this list of conditions and the following disclaimer in the 12266077Sdes * documentation and/or other materials provided with the distribution. 13266077Sdes * 14266077Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15287915Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16266077Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17266077Sdes * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18266077Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19266077Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20266077Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21266077Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22266077Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23266077Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24266077Sdes * SUCH DAMAGE. 25266077Sdes */ 26266077Sdes 27266077Sdes#include <sys/cdefs.h> 28266077Sdes__FBSDID("$FreeBSD: stable/11/sys/arm/nvidia/tegra_ehci.c 308335 2016-11-05 10:56:32Z mmel $"); 29266077Sdes 30266077Sdes/* 31266077Sdes * EHCI driver for Tegra SoCs. 32266077Sdes */ 33266077Sdes#include "opt_bus.h" 34266077Sdes#include "opt_platform.h" 35266077Sdes 36266077Sdes#include <sys/param.h> 37266077Sdes#include <sys/systm.h> 38266077Sdes#include <sys/kernel.h> 39266077Sdes#include <sys/module.h> 40266077Sdes#include <sys/bus.h> 41266077Sdes#include <sys/condvar.h> 42266077Sdes#include <sys/rman.h> 43266077Sdes 44266077Sdes#include <machine/bus.h> 45266077Sdes#include <machine/resource.h> 46266077Sdes 47266077Sdes#include <dev/extres/clk/clk.h> 48266077Sdes#include <dev/extres/hwreset/hwreset.h> 49266077Sdes#include <dev/extres/phy/phy.h> 50266077Sdes#include <dev/ofw/ofw_bus.h> 51266077Sdes#include <dev/ofw/ofw_bus_subr.h> 52266077Sdes#include <dev/usb/usb.h> 53266077Sdes#include <dev/usb/usbdi.h> 54266077Sdes#include <dev/usb/usb_busdma.h> 55266077Sdes#include <dev/usb/usb_process.h> 56266077Sdes#include <dev/usb/usb_controller.h> 57266077Sdes#include <dev/usb/usb_bus.h> 58266077Sdes#include <dev/usb/controller/ehci.h> 59266077Sdes#include <dev/usb/controller/ehcireg.h> 60266077Sdes 61266077Sdes#include "usbdevs.h" 62266077Sdes 63266077Sdes#define TEGRA_EHCI_REG_OFF 0x100 64266077Sdes#define TEGRA_EHCI_REG_SIZE 0x100 65266077Sdes 66266077Sdes/* Compatible devices. */ 67266077Sdes#define TEGRA124_EHCI 1 68266077Sdesstatic struct ofw_compat_data compat_data[] = { 69266077Sdes {"nvidia,tegra124-ehci", (uintptr_t)TEGRA124_EHCI}, 70266077Sdes {NULL, 0}, 71266077Sdes}; 72266077Sdes 73266077Sdesstruct tegra_ehci_softc { 74266077Sdes ehci_softc_t ehci_softc; 75266077Sdes device_t dev; 76266077Sdes struct resource *ehci_mem_res; /* EHCI core regs. */ 77266077Sdes struct resource *ehci_irq_res; /* EHCI core IRQ. */ 78266077Sdes int usb_alloc_called; 79266077Sdes clk_t clk; 80266077Sdes phy_t phy; 81266077Sdes hwreset_t reset; 82266077Sdes}; 83266077Sdes 84266077Sdesstatic void 85266077Sdestegra_ehci_post_reset(struct ehci_softc *ehci_softc) 86266077Sdes{ 87266077Sdes uint32_t usbmode; 88266077Sdes 89266077Sdes /* Force HOST mode. */ 90266077Sdes usbmode = EOREAD4(ehci_softc, EHCI_USBMODE_LPM); 91266077Sdes usbmode &= ~EHCI_UM_CM; 92266077Sdes usbmode |= EHCI_UM_CM_HOST; 93266077Sdes device_printf(ehci_softc->sc_bus.bdev, "set host controller mode\n"); 94266077Sdes EOWRITE4(ehci_softc, EHCI_USBMODE_LPM, usbmode); 95266077Sdes} 96266077Sdes 97266077Sdesstatic int 98266077Sdestegra_ehci_probe(device_t dev) 99266077Sdes{ 100266077Sdes 101266077Sdes if (!ofw_bus_status_okay(dev)) 102266077Sdes return (ENXIO); 103266077Sdes 104266077Sdes if (ofw_bus_search_compatible(dev, compat_data)->ocd_data != 0) { 105266077Sdes device_set_desc(dev, "Nvidia Tegra EHCI controller"); 106266077Sdes return (BUS_PROBE_DEFAULT); 107266077Sdes } 108266077Sdes return (ENXIO); 109266077Sdes} 110266077Sdes 111266077Sdesstatic int 112266077Sdestegra_ehci_detach(device_t dev) 113266077Sdes{ 114266077Sdes struct tegra_ehci_softc *sc; 115266077Sdes ehci_softc_t *esc; 116266077Sdes 117266077Sdes sc = device_get_softc(dev); 118266077Sdes 119266077Sdes esc = &sc->ehci_softc; 120266077Sdes if (sc->clk != NULL) 121266077Sdes clk_release(sc->clk); 122266077Sdes if (esc->sc_bus.bdev != NULL) 123266077Sdes device_delete_child(dev, esc->sc_bus.bdev); 124266077Sdes if (esc->sc_flags & EHCI_SCFLG_DONEINIT) 125266077Sdes ehci_detach(esc); 126266077Sdes if (esc->sc_intr_hdl != NULL) 127266077Sdes bus_teardown_intr(dev, esc->sc_irq_res, 128266077Sdes esc->sc_intr_hdl); 129266077Sdes if (sc->ehci_irq_res != NULL) 130266077Sdes bus_release_resource(dev, SYS_RES_IRQ, 0, 131266077Sdes sc->ehci_irq_res); 132266077Sdes if (sc->ehci_mem_res != NULL) 133266077Sdes bus_release_resource(dev, SYS_RES_MEMORY, 0, 134266077Sdes sc->ehci_mem_res); 135266077Sdes if (sc->usb_alloc_called) 136266077Sdes usb_bus_mem_free_all(&esc->sc_bus, &ehci_iterate_hw_softc); 137266077Sdes 138266077Sdes /* During module unload there are lots of children leftover. */ 139266077Sdes device_delete_children(dev); 140266077Sdes 141266077Sdes return (0); 142266077Sdes} 143266077Sdes 144266077Sdesstatic int 145266077Sdestegra_ehci_attach(device_t dev) 146266077Sdes{ 147266077Sdes struct tegra_ehci_softc *sc; 148266077Sdes ehci_softc_t *esc; 149266077Sdes int rv, rid; 150266077Sdes uint64_t freq; 151266077Sdes phandle_t node; 152266077Sdes 153266077Sdes sc = device_get_softc(dev); 154266077Sdes sc->dev = dev; 155266077Sdes node = ofw_bus_get_node(dev); 156266077Sdes esc = &sc->ehci_softc; 157266077Sdes 158266077Sdes /* Allocate resources. */ 159266077Sdes rid = 0; 160266077Sdes sc->ehci_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 161266077Sdes RF_ACTIVE | RF_SHAREABLE); 162266077Sdes if (sc->ehci_mem_res == NULL) { 163266077Sdes device_printf(dev, "Cannot allocate memory resources\n"); 164266077Sdes rv = ENXIO; 165266077Sdes goto out; 166266077Sdes } 167266077Sdes 168266077Sdes rid = 0; 169266077Sdes sc->ehci_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 170266077Sdes RF_ACTIVE); 171266077Sdes if (sc->ehci_irq_res == NULL) { 172266077Sdes device_printf(dev, "Cannot allocate IRQ resources\n"); 173266077Sdes rv = ENXIO; 174266077Sdes goto out; 175266077Sdes } 176266077Sdes 177266077Sdes rv = hwreset_get_by_ofw_name(dev, 0, "usb", &sc->reset); 178266077Sdes if (rv != 0) { 179 device_printf(dev, "Cannot get reset\n"); 180 rv = ENXIO; 181 goto out; 182 } 183 184 rv = phy_get_by_ofw_property(sc->dev, 0, "nvidia,phy", &sc->phy); 185 if (rv != 0) { 186 device_printf(sc->dev, "Cannot get 'nvidia,phy' phy\n"); 187 rv = ENXIO; 188 goto out; 189 } 190 191 rv = clk_get_by_ofw_index(sc->dev, 0, 0, &sc->clk); 192 if (rv != 0) { 193 device_printf(dev, "Cannot get clock\n"); 194 goto out; 195 } 196 197 rv = clk_enable(sc->clk); 198 if (rv != 0) { 199 device_printf(dev, "Cannot enable clock\n"); 200 goto out; 201 } 202 203 freq = 0; 204 rv = clk_get_freq(sc->clk, &freq); 205 if (rv != 0) { 206 device_printf(dev, "Cannot get clock frequency\n"); 207 goto out; 208 } 209 210 rv = hwreset_deassert(sc->reset); 211 if (rv != 0) { 212 device_printf(dev, "Cannot clear reset: %d\n", rv); 213 rv = ENXIO; 214 goto out; 215 } 216 217 rv = phy_enable(sc->dev, sc->phy); 218 if (rv != 0) { 219 device_printf(dev, "Cannot enable phy: %d\n", rv); 220 goto out; 221 } 222 223 /* Fill data for EHCI driver. */ 224 esc->sc_vendor_get_port_speed = ehci_get_port_speed_hostc; 225 esc->sc_vendor_post_reset = tegra_ehci_post_reset; 226 esc->sc_io_tag = rman_get_bustag(sc->ehci_mem_res); 227 esc->sc_bus.parent = dev; 228 esc->sc_bus.devices = esc->sc_devices; 229 esc->sc_bus.devices_max = EHCI_MAX_DEVICES; 230 esc->sc_bus.dma_bits = 32; 231 232 /* Allocate all DMA memory. */ 233 rv = usb_bus_mem_alloc_all(&esc->sc_bus, USB_GET_DMA_TAG(dev), 234 &ehci_iterate_hw_softc); 235 sc->usb_alloc_called = 1; 236 if (rv != 0) { 237 device_printf(dev, "usb_bus_mem_alloc_all() failed\n"); 238 rv = ENOMEM; 239 goto out; 240 } 241 242 /* 243 * Set handle to USB related registers subregion used by 244 * generic EHCI driver. 245 */ 246 rv = bus_space_subregion(esc->sc_io_tag, 247 rman_get_bushandle(sc->ehci_mem_res), 248 TEGRA_EHCI_REG_OFF, TEGRA_EHCI_REG_SIZE, &esc->sc_io_hdl); 249 if (rv != 0) { 250 device_printf(dev, "Could not create USB memory subregion\n"); 251 rv = ENXIO; 252 goto out; 253 } 254 255 /* Setup interrupt handler. */ 256 rv = bus_setup_intr(dev, sc->ehci_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 257 NULL, (driver_intr_t *)ehci_interrupt, esc, &esc->sc_intr_hdl); 258 if (rv != 0) { 259 device_printf(dev, "Could not setup IRQ\n"); 260 goto out; 261 } 262 263 /* Add USB bus device. */ 264 esc->sc_bus.bdev = device_add_child(dev, "usbus", -1); 265 if (esc->sc_bus.bdev == NULL) { 266 device_printf(dev, "Could not add USB device\n"); 267 goto out; 268 } 269 device_set_ivars(esc->sc_bus.bdev, &esc->sc_bus); 270 271 esc->sc_id_vendor = USB_VENDOR_FREESCALE; 272 strlcpy(esc->sc_vendor, "Nvidia", sizeof(esc->sc_vendor)); 273 274 /* Set flags that affect ehci_init() behavior. */ 275 esc->sc_flags |= EHCI_SCFLG_TT; 276 esc->sc_flags |= EHCI_SCFLG_NORESTERM; 277 rv = ehci_init(esc); 278 if (rv != 0) { 279 device_printf(dev, "USB init failed: %d\n", 280 rv); 281 goto out; 282 } 283 esc->sc_flags |= EHCI_SCFLG_DONEINIT; 284 285 /* Probe the bus. */ 286 rv = device_probe_and_attach(esc->sc_bus.bdev); 287 if (rv != 0) { 288 device_printf(dev, 289 "device_probe_and_attach() failed\n"); 290 goto out; 291 } 292 return (0); 293 294out: 295 tegra_ehci_detach(dev); 296 return (rv); 297} 298 299static device_method_t ehci_methods[] = { 300 /* Device interface */ 301 DEVMETHOD(device_probe, tegra_ehci_probe), 302 DEVMETHOD(device_attach, tegra_ehci_attach), 303 DEVMETHOD(device_detach, tegra_ehci_detach), 304 DEVMETHOD(device_suspend, bus_generic_suspend), 305 DEVMETHOD(device_resume, bus_generic_resume), 306 DEVMETHOD(device_shutdown, bus_generic_shutdown), 307 308 /* Bus interface */ 309 DEVMETHOD(bus_print_child, bus_generic_print_child), 310 311 DEVMETHOD_END 312}; 313 314static devclass_t ehci_devclass; 315static DEFINE_CLASS_0(ehci, ehci_driver, ehci_methods, 316 sizeof(struct tegra_ehci_softc)); 317DRIVER_MODULE(tegra_ehci, simplebus, ehci_driver, ehci_devclass, NULL, NULL); 318MODULE_DEPEND(tegra_ehci, usb, 1, 1, 1); 319