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/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>
22
23 #include <linux/mmc/host.h>
24 #include <linux/mmc/card.h>
25 #include <linux/mmc/slot-gpio.h>
26
27 #include "core.h"
28 #include "crypto.h"
29 #include "host.h"
30 #include "slot-gpio.h"
31 #include "pwrseq.h"
32 #include "sdio_ops.h"
33
34 #define cls_dev_to_mmc_host(d) container_of(d, struct mmc_host, class_dev)
35
36 static DEFINE_IDA(mmc_host_ida);
37
38 #ifdef CONFIG_PM_SLEEP
mmc_host_class_prepare(struct device * dev)39 static int mmc_host_class_prepare(struct device *dev)
40 {
41 struct mmc_host *host = cls_dev_to_mmc_host(dev);
42
43 /*
44 * It's safe to access the bus_ops pointer, as both userspace and the
45 * workqueue for detecting cards are frozen at this point.
46 */
47 if (!host->bus_ops)
48 return 0;
49
50 /* Validate conditions for system suspend. */
51 if (host->bus_ops->pre_suspend)
52 return host->bus_ops->pre_suspend(host);
53
54 return 0;
55 }
56
mmc_host_class_complete(struct device * dev)57 static void mmc_host_class_complete(struct device *dev)
58 {
59 struct mmc_host *host = cls_dev_to_mmc_host(dev);
60
61 _mmc_detect_change(host, 0, false);
62 }
63
64 static const struct dev_pm_ops mmc_host_class_dev_pm_ops = {
65 .prepare = mmc_host_class_prepare,
66 .complete = mmc_host_class_complete,
67 };
68
69 #define MMC_HOST_CLASS_DEV_PM_OPS (&mmc_host_class_dev_pm_ops)
70 #else
71 #define MMC_HOST_CLASS_DEV_PM_OPS NULL
72 #endif
73
mmc_host_classdev_release(struct device * dev)74 static void mmc_host_classdev_release(struct device *dev)
75 {
76 struct mmc_host *host = cls_dev_to_mmc_host(dev);
77 wakeup_source_unregister(host->ws);
78 if (of_alias_get_id(host->parent->of_node, "mmc") < 0)
79 ida_simple_remove(&mmc_host_ida, host->index);
80 kfree(host);
81 }
82
mmc_host_classdev_shutdown(struct device * dev)83 static int mmc_host_classdev_shutdown(struct device *dev)
84 {
85 struct mmc_host *host = cls_dev_to_mmc_host(dev);
86
87 __mmc_stop_host(host);
88 return 0;
89 }
90
91 static struct class mmc_host_class = {
92 .name = "mmc_host",
93 .dev_release = mmc_host_classdev_release,
94 .shutdown_pre = mmc_host_classdev_shutdown,
95 .pm = MMC_HOST_CLASS_DEV_PM_OPS,
96 };
97
mmc_register_host_class(void)98 int mmc_register_host_class(void)
99 {
100 return class_register(&mmc_host_class);
101 }
102
mmc_unregister_host_class(void)103 void mmc_unregister_host_class(void)
104 {
105 class_unregister(&mmc_host_class);
106 }
107
mmc_retune_enable(struct mmc_host * host)108 void mmc_retune_enable(struct mmc_host *host)
109 {
110 host->can_retune = 1;
111 if (host->retune_period)
112 mod_timer(&host->retune_timer,
113 jiffies + host->retune_period * HZ);
114 }
115
116 /*
117 * Pause re-tuning for a small set of operations. The pause begins after the
118 * next command and after first doing re-tuning.
119 */
mmc_retune_pause(struct mmc_host * host)120 void mmc_retune_pause(struct mmc_host *host)
121 {
122 if (!host->retune_paused) {
123 host->retune_paused = 1;
124 mmc_retune_needed(host);
125 mmc_retune_hold(host);
126 }
127 }
128 EXPORT_SYMBOL(mmc_retune_pause);
129
mmc_retune_unpause(struct mmc_host * host)130 void mmc_retune_unpause(struct mmc_host *host)
131 {
132 if (host->retune_paused) {
133 host->retune_paused = 0;
134 mmc_retune_release(host);
135 }
136 }
137 EXPORT_SYMBOL(mmc_retune_unpause);
138
mmc_retune_disable(struct mmc_host * host)139 void mmc_retune_disable(struct mmc_host *host)
140 {
141 mmc_retune_unpause(host);
142 host->can_retune = 0;
143 del_timer_sync(&host->retune_timer);
144 host->retune_now = 0;
145 host->need_retune = 0;
146 }
147
mmc_retune_timer_stop(struct mmc_host * host)148 void mmc_retune_timer_stop(struct mmc_host *host)
149 {
150 del_timer_sync(&host->retune_timer);
151 }
152 EXPORT_SYMBOL(mmc_retune_timer_stop);
153
mmc_retune_hold(struct mmc_host * host)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
mmc_retune_release(struct mmc_host * host)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
mmc_retune(struct mmc_host * host)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
mmc_retune_timer(struct timer_list * t)207 static void mmc_retune_timer(struct timer_list *t)
208 {
209 struct mmc_host *host = from_timer(host, t, retune_timer);
210
211 mmc_retune_needed(host);
212 }
213
214 /**
215 * mmc_of_parse() - parse host's device-tree node
216 * @host: host whose node should be parsed.
217 *
218 * To keep the rest of the MMC subsystem unaware of whether DT has been
219 * used to to instantiate and configure this host instance or not, we
220 * parse the properties and set respective generic mmc-host flags and
221 * parameters.
222 */
mmc_of_parse(struct mmc_host * host)223 int mmc_of_parse(struct mmc_host *host)
224 {
225 struct device *dev = host->parent;
226 u32 bus_width, drv_type, cd_debounce_delay_ms;
227 int ret;
228
229 if (!dev || !dev_fwnode(dev))
230 return 0;
231
232 /* "bus-width" is translated to MMC_CAP_*_BIT_DATA flags */
233 if (device_property_read_u32(dev, "bus-width", &bus_width) < 0) {
234 dev_dbg(host->parent,
235 "\"bus-width\" property is missing, assuming 1 bit.\n");
236 bus_width = 1;
237 }
238
239 switch (bus_width) {
240 case 8:
241 host->caps |= MMC_CAP_8_BIT_DATA;
242 fallthrough; /* Hosts capable of 8-bit can also do 4 bits */
243 case 4:
244 host->caps |= MMC_CAP_4_BIT_DATA;
245 break;
246 case 1:
247 break;
248 default:
249 dev_err(host->parent,
250 "Invalid \"bus-width\" value %u!\n", bus_width);
251 return -EINVAL;
252 }
253
254 /* f_max is obtained from the optional "max-frequency" property */
255 device_property_read_u32(dev, "max-frequency", &host->f_max);
256
257 /*
258 * Configure CD and WP pins. They are both by default active low to
259 * match the SDHCI spec. If GPIOs are provided for CD and / or WP, the
260 * mmc-gpio helpers are used to attach, configure and use them. If
261 * polarity inversion is specified in DT, one of MMC_CAP2_CD_ACTIVE_HIGH
262 * and MMC_CAP2_RO_ACTIVE_HIGH capability-2 flags is set. If the
263 * "broken-cd" property is provided, the MMC_CAP_NEEDS_POLL capability
264 * is set. If the "non-removable" property is found, the
265 * MMC_CAP_NONREMOVABLE capability is set and no card-detection
266 * configuration is performed.
267 */
268
269 /* Parse Card Detection */
270
271 if (device_property_read_bool(dev, "non-removable")) {
272 host->caps |= MMC_CAP_NONREMOVABLE;
273 } else {
274 if (device_property_read_bool(dev, "cd-inverted"))
275 host->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
276
277 if (device_property_read_u32(dev, "cd-debounce-delay-ms",
278 &cd_debounce_delay_ms))
279 cd_debounce_delay_ms = 200;
280
281 if (device_property_read_bool(dev, "broken-cd"))
282 host->caps |= MMC_CAP_NEEDS_POLL;
283
284 ret = mmc_gpiod_request_cd(host, "cd", 0, false,
285 cd_debounce_delay_ms * 1000);
286 if (!ret)
287 dev_info(host->parent, "Got CD GPIO\n");
288 else if (ret != -ENOENT && ret != -ENOSYS)
289 return ret;
290 }
291
292 /* Parse Write Protection */
293
294 if (device_property_read_bool(dev, "wp-inverted"))
295 host->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
296
297 ret = mmc_gpiod_request_ro(host, "wp", 0, 0);
298 if (!ret)
299 dev_info(host->parent, "Got WP GPIO\n");
300 else if (ret != -ENOENT && ret != -ENOSYS)
301 return ret;
302
303 if (device_property_read_bool(dev, "disable-wp"))
304 host->caps2 |= MMC_CAP2_NO_WRITE_PROTECT;
305
306 if (device_property_read_bool(dev, "cap-sd-highspeed"))
307 host->caps |= MMC_CAP_SD_HIGHSPEED;
308 if (device_property_read_bool(dev, "cap-mmc-highspeed"))
309 host->caps |= MMC_CAP_MMC_HIGHSPEED;
310 if (device_property_read_bool(dev, "sd-uhs-sdr12"))
311 host->caps |= MMC_CAP_UHS_SDR12;
312 if (device_property_read_bool(dev, "sd-uhs-sdr25"))
313 host->caps |= MMC_CAP_UHS_SDR25;
314 if (device_property_read_bool(dev, "sd-uhs-sdr50"))
315 host->caps |= MMC_CAP_UHS_SDR50;
316 if (device_property_read_bool(dev, "sd-uhs-sdr104"))
317 host->caps |= MMC_CAP_UHS_SDR104;
318 if (device_property_read_bool(dev, "sd-uhs-ddr50"))
319 host->caps |= MMC_CAP_UHS_DDR50;
320 if (device_property_read_bool(dev, "cap-power-off-card"))
321 host->caps |= MMC_CAP_POWER_OFF_CARD;
322 if (device_property_read_bool(dev, "cap-mmc-hw-reset"))
323 host->caps |= MMC_CAP_HW_RESET;
324 if (device_property_read_bool(dev, "cap-sdio-irq"))
325 host->caps |= MMC_CAP_SDIO_IRQ;
326 if (device_property_read_bool(dev, "full-pwr-cycle"))
327 host->caps2 |= MMC_CAP2_FULL_PWR_CYCLE;
328 if (device_property_read_bool(dev, "full-pwr-cycle-in-suspend"))
329 host->caps2 |= MMC_CAP2_FULL_PWR_CYCLE_IN_SUSPEND;
330 if (device_property_read_bool(dev, "keep-power-in-suspend"))
331 host->pm_caps |= MMC_PM_KEEP_POWER;
332 if (device_property_read_bool(dev, "wakeup-source") ||
333 device_property_read_bool(dev, "enable-sdio-wakeup")) /* legacy */
334 host->pm_caps |= MMC_PM_WAKE_SDIO_IRQ;
335 if (device_property_read_bool(dev, "mmc-ddr-3_3v"))
336 host->caps |= MMC_CAP_3_3V_DDR;
337 if (device_property_read_bool(dev, "mmc-ddr-1_8v"))
338 host->caps |= MMC_CAP_1_8V_DDR;
339 if (device_property_read_bool(dev, "mmc-ddr-1_2v"))
340 host->caps |= MMC_CAP_1_2V_DDR;
341 if (device_property_read_bool(dev, "mmc-hs200-1_8v"))
342 host->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
343 if (device_property_read_bool(dev, "mmc-hs200-1_2v"))
344 host->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
345 if (device_property_read_bool(dev, "mmc-hs400-1_8v"))
346 host->caps2 |= MMC_CAP2_HS400_1_8V | MMC_CAP2_HS200_1_8V_SDR;
347 if (device_property_read_bool(dev, "mmc-hs400-1_2v"))
348 host->caps2 |= MMC_CAP2_HS400_1_2V | MMC_CAP2_HS200_1_2V_SDR;
349 if (device_property_read_bool(dev, "mmc-hs400-enhanced-strobe"))
350 host->caps2 |= MMC_CAP2_HS400_ES;
351 if (device_property_read_bool(dev, "no-sdio"))
352 host->caps2 |= MMC_CAP2_NO_SDIO;
353 if (device_property_read_bool(dev, "no-sd"))
354 host->caps2 |= MMC_CAP2_NO_SD;
355 if (device_property_read_bool(dev, "no-mmc"))
356 host->caps2 |= MMC_CAP2_NO_MMC;
357 if (device_property_read_bool(dev, "no-prescan-powerup"))
358 host->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP;
359
360 /* Must be after "non-removable" check */
361 if (device_property_read_u32(dev, "fixed-emmc-driver-type", &drv_type) == 0) {
362 if (host->caps & MMC_CAP_NONREMOVABLE)
363 host->fixed_drv_type = drv_type;
364 else
365 dev_err(host->parent,
366 "can't use fixed driver type, media is removable\n");
367 }
368
369 host->dsr_req = !device_property_read_u32(dev, "dsr", &host->dsr);
370 if (host->dsr_req && (host->dsr & ~0xffff)) {
371 dev_err(host->parent,
372 "device tree specified broken value for DSR: 0x%x, ignoring\n",
373 host->dsr);
374 host->dsr_req = 0;
375 }
376
377 device_property_read_u32(dev, "post-power-on-delay-ms",
378 &host->ios.power_delay_ms);
379
380 return mmc_pwrseq_alloc(host);
381 }
382
383 EXPORT_SYMBOL(mmc_of_parse);
384
385 /**
386 * mmc_of_parse_voltage - return mask of supported voltages
387 * @np: The device node need to be parsed.
388 * @mask: mask of voltages available for MMC/SD/SDIO
389 *
390 * Parse the "voltage-ranges" DT property, returning zero if it is not
391 * found, negative errno if the voltage-range specification is invalid,
392 * or one if the voltage-range is specified and successfully parsed.
393 */
mmc_of_parse_voltage(struct device_node * np,u32 * mask)394 int mmc_of_parse_voltage(struct device_node *np, u32 *mask)
395 {
396 const u32 *voltage_ranges;
397 int num_ranges, i;
398
399 voltage_ranges = of_get_property(np, "voltage-ranges", &num_ranges);
400 if (!voltage_ranges) {
401 pr_debug("%pOF: voltage-ranges unspecified\n", np);
402 return 0;
403 }
404 num_ranges = num_ranges / sizeof(*voltage_ranges) / 2;
405 if (!num_ranges) {
406 pr_err("%pOF: voltage-ranges empty\n", np);
407 return -EINVAL;
408 }
409
410 for (i = 0; i < num_ranges; i++) {
411 const int j = i * 2;
412 u32 ocr_mask;
413
414 ocr_mask = mmc_vddrange_to_ocrmask(
415 be32_to_cpu(voltage_ranges[j]),
416 be32_to_cpu(voltage_ranges[j + 1]));
417 if (!ocr_mask) {
418 pr_err("%pOF: voltage-range #%d is invalid\n",
419 np, i);
420 return -EINVAL;
421 }
422 *mask |= ocr_mask;
423 }
424
425 return 1;
426 }
427 EXPORT_SYMBOL(mmc_of_parse_voltage);
428
429 /**
430 * mmc_first_nonreserved_index() - get the first index that is not reserved
431 */
mmc_first_nonreserved_index(void)432 static int mmc_first_nonreserved_index(void)
433 {
434 int max;
435
436 max = of_alias_get_highest_id("mmc");
437 if (max < 0)
438 return 0;
439
440 return max + 1;
441 }
442
443 /**
444 * mmc_alloc_host - initialise the per-host structure.
445 * @extra: sizeof private data structure
446 * @dev: pointer to host device model structure
447 *
448 * Initialise the per-host structure.
449 */
mmc_alloc_host(int extra,struct device * dev)450 struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
451 {
452 int index;
453 struct mmc_host *host;
454 int alias_id, min_idx, max_idx;
455
456 host = kzalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL);
457 if (!host)
458 return NULL;
459
460 /* scanning will be enabled when we're ready */
461 host->rescan_disable = 1;
462
463 alias_id = of_alias_get_id(dev->of_node, "mmc");
464 if (alias_id >= 0) {
465 index = alias_id;
466 } else {
467 min_idx = mmc_first_nonreserved_index();
468 max_idx = 0;
469
470 index = ida_simple_get(&mmc_host_ida, min_idx, max_idx, GFP_KERNEL);
471 if (index < 0) {
472 kfree(host);
473 return NULL;
474 }
475 }
476
477 host->index = index;
478
479 dev_set_name(&host->class_dev, "mmc%d", host->index);
480 host->ws = wakeup_source_register(NULL, dev_name(&host->class_dev));
481
482 host->parent = dev;
483 host->class_dev.parent = dev;
484 host->class_dev.class = &mmc_host_class;
485 device_initialize(&host->class_dev);
486 device_enable_async_suspend(&host->class_dev);
487
488 if (mmc_gpio_alloc(host)) {
489 put_device(&host->class_dev);
490 return NULL;
491 }
492
493 spin_lock_init(&host->lock);
494 init_waitqueue_head(&host->wq);
495 INIT_DELAYED_WORK(&host->detect, mmc_rescan);
496 INIT_DELAYED_WORK(&host->sdio_irq_work, sdio_irq_work);
497 timer_setup(&host->retune_timer, mmc_retune_timer, 0);
498
499 /*
500 * By default, hosts do not support SGIO or large requests.
501 * They have to set these according to their abilities.
502 */
503 host->max_segs = 1;
504 host->max_seg_size = PAGE_SIZE;
505
506 host->max_req_size = PAGE_SIZE;
507 host->max_blk_size = 512;
508 host->max_blk_count = PAGE_SIZE / 512;
509
510 host->fixed_drv_type = -EINVAL;
511 host->ios.power_delay_ms = 10;
512 host->ios.power_mode = MMC_POWER_UNDEFINED;
513
514 return host;
515 }
516
517 EXPORT_SYMBOL(mmc_alloc_host);
518
mmc_validate_host_caps(struct mmc_host * host)519 static int mmc_validate_host_caps(struct mmc_host *host)
520 {
521 if (host->caps & MMC_CAP_SDIO_IRQ && !host->ops->enable_sdio_irq) {
522 dev_warn(host->parent, "missing ->enable_sdio_irq() ops\n");
523 return -EINVAL;
524 }
525
526 return 0;
527 }
528
529 /**
530 * mmc_add_host - initialise host hardware
531 * @host: mmc host
532 *
533 * Register the host with the driver model. The host must be
534 * prepared to start servicing requests before this function
535 * completes.
536 */
mmc_add_host(struct mmc_host * host)537 int mmc_add_host(struct mmc_host *host)
538 {
539 int err;
540
541 err = mmc_validate_host_caps(host);
542 if (err)
543 return err;
544
545 err = device_add(&host->class_dev);
546 if (err)
547 return err;
548
549 led_trigger_register_simple(dev_name(&host->class_dev), &host->led);
550
551 #ifdef CONFIG_DEBUG_FS
552 mmc_add_host_debugfs(host);
553 #endif
554
555 mmc_start_host(host);
556 return 0;
557 }
558
559 EXPORT_SYMBOL(mmc_add_host);
560
561 /**
562 * mmc_remove_host - remove host hardware
563 * @host: mmc host
564 *
565 * Unregister and remove all cards associated with this host,
566 * and power down the MMC bus. No new requests will be issued
567 * after this function has returned.
568 */
mmc_remove_host(struct mmc_host * host)569 void mmc_remove_host(struct mmc_host *host)
570 {
571 mmc_stop_host(host);
572
573 #ifdef CONFIG_DEBUG_FS
574 mmc_remove_host_debugfs(host);
575 #endif
576
577 device_del(&host->class_dev);
578
579 led_trigger_unregister_simple(host->led);
580 }
581
582 EXPORT_SYMBOL(mmc_remove_host);
583
584 /**
585 * mmc_free_host - free the host structure
586 * @host: mmc host
587 *
588 * Free the host once all references to it have been dropped.
589 */
mmc_free_host(struct mmc_host * host)590 void mmc_free_host(struct mmc_host *host)
591 {
592 mmc_pwrseq_free(host);
593 put_device(&host->class_dev);
594 }
595
596 EXPORT_SYMBOL(mmc_free_host);
597