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