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