ohci_pci.c revision 1.40
1/*	$NetBSD: ohci_pci.c,v 1.40 2009/04/17 17:31:01 dyoung 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 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include <sys/cdefs.h>
34__KERNEL_RCSID(0, "$NetBSD: ohci_pci.c,v 1.40 2009/04/17 17:31:01 dyoung Exp $");
35
36#include "ehci.h"
37
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/kernel.h>
41#include <sys/device.h>
42#include <sys/proc.h>
43#include <sys/queue.h>
44
45#include <sys/bus.h>
46
47#include <dev/pci/pcivar.h>
48#include <dev/pci/usb_pci.h>
49
50#include <dev/usb/usb.h>
51#include <dev/usb/usbdi.h>
52#include <dev/usb/usbdivar.h>
53#include <dev/usb/usb_mem.h>
54
55#include <dev/usb/ohcireg.h>
56#include <dev/usb/ohcivar.h>
57
58struct ohci_pci_softc {
59	ohci_softc_t		sc;
60#if NEHCI > 0
61	struct usb_pci		sc_pci;
62#endif
63	pci_chipset_tag_t	sc_pc;
64	pcitag_t		sc_tag;
65	void 			*sc_ih;		/* interrupt vectoring */
66};
67
68static int
69ohci_pci_match(device_t parent, struct cfdata *match, void *aux)
70{
71	struct pci_attach_args *pa = (struct pci_attach_args *) aux;
72
73	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_SERIALBUS &&
74	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_SERIALBUS_USB &&
75	    PCI_INTERFACE(pa->pa_class) == PCI_INTERFACE_OHCI)
76		return (1);
77
78	return (0);
79}
80
81static void
82ohci_pci_attach(device_t parent, device_t self, void *aux)
83{
84	struct ohci_pci_softc *sc = device_private(self);
85	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
86	pci_chipset_tag_t pc = pa->pa_pc;
87	pcitag_t tag = pa->pa_tag;
88	char const *intrstr;
89	pci_intr_handle_t ih;
90	pcireg_t csr;
91	char devinfo[256];
92	usbd_status r;
93	const char *vendor;
94	const char *devname = device_xname(self);
95
96	sc->sc.sc_dev = self;
97	sc->sc.sc_bus.hci_private = sc;
98
99	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
100	printf(": %s (rev. 0x%02x)\n", devinfo, PCI_REVISION(pa->pa_class));
101
102	/* Map I/O registers */
103	if (pci_mapreg_map(pa, PCI_CBMEM, PCI_MAPREG_TYPE_MEM, 0,
104			   &sc->sc.iot, &sc->sc.ioh, NULL, &sc->sc.sc_size)) {
105		printf("%s: can't map mem space\n", devname);
106		return;
107	}
108
109	/* Disable interrupts, so we don't get any spurious ones. */
110	bus_space_write_4(sc->sc.iot, sc->sc.ioh, OHCI_INTERRUPT_DISABLE,
111			  OHCI_ALL_INTRS);
112
113	sc->sc_pc = pc;
114	sc->sc_tag = tag;
115	sc->sc.sc_bus.dmatag = pa->pa_dmat;
116
117	/* Enable the device. */
118	csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
119	pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG,
120		       csr | PCI_COMMAND_MASTER_ENABLE);
121
122	/* Map and establish the interrupt. */
123	if (pci_intr_map(pa, &ih)) {
124		printf("%s: couldn't map interrupt\n", devname);
125		return;
126	}
127	intrstr = pci_intr_string(pc, ih);
128	sc->sc_ih = pci_intr_establish(pc, ih, IPL_USB, ohci_intr, sc);
129	if (sc->sc_ih == NULL) {
130		printf("%s: couldn't establish interrupt", devname);
131		if (intrstr != NULL)
132			printf(" at %s", intrstr);
133		printf("\n");
134		return;
135	}
136	printf("%s: interrupting at %s\n", devname, intrstr);
137
138	/* Figure out vendor for root hub descriptor. */
139	vendor = pci_findvendor(pa->pa_id);
140	sc->sc.sc_id_vendor = PCI_VENDOR(pa->pa_id);
141	if (vendor)
142		strlcpy(sc->sc.sc_vendor, vendor, sizeof(sc->sc.sc_vendor));
143	else
144		snprintf(sc->sc.sc_vendor, sizeof(sc->sc.sc_vendor),
145		    "vendor 0x%04x", PCI_VENDOR(pa->pa_id));
146
147	r = ohci_init(&sc->sc);
148	if (r != USBD_NORMAL_COMPLETION) {
149		printf("%s: init failed, error=%d\n", devname, r);
150		return;
151	}
152
153#if NEHCI > 0
154	usb_pci_add(&sc->sc_pci, pa, self);
155#endif
156
157	if (!pmf_device_register1(self, ohci_suspend, ohci_resume,
158	                          ohci_shutdown))
159		aprint_error_dev(self, "couldn't establish power handler\n");
160
161	/* Attach usb device. */
162	sc->sc.sc_child = config_found(self, &sc->sc.sc_bus, usbctlprint);
163}
164
165static int
166ohci_pci_detach(device_ptr_t self, int flags)
167{
168	struct ohci_pci_softc *sc = device_private(self);
169	int rv;
170
171	rv = ohci_detach(&sc->sc, flags);
172	if (rv)
173		return (rv);
174
175	/* Disable interrupts, so we don't get any spurious ones. */
176	bus_space_write_4(sc->sc.iot, sc->sc.ioh, OHCI_INTERRUPT_DISABLE,
177			  OHCI_ALL_INTRS);
178
179	if (sc->sc_ih != NULL) {
180		pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
181		sc->sc_ih = NULL;
182	}
183	if (sc->sc.sc_size) {
184		bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size);
185		sc->sc.sc_size = 0;
186	}
187#if NEHCI > 0
188	usb_pci_rem(&sc->sc_pci);
189#endif
190	return (0);
191}
192
193CFATTACH_DECL2_NEW(ohci_pci, sizeof(struct ohci_pci_softc),
194    ohci_pci_match, ohci_pci_attach, ohci_pci_detach, ohci_activate, NULL,
195    ohci_childdet);
196