1 /*-
2 * Copyright (c) 2012 Ganbold Tsagaankhuu <ganbold@freebsd.org>
3 * Copyright (c) 2016 The FreeBSD Foundation
4 * All rights reserved.
5 *
6 * This software was developed by Andrew Turner under
7 * sponsorship from the FreeBSD Foundation.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #include "opt_bus.h"
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/bus.h>
36 #include <sys/condvar.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39
40 #include <dev/usb/usb.h>
41 #include <dev/usb/usbdi.h>
42
43 #include <dev/usb/usb_core.h>
44 #include <dev/usb/usb_busdma.h>
45 #include <dev/usb/usb_process.h>
46
47 #include <dev/usb/usb_controller.h>
48 #include <dev/usb/usb_bus.h>
49 #include <dev/usb/controller/ehci.h>
50
51 #include <dev/fdt/fdt_common.h>
52 #include <dev/ofw/openfirm.h>
53 #include <dev/ofw/ofw_bus.h>
54 #include <dev/ofw/ofw_bus_subr.h>
55
56 #include <dev/clk/clk.h>
57 #include <dev/hwreset/hwreset.h>
58 #include <dev/phy/phy.h>
59 #include <dev/phy/phy_usb.h>
60
61 #include "generic_ehci.h"
62
63 struct clk_list {
64 TAILQ_ENTRY(clk_list) next;
65 clk_t clk;
66 };
67
68 struct hwrst_list {
69 TAILQ_ENTRY(hwrst_list) next;
70 hwreset_t rst;
71 };
72
73 struct phy_list {
74 TAILQ_ENTRY(phy_list) next;
75 phy_t phy;
76 };
77
78 struct generic_ehci_fdt_softc {
79 ehci_softc_t ehci_sc;
80
81 TAILQ_HEAD(, clk_list) clk_list;
82 TAILQ_HEAD(, hwrst_list) rst_list;
83 TAILQ_HEAD(, phy_list) phy_list;
84 };
85
86 static device_probe_t generic_ehci_fdt_probe;
87 static device_attach_t generic_ehci_fdt_attach;
88 static device_detach_t generic_ehci_fdt_detach;
89
90 static int
generic_ehci_fdt_probe(device_t self)91 generic_ehci_fdt_probe(device_t self)
92 {
93
94 if (!ofw_bus_status_okay(self))
95 return (ENXIO);
96
97 if (!ofw_bus_is_compatible(self, "generic-ehci"))
98 return (ENXIO);
99
100 device_set_desc(self, "Generic EHCI Controller");
101 return (BUS_PROBE_DEFAULT);
102 }
103
104 static int
generic_ehci_fdt_attach(device_t dev)105 generic_ehci_fdt_attach(device_t dev)
106 {
107 int err;
108 struct generic_ehci_fdt_softc *sc;
109 struct clk_list *clkp;
110 clk_t clk;
111 struct hwrst_list *rstp;
112 hwreset_t rst;
113 struct phy_list *phyp;
114 phy_t phy;
115 int off;
116
117 sc = device_get_softc(dev);
118
119 TAILQ_INIT(&sc->clk_list);
120 /* Enable clock */
121 for (off = 0; clk_get_by_ofw_index(dev, 0, off, &clk) == 0; off++) {
122 err = clk_enable(clk);
123 if (err != 0) {
124 device_printf(dev, "Could not enable clock %s\n",
125 clk_get_name(clk));
126 goto error;
127 }
128 clkp = malloc(sizeof(*clkp), M_DEVBUF, M_WAITOK | M_ZERO);
129 clkp->clk = clk;
130 TAILQ_INSERT_TAIL(&sc->clk_list, clkp, next);
131 }
132
133 /* De-assert reset */
134 TAILQ_INIT(&sc->rst_list);
135 for (off = 0; hwreset_get_by_ofw_idx(dev, 0, off, &rst) == 0; off++) {
136 err = hwreset_deassert(rst);
137 if (err != 0) {
138 device_printf(dev, "Could not de-assert reset\n");
139 goto error;
140 }
141 rstp = malloc(sizeof(*rstp), M_DEVBUF, M_WAITOK | M_ZERO);
142 rstp->rst = rst;
143 TAILQ_INSERT_TAIL(&sc->rst_list, rstp, next);
144 }
145
146 /* Enable USB PHY */
147 TAILQ_INIT(&sc->phy_list);
148 for (off = 0; phy_get_by_ofw_idx(dev, 0, off, &phy) == 0; off++) {
149 err = phy_usb_set_mode(phy, PHY_USB_MODE_HOST);
150 if (err != 0) {
151 device_printf(dev, "Could not set phy to host mode\n");
152 goto error;
153 }
154 err = phy_enable(phy);
155 if (err != 0) {
156 device_printf(dev, "Could not enable phy\n");
157 goto error;
158 }
159 phyp = malloc(sizeof(*phyp), M_DEVBUF, M_WAITOK | M_ZERO);
160 phyp->phy = phy;
161 TAILQ_INSERT_TAIL(&sc->phy_list, phyp, next);
162 }
163
164 err = generic_ehci_attach(dev);
165 if (err != 0)
166 goto error;
167
168 return (0);
169
170 error:
171 generic_ehci_fdt_detach(dev);
172 return (err);
173 }
174
175 static int
generic_ehci_fdt_detach(device_t dev)176 generic_ehci_fdt_detach(device_t dev)
177 {
178 struct generic_ehci_fdt_softc *sc;
179 struct clk_list *clk, *clk_tmp;
180 struct hwrst_list *rst, *rst_tmp;
181 struct phy_list *phy, *phy_tmp;
182 int err;
183
184 err = generic_ehci_detach(dev);
185 if (err != 0)
186 return (err);
187
188 sc = device_get_softc(dev);
189
190 /* Disable clock */
191 TAILQ_FOREACH_SAFE(clk, &sc->clk_list, next, clk_tmp) {
192 err = clk_disable(clk->clk);
193 if (err != 0)
194 device_printf(dev, "Could not disable clock %s\n",
195 clk_get_name(clk->clk));
196 err = clk_release(clk->clk);
197 if (err != 0)
198 device_printf(dev, "Could not release clock %s\n",
199 clk_get_name(clk->clk));
200 TAILQ_REMOVE(&sc->clk_list, clk, next);
201 free(clk, M_DEVBUF);
202 }
203
204 /* Assert reset */
205 TAILQ_FOREACH_SAFE(rst, &sc->rst_list, next, rst_tmp) {
206 hwreset_assert(rst->rst);
207 hwreset_release(rst->rst);
208 TAILQ_REMOVE(&sc->rst_list, rst, next);
209 free(rst, M_DEVBUF);
210 }
211
212 /* Disable phys */
213 TAILQ_FOREACH_SAFE(phy, &sc->phy_list, next, phy_tmp) {
214 err = phy_disable(phy->phy);
215 if (err != 0)
216 device_printf(dev, "Could not disable phy\n");
217 phy_release(phy->phy);
218 TAILQ_REMOVE(&sc->phy_list, phy, next);
219 free(phy, M_DEVBUF);
220 }
221
222 return (0);
223 }
224
225 static device_method_t ehci_fdt_methods[] = {
226 /* Device interface */
227 DEVMETHOD(device_probe, generic_ehci_fdt_probe),
228 DEVMETHOD(device_attach, generic_ehci_fdt_attach),
229 DEVMETHOD(device_detach, generic_ehci_fdt_detach),
230
231 DEVMETHOD_END
232 };
233
234 DEFINE_CLASS_1(ehci, ehci_fdt_driver, ehci_fdt_methods,
235 sizeof(ehci_softc_t), generic_ehci_driver);
236
237 DRIVER_MODULE(generic_ehci, simplebus, ehci_fdt_driver, 0, 0);
238 MODULE_DEPEND(generic_ehci, usb, 1, 1, 1);
239