1 /*        $NetBSD: rmixl_ohci.c,v 1.8 2021/11/10 17:19:29 msaitoh 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 "locators.h"
33 
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: rmixl_ohci.c,v 1.8 2021/11/10 17:19:29 msaitoh Exp $");
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/ohcireg.h>
54 #include <dev/usb/ohcivar.h>
55 
56 
57 static int          rmixl_ohci_match(device_t, cfdata_t, void *);
58 static void         rmixl_ohci_attach(device_t, device_t, void *);
59 
60 
61 CFATTACH_DECL_NEW(rmixl_ohci, sizeof (ohci_softc_t),
62     rmixl_ohci_match, rmixl_ohci_attach, NULL, NULL);
63 
64 int
rmixl_ohci_match(device_t parent,cfdata_t match,void * aux)65 rmixl_ohci_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_0HCI0_BASE))
70         ||  (usbi->usbi_addr == (RMIXL_IO_DEV_USB_A+RMIXL_USB_HOST_0HCI1_BASE)))
71                 return rmixl_probe_4((volatile uint32_t *)
72                               RMIXL_IOREG_VADDR(usbi->usbi_addr));
73 
74         return 0;
75 }
76 
77 void
rmixl_ohci_attach(device_t parent,device_t self,void * aux)78 rmixl_ohci_attach(device_t parent, device_t self, void *aux)
79 {
80           ohci_softc_t * const sc = device_private(self);
81           rmixl_usbi_softc_t * const psc = device_private(parent);
82           struct rmixl_usbi_attach_args * const usbi = aux;
83           void *ih = NULL;
84           uint32_t r;
85 
86           /* check state of IO_AD9 signal latched in GPIO Reset Config reg */
87           r = RMIXL_IOREG_READ(RMIXL_IO_DEV_GPIO + RMIXL_GPIO_RESET_CFG);
88           if ((r & RMIXL_GPIO_RESET_CFG_USB_DEV) == 0) {
89                     aprint_error_dev(self,
90                               "IO_AD9 selects Device mode, abort Host attach\n");
91                     return;
92           }
93 
94           sc->sc_dev = self;
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_hcpriv = sc;
99 
100           if (bus_space_map(sc->iot, usbi->usbi_addr, sc->sc_size, 0, &sc->ioh)) {
101                     aprint_error_dev(self, "unable to map registers\n");
102                     return;
103           }
104 
105           /* Disable OHCI interrupts */
106           bus_space_write_4(sc->iot, sc->ioh, OHCI_INTERRUPT_DISABLE,
107                                         OHCI_ALL_INTRS);
108 
109           /* establish interrupt */
110           if (usbi->usbi_intr != RMIXL_USBICF_INTR_DEFAULT) {
111                     ih = rmixl_usbi_intr_establish(device_private(parent),
112                               usbi->usbi_intr, ohci_intr, sc);
113                     if (ih == NULL)
114                               panic("%s: couldn't establish interrupt",
115                                         device_xname(self));
116           }
117 
118           /* we handle endianness in bus space */
119           sc->sc_endian = OHCI_HOST_ENDIAN;
120 
121           int err = ohci_init(sc);
122           if (err) {
123                     aprint_error_dev(self, "init failed, error=%d\n", err);
124                     if (ih != NULL)
125                               rmixl_intr_disestablish(ih);
126                     return;
127           }
128 
129           if (psc->sc_ohci_devs[0] == NULL) {
130                     psc->sc_ohci_devs[0] = self;
131           } else if (psc->sc_ohci_devs[1] == NULL) {
132                     psc->sc_ohci_devs[1] = self;
133           } else {
134                     panic("%s: too many ohci devices", __func__);
135           }
136 
137           /* Attach USB device */
138           sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint, CFARGS_NONE);
139 }
140