Home | History | Annotate | Line # | Download | only in acpi
sdhc_acpi.c revision 1.10
      1 /*	$NetBSD: sdhc_acpi.c,v 1.10 2020/02/01 13:09:08 jmcneill Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2016 Kimihiro Nonaka <nonaka (at) NetBSD.org>
      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: sdhc_acpi.c,v 1.10 2020/02/01 13:09:08 jmcneill Exp $");
     30 
     31 #include <sys/param.h>
     32 #include <sys/device.h>
     33 #include <sys/systm.h>
     34 #include <sys/kmem.h>
     35 
     36 #include <dev/acpi/acpireg.h>
     37 #include <dev/acpi/acpivar.h>
     38 #include <dev/acpi/acpi_intr.h>
     39 
     40 #include <dev/sdmmc/sdhcreg.h>
     41 #include <dev/sdmmc/sdhcvar.h>
     42 #include <dev/sdmmc/sdmmcvar.h>
     43 
     44 #define _COMPONENT	ACPI_RESOURCE_COMPONENT
     45 ACPI_MODULE_NAME	("sdhc_acpi")
     46 
     47 static int	sdhc_acpi_match(device_t, cfdata_t, void *);
     48 static void	sdhc_acpi_attach(device_t, device_t, void *);
     49 static int	sdhc_acpi_detach(device_t, int);
     50 static bool	sdhc_acpi_resume(device_t, const pmf_qual_t *);
     51 
     52 struct sdhc_acpi_softc {
     53 	struct sdhc_softc sc;
     54 	bus_space_tag_t sc_memt;
     55 	bus_space_handle_t sc_memh;
     56 	bus_size_t sc_memsize;
     57 	void *sc_ih;
     58 
     59 	ACPI_HANDLE sc_crs, sc_srs;
     60 	ACPI_BUFFER sc_crs_buffer;
     61 };
     62 
     63 CFATTACH_DECL_NEW(sdhc_acpi, sizeof(struct sdhc_acpi_softc),
     64     sdhc_acpi_match, sdhc_acpi_attach, sdhc_acpi_detach, NULL);
     65 
     66 static void	sdhc_acpi_intel_emmc_hw_reset(struct sdhc_softc *,
     67 		    struct sdhc_host *);
     68 
     69 static const struct sdhc_acpi_slot {
     70 	const char *hid;
     71 	const char *uid;
     72 	int type;
     73 #define	SLOT_TYPE_SD	0	/* SD or SDIO */
     74 #define	SLOT_TYPE_EMMC	1	/* eMMC */
     75 } sdhc_acpi_slot_map[] = {
     76 	{ "80865ACA",	NULL,	SLOT_TYPE_SD },
     77 	{ "80865ACC",	NULL,	SLOT_TYPE_EMMC },
     78 	{ "80865AD0",	NULL,	SLOT_TYPE_SD },
     79 	{ "80860F14",   "1",	SLOT_TYPE_EMMC },
     80 	{ "80860F14",   "3",	SLOT_TYPE_SD },
     81 	{ "80860F16",   NULL,	SLOT_TYPE_SD },
     82 	{ "INT33BB",	"2",	SLOT_TYPE_SD },
     83 	{ "INT33BB",	"3",	SLOT_TYPE_SD },
     84 	{ "INT33C6",	NULL,	SLOT_TYPE_SD },
     85 	{ "INT3436",	NULL,	SLOT_TYPE_SD },
     86 	{ "INT344D",	NULL,	SLOT_TYPE_SD },
     87 	{ "PNP0D40",	NULL,	SLOT_TYPE_SD },
     88 	{ "PNP0FFF",	"3",	SLOT_TYPE_SD },
     89 };
     90 
     91 static const struct sdhc_acpi_slot *
     92 sdhc_acpi_find_slot(ACPI_DEVICE_INFO *ad)
     93 {
     94 	const struct sdhc_acpi_slot *slot;
     95 	const char *hid, *uid;
     96 	size_t i;
     97 
     98 	hid = ad->HardwareId.String;
     99 	uid = ad->UniqueId.String;
    100 
    101 	if (!(ad->Valid & ACPI_VALID_HID) || hid == NULL)
    102 		return NULL;
    103 
    104 	for (i = 0; i < __arraycount(sdhc_acpi_slot_map); i++) {
    105 		slot = &sdhc_acpi_slot_map[i];
    106 		const char * const slot_id[] = { slot->hid, NULL };
    107 		if (acpi_match_hid(ad, slot_id)) {
    108 			if (slot->uid == NULL ||
    109 			    ((ad->Valid & ACPI_VALID_UID) != 0 &&
    110 			     uid != NULL &&
    111 			     strcmp(uid, slot->uid) == 0))
    112 				return slot;
    113 		}
    114 	}
    115 	return NULL;
    116 }
    117 
    118 static int
    119 sdhc_acpi_match(device_t parent, cfdata_t match, void *opaque)
    120 {
    121 	struct acpi_attach_args *aa = opaque;
    122 
    123 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
    124 		return 0;
    125 
    126 	return sdhc_acpi_find_slot(aa->aa_node->ad_devinfo) != NULL;
    127 }
    128 
    129 static void
    130 sdhc_acpi_attach(device_t parent, device_t self, void *opaque)
    131 {
    132 	struct sdhc_acpi_softc *sc = device_private(self);
    133 	struct acpi_attach_args *aa = opaque;
    134 	const struct sdhc_acpi_slot *slot;
    135 	struct acpi_resources res;
    136 	struct acpi_mem *mem;
    137 	struct acpi_irq *irq;
    138 	ACPI_STATUS rv;
    139 	ACPI_INTEGER clock_freq;
    140 
    141 	sc->sc.sc_dev = self;
    142 	sc->sc.sc_dmat = aa->aa_dmat;
    143 	sc->sc.sc_host = NULL;
    144 	sc->sc_memt = aa->aa_memt;
    145 
    146 	slot = sdhc_acpi_find_slot(aa->aa_node->ad_devinfo);
    147 	if (slot->type == SLOT_TYPE_EMMC)
    148 		sc->sc.sc_vendor_hw_reset = sdhc_acpi_intel_emmc_hw_reset;
    149 
    150 	rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
    151 	    &res, &acpi_resource_parse_ops_default);
    152 	if (ACPI_FAILURE(rv))
    153 		return;
    154 
    155 	AcpiGetHandle(aa->aa_node->ad_handle, "_CRS", &sc->sc_crs);
    156 	AcpiGetHandle(aa->aa_node->ad_handle, "_SRS", &sc->sc_srs);
    157 	if (sc->sc_crs && sc->sc_srs) {
    158 		/* XXX Why need this? */
    159 		sc->sc_crs_buffer.Pointer = NULL;
    160 		sc->sc_crs_buffer.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
    161 		rv = AcpiGetCurrentResources(sc->sc_crs, &sc->sc_crs_buffer);
    162 		if (ACPI_FAILURE(rv))
    163 			sc->sc_crs = sc->sc_srs = NULL;
    164 	}
    165 
    166 	mem = acpi_res_mem(&res, 0);
    167 	irq = acpi_res_irq(&res, 0);
    168 	if (mem == NULL || irq == NULL) {
    169 		aprint_error_dev(self, "incomplete resources\n");
    170 		goto cleanup;
    171 	}
    172 	if (mem->ar_length == 0) {
    173 		aprint_error_dev(self, "zero length memory resource\n");
    174 		goto cleanup;
    175 	}
    176 	sc->sc_memsize = mem->ar_length;
    177 
    178 	if (bus_space_map(sc->sc_memt, mem->ar_base, sc->sc_memsize, 0,
    179 	    &sc->sc_memh)) {
    180 		aprint_error_dev(self, "couldn't map registers\n");
    181 		goto cleanup;
    182 	}
    183 
    184 	sc->sc_ih = acpi_intr_establish(self,
    185 	    (uint64_t)(uintptr_t)aa->aa_node->ad_handle,
    186 	    IPL_BIO, false, sdhc_intr, &sc->sc, device_xname(self));
    187 	if (sc->sc_ih == NULL) {
    188 		aprint_error_dev(self,
    189 		    "couldn't establish interrupt handler\n");
    190 		goto unmap;
    191 	}
    192 
    193 	sc->sc.sc_host = kmem_zalloc(sizeof(struct sdhc_host *), KM_SLEEP);
    194 
    195 	/* Enable DMA transfer */
    196 	sc->sc.sc_flags |= SDHC_FLAG_USE_DMA;
    197 
    198 	/* Read clock frequency from device properties */
    199 	rv = acpi_dsd_integer(aa->aa_node->ad_handle, "clock-frequency",
    200 	    &clock_freq);
    201 	if (ACPI_SUCCESS(rv))
    202 		sc->sc.sc_clkbase = clock_freq / 1000;
    203 
    204 	if (sdhc_host_found(&sc->sc, sc->sc_memt, sc->sc_memh,
    205 	    sc->sc_memsize) != 0) {
    206 		aprint_error_dev(self, "couldn't initialize host\n");
    207 		goto fail;
    208 	}
    209 
    210 	if (!pmf_device_register1(self, sdhc_suspend, sdhc_acpi_resume,
    211 	    sdhc_shutdown)) {
    212 		aprint_error_dev(self, "couldn't establish powerhook\n");
    213 	}
    214 
    215 	acpi_resource_cleanup(&res);
    216 	return;
    217 
    218 fail:
    219 	if (sc->sc.sc_host != NULL)
    220 		kmem_free(sc->sc.sc_host, sizeof(struct sdhc_host *));
    221 	sc->sc.sc_host = NULL;
    222 	if (sc->sc_ih != NULL)
    223 		acpi_intr_disestablish(sc->sc_ih);
    224 	sc->sc_ih = NULL;
    225 unmap:
    226 	bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_memsize);
    227 	sc->sc_memsize = 0;
    228 cleanup:
    229 	if (sc->sc_crs_buffer.Pointer)
    230 		ACPI_FREE(sc->sc_crs_buffer.Pointer);
    231 	sc->sc_crs_buffer.Pointer = NULL;
    232 	acpi_resource_cleanup(&res);
    233 }
    234 
    235 static int
    236 sdhc_acpi_detach(device_t self, int flags)
    237 {
    238 	struct sdhc_acpi_softc *sc = device_private(self);
    239 	int rv;
    240 
    241 	pmf_device_deregister(self);
    242 
    243 	rv = sdhc_detach(&sc->sc, flags);
    244 	if (rv)
    245 		return rv;
    246 
    247 	if (sc->sc_ih != NULL)
    248 		acpi_intr_disestablish(sc->sc_ih);
    249 
    250 	if (sc->sc.sc_host != NULL)
    251 		kmem_free(sc->sc.sc_host, sizeof(struct sdhc_host *));
    252 
    253 	if (sc->sc_memsize > 0)
    254 		bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_memsize);
    255 
    256 	if (sc->sc_crs_buffer.Pointer)
    257 		ACPI_FREE(sc->sc_crs_buffer.Pointer);
    258 
    259 	return 0;
    260 }
    261 
    262 static bool
    263 sdhc_acpi_resume(device_t self, const pmf_qual_t *qual)
    264 {
    265 	struct sdhc_acpi_softc *sc = device_private(self);
    266 	ACPI_STATUS rv;
    267 
    268 	if (sc->sc_crs && sc->sc_srs) {
    269 		rv = AcpiSetCurrentResources(sc->sc_srs, &sc->sc_crs_buffer);
    270 		if (ACPI_FAILURE(rv))
    271 			printf("%s: _SRS failed: %s\n",
    272 			    device_xname(self), AcpiFormatException(rv));
    273 	}
    274 
    275 	return sdhc_resume(self, qual);
    276 }
    277 
    278 static void
    279 sdhc_acpi_intel_emmc_hw_reset(struct sdhc_softc *sc, struct sdhc_host *hp)
    280 {
    281 	kmutex_t *plock = sdhc_host_lock(hp);
    282 	uint8_t reg;
    283 
    284 	mutex_enter(plock);
    285 
    286 	reg = sdhc_host_read_1(hp, SDHC_POWER_CTL);
    287 	reg |= 0x10;
    288 	sdhc_host_write_1(hp, SDHC_POWER_CTL, reg);
    289 
    290 	sdmmc_delay(10);
    291 
    292 	reg &= ~0x10;
    293 	sdhc_host_write_1(hp, SDHC_POWER_CTL, reg);
    294 
    295 	sdmmc_delay(1000);
    296 
    297 	mutex_exit(plock);
    298 }
    299