ohci_pci.c revision 1.60
11.60Sriastrad/* $NetBSD: ohci_pci.c,v 1.60 2025/03/31 14:46:42 riastradh 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.60Sriastrad__KERNEL_RCSID(0, "$NetBSD: ohci_pci.c,v 1.60 2025/03/31 14:46:42 riastradh 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.51Sskrll#include <dev/pci/pcidevs.h> 491.19Saugustss#include <dev/pci/usb_pci.h> 501.1Saugustss 511.1Saugustss#include <dev/usb/usb.h> 521.1Saugustss#include <dev/usb/usbdi.h> 531.1Saugustss#include <dev/usb/usbdivar.h> 541.3Saugustss#include <dev/usb/usb_mem.h> 551.3Saugustss 561.1Saugustss#include <dev/usb/ohcireg.h> 571.1Saugustss#include <dev/usb/ohcivar.h> 581.1Saugustss 591.14Saugustssstruct ohci_pci_softc { 601.14Saugustss ohci_softc_t sc; 611.19Saugustss#if NEHCI > 0 621.19Saugustss struct usb_pci sc_pci; 631.19Saugustss#endif 641.14Saugustss pci_chipset_tag_t sc_pc; 651.33Sjmcneill pcitag_t sc_tag; 661.14Saugustss void *sc_ih; /* interrupt vectoring */ 671.14Saugustss}; 681.1Saugustss 691.28Sthorpejstatic int 701.41Sdyoungohci_pci_match(device_t parent, cfdata_t match, void *aux) 711.1Saugustss{ 721.1Saugustss struct pci_attach_args *pa = (struct pci_attach_args *) aux; 731.1Saugustss 741.4Saugustss if (PCI_CLASS(pa->pa_class) == PCI_CLASS_SERIALBUS && 751.4Saugustss PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_SERIALBUS_USB && 761.4Saugustss PCI_INTERFACE(pa->pa_class) == PCI_INTERFACE_OHCI) 771.41Sdyoung return 1; 781.27Sperry 791.41Sdyoung return 0; 801.1Saugustss} 811.1Saugustss 821.28Sthorpejstatic void 831.36Sdyoungohci_pci_attach(device_t parent, device_t self, void *aux) 841.1Saugustss{ 851.36Sdyoung struct ohci_pci_softc *sc = device_private(self); 861.1Saugustss struct pci_attach_args *pa = (struct pci_attach_args *)aux; 871.1Saugustss pci_chipset_tag_t pc = pa->pa_pc; 881.17Saugustss pcitag_t tag = pa->pa_tag; 891.1Saugustss char const *intrstr; 901.1Saugustss pci_intr_handle_t ih; 911.1Saugustss pcireg_t csr; 921.52Schristos char intrbuf[PCI_INTRSTR_LEN]; 931.37Sdrochner 941.37Sdrochner sc->sc.sc_dev = self; 951.55Sskrll sc->sc.sc_bus.ub_hcpriv = sc; 961.1Saugustss 971.51Sskrll if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_NS && 981.51Sskrll PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_NS_USB) { 991.51Sskrll sc->sc.sc_flags = OHCIF_SUPERIO; 1001.51Sskrll } 1011.51Sskrll 1021.48Sdrochner pci_aprint_devinfo(pa, "USB Controller"); 1031.10Saugustss 1041.49Smacallan /* check if memory space access is enabled */ 1051.49Smacallan csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG); 1061.49Smacallan#ifdef DEBUG 1071.49Smacallan printf("csr: %08x\n", csr); 1081.49Smacallan#endif 1091.49Smacallan if ((csr & PCI_COMMAND_MEM_ENABLE) == 0) { 1101.49Smacallan aprint_error_dev(self, "memory access is disabled\n"); 1111.49Smacallan return; 1121.49Smacallan } 1131.49Smacallan 1141.1Saugustss /* Map I/O registers */ 1151.1Saugustss if (pci_mapreg_map(pa, PCI_CBMEM, PCI_MAPREG_TYPE_MEM, 0, 1161.15Saugustss &sc->sc.iot, &sc->sc.ioh, NULL, &sc->sc.sc_size)) { 1171.42Scegger sc->sc.sc_size = 0; 1181.42Scegger aprint_error_dev(self, "can't map mem space\n"); 1191.1Saugustss return; 1201.1Saugustss } 1211.11Saugustss 1221.14Saugustss /* Disable interrupts, so we don't get any spurious ones. */ 1231.14Saugustss bus_space_write_4(sc->sc.iot, sc->sc.ioh, OHCI_INTERRUPT_DISABLE, 1241.14Saugustss OHCI_ALL_INTRS); 1251.1Saugustss 1261.14Saugustss sc->sc_pc = pc; 1271.33Sjmcneill sc->sc_tag = tag; 1281.55Sskrll sc->sc.sc_bus.ub_dmatag = pa->pa_dmat; 1291.1Saugustss 1301.1Saugustss /* Enable the device. */ 1311.17Saugustss pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, 1321.1Saugustss csr | PCI_COMMAND_MASTER_ENABLE); 1331.1Saugustss 1341.1Saugustss /* Map and establish the interrupt. */ 1351.18Ssommerfe if (pci_intr_map(pa, &ih)) { 1361.42Scegger aprint_error_dev(self, "couldn't map interrupt\n"); 1371.42Scegger goto fail; 1381.1Saugustss } 1391.42Scegger 1401.42Scegger /* 1411.42Scegger * Allocate IRQ 1421.42Scegger */ 1431.52Schristos intrstr = pci_intr_string(pc, ih, intrbuf, sizeof(intrbuf)); 1441.56Smsaitoh sc->sc_ih = pci_intr_establish_xname(pc, ih, IPL_USB, ohci_intr, sc, 1451.56Smsaitoh device_xname(self)); 1461.1Saugustss if (sc->sc_ih == NULL) { 1471.42Scegger aprint_error_dev(self, "couldn't establish interrupt"); 1481.1Saugustss if (intrstr != NULL) 1491.42Scegger aprint_error(" at %s", intrstr); 1501.42Scegger aprint_error("\n"); 1511.42Scegger goto fail; 1521.1Saugustss } 1531.42Scegger aprint_normal_dev(self, "interrupting at %s\n", intrstr); 1541.1Saugustss 1551.55Sskrll int err = ohci_init(&sc->sc); 1561.55Sskrll if (err) { 1571.55Sskrll aprint_error_dev(self, "init failed, error=%d\n", err); 1581.42Scegger goto fail; 1591.1Saugustss } 1601.1Saugustss 1611.19Saugustss#if NEHCI > 0 1621.37Sdrochner usb_pci_add(&sc->sc_pci, pa, self); 1631.19Saugustss#endif 1641.19Saugustss 1651.35Sdyoung if (!pmf_device_register1(self, ohci_suspend, ohci_resume, 1661.35Sdyoung ohci_shutdown)) 1671.33Sjmcneill aprint_error_dev(self, "couldn't establish power handler\n"); 1681.33Sjmcneill 1691.1Saugustss /* Attach usb device. */ 1701.58Sthorpej sc->sc.sc_child = config_found(self, &sc->sc.sc_bus, usbctlprint, 1711.59Sthorpej CFARGS_NONE); 1721.42Scegger return; 1731.42Scegger 1741.42Sceggerfail: 1751.42Scegger if (sc->sc_ih) { 1761.42Scegger pci_intr_disestablish(sc->sc_pc, sc->sc_ih); 1771.42Scegger sc->sc_ih = NULL; 1781.42Scegger } 1791.42Scegger if (sc->sc.sc_size) { 1801.42Scegger bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size); 1811.42Scegger sc->sc.sc_size = 0; 1821.42Scegger } 1831.42Scegger return; 1841.14Saugustss} 1851.14Saugustss 1861.28Sthorpejstatic int 1871.41Sdyoungohci_pci_detach(device_t self, int flags) 1881.14Saugustss{ 1891.36Sdyoung struct ohci_pci_softc *sc = device_private(self); 1901.60Sriastrad int error; 1911.14Saugustss 1921.60Sriastrad /* 1931.60Sriastrad * Detach the USB child first. Disconnects all USB devices and 1941.60Sriastrad * prevents connecting new ones. 1951.60Sriastrad */ 1961.60Sriastrad error = config_detach_children(self, flags); 1971.60Sriastrad if (error) 1981.60Sriastrad return error; 1991.40Sdyoung 2001.60Sriastrad /* 2011.60Sriastrad * Stop listing this as a possible companion controller for 2021.60Sriastrad * ehci(4). 2031.60Sriastrad */ 2041.60Sriastrad#if NEHCI > 0 2051.60Sriastrad usb_pci_rem(&sc->sc_pci); 2061.60Sriastrad#endif 2071.60Sriastrad 2081.60Sriastrad /* 2091.60Sriastrad * Shut down the controller and block interrupts at the device 2101.60Sriastrad * level. Once we have shut down the controller, the shutdown 2111.60Sriastrad * handler no longer needed -- deregister it from PMF. 2121.60Sriastrad * (Harmless to call ohci_shutdown more than once, so no 2131.60Sriastrad * synchronization needed.) 2141.60Sriastrad */ 2151.60Sriastrad ohci_shutdown(self, 0); 2161.47Sdyoung pmf_device_deregister(self); 2171.47Sdyoung 2181.60Sriastrad /* 2191.60Sriastrad * Interrupts are blocked at the device level by ohci_shutdown. 2201.60Sriastrad * Disestablish the interrupt handler. This waits for it to 2211.60Sriastrad * complete on all CPUs. 2221.60Sriastrad */ 2231.15Saugustss if (sc->sc_ih != NULL) { 2241.14Saugustss pci_intr_disestablish(sc->sc_pc, sc->sc_ih); 2251.15Saugustss sc->sc_ih = NULL; 2261.14Saugustss } 2271.60Sriastrad 2281.60Sriastrad /* 2291.60Sriastrad * Free the bus-independent ohci(4) state now that the 2301.60Sriastrad * interrupt handler has ceased to run on all CPUs. 2311.60Sriastrad */ 2321.60Sriastrad ohci_detach(&sc->sc); 2331.60Sriastrad 2341.60Sriastrad /* 2351.60Sriastrad * Unmap the registers now that we're all done with them. 2361.60Sriastrad */ 2371.15Saugustss if (sc->sc.sc_size) { 2381.15Saugustss bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size); 2391.15Saugustss sc->sc.sc_size = 0; 2401.14Saugustss } 2411.60Sriastrad 2421.41Sdyoung return 0; 2431.1Saugustss} 2441.28Sthorpej 2451.43SceggerCFATTACH_DECL3_NEW(ohci_pci, sizeof(struct ohci_pci_softc), 2461.34Sdyoung ohci_pci_match, ohci_pci_attach, ohci_pci_detach, ohci_activate, NULL, 2471.43Scegger ohci_childdet, DVF_DETACH_SHUTDOWN); 248