Home | History | Annotate | Line # | Download | only in acpi
ohci_acpi.c revision 1.1
      1 /* $NetBSD: ohci_acpi.c,v 1.1 2021/12/24 00:24:49 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2021 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jared McNeill <jmcneill (at) invisible.ca>.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: ohci_acpi.c,v 1.1 2021/12/24 00:24:49 jmcneill Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/bus.h>
     37 #include <sys/cpu.h>
     38 #include <sys/device.h>
     39 
     40 #include <dev/usb/usb.h>
     41 #include <dev/usb/usbdi.h>
     42 #include <dev/usb/usbdivar.h>
     43 #include <dev/usb/usb_mem.h>
     44 #include <dev/usb/ohcireg.h>
     45 #include <dev/usb/ohcivar.h>
     46 
     47 #include <dev/acpi/acpireg.h>
     48 #include <dev/acpi/acpivar.h>
     49 #include <dev/acpi/acpi_intr.h>
     50 #include <dev/acpi/acpi_usb.h>
     51 
     52 #include <dev/pci/pcireg.h>
     53 
     54 static int	ohci_acpi_match(device_t, cfdata_t, void *);
     55 static void	ohci_acpi_attach(device_t, device_t, void *);
     56 
     57 CFATTACH_DECL2_NEW(ohci_acpi, sizeof(struct ohci_softc),
     58 	ohci_acpi_match, ohci_acpi_attach, NULL,
     59 	ohci_activate, NULL, ohci_childdet);
     60 
     61 static int
     62 ohci_acpi_match(device_t parent, cfdata_t cf, void *aux)
     63 {
     64 	struct acpi_attach_args *aa = aux;
     65 
     66 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE) {
     67 		return 0;
     68 	}
     69 
     70 	return acpi_match_class(aa->aa_node->ad_handle,
     71 	    PCI_CLASS_SERIALBUS,
     72 	    PCI_SUBCLASS_SERIALBUS_USB,
     73 	    PCI_INTERFACE_OHCI);
     74 }
     75 
     76 static void
     77 ohci_acpi_attach(device_t parent, device_t self, void *aux)
     78 {
     79 	struct ohci_softc * const sc = device_private(self);
     80 	struct acpi_attach_args *aa = aux;
     81 	struct acpi_resources res;
     82 	struct acpi_mem *mem;
     83 	struct acpi_irq *irq;
     84 	ACPI_STATUS rv;
     85 	int error;
     86 	void *ih;
     87 
     88 	sc->sc_dev = self;
     89 	sc->sc_bus.ub_hcpriv = sc;
     90 	sc->sc_bus.ub_dmatag = aa->aa_dmat;
     91 	sc->sc_bus.ub_revision = USBREV_1_0;
     92 
     93 	rv = acpi_resource_parse(sc->sc_dev, aa->aa_node->ad_handle, "_CRS",
     94 	    &res, &acpi_resource_parse_ops_default);
     95 	if (ACPI_FAILURE(rv)) {
     96 		return;
     97 	}
     98 
     99 	mem = acpi_res_mem(&res, 0);
    100 	if (mem == NULL) {
    101 		aprint_error_dev(self, "couldn't find mem resource\n");
    102 		goto done;
    103 	}
    104 
    105 	irq = acpi_res_irq(&res, 0);
    106 	if (irq == NULL) {
    107 		aprint_error_dev(self, "couldn't find irq resource\n");
    108 		goto done;
    109 	}
    110 
    111 	sc->sc_size = mem->ar_length;
    112 	sc->iot = aa->aa_memt;
    113 	error = bus_space_map(sc->iot, mem->ar_base, mem->ar_length, 0, &sc->ioh);
    114 	if (error) {
    115 		aprint_error_dev(self, "couldn't map registers\n");
    116 		goto done;
    117 	}
    118 
    119 	/* Disable interrupts */
    120 	bus_space_write_4(sc->iot, sc->ioh, OHCI_INTERRUPT_DISABLE,
    121 	    OHCI_ALL_INTRS);
    122 
    123 	ih = acpi_intr_establish(self,
    124 	    (uint64_t)(uintptr_t)aa->aa_node->ad_handle,
    125 	    IPL_USB, true, ohci_intr, sc, device_xname(self));
    126 	if (ih == NULL) {
    127 		aprint_error_dev(self, "couldn't establish interrupt\n");
    128 		goto done;
    129 	}
    130 
    131 	error = ohci_init(sc);
    132 	if (error) {
    133 		aprint_error_dev(self, "init failed, error = %d\n", error);
    134 		acpi_intr_disestablish(ih);
    135 		goto done;
    136 	}
    137 
    138 	sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint, CFARGS_NONE);
    139 
    140 done:
    141 	acpi_resource_cleanup(&res);
    142 }
    143