Home | History | Annotate | Line # | Download | only in acpi
sdhc_acpi.c revision 1.2.2.3
      1  1.2.2.3  skrll /*	$NetBSD: sdhc_acpi.c,v 1.2.2.3 2016/10/05 20:55:40 skrll Exp $	*/
      2  1.2.2.2  skrll 
      3  1.2.2.2  skrll /*
      4  1.2.2.2  skrll  * Copyright (c) 2016 Kimihiro Nonaka <nonaka (at) NetBSD.org>
      5  1.2.2.2  skrll  * All rights reserved.
      6  1.2.2.2  skrll  *
      7  1.2.2.2  skrll  * Redistribution and use in source and binary forms, with or without
      8  1.2.2.2  skrll  * modification, are permitted provided that the following conditions
      9  1.2.2.2  skrll  * are met:
     10  1.2.2.2  skrll  * 1. Redistributions of source code must retain the above copyright
     11  1.2.2.2  skrll  *    notice, this list of conditions and the following disclaimer.
     12  1.2.2.2  skrll  * 2. The name of the author may not be used to endorse or promote products
     13  1.2.2.2  skrll  *    derived from this software without specific prior written permission.
     14  1.2.2.2  skrll  *
     15  1.2.2.2  skrll  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  1.2.2.2  skrll  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17  1.2.2.2  skrll  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18  1.2.2.2  skrll  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19  1.2.2.2  skrll  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     20  1.2.2.2  skrll  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     21  1.2.2.2  skrll  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     22  1.2.2.2  skrll  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     23  1.2.2.2  skrll  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  1.2.2.2  skrll  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  1.2.2.2  skrll  * SUCH DAMAGE.
     26  1.2.2.2  skrll  */
     27  1.2.2.2  skrll 
     28  1.2.2.2  skrll #include <sys/cdefs.h>
     29  1.2.2.3  skrll __KERNEL_RCSID(0, "$NetBSD: sdhc_acpi.c,v 1.2.2.3 2016/10/05 20:55:40 skrll Exp $");
     30  1.2.2.2  skrll 
     31  1.2.2.2  skrll #include <sys/param.h>
     32  1.2.2.2  skrll #include <sys/device.h>
     33  1.2.2.2  skrll #include <sys/systm.h>
     34  1.2.2.2  skrll #include <sys/kmem.h>
     35  1.2.2.2  skrll 
     36  1.2.2.2  skrll #include <dev/acpi/acpireg.h>
     37  1.2.2.2  skrll #include <dev/acpi/acpivar.h>
     38  1.2.2.2  skrll 
     39  1.2.2.2  skrll #include <dev/sdmmc/sdhcreg.h>
     40  1.2.2.2  skrll #include <dev/sdmmc/sdhcvar.h>
     41  1.2.2.2  skrll #include <dev/sdmmc/sdmmcvar.h>
     42  1.2.2.2  skrll 
     43  1.2.2.2  skrll #define _COMPONENT	ACPI_RESOURCE_COMPONENT
     44  1.2.2.2  skrll ACPI_MODULE_NAME	("sdhc_acpi")
     45  1.2.2.2  skrll 
     46  1.2.2.2  skrll static int	sdhc_acpi_match(device_t, cfdata_t, void *);
     47  1.2.2.2  skrll static void	sdhc_acpi_attach(device_t, device_t, void *);
     48  1.2.2.2  skrll static int	sdhc_acpi_detach(device_t, int);
     49  1.2.2.2  skrll static bool	sdhc_acpi_resume(device_t, const pmf_qual_t *);
     50  1.2.2.2  skrll 
     51  1.2.2.2  skrll struct sdhc_acpi_softc {
     52  1.2.2.2  skrll 	struct sdhc_softc sc;
     53  1.2.2.2  skrll 	int sc_irq;
     54  1.2.2.2  skrll 
     55  1.2.2.2  skrll 	ACPI_HANDLE sc_crs, sc_srs;
     56  1.2.2.2  skrll 	ACPI_BUFFER sc_crs_buffer;
     57  1.2.2.2  skrll };
     58  1.2.2.2  skrll 
     59  1.2.2.2  skrll CFATTACH_DECL_NEW(sdhc_acpi, sizeof(struct sdhc_acpi_softc),
     60  1.2.2.2  skrll     sdhc_acpi_match, sdhc_acpi_attach, sdhc_acpi_detach, NULL);
     61  1.2.2.2  skrll 
     62  1.2.2.2  skrll static uint32_t sdhc_acpi_intr(void *);
     63  1.2.2.2  skrll 
     64  1.2.2.2  skrll static const char * const sdhc_acpi_ids[] = {
     65  1.2.2.2  skrll 	"80860F14",
     66  1.2.2.2  skrll 	"80860F16",
     67  1.2.2.2  skrll 	NULL
     68  1.2.2.2  skrll };
     69  1.2.2.2  skrll 
     70  1.2.2.2  skrll static int
     71  1.2.2.2  skrll sdhc_acpi_match(device_t parent, cfdata_t match, void *opaque)
     72  1.2.2.2  skrll {
     73  1.2.2.2  skrll 	struct acpi_attach_args *aa = opaque;
     74  1.2.2.2  skrll 
     75  1.2.2.2  skrll 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
     76  1.2.2.2  skrll 		return 0;
     77  1.2.2.2  skrll 
     78  1.2.2.2  skrll 	return acpi_match_hid(aa->aa_node->ad_devinfo, sdhc_acpi_ids);
     79  1.2.2.2  skrll }
     80  1.2.2.2  skrll 
     81  1.2.2.2  skrll static void
     82  1.2.2.2  skrll sdhc_acpi_attach(device_t parent, device_t self, void *opaque)
     83  1.2.2.2  skrll {
     84  1.2.2.2  skrll 	struct sdhc_acpi_softc *sc = device_private(self);
     85  1.2.2.2  skrll 	struct acpi_attach_args *aa = opaque;
     86  1.2.2.2  skrll 	struct acpi_resources res;
     87  1.2.2.2  skrll 	struct acpi_mem *mem;
     88  1.2.2.2  skrll 	struct acpi_irq *irq;
     89  1.2.2.2  skrll 	bus_space_handle_t memh;
     90  1.2.2.2  skrll 	ACPI_STATUS rv;
     91  1.2.2.2  skrll 
     92  1.2.2.2  skrll 	sc->sc.sc_dev = self;
     93  1.2.2.2  skrll 	sc->sc.sc_dmat = aa->aa_dmat;
     94  1.2.2.2  skrll 	sc->sc.sc_host = NULL;
     95  1.2.2.2  skrll 	sc->sc_irq = -1;
     96  1.2.2.2  skrll 
     97  1.2.2.2  skrll 	rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
     98  1.2.2.2  skrll 	    &res, &acpi_resource_parse_ops_default);
     99  1.2.2.2  skrll 	if (ACPI_FAILURE(rv))
    100  1.2.2.2  skrll 		return;
    101  1.2.2.2  skrll 
    102  1.2.2.2  skrll 	AcpiGetHandle(aa->aa_node->ad_handle, "_CRS", &sc->sc_crs);
    103  1.2.2.2  skrll 	AcpiGetHandle(aa->aa_node->ad_handle, "_SRS", &sc->sc_srs);
    104  1.2.2.2  skrll 	if (sc->sc_crs && sc->sc_srs) {
    105  1.2.2.2  skrll 		/* XXX Why need this? */
    106  1.2.2.2  skrll 		sc->sc_crs_buffer.Pointer = NULL;
    107  1.2.2.2  skrll 		sc->sc_crs_buffer.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
    108  1.2.2.2  skrll 		rv = AcpiGetCurrentResources(sc->sc_crs, &sc->sc_crs_buffer);
    109  1.2.2.2  skrll 		if (ACPI_FAILURE(rv))
    110  1.2.2.2  skrll 			sc->sc_crs = sc->sc_srs = NULL;
    111  1.2.2.2  skrll 	}
    112  1.2.2.2  skrll 
    113  1.2.2.2  skrll 	mem = acpi_res_mem(&res, 0);
    114  1.2.2.2  skrll 	irq = acpi_res_irq(&res, 0);
    115  1.2.2.2  skrll 	if (mem == NULL || irq == NULL) {
    116  1.2.2.2  skrll 		aprint_error_dev(self, "incomplete resources\n");
    117  1.2.2.2  skrll 		goto cleanup;
    118  1.2.2.2  skrll 	}
    119  1.2.2.2  skrll 
    120  1.2.2.2  skrll 	if (bus_space_map(aa->aa_memt, mem->ar_base, mem->ar_length, 0,
    121  1.2.2.2  skrll 	    &memh)) {
    122  1.2.2.2  skrll 		aprint_error_dev(self, "couldn't map registers\n");
    123  1.2.2.2  skrll 		goto cleanup;
    124  1.2.2.2  skrll 	}
    125  1.2.2.2  skrll 
    126  1.2.2.2  skrll 	/* XXX acpi_intr_establish? */
    127  1.2.2.2  skrll 	rv = AcpiOsInstallInterruptHandler(irq->ar_irq, sdhc_acpi_intr, sc);
    128  1.2.2.2  skrll 	if (ACPI_FAILURE(rv)) {
    129  1.2.2.2  skrll 		aprint_error_dev(self,
    130  1.2.2.2  skrll 		    "couldn't establish interrupt handler\n");
    131  1.2.2.2  skrll 		goto cleanup;
    132  1.2.2.2  skrll 	}
    133  1.2.2.2  skrll 	sc->sc_irq = irq->ar_irq;
    134  1.2.2.2  skrll 
    135  1.2.2.2  skrll 	sc->sc.sc_host = kmem_zalloc(sizeof(struct sdhc_host *), KM_NOSLEEP);
    136  1.2.2.2  skrll 	if (sc->sc.sc_host == NULL) {
    137  1.2.2.2  skrll 		aprint_error_dev(self, "couldn't alloc memory\n");
    138  1.2.2.2  skrll 		goto cleanup;
    139  1.2.2.2  skrll 	}
    140  1.2.2.2  skrll 
    141  1.2.2.3  skrll 	/* Enable DMA transfer */
    142  1.2.2.3  skrll 	sc->sc.sc_flags |= SDHC_FLAG_USE_DMA;
    143  1.2.2.3  skrll 
    144  1.2.2.2  skrll 	if (sdhc_host_found(&sc->sc, aa->aa_memt, memh, mem->ar_length) != 0) {
    145  1.2.2.2  skrll 		aprint_error_dev(self, "couldn't initialize host\n");
    146  1.2.2.2  skrll 		goto cleanup;
    147  1.2.2.2  skrll 	}
    148  1.2.2.2  skrll 
    149  1.2.2.2  skrll 	if (!pmf_device_register1(self, sdhc_suspend, sdhc_acpi_resume,
    150  1.2.2.2  skrll 	    sdhc_shutdown)) {
    151  1.2.2.2  skrll 		aprint_error_dev(self, "couldn't establish powerhook\n");
    152  1.2.2.2  skrll 	}
    153  1.2.2.2  skrll 
    154  1.2.2.2  skrll 	acpi_resource_cleanup(&res);
    155  1.2.2.2  skrll 	return;
    156  1.2.2.2  skrll 
    157  1.2.2.2  skrll cleanup:
    158  1.2.2.2  skrll 	acpi_resource_cleanup(&res);
    159  1.2.2.2  skrll 	if (sc->sc_crs_buffer.Pointer)
    160  1.2.2.2  skrll 		ACPI_FREE(sc->sc_crs_buffer.Pointer);
    161  1.2.2.2  skrll 	if (sc->sc_irq >= 0)
    162  1.2.2.2  skrll 		/* XXX acpi_intr_disestablish? */
    163  1.2.2.2  skrll 		AcpiOsRemoveInterruptHandler(sc->sc_irq, sdhc_acpi_intr);
    164  1.2.2.2  skrll 	if (sc->sc.sc_host != NULL)
    165  1.2.2.2  skrll 		kmem_free(sc->sc.sc_host, sizeof(struct sdhc_host *));
    166  1.2.2.2  skrll }
    167  1.2.2.2  skrll 
    168  1.2.2.2  skrll static int
    169  1.2.2.2  skrll sdhc_acpi_detach(device_t self, int flags)
    170  1.2.2.2  skrll {
    171  1.2.2.2  skrll 	struct sdhc_acpi_softc *sc = device_private(self);
    172  1.2.2.2  skrll 	int rv;
    173  1.2.2.2  skrll 
    174  1.2.2.2  skrll 	pmf_device_deregister(self);
    175  1.2.2.2  skrll 
    176  1.2.2.2  skrll 	if (sc->sc_crs_buffer.Pointer)
    177  1.2.2.2  skrll 		ACPI_FREE(sc->sc_crs_buffer.Pointer);
    178  1.2.2.2  skrll 
    179  1.2.2.2  skrll 	rv = sdhc_detach(&sc->sc, flags);
    180  1.2.2.2  skrll 	if (rv)
    181  1.2.2.2  skrll 		return rv;
    182  1.2.2.2  skrll 
    183  1.2.2.2  skrll 	if (sc->sc_irq >= 0)
    184  1.2.2.2  skrll 		/* XXX acpi_intr_disestablish? */
    185  1.2.2.2  skrll 		AcpiOsRemoveInterruptHandler(sc->sc_irq, sdhc_acpi_intr);
    186  1.2.2.2  skrll 
    187  1.2.2.2  skrll 	if (sc->sc.sc_host != NULL)
    188  1.2.2.2  skrll 		kmem_free(sc->sc.sc_host, sizeof(struct sdhc_host *));
    189  1.2.2.2  skrll 
    190  1.2.2.2  skrll 	return 0;
    191  1.2.2.2  skrll }
    192  1.2.2.2  skrll 
    193  1.2.2.2  skrll static bool
    194  1.2.2.2  skrll sdhc_acpi_resume(device_t self, const pmf_qual_t *qual)
    195  1.2.2.2  skrll {
    196  1.2.2.2  skrll 	struct sdhc_acpi_softc *sc = device_private(self);
    197  1.2.2.2  skrll 	ACPI_STATUS rv;
    198  1.2.2.2  skrll 
    199  1.2.2.2  skrll 	if (sc->sc_crs && sc->sc_srs) {
    200  1.2.2.2  skrll 		rv = AcpiSetCurrentResources(sc->sc_srs, &sc->sc_crs_buffer);
    201  1.2.2.2  skrll 		if (ACPI_FAILURE(rv))
    202  1.2.2.2  skrll 			printf("%s: _SRS failed: %s\n",
    203  1.2.2.2  skrll 			    device_xname(self), AcpiFormatException(rv));
    204  1.2.2.2  skrll 	}
    205  1.2.2.2  skrll 
    206  1.2.2.2  skrll 	return sdhc_resume(self, qual);
    207  1.2.2.2  skrll }
    208  1.2.2.2  skrll 
    209  1.2.2.2  skrll static uint32_t
    210  1.2.2.2  skrll sdhc_acpi_intr(void *context)
    211  1.2.2.2  skrll {
    212  1.2.2.2  skrll 	struct sdhc_acpi_softc *sc = context;
    213  1.2.2.2  skrll 
    214  1.2.2.2  skrll 	if (!sdhc_intr(&sc->sc))
    215  1.2.2.2  skrll 		return ACPI_INTERRUPT_NOT_HANDLED;
    216  1.2.2.2  skrll 	return ACPI_INTERRUPT_HANDLED;
    217  1.2.2.2  skrll }
    218