1 1.1 yurix /* $NetBSD $ */ 2 1.1 yurix 3 1.1 yurix /*- 4 1.1 yurix * Copyright (c) 2026 The NetBSD Foundation, Inc. 5 1.1 yurix * All rights reserved. 6 1.1 yurix * 7 1.1 yurix * This code is derived from software contributed to The NetBSD Foundation 8 1.1 yurix * by Yuri Honegger. 9 1.1 yurix * 10 1.1 yurix * Redistribution and use in source and binary forms, with or without 11 1.1 yurix * modification, are permitted provided that the following conditions 12 1.1 yurix * are met: 13 1.1 yurix * 1. Redistributions of source code must retain the above copyright 14 1.1 yurix * notice, this list of conditions and the following disclaimer. 15 1.1 yurix * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 yurix * notice, this list of conditions and the following disclaimer in the 17 1.1 yurix * documentation and/or other materials provided with the distribution. 18 1.1 yurix * 19 1.1 yurix * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.1 yurix * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.1 yurix * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.1 yurix * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.1 yurix * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.1 yurix * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.1 yurix * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.1 yurix * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.1 yurix * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.1 yurix * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 yurix * POSSIBILITY OF SUCH DAMAGE. 30 1.1 yurix */ 31 1.1 yurix 32 1.1 yurix /* 33 1.1 yurix * Driver for the USB1.1 OHCI port on the TI AM18XX family of SoCs. 34 1.1 yurix */ 35 1.1 yurix 36 1.1 yurix #include <sys/param.h> 37 1.1 yurix #include <sys/bus.h> 38 1.1 yurix #include <sys/cdefs.h> 39 1.1 yurix #include <sys/device.h> 40 1.1 yurix 41 1.1 yurix #include <dev/clk/clk.h> 42 1.1 yurix #include <dev/fdt/fdtvar.h> 43 1.1 yurix #include <dev/usb/usb.h> 44 1.1 yurix #include <dev/usb/usbdi.h> 45 1.1 yurix #include <dev/usb/usbdivar.h> 46 1.1 yurix #include <dev/usb/usb_mem.h> 47 1.1 yurix #include <dev/usb/ohcireg.h> 48 1.1 yurix #include <dev/usb/ohcivar.h> 49 1.1 yurix 50 1.1 yurix struct am18xx_ohci_softc { 51 1.1 yurix struct ohci_softc ohci_sc; 52 1.1 yurix struct clk *sc_clk; 53 1.1 yurix struct fdtbus_phy *sc_phy; 54 1.1 yurix }; 55 1.1 yurix 56 1.1 yurix static int am18xx_ohci_match(device_t, cfdata_t, void *); 57 1.1 yurix static void am18xx_ohci_attach(device_t, device_t, void *); 58 1.1 yurix 59 1.1 yurix static int am18xx_ohci_enable_clocks(struct am18xx_ohci_softc *); 60 1.1 yurix 61 1.1 yurix CFATTACH_DECL_NEW(am18xxohci, sizeof(struct am18xx_ohci_softc), 62 1.1 yurix am18xx_ohci_match, am18xx_ohci_attach, NULL, NULL); 63 1.1 yurix 64 1.1 yurix static const struct device_compatible_entry compat_data[] = { 65 1.1 yurix { .compat = "ti,da830-ohci" }, 66 1.1 yurix DEVICE_COMPAT_EOL 67 1.1 yurix }; 68 1.1 yurix 69 1.1 yurix static int 70 1.1 yurix am18xx_ohci_enable_clocks(struct am18xx_ohci_softc *sc) 71 1.1 yurix { 72 1.1 yurix int error; 73 1.1 yurix 74 1.1 yurix error = clk_enable(sc->sc_clk); 75 1.1 yurix if (error != 0) { 76 1.1 yurix return error; 77 1.1 yurix } 78 1.1 yurix 79 1.1 yurix error = fdtbus_phy_enable(sc->sc_phy, true); 80 1.1 yurix if (error != 0) { 81 1.1 yurix return error; 82 1.1 yurix } 83 1.1 yurix 84 1.1 yurix return 0; 85 1.1 yurix } 86 1.1 yurix 87 1.1 yurix int 88 1.1 yurix am18xx_ohci_match(device_t parent, cfdata_t cf, void *aux) 89 1.1 yurix { 90 1.1 yurix struct fdt_attach_args *const faa = aux; 91 1.1 yurix 92 1.1 yurix return of_compatible_match(faa->faa_phandle, compat_data); 93 1.1 yurix } 94 1.1 yurix 95 1.1 yurix void 96 1.1 yurix am18xx_ohci_attach(device_t parent, device_t self, void *aux) 97 1.1 yurix { 98 1.1 yurix struct am18xx_ohci_softc *const sc = device_private(self); 99 1.1 yurix struct fdt_attach_args *const faa = aux; 100 1.1 yurix const int phandle = faa->faa_phandle; 101 1.1 yurix bus_addr_t addr; 102 1.1 yurix bus_size_t size; 103 1.1 yurix char intrstr[128]; 104 1.1 yurix int error; 105 1.1 yurix 106 1.1 yurix sc->ohci_sc.iot = faa->faa_bst; 107 1.1 yurix sc->ohci_sc.sc_dev = self; 108 1.1 yurix sc->ohci_sc.sc_bus.ub_hcpriv = &sc->ohci_sc; 109 1.1 yurix sc->ohci_sc.sc_bus.ub_dmatag = faa->faa_dmat; 110 1.1 yurix sc->ohci_sc.sc_flags = 0; 111 1.1 yurix 112 1.1 yurix /* get USB 1.1 clock */ 113 1.1 yurix sc->sc_clk = fdtbus_clock_get_index(phandle, 0); 114 1.1 yurix if (sc->sc_clk == NULL) { 115 1.1 yurix aprint_error(": couldn't get usb clock\n"); 116 1.1 yurix return; 117 1.1 yurix } 118 1.1 yurix 119 1.1 yurix /* get phy */ 120 1.1 yurix sc->sc_phy = fdtbus_phy_get(phandle, "usb-phy"); 121 1.1 yurix if (sc->sc_phy == NULL) { 122 1.1 yurix aprint_error(": couldn't get usb phy\n"); 123 1.1 yurix return; 124 1.1 yurix } 125 1.1 yurix 126 1.1 yurix /* enable all clocks */ 127 1.1 yurix if (am18xx_ohci_enable_clocks(sc)) { 128 1.1 yurix aprint_error(": couldn't enable clocks\n"); 129 1.1 yurix return; 130 1.1 yurix } 131 1.1 yurix 132 1.1 yurix /* map bus space */ 133 1.1 yurix if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 134 1.1 yurix aprint_error(": couldn't get registers\n"); 135 1.1 yurix return; 136 1.1 yurix } 137 1.1 yurix if (bus_space_map(sc->ohci_sc.iot, addr, size, 0, &sc->ohci_sc.ioh)) { 138 1.1 yurix aprint_error(": couldn't map registers\n"); 139 1.1 yurix return; 140 1.1 yurix } 141 1.1 yurix sc->ohci_sc.sc_size = size; 142 1.1 yurix 143 1.1 yurix /* disable interrupts on the ohci side */ 144 1.1 yurix bus_space_write_4(sc->ohci_sc.iot, sc->ohci_sc.ioh, 145 1.1 yurix OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS); 146 1.1 yurix 147 1.1 yurix /* establish interrupt */ 148 1.1 yurix if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) { 149 1.1 yurix aprint_error(": failed to decode interrupt\n"); 150 1.1 yurix return; 151 1.1 yurix } 152 1.1 yurix void *ih = fdtbus_intr_establish_xname(phandle, 0, IPL_USB, 153 1.1 yurix FDT_INTR_MPSAFE, ohci_intr, &sc->ohci_sc, device_xname(self)); 154 1.1 yurix if (ih == NULL) { 155 1.1 yurix aprint_error_dev(self, ": couldn't install interrupt %s\n", 156 1.1 yurix intrstr); 157 1.1 yurix return; 158 1.1 yurix } 159 1.1 yurix 160 1.1 yurix aprint_normal(": ohci on %s\n", intrstr); 161 1.1 yurix 162 1.1 yurix /* initialize the controller */ 163 1.1 yurix error = ohci_init(&sc->ohci_sc); 164 1.1 yurix if (error) { 165 1.1 yurix aprint_error_dev(self, ": init failed, error=%d\n", error); 166 1.1 yurix return; 167 1.1 yurix } 168 1.1 yurix 169 1.1 yurix sc->ohci_sc.sc_child = config_found(self, &sc->ohci_sc.sc_bus, 170 1.1 yurix usbctlprint, CFARGS_NONE); 171 1.1 yurix } 172