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