]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/usb/controller/ohci_s3c24x0.c
Upgrade our copies of clang, llvm, lld, lldb, compiler-rt and libc++ to
[FreeBSD/FreeBSD.git] / sys / dev / usb / controller / ohci_s3c24x0.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2006 M. Warner Losh.  All rights reserved.
5  * Copyright (c) 2009 Andrew Turner.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/stdint.h>
32 #include <sys/stddef.h>
33 #include <sys/param.h>
34 #include <sys/queue.h>
35 #include <sys/types.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/bus.h>
39 #include <sys/module.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <sys/condvar.h>
43 #include <sys/sysctl.h>
44 #include <sys/sx.h>
45 #include <sys/unistd.h>
46 #include <sys/callout.h>
47 #include <sys/malloc.h>
48 #include <sys/priv.h>
49
50 #include <dev/usb/usb.h>
51 #include <dev/usb/usbdi.h>
52
53 #include <dev/usb/usb_core.h>
54 #include <dev/usb/usb_busdma.h>
55 #include <dev/usb/usb_process.h>
56 #include <dev/usb/usb_util.h>
57
58 #include <dev/usb/usb_controller.h>
59 #include <dev/usb/usb_bus.h>
60 #include <dev/usb/controller/ohci.h>
61 #include <dev/usb/controller/ohcireg.h>
62
63 #include <sys/rman.h>
64
65 #include <arm/samsung/s3c2xx0/s3c24x0reg.h>
66
67 static device_probe_t ohci_s3c24x0_probe;
68 static device_attach_t ohci_s3c24x0_attach;
69 static device_detach_t ohci_s3c24x0_detach;
70
71 static int
72 ohci_s3c24x0_probe(device_t dev)
73 {
74         device_set_desc(dev, "S3C24x0 integrated OHCI controller");
75         return (BUS_PROBE_DEFAULT);
76 }
77
78 static int
79 ohci_s3c24x0_attach(device_t dev)
80 {
81         struct ohci_softc *sc = device_get_softc(dev);
82         int err;
83         int rid;
84
85         /* initialise some bus fields */
86         sc->sc_bus.parent = dev;
87         sc->sc_bus.devices = sc->sc_devices;
88         sc->sc_bus.devices_max = OHCI_MAX_DEVICES;
89         sc->sc_bus.dma_bits = 32;
90
91         /* get all DMA memory */
92         if (usb_bus_mem_alloc_all(&sc->sc_bus, USB_GET_DMA_TAG(dev),
93             &ohci_iterate_hw_softc)) {
94                 return (ENOMEM);
95         }
96
97         sc->sc_dev = dev;
98
99         rid = 0;
100         sc->sc_io_res = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
101             &rid, RF_ACTIVE);
102
103         if (!(sc->sc_io_res)) {
104                 err = ENOMEM;
105                 goto error;
106         }
107         sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
108         sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
109         sc->sc_io_size = rman_get_size(sc->sc_io_res);
110
111         rid = 0;
112         sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
113             RF_ACTIVE);
114         if (!(sc->sc_irq_res)) {
115                 goto error;
116         }
117         sc->sc_bus.bdev = device_add_child(dev, "usbus", -1);
118         if (!(sc->sc_bus.bdev)) {
119                 goto error;
120         }
121         device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
122
123         strlcpy(sc->sc_vendor, "Samsung", sizeof(sc->sc_vendor));
124
125         err = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
126             NULL, (void *)ohci_interrupt, sc, &sc->sc_intr_hdl);
127         if (err) {
128                 sc->sc_intr_hdl = NULL;
129                 goto error;
130         }
131
132         bus_space_write_4(sc->sc_io_tag, sc->sc_io_hdl,
133             OHCI_CONTROL, 0);
134
135         err = ohci_init(sc);
136         if (!err) {
137                 err = device_probe_and_attach(sc->sc_bus.bdev);
138         }
139         if (err) {
140                 goto error;
141         }
142         return (0);
143
144 error:
145         ohci_s3c24x0_detach(dev);
146         return (ENXIO);
147 }
148
149 static int
150 ohci_s3c24x0_detach(device_t dev)
151 {
152         struct ohci_softc *sc = device_get_softc(dev);
153         int err;
154
155         /* during module unload there are lots of children leftover */
156         device_delete_children(dev);
157
158         /*
159          * Put the controller into reset, then disable clocks and do
160          * the MI tear down.  We have to disable the clocks/hardware
161          * after we do the rest of the teardown.  We also disable the
162          * clocks in the opposite order we acquire them, but that
163          * doesn't seem to be absolutely necessary.  We free up the
164          * clocks after we disable them, so the system could, in
165          * theory, reuse them.
166          */
167         bus_space_write_4(sc->sc_io_tag, sc->sc_io_hdl,
168             OHCI_CONTROL, 0);
169
170         if (sc->sc_irq_res && sc->sc_intr_hdl) {
171                 /*
172                  * only call ohci_detach() after ohci_init()
173                  */
174                 ohci_detach(sc);
175
176                 err = bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_intr_hdl);
177                 sc->sc_intr_hdl = NULL;
178         }
179         if (sc->sc_irq_res) {
180                 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
181                 sc->sc_irq_res = NULL;
182         }
183         if (sc->sc_io_res) {
184                 bus_release_resource(dev, SYS_RES_MEMORY, 0,
185                     sc->sc_io_res);
186                 sc->sc_io_res = NULL;
187         }
188         usb_bus_mem_free_all(&sc->sc_bus, &ohci_iterate_hw_softc);
189
190         return (0);
191 }
192
193 static device_method_t ohci_methods[] = {
194         /* Device interface */
195         DEVMETHOD(device_probe, ohci_s3c24x0_probe),
196         DEVMETHOD(device_attach, ohci_s3c24x0_attach),
197         DEVMETHOD(device_detach, ohci_s3c24x0_detach),
198         DEVMETHOD(device_suspend, bus_generic_suspend),
199         DEVMETHOD(device_resume, bus_generic_resume),
200         DEVMETHOD(device_shutdown, bus_generic_shutdown),
201
202         DEVMETHOD_END
203 };
204
205 static driver_t ohci_driver = {
206         .name = "ohci",
207         .methods = ohci_methods,
208         .size = sizeof(struct ohci_softc),
209 };
210
211 static devclass_t ohci_devclass;
212
213 DRIVER_MODULE(ohci, s3c24x0, ohci_driver, ohci_devclass, 0, 0);
214 MODULE_DEPEND(ohci, usb, 1, 1, 1);