1 /* $OpenBSD: sdhc_acpi.c,v 1.22 2022/10/08 19:46:52 kettenis Exp $ */ 2 /* 3 * Copyright (c) 2016 Mark Kettenis 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include <sys/param.h> 19 #include <sys/malloc.h> 20 #include <sys/systm.h> 21 22 #include <dev/acpi/acpireg.h> 23 #include <dev/acpi/acpivar.h> 24 #include <dev/acpi/acpidev.h> 25 #include <dev/acpi/amltypes.h> 26 #include <dev/acpi/dsdt.h> 27 #undef DEVNAME 28 #include <dev/sdmmc/sdhcreg.h> 29 #include <dev/sdmmc/sdhcvar.h> 30 #include <dev/sdmmc/sdmmcvar.h> 31 32 struct sdhc_acpi_softc { 33 struct sdhc_softc sc; 34 struct acpi_softc *sc_acpi; 35 struct aml_node *sc_node; 36 37 bus_space_tag_t sc_memt; 38 bus_space_handle_t sc_memh; 39 void *sc_ih; 40 41 struct aml_node *sc_gpio_int_node; 42 struct aml_node *sc_gpio_io_node; 43 uint16_t sc_gpio_int_pin; 44 uint16_t sc_gpio_int_flags; 45 uint16_t sc_gpio_io_pin; 46 47 struct sdhc_host *sc_host; 48 }; 49 50 int sdhc_acpi_match(struct device *, void *, void *); 51 void sdhc_acpi_attach(struct device *, struct device *, void *); 52 53 const struct cfattach sdhc_acpi_ca = { 54 sizeof(struct sdhc_acpi_softc), sdhc_acpi_match, sdhc_acpi_attach 55 }; 56 57 const char *sdhc_hids[] = { 58 "PNP0D40", 59 "80860F14", 60 "BCM2847", /* Raspberry Pi3/4 "arasan" controller */ 61 "BRCME88C", /* Raspberry Pi4 "emmc2" controller */ 62 "INT33BB", 63 "PNP0FFF", 64 "QCOM24BF", 65 NULL 66 }; 67 68 int sdhc_acpi_parse_resources(int, union acpi_resource *, void *); 69 int sdhc_acpi_card_detect_nonremovable(struct sdhc_softc *); 70 int sdhc_acpi_card_detect_gpio(struct sdhc_softc *); 71 int sdhc_acpi_card_detect_intr(void *); 72 void sdhc_acpi_power_on(struct sdhc_acpi_softc *, struct aml_node *); 73 void sdhc_acpi_explore(struct sdhc_acpi_softc *); 74 75 int 76 sdhc_acpi_match(struct device *parent, void *match, void *aux) 77 { 78 struct acpi_attach_args *aaa = aux; 79 struct cfdata *cf = match; 80 81 if (aaa->aaa_naddr < 1 || aaa->aaa_nirq < 1) 82 return 0; 83 return acpi_matchhids(aaa, sdhc_hids, cf->cf_driver->cd_name); 84 } 85 86 void 87 sdhc_acpi_attach(struct device *parent, struct device *self, void *aux) 88 { 89 struct sdhc_acpi_softc *sc = (struct sdhc_acpi_softc *)self; 90 struct acpi_attach_args *aaa = aux; 91 struct aml_value res; 92 uint64_t capmask, capset; 93 94 sc->sc_acpi = (struct acpi_softc *)parent; 95 sc->sc_node = aaa->aaa_node; 96 printf(" %s", sc->sc_node->name); 97 98 if (aml_evalname(sc->sc_acpi, sc->sc_node, "_CRS", 0, NULL, &res)) { 99 printf(": can't find registers\n"); 100 return; 101 } 102 103 aml_parse_resource(&res, sdhc_acpi_parse_resources, sc); 104 105 printf(" addr 0x%llx/0x%llx", aaa->aaa_addr[0], aaa->aaa_size[0]); 106 printf(" irq %d", aaa->aaa_irq[0]); 107 108 sc->sc_memt = aaa->aaa_bst[0]; 109 if (bus_space_map(sc->sc_memt, aaa->aaa_addr[0], aaa->aaa_size[0], 110 0, &sc->sc_memh)) { 111 printf(": can't map registers\n"); 112 return; 113 } 114 115 sc->sc_ih = acpi_intr_establish(aaa->aaa_irq[0], aaa->aaa_irq_flags[0], 116 IPL_BIO, sdhc_intr, sc, sc->sc.sc_dev.dv_xname); 117 if (sc->sc_ih == NULL) { 118 printf(": can't establish interrupt\n"); 119 return; 120 } 121 122 if (sc->sc_gpio_io_node && sc->sc_gpio_io_node->gpio) { 123 sc->sc.sc_card_detect = sdhc_acpi_card_detect_gpio; 124 printf(", gpio"); 125 } 126 127 printf("\n"); 128 129 if (sc->sc_gpio_int_node && sc->sc_gpio_int_node->gpio) { 130 struct acpi_gpio *gpio = sc->sc_gpio_int_node->gpio; 131 132 gpio->intr_establish(gpio->cookie, sc->sc_gpio_int_pin, 133 sc->sc_gpio_int_flags, sdhc_acpi_card_detect_intr, sc); 134 } 135 136 sdhc_acpi_power_on(sc, sc->sc_node); 137 sdhc_acpi_explore(sc); 138 139 capmask = acpi_getpropint(sc->sc_node, "sdhci-caps-mask", 0); 140 capset = acpi_getpropint(sc->sc_node, "sdhci-caps", 0); 141 142 /* Raspberry Pi4 "emmc2" controller. */ 143 if (strcmp(aaa->aaa_dev, "BRCME88C") == 0) 144 sc->sc.sc_flags |= SDHC_F_NOPWR0; 145 146 sc->sc.sc_host = &sc->sc_host; 147 sc->sc.sc_dmat = aaa->aaa_dmat; 148 sc->sc.sc_clkbase = acpi_getpropint(sc->sc_node, 149 "clock-frequency", 0) / 1000; 150 sdhc_host_found(&sc->sc, sc->sc_memt, sc->sc_memh, 151 aaa->aaa_size[0], 1, capmask, capset); 152 } 153 154 int 155 sdhc_acpi_parse_resources(int crsidx, union acpi_resource *crs, void *arg) 156 { 157 struct sdhc_acpi_softc *sc = arg; 158 int type = AML_CRSTYPE(crs); 159 struct aml_node *node; 160 uint16_t pin; 161 162 switch (type) { 163 case LR_GPIO: 164 node = aml_searchname(sc->sc_node, (char *)&crs->pad[crs->lr_gpio.res_off]); 165 pin = *(uint16_t *)&crs->pad[crs->lr_gpio.pin_off]; 166 if (crs->lr_gpio.type == LR_GPIO_INT) { 167 sc->sc_gpio_int_node = node; 168 sc->sc_gpio_int_pin = pin; 169 sc->sc_gpio_int_flags = crs->lr_gpio.tflags; 170 } else if (crs->lr_gpio.type == LR_GPIO_IO) { 171 sc->sc_gpio_io_node = node; 172 sc->sc_gpio_io_pin = pin; 173 } 174 break; 175 } 176 177 return 0; 178 } 179 180 int 181 sdhc_acpi_card_detect_nonremovable(struct sdhc_softc *ssc) 182 { 183 return 1; 184 } 185 186 int 187 sdhc_acpi_card_detect_gpio(struct sdhc_softc *ssc) 188 { 189 struct sdhc_acpi_softc *sc = (struct sdhc_acpi_softc *)ssc; 190 struct acpi_gpio *gpio = sc->sc_gpio_io_node->gpio; 191 uint16_t pin = sc->sc_gpio_io_pin; 192 193 /* Card detect GPIO signal is active-low. */ 194 return !gpio->read_pin(gpio->cookie, pin); 195 } 196 197 int 198 sdhc_acpi_card_detect_intr(void *arg) 199 { 200 struct sdhc_acpi_softc *sc = arg; 201 202 sdhc_needs_discover(&sc->sc); 203 204 return (1); 205 } 206 207 void 208 sdhc_acpi_power_on(struct sdhc_acpi_softc *sc, struct aml_node *node) 209 { 210 node = aml_searchname(node, "_PS0"); 211 if (node && aml_evalnode(sc->sc_acpi, node, 0, NULL, NULL)) 212 printf("%s: _PS0 failed\n", sc->sc.sc_dev.dv_xname); 213 } 214 215 int 216 sdhc_acpi_do_explore(struct aml_node *node, void *arg) 217 { 218 struct sdhc_acpi_softc *sc = arg; 219 int64_t sta, rmv; 220 221 /* We're only interested in our children. */ 222 if (node == sc->sc_node) 223 return 0; 224 225 /* Only consider devices that are actually present. */ 226 if (node->value == NULL || 227 node->value->type != AML_OBJTYPE_DEVICE) 228 return 1; 229 if (aml_evalinteger(sc->sc_acpi, node, "_STA", 0, NULL, &sta)) 230 sta = STA_PRESENT | STA_ENABLED | STA_DEV_OK | 0x1000; 231 if ((sta & STA_PRESENT) == 0) 232 return 1; 233 234 acpi_attach_deps(sc->sc_acpi, node); 235 236 /* Override card detect if we have non-removable devices. */ 237 if (aml_evalinteger(sc->sc_acpi, node, "_RMV", 0, NULL, &rmv)) 238 rmv = 1; 239 if (rmv == 0) { 240 sc->sc.sc_flags |= SDHC_F_NONREMOVABLE; 241 if (sc->sc.sc_card_detect == NULL) { 242 sc->sc.sc_card_detect = 243 sdhc_acpi_card_detect_nonremovable; 244 } 245 } 246 247 sdhc_acpi_power_on(sc, node); 248 249 return 1; 250 } 251 252 void 253 sdhc_acpi_explore(struct sdhc_acpi_softc *sc) 254 { 255 aml_walknodes(sc->sc_node, AML_WALK_PRE, sdhc_acpi_do_explore, sc); 256 } 257