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