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