1 /* $OpenBSD: sdhc_acpi.c,v 1.21 2022/04/06 18:59:27 naddy 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 NULL 65 }; 66 67 int sdhc_acpi_parse_resources(int, union acpi_resource *, void *); 68 int sdhc_acpi_card_detect_nonremovable(struct sdhc_softc *); 69 int sdhc_acpi_card_detect_gpio(struct sdhc_softc *); 70 int sdhc_acpi_card_detect_intr(void *); 71 void sdhc_acpi_power_on(struct sdhc_acpi_softc *, struct aml_node *); 72 void sdhc_acpi_explore(struct sdhc_acpi_softc *); 73 74 int 75 sdhc_acpi_match(struct device *parent, void *match, void *aux) 76 { 77 struct acpi_attach_args *aaa = aux; 78 struct cfdata *cf = match; 79 80 if (aaa->aaa_naddr < 1 || aaa->aaa_nirq < 1) 81 return 0; 82 return acpi_matchhids(aaa, sdhc_hids, cf->cf_driver->cd_name); 83 } 84 85 void 86 sdhc_acpi_attach(struct device *parent, struct device *self, void *aux) 87 { 88 struct sdhc_acpi_softc *sc = (struct sdhc_acpi_softc *)self; 89 struct acpi_attach_args *aaa = aux; 90 struct aml_value res; 91 uint64_t capmask, capset; 92 93 sc->sc_acpi = (struct acpi_softc *)parent; 94 sc->sc_node = aaa->aaa_node; 95 printf(" %s", sc->sc_node->name); 96 97 if (aml_evalname(sc->sc_acpi, sc->sc_node, "_CRS", 0, NULL, &res)) { 98 printf(": can't find registers\n"); 99 return; 100 } 101 102 aml_parse_resource(&res, sdhc_acpi_parse_resources, sc); 103 104 printf(" addr 0x%llx/0x%llx", aaa->aaa_addr[0], aaa->aaa_size[0]); 105 printf(" irq %d", aaa->aaa_irq[0]); 106 107 sc->sc_memt = aaa->aaa_bst[0]; 108 if (bus_space_map(sc->sc_memt, aaa->aaa_addr[0], aaa->aaa_size[0], 109 0, &sc->sc_memh)) { 110 printf(": can't map registers\n"); 111 return; 112 } 113 114 sc->sc_ih = acpi_intr_establish(aaa->aaa_irq[0], aaa->aaa_irq_flags[0], 115 IPL_BIO, sdhc_intr, sc, sc->sc.sc_dev.dv_xname); 116 if (sc->sc_ih == NULL) { 117 printf(": can't establish interrupt\n"); 118 return; 119 } 120 121 if (sc->sc_gpio_io_node && sc->sc_gpio_io_node->gpio) { 122 sc->sc.sc_card_detect = sdhc_acpi_card_detect_gpio; 123 printf(", gpio"); 124 } 125 126 printf("\n"); 127 128 if (sc->sc_gpio_int_node && sc->sc_gpio_int_node->gpio) { 129 struct acpi_gpio *gpio = sc->sc_gpio_int_node->gpio; 130 131 gpio->intr_establish(gpio->cookie, sc->sc_gpio_int_pin, 132 sc->sc_gpio_int_flags, sdhc_acpi_card_detect_intr, sc); 133 } 134 135 sdhc_acpi_power_on(sc, sc->sc_node); 136 sdhc_acpi_explore(sc); 137 138 capmask = acpi_getpropint(sc->sc_node, "sdhci-caps-mask", 0); 139 capset = acpi_getpropint(sc->sc_node, "sdhci-caps", 0); 140 141 /* Raspberry Pi4 "emmc2" controller. */ 142 if (strcmp(aaa->aaa_dev, "BRCME88C") == 0) 143 sc->sc.sc_flags |= SDHC_F_NOPWR0; 144 145 sc->sc.sc_host = &sc->sc_host; 146 sc->sc.sc_dmat = aaa->aaa_dmat; 147 sc->sc.sc_clkbase = acpi_getpropint(sc->sc_node, 148 "clock-frequency", 0) / 1000; 149 sdhc_host_found(&sc->sc, sc->sc_memt, sc->sc_memh, 150 aaa->aaa_size[0], 1, capmask, capset); 151 } 152 153 int 154 sdhc_acpi_parse_resources(int crsidx, union acpi_resource *crs, void *arg) 155 { 156 struct sdhc_acpi_softc *sc = arg; 157 int type = AML_CRSTYPE(crs); 158 struct aml_node *node; 159 uint16_t pin; 160 161 switch (type) { 162 case LR_GPIO: 163 node = aml_searchname(sc->sc_node, (char *)&crs->pad[crs->lr_gpio.res_off]); 164 pin = *(uint16_t *)&crs->pad[crs->lr_gpio.pin_off]; 165 if (crs->lr_gpio.type == LR_GPIO_INT) { 166 sc->sc_gpio_int_node = node; 167 sc->sc_gpio_int_pin = pin; 168 sc->sc_gpio_int_flags = crs->lr_gpio.tflags; 169 } else if (crs->lr_gpio.type == LR_GPIO_IO) { 170 sc->sc_gpio_io_node = node; 171 sc->sc_gpio_io_pin = pin; 172 } 173 break; 174 } 175 176 return 0; 177 } 178 179 int 180 sdhc_acpi_card_detect_nonremovable(struct sdhc_softc *ssc) 181 { 182 return 1; 183 } 184 185 int 186 sdhc_acpi_card_detect_gpio(struct sdhc_softc *ssc) 187 { 188 struct sdhc_acpi_softc *sc = (struct sdhc_acpi_softc *)ssc; 189 struct acpi_gpio *gpio = sc->sc_gpio_io_node->gpio; 190 uint16_t pin = sc->sc_gpio_io_pin; 191 192 /* Card detect GPIO signal is active-low. */ 193 return !gpio->read_pin(gpio->cookie, pin); 194 } 195 196 int 197 sdhc_acpi_card_detect_intr(void *arg) 198 { 199 struct sdhc_acpi_softc *sc = arg; 200 201 sdhc_needs_discover(&sc->sc); 202 203 return (1); 204 } 205 206 void 207 sdhc_acpi_power_on(struct sdhc_acpi_softc *sc, struct aml_node *node) 208 { 209 node = aml_searchname(node, "_PS0"); 210 if (node && aml_evalnode(sc->sc_acpi, node, 0, NULL, NULL)) 211 printf("%s: _PS0 failed\n", sc->sc.sc_dev.dv_xname); 212 } 213 214 int 215 sdhc_acpi_do_explore(struct aml_node *node, void *arg) 216 { 217 struct sdhc_acpi_softc *sc = arg; 218 int64_t sta, rmv; 219 220 /* We're only interested in our children. */ 221 if (node == sc->sc_node) 222 return 0; 223 224 /* Only consider devices that are actually present. */ 225 if (node->value == NULL || 226 node->value->type != AML_OBJTYPE_DEVICE) 227 return 1; 228 if (aml_evalinteger(sc->sc_acpi, node, "_STA", 0, NULL, &sta)) 229 sta = STA_PRESENT | STA_ENABLED | STA_DEV_OK | 0x1000; 230 if ((sta & STA_PRESENT) == 0) 231 return 1; 232 233 acpi_attach_deps(sc->sc_acpi, node); 234 235 /* Override card detect if we have non-removable devices. */ 236 if (aml_evalinteger(sc->sc_acpi, node, "_RMV", 0, NULL, &rmv)) 237 rmv = 1; 238 if (rmv == 0) { 239 sc->sc.sc_flags |= SDHC_F_NONREMOVABLE; 240 if (sc->sc.sc_card_detect == NULL) { 241 sc->sc.sc_card_detect = 242 sdhc_acpi_card_detect_nonremovable; 243 } 244 } 245 246 sdhc_acpi_power_on(sc, node); 247 248 return 1; 249 } 250 251 void 252 sdhc_acpi_explore(struct sdhc_acpi_softc *sc) 253 { 254 aml_walknodes(sc->sc_node, AML_WALK_PRE, sdhc_acpi_do_explore, sc); 255 } 256