1/*	$NetBSD: ehci_arbus.c,v 1.10 2021/08/07 16:18:59 thorpej Exp $	*/
2
3/*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matt Thomas of 3am Software Foundry.
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: ehci_arbus.c,v 1.10 2021/08/07 16:18:59 thorpej Exp $");
34
35#include "locators.h"
36
37#include <sys/param.h>
38#include <sys/bus.h>
39#include <sys/device.h>
40#include <sys/systm.h>
41
42#include <mips/locore.h>
43#include <mips/atheros/include/ar9344reg.h>
44#include <mips/atheros/include/arbusvar.h>
45
46#include <dev/usb/usb.h>
47#include <dev/usb/usbdi.h>
48#include <dev/usb/usbdivar.h>
49#include <dev/usb/usb_mem.h>
50
51#include <dev/usb/ehcireg.h>
52#include <dev/usb/ehcivar.h>
53
54/*
55 * This is relative to the start of the unreserved registers in USB controller
56 * block and not the full USB block which would be 0x1a8.
57 */
58#define	ARBUS_USBMODE		0xa8			/* USB mode */
59#define	 USBMODE_CM		__BITS(0,1)		/* Controller Mode */
60#define	 USBMODE_CM_IDLE	__SHIFTIN(0,USBMODE_CM)	/* Idle (both) */
61#define	 USBMODE_CM_DEVICE	__SHIFTIN(2,USBMODE_CM)	/* Device Controller */
62#define	 USBMODE_CM_HOST	__SHIFTIN(3,USBMODE_CM)	/* Host Controller */
63
64static int	ehci_arbus_match(device_t, cfdata_t, void *);
65static void	ehci_arbus_attach(device_t, device_t, void *);
66
67CFATTACH_DECL_NEW(ehci_arbus, sizeof (ehci_softc_t),
68    ehci_arbus_match, ehci_arbus_attach, NULL, NULL);
69
70static void ehci_arbus_init(struct ehci_softc *);
71
72int
73ehci_arbus_match(device_t parent, cfdata_t cf, void *aux)
74{
75	struct arbus_attach_args *aa = aux;
76
77	if (strcmp(aa->aa_name, cf->cf_name) != 0)
78		return 0;
79
80	if (badaddr((void *)MIPS_PHYS_TO_KSEG1(aa->aa_addr), 4))
81		return 0;
82
83        return 1;	/* XXX */
84}
85
86void
87ehci_arbus_attach(device_t parent, device_t self, void *aux)
88{
89	ehci_softc_t *sc = device_private(self);
90	struct arbus_attach_args * const aa = aux;
91	void *ih = NULL;
92	int error;
93
94	sc->iot = aa->aa_bst_le;
95	sc->sc_size = aa->aa_size;
96	//sc->sc_bus.ub_hcpriv = sc;
97	sc->sc_bus.ub_dmatag = aa->aa_dmat;
98	sc->sc_bus.ub_revision = USBREV_1_0;
99	sc->sc_flags |= EHCIF_ETTF;
100	sc->sc_vendor_init = ehci_arbus_init;
101
102	error = bus_space_map(aa->aa_bst, aa->aa_addr, aa->aa_size, 0,
103	    &sc->ioh);
104
105	if (error) {
106		aprint_error(": failed to map registers: %d\n", error);
107		return;
108	}
109
110	/* The recommended value is 0x20 for both ports and the host */
111	REGVAL(AR9344_USB_CONFIG_BASE) = 0x20c00;	/* magic */
112	DELAY(1000);
113
114	/* get offset to operational regs */
115	uint32_t r = bus_space_read_4(aa->aa_bst, sc->ioh, 0);
116	if (r != 0x40) {
117		aprint_error(": error: CAPLENGTH (%#x) != 0x40\n", sc->sc_offs);
118		return;
119	}
120
121	sc->sc_offs = EREAD1(sc, EHCI_CAPLENGTH);
122
123	aprint_normal("\n");
124
125	/* Disable EHCI interrupts */
126	EOWRITE4(sc, EHCI_USBINTR, 0);
127
128	/* establish interrupt */
129	ih = arbus_intr_establish(aa->aa_cirq, aa->aa_mirq, ehci_intr, sc);
130	if (ih == NULL)
131		panic("%s: couldn't establish interrupt",
132		    device_xname(self));
133
134	/*
135	 * There are no companion controllers
136	 */
137	sc->sc_ncomp = 0;
138
139	error = ehci_init(sc);
140	if (error) {
141		aprint_error("%s: init failed, error=%d\n", device_xname(self),
142		    error);
143		if (ih != NULL)
144			arbus_intr_disestablish(ih);
145		return;
146	}
147
148	/* Attach USB device */
149	sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint, CFARGS_NONE);
150}
151
152static void
153ehci_arbus_init(struct ehci_softc *sc)
154{
155	/* Set host mode */
156	uint32_t old = bus_space_read_4(sc->iot, sc->ioh, ARBUS_USBMODE);
157	uint32_t reg = old;
158
159	reg &= ~USBMODE_CM;
160	reg |= USBMODE_CM_HOST;
161	if (reg != old)
162		bus_space_write_4(sc->iot, sc->ioh, ARBUS_USBMODE, reg);
163
164}
165