at commit c03c1a14
| 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * linux/drivers/mmc/core/host.c |
| 4 | * |
| 5 | * Copyright (C) 2003 Russell King, All Rights Reserved. |
| 6 | * Copyright (C) 2007-2008 Pierre Ossman |
| 7 | * Copyright (C) 2010 Linus Walleij |
| 8 | * |
| 9 | * MMC host class device management |
| 10 | */ |
| 11 | |
| 12 | #include <linux/device.h> |
| 13 | #include <linux/err.h> |
| 14 | #include <linux/idr.h> |
| 15 | #include <linux/of.h> |
| 16 | #include <linux/pagemap.h> |
| 17 | #include <linux/export.h> |
| 18 | #include <linux/leds.h> |
| 19 | #include <linux/slab.h> |
| 20 | |
| 21 | #include <linux/mmc/host.h> |
| 22 | #include <linux/mmc/card.h> |
| 23 | #include <linux/mmc/slot-gpio.h> |
| 24 | |
| 25 | #include "core.h" |
| 26 | #include "crypto.h" |
| 27 | #include "host.h" |
| 28 | #include "slot-gpio.h" |
| 29 | #include "pwrseq.h" |
| 30 | #include "sdio_ops.h" |
| 31 | |
| 32 | #define cls_dev_to_mmc_host(d) container_of(d, struct mmc_host, class_dev) |
| 33 | |
| 34 | static DEFINE_IDA(mmc_host_ida); |
| 35 | |
| 36 | static int mmc_host_class_prepare(struct device *dev) |
| 37 | { |
| 38 | struct mmc_host *host = cls_dev_to_mmc_host(dev); |
| 39 | |
| 40 | /* |
| 41 | * It's safe to access the bus_ops pointer, as both userspace and the |
| 42 | * workqueue for detecting cards are frozen at this point. |
| 43 | */ |
| 44 | if (!host->bus_ops) |
| 45 | return 0; |
| 46 | |
| 47 | /* Validate conditions for system suspend. */ |
| 48 | if (host->bus_ops->pre_suspend) |
| 49 | return host->bus_ops->pre_suspend(host); |
| 50 | |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | static void mmc_host_class_complete(struct device *dev) |
| 55 | { |
| 56 | struct mmc_host *host = cls_dev_to_mmc_host(dev); |
| 57 | |
| 58 | _mmc_detect_change(host, 0, false); |
| 59 | } |
| 60 | |
| 61 | static const struct dev_pm_ops mmc_host_class_dev_pm_ops = { |
| 62 | .prepare = pm_sleep_ptr(mmc_host_class_prepare), |
| 63 | .complete = pm_sleep_ptr(mmc_host_class_complete), |
| 64 | }; |
| 65 | |
| 66 | static void mmc_host_classdev_release(struct device *dev) |
| 67 | { |
| 68 | struct mmc_host *host = cls_dev_to_mmc_host(dev); |
| 69 | wakeup_source_unregister(host->ws); |
| 70 | if (of_alias_get_id(host->parent->of_node, "mmc") < 0) |
| 71 | ida_free(&mmc_host_ida, host->index); |
| 72 | kfree(host); |
| 73 | } |
| 74 | |
| 75 | static int mmc_host_classdev_shutdown(struct device *dev) |
| 76 | { |
| 77 | struct mmc_host *host = cls_dev_to_mmc_host(dev); |
| 78 | |
| 79 | __mmc_stop_host(host); |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | static const struct class mmc_host_class = { |
| 84 | .name = "mmc_host", |
| 85 | .dev_release = mmc_host_classdev_release, |
| 86 | .shutdown_pre = mmc_host_classdev_shutdown, |
| 87 | .pm = pm_ptr(&mmc_host_class_dev_pm_ops), |
| 88 | }; |
| 89 | |
| 90 | int mmc_register_host_class(void) |
| 91 | { |
| 92 | return class_register(&mmc_host_class); |
| 93 | } |
| 94 | |
| 95 | void mmc_unregister_host_class(void) |
| 96 | { |
| 97 | class_unregister(&mmc_host_class); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * mmc_retune_enable() - enter a transfer mode that requires retuning |
| 102 | * @host: host which should retune now |
| 103 | */ |
| 104 | void mmc_retune_enable(struct mmc_host *host) |
| 105 | { |
| 106 | host->can_retune = 1; |
| 107 | if (host->retune_period) |
| 108 | mod_timer(&host->retune_timer, |
| 109 | jiffies + host->retune_period * HZ); |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * Pause re-tuning for a small set of operations. The pause begins after the |
| 114 | * next command. |
| 115 | */ |
| 116 | void mmc_retune_pause(struct mmc_host *host) |
| 117 | { |
| 118 | if (!host->retune_paused) { |
| 119 | host->retune_paused = 1; |
| 120 | mmc_retune_hold(host); |
| 121 | } |
| 122 | } |
| 123 | EXPORT_SYMBOL(mmc_retune_pause); |
| 124 | |
| 125 | void mmc_retune_unpause(struct mmc_host *host) |
| 126 | { |
| 127 | if (host->retune_paused) { |
| 128 | host->retune_paused = 0; |
| 129 | mmc_retune_release(host); |
| 130 | } |
| 131 | } |
| 132 | EXPORT_SYMBOL(mmc_retune_unpause); |
| 133 | |
| 134 | /** |
| 135 | * mmc_retune_disable() - exit a transfer mode that requires retuning |
| 136 | * @host: host which should not retune anymore |
| 137 | * |
| 138 | * It is not meant for temporarily preventing retuning! |
| 139 | */ |
| 140 | void mmc_retune_disable(struct mmc_host *host) |
| 141 | { |
| 142 | mmc_retune_unpause(host); |
| 143 | host->can_retune = 0; |
| 144 | timer_delete_sync(&host->retune_timer); |
| 145 | mmc_retune_clear(host); |
| 146 | } |
| 147 | |
| 148 | void mmc_retune_timer_stop(struct mmc_host *host) |
| 149 | { |
| 150 | timer_delete_sync(&host->retune_timer); |
| 151 | } |
| 152 | EXPORT_SYMBOL(mmc_retune_timer_stop); |
| 153 | |
| 154 | void mmc_retune_hold(struct mmc_host *host) |
| 155 | { |
| 156 | if (!host->hold_retune) |
| 157 | host->retune_now = 1; |
| 158 | host->hold_retune += 1; |
| 159 | } |
| 160 | |
| 161 | void mmc_retune_release(struct mmc_host *host) |
| 162 | { |
| 163 | if (host->hold_retune) |
| 164 | host->hold_retune -= 1; |
| 165 | else |
| 166 | WARN_ON(1); |
| 167 | } |
| 168 | EXPORT_SYMBOL(mmc_retune_release); |
| 169 | |
| 170 | int mmc_retune(struct mmc_host *host) |
| 171 | { |
| 172 | bool return_to_hs400 = false; |
| 173 | int err; |
| 174 | |
| 175 | if (host->retune_now) |
| 176 | host->retune_now = 0; |
| 177 | else |
| 178 | return 0; |
| 179 | |
| 180 | if (!host->need_retune || host->doing_retune || !host->card) |
| 181 | return 0; |
| 182 | |
| 183 | host->need_retune = 0; |
| 184 | |
| 185 | host->doing_retune = 1; |
| 186 | |
| 187 | if (host->ios.timing == MMC_TIMING_MMC_HS400) { |
| 188 | err = mmc_hs400_to_hs200(host->card); |
| 189 | if (err) |
| 190 | goto out; |
| 191 | |
| 192 | return_to_hs400 = true; |
| 193 | } |
| 194 | |
| 195 | err = mmc_execute_tuning(host->card); |
| 196 | if (err) |
| 197 | goto out; |
| 198 | |
| 199 | if (return_to_hs400) |
| 200 | err = mmc_hs200_to_hs400(host->card); |
| 201 | out: |
| 202 | host->doing_retune = 0; |
| 203 | |
| 204 | return err; |
| 205 | } |
| 206 | |
| 207 | static void mmc_retune_timer(struct timer_list *t) |
| 208 | { |
| 209 | struct mmc_host *host = timer_container_of(host, t, retune_timer); |
| 210 | |
| 211 | mmc_retune_needed(host); |
| 212 | } |
| 213 | |
| 214 | static void mmc_of_parse_timing_phase(struct device *dev, const char *prop, |
| 215 | struct mmc_clk_phase *phase) |
| 216 | { |
| 217 | int degrees[2] = {0}; |
| 218 | int rc; |
| 219 | |
| 220 | rc = device_property_read_u32_array(dev, prop, degrees, 2); |
| 221 | phase->valid = !rc; |
| 222 | if (phase->valid) { |
| 223 | phase->in_deg = degrees[0]; |
| 224 | phase->out_deg = degrees[1]; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | void |
| 229 | mmc_of_parse_clk_phase(struct device *dev, struct mmc_clk_phase_map *map) |
| 230 | { |
| 231 | mmc_of_parse_timing_phase(dev, "clk-phase-legacy", |
| 232 | &map->phase[MMC_TIMING_LEGACY]); |
| 233 | mmc_of_parse_timing_phase(dev, "clk-phase-mmc-hs", |
| 234 | &map->phase[MMC_TIMING_MMC_HS]); |
| 235 | mmc_of_parse_timing_phase(dev, "clk-phase-sd-hs", |
| 236 | &map->phase[MMC_TIMING_SD_HS]); |
| 237 | mmc_of_parse_timing_phase(dev, "clk-phase-uhs-sdr12", |
| 238 | &map->phase[MMC_TIMING_UHS_SDR12]); |
| 239 | mmc_of_parse_timing_phase(dev, "clk-phase-uhs-sdr25", |
| 240 | &map->phase[MMC_TIMING_UHS_SDR25]); |
| 241 | mmc_of_parse_timing_phase(dev, "clk-phase-uhs-sdr50", |
| 242 | &map->phase[MMC_TIMING_UHS_SDR50]); |
| 243 | mmc_of_parse_timing_phase(dev, "clk-phase-uhs-sdr104", |
| 244 | &map->phase[MMC_TIMING_UHS_SDR104]); |
| 245 | mmc_of_parse_timing_phase(dev, "clk-phase-uhs-ddr50", |
| 246 | &map->phase[MMC_TIMING_UHS_DDR50]); |
| 247 | mmc_of_parse_timing_phase(dev, "clk-phase-mmc-ddr52", |
| 248 | &map->phase[MMC_TIMING_MMC_DDR52]); |
| 249 | mmc_of_parse_timing_phase(dev, "clk-phase-mmc-hs200", |
| 250 | &map->phase[MMC_TIMING_MMC_HS200]); |
| 251 | mmc_of_parse_timing_phase(dev, "clk-phase-mmc-hs400", |
| 252 | &map->phase[MMC_TIMING_MMC_HS400]); |
| 253 | } |
| 254 | EXPORT_SYMBOL(mmc_of_parse_clk_phase); |
| 255 | |
| 256 | /** |
| 257 | * mmc_of_parse() - parse host's device properties |
| 258 | * @host: host whose properties should be parsed. |
| 259 | * |
| 260 | * To keep the rest of the MMC subsystem unaware of whether DT has been |
| 261 | * used to instantiate and configure this host instance or not, we |
| 262 | * parse the properties and set respective generic mmc-host flags and |
| 263 | * parameters. |
| 264 | */ |
| 265 | int mmc_of_parse(struct mmc_host *host) |
| 266 | { |
| 267 | struct device *dev = host->parent; |
| 268 | u32 bus_width, drv_type, cd_debounce_delay_ms; |
| 269 | int ret; |
| 270 | |
| 271 | if (!dev || !dev_fwnode(dev)) |
| 272 | return 0; |
| 273 | |
| 274 | /* "bus-width" is translated to MMC_CAP_*_BIT_DATA flags */ |
| 275 | if (device_property_read_u32(dev, "bus-width", &bus_width) < 0) { |
| 276 | dev_dbg(host->parent, |
| 277 | "\"bus-width\" property is missing, assuming 1 bit.\n"); |
| 278 | bus_width = 1; |
| 279 | } |
| 280 | |
| 281 | switch (bus_width) { |
| 282 | case 8: |
| 283 | host->caps |= MMC_CAP_8_BIT_DATA; |
| 284 | fallthrough; /* Hosts capable of 8-bit can also do 4 bits */ |
| 285 | case 4: |
| 286 | host->caps |= MMC_CAP_4_BIT_DATA; |
| 287 | break; |
| 288 | case 1: |
| 289 | break; |
| 290 | default: |
| 291 | dev_err(host->parent, |
| 292 | "Invalid \"bus-width\" value %u!\n", bus_width); |
| 293 | return -EINVAL; |
| 294 | } |
| 295 | |
| 296 | /* f_max is obtained from the optional "max-frequency" property */ |
| 297 | device_property_read_u32(dev, "max-frequency", &host->f_max); |
| 298 | |
| 299 | device_property_read_u32(dev, "max-sd-hs-hz", &host->max_sd_hs_hz); |
| 300 | |
| 301 | /* |
| 302 | * Configure CD and WP pins. They are both by default active low to |
| 303 | * match the SDHCI spec. If GPIOs are provided for CD and / or WP, the |
| 304 | * mmc-gpio helpers are used to attach, configure and use them. If |
| 305 | * polarity inversion is specified in DT, one of MMC_CAP2_CD_ACTIVE_HIGH |
| 306 | * and MMC_CAP2_RO_ACTIVE_HIGH capability-2 flags is set. If the |
| 307 | * "broken-cd" property is provided, the MMC_CAP_NEEDS_POLL capability |
| 308 | * is set. If the "non-removable" property is found, the |
| 309 | * MMC_CAP_NONREMOVABLE capability is set and no card-detection |
| 310 | * configuration is performed. |
| 311 | */ |
| 312 | |
| 313 | /* Parse Card Detection */ |
| 314 | |
| 315 | if (device_property_read_bool(dev, "non-removable")) { |
| 316 | host->caps |= MMC_CAP_NONREMOVABLE; |
| 317 | } else { |
| 318 | if (device_property_read_bool(dev, "cd-inverted")) |
| 319 | host->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH; |
| 320 | |
| 321 | if (device_property_read_u32(dev, "cd-debounce-delay-ms", |
| 322 | &cd_debounce_delay_ms)) |
| 323 | cd_debounce_delay_ms = 200; |
| 324 | |
| 325 | if (device_property_read_bool(dev, "broken-cd")) |
| 326 | host->caps |= MMC_CAP_NEEDS_POLL; |
| 327 | |
| 328 | ret = mmc_gpiod_request_cd(host, "cd", 0, false, |
| 329 | cd_debounce_delay_ms * 1000); |
| 330 | if (!ret) |
| 331 | dev_info(host->parent, "Got CD GPIO\n"); |
| 332 | else if (ret != -ENOENT && ret != -ENOSYS) |
| 333 | return ret; |
| 334 | } |
| 335 | |
| 336 | /* Parse Write Protection */ |
| 337 | |
| 338 | if (device_property_read_bool(dev, "wp-inverted")) |
| 339 | host->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH; |
| 340 | |
| 341 | ret = mmc_gpiod_request_ro(host, "wp", 0, 0); |
| 342 | if (!ret) |
| 343 | dev_info(host->parent, "Got WP GPIO\n"); |
| 344 | else if (ret != -ENOENT && ret != -ENOSYS) |
| 345 | return ret; |
| 346 | |
| 347 | if (device_property_read_bool(dev, "disable-wp")) |
| 348 | host->caps2 |= MMC_CAP2_NO_WRITE_PROTECT; |
| 349 | |
| 350 | if (device_property_read_bool(dev, "cap-sd-highspeed")) |
| 351 | host->caps |= MMC_CAP_SD_HIGHSPEED; |
| 352 | if (device_property_read_bool(dev, "cap-mmc-highspeed")) |
| 353 | host->caps |= MMC_CAP_MMC_HIGHSPEED; |
| 354 | if (device_property_read_bool(dev, "sd-uhs-sdr12")) |
| 355 | host->caps |= MMC_CAP_UHS_SDR12; |
| 356 | if (device_property_read_bool(dev, "sd-uhs-sdr25")) |
| 357 | host->caps |= MMC_CAP_UHS_SDR25; |
| 358 | if (device_property_read_bool(dev, "sd-uhs-sdr50")) |
| 359 | host->caps |= MMC_CAP_UHS_SDR50; |
| 360 | if (device_property_read_bool(dev, "sd-uhs-sdr104")) |
| 361 | host->caps |= MMC_CAP_UHS_SDR104; |
| 362 | if (device_property_read_bool(dev, "sd-uhs-ddr50")) |
| 363 | host->caps |= MMC_CAP_UHS_DDR50; |
| 364 | if (device_property_read_bool(dev, "cap-power-off-card")) |
| 365 | host->caps |= MMC_CAP_POWER_OFF_CARD; |
| 366 | if (device_property_read_bool(dev, "cap-mmc-hw-reset")) |
| 367 | host->caps |= MMC_CAP_HW_RESET; |
| 368 | if (device_property_read_bool(dev, "cap-sdio-irq")) |
| 369 | host->caps |= MMC_CAP_SDIO_IRQ; |
| 370 | if (device_property_read_bool(dev, "full-pwr-cycle")) |
| 371 | host->caps2 |= MMC_CAP2_FULL_PWR_CYCLE; |
| 372 | if (device_property_read_bool(dev, "full-pwr-cycle-in-suspend")) |
| 373 | host->caps2 |= MMC_CAP2_FULL_PWR_CYCLE_IN_SUSPEND; |
| 374 | if (device_property_read_bool(dev, "keep-power-in-suspend")) |
| 375 | host->pm_caps |= MMC_PM_KEEP_POWER; |
| 376 | if (device_property_read_bool(dev, "wakeup-source")) |
| 377 | host->pm_caps |= MMC_PM_WAKE_SDIO_IRQ; |
| 378 | if (device_property_read_bool(dev, "mmc-ddr-3_3v")) |
| 379 | host->caps |= MMC_CAP_3_3V_DDR; |
| 380 | if (device_property_read_bool(dev, "mmc-ddr-1_8v")) |
| 381 | host->caps |= MMC_CAP_1_8V_DDR; |
| 382 | if (device_property_read_bool(dev, "mmc-ddr-1_2v")) |
| 383 | host->caps |= MMC_CAP_1_2V_DDR; |
| 384 | if (device_property_read_bool(dev, "mmc-hs200-1_8v")) |
| 385 | host->caps2 |= MMC_CAP2_HS200_1_8V_SDR; |
| 386 | if (device_property_read_bool(dev, "mmc-hs200-1_2v")) |
| 387 | host->caps2 |= MMC_CAP2_HS200_1_2V_SDR; |
| 388 | if (device_property_read_bool(dev, "mmc-hs400-1_8v")) |
| 389 | host->caps2 |= MMC_CAP2_HS400_1_8V | MMC_CAP2_HS200_1_8V_SDR; |
| 390 | if (device_property_read_bool(dev, "mmc-hs400-1_2v")) |
| 391 | host->caps2 |= MMC_CAP2_HS400_1_2V | MMC_CAP2_HS200_1_2V_SDR; |
| 392 | if (device_property_read_bool(dev, "mmc-hs400-enhanced-strobe")) |
| 393 | host->caps2 |= MMC_CAP2_HS400_ES; |
| 394 | if (device_property_read_bool(dev, "no-sdio")) |
| 395 | host->caps2 |= MMC_CAP2_NO_SDIO; |
| 396 | if (device_property_read_bool(dev, "no-sd")) |
| 397 | host->caps2 |= MMC_CAP2_NO_SD; |
| 398 | if (device_property_read_bool(dev, "no-mmc")) |
| 399 | host->caps2 |= MMC_CAP2_NO_MMC; |
| 400 | if (device_property_read_bool(dev, "no-mmc-hs400")) |
| 401 | host->caps2 &= ~(MMC_CAP2_HS400_1_8V | MMC_CAP2_HS400_1_2V | |
| 402 | MMC_CAP2_HS400_ES); |
| 403 | |
| 404 | /* Must be after "non-removable" check */ |
| 405 | if (device_property_read_u32(dev, "fixed-emmc-driver-type", &drv_type) == 0) { |
| 406 | if (host->caps & MMC_CAP_NONREMOVABLE) |
| 407 | host->fixed_drv_type = drv_type; |
| 408 | else |
| 409 | dev_err(host->parent, |
| 410 | "can't use fixed driver type, media is removable\n"); |
| 411 | } |
| 412 | |
| 413 | host->dsr_req = !device_property_read_u32(dev, "dsr", &host->dsr); |
| 414 | if (host->dsr_req && (host->dsr & ~0xffff)) { |
| 415 | dev_err(host->parent, |
| 416 | "device tree specified broken value for DSR: 0x%x, ignoring\n", |
| 417 | host->dsr); |
| 418 | host->dsr_req = 0; |
| 419 | } |
| 420 | |
| 421 | device_property_read_u32(dev, "post-power-on-delay-ms", |
| 422 | &host->ios.power_delay_ms); |
| 423 | |
| 424 | return mmc_pwrseq_alloc(host); |
| 425 | } |
| 426 | |
| 427 | EXPORT_SYMBOL(mmc_of_parse); |
| 428 | |
| 429 | /** |
| 430 | * mmc_of_parse_voltage - return mask of supported voltages |
| 431 | * @host: host whose properties should be parsed. |
| 432 | * @mask: mask of voltages available for MMC/SD/SDIO |
| 433 | * |
| 434 | * Parse the "voltage-ranges" property, returning zero if it is not |
| 435 | * found, negative errno if the voltage-range specification is invalid, |
| 436 | * or one if the voltage-range is specified and successfully parsed. |
| 437 | */ |
| 438 | int mmc_of_parse_voltage(struct mmc_host *host, u32 *mask) |
| 439 | { |
| 440 | const char *prop = "voltage-ranges"; |
| 441 | struct device *dev = host->parent; |
| 442 | u32 *voltage_ranges; |
| 443 | int num_ranges, i; |
| 444 | int ret; |
| 445 | |
| 446 | if (!device_property_present(dev, prop)) { |
| 447 | dev_dbg(dev, "%s unspecified\n", prop); |
| 448 | return 0; |
| 449 | } |
| 450 | |
| 451 | ret = device_property_count_u32(dev, prop); |
| 452 | if (ret < 0) |
| 453 | return ret; |
| 454 | |
| 455 | num_ranges = ret / 2; |
| 456 | if (!num_ranges) { |
| 457 | dev_err(dev, "%s empty\n", prop); |
| 458 | return -EINVAL; |
| 459 | } |
| 460 | |
| 461 | voltage_ranges = kcalloc(2 * num_ranges, sizeof(*voltage_ranges), GFP_KERNEL); |
| 462 | if (!voltage_ranges) |
| 463 | return -ENOMEM; |
| 464 | |
| 465 | ret = device_property_read_u32_array(dev, prop, voltage_ranges, 2 * num_ranges); |
| 466 | if (ret) { |
| 467 | kfree(voltage_ranges); |
| 468 | return ret; |
| 469 | } |
| 470 | |
| 471 | for (i = 0; i < num_ranges; i++) { |
| 472 | const int j = i * 2; |
| 473 | u32 ocr_mask; |
| 474 | |
| 475 | ocr_mask = mmc_vddrange_to_ocrmask(voltage_ranges[j + 0], |
| 476 | voltage_ranges[j + 1]); |
| 477 | if (!ocr_mask) { |
| 478 | dev_err(dev, "range #%d in %s is invalid\n", i, prop); |
| 479 | kfree(voltage_ranges); |
| 480 | return -EINVAL; |
| 481 | } |
| 482 | *mask |= ocr_mask; |
| 483 | } |
| 484 | |
| 485 | kfree(voltage_ranges); |
| 486 | |
| 487 | return 1; |
| 488 | } |
| 489 | EXPORT_SYMBOL(mmc_of_parse_voltage); |
| 490 | |
| 491 | /** |
| 492 | * mmc_first_nonreserved_index() - get the first index that is not reserved |
| 493 | */ |
| 494 | static int mmc_first_nonreserved_index(void) |
| 495 | { |
| 496 | int max; |
| 497 | |
| 498 | max = of_alias_get_highest_id("mmc"); |
| 499 | if (max < 0) |
| 500 | return 0; |
| 501 | |
| 502 | return max + 1; |
| 503 | } |
| 504 | |
| 505 | /** |
| 506 | * mmc_alloc_host - initialise the per-host structure. |
| 507 | * @extra: sizeof private data structure |
| 508 | * @dev: pointer to host device model structure |
| 509 | * |
| 510 | * Initialise the per-host structure. |
| 511 | */ |
| 512 | struct mmc_host *mmc_alloc_host(int extra, struct device *dev) |
| 513 | { |
| 514 | int index; |
| 515 | struct mmc_host *host; |
| 516 | int alias_id, min_idx, max_idx; |
| 517 | |
| 518 | host = kzalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL); |
| 519 | if (!host) |
| 520 | return NULL; |
| 521 | |
| 522 | /* scanning will be enabled when we're ready */ |
| 523 | host->rescan_disable = 1; |
| 524 | |
| 525 | alias_id = of_alias_get_id(dev->of_node, "mmc"); |
| 526 | if (alias_id >= 0) { |
| 527 | index = alias_id; |
| 528 | } else { |
| 529 | min_idx = mmc_first_nonreserved_index(); |
| 530 | max_idx = 0; |
| 531 | |
| 532 | index = ida_alloc_range(&mmc_host_ida, min_idx, max_idx - 1, |
| 533 | GFP_KERNEL); |
| 534 | if (index < 0) { |
| 535 | kfree(host); |
| 536 | return NULL; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | host->index = index; |
| 541 | |
| 542 | dev_set_name(&host->class_dev, "mmc%d", host->index); |
| 543 | host->ws = wakeup_source_register(NULL, dev_name(&host->class_dev)); |
| 544 | |
| 545 | host->parent = dev; |
| 546 | host->class_dev.parent = dev; |
| 547 | host->class_dev.class = &mmc_host_class; |
| 548 | device_initialize(&host->class_dev); |
| 549 | device_enable_async_suspend(&host->class_dev); |
| 550 | |
| 551 | if (mmc_gpio_alloc(host)) { |
| 552 | put_device(&host->class_dev); |
| 553 | return NULL; |
| 554 | } |
| 555 | |
| 556 | spin_lock_init(&host->lock); |
| 557 | init_waitqueue_head(&host->wq); |
| 558 | INIT_DELAYED_WORK(&host->detect, mmc_rescan); |
| 559 | INIT_WORK(&host->sdio_irq_work, sdio_irq_work); |
| 560 | timer_setup(&host->retune_timer, mmc_retune_timer, 0); |
| 561 | |
| 562 | INIT_WORK(&host->supply.uv_work, mmc_undervoltage_workfn); |
| 563 | |
| 564 | /* |
| 565 | * By default, hosts do not support SGIO or large requests. |
| 566 | * They have to set these according to their abilities. |
| 567 | */ |
| 568 | host->max_segs = 1; |
| 569 | host->max_seg_size = PAGE_SIZE; |
| 570 | |
| 571 | host->max_req_size = PAGE_SIZE; |
| 572 | host->max_blk_size = 512; |
| 573 | host->max_blk_count = PAGE_SIZE / 512; |
| 574 | |
| 575 | host->fixed_drv_type = -EINVAL; |
| 576 | host->ios.power_delay_ms = 10; |
| 577 | host->ios.power_mode = MMC_POWER_UNDEFINED; |
| 578 | |
| 579 | return host; |
| 580 | } |
| 581 | |
| 582 | EXPORT_SYMBOL(mmc_alloc_host); |
| 583 | |
| 584 | static void devm_mmc_host_release(struct device *dev, void *res) |
| 585 | { |
| 586 | mmc_free_host(*(struct mmc_host **)res); |
| 587 | } |
| 588 | |
| 589 | struct mmc_host *devm_mmc_alloc_host(struct device *dev, int extra) |
| 590 | { |
| 591 | struct mmc_host **dr, *host; |
| 592 | |
| 593 | dr = devres_alloc(devm_mmc_host_release, sizeof(*dr), GFP_KERNEL); |
| 594 | if (!dr) |
| 595 | return NULL; |
| 596 | |
| 597 | host = mmc_alloc_host(extra, dev); |
| 598 | if (!host) { |
| 599 | devres_free(dr); |
| 600 | return NULL; |
| 601 | } |
| 602 | |
| 603 | *dr = host; |
| 604 | devres_add(dev, dr); |
| 605 | |
| 606 | return host; |
| 607 | } |
| 608 | EXPORT_SYMBOL(devm_mmc_alloc_host); |
| 609 | |
| 610 | static int mmc_validate_host_caps(struct mmc_host *host) |
| 611 | { |
| 612 | struct device *dev = host->parent; |
| 613 | u32 caps = host->caps, caps2 = host->caps2; |
| 614 | |
| 615 | if (caps & MMC_CAP_SDIO_IRQ && !host->ops->enable_sdio_irq) { |
| 616 | dev_warn(dev, "missing ->enable_sdio_irq() ops\n"); |
| 617 | return -EINVAL; |
| 618 | } |
| 619 | |
| 620 | /* UHS/DDR/HS200 modes require at least 4-bit bus */ |
| 621 | if (!(caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA)) && |
| 622 | ((caps & (MMC_CAP_UHS | MMC_CAP_DDR)) || (caps2 & MMC_CAP2_HS200))) { |
| 623 | dev_warn(dev, "drop UHS/DDR/HS200 support since 1-bit bus only\n"); |
| 624 | caps &= ~(MMC_CAP_UHS | MMC_CAP_DDR); |
| 625 | caps2 &= ~MMC_CAP2_HS200; |
| 626 | } |
| 627 | |
| 628 | /* HS400 and HS400ES modes require 8-bit bus */ |
| 629 | if (caps2 & (MMC_CAP2_HS400_ES | MMC_CAP2_HS400) && |
| 630 | !(caps & MMC_CAP_8_BIT_DATA) && !(caps2 & MMC_CAP2_NO_MMC)) { |
| 631 | dev_warn(dev, "drop HS400 support since no 8-bit bus\n"); |
| 632 | caps2 &= ~(MMC_CAP2_HS400_ES | MMC_CAP2_HS400); |
| 633 | } |
| 634 | |
| 635 | host->caps = caps; |
| 636 | host->caps2 = caps2; |
| 637 | |
| 638 | return 0; |
| 639 | } |
| 640 | |
| 641 | /** |
| 642 | * mmc_add_host - initialise host hardware |
| 643 | * @host: mmc host |
| 644 | * |
| 645 | * Register the host with the driver model. The host must be |
| 646 | * prepared to start servicing requests before this function |
| 647 | * completes. |
| 648 | */ |
| 649 | int mmc_add_host(struct mmc_host *host) |
| 650 | { |
| 651 | int err; |
| 652 | |
| 653 | err = mmc_validate_host_caps(host); |
| 654 | if (err) |
| 655 | return err; |
| 656 | |
| 657 | err = device_add(&host->class_dev); |
| 658 | if (err) |
| 659 | return err; |
| 660 | |
| 661 | led_trigger_register_simple(dev_name(&host->class_dev), &host->led); |
| 662 | |
| 663 | mmc_add_host_debugfs(host); |
| 664 | |
| 665 | mmc_start_host(host); |
| 666 | return 0; |
| 667 | } |
| 668 | |
| 669 | EXPORT_SYMBOL(mmc_add_host); |
| 670 | |
| 671 | /** |
| 672 | * mmc_remove_host - remove host hardware |
| 673 | * @host: mmc host |
| 674 | * |
| 675 | * Unregister and remove all cards associated with this host, |
| 676 | * and power down the MMC bus. No new requests will be issued |
| 677 | * after this function has returned. |
| 678 | */ |
| 679 | void mmc_remove_host(struct mmc_host *host) |
| 680 | { |
| 681 | mmc_stop_host(host); |
| 682 | |
| 683 | mmc_remove_host_debugfs(host); |
| 684 | |
| 685 | device_del(&host->class_dev); |
| 686 | |
| 687 | led_trigger_unregister_simple(host->led); |
| 688 | } |
| 689 | |
| 690 | EXPORT_SYMBOL(mmc_remove_host); |
| 691 | |
| 692 | /** |
| 693 | * mmc_free_host - free the host structure |
| 694 | * @host: mmc host |
| 695 | * |
| 696 | * Free the host once all references to it have been dropped. |
| 697 | */ |
| 698 | void mmc_free_host(struct mmc_host *host) |
| 699 | { |
| 700 | cancel_delayed_work_sync(&host->detect); |
| 701 | cancel_work_sync(&host->sdio_irq_work); |
| 702 | mmc_pwrseq_free(host); |
| 703 | put_device(&host->class_dev); |
| 704 | } |
| 705 | |
| 706 | EXPORT_SYMBOL(mmc_free_host); |