bcm53xx_usb.c revision 1.2.4.2 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.2 matt
30 1.2.4.2 matt #include "locators.h"
31 1.2.4.2 matt
32 1.2.4.2 matt #include <sys/cdefs.h>
33 1.2.4.2 matt
34 1.2.4.2 matt __KERNEL_RCSID(1, "$NetBSD: bcm53xx_usb.c,v 1.2.4.2 2012/11/28 22:40:24 matt Exp $");
35 1.2.4.2 matt
36 1.2.4.2 matt #include <sys/bus.h>
37 1.2.4.2 matt #include <sys/device.h>
38 1.2.4.2 matt #include <sys/intr.h>
39 1.2.4.2 matt #include <sys/systm.h>
40 1.2.4.2 matt
41 1.2.4.2 matt #include <arm/broadcom/bcm53xx_reg.h>
42 1.2.4.2 matt #include <arm/broadcom/bcm53xx_var.h>
43 1.2.4.2 matt
44 1.2.4.2 matt #include <dev/usb/usb.h>
45 1.2.4.2 matt #include <dev/usb/usbdi.h>
46 1.2.4.2 matt #include <dev/usb/usbdivar.h>
47 1.2.4.2 matt #include <dev/usb/usb_mem.h>
48 1.2.4.2 matt
49 1.2.4.2 matt #include <dev/usb/ohcireg.h>
50 1.2.4.2 matt #include <dev/usb/ohcivar.h>
51 1.2.4.2 matt
52 1.2.4.2 matt #include <dev/usb/ehcireg.h>
53 1.2.4.2 matt #include <dev/usb/ehcivar.h>
54 1.2.4.2 matt
55 1.2.4.2 matt #include <dev/pci/pcidevs.h>
56 1.2.4.2 matt
57 1.2.4.2 matt struct bcmusb_softc {
58 1.2.4.2 matt device_t usbsc_dev;
59 1.2.4.2 matt bus_dma_tag_t usbsc_dmat;
60 1.2.4.2 matt bus_space_tag_t usbsc_bst;
61 1.2.4.2 matt bus_space_handle_t usbsc_ehci_bsh;
62 1.2.4.2 matt bus_space_handle_t usbsc_ohci_bsh;
63 1.2.4.2 matt
64 1.2.4.2 matt device_t usbsc_ohci_dev;
65 1.2.4.2 matt device_t usbsc_ehci_dev;
66 1.2.4.2 matt void *usbsc_ohci_sc;
67 1.2.4.2 matt void *usbsc_ehci_sc;
68 1.2.4.2 matt void *usbsc_ih;
69 1.2.4.2 matt };
70 1.2.4.2 matt
71 1.2.4.2 matt struct bcmusb_attach_args {
72 1.2.4.2 matt const char *usbaa_name;
73 1.2.4.2 matt bus_dma_tag_t usbaa_dmat;
74 1.2.4.2 matt bus_space_tag_t usbaa_bst;
75 1.2.4.2 matt bus_space_handle_t usbaa_bsh;
76 1.2.4.2 matt bus_size_t usbaa_size;
77 1.2.4.2 matt };
78 1.2.4.2 matt
79 1.2.4.2 matt #ifdef OHCI_DEBUG
80 1.2.4.2 matt #define OHCI_DPRINTF(x) if (ohcidebug) printf x
81 1.2.4.2 matt extern int ohcidebug;
82 1.2.4.2 matt #else
83 1.2.4.2 matt #define OHCI_DPRINTF(x)
84 1.2.4.2 matt #endif
85 1.2.4.2 matt
86 1.2.4.2 matt static int ohci_bcmusb_match(device_t, cfdata_t, void *);
87 1.2.4.2 matt static void ohci_bcmusb_attach(device_t, device_t, void *);
88 1.2.4.2 matt
89 1.2.4.2 matt CFATTACH_DECL_NEW(ohci_bcmusb, sizeof(struct ohci_softc),
90 1.2.4.2 matt ohci_bcmusb_match, ohci_bcmusb_attach, NULL, NULL);
91 1.2.4.2 matt
92 1.2.4.2 matt static int
93 1.2.4.2 matt ohci_bcmusb_match(device_t parent, cfdata_t cf, void *aux)
94 1.2.4.2 matt {
95 1.2.4.2 matt struct bcmusb_attach_args * const usbaa = aux;
96 1.2.4.2 matt
97 1.2.4.2 matt if (strcmp(cf->cf_name, usbaa->usbaa_name))
98 1.2.4.2 matt return 0;
99 1.2.4.2 matt
100 1.2.4.2 matt return 1;
101 1.2.4.2 matt }
102 1.2.4.2 matt
103 1.2.4.2 matt static void
104 1.2.4.2 matt ohci_bcmusb_attach(device_t parent, device_t self, void *aux)
105 1.2.4.2 matt {
106 1.2.4.2 matt struct ohci_softc * const sc = device_private(self);
107 1.2.4.2 matt struct bcmusb_attach_args * const usbaa = aux;
108 1.2.4.2 matt
109 1.2.4.2 matt sc->sc_dev = self;
110 1.2.4.2 matt
111 1.2.4.2 matt sc->iot = usbaa->usbaa_bst;
112 1.2.4.2 matt sc->ioh = usbaa->usbaa_bsh;
113 1.2.4.2 matt sc->sc_size = usbaa->usbaa_size;
114 1.2.4.2 matt sc->sc_bus.dmatag = usbaa->usbaa_dmat;
115 1.2.4.2 matt sc->sc_bus.hci_private = sc;
116 1.2.4.2 matt
117 1.2.4.2 matt sc->sc_id_vendor = PCI_VENDOR_BROADCOM;
118 1.2.4.2 matt strlcpy(sc->sc_vendor, "Broadcom", sizeof(sc->sc_vendor));
119 1.2.4.2 matt
120 1.2.4.2 matt aprint_naive(": OHCI USB controller\n");
121 1.2.4.2 matt aprint_normal(": OHCI USB controller\n");
122 1.2.4.2 matt
123 1.2.4.2 matt int error = ohci_init(sc);
124 1.2.4.2 matt if (error != USBD_NORMAL_COMPLETION) {
125 1.2.4.2 matt aprint_error_dev(self, "init failed, error=%d\n", error);
126 1.2.4.2 matt } else {
127 1.2.4.2 matt /* Attach usb device. */
128 1.2.4.2 matt sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint);
129 1.2.4.2 matt }
130 1.2.4.2 matt }
131 1.2.4.2 matt
132 1.2.4.2 matt #ifdef EHCI_DEBUG
133 1.2.4.2 matt #define EHCI_DPRINTF(x) if (ehcidebug) printf x
134 1.2.4.2 matt extern int ehcidebug;
135 1.2.4.2 matt #else
136 1.2.4.2 matt #define EHCI_DPRINTF(x)
137 1.2.4.2 matt #endif
138 1.2.4.2 matt
139 1.2.4.2 matt static int ehci_bcmusb_match(device_t, cfdata_t, void *);
140 1.2.4.2 matt static void ehci_bcmusb_attach(device_t, device_t, void *);
141 1.2.4.2 matt
142 1.2.4.2 matt CFATTACH_DECL_NEW(ehci_bcmusb, sizeof(struct ehci_softc),
143 1.2.4.2 matt ehci_bcmusb_match, ehci_bcmusb_attach, NULL, NULL);
144 1.2.4.2 matt
145 1.2.4.2 matt static int
146 1.2.4.2 matt ehci_bcmusb_match(device_t parent, cfdata_t cf, void *aux)
147 1.2.4.2 matt {
148 1.2.4.2 matt struct bcmusb_attach_args * const usbaa = aux;
149 1.2.4.2 matt
150 1.2.4.2 matt if (strcmp(cf->cf_name, usbaa->usbaa_name))
151 1.2.4.2 matt return 0;
152 1.2.4.2 matt
153 1.2.4.2 matt return 1;
154 1.2.4.2 matt }
155 1.2.4.2 matt
156 1.2.4.2 matt static void
157 1.2.4.2 matt ehci_bcmusb_attach(device_t parent, device_t self, void *aux)
158 1.2.4.2 matt {
159 1.2.4.2 matt struct bcmusb_softc * const usbsc = device_private(parent);
160 1.2.4.2 matt struct ehci_softc * const sc = device_private(self);
161 1.2.4.2 matt struct bcmusb_attach_args * const usbaa = aux;
162 1.2.4.2 matt
163 1.2.4.2 matt sc->sc_dev = self;
164 1.2.4.2 matt
165 1.2.4.2 matt sc->iot = usbaa->usbaa_bst;
166 1.2.4.2 matt sc->ioh = usbaa->usbaa_bsh;
167 1.2.4.2 matt sc->sc_size = usbaa->usbaa_size;
168 1.2.4.2 matt sc->sc_bus.dmatag = usbaa->usbaa_dmat;
169 1.2.4.2 matt sc->sc_bus.hci_private = sc;
170 1.2.4.2 matt sc->sc_bus.usbrev = USBREV_2_0;
171 1.2.4.2 matt sc->sc_ncomp = 0;
172 1.2.4.2 matt if (usbsc->usbsc_ohci_dev != NULL) {
173 1.2.4.2 matt sc->sc_comps[sc->sc_ncomp++] = usbsc->usbsc_ohci_dev;
174 1.2.4.2 matt }
175 1.2.4.2 matt
176 1.2.4.2 matt sc->sc_id_vendor = PCI_VENDOR_BROADCOM;
177 1.2.4.2 matt strlcpy(sc->sc_vendor, "Broadcom", sizeof(sc->sc_vendor));
178 1.2.4.2 matt
179 1.2.4.2 matt aprint_naive(": EHCI USB controller\n");
180 1.2.4.2 matt aprint_normal(": ECHI USB controller\n");
181 1.2.4.2 matt
182 1.2.4.2 matt int error = ehci_init(sc);
183 1.2.4.2 matt if (error != USBD_NORMAL_COMPLETION) {
184 1.2.4.2 matt aprint_error_dev(self, "init failed, error=%d\n", error);
185 1.2.4.2 matt } else {
186 1.2.4.2 matt /* Attach usb device. */
187 1.2.4.2 matt sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint);
188 1.2.4.2 matt }
189 1.2.4.2 matt }
190 1.2.4.2 matt
191 1.2.4.2 matt /*
192 1.2.4.2 matt * There's only IRQ shared between both OCHI and EHCI devices.
193 1.2.4.2 matt */
194 1.2.4.2 matt static int
195 1.2.4.2 matt bcmusb_intr(void *arg)
196 1.2.4.2 matt {
197 1.2.4.2 matt struct bcmusb_softc * const usbsc = arg;
198 1.2.4.2 matt int rv0 = 0, rv1 = 0;
199 1.2.4.2 matt
200 1.2.4.2 matt if (usbsc->usbsc_ohci_sc)
201 1.2.4.2 matt rv0 = ohci_intr(usbsc->usbsc_ohci_sc);
202 1.2.4.2 matt
203 1.2.4.2 matt if (usbsc->usbsc_ehci_sc)
204 1.2.4.2 matt rv1 = ehci_intr(usbsc->usbsc_ehci_sc);
205 1.2.4.2 matt
206 1.2.4.2 matt return rv0 ? rv0 : rv1;
207 1.2.4.2 matt }
208 1.2.4.2 matt
209 1.2.4.2 matt static int bcmusb_ccb_match(device_t, cfdata_t, void *);
210 1.2.4.2 matt static void bcmusb_ccb_attach(device_t, device_t, void *);
211 1.2.4.2 matt
212 1.2.4.2 matt CFATTACH_DECL_NEW(bcmusb_ccb, sizeof(struct bcmusb_softc),
213 1.2.4.2 matt bcmusb_ccb_match, bcmusb_ccb_attach, NULL, NULL);
214 1.2.4.2 matt
215 1.2.4.2 matt int
216 1.2.4.2 matt bcmusb_ccb_match(device_t parent, cfdata_t cf, void *aux)
217 1.2.4.2 matt {
218 1.2.4.2 matt struct bcmccb_attach_args * const ccbaa = aux;
219 1.2.4.2 matt const struct bcm_locators * const loc = &ccbaa->ccbaa_loc;
220 1.2.4.2 matt
221 1.2.4.2 matt if (strcmp(cf->cf_name, loc->loc_name) != 0)
222 1.2.4.2 matt return 0;
223 1.2.4.2 matt
224 1.2.4.2 matt KASSERT(cf->cf_loc[BCMCCBCF_PORT] == BCMCCBCF_PORT_DEFAULT);
225 1.2.4.2 matt
226 1.2.4.2 matt return 1;
227 1.2.4.2 matt }
228 1.2.4.2 matt
229 1.2.4.2 matt void
230 1.2.4.2 matt bcmusb_ccb_attach(device_t parent, device_t self, void *aux)
231 1.2.4.2 matt {
232 1.2.4.2 matt struct bcmusb_softc * const usbsc = device_private(self);
233 1.2.4.2 matt const struct bcmccb_attach_args * const ccbaa = aux;
234 1.2.4.2 matt const struct bcm_locators * const loc = &ccbaa->ccbaa_loc;
235 1.2.4.2 matt
236 1.2.4.2 matt usbsc->usbsc_bst = ccbaa->ccbaa_ccb_bst;
237 1.2.4.2 matt usbsc->usbsc_dmat = ccbaa->ccbaa_dmat;
238 1.2.4.2 matt
239 1.2.4.2 matt bus_space_subregion(usbsc->usbsc_bst, ccbaa->ccbaa_ccb_bsh, EHCI_BASE,
240 1.2.4.2 matt 0x1000, &usbsc->usbsc_ehci_bsh);
241 1.2.4.2 matt bus_space_subregion(usbsc->usbsc_bst, ccbaa->ccbaa_ccb_bsh, OHCI_BASE,
242 1.2.4.2 matt 0x1000, &usbsc->usbsc_ohci_bsh);
243 1.2.4.2 matt
244 1.2.4.2 matt /*
245 1.2.4.2 matt * Disable interrupts
246 1.2.4.2 matt */
247 1.2.4.2 matt bus_space_write_4(usbsc->usbsc_bst, usbsc->usbsc_ohci_bsh,
248 1.2.4.2 matt OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS);
249 1.2.4.2 matt bus_size_t caplength = bus_space_read_1(usbsc->usbsc_bst,
250 1.2.4.2 matt usbsc->usbsc_ehci_bsh, EHCI_CAPLENGTH);
251 1.2.4.2 matt bus_space_write_4(usbsc->usbsc_bst, usbsc->usbsc_ehci_bsh,
252 1.2.4.2 matt caplength + EHCI_USBINTR, 0);
253 1.2.4.2 matt
254 1.2.4.2 matt aprint_naive("\n");
255 1.2.4.2 matt aprint_normal("\n");
256 1.2.4.2 matt
257 1.2.4.2 matt struct bcmusb_attach_args usbaa_ohci = {
258 1.2.4.2 matt .usbaa_name = "ohci",
259 1.2.4.2 matt .usbaa_dmat = usbsc->usbsc_dmat,
260 1.2.4.2 matt .usbaa_bst = usbsc->usbsc_bst,
261 1.2.4.2 matt .usbaa_bsh = usbsc->usbsc_ohci_bsh,
262 1.2.4.2 matt .usbaa_size = 0x100,
263 1.2.4.2 matt };
264 1.2.4.2 matt
265 1.2.4.2 matt usbsc->usbsc_ohci_dev = config_found(self, &usbaa_ohci, NULL);
266 1.2.4.2 matt if (usbsc->usbsc_ohci_dev != NULL)
267 1.2.4.2 matt usbsc->usbsc_ohci_sc = device_private(usbsc->usbsc_ohci_dev);
268 1.2.4.2 matt
269 1.2.4.2 matt struct bcmusb_attach_args usbaa_ehci = {
270 1.2.4.2 matt .usbaa_name = "ehci",
271 1.2.4.2 matt .usbaa_dmat = usbsc->usbsc_dmat,
272 1.2.4.2 matt .usbaa_bst = usbsc->usbsc_bst,
273 1.2.4.2 matt .usbaa_bsh = usbsc->usbsc_ehci_bsh,
274 1.2.4.2 matt .usbaa_size = 0x100,
275 1.2.4.2 matt };
276 1.2.4.2 matt
277 1.2.4.2 matt usbsc->usbsc_ehci_dev = config_found(self, &usbaa_ehci, NULL);
278 1.2.4.2 matt if (usbsc->usbsc_ehci_dev != NULL)
279 1.2.4.2 matt usbsc->usbsc_ehci_sc = device_private(usbsc->usbsc_ehci_dev);
280 1.2.4.2 matt
281 1.2.4.2 matt usbsc->usbsc_ih = intr_establish(loc->loc_intrs[0], IPL_USB, IST_LEVEL,
282 1.2.4.2 matt bcmusb_intr, usbsc);
283 1.2.4.2 matt if (usbsc->usbsc_ih == NULL) {
284 1.2.4.2 matt aprint_error_dev(self, "failed to establish interrupt %d\n",
285 1.2.4.2 matt loc->loc_intrs[0]);
286 1.2.4.2 matt return;
287 1.2.4.2 matt }
288 1.2.4.2 matt aprint_normal_dev(self, "interrupting on irq %d\n", loc->loc_intrs[0]);
289 1.2.4.2 matt }
290