1 | /*- |
---|
2 | * Copyright (c) 2012 Hans Petter Selasky. |
---|
3 | * Copyright (c) 2015 embedded brains GmbH. |
---|
4 | * All rights reserved. |
---|
5 | * |
---|
6 | * Redistribution and use in source and binary forms, with or without |
---|
7 | * modification, are permitted provided that the following conditions |
---|
8 | * are met: |
---|
9 | * 1. Redistributions of source code must retain the above copyright |
---|
10 | * notice, this list of conditions and the following disclaimer. |
---|
11 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
12 | * notice, this list of conditions and the following disclaimer in the |
---|
13 | * documentation and/or other materials provided with the distribution. |
---|
14 | * |
---|
15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
---|
16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
19 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
20 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
21 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
22 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
23 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
24 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
25 | * SUCH DAMAGE. |
---|
26 | */ |
---|
27 | |
---|
28 | #include <machine/rtems-bsd-kernel-space.h> |
---|
29 | |
---|
30 | #include <sys/cdefs.h> |
---|
31 | __FBSDID("$FreeBSD$"); |
---|
32 | |
---|
33 | #include <sys/param.h> |
---|
34 | #include <sys/systm.h> |
---|
35 | #include <sys/bus.h> |
---|
36 | #include <sys/condvar.h> |
---|
37 | #include <sys/kernel.h> |
---|
38 | #include <sys/lock.h> |
---|
39 | #include <sys/malloc.h> |
---|
40 | #include <sys/module.h> |
---|
41 | #include <sys/mutex.h> |
---|
42 | #include <sys/rman.h> |
---|
43 | |
---|
44 | #include <dev/usb/usb.h> |
---|
45 | #include <dev/usb/usbdi.h> |
---|
46 | |
---|
47 | #include <dev/usb/usb_core.h> |
---|
48 | #include <dev/usb/usb_busdma.h> |
---|
49 | #include <dev/usb/usb_process.h> |
---|
50 | #include <dev/usb/usb_util.h> |
---|
51 | |
---|
52 | #include <dev/usb/usb_controller.h> |
---|
53 | #include <dev/usb/usb_bus.h> |
---|
54 | |
---|
55 | #include <dev/usb/controller/dwc_otg.h> |
---|
56 | |
---|
57 | static device_probe_t dwc_otg_probe; |
---|
58 | static device_detach_t dwc_otg_detach; |
---|
59 | |
---|
60 | static int |
---|
61 | dwc_otg_probe(device_t dev) |
---|
62 | { |
---|
63 | |
---|
64 | device_set_desc(dev, "DWC OTG 2.0 integrated USB controller"); |
---|
65 | |
---|
66 | return (BUS_PROBE_DEFAULT); |
---|
67 | } |
---|
68 | |
---|
69 | int |
---|
70 | dwc_otg_attach(device_t dev) |
---|
71 | { |
---|
72 | struct dwc_otg_softc *sc = device_get_softc(dev); |
---|
73 | char usb_mode[24]; |
---|
74 | int err; |
---|
75 | int rid; |
---|
76 | |
---|
77 | /* initialise some bus fields */ |
---|
78 | sc->sc_bus.parent = dev; |
---|
79 | sc->sc_bus.devices = sc->sc_devices; |
---|
80 | sc->sc_bus.devices_max = DWC_OTG_MAX_DEVICES; |
---|
81 | sc->sc_bus.dma_bits = 32; |
---|
82 | |
---|
83 | /* force USB host mode */ |
---|
84 | sc->sc_mode = DWC_MODE_HOST; |
---|
85 | |
---|
86 | /* get all DMA memory */ |
---|
87 | if (usb_bus_mem_alloc_all(&sc->sc_bus, |
---|
88 | USB_GET_DMA_TAG(dev), NULL)) { |
---|
89 | return (ENOMEM); |
---|
90 | } |
---|
91 | rid = 0; |
---|
92 | sc->sc_io_res = |
---|
93 | bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE); |
---|
94 | |
---|
95 | if (!(sc->sc_io_res)) { |
---|
96 | err = ENOMEM; |
---|
97 | goto error; |
---|
98 | } |
---|
99 | sc->sc_io_tag = rman_get_bustag(sc->sc_io_res); |
---|
100 | sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res); |
---|
101 | sc->sc_io_size = rman_get_size(sc->sc_io_res); |
---|
102 | |
---|
103 | rid = 0; |
---|
104 | sc->sc_irq_res = |
---|
105 | bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE); |
---|
106 | if (sc->sc_irq_res == NULL) |
---|
107 | goto error; |
---|
108 | |
---|
109 | sc->sc_bus.bdev = device_add_child(dev, "usbus", -1); |
---|
110 | if (sc->sc_bus.bdev == NULL) |
---|
111 | goto error; |
---|
112 | |
---|
113 | device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus); |
---|
114 | |
---|
115 | err = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_TTY | INTR_MPSAFE, |
---|
116 | &dwc_otg_filter_interrupt, &dwc_otg_interrupt, sc, &sc->sc_intr_hdl); |
---|
117 | if (err) { |
---|
118 | sc->sc_intr_hdl = NULL; |
---|
119 | goto error; |
---|
120 | } |
---|
121 | err = dwc_otg_init(sc); |
---|
122 | if (err == 0) { |
---|
123 | err = device_probe_and_attach(sc->sc_bus.bdev); |
---|
124 | } |
---|
125 | if (err) |
---|
126 | goto error; |
---|
127 | |
---|
128 | |
---|
129 | return (0); |
---|
130 | |
---|
131 | error: |
---|
132 | return (ENXIO); |
---|
133 | } |
---|
134 | |
---|
135 | |
---|
136 | static device_method_t dwc_otg_methods[] = { |
---|
137 | /* Device interface */ |
---|
138 | DEVMETHOD(device_probe, dwc_otg_probe), |
---|
139 | DEVMETHOD(device_attach, dwc_otg_attach), |
---|
140 | DEVMETHOD(device_suspend, bus_generic_suspend), |
---|
141 | DEVMETHOD(device_resume, bus_generic_resume), |
---|
142 | DEVMETHOD(device_shutdown, bus_generic_shutdown), |
---|
143 | |
---|
144 | DEVMETHOD_END |
---|
145 | }; |
---|
146 | |
---|
147 | driver_t dwc_otg_driver = { |
---|
148 | .name = "dwcotg", |
---|
149 | .methods = dwc_otg_methods, |
---|
150 | .size = sizeof(struct dwc_otg_softc), |
---|
151 | }; |
---|
152 | |
---|
153 | static devclass_t dwc_otg_devclass; |
---|
154 | |
---|
155 | DRIVER_MODULE(dwcotg, nexus, dwc_otg_driver, dwc_otg_devclass, 0, 0); |
---|
156 | MODULE_DEPEND(dwcotg, usb, 1, 1, 1); |
---|