ar71xx_ohci.c revision 308401
1/*-
2 * Copyright (c) 2009, Oleksandr Tymoshenko <gonzo@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice unmodified, this list of conditions, and the following
10 *    disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28#include <sys/cdefs.h>
29__FBSDID("$FreeBSD: stable/11/sys/mips/atheros/ar71xx_ohci.c 308401 2016-11-07 08:36:06Z hselasky $");
30
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/bus.h>
34#include <sys/rman.h>
35#include <sys/condvar.h>
36#include <sys/kernel.h>
37#include <sys/module.h>
38
39#include <dev/usb/usb.h>
40#include <dev/usb/usbdi.h>
41
42#include <dev/usb/usb_core.h>
43#include <dev/usb/usb_busdma.h>
44#include <dev/usb/usb_process.h>
45#include <dev/usb/usb_util.h>
46
47#include <dev/usb/usb_controller.h>
48#include <dev/usb/usb_bus.h>
49#include <dev/usb/controller/ohci.h>
50#include <dev/usb/controller/ohcireg.h>
51
52#include <mips/atheros/ar71xxreg.h> /* for stuff in ar71xx_cpudef.h */
53#include <mips/atheros/ar71xx_cpudef.h>
54
55static int ar71xx_ohci_attach(device_t dev);
56static int ar71xx_ohci_detach(device_t dev);
57static int ar71xx_ohci_probe(device_t dev);
58
59struct ar71xx_ohci_softc
60{
61	struct ohci_softc sc_ohci;
62};
63
64static int
65ar71xx_ohci_probe(device_t dev)
66{
67	device_set_desc(dev, "AR71XX integrated OHCI controller");
68	return (BUS_PROBE_DEFAULT);
69}
70
71static void
72ar71xx_ohci_intr(void *arg)
73{
74
75	/* XXX TODO: should really see if this was our interrupt.. */
76	ar71xx_device_flush_ddr(AR71XX_CPU_DDR_FLUSH_USB);
77	ohci_interrupt(arg);
78}
79
80
81static int
82ar71xx_ohci_attach(device_t dev)
83{
84	struct ar71xx_ohci_softc *sc = device_get_softc(dev);
85	int err;
86	int rid;
87
88	/* initialise some bus fields */
89	sc->sc_ohci.sc_bus.parent = dev;
90	sc->sc_ohci.sc_bus.devices = sc->sc_ohci.sc_devices;
91	sc->sc_ohci.sc_bus.devices_max = OHCI_MAX_DEVICES;
92	sc->sc_ohci.sc_bus.dma_bits = 32;
93
94	/* get all DMA memory */
95	if (usb_bus_mem_alloc_all(&sc->sc_ohci.sc_bus,
96	    USB_GET_DMA_TAG(dev), &ohci_iterate_hw_softc)) {
97		return (ENOMEM);
98	}
99
100	sc->sc_ohci.sc_dev = dev;
101
102	rid = 0;
103	sc->sc_ohci.sc_io_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
104	    RF_ACTIVE);
105	if (sc->sc_ohci.sc_io_res == NULL) {
106		err = ENOMEM;
107		goto error;
108	}
109	sc->sc_ohci.sc_io_tag = rman_get_bustag(sc->sc_ohci.sc_io_res);
110	sc->sc_ohci.sc_io_hdl = rman_get_bushandle(sc->sc_ohci.sc_io_res);
111	sc->sc_ohci.sc_io_size = rman_get_size(sc->sc_ohci.sc_io_res);
112
113	rid = 0;
114	sc->sc_ohci.sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
115	    RF_ACTIVE);
116	if (sc->sc_ohci.sc_irq_res == NULL) {
117		err = ENOMEM;
118		goto error;
119	}
120	sc->sc_ohci.sc_bus.bdev = device_add_child(dev, "usbus", -1);
121	if (sc->sc_ohci.sc_bus.bdev == NULL) {
122		err = ENOMEM;
123		goto error;
124	}
125	device_set_ivars(sc->sc_ohci.sc_bus.bdev, &sc->sc_ohci.sc_bus);
126
127	err = bus_setup_intr(dev, sc->sc_ohci.sc_irq_res,
128	    INTR_TYPE_BIO | INTR_MPSAFE, NULL,
129	    ar71xx_ohci_intr, sc, &sc->sc_ohci.sc_intr_hdl);
130	if (err) {
131		err = ENXIO;
132		goto error;
133	}
134
135	strlcpy(sc->sc_ohci.sc_vendor, "Atheros", sizeof(sc->sc_ohci.sc_vendor));
136
137	bus_space_write_4(sc->sc_ohci.sc_io_tag, sc->sc_ohci.sc_io_hdl, OHCI_CONTROL, 0);
138
139	err = ohci_init(&sc->sc_ohci);
140	if (!err)
141		err = device_probe_and_attach(sc->sc_ohci.sc_bus.bdev);
142
143	if (err)
144		goto error;
145	return (0);
146
147error:
148	if (err) {
149		ar71xx_ohci_detach(dev);
150		return (err);
151	}
152	return (err);
153}
154
155static int
156ar71xx_ohci_detach(device_t dev)
157{
158	struct ar71xx_ohci_softc *sc = device_get_softc(dev);
159
160	/* during module unload there are lots of children leftover */
161	device_delete_children(dev);
162
163	/*
164	 * Put the controller into reset, then disable clocks and do
165	 * the MI tear down.  We have to disable the clocks/hardware
166	 * after we do the rest of the teardown.  We also disable the
167	 * clocks in the opposite order we acquire them, but that
168	 * doesn't seem to be absolutely necessary.  We free up the
169	 * clocks after we disable them, so the system could, in
170	 * theory, reuse them.
171	 */
172	bus_space_write_4(sc->sc_ohci.sc_io_tag, sc->sc_ohci.sc_io_hdl,
173	    OHCI_CONTROL, 0);
174
175	if (sc->sc_ohci.sc_intr_hdl) {
176		bus_teardown_intr(dev, sc->sc_ohci.sc_irq_res, sc->sc_ohci.sc_intr_hdl);
177		sc->sc_ohci.sc_intr_hdl = NULL;
178	}
179
180	if (sc->sc_ohci.sc_irq_res && sc->sc_ohci.sc_intr_hdl) {
181		/*
182		 * only call ohci_detach() after ohci_init()
183		 */
184		ohci_detach(&sc->sc_ohci);
185
186		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_ohci.sc_irq_res);
187		sc->sc_ohci.sc_irq_res = NULL;
188	}
189	if (sc->sc_ohci.sc_io_res) {
190		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_ohci.sc_io_res);
191		sc->sc_ohci.sc_io_res = NULL;
192		sc->sc_ohci.sc_io_tag = 0;
193		sc->sc_ohci.sc_io_hdl = 0;
194	}
195	usb_bus_mem_free_all(&sc->sc_ohci.sc_bus, &ohci_iterate_hw_softc);
196
197	return (0);
198}
199
200static device_method_t ohci_methods[] = {
201	/* Device interface */
202	DEVMETHOD(device_probe, ar71xx_ohci_probe),
203	DEVMETHOD(device_attach, ar71xx_ohci_attach),
204	DEVMETHOD(device_detach, ar71xx_ohci_detach),
205	DEVMETHOD(device_suspend, bus_generic_suspend),
206	DEVMETHOD(device_resume, bus_generic_resume),
207	DEVMETHOD(device_shutdown, bus_generic_shutdown),
208
209	DEVMETHOD_END
210};
211
212static driver_t ohci_driver = {
213	.name = "ohci",
214	.methods = ohci_methods,
215	.size = sizeof(struct ar71xx_ohci_softc),
216};
217
218static devclass_t ohci_devclass;
219
220DRIVER_MODULE(ohci, apb, ohci_driver, ohci_devclass, 0, 0);
221