1/*	$NetBSD: obio_ehci.c,v 1.7 2021/08/07 16:18:44 thorpej Exp $	*/
2
3/*
4 * Copyright (c) 2001, 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart@augustsson.net).
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: obio_ehci.c,v 1.7 2021/08/07 16:18:44 thorpej Exp $");
34
35#include "locators.h"
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/kernel.h>
40#include <sys/device.h>
41#include <sys/proc.h>
42#include <sys/queue.h>
43
44#include <sys/bus.h>
45
46#include <arm/gemini/gemini_reg.h>
47#include <arm/gemini/gemini_obiovar.h>
48
49#include <dev/usb/usb.h>
50#include <dev/usb/usbdi.h>
51#include <dev/usb/usbdivar.h>
52#include <dev/usb/usb_mem.h>
53
54#include <dev/usb/ehcireg.h>
55#include <dev/usb/ehcivar.h>
56
57#ifdef EHCI_DEBUG
58#define DPRINTF(x)	if (ehcidebug) printf x
59extern int ehcidebug;
60#else
61#define DPRINTF(x)
62#endif
63
64#define	EHCI_HCOTGDEV_INTR_STATUS		0xc0
65#define	EHCI_HCOTGDEV_INTR_MASK			0xc4
66#define	HC_INT					0x04
67#define	OTG_INT					0x02
68#define	DEV_INT					0x01
69
70static int
71ehci_obio_intr(void *arg)
72{
73	struct ehci_softc * const sc = arg;
74	int rv = 0;
75	uint32_t v;
76
77	v = bus_space_read_4(sc->iot, sc->ioh, EHCI_HCOTGDEV_INTR_STATUS);
78	bus_space_write_4(sc->iot, sc->ioh, EHCI_HCOTGDEV_INTR_STATUS, v);
79	if (v & HC_INT)
80		rv = ehci_intr(sc);
81	return rv;
82}
83
84static int
85ehci_obio_match(device_t parent, cfdata_t match, void *aux)
86{
87	struct obio_attach_args * const obio = aux;
88
89	if (obio->obio_addr == GEMINI_USB0_BASE
90	    || obio->obio_addr == GEMINI_USB1_BASE)
91		return 1;
92
93	return 0;
94}
95
96static void
97ehci_obio_attach(device_t parent, device_t self, void *aux)
98{
99	struct ehci_softc * const sc = device_private(self);
100	struct obio_attach_args * const obio = aux;
101	const char * const devname = device_xname(self);
102
103	sc->sc_dev = self;
104	sc->sc_bus.ub_hcpriv = sc;
105	sc->iot = obio->obio_iot;
106
107	aprint_naive(": EHCI USB controller\n");
108	aprint_normal(": EHCI USB controller\n");
109
110	/* Map I/O registers */
111	if (bus_space_map(sc->iot, obio->obio_addr, obio->obio_size, 0,
112			   &sc->ioh)) {
113		aprint_error("%s: can't map memory space\n", devname);
114		return;
115	}
116
117	sc->sc_bus.ub_dmatag = obio->obio_dmat;
118
119	/* Disable interrupts, so we don't get any spurious ones. */
120	sc->sc_offs = EREAD1(sc, EHCI_CAPLENGTH);
121	DPRINTF(("%s: offs=%d\n", devname, sc->sc_offs));
122	EOWRITE4(sc, EHCI_USBINTR, 0);
123	bus_space_write_4(sc->iot, sc->ioh, EHCI_HCOTGDEV_INTR_MASK,
124	    OTG_INT|DEV_INT);
125
126	if (obio->obio_intr != OBIOCF_INTR_DEFAULT) {
127		intr_establish(obio->obio_intr, IPL_USB, IST_LEVEL,
128		    ehci_obio_intr, sc);
129	}
130
131	sc->sc_bus.ub_revision = USBREV_2_0;
132
133	int err = ehci_init(sc);
134	if (err) {
135		aprint_error("%s: init failed, error=%d\n", devname, err);
136		return;
137	}
138
139	/* Attach usb device. */
140	sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint, CFARGS_NONE);
141}
142
143CFATTACH_DECL2_NEW(ehci_obio, sizeof(struct ehci_softc),
144    ehci_obio_match, ehci_obio_attach, NULL, NULL, NULL, ehci_childdet);
145