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/export.h>
19 #include <linux/leds.h>
20 #include <linux/slab.h>
22 #include <linux/mmc/host.h>
23 #include <linux/mmc/card.h>
24 #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 ida_simple_remove(&mmc_host_ida, host->index);
79 static int mmc_host_classdev_shutdown(struct device *dev)
81 struct mmc_host *host = cls_dev_to_mmc_host(dev);
83 __mmc_stop_host(host);
87 static struct class mmc_host_class = {
89 .dev_release = mmc_host_classdev_release,
90 .shutdown_pre = mmc_host_classdev_shutdown,
91 .pm = MMC_HOST_CLASS_DEV_PM_OPS,
94 int mmc_register_host_class(void)
96 return class_register(&mmc_host_class);
99 void mmc_unregister_host_class(void)
101 class_unregister(&mmc_host_class);
104 void mmc_retune_enable(struct mmc_host *host)
106 host->can_retune = 1;
107 if (host->retune_period)
108 mod_timer(&host->retune_timer,
109 jiffies + host->retune_period * HZ);
113 * Pause re-tuning for a small set of operations. The pause begins after the
114 * next command and after first doing re-tuning.
116 void mmc_retune_pause(struct mmc_host *host)
118 if (!host->retune_paused) {
119 host->retune_paused = 1;
120 mmc_retune_needed(host);
121 mmc_retune_hold(host);
124 EXPORT_SYMBOL(mmc_retune_pause);
126 void mmc_retune_unpause(struct mmc_host *host)
128 if (host->retune_paused) {
129 host->retune_paused = 0;
130 mmc_retune_release(host);
133 EXPORT_SYMBOL(mmc_retune_unpause);
135 void mmc_retune_disable(struct mmc_host *host)
137 mmc_retune_unpause(host);
138 host->can_retune = 0;
139 del_timer_sync(&host->retune_timer);
140 host->retune_now = 0;
141 host->need_retune = 0;
144 void mmc_retune_timer_stop(struct mmc_host *host)
146 del_timer_sync(&host->retune_timer);
148 EXPORT_SYMBOL(mmc_retune_timer_stop);
150 void mmc_retune_hold(struct mmc_host *host)
152 if (!host->hold_retune)
153 host->retune_now = 1;
154 host->hold_retune += 1;
157 void mmc_retune_release(struct mmc_host *host)
159 if (host->hold_retune)
160 host->hold_retune -= 1;
164 EXPORT_SYMBOL(mmc_retune_release);
166 int mmc_retune(struct mmc_host *host)
168 bool return_to_hs400 = false;
171 if (host->retune_now)
172 host->retune_now = 0;
176 if (!host->need_retune || host->doing_retune || !host->card)
179 host->need_retune = 0;
181 host->doing_retune = 1;
183 if (host->ios.timing == MMC_TIMING_MMC_HS400) {
184 err = mmc_hs400_to_hs200(host->card);
188 return_to_hs400 = true;
191 err = mmc_execute_tuning(host->card);
196 err = mmc_hs200_to_hs400(host->card);
198 host->doing_retune = 0;
203 static void mmc_retune_timer(struct timer_list *t)
205 struct mmc_host *host = from_timer(host, t, retune_timer);
207 mmc_retune_needed(host);
211 * mmc_of_parse() - parse host's device-tree node
212 * @host: host whose node should be parsed.
214 * To keep the rest of the MMC subsystem unaware of whether DT has been
215 * used to to instantiate and configure this host instance or not, we
216 * parse the properties and set respective generic mmc-host flags and
219 int mmc_of_parse(struct mmc_host *host)
221 struct device *dev = host->parent;
222 u32 bus_width, drv_type, cd_debounce_delay_ms;
224 bool cd_cap_invert, cd_gpio_invert = false;
226 if (!dev || !dev_fwnode(dev))
229 /* "bus-width" is translated to MMC_CAP_*_BIT_DATA flags */
230 if (device_property_read_u32(dev, "bus-width", &bus_width) < 0) {
231 dev_dbg(host->parent,
232 "\"bus-width\" property is missing, assuming 1 bit.\n");
238 host->caps |= MMC_CAP_8_BIT_DATA;
239 /* fall through - Hosts capable of 8-bit can also do 4 bits */
241 host->caps |= MMC_CAP_4_BIT_DATA;
246 dev_err(host->parent,
247 "Invalid \"bus-width\" value %u!\n", bus_width);
251 /* f_max is obtained from the optional "max-frequency" property */
252 device_property_read_u32(dev, "max-frequency", &host->f_max);
255 * Configure CD and WP pins. They are both by default active low to
256 * match the SDHCI spec. If GPIOs are provided for CD and / or WP, the
257 * mmc-gpio helpers are used to attach, configure and use them. If
258 * polarity inversion is specified in DT, one of MMC_CAP2_CD_ACTIVE_HIGH
259 * and MMC_CAP2_RO_ACTIVE_HIGH capability-2 flags is set. If the
260 * "broken-cd" property is provided, the MMC_CAP_NEEDS_POLL capability
261 * is set. If the "non-removable" property is found, the
262 * MMC_CAP_NONREMOVABLE capability is set and no card-detection
263 * configuration is performed.
266 /* Parse Card Detection */
267 if (device_property_read_bool(dev, "non-removable")) {
268 host->caps |= MMC_CAP_NONREMOVABLE;
270 cd_cap_invert = device_property_read_bool(dev, "cd-inverted");
272 if (device_property_read_u32(dev, "cd-debounce-delay-ms",
273 &cd_debounce_delay_ms))
274 cd_debounce_delay_ms = 200;
276 if (device_property_read_bool(dev, "broken-cd"))
277 host->caps |= MMC_CAP_NEEDS_POLL;
279 ret = mmc_gpiod_request_cd(host, "cd", 0, false,
280 cd_debounce_delay_ms * 1000,
283 dev_info(host->parent, "Got CD GPIO\n");
284 else if (ret != -ENOENT && ret != -ENOSYS)
288 * There are two ways to flag that the CD line is inverted:
289 * through the cd-inverted flag and by the GPIO line itself
290 * being inverted from the GPIO subsystem. This is a leftover
291 * from the times when the GPIO subsystem did not make it
292 * possible to flag a line as inverted.
294 * If the capability on the host AND the GPIO line are
295 * both inverted, the end result is that the CD line is
298 if (cd_cap_invert ^ cd_gpio_invert)
299 host->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
302 /* Parse Write Protection */
304 if (device_property_read_bool(dev, "wp-inverted"))
305 host->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
307 ret = mmc_gpiod_request_ro(host, "wp", 0, 0, NULL);
309 dev_info(host->parent, "Got WP GPIO\n");
310 else if (ret != -ENOENT && ret != -ENOSYS)
313 if (device_property_read_bool(dev, "disable-wp"))
314 host->caps2 |= MMC_CAP2_NO_WRITE_PROTECT;
316 if (device_property_read_bool(dev, "cap-sd-highspeed"))
317 host->caps |= MMC_CAP_SD_HIGHSPEED;
318 if (device_property_read_bool(dev, "cap-mmc-highspeed"))
319 host->caps |= MMC_CAP_MMC_HIGHSPEED;
320 if (device_property_read_bool(dev, "sd-uhs-sdr12"))
321 host->caps |= MMC_CAP_UHS_SDR12;
322 if (device_property_read_bool(dev, "sd-uhs-sdr25"))
323 host->caps |= MMC_CAP_UHS_SDR25;
324 if (device_property_read_bool(dev, "sd-uhs-sdr50"))
325 host->caps |= MMC_CAP_UHS_SDR50;
326 if (device_property_read_bool(dev, "sd-uhs-sdr104"))
327 host->caps |= MMC_CAP_UHS_SDR104;
328 if (device_property_read_bool(dev, "sd-uhs-ddr50"))
329 host->caps |= MMC_CAP_UHS_DDR50;
330 if (device_property_read_bool(dev, "cap-power-off-card"))
331 host->caps |= MMC_CAP_POWER_OFF_CARD;
332 if (device_property_read_bool(dev, "cap-mmc-hw-reset"))
333 host->caps |= MMC_CAP_HW_RESET;
334 if (device_property_read_bool(dev, "cap-sdio-irq"))
335 host->caps |= MMC_CAP_SDIO_IRQ;
336 if (device_property_read_bool(dev, "full-pwr-cycle"))
337 host->caps2 |= MMC_CAP2_FULL_PWR_CYCLE;
338 if (device_property_read_bool(dev, "keep-power-in-suspend"))
339 host->pm_caps |= MMC_PM_KEEP_POWER;
340 if (device_property_read_bool(dev, "wakeup-source") ||
341 device_property_read_bool(dev, "enable-sdio-wakeup")) /* legacy */
342 host->pm_caps |= MMC_PM_WAKE_SDIO_IRQ;
343 if (device_property_read_bool(dev, "mmc-ddr-3_3v"))
344 host->caps |= MMC_CAP_3_3V_DDR;
345 if (device_property_read_bool(dev, "mmc-ddr-1_8v"))
346 host->caps |= MMC_CAP_1_8V_DDR;
347 if (device_property_read_bool(dev, "mmc-ddr-1_2v"))
348 host->caps |= MMC_CAP_1_2V_DDR;
349 if (device_property_read_bool(dev, "mmc-hs200-1_8v"))
350 host->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
351 if (device_property_read_bool(dev, "mmc-hs200-1_2v"))
352 host->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
353 if (device_property_read_bool(dev, "mmc-hs400-1_8v"))
354 host->caps2 |= MMC_CAP2_HS400_1_8V | MMC_CAP2_HS200_1_8V_SDR;
355 if (device_property_read_bool(dev, "mmc-hs400-1_2v"))
356 host->caps2 |= MMC_CAP2_HS400_1_2V | MMC_CAP2_HS200_1_2V_SDR;
357 if (device_property_read_bool(dev, "mmc-hs400-enhanced-strobe"))
358 host->caps2 |= MMC_CAP2_HS400_ES;
359 if (device_property_read_bool(dev, "no-sdio"))
360 host->caps2 |= MMC_CAP2_NO_SDIO;
361 if (device_property_read_bool(dev, "no-sd"))
362 host->caps2 |= MMC_CAP2_NO_SD;
363 if (device_property_read_bool(dev, "no-mmc"))
364 host->caps2 |= MMC_CAP2_NO_MMC;
366 /* Must be after "non-removable" check */
367 if (device_property_read_u32(dev, "fixed-emmc-driver-type", &drv_type) == 0) {
368 if (host->caps & MMC_CAP_NONREMOVABLE)
369 host->fixed_drv_type = drv_type;
371 dev_err(host->parent,
372 "can't use fixed driver type, media is removable\n");
375 host->dsr_req = !device_property_read_u32(dev, "dsr", &host->dsr);
376 if (host->dsr_req && (host->dsr & ~0xffff)) {
377 dev_err(host->parent,
378 "device tree specified broken value for DSR: 0x%x, ignoring\n",
383 device_property_read_u32(dev, "post-power-on-delay-ms",
384 &host->ios.power_delay_ms);
386 return mmc_pwrseq_alloc(host);
389 EXPORT_SYMBOL(mmc_of_parse);
392 * mmc_of_parse_voltage - return mask of supported voltages
393 * @np: The device node need to be parsed.
394 * @mask: mask of voltages available for MMC/SD/SDIO
396 * Parse the "voltage-ranges" DT property, returning zero if it is not
397 * found, negative errno if the voltage-range specification is invalid,
398 * or one if the voltage-range is specified and successfully parsed.
400 int mmc_of_parse_voltage(struct device_node *np, u32 *mask)
402 const u32 *voltage_ranges;
405 voltage_ranges = of_get_property(np, "voltage-ranges", &num_ranges);
406 if (!voltage_ranges) {
407 pr_debug("%pOF: voltage-ranges unspecified\n", np);
410 num_ranges = num_ranges / sizeof(*voltage_ranges) / 2;
412 pr_err("%pOF: voltage-ranges empty\n", np);
416 for (i = 0; i < num_ranges; i++) {
420 ocr_mask = mmc_vddrange_to_ocrmask(
421 be32_to_cpu(voltage_ranges[j]),
422 be32_to_cpu(voltage_ranges[j + 1]));
424 pr_err("%pOF: voltage-range #%d is invalid\n",
433 EXPORT_SYMBOL(mmc_of_parse_voltage);
436 * mmc_alloc_host - initialise the per-host structure.
437 * @extra: sizeof private data structure
438 * @dev: pointer to host device model structure
440 * Initialise the per-host structure.
442 struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
445 struct mmc_host *host;
447 host = kzalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL);
451 /* scanning will be enabled when we're ready */
452 host->rescan_disable = 1;
454 err = ida_simple_get(&mmc_host_ida, 0, 0, GFP_KERNEL);
462 dev_set_name(&host->class_dev, "mmc%d", host->index);
465 host->class_dev.parent = dev;
466 host->class_dev.class = &mmc_host_class;
467 device_initialize(&host->class_dev);
468 device_enable_async_suspend(&host->class_dev);
470 if (mmc_gpio_alloc(host)) {
471 put_device(&host->class_dev);
475 spin_lock_init(&host->lock);
476 init_waitqueue_head(&host->wq);
477 INIT_DELAYED_WORK(&host->detect, mmc_rescan);
478 INIT_DELAYED_WORK(&host->sdio_irq_work, sdio_irq_work);
479 timer_setup(&host->retune_timer, mmc_retune_timer, 0);
482 * By default, hosts do not support SGIO or large requests.
483 * They have to set these according to their abilities.
486 host->max_seg_size = PAGE_SIZE;
488 host->max_req_size = PAGE_SIZE;
489 host->max_blk_size = 512;
490 host->max_blk_count = PAGE_SIZE / 512;
492 host->fixed_drv_type = -EINVAL;
493 host->ios.power_delay_ms = 10;
498 EXPORT_SYMBOL(mmc_alloc_host);
500 static int mmc_validate_host_caps(struct mmc_host *host)
502 if (host->caps & MMC_CAP_SDIO_IRQ && !host->ops->enable_sdio_irq) {
503 dev_warn(host->parent, "missing ->enable_sdio_irq() ops\n");
511 * mmc_add_host - initialise host hardware
514 * Register the host with the driver model. The host must be
515 * prepared to start servicing requests before this function
518 int mmc_add_host(struct mmc_host *host)
522 err = mmc_validate_host_caps(host);
526 err = device_add(&host->class_dev);
530 led_trigger_register_simple(dev_name(&host->class_dev), &host->led);
532 #ifdef CONFIG_DEBUG_FS
533 mmc_add_host_debugfs(host);
536 mmc_start_host(host);
540 EXPORT_SYMBOL(mmc_add_host);
543 * mmc_remove_host - remove host hardware
546 * Unregister and remove all cards associated with this host,
547 * and power down the MMC bus. No new requests will be issued
548 * after this function has returned.
550 void mmc_remove_host(struct mmc_host *host)
554 #ifdef CONFIG_DEBUG_FS
555 mmc_remove_host_debugfs(host);
558 device_del(&host->class_dev);
560 led_trigger_unregister_simple(host->led);
563 EXPORT_SYMBOL(mmc_remove_host);
566 * mmc_free_host - free the host structure
569 * Free the host once all references to it have been dropped.
571 void mmc_free_host(struct mmc_host *host)
573 mmc_pwrseq_free(host);
574 put_device(&host->class_dev);
577 EXPORT_SYMBOL(mmc_free_host);