ohci_ec.c revision 228483
1/*-
2 * Copyright (c) 2009 Yohanes Nugroho <yohanes@gmail.com>
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, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include <sys/cdefs.h>
27__FBSDID("$FreeBSD: head/sys/arm/econa/ohci_ec.c 228483 2011-12-14 00:28:54Z hselasky $");
28
29#include <sys/stdint.h>
30#include <sys/stddef.h>
31#include <sys/param.h>
32#include <sys/queue.h>
33#include <sys/types.h>
34#include <sys/systm.h>
35#include <sys/kernel.h>
36#include <sys/bus.h>
37#include <sys/module.h>
38#include <sys/lock.h>
39#include <sys/mutex.h>
40#include <sys/condvar.h>
41#include <sys/sysctl.h>
42#include <sys/sx.h>
43#include <sys/unistd.h>
44#include <sys/callout.h>
45#include <sys/malloc.h>
46#include <sys/priv.h>
47
48#include <dev/usb/usb.h>
49#include <dev/usb/usbdi.h>
50
51#include <dev/usb/usb_core.h>
52#include <dev/usb/usb_busdma.h>
53#include <dev/usb/usb_process.h>
54#include <dev/usb/usb_util.h>
55
56#include <dev/usb/usb_controller.h>
57#include <dev/usb/usb_bus.h>
58#include <dev/usb/controller/ohci.h>
59#include <dev/usb/controller/ohcireg.h>
60
61#include <sys/rman.h>
62
63#include <arm/econa/econa_reg.h>
64
65#define	MEM_RID	0
66
67static device_probe_t ohci_ec_probe;
68static device_attach_t ohci_ec_attach;
69static device_detach_t ohci_ec_detach;
70
71struct ec_ohci_softc {
72	struct ohci_softc sc_ohci;	/* must be first */
73};
74
75static int
76ohci_ec_probe(device_t dev)
77{
78	device_set_desc(dev, "Econa integrated OHCI controller");
79	return (BUS_PROBE_DEFAULT);
80}
81
82static int
83ohci_ec_attach(device_t dev)
84{
85	struct ec_ohci_softc *sc = device_get_softc(dev);
86	bus_space_handle_t bsh;
87	int err;
88	int rid;
89
90	/* initialise some bus fields */
91	sc->sc_ohci.sc_bus.parent = dev;
92	sc->sc_ohci.sc_bus.devices = sc->sc_ohci.sc_devices;
93	sc->sc_ohci.sc_bus.devices_max = OHCI_MAX_DEVICES;
94
95	/* get all DMA memory */
96	if (usb_bus_mem_alloc_all(&sc->sc_ohci.sc_bus,
97	    USB_GET_DMA_TAG(dev), &ohci_iterate_hw_softc)) {
98		return (ENOMEM);
99	}
100	sc->sc_ohci.sc_dev = dev;
101
102	rid = MEM_RID;
103
104	sc->sc_ohci.sc_io_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
105	    &rid, RF_ACTIVE);
106
107	if (!(sc->sc_ohci.sc_io_res)) {
108		err = ENOMEM;
109		goto error;
110	}
111	sc->sc_ohci.sc_io_tag = rman_get_bustag(sc->sc_ohci.sc_io_res);
112	bsh = rman_get_bushandle(sc->sc_ohci.sc_io_res);
113	/* Undocumented magic initialization */
114	bus_space_write_4((sc)->sc_ohci.sc_io_tag, bsh,0x04, 0x146);
115
116	bus_space_write_4((sc)->sc_ohci.sc_io_tag, bsh,0x44, 0x0200);
117
118	DELAY(1000);
119
120	sc->sc_ohci.sc_io_size = rman_get_size(sc->sc_ohci.sc_io_res);
121
122	if (bus_space_subregion(sc->sc_ohci.sc_io_tag, bsh, 0x4000000,
123	    sc->sc_ohci.sc_io_size, &sc->sc_ohci.sc_io_hdl) != 0)
124		panic("%s: unable to subregion USB host registers",
125		    device_get_name(dev));
126
127	rid = 0;
128	sc->sc_ohci.sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
129	    RF_ACTIVE);
130	if (!(sc->sc_ohci.sc_irq_res)) {
131		goto error;
132	}
133	sc->sc_ohci.sc_bus.bdev = device_add_child(dev, "usbus", -1);
134	if (!(sc->sc_ohci.sc_bus.bdev)) {
135		goto error;
136	}
137	device_set_ivars(sc->sc_ohci.sc_bus.bdev, &sc->sc_ohci.sc_bus);
138
139	strlcpy(sc->sc_ohci.sc_vendor, "Cavium",
140		sizeof(sc->sc_ohci.sc_vendor));
141
142#if (__FreeBSD_version >= 700031)
143	err = bus_setup_intr(dev, sc->sc_ohci.sc_irq_res,
144	    INTR_TYPE_BIO | INTR_MPSAFE,  NULL,
145	    (driver_intr_t *)ohci_interrupt, sc,
146	    &sc->sc_ohci.sc_intr_hdl);
147#else
148	err = bus_setup_intr(dev, sc->sc_ohci.sc_irq_res,
149	    INTR_TYPE_BIO | INTR_MPSAFE,
150	    (driver_intr_t *)ohci_interrupt, sc,
151	    &sc->sc_ohci.sc_intr_hdl);
152#endif
153	if (err) {
154		sc->sc_ohci.sc_intr_hdl = NULL;
155		goto error;
156	}
157
158	bus_space_write_4(sc->sc_ohci.sc_io_tag, sc->sc_ohci.sc_io_hdl,
159	    OHCI_CONTROL, 0);
160
161	err = ohci_init(&sc->sc_ohci);
162	if (!err) {
163		err = device_probe_and_attach(sc->sc_ohci.sc_bus.bdev);
164	}
165	if (err) {
166		goto error;
167	}
168	return (0);
169
170error:
171	ohci_ec_detach(dev);
172	return (ENXIO);
173}
174
175static int
176ohci_ec_detach(device_t dev)
177{
178	struct ec_ohci_softc *sc = device_get_softc(dev);
179	device_t bdev;
180	int err;
181
182	if (sc->sc_ohci.sc_bus.bdev) {
183		bdev = sc->sc_ohci.sc_bus.bdev;
184		device_detach(bdev);
185		device_delete_child(dev, bdev);
186	}
187	/* during module unload there are lots of children leftover */
188	device_delete_children(dev);
189
190	bus_space_write_4(sc->sc_ohci.sc_io_tag, sc->sc_ohci.sc_io_hdl,
191	    OHCI_CONTROL, 0);
192
193	if (sc->sc_ohci.sc_irq_res && sc->sc_ohci.sc_intr_hdl) {
194		/*
195		 * only call ohci_detach() after ohci_init()
196		 */
197		ohci_detach(&sc->sc_ohci);
198
199		err = bus_teardown_intr(dev, sc->sc_ohci.sc_irq_res,
200		    sc->sc_ohci.sc_intr_hdl);
201		sc->sc_ohci.sc_intr_hdl = NULL;
202	}
203	if (sc->sc_ohci.sc_irq_res) {
204		bus_release_resource(dev, SYS_RES_IRQ, 0,
205		    sc->sc_ohci.sc_irq_res);
206		sc->sc_ohci.sc_irq_res = NULL;
207	}
208	if (sc->sc_ohci.sc_io_res) {
209		bus_release_resource(dev, SYS_RES_MEMORY, MEM_RID,
210		    sc->sc_ohci.sc_io_res);
211		sc->sc_ohci.sc_io_res = NULL;
212	}
213	usb_bus_mem_free_all(&sc->sc_ohci.sc_bus, &ohci_iterate_hw_softc);
214
215	return (0);
216}
217
218static device_method_t ohci_methods[] = {
219	/* Device interface */
220	DEVMETHOD(device_probe, ohci_ec_probe),
221	DEVMETHOD(device_attach, ohci_ec_attach),
222	DEVMETHOD(device_detach, ohci_ec_detach),
223	DEVMETHOD(device_resume, bus_generic_resume),
224	DEVMETHOD(device_suspend, bus_generic_suspend),
225	DEVMETHOD(device_shutdown, bus_generic_shutdown),
226
227	DEVMETHOD_END
228};
229
230static driver_t ohci_driver = {
231	.name = "ohci",
232	.methods = ohci_methods,
233	.size = sizeof(struct ec_ohci_softc),
234};
235
236static devclass_t ohci_devclass;
237
238DRIVER_MODULE(ohci, econaarm, ohci_driver, ohci_devclass, 0, 0);
239MODULE_DEPEND(ohci, usb, 1, 1, 1);
240