ohci_pci.c revision 1.36
11.36Sdyoung/* $NetBSD: ohci_pci.c,v 1.36 2008/03/07 21:48:46 dyoung 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 * 3. All advertising materials mentioning features or use of this software 201.1Saugustss * must display the following acknowledgement: 211.1Saugustss * This product includes software developed by the NetBSD 221.1Saugustss * Foundation, Inc. and its contributors. 231.1Saugustss * 4. Neither the name of The NetBSD Foundation nor the names of its 241.1Saugustss * contributors may be used to endorse or promote products derived 251.1Saugustss * from this software without specific prior written permission. 261.1Saugustss * 271.1Saugustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 281.1Saugustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 291.1Saugustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 301.1Saugustss * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 311.1Saugustss * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 321.1Saugustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 331.1Saugustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 341.1Saugustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 351.1Saugustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 361.1Saugustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 371.1Saugustss * POSSIBILITY OF SUCH DAMAGE. 381.1Saugustss */ 391.20Slukem 401.20Slukem#include <sys/cdefs.h> 411.36Sdyoung__KERNEL_RCSID(0, "$NetBSD: ohci_pci.c,v 1.36 2008/03/07 21:48:46 dyoung Exp $"); 421.1Saugustss 431.19Saugustss#include "ehci.h" 441.19Saugustss 451.1Saugustss#include <sys/param.h> 461.1Saugustss#include <sys/systm.h> 471.1Saugustss#include <sys/kernel.h> 481.1Saugustss#include <sys/device.h> 491.1Saugustss#include <sys/proc.h> 501.1Saugustss#include <sys/queue.h> 511.1Saugustss 521.32Sad#include <sys/bus.h> 531.3Saugustss 541.1Saugustss#include <dev/pci/pcivar.h> 551.19Saugustss#include <dev/pci/usb_pci.h> 561.1Saugustss 571.1Saugustss#include <dev/usb/usb.h> 581.1Saugustss#include <dev/usb/usbdi.h> 591.1Saugustss#include <dev/usb/usbdivar.h> 601.3Saugustss#include <dev/usb/usb_mem.h> 611.3Saugustss 621.1Saugustss#include <dev/usb/ohcireg.h> 631.1Saugustss#include <dev/usb/ohcivar.h> 641.1Saugustss 651.14Saugustssstruct ohci_pci_softc { 661.14Saugustss ohci_softc_t sc; 671.19Saugustss#if NEHCI > 0 681.19Saugustss struct usb_pci sc_pci; 691.19Saugustss#endif 701.14Saugustss pci_chipset_tag_t sc_pc; 711.33Sjmcneill pcitag_t sc_tag; 721.14Saugustss void *sc_ih; /* interrupt vectoring */ 731.14Saugustss}; 741.1Saugustss 751.28Sthorpejstatic int 761.36Sdyoungohci_pci_match(device_t parent, struct cfdata *match, void *aux) 771.1Saugustss{ 781.1Saugustss struct pci_attach_args *pa = (struct pci_attach_args *) aux; 791.1Saugustss 801.4Saugustss if (PCI_CLASS(pa->pa_class) == PCI_CLASS_SERIALBUS && 811.4Saugustss PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_SERIALBUS_USB && 821.4Saugustss PCI_INTERFACE(pa->pa_class) == PCI_INTERFACE_OHCI) 831.14Saugustss return (1); 841.27Sperry 851.14Saugustss return (0); 861.1Saugustss} 871.1Saugustss 881.28Sthorpejstatic void 891.36Sdyoungohci_pci_attach(device_t parent, device_t self, void *aux) 901.1Saugustss{ 911.36Sdyoung struct ohci_pci_softc *sc = device_private(self); 921.1Saugustss struct pci_attach_args *pa = (struct pci_attach_args *)aux; 931.1Saugustss pci_chipset_tag_t pc = pa->pa_pc; 941.17Saugustss pcitag_t tag = pa->pa_tag; 951.1Saugustss char const *intrstr; 961.1Saugustss pci_intr_handle_t ih; 971.1Saugustss pcireg_t csr; 981.1Saugustss char devinfo[256]; 991.1Saugustss usbd_status r; 1001.26Smycroft const char *vendor; 1011.26Smycroft const char *devname = sc->sc.sc_bus.bdev.dv_xname; 1021.1Saugustss 1031.25Sitojun pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo)); 1041.1Saugustss printf(": %s (rev. 0x%02x)\n", devinfo, PCI_REVISION(pa->pa_class)); 1051.10Saugustss 1061.1Saugustss /* Map I/O registers */ 1071.1Saugustss if (pci_mapreg_map(pa, PCI_CBMEM, PCI_MAPREG_TYPE_MEM, 0, 1081.15Saugustss &sc->sc.iot, &sc->sc.ioh, NULL, &sc->sc.sc_size)) { 1091.14Saugustss printf("%s: can't map mem space\n", devname); 1101.1Saugustss return; 1111.1Saugustss } 1121.11Saugustss 1131.14Saugustss /* Disable interrupts, so we don't get any spurious ones. */ 1141.14Saugustss bus_space_write_4(sc->sc.iot, sc->sc.ioh, OHCI_INTERRUPT_DISABLE, 1151.14Saugustss OHCI_ALL_INTRS); 1161.1Saugustss 1171.14Saugustss sc->sc_pc = pc; 1181.33Sjmcneill sc->sc_tag = tag; 1191.14Saugustss sc->sc.sc_bus.dmatag = pa->pa_dmat; 1201.1Saugustss 1211.1Saugustss /* Enable the device. */ 1221.17Saugustss csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG); 1231.17Saugustss pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, 1241.1Saugustss csr | PCI_COMMAND_MASTER_ENABLE); 1251.1Saugustss 1261.1Saugustss /* Map and establish the interrupt. */ 1271.18Ssommerfe if (pci_intr_map(pa, &ih)) { 1281.14Saugustss printf("%s: couldn't map interrupt\n", devname); 1291.1Saugustss return; 1301.1Saugustss } 1311.1Saugustss intrstr = pci_intr_string(pc, ih); 1321.1Saugustss sc->sc_ih = pci_intr_establish(pc, ih, IPL_USB, ohci_intr, sc); 1331.1Saugustss if (sc->sc_ih == NULL) { 1341.14Saugustss printf("%s: couldn't establish interrupt", devname); 1351.1Saugustss if (intrstr != NULL) 1361.1Saugustss printf(" at %s", intrstr); 1371.1Saugustss printf("\n"); 1381.1Saugustss return; 1391.1Saugustss } 1401.14Saugustss printf("%s: interrupting at %s\n", devname, intrstr); 1411.1Saugustss 1421.1Saugustss /* Figure out vendor for root hub descriptor. */ 1431.1Saugustss vendor = pci_findvendor(pa->pa_id); 1441.14Saugustss sc->sc.sc_id_vendor = PCI_VENDOR(pa->pa_id); 1451.1Saugustss if (vendor) 1461.24Sitojun strlcpy(sc->sc.sc_vendor, vendor, sizeof(sc->sc.sc_vendor)); 1471.1Saugustss else 1481.24Sitojun snprintf(sc->sc.sc_vendor, sizeof(sc->sc.sc_vendor), 1491.24Sitojun "vendor 0x%04x", PCI_VENDOR(pa->pa_id)); 1501.27Sperry 1511.14Saugustss r = ohci_init(&sc->sc); 1521.1Saugustss if (r != USBD_NORMAL_COMPLETION) { 1531.14Saugustss printf("%s: init failed, error=%d\n", devname, r); 1541.1Saugustss return; 1551.1Saugustss } 1561.1Saugustss 1571.19Saugustss#if NEHCI > 0 1581.19Saugustss usb_pci_add(&sc->sc_pci, pa, &sc->sc.sc_bus); 1591.19Saugustss#endif 1601.19Saugustss 1611.35Sdyoung if (!pmf_device_register1(self, ohci_suspend, ohci_resume, 1621.35Sdyoung ohci_shutdown)) 1631.33Sjmcneill aprint_error_dev(self, "couldn't establish power handler\n"); 1641.33Sjmcneill 1651.1Saugustss /* Attach usb device. */ 1661.36Sdyoung sc->sc.sc_child = config_found(self, &sc->sc.sc_bus, usbctlprint); 1671.14Saugustss} 1681.14Saugustss 1691.28Sthorpejstatic int 1701.17Saugustssohci_pci_detach(device_ptr_t self, int flags) 1711.14Saugustss{ 1721.36Sdyoung struct ohci_pci_softc *sc = device_private(self); 1731.14Saugustss int rv; 1741.14Saugustss 1751.14Saugustss rv = ohci_detach(&sc->sc, flags); 1761.14Saugustss if (rv) 1771.14Saugustss return (rv); 1781.15Saugustss if (sc->sc_ih != NULL) { 1791.14Saugustss pci_intr_disestablish(sc->sc_pc, sc->sc_ih); 1801.15Saugustss sc->sc_ih = NULL; 1811.14Saugustss } 1821.15Saugustss if (sc->sc.sc_size) { 1831.15Saugustss bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size); 1841.15Saugustss sc->sc.sc_size = 0; 1851.14Saugustss } 1861.19Saugustss#if NEHCI > 0 1871.19Saugustss usb_pci_rem(&sc->sc_pci); 1881.19Saugustss#endif 1891.14Saugustss return (0); 1901.1Saugustss} 1911.28Sthorpej 1921.34SdyoungCFATTACH_DECL2(ohci_pci, sizeof(struct ohci_pci_softc), 1931.34Sdyoung ohci_pci_match, ohci_pci_attach, ohci_pci_detach, ohci_activate, NULL, 1941.34Sdyoung ohci_childdet); 195