1/*	$NetBSD: epohci.c,v 1.11 2021/08/07 16:18:43 thorpej Exp $ */
2
3/*-
4 * Copyright (c) 2004 Jesse Off
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29/*
30 * USB Open Host Controller driver.
31 *
32 * OHCI spec: ftp://ftp.compaq.com/pub/supportinformation/papers/hcir1_0a.exe
33 * USB spec: http://www.usb.org/developers/data/usb11.pdf
34 */
35
36#include <sys/cdefs.h>
37__KERNEL_RCSID(0, "$NetBSD: epohci.c,v 1.11 2021/08/07 16:18:43 thorpej Exp $");
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/kernel.h>
42#include <sys/device.h>
43#include <sys/proc.h>
44#include <sys/queue.h>
45
46/* busdma */
47#include <sys/mbuf.h>
48#include <uvm/uvm_extern.h>
49
50#include <sys/bus.h>
51
52#include <dev/usb/usb.h>
53#include <dev/usb/usbdi.h>
54#include <dev/usb/usbdivar.h>
55#include <dev/usb/usb_mem.h>
56
57#include <dev/usb/ohcireg.h>
58#include <dev/usb/ohcivar.h>
59
60#include <arm/ep93xx/ep93xxreg.h>
61#include <arm/ep93xx/ep93xxvar.h>
62#include <arm/ep93xx/epsocvar.h>
63
64int epohci_match(device_t, cfdata_t, void *);
65void epohci_attach(device_t, device_t, void *);
66void epohci_callback(device_t);
67
68struct epohci_softc {
69	struct ohci_softc sc;
70	void	*sc_ih;
71	int	sc_intr;
72};
73
74CFATTACH_DECL_NEW(epohci, sizeof(struct epohci_softc),
75    epohci_match, epohci_attach, NULL, NULL);
76
77int
78epohci_match(device_t parent, cfdata_t match, void *aux)
79{
80	/* EP93xx builtin OHCI module */
81
82	return 1;
83}
84
85void
86epohci_attach(device_t parent, device_t self, void *aux)
87{
88	struct epohci_softc *sc = device_private(self);
89	struct epsoc_attach_args *sa = aux;
90	uint32_t i;
91	bus_space_handle_t syscon_ioh;
92
93	sc->sc.sc_dev = self;
94	sc->sc.sc_bus.ub_hcpriv = sc;
95
96	sc->sc.iot = sa->sa_iot;
97	sc->sc.sc_bus.ub_dmatag = sa->sa_dmat;
98	sc->sc_intr = sa->sa_intr;
99
100	/* Map I/O space */
101	if (bus_space_map(sc->sc.iot, sa->sa_addr, sa->sa_size,
102	    0, &sc->sc.ioh)) {
103		printf(": cannot map mem space\n");
104		return;
105	}
106
107	/* Enable hclk clock gating to the USB block. */
108
109	bus_space_map(sc->sc.iot, EP93XX_APB_HWBASE + EP93XX_APB_SYSCON,
110		EP93XX_APB_SYSCON_SIZE, 0, &syscon_ioh);
111	i = bus_space_read_4(sc->sc.iot, syscon_ioh, EP93XX_SYSCON_PwrCnt);
112	i |= 0x10000000;
113	bus_space_write_4(sc->sc.iot, syscon_ioh, EP93XX_SYSCON_PwrCnt, i);
114
115	/*
116	 * Not sure if I understand the datasheet here, but we must wait a
117	 * few instructions and also make certain PLL2 is stable before
118	 * continuing.  Hopefully, the check for PLL2 is enough, as the
119	 * datasheet is elusive to actually how many insns we need.
120	 */
121
122	do {
123		i = bus_space_read_4(sc->sc.iot, syscon_ioh,
124			EP93XX_SYSCON_PwrSts);
125	} while ((i & 0x100) == 0);
126	bus_space_unmap(sc->sc.iot, syscon_ioh, EP93XX_APB_SYSCON_SIZE);
127
128	printf("\n");
129
130        /* Defer the rest until later */
131        config_defer(self, epohci_callback);
132}
133
134void
135epohci_callback(device_t self)
136{
137	struct epohci_softc *sc = device_private(self);
138
139	/* Disable interrupts, so we don't get any spurious ones. */
140	bus_space_write_4(sc->sc.iot, sc->sc.ioh, OHCI_INTERRUPT_DISABLE,
141			  OHCI_ALL_INTRS);
142
143	sc->sc_ih = ep93xx_intr_establish(sc->sc_intr, IPL_USB,
144		ohci_intr, sc);
145	int err = ohci_init(&sc->sc);
146
147	if (err) {
148		printf("%s: init failed, error=%d\n", device_xname(self), err);
149
150		ep93xx_intr_disestablish(sc->sc_ih);
151		return;
152	}
153
154	/* Attach usb device. */
155	sc->sc.sc_child = config_found(self, &sc->sc.sc_bus, usbctlprint,
156	    CFARGS_NONE);
157
158}
159