a10_ehci.c revision 330897
123462Sjmg/*- 250476Speter * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 323462Sjmg * 4152306Sru * Copyright (c) 2012 Ganbold Tsagaankhuu <ganbold@freebsd.org> 5152306Sru * All rights reserved. 679538Sru * 71079Swollman * Redistribution and use in source and binary forms, with or without 816996Swosch * modification, are permitted provided that the following conditions 9739Sache * are met: 10739Sache * 1. Redistributions of source code must retain the above copyright 111079Swollman * notice, this list of conditions and the following disclaimer. 1276175Sschweikh * 2. Redistributions in binary form must reproduce the above copyright 13152306Sru * notice, this list of conditions and the following disclaimer in the 141079Swollman * documentation and/or other materials provided with the distribution. 152Srgrimes * 1676175Sschweikh * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 171079Swollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1824091Smpp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1968716Sru * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 201079Swollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2176175Sschweikh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 221079Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 231079Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 241079Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2557731Ssheldonh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2657731Ssheldonh * SUCH DAMAGE. 271079Swollman */ 281079Swollman 291079Swollman/* 3057731Ssheldonh * Allwinner A10 attachment driver for the USB Enhanced Host Controller. 3157731Ssheldonh */ 3257731Ssheldonh 3376175Sschweikh#include <sys/cdefs.h> 341079Swollman__FBSDID("$FreeBSD: stable/11/sys/arm/allwinner/a10_ehci.c 330897 2018-03-14 03:19:51Z eadler $"); 351079Swollman 361079Swollman#include "opt_bus.h" 372Srgrimes 381079Swollman#include <sys/param.h> 391079Swollman#include <sys/systm.h> 402Srgrimes#include <sys/bus.h> 4157731Ssheldonh#include <sys/rman.h> 4257731Ssheldonh#include <sys/condvar.h> 431079Swollman#include <sys/kernel.h> 4476175Sschweikh#include <sys/module.h> 451079Swollman 461079Swollman#include <machine/bus.h> 4776175Sschweikh#include <dev/ofw/ofw_bus.h> 481079Swollman#include <dev/ofw/ofw_bus_subr.h> 491079Swollman 50152306Sru#include <dev/usb/usb.h> 511079Swollman#include <dev/usb/usbdi.h> 521079Swollman 531079Swollman#include <dev/usb/usb_core.h> 541079Swollman#include <dev/usb/usb_busdma.h> 552Srgrimes#include <dev/usb/usb_process.h> 561079Swollman#include <dev/usb/usb_util.h> 571079Swollman 581079Swollman#include <dev/usb/usb_controller.h> 591079Swollman#include <dev/usb/usb_bus.h> 601079Swollman#include <dev/usb/controller/ehci.h> 611079Swollman#include <dev/usb/controller/ehcireg.h> 6276175Sschweikh 631079Swollman#include <arm/allwinner/aw_machdep.h> 641079Swollman#include <dev/extres/clk/clk.h> 652Srgrimes#include <dev/extres/hwreset/hwreset.h> 662Srgrimes#include <dev/extres/phy/phy.h> 672Srgrimes 681079Swollman#define EHCI_HC_DEVSTR "Allwinner Integrated USB 2.0 controller" 6989612Smpp 7076175Sschweikh#define SW_USB_PMU_IRQ_ENABLE 0x800 71131530Sru 72131530Sru#define SW_SDRAM_REG_HPCR_USB1 (0x250 + ((1 << 2) * 4)) 731079Swollman#define SW_SDRAM_REG_HPCR_USB2 (0x250 + ((1 << 2) * 5)) 741079Swollman#define SW_SDRAM_BP_HPCR_ACCESS (1 << 0) 751079Swollman 761079Swollman#define SW_ULPI_BYPASS (1 << 0) 771079Swollman#define SW_AHB_INCRX_ALIGN (1 << 8) 7857731Ssheldonh#define SW_AHB_INCR4 (1 << 9) 7957731Ssheldonh#define SW_AHB_INCR8 (1 << 10) 801079Swollman 811079Swollman#define USB_CONF(d) \ 8246068Swosch (void *)ofw_bus_search_compatible((d), compat_data)->ocd_data 832Srgrimes 84131530Sru#define A10_READ_4(sc, reg) \ 85131530Sru bus_space_read_4((sc)->sc_io_tag, (sc)->sc_io_hdl, reg) 862Srgrimes 871079Swollman#define A10_WRITE_4(sc, reg, data) \ 882Srgrimes bus_space_write_4((sc)->sc_io_tag, (sc)->sc_io_hdl, reg, data) 8957731Ssheldonh 9057731Ssheldonhstatic device_attach_t a10_ehci_attach; 911079Swollmanstatic device_detach_t a10_ehci_detach; 921079Swollman 931079Swollmanstruct aw_ehci_softc { 94131530Sru ehci_softc_t sc; 95131530Sru clk_t clk; 9676175Sschweikh hwreset_t rst; 971079Swollman phy_t phy; 98131530Sru}; 99131530Sru 1001079Swollmanstruct aw_ehci_conf { 101131530Sru bool sdram_init; 102131530Sru}; 1031079Swollman 1041079Swollmanstatic const struct aw_ehci_conf a10_ehci_conf = { 1051079Swollman .sdram_init = true, 1061079Swollman}; 1071079Swollman 1081079Swollmanstatic const struct aw_ehci_conf a31_ehci_conf = { 1091079Swollman .sdram_init = false, 1101079Swollman}; 1111079Swollman 1121079Swollmanstatic struct ofw_compat_data compat_data[] = { 1131079Swollman { "allwinner,sun4i-a10-ehci", (uintptr_t)&a10_ehci_conf }, 1141079Swollman { "allwinner,sun5i-a13-ehci", (uintptr_t)&a10_ehci_conf }, 11557731Ssheldonh { "allwinner,sun6i-a31-ehci", (uintptr_t)&a31_ehci_conf }, 11657731Ssheldonh { "allwinner,sun7i-a20-ehci", (uintptr_t)&a10_ehci_conf }, 117131530Sru { "allwinner,sun8i-a83t-ehci", (uintptr_t)&a31_ehci_conf }, 118131530Sru { "allwinner,sun8i-h3-ehci", (uintptr_t)&a31_ehci_conf }, 119131530Sru { NULL, (uintptr_t)NULL } 120131530Sru}; 12176175Sschweikh 1221079Swollmanstatic int 1231079Swollmana10_ehci_probe(device_t self) 1241079Swollman{ 1251079Swollman 1261079Swollman if (!ofw_bus_status_okay(self)) 1271079Swollman return (ENXIO); 1281079Swollman 12976175Sschweikh if (ofw_bus_search_compatible(self, compat_data)->ocd_data == 0) 1301079Swollman return (ENXIO); 1311079Swollman 1321079Swollman device_set_desc(self, EHCI_HC_DEVSTR); 133739Sache 1341079Swollman return (BUS_PROBE_DEFAULT); 135131530Sru} 136131530Sru 1371079Swollmanstatic int 13857731Ssheldonha10_ehci_attach(device_t self) 13957731Ssheldonh{ 1401079Swollman struct aw_ehci_softc *aw_sc = device_get_softc(self); 1411079Swollman ehci_softc_t *sc = &aw_sc->sc; 1421079Swollman const struct aw_ehci_conf *conf; 1431079Swollman bus_space_handle_t bsh; 1441079Swollman int err; 1451079Swollman int rid; 1461079Swollman uint32_t reg_value = 0; 1471079Swollman 14876175Sschweikh conf = USB_CONF(self); 1491079Swollman 15076175Sschweikh /* initialise some bus fields */ 15171895Sru sc->sc_bus.parent = self; 1521079Swollman sc->sc_bus.devices = sc->sc_devices; 153131530Sru sc->sc_bus.devices_max = EHCI_MAX_DEVICES; 154131530Sru sc->sc_bus.dma_bits = 32; 1551079Swollman 1561079Swollman /* get all DMA memory */ 157131530Sru if (usb_bus_mem_alloc_all(&sc->sc_bus, 158131530Sru USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc)) { 1592Srgrimes return (ENOMEM); 1601079Swollman } 1612Srgrimes 16279727Sschweikh sc->sc_bus.usbrev = USB_REV_2_0; 1632Srgrimes 1642Srgrimes rid = 0; 16579727Sschweikh sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid, RF_ACTIVE); 16679727Sschweikh if (!sc->sc_io_res) { 1672Srgrimes device_printf(self, "Could not map memory\n"); 16879727Sschweikh goto error; 1692Srgrimes } 17079727Sschweikh 1712Srgrimes sc->sc_io_tag = rman_get_bustag(sc->sc_io_res); 17279727Sschweikh sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res); 1732Srgrimes bsh = rman_get_bushandle(sc->sc_io_res); 17479727Sschweikh 17579727Sschweikh sc->sc_io_size = rman_get_size(sc->sc_io_res); 1762Srgrimes 17779727Sschweikh if (bus_space_subregion(sc->sc_io_tag, bsh, 0x00, 1781079Swollman sc->sc_io_size, &sc->sc_io_hdl) != 0) 1791079Swollman panic("%s: unable to subregion USB host registers", 1801079Swollman device_get_name(self)); 1811079Swollman 18271895Sru rid = 0; 1831079Swollman sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid, 18457731Ssheldonh RF_SHAREABLE | RF_ACTIVE); 18557731Ssheldonh if (sc->sc_irq_res == NULL) { 1861079Swollman device_printf(self, "Could not allocate irq\n"); 1871079Swollman goto error; 1881079Swollman } 1891079Swollman sc->sc_bus.bdev = device_add_child(self, "usbus", -1); 1901079Swollman if (!sc->sc_bus.bdev) { 1911079Swollman device_printf(self, "Could not add USB device\n"); 1921079Swollman goto error; 1931079Swollman } 1941079Swollman device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus); 1951079Swollman device_set_desc(sc->sc_bus.bdev, EHCI_HC_DEVSTR); 1961079Swollman 19757731Ssheldonh sprintf(sc->sc_vendor, "Allwinner"); 19857731Ssheldonh 19957731Ssheldonh err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 20057731Ssheldonh NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl); 20176175Sschweikh if (err) { 2021079Swollman device_printf(self, "Could not setup irq, %d\n", err); 203739Sache sc->sc_intr_hdl = NULL; 204739Sache goto error; 205131530Sru } 206131530Sru 2071079Swollman sc->sc_flags |= EHCI_SCFLG_DONTRESET; 2081079Swollman 2091079Swollman /* De-assert reset */ 2102Srgrimes if (hwreset_get_by_ofw_idx(self, 0, 0, &aw_sc->rst) == 0) { 2112Srgrimes err = hwreset_deassert(aw_sc->rst); 212140561Sru if (err != 0) { 213140561Sru device_printf(self, "Could not de-assert reset\n"); 214140561Sru goto error; 215140561Sru } 216140561Sru } 217140561Sru 218140561Sru /* Enable clock for USB */ 219140561Sru err = clk_get_by_ofw_index(self, 0, 0, &aw_sc->clk); 220140561Sru if (err != 0) { 221140561Sru device_printf(self, "Could not get clock\n"); 222140561Sru goto error; 223140561Sru } 224140561Sru err = clk_enable(aw_sc->clk); 225140561Sru if (err != 0) { 226140561Sru device_printf(self, "Could not enable clock\n"); 227140561Sru goto error; 228140561Sru } 2291079Swollman 2302Srgrimes /* Enable USB PHY */ 2312Srgrimes err = phy_get_by_ofw_name(self, 0, "usb", &aw_sc->phy); 23257731Ssheldonh if (err != 0) { 23357731Ssheldonh device_printf(self, "Could not get phy\n"); 2341079Swollman goto error; 235739Sache } 236739Sache err = phy_enable(self, aw_sc->phy); 237131530Sru if (err != 0) { 238131530Sru device_printf(self, "Could not enable phy\n"); 239739Sache goto error; 240131530Sru } 241131530Sru 24276175Sschweikh /* Enable passby */ 2431079Swollman reg_value = A10_READ_4(sc, SW_USB_PMU_IRQ_ENABLE); 2441079Swollman reg_value |= SW_AHB_INCR8; /* AHB INCR8 enable */ 245739Sache reg_value |= SW_AHB_INCR4; /* AHB burst type INCR4 enable */ 2461079Swollman reg_value |= SW_AHB_INCRX_ALIGN; /* AHB INCRX align enable */ 2472Srgrimes reg_value |= SW_ULPI_BYPASS; /* ULPI bypass enable */ 2482Srgrimes A10_WRITE_4(sc, SW_USB_PMU_IRQ_ENABLE, reg_value); 2492Srgrimes 250 /* Configure port */ 251 if (conf->sdram_init) { 252 reg_value = A10_READ_4(sc, SW_SDRAM_REG_HPCR_USB2); 253 reg_value |= SW_SDRAM_BP_HPCR_ACCESS; 254 A10_WRITE_4(sc, SW_SDRAM_REG_HPCR_USB2, reg_value); 255 } 256 257 err = ehci_init(sc); 258 if (!err) { 259 err = device_probe_and_attach(sc->sc_bus.bdev); 260 } 261 if (err) { 262 device_printf(self, "USB init failed err=%d\n", err); 263 goto error; 264 } 265 return (0); 266 267error: 268 if (aw_sc->clk != NULL) { 269 clk_disable(aw_sc->clk); 270 clk_release(aw_sc->clk); 271 } 272 a10_ehci_detach(self); 273 return (ENXIO); 274} 275 276static int 277a10_ehci_detach(device_t self) 278{ 279 struct aw_ehci_softc *aw_sc = device_get_softc(self); 280 ehci_softc_t *sc = &aw_sc->sc; 281 const struct aw_ehci_conf *conf; 282 int err; 283 uint32_t reg_value = 0; 284 285 conf = USB_CONF(self); 286 287 /* during module unload there are lots of children leftover */ 288 device_delete_children(self); 289 290 if (sc->sc_irq_res && sc->sc_intr_hdl) { 291 /* 292 * only call ehci_detach() after ehci_init() 293 */ 294 ehci_detach(sc); 295 296 err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl); 297 298 if (err) 299 /* XXX or should we panic? */ 300 device_printf(self, "Could not tear down irq, %d\n", 301 err); 302 sc->sc_intr_hdl = NULL; 303 } 304 305 if (sc->sc_irq_res) { 306 bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res); 307 sc->sc_irq_res = NULL; 308 } 309 if (sc->sc_io_res) { 310 bus_release_resource(self, SYS_RES_MEMORY, 0, 311 sc->sc_io_res); 312 sc->sc_io_res = NULL; 313 } 314 usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc); 315 316 /* Disable configure port */ 317 if (conf->sdram_init) { 318 reg_value = A10_READ_4(sc, SW_SDRAM_REG_HPCR_USB2); 319 reg_value &= ~SW_SDRAM_BP_HPCR_ACCESS; 320 A10_WRITE_4(sc, SW_SDRAM_REG_HPCR_USB2, reg_value); 321 } 322 323 /* Disable passby */ 324 reg_value = A10_READ_4(sc, SW_USB_PMU_IRQ_ENABLE); 325 reg_value &= ~SW_AHB_INCR8; /* AHB INCR8 disable */ 326 reg_value &= ~SW_AHB_INCR4; /* AHB burst type INCR4 disable */ 327 reg_value &= ~SW_AHB_INCRX_ALIGN; /* AHB INCRX align disable */ 328 reg_value &= ~SW_ULPI_BYPASS; /* ULPI bypass disable */ 329 A10_WRITE_4(sc, SW_USB_PMU_IRQ_ENABLE, reg_value); 330 331 /* Disable clock for USB */ 332 if (aw_sc->clk != NULL) { 333 clk_disable(aw_sc->clk); 334 clk_release(aw_sc->clk); 335 } 336 337 /* Assert reset */ 338 if (aw_sc->rst != NULL) { 339 hwreset_assert(aw_sc->rst); 340 hwreset_release(aw_sc->rst); 341 } 342 343 return (0); 344} 345 346static device_method_t ehci_methods[] = { 347 /* Device interface */ 348 DEVMETHOD(device_probe, a10_ehci_probe), 349 DEVMETHOD(device_attach, a10_ehci_attach), 350 DEVMETHOD(device_detach, a10_ehci_detach), 351 DEVMETHOD(device_suspend, bus_generic_suspend), 352 DEVMETHOD(device_resume, bus_generic_resume), 353 DEVMETHOD(device_shutdown, bus_generic_shutdown), 354 355 DEVMETHOD_END 356}; 357 358static driver_t ehci_driver = { 359 .name = "ehci", 360 .methods = ehci_methods, 361 .size = sizeof(struct aw_ehci_softc), 362}; 363 364static devclass_t ehci_devclass; 365 366DRIVER_MODULE(ehci, simplebus, ehci_driver, ehci_devclass, 0, 0); 367MODULE_DEPEND(ehci, usb, 1, 1, 1); 368