Home | History | Annotate | Line # | Download | only in acpi
      1  1.7  jmcneill /* $NetBSD: wb_acpi.c,v 1.7 2020/12/07 10:02:51 jmcneill Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*
      4  1.1  jmcneill  * Copyright (c) 2009 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  1.1  jmcneill  * All rights reserved.
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8  1.1  jmcneill  * modification, are permitted provided that the following conditions
      9  1.1  jmcneill  * are met:
     10  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jmcneill  * 2. The name of the author may not be used to endorse or promote products
     13  1.1  jmcneill  *    derived from this software without specific prior written permission.
     14  1.1  jmcneill  *
     15  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17  1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18  1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19  1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     20  1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     21  1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     22  1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     23  1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  1.1  jmcneill  * SUCH DAMAGE.
     26  1.1  jmcneill  */
     27  1.1  jmcneill 
     28  1.1  jmcneill #include <sys/cdefs.h>
     29  1.7  jmcneill __KERNEL_RCSID(0, "$NetBSD: wb_acpi.c,v 1.7 2020/12/07 10:02:51 jmcneill Exp $");
     30  1.1  jmcneill 
     31  1.1  jmcneill #include <sys/param.h>
     32  1.2    jruoho #include <sys/device.h>
     33  1.1  jmcneill #include <sys/systm.h>
     34  1.1  jmcneill 
     35  1.5  jmcneill #include <dev/acpi/acpireg.h>
     36  1.2    jruoho #include <dev/acpi/acpivar.h>
     37  1.6  jmcneill #include <dev/acpi/acpi_intr.h>
     38  1.2    jruoho 
     39  1.2    jruoho #include <dev/ic/w83l518dvar.h>
     40  1.2    jruoho #include <dev/ic/w83l518dreg.h>
     41  1.1  jmcneill 
     42  1.1  jmcneill #include <dev/isa/isadmavar.h>
     43  1.1  jmcneill 
     44  1.1  jmcneill #include <dev/sdmmc/sdmmcvar.h>
     45  1.1  jmcneill 
     46  1.4  jmcneill #define _COMPONENT	ACPI_RESOURCE_COMPONENT
     47  1.4  jmcneill ACPI_MODULE_NAME	("wb_acpi")
     48  1.4  jmcneill 
     49  1.1  jmcneill static int	wb_acpi_match(device_t, cfdata_t, void *);
     50  1.1  jmcneill static void	wb_acpi_attach(device_t, device_t, void *);
     51  1.1  jmcneill static int	wb_acpi_detach(device_t, int);
     52  1.3  jmcneill static bool	wb_acpi_suspend(device_t, const pmf_qual_t *);
     53  1.3  jmcneill static bool	wb_acpi_resume(device_t, const pmf_qual_t *);
     54  1.1  jmcneill 
     55  1.1  jmcneill struct wb_acpi_softc {
     56  1.1  jmcneill 	struct wb_softc sc_wb;
     57  1.1  jmcneill 	isa_chipset_tag_t sc_ic;
     58  1.1  jmcneill 	void *sc_ih;
     59  1.1  jmcneill 	int sc_ioh_length;
     60  1.3  jmcneill 
     61  1.3  jmcneill 	ACPI_HANDLE sc_crs, sc_srs;
     62  1.3  jmcneill 	ACPI_BUFFER sc_crs_buffer;
     63  1.1  jmcneill };
     64  1.1  jmcneill 
     65  1.1  jmcneill CFATTACH_DECL_NEW(wb_acpi, sizeof(struct wb_acpi_softc),
     66  1.1  jmcneill     wb_acpi_match,
     67  1.1  jmcneill     wb_acpi_attach,
     68  1.1  jmcneill     wb_acpi_detach,
     69  1.1  jmcneill     NULL
     70  1.1  jmcneill );
     71  1.1  jmcneill 
     72  1.1  jmcneill static const char * const wb_acpi_ids[] = {
     73  1.1  jmcneill #if notyet
     74  1.1  jmcneill 	"WEC0515",	/* Memory Stick interface */
     75  1.1  jmcneill #endif
     76  1.1  jmcneill 	"WEC0517",	/* SD Memory Card interface */
     77  1.1  jmcneill 	NULL
     78  1.1  jmcneill };
     79  1.1  jmcneill 
     80  1.1  jmcneill static int
     81  1.1  jmcneill wb_acpi_match(device_t parent, cfdata_t match, void *opaque)
     82  1.1  jmcneill {
     83  1.1  jmcneill 	struct acpi_attach_args *aa = opaque;
     84  1.1  jmcneill 
     85  1.1  jmcneill 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
     86  1.1  jmcneill 		return 0;
     87  1.1  jmcneill 
     88  1.1  jmcneill 	return acpi_match_hid(aa->aa_node->ad_devinfo, wb_acpi_ids);
     89  1.1  jmcneill }
     90  1.1  jmcneill 
     91  1.1  jmcneill static void
     92  1.1  jmcneill wb_acpi_attach(device_t parent, device_t self, void *opaque)
     93  1.1  jmcneill {
     94  1.1  jmcneill 	struct wb_acpi_softc *sc = device_private(self);
     95  1.1  jmcneill 	struct acpi_attach_args *aa = opaque;
     96  1.1  jmcneill 	struct acpi_resources res;
     97  1.1  jmcneill 	struct acpi_io *io;
     98  1.1  jmcneill 	struct acpi_irq *irq;
     99  1.1  jmcneill 	bus_space_handle_t ioh;
    100  1.1  jmcneill 	ACPI_STATUS rv;
    101  1.1  jmcneill 
    102  1.1  jmcneill 	sc->sc_ic = aa->aa_ic;
    103  1.1  jmcneill 
    104  1.1  jmcneill 	rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
    105  1.1  jmcneill 	    &res, &acpi_resource_parse_ops_default);
    106  1.1  jmcneill 	if (ACPI_FAILURE(rv))
    107  1.1  jmcneill 		return;
    108  1.1  jmcneill 
    109  1.3  jmcneill 	AcpiGetHandle(aa->aa_node->ad_handle, "_CRS", &sc->sc_crs);
    110  1.3  jmcneill 	AcpiGetHandle(aa->aa_node->ad_handle, "_SRS", &sc->sc_srs);
    111  1.3  jmcneill 	if (sc->sc_crs && sc->sc_srs) {
    112  1.3  jmcneill 		sc->sc_crs_buffer.Pointer = NULL;
    113  1.3  jmcneill 		sc->sc_crs_buffer.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
    114  1.3  jmcneill 		rv = AcpiGetCurrentResources(sc->sc_crs, &sc->sc_crs_buffer);
    115  1.3  jmcneill 		if (ACPI_FAILURE(rv))
    116  1.3  jmcneill 			sc->sc_crs = sc->sc_srs = NULL;
    117  1.3  jmcneill 	}
    118  1.3  jmcneill 
    119  1.1  jmcneill 	io = acpi_res_io(&res, 0);
    120  1.1  jmcneill 	irq = acpi_res_irq(&res, 0);
    121  1.1  jmcneill 	if (io == NULL || irq == NULL) {
    122  1.1  jmcneill 		aprint_error_dev(self, "incomplete resources\n");
    123  1.1  jmcneill 		goto cleanup;
    124  1.1  jmcneill 	}
    125  1.1  jmcneill 
    126  1.1  jmcneill 	if (bus_space_map(aa->aa_iot, io->ar_base, io->ar_length, 0, &ioh)) {
    127  1.1  jmcneill 		aprint_error_dev(self, "couldn't map registers\n");
    128  1.1  jmcneill 		goto cleanup;
    129  1.1  jmcneill 	}
    130  1.1  jmcneill 	sc->sc_ioh_length = io->ar_length;
    131  1.1  jmcneill 
    132  1.7  jmcneill 	sc->sc_ih = acpi_intr_establish(self,
    133  1.7  jmcneill 	    (uint64_t)(uintptr_t)aa->aa_node->ad_handle,
    134  1.6  jmcneill 	    IPL_SDMMC, false, wb_intr, &sc->sc_wb, device_xname(self));
    135  1.1  jmcneill 	if (sc->sc_ih == NULL) {
    136  1.1  jmcneill 		aprint_error_dev(self,
    137  1.1  jmcneill 		    "couldn't establish interrupt handler\n");
    138  1.1  jmcneill 		goto cleanup;
    139  1.1  jmcneill 	}
    140  1.1  jmcneill 
    141  1.1  jmcneill 	sc->sc_wb.wb_dev = self;
    142  1.1  jmcneill 	sc->sc_wb.wb_type = WB_DEVNO_SD;
    143  1.1  jmcneill 	sc->sc_wb.wb_iot = aa->aa_iot;
    144  1.1  jmcneill 	sc->sc_wb.wb_ioh = ioh;
    145  1.1  jmcneill 	sc->sc_wb.wb_irq = irq->ar_irq;
    146  1.1  jmcneill 	sc->sc_wb.wb_base = io->ar_base;
    147  1.1  jmcneill 	wb_attach(&sc->sc_wb);
    148  1.3  jmcneill 
    149  1.3  jmcneill 	pmf_device_register(self, wb_acpi_suspend, wb_acpi_resume);
    150  1.1  jmcneill 
    151  1.1  jmcneill cleanup:
    152  1.1  jmcneill 	acpi_resource_cleanup(&res);
    153  1.1  jmcneill }
    154  1.1  jmcneill 
    155  1.1  jmcneill static int
    156  1.1  jmcneill wb_acpi_detach(device_t self, int flags)
    157  1.1  jmcneill {
    158  1.1  jmcneill 	struct wb_acpi_softc *sc = device_private(self);
    159  1.1  jmcneill 	int rv;
    160  1.1  jmcneill 
    161  1.3  jmcneill 	pmf_device_deregister(self);
    162  1.3  jmcneill 
    163  1.3  jmcneill 	if (sc->sc_crs_buffer.Pointer)
    164  1.3  jmcneill 		ACPI_FREE(sc->sc_crs_buffer.Pointer);
    165  1.3  jmcneill 
    166  1.1  jmcneill 	rv = wb_detach(&sc->sc_wb, flags);
    167  1.1  jmcneill 	if (rv)
    168  1.1  jmcneill 		return rv;
    169  1.1  jmcneill 
    170  1.1  jmcneill 	if (sc->sc_ih)
    171  1.6  jmcneill 		acpi_intr_disestablish(sc->sc_ih);
    172  1.1  jmcneill 
    173  1.1  jmcneill 	if (sc->sc_ioh_length > 0)
    174  1.1  jmcneill 		bus_space_unmap(sc->sc_wb.wb_iot, sc->sc_wb.wb_ioh,
    175  1.1  jmcneill 		    sc->sc_ioh_length);
    176  1.1  jmcneill 
    177  1.1  jmcneill 	return 0;
    178  1.1  jmcneill }
    179  1.3  jmcneill 
    180  1.3  jmcneill static bool
    181  1.3  jmcneill wb_acpi_suspend(device_t self, const pmf_qual_t *qual)
    182  1.3  jmcneill {
    183  1.3  jmcneill 	struct wb_acpi_softc *sc = device_private(self);
    184  1.3  jmcneill 
    185  1.3  jmcneill 	return wb_suspend(&sc->sc_wb);
    186  1.3  jmcneill }
    187  1.3  jmcneill 
    188  1.3  jmcneill static bool
    189  1.3  jmcneill wb_acpi_resume(device_t self, const pmf_qual_t *qual)
    190  1.3  jmcneill {
    191  1.3  jmcneill 	struct wb_acpi_softc *sc = device_private(self);
    192  1.3  jmcneill 	ACPI_STATUS rv;
    193  1.3  jmcneill 
    194  1.3  jmcneill 	if (sc->sc_crs && sc->sc_srs) {
    195  1.3  jmcneill 		rv = AcpiSetCurrentResources(sc->sc_srs, &sc->sc_crs_buffer);
    196  1.3  jmcneill 		if (ACPI_FAILURE(rv))
    197  1.3  jmcneill 			printf("%s: _SRS failed: %s\n",
    198  1.3  jmcneill 			    device_xname(self), AcpiFormatException(rv));
    199  1.3  jmcneill 	}
    200  1.3  jmcneill 
    201  1.3  jmcneill 	return wb_resume(&sc->sc_wb);
    202  1.3  jmcneill }
    203