0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <linux/device.h>
0013 #include <linux/err.h>
0014 #include <linux/idr.h>
0015 #include <linux/of.h>
0016 #include <linux/of_gpio.h>
0017 #include <linux/pagemap.h>
0018 #include <linux/pm_wakeup.h>
0019 #include <linux/export.h>
0020 #include <linux/leds.h>
0021 #include <linux/slab.h>
0022
0023 #include <linux/mmc/host.h>
0024 #include <linux/mmc/card.h>
0025 #include <linux/mmc/slot-gpio.h>
0026
0027 #include "core.h"
0028 #include "crypto.h"
0029 #include "host.h"
0030 #include "slot-gpio.h"
0031 #include "pwrseq.h"
0032 #include "sdio_ops.h"
0033
0034 #define cls_dev_to_mmc_host(d) container_of(d, struct mmc_host, class_dev)
0035
0036 static DEFINE_IDA(mmc_host_ida);
0037
0038 #ifdef CONFIG_PM_SLEEP
0039 static int mmc_host_class_prepare(struct device *dev)
0040 {
0041 struct mmc_host *host = cls_dev_to_mmc_host(dev);
0042
0043
0044
0045
0046
0047 if (!host->bus_ops)
0048 return 0;
0049
0050
0051 if (host->bus_ops->pre_suspend)
0052 return host->bus_ops->pre_suspend(host);
0053
0054 return 0;
0055 }
0056
0057 static void mmc_host_class_complete(struct device *dev)
0058 {
0059 struct mmc_host *host = cls_dev_to_mmc_host(dev);
0060
0061 _mmc_detect_change(host, 0, false);
0062 }
0063
0064 static const struct dev_pm_ops mmc_host_class_dev_pm_ops = {
0065 .prepare = mmc_host_class_prepare,
0066 .complete = mmc_host_class_complete,
0067 };
0068
0069 #define MMC_HOST_CLASS_DEV_PM_OPS (&mmc_host_class_dev_pm_ops)
0070 #else
0071 #define MMC_HOST_CLASS_DEV_PM_OPS NULL
0072 #endif
0073
0074 static void mmc_host_classdev_release(struct device *dev)
0075 {
0076 struct mmc_host *host = cls_dev_to_mmc_host(dev);
0077 wakeup_source_unregister(host->ws);
0078 if (of_alias_get_id(host->parent->of_node, "mmc") < 0)
0079 ida_simple_remove(&mmc_host_ida, host->index);
0080 kfree(host);
0081 }
0082
0083 static int mmc_host_classdev_shutdown(struct device *dev)
0084 {
0085 struct mmc_host *host = cls_dev_to_mmc_host(dev);
0086
0087 __mmc_stop_host(host);
0088 return 0;
0089 }
0090
0091 static struct class mmc_host_class = {
0092 .name = "mmc_host",
0093 .dev_release = mmc_host_classdev_release,
0094 .shutdown_pre = mmc_host_classdev_shutdown,
0095 .pm = MMC_HOST_CLASS_DEV_PM_OPS,
0096 };
0097
0098 int mmc_register_host_class(void)
0099 {
0100 return class_register(&mmc_host_class);
0101 }
0102
0103 void mmc_unregister_host_class(void)
0104 {
0105 class_unregister(&mmc_host_class);
0106 }
0107
0108
0109
0110
0111
0112 void mmc_retune_enable(struct mmc_host *host)
0113 {
0114 host->can_retune = 1;
0115 if (host->retune_period)
0116 mod_timer(&host->retune_timer,
0117 jiffies + host->retune_period * HZ);
0118 }
0119
0120
0121
0122
0123
0124 void mmc_retune_pause(struct mmc_host *host)
0125 {
0126 if (!host->retune_paused) {
0127 host->retune_paused = 1;
0128 mmc_retune_needed(host);
0129 mmc_retune_hold(host);
0130 }
0131 }
0132 EXPORT_SYMBOL(mmc_retune_pause);
0133
0134 void mmc_retune_unpause(struct mmc_host *host)
0135 {
0136 if (host->retune_paused) {
0137 host->retune_paused = 0;
0138 mmc_retune_release(host);
0139 }
0140 }
0141 EXPORT_SYMBOL(mmc_retune_unpause);
0142
0143
0144
0145
0146
0147
0148
0149 void mmc_retune_disable(struct mmc_host *host)
0150 {
0151 mmc_retune_unpause(host);
0152 host->can_retune = 0;
0153 del_timer_sync(&host->retune_timer);
0154 mmc_retune_clear(host);
0155 }
0156
0157 void mmc_retune_timer_stop(struct mmc_host *host)
0158 {
0159 del_timer_sync(&host->retune_timer);
0160 }
0161 EXPORT_SYMBOL(mmc_retune_timer_stop);
0162
0163 void mmc_retune_hold(struct mmc_host *host)
0164 {
0165 if (!host->hold_retune)
0166 host->retune_now = 1;
0167 host->hold_retune += 1;
0168 }
0169
0170 void mmc_retune_release(struct mmc_host *host)
0171 {
0172 if (host->hold_retune)
0173 host->hold_retune -= 1;
0174 else
0175 WARN_ON(1);
0176 }
0177 EXPORT_SYMBOL(mmc_retune_release);
0178
0179 int mmc_retune(struct mmc_host *host)
0180 {
0181 bool return_to_hs400 = false;
0182 int err;
0183
0184 if (host->retune_now)
0185 host->retune_now = 0;
0186 else
0187 return 0;
0188
0189 if (!host->need_retune || host->doing_retune || !host->card)
0190 return 0;
0191
0192 host->need_retune = 0;
0193
0194 host->doing_retune = 1;
0195
0196 if (host->ios.timing == MMC_TIMING_MMC_HS400) {
0197 err = mmc_hs400_to_hs200(host->card);
0198 if (err)
0199 goto out;
0200
0201 return_to_hs400 = true;
0202 }
0203
0204 err = mmc_execute_tuning(host->card);
0205 if (err)
0206 goto out;
0207
0208 if (return_to_hs400)
0209 err = mmc_hs200_to_hs400(host->card);
0210 out:
0211 host->doing_retune = 0;
0212
0213 return err;
0214 }
0215
0216 static void mmc_retune_timer(struct timer_list *t)
0217 {
0218 struct mmc_host *host = from_timer(host, t, retune_timer);
0219
0220 mmc_retune_needed(host);
0221 }
0222
0223 static void mmc_of_parse_timing_phase(struct device *dev, const char *prop,
0224 struct mmc_clk_phase *phase)
0225 {
0226 int degrees[2] = {0};
0227 int rc;
0228
0229 rc = device_property_read_u32_array(dev, prop, degrees, 2);
0230 phase->valid = !rc;
0231 if (phase->valid) {
0232 phase->in_deg = degrees[0];
0233 phase->out_deg = degrees[1];
0234 }
0235 }
0236
0237 void
0238 mmc_of_parse_clk_phase(struct mmc_host *host, struct mmc_clk_phase_map *map)
0239 {
0240 struct device *dev = host->parent;
0241
0242 mmc_of_parse_timing_phase(dev, "clk-phase-legacy",
0243 &map->phase[MMC_TIMING_LEGACY]);
0244 mmc_of_parse_timing_phase(dev, "clk-phase-mmc-hs",
0245 &map->phase[MMC_TIMING_MMC_HS]);
0246 mmc_of_parse_timing_phase(dev, "clk-phase-sd-hs",
0247 &map->phase[MMC_TIMING_SD_HS]);
0248 mmc_of_parse_timing_phase(dev, "clk-phase-uhs-sdr12",
0249 &map->phase[MMC_TIMING_UHS_SDR12]);
0250 mmc_of_parse_timing_phase(dev, "clk-phase-uhs-sdr25",
0251 &map->phase[MMC_TIMING_UHS_SDR25]);
0252 mmc_of_parse_timing_phase(dev, "clk-phase-uhs-sdr50",
0253 &map->phase[MMC_TIMING_UHS_SDR50]);
0254 mmc_of_parse_timing_phase(dev, "clk-phase-uhs-sdr104",
0255 &map->phase[MMC_TIMING_UHS_SDR104]);
0256 mmc_of_parse_timing_phase(dev, "clk-phase-uhs-ddr50",
0257 &map->phase[MMC_TIMING_UHS_DDR50]);
0258 mmc_of_parse_timing_phase(dev, "clk-phase-mmc-ddr52",
0259 &map->phase[MMC_TIMING_MMC_DDR52]);
0260 mmc_of_parse_timing_phase(dev, "clk-phase-mmc-hs200",
0261 &map->phase[MMC_TIMING_MMC_HS200]);
0262 mmc_of_parse_timing_phase(dev, "clk-phase-mmc-hs400",
0263 &map->phase[MMC_TIMING_MMC_HS400]);
0264 }
0265 EXPORT_SYMBOL(mmc_of_parse_clk_phase);
0266
0267
0268
0269
0270
0271
0272
0273
0274
0275
0276 int mmc_of_parse(struct mmc_host *host)
0277 {
0278 struct device *dev = host->parent;
0279 u32 bus_width, drv_type, cd_debounce_delay_ms;
0280 int ret;
0281
0282 if (!dev || !dev_fwnode(dev))
0283 return 0;
0284
0285
0286 if (device_property_read_u32(dev, "bus-width", &bus_width) < 0) {
0287 dev_dbg(host->parent,
0288 "\"bus-width\" property is missing, assuming 1 bit.\n");
0289 bus_width = 1;
0290 }
0291
0292 switch (bus_width) {
0293 case 8:
0294 host->caps |= MMC_CAP_8_BIT_DATA;
0295 fallthrough;
0296 case 4:
0297 host->caps |= MMC_CAP_4_BIT_DATA;
0298 break;
0299 case 1:
0300 break;
0301 default:
0302 dev_err(host->parent,
0303 "Invalid \"bus-width\" value %u!\n", bus_width);
0304 return -EINVAL;
0305 }
0306
0307
0308 device_property_read_u32(dev, "max-frequency", &host->f_max);
0309
0310
0311
0312
0313
0314
0315
0316
0317
0318
0319
0320
0321
0322
0323
0324 if (device_property_read_bool(dev, "non-removable")) {
0325 host->caps |= MMC_CAP_NONREMOVABLE;
0326 } else {
0327 if (device_property_read_bool(dev, "cd-inverted"))
0328 host->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
0329
0330 if (device_property_read_u32(dev, "cd-debounce-delay-ms",
0331 &cd_debounce_delay_ms))
0332 cd_debounce_delay_ms = 200;
0333
0334 if (device_property_read_bool(dev, "broken-cd"))
0335 host->caps |= MMC_CAP_NEEDS_POLL;
0336
0337 ret = mmc_gpiod_request_cd(host, "cd", 0, false,
0338 cd_debounce_delay_ms * 1000);
0339 if (!ret)
0340 dev_info(host->parent, "Got CD GPIO\n");
0341 else if (ret != -ENOENT && ret != -ENOSYS)
0342 return ret;
0343 }
0344
0345
0346
0347 if (device_property_read_bool(dev, "wp-inverted"))
0348 host->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
0349
0350 ret = mmc_gpiod_request_ro(host, "wp", 0, 0);
0351 if (!ret)
0352 dev_info(host->parent, "Got WP GPIO\n");
0353 else if (ret != -ENOENT && ret != -ENOSYS)
0354 return ret;
0355
0356 if (device_property_read_bool(dev, "disable-wp"))
0357 host->caps2 |= MMC_CAP2_NO_WRITE_PROTECT;
0358
0359 if (device_property_read_bool(dev, "cap-sd-highspeed"))
0360 host->caps |= MMC_CAP_SD_HIGHSPEED;
0361 if (device_property_read_bool(dev, "cap-mmc-highspeed"))
0362 host->caps |= MMC_CAP_MMC_HIGHSPEED;
0363 if (device_property_read_bool(dev, "sd-uhs-sdr12"))
0364 host->caps |= MMC_CAP_UHS_SDR12;
0365 if (device_property_read_bool(dev, "sd-uhs-sdr25"))
0366 host->caps |= MMC_CAP_UHS_SDR25;
0367 if (device_property_read_bool(dev, "sd-uhs-sdr50"))
0368 host->caps |= MMC_CAP_UHS_SDR50;
0369 if (device_property_read_bool(dev, "sd-uhs-sdr104"))
0370 host->caps |= MMC_CAP_UHS_SDR104;
0371 if (device_property_read_bool(dev, "sd-uhs-ddr50"))
0372 host->caps |= MMC_CAP_UHS_DDR50;
0373 if (device_property_read_bool(dev, "cap-power-off-card"))
0374 host->caps |= MMC_CAP_POWER_OFF_CARD;
0375 if (device_property_read_bool(dev, "cap-mmc-hw-reset"))
0376 host->caps |= MMC_CAP_HW_RESET;
0377 if (device_property_read_bool(dev, "cap-sdio-irq"))
0378 host->caps |= MMC_CAP_SDIO_IRQ;
0379 if (device_property_read_bool(dev, "full-pwr-cycle"))
0380 host->caps2 |= MMC_CAP2_FULL_PWR_CYCLE;
0381 if (device_property_read_bool(dev, "full-pwr-cycle-in-suspend"))
0382 host->caps2 |= MMC_CAP2_FULL_PWR_CYCLE_IN_SUSPEND;
0383 if (device_property_read_bool(dev, "keep-power-in-suspend"))
0384 host->pm_caps |= MMC_PM_KEEP_POWER;
0385 if (device_property_read_bool(dev, "wakeup-source") ||
0386 device_property_read_bool(dev, "enable-sdio-wakeup"))
0387 host->pm_caps |= MMC_PM_WAKE_SDIO_IRQ;
0388 if (device_property_read_bool(dev, "mmc-ddr-3_3v"))
0389 host->caps |= MMC_CAP_3_3V_DDR;
0390 if (device_property_read_bool(dev, "mmc-ddr-1_8v"))
0391 host->caps |= MMC_CAP_1_8V_DDR;
0392 if (device_property_read_bool(dev, "mmc-ddr-1_2v"))
0393 host->caps |= MMC_CAP_1_2V_DDR;
0394 if (device_property_read_bool(dev, "mmc-hs200-1_8v"))
0395 host->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
0396 if (device_property_read_bool(dev, "mmc-hs200-1_2v"))
0397 host->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
0398 if (device_property_read_bool(dev, "mmc-hs400-1_8v"))
0399 host->caps2 |= MMC_CAP2_HS400_1_8V | MMC_CAP2_HS200_1_8V_SDR;
0400 if (device_property_read_bool(dev, "mmc-hs400-1_2v"))
0401 host->caps2 |= MMC_CAP2_HS400_1_2V | MMC_CAP2_HS200_1_2V_SDR;
0402 if (device_property_read_bool(dev, "mmc-hs400-enhanced-strobe"))
0403 host->caps2 |= MMC_CAP2_HS400_ES;
0404 if (device_property_read_bool(dev, "no-sdio"))
0405 host->caps2 |= MMC_CAP2_NO_SDIO;
0406 if (device_property_read_bool(dev, "no-sd"))
0407 host->caps2 |= MMC_CAP2_NO_SD;
0408 if (device_property_read_bool(dev, "no-mmc"))
0409 host->caps2 |= MMC_CAP2_NO_MMC;
0410 if (device_property_read_bool(dev, "no-mmc-hs400"))
0411 host->caps2 &= ~(MMC_CAP2_HS400_1_8V | MMC_CAP2_HS400_1_2V |
0412 MMC_CAP2_HS400_ES);
0413
0414
0415 if (device_property_read_u32(dev, "fixed-emmc-driver-type", &drv_type) == 0) {
0416 if (host->caps & MMC_CAP_NONREMOVABLE)
0417 host->fixed_drv_type = drv_type;
0418 else
0419 dev_err(host->parent,
0420 "can't use fixed driver type, media is removable\n");
0421 }
0422
0423 host->dsr_req = !device_property_read_u32(dev, "dsr", &host->dsr);
0424 if (host->dsr_req && (host->dsr & ~0xffff)) {
0425 dev_err(host->parent,
0426 "device tree specified broken value for DSR: 0x%x, ignoring\n",
0427 host->dsr);
0428 host->dsr_req = 0;
0429 }
0430
0431 device_property_read_u32(dev, "post-power-on-delay-ms",
0432 &host->ios.power_delay_ms);
0433
0434 return mmc_pwrseq_alloc(host);
0435 }
0436
0437 EXPORT_SYMBOL(mmc_of_parse);
0438
0439
0440
0441
0442
0443
0444
0445
0446
0447
0448 int mmc_of_parse_voltage(struct mmc_host *host, u32 *mask)
0449 {
0450 const char *prop = "voltage-ranges";
0451 struct device *dev = host->parent;
0452 u32 *voltage_ranges;
0453 int num_ranges, i;
0454 int ret;
0455
0456 if (!device_property_present(dev, prop)) {
0457 dev_dbg(dev, "%s unspecified\n", prop);
0458 return 0;
0459 }
0460
0461 ret = device_property_count_u32(dev, prop);
0462 if (ret < 0)
0463 return ret;
0464
0465 num_ranges = ret / 2;
0466 if (!num_ranges) {
0467 dev_err(dev, "%s empty\n", prop);
0468 return -EINVAL;
0469 }
0470
0471 voltage_ranges = kcalloc(2 * num_ranges, sizeof(*voltage_ranges), GFP_KERNEL);
0472 if (!voltage_ranges)
0473 return -ENOMEM;
0474
0475 ret = device_property_read_u32_array(dev, prop, voltage_ranges, 2 * num_ranges);
0476 if (ret) {
0477 kfree(voltage_ranges);
0478 return ret;
0479 }
0480
0481 for (i = 0; i < num_ranges; i++) {
0482 const int j = i * 2;
0483 u32 ocr_mask;
0484
0485 ocr_mask = mmc_vddrange_to_ocrmask(voltage_ranges[j + 0],
0486 voltage_ranges[j + 1]);
0487 if (!ocr_mask) {
0488 dev_err(dev, "range #%d in %s is invalid\n", i, prop);
0489 kfree(voltage_ranges);
0490 return -EINVAL;
0491 }
0492 *mask |= ocr_mask;
0493 }
0494
0495 kfree(voltage_ranges);
0496
0497 return 1;
0498 }
0499 EXPORT_SYMBOL(mmc_of_parse_voltage);
0500
0501
0502
0503
0504 static int mmc_first_nonreserved_index(void)
0505 {
0506 int max;
0507
0508 max = of_alias_get_highest_id("mmc");
0509 if (max < 0)
0510 return 0;
0511
0512 return max + 1;
0513 }
0514
0515
0516
0517
0518
0519
0520
0521
0522 struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
0523 {
0524 int index;
0525 struct mmc_host *host;
0526 int alias_id, min_idx, max_idx;
0527
0528 host = kzalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL);
0529 if (!host)
0530 return NULL;
0531
0532
0533 host->rescan_disable = 1;
0534
0535 alias_id = of_alias_get_id(dev->of_node, "mmc");
0536 if (alias_id >= 0) {
0537 index = alias_id;
0538 } else {
0539 min_idx = mmc_first_nonreserved_index();
0540 max_idx = 0;
0541
0542 index = ida_simple_get(&mmc_host_ida, min_idx, max_idx, GFP_KERNEL);
0543 if (index < 0) {
0544 kfree(host);
0545 return NULL;
0546 }
0547 }
0548
0549 host->index = index;
0550
0551 dev_set_name(&host->class_dev, "mmc%d", host->index);
0552 host->ws = wakeup_source_register(NULL, dev_name(&host->class_dev));
0553
0554 host->parent = dev;
0555 host->class_dev.parent = dev;
0556 host->class_dev.class = &mmc_host_class;
0557 device_initialize(&host->class_dev);
0558 device_enable_async_suspend(&host->class_dev);
0559
0560 if (mmc_gpio_alloc(host)) {
0561 put_device(&host->class_dev);
0562 return NULL;
0563 }
0564
0565 spin_lock_init(&host->lock);
0566 init_waitqueue_head(&host->wq);
0567 INIT_DELAYED_WORK(&host->detect, mmc_rescan);
0568 INIT_DELAYED_WORK(&host->sdio_irq_work, sdio_irq_work);
0569 timer_setup(&host->retune_timer, mmc_retune_timer, 0);
0570
0571
0572
0573
0574
0575 host->max_segs = 1;
0576 host->max_seg_size = PAGE_SIZE;
0577
0578 host->max_req_size = PAGE_SIZE;
0579 host->max_blk_size = 512;
0580 host->max_blk_count = PAGE_SIZE / 512;
0581
0582 host->fixed_drv_type = -EINVAL;
0583 host->ios.power_delay_ms = 10;
0584 host->ios.power_mode = MMC_POWER_UNDEFINED;
0585
0586 return host;
0587 }
0588
0589 EXPORT_SYMBOL(mmc_alloc_host);
0590
0591 static int mmc_validate_host_caps(struct mmc_host *host)
0592 {
0593 struct device *dev = host->parent;
0594 u32 caps = host->caps, caps2 = host->caps2;
0595
0596 if (caps & MMC_CAP_SDIO_IRQ && !host->ops->enable_sdio_irq) {
0597 dev_warn(dev, "missing ->enable_sdio_irq() ops\n");
0598 return -EINVAL;
0599 }
0600
0601 if (caps2 & (MMC_CAP2_HS400_ES | MMC_CAP2_HS400) &&
0602 !(caps & MMC_CAP_8_BIT_DATA) && !(caps2 & MMC_CAP2_NO_MMC)) {
0603 dev_warn(dev, "drop HS400 support since no 8-bit bus\n");
0604 host->caps2 = caps2 & ~MMC_CAP2_HS400_ES & ~MMC_CAP2_HS400;
0605 }
0606
0607 return 0;
0608 }
0609
0610
0611
0612
0613
0614
0615
0616
0617
0618 int mmc_add_host(struct mmc_host *host)
0619 {
0620 int err;
0621
0622 err = mmc_validate_host_caps(host);
0623 if (err)
0624 return err;
0625
0626 err = device_add(&host->class_dev);
0627 if (err)
0628 return err;
0629
0630 led_trigger_register_simple(dev_name(&host->class_dev), &host->led);
0631
0632 #ifdef CONFIG_DEBUG_FS
0633 mmc_add_host_debugfs(host);
0634 #endif
0635
0636 mmc_start_host(host);
0637 return 0;
0638 }
0639
0640 EXPORT_SYMBOL(mmc_add_host);
0641
0642
0643
0644
0645
0646
0647
0648
0649
0650 void mmc_remove_host(struct mmc_host *host)
0651 {
0652 mmc_stop_host(host);
0653
0654 #ifdef CONFIG_DEBUG_FS
0655 mmc_remove_host_debugfs(host);
0656 #endif
0657
0658 device_del(&host->class_dev);
0659
0660 led_trigger_unregister_simple(host->led);
0661 }
0662
0663 EXPORT_SYMBOL(mmc_remove_host);
0664
0665
0666
0667
0668
0669
0670
0671 void mmc_free_host(struct mmc_host *host)
0672 {
0673 mmc_pwrseq_free(host);
0674 put_device(&host->class_dev);
0675 }
0676
0677 EXPORT_SYMBOL(mmc_free_host);