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