treewide: Use fallthrough pseudo-keyword
[linux-block.git] / drivers / mmc / core / host.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
b93931a6
PO
2/*
3 * linux/drivers/mmc/core/host.c
4 *
5 * Copyright (C) 2003 Russell King, All Rights Reserved.
ff3112f5 6 * Copyright (C) 2007-2008 Pierre Ossman
04566831 7 * Copyright (C) 2010 Linus Walleij
b93931a6 8 *
b93931a6
PO
9 * MMC host class device management
10 */
11
12#include <linux/device.h>
13#include <linux/err.h>
14#include <linux/idr.h>
6c56e7a0
GL
15#include <linux/of.h>
16#include <linux/of_gpio.h>
b93931a6 17#include <linux/pagemap.h>
b52fb259 18#include <linux/pm_wakeup.h>
3ef77af1 19#include <linux/export.h>
af8350c7 20#include <linux/leds.h>
5a0e3ad6 21#include <linux/slab.h>
b93931a6
PO
22
23#include <linux/mmc/host.h>
04566831 24#include <linux/mmc/card.h>
6c56e7a0 25#include <linux/mmc/slot-gpio.h>
b93931a6
PO
26
27#include "core.h"
28#include "host.h"
df8aca16 29#include "slot-gpio.h"
3aa8793f 30#include "pwrseq.h"
68269660 31#include "sdio_ops.h"
b93931a6
PO
32
33#define cls_dev_to_mmc_host(d) container_of(d, struct mmc_host, class_dev)
34
5674a9ba 35static DEFINE_IDA(mmc_host_ida);
e2d1926b 36
b93931a6
PO
37static void mmc_host_classdev_release(struct device *dev)
38{
39 struct mmc_host *host = cls_dev_to_mmc_host(dev);
b52fb259 40 wakeup_source_unregister(host->ws);
7413d1f5 41 ida_simple_remove(&mmc_host_ida, host->index);
b93931a6
PO
42 kfree(host);
43}
44
45static struct class mmc_host_class = {
46 .name = "mmc_host",
47 .dev_release = mmc_host_classdev_release,
48};
49
50int mmc_register_host_class(void)
51{
52 return class_register(&mmc_host_class);
53}
54
55void mmc_unregister_host_class(void)
56{
57 class_unregister(&mmc_host_class);
58}
59
dfa13ebb
AH
60void mmc_retune_enable(struct mmc_host *host)
61{
62 host->can_retune = 1;
63 if (host->retune_period)
64 mod_timer(&host->retune_timer,
65 jiffies + host->retune_period * HZ);
66}
67
7ff27609
AH
68/*
69 * Pause re-tuning for a small set of operations. The pause begins after the
70 * next command and after first doing re-tuning.
71 */
72void mmc_retune_pause(struct mmc_host *host)
73{
74 if (!host->retune_paused) {
75 host->retune_paused = 1;
76 mmc_retune_needed(host);
77 mmc_retune_hold(host);
78 }
79}
80EXPORT_SYMBOL(mmc_retune_pause);
81
82void mmc_retune_unpause(struct mmc_host *host)
83{
84 if (host->retune_paused) {
85 host->retune_paused = 0;
86 mmc_retune_release(host);
87 }
88}
89EXPORT_SYMBOL(mmc_retune_unpause);
90
dfa13ebb
AH
91void mmc_retune_disable(struct mmc_host *host)
92{
7ff27609 93 mmc_retune_unpause(host);
dfa13ebb
AH
94 host->can_retune = 0;
95 del_timer_sync(&host->retune_timer);
96 host->retune_now = 0;
97 host->need_retune = 0;
98}
99
100void mmc_retune_timer_stop(struct mmc_host *host)
101{
102 del_timer_sync(&host->retune_timer);
103}
104EXPORT_SYMBOL(mmc_retune_timer_stop);
105
106void mmc_retune_hold(struct mmc_host *host)
107{
108 if (!host->hold_retune)
109 host->retune_now = 1;
110 host->hold_retune += 1;
111}
112
113void mmc_retune_release(struct mmc_host *host)
114{
115 if (host->hold_retune)
116 host->hold_retune -= 1;
117 else
118 WARN_ON(1);
119}
cb39f61e 120EXPORT_SYMBOL(mmc_retune_release);
dfa13ebb
AH
121
122int mmc_retune(struct mmc_host *host)
123{
6376f69d 124 bool return_to_hs400 = false;
dfa13ebb
AH
125 int err;
126
127 if (host->retune_now)
128 host->retune_now = 0;
129 else
130 return 0;
131
132 if (!host->need_retune || host->doing_retune || !host->card)
133 return 0;
134
135 host->need_retune = 0;
136
137 host->doing_retune = 1;
138
6376f69d
AH
139 if (host->ios.timing == MMC_TIMING_MMC_HS400) {
140 err = mmc_hs400_to_hs200(host->card);
141 if (err)
142 goto out;
143
144 return_to_hs400 = true;
6376f69d
AH
145 }
146
dfa13ebb 147 err = mmc_execute_tuning(host->card);
6376f69d
AH
148 if (err)
149 goto out;
dfa13ebb 150
6376f69d
AH
151 if (return_to_hs400)
152 err = mmc_hs200_to_hs400(host->card);
153out:
dfa13ebb
AH
154 host->doing_retune = 0;
155
156 return err;
157}
158
e99e88a9 159static void mmc_retune_timer(struct timer_list *t)
dfa13ebb 160{
e99e88a9 161 struct mmc_host *host = from_timer(host, t, retune_timer);
dfa13ebb
AH
162
163 mmc_retune_needed(host);
164}
165
6c56e7a0
GL
166/**
167 * mmc_of_parse() - parse host's device-tree node
168 * @host: host whose node should be parsed.
169 *
170 * To keep the rest of the MMC subsystem unaware of whether DT has been
171 * used to to instantiate and configure this host instance or not, we
172 * parse the properties and set respective generic mmc-host flags and
173 * parameters.
174 */
ec0a7517 175int mmc_of_parse(struct mmc_host *host)
6c56e7a0 176{
73a47a9b 177 struct device *dev = host->parent;
bfd694d5 178 u32 bus_width, drv_type, cd_debounce_delay_ms;
90614cd9 179 int ret;
6c56e7a0 180
73a47a9b 181 if (!dev || !dev_fwnode(dev))
ec0a7517 182 return 0;
6c56e7a0 183
6c56e7a0 184 /* "bus-width" is translated to MMC_CAP_*_BIT_DATA flags */
73a47a9b 185 if (device_property_read_u32(dev, "bus-width", &bus_width) < 0) {
6c56e7a0
GL
186 dev_dbg(host->parent,
187 "\"bus-width\" property is missing, assuming 1 bit.\n");
188 bus_width = 1;
189 }
190
191 switch (bus_width) {
192 case 8:
193 host->caps |= MMC_CAP_8_BIT_DATA;
df561f66 194 fallthrough; /* Hosts capable of 8-bit can also do 4 bits */
6c56e7a0
GL
195 case 4:
196 host->caps |= MMC_CAP_4_BIT_DATA;
197 break;
198 case 1:
199 break;
200 default:
201 dev_err(host->parent,
1c279f4f 202 "Invalid \"bus-width\" value %u!\n", bus_width);
ec0a7517 203 return -EINVAL;
6c56e7a0
GL
204 }
205
206 /* f_max is obtained from the optional "max-frequency" property */
73a47a9b 207 device_property_read_u32(dev, "max-frequency", &host->f_max);
6c56e7a0
GL
208
209 /*
210 * Configure CD and WP pins. They are both by default active low to
211 * match the SDHCI spec. If GPIOs are provided for CD and / or WP, the
212 * mmc-gpio helpers are used to attach, configure and use them. If
213 * polarity inversion is specified in DT, one of MMC_CAP2_CD_ACTIVE_HIGH
214 * and MMC_CAP2_RO_ACTIVE_HIGH capability-2 flags is set. If the
215 * "broken-cd" property is provided, the MMC_CAP_NEEDS_POLL capability
216 * is set. If the "non-removable" property is found, the
217 * MMC_CAP_NONREMOVABLE capability is set and no card-detection
218 * configuration is performed.
219 */
220
221 /* Parse Card Detection */
0f7c815d 222
73a47a9b 223 if (device_property_read_bool(dev, "non-removable")) {
6c56e7a0
GL
224 host->caps |= MMC_CAP_NONREMOVABLE;
225 } else {
0f7c815d
MM
226 if (device_property_read_bool(dev, "cd-inverted"))
227 host->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
6c56e7a0 228
bfd694d5
SL
229 if (device_property_read_u32(dev, "cd-debounce-delay-ms",
230 &cd_debounce_delay_ms))
231 cd_debounce_delay_ms = 200;
232
73a47a9b 233 if (device_property_read_bool(dev, "broken-cd"))
6c56e7a0
GL
234 host->caps |= MMC_CAP_NEEDS_POLL;
235
e3e4767b 236 ret = mmc_gpiod_request_cd(host, "cd", 0, false,
d0052ad9 237 cd_debounce_delay_ms * 1000);
9116752f 238 if (!ret)
98e90de9 239 dev_info(host->parent, "Got CD GPIO\n");
43934ece 240 else if (ret != -ENOENT && ret != -ENOSYS)
9116752f 241 return ret;
6c56e7a0
GL
242 }
243
244 /* Parse Write Protection */
6c56e7a0 245
9073d10b
MM
246 if (device_property_read_bool(dev, "wp-inverted"))
247 host->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
248
d0052ad9 249 ret = mmc_gpiod_request_ro(host, "wp", 0, 0);
9116752f 250 if (!ret)
98e90de9 251 dev_info(host->parent, "Got WP GPIO\n");
43934ece 252 else if (ret != -ENOENT && ret != -ENOSYS)
9116752f 253 return ret;
2fdb6e2d 254
73a47a9b 255 if (device_property_read_bool(dev, "disable-wp"))
19f44246
LPC
256 host->caps2 |= MMC_CAP2_NO_WRITE_PROTECT;
257
73a47a9b 258 if (device_property_read_bool(dev, "cap-sd-highspeed"))
2fdb6e2d 259 host->caps |= MMC_CAP_SD_HIGHSPEED;
73a47a9b 260 if (device_property_read_bool(dev, "cap-mmc-highspeed"))
2fdb6e2d 261 host->caps |= MMC_CAP_MMC_HIGHSPEED;
73a47a9b 262 if (device_property_read_bool(dev, "sd-uhs-sdr12"))
b66bd0e4 263 host->caps |= MMC_CAP_UHS_SDR12;
73a47a9b 264 if (device_property_read_bool(dev, "sd-uhs-sdr25"))
b66bd0e4 265 host->caps |= MMC_CAP_UHS_SDR25;
73a47a9b 266 if (device_property_read_bool(dev, "sd-uhs-sdr50"))
b66bd0e4 267 host->caps |= MMC_CAP_UHS_SDR50;
73a47a9b 268 if (device_property_read_bool(dev, "sd-uhs-sdr104"))
b66bd0e4 269 host->caps |= MMC_CAP_UHS_SDR104;
73a47a9b 270 if (device_property_read_bool(dev, "sd-uhs-ddr50"))
b66bd0e4 271 host->caps |= MMC_CAP_UHS_DDR50;
73a47a9b 272 if (device_property_read_bool(dev, "cap-power-off-card"))
2fdb6e2d 273 host->caps |= MMC_CAP_POWER_OFF_CARD;
73a47a9b 274 if (device_property_read_bool(dev, "cap-mmc-hw-reset"))
794f1578 275 host->caps |= MMC_CAP_HW_RESET;
73a47a9b 276 if (device_property_read_bool(dev, "cap-sdio-irq"))
2fdb6e2d 277 host->caps |= MMC_CAP_SDIO_IRQ;
73a47a9b 278 if (device_property_read_bool(dev, "full-pwr-cycle"))
5a36d6bc 279 host->caps2 |= MMC_CAP2_FULL_PWR_CYCLE;
ec02760b
YS
280 if (device_property_read_bool(dev, "full-pwr-cycle-in-suspend"))
281 host->caps2 |= MMC_CAP2_FULL_PWR_CYCLE_IN_SUSPEND;
73a47a9b 282 if (device_property_read_bool(dev, "keep-power-in-suspend"))
2fdb6e2d 283 host->pm_caps |= MMC_PM_KEEP_POWER;
73a47a9b
DW
284 if (device_property_read_bool(dev, "wakeup-source") ||
285 device_property_read_bool(dev, "enable-sdio-wakeup")) /* legacy */
2fdb6e2d 286 host->pm_caps |= MMC_PM_WAKE_SDIO_IRQ;
73a47a9b 287 if (device_property_read_bool(dev, "mmc-ddr-3_3v"))
fb00f196 288 host->caps |= MMC_CAP_3_3V_DDR;
73a47a9b 289 if (device_property_read_bool(dev, "mmc-ddr-1_8v"))
c0baf848 290 host->caps |= MMC_CAP_1_8V_DDR;
73a47a9b 291 if (device_property_read_bool(dev, "mmc-ddr-1_2v"))
c0baf848 292 host->caps |= MMC_CAP_1_2V_DDR;
73a47a9b 293 if (device_property_read_bool(dev, "mmc-hs200-1_8v"))
321bd41d 294 host->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
73a47a9b 295 if (device_property_read_bool(dev, "mmc-hs200-1_2v"))
321bd41d 296 host->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
73a47a9b 297 if (device_property_read_bool(dev, "mmc-hs400-1_8v"))
c373eb48 298 host->caps2 |= MMC_CAP2_HS400_1_8V | MMC_CAP2_HS200_1_8V_SDR;
73a47a9b 299 if (device_property_read_bool(dev, "mmc-hs400-1_2v"))
c373eb48 300 host->caps2 |= MMC_CAP2_HS400_1_2V | MMC_CAP2_HS200_1_2V_SDR;
73a47a9b 301 if (device_property_read_bool(dev, "mmc-hs400-enhanced-strobe"))
ef29c0e2 302 host->caps2 |= MMC_CAP2_HS400_ES;
73a47a9b 303 if (device_property_read_bool(dev, "no-sdio"))
6ae3e537 304 host->caps2 |= MMC_CAP2_NO_SDIO;
73a47a9b 305 if (device_property_read_bool(dev, "no-sd"))
6ae3e537 306 host->caps2 |= MMC_CAP2_NO_SD;
73a47a9b 307 if (device_property_read_bool(dev, "no-mmc"))
6ae3e537 308 host->caps2 |= MMC_CAP2_NO_MMC;
ec0a7517 309
6186d06c
WS
310 /* Must be after "non-removable" check */
311 if (device_property_read_u32(dev, "fixed-emmc-driver-type", &drv_type) == 0) {
312 if (host->caps & MMC_CAP_NONREMOVABLE)
313 host->fixed_drv_type = drv_type;
314 else
315 dev_err(host->parent,
316 "can't use fixed driver type, media is removable\n");
317 }
318
73a47a9b 319 host->dsr_req = !device_property_read_u32(dev, "dsr", &host->dsr);
3d705d14
SH
320 if (host->dsr_req && (host->dsr & ~0xffff)) {
321 dev_err(host->parent,
322 "device tree specified broken value for DSR: 0x%x, ignoring\n",
323 host->dsr);
324 host->dsr_req = 0;
325 }
326
6d796c68
SL
327 device_property_read_u32(dev, "post-power-on-delay-ms",
328 &host->ios.power_delay_ms);
329
3aa8793f 330 return mmc_pwrseq_alloc(host);
6c56e7a0
GL
331}
332
333EXPORT_SYMBOL(mmc_of_parse);
334
9d2d2430
UH
335/**
336 * mmc_of_parse_voltage - return mask of supported voltages
337 * @np: The device node need to be parsed.
338 * @mask: mask of voltages available for MMC/SD/SDIO
339 *
340 * Parse the "voltage-ranges" DT property, returning zero if it is not
341 * found, negative errno if the voltage-range specification is invalid,
342 * or one if the voltage-range is specified and successfully parsed.
343 */
344int mmc_of_parse_voltage(struct device_node *np, u32 *mask)
345{
346 const u32 *voltage_ranges;
347 int num_ranges, i;
348
349 voltage_ranges = of_get_property(np, "voltage-ranges", &num_ranges);
9d2d2430
UH
350 if (!voltage_ranges) {
351 pr_debug("%pOF: voltage-ranges unspecified\n", np);
352 return 0;
353 }
42c38d4a 354 num_ranges = num_ranges / sizeof(*voltage_ranges) / 2;
9d2d2430
UH
355 if (!num_ranges) {
356 pr_err("%pOF: voltage-ranges empty\n", np);
357 return -EINVAL;
358 }
359
360 for (i = 0; i < num_ranges; i++) {
361 const int j = i * 2;
362 u32 ocr_mask;
363
364 ocr_mask = mmc_vddrange_to_ocrmask(
365 be32_to_cpu(voltage_ranges[j]),
366 be32_to_cpu(voltage_ranges[j + 1]));
367 if (!ocr_mask) {
368 pr_err("%pOF: voltage-range #%d is invalid\n",
369 np, i);
370 return -EINVAL;
371 }
372 *mask |= ocr_mask;
373 }
374
375 return 1;
376}
377EXPORT_SYMBOL(mmc_of_parse_voltage);
378
b93931a6
PO
379/**
380 * mmc_alloc_host - initialise the per-host structure.
381 * @extra: sizeof private data structure
382 * @dev: pointer to host device model structure
383 *
384 * Initialise the per-host structure.
385 */
386struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
387{
ff3112f5 388 int err;
b93931a6
PO
389 struct mmc_host *host;
390
be760a9d 391 host = kzalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL);
b93931a6
PO
392 if (!host)
393 return NULL;
394
d9adcc12
GL
395 /* scanning will be enabled when we're ready */
396 host->rescan_disable = 1;
5674a9ba 397
7413d1f5
HK
398 err = ida_simple_get(&mmc_host_ida, 0, 0, GFP_KERNEL);
399 if (err < 0) {
5674a9ba
UH
400 kfree(host);
401 return NULL;
402 }
403
7413d1f5 404 host->index = err;
ff3112f5 405
d1b26863 406 dev_set_name(&host->class_dev, "mmc%d", host->index);
b52fb259 407 host->ws = wakeup_source_register(NULL, dev_name(&host->class_dev));
ff3112f5 408
b93931a6
PO
409 host->parent = dev;
410 host->class_dev.parent = dev;
411 host->class_dev.class = &mmc_host_class;
412 device_initialize(&host->class_dev);
ccf7bfdc 413 device_enable_async_suspend(&host->class_dev);
b93931a6 414
df8aca16
UH
415 if (mmc_gpio_alloc(host)) {
416 put_device(&host->class_dev);
417 return NULL;
418 }
04566831 419
b93931a6
PO
420 spin_lock_init(&host->lock);
421 init_waitqueue_head(&host->wq);
422 INIT_DELAYED_WORK(&host->detect, mmc_rescan);
68269660 423 INIT_DELAYED_WORK(&host->sdio_irq_work, sdio_irq_work);
e99e88a9 424 timer_setup(&host->retune_timer, mmc_retune_timer, 0);
b93931a6
PO
425
426 /*
427 * By default, hosts do not support SGIO or large requests.
428 * They have to set these according to their abilities.
429 */
a36274e0 430 host->max_segs = 1;
09cbfeaf 431 host->max_seg_size = PAGE_SIZE;
b93931a6 432
09cbfeaf 433 host->max_req_size = PAGE_SIZE;
b93931a6 434 host->max_blk_size = 512;
09cbfeaf 435 host->max_blk_count = PAGE_SIZE / 512;
b93931a6 436
6186d06c 437 host->fixed_drv_type = -EINVAL;
6d796c68 438 host->ios.power_delay_ms = 10;
ed54ef98 439 host->ios.power_mode = MMC_POWER_UNDEFINED;
6186d06c 440
b93931a6
PO
441 return host;
442}
443
444EXPORT_SYMBOL(mmc_alloc_host);
445
446/**
447 * mmc_add_host - initialise host hardware
448 * @host: mmc host
67a61c48
PO
449 *
450 * Register the host with the driver model. The host must be
451 * prepared to start servicing requests before this function
452 * completes.
b93931a6
PO
453 */
454int mmc_add_host(struct mmc_host *host)
455{
456 int err;
457
17b759af
NP
458 WARN_ON((host->caps & MMC_CAP_SDIO_IRQ) &&
459 !host->ops->enable_sdio_irq);
460
b93931a6
PO
461 err = device_add(&host->class_dev);
462 if (err)
463 return err;
464
f317dfeb
WS
465 led_trigger_register_simple(dev_name(&host->class_dev), &host->led);
466
6edd8ee6
HS
467#ifdef CONFIG_DEBUG_FS
468 mmc_add_host_debugfs(host);
469#endif
470
b93931a6 471 mmc_start_host(host);
8dede18e 472 mmc_register_pm_notifier(host);
b93931a6
PO
473
474 return 0;
475}
476
477EXPORT_SYMBOL(mmc_add_host);
478
479/**
480 * mmc_remove_host - remove host hardware
481 * @host: mmc host
482 *
483 * Unregister and remove all cards associated with this host,
67a61c48
PO
484 * and power down the MMC bus. No new requests will be issued
485 * after this function has returned.
b93931a6
PO
486 */
487void mmc_remove_host(struct mmc_host *host)
488{
8dede18e 489 mmc_unregister_pm_notifier(host);
b93931a6
PO
490 mmc_stop_host(host);
491
6edd8ee6
HS
492#ifdef CONFIG_DEBUG_FS
493 mmc_remove_host_debugfs(host);
494#endif
495
b93931a6
PO
496 device_del(&host->class_dev);
497
77f1fd6e 498 led_trigger_unregister_simple(host->led);
b93931a6
PO
499}
500
501EXPORT_SYMBOL(mmc_remove_host);
502
503/**
504 * mmc_free_host - free the host structure
505 * @host: mmc host
506 *
507 * Free the host once all references to it have been dropped.
508 */
509void mmc_free_host(struct mmc_host *host)
510{
3aa8793f 511 mmc_pwrseq_free(host);
b93931a6
PO
512 put_device(&host->class_dev);
513}
514
515EXPORT_SYMBOL(mmc_free_host);