ralink_ohci.c revision 1.1.2.1 1 /* $NetBSD: ralink_ohci.c,v 1.1.2.1 2011/07/01 05:45:45 matt Exp $ */
2 /*-
3 * Copyright (c) 2011 CradlePoint Technology, Inc.
4 * All rights reserved.
5 *
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 CRADLEPOINT TECHNOLOGY, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /* ralink_ohci.c -- Ralink OHCI USB Driver */
30
31 #include "ehci.h"
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: ralink_ohci.c,v 1.1.2.1 2011/07/01 05:45:45 matt Exp $");
35
36 #include <sys/param.h>
37 #include <sys/bus.h>
38
39 #include <dev/usb/usb.h>
40 #include <dev/usb/usbdi.h>
41 #include <dev/usb/usbdivar.h>
42 #include <dev/usb/usb_mem.h>
43
44 #include <dev/usb/ohcireg.h>
45 #include <dev/usb/ohcivar.h>
46
47 #include <mips/ralink/ralink_var.h>
48 #include <mips/ralink/ralink_reg.h>
49 #include <mips/ralink/ralink_usbhcvar.h>
50
51 #define OREAD4(sc, a) bus_space_read_4((sc)->iot, (sc)->ioh, (a))
52 #define OWRITE4(sc, a, x) bus_space_write_4((sc)->iot, (sc)->ioh, (a), (x))
53
54 #define RT3XXX_OHCI_BASE 0x101c1000
55 #define RT3XXX_BLOCK_SIZE 0x1000
56
57 struct ralink_ohci_softc {
58 struct ohci_softc sc_ohci;
59 #if NEHCI > 0
60 struct ralink_usb_hc sc_hc;
61 #endif
62 void *sc_ih;
63 };
64
65 static int ralink_ohci_match(device_t, cfdata_t, void *);
66 static void ralink_ohci_attach(device_t, device_t, void *);
67 static int ralink_ohci_detach(device_t, int);
68 static bool ralink_ohci_suspend(device_t PMF_FN_PROTO);
69 static bool ralink_ohci_resume(device_t PMF_FN_PROTO);
70
71 CFATTACH_DECL2_NEW(ralink_ohci, sizeof(struct ralink_ohci_softc),
72 ralink_ohci_match, ralink_ohci_attach, ralink_ohci_detach,
73 ohci_activate, NULL, ohci_childdet);
74
75 /*
76 * ralink_ohci_match
77 */
78 static int
79 ralink_ohci_match(device_t parent, cfdata_t cf, void *aux)
80 {
81 return 1;
82 }
83
84 /*
85 * ralink_ohci_attach
86 */
87 static void
88 ralink_ohci_attach(device_t parent, device_t self, void *aux)
89 {
90 struct ralink_ohci_softc * const sc = device_private(self);
91 const struct mainbus_attach_args *ma = aux;
92 usbd_status status;
93 int error;
94 #ifdef RALINK_OHCI_DEBUG
95 const char * const devname = device_xname(self);
96 #endif
97
98 aprint_naive(": OHCI USB controller\n");
99 aprint_normal(": OHCI USB controller\n");
100
101 sc->sc_ohci.sc_dev = self;
102 sc->sc_ohci.sc_bus.hci_private = sc;
103 sc->sc_ohci.iot = ma->ma_memt;
104 sc->sc_ohci.sc_bus.dmatag = ma->ma_dmat;
105
106 /* Map I/O registers */
107 if ((error = bus_space_map(sc->sc_ohci.iot, RT3XXX_OHCI_BASE,
108 RT3XXX_BLOCK_SIZE, 0, &sc->sc_ohci.ioh)) != 0) {
109 aprint_error_dev(self, "can't map OHCI registers, "
110 "error=%d\n", error);
111 return;
112 }
113
114 sc->sc_ohci.sc_size = RT3XXX_BLOCK_SIZE;
115
116 #ifdef RALINK_OHCI_DEBUG
117 printf("%s sc: %p ma: %p\n", devname, sc, ma);
118 printf("%s memt: %p dmat: %p\n", devname, ma->ma_memt, ma->ma_dmat);
119
120 printf("%s: OHCI HcRevision=0x%x\n", devname,
121 OREAD4(&sc->sc_ohci, OHCI_REVISION));
122 printf("%s: OHCI HcControl=0x%x\n", devname,
123 OREAD4(&sc->sc_ohci, OHCI_CONTROL));
124 printf("%s: OHCI HcCommandStatus=0x%x\n", devname,
125 OREAD4(&sc->sc_ohci, OHCI_COMMAND_STATUS));
126 printf("%s: OHCI HcInterruptStatus=0x%x\n", devname,
127 OREAD4(&sc->sc_ohci, OHCI_INTERRUPT_STATUS));
128 #endif
129
130 /* Disable OHCI interrupts. */
131 OWRITE4(&sc->sc_ohci, OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS);
132
133 /* establish the MIPS level interrupt */
134 sc->sc_ih = ra_intr_establish(RA_IRQ_USB, ohci_intr, sc, 0);
135 if (sc->sc_ih == NULL) {
136 aprint_error_dev(self, "unable to establish irq %d\n",
137 RA_IRQ_USB);
138 goto fail_0;
139 }
140
141 /* Set vendor for root hub descriptor. */
142 sc->sc_ohci.sc_id_vendor = 0x1814;
143 strlcpy(sc->sc_ohci.sc_vendor, "Ralink", sizeof(sc->sc_ohci.sc_vendor));
144
145 /* Initialize OHCI */
146 status = ohci_init(&sc->sc_ohci);
147 if (status != USBD_NORMAL_COMPLETION) {
148 aprint_error_dev(self, "init failed, error=%d\n", status);
149 goto fail_0;
150 }
151
152 #if NEHCI > 0
153 ralink_usb_hc_add(&sc->sc_hc, self);
154 #endif
155
156 if (!pmf_device_register1(self, ralink_ohci_suspend, ralink_ohci_resume,
157 ohci_shutdown))
158 aprint_error_dev(self, "couldn't establish power handler\n");
159
160 /* Attach usb device. */
161 sc->sc_ohci.sc_child = config_found(self, &sc->sc_ohci.sc_bus,
162 usbctlprint);
163
164 return;
165
166 fail_0:
167 bus_space_unmap(sc->sc_ohci.iot, sc->sc_ohci.ioh, sc->sc_ohci.sc_size);
168 sc->sc_ohci.sc_size = 0;
169 }
170
171 static int
172 ralink_ohci_detach(device_t self, int flags)
173 {
174 struct ralink_ohci_softc *sc = device_private(self);
175 int rv;
176
177 pmf_device_deregister(self);
178
179 rv = ohci_detach(&sc->sc_ohci, flags);
180 if (rv != 0)
181 return rv;
182
183 if (sc->sc_ih != NULL) {
184 ra_intr_disestablish(sc->sc_ih);
185 sc->sc_ih = NULL;
186 }
187
188 if (sc->sc_ohci.sc_size == 0) {
189 bus_space_unmap(sc->sc_ohci.iot, sc->sc_ohci.ioh,
190 sc->sc_ohci.sc_size);
191 sc->sc_ohci.sc_size = 0;
192 }
193
194 #if NEHCI > 0
195 ralink_usb_hc_rem(&sc->sc_hc);
196 #endif
197
198 return 0;
199 }
200
201 /*
202 * ralink_ohci_suspend
203 */
204 static bool
205 ralink_ohci_suspend(device_t dv PMF_FN_ARGS)
206 {
207 ohci_suspend(dv PMF_FN_CALL);
208
209 return true;
210 }
211
212 /*
213 * ralink_ohci_resume
214 */
215 static bool
216 ralink_ohci_resume(device_t dv PMF_FN_ARGS)
217 {
218 return ohci_resume(dv PMF_FN_CALL);
219 }
220