ralink_ehci.c revision 1.3 1 1.3 matt /* $NetBSD: ralink_ehci.c,v 1.3 2012/07/20 02:14:02 matt Exp $ */
2 1.2 matt /*-
3 1.2 matt * Copyright (c) 2011 CradlePoint Technology, Inc.
4 1.2 matt * All rights reserved.
5 1.2 matt *
6 1.2 matt *
7 1.2 matt * Redistribution and use in source and binary forms, with or without
8 1.2 matt * modification, are permitted provided that the following conditions
9 1.2 matt * are met:
10 1.2 matt * 1. Redistributions of source code must retain the above copyright
11 1.2 matt * notice, this list of conditions and the following disclaimer.
12 1.2 matt * 2. Redistributions in binary form must reproduce the above copyright
13 1.2 matt * notice, this list of conditions and the following disclaimer in the
14 1.2 matt * documentation and/or other materials provided with the distribution.
15 1.2 matt *
16 1.2 matt * THIS SOFTWARE IS PROVIDED BY CRADLEPOINT TECHNOLOGY, INC. AND CONTRIBUTORS
17 1.2 matt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.2 matt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.2 matt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
20 1.2 matt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.2 matt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.2 matt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.2 matt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.2 matt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.2 matt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.2 matt * POSSIBILITY OF SUCH DAMAGE.
27 1.2 matt */
28 1.2 matt
29 1.2 matt /* ralink_ehci.c -- Ralink EHCI USB Driver */
30 1.2 matt
31 1.2 matt #include <sys/cdefs.h>
32 1.3 matt __KERNEL_RCSID(0, "$NetBSD: ralink_ehci.c,v 1.3 2012/07/20 02:14:02 matt Exp $");
33 1.2 matt
34 1.2 matt #include <sys/param.h>
35 1.2 matt #include <sys/bus.h>
36 1.2 matt
37 1.2 matt #include <dev/usb/usb.h>
38 1.2 matt #include <dev/usb/usbdi.h>
39 1.2 matt #include <dev/usb/usbdivar.h>
40 1.2 matt #include <dev/usb/usb_mem.h>
41 1.2 matt
42 1.2 matt #include <dev/usb/ehcireg.h>
43 1.2 matt #include <dev/usb/ehcivar.h>
44 1.2 matt
45 1.2 matt #include <mips/ralink/ralink_usbhcvar.h>
46 1.2 matt
47 1.2 matt #include <mips/ralink/ralink_var.h>
48 1.2 matt #include <mips/ralink/ralink_reg.h>
49 1.2 matt
50 1.2 matt #define RT3XXX_EHCI_BASE 0x101c0000
51 1.2 matt #define RT3XXX_BLOCK_SIZE 0x1000
52 1.2 matt
53 1.2 matt struct ralink_ehci_softc {
54 1.2 matt struct ehci_softc sc_ehci;
55 1.2 matt void *sc_ih;
56 1.2 matt };
57 1.2 matt
58 1.2 matt static int ralink_ehci_match(device_t, cfdata_t, void *);
59 1.2 matt static void ralink_ehci_attach(device_t, device_t, void *);
60 1.2 matt static int ralink_ehci_detach(device_t, int);
61 1.2 matt
62 1.2 matt CFATTACH_DECL2_NEW(ralink_ehci, sizeof(struct ralink_ehci_softc),
63 1.2 matt ralink_ehci_match, ralink_ehci_attach, ralink_ehci_detach,
64 1.2 matt ehci_activate, NULL, ehci_childdet);
65 1.2 matt
66 1.2 matt static TAILQ_HEAD(, ralink_usb_hc) ralink_usb_alldevs =
67 1.2 matt TAILQ_HEAD_INITIALIZER(ralink_usb_alldevs);
68 1.2 matt
69 1.2 matt #if 0
70 1.2 matt struct usb_hc_alldevs ralink_usb_alldevs = TAILQ_HEAD_INITIALIZER(ralink_usb_alldevs);
71 1.2 matt #endif
72 1.2 matt
73 1.2 matt /*
74 1.2 matt * ralink_ehci_match
75 1.2 matt */
76 1.2 matt static int
77 1.2 matt ralink_ehci_match(device_t parent, cfdata_t cf, void *aux)
78 1.2 matt {
79 1.2 matt
80 1.2 matt return 1;
81 1.2 matt }
82 1.2 matt
83 1.2 matt /*
84 1.2 matt * ralink_ehci_attach
85 1.2 matt */
86 1.2 matt static void
87 1.2 matt ralink_ehci_attach(device_t parent, device_t self, void *aux)
88 1.2 matt {
89 1.2 matt struct ralink_ehci_softc * const sc = device_private(self);
90 1.2 matt const struct mainbus_attach_args *ma = aux;
91 1.2 matt struct ralink_usb_hc *ruh;
92 1.2 matt uint32_t r;
93 1.2 matt bus_space_handle_t sysctl_memh;
94 1.2 matt usbd_status status;
95 1.2 matt int error;
96 1.2 matt #ifdef RALINK_EHCI_DEBUG
97 1.2 matt const char *const devname = device_xname(self);
98 1.2 matt #endif
99 1.2 matt
100 1.2 matt aprint_naive(": EHCI USB controller\n");
101 1.2 matt aprint_normal(": EHCI USB controller\n");
102 1.2 matt
103 1.2 matt sc->sc_ehci.sc_dev = self;
104 1.2 matt sc->sc_ehci.sc_bus.hci_private = sc;
105 1.2 matt sc->sc_ehci.iot = ma->ma_memt;
106 1.2 matt sc->sc_ehci.sc_bus.dmatag = ma->ma_dmat;
107 1.2 matt
108 1.2 matt /* Map Sysctl registers */
109 1.2 matt if (((error = bus_space_map(ma->ma_memt, RA_SYSCTL_BASE, 0x10000,
110 1.2 matt 0, &sysctl_memh) != 0)) != 0) {
111 1.2 matt aprint_error_dev(self, "can't map Sysctl registers, "
112 1.2 matt "error=%d\n", error);
113 1.2 matt return;
114 1.2 matt }
115 1.2 matt
116 1.2 matt /* The USB block defaults PHY0 to device mode, change to host mode */
117 1.2 matt r = bus_space_read_4(ma->ma_memt, sysctl_memh, RA_SYSCTL_CFG1);
118 1.2 matt r |= (1 << 10);
119 1.2 matt bus_space_write_4(ma->ma_memt, sysctl_memh, RA_SYSCTL_CFG1, r);
120 1.2 matt
121 1.2 matt /* done with Sysctl regs */
122 1.2 matt bus_space_unmap(ma->ma_memt, sysctl_memh, 0x10000);
123 1.2 matt
124 1.2 matt /* Map EHCI registers */
125 1.2 matt if ((error = bus_space_map(sc->sc_ehci.iot, RT3XXX_EHCI_BASE,
126 1.2 matt RT3XXX_BLOCK_SIZE, 0, &sc->sc_ehci.ioh)) != 0) {
127 1.2 matt aprint_error_dev(self, "can't map EHCI registers, "
128 1.2 matt "error=%d\n", error);
129 1.2 matt return;
130 1.2 matt }
131 1.2 matt
132 1.2 matt sc->sc_ehci.sc_size = RT3XXX_BLOCK_SIZE;
133 1.2 matt sc->sc_ehci.sc_bus.usbrev = USBREV_2_0;
134 1.2 matt
135 1.2 matt #ifdef RALINK_EHCI_DEBUG
136 1.2 matt printf("%s sc: %p ma: %p\n", devname, sc, ma);
137 1.2 matt printf("%s memt: %p dmat: %p\n", devname, ma->ma_memt, ma->ma_dmat);
138 1.2 matt printf("%s: EHCI HCCAPBASE=0x%x\n", devname,
139 1.2 matt EREAD4(&sc->sc_ehci, EHCI_CAPLENGTH));
140 1.2 matt printf("%s: EHCI HCSPARAMS=0x%x\n", devname,
141 1.2 matt EREAD4(&sc->sc_ehci, EHCI_HCSPARAMS));
142 1.2 matt printf("%s: EHCI HCCPARAMS=0x%x\n", devname,
143 1.2 matt EREAD4(&sc->sc_ehci, EHCI_HCCPARAMS));
144 1.2 matt printf("%s: EHCI HCSP_PORTROUTE=0x%x\n", devname,
145 1.2 matt EREAD4(&sc->sc_ehci, EHCI_HCSP_PORTROUTE));
146 1.2 matt #endif
147 1.2 matt
148 1.2 matt /* Disable EHCI interrupts. */
149 1.2 matt sc->sc_ehci.sc_offs = EREAD1(&sc->sc_ehci, EHCI_CAPLENGTH);
150 1.3 matt EOWRITE4(&sc->sc_ehci, EHCI_USBINTR, 0);
151 1.2 matt
152 1.2 matt #ifdef RALINK_EHCI_DEBUG
153 1.2 matt printf("%s: EHCI USBCMD=0x%x\n", devname,
154 1.2 matt EOREAD4(&sc->sc_ehci, EHCI_USBCMD));
155 1.2 matt printf("%s: EHCI USBSTS=0x%x\n", devname,
156 1.2 matt EOREAD4(&sc->sc_ehci, EHCI_USBSTS));
157 1.2 matt printf("%s: EHCI USBINTR=0x%x\n", devname,
158 1.2 matt EOREAD4(&sc->sc_ehci, EHCI_USBINTR));
159 1.2 matt #endif
160 1.2 matt
161 1.2 matt /* Establish the MIPS level interrupt */
162 1.2 matt sc->sc_ih = ra_intr_establish(RA_IRQ_USB, ehci_intr, sc, 1);
163 1.2 matt if (sc->sc_ih == NULL) {
164 1.2 matt aprint_error_dev(self, "unable to establish irq %d\n",
165 1.2 matt RA_IRQ_USB);
166 1.2 matt goto fail_0;
167 1.2 matt }
168 1.2 matt
169 1.2 matt /*
170 1.2 matt * Find companion controllers. According to the spec they always
171 1.2 matt * have lower function numbers so they should be enumerated already.
172 1.2 matt */
173 1.2 matt int ncomp = 0;
174 1.2 matt TAILQ_FOREACH(ruh, &ralink_usb_alldevs, next) {
175 1.2 matt aprint_normal_dev(self, "companion %s\n",
176 1.2 matt device_xname(ruh->usb));
177 1.2 matt sc->sc_ehci.sc_comps[ncomp++] = ruh->usb;
178 1.2 matt if (ncomp >= EHCI_COMPANION_MAX)
179 1.2 matt break;
180 1.2 matt }
181 1.2 matt sc->sc_ehci.sc_ncomp = ncomp;
182 1.2 matt
183 1.2 matt /* set vendor for root hub descriptor. */
184 1.2 matt sc->sc_ehci.sc_id_vendor = 0x1814; /* XXX */
185 1.2 matt strlcpy(sc->sc_ehci.sc_vendor, "Ralink", sizeof(sc->sc_ehci.sc_vendor));
186 1.2 matt
187 1.2 matt /* Initialize EHCI */
188 1.2 matt status = ehci_init(&sc->sc_ehci);
189 1.2 matt if (status != USBD_NORMAL_COMPLETION) {
190 1.2 matt aprint_error_dev(self, "init failed, error=%d\n", status);
191 1.2 matt goto fail_1;
192 1.2 matt }
193 1.2 matt
194 1.2 matt if (!pmf_device_register1(self, ehci_suspend, ehci_resume,
195 1.2 matt ehci_shutdown))
196 1.2 matt aprint_error_dev(self, "couldn't establish power handler\n");
197 1.2 matt
198 1.2 matt /* Attach usb device. */
199 1.2 matt sc->sc_ehci.sc_child = config_found(self, &sc->sc_ehci.sc_bus,
200 1.2 matt usbctlprint);
201 1.2 matt
202 1.2 matt return;
203 1.2 matt
204 1.2 matt fail_1:
205 1.2 matt ra_intr_disestablish(sc->sc_ih);
206 1.2 matt sc->sc_ih = NULL;
207 1.2 matt fail_0:
208 1.2 matt bus_space_unmap(sc->sc_ehci.iot, sc->sc_ehci.ioh, sc->sc_ehci.sc_size);
209 1.2 matt sc->sc_ehci.sc_size = 0;
210 1.2 matt }
211 1.2 matt
212 1.2 matt static int
213 1.2 matt ralink_ehci_detach(device_t self, int flags)
214 1.2 matt {
215 1.2 matt struct ralink_ehci_softc * const sc = device_private(self);
216 1.2 matt int rv;
217 1.2 matt
218 1.2 matt pmf_device_deregister(self);
219 1.2 matt
220 1.2 matt rv = ehci_detach(&sc->sc_ehci, flags);
221 1.2 matt if (rv)
222 1.2 matt return rv;
223 1.2 matt
224 1.2 matt if (sc->sc_ih != NULL) {
225 1.2 matt ra_intr_disestablish(sc->sc_ih);
226 1.2 matt sc->sc_ih = NULL;
227 1.2 matt }
228 1.2 matt
229 1.2 matt if (sc->sc_ehci.sc_size != 0) {
230 1.2 matt bus_space_unmap(sc->sc_ehci.iot, sc->sc_ehci.ioh,
231 1.2 matt sc->sc_ehci.sc_size);
232 1.2 matt sc->sc_ehci.sc_size = 0;
233 1.2 matt }
234 1.2 matt
235 1.2 matt return 0;
236 1.2 matt }
237 1.2 matt
238 1.2 matt void
239 1.2 matt ralink_usb_hc_add(struct ralink_usb_hc *ruh, device_t usbp)
240 1.2 matt {
241 1.2 matt TAILQ_INSERT_TAIL(&ralink_usb_alldevs, ruh, next);
242 1.2 matt ruh->usb = usbp;
243 1.2 matt }
244 1.2 matt
245 1.2 matt void
246 1.2 matt ralink_usb_hc_rem(struct ralink_usb_hc *ruh)
247 1.2 matt {
248 1.2 matt TAILQ_REMOVE(&ralink_usb_alldevs, ruh, next);
249 1.2 matt }
250