1/*	$OpenBSD: ohci_pci.c,v 1.43 2024/05/24 06:02:58 jsg Exp $	*/
2/*	$NetBSD: ohci_pci.c,v 1.23 2002/10/02 16:51:47 thorpej Exp $	*/
3
4/*
5 * Copyright (c) 1998 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (lennart@augustsson.net) at
10 * Carlstedt Research & Technology.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in the
19 *    documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34/*
35 * USB Open Host Controller driver.
36 *
37 * OHCI spec: http://www.intel.com/design/usb/ohci11d.pdf
38 * USB spec: http://www.teleport.com/cgi-bin/mailmerge.cgi/~usb/cgiform.tpl
39 */
40
41#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/device.h>
44#include <sys/timeout.h>
45#include <sys/queue.h>
46
47#include <machine/bus.h>
48
49#include <dev/pci/pcivar.h>
50
51#include <dev/usb/usb.h>
52#include <dev/usb/usbdi.h>
53#include <dev/usb/usbdivar.h>
54
55#include <dev/usb/ohcireg.h>
56#include <dev/usb/ohcivar.h>
57
58int	ohci_pci_match(struct device *, void *, void *);
59void	ohci_pci_attach(struct device *, struct device *, void *);
60void	ohci_pci_attach_deferred(struct device *);
61int	ohci_pci_detach(struct device *, int);
62
63struct ohci_pci_softc {
64	struct ohci_softc	sc;
65	pci_chipset_tag_t	sc_pc;
66	void 			*sc_ih;		/* interrupt vectoring */
67};
68
69const struct cfattach ohci_pci_ca = {
70	sizeof(struct ohci_pci_softc), ohci_pci_match, ohci_pci_attach,
71	ohci_pci_detach, ohci_activate
72};
73
74int
75ohci_pci_match(struct device *parent, void *match, void *aux)
76{
77	struct pci_attach_args *pa = (struct pci_attach_args *) aux;
78
79	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_SERIALBUS &&
80	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_SERIALBUS_USB &&
81	    PCI_INTERFACE(pa->pa_class) == PCI_INTERFACE_OHCI)
82		return (1);
83
84	return (0);
85}
86
87void
88ohci_pci_attach(struct device *parent, struct device *self, void *aux)
89{
90	struct ohci_pci_softc *sc = (struct ohci_pci_softc *)self;
91	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
92	pci_chipset_tag_t pc = pa->pa_pc;
93	char const *intrstr;
94	pci_intr_handle_t ih;
95	int s;
96	const char *vendor;
97	char *devname = sc->sc.sc_bus.bdev.dv_xname;
98
99	/* Map I/O registers */
100	if (pci_mapreg_map(pa, PCI_CBMEM, PCI_MAPREG_TYPE_MEM, 0,
101		    &sc->sc.iot, &sc->sc.ioh, NULL, &sc->sc.sc_size, 0)) {
102		printf(": can't map mem space\n");
103		return;
104	}
105
106	/* Record what interrupts were enabled by SMM/BIOS. */
107	sc->sc.sc_intre = bus_space_read_4(sc->sc.iot, sc->sc.ioh,
108	    OHCI_INTERRUPT_ENABLE);
109
110	/* Disable interrupts, so we don't get any spurious ones. */
111	bus_space_write_4(sc->sc.iot, sc->sc.ioh, OHCI_INTERRUPT_DISABLE,
112			  OHCI_MIE);
113
114	sc->sc_pc = pc;
115	sc->sc.sc_bus.dmatag = pa->pa_dmat;
116
117	bus_space_barrier(sc->sc.iot, sc->sc.ioh, 0, sc->sc.sc_size,
118	    BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE);
119	bus_space_write_4(sc->sc.iot, sc->sc.ioh,
120	    OHCI_INTERRUPT_DISABLE, OHCI_MIE);
121
122	s = splusb();
123	/* Map and establish the interrupt. */
124	if (pci_intr_map(pa, &ih)) {
125		printf(": couldn't map interrupt\n");
126		bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size);
127		splx(s);
128		return;
129	}
130
131	intrstr = pci_intr_string(pc, ih);
132	sc->sc_ih = pci_intr_establish(pc, ih, IPL_USB, ohci_intr, sc, devname);
133	if (sc->sc_ih == NULL) {
134		printf(": couldn't establish interrupt");
135		if (intrstr != NULL)
136			printf(" at %s", intrstr);
137		printf("\n");
138		bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size);
139		splx(s);
140		return;
141	}
142	printf(": %s, ", intrstr);
143
144	/* Figure out vendor for root hub descriptor. */
145	vendor = pci_findvendor(pa->pa_id);
146	sc->sc.sc_id_vendor = PCI_VENDOR(pa->pa_id);
147	if (vendor)
148		strlcpy(sc->sc.sc_vendor, vendor, sizeof (sc->sc.sc_vendor));
149	else
150		snprintf(sc->sc.sc_vendor, sizeof (sc->sc.sc_vendor),
151		    "vendor 0x%04x", PCI_VENDOR(pa->pa_id));
152
153	/* Display revision and perform legacy emulation handover. */
154	if (ohci_checkrev(&sc->sc) != USBD_NORMAL_COMPLETION ||
155	    ohci_handover(&sc->sc) != USBD_NORMAL_COMPLETION) {
156		bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size);
157		pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
158		splx(s);
159		return;
160	}
161
162	/* Ignore interrupts for now */
163	sc->sc.sc_bus.dying = 1;
164
165	config_defer(self, ohci_pci_attach_deferred);
166
167	splx(s);
168
169	return;
170}
171
172void
173ohci_pci_attach_deferred(struct device *self)
174{
175	struct ohci_pci_softc *sc = (struct ohci_pci_softc *)self;
176	usbd_status r;
177	int s;
178
179	s = splusb();
180
181	sc->sc.sc_bus.dying = 0;
182
183	r = ohci_init(&sc->sc);
184	if (r != USBD_NORMAL_COMPLETION) {
185		printf("%s: init failed, error=%d\n",
186		    sc->sc.sc_bus.bdev.dv_xname, r);
187		bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size);
188		pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
189		splx(s);
190		return;
191	}
192	splx(s);
193
194	/* Attach usb device. */
195	config_found(self, &sc->sc.sc_bus, usbctlprint);
196}
197
198int
199ohci_pci_detach(struct device *self, int flags)
200{
201	struct ohci_pci_softc *sc = (struct ohci_pci_softc *)self;
202	int rv;
203
204	rv = ohci_detach(self, flags);
205	if (rv)
206		return (rv);
207
208	if (sc->sc_ih != NULL) {
209		pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
210		sc->sc_ih = NULL;
211	}
212	if (sc->sc.sc_size) {
213		bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size);
214		sc->sc.sc_size = 0;
215	}
216	return (0);
217}
218