ohci_pci.c revision 1.19
1/*	$NetBSD: ohci_pci.c,v 1.19 2001/11/06 03:17:36 augustss Exp $	*/
2
3/*
4 * Copyright (c) 1998 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) at
9 * Carlstedt Research & Technology.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 *    must display the following acknowledgement:
21 *        This product includes software developed by the NetBSD
22 *        Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 *    contributors may be used to endorse or promote products derived
25 *    from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40#include "ehci.h"
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/kernel.h>
45#include <sys/device.h>
46#include <sys/proc.h>
47#include <sys/queue.h>
48
49#include <machine/bus.h>
50
51#include <dev/pci/pcivar.h>
52#include <dev/pci/usb_pci.h>
53
54#include <dev/usb/usb.h>
55#include <dev/usb/usbdi.h>
56#include <dev/usb/usbdivar.h>
57#include <dev/usb/usb_mem.h>
58
59#include <dev/usb/ohcireg.h>
60#include <dev/usb/ohcivar.h>
61
62int	ohci_pci_match(struct device *, struct cfdata *, void *);
63void	ohci_pci_attach(struct device *, struct device *, void *);
64int	ohci_pci_detach(device_ptr_t, int);
65
66struct ohci_pci_softc {
67	ohci_softc_t		sc;
68#if NEHCI > 0
69	struct usb_pci		sc_pci;
70#endif
71	pci_chipset_tag_t	sc_pc;
72	void 			*sc_ih;		/* interrupt vectoring */
73};
74
75struct cfattach ohci_pci_ca = {
76	sizeof(struct ohci_pci_softc), ohci_pci_match, ohci_pci_attach,
77	ohci_pci_detach, ohci_activate
78};
79
80int
81ohci_pci_match(struct device *parent, struct cfdata *match, void *aux)
82{
83	struct pci_attach_args *pa = (struct pci_attach_args *) aux;
84
85	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_SERIALBUS &&
86	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_SERIALBUS_USB &&
87	    PCI_INTERFACE(pa->pa_class) == PCI_INTERFACE_OHCI)
88		return (1);
89
90	return (0);
91}
92
93void
94ohci_pci_attach(struct device *parent, struct device *self, void *aux)
95{
96	struct ohci_pci_softc *sc = (struct ohci_pci_softc *)self;
97	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
98	pci_chipset_tag_t pc = pa->pa_pc;
99	pcitag_t tag = pa->pa_tag;
100	char const *intrstr;
101	pci_intr_handle_t ih;
102	pcireg_t csr;
103	char devinfo[256];
104	usbd_status r;
105	char *vendor;
106	char *devname = sc->sc.sc_bus.bdev.dv_xname;
107
108	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
109	printf(": %s (rev. 0x%02x)\n", devinfo, PCI_REVISION(pa->pa_class));
110
111	/* Map I/O registers */
112	if (pci_mapreg_map(pa, PCI_CBMEM, PCI_MAPREG_TYPE_MEM, 0,
113			   &sc->sc.iot, &sc->sc.ioh, NULL, &sc->sc.sc_size)) {
114		printf("%s: can't map mem space\n", devname);
115		return;
116	}
117
118	/* Disable interrupts, so we don't get any spurious ones. */
119	bus_space_write_4(sc->sc.iot, sc->sc.ioh, OHCI_INTERRUPT_DISABLE,
120			  OHCI_ALL_INTRS);
121
122	sc->sc_pc = pc;
123	sc->sc.sc_bus.dmatag = pa->pa_dmat;
124
125	/* Enable the device. */
126	csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
127	pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG,
128		       csr | PCI_COMMAND_MASTER_ENABLE);
129
130	/* Map and establish the interrupt. */
131	if (pci_intr_map(pa, &ih)) {
132		printf("%s: couldn't map interrupt\n", devname);
133		return;
134	}
135	intrstr = pci_intr_string(pc, ih);
136	sc->sc_ih = pci_intr_establish(pc, ih, IPL_USB, ohci_intr, sc);
137	if (sc->sc_ih == NULL) {
138		printf("%s: couldn't establish interrupt", devname);
139		if (intrstr != NULL)
140			printf(" at %s", intrstr);
141		printf("\n");
142		return;
143	}
144	printf("%s: interrupting at %s\n", devname, intrstr);
145
146	/* Figure out vendor for root hub descriptor. */
147	vendor = pci_findvendor(pa->pa_id);
148	sc->sc.sc_id_vendor = PCI_VENDOR(pa->pa_id);
149	if (vendor)
150		strncpy(sc->sc.sc_vendor, vendor,
151			sizeof(sc->sc.sc_vendor) - 1);
152	else
153		sprintf(sc->sc.sc_vendor, "vendor 0x%04x",
154			PCI_VENDOR(pa->pa_id));
155
156	r = ohci_init(&sc->sc);
157	if (r != USBD_NORMAL_COMPLETION) {
158		printf("%s: init failed, error=%d\n", devname, r);
159		return;
160	}
161
162#if NEHCI > 0
163	usb_pci_add(&sc->sc_pci, pa, &sc->sc.sc_bus);
164#endif
165
166	/* Attach usb device. */
167	sc->sc.sc_child = config_found((void *)sc, &sc->sc.sc_bus,
168				       usbctlprint);
169}
170
171int
172ohci_pci_detach(device_ptr_t self, int flags)
173{
174	struct ohci_pci_softc *sc = (struct ohci_pci_softc *)self;
175	int rv;
176
177	rv = ohci_detach(&sc->sc, flags);
178	if (rv)
179		return (rv);
180	if (sc->sc_ih != NULL) {
181		pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
182		sc->sc_ih = NULL;
183	}
184	if (sc->sc.sc_size) {
185		bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size);
186		sc->sc.sc_size = 0;
187	}
188#if NEHCI > 0
189	usb_pci_rem(&sc->sc_pci);
190#endif
191	return (0);
192}
193