at91_ohci.c revision 276717
1121982Sjhb/*-
2121982Sjhb * Copyright (c) 2006 M. Warner Losh.  All rights reserved.
3121982Sjhb *
4121982Sjhb * Redistribution and use in source and binary forms, with or without
5121982Sjhb * modification, are permitted provided that the following conditions
6121982Sjhb * are met:
7121982Sjhb * 1. Redistributions of source code must retain the above copyright
8121982Sjhb *    notice, this list of conditions and the following disclaimer.
9121982Sjhb * 2. Redistributions in binary form must reproduce the above copyright
10121982Sjhb *    notice, this list of conditions and the following disclaimer in the
11121982Sjhb *    documentation and/or other materials provided with the distribution.
12121982Sjhb *
13121982Sjhb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14121982Sjhb * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15121982Sjhb * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16121982Sjhb * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17121982Sjhb * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18121982Sjhb * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19121982Sjhb * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20121982Sjhb * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21121982Sjhb * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22121982Sjhb * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23121982Sjhb */
24121982Sjhb
25121982Sjhb#include <sys/cdefs.h>
26121982Sjhb__FBSDID("$FreeBSD: head/sys/dev/usb/controller/ohci_atmelarm.c 276717 2015-01-05 20:22:18Z hselasky $");
27121982Sjhb
28121982Sjhb#include <sys/stdint.h>
29121982Sjhb#include <sys/stddef.h>
30121982Sjhb#include <sys/param.h>
31121982Sjhb#include <sys/queue.h>
32121982Sjhb#include <sys/types.h>
33121982Sjhb#include <sys/systm.h>
34121982Sjhb#include <sys/kernel.h>
35121982Sjhb#include <sys/bus.h>
36121982Sjhb#include <sys/module.h>
37121982Sjhb#include <sys/lock.h>
38121982Sjhb#include <sys/mutex.h>
39121982Sjhb#include <sys/condvar.h>
40121982Sjhb#include <sys/sysctl.h>
41121982Sjhb#include <sys/sx.h>
42121982Sjhb#include <sys/unistd.h>
43121982Sjhb#include <sys/callout.h>
44121982Sjhb#include <sys/malloc.h>
45121982Sjhb#include <sys/priv.h>
46121982Sjhb
47121982Sjhb#include <dev/usb/usb.h>
48121982Sjhb#include <dev/usb/usbdi.h>
49121982Sjhb
50121982Sjhb#include <dev/usb/usb_core.h>
51121982Sjhb#include <dev/usb/usb_busdma.h>
52122572Sjhb#include <dev/usb/usb_process.h>
53121982Sjhb#include <dev/usb/usb_util.h>
54121982Sjhb
55121982Sjhb#include <dev/usb/usb_controller.h>
56121982Sjhb#include <dev/usb/usb_bus.h>
57121982Sjhb#include <dev/usb/controller/ohci.h>
58121982Sjhb#include <dev/usb/controller/ohcireg.h>
59121982Sjhb
60151658Sjhb#include <sys/rman.h>
61121982Sjhb
62121982Sjhb#include <arm/at91/at91_pmcvar.h>
63121982Sjhb
64121982Sjhb#define	MEM_RID	0
65163219Sjhb
66121982Sjhbstatic device_probe_t ohci_atmelarm_probe;
67156124Sjhbstatic device_attach_t ohci_atmelarm_attach;
68156124Sjhbstatic device_detach_t ohci_atmelarm_detach;
69156124Sjhb
70156124Sjhbstruct at91_ohci_softc {
71156124Sjhb	struct ohci_softc sc_ohci;	/* must be first */
72156124Sjhb	struct at91_pmc_clock *mclk;
73121982Sjhb	struct at91_pmc_clock *iclk;
74163219Sjhb	struct at91_pmc_clock *fclk;
75121982Sjhb};
76121982Sjhb
77121982Sjhbstatic int
78121982Sjhbohci_atmelarm_probe(device_t dev)
79163219Sjhb{
80163219Sjhb
81163219Sjhb	device_set_desc(dev, "AT91 integrated OHCI controller");
82163219Sjhb	return (BUS_PROBE_DEFAULT);
83163219Sjhb}
84163219Sjhb
85163219Sjhbstatic int
86163219Sjhbohci_atmelarm_attach(device_t dev)
87163219Sjhb{
88163219Sjhb	struct at91_ohci_softc *sc = device_get_softc(dev);
89163219Sjhb	int err;
90163219Sjhb	int rid;
91121982Sjhb
92163219Sjhb	/* initialise some bus fields */
93163219Sjhb	sc->sc_ohci.sc_bus.parent = dev;
94163219Sjhb	sc->sc_ohci.sc_bus.devices = sc->sc_ohci.sc_devices;
95163219Sjhb	sc->sc_ohci.sc_bus.devices_max = OHCI_MAX_DEVICES;
96163219Sjhb	sc->sc_ohci.sc_bus.dma_bits = 32;
97163219Sjhb
98163219Sjhb	/* get all DMA memory */
99163219Sjhb	if (usb_bus_mem_alloc_all(&sc->sc_ohci.sc_bus,
100163219Sjhb	    USB_GET_DMA_TAG(dev), &ohci_iterate_hw_softc)) {
101163219Sjhb		return (ENOMEM);
102163219Sjhb	}
103163219Sjhb	sc->mclk = at91_pmc_clock_ref("mck");
104163219Sjhb	sc->iclk = at91_pmc_clock_ref("ohci_clk");
105163219Sjhb	sc->fclk = at91_pmc_clock_ref("uhpck");
106163219Sjhb
107163219Sjhb	sc->sc_ohci.sc_dev = dev;
108163219Sjhb
109163219Sjhb	rid = MEM_RID;
110163219Sjhb	sc->sc_ohci.sc_io_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
111163219Sjhb	    &rid, RF_ACTIVE);
112163219Sjhb
113163219Sjhb	if (!(sc->sc_ohci.sc_io_res)) {
114121982Sjhb		err = ENOMEM;
115121982Sjhb		goto error;
116121982Sjhb	}
117121982Sjhb	sc->sc_ohci.sc_io_tag = rman_get_bustag(sc->sc_ohci.sc_io_res);
118121982Sjhb	sc->sc_ohci.sc_io_hdl = rman_get_bushandle(sc->sc_ohci.sc_io_res);
119121982Sjhb	sc->sc_ohci.sc_io_size = rman_get_size(sc->sc_ohci.sc_io_res);
120121982Sjhb
121121982Sjhb	rid = 0;
122121982Sjhb	sc->sc_ohci.sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
123163219Sjhb	    RF_ACTIVE);
124121982Sjhb	if (!(sc->sc_ohci.sc_irq_res)) {
125121982Sjhb		goto error;
126121982Sjhb	}
127151658Sjhb	sc->sc_ohci.sc_bus.bdev = device_add_child(dev, "usbus", -1);
128121982Sjhb	if (!(sc->sc_ohci.sc_bus.bdev)) {
129121982Sjhb		goto error;
130121982Sjhb	}
131121982Sjhb	device_set_ivars(sc->sc_ohci.sc_bus.bdev, &sc->sc_ohci.sc_bus);
132121982Sjhb
133121982Sjhb	strlcpy(sc->sc_ohci.sc_vendor, "Atmel", sizeof(sc->sc_ohci.sc_vendor));
134151658Sjhb
135121982Sjhb	err = bus_setup_intr(dev, sc->sc_ohci.sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
136121982Sjhb	    NULL, (driver_intr_t *)ohci_interrupt, sc, &sc->sc_ohci.sc_intr_hdl);
137121982Sjhb	if (err) {
138121982Sjhb		sc->sc_ohci.sc_intr_hdl = NULL;
139156124Sjhb		goto error;
140121982Sjhb	}
141121982Sjhb	/*
142121982Sjhb	 * turn on the clocks from the AT91's point of view.  Keep the unit in reset.
143121982Sjhb	 */
144121982Sjhb	at91_pmc_clock_enable(sc->mclk);
145121982Sjhb	at91_pmc_clock_enable(sc->iclk);
146121982Sjhb	at91_pmc_clock_enable(sc->fclk);
147121982Sjhb	bus_space_write_4(sc->sc_ohci.sc_io_tag, sc->sc_ohci.sc_io_hdl,
148121982Sjhb	    OHCI_CONTROL, 0);
149121982Sjhb
150121982Sjhb	err = ohci_init(&sc->sc_ohci);
151121982Sjhb	if (!err) {
152121982Sjhb		err = device_probe_and_attach(sc->sc_ohci.sc_bus.bdev);
153121982Sjhb	}
154121982Sjhb	if (err) {
155121982Sjhb		goto error;
156121982Sjhb	}
157121982Sjhb	return (0);
158121982Sjhb
159121982Sjhberror:
160121982Sjhb	ohci_atmelarm_detach(dev);
161151658Sjhb	return (ENXIO);
162151658Sjhb}
163121982Sjhb
164121982Sjhbstatic int
165156124Sjhbohci_atmelarm_detach(device_t dev)
166156124Sjhb{
167156124Sjhb	struct at91_ohci_softc *sc = device_get_softc(dev);
168156124Sjhb	device_t bdev;
169156124Sjhb	int err;
170156124Sjhb
171156124Sjhb	if (sc->sc_ohci.sc_bus.bdev) {
172156124Sjhb		bdev = sc->sc_ohci.sc_bus.bdev;
173156124Sjhb		device_detach(bdev);
174156124Sjhb		device_delete_child(dev, bdev);
175156124Sjhb	}
176121982Sjhb	/* during module unload there are lots of children leftover */
177121982Sjhb	device_delete_children(dev);
178121982Sjhb
179121982Sjhb	/*
180121982Sjhb	 * Put the controller into reset, then disable clocks and do
181121982Sjhb	 * the MI tear down.  We have to disable the clocks/hardware
182121982Sjhb	 * after we do the rest of the teardown.  We also disable the
183121982Sjhb	 * clocks in the opposite order we acquire them, but that
184121982Sjhb	 * doesn't seem to be absolutely necessary.  We free up the
185121982Sjhb	 * clocks after we disable them, so the system could, in
186151658Sjhb	 * theory, reuse them.
187121982Sjhb	 */
188121982Sjhb	bus_space_write_4(sc->sc_ohci.sc_io_tag, sc->sc_ohci.sc_io_hdl,
189121982Sjhb	    OHCI_CONTROL, 0);
190121982Sjhb
191121982Sjhb	at91_pmc_clock_disable(sc->fclk);
192121982Sjhb	at91_pmc_clock_disable(sc->iclk);
193121982Sjhb	at91_pmc_clock_disable(sc->mclk);
194128931Sjhb	at91_pmc_clock_deref(sc->fclk);
195128931Sjhb	at91_pmc_clock_deref(sc->iclk);
196128931Sjhb	at91_pmc_clock_deref(sc->mclk);
197128931Sjhb
198128931Sjhb	if (sc->sc_ohci.sc_irq_res && sc->sc_ohci.sc_intr_hdl) {
199128931Sjhb		/*
200128931Sjhb		 * only call ohci_detach() after ohci_init()
201128931Sjhb		 */
202128931Sjhb		ohci_detach(&sc->sc_ohci);
203128931Sjhb
204128931Sjhb		err = bus_teardown_intr(dev, sc->sc_ohci.sc_irq_res, sc->sc_ohci.sc_intr_hdl);
205121982Sjhb		sc->sc_ohci.sc_intr_hdl = NULL;
206153146Sjhb	}
207121982Sjhb	if (sc->sc_ohci.sc_irq_res) {
208122572Sjhb		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_ohci.sc_irq_res);
209151658Sjhb		sc->sc_ohci.sc_irq_res = NULL;
210151658Sjhb	}
211151658Sjhb	if (sc->sc_ohci.sc_io_res) {
212121982Sjhb		bus_release_resource(dev, SYS_RES_MEMORY, MEM_RID,
213122572Sjhb		    sc->sc_ohci.sc_io_res);
214122572Sjhb		sc->sc_ohci.sc_io_res = NULL;
215121982Sjhb	}
216121982Sjhb	usb_bus_mem_free_all(&sc->sc_ohci.sc_bus, &ohci_iterate_hw_softc);
217121982Sjhb
218121982Sjhb	return (0);
219121982Sjhb}
220121982Sjhb
221137165Sscottlstatic device_method_t ohci_methods[] = {
222144971Sjhb	/* Device interface */
223121982Sjhb	DEVMETHOD(device_probe, ohci_atmelarm_probe),
224151658Sjhb	DEVMETHOD(device_attach, ohci_atmelarm_attach),
225122572Sjhb	DEVMETHOD(device_detach, ohci_atmelarm_detach),
226121982Sjhb	DEVMETHOD(device_suspend, bus_generic_suspend),
227122572Sjhb	DEVMETHOD(device_resume, bus_generic_resume),
228122572Sjhb	DEVMETHOD(device_shutdown, bus_generic_shutdown),
229121982Sjhb
230122572Sjhb	DEVMETHOD_END
231122572Sjhb};
232122572Sjhb
233122572Sjhbstatic driver_t ohci_driver = {
234151658Sjhb	.name = "ohci",
235151658Sjhb	.methods = ohci_methods,
236151658Sjhb	.size = sizeof(struct at91_ohci_softc),
237151658Sjhb};
238151658Sjhb
239133017Sscottlstatic devclass_t ohci_devclass;
240137165Sscottl
241121982SjhbDRIVER_MODULE(ohci, atmelarm, ohci_driver, ohci_devclass, 0, 0);
242121982SjhbMODULE_DEPEND(ohci, usb, 1, 1, 1);
243121982Sjhb