Home | History | Annotate | Line # | Download | only in acpi
sdhc_acpi.c revision 1.2.4.1
      1  1.2.4.1  pgoyette /*	$NetBSD: sdhc_acpi.c,v 1.2.4.1 2017/03/20 06:57:27 pgoyette Exp $	*/
      2      1.1    nonaka 
      3      1.1    nonaka /*
      4      1.1    nonaka  * Copyright (c) 2016 Kimihiro Nonaka <nonaka (at) NetBSD.org>
      5      1.1    nonaka  * All rights reserved.
      6      1.1    nonaka  *
      7      1.1    nonaka  * Redistribution and use in source and binary forms, with or without
      8      1.1    nonaka  * modification, are permitted provided that the following conditions
      9      1.1    nonaka  * are met:
     10      1.1    nonaka  * 1. Redistributions of source code must retain the above copyright
     11      1.1    nonaka  *    notice, this list of conditions and the following disclaimer.
     12      1.1    nonaka  * 2. The name of the author may not be used to endorse or promote products
     13      1.1    nonaka  *    derived from this software without specific prior written permission.
     14      1.1    nonaka  *
     15      1.1    nonaka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16      1.1    nonaka  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17      1.1    nonaka  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18      1.1    nonaka  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19      1.1    nonaka  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     20      1.1    nonaka  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     21      1.1    nonaka  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     22      1.1    nonaka  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     23      1.1    nonaka  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24      1.1    nonaka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25      1.1    nonaka  * SUCH DAMAGE.
     26      1.1    nonaka  */
     27      1.1    nonaka 
     28      1.1    nonaka #include <sys/cdefs.h>
     29  1.2.4.1  pgoyette __KERNEL_RCSID(0, "$NetBSD: sdhc_acpi.c,v 1.2.4.1 2017/03/20 06:57:27 pgoyette Exp $");
     30      1.1    nonaka 
     31      1.1    nonaka #include <sys/param.h>
     32      1.1    nonaka #include <sys/device.h>
     33      1.1    nonaka #include <sys/systm.h>
     34      1.1    nonaka #include <sys/kmem.h>
     35      1.1    nonaka 
     36      1.1    nonaka #include <dev/acpi/acpireg.h>
     37      1.1    nonaka #include <dev/acpi/acpivar.h>
     38      1.1    nonaka 
     39      1.1    nonaka #include <dev/sdmmc/sdhcreg.h>
     40      1.1    nonaka #include <dev/sdmmc/sdhcvar.h>
     41      1.1    nonaka #include <dev/sdmmc/sdmmcvar.h>
     42      1.1    nonaka 
     43      1.1    nonaka #define _COMPONENT	ACPI_RESOURCE_COMPONENT
     44      1.1    nonaka ACPI_MODULE_NAME	("sdhc_acpi")
     45      1.1    nonaka 
     46      1.1    nonaka static int	sdhc_acpi_match(device_t, cfdata_t, void *);
     47      1.1    nonaka static void	sdhc_acpi_attach(device_t, device_t, void *);
     48      1.1    nonaka static int	sdhc_acpi_detach(device_t, int);
     49      1.1    nonaka static bool	sdhc_acpi_resume(device_t, const pmf_qual_t *);
     50      1.1    nonaka 
     51      1.1    nonaka struct sdhc_acpi_softc {
     52      1.1    nonaka 	struct sdhc_softc sc;
     53  1.2.4.1  pgoyette 	bus_space_tag_t sc_memt;
     54  1.2.4.1  pgoyette 	bus_space_handle_t sc_memh;
     55  1.2.4.1  pgoyette 	bus_size_t sc_memsize;
     56      1.1    nonaka 	int sc_irq;
     57      1.1    nonaka 
     58      1.1    nonaka 	ACPI_HANDLE sc_crs, sc_srs;
     59      1.1    nonaka 	ACPI_BUFFER sc_crs_buffer;
     60      1.1    nonaka };
     61      1.1    nonaka 
     62      1.1    nonaka CFATTACH_DECL_NEW(sdhc_acpi, sizeof(struct sdhc_acpi_softc),
     63      1.1    nonaka     sdhc_acpi_match, sdhc_acpi_attach, sdhc_acpi_detach, NULL);
     64      1.1    nonaka 
     65  1.2.4.1  pgoyette static uint32_t	sdhc_acpi_intr(void *);
     66  1.2.4.1  pgoyette static void	sdhc_acpi_intel_emmc_hw_reset(struct sdhc_softc *,
     67  1.2.4.1  pgoyette 		    struct sdhc_host *);
     68  1.2.4.1  pgoyette 
     69  1.2.4.1  pgoyette static const struct sdhc_acpi_slot {
     70  1.2.4.1  pgoyette 	const char *hid;
     71  1.2.4.1  pgoyette 	const char *uid;
     72  1.2.4.1  pgoyette 	int type;
     73  1.2.4.1  pgoyette #define	SLOT_TYPE_SD	0	/* SD or SDIO */
     74  1.2.4.1  pgoyette #define	SLOT_TYPE_EMMC	1	/* eMMC */
     75  1.2.4.1  pgoyette } sdhc_acpi_slot_map[] = {
     76  1.2.4.1  pgoyette 	{ "80865ACA",	NULL,	SLOT_TYPE_SD },
     77  1.2.4.1  pgoyette 	{ "80865ACC",	NULL,	SLOT_TYPE_EMMC },
     78  1.2.4.1  pgoyette 	{ "80865AD0",	NULL,	SLOT_TYPE_SD },
     79  1.2.4.1  pgoyette 	{ "80860F14",   "1",	SLOT_TYPE_EMMC },
     80  1.2.4.1  pgoyette 	{ "80860F14",   "3",	SLOT_TYPE_SD },
     81  1.2.4.1  pgoyette 	{ "80860F16",   NULL,	SLOT_TYPE_SD },
     82  1.2.4.1  pgoyette 	{ "INT33BB",	"2",	SLOT_TYPE_SD },
     83  1.2.4.1  pgoyette 	{ "INT33BB",	"3",	SLOT_TYPE_SD },
     84  1.2.4.1  pgoyette 	{ "INT33C6",	NULL,	SLOT_TYPE_SD },
     85  1.2.4.1  pgoyette 	{ "INT3436",	NULL,	SLOT_TYPE_SD },
     86  1.2.4.1  pgoyette 	{ "INT344D",	NULL,	SLOT_TYPE_SD },
     87  1.2.4.1  pgoyette 	{ "PNP0D40",	NULL,	SLOT_TYPE_SD },
     88  1.2.4.1  pgoyette 	{ "PNP0FFF",	"3",	SLOT_TYPE_SD },
     89      1.1    nonaka };
     90      1.1    nonaka 
     91  1.2.4.1  pgoyette static const struct sdhc_acpi_slot *
     92  1.2.4.1  pgoyette sdhc_acpi_find_slot(ACPI_DEVICE_INFO *ad)
     93  1.2.4.1  pgoyette {
     94  1.2.4.1  pgoyette 	const struct sdhc_acpi_slot *slot;
     95  1.2.4.1  pgoyette 	const char *hid, *uid;
     96  1.2.4.1  pgoyette 	size_t i;
     97  1.2.4.1  pgoyette 
     98  1.2.4.1  pgoyette 	hid = ad->HardwareId.String;
     99  1.2.4.1  pgoyette 	uid = ad->UniqueId.String;
    100  1.2.4.1  pgoyette 
    101  1.2.4.1  pgoyette 	if (!(ad->Valid & ACPI_VALID_HID) || hid == NULL)
    102  1.2.4.1  pgoyette 		return NULL;
    103  1.2.4.1  pgoyette 
    104  1.2.4.1  pgoyette 	for (i = 0; i < __arraycount(sdhc_acpi_slot_map); i++) {
    105  1.2.4.1  pgoyette 		slot = &sdhc_acpi_slot_map[i];
    106  1.2.4.1  pgoyette 		if (strcmp(hid, slot->hid) == 0) {
    107  1.2.4.1  pgoyette 			if (slot->uid == NULL ||
    108  1.2.4.1  pgoyette 			    ((ad->Valid & ACPI_VALID_UID) != 0 &&
    109  1.2.4.1  pgoyette 			     uid != NULL &&
    110  1.2.4.1  pgoyette 			     strcmp(uid, slot->uid) == 0))
    111  1.2.4.1  pgoyette 				return slot;
    112  1.2.4.1  pgoyette 		}
    113  1.2.4.1  pgoyette 	}
    114  1.2.4.1  pgoyette 	return NULL;
    115  1.2.4.1  pgoyette }
    116  1.2.4.1  pgoyette 
    117      1.1    nonaka static int
    118      1.1    nonaka sdhc_acpi_match(device_t parent, cfdata_t match, void *opaque)
    119      1.1    nonaka {
    120      1.1    nonaka 	struct acpi_attach_args *aa = opaque;
    121      1.1    nonaka 
    122      1.1    nonaka 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
    123      1.1    nonaka 		return 0;
    124      1.1    nonaka 
    125  1.2.4.1  pgoyette 	return sdhc_acpi_find_slot(aa->aa_node->ad_devinfo) != NULL;
    126      1.1    nonaka }
    127      1.1    nonaka 
    128      1.1    nonaka static void
    129      1.1    nonaka sdhc_acpi_attach(device_t parent, device_t self, void *opaque)
    130      1.1    nonaka {
    131      1.1    nonaka 	struct sdhc_acpi_softc *sc = device_private(self);
    132      1.1    nonaka 	struct acpi_attach_args *aa = opaque;
    133  1.2.4.1  pgoyette 	const struct sdhc_acpi_slot *slot;
    134      1.1    nonaka 	struct acpi_resources res;
    135      1.1    nonaka 	struct acpi_mem *mem;
    136      1.1    nonaka 	struct acpi_irq *irq;
    137      1.1    nonaka 	ACPI_STATUS rv;
    138      1.1    nonaka 
    139      1.1    nonaka 	sc->sc.sc_dev = self;
    140      1.1    nonaka 	sc->sc.sc_dmat = aa->aa_dmat;
    141      1.1    nonaka 	sc->sc.sc_host = NULL;
    142  1.2.4.1  pgoyette 	sc->sc_memt = aa->aa_memt;
    143      1.1    nonaka 	sc->sc_irq = -1;
    144      1.1    nonaka 
    145  1.2.4.1  pgoyette 	slot = sdhc_acpi_find_slot(aa->aa_node->ad_devinfo);
    146  1.2.4.1  pgoyette 	if (slot->type == SLOT_TYPE_EMMC)
    147  1.2.4.1  pgoyette 		sc->sc.sc_vendor_hw_reset = sdhc_acpi_intel_emmc_hw_reset;
    148  1.2.4.1  pgoyette 
    149      1.1    nonaka 	rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
    150      1.1    nonaka 	    &res, &acpi_resource_parse_ops_default);
    151      1.1    nonaka 	if (ACPI_FAILURE(rv))
    152      1.1    nonaka 		return;
    153      1.1    nonaka 
    154      1.1    nonaka 	AcpiGetHandle(aa->aa_node->ad_handle, "_CRS", &sc->sc_crs);
    155      1.1    nonaka 	AcpiGetHandle(aa->aa_node->ad_handle, "_SRS", &sc->sc_srs);
    156      1.1    nonaka 	if (sc->sc_crs && sc->sc_srs) {
    157      1.1    nonaka 		/* XXX Why need this? */
    158      1.1    nonaka 		sc->sc_crs_buffer.Pointer = NULL;
    159      1.1    nonaka 		sc->sc_crs_buffer.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
    160      1.1    nonaka 		rv = AcpiGetCurrentResources(sc->sc_crs, &sc->sc_crs_buffer);
    161      1.1    nonaka 		if (ACPI_FAILURE(rv))
    162      1.1    nonaka 			sc->sc_crs = sc->sc_srs = NULL;
    163      1.1    nonaka 	}
    164      1.1    nonaka 
    165      1.1    nonaka 	mem = acpi_res_mem(&res, 0);
    166      1.1    nonaka 	irq = acpi_res_irq(&res, 0);
    167      1.1    nonaka 	if (mem == NULL || irq == NULL) {
    168      1.1    nonaka 		aprint_error_dev(self, "incomplete resources\n");
    169      1.1    nonaka 		goto cleanup;
    170      1.1    nonaka 	}
    171  1.2.4.1  pgoyette 	sc->sc_memsize = mem->ar_length;
    172      1.1    nonaka 
    173  1.2.4.1  pgoyette 	if (bus_space_map(sc->sc_memt, mem->ar_base, sc->sc_memsize, 0,
    174  1.2.4.1  pgoyette 	    &sc->sc_memh)) {
    175      1.1    nonaka 		aprint_error_dev(self, "couldn't map registers\n");
    176      1.1    nonaka 		goto cleanup;
    177      1.1    nonaka 	}
    178      1.1    nonaka 
    179      1.1    nonaka 	/* XXX acpi_intr_establish? */
    180      1.1    nonaka 	rv = AcpiOsInstallInterruptHandler(irq->ar_irq, sdhc_acpi_intr, sc);
    181      1.1    nonaka 	if (ACPI_FAILURE(rv)) {
    182      1.1    nonaka 		aprint_error_dev(self,
    183      1.1    nonaka 		    "couldn't establish interrupt handler\n");
    184  1.2.4.1  pgoyette 		goto unmap;
    185      1.1    nonaka 	}
    186      1.1    nonaka 	sc->sc_irq = irq->ar_irq;
    187      1.1    nonaka 
    188      1.1    nonaka 	sc->sc.sc_host = kmem_zalloc(sizeof(struct sdhc_host *), KM_NOSLEEP);
    189      1.1    nonaka 	if (sc->sc.sc_host == NULL) {
    190      1.1    nonaka 		aprint_error_dev(self, "couldn't alloc memory\n");
    191  1.2.4.1  pgoyette 		goto intr_disestablish;
    192      1.1    nonaka 	}
    193      1.1    nonaka 
    194  1.2.4.1  pgoyette 	/* Enable DMA transfer */
    195  1.2.4.1  pgoyette 	sc->sc.sc_flags |= SDHC_FLAG_USE_DMA;
    196  1.2.4.1  pgoyette 
    197  1.2.4.1  pgoyette 	if (sdhc_host_found(&sc->sc, sc->sc_memt, sc->sc_memh,
    198  1.2.4.1  pgoyette 	    sc->sc_memsize) != 0) {
    199      1.1    nonaka 		aprint_error_dev(self, "couldn't initialize host\n");
    200  1.2.4.1  pgoyette 		goto fail;
    201      1.1    nonaka 	}
    202      1.1    nonaka 
    203      1.1    nonaka 	if (!pmf_device_register1(self, sdhc_suspend, sdhc_acpi_resume,
    204      1.1    nonaka 	    sdhc_shutdown)) {
    205      1.1    nonaka 		aprint_error_dev(self, "couldn't establish powerhook\n");
    206      1.1    nonaka 	}
    207      1.1    nonaka 
    208      1.1    nonaka 	acpi_resource_cleanup(&res);
    209      1.1    nonaka 	return;
    210      1.1    nonaka 
    211  1.2.4.1  pgoyette fail:
    212  1.2.4.1  pgoyette 	if (sc->sc.sc_host != NULL)
    213  1.2.4.1  pgoyette 		kmem_free(sc->sc.sc_host, sizeof(struct sdhc_host *));
    214  1.2.4.1  pgoyette 	sc->sc.sc_host = NULL;
    215  1.2.4.1  pgoyette intr_disestablish:
    216      1.1    nonaka 	if (sc->sc_irq >= 0)
    217      1.1    nonaka 		/* XXX acpi_intr_disestablish? */
    218      1.1    nonaka 		AcpiOsRemoveInterruptHandler(sc->sc_irq, sdhc_acpi_intr);
    219  1.2.4.1  pgoyette 	sc->sc_irq = -1;
    220  1.2.4.1  pgoyette unmap:
    221  1.2.4.1  pgoyette 	bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_memsize);
    222  1.2.4.1  pgoyette 	sc->sc_memsize = 0;
    223  1.2.4.1  pgoyette cleanup:
    224  1.2.4.1  pgoyette 	if (sc->sc_crs_buffer.Pointer)
    225  1.2.4.1  pgoyette 		ACPI_FREE(sc->sc_crs_buffer.Pointer);
    226  1.2.4.1  pgoyette 	sc->sc_crs_buffer.Pointer = NULL;
    227  1.2.4.1  pgoyette 	acpi_resource_cleanup(&res);
    228      1.1    nonaka }
    229      1.1    nonaka 
    230      1.1    nonaka static int
    231      1.1    nonaka sdhc_acpi_detach(device_t self, int flags)
    232      1.1    nonaka {
    233      1.1    nonaka 	struct sdhc_acpi_softc *sc = device_private(self);
    234      1.1    nonaka 	int rv;
    235      1.1    nonaka 
    236      1.1    nonaka 	pmf_device_deregister(self);
    237      1.1    nonaka 
    238      1.1    nonaka 	rv = sdhc_detach(&sc->sc, flags);
    239      1.1    nonaka 	if (rv)
    240      1.1    nonaka 		return rv;
    241      1.1    nonaka 
    242      1.1    nonaka 	if (sc->sc_irq >= 0)
    243      1.1    nonaka 		/* XXX acpi_intr_disestablish? */
    244      1.1    nonaka 		AcpiOsRemoveInterruptHandler(sc->sc_irq, sdhc_acpi_intr);
    245      1.1    nonaka 
    246      1.1    nonaka 	if (sc->sc.sc_host != NULL)
    247      1.1    nonaka 		kmem_free(sc->sc.sc_host, sizeof(struct sdhc_host *));
    248      1.1    nonaka 
    249  1.2.4.1  pgoyette 	if (sc->sc_memsize > 0)
    250  1.2.4.1  pgoyette 		bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_memsize);
    251  1.2.4.1  pgoyette 
    252  1.2.4.1  pgoyette 	if (sc->sc_crs_buffer.Pointer)
    253  1.2.4.1  pgoyette 		ACPI_FREE(sc->sc_crs_buffer.Pointer);
    254  1.2.4.1  pgoyette 
    255      1.1    nonaka 	return 0;
    256      1.1    nonaka }
    257      1.1    nonaka 
    258      1.1    nonaka static bool
    259      1.1    nonaka sdhc_acpi_resume(device_t self, const pmf_qual_t *qual)
    260      1.1    nonaka {
    261      1.1    nonaka 	struct sdhc_acpi_softc *sc = device_private(self);
    262      1.1    nonaka 	ACPI_STATUS rv;
    263      1.1    nonaka 
    264      1.1    nonaka 	if (sc->sc_crs && sc->sc_srs) {
    265      1.1    nonaka 		rv = AcpiSetCurrentResources(sc->sc_srs, &sc->sc_crs_buffer);
    266      1.1    nonaka 		if (ACPI_FAILURE(rv))
    267      1.1    nonaka 			printf("%s: _SRS failed: %s\n",
    268      1.1    nonaka 			    device_xname(self), AcpiFormatException(rv));
    269      1.1    nonaka 	}
    270      1.1    nonaka 
    271      1.1    nonaka 	return sdhc_resume(self, qual);
    272      1.1    nonaka }
    273      1.1    nonaka 
    274      1.1    nonaka static uint32_t
    275      1.1    nonaka sdhc_acpi_intr(void *context)
    276      1.1    nonaka {
    277      1.1    nonaka 	struct sdhc_acpi_softc *sc = context;
    278      1.1    nonaka 
    279      1.1    nonaka 	if (!sdhc_intr(&sc->sc))
    280      1.1    nonaka 		return ACPI_INTERRUPT_NOT_HANDLED;
    281      1.1    nonaka 	return ACPI_INTERRUPT_HANDLED;
    282      1.1    nonaka }
    283  1.2.4.1  pgoyette 
    284  1.2.4.1  pgoyette static void
    285  1.2.4.1  pgoyette sdhc_acpi_intel_emmc_hw_reset(struct sdhc_softc *sc, struct sdhc_host *hp)
    286  1.2.4.1  pgoyette {
    287  1.2.4.1  pgoyette 	kmutex_t *plock = sdhc_host_lock(hp);
    288  1.2.4.1  pgoyette 	uint8_t reg;
    289  1.2.4.1  pgoyette 
    290  1.2.4.1  pgoyette 	mutex_enter(plock);
    291  1.2.4.1  pgoyette 
    292  1.2.4.1  pgoyette 	reg = sdhc_host_read_1(hp, SDHC_POWER_CTL);
    293  1.2.4.1  pgoyette 	reg |= 0x10;
    294  1.2.4.1  pgoyette 	sdhc_host_write_1(hp, SDHC_POWER_CTL, reg);
    295  1.2.4.1  pgoyette 
    296  1.2.4.1  pgoyette 	sdmmc_delay(10);
    297  1.2.4.1  pgoyette 
    298  1.2.4.1  pgoyette 	reg &= ~0x10;
    299  1.2.4.1  pgoyette 	sdhc_host_write_1(hp, SDHC_POWER_CTL, reg);
    300  1.2.4.1  pgoyette 
    301  1.2.4.1  pgoyette 	sdmmc_delay(1000);
    302  1.2.4.1  pgoyette 
    303  1.2.4.1  pgoyette 	mutex_exit(plock);
    304  1.2.4.1  pgoyette }
    305