pq3ehci.c revision 1.1.4.1 1 /* $NetBSD: pq3ehci.c,v 1.1.4.1 2011/06/06 09:06:26 jruoho Exp $ */
2 /*-
3 * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Matt Thomas of 3am Software Foundry.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: pq3ehci.c,v 1.1.4.1 2011/06/06 09:06:26 jruoho Exp $");
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/device.h>
37 #include <sys/kernel.h>
38 #include <sys/proc.h>
39 #include <sys/queue.h>
40
41 #include <sys/bus.h>
42
43 #include <powerpc/booke/cpuvar.h>
44 #include <powerpc/booke/e500var.h>
45 #include <powerpc/booke/e500reg.h>
46
47 #include <dev/usb/usb.h>
48 #include <dev/usb/usbdi.h>
49 #include <dev/usb/usbdivar.h>
50 #include <dev/usb/usb_mem.h>
51
52 #include <dev/usb/ehcireg.h>
53 #include <dev/usb/ehcivar.h>
54
55 #ifdef EHCI_DEBUG
56 #define DPRINTF(x) if (ehcidebug) printf x
57 extern int ehcidebug;
58 #else
59 #define DPRINTF(x)
60 #endif
61
62 static int pq3ehci_match(device_t, cfdata_t, void *);
63 static void pq3ehci_attach(device_t, device_t, void *);
64
65 struct pq3ehci_softc {
66 ehci_softc_t sc;
67 void *sc_ih; /* interrupt vectoring */
68 };
69
70 CFATTACH_DECL_NEW(pq3ehci, sizeof(struct pq3ehci_softc),
71 pq3ehci_match, pq3ehci_attach, NULL, NULL);
72
73 static int
74 pq3ehci_match(device_t parent, cfdata_t cf, void *aux)
75 {
76
77 if (!e500_cpunode_submatch(parent, cf, cf->cf_name, aux))
78 return 0;
79
80 return 1;
81 }
82
83 static void
84 pq3ehci_attach(device_t parent, device_t self, void *aux)
85 {
86 struct cpunode_softc * const psc = device_private(parent);
87 struct pq3ehci_softc * const sc = device_private(self);
88 struct cpunode_attach_args * const cna = aux;
89 struct cpunode_locators * const cnl = &cna->cna_locs;
90 int error;
91
92 psc->sc_children |= cna->cna_childmask;
93 sc->sc.iot = cna->cna_le_memt; /* EHCI registers are little endian */
94 sc->sc.sc_dev = self;
95 sc->sc.sc_bus.dmatag = cna->cna_dmat;
96 sc->sc.sc_bus.hci_private = sc;
97 sc->sc.sc_bus.usbrev = USBREV_2_0;
98 sc->sc.sc_ncomp = 0;
99 sc->sc.sc_flags |= EHCIF_ETTF;
100
101 aprint_naive(": USB controller\n");
102 aprint_normal(": USB controller\n");
103
104 error = bus_space_map(sc->sc.iot, cnl->cnl_addr, cnl->cnl_size, 0,
105 &sc->sc.ioh);
106 if (error) {
107 aprint_error_dev(self,
108 "can't map registers for %s#%u: %d\n",
109 cnl->cnl_name, cnl->cnl_instance, error);
110 return;
111 }
112 sc->sc.sc_size = cnl->cnl_size;
113
114 /*
115 * We need to tell the USB interface to snoop all off RAM starting
116 * at 0. Since it can do it by powers of 2, get the highest RAM
117 * address and roughly round it to the next power of 2 and find
118 * the number of leading zero bits.
119 */
120 cpu_write_4(cnl->cnl_addr + USB_SNOOP1,
121 SNOOP_2GB - __builtin_clz(curcpu()->ci_softc->cpu_highmem * 2 - 1));
122
123 sc->sc_ih = intr_establish(cnl->cnl_intrs[0], IPL_USB, IST_ONCHIP,
124 ehci_intr, sc);
125 if (sc->sc_ih == NULL) {
126 aprint_error_dev(self, "failed to establish interrupt %d\n",
127 cnl->cnl_intrs[0]);
128 goto fail;
129 }
130 aprint_normal_dev(self, "interrupting on irq %d\n",
131 cnl->cnl_intrs[0]);
132
133 /* offs is needed for EOWRITEx */
134 sc->sc.sc_offs = EREAD1(&sc->sc, EHCI_CAPLENGTH);
135
136 /* Disable interrupts, so we don't get any spurious ones. */
137 DPRINTF(("%s: offs=%d\n", device_xname(self), sc->sc.sc_offs));
138 EOWRITE2(&sc->sc, EHCI_USBINTR, 0);
139
140 error = ehci_init(&sc->sc);
141 if (error != USBD_NORMAL_COMPLETION) {
142 aprint_error_dev(self, "init failed, error=%d\n", error);
143 goto fail;
144 }
145
146 /* Attach usb device. */
147 sc->sc.sc_child = config_found(self, &sc->sc.sc_bus, usbctlprint);
148 return;
149
150 fail:
151 if (sc->sc_ih) {
152 intr_disestablish(sc->sc_ih);
153 sc->sc_ih = NULL;
154 }
155 if (sc->sc.sc_size) {
156 bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size);
157 sc->sc.sc_size = 0;
158 }
159 return;
160 }
161