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