1/*	$NetBSD: obio_ehci.c,v 1.1 2008/10/24 16:48:29 matt 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.1 2008/10/24 16:48:29 matt 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/pci/pcidevs.h>
50
51#include <dev/usb/usb.h>
52#include <dev/usb/usbdi.h>
53#include <dev/usb/usbdivar.h>
54#include <dev/usb/usb_mem.h>
55
56#include <dev/usb/ehcireg.h>
57#include <dev/usb/ehcivar.h>
58
59#ifdef EHCI_DEBUG
60#define DPRINTF(x)	if (ehcidebug) printf x
61extern int ehcidebug;
62#else
63#define DPRINTF(x)
64#endif
65
66#define	EHCI_HCOTGDEV_INTR_STATUS		0xc0
67#define	EHCI_HCOTGDEV_INTR_MASK			0xc4
68#define	HC_INT					0x04
69#define	OTG_INT					0x02
70#define	DEV_INT					0x01
71
72static int
73ehci_obio_intr(void *arg)
74{
75	struct ehci_softc * const sc = arg;
76	int rv = 0;
77	uint32_t v;
78
79	v = bus_space_read_4(sc->iot, sc->ioh, EHCI_HCOTGDEV_INTR_STATUS);
80	bus_space_write_4(sc->iot, sc->ioh, EHCI_HCOTGDEV_INTR_STATUS, v);
81	if (v & HC_INT)
82		rv = ehci_intr(sc);
83	return rv;
84}
85
86static int
87ehci_obio_match(device_t parent, cfdata_t match, void *aux)
88{
89	struct obio_attach_args * const obio = aux;
90
91	if (obio->obio_addr == GEMINI_USB0_BASE
92	    || obio->obio_addr == GEMINI_USB1_BASE)
93		return 1;
94
95	return 0;
96}
97
98static void
99ehci_obio_attach(device_t parent, device_t self, void *aux)
100{
101	struct ehci_softc * const sc = device_private(self);
102	struct obio_attach_args * const obio = aux;
103	const char * const devname = device_xname(self);
104	usbd_status r;
105
106	sc->sc_dev = self;
107	sc->sc_bus.hci_private = sc;
108	sc->iot = obio->obio_iot;
109
110	aprint_naive(": EHCI USB controller\n");
111	aprint_normal(": EHCI USB controller\n");
112
113	/* Map I/O registers */
114	if (bus_space_map(sc->iot, obio->obio_addr, obio->obio_size, 0,
115			   &sc->ioh)) {
116		aprint_error("%s: can't map memory space\n", devname);
117		return;
118	}
119
120	sc->sc_bus.dmatag = obio->obio_dmat;
121
122	/* Disable interrupts, so we don't get any spurious ones. */
123	sc->sc_offs = EREAD1(sc, EHCI_CAPLENGTH);
124	DPRINTF(("%s: offs=%d\n", devname, sc->sc_offs));
125	EOWRITE2(sc, EHCI_USBINTR, 0);
126	bus_space_write_4(sc->iot, sc->ioh, EHCI_HCOTGDEV_INTR_MASK,
127	    OTG_INT|DEV_INT);
128
129	if (obio->obio_intr != OBIOCF_INTR_DEFAULT) {
130		intr_establish(obio->obio_intr, IPL_USB, IST_LEVEL,
131		    ehci_obio_intr, sc);
132	}
133
134	sc->sc_bus.usbrev = USBREV_2_0;
135
136	/* Figure out vendor for root hub descriptor. */
137	sc->sc_id_vendor = PCI_VENDOR_FARADAY;
138	strlcpy(sc->sc_vendor, "SL351x", sizeof(sc->sc_vendor));
139
140	r = ehci_init(sc);
141	if (r != USBD_NORMAL_COMPLETION) {
142		aprint_error("%s: init failed, error=%d\n", devname, r);
143		return;
144	}
145
146	/* Attach usb device. */
147	sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint);
148}
149
150CFATTACH_DECL2_NEW(ehci_obio, sizeof(struct ehci_softc),
151    ehci_obio_match, ehci_obio_attach, NULL, NULL, NULL, ehci_childdet);
152