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