1 1.4 jmcneill /* $NetBSD: bcm2838_emmc2_acpi.c,v 1.4 2025/09/25 01:19:15 jmcneill Exp $ */ 2 1.1 jmcneill 3 1.1 jmcneill /* 4 1.1 jmcneill * Copyright (c) 2021 Jared McNeill <jmcneill (at) invisible.ca> 5 1.1 jmcneill * All rights reserved. 6 1.1 jmcneill * 7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without 8 1.1 jmcneill * modification, are permitted provided that the following conditions 9 1.1 jmcneill * are met: 10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright 11 1.1 jmcneill * notice, this list of conditions and the following disclaimer. 12 1.1 jmcneill * 2. The name of the author may not be used to endorse or promote products 13 1.1 jmcneill * derived from this software without specific prior written permission. 14 1.1 jmcneill * 15 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 20 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 22 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 1.1 jmcneill * SUCH DAMAGE. 26 1.1 jmcneill */ 27 1.1 jmcneill 28 1.1 jmcneill #include <sys/cdefs.h> 29 1.4 jmcneill __KERNEL_RCSID(0, "$NetBSD: bcm2838_emmc2_acpi.c,v 1.4 2025/09/25 01:19:15 jmcneill Exp $"); 30 1.1 jmcneill 31 1.1 jmcneill #include <sys/param.h> 32 1.1 jmcneill #include <sys/device.h> 33 1.1 jmcneill #include <sys/systm.h> 34 1.1 jmcneill #include <sys/kmem.h> 35 1.1 jmcneill 36 1.1 jmcneill #include <dev/acpi/acpireg.h> 37 1.1 jmcneill #include <dev/acpi/acpivar.h> 38 1.1 jmcneill #include <dev/acpi/acpi_intr.h> 39 1.1 jmcneill 40 1.1 jmcneill #include <dev/sdmmc/sdhcreg.h> 41 1.1 jmcneill #include <dev/sdmmc/sdhcvar.h> 42 1.1 jmcneill #include <dev/sdmmc/sdmmcvar.h> 43 1.1 jmcneill 44 1.1 jmcneill #define _COMPONENT ACPI_RESOURCE_COMPONENT 45 1.1 jmcneill ACPI_MODULE_NAME ("bcmemmc2_acpi") 46 1.1 jmcneill 47 1.1 jmcneill static int bcmemmc2_acpi_match(device_t, cfdata_t, void *); 48 1.1 jmcneill static void bcmemmc2_acpi_attach(device_t, device_t, void *); 49 1.1 jmcneill static void bcmemmc2_acpi_attach1(device_t); 50 1.1 jmcneill 51 1.1 jmcneill static const char * const compatible[] = { 52 1.1 jmcneill "BRCME88C", 53 1.1 jmcneill NULL 54 1.1 jmcneill }; 55 1.1 jmcneill 56 1.1 jmcneill struct bcmemmc2_acpi_softc { 57 1.1 jmcneill struct sdhc_softc sc; 58 1.1 jmcneill bus_space_tag_t sc_memt; 59 1.1 jmcneill bus_space_handle_t sc_memh; 60 1.1 jmcneill bus_size_t sc_memsize; 61 1.1 jmcneill void *sc_ih; 62 1.1 jmcneill struct sdhc_host *sc_hosts[1]; 63 1.1 jmcneill }; 64 1.1 jmcneill 65 1.1 jmcneill CFATTACH_DECL_NEW(bcmemmc2_acpi, sizeof(struct bcmemmc2_acpi_softc), 66 1.1 jmcneill bcmemmc2_acpi_match, bcmemmc2_acpi_attach, NULL, NULL); 67 1.1 jmcneill 68 1.1 jmcneill static int 69 1.1 jmcneill bcmemmc2_acpi_match(device_t parent, cfdata_t match, void *opaque) 70 1.1 jmcneill { 71 1.1 jmcneill struct acpi_attach_args *aa = opaque; 72 1.1 jmcneill 73 1.1 jmcneill if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE) 74 1.1 jmcneill return 0; 75 1.1 jmcneill 76 1.1 jmcneill return acpi_match_hid(aa->aa_node->ad_devinfo, compatible); 77 1.1 jmcneill } 78 1.1 jmcneill 79 1.1 jmcneill static void 80 1.1 jmcneill bcmemmc2_acpi_attach(device_t parent, device_t self, void *opaque) 81 1.1 jmcneill { 82 1.1 jmcneill struct bcmemmc2_acpi_softc *sc = device_private(self); 83 1.1 jmcneill struct acpi_attach_args *aa = opaque; 84 1.1 jmcneill struct acpi_resources res; 85 1.1 jmcneill struct acpi_mem *mem; 86 1.1 jmcneill struct acpi_irq *irq; 87 1.1 jmcneill ACPI_STATUS rv; 88 1.1 jmcneill 89 1.1 jmcneill sc->sc.sc_dev = self; 90 1.1 jmcneill sc->sc.sc_dmat = aa->aa_dmat; 91 1.1 jmcneill sc->sc.sc_host = sc->sc_hosts; 92 1.1 jmcneill sc->sc_memt = aa->aa_memt; 93 1.1 jmcneill 94 1.1 jmcneill rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS", 95 1.1 jmcneill &res, &acpi_resource_parse_ops_default); 96 1.1 jmcneill if (ACPI_FAILURE(rv)) 97 1.1 jmcneill return; 98 1.1 jmcneill 99 1.1 jmcneill mem = acpi_res_mem(&res, 0); 100 1.1 jmcneill irq = acpi_res_irq(&res, 0); 101 1.1 jmcneill if (mem == NULL || irq == NULL) { 102 1.1 jmcneill aprint_error_dev(self, "incomplete resources\n"); 103 1.1 jmcneill goto cleanup; 104 1.1 jmcneill } 105 1.1 jmcneill if (mem->ar_length == 0) { 106 1.1 jmcneill aprint_error_dev(self, "zero length memory resource\n"); 107 1.1 jmcneill goto cleanup; 108 1.1 jmcneill } 109 1.1 jmcneill sc->sc_memsize = mem->ar_length; 110 1.1 jmcneill 111 1.1 jmcneill if (bus_space_map(sc->sc_memt, mem->ar_base, sc->sc_memsize, 0, 112 1.1 jmcneill &sc->sc_memh)) { 113 1.1 jmcneill aprint_error_dev(self, "couldn't map registers\n"); 114 1.1 jmcneill goto cleanup; 115 1.1 jmcneill } 116 1.1 jmcneill 117 1.1 jmcneill /* 118 1.1 jmcneill * The controller doesn't honour standard SDHCI voltage registers, so 119 1.1 jmcneill * disable UHS modes. 120 1.1 jmcneill */ 121 1.1 jmcneill sc->sc.sc_flags = SDHC_FLAG_32BIT_ACCESS | 122 1.4 jmcneill SDHC_FLAG_NO_PWR0 | 123 1.2 jmcneill #if notyet 124 1.1 jmcneill SDHC_FLAG_USE_DMA | 125 1.2 jmcneill #endif 126 1.1 jmcneill SDHC_FLAG_NO_1_8_V; 127 1.1 jmcneill 128 1.1 jmcneill sc->sc_ih = acpi_intr_establish(self, 129 1.1 jmcneill (uint64_t)(uintptr_t)aa->aa_node->ad_handle, 130 1.1 jmcneill IPL_BIO, true, sdhc_intr, &sc->sc, device_xname(self)); 131 1.1 jmcneill if (sc->sc_ih == NULL) { 132 1.1 jmcneill aprint_error_dev(self, 133 1.1 jmcneill "couldn't establish interrupt handler\n"); 134 1.1 jmcneill goto unmap; 135 1.1 jmcneill } 136 1.1 jmcneill 137 1.1 jmcneill config_defer(self, bcmemmc2_acpi_attach1); 138 1.1 jmcneill 139 1.1 jmcneill acpi_resource_cleanup(&res); 140 1.1 jmcneill return; 141 1.1 jmcneill 142 1.1 jmcneill unmap: 143 1.1 jmcneill bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_memsize); 144 1.1 jmcneill sc->sc_memsize = 0; 145 1.1 jmcneill cleanup: 146 1.1 jmcneill acpi_resource_cleanup(&res); 147 1.1 jmcneill } 148 1.1 jmcneill 149 1.1 jmcneill static void 150 1.1 jmcneill bcmemmc2_acpi_attach1(device_t self) 151 1.1 jmcneill { 152 1.1 jmcneill struct bcmemmc2_acpi_softc *sc = device_private(self); 153 1.1 jmcneill 154 1.1 jmcneill if (sdhc_host_found(&sc->sc, sc->sc_memt, sc->sc_memh, 155 1.1 jmcneill sc->sc_memsize) != 0) { 156 1.1 jmcneill aprint_error_dev(self, "couldn't initialize host\n"); 157 1.1 jmcneill goto fail; 158 1.1 jmcneill } 159 1.1 jmcneill 160 1.1 jmcneill return; 161 1.1 jmcneill 162 1.1 jmcneill fail: 163 1.1 jmcneill if (sc->sc_ih != NULL) 164 1.1 jmcneill acpi_intr_disestablish(sc->sc_ih); 165 1.1 jmcneill sc->sc_ih = NULL; 166 1.1 jmcneill } 167