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/pagemap.h>
17 #include <linux/export.h>
18 #include <linux/leds.h>
19 #include <linux/slab.h>
20
21 #include <linux/mmc/host.h>
22 #include <linux/mmc/card.h>
23 #include <linux/mmc/slot-gpio.h>
24
25 #include "core.h"
26 #include "crypto.h"
27 #include "host.h"
28 #include "slot-gpio.h"
29 #include "pwrseq.h"
30 #include "sdio_ops.h"
31
32 #define cls_dev_to_mmc_host(d) container_of(d, struct mmc_host, class_dev)
33
34 static DEFINE_IDA(mmc_host_ida);
35
36 #ifdef CONFIG_PM_SLEEP
mmc_host_class_prepare(struct device * dev)37 static int mmc_host_class_prepare(struct device *dev)
38 {
39 struct mmc_host *host = cls_dev_to_mmc_host(dev);
40
41 /*
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.
44 */
45 if (!host->bus_ops)
46 return 0;
47
48 /* Validate conditions for system suspend. */
49 if (host->bus_ops->pre_suspend)
50 return host->bus_ops->pre_suspend(host);
51
52 return 0;
53 }
54
mmc_host_class_complete(struct device * dev)55 static void mmc_host_class_complete(struct device *dev)
56 {
57 struct mmc_host *host = cls_dev_to_mmc_host(dev);
58
59 _mmc_detect_change(host, 0, false);
60 }
61
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,
65 };
66
67 #define MMC_HOST_CLASS_DEV_PM_OPS (&mmc_host_class_dev_pm_ops)
68 #else
69 #define MMC_HOST_CLASS_DEV_PM_OPS NULL
70 #endif
71
mmc_host_classdev_release(struct device * dev)72 static void mmc_host_classdev_release(struct device *dev)
73 {
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);
78 kfree(host);
79 }
80
mmc_host_classdev_shutdown(struct device * dev)81 static int mmc_host_classdev_shutdown(struct device *dev)
82 {
83 struct mmc_host *host = cls_dev_to_mmc_host(dev);
84
85 __mmc_stop_host(host);
86 return 0;
87 }
88
89 static const struct class mmc_host_class = {
90 .name = "mmc_host",
91 .dev_release = mmc_host_classdev_release,
92 .shutdown_pre = mmc_host_classdev_shutdown,
93 .pm = MMC_HOST_CLASS_DEV_PM_OPS,
94 };
95
mmc_register_host_class(void)96 int mmc_register_host_class(void)
97 {
98 return class_register(&mmc_host_class);
99 }
100
mmc_unregister_host_class(void)101 void mmc_unregister_host_class(void)
102 {
103 class_unregister(&mmc_host_class);
104 }
105
106 /**
107 * mmc_retune_enable() - enter a transfer mode that requires retuning
108 * @host: host which should retune now
109 */
mmc_retune_enable(struct mmc_host * host)110 void mmc_retune_enable(struct mmc_host *host)
111 {
112 host->can_retune = 1;
113 if (host->retune_period)
114 mod_timer(&host->retune_timer,
115 jiffies + host->retune_period * HZ);
116 }
117
118 /*
119 * Pause re-tuning for a small set of operations. The pause begins after the
120 * next command.
121 */
mmc_retune_pause(struct mmc_host * host)122 void mmc_retune_pause(struct mmc_host *host)
123 {
124 if (!host->retune_paused) {
125 host->retune_paused = 1;
126 mmc_retune_hold(host);
127 }
128 }
129 EXPORT_SYMBOL(mmc_retune_pause);
130
mmc_retune_unpause(struct mmc_host * host)131 void mmc_retune_unpause(struct mmc_host *host)
132 {
133 if (host->retune_paused) {
134 host->retune_paused = 0;
135 mmc_retune_release(host);
136 }
137 }
138 EXPORT_SYMBOL(mmc_retune_unpause);
139
140 /**
141 * mmc_retune_disable() - exit a transfer mode that requires retuning
142 * @host: host which should not retune anymore
143 *
144 * It is not meant for temporarily preventing retuning!
145 */
mmc_retune_disable(struct mmc_host * host)146 void mmc_retune_disable(struct mmc_host *host)
147 {
148 mmc_retune_unpause(host);
149 host->can_retune = 0;
150 del_timer_sync(&host->retune_timer);
151 mmc_retune_clear(host);
152 }
153
mmc_retune_timer_stop(struct mmc_host * host)154 void mmc_retune_timer_stop(struct mmc_host *host)
155 {
156 del_timer_sync(&host->retune_timer);
157 }
158 EXPORT_SYMBOL(mmc_retune_timer_stop);
159
mmc_retune_hold(struct mmc_host * host)160 void mmc_retune_hold(struct mmc_host *host)
161 {
162 if (!host->hold_retune)
163 host->retune_now = 1;
164 host->hold_retune += 1;
165 }
166
mmc_retune_release(struct mmc_host * host)167 void mmc_retune_release(struct mmc_host *host)
168 {
169 if (host->hold_retune)
170 host->hold_retune -= 1;
171 else
172 WARN_ON(1);
173 }
174 EXPORT_SYMBOL(mmc_retune_release);
175
mmc_retune(struct mmc_host * host)176 int mmc_retune(struct mmc_host *host)
177 {
178 bool return_to_hs400 = false;
179 int err;
180
181 if (host->retune_now)
182 host->retune_now = 0;
183 else
184 return 0;
185
186 if (!host->need_retune || host->doing_retune || !host->card)
187 return 0;
188
189 host->need_retune = 0;
190
191 host->doing_retune = 1;
192
193 if (host->ios.timing == MMC_TIMING_MMC_HS400) {
194 err = mmc_hs400_to_hs200(host->card);
195 if (err)
196 goto out;
197
198 return_to_hs400 = true;
199 }
200
201 err = mmc_execute_tuning(host->card);
202 if (err)
203 goto out;
204
205 if (return_to_hs400)
206 err = mmc_hs200_to_hs400(host->card);
207 out:
208 host->doing_retune = 0;
209
210 return err;
211 }
212
mmc_retune_timer(struct timer_list * t)213 static void mmc_retune_timer(struct timer_list *t)
214 {
215 struct mmc_host *host = from_timer(host, t, retune_timer);
216
217 mmc_retune_needed(host);
218 }
219
mmc_of_parse_timing_phase(struct device * dev,const char * prop,struct mmc_clk_phase * phase)220 static void mmc_of_parse_timing_phase(struct device *dev, const char *prop,
221 struct mmc_clk_phase *phase)
222 {
223 int degrees[2] = {0};
224 int rc;
225
226 rc = device_property_read_u32_array(dev, prop, degrees, 2);
227 phase->valid = !rc;
228 if (phase->valid) {
229 phase->in_deg = degrees[0];
230 phase->out_deg = degrees[1];
231 }
232 }
233
234 void
mmc_of_parse_clk_phase(struct device * dev,struct mmc_clk_phase_map * map)235 mmc_of_parse_clk_phase(struct device *dev, struct mmc_clk_phase_map *map)
236 {
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]);
259 }
260 EXPORT_SYMBOL(mmc_of_parse_clk_phase);
261
262 /**
263 * mmc_of_parse() - parse host's device properties
264 * @host: host whose properties should be parsed.
265 *
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
269 * parameters.
270 */
mmc_of_parse(struct mmc_host * host)271 int mmc_of_parse(struct mmc_host *host)
272 {
273 struct device *dev = host->parent;
274 u32 bus_width, drv_type, cd_debounce_delay_ms;
275 int ret;
276
277 if (!dev || !dev_fwnode(dev))
278 return 0;
279
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");
284 bus_width = 1;
285 }
286
287 switch (bus_width) {
288 case 8:
289 host->caps |= MMC_CAP_8_BIT_DATA;
290 fallthrough; /* Hosts capable of 8-bit can also do 4 bits */
291 case 4:
292 host->caps |= MMC_CAP_4_BIT_DATA;
293 break;
294 case 1:
295 break;
296 default:
297 dev_err(host->parent,
298 "Invalid \"bus-width\" value %u!\n", bus_width);
299 return -EINVAL;
300 }
301
302 /* f_max is obtained from the optional "max-frequency" property */
303 device_property_read_u32(dev, "max-frequency", &host->f_max);
304
305 /*
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.
315 */
316
317 /* Parse Card Detection */
318
319 if (device_property_read_bool(dev, "non-removable")) {
320 host->caps |= MMC_CAP_NONREMOVABLE;
321 } else {
322 if (device_property_read_bool(dev, "cd-inverted"))
323 host->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
324
325 if (device_property_read_u32(dev, "cd-debounce-delay-ms",
326 &cd_debounce_delay_ms))
327 cd_debounce_delay_ms = 200;
328
329 if (device_property_read_bool(dev, "broken-cd"))
330 host->caps |= MMC_CAP_NEEDS_POLL;
331
332 ret = mmc_gpiod_request_cd(host, "cd", 0, false,
333 cd_debounce_delay_ms * 1000);
334 if (!ret)
335 dev_info(host->parent, "Got CD GPIO\n");
336 else if (ret != -ENOENT && ret != -ENOSYS)
337 return ret;
338 }
339
340 /* Parse Write Protection */
341
342 if (device_property_read_bool(dev, "wp-inverted"))
343 host->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
344
345 ret = mmc_gpiod_request_ro(host, "wp", 0, 0);
346 if (!ret)
347 dev_info(host->parent, "Got WP GPIO\n");
348 else if (ret != -ENOENT && ret != -ENOSYS)
349 return ret;
350
351 if (device_property_read_bool(dev, "disable-wp"))
352 host->caps2 |= MMC_CAP2_NO_WRITE_PROTECT;
353
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 |
407 MMC_CAP2_HS400_ES);
408
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;
413 else
414 dev_err(host->parent,
415 "can't use fixed driver type, media is removable\n");
416 }
417
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",
422 host->dsr);
423 host->dsr_req = 0;
424 }
425
426 device_property_read_u32(dev, "post-power-on-delay-ms",
427 &host->ios.power_delay_ms);
428
429 return mmc_pwrseq_alloc(host);
430 }
431
432 EXPORT_SYMBOL(mmc_of_parse);
433
434 /**
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
438 *
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.
442 */
mmc_of_parse_voltage(struct mmc_host * host,u32 * mask)443 int mmc_of_parse_voltage(struct mmc_host *host, u32 *mask)
444 {
445 const char *prop = "voltage-ranges";
446 struct device *dev = host->parent;
447 u32 *voltage_ranges;
448 int num_ranges, i;
449 int ret;
450
451 if (!device_property_present(dev, prop)) {
452 dev_dbg(dev, "%s unspecified\n", prop);
453 return 0;
454 }
455
456 ret = device_property_count_u32(dev, prop);
457 if (ret < 0)
458 return ret;
459
460 num_ranges = ret / 2;
461 if (!num_ranges) {
462 dev_err(dev, "%s empty\n", prop);
463 return -EINVAL;
464 }
465
466 voltage_ranges = kcalloc(2 * num_ranges, sizeof(*voltage_ranges), GFP_KERNEL);
467 if (!voltage_ranges)
468 return -ENOMEM;
469
470 ret = device_property_read_u32_array(dev, prop, voltage_ranges, 2 * num_ranges);
471 if (ret) {
472 kfree(voltage_ranges);
473 return ret;
474 }
475
476 for (i = 0; i < num_ranges; i++) {
477 const int j = i * 2;
478 u32 ocr_mask;
479
480 ocr_mask = mmc_vddrange_to_ocrmask(voltage_ranges[j + 0],
481 voltage_ranges[j + 1]);
482 if (!ocr_mask) {
483 dev_err(dev, "range #%d in %s is invalid\n", i, prop);
484 kfree(voltage_ranges);
485 return -EINVAL;
486 }
487 *mask |= ocr_mask;
488 }
489
490 kfree(voltage_ranges);
491
492 return 1;
493 }
494 EXPORT_SYMBOL(mmc_of_parse_voltage);
495
496 /**
497 * mmc_first_nonreserved_index() - get the first index that is not reserved
498 */
mmc_first_nonreserved_index(void)499 static int mmc_first_nonreserved_index(void)
500 {
501 int max;
502
503 max = of_alias_get_highest_id("mmc");
504 if (max < 0)
505 return 0;
506
507 return max + 1;
508 }
509
510 /**
511 * mmc_alloc_host - initialise the per-host structure.
512 * @extra: sizeof private data structure
513 * @dev: pointer to host device model structure
514 *
515 * Initialise the per-host structure.
516 */
mmc_alloc_host(int extra,struct device * dev)517 struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
518 {
519 int index;
520 struct mmc_host *host;
521 int alias_id, min_idx, max_idx;
522
523 host = kzalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL);
524 if (!host)
525 return NULL;
526
527 /* scanning will be enabled when we're ready */
528 host->rescan_disable = 1;
529
530 alias_id = of_alias_get_id(dev->of_node, "mmc");
531 if (alias_id >= 0) {
532 index = alias_id;
533 } else {
534 min_idx = mmc_first_nonreserved_index();
535 max_idx = 0;
536
537 index = ida_alloc_range(&mmc_host_ida, min_idx, max_idx - 1,
538 GFP_KERNEL);
539 if (index < 0) {
540 kfree(host);
541 return NULL;
542 }
543 }
544
545 host->index = index;
546
547 dev_set_name(&host->class_dev, "mmc%d", host->index);
548 host->ws = wakeup_source_register(NULL, dev_name(&host->class_dev));
549
550 host->parent = 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);
555
556 if (mmc_gpio_alloc(host)) {
557 put_device(&host->class_dev);
558 return NULL;
559 }
560
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);
566
567 /*
568 * By default, hosts do not support SGIO or large requests.
569 * They have to set these according to their abilities.
570 */
571 host->max_segs = 1;
572 host->max_seg_size = PAGE_SIZE;
573
574 host->max_req_size = PAGE_SIZE;
575 host->max_blk_size = 512;
576 host->max_blk_count = PAGE_SIZE / 512;
577
578 host->fixed_drv_type = -EINVAL;
579 host->ios.power_delay_ms = 10;
580 host->ios.power_mode = MMC_POWER_UNDEFINED;
581
582 return host;
583 }
584
585 EXPORT_SYMBOL(mmc_alloc_host);
586
devm_mmc_host_release(struct device * dev,void * res)587 static void devm_mmc_host_release(struct device *dev, void *res)
588 {
589 mmc_free_host(*(struct mmc_host **)res);
590 }
591
devm_mmc_alloc_host(struct device * dev,int extra)592 struct mmc_host *devm_mmc_alloc_host(struct device *dev, int extra)
593 {
594 struct mmc_host **dr, *host;
595
596 dr = devres_alloc(devm_mmc_host_release, sizeof(*dr), GFP_KERNEL);
597 if (!dr)
598 return NULL;
599
600 host = mmc_alloc_host(extra, dev);
601 if (!host) {
602 devres_free(dr);
603 return NULL;
604 }
605
606 *dr = host;
607 devres_add(dev, dr);
608
609 return host;
610 }
611 EXPORT_SYMBOL(devm_mmc_alloc_host);
612
mmc_validate_host_caps(struct mmc_host * host)613 static int mmc_validate_host_caps(struct mmc_host *host)
614 {
615 struct device *dev = host->parent;
616 u32 caps = host->caps, caps2 = host->caps2;
617
618 if (caps & MMC_CAP_SDIO_IRQ && !host->ops->enable_sdio_irq) {
619 dev_warn(dev, "missing ->enable_sdio_irq() ops\n");
620 return -EINVAL;
621 }
622
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;
627 }
628
629 return 0;
630 }
631
632 /**
633 * mmc_add_host - initialise host hardware
634 * @host: mmc host
635 *
636 * Register the host with the driver model. The host must be
637 * prepared to start servicing requests before this function
638 * completes.
639 */
mmc_add_host(struct mmc_host * host)640 int mmc_add_host(struct mmc_host *host)
641 {
642 int err;
643
644 err = mmc_validate_host_caps(host);
645 if (err)
646 return err;
647
648 err = device_add(&host->class_dev);
649 if (err)
650 return err;
651
652 led_trigger_register_simple(dev_name(&host->class_dev), &host->led);
653
654 mmc_add_host_debugfs(host);
655
656 mmc_start_host(host);
657 return 0;
658 }
659
660 EXPORT_SYMBOL(mmc_add_host);
661
662 /**
663 * mmc_remove_host - remove host hardware
664 * @host: mmc host
665 *
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.
669 */
mmc_remove_host(struct mmc_host * host)670 void mmc_remove_host(struct mmc_host *host)
671 {
672 mmc_stop_host(host);
673
674 mmc_remove_host_debugfs(host);
675
676 device_del(&host->class_dev);
677
678 led_trigger_unregister_simple(host->led);
679 }
680
681 EXPORT_SYMBOL(mmc_remove_host);
682
683 /**
684 * mmc_free_host - free the host structure
685 * @host: mmc host
686 *
687 * Free the host once all references to it have been dropped.
688 */
mmc_free_host(struct mmc_host * host)689 void mmc_free_host(struct mmc_host *host)
690 {
691 cancel_delayed_work_sync(&host->detect);
692 mmc_pwrseq_free(host);
693 put_device(&host->class_dev);
694 }
695
696 EXPORT_SYMBOL(mmc_free_host);
697