1 /* $NetBSD: bcm2838_emmc2_acpi.c,v 1.3 2022/02/06 15:52:20 jmcneill Exp $ */ 2 3 /* 4 * Copyright (c) 2021 Jared McNeill <jmcneill (at) invisible.ca> 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: bcm2838_emmc2_acpi.c,v 1.3 2022/02/06 15:52:20 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 ("bcmemmc2_acpi") 46 47 static int bcmemmc2_acpi_match(device_t, cfdata_t, void *); 48 static void bcmemmc2_acpi_attach(device_t, device_t, void *); 49 static void bcmemmc2_acpi_attach1(device_t); 50 51 static const char * const compatible[] = { 52 "BRCME88C", 53 NULL 54 }; 55 56 struct bcmemmc2_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 struct sdhc_host *sc_hosts[1]; 63 }; 64 65 CFATTACH_DECL_NEW(bcmemmc2_acpi, sizeof(struct bcmemmc2_acpi_softc), 66 bcmemmc2_acpi_match, bcmemmc2_acpi_attach, NULL, NULL); 67 68 static int 69 bcmemmc2_acpi_match(device_t parent, cfdata_t match, void *opaque) 70 { 71 struct acpi_attach_args *aa = opaque; 72 73 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE) 74 return 0; 75 76 return acpi_match_hid(aa->aa_node->ad_devinfo, compatible); 77 } 78 79 static void 80 bcmemmc2_acpi_attach(device_t parent, device_t self, void *opaque) 81 { 82 struct bcmemmc2_acpi_softc *sc = device_private(self); 83 struct acpi_attach_args *aa = opaque; 84 struct acpi_resources res; 85 struct acpi_mem *mem; 86 struct acpi_irq *irq; 87 ACPI_STATUS rv; 88 89 sc->sc.sc_dev = self; 90 sc->sc.sc_dmat = aa->aa_dmat; 91 sc->sc.sc_host = sc->sc_hosts; 92 sc->sc_memt = aa->aa_memt; 93 94 rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS", 95 &res, &acpi_resource_parse_ops_default); 96 if (ACPI_FAILURE(rv)) 97 return; 98 99 mem = acpi_res_mem(&res, 0); 100 irq = acpi_res_irq(&res, 0); 101 if (mem == NULL || irq == NULL) { 102 aprint_error_dev(self, "incomplete resources\n"); 103 goto cleanup; 104 } 105 if (mem->ar_length == 0) { 106 aprint_error_dev(self, "zero length memory resource\n"); 107 goto cleanup; 108 } 109 sc->sc_memsize = mem->ar_length; 110 111 if (bus_space_map(sc->sc_memt, mem->ar_base, sc->sc_memsize, 0, 112 &sc->sc_memh)) { 113 aprint_error_dev(self, "couldn't map registers\n"); 114 goto cleanup; 115 } 116 117 /* 118 * The controller doesn't honour standard SDHCI voltage registers, so 119 * disable UHS modes. 120 */ 121 sc->sc.sc_flags = SDHC_FLAG_32BIT_ACCESS | 122 #if notyet 123 SDHC_FLAG_USE_DMA | 124 #endif 125 SDHC_FLAG_NO_1_8_V; 126 127 sc->sc_ih = acpi_intr_establish(self, 128 (uint64_t)(uintptr_t)aa->aa_node->ad_handle, 129 IPL_BIO, true, sdhc_intr, &sc->sc, device_xname(self)); 130 if (sc->sc_ih == NULL) { 131 aprint_error_dev(self, 132 "couldn't establish interrupt handler\n"); 133 goto unmap; 134 } 135 136 config_defer(self, bcmemmc2_acpi_attach1); 137 138 acpi_resource_cleanup(&res); 139 return; 140 141 unmap: 142 bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_memsize); 143 sc->sc_memsize = 0; 144 cleanup: 145 acpi_resource_cleanup(&res); 146 } 147 148 static void 149 bcmemmc2_acpi_attach1(device_t self) 150 { 151 struct bcmemmc2_acpi_softc *sc = device_private(self); 152 153 if (sdhc_host_found(&sc->sc, sc->sc_memt, sc->sc_memh, 154 sc->sc_memsize) != 0) { 155 aprint_error_dev(self, "couldn't initialize host\n"); 156 goto fail; 157 } 158 159 return; 160 161 fail: 162 if (sc->sc_ih != NULL) 163 acpi_intr_disestablish(sc->sc_ih); 164 sc->sc_ih = NULL; 165 } 166