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