]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/usb/controller/ohci_s3c24x0.c
MFC r307518:
[FreeBSD/stable/10.git] / sys / dev / usb / controller / ohci_s3c24x0.c
1 /*-
2  * Copyright (c) 2006 M. Warner Losh.  All rights reserved.
3  * Copyright (c) 2009 Andrew Turner.  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$");
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/samsung/s3c2xx0/s3c24x0reg.h>
64
65 static device_probe_t ohci_s3c24x0_probe;
66 static device_attach_t ohci_s3c24x0_attach;
67 static device_detach_t ohci_s3c24x0_detach;
68
69 static int
70 ohci_s3c24x0_probe(device_t dev)
71 {
72         device_set_desc(dev, "S3C24x0 integrated OHCI controller");
73         return (BUS_PROBE_DEFAULT);
74 }
75
76 static int
77 ohci_s3c24x0_attach(device_t dev)
78 {
79         struct ohci_softc *sc = device_get_softc(dev);
80         int err;
81         int rid;
82
83         /* initialise some bus fields */
84         sc->sc_bus.parent = dev;
85         sc->sc_bus.devices = sc->sc_devices;
86         sc->sc_bus.devices_max = OHCI_MAX_DEVICES;
87         sc->sc_bus.dma_bits = 32;
88
89         /* get all DMA memory */
90         if (usb_bus_mem_alloc_all(&sc->sc_bus, USB_GET_DMA_TAG(dev),
91             &ohci_iterate_hw_softc)) {
92                 return (ENOMEM);
93         }
94
95         sc->sc_dev = dev;
96
97         rid = 0;
98         sc->sc_io_res = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
99             &rid, RF_ACTIVE);
100
101         if (!(sc->sc_io_res)) {
102                 err = ENOMEM;
103                 goto error;
104         }
105         sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
106         sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
107         sc->sc_io_size = rman_get_size(sc->sc_io_res);
108
109         rid = 0;
110         sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
111             RF_ACTIVE);
112         if (!(sc->sc_irq_res)) {
113                 goto error;
114         }
115         sc->sc_bus.bdev = device_add_child(dev, "usbus", -1);
116         if (!(sc->sc_bus.bdev)) {
117                 goto error;
118         }
119         device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
120
121         strlcpy(sc->sc_vendor, "Samsung", sizeof(sc->sc_vendor));
122
123         err = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
124             NULL, (void *)ohci_interrupt, sc, &sc->sc_intr_hdl);
125         if (err) {
126                 sc->sc_intr_hdl = NULL;
127                 goto error;
128         }
129
130         bus_space_write_4(sc->sc_io_tag, sc->sc_io_hdl,
131             OHCI_CONTROL, 0);
132
133         err = ohci_init(sc);
134         if (!err) {
135                 err = device_probe_and_attach(sc->sc_bus.bdev);
136         }
137         if (err) {
138                 goto error;
139         }
140         return (0);
141
142 error:
143         ohci_s3c24x0_detach(dev);
144         return (ENXIO);
145 }
146
147 static int
148 ohci_s3c24x0_detach(device_t dev)
149 {
150         struct ohci_softc *sc = device_get_softc(dev);
151         int err;
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_io_tag, sc->sc_io_hdl,
166             OHCI_CONTROL, 0);
167
168         if (sc->sc_irq_res && sc->sc_intr_hdl) {
169                 /*
170                  * only call ohci_detach() after ohci_init()
171                  */
172                 ohci_detach(sc);
173
174                 err = bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_intr_hdl);
175                 sc->sc_intr_hdl = NULL;
176         }
177         if (sc->sc_irq_res) {
178                 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
179                 sc->sc_irq_res = NULL;
180         }
181         if (sc->sc_io_res) {
182                 bus_release_resource(dev, SYS_RES_MEMORY, 0,
183                     sc->sc_io_res);
184                 sc->sc_io_res = NULL;
185         }
186         usb_bus_mem_free_all(&sc->sc_bus, &ohci_iterate_hw_softc);
187
188         return (0);
189 }
190
191 static device_method_t ohci_methods[] = {
192         /* Device interface */
193         DEVMETHOD(device_probe, ohci_s3c24x0_probe),
194         DEVMETHOD(device_attach, ohci_s3c24x0_attach),
195         DEVMETHOD(device_detach, ohci_s3c24x0_detach),
196         DEVMETHOD(device_suspend, bus_generic_suspend),
197         DEVMETHOD(device_resume, bus_generic_resume),
198         DEVMETHOD(device_shutdown, bus_generic_shutdown),
199
200         DEVMETHOD_END
201 };
202
203 static driver_t ohci_driver = {
204         .name = "ohci",
205         .methods = ohci_methods,
206         .size = sizeof(struct ohci_softc),
207 };
208
209 static devclass_t ohci_devclass;
210
211 DRIVER_MODULE(ohci, s3c24x0, ohci_driver, ohci_devclass, 0, 0);
212 MODULE_DEPEND(ohci, usb, 1, 1, 1);