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_swi_giant, 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_swi_giant, 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_swi_giant, &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 there is no cd-gpios property, then presumably the hardware 164 * PRESENT_STATE register and interrupts will reflect card state 165 * properly, and there's nothing more for us to do. Our get_present() 166 * will return sdhci_generic_get_card_present() because cd_pin is NULL. 167 * 168 * If there is a property, make sure we can read the pin. 169 */ 170 if (gpio_pin_get_by_ofw_property(dev, node, "cd-gpios", 171 &helper->cd_pin)) 172 return; 173 174 if (gpio_pin_getcaps(helper->cd_pin, &pincaps) != 0 || 175 !(pincaps & GPIO_PIN_INPUT)) { 176 device_printf(dev, "Cannot read card-detect gpio pin; " 177 "setting card-always-present flag.\n"); 178 helper->cd_disabled = true; 179 return; 180 } 181 182 /* 183 * If the pin can trigger an interrupt on both rising and falling edges, 184 * we can use it to detect card presence changes. If not, we'll request 185 * card presence polling instead of using interrupts. 186 */ 187 if (!(pincaps & GPIO_INTR_EDGE_BOTH)) { 188 if (bootverbose) 189 device_printf(dev, "Cannot configure " 190 "GPIO_INTR_EDGE_BOTH for card detect\n"); 191 goto without_interrupts; 192 } 193 194 if (helper->cd_handler == NULL) { 195 if (bootverbose) 196 device_printf(dev, "Cannot configure " 197 "interrupts as no cd_handler is set\n"); 198 goto without_interrupts; 199 } 200 201 /* 202 * Create an interrupt resource from the pin and set up the interrupt. 203 */ 204 if ((helper->cd_ires = gpio_alloc_intr_resource(dev, &helper->cd_irid, 205 RF_ACTIVE, helper->cd_pin, GPIO_INTR_EDGE_BOTH)) == NULL) { 206 if (bootverbose) 207 device_printf(dev, "Cannot allocate an IRQ for card " 208 "detect GPIO\n"); 209 goto without_interrupts; 210 } 211 212 if (bus_setup_intr(dev, helper->cd_ires, INTR_TYPE_BIO | INTR_MPSAFE, 213 NULL, cd_intr, helper, &helper->cd_ihandler) != 0) { 214 device_printf(dev, "Unable to setup card-detect irq handler\n"); 215 helper->cd_ihandler = NULL; 216 goto without_interrupts; 217 } 218 219 without_interrupts: 220 /* 221 * If we have a readable gpio pin, but didn't successfully configure 222 * gpio interrupts, setup a timeout task to poll the pin 223 */ 224 if (helper->cd_ihandler == NULL) { 225 cd_mode_str = "polling"; 226 } else { 227 cd_mode_str = "interrupts"; 228 } 229 230 if (bootverbose) { 231 device_printf(dev, "Card presence detect on %s pin %u, " 232 "configured for %s.\n", 233 device_get_nameunit(helper->cd_pin->dev), helper->cd_pin->pin, 234 cd_mode_str); 235 } 236 } 237 238 /* 239 * Write protect setup. 240 */ 241 static void 242 wp_setup(struct mmc_helper *helper, phandle_t node) 243 { 244 device_t dev; 245 246 dev = helper->dev; 247 248 if (OF_hasprop(node, "disable-wp")) { 249 helper->wp_disabled = true; 250 if (bootverbose) 251 device_printf(dev, "Write protect disabled\n"); 252 return; 253 } 254 255 if (gpio_pin_get_by_ofw_property(dev, node, "wp-gpios", &helper->wp_pin)) 256 return; 257 258 if (bootverbose) 259 device_printf(dev, "Write protect switch on %s pin %u\n", 260 device_get_nameunit(helper->wp_pin->dev), helper->wp_pin->pin); 261 } 262 263 int 264 mmc_fdt_gpio_setup(device_t dev, phandle_t node, struct mmc_helper *helper, 265 mmc_fdt_cd_handler handler) 266 { 267 268 if (node <= 0) 269 node = ofw_bus_get_node(dev); 270 if (node <= 0) { 271 device_printf(dev, "Cannot get node for device\n"); 272 return (ENXIO); 273 } 274 275 helper->dev = dev; 276 helper->cd_handler = handler; 277 cd_setup(helper, node); 278 wp_setup(helper, node); 279 280 /* 281 * Schedule a card detection 282 */ 283 taskqueue_enqueue_timeout_sbt(taskqueue_swi_giant, 284 &helper->cd_delayed_task, mstosbt(500), 0, C_PREL(2)); 285 return (0); 286 } 287 288 void 289 mmc_fdt_gpio_teardown(struct mmc_helper *helper) 290 { 291 292 if (helper == NULL) 293 return; 294 295 if (helper->cd_ihandler != NULL) 296 bus_teardown_intr(helper->dev, helper->cd_ires, helper->cd_ihandler); 297 if (helper->wp_pin != NULL) 298 gpio_pin_release(helper->wp_pin); 299 if (helper->cd_pin != NULL) 300 gpio_pin_release(helper->cd_pin); 301 if (helper->cd_ires != NULL) 302 bus_release_resource(helper->dev, SYS_RES_IRQ, 0, helper->cd_ires); 303 304 taskqueue_drain_timeout(taskqueue_swi_giant, &helper->cd_delayed_task); 305 } 306 307 bool 308 mmc_fdt_gpio_get_present(struct mmc_helper *helper) 309 { 310 bool pinstate; 311 312 if (helper->cd_disabled) 313 return (true); 314 if (helper->cd_pin == NULL) 315 return (false); 316 317 gpio_pin_is_active(helper->cd_pin, &pinstate); 318 319 return (pinstate ^ (bool)(helper->props & MMC_PROP_CD_INVERTED)); 320 } 321 322 bool 323 mmc_fdt_gpio_get_readonly(struct mmc_helper *helper) 324 { 325 bool pinstate; 326 327 if (helper->wp_disabled) 328 return (false); 329 330 if (helper->wp_pin == NULL) 331 return (false); 332 333 gpio_pin_is_active(helper->wp_pin, &pinstate); 334 335 return (pinstate ^ (bool)(helper->props & MMC_PROP_WP_INVERTED)); 336 } 337 338 void 339 mmc_fdt_set_power(struct mmc_helper *helper, enum mmc_power_mode power_mode) 340 { 341 int reg_status; 342 int rv; 343 344 switch (power_mode) { 345 case power_on: 346 break; 347 case power_off: 348 if (helper->vmmc_supply) { 349 rv = regulator_status(helper->vmmc_supply, ®_status); 350 if (rv == 0 && reg_status == REGULATOR_STATUS_ENABLED) 351 regulator_disable(helper->vmmc_supply); 352 } 353 if (helper->vqmmc_supply) { 354 rv = regulator_status(helper->vqmmc_supply, ®_status); 355 if (rv == 0 && reg_status == REGULATOR_STATUS_ENABLED) 356 regulator_disable(helper->vqmmc_supply); 357 } 358 if (helper->mmc_pwrseq) 359 MMC_PWRSEQ_SET_POWER(helper->mmc_pwrseq, false); 360 break; 361 case power_up: 362 if (helper->vmmc_supply) { 363 rv = regulator_status(helper->vmmc_supply, ®_status); 364 if (rv == 0 && reg_status != REGULATOR_STATUS_ENABLED) 365 regulator_enable(helper->vmmc_supply); 366 } 367 if (helper->vqmmc_supply) { 368 rv = regulator_status(helper->vqmmc_supply, ®_status); 369 if (rv == 0 && reg_status != REGULATOR_STATUS_ENABLED) 370 regulator_enable(helper->vqmmc_supply); 371 } 372 if (helper->mmc_pwrseq) 373 MMC_PWRSEQ_SET_POWER(helper->mmc_pwrseq, true); 374 break; 375 } 376 } 377