1184610Salfred/*-
2184610Salfred * Copyright (c) 2006 M. Warner Losh.  All rights reserved.
3184610Salfred *
4184610Salfred * Redistribution and use in source and binary forms, with or without
5184610Salfred * modification, are permitted provided that the following conditions
6184610Salfred * are met:
7184610Salfred * 1. Redistributions of source code must retain the above copyright
8184610Salfred *    notice, this list of conditions and the following disclaimer.
9184610Salfred * 2. Redistributions in binary form must reproduce the above copyright
10184610Salfred *    notice, this list of conditions and the following disclaimer in the
11184610Salfred *    documentation and/or other materials provided with the distribution.
12184610Salfred *
13184610Salfred * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14184610Salfred * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15184610Salfred * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16184610Salfred * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17184610Salfred * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18184610Salfred * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19184610Salfred * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20184610Salfred * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21184610Salfred * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22184610Salfred * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23184610Salfred */
24184610Salfred
25184610Salfred#include <sys/cdefs.h>
26184610Salfred__FBSDID("$FreeBSD: releng/10.2/sys/dev/usb/controller/ohci_atmelarm.c 278278 2015-02-05 20:03:02Z hselasky $");
27184610Salfred
28194677Sthompsa#include <sys/stdint.h>
29194677Sthompsa#include <sys/stddef.h>
30194677Sthompsa#include <sys/param.h>
31194677Sthompsa#include <sys/queue.h>
32194677Sthompsa#include <sys/types.h>
33194677Sthompsa#include <sys/systm.h>
34194677Sthompsa#include <sys/kernel.h>
35194677Sthompsa#include <sys/bus.h>
36194677Sthompsa#include <sys/module.h>
37194677Sthompsa#include <sys/lock.h>
38194677Sthompsa#include <sys/mutex.h>
39194677Sthompsa#include <sys/condvar.h>
40194677Sthompsa#include <sys/sysctl.h>
41194677Sthompsa#include <sys/sx.h>
42194677Sthompsa#include <sys/unistd.h>
43194677Sthompsa#include <sys/callout.h>
44194677Sthompsa#include <sys/malloc.h>
45194677Sthompsa#include <sys/priv.h>
46194677Sthompsa
47188942Sthompsa#include <dev/usb/usb.h>
48194677Sthompsa#include <dev/usb/usbdi.h>
49184610Salfred
50188942Sthompsa#include <dev/usb/usb_core.h>
51188942Sthompsa#include <dev/usb/usb_busdma.h>
52188942Sthompsa#include <dev/usb/usb_process.h>
53188942Sthompsa#include <dev/usb/usb_util.h>
54184610Salfred
55188942Sthompsa#include <dev/usb/usb_controller.h>
56188942Sthompsa#include <dev/usb/usb_bus.h>
57188942Sthompsa#include <dev/usb/controller/ohci.h>
58198151Sthompsa#include <dev/usb/controller/ohcireg.h>
59184610Salfred
60184610Salfred#include <sys/rman.h>
61184610Salfred
62184610Salfred#include <arm/at91/at91_pmcvar.h>
63184610Salfred
64184610Salfred#define	MEM_RID	0
65184610Salfred
66184610Salfredstatic device_probe_t ohci_atmelarm_probe;
67184610Salfredstatic device_attach_t ohci_atmelarm_attach;
68184610Salfredstatic device_detach_t ohci_atmelarm_detach;
69184610Salfred
70184610Salfredstruct at91_ohci_softc {
71184610Salfred	struct ohci_softc sc_ohci;	/* must be first */
72239531Shselasky	struct at91_pmc_clock *mclk;
73184610Salfred	struct at91_pmc_clock *iclk;
74184610Salfred	struct at91_pmc_clock *fclk;
75184610Salfred};
76184610Salfred
77184610Salfredstatic int
78184610Salfredohci_atmelarm_probe(device_t dev)
79184610Salfred{
80238819Simp
81184610Salfred	device_set_desc(dev, "AT91 integrated OHCI controller");
82184610Salfred	return (BUS_PROBE_DEFAULT);
83184610Salfred}
84184610Salfred
85184610Salfredstatic int
86184610Salfredohci_atmelarm_attach(device_t dev)
87184610Salfred{
88184610Salfred	struct at91_ohci_softc *sc = device_get_softc(dev);
89184610Salfred	int err;
90184610Salfred	int rid;
91184610Salfred
92187170Sthompsa	/* initialise some bus fields */
93187170Sthompsa	sc->sc_ohci.sc_bus.parent = dev;
94187170Sthompsa	sc->sc_ohci.sc_bus.devices = sc->sc_ohci.sc_devices;
95187170Sthompsa	sc->sc_ohci.sc_bus.devices_max = OHCI_MAX_DEVICES;
96278278Shselasky	sc->sc_ohci.sc_bus.dma_bits = 32;
97187170Sthompsa
98184610Salfred	/* get all DMA memory */
99194228Sthompsa	if (usb_bus_mem_alloc_all(&sc->sc_ohci.sc_bus,
100184610Salfred	    USB_GET_DMA_TAG(dev), &ohci_iterate_hw_softc)) {
101187170Sthompsa		return (ENOMEM);
102184610Salfred	}
103239531Shselasky	sc->mclk = at91_pmc_clock_ref("mck");
104184610Salfred	sc->iclk = at91_pmc_clock_ref("ohci_clk");
105184610Salfred	sc->fclk = at91_pmc_clock_ref("uhpck");
106184610Salfred
107184610Salfred	sc->sc_ohci.sc_dev = dev;
108184610Salfred
109184610Salfred	rid = MEM_RID;
110184610Salfred	sc->sc_ohci.sc_io_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
111184610Salfred	    &rid, RF_ACTIVE);
112184610Salfred
113184610Salfred	if (!(sc->sc_ohci.sc_io_res)) {
114184610Salfred		err = ENOMEM;
115184610Salfred		goto error;
116184610Salfred	}
117184610Salfred	sc->sc_ohci.sc_io_tag = rman_get_bustag(sc->sc_ohci.sc_io_res);
118184610Salfred	sc->sc_ohci.sc_io_hdl = rman_get_bushandle(sc->sc_ohci.sc_io_res);
119184610Salfred	sc->sc_ohci.sc_io_size = rman_get_size(sc->sc_ohci.sc_io_res);
120184610Salfred
121184610Salfred	rid = 0;
122184610Salfred	sc->sc_ohci.sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
123184610Salfred	    RF_ACTIVE);
124184610Salfred	if (!(sc->sc_ohci.sc_irq_res)) {
125184610Salfred		goto error;
126184610Salfred	}
127184610Salfred	sc->sc_ohci.sc_bus.bdev = device_add_child(dev, "usbus", -1);
128184610Salfred	if (!(sc->sc_ohci.sc_bus.bdev)) {
129184610Salfred		goto error;
130184610Salfred	}
131184610Salfred	device_set_ivars(sc->sc_ohci.sc_bus.bdev, &sc->sc_ohci.sc_bus);
132184610Salfred
133184610Salfred	strlcpy(sc->sc_ohci.sc_vendor, "Atmel", sizeof(sc->sc_ohci.sc_vendor));
134184610Salfred
135184610Salfred	err = bus_setup_intr(dev, sc->sc_ohci.sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
136190183Sthompsa	    NULL, (driver_intr_t *)ohci_interrupt, sc, &sc->sc_ohci.sc_intr_hdl);
137184610Salfred	if (err) {
138184610Salfred		sc->sc_ohci.sc_intr_hdl = NULL;
139184610Salfred		goto error;
140184610Salfred	}
141184610Salfred	/*
142184610Salfred	 * turn on the clocks from the AT91's point of view.  Keep the unit in reset.
143184610Salfred	 */
144239531Shselasky	at91_pmc_clock_enable(sc->mclk);
145184610Salfred	at91_pmc_clock_enable(sc->iclk);
146184610Salfred	at91_pmc_clock_enable(sc->fclk);
147184610Salfred	bus_space_write_4(sc->sc_ohci.sc_io_tag, sc->sc_ohci.sc_io_hdl,
148184610Salfred	    OHCI_CONTROL, 0);
149184610Salfred
150184610Salfred	err = ohci_init(&sc->sc_ohci);
151184610Salfred	if (!err) {
152184610Salfred		err = device_probe_and_attach(sc->sc_ohci.sc_bus.bdev);
153184610Salfred	}
154184610Salfred	if (err) {
155184610Salfred		goto error;
156184610Salfred	}
157184610Salfred	return (0);
158184610Salfred
159184610Salfrederror:
160184610Salfred	ohci_atmelarm_detach(dev);
161184610Salfred	return (ENXIO);
162184610Salfred}
163184610Salfred
164184610Salfredstatic int
165184610Salfredohci_atmelarm_detach(device_t dev)
166184610Salfred{
167184610Salfred	struct at91_ohci_softc *sc = device_get_softc(dev);
168184610Salfred	device_t bdev;
169184610Salfred	int err;
170184610Salfred
171184610Salfred	if (sc->sc_ohci.sc_bus.bdev) {
172184610Salfred		bdev = sc->sc_ohci.sc_bus.bdev;
173184610Salfred		device_detach(bdev);
174184610Salfred		device_delete_child(dev, bdev);
175184610Salfred	}
176184610Salfred	/* during module unload there are lots of children leftover */
177227849Shselasky	device_delete_children(dev);
178184610Salfred
179184610Salfred	/*
180184610Salfred	 * Put the controller into reset, then disable clocks and do
181184610Salfred	 * the MI tear down.  We have to disable the clocks/hardware
182184610Salfred	 * after we do the rest of the teardown.  We also disable the
183184610Salfred	 * clocks in the opposite order we acquire them, but that
184184610Salfred	 * doesn't seem to be absolutely necessary.  We free up the
185184610Salfred	 * clocks after we disable them, so the system could, in
186184610Salfred	 * theory, reuse them.
187184610Salfred	 */
188184610Salfred	bus_space_write_4(sc->sc_ohci.sc_io_tag, sc->sc_ohci.sc_io_hdl,
189184610Salfred	    OHCI_CONTROL, 0);
190184610Salfred
191184610Salfred	at91_pmc_clock_disable(sc->fclk);
192184610Salfred	at91_pmc_clock_disable(sc->iclk);
193239531Shselasky	at91_pmc_clock_disable(sc->mclk);
194184610Salfred	at91_pmc_clock_deref(sc->fclk);
195184610Salfred	at91_pmc_clock_deref(sc->iclk);
196239531Shselasky	at91_pmc_clock_deref(sc->mclk);
197184610Salfred
198184610Salfred	if (sc->sc_ohci.sc_irq_res && sc->sc_ohci.sc_intr_hdl) {
199184610Salfred		/*
200184610Salfred		 * only call ohci_detach() after ohci_init()
201184610Salfred		 */
202184610Salfred		ohci_detach(&sc->sc_ohci);
203184610Salfred
204184610Salfred		err = bus_teardown_intr(dev, sc->sc_ohci.sc_irq_res, sc->sc_ohci.sc_intr_hdl);
205184610Salfred		sc->sc_ohci.sc_intr_hdl = NULL;
206184610Salfred	}
207184610Salfred	if (sc->sc_ohci.sc_irq_res) {
208184610Salfred		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_ohci.sc_irq_res);
209184610Salfred		sc->sc_ohci.sc_irq_res = NULL;
210184610Salfred	}
211184610Salfred	if (sc->sc_ohci.sc_io_res) {
212184610Salfred		bus_release_resource(dev, SYS_RES_MEMORY, MEM_RID,
213184610Salfred		    sc->sc_ohci.sc_io_res);
214184610Salfred		sc->sc_ohci.sc_io_res = NULL;
215184610Salfred	}
216194228Sthompsa	usb_bus_mem_free_all(&sc->sc_ohci.sc_bus, &ohci_iterate_hw_softc);
217184610Salfred
218184610Salfred	return (0);
219184610Salfred}
220184610Salfred
221184610Salfredstatic device_method_t ohci_methods[] = {
222184610Salfred	/* Device interface */
223184610Salfred	DEVMETHOD(device_probe, ohci_atmelarm_probe),
224184610Salfred	DEVMETHOD(device_attach, ohci_atmelarm_attach),
225184610Salfred	DEVMETHOD(device_detach, ohci_atmelarm_detach),
226228483Shselasky	DEVMETHOD(device_suspend, bus_generic_suspend),
227228483Shselasky	DEVMETHOD(device_resume, bus_generic_resume),
228184610Salfred	DEVMETHOD(device_shutdown, bus_generic_shutdown),
229184610Salfred
230227843Smarius	DEVMETHOD_END
231184610Salfred};
232184610Salfred
233184610Salfredstatic driver_t ohci_driver = {
234228483Shselasky	.name = "ohci",
235228483Shselasky	.methods = ohci_methods,
236228483Shselasky	.size = sizeof(struct at91_ohci_softc),
237184610Salfred};
238184610Salfred
239184610Salfredstatic devclass_t ohci_devclass;
240184610Salfred
241184610SalfredDRIVER_MODULE(ohci, atmelarm, ohci_driver, ohci_devclass, 0, 0);
242188942SthompsaMODULE_DEPEND(ohci, usb, 1, 1, 1);
243