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