11.3Sskrll/* $NetBSD: dwcmmc_acpi.c,v 1.3 2024/02/09 16:53:15 skrll Exp $ */ 21.1Sjmcneill 31.1Sjmcneill/*- 41.1Sjmcneill * Copyright (c) 2022 Jared McNeill <jmcneill@invisible.ca> 51.1Sjmcneill * All rights reserved. 61.1Sjmcneill * 71.1Sjmcneill * Redistribution and use in source and binary forms, with or without 81.1Sjmcneill * modification, are permitted provided that the following conditions 91.1Sjmcneill * are met: 101.1Sjmcneill * 1. Redistributions of source code must retain the above copyright 111.1Sjmcneill * notice, this list of conditions and the following disclaimer. 121.1Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright 131.1Sjmcneill * notice, this list of conditions and the following disclaimer in the 141.1Sjmcneill * documentation and/or other materials provided with the distribution. 151.1Sjmcneill * 161.1Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 171.1Sjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 181.1Sjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 191.1Sjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 201.1Sjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 211.1Sjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 221.1Sjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 231.1Sjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 241.1Sjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 251.1Sjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 261.1Sjmcneill * SUCH DAMAGE. 271.1Sjmcneill */ 281.1Sjmcneill 291.1Sjmcneill#include <sys/cdefs.h> 301.3Sskrll__KERNEL_RCSID(0, "$NetBSD: dwcmmc_acpi.c,v 1.3 2024/02/09 16:53:15 skrll Exp $"); 311.1Sjmcneill 321.1Sjmcneill#include <sys/param.h> 331.1Sjmcneill#include <sys/bus.h> 341.1Sjmcneill#include <sys/cpu.h> 351.1Sjmcneill#include <sys/device.h> 361.1Sjmcneill 371.3Sskrll#include <dev/ic/dwc_mmc_var.h> 381.3Sskrll#include <dev/ic/dwc_mmc_reg.h> 391.3Sskrll#include <dev/sdmmc/sdmmcchip.h> 401.1Sjmcneill 411.1Sjmcneill#include <dev/acpi/acpireg.h> 421.1Sjmcneill#include <dev/acpi/acpivar.h> 431.1Sjmcneill#include <dev/acpi/acpi_intr.h> 441.1Sjmcneill 451.1Sjmcneill/* 461.1Sjmcneill * FIXME: Remove separate compat_data arrays once acpi_compatible_lookup 471.1Sjmcneill * grows support for DT link devices. 481.1Sjmcneill */ 491.1Sjmcneillstatic const struct device_compatible_entry rockchip_compat_data[] = { 501.1Sjmcneill { .compat = "rockchip,rk3288-dw-mshc" }, 511.1Sjmcneill DEVICE_COMPAT_EOL 521.1Sjmcneill}; 531.1Sjmcneillstatic const struct device_compatible_entry compat_data[] = { 541.1Sjmcneill { .compat = "snps,dw-mshc" }, 551.1Sjmcneill DEVICE_COMPAT_EOL 561.1Sjmcneill}; 571.1Sjmcneill 581.1Sjmcneillstatic int dwcmmc_acpi_match(device_t, cfdata_t, void *); 591.1Sjmcneillstatic void dwcmmc_acpi_attach(device_t, device_t, void *); 601.1Sjmcneill 611.1Sjmcneillstatic int dwcmmc_acpi_init_props(struct dwc_mmc_softc *, ACPI_HANDLE); 621.1Sjmcneill 631.1SjmcneillCFATTACH_DECL_NEW(dwcmmc_acpi, sizeof(struct dwc_mmc_softc), 641.1Sjmcneill dwcmmc_acpi_match, dwcmmc_acpi_attach, NULL, NULL); 651.1Sjmcneill 661.1Sjmcneillstatic int 671.1Sjmcneilldwcmmc_acpi_match(device_t parent, cfdata_t cf, void *aux) 681.1Sjmcneill{ 691.1Sjmcneill struct acpi_attach_args *aa = aux; 701.1Sjmcneill int match; 711.1Sjmcneill 721.1Sjmcneill match = acpi_compatible_match(aa, rockchip_compat_data); 731.1Sjmcneill if (!match) { 741.1Sjmcneill match = acpi_compatible_match(aa, compat_data); 751.1Sjmcneill } 761.1Sjmcneill 771.1Sjmcneill return match; 781.1Sjmcneill} 791.1Sjmcneill 801.1Sjmcneillstatic void 811.1Sjmcneilldwcmmc_acpi_attach(device_t parent, device_t self, void *aux) 821.1Sjmcneill{ 831.1Sjmcneill struct dwc_mmc_softc * const sc = device_private(self); 841.1Sjmcneill struct acpi_attach_args *aa = aux; 851.1Sjmcneill ACPI_HANDLE handle = aa->aa_node->ad_handle; 861.1Sjmcneill struct acpi_resources res; 871.1Sjmcneill struct acpi_mem *mem; 881.1Sjmcneill struct acpi_irq *irq; 891.1Sjmcneill ACPI_STATUS rv; 901.1Sjmcneill int error; 911.1Sjmcneill void *ih; 921.1Sjmcneill 931.1Sjmcneill sc->sc_dev = self; 941.1Sjmcneill 951.1Sjmcneill rv = acpi_resource_parse(sc->sc_dev, handle, "_CRS", 961.1Sjmcneill &res, &acpi_resource_parse_ops_default); 971.1Sjmcneill if (ACPI_FAILURE(rv)) { 981.1Sjmcneill return; 991.1Sjmcneill } 1001.1Sjmcneill 1011.1Sjmcneill sc->sc_flags = DWC_MMC_F_USE_HOLD_REG | DWC_MMC_F_DMA; 1021.1Sjmcneill 1031.1Sjmcneill if (acpi_compatible_match(aa, rockchip_compat_data)) { 1041.1Sjmcneill sc->sc_ciu_div = 2; 1051.1Sjmcneill sc->sc_intr_cardmask = DWC_MMC_INT_SDIO_INT(8); 1061.1Sjmcneill } 1071.1Sjmcneill 1081.1Sjmcneill mem = acpi_res_mem(&res, 0); 1091.1Sjmcneill if (mem == NULL) { 1101.1Sjmcneill aprint_error_dev(self, "couldn't find mem resource\n"); 1111.1Sjmcneill goto done; 1121.1Sjmcneill } 1131.1Sjmcneill 1141.1Sjmcneill irq = acpi_res_irq(&res, 0); 1151.1Sjmcneill if (irq == NULL) { 1161.1Sjmcneill aprint_error_dev(self, "couldn't find irq resource\n"); 1171.1Sjmcneill goto done; 1181.1Sjmcneill } 1191.1Sjmcneill 1201.1Sjmcneill sc->sc_bst = aa->aa_memt; 1211.1Sjmcneill error = bus_space_map(sc->sc_bst, mem->ar_base, mem->ar_length, 1221.1Sjmcneill 0, &sc->sc_bsh); 1231.1Sjmcneill if (error != 0) { 1241.1Sjmcneill aprint_error_dev(self, "couldn't map registers\n"); 1251.1Sjmcneill goto done; 1261.1Sjmcneill } 1271.1Sjmcneill sc->sc_dmat = aa->aa_dmat; 1281.1Sjmcneill 1291.1Sjmcneill if (dwcmmc_acpi_init_props(sc, handle) != 0) { 1301.1Sjmcneill goto done; 1311.1Sjmcneill } 1321.1Sjmcneill 1331.1Sjmcneill if (dwc_mmc_init(sc) != 0) { 1341.1Sjmcneill goto done; 1351.1Sjmcneill } 1361.1Sjmcneill 1371.1Sjmcneill ih = acpi_intr_establish(self, (uint64_t)(uintptr_t)handle, IPL_NET, 1381.1Sjmcneill false, dwc_mmc_intr, sc, device_xname(self)); 1391.1Sjmcneill if (ih == NULL) { 1401.1Sjmcneill aprint_error_dev(self, "couldn't establish interrupt\n"); 1411.1Sjmcneill goto done; 1421.1Sjmcneill } 1431.1Sjmcneill 1441.1Sjmcneilldone: 1451.1Sjmcneill acpi_resource_cleanup(&res); 1461.1Sjmcneill} 1471.1Sjmcneill 1481.1Sjmcneillstatic int 1491.1Sjmcneilldwcmmc_acpi_init_props(struct dwc_mmc_softc *sc, ACPI_HANDLE handle) 1501.1Sjmcneill{ 1511.1Sjmcneill ACPI_INTEGER ival; 1521.2Sjmcneill bool bval; 1531.1Sjmcneill 1541.1Sjmcneill /* Defaults */ 1551.1Sjmcneill sc->sc_fifo_depth = 0; 1561.1Sjmcneill sc->sc_clock_freq = UINT_MAX; 1571.1Sjmcneill sc->sc_bus_width = 4; 1581.1Sjmcneill 1591.1Sjmcneill /* Get settings from DSD properties */ 1601.1Sjmcneill if (ACPI_SUCCESS(acpi_dsd_integer(handle, "fifo-depth", &ival))) { 1611.1Sjmcneill sc->sc_fifo_depth = ival; 1621.1Sjmcneill } 1631.1Sjmcneill if (ACPI_SUCCESS(acpi_dsd_integer(handle, "max-frequency", &ival))) { 1641.1Sjmcneill sc->sc_clock_freq = ival; 1651.1Sjmcneill } 1661.1Sjmcneill if (ACPI_SUCCESS(acpi_dsd_integer(handle, "bus-width", &ival))) { 1671.1Sjmcneill sc->sc_bus_width = ival; 1681.1Sjmcneill } 1691.2Sjmcneill bval = false; 1701.2Sjmcneill acpi_dsd_bool(handle, "non-removable", &bval); 1711.2Sjmcneill if (bval) { 1721.2Sjmcneill sc->sc_flags |= DWC_MMC_F_NON_REMOVABLE; 1731.2Sjmcneill } 1741.2Sjmcneill bval = false; 1751.2Sjmcneill acpi_dsd_bool(handle, "broken-cd", &bval); 1761.2Sjmcneill if (bval) { 1771.2Sjmcneill sc->sc_flags |= DWC_MMC_F_BROKEN_CD; 1781.2Sjmcneill } 1791.1Sjmcneill 1801.1Sjmcneill if (sc->sc_clock_freq == 0 || sc->sc_clock_freq == UINT_MAX) { 1811.1Sjmcneill aprint_error_dev(sc->sc_dev, "clock frequency not specified\n"); 1821.1Sjmcneill return ENXIO; 1831.1Sjmcneill } 1841.1Sjmcneill 1851.1Sjmcneill return 0; 1861.1Sjmcneill} 187