xref: /netbsd-src/sys/arch/mips/alchemy/dev/ohci_aubus.c (revision c7fb772b85b2b5d4cfb282f868f454b4701534fd)
1*c7fb772bSthorpej /*	$NetBSD: ohci_aubus.c,v 1.18 2021/08/07 16:18:58 thorpej Exp $	*/
2badb1c52Shpeyerl 
3badb1c52Shpeyerl /*-
4badb1c52Shpeyerl  * Copyright (c) 1998, 1999, 2000, 2002, 2003 The NetBSD Foundation, Inc.
5badb1c52Shpeyerl  * All rights reserved.
6badb1c52Shpeyerl  *
7badb1c52Shpeyerl  * This code is derived from software contributed to The NetBSD Foundation
8badb1c52Shpeyerl  * by Herb Peyerl of Middle Digital Inc.
9badb1c52Shpeyerl  *
10badb1c52Shpeyerl  * Redistribution and use in source and binary forms, with or without
11badb1c52Shpeyerl  * modification, are permitted provided that the following conditions
12badb1c52Shpeyerl  * are met:
13badb1c52Shpeyerl  * 1. Redistributions of source code must retain the above copyright
14badb1c52Shpeyerl  *    notice, this list of conditions and the following disclaimer.
15badb1c52Shpeyerl  * 2. Redistributions in binary form must reproduce the above copyright
16badb1c52Shpeyerl  *    notice, this list of conditions and the following disclaimer in the
17badb1c52Shpeyerl  *    documentation and/or other materials provided with the distribution.
18badb1c52Shpeyerl  *
19badb1c52Shpeyerl  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20badb1c52Shpeyerl  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21badb1c52Shpeyerl  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22badb1c52Shpeyerl  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23badb1c52Shpeyerl  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24badb1c52Shpeyerl  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25badb1c52Shpeyerl  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26badb1c52Shpeyerl  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27badb1c52Shpeyerl  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28badb1c52Shpeyerl  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29badb1c52Shpeyerl  * POSSIBILITY OF SUCH DAMAGE.
30badb1c52Shpeyerl  */
31badb1c52Shpeyerl 
324b2744bfSlukem #include <sys/cdefs.h>
33*c7fb772bSthorpej __KERNEL_RCSID(0, "$NetBSD: ohci_aubus.c,v 1.18 2021/08/07 16:18:58 thorpej Exp $");
344b2744bfSlukem 
35badb1c52Shpeyerl #include <sys/param.h>
36badb1c52Shpeyerl #include <sys/systm.h>
37badb1c52Shpeyerl #include <sys/device.h>
38badb1c52Shpeyerl 
39e265f67bSdyoung #include <sys/bus.h>
40badb1c52Shpeyerl 
41badb1c52Shpeyerl #include <mips/alchemy/include/aureg.h>
42badb1c52Shpeyerl #include <mips/alchemy/include/auvar.h>
43badb1c52Shpeyerl #include <mips/alchemy/include/aubusvar.h>
440129c249Sgdamore #include <mips/alchemy/dev/ohcireg.h>
45badb1c52Shpeyerl 
46badb1c52Shpeyerl #include <dev/usb/usb.h>
47badb1c52Shpeyerl #include <dev/usb/usbdi.h>
48badb1c52Shpeyerl #include <dev/usb/usbdivar.h>
49badb1c52Shpeyerl #include <dev/usb/usb_mem.h>
50badb1c52Shpeyerl 
51badb1c52Shpeyerl #include <dev/usb/ohcireg.h>
52badb1c52Shpeyerl #include <dev/usb/ohcivar.h>
53badb1c52Shpeyerl 
54badb1c52Shpeyerl 
5550ea53d9Sdogcow static int	ohci_aubus_match(device_t, cfdata_t, void *);
5650ea53d9Sdogcow static void	ohci_aubus_attach(device_t, device_t, void *);
57badb1c52Shpeyerl 
5850ea53d9Sdogcow CFATTACH_DECL_NEW(ohci_aubus, sizeof (ohci_softc_t),
59badb1c52Shpeyerl     ohci_aubus_match, ohci_aubus_attach, NULL, NULL);
60badb1c52Shpeyerl 
61badb1c52Shpeyerl int
ohci_aubus_match(device_t parent,cfdata_t match,void * aux)6250ea53d9Sdogcow ohci_aubus_match(device_t parent, cfdata_t match, void *aux)
63badb1c52Shpeyerl {
64badb1c52Shpeyerl 	struct aubus_attach_args *aa = aux;
65badb1c52Shpeyerl 
66badb1c52Shpeyerl 	if (strcmp(aa->aa_name, match->cf_name) == 0)
674e8e6643Sskrll 		return 1;
68badb1c52Shpeyerl 
694e8e6643Sskrll 	return 0;
70badb1c52Shpeyerl }
71badb1c52Shpeyerl 
72badb1c52Shpeyerl void
ohci_aubus_attach(device_t parent,device_t self,void * aux)7350ea53d9Sdogcow ohci_aubus_attach(device_t parent, device_t self, void *aux)
74badb1c52Shpeyerl {
7550ea53d9Sdogcow 	ohci_softc_t *sc = device_private(self);
76e93422faSsimonb 	void *ih;
77badb1c52Shpeyerl 	uint32_t x, tmp;
78e7b5db7cStron 	bus_addr_t usbh_base, usbh_enable;
79badb1c52Shpeyerl 	struct aubus_attach_args *aa = aux;
80badb1c52Shpeyerl 
81e7b5db7cStron 	usbh_base = aa->aa_addrs[0];
82e7b5db7cStron 	usbh_enable = aa->aa_addrs[1];
83e7b5db7cStron 	sc->sc_size = aa->aa_addrs[2];
84e93422faSsimonb 	sc->iot = aa->aa_st;
854e8e6643Sskrll 	sc->sc_bus.ub_dmatag = (bus_dma_tag_t)aa->aa_dt;
86badb1c52Shpeyerl 
8716304febSdrochner 	sc->sc_dev = self;
884e8e6643Sskrll 	sc->sc_bus.ub_hcpriv = sc;
8916304febSdrochner 
90e7b5db7cStron 	if (bus_space_map(sc->iot, usbh_base, sc->sc_size, 0, &sc->ioh)) {
910e06ed5dSdogcow 		aprint_error_dev(self, "unable to map USBH registers\n");
92badb1c52Shpeyerl 		return;
93badb1c52Shpeyerl 	}
94badb1c52Shpeyerl 	/*
95badb1c52Shpeyerl 	 * Enable the USB Host controller here.
96badb1c52Shpeyerl 	 * As per 7.2 in the Au1500 manual:
97badb1c52Shpeyerl 	 *
98badb1c52Shpeyerl 	 *  (1) Set CE bit to enable clocks.
99badb1c52Shpeyerl 	 *  (2) Set E to enable OHCI
100badb1c52Shpeyerl 	 *  (3) Clear HCFS in OHCI_CONTROL.
101badb1c52Shpeyerl 	 *  (4) Wait for RD bit to be set.
102badb1c52Shpeyerl 	 */
103e7b5db7cStron 	x = bus_space_read_4(sc->iot, sc->ioh, usbh_enable);
104badb1c52Shpeyerl 	x |= UE_CE;
105e7b5db7cStron 	bus_space_write_4(sc->iot, sc->ioh, usbh_enable, x);
106badb1c52Shpeyerl 	delay(10);
107513b3305Ssimonb 	x |= UE_E;
108513b3305Ssimonb #ifdef __MIPSEB__
109513b3305Ssimonb 	x |= UE_BE;
110513b3305Ssimonb #endif
111e7b5db7cStron 	bus_space_write_4(sc->iot, sc->ioh, usbh_enable, x);
112badb1c52Shpeyerl 	delay(10);
113e93422faSsimonb 	x = bus_space_read_4(sc->iot, sc->ioh, OHCI_CONTROL);
114badb1c52Shpeyerl 	x &= ~(OHCI_HCFS_MASK);
115e93422faSsimonb 	bus_space_write_4(sc->iot, sc->ioh, OHCI_CONTROL, x);
116badb1c52Shpeyerl 	delay(10);
1175eefd45bShpeyerl 	/*  Need to read USBH_ENABLE twice in succession according to
1185eefd45bShpeyerl          *  au1500 Errata #7.
1195eefd45bShpeyerl          */
120badb1c52Shpeyerl 	for (x = 100; x; x--) {
121e7b5db7cStron 		bus_space_read_4(sc->iot, sc->ioh, usbh_enable);
122e7b5db7cStron 		tmp = bus_space_read_4(sc->iot, sc->ioh, usbh_enable);
123badb1c52Shpeyerl 		if (tmp&UE_RD)
124badb1c52Shpeyerl 			break;
125badb1c52Shpeyerl 		delay(1000);
126badb1c52Shpeyerl 	}
1274e8e6643Sskrll 
1284e8e6643Sskrll 	if (x == 0) {
1294e8e6643Sskrll 		aprint_error_dev(self, "device not ready\n");
1304e8e6643Sskrll 		return;
1314e8e6643Sskrll 	}
1324e8e6643Sskrll 
133e7b5db7cStron 	printf(": Alchemy OHCI\n");
134badb1c52Shpeyerl 
135badb1c52Shpeyerl 	/* Disable OHCI interrupts */
136e93422faSsimonb 	bus_space_write_4(sc->iot, sc->ioh, OHCI_INTERRUPT_DISABLE,
137badb1c52Shpeyerl 				OHCI_ALL_INTRS);
138badb1c52Shpeyerl 	/* hook interrupt */
139e93422faSsimonb 	ih = au_intr_establish(aa->aa_irq[0], 0, IPL_USB, IST_LEVEL_LOW,
140badb1c52Shpeyerl 			ohci_intr, sc);
141e93422faSsimonb 	if (ih == NULL) {
1420e06ed5dSdogcow 		aprint_error_dev(self,"couldn't establish interrupt\n");
143badb1c52Shpeyerl 	}
144badb1c52Shpeyerl 
1458b0925bcStron 	sc->sc_endian = OHCI_HOST_ENDIAN;
1468b0925bcStron 
1474e8e6643Sskrll 	int err = ohci_init(sc);
1484e8e6643Sskrll 	if (err != USBD_NORMAL_COMPLETION) {
1494e8e6643Sskrll 		aprint_error_dev(self, "init failed, error=%d\n", err);
150e93422faSsimonb 		au_intr_disestablish(ih);
151badb1c52Shpeyerl 		return;
152badb1c52Shpeyerl 	}
153badb1c52Shpeyerl 
154badb1c52Shpeyerl 	/* Attach USB device */
155*c7fb772bSthorpej 	sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint, CFARGS_NONE);
156badb1c52Shpeyerl 
157badb1c52Shpeyerl }
158