xref: /freebsd/sys/dev/usb/controller/generic_ehci_fdt.c (revision 9e5787d2284e187abb5b654d924394a65772e004)
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 <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include "opt_bus.h"
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/bus.h>
39 #include <sys/condvar.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
42 
43 #include <dev/usb/usb.h>
44 #include <dev/usb/usbdi.h>
45 
46 #include <dev/usb/usb_core.h>
47 #include <dev/usb/usb_busdma.h>
48 #include <dev/usb/usb_process.h>
49 
50 #include <dev/usb/usb_controller.h>
51 #include <dev/usb/usb_bus.h>
52 #include <dev/usb/controller/ehci.h>
53 
54 #include <dev/fdt/fdt_common.h>
55 #include <dev/ofw/openfirm.h>
56 #include <dev/ofw/ofw_bus.h>
57 #include <dev/ofw/ofw_bus_subr.h>
58 
59 #ifdef EXT_RESOURCES
60 #include <dev/extres/clk/clk.h>
61 #include <dev/extres/hwreset/hwreset.h>
62 #include <dev/extres/phy/phy.h>
63 #include <dev/extres/phy/phy_usb.h>
64 #endif
65 
66 #include "generic_ehci.h"
67 
68 #ifdef EXT_RESOURCES
69 struct clk_list {
70 	TAILQ_ENTRY(clk_list)	next;
71 	clk_t			clk;
72 };
73 
74 
75 struct hwrst_list {
76 	TAILQ_ENTRY(hwrst_list)	next;
77 	hwreset_t		rst;
78 };
79 
80 struct phy_list {
81 	TAILQ_ENTRY(phy_list)	next;
82 	phy_t			phy;
83 };
84 #endif
85 
86 struct generic_ehci_fdt_softc {
87 	ehci_softc_t	ehci_sc;
88 
89 #ifdef EXT_RESOURCES
90 	TAILQ_HEAD(, clk_list)	clk_list;
91 	TAILQ_HEAD(, hwrst_list)	rst_list;
92 	TAILQ_HEAD(, phy_list)		phy_list;
93 #endif
94 };
95 
96 static device_probe_t generic_ehci_fdt_probe;
97 static device_attach_t generic_ehci_fdt_attach;
98 static device_detach_t generic_ehci_fdt_detach;
99 
100 static int
101 generic_ehci_fdt_probe(device_t self)
102 {
103 
104 	if (!ofw_bus_status_okay(self))
105 		return (ENXIO);
106 
107 	if (!ofw_bus_is_compatible(self, "generic-ehci"))
108 		return (ENXIO);
109 
110 	device_set_desc(self, "Generic EHCI Controller");
111 	return (BUS_PROBE_DEFAULT);
112 }
113 
114 static int
115 generic_ehci_fdt_attach(device_t dev)
116 {
117 	int err;
118 #ifdef EXT_RESOURCES
119 	struct generic_ehci_fdt_softc *sc;
120 	struct clk_list *clkp;
121 	clk_t clk;
122 	struct hwrst_list *rstp;
123 	hwreset_t rst;
124 	struct phy_list *phyp;
125 	phy_t phy;
126 	int off;
127 
128 	sc = device_get_softc(dev);
129 
130 	TAILQ_INIT(&sc->clk_list);
131 	/* Enable clock */
132 	for (off = 0; clk_get_by_ofw_index(dev, 0, off, &clk) == 0; off++) {
133 		err = clk_enable(clk);
134 		if (err != 0) {
135 			device_printf(dev, "Could not enable clock %s\n",
136 			    clk_get_name(clk));
137 			goto error;
138 		}
139 		clkp = malloc(sizeof(*clkp), M_DEVBUF, M_WAITOK | M_ZERO);
140 		clkp->clk = clk;
141 		TAILQ_INSERT_TAIL(&sc->clk_list, clkp, next);
142 	}
143 
144 	/* De-assert reset */
145 	TAILQ_INIT(&sc->rst_list);
146 	for (off = 0; hwreset_get_by_ofw_idx(dev, 0, off, &rst) == 0; off++) {
147 		err = hwreset_deassert(rst);
148 		if (err != 0) {
149 			device_printf(dev, "Could not de-assert reset\n");
150 			goto error;
151 		}
152 		rstp = malloc(sizeof(*rstp), M_DEVBUF, M_WAITOK | M_ZERO);
153 		rstp->rst = rst;
154 		TAILQ_INSERT_TAIL(&sc->rst_list, rstp, next);
155 	}
156 
157 	/* Enable USB PHY */
158 	TAILQ_INIT(&sc->phy_list);
159 	for (off = 0; phy_get_by_ofw_idx(dev, 0, off, &phy) == 0; off++) {
160 		err = phy_usb_set_mode(phy, PHY_USB_MODE_HOST);
161 		if (err != 0) {
162 			device_printf(dev, "Could not set phy to host mode\n");
163 			goto error;
164 		}
165 		err = phy_enable(phy);
166 		if (err != 0) {
167 			device_printf(dev, "Could not enable phy\n");
168 			goto error;
169 		}
170 		phyp = malloc(sizeof(*phyp), M_DEVBUF, M_WAITOK | M_ZERO);
171 		phyp->phy = phy;
172 		TAILQ_INSERT_TAIL(&sc->phy_list, phyp, next);
173 	}
174 #endif
175 
176 	err = generic_ehci_attach(dev);
177 	if (err != 0)
178 		goto error;
179 
180 	return (0);
181 
182 error:
183 	generic_ehci_fdt_detach(dev);
184 	return (err);
185 }
186 
187 static int
188 generic_ehci_fdt_detach(device_t dev)
189 {
190 #ifdef EXT_RESOURCES
191 	struct generic_ehci_fdt_softc *sc;
192 	struct clk_list *clk, *clk_tmp;
193 	struct hwrst_list *rst, *rst_tmp;
194 	struct phy_list *phy, *phy_tmp;
195 #endif
196 	int err;
197 
198 	err = generic_ehci_detach(dev);
199 	if (err != 0)
200 		return (err);
201 
202 #ifdef EXT_RESOURCES
203 	sc = device_get_softc(dev);
204 
205 	/* Disable clock */
206 	TAILQ_FOREACH_SAFE(clk, &sc->clk_list, next, clk_tmp) {
207 		err = clk_disable(clk->clk);
208 		if (err != 0)
209 			device_printf(dev, "Could not disable clock %s\n",
210 			    clk_get_name(clk->clk));
211 		err = clk_release(clk->clk);
212 		if (err != 0)
213 			device_printf(dev, "Could not release clock %s\n",
214 			    clk_get_name(clk->clk));
215 		TAILQ_REMOVE(&sc->clk_list, clk, next);
216 		free(clk, M_DEVBUF);
217 	}
218 
219 	/* Assert reset */
220 	TAILQ_FOREACH_SAFE(rst, &sc->rst_list, next, rst_tmp) {
221 		hwreset_assert(rst->rst);
222 		hwreset_release(rst->rst);
223 		TAILQ_REMOVE(&sc->rst_list, rst, next);
224 		free(rst, M_DEVBUF);
225 	}
226 
227 	/* Disable phys */
228 	TAILQ_FOREACH_SAFE(phy, &sc->phy_list, next, phy_tmp) {
229 		err = phy_disable(phy->phy);
230 		if (err != 0)
231 			device_printf(dev, "Could not disable phy\n");
232 		phy_release(phy->phy);
233 		TAILQ_REMOVE(&sc->phy_list, phy, next);
234 		free(phy, M_DEVBUF);
235 	}
236 #endif
237 
238 	return (0);
239 }
240 
241 static device_method_t ehci_fdt_methods[] = {
242 	/* Device interface */
243 	DEVMETHOD(device_probe,		generic_ehci_fdt_probe),
244 	DEVMETHOD(device_attach,	generic_ehci_fdt_attach),
245 	DEVMETHOD(device_detach,	generic_ehci_fdt_detach),
246 
247 	DEVMETHOD_END
248 };
249 
250 DEFINE_CLASS_1(ehci, ehci_fdt_driver, ehci_fdt_methods,
251     sizeof(ehci_softc_t), generic_ehci_driver);
252 
253 static devclass_t ehci_fdt_devclass;
254 
255 DRIVER_MODULE(generic_ehci, simplebus, ehci_fdt_driver, ehci_fdt_devclass, 0, 0);
256 MODULE_DEPEND(generic_ehci, usb, 1, 1, 1);
257