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