1e63fbd7bSEmmanuel Vadot /* 2e63fbd7bSEmmanuel Vadot * Copyright 2019 Emmanuel Vadot <manu@freebsd.org> 3e63fbd7bSEmmanuel Vadot * Copyright (c) 2017 Ian Lepore <ian@freebsd.org> All rights reserved. 4e63fbd7bSEmmanuel Vadot * 5e63fbd7bSEmmanuel Vadot * Redistribution and use in source and binary forms, with or without 6e63fbd7bSEmmanuel Vadot * modification, are permitted provided that the following conditions are 7e63fbd7bSEmmanuel Vadot * met: 8e63fbd7bSEmmanuel Vadot * 9e63fbd7bSEmmanuel Vadot * 1. Redistributions of source code must retain the above copyright 10e63fbd7bSEmmanuel Vadot * notice, this list of conditions and the following disclaimer. 11e63fbd7bSEmmanuel Vadot * 2. Redistributions in binary form must reproduce the above copyright 12e63fbd7bSEmmanuel Vadot * notice, this list of conditions and the following disclaimer in the 13e63fbd7bSEmmanuel Vadot * documentation and/or other materials provided with the distribution. 14e63fbd7bSEmmanuel Vadot * 15e63fbd7bSEmmanuel Vadot * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16e63fbd7bSEmmanuel Vadot * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17e63fbd7bSEmmanuel Vadot * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18e63fbd7bSEmmanuel Vadot * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE 19e63fbd7bSEmmanuel Vadot * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20e63fbd7bSEmmanuel Vadot * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21e63fbd7bSEmmanuel Vadot * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 22e63fbd7bSEmmanuel Vadot * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23e63fbd7bSEmmanuel Vadot * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24e63fbd7bSEmmanuel Vadot * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 25e63fbd7bSEmmanuel Vadot * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26e63fbd7bSEmmanuel Vadot */ 27e63fbd7bSEmmanuel Vadot 28e63fbd7bSEmmanuel Vadot #include <sys/cdefs.h> 29e63fbd7bSEmmanuel Vadot __FBSDID("$FreeBSD$"); 30e63fbd7bSEmmanuel Vadot 31e63fbd7bSEmmanuel Vadot #include <sys/param.h> 32e63fbd7bSEmmanuel Vadot #include <sys/bus.h> 33e63fbd7bSEmmanuel Vadot #include <sys/kernel.h> 34e63fbd7bSEmmanuel Vadot #include <sys/gpio.h> 35e63fbd7bSEmmanuel Vadot #include <sys/taskqueue.h> 36e63fbd7bSEmmanuel Vadot 37e63fbd7bSEmmanuel Vadot #include <dev/mmc/bridge.h> 38e63fbd7bSEmmanuel Vadot #include <dev/mmc/mmc_fdt_helpers.h> 39e63fbd7bSEmmanuel Vadot 40e63fbd7bSEmmanuel Vadot #include <dev/gpio/gpiobusvar.h> 41e63fbd7bSEmmanuel Vadot #include <dev/ofw/ofw_bus.h> 42e63fbd7bSEmmanuel Vadot #include <dev/ofw/ofw_bus_subr.h> 43e63fbd7bSEmmanuel Vadot 44e63fbd7bSEmmanuel Vadot #ifdef EXT_RESOURCES 45e63fbd7bSEmmanuel Vadot #include <dev/extres/regulator/regulator.h> 46e63fbd7bSEmmanuel Vadot #endif 47e63fbd7bSEmmanuel Vadot 48e63fbd7bSEmmanuel Vadot static inline void 49e63fbd7bSEmmanuel Vadot mmc_fdt_parse_sd_speed(phandle_t node, struct mmc_host *host) 50e63fbd7bSEmmanuel Vadot { 51e63fbd7bSEmmanuel Vadot bool no_18v = false; 52e63fbd7bSEmmanuel Vadot 53e63fbd7bSEmmanuel Vadot /* 54e63fbd7bSEmmanuel Vadot * Parse SD supported modes 55e63fbd7bSEmmanuel Vadot * All UHS-I modes requires 1.8V signaling. 56e63fbd7bSEmmanuel Vadot */ 57e63fbd7bSEmmanuel Vadot if (OF_hasprop(node, "no1-8-v")) 58e63fbd7bSEmmanuel Vadot no_18v = true; 59e63fbd7bSEmmanuel Vadot if (OF_hasprop(node, "cap-sd-highspeed")) 60e63fbd7bSEmmanuel Vadot host->caps |= MMC_CAP_HSPEED; 61e63fbd7bSEmmanuel Vadot if (OF_hasprop(node, "sd-uhs-sdr12") && no_18v == false) 62e63fbd7bSEmmanuel Vadot host->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_SIGNALING_180; 63e63fbd7bSEmmanuel Vadot if (OF_hasprop(node, "sd-uhs-sdr25") && no_18v == false) 64e63fbd7bSEmmanuel Vadot host->caps |= MMC_CAP_UHS_SDR25 | MMC_CAP_SIGNALING_180; 65e63fbd7bSEmmanuel Vadot if (OF_hasprop(node, "sd-uhs-sdr50") && no_18v == false) 66e63fbd7bSEmmanuel Vadot host->caps |= MMC_CAP_UHS_SDR50 | MMC_CAP_SIGNALING_180; 67e63fbd7bSEmmanuel Vadot if (OF_hasprop(node, "sd-uhs-sdr104") && no_18v == false) 68e63fbd7bSEmmanuel Vadot host->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_SIGNALING_180; 69e63fbd7bSEmmanuel Vadot if (OF_hasprop(node, "sd-uhs-ddr50") && no_18v == false) 70e63fbd7bSEmmanuel Vadot host->caps |= MMC_CAP_UHS_DDR50 | MMC_CAP_SIGNALING_180; 71e63fbd7bSEmmanuel Vadot } 72e63fbd7bSEmmanuel Vadot 73e63fbd7bSEmmanuel Vadot static inline void 74e63fbd7bSEmmanuel Vadot mmc_fdt_parse_mmc_speed(phandle_t node, struct mmc_host *host) 75e63fbd7bSEmmanuel Vadot { 76e63fbd7bSEmmanuel Vadot 77e63fbd7bSEmmanuel Vadot /* Parse eMMC supported modes */ 78e63fbd7bSEmmanuel Vadot if (OF_hasprop(node, "cap-mmc-highspeed")) 79e63fbd7bSEmmanuel Vadot host->caps |= MMC_CAP_HSPEED; 80e63fbd7bSEmmanuel Vadot if (OF_hasprop(node, "mmc-ddr-1_2v")) 81e63fbd7bSEmmanuel Vadot host->caps |= MMC_CAP_MMC_DDR52_120 | MMC_CAP_SIGNALING_120; 82e63fbd7bSEmmanuel Vadot if (OF_hasprop(node, "mmc-ddr-1_8v")) 83e63fbd7bSEmmanuel Vadot host->caps |= MMC_CAP_MMC_DDR52_180 | MMC_CAP_SIGNALING_180; 84e63fbd7bSEmmanuel Vadot if (OF_hasprop(node, "mmc-ddr-3_3v")) 85e63fbd7bSEmmanuel Vadot host->caps |= MMC_CAP_SIGNALING_330; 86e63fbd7bSEmmanuel Vadot if (OF_hasprop(node, "mmc-hs200-1_2v")) 87e63fbd7bSEmmanuel Vadot host->caps |= MMC_CAP_MMC_HS200_120 | MMC_CAP_SIGNALING_120; 88e63fbd7bSEmmanuel Vadot if (OF_hasprop(node, "mmc-hs200-1_8v")) 89e63fbd7bSEmmanuel Vadot host->caps |= MMC_CAP_MMC_HS200_180 | MMC_CAP_SIGNALING_180; 90e63fbd7bSEmmanuel Vadot if (OF_hasprop(node, "mmc-hs400-1_2v")) 91e63fbd7bSEmmanuel Vadot host->caps |= MMC_CAP_MMC_HS400_120 | MMC_CAP_SIGNALING_120; 92e63fbd7bSEmmanuel Vadot if (OF_hasprop(node, "mmc-hs400-1_8v")) 93e63fbd7bSEmmanuel Vadot host->caps |= MMC_CAP_MMC_HS400_180 | MMC_CAP_SIGNALING_180; 94e63fbd7bSEmmanuel Vadot if (OF_hasprop(node, "mmc-hs400-enhanced-strobe")) 95e63fbd7bSEmmanuel Vadot host->caps |= MMC_CAP_MMC_ENH_STROBE; 96e63fbd7bSEmmanuel Vadot } 97e63fbd7bSEmmanuel Vadot 98e63fbd7bSEmmanuel Vadot int 99e63fbd7bSEmmanuel Vadot mmc_fdt_parse(device_t dev, phandle_t node, struct mmc_fdt_helper *helper, 100e63fbd7bSEmmanuel Vadot struct mmc_host *host) 101e63fbd7bSEmmanuel Vadot { 102e63fbd7bSEmmanuel Vadot uint32_t bus_width; 103e63fbd7bSEmmanuel Vadot 104e63fbd7bSEmmanuel Vadot if (node <= 0) 105e63fbd7bSEmmanuel Vadot node = ofw_bus_get_node(dev); 106e63fbd7bSEmmanuel Vadot if (node <= 0) 107e63fbd7bSEmmanuel Vadot return (ENXIO); 108e63fbd7bSEmmanuel Vadot 109e63fbd7bSEmmanuel Vadot if (OF_getencprop(node, "bus-width", &bus_width, sizeof(uint32_t)) <= 0) 110e63fbd7bSEmmanuel Vadot bus_width = 1; 111e63fbd7bSEmmanuel Vadot 112e63fbd7bSEmmanuel Vadot if (bus_width >= 4) 113e63fbd7bSEmmanuel Vadot host->caps |= MMC_CAP_4_BIT_DATA; 114e63fbd7bSEmmanuel Vadot if (bus_width >= 8) 115e63fbd7bSEmmanuel Vadot host->caps |= MMC_CAP_8_BIT_DATA; 116e63fbd7bSEmmanuel Vadot 117e63fbd7bSEmmanuel Vadot /* 118e63fbd7bSEmmanuel Vadot * max-frequency is optional, drivers should tweak this value 119e63fbd7bSEmmanuel Vadot * if it's not present based on the clock that the mmc controller 120e63fbd7bSEmmanuel Vadot * operates on 121e63fbd7bSEmmanuel Vadot */ 122e63fbd7bSEmmanuel Vadot OF_getencprop(node, "max-frequency", &host->f_max, sizeof(uint32_t)); 123e63fbd7bSEmmanuel Vadot 124e63fbd7bSEmmanuel Vadot if (OF_hasprop(node, "broken-cd")) 125e63fbd7bSEmmanuel Vadot helper->props |= MMC_PROP_BROKEN_CD; 126e63fbd7bSEmmanuel Vadot if (OF_hasprop(node, "non-removable")) 127e63fbd7bSEmmanuel Vadot helper->props |= MMC_PROP_NON_REMOVABLE; 128e63fbd7bSEmmanuel Vadot if (OF_hasprop(node, "wp-inverted")) 129e63fbd7bSEmmanuel Vadot helper->props |= MMC_PROP_WP_INVERTED; 130e63fbd7bSEmmanuel Vadot if (OF_hasprop(node, "cd-inverted")) 131e63fbd7bSEmmanuel Vadot helper->props |= MMC_PROP_CD_INVERTED; 132e63fbd7bSEmmanuel Vadot if (OF_hasprop(node, "no-sdio")) 133e63fbd7bSEmmanuel Vadot helper->props |= MMC_PROP_NO_SDIO; 134e63fbd7bSEmmanuel Vadot if (OF_hasprop(node, "no-sd")) 135e63fbd7bSEmmanuel Vadot helper->props |= MMC_PROP_NO_SD; 136e63fbd7bSEmmanuel Vadot if (OF_hasprop(node, "no-mmc")) 137e63fbd7bSEmmanuel Vadot helper->props |= MMC_PROP_NO_MMC; 138e63fbd7bSEmmanuel Vadot 139e63fbd7bSEmmanuel Vadot if (!(helper->props & MMC_PROP_NO_SD)) 140e63fbd7bSEmmanuel Vadot mmc_fdt_parse_sd_speed(node, host); 141e63fbd7bSEmmanuel Vadot 142e63fbd7bSEmmanuel Vadot if (!(helper->props & MMC_PROP_NO_MMC)) 143e63fbd7bSEmmanuel Vadot mmc_fdt_parse_mmc_speed(node, host); 144e63fbd7bSEmmanuel Vadot 145e63fbd7bSEmmanuel Vadot #ifdef EXT_RESOURCES 146e63fbd7bSEmmanuel Vadot /* 147e63fbd7bSEmmanuel Vadot * Get the regulators if they are supported and 148e63fbd7bSEmmanuel Vadot * clean the non supported modes based on the available voltages. 149e63fbd7bSEmmanuel Vadot */ 150e63fbd7bSEmmanuel Vadot if (regulator_get_by_ofw_property(dev, 0, "vmmc-supply", 151e63fbd7bSEmmanuel Vadot &helper->vmmc_supply) == 0) { 152e63fbd7bSEmmanuel Vadot if (bootverbose) 153e63fbd7bSEmmanuel Vadot device_printf(dev, "vmmc-supply regulator found\n"); 154e63fbd7bSEmmanuel Vadot } 155e63fbd7bSEmmanuel Vadot if (regulator_get_by_ofw_property(dev, 0, "vqmmc-supply", 156e63fbd7bSEmmanuel Vadot &helper->vqmmc_supply) == 0 && bootverbose) { 157e63fbd7bSEmmanuel Vadot if (bootverbose) 158e63fbd7bSEmmanuel Vadot device_printf(dev, "vqmmc-supply regulator found\n"); 159e63fbd7bSEmmanuel Vadot } 160e63fbd7bSEmmanuel Vadot 161e63fbd7bSEmmanuel Vadot if (helper->vqmmc_supply != NULL) { 162e63fbd7bSEmmanuel Vadot if (regulator_check_voltage(helper->vqmmc_supply, 1200000) == 0) 163e63fbd7bSEmmanuel Vadot host->caps |= MMC_CAP_SIGNALING_120; 164e63fbd7bSEmmanuel Vadot else 165e63fbd7bSEmmanuel Vadot host->caps &= ~( MMC_CAP_MMC_HS400_120 | 166e63fbd7bSEmmanuel Vadot MMC_CAP_MMC_HS200_120 | 167e63fbd7bSEmmanuel Vadot MMC_CAP_MMC_DDR52_120); 168e63fbd7bSEmmanuel Vadot if (regulator_check_voltage(helper->vqmmc_supply, 1800000) == 0) 169e63fbd7bSEmmanuel Vadot host->caps |= MMC_CAP_SIGNALING_180; 170e63fbd7bSEmmanuel Vadot else 171e63fbd7bSEmmanuel Vadot host->caps &= ~(MMC_CAP_MMC_HS400_180 | 172e63fbd7bSEmmanuel Vadot MMC_CAP_MMC_HS200_180 | 173e63fbd7bSEmmanuel Vadot MMC_CAP_MMC_DDR52_180 | 174e63fbd7bSEmmanuel Vadot MMC_CAP_UHS_DDR50 | 175e63fbd7bSEmmanuel Vadot MMC_CAP_UHS_SDR104 | 176e63fbd7bSEmmanuel Vadot MMC_CAP_UHS_SDR50 | 177e63fbd7bSEmmanuel Vadot MMC_CAP_UHS_SDR25); 178e63fbd7bSEmmanuel Vadot if (regulator_check_voltage(helper->vqmmc_supply, 3300000) == 0) 179e63fbd7bSEmmanuel Vadot host->caps |= MMC_CAP_SIGNALING_330; 180e63fbd7bSEmmanuel Vadot } else 181e63fbd7bSEmmanuel Vadot host->caps |= MMC_CAP_SIGNALING_330; 182e63fbd7bSEmmanuel Vadot #endif 183e63fbd7bSEmmanuel Vadot 184e63fbd7bSEmmanuel Vadot return (0); 185e63fbd7bSEmmanuel Vadot } 186e63fbd7bSEmmanuel Vadot 187e63fbd7bSEmmanuel Vadot /* 188e63fbd7bSEmmanuel Vadot * Card detect interrupt handler. 189e63fbd7bSEmmanuel Vadot */ 190e63fbd7bSEmmanuel Vadot static void 191e63fbd7bSEmmanuel Vadot cd_intr(void *arg) 192e63fbd7bSEmmanuel Vadot { 193e63fbd7bSEmmanuel Vadot struct mmc_fdt_helper *helper = arg; 194e63fbd7bSEmmanuel Vadot 195e63fbd7bSEmmanuel Vadot taskqueue_enqueue_timeout(taskqueue_swi_giant, 196e63fbd7bSEmmanuel Vadot &helper->cd_delayed_task, -(hz / 2)); 197e63fbd7bSEmmanuel Vadot } 198e63fbd7bSEmmanuel Vadot 199e63fbd7bSEmmanuel Vadot static void 200e63fbd7bSEmmanuel Vadot cd_card_task(void *arg, int pending __unused) 201e63fbd7bSEmmanuel Vadot { 202e63fbd7bSEmmanuel Vadot struct mmc_fdt_helper *helper = arg; 203e63fbd7bSEmmanuel Vadot bool cd_present; 204e63fbd7bSEmmanuel Vadot 205e63fbd7bSEmmanuel Vadot cd_present = mmc_fdt_gpio_get_present(helper); 206e63fbd7bSEmmanuel Vadot if(helper->cd_handler && cd_present != helper->cd_present) 207e63fbd7bSEmmanuel Vadot helper->cd_handler(helper->dev, 208e63fbd7bSEmmanuel Vadot cd_present); 209e63fbd7bSEmmanuel Vadot helper->cd_present = cd_present; 210e63fbd7bSEmmanuel Vadot 211e63fbd7bSEmmanuel Vadot /* If we're polling re-schedule the task */ 212e63fbd7bSEmmanuel Vadot if (helper->cd_ihandler == NULL) 213e63fbd7bSEmmanuel Vadot taskqueue_enqueue_timeout_sbt(taskqueue_swi_giant, 214e63fbd7bSEmmanuel Vadot &helper->cd_delayed_task, mstosbt(500), 0, C_PREL(2)); 215e63fbd7bSEmmanuel Vadot } 216e63fbd7bSEmmanuel Vadot 217e63fbd7bSEmmanuel Vadot /* 218e63fbd7bSEmmanuel Vadot * Card detect setup. 219e63fbd7bSEmmanuel Vadot */ 220ac8dcdddSEmmanuel Vadot static void 221e63fbd7bSEmmanuel Vadot cd_setup(struct mmc_fdt_helper *helper, phandle_t node) 222e63fbd7bSEmmanuel Vadot { 223e63fbd7bSEmmanuel Vadot int pincaps; 224e63fbd7bSEmmanuel Vadot device_t dev; 225e63fbd7bSEmmanuel Vadot const char *cd_mode_str; 226e63fbd7bSEmmanuel Vadot 227e63fbd7bSEmmanuel Vadot dev = helper->dev; 228*4fda71e8SEmmanuel Vadot 229*4fda71e8SEmmanuel Vadot TIMEOUT_TASK_INIT(taskqueue_swi_giant, &helper->cd_delayed_task, 0, 230*4fda71e8SEmmanuel Vadot cd_card_task, helper); 231*4fda71e8SEmmanuel Vadot 232e63fbd7bSEmmanuel Vadot /* 233e63fbd7bSEmmanuel Vadot * If the device is flagged as non-removable, set that slot option, and 234e63fbd7bSEmmanuel Vadot * set a flag to make sdhci_fdt_gpio_get_present() always return true. 235e63fbd7bSEmmanuel Vadot */ 236e63fbd7bSEmmanuel Vadot if (helper->props & MMC_PROP_NON_REMOVABLE) { 237e63fbd7bSEmmanuel Vadot helper->cd_disabled = true; 238e63fbd7bSEmmanuel Vadot if (bootverbose) 239e63fbd7bSEmmanuel Vadot device_printf(dev, "Non-removable media\n"); 240ac8dcdddSEmmanuel Vadot return; 241e63fbd7bSEmmanuel Vadot } 242e63fbd7bSEmmanuel Vadot 243e63fbd7bSEmmanuel Vadot /* 244e63fbd7bSEmmanuel Vadot * If there is no cd-gpios property, then presumably the hardware 245e63fbd7bSEmmanuel Vadot * PRESENT_STATE register and interrupts will reflect card state 246e63fbd7bSEmmanuel Vadot * properly, and there's nothing more for us to do. Our get_present() 247e63fbd7bSEmmanuel Vadot * will return sdhci_generic_get_card_present() because cd_pin is NULL. 248e63fbd7bSEmmanuel Vadot * 249e63fbd7bSEmmanuel Vadot * If there is a property, make sure we can read the pin. 250e63fbd7bSEmmanuel Vadot */ 251e63fbd7bSEmmanuel Vadot if (gpio_pin_get_by_ofw_property(dev, node, "cd-gpios", 252e63fbd7bSEmmanuel Vadot &helper->cd_pin)) 253ac8dcdddSEmmanuel Vadot return; 254e63fbd7bSEmmanuel Vadot 255e63fbd7bSEmmanuel Vadot if (gpio_pin_getcaps(helper->cd_pin, &pincaps) != 0 || 256e63fbd7bSEmmanuel Vadot !(pincaps & GPIO_PIN_INPUT)) { 257e63fbd7bSEmmanuel Vadot device_printf(dev, "Cannot read card-detect gpio pin; " 258e63fbd7bSEmmanuel Vadot "setting card-always-present flag.\n"); 259e63fbd7bSEmmanuel Vadot helper->cd_disabled = true; 260ac8dcdddSEmmanuel Vadot return; 261e63fbd7bSEmmanuel Vadot } 262e63fbd7bSEmmanuel Vadot 263e63fbd7bSEmmanuel Vadot /* 264e63fbd7bSEmmanuel Vadot * If the pin can trigger an interrupt on both rising and falling edges, 265e63fbd7bSEmmanuel Vadot * we can use it to detect card presence changes. If not, we'll request 266e63fbd7bSEmmanuel Vadot * card presence polling instead of using interrupts. 267e63fbd7bSEmmanuel Vadot */ 268e63fbd7bSEmmanuel Vadot if (!(pincaps & GPIO_INTR_EDGE_BOTH)) { 269e63fbd7bSEmmanuel Vadot if (bootverbose) 270e63fbd7bSEmmanuel Vadot device_printf(dev, "Cannot configure " 271e63fbd7bSEmmanuel Vadot "GPIO_INTR_EDGE_BOTH for card detect\n"); 272e63fbd7bSEmmanuel Vadot goto without_interrupts; 273e63fbd7bSEmmanuel Vadot } 274e63fbd7bSEmmanuel Vadot 275e63fbd7bSEmmanuel Vadot if (helper->cd_handler == NULL) { 276e63fbd7bSEmmanuel Vadot if (bootverbose) 277e63fbd7bSEmmanuel Vadot device_printf(dev, "Cannot configure " 278e63fbd7bSEmmanuel Vadot "interrupts as no cd_handler is set\n"); 279e63fbd7bSEmmanuel Vadot goto without_interrupts; 280e63fbd7bSEmmanuel Vadot } 281e63fbd7bSEmmanuel Vadot 282e63fbd7bSEmmanuel Vadot /* 283e63fbd7bSEmmanuel Vadot * Create an interrupt resource from the pin and set up the interrupt. 284e63fbd7bSEmmanuel Vadot */ 285e63fbd7bSEmmanuel Vadot if ((helper->cd_ires = gpio_alloc_intr_resource(dev, &helper->cd_irid, 286e63fbd7bSEmmanuel Vadot RF_ACTIVE, helper->cd_pin, GPIO_INTR_EDGE_BOTH)) == NULL) { 287e63fbd7bSEmmanuel Vadot if (bootverbose) 288e63fbd7bSEmmanuel Vadot device_printf(dev, "Cannot allocate an IRQ for card " 289e63fbd7bSEmmanuel Vadot "detect GPIO\n"); 290e63fbd7bSEmmanuel Vadot goto without_interrupts; 291e63fbd7bSEmmanuel Vadot } 292e63fbd7bSEmmanuel Vadot 293e63fbd7bSEmmanuel Vadot if (bus_setup_intr(dev, helper->cd_ires, INTR_TYPE_BIO | INTR_MPSAFE, 294e63fbd7bSEmmanuel Vadot NULL, cd_intr, helper, &helper->cd_ihandler) != 0) { 295e63fbd7bSEmmanuel Vadot device_printf(dev, "Unable to setup card-detect irq handler\n"); 296e63fbd7bSEmmanuel Vadot helper->cd_ihandler = NULL; 297e63fbd7bSEmmanuel Vadot goto without_interrupts; 298e63fbd7bSEmmanuel Vadot } 299e63fbd7bSEmmanuel Vadot 300e63fbd7bSEmmanuel Vadot without_interrupts: 301e63fbd7bSEmmanuel Vadot /* 302e63fbd7bSEmmanuel Vadot * If we have a readable gpio pin, but didn't successfully configure 303e63fbd7bSEmmanuel Vadot * gpio interrupts, setup a timeout task to poll the pin 304e63fbd7bSEmmanuel Vadot */ 305e63fbd7bSEmmanuel Vadot if (helper->cd_ihandler == NULL) { 306e63fbd7bSEmmanuel Vadot cd_mode_str = "polling"; 307e63fbd7bSEmmanuel Vadot } else { 308e63fbd7bSEmmanuel Vadot cd_mode_str = "interrupts"; 309e63fbd7bSEmmanuel Vadot } 310e63fbd7bSEmmanuel Vadot 311e63fbd7bSEmmanuel Vadot if (bootverbose) { 312e63fbd7bSEmmanuel Vadot device_printf(dev, "Card presence detect on %s pin %u, " 313e63fbd7bSEmmanuel Vadot "configured for %s.\n", 314e63fbd7bSEmmanuel Vadot device_get_nameunit(helper->cd_pin->dev), helper->cd_pin->pin, 315e63fbd7bSEmmanuel Vadot cd_mode_str); 316e63fbd7bSEmmanuel Vadot } 317e63fbd7bSEmmanuel Vadot } 318e63fbd7bSEmmanuel Vadot 319e63fbd7bSEmmanuel Vadot /* 320e63fbd7bSEmmanuel Vadot * Write protect setup. 321e63fbd7bSEmmanuel Vadot */ 322e63fbd7bSEmmanuel Vadot static void 323e63fbd7bSEmmanuel Vadot wp_setup(struct mmc_fdt_helper *helper, phandle_t node) 324e63fbd7bSEmmanuel Vadot { 325e63fbd7bSEmmanuel Vadot device_t dev; 326e63fbd7bSEmmanuel Vadot 327e63fbd7bSEmmanuel Vadot dev = helper->dev; 328e63fbd7bSEmmanuel Vadot 329e63fbd7bSEmmanuel Vadot if (OF_hasprop(node, "disable-wp")) { 330e63fbd7bSEmmanuel Vadot helper->wp_disabled = true; 331e63fbd7bSEmmanuel Vadot if (bootverbose) 332e63fbd7bSEmmanuel Vadot device_printf(dev, "Write protect disabled\n"); 333e63fbd7bSEmmanuel Vadot return; 334e63fbd7bSEmmanuel Vadot } 335e63fbd7bSEmmanuel Vadot 336e63fbd7bSEmmanuel Vadot if (gpio_pin_get_by_ofw_property(dev, node, "wp-gpios", &helper->wp_pin)) 337e63fbd7bSEmmanuel Vadot return; 338e63fbd7bSEmmanuel Vadot 339e63fbd7bSEmmanuel Vadot if (bootverbose) 340e63fbd7bSEmmanuel Vadot device_printf(dev, "Write protect switch on %s pin %u\n", 341e63fbd7bSEmmanuel Vadot device_get_nameunit(helper->wp_pin->dev), helper->wp_pin->pin); 342e63fbd7bSEmmanuel Vadot } 343e63fbd7bSEmmanuel Vadot 344e63fbd7bSEmmanuel Vadot int 345e63fbd7bSEmmanuel Vadot mmc_fdt_gpio_setup(device_t dev, phandle_t node, struct mmc_fdt_helper *helper, 346e63fbd7bSEmmanuel Vadot mmc_fdt_cd_handler handler) 347e63fbd7bSEmmanuel Vadot { 348e63fbd7bSEmmanuel Vadot 349e63fbd7bSEmmanuel Vadot if (node <= 0) 350e63fbd7bSEmmanuel Vadot node = ofw_bus_get_node(dev); 351e63fbd7bSEmmanuel Vadot if (node <= 0) { 352e63fbd7bSEmmanuel Vadot device_printf(dev, "Cannot get node for device\n"); 353e63fbd7bSEmmanuel Vadot return (ENXIO); 354e63fbd7bSEmmanuel Vadot } 355e63fbd7bSEmmanuel Vadot 356e63fbd7bSEmmanuel Vadot helper->dev = dev; 357e63fbd7bSEmmanuel Vadot helper->cd_handler = handler; 358ac8dcdddSEmmanuel Vadot cd_setup(helper, node); 359e63fbd7bSEmmanuel Vadot wp_setup(helper, node); 360e63fbd7bSEmmanuel Vadot 361e63fbd7bSEmmanuel Vadot /* 362e63fbd7bSEmmanuel Vadot * Schedule a card detection 363e63fbd7bSEmmanuel Vadot */ 364e63fbd7bSEmmanuel Vadot taskqueue_enqueue_timeout_sbt(taskqueue_swi_giant, 365e63fbd7bSEmmanuel Vadot &helper->cd_delayed_task, mstosbt(500), 0, C_PREL(2)); 366e63fbd7bSEmmanuel Vadot return (0); 367e63fbd7bSEmmanuel Vadot } 368e63fbd7bSEmmanuel Vadot 369e63fbd7bSEmmanuel Vadot void 370e63fbd7bSEmmanuel Vadot mmc_fdt_gpio_teardown(struct mmc_fdt_helper *helper) 371e63fbd7bSEmmanuel Vadot { 372e63fbd7bSEmmanuel Vadot 373e63fbd7bSEmmanuel Vadot if (helper == NULL) 374e63fbd7bSEmmanuel Vadot return; 375e63fbd7bSEmmanuel Vadot 376e63fbd7bSEmmanuel Vadot if (helper->cd_ihandler != NULL) 377e63fbd7bSEmmanuel Vadot bus_teardown_intr(helper->dev, helper->cd_ires, helper->cd_ihandler); 378e63fbd7bSEmmanuel Vadot if (helper->wp_pin != NULL) 379e63fbd7bSEmmanuel Vadot gpio_pin_release(helper->wp_pin); 380e63fbd7bSEmmanuel Vadot if (helper->cd_pin != NULL) 381e63fbd7bSEmmanuel Vadot gpio_pin_release(helper->cd_pin); 382e63fbd7bSEmmanuel Vadot if (helper->cd_ires != NULL) 383e63fbd7bSEmmanuel Vadot bus_release_resource(helper->dev, SYS_RES_IRQ, 0, helper->cd_ires); 384e63fbd7bSEmmanuel Vadot } 385e63fbd7bSEmmanuel Vadot 386e63fbd7bSEmmanuel Vadot bool 387e63fbd7bSEmmanuel Vadot mmc_fdt_gpio_get_present(struct mmc_fdt_helper *helper) 388e63fbd7bSEmmanuel Vadot { 389e63fbd7bSEmmanuel Vadot bool pinstate; 390e63fbd7bSEmmanuel Vadot 391e63fbd7bSEmmanuel Vadot if (helper->cd_disabled) 392e63fbd7bSEmmanuel Vadot return (true); 393e63fbd7bSEmmanuel Vadot if (helper->cd_pin == NULL) 394e63fbd7bSEmmanuel Vadot return (false); 395e63fbd7bSEmmanuel Vadot 396e63fbd7bSEmmanuel Vadot gpio_pin_is_active(helper->cd_pin, &pinstate); 397e63fbd7bSEmmanuel Vadot 398e63fbd7bSEmmanuel Vadot return (pinstate ^ (helper->props & MMC_PROP_CD_INVERTED)); 399e63fbd7bSEmmanuel Vadot } 400e63fbd7bSEmmanuel Vadot 401e63fbd7bSEmmanuel Vadot bool 402e63fbd7bSEmmanuel Vadot mmc_fdt_gpio_get_readonly(struct mmc_fdt_helper *helper) 403e63fbd7bSEmmanuel Vadot { 404e63fbd7bSEmmanuel Vadot bool pinstate; 405e63fbd7bSEmmanuel Vadot 406e63fbd7bSEmmanuel Vadot if (helper->wp_disabled) 407e63fbd7bSEmmanuel Vadot return (false); 408e63fbd7bSEmmanuel Vadot 409e63fbd7bSEmmanuel Vadot if (helper->wp_pin == NULL) 410e63fbd7bSEmmanuel Vadot return (false); 411e63fbd7bSEmmanuel Vadot 412e63fbd7bSEmmanuel Vadot gpio_pin_is_active(helper->wp_pin, &pinstate); 413e63fbd7bSEmmanuel Vadot 414e63fbd7bSEmmanuel Vadot return (pinstate ^ (helper->props & MMC_PROP_WP_INVERTED)); 415e63fbd7bSEmmanuel Vadot } 416