1 /* $NetBSD: rmixl_ehci.c,v 1.8 2021/08/07 16:18:59 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 1999, 2000, 2002, 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Herb Peyerl of Middle Digital Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: rmixl_ehci.c,v 1.8 2021/08/07 16:18:59 thorpej Exp $");
34
35 #include "locators.h"
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/device.h>
40
41 #include <sys/bus.h>
42
43 #include <mips/rmi/rmixlreg.h>
44 #include <mips/rmi/rmixlvar.h>
45 #include <mips/rmi/rmixl_intr.h>
46 #include <mips/rmi/rmixl_usbivar.h>
47
48 #include <dev/usb/usb.h>
49 #include <dev/usb/usbdi.h>
50 #include <dev/usb/usbdivar.h>
51 #include <dev/usb/usb_mem.h>
52
53 #include <dev/usb/ehcireg.h>
54 #include <dev/usb/ehcivar.h>
55
56
57 static int rmixl_ehci_match(device_t, cfdata_t, void *);
58 static void rmixl_ehci_attach(device_t, device_t, void *);
59
60
61 CFATTACH_DECL_NEW(rmixl_ehci, sizeof (ehci_softc_t),
62 rmixl_ehci_match, rmixl_ehci_attach, NULL, NULL);
63
64 int
rmixl_ehci_match(device_t parent,cfdata_t match,void * aux)65 rmixl_ehci_match(device_t parent, cfdata_t match, void *aux)
66 {
67 struct rmixl_usbi_attach_args *usbi = aux;
68
69 if (usbi->usbi_addr == (RMIXL_IO_DEV_USB_A + RMIXL_USB_HOST_EHCI_BASE))
70 return rmixl_probe_4((volatile uint32_t *)
71 RMIXL_IOREG_VADDR(usbi->usbi_addr));
72
73 return 0;
74 }
75
76 void
rmixl_ehci_attach(device_t parent,device_t self,void * aux)77 rmixl_ehci_attach(device_t parent, device_t self, void *aux)
78 {
79 ehci_softc_t * const sc = device_private(self);
80 rmixl_usbi_softc_t * const psc = device_private(parent);
81 struct rmixl_usbi_attach_args * const usbi = aux;
82 void *ih = NULL;
83 uint32_t r;
84
85 /* check state of IO_AD9 signal latched in GPIO Reset Config reg */
86 r = RMIXL_IOREG_READ(RMIXL_IO_DEV_GPIO + RMIXL_GPIO_RESET_CFG);
87 if ((r & RMIXL_GPIO_RESET_CFG_USB_DEV) == 0) {
88 aprint_error_dev(self,
89 "IO_AD9 selects Device mode, abort Host attach\n");
90 return;
91 }
92
93 sc->sc_dev = self;
94 sc->sc_bus.ub_hcpriv = sc;
95 sc->iot = usbi->usbi_el_bst;
96 sc->sc_size = usbi->usbi_size;
97 sc->sc_bus.ub_dmatag = usbi->usbi_dmat;
98 sc->sc_bus.ub_revision = USBREV_1_0;
99
100 /*
101 * Grab the companion OHCI devices from our parent.
102 */
103 if (psc->sc_ohci_devs[1] != NULL) {
104 sc->sc_comps[0] = psc->sc_ohci_devs[0];
105 sc->sc_comps[1] = psc->sc_ohci_devs[1];
106 sc->sc_ncomp = 2;
107 } else {
108 sc->sc_comps[0] = psc->sc_ohci_devs[0];
109 sc->sc_ncomp = 1;
110 }
111
112 if (bus_space_map(sc->iot, usbi->usbi_addr, sc->sc_size, 0, &sc->ioh)) {
113 aprint_error_dev(self, "unable to map registers\n");
114 return;
115 }
116
117 /* get offset to operational regs */
118 sc->sc_offs = EREAD1(sc, EHCI_CAPLENGTH);
119
120 /* Disable EHCI interrupts */
121 EOWRITE4(sc, EHCI_USBINTR, 0);
122
123 /* establish interrupt */
124 if (usbi->usbi_intr != RMIXL_USBICF_INTR_DEFAULT) {
125 ih = rmixl_usbi_intr_establish(device_private(parent),
126 usbi->usbi_intr, ehci_intr, sc);
127 if (ih == NULL)
128 panic("%s: couldn't establish interrupt",
129 device_xname(self));
130 }
131
132 int err = ehci_init(sc);
133 if (err) {
134 aprint_error_dev(self, "init failed, error=%d\n", err);
135 if (ih != NULL)
136 rmixl_intr_disestablish(ih);
137 return;
138 }
139
140 /* Attach USB device */
141 sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint, CFARGS_NONE);
142 }
143
144