ohci_pci.c revision 1.42
11.42Scegger/*	$NetBSD: ohci_pci.c,v 1.42 2009/04/26 08:46:10 cegger Exp $	*/
21.1Saugustss
31.1Saugustss/*
41.1Saugustss * Copyright (c) 1998 The NetBSD Foundation, Inc.
51.1Saugustss * All rights reserved.
61.1Saugustss *
71.5Saugustss * This code is derived from software contributed to The NetBSD Foundation
81.16Saugustss * by Lennart Augustsson (lennart@augustsson.net) at
91.5Saugustss * Carlstedt Research & Technology.
101.1Saugustss *
111.1Saugustss * Redistribution and use in source and binary forms, with or without
121.1Saugustss * modification, are permitted provided that the following conditions
131.1Saugustss * are met:
141.1Saugustss * 1. Redistributions of source code must retain the above copyright
151.1Saugustss *    notice, this list of conditions and the following disclaimer.
161.1Saugustss * 2. Redistributions in binary form must reproduce the above copyright
171.1Saugustss *    notice, this list of conditions and the following disclaimer in the
181.1Saugustss *    documentation and/or other materials provided with the distribution.
191.1Saugustss *
201.1Saugustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
211.1Saugustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
221.1Saugustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
231.1Saugustss * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
241.1Saugustss * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
251.1Saugustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
261.1Saugustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
271.1Saugustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
281.1Saugustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
291.1Saugustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
301.1Saugustss * POSSIBILITY OF SUCH DAMAGE.
311.1Saugustss */
321.20Slukem
331.20Slukem#include <sys/cdefs.h>
341.42Scegger__KERNEL_RCSID(0, "$NetBSD: ohci_pci.c,v 1.42 2009/04/26 08:46:10 cegger Exp $");
351.1Saugustss
361.19Saugustss#include "ehci.h"
371.19Saugustss
381.1Saugustss#include <sys/param.h>
391.1Saugustss#include <sys/systm.h>
401.1Saugustss#include <sys/kernel.h>
411.1Saugustss#include <sys/device.h>
421.1Saugustss#include <sys/proc.h>
431.1Saugustss#include <sys/queue.h>
441.1Saugustss
451.32Sad#include <sys/bus.h>
461.3Saugustss
471.1Saugustss#include <dev/pci/pcivar.h>
481.19Saugustss#include <dev/pci/usb_pci.h>
491.1Saugustss
501.1Saugustss#include <dev/usb/usb.h>
511.1Saugustss#include <dev/usb/usbdi.h>
521.1Saugustss#include <dev/usb/usbdivar.h>
531.3Saugustss#include <dev/usb/usb_mem.h>
541.3Saugustss
551.1Saugustss#include <dev/usb/ohcireg.h>
561.1Saugustss#include <dev/usb/ohcivar.h>
571.1Saugustss
581.14Saugustssstruct ohci_pci_softc {
591.14Saugustss	ohci_softc_t		sc;
601.19Saugustss#if NEHCI > 0
611.19Saugustss	struct usb_pci		sc_pci;
621.19Saugustss#endif
631.14Saugustss	pci_chipset_tag_t	sc_pc;
641.33Sjmcneill	pcitag_t		sc_tag;
651.14Saugustss	void 			*sc_ih;		/* interrupt vectoring */
661.14Saugustss};
671.1Saugustss
681.28Sthorpejstatic int
691.41Sdyoungohci_pci_match(device_t parent, cfdata_t match, void *aux)
701.1Saugustss{
711.1Saugustss	struct pci_attach_args *pa = (struct pci_attach_args *) aux;
721.1Saugustss
731.4Saugustss	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_SERIALBUS &&
741.4Saugustss	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_SERIALBUS_USB &&
751.4Saugustss	    PCI_INTERFACE(pa->pa_class) == PCI_INTERFACE_OHCI)
761.41Sdyoung		return 1;
771.27Sperry
781.41Sdyoung	return 0;
791.1Saugustss}
801.1Saugustss
811.28Sthorpejstatic void
821.36Sdyoungohci_pci_attach(device_t parent, device_t self, void *aux)
831.1Saugustss{
841.36Sdyoung	struct ohci_pci_softc *sc = device_private(self);
851.1Saugustss	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
861.1Saugustss	pci_chipset_tag_t pc = pa->pa_pc;
871.17Saugustss	pcitag_t tag = pa->pa_tag;
881.1Saugustss	char const *intrstr;
891.1Saugustss	pci_intr_handle_t ih;
901.1Saugustss	pcireg_t csr;
911.1Saugustss	char devinfo[256];
921.1Saugustss	usbd_status r;
931.26Smycroft	const char *vendor;
941.37Sdrochner
951.37Sdrochner	sc->sc.sc_dev = self;
961.38Snakayama	sc->sc.sc_bus.hci_private = sc;
971.1Saugustss
981.25Sitojun	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
991.42Scegger	aprint_normal(": %s (rev. 0x%02x)\n",
1001.42Scegger	    devinfo, PCI_REVISION(pa->pa_class));
1011.10Saugustss
1021.1Saugustss	/* Map I/O registers */
1031.1Saugustss	if (pci_mapreg_map(pa, PCI_CBMEM, PCI_MAPREG_TYPE_MEM, 0,
1041.15Saugustss			   &sc->sc.iot, &sc->sc.ioh, NULL, &sc->sc.sc_size)) {
1051.42Scegger		sc->sc.sc_size = 0;
1061.42Scegger		aprint_error_dev(self, "can't map mem space\n");
1071.1Saugustss		return;
1081.1Saugustss	}
1091.11Saugustss
1101.14Saugustss	/* Disable interrupts, so we don't get any spurious ones. */
1111.14Saugustss	bus_space_write_4(sc->sc.iot, sc->sc.ioh, OHCI_INTERRUPT_DISABLE,
1121.14Saugustss			  OHCI_ALL_INTRS);
1131.1Saugustss
1141.14Saugustss	sc->sc_pc = pc;
1151.33Sjmcneill	sc->sc_tag = tag;
1161.14Saugustss	sc->sc.sc_bus.dmatag = pa->pa_dmat;
1171.1Saugustss
1181.1Saugustss	/* Enable the device. */
1191.17Saugustss	csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
1201.17Saugustss	pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG,
1211.1Saugustss		       csr | PCI_COMMAND_MASTER_ENABLE);
1221.1Saugustss
1231.1Saugustss	/* Map and establish the interrupt. */
1241.18Ssommerfe	if (pci_intr_map(pa, &ih)) {
1251.42Scegger		aprint_error_dev(self, "couldn't map interrupt\n");
1261.42Scegger		goto fail;
1271.1Saugustss	}
1281.42Scegger
1291.42Scegger	/*
1301.42Scegger	 * Allocate IRQ
1311.42Scegger	 */
1321.1Saugustss	intrstr = pci_intr_string(pc, ih);
1331.1Saugustss	sc->sc_ih = pci_intr_establish(pc, ih, IPL_USB, ohci_intr, sc);
1341.1Saugustss	if (sc->sc_ih == NULL) {
1351.42Scegger		aprint_error_dev(self, "couldn't establish interrupt");
1361.1Saugustss		if (intrstr != NULL)
1371.42Scegger			aprint_error(" at %s", intrstr);
1381.42Scegger		aprint_error("\n");
1391.42Scegger		goto fail;
1401.1Saugustss	}
1411.42Scegger	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
1421.1Saugustss
1431.1Saugustss	/* Figure out vendor for root hub descriptor. */
1441.1Saugustss	vendor = pci_findvendor(pa->pa_id);
1451.14Saugustss	sc->sc.sc_id_vendor = PCI_VENDOR(pa->pa_id);
1461.1Saugustss	if (vendor)
1471.24Sitojun		strlcpy(sc->sc.sc_vendor, vendor, sizeof(sc->sc.sc_vendor));
1481.1Saugustss	else
1491.24Sitojun		snprintf(sc->sc.sc_vendor, sizeof(sc->sc.sc_vendor),
1501.24Sitojun		    "vendor 0x%04x", PCI_VENDOR(pa->pa_id));
1511.27Sperry
1521.14Saugustss	r = ohci_init(&sc->sc);
1531.1Saugustss	if (r != USBD_NORMAL_COMPLETION) {
1541.42Scegger		aprint_error_dev(self, "init failed, error=%d\n", r);
1551.42Scegger		goto fail;
1561.1Saugustss	}
1571.1Saugustss
1581.19Saugustss#if NEHCI > 0
1591.37Sdrochner	usb_pci_add(&sc->sc_pci, pa, self);
1601.19Saugustss#endif
1611.19Saugustss
1621.35Sdyoung	if (!pmf_device_register1(self, ohci_suspend, ohci_resume,
1631.35Sdyoung	                          ohci_shutdown))
1641.33Sjmcneill		aprint_error_dev(self, "couldn't establish power handler\n");
1651.33Sjmcneill
1661.1Saugustss	/* Attach usb device. */
1671.36Sdyoung	sc->sc.sc_child = config_found(self, &sc->sc.sc_bus, usbctlprint);
1681.42Scegger	return;
1691.42Scegger
1701.42Sceggerfail:
1711.42Scegger	if (sc->sc_ih) {
1721.42Scegger		pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
1731.42Scegger		sc->sc_ih = NULL;
1741.42Scegger	}
1751.42Scegger	if (sc->sc.sc_size) {
1761.42Scegger		bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size);
1771.42Scegger		sc->sc.sc_size = 0;
1781.42Scegger	}
1791.42Scegger	return;
1801.14Saugustss}
1811.14Saugustss
1821.28Sthorpejstatic int
1831.41Sdyoungohci_pci_detach(device_t self, int flags)
1841.14Saugustss{
1851.36Sdyoung	struct ohci_pci_softc *sc = device_private(self);
1861.14Saugustss	int rv;
1871.14Saugustss
1881.42Scegger	pmf_device_deregister(self);
1891.14Saugustss	rv = ohci_detach(&sc->sc, flags);
1901.14Saugustss	if (rv)
1911.41Sdyoung		return rv;
1921.40Sdyoung
1931.42Scegger	if (sc->sc.sc_size) {
1941.42Scegger		/* Disable interrupts, so we don't get any spurious ones. */
1951.42Scegger		bus_space_write_4(sc->sc.iot, sc->sc.ioh,
1961.42Scegger				  OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS);
1971.42Scegger	}
1981.40Sdyoung
1991.15Saugustss	if (sc->sc_ih != NULL) {
2001.14Saugustss		pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
2011.15Saugustss		sc->sc_ih = NULL;
2021.14Saugustss	}
2031.15Saugustss	if (sc->sc.sc_size) {
2041.15Saugustss		bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size);
2051.15Saugustss		sc->sc.sc_size = 0;
2061.14Saugustss	}
2071.19Saugustss#if NEHCI > 0
2081.19Saugustss	usb_pci_rem(&sc->sc_pci);
2091.19Saugustss#endif
2101.41Sdyoung	return 0;
2111.1Saugustss}
2121.28Sthorpej
2131.37SdrochnerCFATTACH_DECL2_NEW(ohci_pci, sizeof(struct ohci_pci_softc),
2141.34Sdyoung    ohci_pci_match, ohci_pci_attach, ohci_pci_detach, ohci_activate, NULL,
2151.34Sdyoung    ohci_childdet);
216