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