FreeBSD kernel usb device Code
generic_ehci_fdt.c
Go to the documentation of this file.
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
51#include <dev/usb/usb_bus.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
66struct clk_list {
67 TAILQ_ENTRY(clk_list) next;
68 clk_t clk;
69};
70
71struct hwrst_list {
72 TAILQ_ENTRY(hwrst_list) next;
73 hwreset_t rst;
74};
75
76struct phy_list {
77 TAILQ_ENTRY(phy_list) next;
78 phy_t phy;
79};
80
83
85 TAILQ_HEAD(, hwrst_list) rst_list;
87};
88
89static device_probe_t generic_ehci_fdt_probe;
90static device_attach_t generic_ehci_fdt_attach;
91static device_detach_t generic_ehci_fdt_detach;
92
93static int
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
107static int
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
168 if (err != 0)
169 goto error;
170
171 return (0);
172
173error:
175 return (err);
176}
177
178static int
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
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
228static 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
237DEFINE_CLASS_1(ehci, ehci_fdt_driver, ehci_fdt_methods,
239
240static devclass_t ehci_fdt_devclass;
241
242DRIVER_MODULE(generic_ehci, simplebus, ehci_fdt_driver, ehci_fdt_devclass, 0, 0);
243MODULE_DEPEND(generic_ehci, usb, 1, 1, 1);
struct ehci_itd * next
Definition: ehci.h:29
driver_t generic_ehci_driver
Definition: generic_ehci.c:186
device_detach_t generic_ehci_detach
Definition: generic_ehci.h:40
device_attach_t generic_ehci_attach
Definition: generic_ehci.h:39
DRIVER_MODULE(generic_ehci, simplebus, ehci_fdt_driver, ehci_fdt_devclass, 0, 0)
static device_method_t ehci_fdt_methods[]
DEFINE_CLASS_1(ehci, ehci_fdt_driver, ehci_fdt_methods, sizeof(ehci_softc_t), generic_ehci_driver)
static device_detach_t generic_ehci_fdt_detach
static device_probe_t generic_ehci_fdt_probe
MODULE_DEPEND(generic_ehci, usb, 1, 1, 1)
__FBSDID("$FreeBSD$")
static device_attach_t generic_ehci_fdt_attach
static devclass_t ehci_fdt_devclass
struct @109 error
device_t dev
TAILQ_HEAD(, urb) bsd_urb_list
INTERFACE usb
Definition: usb_if.m:35