1205354Simp/*-
2205354Simp * Copyright (c) 2006 M. Warner Losh.  All rights reserved.
3205354Simp * Copyright (c) 2009 Andrew Turner.  All rights reserved.
4205354Simp *
5205354Simp * Redistribution and use in source and binary forms, with or without
6205354Simp * modification, are permitted provided that the following conditions
7205354Simp * are met:
8205354Simp * 1. Redistributions of source code must retain the above copyright
9205354Simp *    notice, this list of conditions and the following disclaimer.
10205354Simp * 2. Redistributions in binary form must reproduce the above copyright
11205354Simp *    notice, this list of conditions and the following disclaimer in the
12205354Simp *    documentation and/or other materials provided with the distribution.
13205354Simp *
14205354Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15205354Simp * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16205354Simp * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17205354Simp * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18205354Simp * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19205354Simp * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20205354Simp * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21205354Simp * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22205354Simp * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23205354Simp * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24205354Simp */
25205354Simp
26205354Simp#include <sys/cdefs.h>
27205354Simp__FBSDID("$FreeBSD$");
28205354Simp
29205354Simp#include <sys/stdint.h>
30205354Simp#include <sys/stddef.h>
31205354Simp#include <sys/param.h>
32205354Simp#include <sys/queue.h>
33205354Simp#include <sys/types.h>
34205354Simp#include <sys/systm.h>
35205354Simp#include <sys/kernel.h>
36205354Simp#include <sys/bus.h>
37205354Simp#include <sys/module.h>
38205354Simp#include <sys/lock.h>
39205354Simp#include <sys/mutex.h>
40205354Simp#include <sys/condvar.h>
41205354Simp#include <sys/sysctl.h>
42205354Simp#include <sys/sx.h>
43205354Simp#include <sys/unistd.h>
44205354Simp#include <sys/callout.h>
45205354Simp#include <sys/malloc.h>
46205354Simp#include <sys/priv.h>
47205354Simp
48205354Simp#include <dev/usb/usb.h>
49205354Simp#include <dev/usb/usbdi.h>
50205354Simp
51205354Simp#include <dev/usb/usb_core.h>
52205354Simp#include <dev/usb/usb_busdma.h>
53205354Simp#include <dev/usb/usb_process.h>
54205354Simp#include <dev/usb/usb_util.h>
55205354Simp
56205354Simp#include <dev/usb/usb_controller.h>
57205354Simp#include <dev/usb/usb_bus.h>
58205354Simp#include <dev/usb/controller/ohci.h>
59205354Simp#include <dev/usb/controller/ohcireg.h>
60205354Simp
61205354Simp#include <sys/rman.h>
62205354Simp
63205354Simp#include <arm/s3c2xx0/s3c24x0reg.h>
64205354Simp
65205354Simpstatic device_probe_t ohci_s3c24x0_probe;
66205354Simpstatic device_attach_t ohci_s3c24x0_attach;
67205354Simpstatic device_detach_t ohci_s3c24x0_detach;
68205354Simp
69205354Simpstatic int
70205354Simpohci_s3c24x0_probe(device_t dev)
71205354Simp{
72205354Simp	device_set_desc(dev, "S3C24x0 integrated OHCI controller");
73205354Simp	return (BUS_PROBE_DEFAULT);
74205354Simp}
75205354Simp
76205354Simpstatic int
77205354Simpohci_s3c24x0_attach(device_t dev)
78205354Simp{
79205354Simp	struct ohci_softc *sc = device_get_softc(dev);
80205354Simp	int err;
81205354Simp	int rid;
82205354Simp
83205354Simp	/* initialise some bus fields */
84205354Simp	sc->sc_bus.parent = dev;
85205354Simp	sc->sc_bus.devices = sc->sc_devices;
86205354Simp	sc->sc_bus.devices_max = OHCI_MAX_DEVICES;
87205354Simp
88205354Simp	/* get all DMA memory */
89205354Simp	if (usb_bus_mem_alloc_all(&sc->sc_bus, USB_GET_DMA_TAG(dev),
90205354Simp	    &ohci_iterate_hw_softc)) {
91205354Simp		return (ENOMEM);
92205354Simp	}
93205354Simp
94205354Simp	sc->sc_dev = dev;
95205354Simp
96205354Simp	rid = 0;
97205354Simp	sc->sc_io_res = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
98205354Simp	    &rid, RF_ACTIVE);
99205354Simp
100205354Simp	if (!(sc->sc_io_res)) {
101205354Simp		err = ENOMEM;
102205354Simp		goto error;
103205354Simp	}
104205354Simp	sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
105205354Simp	sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
106205354Simp	sc->sc_io_size = rman_get_size(sc->sc_io_res);
107205354Simp
108205354Simp	rid = 0;
109205354Simp	sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
110205354Simp	    RF_ACTIVE);
111205354Simp	if (!(sc->sc_irq_res)) {
112205354Simp		goto error;
113205354Simp	}
114205354Simp	sc->sc_bus.bdev = device_add_child(dev, "usbus", -1);
115205354Simp	if (!(sc->sc_bus.bdev)) {
116205354Simp		goto error;
117205354Simp	}
118205354Simp	device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
119205354Simp
120205354Simp	strlcpy(sc->sc_vendor, "Samsung", sizeof(sc->sc_vendor));
121205354Simp
122205354Simp	err = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
123205354Simp	    NULL, (void *)ohci_interrupt, sc, &sc->sc_intr_hdl);
124205354Simp	if (err) {
125205354Simp		sc->sc_intr_hdl = NULL;
126205354Simp		goto error;
127205354Simp	}
128205354Simp
129205354Simp	bus_space_write_4(sc->sc_io_tag, sc->sc_io_hdl,
130205354Simp	    OHCI_CONTROL, 0);
131205354Simp
132205354Simp	err = ohci_init(sc);
133205354Simp	if (!err) {
134205354Simp		err = device_probe_and_attach(sc->sc_bus.bdev);
135205354Simp	}
136205354Simp	if (err) {
137205354Simp		goto error;
138205354Simp	}
139205354Simp	return (0);
140205354Simp
141205354Simperror:
142205354Simp	ohci_s3c24x0_detach(dev);
143205354Simp	return (ENXIO);
144205354Simp}
145205354Simp
146205354Simpstatic int
147205354Simpohci_s3c24x0_detach(device_t dev)
148205354Simp{
149205354Simp	struct ohci_softc *sc = device_get_softc(dev);
150205354Simp	device_t bdev;
151205354Simp	int err;
152205354Simp
153205354Simp	if (sc->sc_bus.bdev) {
154205354Simp		bdev = sc->sc_bus.bdev;
155205354Simp		device_detach(bdev);
156205354Simp		device_delete_child(dev, bdev);
157205354Simp	}
158205354Simp	/* during module unload there are lots of children leftover */
159227849Shselasky	device_delete_children(dev);
160205354Simp
161205354Simp	/*
162205354Simp	 * Put the controller into reset, then disable clocks and do
163205354Simp	 * the MI tear down.  We have to disable the clocks/hardware
164205354Simp	 * after we do the rest of the teardown.  We also disable the
165205354Simp	 * clocks in the opposite order we acquire them, but that
166205354Simp	 * doesn't seem to be absolutely necessary.  We free up the
167205354Simp	 * clocks after we disable them, so the system could, in
168205354Simp	 * theory, reuse them.
169205354Simp	 */
170205354Simp	bus_space_write_4(sc->sc_io_tag, sc->sc_io_hdl,
171205354Simp	    OHCI_CONTROL, 0);
172205354Simp
173205354Simp	if (sc->sc_irq_res && sc->sc_intr_hdl) {
174205354Simp		/*
175205354Simp		 * only call ohci_detach() after ohci_init()
176205354Simp		 */
177205354Simp		ohci_detach(sc);
178205354Simp
179205354Simp		err = bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_intr_hdl);
180205354Simp		sc->sc_intr_hdl = NULL;
181205354Simp	}
182205354Simp	if (sc->sc_irq_res) {
183205354Simp		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
184205354Simp		sc->sc_irq_res = NULL;
185205354Simp	}
186205354Simp	if (sc->sc_io_res) {
187205354Simp		bus_release_resource(dev, SYS_RES_MEMORY, 0,
188205354Simp		    sc->sc_io_res);
189205354Simp		sc->sc_io_res = NULL;
190205354Simp	}
191205354Simp	usb_bus_mem_free_all(&sc->sc_bus, &ohci_iterate_hw_softc);
192205354Simp
193205354Simp	return (0);
194205354Simp}
195205354Simp
196205354Simpstatic device_method_t ohci_methods[] = {
197205354Simp	/* Device interface */
198205354Simp	DEVMETHOD(device_probe, ohci_s3c24x0_probe),
199205354Simp	DEVMETHOD(device_attach, ohci_s3c24x0_attach),
200205354Simp	DEVMETHOD(device_detach, ohci_s3c24x0_detach),
201228483Shselasky	DEVMETHOD(device_suspend, bus_generic_suspend),
202228483Shselasky	DEVMETHOD(device_resume, bus_generic_resume),
203205354Simp	DEVMETHOD(device_shutdown, bus_generic_shutdown),
204205354Simp
205227843Smarius	DEVMETHOD_END
206205354Simp};
207205354Simp
208205354Simpstatic driver_t ohci_driver = {
209228483Shselasky	.name = "ohci",
210228483Shselasky	.methods = ohci_methods,
211228483Shselasky	.size = sizeof(struct ohci_softc),
212205354Simp};
213205354Simp
214205354Simpstatic devclass_t ohci_devclass;
215205354Simp
216205354SimpDRIVER_MODULE(ohci, s3c24x0, ohci_driver, ohci_devclass, 0, 0);
217205354SimpMODULE_DEPEND(ohci, usb, 1, 1, 1);
218