rmixl_usbi.c revision 1.1.2.5 1 /* $NetBSD: rmixl_usbi.c,v 1.1.2.5 2010/04/12 22:42:07 cliff Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 1999, 2000, 2002, 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Cliff Neighbors
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: rmixl_usbi.c,v 1.1.2.5 2010/04/12 22:42:07 cliff Exp $");
34
35 #include "locators.h"
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/device.h>
40
41 #include <machine/bus.h>
42
43 #include <mips/rmi/rmixlreg.h>
44 #include <mips/rmi/rmixlvar.h>
45 #include <mips/rmi/rmixl_intr.h>
46 #include <mips/rmi/rmixl_obiovar.h>
47 #include <mips/rmi/rmixl_usbivar.h>
48
49 #include <dev/usb/usb.h>
50 #include <dev/usb/usbdi.h>
51 #include <dev/usb/usbdivar.h>
52 #include <dev/usb/usb_mem.h>
53
54 /*
55 * USB I/O register byte order is
56 * LITTLE ENDIAN regardless of code model
57 */
58 #define RMIXL_USBI_GEN_VADDR(o) \
59 (volatile uint32_t *)MIPS_PHYS_TO_KSEG1( \
60 rmixl_configuration.rc_io_pbase + RMIXL_IO_DEV_USB_B + (o))
61 #define RMIXL_USBI_GEN_READ(o) le32toh(*RMIXL_USBI_GEN_VADDR(o))
62 #define RMIXL_USBI_GEN_WRITE(o,v) *RMIXL_USBI_GEN_VADDR(o) = htole32(v)
63
64 static const char *rmixl_usbi_intrnames[RMIXL_UB_INTERRUPT_MAX+1] = {
65 "int 0 (ohci0)",
66 "int 1 (ohci1)",
67 "int 2 (ehci)",
68 "int 3 (device)",
69 "int 4 (phy)",
70 "int 5 (force)"
71 };
72
73 typedef struct rmixl_usbi_dispatch {
74 int (*func)(void *);
75 void *arg;
76 struct evcnt count;
77 } rmixl_usbi_dispatch_t;
78
79 typedef struct rmixl_usbi_softc {
80 device_t sc_dev;
81 bus_space_tag_t sc_eb_bst;
82 bus_space_tag_t sc_el_bst;
83 bus_addr_t sc_addr;
84 bus_size_t sc_size;
85 bus_dma_tag_t sc_dmat;
86 rmixl_usbi_dispatch_t sc_dispatch[RMIXL_UB_INTERRUPT_MAX + 1];
87 } rmixl_usbi_softc_t;
88
89
90 static int rmixl_usbi_match(device_t, cfdata_t, void *);
91 static void rmixl_usbi_attach(device_t, device_t, void *);
92 static int rmixl_usbi_print(void *, const char *);
93 static int rmixl_usbi_search(device_t, cfdata_t, const int *, void *);
94 static int rmixl_usbi_intr(void *);
95
96 #ifdef RMIXL_USBI_DEBUG
97 int rmixl_usbi_rdump(void);
98 rmixl_usbi_softc_t *rmixl_usbi_sc;
99 #endif
100
101
102 CFATTACH_DECL_NEW(rmixl_usbi, sizeof (rmixl_usbi_softc_t),
103 rmixl_usbi_match, rmixl_usbi_attach, NULL, NULL);
104
105 int
106 rmixl_usbi_match(device_t parent, cfdata_t match, void *aux)
107 {
108 struct obio_attach_args *obio = aux;
109
110 if (obio->obio_addr == RMIXL_IO_DEV_USB_B)
111 return rmixl_probe_4((volatile uint32_t *)RMIXL_IOREG_VADDR(obio->obio_addr));
112
113 return 0;
114 }
115
116 void
117 rmixl_usbi_attach(device_t parent, device_t self, void *aux)
118 {
119 rmixl_usbi_softc_t *sc = device_private(self);
120 struct obio_attach_args *obio = aux;
121 uint32_t r;
122 void *ih;
123
124 #ifdef RMIXL_USBI_DEBUG
125 rmixl_usbi_sc = sc;
126 #endif
127 sc->sc_dev = self;
128 sc->sc_eb_bst = obio->obio_eb_bst;
129 sc->sc_el_bst = obio->obio_el_bst;
130 sc->sc_addr = obio->obio_addr;
131 sc->sc_size = obio->obio_size;
132 sc->sc_dmat = obio->obio_32bit_dmat;
133
134 aprint_normal("\n%s", device_xname(self));
135
136 /*
137 * fail attach if USB interface is disabled GPIO LOW_PWR_DIS reg
138 */
139 r = RMIXL_IOREG_READ(RMIXL_IO_DEV_GPIO + RMIXL_GPIO_LOW_PWR_DIS);
140 if ((r & RMIXL_GPIO_LOW_PWR_DIS_USB) != 0) {
141 aprint_error(": USB_DIS set in LOW_PWR_DIS, abort attach\n");
142 return;
143 }
144
145 /*
146 * fail attach if USB interface BIST failed
147 */
148 r = RMIXL_IOREG_READ(RMIXL_IO_DEV_GPIO + RMIXL_GPIO_BIST_EACH_STS);
149 aprint_normal(": BIST status=");
150 if ((r & __BIT(18)) == 0) { /* XXX USB_BIST */
151 aprint_normal("FAIL\n");
152 return;
153 }
154 aprint_normal("OK");
155
156 /*
157 * set BYTESWAP_EN register nonzero when software is little endian
158 */
159 #if BYTE_ORDER == BIG_ENDIAN
160 r = 0;
161 #else
162 r = 1;
163 #endif
164 RMIXL_USBI_GEN_WRITE(RMIXL_USB_BYTESWAP_EN, r);
165 aprint_normal(" byteswap enable=%d", r);
166 for (int intr=0; intr <= RMIXL_UB_INTERRUPT_MAX; intr++) {
167 evcnt_attach_dynamic(&sc->sc_dispatch[intr].count,
168 EVCNT_TYPE_INTR, NULL, "rmixl_usbi",
169 rmixl_usbi_intrnames[intr]);
170 }
171
172 /* Disable all usb interface interrupts */
173 RMIXL_USBI_GEN_WRITE(RMIXL_USB_INTERRUPT_ENABLE, 0);
174
175 /* establish interrupt */
176 ih = rmixl_intr_establish(obio->obio_intr, obio->obio_tmsk,
177 IPL_USB, RMIXL_TRIG_LEVEL, RMIXL_POLR_HIGH,
178 rmixl_usbi_intr, sc, false);
179 if (ih == NULL)
180 panic("%s: couldn't establish interrupt", device_xname(self));
181
182 aprint_normal("\n");
183
184 /* attach any children */
185 config_search_ia(rmixl_usbi_search, self, "rmixl_usbi", NULL);
186 }
187
188 static int
189 rmixl_usbi_print(void *aux, const char *pnp)
190 {
191 struct rmixl_usbi_attach_args *usbi = aux;
192
193 if (usbi->usbi_addr != RMIXL_USBICF_ADDR_DEFAULT) {
194 aprint_normal(" addr %#"PRIxBUSADDR, usbi->usbi_addr);
195 if (usbi->usbi_size != RMIXL_USBICF_SIZE_DEFAULT)
196 aprint_normal("-%#"PRIxBUSADDR,
197 usbi->usbi_addr + (usbi->usbi_size - 1));
198 }
199 if (usbi->usbi_intr != RMIXL_USBICF_INTR_DEFAULT)
200 aprint_normal(" intr %d", usbi->usbi_intr);
201
202 aprint_normal("\n");
203
204 return (UNCONF);
205 }
206
207 static int
208 rmixl_usbi_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
209 {
210 struct rmixl_usbi_softc *sc = device_private(parent);
211 struct rmixl_usbi_attach_args usbi;
212
213 usbi.usbi_eb_bst = sc->sc_eb_bst;
214 usbi.usbi_el_bst = sc->sc_el_bst;
215 usbi.usbi_addr = cf->cf_loc[RMIXL_USBICF_ADDR];
216 usbi.usbi_size = cf->cf_loc[RMIXL_USBICF_SIZE];
217 usbi.usbi_intr = cf->cf_loc[RMIXL_USBICF_INTR];
218 usbi.usbi_dmat = sc->sc_dmat;
219
220 if (config_match(parent, cf, &usbi) > 0)
221 config_attach(parent, cf, &usbi, rmixl_usbi_print);
222
223 return 0;
224 }
225
226
227 void
228 rmixl_usbi_intr_disestablish(void *uh, void *ih)
229 {
230 rmixl_usbi_softc_t *sc = uh;
231 u_int intr;
232
233 for (intr=0; intr <= RMIXL_UB_INTERRUPT_MAX; intr++) {
234 if (ih == &sc->sc_dispatch[intr]) {
235 uint32_t r;
236
237 /* disable this interrupt in the usb interface */
238 r = RMIXL_USBI_GEN_READ(RMIXL_USB_INTERRUPT_ENABLE);
239 r &= 1 << intr;
240 RMIXL_USBI_GEN_WRITE(RMIXL_USB_INTERRUPT_ENABLE, r);
241
242 /* free the dispatch slot */
243 sc->sc_dispatch[intr].func = NULL;
244 sc->sc_dispatch[intr].arg = NULL;
245
246 break;
247 }
248 }
249 }
250
251 void *
252 rmixl_usbi_intr_establish(void *uh, u_int intr, int (func)(void *), void *arg)
253 {
254 rmixl_usbi_softc_t *sc = uh;
255 uint32_t r;
256 void *ih = NULL;
257 int s;
258
259 s = splusb();
260
261 if (intr > RMIXL_UB_INTERRUPT_MAX) {
262 aprint_error_dev(sc->sc_dev, "invalid intr %d\n", intr);
263 goto out;
264 }
265
266 if (sc->sc_dispatch[intr].func != NULL) {
267 aprint_error_dev(sc->sc_dev, "intr %dq busy\n", intr);
268 goto out;
269 }
270
271 sc->sc_dispatch[intr].func = func;
272 sc->sc_dispatch[intr].arg = arg;
273 ih = &sc->sc_dispatch[intr];
274
275 /* enable this interrupt in the usb interface */
276 r = RMIXL_USBI_GEN_READ(RMIXL_USB_INTERRUPT_ENABLE);
277 r |= 1 << intr;
278 RMIXL_USBI_GEN_WRITE(RMIXL_USB_INTERRUPT_ENABLE, r);
279
280 out:
281 splx(s);
282 return ih;
283 }
284
285 static int
286 rmixl_usbi_intr(void *arg)
287 {
288 rmixl_usbi_softc_t *sc = arg;
289 uint32_t r;
290 int intr;
291 int rv = 0;
292
293 r = RMIXL_USBI_GEN_READ(RMIXL_USB_INTERRUPT_STATUS);
294 if (r != 0) {
295 for (intr=0; intr <= RMIXL_UB_INTERRUPT_MAX; intr++) {
296 uint32_t bit = 1 << intr;
297 if ((r & bit) != 0) {
298 int (*f)(void *) = sc->sc_dispatch[intr].func;
299 void *a = sc->sc_dispatch[intr].arg;
300 if (f != NULL) {
301 (void)(*f)(a);
302 sc->sc_dispatch[intr].count.ev_count++;
303 rv = 1;
304 }
305 }
306 }
307 }
308
309 return rv;
310 }
311
312 #ifdef RMIXL_USBI_DEBUG
313 int
314 rmixl_usbi_rdump(void)
315 {
316 rmixl_usbi_softc_t *sc = rmixl_usbi_sc;
317 uint32_t r;
318
319 if (sc == NULL)
320 return -1;
321
322 printf("\n%s:\n", __func__);
323 r = RMIXL_USBI_GEN_READ(RMIXL_USB_GEN_CTRL1);
324 printf(" USB_GEN_CTRL1 %#x\n", r);
325 r = RMIXL_USBI_GEN_READ(RMIXL_USB_GEN_CTRL2);
326 printf(" USB_GEN_CTRL2 %#x\n", r);
327 r = RMIXL_USBI_GEN_READ(RMIXL_USB_GEN_CTRL3);
328 printf(" USB_GEN_CTRL3 %#x\n", r);
329 r = RMIXL_USBI_GEN_READ(RMIXL_USB_IOBM_TIMER);
330 printf(" USB_IOBM_TIMER %#x\n", r);
331 r = RMIXL_USBI_GEN_READ(RMIXL_USB_VBUS_TIMER);
332 printf(" USB_VBUS_TIMER %#x\n", r);
333 r = RMIXL_USBI_GEN_READ(RMIXL_USB_BYTESWAP_EN);
334 printf(" USB_BYTESWAP_EN %#x\n", r);
335 r = RMIXL_USBI_GEN_READ(RMIXL_USB_COHERENT_MEM_BASE);
336 printf(" USB_COHERENT_MEM_BASE %#x\n", r);
337 r = RMIXL_USBI_GEN_READ(RMIXL_USB_COHERENT_MEM_LIMIT);
338 printf(" USB_COHERENT_MEM_LIMIT %#x\n", r);
339 r = RMIXL_USBI_GEN_READ(RMIXL_USB_L2ALLOC_MEM_BASE);
340 printf(" USB_L2ALLOC_MEM_BASE %#x\n", r);
341 r = RMIXL_USBI_GEN_READ(RMIXL_USB_L2ALLOC_MEM_LIMIT);
342 printf(" USB_L2ALLOC_MEM_LIMIT %#x\n", r);
343 r = RMIXL_USBI_GEN_READ(RMIXL_USB_READEX_MEM_BASE);
344 printf(" USB_READEX_MEM_BASE %#x\n", r);
345 r = RMIXL_USBI_GEN_READ(RMIXL_USB_READEX_MEM_LIMIT);
346 printf(" USB_READEX_MEM_LIMIT %#x\n", r);
347 r = RMIXL_USBI_GEN_READ(RMIXL_USB_PHY_STATUS);
348 printf(" USB_PHY_STATUS %#x\n", r);
349 r = RMIXL_USBI_GEN_READ(RMIXL_USB_INTERRUPT_STATUS);
350 printf(" USB_INTERRUPT_STATUS %#x\n", r);
351 r = RMIXL_USBI_GEN_READ(RMIXL_USB_INTERRUPT_ENABLE);
352 printf(" USB_INTERRUPT_ENABLE %#x\n", r);
353
354 return 0;
355 }
356 #endif
357