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 static inline void 49 mmc_fdt_parse_sd_speed(phandle_t node, struct mmc_host *host) 50 { 51 bool no_18v = false; 52 53 /* 54 * Parse SD supported modes 55 * All UHS-I modes requires 1.8V signaling. 56 */ 57 if (OF_hasprop(node, "no1-8-v")) 58 no_18v = true; 59 if (OF_hasprop(node, "cap-sd-highspeed")) 60 host->caps |= MMC_CAP_HSPEED; 61 if (OF_hasprop(node, "sd-uhs-sdr12") && no_18v == false) 62 host->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_SIGNALING_180; 63 if (OF_hasprop(node, "sd-uhs-sdr25") && no_18v == false) 64 host->caps |= MMC_CAP_UHS_SDR25 | MMC_CAP_SIGNALING_180; 65 if (OF_hasprop(node, "sd-uhs-sdr50") && no_18v == false) 66 host->caps |= MMC_CAP_UHS_SDR50 | MMC_CAP_SIGNALING_180; 67 if (OF_hasprop(node, "sd-uhs-sdr104") && no_18v == false) 68 host->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_SIGNALING_180; 69 if (OF_hasprop(node, "sd-uhs-ddr50") && no_18v == false) 70 host->caps |= MMC_CAP_UHS_DDR50 | MMC_CAP_SIGNALING_180; 71 } 72 73 static inline void 74 mmc_fdt_parse_mmc_speed(phandle_t node, struct mmc_host *host) 75 { 76 77 /* Parse eMMC supported modes */ 78 if (OF_hasprop(node, "cap-mmc-highspeed")) 79 host->caps |= MMC_CAP_HSPEED; 80 if (OF_hasprop(node, "mmc-ddr-1_2v")) 81 host->caps |= MMC_CAP_MMC_DDR52_120 | MMC_CAP_SIGNALING_120; 82 if (OF_hasprop(node, "mmc-ddr-1_8v")) 83 host->caps |= MMC_CAP_MMC_DDR52_180 | MMC_CAP_SIGNALING_180; 84 if (OF_hasprop(node, "mmc-ddr-3_3v")) 85 host->caps |= MMC_CAP_SIGNALING_330; 86 if (OF_hasprop(node, "mmc-hs200-1_2v")) 87 host->caps |= MMC_CAP_MMC_HS200_120 | MMC_CAP_SIGNALING_120; 88 if (OF_hasprop(node, "mmc-hs200-1_8v")) 89 host->caps |= MMC_CAP_MMC_HS200_180 | MMC_CAP_SIGNALING_180; 90 if (OF_hasprop(node, "mmc-hs400-1_2v")) 91 host->caps |= MMC_CAP_MMC_HS400_120 | MMC_CAP_SIGNALING_120; 92 if (OF_hasprop(node, "mmc-hs400-1_8v")) 93 host->caps |= MMC_CAP_MMC_HS400_180 | MMC_CAP_SIGNALING_180; 94 if (OF_hasprop(node, "mmc-hs400-enhanced-strobe")) 95 host->caps |= MMC_CAP_MMC_ENH_STROBE; 96 } 97 98 int 99 mmc_fdt_parse(device_t dev, phandle_t node, struct mmc_fdt_helper *helper, 100 struct mmc_host *host) 101 { 102 uint32_t bus_width; 103 104 if (node <= 0) 105 node = ofw_bus_get_node(dev); 106 if (node <= 0) 107 return (ENXIO); 108 109 if (OF_getencprop(node, "bus-width", &bus_width, sizeof(uint32_t)) <= 0) 110 bus_width = 1; 111 112 if (bus_width >= 4) 113 host->caps |= MMC_CAP_4_BIT_DATA; 114 if (bus_width >= 8) 115 host->caps |= MMC_CAP_8_BIT_DATA; 116 117 /* 118 * max-frequency is optional, drivers should tweak this value 119 * if it's not present based on the clock that the mmc controller 120 * operates on 121 */ 122 OF_getencprop(node, "max-frequency", &host->f_max, sizeof(uint32_t)); 123 124 if (OF_hasprop(node, "broken-cd")) 125 helper->props |= MMC_PROP_BROKEN_CD; 126 if (OF_hasprop(node, "non-removable")) 127 helper->props |= MMC_PROP_NON_REMOVABLE; 128 if (OF_hasprop(node, "wp-inverted")) 129 helper->props |= MMC_PROP_WP_INVERTED; 130 if (OF_hasprop(node, "cd-inverted")) 131 helper->props |= MMC_PROP_CD_INVERTED; 132 if (OF_hasprop(node, "no-sdio")) 133 helper->props |= MMC_PROP_NO_SDIO; 134 if (OF_hasprop(node, "no-sd")) 135 helper->props |= MMC_PROP_NO_SD; 136 if (OF_hasprop(node, "no-mmc")) 137 helper->props |= MMC_PROP_NO_MMC; 138 139 if (!(helper->props & MMC_PROP_NO_SD)) 140 mmc_fdt_parse_sd_speed(node, host); 141 142 if (!(helper->props & MMC_PROP_NO_MMC)) 143 mmc_fdt_parse_mmc_speed(node, host); 144 145 #ifdef EXT_RESOURCES 146 /* 147 * Get the regulators if they are supported and 148 * clean the non supported modes based on the available voltages. 149 */ 150 if (regulator_get_by_ofw_property(dev, 0, "vmmc-supply", 151 &helper->vmmc_supply) == 0) { 152 if (bootverbose) 153 device_printf(dev, "vmmc-supply regulator found\n"); 154 } 155 if (regulator_get_by_ofw_property(dev, 0, "vqmmc-supply", 156 &helper->vqmmc_supply) == 0 && bootverbose) { 157 if (bootverbose) 158 device_printf(dev, "vqmmc-supply regulator found\n"); 159 } 160 161 if (helper->vqmmc_supply != NULL) { 162 if (regulator_check_voltage(helper->vqmmc_supply, 1200000) == 0) 163 host->caps |= MMC_CAP_SIGNALING_120; 164 else 165 host->caps &= ~( MMC_CAP_MMC_HS400_120 | 166 MMC_CAP_MMC_HS200_120 | 167 MMC_CAP_MMC_DDR52_120); 168 if (regulator_check_voltage(helper->vqmmc_supply, 1800000) == 0) 169 host->caps |= MMC_CAP_SIGNALING_180; 170 else 171 host->caps &= ~(MMC_CAP_MMC_HS400_180 | 172 MMC_CAP_MMC_HS200_180 | 173 MMC_CAP_MMC_DDR52_180 | 174 MMC_CAP_UHS_DDR50 | 175 MMC_CAP_UHS_SDR104 | 176 MMC_CAP_UHS_SDR50 | 177 MMC_CAP_UHS_SDR25); 178 if (regulator_check_voltage(helper->vqmmc_supply, 3300000) == 0) 179 host->caps |= MMC_CAP_SIGNALING_330; 180 } else 181 host->caps |= MMC_CAP_SIGNALING_330; 182 #endif 183 184 return (0); 185 } 186 187 /* 188 * Card detect interrupt handler. 189 */ 190 static void 191 cd_intr(void *arg) 192 { 193 struct mmc_fdt_helper *helper = arg; 194 195 taskqueue_enqueue_timeout(taskqueue_swi_giant, 196 &helper->cd_delayed_task, -(hz / 2)); 197 } 198 199 static void 200 cd_card_task(void *arg, int pending __unused) 201 { 202 struct mmc_fdt_helper *helper = arg; 203 bool cd_present; 204 205 cd_present = mmc_fdt_gpio_get_present(helper); 206 if(helper->cd_handler && cd_present != helper->cd_present) 207 helper->cd_handler(helper->dev, 208 cd_present); 209 helper->cd_present = cd_present; 210 211 /* If we're polling re-schedule the task */ 212 if (helper->cd_ihandler == NULL) 213 taskqueue_enqueue_timeout_sbt(taskqueue_swi_giant, 214 &helper->cd_delayed_task, mstosbt(500), 0, C_PREL(2)); 215 } 216 217 /* 218 * Card detect setup. 219 */ 220 static void 221 cd_setup(struct mmc_fdt_helper *helper, phandle_t node) 222 { 223 int pincaps; 224 device_t dev; 225 const char *cd_mode_str; 226 227 dev = helper->dev; 228 229 TIMEOUT_TASK_INIT(taskqueue_swi_giant, &helper->cd_delayed_task, 0, 230 cd_card_task, helper); 231 232 /* 233 * If the device is flagged as non-removable, set that slot option, and 234 * set a flag to make sdhci_fdt_gpio_get_present() always return true. 235 */ 236 if (helper->props & MMC_PROP_NON_REMOVABLE) { 237 helper->cd_disabled = true; 238 if (bootverbose) 239 device_printf(dev, "Non-removable media\n"); 240 return; 241 } 242 243 /* 244 * If there is no cd-gpios property, then presumably the hardware 245 * PRESENT_STATE register and interrupts will reflect card state 246 * properly, and there's nothing more for us to do. Our get_present() 247 * will return sdhci_generic_get_card_present() because cd_pin is NULL. 248 * 249 * If there is a property, make sure we can read the pin. 250 */ 251 if (gpio_pin_get_by_ofw_property(dev, node, "cd-gpios", 252 &helper->cd_pin)) 253 return; 254 255 if (gpio_pin_getcaps(helper->cd_pin, &pincaps) != 0 || 256 !(pincaps & GPIO_PIN_INPUT)) { 257 device_printf(dev, "Cannot read card-detect gpio pin; " 258 "setting card-always-present flag.\n"); 259 helper->cd_disabled = true; 260 return; 261 } 262 263 /* 264 * If the pin can trigger an interrupt on both rising and falling edges, 265 * we can use it to detect card presence changes. If not, we'll request 266 * card presence polling instead of using interrupts. 267 */ 268 if (!(pincaps & GPIO_INTR_EDGE_BOTH)) { 269 if (bootverbose) 270 device_printf(dev, "Cannot configure " 271 "GPIO_INTR_EDGE_BOTH for card detect\n"); 272 goto without_interrupts; 273 } 274 275 if (helper->cd_handler == NULL) { 276 if (bootverbose) 277 device_printf(dev, "Cannot configure " 278 "interrupts as no cd_handler is set\n"); 279 goto without_interrupts; 280 } 281 282 /* 283 * Create an interrupt resource from the pin and set up the interrupt. 284 */ 285 if ((helper->cd_ires = gpio_alloc_intr_resource(dev, &helper->cd_irid, 286 RF_ACTIVE, helper->cd_pin, GPIO_INTR_EDGE_BOTH)) == NULL) { 287 if (bootverbose) 288 device_printf(dev, "Cannot allocate an IRQ for card " 289 "detect GPIO\n"); 290 goto without_interrupts; 291 } 292 293 if (bus_setup_intr(dev, helper->cd_ires, INTR_TYPE_BIO | INTR_MPSAFE, 294 NULL, cd_intr, helper, &helper->cd_ihandler) != 0) { 295 device_printf(dev, "Unable to setup card-detect irq handler\n"); 296 helper->cd_ihandler = NULL; 297 goto without_interrupts; 298 } 299 300 without_interrupts: 301 /* 302 * If we have a readable gpio pin, but didn't successfully configure 303 * gpio interrupts, setup a timeout task to poll the pin 304 */ 305 if (helper->cd_ihandler == NULL) { 306 cd_mode_str = "polling"; 307 } else { 308 cd_mode_str = "interrupts"; 309 } 310 311 if (bootverbose) { 312 device_printf(dev, "Card presence detect on %s pin %u, " 313 "configured for %s.\n", 314 device_get_nameunit(helper->cd_pin->dev), helper->cd_pin->pin, 315 cd_mode_str); 316 } 317 } 318 319 /* 320 * Write protect setup. 321 */ 322 static void 323 wp_setup(struct mmc_fdt_helper *helper, phandle_t node) 324 { 325 device_t dev; 326 327 dev = helper->dev; 328 329 if (OF_hasprop(node, "disable-wp")) { 330 helper->wp_disabled = true; 331 if (bootverbose) 332 device_printf(dev, "Write protect disabled\n"); 333 return; 334 } 335 336 if (gpio_pin_get_by_ofw_property(dev, node, "wp-gpios", &helper->wp_pin)) 337 return; 338 339 if (bootverbose) 340 device_printf(dev, "Write protect switch on %s pin %u\n", 341 device_get_nameunit(helper->wp_pin->dev), helper->wp_pin->pin); 342 } 343 344 int 345 mmc_fdt_gpio_setup(device_t dev, phandle_t node, struct mmc_fdt_helper *helper, 346 mmc_fdt_cd_handler handler) 347 { 348 349 if (node <= 0) 350 node = ofw_bus_get_node(dev); 351 if (node <= 0) { 352 device_printf(dev, "Cannot get node for device\n"); 353 return (ENXIO); 354 } 355 356 helper->dev = dev; 357 helper->cd_handler = handler; 358 cd_setup(helper, node); 359 wp_setup(helper, node); 360 361 /* 362 * Schedule a card detection 363 */ 364 taskqueue_enqueue_timeout_sbt(taskqueue_swi_giant, 365 &helper->cd_delayed_task, mstosbt(500), 0, C_PREL(2)); 366 return (0); 367 } 368 369 void 370 mmc_fdt_gpio_teardown(struct mmc_fdt_helper *helper) 371 { 372 373 if (helper == NULL) 374 return; 375 376 if (helper->cd_ihandler != NULL) 377 bus_teardown_intr(helper->dev, helper->cd_ires, helper->cd_ihandler); 378 if (helper->wp_pin != NULL) 379 gpio_pin_release(helper->wp_pin); 380 if (helper->cd_pin != NULL) 381 gpio_pin_release(helper->cd_pin); 382 if (helper->cd_ires != NULL) 383 bus_release_resource(helper->dev, SYS_RES_IRQ, 0, helper->cd_ires); 384 385 taskqueue_drain_timeout(taskqueue_swi_giant, &helper->cd_delayed_task); 386 } 387 388 bool 389 mmc_fdt_gpio_get_present(struct mmc_fdt_helper *helper) 390 { 391 bool pinstate; 392 393 if (helper->cd_disabled) 394 return (true); 395 if (helper->cd_pin == NULL) 396 return (false); 397 398 gpio_pin_is_active(helper->cd_pin, &pinstate); 399 400 return (pinstate ^ (helper->props & MMC_PROP_CD_INVERTED)); 401 } 402 403 bool 404 mmc_fdt_gpio_get_readonly(struct mmc_fdt_helper *helper) 405 { 406 bool pinstate; 407 408 if (helper->wp_disabled) 409 return (false); 410 411 if (helper->wp_pin == NULL) 412 return (false); 413 414 gpio_pin_is_active(helper->wp_pin, &pinstate); 415 416 return (pinstate ^ (helper->props & MMC_PROP_WP_INVERTED)); 417 } 418