1 /*-
2 * Copyright (c) 2015, Alexander Kabaev <kan@FreeBSD.org>
3 * Copyright (c) 2009, Oleksandr Tymoshenko <gonzo@FreeBSD.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice unmodified, this list of conditions, and the following
11 * disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/rman.h>
36 #include <sys/condvar.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/gpio.h>
40
41 #include <dev/extres/clk/clk.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 #include <dev/usb/usb_util.h>
50
51 #include <dev/usb/usb_controller.h>
52 #include <dev/usb/usb_bus.h>
53 #include <dev/usb/controller/ohci.h>
54 #include <dev/usb/controller/ohcireg.h>
55
56 #include <dev/ofw/ofw_bus.h>
57 #include <dev/ofw/ofw_bus_subr.h>
58
59 #include <dev/gpio/gpiobusvar.h>
60
61 #include <mips/ingenic/jz4780_clock.h>
62 #include <mips/ingenic/jz4780_regs.h>
63
64 static int jz4780_ohci_attach(device_t dev);
65 static int jz4780_ohci_detach(device_t dev);
66 static int jz4780_ohci_probe(device_t dev);
67
68 struct jz4780_ohci_softc
69 {
70 struct ohci_softc sc_ohci;
71 struct gpiobus_pin *gpio_vbus;
72 clk_t clk;
73 };
74
75 static int
76 jz4780_ohci_probe(device_t dev)
77 {
78 if (!ofw_bus_status_okay(dev))
79 return (ENXIO);
80
81 if (!ofw_bus_is_compatible(dev, "ingenic,jz4780-ohci"))
82 return (ENXIO);
83
84 device_set_desc(dev, "Ingenic JZ4780 OHCI");
85
86 return (BUS_PROBE_DEFAULT);
87 }
88
89 static int
90 jz4780_ohci_vbus_gpio_enable(device_t dev, struct jz4780_ohci_softc *sc)
91 {
92 struct gpiobus_pin *gpio_vbus;
93 int error;
94
95 error = ofw_gpiobus_parse_gpios(dev, "ingenic,vbus-gpio", &gpio_vbus);
96 /*
97 * The pin can be mapped already by other device. Assume it also has need
98 * activated and proceed happily.
99 */
100 if (error <= 0)
101 return (0);
102
103 sc->gpio_vbus = gpio_vbus;
104 if (error > 1) {
105 device_printf(dev, "too many vbus gpios\n");
106 return (ENXIO);
107 }
108
109 if (sc->gpio_vbus != NULL) {
110 error = GPIO_PIN_SET(sc->gpio_vbus->dev, sc->gpio_vbus->pin, 1);
111 if (error != 0) {
112 device_printf(dev, "Cannot configure GPIO pin %d on %s\n",
113 sc->gpio_vbus->pin, device_get_nameunit(sc->gpio_vbus->dev));
114 return (error);
115 }
116
117 error = GPIO_PIN_SETFLAGS(sc->gpio_vbus->dev, sc->gpio_vbus->pin,
118 GPIO_PIN_OUTPUT);
119 if (error != 0) {
120 device_printf(dev, "Cannot configure GPIO pin %d on %s\n",
121 sc->gpio_vbus->pin, device_get_nameunit(sc->gpio_vbus->dev));
122 return (error);
123 }
124 }
125 return (0);
126 }
127
128 static int
129 jz4780_ohci_clk_enable(device_t dev)
130 {
131 struct jz4780_ohci_softc *sc;
132 int err;
133
134 sc = device_get_softc(dev);
135
136 err = clk_get_by_ofw_index(dev, 0, 0, &sc->clk);
137 if (err != 0) {
138 device_printf(dev, "unable to lookup device clock\n");
139 return (err);
140 }
141 err = clk_enable(sc->clk);
142 if (err != 0) {
143 device_printf(dev, "unable to enable device clock\n");
144 return (err);
145 }
146 err = clk_set_freq(sc->clk, 48000000, 0);
147 if (err != 0) {
148 device_printf(dev, "unable to set device clock to 48 kHZ\n");
149 return (err);
150 }
151 return (0);
152 }
153
154 static int
155 jz4780_ohci_attach(device_t dev)
156 {
157 struct jz4780_ohci_softc *sc = device_get_softc(dev);
158 int err;
159 int rid;
160
161 /* initialize some bus fields */
162 sc->sc_ohci.sc_bus.parent = dev;
163 sc->sc_ohci.sc_bus.devices = sc->sc_ohci.sc_devices;
164 sc->sc_ohci.sc_bus.devices_max = OHCI_MAX_DEVICES;
165 sc->sc_ohci.sc_bus.dma_bits = 32;
166
167 /* get all DMA memory */
168 if (usb_bus_mem_alloc_all(&sc->sc_ohci.sc_bus,
169 USB_GET_DMA_TAG(dev), &ohci_iterate_hw_softc)) {
170 return (ENOMEM);
171 }
172
173 sc->sc_ohci.sc_dev = dev;
174
175 /* frob vbus gpio */
176 err = jz4780_ohci_vbus_gpio_enable(dev, sc);
177 if (err)
178 goto error;
179
180 err = jz4780_ohci_clk_enable(dev);
181 if (err)
182 goto error;
183
184 rid = 0;
185 sc->sc_ohci.sc_io_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
186 RF_ACTIVE);
187 if (sc->sc_ohci.sc_io_res == NULL) {
188 err = ENOMEM;
189 goto error;
190 }
191 sc->sc_ohci.sc_io_tag = rman_get_bustag(sc->sc_ohci.sc_io_res);
192 sc->sc_ohci.sc_io_hdl = rman_get_bushandle(sc->sc_ohci.sc_io_res);
193 sc->sc_ohci.sc_io_size = rman_get_size(sc->sc_ohci.sc_io_res);
194
195 rid = 0;
196 sc->sc_ohci.sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
197 RF_ACTIVE);
198 if (sc->sc_ohci.sc_irq_res == NULL) {
199 err = ENOMEM;
200 goto error;
201 }
202
203 if (jz4780_ohci_enable() != 0) {
204 device_printf(dev, "CGU failed to enable OHCI\n");
205 err = ENXIO;
206 goto error;
207 }
208
209 sc->sc_ohci.sc_bus.bdev = device_add_child(dev, "usbus", -1);
210 if (sc->sc_ohci.sc_bus.bdev == NULL) {
211 err = ENOMEM;
212 goto error;
213 }
214 device_set_ivars(sc->sc_ohci.sc_bus.bdev, &sc->sc_ohci.sc_bus);
215
216 err = bus_setup_intr(dev, sc->sc_ohci.sc_irq_res,
217 INTR_TYPE_BIO | INTR_MPSAFE, NULL,
218 (driver_intr_t *)ohci_interrupt, sc, &sc->sc_ohci.sc_intr_hdl);
219 if (err) {
220 err = ENXIO;
221 goto error;
222 }
223
224 strlcpy(sc->sc_ohci.sc_vendor, "Ingenic", sizeof(sc->sc_ohci.sc_vendor));
225 bus_space_write_4(sc->sc_ohci.sc_io_tag, sc->sc_ohci.sc_io_hdl, OHCI_CONTROL, 0);
226
227 err = ohci_init(&sc->sc_ohci);
228 if (!err)
229 err = device_probe_and_attach(sc->sc_ohci.sc_bus.bdev);
230
231 if (err)
232 goto error;
233 return (0);
234
235 error:
236 if (err)
237 jz4780_ohci_detach(dev);
238 return (err);
239 }
240
241 static int
242 jz4780_ohci_detach(device_t dev)
243 {
244 struct jz4780_ohci_softc *sc = device_get_softc(dev);
245 device_t bdev;
246
247 if (sc->sc_ohci.sc_bus.bdev) {
248 bdev = sc->sc_ohci.sc_bus.bdev;
249 device_detach(bdev);
250 device_delete_child(dev, bdev);
251 }
252 /* during module unload there are lots of children leftover */
253 device_delete_children(dev);
254
255 /*
256 * Put the controller into reset, then disable clocks and do
257 * the MI tear down. We have to disable the clocks/hardware
258 * after we do the rest of the teardown. We also disable the
259 * clocks in the opposite order we acquire them, but that
260 * doesn't seem to be absolutely necessary. We free up the
261 * clocks after we disable them, so the system could, in
262 * theory, reuse them.
263 */
264 if (sc->sc_ohci.sc_io_res != NULL) {
265 bus_space_write_4(sc->sc_ohci.sc_io_tag, sc->sc_ohci.sc_io_hdl,
266 OHCI_CONTROL, 0);
267 }
268
269 if (sc->sc_ohci.sc_intr_hdl) {
270 bus_teardown_intr(dev, sc->sc_ohci.sc_irq_res, sc->sc_ohci.sc_intr_hdl);
271 sc->sc_ohci.sc_intr_hdl = NULL;
272 }
273
274 if (sc->sc_ohci.sc_irq_res && sc->sc_ohci.sc_intr_hdl) {
275 /*
276 * only call ohci_detach() after ohci_init()
277 */
278 ohci_detach(&sc->sc_ohci);
279
280 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_ohci.sc_irq_res);
281 sc->sc_ohci.sc_irq_res = NULL;
282 }
283 if (sc->sc_ohci.sc_io_res) {
284 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_ohci.sc_io_res);
285 sc->sc_ohci.sc_io_res = NULL;
286 sc->sc_ohci.sc_io_tag = 0;
287 sc->sc_ohci.sc_io_hdl = 0;
288 }
289
290 if (sc->clk != NULL)
291 clk_release(sc->clk);
292
293 usb_bus_mem_free_all(&sc->sc_ohci.sc_bus, &ohci_iterate_hw_softc);
294 free(sc->gpio_vbus, M_DEVBUF);
295 return (0);
296 }
297
298 static device_method_t ohci_methods[] = {
299 /* Device interface */
300 DEVMETHOD(device_probe, jz4780_ohci_probe),
301 DEVMETHOD(device_attach, jz4780_ohci_attach),
302 DEVMETHOD(device_detach, jz4780_ohci_detach),
303 DEVMETHOD(device_suspend, bus_generic_suspend),
304 DEVMETHOD(device_resume, bus_generic_resume),
305 DEVMETHOD(device_shutdown, bus_generic_shutdown),
306
307 DEVMETHOD_END
308 };
309
310 static driver_t ohci_driver = {
311 .name = "ohci",
312 .methods = ohci_methods,
313 .size = sizeof(struct jz4780_ohci_softc),
314 };
315
316 static devclass_t ohci_devclass;
317
318 DRIVER_MODULE(ohci, simplebus, ohci_driver, ohci_devclass, 0, 0);
Cache object: 7db49cbe45a2afa18746acafc7b59d97
|