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/of_gpio.h>
17 #include <linux/pagemap.h>
18 #include <linux/pm_wakeup.h>
19 #include <linux/export.h>
20 #include <linux/leds.h>
21 #include <linux/slab.h>
23 #include <linux/mmc/host.h>
24 #include <linux/mmc/card.h>
25 #include <linux/mmc/slot-gpio.h>
29 #include "slot-gpio.h"
33 #define cls_dev_to_mmc_host(d) container_of(d, struct mmc_host, class_dev)
35 static DEFINE_IDA(mmc_host_ida);
37 #ifdef CONFIG_PM_SLEEP
38 static int mmc_host_class_prepare(struct device *dev)
40 struct mmc_host *host = cls_dev_to_mmc_host(dev);
43 * It's safe to access the bus_ops pointer, as both userspace and the
44 * workqueue for detecting cards are frozen at this point.
49 /* Validate conditions for system suspend. */
50 if (host->bus_ops->pre_suspend)
51 return host->bus_ops->pre_suspend(host);
56 static void mmc_host_class_complete(struct device *dev)
58 struct mmc_host *host = cls_dev_to_mmc_host(dev);
60 _mmc_detect_change(host, 0, false);
63 static const struct dev_pm_ops mmc_host_class_dev_pm_ops = {
64 .prepare = mmc_host_class_prepare,
65 .complete = mmc_host_class_complete,
68 #define MMC_HOST_CLASS_DEV_PM_OPS (&mmc_host_class_dev_pm_ops)
70 #define MMC_HOST_CLASS_DEV_PM_OPS NULL
73 static void mmc_host_classdev_release(struct device *dev)
75 struct mmc_host *host = cls_dev_to_mmc_host(dev);
76 wakeup_source_unregister(host->ws);
77 if (of_alias_get_id(host->parent->of_node, "mmc") < 0)
78 ida_simple_remove(&mmc_host_ida, host->index);
82 static int mmc_host_classdev_shutdown(struct device *dev)
84 struct mmc_host *host = cls_dev_to_mmc_host(dev);
86 __mmc_stop_host(host);
90 static struct class mmc_host_class = {
92 .dev_release = mmc_host_classdev_release,
93 .shutdown_pre = mmc_host_classdev_shutdown,
94 .pm = MMC_HOST_CLASS_DEV_PM_OPS,
97 int mmc_register_host_class(void)
99 return class_register(&mmc_host_class);
102 void mmc_unregister_host_class(void)
104 class_unregister(&mmc_host_class);
107 void mmc_retune_enable(struct mmc_host *host)
109 host->can_retune = 1;
110 if (host->retune_period)
111 mod_timer(&host->retune_timer,
112 jiffies + host->retune_period * HZ);
116 * Pause re-tuning for a small set of operations. The pause begins after the
117 * next command and after first doing re-tuning.
119 void mmc_retune_pause(struct mmc_host *host)
121 if (!host->retune_paused) {
122 host->retune_paused = 1;
123 mmc_retune_needed(host);
124 mmc_retune_hold(host);
127 EXPORT_SYMBOL(mmc_retune_pause);
129 void mmc_retune_unpause(struct mmc_host *host)
131 if (host->retune_paused) {
132 host->retune_paused = 0;
133 mmc_retune_release(host);
136 EXPORT_SYMBOL(mmc_retune_unpause);
138 void mmc_retune_disable(struct mmc_host *host)
140 mmc_retune_unpause(host);
141 host->can_retune = 0;
142 del_timer_sync(&host->retune_timer);
143 host->retune_now = 0;
144 host->need_retune = 0;
147 void mmc_retune_timer_stop(struct mmc_host *host)
149 del_timer_sync(&host->retune_timer);
151 EXPORT_SYMBOL(mmc_retune_timer_stop);
153 void mmc_retune_hold(struct mmc_host *host)
155 if (!host->hold_retune)
156 host->retune_now = 1;
157 host->hold_retune += 1;
160 void mmc_retune_release(struct mmc_host *host)
162 if (host->hold_retune)
163 host->hold_retune -= 1;
167 EXPORT_SYMBOL(mmc_retune_release);
169 int mmc_retune(struct mmc_host *host)
171 bool return_to_hs400 = false;
174 if (host->retune_now)
175 host->retune_now = 0;
179 if (!host->need_retune || host->doing_retune || !host->card)
182 host->need_retune = 0;
184 host->doing_retune = 1;
186 if (host->ios.timing == MMC_TIMING_MMC_HS400) {
187 err = mmc_hs400_to_hs200(host->card);
191 return_to_hs400 = true;
194 err = mmc_execute_tuning(host->card);
199 err = mmc_hs200_to_hs400(host->card);
201 host->doing_retune = 0;
206 static void mmc_retune_timer(struct timer_list *t)
208 struct mmc_host *host = from_timer(host, t, retune_timer);
210 mmc_retune_needed(host);
214 * mmc_of_parse() - parse host's device-tree node
215 * @host: host whose node should be parsed.
217 * To keep the rest of the MMC subsystem unaware of whether DT has been
218 * used to to instantiate and configure this host instance or not, we
219 * parse the properties and set respective generic mmc-host flags and
222 int mmc_of_parse(struct mmc_host *host)
224 struct device *dev = host->parent;
225 u32 bus_width, drv_type, cd_debounce_delay_ms;
228 if (!dev || !dev_fwnode(dev))
231 /* "bus-width" is translated to MMC_CAP_*_BIT_DATA flags */
232 if (device_property_read_u32(dev, "bus-width", &bus_width) < 0) {
233 dev_dbg(host->parent,
234 "\"bus-width\" property is missing, assuming 1 bit.\n");
240 host->caps |= MMC_CAP_8_BIT_DATA;
241 fallthrough; /* Hosts capable of 8-bit can also do 4 bits */
243 host->caps |= MMC_CAP_4_BIT_DATA;
248 dev_err(host->parent,
249 "Invalid \"bus-width\" value %u!\n", bus_width);
253 /* f_max is obtained from the optional "max-frequency" property */
254 device_property_read_u32(dev, "max-frequency", &host->f_max);
257 * Configure CD and WP pins. They are both by default active low to
258 * match the SDHCI spec. If GPIOs are provided for CD and / or WP, the
259 * mmc-gpio helpers are used to attach, configure and use them. If
260 * polarity inversion is specified in DT, one of MMC_CAP2_CD_ACTIVE_HIGH
261 * and MMC_CAP2_RO_ACTIVE_HIGH capability-2 flags is set. If the
262 * "broken-cd" property is provided, the MMC_CAP_NEEDS_POLL capability
263 * is set. If the "non-removable" property is found, the
264 * MMC_CAP_NONREMOVABLE capability is set and no card-detection
265 * configuration is performed.
268 /* Parse Card Detection */
270 if (device_property_read_bool(dev, "non-removable")) {
271 host->caps |= MMC_CAP_NONREMOVABLE;
273 if (device_property_read_bool(dev, "cd-inverted"))
274 host->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
276 if (device_property_read_u32(dev, "cd-debounce-delay-ms",
277 &cd_debounce_delay_ms))
278 cd_debounce_delay_ms = 200;
280 if (device_property_read_bool(dev, "broken-cd"))
281 host->caps |= MMC_CAP_NEEDS_POLL;
283 ret = mmc_gpiod_request_cd(host, "cd", 0, false,
284 cd_debounce_delay_ms * 1000);
286 dev_info(host->parent, "Got CD GPIO\n");
287 else if (ret != -ENOENT && ret != -ENOSYS)
291 /* Parse Write Protection */
293 if (device_property_read_bool(dev, "wp-inverted"))
294 host->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
296 ret = mmc_gpiod_request_ro(host, "wp", 0, 0);
298 dev_info(host->parent, "Got WP GPIO\n");
299 else if (ret != -ENOENT && ret != -ENOSYS)
302 if (device_property_read_bool(dev, "disable-wp"))
303 host->caps2 |= MMC_CAP2_NO_WRITE_PROTECT;
305 if (device_property_read_bool(dev, "cap-sd-highspeed"))
306 host->caps |= MMC_CAP_SD_HIGHSPEED;
307 if (device_property_read_bool(dev, "cap-mmc-highspeed"))
308 host->caps |= MMC_CAP_MMC_HIGHSPEED;
309 if (device_property_read_bool(dev, "sd-uhs-sdr12"))
310 host->caps |= MMC_CAP_UHS_SDR12;
311 if (device_property_read_bool(dev, "sd-uhs-sdr25"))
312 host->caps |= MMC_CAP_UHS_SDR25;
313 if (device_property_read_bool(dev, "sd-uhs-sdr50"))
314 host->caps |= MMC_CAP_UHS_SDR50;
315 if (device_property_read_bool(dev, "sd-uhs-sdr104"))
316 host->caps |= MMC_CAP_UHS_SDR104;
317 if (device_property_read_bool(dev, "sd-uhs-ddr50"))
318 host->caps |= MMC_CAP_UHS_DDR50;
319 if (device_property_read_bool(dev, "cap-power-off-card"))
320 host->caps |= MMC_CAP_POWER_OFF_CARD;
321 if (device_property_read_bool(dev, "cap-mmc-hw-reset"))
322 host->caps |= MMC_CAP_HW_RESET;
323 if (device_property_read_bool(dev, "cap-sdio-irq"))
324 host->caps |= MMC_CAP_SDIO_IRQ;
325 if (device_property_read_bool(dev, "full-pwr-cycle"))
326 host->caps2 |= MMC_CAP2_FULL_PWR_CYCLE;
327 if (device_property_read_bool(dev, "full-pwr-cycle-in-suspend"))
328 host->caps2 |= MMC_CAP2_FULL_PWR_CYCLE_IN_SUSPEND;
329 if (device_property_read_bool(dev, "keep-power-in-suspend"))
330 host->pm_caps |= MMC_PM_KEEP_POWER;
331 if (device_property_read_bool(dev, "wakeup-source") ||
332 device_property_read_bool(dev, "enable-sdio-wakeup")) /* legacy */
333 host->pm_caps |= MMC_PM_WAKE_SDIO_IRQ;
334 if (device_property_read_bool(dev, "mmc-ddr-3_3v"))
335 host->caps |= MMC_CAP_3_3V_DDR;
336 if (device_property_read_bool(dev, "mmc-ddr-1_8v"))
337 host->caps |= MMC_CAP_1_8V_DDR;
338 if (device_property_read_bool(dev, "mmc-ddr-1_2v"))
339 host->caps |= MMC_CAP_1_2V_DDR;
340 if (device_property_read_bool(dev, "mmc-hs200-1_8v"))
341 host->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
342 if (device_property_read_bool(dev, "mmc-hs200-1_2v"))
343 host->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
344 if (device_property_read_bool(dev, "mmc-hs400-1_8v"))
345 host->caps2 |= MMC_CAP2_HS400_1_8V | MMC_CAP2_HS200_1_8V_SDR;
346 if (device_property_read_bool(dev, "mmc-hs400-1_2v"))
347 host->caps2 |= MMC_CAP2_HS400_1_2V | MMC_CAP2_HS200_1_2V_SDR;
348 if (device_property_read_bool(dev, "mmc-hs400-enhanced-strobe"))
349 host->caps2 |= MMC_CAP2_HS400_ES;
350 if (device_property_read_bool(dev, "no-sdio"))
351 host->caps2 |= MMC_CAP2_NO_SDIO;
352 if (device_property_read_bool(dev, "no-sd"))
353 host->caps2 |= MMC_CAP2_NO_SD;
354 if (device_property_read_bool(dev, "no-mmc"))
355 host->caps2 |= MMC_CAP2_NO_MMC;
357 /* Must be after "non-removable" check */
358 if (device_property_read_u32(dev, "fixed-emmc-driver-type", &drv_type) == 0) {
359 if (host->caps & MMC_CAP_NONREMOVABLE)
360 host->fixed_drv_type = drv_type;
362 dev_err(host->parent,
363 "can't use fixed driver type, media is removable\n");
366 host->dsr_req = !device_property_read_u32(dev, "dsr", &host->dsr);
367 if (host->dsr_req && (host->dsr & ~0xffff)) {
368 dev_err(host->parent,
369 "device tree specified broken value for DSR: 0x%x, ignoring\n",
374 device_property_read_u32(dev, "post-power-on-delay-ms",
375 &host->ios.power_delay_ms);
377 return mmc_pwrseq_alloc(host);
380 EXPORT_SYMBOL(mmc_of_parse);
383 * mmc_of_parse_voltage - return mask of supported voltages
384 * @np: The device node need to be parsed.
385 * @mask: mask of voltages available for MMC/SD/SDIO
387 * Parse the "voltage-ranges" DT property, returning zero if it is not
388 * found, negative errno if the voltage-range specification is invalid,
389 * or one if the voltage-range is specified and successfully parsed.
391 int mmc_of_parse_voltage(struct device_node *np, u32 *mask)
393 const u32 *voltage_ranges;
396 voltage_ranges = of_get_property(np, "voltage-ranges", &num_ranges);
397 if (!voltage_ranges) {
398 pr_debug("%pOF: voltage-ranges unspecified\n", np);
401 num_ranges = num_ranges / sizeof(*voltage_ranges) / 2;
403 pr_err("%pOF: voltage-ranges empty\n", np);
407 for (i = 0; i < num_ranges; i++) {
411 ocr_mask = mmc_vddrange_to_ocrmask(
412 be32_to_cpu(voltage_ranges[j]),
413 be32_to_cpu(voltage_ranges[j + 1]));
415 pr_err("%pOF: voltage-range #%d is invalid\n",
424 EXPORT_SYMBOL(mmc_of_parse_voltage);
427 * mmc_first_nonreserved_index() - get the first index that is not reserved
429 static int mmc_first_nonreserved_index(void)
433 max = of_alias_get_highest_id("mmc");
441 * mmc_alloc_host - initialise the per-host structure.
442 * @extra: sizeof private data structure
443 * @dev: pointer to host device model structure
445 * Initialise the per-host structure.
447 struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
450 struct mmc_host *host;
451 int alias_id, min_idx, max_idx;
453 host = kzalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL);
457 /* scanning will be enabled when we're ready */
458 host->rescan_disable = 1;
460 alias_id = of_alias_get_id(dev->of_node, "mmc");
464 min_idx = mmc_first_nonreserved_index();
467 index = ida_simple_get(&mmc_host_ida, min_idx, max_idx, GFP_KERNEL);
476 dev_set_name(&host->class_dev, "mmc%d", host->index);
477 host->ws = wakeup_source_register(NULL, dev_name(&host->class_dev));
480 host->class_dev.parent = dev;
481 host->class_dev.class = &mmc_host_class;
482 device_initialize(&host->class_dev);
483 device_enable_async_suspend(&host->class_dev);
485 if (mmc_gpio_alloc(host)) {
486 put_device(&host->class_dev);
490 spin_lock_init(&host->lock);
491 init_waitqueue_head(&host->wq);
492 INIT_DELAYED_WORK(&host->detect, mmc_rescan);
493 INIT_DELAYED_WORK(&host->sdio_irq_work, sdio_irq_work);
494 timer_setup(&host->retune_timer, mmc_retune_timer, 0);
497 * By default, hosts do not support SGIO or large requests.
498 * They have to set these according to their abilities.
501 host->max_seg_size = PAGE_SIZE;
503 host->max_req_size = PAGE_SIZE;
504 host->max_blk_size = 512;
505 host->max_blk_count = PAGE_SIZE / 512;
507 host->fixed_drv_type = -EINVAL;
508 host->ios.power_delay_ms = 10;
509 host->ios.power_mode = MMC_POWER_UNDEFINED;
514 EXPORT_SYMBOL(mmc_alloc_host);
516 static int mmc_validate_host_caps(struct mmc_host *host)
518 if (host->caps & MMC_CAP_SDIO_IRQ && !host->ops->enable_sdio_irq) {
519 dev_warn(host->parent, "missing ->enable_sdio_irq() ops\n");
527 * mmc_add_host - initialise host hardware
530 * Register the host with the driver model. The host must be
531 * prepared to start servicing requests before this function
534 int mmc_add_host(struct mmc_host *host)
538 err = mmc_validate_host_caps(host);
542 err = device_add(&host->class_dev);
546 led_trigger_register_simple(dev_name(&host->class_dev), &host->led);
548 #ifdef CONFIG_DEBUG_FS
549 mmc_add_host_debugfs(host);
552 mmc_start_host(host);
556 EXPORT_SYMBOL(mmc_add_host);
559 * mmc_remove_host - remove host hardware
562 * Unregister and remove all cards associated with this host,
563 * and power down the MMC bus. No new requests will be issued
564 * after this function has returned.
566 void mmc_remove_host(struct mmc_host *host)
570 #ifdef CONFIG_DEBUG_FS
571 mmc_remove_host_debugfs(host);
574 device_del(&host->class_dev);
576 led_trigger_unregister_simple(host->led);
579 EXPORT_SYMBOL(mmc_remove_host);
582 * mmc_free_host - free the host structure
585 * Free the host once all references to it have been dropped.
587 void mmc_free_host(struct mmc_host *host)
589 mmc_pwrseq_free(host);
590 put_device(&host->class_dev);
593 EXPORT_SYMBOL(mmc_free_host);