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