bcm53xx_usb.c revision 1.2.4.3 1 1.2.4.2 matt /*-
2 1.2.4.2 matt * Copyright (c) 2012 The NetBSD Foundation, Inc.
3 1.2.4.2 matt * All rights reserved.
4 1.2.4.2 matt *
5 1.2.4.2 matt * This code is derived from software contributed to The NetBSD Foundation
6 1.2.4.2 matt * by Matt Thomas of 3am Software Foundry.
7 1.2.4.2 matt *
8 1.2.4.2 matt * Redistribution and use in source and binary forms, with or without
9 1.2.4.2 matt * modification, are permitted provided that the following conditions
10 1.2.4.2 matt * are met:
11 1.2.4.2 matt * 1. Redistributions of source code must retain the above copyright
12 1.2.4.2 matt * notice, this list of conditions and the following disclaimer.
13 1.2.4.2 matt * 2. Redistributions in binary form must reproduce the above copyright
14 1.2.4.2 matt * notice, this list of conditions and the following disclaimer in the
15 1.2.4.2 matt * documentation and/or other materials provided with the distribution.
16 1.2.4.2 matt *
17 1.2.4.2 matt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 1.2.4.2 matt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 1.2.4.2 matt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 1.2.4.2 matt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 1.2.4.2 matt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 1.2.4.2 matt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 1.2.4.2 matt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 1.2.4.2 matt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 1.2.4.2 matt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 1.2.4.2 matt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 1.2.4.2 matt * POSSIBILITY OF SUCH DAMAGE.
28 1.2.4.2 matt */
29 1.2.4.3 matt #define USBH_PRIVATE
30 1.2.4.2 matt
31 1.2.4.2 matt #include "locators.h"
32 1.2.4.2 matt
33 1.2.4.2 matt #include <sys/cdefs.h>
34 1.2.4.2 matt
35 1.2.4.3 matt __KERNEL_RCSID(1, "$NetBSD: bcm53xx_usb.c,v 1.2.4.3 2012/11/29 18:42:05 matt Exp $");
36 1.2.4.2 matt
37 1.2.4.2 matt #include <sys/bus.h>
38 1.2.4.2 matt #include <sys/device.h>
39 1.2.4.2 matt #include <sys/intr.h>
40 1.2.4.2 matt #include <sys/systm.h>
41 1.2.4.2 matt
42 1.2.4.2 matt #include <arm/broadcom/bcm53xx_reg.h>
43 1.2.4.2 matt #include <arm/broadcom/bcm53xx_var.h>
44 1.2.4.2 matt
45 1.2.4.2 matt #include <dev/usb/usb.h>
46 1.2.4.2 matt #include <dev/usb/usbdi.h>
47 1.2.4.2 matt #include <dev/usb/usbdivar.h>
48 1.2.4.2 matt #include <dev/usb/usb_mem.h>
49 1.2.4.2 matt
50 1.2.4.2 matt #include <dev/usb/ohcireg.h>
51 1.2.4.2 matt #include <dev/usb/ohcivar.h>
52 1.2.4.2 matt
53 1.2.4.2 matt #include <dev/usb/ehcireg.h>
54 1.2.4.2 matt #include <dev/usb/ehcivar.h>
55 1.2.4.2 matt
56 1.2.4.2 matt #include <dev/pci/pcidevs.h>
57 1.2.4.2 matt
58 1.2.4.2 matt struct bcmusb_softc {
59 1.2.4.2 matt device_t usbsc_dev;
60 1.2.4.2 matt bus_dma_tag_t usbsc_dmat;
61 1.2.4.2 matt bus_space_tag_t usbsc_bst;
62 1.2.4.2 matt bus_space_handle_t usbsc_ehci_bsh;
63 1.2.4.2 matt bus_space_handle_t usbsc_ohci_bsh;
64 1.2.4.2 matt
65 1.2.4.2 matt device_t usbsc_ohci_dev;
66 1.2.4.2 matt device_t usbsc_ehci_dev;
67 1.2.4.2 matt void *usbsc_ohci_sc;
68 1.2.4.2 matt void *usbsc_ehci_sc;
69 1.2.4.2 matt void *usbsc_ih;
70 1.2.4.2 matt };
71 1.2.4.2 matt
72 1.2.4.2 matt struct bcmusb_attach_args {
73 1.2.4.2 matt const char *usbaa_name;
74 1.2.4.2 matt bus_dma_tag_t usbaa_dmat;
75 1.2.4.2 matt bus_space_tag_t usbaa_bst;
76 1.2.4.2 matt bus_space_handle_t usbaa_bsh;
77 1.2.4.2 matt bus_size_t usbaa_size;
78 1.2.4.2 matt };
79 1.2.4.2 matt
80 1.2.4.2 matt #ifdef OHCI_DEBUG
81 1.2.4.2 matt #define OHCI_DPRINTF(x) if (ohcidebug) printf x
82 1.2.4.2 matt extern int ohcidebug;
83 1.2.4.2 matt #else
84 1.2.4.2 matt #define OHCI_DPRINTF(x)
85 1.2.4.2 matt #endif
86 1.2.4.2 matt
87 1.2.4.2 matt static int ohci_bcmusb_match(device_t, cfdata_t, void *);
88 1.2.4.2 matt static void ohci_bcmusb_attach(device_t, device_t, void *);
89 1.2.4.2 matt
90 1.2.4.2 matt CFATTACH_DECL_NEW(ohci_bcmusb, sizeof(struct ohci_softc),
91 1.2.4.2 matt ohci_bcmusb_match, ohci_bcmusb_attach, NULL, NULL);
92 1.2.4.2 matt
93 1.2.4.2 matt static int
94 1.2.4.2 matt ohci_bcmusb_match(device_t parent, cfdata_t cf, void *aux)
95 1.2.4.2 matt {
96 1.2.4.2 matt struct bcmusb_attach_args * const usbaa = aux;
97 1.2.4.2 matt
98 1.2.4.2 matt if (strcmp(cf->cf_name, usbaa->usbaa_name))
99 1.2.4.2 matt return 0;
100 1.2.4.2 matt
101 1.2.4.2 matt return 1;
102 1.2.4.2 matt }
103 1.2.4.2 matt
104 1.2.4.2 matt static void
105 1.2.4.2 matt ohci_bcmusb_attach(device_t parent, device_t self, void *aux)
106 1.2.4.2 matt {
107 1.2.4.2 matt struct ohci_softc * const sc = device_private(self);
108 1.2.4.2 matt struct bcmusb_attach_args * const usbaa = aux;
109 1.2.4.2 matt
110 1.2.4.2 matt sc->sc_dev = self;
111 1.2.4.2 matt
112 1.2.4.2 matt sc->iot = usbaa->usbaa_bst;
113 1.2.4.2 matt sc->ioh = usbaa->usbaa_bsh;
114 1.2.4.2 matt sc->sc_size = usbaa->usbaa_size;
115 1.2.4.2 matt sc->sc_bus.dmatag = usbaa->usbaa_dmat;
116 1.2.4.2 matt sc->sc_bus.hci_private = sc;
117 1.2.4.2 matt
118 1.2.4.2 matt sc->sc_id_vendor = PCI_VENDOR_BROADCOM;
119 1.2.4.2 matt strlcpy(sc->sc_vendor, "Broadcom", sizeof(sc->sc_vendor));
120 1.2.4.2 matt
121 1.2.4.2 matt aprint_naive(": OHCI USB controller\n");
122 1.2.4.2 matt aprint_normal(": OHCI USB controller\n");
123 1.2.4.2 matt
124 1.2.4.2 matt int error = ohci_init(sc);
125 1.2.4.2 matt if (error != USBD_NORMAL_COMPLETION) {
126 1.2.4.2 matt aprint_error_dev(self, "init failed, error=%d\n", error);
127 1.2.4.2 matt } else {
128 1.2.4.2 matt /* Attach usb device. */
129 1.2.4.2 matt sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint);
130 1.2.4.2 matt }
131 1.2.4.2 matt }
132 1.2.4.2 matt
133 1.2.4.2 matt #ifdef EHCI_DEBUG
134 1.2.4.2 matt #define EHCI_DPRINTF(x) if (ehcidebug) printf x
135 1.2.4.2 matt extern int ehcidebug;
136 1.2.4.2 matt #else
137 1.2.4.2 matt #define EHCI_DPRINTF(x)
138 1.2.4.2 matt #endif
139 1.2.4.2 matt
140 1.2.4.2 matt static int ehci_bcmusb_match(device_t, cfdata_t, void *);
141 1.2.4.2 matt static void ehci_bcmusb_attach(device_t, device_t, void *);
142 1.2.4.2 matt
143 1.2.4.2 matt CFATTACH_DECL_NEW(ehci_bcmusb, sizeof(struct ehci_softc),
144 1.2.4.2 matt ehci_bcmusb_match, ehci_bcmusb_attach, NULL, NULL);
145 1.2.4.2 matt
146 1.2.4.2 matt static int
147 1.2.4.2 matt ehci_bcmusb_match(device_t parent, cfdata_t cf, void *aux)
148 1.2.4.2 matt {
149 1.2.4.2 matt struct bcmusb_attach_args * const usbaa = aux;
150 1.2.4.2 matt
151 1.2.4.2 matt if (strcmp(cf->cf_name, usbaa->usbaa_name))
152 1.2.4.2 matt return 0;
153 1.2.4.2 matt
154 1.2.4.2 matt return 1;
155 1.2.4.2 matt }
156 1.2.4.2 matt
157 1.2.4.2 matt static void
158 1.2.4.2 matt ehci_bcmusb_attach(device_t parent, device_t self, void *aux)
159 1.2.4.2 matt {
160 1.2.4.2 matt struct bcmusb_softc * const usbsc = device_private(parent);
161 1.2.4.2 matt struct ehci_softc * const sc = device_private(self);
162 1.2.4.2 matt struct bcmusb_attach_args * const usbaa = aux;
163 1.2.4.2 matt
164 1.2.4.2 matt sc->sc_dev = self;
165 1.2.4.2 matt
166 1.2.4.2 matt sc->iot = usbaa->usbaa_bst;
167 1.2.4.2 matt sc->ioh = usbaa->usbaa_bsh;
168 1.2.4.2 matt sc->sc_size = usbaa->usbaa_size;
169 1.2.4.2 matt sc->sc_bus.dmatag = usbaa->usbaa_dmat;
170 1.2.4.2 matt sc->sc_bus.hci_private = sc;
171 1.2.4.2 matt sc->sc_bus.usbrev = USBREV_2_0;
172 1.2.4.2 matt sc->sc_ncomp = 0;
173 1.2.4.2 matt if (usbsc->usbsc_ohci_dev != NULL) {
174 1.2.4.2 matt sc->sc_comps[sc->sc_ncomp++] = usbsc->usbsc_ohci_dev;
175 1.2.4.2 matt }
176 1.2.4.2 matt
177 1.2.4.2 matt sc->sc_id_vendor = PCI_VENDOR_BROADCOM;
178 1.2.4.2 matt strlcpy(sc->sc_vendor, "Broadcom", sizeof(sc->sc_vendor));
179 1.2.4.2 matt
180 1.2.4.2 matt aprint_naive(": EHCI USB controller\n");
181 1.2.4.2 matt aprint_normal(": ECHI USB controller\n");
182 1.2.4.2 matt
183 1.2.4.2 matt int error = ehci_init(sc);
184 1.2.4.2 matt if (error != USBD_NORMAL_COMPLETION) {
185 1.2.4.2 matt aprint_error_dev(self, "init failed, error=%d\n", error);
186 1.2.4.2 matt } else {
187 1.2.4.2 matt /* Attach usb device. */
188 1.2.4.2 matt sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint);
189 1.2.4.2 matt }
190 1.2.4.2 matt }
191 1.2.4.2 matt
192 1.2.4.2 matt /*
193 1.2.4.2 matt * There's only IRQ shared between both OCHI and EHCI devices.
194 1.2.4.2 matt */
195 1.2.4.2 matt static int
196 1.2.4.2 matt bcmusb_intr(void *arg)
197 1.2.4.2 matt {
198 1.2.4.2 matt struct bcmusb_softc * const usbsc = arg;
199 1.2.4.2 matt int rv0 = 0, rv1 = 0;
200 1.2.4.2 matt
201 1.2.4.2 matt if (usbsc->usbsc_ohci_sc)
202 1.2.4.2 matt rv0 = ohci_intr(usbsc->usbsc_ohci_sc);
203 1.2.4.2 matt
204 1.2.4.2 matt if (usbsc->usbsc_ehci_sc)
205 1.2.4.2 matt rv1 = ehci_intr(usbsc->usbsc_ehci_sc);
206 1.2.4.2 matt
207 1.2.4.2 matt return rv0 ? rv0 : rv1;
208 1.2.4.2 matt }
209 1.2.4.2 matt
210 1.2.4.2 matt static int bcmusb_ccb_match(device_t, cfdata_t, void *);
211 1.2.4.2 matt static void bcmusb_ccb_attach(device_t, device_t, void *);
212 1.2.4.2 matt
213 1.2.4.2 matt CFATTACH_DECL_NEW(bcmusb_ccb, sizeof(struct bcmusb_softc),
214 1.2.4.2 matt bcmusb_ccb_match, bcmusb_ccb_attach, NULL, NULL);
215 1.2.4.2 matt
216 1.2.4.2 matt int
217 1.2.4.2 matt bcmusb_ccb_match(device_t parent, cfdata_t cf, void *aux)
218 1.2.4.2 matt {
219 1.2.4.2 matt struct bcmccb_attach_args * const ccbaa = aux;
220 1.2.4.2 matt const struct bcm_locators * const loc = &ccbaa->ccbaa_loc;
221 1.2.4.2 matt
222 1.2.4.2 matt if (strcmp(cf->cf_name, loc->loc_name) != 0)
223 1.2.4.2 matt return 0;
224 1.2.4.2 matt
225 1.2.4.2 matt KASSERT(cf->cf_loc[BCMCCBCF_PORT] == BCMCCBCF_PORT_DEFAULT);
226 1.2.4.2 matt
227 1.2.4.2 matt return 1;
228 1.2.4.2 matt }
229 1.2.4.2 matt
230 1.2.4.2 matt void
231 1.2.4.2 matt bcmusb_ccb_attach(device_t parent, device_t self, void *aux)
232 1.2.4.2 matt {
233 1.2.4.2 matt struct bcmusb_softc * const usbsc = device_private(self);
234 1.2.4.2 matt const struct bcmccb_attach_args * const ccbaa = aux;
235 1.2.4.2 matt const struct bcm_locators * const loc = &ccbaa->ccbaa_loc;
236 1.2.4.2 matt
237 1.2.4.2 matt usbsc->usbsc_bst = ccbaa->ccbaa_ccb_bst;
238 1.2.4.2 matt usbsc->usbsc_dmat = ccbaa->ccbaa_dmat;
239 1.2.4.2 matt
240 1.2.4.2 matt bus_space_subregion(usbsc->usbsc_bst, ccbaa->ccbaa_ccb_bsh, EHCI_BASE,
241 1.2.4.2 matt 0x1000, &usbsc->usbsc_ehci_bsh);
242 1.2.4.2 matt bus_space_subregion(usbsc->usbsc_bst, ccbaa->ccbaa_ccb_bsh, OHCI_BASE,
243 1.2.4.2 matt 0x1000, &usbsc->usbsc_ohci_bsh);
244 1.2.4.2 matt
245 1.2.4.2 matt /*
246 1.2.4.3 matt * Bring the PHYs out of reset.
247 1.2.4.3 matt */
248 1.2.4.3 matt bus_space_write_4(usbsc->usbsc_bst, usbsc->usbsc_ehci_bsh,
249 1.2.4.3 matt USBH_PHY_CTRL_P0, USBH_PHY_CTRL_INIT);
250 1.2.4.3 matt bus_space_write_4(usbsc->usbsc_bst, usbsc->usbsc_ehci_bsh,
251 1.2.4.3 matt USBH_PHY_CTRL_P1, USBH_PHY_CTRL_INIT);
252 1.2.4.3 matt
253 1.2.4.3 matt /*
254 1.2.4.2 matt * Disable interrupts
255 1.2.4.2 matt */
256 1.2.4.2 matt bus_space_write_4(usbsc->usbsc_bst, usbsc->usbsc_ohci_bsh,
257 1.2.4.2 matt OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS);
258 1.2.4.2 matt bus_size_t caplength = bus_space_read_1(usbsc->usbsc_bst,
259 1.2.4.2 matt usbsc->usbsc_ehci_bsh, EHCI_CAPLENGTH);
260 1.2.4.2 matt bus_space_write_4(usbsc->usbsc_bst, usbsc->usbsc_ehci_bsh,
261 1.2.4.2 matt caplength + EHCI_USBINTR, 0);
262 1.2.4.2 matt
263 1.2.4.2 matt aprint_naive("\n");
264 1.2.4.2 matt aprint_normal("\n");
265 1.2.4.2 matt
266 1.2.4.2 matt struct bcmusb_attach_args usbaa_ohci = {
267 1.2.4.2 matt .usbaa_name = "ohci",
268 1.2.4.2 matt .usbaa_dmat = usbsc->usbsc_dmat,
269 1.2.4.2 matt .usbaa_bst = usbsc->usbsc_bst,
270 1.2.4.2 matt .usbaa_bsh = usbsc->usbsc_ohci_bsh,
271 1.2.4.2 matt .usbaa_size = 0x100,
272 1.2.4.2 matt };
273 1.2.4.2 matt
274 1.2.4.2 matt usbsc->usbsc_ohci_dev = config_found(self, &usbaa_ohci, NULL);
275 1.2.4.2 matt if (usbsc->usbsc_ohci_dev != NULL)
276 1.2.4.2 matt usbsc->usbsc_ohci_sc = device_private(usbsc->usbsc_ohci_dev);
277 1.2.4.2 matt
278 1.2.4.2 matt struct bcmusb_attach_args usbaa_ehci = {
279 1.2.4.2 matt .usbaa_name = "ehci",
280 1.2.4.2 matt .usbaa_dmat = usbsc->usbsc_dmat,
281 1.2.4.2 matt .usbaa_bst = usbsc->usbsc_bst,
282 1.2.4.2 matt .usbaa_bsh = usbsc->usbsc_ehci_bsh,
283 1.2.4.2 matt .usbaa_size = 0x100,
284 1.2.4.2 matt };
285 1.2.4.2 matt
286 1.2.4.2 matt usbsc->usbsc_ehci_dev = config_found(self, &usbaa_ehci, NULL);
287 1.2.4.2 matt if (usbsc->usbsc_ehci_dev != NULL)
288 1.2.4.2 matt usbsc->usbsc_ehci_sc = device_private(usbsc->usbsc_ehci_dev);
289 1.2.4.2 matt
290 1.2.4.2 matt usbsc->usbsc_ih = intr_establish(loc->loc_intrs[0], IPL_USB, IST_LEVEL,
291 1.2.4.2 matt bcmusb_intr, usbsc);
292 1.2.4.2 matt if (usbsc->usbsc_ih == NULL) {
293 1.2.4.2 matt aprint_error_dev(self, "failed to establish interrupt %d\n",
294 1.2.4.2 matt loc->loc_intrs[0]);
295 1.2.4.2 matt return;
296 1.2.4.2 matt }
297 1.2.4.2 matt aprint_normal_dev(self, "interrupting on irq %d\n", loc->loc_intrs[0]);
298 1.2.4.2 matt }
299