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