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