1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/drivers/mmc/core/host.c
5 * Copyright (C) 2003 Russell King, All Rights Reserved.
6 * Copyright (C) 2007-2008 Pierre Ossman
7 * Copyright (C) 2010 Linus Walleij
9 * MMC host class device management
12 #include <linux/device.h>
13 #include <linux/err.h>
14 #include <linux/idr.h>
16 #include <linux/pagemap.h>
17 #include <linux/export.h>
18 #include <linux/leds.h>
19 #include <linux/slab.h>
21 #include <linux/mmc/host.h>
22 #include <linux/mmc/card.h>
23 #include <linux/mmc/slot-gpio.h>
28 #include "slot-gpio.h"
32 #define cls_dev_to_mmc_host(d) container_of(d, struct mmc_host, class_dev)
34 static DEFINE_IDA(mmc_host_ida);
36 #ifdef CONFIG_PM_SLEEP
37 static int mmc_host_class_prepare(struct device *dev)
39 struct mmc_host *host = cls_dev_to_mmc_host(dev);
42 * It's safe to access the bus_ops pointer, as both userspace and the
43 * workqueue for detecting cards are frozen at this point.
48 /* Validate conditions for system suspend. */
49 if (host->bus_ops->pre_suspend)
50 return host->bus_ops->pre_suspend(host);
55 static void mmc_host_class_complete(struct device *dev)
57 struct mmc_host *host = cls_dev_to_mmc_host(dev);
59 _mmc_detect_change(host, 0, false);
62 static const struct dev_pm_ops mmc_host_class_dev_pm_ops = {
63 .prepare = mmc_host_class_prepare,
64 .complete = mmc_host_class_complete,
67 #define MMC_HOST_CLASS_DEV_PM_OPS (&mmc_host_class_dev_pm_ops)
69 #define MMC_HOST_CLASS_DEV_PM_OPS NULL
72 static void mmc_host_classdev_release(struct device *dev)
74 struct mmc_host *host = cls_dev_to_mmc_host(dev);
75 wakeup_source_unregister(host->ws);
76 if (of_alias_get_id(host->parent->of_node, "mmc") < 0)
77 ida_free(&mmc_host_ida, host->index);
81 static int mmc_host_classdev_shutdown(struct device *dev)
83 struct mmc_host *host = cls_dev_to_mmc_host(dev);
85 __mmc_stop_host(host);
89 static const struct class mmc_host_class = {
91 .dev_release = mmc_host_classdev_release,
92 .shutdown_pre = mmc_host_classdev_shutdown,
93 .pm = MMC_HOST_CLASS_DEV_PM_OPS,
96 int mmc_register_host_class(void)
98 return class_register(&mmc_host_class);
101 void mmc_unregister_host_class(void)
103 class_unregister(&mmc_host_class);
107 * mmc_retune_enable() - enter a transfer mode that requires retuning
108 * @host: host which should retune now
110 void mmc_retune_enable(struct mmc_host *host)
112 host->can_retune = 1;
113 if (host->retune_period)
114 mod_timer(&host->retune_timer,
115 jiffies + host->retune_period * HZ);
119 * Pause re-tuning for a small set of operations. The pause begins after the
122 void mmc_retune_pause(struct mmc_host *host)
124 if (!host->retune_paused) {
125 host->retune_paused = 1;
126 mmc_retune_hold(host);
129 EXPORT_SYMBOL(mmc_retune_pause);
131 void mmc_retune_unpause(struct mmc_host *host)
133 if (host->retune_paused) {
134 host->retune_paused = 0;
135 mmc_retune_release(host);
138 EXPORT_SYMBOL(mmc_retune_unpause);
141 * mmc_retune_disable() - exit a transfer mode that requires retuning
142 * @host: host which should not retune anymore
144 * It is not meant for temporarily preventing retuning!
146 void mmc_retune_disable(struct mmc_host *host)
148 mmc_retune_unpause(host);
149 host->can_retune = 0;
150 del_timer_sync(&host->retune_timer);
151 mmc_retune_clear(host);
154 void mmc_retune_timer_stop(struct mmc_host *host)
156 del_timer_sync(&host->retune_timer);
158 EXPORT_SYMBOL(mmc_retune_timer_stop);
160 void mmc_retune_hold(struct mmc_host *host)
162 if (!host->hold_retune)
163 host->retune_now = 1;
164 host->hold_retune += 1;
167 void mmc_retune_release(struct mmc_host *host)
169 if (host->hold_retune)
170 host->hold_retune -= 1;
174 EXPORT_SYMBOL(mmc_retune_release);
176 int mmc_retune(struct mmc_host *host)
178 bool return_to_hs400 = false;
181 if (host->retune_now)
182 host->retune_now = 0;
186 if (!host->need_retune || host->doing_retune || !host->card)
189 host->need_retune = 0;
191 host->doing_retune = 1;
193 if (host->ios.timing == MMC_TIMING_MMC_HS400) {
194 err = mmc_hs400_to_hs200(host->card);
198 return_to_hs400 = true;
201 err = mmc_execute_tuning(host->card);
206 err = mmc_hs200_to_hs400(host->card);
208 host->doing_retune = 0;
213 static void mmc_retune_timer(struct timer_list *t)
215 struct mmc_host *host = from_timer(host, t, retune_timer);
217 mmc_retune_needed(host);
220 static void mmc_of_parse_timing_phase(struct device *dev, const char *prop,
221 struct mmc_clk_phase *phase)
223 int degrees[2] = {0};
226 rc = device_property_read_u32_array(dev, prop, degrees, 2);
229 phase->in_deg = degrees[0];
230 phase->out_deg = degrees[1];
235 mmc_of_parse_clk_phase(struct device *dev, struct mmc_clk_phase_map *map)
237 mmc_of_parse_timing_phase(dev, "clk-phase-legacy",
238 &map->phase[MMC_TIMING_LEGACY]);
239 mmc_of_parse_timing_phase(dev, "clk-phase-mmc-hs",
240 &map->phase[MMC_TIMING_MMC_HS]);
241 mmc_of_parse_timing_phase(dev, "clk-phase-sd-hs",
242 &map->phase[MMC_TIMING_SD_HS]);
243 mmc_of_parse_timing_phase(dev, "clk-phase-uhs-sdr12",
244 &map->phase[MMC_TIMING_UHS_SDR12]);
245 mmc_of_parse_timing_phase(dev, "clk-phase-uhs-sdr25",
246 &map->phase[MMC_TIMING_UHS_SDR25]);
247 mmc_of_parse_timing_phase(dev, "clk-phase-uhs-sdr50",
248 &map->phase[MMC_TIMING_UHS_SDR50]);
249 mmc_of_parse_timing_phase(dev, "clk-phase-uhs-sdr104",
250 &map->phase[MMC_TIMING_UHS_SDR104]);
251 mmc_of_parse_timing_phase(dev, "clk-phase-uhs-ddr50",
252 &map->phase[MMC_TIMING_UHS_DDR50]);
253 mmc_of_parse_timing_phase(dev, "clk-phase-mmc-ddr52",
254 &map->phase[MMC_TIMING_MMC_DDR52]);
255 mmc_of_parse_timing_phase(dev, "clk-phase-mmc-hs200",
256 &map->phase[MMC_TIMING_MMC_HS200]);
257 mmc_of_parse_timing_phase(dev, "clk-phase-mmc-hs400",
258 &map->phase[MMC_TIMING_MMC_HS400]);
260 EXPORT_SYMBOL(mmc_of_parse_clk_phase);
263 * mmc_of_parse() - parse host's device properties
264 * @host: host whose properties should be parsed.
266 * To keep the rest of the MMC subsystem unaware of whether DT has been
267 * used to instantiate and configure this host instance or not, we
268 * parse the properties and set respective generic mmc-host flags and
271 int mmc_of_parse(struct mmc_host *host)
273 struct device *dev = host->parent;
274 u32 bus_width, drv_type, cd_debounce_delay_ms;
277 if (!dev || !dev_fwnode(dev))
280 /* "bus-width" is translated to MMC_CAP_*_BIT_DATA flags */
281 if (device_property_read_u32(dev, "bus-width", &bus_width) < 0) {
282 dev_dbg(host->parent,
283 "\"bus-width\" property is missing, assuming 1 bit.\n");
289 host->caps |= MMC_CAP_8_BIT_DATA;
290 fallthrough; /* Hosts capable of 8-bit can also do 4 bits */
292 host->caps |= MMC_CAP_4_BIT_DATA;
297 dev_err(host->parent,
298 "Invalid \"bus-width\" value %u!\n", bus_width);
302 /* f_max is obtained from the optional "max-frequency" property */
303 device_property_read_u32(dev, "max-frequency", &host->f_max);
306 * Configure CD and WP pins. They are both by default active low to
307 * match the SDHCI spec. If GPIOs are provided for CD and / or WP, the
308 * mmc-gpio helpers are used to attach, configure and use them. If
309 * polarity inversion is specified in DT, one of MMC_CAP2_CD_ACTIVE_HIGH
310 * and MMC_CAP2_RO_ACTIVE_HIGH capability-2 flags is set. If the
311 * "broken-cd" property is provided, the MMC_CAP_NEEDS_POLL capability
312 * is set. If the "non-removable" property is found, the
313 * MMC_CAP_NONREMOVABLE capability is set and no card-detection
314 * configuration is performed.
317 /* Parse Card Detection */
319 if (device_property_read_bool(dev, "non-removable")) {
320 host->caps |= MMC_CAP_NONREMOVABLE;
322 if (device_property_read_bool(dev, "cd-inverted"))
323 host->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
325 if (device_property_read_u32(dev, "cd-debounce-delay-ms",
326 &cd_debounce_delay_ms))
327 cd_debounce_delay_ms = 200;
329 if (device_property_read_bool(dev, "broken-cd"))
330 host->caps |= MMC_CAP_NEEDS_POLL;
332 ret = mmc_gpiod_request_cd(host, "cd", 0, false,
333 cd_debounce_delay_ms * 1000);
335 dev_info(host->parent, "Got CD GPIO\n");
336 else if (ret != -ENOENT && ret != -ENOSYS)
340 /* Parse Write Protection */
342 if (device_property_read_bool(dev, "wp-inverted"))
343 host->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
345 ret = mmc_gpiod_request_ro(host, "wp", 0, 0);
347 dev_info(host->parent, "Got WP GPIO\n");
348 else if (ret != -ENOENT && ret != -ENOSYS)
351 if (device_property_read_bool(dev, "disable-wp"))
352 host->caps2 |= MMC_CAP2_NO_WRITE_PROTECT;
354 if (device_property_read_bool(dev, "cap-sd-highspeed"))
355 host->caps |= MMC_CAP_SD_HIGHSPEED;
356 if (device_property_read_bool(dev, "cap-mmc-highspeed"))
357 host->caps |= MMC_CAP_MMC_HIGHSPEED;
358 if (device_property_read_bool(dev, "sd-uhs-sdr12"))
359 host->caps |= MMC_CAP_UHS_SDR12;
360 if (device_property_read_bool(dev, "sd-uhs-sdr25"))
361 host->caps |= MMC_CAP_UHS_SDR25;
362 if (device_property_read_bool(dev, "sd-uhs-sdr50"))
363 host->caps |= MMC_CAP_UHS_SDR50;
364 if (device_property_read_bool(dev, "sd-uhs-sdr104"))
365 host->caps |= MMC_CAP_UHS_SDR104;
366 if (device_property_read_bool(dev, "sd-uhs-ddr50"))
367 host->caps |= MMC_CAP_UHS_DDR50;
368 if (device_property_read_bool(dev, "cap-power-off-card"))
369 host->caps |= MMC_CAP_POWER_OFF_CARD;
370 if (device_property_read_bool(dev, "cap-mmc-hw-reset"))
371 host->caps |= MMC_CAP_HW_RESET;
372 if (device_property_read_bool(dev, "cap-sdio-irq"))
373 host->caps |= MMC_CAP_SDIO_IRQ;
374 if (device_property_read_bool(dev, "full-pwr-cycle"))
375 host->caps2 |= MMC_CAP2_FULL_PWR_CYCLE;
376 if (device_property_read_bool(dev, "full-pwr-cycle-in-suspend"))
377 host->caps2 |= MMC_CAP2_FULL_PWR_CYCLE_IN_SUSPEND;
378 if (device_property_read_bool(dev, "keep-power-in-suspend"))
379 host->pm_caps |= MMC_PM_KEEP_POWER;
380 if (device_property_read_bool(dev, "wakeup-source") ||
381 device_property_read_bool(dev, "enable-sdio-wakeup")) /* legacy */
382 host->pm_caps |= MMC_PM_WAKE_SDIO_IRQ;
383 if (device_property_read_bool(dev, "mmc-ddr-3_3v"))
384 host->caps |= MMC_CAP_3_3V_DDR;
385 if (device_property_read_bool(dev, "mmc-ddr-1_8v"))
386 host->caps |= MMC_CAP_1_8V_DDR;
387 if (device_property_read_bool(dev, "mmc-ddr-1_2v"))
388 host->caps |= MMC_CAP_1_2V_DDR;
389 if (device_property_read_bool(dev, "mmc-hs200-1_8v"))
390 host->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
391 if (device_property_read_bool(dev, "mmc-hs200-1_2v"))
392 host->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
393 if (device_property_read_bool(dev, "mmc-hs400-1_8v"))
394 host->caps2 |= MMC_CAP2_HS400_1_8V | MMC_CAP2_HS200_1_8V_SDR;
395 if (device_property_read_bool(dev, "mmc-hs400-1_2v"))
396 host->caps2 |= MMC_CAP2_HS400_1_2V | MMC_CAP2_HS200_1_2V_SDR;
397 if (device_property_read_bool(dev, "mmc-hs400-enhanced-strobe"))
398 host->caps2 |= MMC_CAP2_HS400_ES;
399 if (device_property_read_bool(dev, "no-sdio"))
400 host->caps2 |= MMC_CAP2_NO_SDIO;
401 if (device_property_read_bool(dev, "no-sd"))
402 host->caps2 |= MMC_CAP2_NO_SD;
403 if (device_property_read_bool(dev, "no-mmc"))
404 host->caps2 |= MMC_CAP2_NO_MMC;
405 if (device_property_read_bool(dev, "no-mmc-hs400"))
406 host->caps2 &= ~(MMC_CAP2_HS400_1_8V | MMC_CAP2_HS400_1_2V |
409 /* Must be after "non-removable" check */
410 if (device_property_read_u32(dev, "fixed-emmc-driver-type", &drv_type) == 0) {
411 if (host->caps & MMC_CAP_NONREMOVABLE)
412 host->fixed_drv_type = drv_type;
414 dev_err(host->parent,
415 "can't use fixed driver type, media is removable\n");
418 host->dsr_req = !device_property_read_u32(dev, "dsr", &host->dsr);
419 if (host->dsr_req && (host->dsr & ~0xffff)) {
420 dev_err(host->parent,
421 "device tree specified broken value for DSR: 0x%x, ignoring\n",
426 device_property_read_u32(dev, "post-power-on-delay-ms",
427 &host->ios.power_delay_ms);
429 return mmc_pwrseq_alloc(host);
432 EXPORT_SYMBOL(mmc_of_parse);
435 * mmc_of_parse_voltage - return mask of supported voltages
436 * @host: host whose properties should be parsed.
437 * @mask: mask of voltages available for MMC/SD/SDIO
439 * Parse the "voltage-ranges" property, returning zero if it is not
440 * found, negative errno if the voltage-range specification is invalid,
441 * or one if the voltage-range is specified and successfully parsed.
443 int mmc_of_parse_voltage(struct mmc_host *host, u32 *mask)
445 const char *prop = "voltage-ranges";
446 struct device *dev = host->parent;
451 if (!device_property_present(dev, prop)) {
452 dev_dbg(dev, "%s unspecified\n", prop);
456 ret = device_property_count_u32(dev, prop);
460 num_ranges = ret / 2;
462 dev_err(dev, "%s empty\n", prop);
466 voltage_ranges = kcalloc(2 * num_ranges, sizeof(*voltage_ranges), GFP_KERNEL);
470 ret = device_property_read_u32_array(dev, prop, voltage_ranges, 2 * num_ranges);
472 kfree(voltage_ranges);
476 for (i = 0; i < num_ranges; i++) {
480 ocr_mask = mmc_vddrange_to_ocrmask(voltage_ranges[j + 0],
481 voltage_ranges[j + 1]);
483 dev_err(dev, "range #%d in %s is invalid\n", i, prop);
484 kfree(voltage_ranges);
490 kfree(voltage_ranges);
494 EXPORT_SYMBOL(mmc_of_parse_voltage);
497 * mmc_first_nonreserved_index() - get the first index that is not reserved
499 static int mmc_first_nonreserved_index(void)
503 max = of_alias_get_highest_id("mmc");
511 * mmc_alloc_host - initialise the per-host structure.
512 * @extra: sizeof private data structure
513 * @dev: pointer to host device model structure
515 * Initialise the per-host structure.
517 struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
520 struct mmc_host *host;
521 int alias_id, min_idx, max_idx;
523 host = kzalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL);
527 /* scanning will be enabled when we're ready */
528 host->rescan_disable = 1;
530 alias_id = of_alias_get_id(dev->of_node, "mmc");
534 min_idx = mmc_first_nonreserved_index();
537 index = ida_alloc_range(&mmc_host_ida, min_idx, max_idx - 1,
547 dev_set_name(&host->class_dev, "mmc%d", host->index);
548 host->ws = wakeup_source_register(NULL, dev_name(&host->class_dev));
551 host->class_dev.parent = dev;
552 host->class_dev.class = &mmc_host_class;
553 device_initialize(&host->class_dev);
554 device_enable_async_suspend(&host->class_dev);
556 if (mmc_gpio_alloc(host)) {
557 put_device(&host->class_dev);
561 spin_lock_init(&host->lock);
562 init_waitqueue_head(&host->wq);
563 INIT_DELAYED_WORK(&host->detect, mmc_rescan);
564 INIT_WORK(&host->sdio_irq_work, sdio_irq_work);
565 timer_setup(&host->retune_timer, mmc_retune_timer, 0);
568 * By default, hosts do not support SGIO or large requests.
569 * They have to set these according to their abilities.
572 host->max_seg_size = PAGE_SIZE;
574 host->max_req_size = PAGE_SIZE;
575 host->max_blk_size = 512;
576 host->max_blk_count = PAGE_SIZE / 512;
578 host->fixed_drv_type = -EINVAL;
579 host->ios.power_delay_ms = 10;
580 host->ios.power_mode = MMC_POWER_UNDEFINED;
585 EXPORT_SYMBOL(mmc_alloc_host);
587 static void devm_mmc_host_release(struct device *dev, void *res)
589 mmc_free_host(*(struct mmc_host **)res);
592 struct mmc_host *devm_mmc_alloc_host(struct device *dev, int extra)
594 struct mmc_host **dr, *host;
596 dr = devres_alloc(devm_mmc_host_release, sizeof(*dr), GFP_KERNEL);
600 host = mmc_alloc_host(extra, dev);
611 EXPORT_SYMBOL(devm_mmc_alloc_host);
613 static int mmc_validate_host_caps(struct mmc_host *host)
615 struct device *dev = host->parent;
616 u32 caps = host->caps, caps2 = host->caps2;
618 if (caps & MMC_CAP_SDIO_IRQ && !host->ops->enable_sdio_irq) {
619 dev_warn(dev, "missing ->enable_sdio_irq() ops\n");
623 if (caps2 & (MMC_CAP2_HS400_ES | MMC_CAP2_HS400) &&
624 !(caps & MMC_CAP_8_BIT_DATA) && !(caps2 & MMC_CAP2_NO_MMC)) {
625 dev_warn(dev, "drop HS400 support since no 8-bit bus\n");
626 host->caps2 = caps2 & ~MMC_CAP2_HS400_ES & ~MMC_CAP2_HS400;
633 * mmc_add_host - initialise host hardware
636 * Register the host with the driver model. The host must be
637 * prepared to start servicing requests before this function
640 int mmc_add_host(struct mmc_host *host)
644 err = mmc_validate_host_caps(host);
648 err = device_add(&host->class_dev);
652 led_trigger_register_simple(dev_name(&host->class_dev), &host->led);
654 mmc_add_host_debugfs(host);
656 mmc_start_host(host);
660 EXPORT_SYMBOL(mmc_add_host);
663 * mmc_remove_host - remove host hardware
666 * Unregister and remove all cards associated with this host,
667 * and power down the MMC bus. No new requests will be issued
668 * after this function has returned.
670 void mmc_remove_host(struct mmc_host *host)
674 mmc_remove_host_debugfs(host);
676 device_del(&host->class_dev);
678 led_trigger_unregister_simple(host->led);
681 EXPORT_SYMBOL(mmc_remove_host);
684 * mmc_free_host - free the host structure
687 * Free the host once all references to it have been dropped.
689 void mmc_free_host(struct mmc_host *host)
691 cancel_delayed_work_sync(&host->detect);
692 mmc_pwrseq_free(host);
693 put_device(&host->class_dev);
696 EXPORT_SYMBOL(mmc_free_host);