Home | History | Annotate | Line # | Download | only in acpi
sdhc_acpi.c revision 1.18
      1 /*	$NetBSD: sdhc_acpi.c,v 1.18 2022/01/15 15:54:40 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.18 2022/01/15 15:54:40 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 /* Rockchip eMMC device-specific method (_DSM) - 434addb0-8ff3-49d5-a724-95844b79ad1f */
     49 static UINT8 sdhc_acpi_rockchip_dsm_uuid[ACPI_UUID_LENGTH] = {
     50 	0xb0, 0xdd, 0x4a, 0x43, 0xf3, 0x8f, 0xd5, 0x49,
     51 	0xa7, 0x24, 0x95, 0x84, 0x4b, 0x79, 0xad, 0x1f
     52 };
     53 #define	ROCKCHIP_DSM_REV			0
     54 #define	ROCKCHIP_DSM_FUNC_SET_CARD_CLOCK	1
     55 
     56 #define _COMPONENT	ACPI_RESOURCE_COMPONENT
     57 ACPI_MODULE_NAME	("sdhc_acpi")
     58 
     59 static int	sdhc_acpi_match(device_t, cfdata_t, void *);
     60 static void	sdhc_acpi_attach(device_t, device_t, void *);
     61 static int	sdhc_acpi_detach(device_t, int);
     62 static bool	sdhc_acpi_resume(device_t, const pmf_qual_t *);
     63 
     64 struct sdhc_acpi_softc {
     65 	struct sdhc_softc sc;
     66 	bus_space_tag_t sc_memt;
     67 	bus_space_handle_t sc_memh;
     68 	bus_size_t sc_memsize;
     69 	void *sc_ih;
     70 	ACPI_HANDLE sc_handle;
     71 
     72 	ACPI_HANDLE sc_crs, sc_srs;
     73 	ACPI_BUFFER sc_crs_buffer;
     74 };
     75 
     76 CFATTACH_DECL_NEW(sdhc_acpi, sizeof(struct sdhc_acpi_softc),
     77     sdhc_acpi_match, sdhc_acpi_attach, sdhc_acpi_detach, NULL);
     78 
     79 static void	sdhc_acpi_intel_emmc_hw_reset(struct sdhc_softc *,
     80 		    struct sdhc_host *);
     81 
     82 static int	sdhc_acpi_rockchip_bus_clock(struct sdhc_softc *,
     83 		    int);
     84 
     85 static const struct sdhc_acpi_slot {
     86 	const char *hid;
     87 	const char *uid;
     88 	int type;
     89 #define	SLOT_TYPE_SD	0	/* SD or SDIO */
     90 #define	SLOT_TYPE_EMMC	1	/* eMMC */
     91 	uint32_t flags;
     92 } sdhc_acpi_slot_map[] = {
     93 	{ .hid = "80865ACA",		 .type = SLOT_TYPE_SD },
     94 	{ .hid = "80865ACC",		 .type = SLOT_TYPE_EMMC },
     95 	{ .hid = "80865AD0",		 .type = SLOT_TYPE_SD },
     96 	{ .hid = "80860F14", .uid = "1", .type = SLOT_TYPE_EMMC },
     97 	{ .hid = "80860F14", .uid = "3", .type = SLOT_TYPE_SD },
     98 	{ .hid = "80860F16",   		 .type = SLOT_TYPE_SD },
     99 	{ .hid = "INT33BB",  .uid = "2", .type = SLOT_TYPE_SD },
    100 	{ .hid = "INT33BB",  .uid = "3", .type = SLOT_TYPE_SD },
    101 	{ .hid = "INT33C6",		 .type = SLOT_TYPE_SD },
    102 	{ .hid = "INT3436",		 .type = SLOT_TYPE_SD },
    103 	{ .hid = "INT344D",		 .type = SLOT_TYPE_SD },
    104 	{ .hid = "NXP0003",  .uid = "0", .type = SLOT_TYPE_SD,
    105 					 .flags = SDHC_ESDHC_FLAGS },
    106 	{ .hid = "NXP0003",  .uid = "1", .type = SLOT_TYPE_EMMC,
    107 					 .flags = SDHC_ESDHC_FLAGS },
    108 	{ .hid = "RKCP0D40",		 .type = SLOT_TYPE_SD,
    109 					 .flags = SDHC_FLAG_32BIT_ACCESS |
    110 						  SDHC_FLAG_8BIT_MODE |
    111 						  SDHC_FLAG_USE_ADMA2 |
    112 						  SDHC_FLAG_SINGLE_POWER_WRITE },
    113 
    114 	/* Generic IDs last */
    115 	{ .hid = "PNP0D40",		 .type = SLOT_TYPE_SD },
    116 	{ .hid = "PNP0FFF",  .uid = "3", .type = SLOT_TYPE_SD },
    117 };
    118 
    119 static const struct sdhc_acpi_slot *
    120 sdhc_acpi_find_slot(ACPI_DEVICE_INFO *ad)
    121 {
    122 	const struct sdhc_acpi_slot *slot;
    123 	const char *hid, *uid;
    124 	size_t i;
    125 
    126 	hid = ad->HardwareId.String;
    127 	uid = ad->UniqueId.String;
    128 
    129 	if (!(ad->Valid & ACPI_VALID_HID) || hid == NULL)
    130 		return NULL;
    131 
    132 	for (i = 0; i < __arraycount(sdhc_acpi_slot_map); i++) {
    133 		slot = &sdhc_acpi_slot_map[i];
    134 		const char * const slot_id[] = { slot->hid, NULL };
    135 		if (acpi_match_hid(ad, slot_id)) {
    136 			if (slot->uid == NULL ||
    137 			    ((ad->Valid & ACPI_VALID_UID) != 0 &&
    138 			     uid != NULL &&
    139 			     strcmp(uid, slot->uid) == 0))
    140 				return slot;
    141 		}
    142 	}
    143 	return NULL;
    144 }
    145 
    146 static int
    147 sdhc_acpi_match(device_t parent, cfdata_t match, void *opaque)
    148 {
    149 	struct acpi_attach_args *aa = opaque;
    150 
    151 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
    152 		return 0;
    153 
    154 	return sdhc_acpi_find_slot(aa->aa_node->ad_devinfo) != NULL;
    155 }
    156 
    157 static void
    158 sdhc_acpi_attach(device_t parent, device_t self, void *opaque)
    159 {
    160 	struct sdhc_acpi_softc *sc = device_private(self);
    161 	struct acpi_attach_args *aa = opaque;
    162 	const struct sdhc_acpi_slot *slot;
    163 	struct acpi_resources res;
    164 	struct acpi_mem *mem;
    165 	struct acpi_irq *irq;
    166 	ACPI_STATUS rv;
    167 	ACPI_INTEGER clock_freq;
    168 	ACPI_INTEGER caps, caps_mask;
    169 	ACPI_INTEGER funcs;
    170 
    171 	sc->sc.sc_dev = self;
    172 	sc->sc.sc_dmat = aa->aa_dmat;
    173 	sc->sc.sc_host = NULL;
    174 	sc->sc_memt = aa->aa_memt;
    175 	sc->sc_handle = aa->aa_node->ad_handle;
    176 
    177 	slot = sdhc_acpi_find_slot(aa->aa_node->ad_devinfo);
    178 	if (slot->type == SLOT_TYPE_EMMC)
    179 		sc->sc.sc_vendor_hw_reset = sdhc_acpi_intel_emmc_hw_reset;
    180 
    181 	rv = acpi_dsm_query(sc->sc_handle, sdhc_acpi_rockchip_dsm_uuid,
    182 	    ROCKCHIP_DSM_REV, &funcs);
    183 	if (ACPI_SUCCESS(rv) &&
    184 	    ISSET(funcs, __BIT(ROCKCHIP_DSM_FUNC_SET_CARD_CLOCK))) {
    185 		sc->sc.sc_vendor_bus_clock = sdhc_acpi_rockchip_bus_clock;
    186 	}
    187 
    188 	rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
    189 	    &res, &acpi_resource_parse_ops_default);
    190 	if (ACPI_FAILURE(rv))
    191 		return;
    192 
    193 	AcpiGetHandle(aa->aa_node->ad_handle, "_CRS", &sc->sc_crs);
    194 	AcpiGetHandle(aa->aa_node->ad_handle, "_SRS", &sc->sc_srs);
    195 	if (sc->sc_crs && sc->sc_srs) {
    196 		/* XXX Why need this? */
    197 		sc->sc_crs_buffer.Pointer = NULL;
    198 		sc->sc_crs_buffer.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
    199 		rv = AcpiGetCurrentResources(sc->sc_crs, &sc->sc_crs_buffer);
    200 		if (ACPI_FAILURE(rv))
    201 			sc->sc_crs = sc->sc_srs = NULL;
    202 	}
    203 
    204 	mem = acpi_res_mem(&res, 0);
    205 	irq = acpi_res_irq(&res, 0);
    206 	if (mem == NULL || irq == NULL) {
    207 		aprint_error_dev(self, "incomplete resources\n");
    208 		goto cleanup;
    209 	}
    210 	if (mem->ar_length == 0) {
    211 		aprint_error_dev(self, "zero length memory resource\n");
    212 		goto cleanup;
    213 	}
    214 	sc->sc_memsize = mem->ar_length;
    215 
    216 	if (bus_space_map(sc->sc_memt, mem->ar_base, sc->sc_memsize, 0,
    217 	    &sc->sc_memh)) {
    218 		aprint_error_dev(self, "couldn't map registers\n");
    219 		goto cleanup;
    220 	}
    221 
    222 	sc->sc_ih = acpi_intr_establish(self,
    223 	    (uint64_t)(uintptr_t)aa->aa_node->ad_handle,
    224 	    IPL_BIO, false, sdhc_intr, &sc->sc, device_xname(self));
    225 	if (sc->sc_ih == NULL) {
    226 		aprint_error_dev(self,
    227 		    "couldn't establish interrupt handler\n");
    228 		goto unmap;
    229 	}
    230 
    231 	sc->sc.sc_host = kmem_zalloc(sizeof(struct sdhc_host *), KM_SLEEP);
    232 
    233 	sc->sc.sc_flags |= slot->flags;
    234 
    235 	/* Enable DMA transfer */
    236 	sc->sc.sc_flags |= SDHC_FLAG_USE_DMA;
    237 
    238 	/* Read clock frequency from device properties */
    239 	rv = acpi_dsd_integer(aa->aa_node->ad_handle, "clock-frequency",
    240 	    &clock_freq);
    241 	if (ACPI_SUCCESS(rv)) {
    242 		sc->sc.sc_clkbase = clock_freq / 1000;
    243 		sc->sc.sc_flags |= SDHC_FLAG_NO_CLKBASE;
    244 	}
    245 
    246 	/* Capability overrides */
    247 	caps = caps_mask = 0;
    248 	acpi_dsd_integer(aa->aa_node->ad_handle, "sdhci-caps-mask", &caps_mask);
    249 	acpi_dsd_integer(aa->aa_node->ad_handle, "sdhci-caps", &caps);
    250 	if (caps || caps_mask) {
    251 		sc->sc.sc_caps = bus_space_read_4(sc->sc_memt, sc->sc_memh,
    252 		    SDHC_CAPABILITIES);
    253 		sc->sc.sc_caps &= ~(caps_mask & 0xffffffff);
    254 		sc->sc.sc_caps |= (caps & 0xffffffff);
    255 		sc->sc.sc_caps2 = bus_space_read_4(sc->sc_memt,
    256 		    sc->sc_memh, SDHC_CAPABILITIES2);
    257 		sc->sc.sc_caps2 &= ~(caps_mask >> 32);
    258 		sc->sc.sc_caps2 |= (caps >> 32);
    259 		sc->sc.sc_flags |= SDHC_FLAG_HOSTCAPS;
    260 	}
    261 
    262 	if (sdhc_host_found(&sc->sc, sc->sc_memt, sc->sc_memh,
    263 	    sc->sc_memsize) != 0) {
    264 		aprint_error_dev(self, "couldn't initialize host\n");
    265 		goto fail;
    266 	}
    267 
    268 	if (!pmf_device_register1(self, sdhc_suspend, sdhc_acpi_resume,
    269 	    sdhc_shutdown)) {
    270 		aprint_error_dev(self, "couldn't establish powerhook\n");
    271 	}
    272 
    273 	acpi_resource_cleanup(&res);
    274 	return;
    275 
    276 fail:
    277 	if (sc->sc.sc_host != NULL)
    278 		kmem_free(sc->sc.sc_host, sizeof(struct sdhc_host *));
    279 	sc->sc.sc_host = NULL;
    280 	if (sc->sc_ih != NULL)
    281 		acpi_intr_disestablish(sc->sc_ih);
    282 	sc->sc_ih = NULL;
    283 unmap:
    284 	bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_memsize);
    285 	sc->sc_memsize = 0;
    286 cleanup:
    287 	if (sc->sc_crs_buffer.Pointer)
    288 		ACPI_FREE(sc->sc_crs_buffer.Pointer);
    289 	sc->sc_crs_buffer.Pointer = NULL;
    290 	acpi_resource_cleanup(&res);
    291 }
    292 
    293 static int
    294 sdhc_acpi_detach(device_t self, int flags)
    295 {
    296 	struct sdhc_acpi_softc *sc = device_private(self);
    297 	int rv;
    298 
    299 	pmf_device_deregister(self);
    300 
    301 	rv = sdhc_detach(&sc->sc, flags);
    302 	if (rv)
    303 		return rv;
    304 
    305 	if (sc->sc_ih != NULL)
    306 		acpi_intr_disestablish(sc->sc_ih);
    307 
    308 	if (sc->sc.sc_host != NULL)
    309 		kmem_free(sc->sc.sc_host, sizeof(struct sdhc_host *));
    310 
    311 	if (sc->sc_memsize > 0)
    312 		bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_memsize);
    313 
    314 	if (sc->sc_crs_buffer.Pointer)
    315 		ACPI_FREE(sc->sc_crs_buffer.Pointer);
    316 
    317 	return 0;
    318 }
    319 
    320 static bool
    321 sdhc_acpi_resume(device_t self, const pmf_qual_t *qual)
    322 {
    323 	struct sdhc_acpi_softc *sc = device_private(self);
    324 	ACPI_STATUS rv;
    325 
    326 	if (sc->sc_crs && sc->sc_srs) {
    327 		rv = AcpiSetCurrentResources(sc->sc_srs, &sc->sc_crs_buffer);
    328 		if (ACPI_FAILURE(rv))
    329 			printf("%s: _SRS failed: %s\n",
    330 			    device_xname(self), AcpiFormatException(rv));
    331 	}
    332 
    333 	return sdhc_resume(self, qual);
    334 }
    335 
    336 static void
    337 sdhc_acpi_intel_emmc_hw_reset(struct sdhc_softc *sc, struct sdhc_host *hp)
    338 {
    339 	kmutex_t *plock = sdhc_host_lock(hp);
    340 	uint8_t reg;
    341 
    342 	mutex_enter(plock);
    343 
    344 	reg = sdhc_host_read_1(hp, SDHC_POWER_CTL);
    345 	reg |= 0x10;
    346 	sdhc_host_write_1(hp, SDHC_POWER_CTL, reg);
    347 
    348 	sdmmc_delay(10);
    349 
    350 	reg &= ~0x10;
    351 	sdhc_host_write_1(hp, SDHC_POWER_CTL, reg);
    352 
    353 	sdmmc_delay(1000);
    354 
    355 	mutex_exit(plock);
    356 }
    357 
    358 static int
    359 sdhc_acpi_rockchip_bus_clock(struct sdhc_softc *sc, int freq)
    360 {
    361 	struct sdhc_acpi_softc *asc = (struct sdhc_acpi_softc *)sc;
    362 	ACPI_STATUS rv;
    363 	ACPI_OBJECT targetfreq;
    364 	ACPI_OBJECT arg3;
    365 	ACPI_INTEGER actfreq;
    366 
    367 	targetfreq.Integer.Type = ACPI_TYPE_INTEGER;
    368 	targetfreq.Integer.Value = freq * 1000;
    369 	arg3.Package.Type = ACPI_TYPE_PACKAGE;
    370 	arg3.Package.Count = 1;
    371 	arg3.Package.Elements = &targetfreq;
    372 
    373 	rv = acpi_dsm_integer(asc->sc_handle, sdhc_acpi_rockchip_dsm_uuid,
    374 	    ROCKCHIP_DSM_REV, ROCKCHIP_DSM_FUNC_SET_CARD_CLOCK, &arg3,
    375 	    &actfreq);
    376 	if (ACPI_FAILURE(rv)) {
    377 		aprint_error_dev(sc->sc_dev,
    378 		    "eMMC Set Card Clock DSM failed: %s\n",
    379 		    AcpiFormatException(rv));
    380 		return ENXIO;
    381 	}
    382 
    383 	aprint_debug_dev(sc->sc_dev,
    384 	    "eMMC Set Card Clock DSM returned %lu Hz\n", actfreq);
    385 	if (actfreq == 0) {
    386 		return EINVAL;
    387 	}
    388 
    389 	return 0;
    390 }
    391