ar71xx_ohci.c revision 278278
150472Speter/*-
233975Sjdp * Copyright (c) 2009, Oleksandr Tymoshenko <gonzo@FreeBSD.org>
333975Sjdp * All rights reserved.
433975Sjdp *
533975Sjdp * Redistribution and use in source and binary forms, with or without
633975Sjdp * modification, are permitted provided that the following conditions
7218822Sdim * are met:
884947Sobrien * 1. Redistributions of source code must retain the above copyright
9215082Simp *    notice unmodified, this list of conditions, and the following
10239272Sgonzo *    disclaimer.
11130575Sobrien * 2. Redistributions in binary form must reproduce the above copyright
12215082Simp *    notice, this list of conditions and the following disclaimer in the
13130575Sobrien *    documentation and/or other materials provided with the distribution.
14215082Simp *
15218822Sdim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16218822Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17215082Simp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18218822Sdim * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19239272Sgonzo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20233644Sjmallett * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21215082Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22215082Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2344360Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2433975Sjdp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2533975Sjdp * SUCH DAMAGE.
2633975Sjdp */
2784902Sobrien
2833975Sjdp#include <sys/cdefs.h>
2984902Sobrien__FBSDID("$FreeBSD: stable/10/sys/mips/atheros/ar71xx_ohci.c 278278 2015-02-05 20:03:02Z hselasky $");
3084902Sobrien
3133975Sjdp#include <sys/param.h>
32215082Simp#include <sys/systm.h>
33215276Simp#include <sys/bus.h>
34217942Sjchandra#include <sys/rman.h>
35166638Smarcel#include <sys/condvar.h>
36166638Smarcel#include <sys/kernel.h>
37166638Smarcel#include <sys/module.h>
38166638Smarcel
39166638Smarcel#include <dev/usb/usb.h>
4090353Sobrien#include <dev/usb/usbdi.h>
4184902Sobrien
42131832Sobrien#include <dev/usb/usb_core.h>
43130575Sobrien#include <dev/usb/usb_busdma.h>
4484902Sobrien#include <dev/usb/usb_process.h>
4534495Sjdp#include <dev/usb/usb_util.h>
46215082Simp
4735709Sjb#include <dev/usb/usb_controller.h>
48215351Snwhitehorn#include <dev/usb/usb_bus.h>
49215351Snwhitehorn#include <dev/usb/controller/ohci.h>
50215351Snwhitehorn#include <dev/usb/controller/ohcireg.h>
51215351Snwhitehorn
5252927Sjbstatic int ar71xx_ohci_attach(device_t dev);
53static int ar71xx_ohci_detach(device_t dev);
54static int ar71xx_ohci_probe(device_t dev);
55
56struct ar71xx_ohci_softc
57{
58	struct ohci_softc sc_ohci;
59};
60
61static int
62ar71xx_ohci_probe(device_t dev)
63{
64	device_set_desc(dev, "AR71XX integrated OHCI controller");
65	return (BUS_PROBE_DEFAULT);
66}
67
68static int
69ar71xx_ohci_attach(device_t dev)
70{
71	struct ar71xx_ohci_softc *sc = device_get_softc(dev);
72	int err;
73	int rid;
74
75	/* initialise some bus fields */
76	sc->sc_ohci.sc_bus.parent = dev;
77	sc->sc_ohci.sc_bus.devices = sc->sc_ohci.sc_devices;
78	sc->sc_ohci.sc_bus.devices_max = OHCI_MAX_DEVICES;
79	sc->sc_ohci.sc_bus.dma_bits = 32;
80
81	/* get all DMA memory */
82	if (usb_bus_mem_alloc_all(&sc->sc_ohci.sc_bus,
83	    USB_GET_DMA_TAG(dev), &ohci_iterate_hw_softc)) {
84		return (ENOMEM);
85	}
86
87	sc->sc_ohci.sc_dev = dev;
88
89	rid = 0;
90	sc->sc_ohci.sc_io_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
91	    RF_ACTIVE);
92	if (sc->sc_ohci.sc_io_res == NULL) {
93		err = ENOMEM;
94		goto error;
95	}
96	sc->sc_ohci.sc_io_tag = rman_get_bustag(sc->sc_ohci.sc_io_res);
97	sc->sc_ohci.sc_io_hdl = rman_get_bushandle(sc->sc_ohci.sc_io_res);
98	sc->sc_ohci.sc_io_size = rman_get_size(sc->sc_ohci.sc_io_res);
99
100	rid = 0;
101	sc->sc_ohci.sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
102	    RF_ACTIVE);
103	if (sc->sc_ohci.sc_irq_res == NULL) {
104		err = ENOMEM;
105		goto error;
106	}
107	sc->sc_ohci.sc_bus.bdev = device_add_child(dev, "usbus", -1);
108	if (sc->sc_ohci.sc_bus.bdev == NULL) {
109		err = ENOMEM;
110		goto error;
111	}
112	device_set_ivars(sc->sc_ohci.sc_bus.bdev, &sc->sc_ohci.sc_bus);
113
114	err = bus_setup_intr(dev, sc->sc_ohci.sc_irq_res,
115	    INTR_TYPE_BIO | INTR_MPSAFE, NULL,
116	    (driver_intr_t *)ohci_interrupt, sc, &sc->sc_ohci.sc_intr_hdl);
117	if (err) {
118		err = ENXIO;
119		goto error;
120	}
121
122	strlcpy(sc->sc_ohci.sc_vendor, "Atheros", sizeof(sc->sc_ohci.sc_vendor));
123
124	bus_space_write_4(sc->sc_ohci.sc_io_tag, sc->sc_ohci.sc_io_hdl, OHCI_CONTROL, 0);
125
126	err = ohci_init(&sc->sc_ohci);
127	if (!err)
128		err = device_probe_and_attach(sc->sc_ohci.sc_bus.bdev);
129
130	if (err)
131		goto error;
132	return (0);
133
134error:
135	if (err) {
136		ar71xx_ohci_detach(dev);
137		return (err);
138	}
139	return (err);
140}
141
142static int
143ar71xx_ohci_detach(device_t dev)
144{
145	struct ar71xx_ohci_softc *sc = device_get_softc(dev);
146	device_t bdev;
147
148	if (sc->sc_ohci.sc_bus.bdev) {
149		bdev = sc->sc_ohci.sc_bus.bdev;
150		device_detach(bdev);
151		device_delete_child(dev, bdev);
152	}
153	/* during module unload there are lots of children leftover */
154	device_delete_children(dev);
155
156	/*
157	 * Put the controller into reset, then disable clocks and do
158	 * the MI tear down.  We have to disable the clocks/hardware
159	 * after we do the rest of the teardown.  We also disable the
160	 * clocks in the opposite order we acquire them, but that
161	 * doesn't seem to be absolutely necessary.  We free up the
162	 * clocks after we disable them, so the system could, in
163	 * theory, reuse them.
164	 */
165	bus_space_write_4(sc->sc_ohci.sc_io_tag, sc->sc_ohci.sc_io_hdl,
166	    OHCI_CONTROL, 0);
167
168	if (sc->sc_ohci.sc_intr_hdl) {
169		bus_teardown_intr(dev, sc->sc_ohci.sc_irq_res, sc->sc_ohci.sc_intr_hdl);
170		sc->sc_ohci.sc_intr_hdl = NULL;
171	}
172
173	if (sc->sc_ohci.sc_irq_res && sc->sc_ohci.sc_intr_hdl) {
174		/*
175		 * only call ohci_detach() after ohci_init()
176		 */
177		ohci_detach(&sc->sc_ohci);
178
179		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_ohci.sc_irq_res);
180		sc->sc_ohci.sc_irq_res = NULL;
181	}
182	if (sc->sc_ohci.sc_io_res) {
183		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_ohci.sc_io_res);
184		sc->sc_ohci.sc_io_res = NULL;
185		sc->sc_ohci.sc_io_tag = 0;
186		sc->sc_ohci.sc_io_hdl = 0;
187	}
188	usb_bus_mem_free_all(&sc->sc_ohci.sc_bus, &ohci_iterate_hw_softc);
189
190	return (0);
191}
192
193static device_method_t ohci_methods[] = {
194	/* Device interface */
195	DEVMETHOD(device_probe, ar71xx_ohci_probe),
196	DEVMETHOD(device_attach, ar71xx_ohci_attach),
197	DEVMETHOD(device_detach, ar71xx_ohci_detach),
198	DEVMETHOD(device_suspend, bus_generic_suspend),
199	DEVMETHOD(device_resume, bus_generic_resume),
200	DEVMETHOD(device_shutdown, bus_generic_shutdown),
201
202	DEVMETHOD_END
203};
204
205static driver_t ohci_driver = {
206	.name = "ohci",
207	.methods = ohci_methods,
208	.size = sizeof(struct ar71xx_ohci_softc),
209};
210
211static devclass_t ohci_devclass;
212
213DRIVER_MODULE(ohci, apb, ohci_driver, ohci_devclass, 0, 0);
214