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/param.h> 29 #include <sys/bus.h> 30 #include <sys/kernel.h> 31 #include <sys/gpio.h> 32 #include <sys/taskqueue.h> 33 34 #include <dev/mmc/bridge.h> 35 #include <dev/mmc/mmc_fdt_helpers.h> 36 37 #include <dev/gpio/gpiobusvar.h> 38 #include <dev/ofw/ofw_bus.h> 39 #include <dev/ofw/ofw_bus_subr.h> 40 41 #include <dev/regulator/regulator.h> 42 43 #include <dev/mmc/mmc_helpers.h> 44 45 #include "mmc_pwrseq_if.h" 46 47 int 48 mmc_fdt_parse(device_t dev, phandle_t node, struct mmc_helper *helper, 49 struct mmc_host *host) 50 { 51 struct mmc_helper mmc_helper; 52 phandle_t pwrseq_xref; 53 54 memset(&mmc_helper, 0, sizeof(mmc_helper)); 55 mmc_parse(dev, &mmc_helper, host); 56 57 helper->props = mmc_helper.props; 58 59 /* 60 * Get the regulators if they are supported and 61 * clean the non supported modes based on the available voltages. 62 */ 63 if (regulator_get_by_ofw_property(dev, 0, "vmmc-supply", 64 &helper->vmmc_supply) == 0) { 65 if (bootverbose) 66 device_printf(dev, "vmmc-supply regulator found\n"); 67 } 68 if (regulator_get_by_ofw_property(dev, 0, "vqmmc-supply", 69 &helper->vqmmc_supply) == 0) { 70 if (bootverbose) 71 device_printf(dev, "vqmmc-supply regulator found\n"); 72 } 73 74 if (helper->vqmmc_supply != NULL) { 75 if (regulator_check_voltage(helper->vqmmc_supply, 1200000) == 0) 76 host->caps |= MMC_CAP_SIGNALING_120; 77 else 78 host->caps &= ~( MMC_CAP_MMC_HS400_120 | 79 MMC_CAP_MMC_HS200_120 | 80 MMC_CAP_MMC_DDR52_120); 81 if (regulator_check_voltage(helper->vqmmc_supply, 1800000) == 0) 82 host->caps |= MMC_CAP_SIGNALING_180; 83 else 84 host->caps &= ~(MMC_CAP_MMC_HS400_180 | 85 MMC_CAP_MMC_HS200_180 | 86 MMC_CAP_MMC_DDR52_180 | 87 MMC_CAP_UHS_DDR50 | 88 MMC_CAP_UHS_SDR104 | 89 MMC_CAP_UHS_SDR50 | 90 MMC_CAP_UHS_SDR25); 91 if (regulator_check_voltage(helper->vqmmc_supply, 3300000) == 0) 92 host->caps |= MMC_CAP_SIGNALING_330; 93 } else 94 host->caps |= MMC_CAP_SIGNALING_330; 95 96 if (OF_hasprop(node, "mmc-pwrseq")) { 97 if (OF_getencprop(node, "mmc-pwrseq", &pwrseq_xref, sizeof(pwrseq_xref)) == -1) { 98 device_printf(dev, "Cannot get the pwrseq_xref property\n"); 99 return (ENXIO); 100 } 101 helper->mmc_pwrseq = OF_device_from_xref(pwrseq_xref); 102 } 103 return (0); 104 } 105 106 /* 107 * Card detect interrupt handler. 108 */ 109 static void 110 cd_intr(void *arg) 111 { 112 struct mmc_helper *helper = arg; 113 114 taskqueue_enqueue_timeout(taskqueue_bus, 115 &helper->cd_delayed_task, -(hz / 2)); 116 } 117 118 static void 119 cd_card_task(void *arg, int pending __unused) 120 { 121 struct mmc_helper *helper = arg; 122 bool cd_present; 123 124 cd_present = mmc_fdt_gpio_get_present(helper); 125 if(helper->cd_handler && cd_present != helper->cd_present) 126 helper->cd_handler(helper->dev, 127 cd_present); 128 helper->cd_present = cd_present; 129 130 /* If we're polling re-schedule the task */ 131 if (helper->cd_ihandler == NULL) 132 taskqueue_enqueue_timeout_sbt(taskqueue_bus, 133 &helper->cd_delayed_task, mstosbt(500), 0, C_PREL(2)); 134 } 135 136 /* 137 * Card detect setup. 138 */ 139 static void 140 cd_setup(struct mmc_helper *helper, phandle_t node) 141 { 142 int pincaps; 143 device_t dev; 144 const char *cd_mode_str; 145 146 dev = helper->dev; 147 148 TIMEOUT_TASK_INIT(taskqueue_bus, &helper->cd_delayed_task, 0, 149 cd_card_task, helper); 150 151 /* 152 * If the device is flagged as non-removable, set that slot option, and 153 * set a flag to make sdhci_fdt_gpio_get_present() always return true. 154 */ 155 if (helper->props & MMC_PROP_NON_REMOVABLE) { 156 helper->cd_disabled = true; 157 if (bootverbose) 158 device_printf(dev, "Non-removable media\n"); 159 return; 160 } 161 162 /* 163 * If the device has no card-detection, treat it as non-removable. 164 * This could be improved by polling for detection. 165 */ 166 if (helper->props & MMC_PROP_BROKEN_CD) { 167 helper->cd_disabled = true; 168 if (bootverbose) 169 device_printf(dev, "Broken card-detect\n"); 170 return; 171 } 172 173 /* 174 * If there is no cd-gpios property, then presumably the hardware 175 * PRESENT_STATE register and interrupts will reflect card state 176 * properly, and there's nothing more for us to do. Our get_present() 177 * will return sdhci_generic_get_card_present() because cd_pin is NULL. 178 * 179 * If there is a property, make sure we can read the pin. 180 */ 181 if (gpio_pin_get_by_ofw_property(dev, node, "cd-gpios", 182 &helper->cd_pin)) 183 return; 184 185 if (gpio_pin_getcaps(helper->cd_pin, &pincaps) != 0 || 186 !(pincaps & GPIO_PIN_INPUT)) { 187 device_printf(dev, "Cannot read card-detect gpio pin; " 188 "setting card-always-present flag.\n"); 189 helper->cd_disabled = true; 190 return; 191 } 192 193 /* 194 * If the pin can trigger an interrupt on both rising and falling edges, 195 * we can use it to detect card presence changes. If not, we'll request 196 * card presence polling instead of using interrupts. 197 */ 198 if (!(pincaps & GPIO_INTR_EDGE_BOTH)) { 199 if (bootverbose) 200 device_printf(dev, "Cannot configure " 201 "GPIO_INTR_EDGE_BOTH for card detect\n"); 202 goto without_interrupts; 203 } 204 205 if (helper->cd_handler == NULL) { 206 if (bootverbose) 207 device_printf(dev, "Cannot configure " 208 "interrupts as no cd_handler is set\n"); 209 goto without_interrupts; 210 } 211 212 /* 213 * Create an interrupt resource from the pin and set up the interrupt. 214 */ 215 if ((helper->cd_ires = gpio_alloc_intr_resource(dev, &helper->cd_irid, 216 RF_ACTIVE, helper->cd_pin, GPIO_INTR_EDGE_BOTH)) == NULL) { 217 if (bootverbose) 218 device_printf(dev, "Cannot allocate an IRQ for card " 219 "detect GPIO\n"); 220 goto without_interrupts; 221 } 222 223 if (bus_setup_intr(dev, helper->cd_ires, INTR_TYPE_BIO | INTR_MPSAFE, 224 NULL, cd_intr, helper, &helper->cd_ihandler) != 0) { 225 device_printf(dev, "Unable to setup card-detect irq handler\n"); 226 helper->cd_ihandler = NULL; 227 goto without_interrupts; 228 } 229 230 without_interrupts: 231 /* 232 * If we have a readable gpio pin, but didn't successfully configure 233 * gpio interrupts, setup a timeout task to poll the pin 234 */ 235 if (helper->cd_ihandler == NULL) { 236 cd_mode_str = "polling"; 237 } else { 238 cd_mode_str = "interrupts"; 239 } 240 241 if (bootverbose) { 242 device_printf(dev, "Card presence detect on %s pin %u, " 243 "configured for %s.\n", 244 device_get_nameunit(helper->cd_pin->dev), helper->cd_pin->pin, 245 cd_mode_str); 246 } 247 } 248 249 /* 250 * Write protect setup. 251 */ 252 static void 253 wp_setup(struct mmc_helper *helper, phandle_t node) 254 { 255 device_t dev; 256 257 dev = helper->dev; 258 259 if (OF_hasprop(node, "disable-wp")) { 260 helper->wp_disabled = true; 261 if (bootverbose) 262 device_printf(dev, "Write protect disabled\n"); 263 return; 264 } 265 266 if (gpio_pin_get_by_ofw_property(dev, node, "wp-gpios", &helper->wp_pin)) 267 return; 268 269 if (bootverbose) 270 device_printf(dev, "Write protect switch on %s pin %u\n", 271 device_get_nameunit(helper->wp_pin->dev), helper->wp_pin->pin); 272 } 273 274 int 275 mmc_fdt_gpio_setup(device_t dev, phandle_t node, struct mmc_helper *helper, 276 mmc_fdt_cd_handler handler) 277 { 278 279 if (node <= 0) 280 node = ofw_bus_get_node(dev); 281 if (node <= 0) { 282 device_printf(dev, "Cannot get node for device\n"); 283 return (ENXIO); 284 } 285 286 helper->dev = dev; 287 helper->cd_handler = handler; 288 cd_setup(helper, node); 289 wp_setup(helper, node); 290 291 /* 292 * Schedule a card detection 293 */ 294 taskqueue_enqueue_timeout_sbt(taskqueue_bus, 295 &helper->cd_delayed_task, mstosbt(500), 0, C_PREL(2)); 296 return (0); 297 } 298 299 void 300 mmc_fdt_gpio_teardown(struct mmc_helper *helper) 301 { 302 303 if (helper == NULL) 304 return; 305 306 if (helper->cd_ihandler != NULL) 307 bus_teardown_intr(helper->dev, helper->cd_ires, helper->cd_ihandler); 308 if (helper->wp_pin != NULL) 309 gpio_pin_release(helper->wp_pin); 310 if (helper->cd_pin != NULL) 311 gpio_pin_release(helper->cd_pin); 312 if (helper->cd_ires != NULL) 313 bus_release_resource(helper->dev, SYS_RES_IRQ, 0, helper->cd_ires); 314 315 taskqueue_drain_timeout(taskqueue_bus, &helper->cd_delayed_task); 316 } 317 318 bool 319 mmc_fdt_gpio_get_present(struct mmc_helper *helper) 320 { 321 bool pinstate; 322 323 if (helper->cd_disabled) 324 return (true); 325 if (helper->cd_pin == NULL) 326 return (false); 327 328 gpio_pin_is_active(helper->cd_pin, &pinstate); 329 330 return (pinstate ^ (bool)(helper->props & MMC_PROP_CD_INVERTED)); 331 } 332 333 bool 334 mmc_fdt_gpio_get_readonly(struct mmc_helper *helper) 335 { 336 bool pinstate; 337 338 if (helper->wp_disabled) 339 return (false); 340 341 if (helper->wp_pin == NULL) 342 return (false); 343 344 gpio_pin_is_active(helper->wp_pin, &pinstate); 345 346 return (pinstate ^ (bool)(helper->props & MMC_PROP_WP_INVERTED)); 347 } 348 349 void 350 mmc_fdt_set_power(struct mmc_helper *helper, enum mmc_power_mode power_mode) 351 { 352 int reg_status; 353 int rv; 354 355 switch (power_mode) { 356 case power_on: 357 break; 358 case power_off: 359 if (helper->vmmc_supply) { 360 rv = regulator_status(helper->vmmc_supply, ®_status); 361 if (rv == 0 && reg_status == REGULATOR_STATUS_ENABLED) 362 regulator_disable(helper->vmmc_supply); 363 } 364 if (helper->vqmmc_supply) { 365 rv = regulator_status(helper->vqmmc_supply, ®_status); 366 if (rv == 0 && reg_status == REGULATOR_STATUS_ENABLED) 367 regulator_disable(helper->vqmmc_supply); 368 } 369 if (helper->mmc_pwrseq) 370 MMC_PWRSEQ_SET_POWER(helper->mmc_pwrseq, false); 371 break; 372 case power_up: 373 if (helper->vmmc_supply) { 374 rv = regulator_status(helper->vmmc_supply, ®_status); 375 if (rv == 0 && reg_status != REGULATOR_STATUS_ENABLED) 376 regulator_enable(helper->vmmc_supply); 377 } 378 if (helper->vqmmc_supply) { 379 rv = regulator_status(helper->vqmmc_supply, ®_status); 380 if (rv == 0 && reg_status != REGULATOR_STATUS_ENABLED) 381 regulator_enable(helper->vqmmc_supply); 382 } 383 if (helper->mmc_pwrseq) 384 MMC_PWRSEQ_SET_POWER(helper->mmc_pwrseq, true); 385 break; 386 } 387 } 388