Lines Matching +full:int +full:- +full:clock +full:- +full:stable +full:- +full:broken
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * linux/drivers/mmc/host/sdhci.c - Secure Digital Host Controller Interface driver
5 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
9 * - JMicron (hardware and technical support)
19 #include <linux/dma-mapping.h>
33 #include <linux/mmc/slot-gpio.h>
40 pr_debug("%s: " DRIVER_NAME ": " f, mmc_hostname(host->mmc), ## x)
43 pr_err("%s: " DRIVER_NAME ": " f, mmc_hostname(host->mmc), ## x)
47 static unsigned int debug_quirks = 0;
48 static unsigned int debug_quirks2;
73 SDHCI_DUMP("Wake-up: 0x%08x | Clock: 0x%08x\n", in sdhci_dumpregs()
76 SDHCI_DUMP("Timeout: 0x%08x | Int stat: 0x%08x\n", in sdhci_dumpregs()
79 SDHCI_DUMP("Int enab: 0x%08x | Sig enab: 0x%08x\n", in sdhci_dumpregs()
82 SDHCI_DUMP("ACmd stat: 0x%08x | Slot int: 0x%08x\n", in sdhci_dumpregs()
100 if (host->flags & SDHCI_USE_ADMA) { in sdhci_dumpregs()
101 if (host->flags & SDHCI_USE_64_BIT_DMA) { in sdhci_dumpregs()
113 if (host->ops->dump_vendor_regs) in sdhci_dumpregs()
114 host->ops->dump_vendor_regs(host); in sdhci_dumpregs()
144 host->v4_mode = true; in sdhci_enable_v4_mode()
151 return cmd->data || cmd->flags & MMC_RSP_BUSY; in sdhci_data_line_cmd()
158 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) || in sdhci_set_card_detection()
159 !mmc_card_is_removable(host->mmc) || mmc_can_gpio_cd(host->mmc)) in sdhci_set_card_detection()
166 host->ier |= present ? SDHCI_INT_CARD_REMOVE : in sdhci_set_card_detection()
169 host->ier &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT); in sdhci_set_card_detection()
172 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE); in sdhci_set_card_detection()
173 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE); in sdhci_set_card_detection()
188 if (host->bus_on) in sdhci_runtime_pm_bus_on()
190 host->bus_on = true; in sdhci_runtime_pm_bus_on()
191 pm_runtime_get_noresume(mmc_dev(host->mmc)); in sdhci_runtime_pm_bus_on()
196 if (!host->bus_on) in sdhci_runtime_pm_bus_off()
198 host->bus_on = false; in sdhci_runtime_pm_bus_off()
199 pm_runtime_put_noidle(mmc_dev(host->mmc)); in sdhci_runtime_pm_bus_off()
209 host->clock = 0; in sdhci_reset()
210 /* Reset-all turns off SD Bus Power */ in sdhci_reset()
211 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON) in sdhci_reset()
226 mmc_hostname(host->mmc), (int)mask); in sdhci_reset()
238 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) { in sdhci_do_reset()
239 struct mmc_host *mmc = host->mmc; in sdhci_do_reset()
241 if (!mmc->ops->get_cd(mmc)) in sdhci_do_reset()
245 host->ops->reset(host, mask); in sdhci_do_reset()
253 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) { in sdhci_reset_for_all()
254 if (host->ops->enable_dma) in sdhci_reset_for_all()
255 host->ops->enable_dma(host); in sdhci_reset_for_all()
258 host->preset_enabled = false; in sdhci_reset_for_all()
273 if (host->quirks2 & SDHCI_QUIRK2_ISSUE_CMD_DAT_RESET_TOGETHER) { in sdhci_reset_for_reason()
299 host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT | in sdhci_set_default_irqs()
305 if (host->tuning_mode == SDHCI_TUNING_MODE_2 || in sdhci_set_default_irqs()
306 host->tuning_mode == SDHCI_TUNING_MODE_3) in sdhci_set_default_irqs()
307 host->ier |= SDHCI_INT_RETUNE; in sdhci_set_default_irqs()
309 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE); in sdhci_set_default_irqs()
310 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE); in sdhci_set_default_irqs()
318 if (host->version < SDHCI_SPEC_200) in sdhci_config_dma()
329 if (!(host->flags & SDHCI_REQ_USE_DMA)) in sdhci_config_dma()
333 if (host->flags & SDHCI_USE_ADMA) in sdhci_config_dma()
336 if (host->flags & SDHCI_USE_64_BIT_DMA) { in sdhci_config_dma()
338 * If v4 mode, all supported DMA can be 64-bit addressing if in sdhci_config_dma()
339 * controller supports 64-bit system address, otherwise only in sdhci_config_dma()
340 * ADMA can support 64-bit addressing. in sdhci_config_dma()
342 if (host->v4_mode) { in sdhci_config_dma()
346 } else if (host->flags & SDHCI_USE_ADMA) { in sdhci_config_dma()
359 static void sdhci_init(struct sdhci_host *host, int soft) in sdhci_init()
361 struct mmc_host *mmc = host->mmc; in sdhci_init()
369 if (host->v4_mode) in sdhci_init()
372 spin_lock_irqsave(&host->lock, flags); in sdhci_init()
374 spin_unlock_irqrestore(&host->lock, flags); in sdhci_init()
376 host->cqe_on = false; in sdhci_init()
379 /* force clock reconfiguration */ in sdhci_init()
380 host->clock = 0; in sdhci_init()
381 host->reinit_uhs = true; in sdhci_init()
382 mmc->ops->set_ios(mmc, &mmc->ios); in sdhci_init()
388 u32 cd = host->ier & (SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT); in sdhci_reinit()
399 if (cd != (host->ier & (SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT))) in sdhci_reinit()
400 mmc_detect_change(host->mmc, msecs_to_jiffies(200)); in sdhci_reinit()
407 if (host->quirks & SDHCI_QUIRK_NO_LED) in __sdhci_led_activate()
419 if (host->quirks & SDHCI_QUIRK_NO_LED) in __sdhci_led_deactivate()
434 spin_lock_irqsave(&host->lock, flags); in sdhci_led_control()
436 if (host->runtime_suspended) in sdhci_led_control()
444 spin_unlock_irqrestore(&host->lock, flags); in sdhci_led_control()
447 static int sdhci_led_register(struct sdhci_host *host) in sdhci_led_register()
449 struct mmc_host *mmc = host->mmc; in sdhci_led_register()
451 if (host->quirks & SDHCI_QUIRK_NO_LED) in sdhci_led_register()
454 snprintf(host->led_name, sizeof(host->led_name), in sdhci_led_register()
457 host->led.name = host->led_name; in sdhci_led_register()
458 host->led.brightness = LED_OFF; in sdhci_led_register()
459 host->led.default_trigger = mmc_hostname(mmc); in sdhci_led_register()
460 host->led.brightness_set = sdhci_led_control; in sdhci_led_register()
462 return led_classdev_register(mmc_dev(mmc), &host->led); in sdhci_led_register()
467 if (host->quirks & SDHCI_QUIRK_NO_LED) in sdhci_led_unregister()
470 led_classdev_unregister(&host->led); in sdhci_led_unregister()
483 static inline int sdhci_led_register(struct sdhci_host *host) in sdhci_led_register()
507 if (sdhci_data_line_cmd(mrq->cmd)) in sdhci_mod_timer()
508 mod_timer(&host->data_timer, timeout); in sdhci_mod_timer()
510 mod_timer(&host->timer, timeout); in sdhci_mod_timer()
515 if (sdhci_data_line_cmd(mrq->cmd)) in sdhci_del_timer()
516 del_timer(&host->data_timer); in sdhci_del_timer()
518 del_timer(&host->timer); in sdhci_del_timer()
523 return host->cmd || host->data_cmd; in sdhci_has_requests()
540 blksize = host->data->blksz; in sdhci_read_block_pio()
544 BUG_ON(!sg_miter_next(&host->sg_miter)); in sdhci_read_block_pio()
546 len = min(host->sg_miter.length, blksize); in sdhci_read_block_pio()
548 blksize -= len; in sdhci_read_block_pio()
549 host->sg_miter.consumed = len; in sdhci_read_block_pio()
551 buf = host->sg_miter.addr; in sdhci_read_block_pio()
563 chunk--; in sdhci_read_block_pio()
564 len--; in sdhci_read_block_pio()
568 sg_miter_stop(&host->sg_miter); in sdhci_read_block_pio()
579 blksize = host->data->blksz; in sdhci_write_block_pio()
584 BUG_ON(!sg_miter_next(&host->sg_miter)); in sdhci_write_block_pio()
586 len = min(host->sg_miter.length, blksize); in sdhci_write_block_pio()
588 blksize -= len; in sdhci_write_block_pio()
589 host->sg_miter.consumed = len; in sdhci_write_block_pio()
591 buf = host->sg_miter.addr; in sdhci_write_block_pio()
598 len--; in sdhci_write_block_pio()
608 sg_miter_stop(&host->sg_miter); in sdhci_write_block_pio()
615 if (host->blocks == 0) in sdhci_transfer_pio()
618 if (host->data->flags & MMC_DATA_READ) in sdhci_transfer_pio()
628 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) && in sdhci_transfer_pio()
629 (host->data->blocks == 1)) in sdhci_transfer_pio()
633 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY) in sdhci_transfer_pio()
636 if (host->data->flags & MMC_DATA_READ) in sdhci_transfer_pio()
641 host->blocks--; in sdhci_transfer_pio()
642 if (host->blocks == 0) in sdhci_transfer_pio()
649 static int sdhci_pre_dma_transfer(struct sdhci_host *host, in sdhci_pre_dma_transfer()
650 struct mmc_data *data, int cookie) in sdhci_pre_dma_transfer()
652 int sg_count; in sdhci_pre_dma_transfer()
658 if (data->host_cookie == COOKIE_PRE_MAPPED) in sdhci_pre_dma_transfer()
659 return data->sg_count; in sdhci_pre_dma_transfer()
662 if (host->bounce_buffer) { in sdhci_pre_dma_transfer()
663 unsigned int length = data->blksz * data->blocks; in sdhci_pre_dma_transfer()
665 if (length > host->bounce_buffer_size) { in sdhci_pre_dma_transfer()
667 mmc_hostname(host->mmc), length, in sdhci_pre_dma_transfer()
668 host->bounce_buffer_size); in sdhci_pre_dma_transfer()
669 return -EIO; in sdhci_pre_dma_transfer()
673 if (host->ops->copy_to_bounce_buffer) { in sdhci_pre_dma_transfer()
674 host->ops->copy_to_bounce_buffer(host, in sdhci_pre_dma_transfer()
677 sg_copy_to_buffer(data->sg, data->sg_len, in sdhci_pre_dma_transfer()
678 host->bounce_buffer, length); in sdhci_pre_dma_transfer()
682 dma_sync_single_for_device(mmc_dev(host->mmc), in sdhci_pre_dma_transfer()
683 host->bounce_addr, in sdhci_pre_dma_transfer()
684 host->bounce_buffer_size, in sdhci_pre_dma_transfer()
690 sg_count = dma_map_sg(mmc_dev(host->mmc), in sdhci_pre_dma_transfer()
691 data->sg, data->sg_len, in sdhci_pre_dma_transfer()
696 return -ENOSPC; in sdhci_pre_dma_transfer()
698 data->sg_count = sg_count; in sdhci_pre_dma_transfer()
699 data->host_cookie = cookie; in sdhci_pre_dma_transfer()
706 return kmap_local_page(sg_page(sg)) + sg->offset; in sdhci_kmap_atomic()
715 dma_addr_t addr, int len, unsigned int cmd) in sdhci_adma_write_desc()
719 /* 32-bit and 64-bit descriptors have these members in same position */ in sdhci_adma_write_desc()
720 dma_desc->cmd = cpu_to_le16(cmd); in sdhci_adma_write_desc()
721 dma_desc->len = cpu_to_le16(len); in sdhci_adma_write_desc()
722 dma_desc->addr_lo = cpu_to_le32(lower_32_bits(addr)); in sdhci_adma_write_desc()
724 if (host->flags & SDHCI_USE_64_BIT_DMA) in sdhci_adma_write_desc()
725 dma_desc->addr_hi = cpu_to_le32(upper_32_bits(addr)); in sdhci_adma_write_desc()
727 *desc += host->desc_sz; in sdhci_adma_write_desc()
733 int len, unsigned int cmd) in __sdhci_adma_write_desc()
735 if (host->ops->adma_write_desc) in __sdhci_adma_write_desc()
736 host->ops->adma_write_desc(host, desc, addr, len, cmd); in __sdhci_adma_write_desc()
745 /* 32-bit and 64-bit descriptors have 'cmd' in same position */ in sdhci_adma_mark_end()
746 dma_desc->cmd |= cpu_to_le16(ADMA2_END); in sdhci_adma_mark_end()
750 struct mmc_data *data, int sg_count) in sdhci_adma_table_pre()
756 int len, offset, i; in sdhci_adma_table_pre()
763 host->sg_count = sg_count; in sdhci_adma_table_pre()
765 desc = host->adma_table; in sdhci_adma_table_pre()
766 align = host->align_buffer; in sdhci_adma_table_pre()
768 align_addr = host->align_addr; in sdhci_adma_table_pre()
770 for_each_sg(data->sg, sg, host->sg_count, i) { in sdhci_adma_table_pre()
776 * be 32-bit aligned. If they aren't, then we use a bounce in sdhci_adma_table_pre()
780 offset = (SDHCI_ADMA2_ALIGN - (addr & SDHCI_ADMA2_MASK)) & in sdhci_adma_table_pre()
783 if (data->flags & MMC_DATA_WRITE) { in sdhci_adma_table_pre()
799 len -= offset; in sdhci_adma_table_pre()
808 while (len > host->max_adma) { in sdhci_adma_table_pre()
809 int n = 32 * 1024; /* 32KiB*/ in sdhci_adma_table_pre()
813 len -= n; in sdhci_adma_table_pre()
825 WARN_ON((desc - host->adma_table) >= host->adma_table_sz); in sdhci_adma_table_pre()
828 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) { in sdhci_adma_table_pre()
830 if (desc != host->adma_table) { in sdhci_adma_table_pre()
831 desc -= host->desc_sz; in sdhci_adma_table_pre()
835 /* Add a terminating entry - nop, end, valid */ in sdhci_adma_table_pre()
844 int i, size; in sdhci_adma_table_post()
848 if (data->flags & MMC_DATA_READ) { in sdhci_adma_table_post()
852 for_each_sg(data->sg, sg, host->sg_count, i) in sdhci_adma_table_post()
859 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg, in sdhci_adma_table_post()
860 data->sg_len, DMA_FROM_DEVICE); in sdhci_adma_table_post()
862 align = host->align_buffer; in sdhci_adma_table_post()
864 for_each_sg(data->sg, sg, host->sg_count, i) { in sdhci_adma_table_post()
866 size = SDHCI_ADMA2_ALIGN - in sdhci_adma_table_post()
883 if (host->flags & SDHCI_USE_64_BIT_DMA) in sdhci_set_adma_addr()
889 if (host->bounce_buffer) in sdhci_sdma_address()
890 return host->bounce_addr; in sdhci_sdma_address()
892 return sg_dma_address(host->data->sg); in sdhci_sdma_address()
897 if (host->v4_mode) in sdhci_set_sdma_addr()
903 static unsigned int sdhci_target_timeout(struct sdhci_host *host, in sdhci_target_timeout()
907 unsigned int target_timeout; in sdhci_target_timeout()
911 target_timeout = cmd->busy_timeout * 1000; in sdhci_target_timeout()
913 target_timeout = DIV_ROUND_UP(data->timeout_ns, 1000); in sdhci_target_timeout()
914 if (host->clock && data->timeout_clks) { in sdhci_target_timeout()
918 * data->timeout_clks is in units of clock cycles. in sdhci_target_timeout()
919 * host->clock is in Hz. target_timeout is in us. in sdhci_target_timeout()
922 val = 1000000ULL * data->timeout_clks; in sdhci_target_timeout()
923 if (do_div(val, host->clock)) in sdhci_target_timeout()
935 struct mmc_data *data = cmd->data; in sdhci_calc_sw_timeout()
936 struct mmc_host *mmc = host->mmc; in sdhci_calc_sw_timeout()
937 struct mmc_ios *ios = &mmc->ios; in sdhci_calc_sw_timeout()
938 unsigned char bus_width = 1 << ios->bus_width; in sdhci_calc_sw_timeout()
939 unsigned int blksz; in sdhci_calc_sw_timeout()
940 unsigned int freq; in sdhci_calc_sw_timeout()
948 blksz = data->blksz; in sdhci_calc_sw_timeout()
949 freq = mmc->actual_clock ? : host->clock; in sdhci_calc_sw_timeout()
955 host->data_timeout = data->blocks * target_timeout + in sdhci_calc_sw_timeout()
958 host->data_timeout = target_timeout; in sdhci_calc_sw_timeout()
961 if (host->data_timeout) in sdhci_calc_sw_timeout()
962 host->data_timeout += MMC_CMD_TRANSFER_TIME; in sdhci_calc_sw_timeout()
977 * longer to time out, but that's much better than having a too-short in sdhci_calc_timeout()
980 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL) in sdhci_calc_timeout()
981 return host->max_timeout_count; in sdhci_calc_timeout()
985 return host->max_timeout_count; in sdhci_calc_timeout()
987 data = cmd->data; in sdhci_calc_timeout()
989 if (!data && !cmd->busy_timeout) in sdhci_calc_timeout()
990 return host->max_timeout_count; in sdhci_calc_timeout()
997 * We do this in steps in order to fit inside a 32 bit int. in sdhci_calc_timeout()
1001 * (2) host->timeout_clk < 2^16 in sdhci_calc_timeout()
1006 current_timeout = (1 << 13) * 1000 / host->timeout_clk; in sdhci_calc_timeout()
1010 if (count > host->max_timeout_count) { in sdhci_calc_timeout()
1011 if (!(host->quirks2 & SDHCI_QUIRK2_DISABLE_HW_TIMEOUT)) in sdhci_calc_timeout()
1013 count, cmd->opcode); in sdhci_calc_timeout()
1014 count = host->max_timeout_count; in sdhci_calc_timeout()
1028 if (host->flags & SDHCI_REQ_USE_DMA) in sdhci_set_transfer_irqs()
1029 host->ier = (host->ier & ~pio_irqs) | dma_irqs; in sdhci_set_transfer_irqs()
1031 host->ier = (host->ier & ~dma_irqs) | pio_irqs; in sdhci_set_transfer_irqs()
1033 if (host->flags & (SDHCI_AUTO_CMD23 | SDHCI_AUTO_CMD12)) in sdhci_set_transfer_irqs()
1034 host->ier |= SDHCI_INT_AUTO_CMD_ERR; in sdhci_set_transfer_irqs()
1036 host->ier &= ~SDHCI_INT_AUTO_CMD_ERR; in sdhci_set_transfer_irqs()
1038 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE); in sdhci_set_transfer_irqs()
1039 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE); in sdhci_set_transfer_irqs()
1045 host->ier |= SDHCI_INT_DATA_TIMEOUT; in sdhci_set_data_timeout_irq()
1047 host->ier &= ~SDHCI_INT_DATA_TIMEOUT; in sdhci_set_data_timeout_irq()
1048 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE); in sdhci_set_data_timeout_irq()
1049 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE); in sdhci_set_data_timeout_irq()
1059 host->quirks2 & SDHCI_QUIRK2_DISABLE_HW_TIMEOUT) { in __sdhci_set_timeout()
1062 } else if (!(host->ier & SDHCI_INT_DATA_TIMEOUT)) { in __sdhci_set_timeout()
1072 if (host->ops->set_timeout) in sdhci_set_timeout()
1073 host->ops->set_timeout(host, cmd); in sdhci_set_timeout()
1081 WARN_ON(host->data); in sdhci_initialize_data()
1084 BUG_ON(data->blksz * data->blocks > 524288); in sdhci_initialize_data()
1085 BUG_ON(data->blksz > host->mmc->max_blk_size); in sdhci_initialize_data()
1086 BUG_ON(data->blocks > 65535); in sdhci_initialize_data()
1088 host->data = data; in sdhci_initialize_data()
1089 host->data_early = 0; in sdhci_initialize_data()
1090 host->data->bytes_xfered = 0; in sdhci_initialize_data()
1098 SDHCI_MAKE_BLKSZ(host->sdma_boundary, data->blksz), in sdhci_set_block_info()
1101 * For Version 4.10 onwards, if v4 mode is enabled, 32-bit Block Count in sdhci_set_block_info()
1102 * can be supported, in that case 16-bit block count register must be 0. in sdhci_set_block_info()
1104 if (host->version >= SDHCI_SPEC_410 && host->v4_mode && in sdhci_set_block_info()
1105 (host->quirks2 & SDHCI_QUIRK2_USE_32BIT_BLK_CNT)) { in sdhci_set_block_info()
1108 sdhci_writew(host, data->blocks, SDHCI_32BIT_BLK_CNT); in sdhci_set_block_info()
1110 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT); in sdhci_set_block_info()
1116 struct mmc_data *data = cmd->data; in sdhci_prepare_data()
1120 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) { in sdhci_prepare_data()
1122 unsigned int length_mask, offset_mask; in sdhci_prepare_data()
1123 int i; in sdhci_prepare_data()
1125 host->flags |= SDHCI_REQ_USE_DMA; in sdhci_prepare_data()
1136 if (host->flags & SDHCI_USE_ADMA) { in sdhci_prepare_data()
1137 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE) { in sdhci_prepare_data()
1147 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE) in sdhci_prepare_data()
1149 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) in sdhci_prepare_data()
1154 for_each_sg(data->sg, sg, data->sg_len, i) { in sdhci_prepare_data()
1155 if (sg->length & length_mask) { in sdhci_prepare_data()
1157 sg->length); in sdhci_prepare_data()
1158 host->flags &= ~SDHCI_REQ_USE_DMA; in sdhci_prepare_data()
1161 if (sg->offset & offset_mask) { in sdhci_prepare_data()
1163 host->flags &= ~SDHCI_REQ_USE_DMA; in sdhci_prepare_data()
1172 if (host->flags & SDHCI_REQ_USE_DMA) { in sdhci_prepare_data()
1173 int sg_cnt = sdhci_pre_dma_transfer(host, data, COOKIE_MAPPED); in sdhci_prepare_data()
1181 host->flags &= ~SDHCI_REQ_USE_DMA; in sdhci_prepare_data()
1182 } else if (host->flags & SDHCI_USE_ADMA) { in sdhci_prepare_data()
1184 sdhci_set_adma_addr(host, host->adma_addr); in sdhci_prepare_data()
1191 if (!(host->flags & SDHCI_REQ_USE_DMA)) { in sdhci_prepare_data()
1192 int flags; in sdhci_prepare_data()
1195 if (host->data->flags & MMC_DATA_READ) in sdhci_prepare_data()
1199 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags); in sdhci_prepare_data()
1200 host->blocks = data->blocks; in sdhci_prepare_data()
1210 static int sdhci_external_dma_init(struct sdhci_host *host) in sdhci_external_dma_init()
1212 int ret = 0; in sdhci_external_dma_init()
1213 struct mmc_host *mmc = host->mmc; in sdhci_external_dma_init()
1215 host->tx_chan = dma_request_chan(mmc_dev(mmc), "tx"); in sdhci_external_dma_init()
1216 if (IS_ERR(host->tx_chan)) { in sdhci_external_dma_init()
1217 ret = PTR_ERR(host->tx_chan); in sdhci_external_dma_init()
1218 if (ret != -EPROBE_DEFER) in sdhci_external_dma_init()
1220 host->tx_chan = NULL; in sdhci_external_dma_init()
1224 host->rx_chan = dma_request_chan(mmc_dev(mmc), "rx"); in sdhci_external_dma_init()
1225 if (IS_ERR(host->rx_chan)) { in sdhci_external_dma_init()
1226 if (host->tx_chan) { in sdhci_external_dma_init()
1227 dma_release_channel(host->tx_chan); in sdhci_external_dma_init()
1228 host->tx_chan = NULL; in sdhci_external_dma_init()
1231 ret = PTR_ERR(host->rx_chan); in sdhci_external_dma_init()
1232 if (ret != -EPROBE_DEFER) in sdhci_external_dma_init()
1234 host->rx_chan = NULL; in sdhci_external_dma_init()
1243 return data->flags & MMC_DATA_WRITE ? host->tx_chan : host->rx_chan; in sdhci_external_dma_channel()
1246 static int sdhci_external_dma_setup(struct sdhci_host *host, in sdhci_external_dma_setup()
1249 int ret, i; in sdhci_external_dma_setup()
1252 struct mmc_data *data = cmd->data; in sdhci_external_dma_setup()
1256 int sg_cnt; in sdhci_external_dma_setup()
1258 if (!host->mapbase) in sdhci_external_dma_setup()
1259 return -EINVAL; in sdhci_external_dma_setup()
1262 cfg.src_addr = host->mapbase + SDHCI_BUFFER; in sdhci_external_dma_setup()
1263 cfg.dst_addr = host->mapbase + SDHCI_BUFFER; in sdhci_external_dma_setup()
1266 cfg.src_maxburst = data->blksz / 4; in sdhci_external_dma_setup()
1267 cfg.dst_maxburst = data->blksz / 4; in sdhci_external_dma_setup()
1270 for (i = 0; i < data->sg_len; i++) { in sdhci_external_dma_setup()
1271 if ((data->sg + i)->length % data->blksz) in sdhci_external_dma_setup()
1272 return -EINVAL; in sdhci_external_dma_setup()
1283 return -EINVAL; in sdhci_external_dma_setup()
1285 dir = data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM; in sdhci_external_dma_setup()
1286 desc = dmaengine_prep_slave_sg(chan, data->sg, data->sg_len, dir, in sdhci_external_dma_setup()
1289 return -EINVAL; in sdhci_external_dma_setup()
1291 desc->callback = NULL; in sdhci_external_dma_setup()
1292 desc->callback_param = NULL; in sdhci_external_dma_setup()
1303 if (host->tx_chan) { in sdhci_external_dma_release()
1304 dma_release_channel(host->tx_chan); in sdhci_external_dma_release()
1305 host->tx_chan = NULL; in sdhci_external_dma_release()
1308 if (host->rx_chan) { in sdhci_external_dma_release()
1309 dma_release_channel(host->rx_chan); in sdhci_external_dma_release()
1310 host->rx_chan = NULL; in sdhci_external_dma_release()
1319 struct mmc_data *data = cmd->data; in __sdhci_external_dma_prepare_data()
1323 host->flags |= SDHCI_REQ_USE_DMA; in __sdhci_external_dma_prepare_data()
1337 mmc_hostname(host->mmc)); in sdhci_external_dma_prepare_data()
1347 if (!cmd->data) in sdhci_external_dma_pre_transfer()
1350 chan = sdhci_external_dma_channel(host, cmd->data); in sdhci_external_dma_pre_transfer()
1357 static inline int sdhci_external_dma_init(struct sdhci_host *host) in sdhci_external_dma_init()
1359 return -EOPNOTSUPP; in sdhci_external_dma_init()
1388 host->use_external_dma = en; in sdhci_switch_external_dma()
1395 return !mrq->sbc && (host->flags & SDHCI_AUTO_CMD12) && in sdhci_auto_cmd12()
1396 !mrq->cap_cmd_during_tfr; in sdhci_auto_cmd12()
1402 return mrq->sbc && (host->flags & SDHCI_AUTO_CMD23); in sdhci_auto_cmd23()
1408 return mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23); in sdhci_manual_cmd23()
1415 bool use_cmd12 = sdhci_auto_cmd12(host, cmd->mrq) && in sdhci_auto_cmd_select()
1416 (cmd->opcode != SD_IO_RW_EXTENDED); in sdhci_auto_cmd_select()
1417 bool use_cmd23 = sdhci_auto_cmd23(host, cmd->mrq); in sdhci_auto_cmd_select()
1424 * here because some controllers (e.g sdhci-of-dwmshc) expect it. in sdhci_auto_cmd_select()
1426 if (host->version >= SDHCI_SPEC_410 && host->v4_mode && in sdhci_auto_cmd_select()
1442 * on successful completion (so no Auto-CMD12). in sdhci_auto_cmd_select()
1454 struct mmc_data *data = cmd->data; in sdhci_set_transfer_mode()
1457 if (host->quirks2 & in sdhci_set_transfer_mode()
1460 if (!mmc_op_tuning(cmd->opcode)) in sdhci_set_transfer_mode()
1471 WARN_ON(!host->data); in sdhci_set_transfer_mode()
1473 if (!(host->quirks2 & SDHCI_QUIRK2_SUPPORT_SINGLE)) in sdhci_set_transfer_mode()
1476 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) { in sdhci_set_transfer_mode()
1479 if (sdhci_auto_cmd23(host, cmd->mrq)) in sdhci_set_transfer_mode()
1480 sdhci_writel(host, cmd->mrq->sbc->arg, SDHCI_ARGUMENT2); in sdhci_set_transfer_mode()
1483 if (data->flags & MMC_DATA_READ) in sdhci_set_transfer_mode()
1485 if (host->flags & SDHCI_REQ_USE_DMA) in sdhci_set_transfer_mode()
1493 return (!(host->flags & SDHCI_DEVICE_DEAD) && in sdhci_needs_reset()
1494 ((mrq->cmd && mrq->cmd->error) || in sdhci_needs_reset()
1495 (mrq->sbc && mrq->sbc->error) || in sdhci_needs_reset()
1496 (mrq->data && mrq->data->stop && mrq->data->stop->error) || in sdhci_needs_reset()
1497 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))); in sdhci_needs_reset()
1502 int i; in sdhci_set_mrq_done()
1505 if (host->mrqs_done[i] == mrq) { in sdhci_set_mrq_done()
1512 if (!host->mrqs_done[i]) { in sdhci_set_mrq_done()
1513 host->mrqs_done[i] = mrq; in sdhci_set_mrq_done()
1523 if (host->cmd && host->cmd->mrq == mrq) in __sdhci_finish_mrq()
1524 host->cmd = NULL; in __sdhci_finish_mrq()
1526 if (host->data_cmd && host->data_cmd->mrq == mrq) in __sdhci_finish_mrq()
1527 host->data_cmd = NULL; in __sdhci_finish_mrq()
1529 if (host->deferred_cmd && host->deferred_cmd->mrq == mrq) in __sdhci_finish_mrq()
1530 host->deferred_cmd = NULL; in __sdhci_finish_mrq()
1532 if (host->data && host->data->mrq == mrq) in __sdhci_finish_mrq()
1533 host->data = NULL; in __sdhci_finish_mrq()
1536 host->pending_reset = true; in __sdhci_finish_mrq()
1550 queue_work(host->complete_wq, &host->complete_work); in sdhci_finish_mrq()
1555 struct mmc_command *data_cmd = host->data_cmd; in __sdhci_finish_data()
1556 struct mmc_data *data = host->data; in __sdhci_finish_data()
1558 host->data = NULL; in __sdhci_finish_data()
1559 host->data_cmd = NULL; in __sdhci_finish_data()
1565 if (data->error) { in __sdhci_finish_data()
1566 if (!host->cmd || host->cmd == data_cmd) in __sdhci_finish_data()
1572 if ((host->flags & (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA)) == in __sdhci_finish_data()
1583 if (data->error) in __sdhci_finish_data()
1584 data->bytes_xfered = 0; in __sdhci_finish_data()
1586 data->bytes_xfered = data->blksz * data->blocks; in __sdhci_finish_data()
1589 * Need to send CMD12 if - in __sdhci_finish_data()
1590 * a) open-ended multiblock transfer not using auto CMD12 (no CMD23) in __sdhci_finish_data()
1593 if (data->stop && in __sdhci_finish_data()
1594 ((!data->mrq->sbc && !sdhci_auto_cmd12(host, data->mrq)) || in __sdhci_finish_data()
1595 data->error)) { in __sdhci_finish_data()
1601 if (data->mrq->cap_cmd_during_tfr) { in __sdhci_finish_data()
1602 __sdhci_finish_mrq(host, data->mrq); in __sdhci_finish_data()
1605 host->cmd = NULL; in __sdhci_finish_data()
1606 if (!sdhci_send_command(host, data->stop)) { in __sdhci_finish_data()
1612 data->stop->error = -EIO; in __sdhci_finish_data()
1613 __sdhci_finish_mrq(host, data->mrq); in __sdhci_finish_data()
1615 WARN_ON(host->deferred_cmd); in __sdhci_finish_data()
1616 host->deferred_cmd = data->stop; in __sdhci_finish_data()
1621 __sdhci_finish_mrq(host, data->mrq); in __sdhci_finish_data()
1632 int flags; in sdhci_send_command()
1636 WARN_ON(host->cmd); in sdhci_send_command()
1639 cmd->error = 0; in sdhci_send_command()
1641 if ((host->quirks2 & SDHCI_QUIRK2_STOP_WITH_TC) && in sdhci_send_command()
1642 cmd->opcode == MMC_STOP_TRANSMISSION) in sdhci_send_command()
1643 cmd->flags |= MMC_RSP_BUSY; in sdhci_send_command()
1651 if (cmd->mrq->data && (cmd == cmd->mrq->data->stop)) in sdhci_send_command()
1657 host->cmd = cmd; in sdhci_send_command()
1658 host->data_timeout = 0; in sdhci_send_command()
1660 WARN_ON(host->data_cmd); in sdhci_send_command()
1661 host->data_cmd = cmd; in sdhci_send_command()
1665 if (cmd->data) { in sdhci_send_command()
1666 if (host->use_external_dma) in sdhci_send_command()
1672 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT); in sdhci_send_command()
1676 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) { in sdhci_send_command()
1679 * This does not happen in practice because 136-bit response in sdhci_send_command()
1683 cmd->flags &= ~MMC_RSP_BUSY; in sdhci_send_command()
1686 if (!(cmd->flags & MMC_RSP_PRESENT)) in sdhci_send_command()
1688 else if (cmd->flags & MMC_RSP_136) in sdhci_send_command()
1690 else if (cmd->flags & MMC_RSP_BUSY) in sdhci_send_command()
1695 if (cmd->flags & MMC_RSP_CRC) in sdhci_send_command()
1697 if (cmd->flags & MMC_RSP_OPCODE) in sdhci_send_command()
1701 if (cmd->data || mmc_op_tuning(cmd->opcode)) in sdhci_send_command()
1705 if (host->data_timeout) in sdhci_send_command()
1706 timeout += nsecs_to_jiffies(host->data_timeout); in sdhci_send_command()
1707 else if (!cmd->data && cmd->busy_timeout > 9000) in sdhci_send_command()
1708 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ; in sdhci_send_command()
1711 sdhci_mod_timer(host, cmd->mrq, timeout); in sdhci_send_command()
1713 if (host->use_external_dma) in sdhci_send_command()
1716 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND); in sdhci_send_command()
1724 if (!present || host->flags & SDHCI_DEVICE_DEAD) { in sdhci_present_error()
1725 cmd->error = -ENOMEDIUM; in sdhci_present_error()
1735 __releases(host->lock) in sdhci_send_command_retry()
1736 __acquires(host->lock) in sdhci_send_command_retry()
1738 struct mmc_command *deferred_cmd = host->deferred_cmd; in sdhci_send_command_retry()
1739 int timeout = 10; /* Approx. 10 ms */ in sdhci_send_command_retry()
1743 if (!timeout--) { in sdhci_send_command_retry()
1745 mmc_hostname(host->mmc)); in sdhci_send_command_retry()
1748 cmd->error = -EIO; in sdhci_send_command_retry()
1752 spin_unlock_irqrestore(&host->lock, flags); in sdhci_send_command_retry()
1756 present = host->mmc->ops->get_cd(host->mmc); in sdhci_send_command_retry()
1758 spin_lock_irqsave(&host->lock, flags); in sdhci_send_command_retry()
1761 if (cmd == deferred_cmd && cmd != host->deferred_cmd) in sdhci_send_command_retry()
1768 if (cmd == host->deferred_cmd) in sdhci_send_command_retry()
1769 host->deferred_cmd = NULL; in sdhci_send_command_retry()
1776 int i, reg; in sdhci_read_rsp_136()
1779 reg = SDHCI_RESPONSE + (3 - i) * 4; in sdhci_read_rsp_136()
1780 cmd->resp[i] = sdhci_readl(host, reg); in sdhci_read_rsp_136()
1783 if (host->quirks2 & SDHCI_QUIRK2_RSP_136_HAS_CRC) in sdhci_read_rsp_136()
1788 cmd->resp[i] <<= 8; in sdhci_read_rsp_136()
1790 cmd->resp[i] |= cmd->resp[i + 1] >> 24; in sdhci_read_rsp_136()
1796 struct mmc_command *cmd = host->cmd; in sdhci_finish_command()
1798 host->cmd = NULL; in sdhci_finish_command()
1800 if (cmd->flags & MMC_RSP_PRESENT) { in sdhci_finish_command()
1801 if (cmd->flags & MMC_RSP_136) { in sdhci_finish_command()
1804 cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE); in sdhci_finish_command()
1808 if (cmd->mrq->cap_cmd_during_tfr && cmd == cmd->mrq->cmd) in sdhci_finish_command()
1809 mmc_command_done(host->mmc, cmd->mrq); in sdhci_finish_command()
1821 if (cmd->flags & MMC_RSP_BUSY) { in sdhci_finish_command()
1822 if (cmd->data) { in sdhci_finish_command()
1824 } else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ) && in sdhci_finish_command()
1825 cmd == host->data_cmd) { in sdhci_finish_command()
1832 if (cmd == cmd->mrq->sbc) { in sdhci_finish_command()
1833 if (!sdhci_send_command(host, cmd->mrq->cmd)) { in sdhci_finish_command()
1834 WARN_ON(host->deferred_cmd); in sdhci_finish_command()
1835 host->deferred_cmd = cmd->mrq->cmd; in sdhci_finish_command()
1840 if (host->data && host->data_early) in sdhci_finish_command()
1843 if (!cmd->data) in sdhci_finish_command()
1844 __sdhci_finish_mrq(host, cmd->mrq); in sdhci_finish_command()
1852 switch (host->timing) { in sdhci_get_preset_value()
1878 pr_warn("%s: Invalid UHS-I mode selected\n", in sdhci_get_preset_value()
1879 mmc_hostname(host->mmc)); in sdhci_get_preset_value()
1886 u16 sdhci_calc_clk(struct sdhci_host *host, unsigned int clock, in sdhci_calc_clk() argument
1887 unsigned int *actual_clock) in sdhci_calc_clk()
1889 int div = 0; /* Initialized for compiler warning */ in sdhci_calc_clk()
1890 int real_div = div, clk_mul = 1; in sdhci_calc_clk()
1894 if (host->version >= SDHCI_SPEC_300) { in sdhci_calc_clk()
1895 if (host->preset_enabled) { in sdhci_calc_clk()
1901 if (host->clk_mul && in sdhci_calc_clk()
1905 clk_mul = host->clk_mul; in sdhci_calc_clk()
1907 real_div = max_t(int, 1, div << 1); in sdhci_calc_clk()
1913 * Check if the Host Controller supports Programmable Clock in sdhci_calc_clk()
1916 if (host->clk_mul) { in sdhci_calc_clk()
1918 if ((host->max_clk * host->clk_mul / div) in sdhci_calc_clk()
1919 <= clock) in sdhci_calc_clk()
1922 if ((host->max_clk * host->clk_mul / div) <= clock) { in sdhci_calc_clk()
1924 * Set Programmable Clock Mode in the Clock in sdhci_calc_clk()
1929 clk_mul = host->clk_mul; in sdhci_calc_clk()
1930 div--; in sdhci_calc_clk()
1933 * Divisor can be too small to reach clock in sdhci_calc_clk()
1934 * speed requirement. Then use the base clock. in sdhci_calc_clk()
1940 if (!host->clk_mul || switch_base_clk) { in sdhci_calc_clk()
1942 if (host->max_clk <= clock) in sdhci_calc_clk()
1947 if ((host->max_clk / div) <= clock) in sdhci_calc_clk()
1953 if ((host->quirks2 & SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN) in sdhci_calc_clk()
1954 && !div && host->max_clk <= 25000000) in sdhci_calc_clk()
1960 if ((host->max_clk / div) <= clock) in sdhci_calc_clk()
1969 *actual_clock = (host->max_clk * clk_mul) / real_div; in sdhci_calc_clk()
1994 pr_err("%s: Internal clock never stabilised.\n", in sdhci_enable_clk()
1995 mmc_hostname(host->mmc)); in sdhci_enable_clk()
2003 if (host->version >= SDHCI_SPEC_410 && host->v4_mode) { in sdhci_enable_clk()
2017 pr_err("%s: PLL clock never stabilised.\n", in sdhci_enable_clk()
2018 mmc_hostname(host->mmc)); in sdhci_enable_clk()
2032 void sdhci_set_clock(struct sdhci_host *host, unsigned int clock) in sdhci_set_clock() argument
2036 host->mmc->actual_clock = 0; in sdhci_set_clock()
2040 if (clock == 0) in sdhci_set_clock()
2043 clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock); in sdhci_set_clock()
2051 struct mmc_host *mmc = host->mmc; in sdhci_set_power_reg()
2053 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd); in sdhci_set_power_reg()
2094 mmc_hostname(host->mmc), vdd); in sdhci_set_power_noreg()
2099 if (host->pwr == pwr) in sdhci_set_power_noreg()
2102 host->pwr = pwr; in sdhci_set_power_noreg()
2106 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON) in sdhci_set_power_noreg()
2113 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE)) in sdhci_set_power_noreg()
2121 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER) in sdhci_set_power_noreg()
2128 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON) in sdhci_set_power_noreg()
2133 * they can apply clock after applying power in sdhci_set_power_noreg()
2135 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER) in sdhci_set_power_noreg()
2144 if (IS_ERR(host->mmc->supply.vmmc)) in sdhci_set_power()
2161 if (!IS_ERR(host->mmc->supply.vmmc)) { in sdhci_set_power_and_bus_voltage()
2162 struct mmc_host *mmc = host->mmc; in sdhci_set_power_and_bus_voltage()
2164 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd); in sdhci_set_power_and_bus_voltage()
2184 present = mmc->ops->get_cd(mmc); in sdhci_request()
2186 spin_lock_irqsave(&host->lock, flags); in sdhci_request()
2190 if (sdhci_present_error(host, mrq->cmd, present)) in sdhci_request()
2193 cmd = sdhci_manual_cmd23(host, mrq) ? mrq->sbc : mrq->cmd; in sdhci_request()
2198 spin_unlock_irqrestore(&host->lock, flags); in sdhci_request()
2204 spin_unlock_irqrestore(&host->lock, flags); in sdhci_request()
2208 int sdhci_request_atomic(struct mmc_host *mmc, struct mmc_request *mrq) in sdhci_request_atomic()
2213 int ret = 0; in sdhci_request_atomic()
2215 spin_lock_irqsave(&host->lock, flags); in sdhci_request_atomic()
2217 if (sdhci_present_error(host, mrq->cmd, true)) { in sdhci_request_atomic()
2222 cmd = sdhci_manual_cmd23(host, mrq) ? mrq->sbc : mrq->cmd; in sdhci_request_atomic()
2228 * again in non-atomic context. So we should not finish this request in sdhci_request_atomic()
2232 ret = -EBUSY; in sdhci_request_atomic()
2237 spin_unlock_irqrestore(&host->lock, flags); in sdhci_request_atomic()
2242 void sdhci_set_bus_width(struct sdhci_host *host, int width) in sdhci_set_bus_width()
2251 if (host->mmc->caps & MMC_CAP_8_BIT_DATA) in sdhci_set_bus_width()
2282 ctrl_2 |= SDHCI_CTRL_HS400; /* Non-standard */ in sdhci_set_uhs_signaling()
2303 return !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) && in sdhci_preset_needed()
2310 * Preset Values are: Driver Strength, Clock Generator and SDCLK/RCLK in sdhci_presetable_values_change()
2312 * Strength needs updating. Note, clock changes are handled separately. in sdhci_presetable_values_change()
2314 return !host->preset_enabled && in sdhci_presetable_values_change()
2315 (sdhci_preset_needed(host, ios->timing) || host->drv_type != ios->drv_type); in sdhci_presetable_values_change()
2321 bool reinit_uhs = host->reinit_uhs; in sdhci_set_ios()
2325 host->reinit_uhs = false; in sdhci_set_ios()
2327 if (ios->power_mode == MMC_POWER_UNDEFINED) in sdhci_set_ios()
2330 if (host->flags & SDHCI_DEVICE_DEAD) { in sdhci_set_ios()
2331 if (!IS_ERR(mmc->supply.vmmc) && in sdhci_set_ios()
2332 ios->power_mode == MMC_POWER_OFF) in sdhci_set_ios()
2333 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0); in sdhci_set_ios()
2341 if (ios->power_mode == MMC_POWER_OFF) { in sdhci_set_ios()
2346 if (host->version >= SDHCI_SPEC_300 && in sdhci_set_ios()
2347 (ios->power_mode == MMC_POWER_UP) && in sdhci_set_ios()
2348 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) in sdhci_set_ios()
2351 if (!ios->clock || ios->clock != host->clock) { in sdhci_set_ios()
2352 turning_on_clk = ios->clock && !host->clock; in sdhci_set_ios()
2354 host->ops->set_clock(host, ios->clock); in sdhci_set_ios()
2355 host->clock = ios->clock; in sdhci_set_ios()
2357 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK && in sdhci_set_ios()
2358 host->clock) { in sdhci_set_ios()
2359 host->timeout_clk = mmc->actual_clock ? in sdhci_set_ios()
2360 mmc->actual_clock / 1000 : in sdhci_set_ios()
2361 host->clock / 1000; in sdhci_set_ios()
2362 mmc->max_busy_timeout = in sdhci_set_ios()
2363 host->ops->get_max_timeout_count ? in sdhci_set_ios()
2364 host->ops->get_max_timeout_count(host) : in sdhci_set_ios()
2366 mmc->max_busy_timeout /= host->timeout_clk; in sdhci_set_ios()
2370 if (host->ops->set_power) in sdhci_set_ios()
2371 host->ops->set_power(host, ios->power_mode, ios->vdd); in sdhci_set_ios()
2373 sdhci_set_power(host, ios->power_mode, ios->vdd); in sdhci_set_ios()
2375 if (host->ops->platform_send_init_74_clocks) in sdhci_set_ios()
2376 host->ops->platform_send_init_74_clocks(host, ios->power_mode); in sdhci_set_ios()
2378 host->ops->set_bus_width(host, ios->bus_width); in sdhci_set_ios()
2381 * Special case to avoid multiple clock changes during voltage in sdhci_set_ios()
2386 host->timing == ios->timing && in sdhci_set_ios()
2387 host->version >= SDHCI_SPEC_300 && in sdhci_set_ios()
2393 if (!(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT)) { in sdhci_set_ios()
2394 if (ios->timing == MMC_TIMING_SD_HS || in sdhci_set_ios()
2395 ios->timing == MMC_TIMING_MMC_HS || in sdhci_set_ios()
2396 ios->timing == MMC_TIMING_MMC_HS400 || in sdhci_set_ios()
2397 ios->timing == MMC_TIMING_MMC_HS200 || in sdhci_set_ios()
2398 ios->timing == MMC_TIMING_MMC_DDR52 || in sdhci_set_ios()
2399 ios->timing == MMC_TIMING_UHS_SDR50 || in sdhci_set_ios()
2400 ios->timing == MMC_TIMING_UHS_SDR104 || in sdhci_set_ios()
2401 ios->timing == MMC_TIMING_UHS_DDR50 || in sdhci_set_ios()
2402 ios->timing == MMC_TIMING_UHS_SDR25) in sdhci_set_ios()
2408 if (host->version >= SDHCI_SPEC_300) { in sdhci_set_ios()
2414 * need to reset SD Clock Enable before changing High in sdhci_set_ios()
2415 * Speed Enable to avoid generating clock glitches. in sdhci_set_ios()
2425 if (!host->preset_enabled) { in sdhci_set_ios()
2432 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A) in sdhci_set_ios()
2434 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_B) in sdhci_set_ios()
2436 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C) in sdhci_set_ios()
2438 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_D) in sdhci_set_ios()
2447 host->drv_type = ios->drv_type; in sdhci_set_ios()
2450 host->ops->set_uhs_signaling(host, ios->timing); in sdhci_set_ios()
2451 host->timing = ios->timing; in sdhci_set_ios()
2453 if (sdhci_preset_needed(host, ios->timing)) { in sdhci_set_ios()
2458 ios->drv_type = FIELD_GET(SDHCI_PRESET_DRV_MASK, in sdhci_set_ios()
2460 host->drv_type = ios->drv_type; in sdhci_set_ios()
2463 /* Re-enable SD Clock */ in sdhci_set_ios()
2464 host->ops->set_clock(host, host->clock); in sdhci_set_ios()
2470 static int sdhci_get_cd(struct mmc_host *mmc) in sdhci_get_cd()
2473 int gpio_cd = mmc_gpio_get_cd(mmc); in sdhci_get_cd()
2475 if (host->flags & SDHCI_DEVICE_DEAD) in sdhci_get_cd()
2490 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) in sdhci_get_cd()
2497 int sdhci_get_cd_nogpio(struct mmc_host *mmc) in sdhci_get_cd_nogpio()
2501 int ret = 0; in sdhci_get_cd_nogpio()
2503 spin_lock_irqsave(&host->lock, flags); in sdhci_get_cd_nogpio()
2505 if (host->flags & SDHCI_DEVICE_DEAD) in sdhci_get_cd_nogpio()
2510 spin_unlock_irqrestore(&host->lock, flags); in sdhci_get_cd_nogpio()
2516 int sdhci_get_ro(struct mmc_host *mmc) in sdhci_get_ro()
2520 int is_readonly; in sdhci_get_ro()
2522 if (host->flags & SDHCI_DEVICE_DEAD) { in sdhci_get_ro()
2524 } else if (host->ops->get_ro) { in sdhci_get_ro()
2525 is_readonly = host->ops->get_ro(host); in sdhci_get_ro()
2529 allow_invert = !(mmc->caps2 & MMC_CAP2_RO_ACTIVE_HIGH); in sdhci_get_ro()
2538 (host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT)) in sdhci_get_ro()
2549 if (host->ops && host->ops->hw_reset) in sdhci_hw_reset()
2550 host->ops->hw_reset(host); in sdhci_hw_reset()
2553 static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable) in sdhci_enable_sdio_irq_nolock()
2555 if (!(host->flags & SDHCI_DEVICE_DEAD)) { in sdhci_enable_sdio_irq_nolock()
2557 host->ier |= SDHCI_INT_CARD_INT; in sdhci_enable_sdio_irq_nolock()
2559 host->ier &= ~SDHCI_INT_CARD_INT; in sdhci_enable_sdio_irq_nolock()
2561 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE); in sdhci_enable_sdio_irq_nolock()
2562 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE); in sdhci_enable_sdio_irq_nolock()
2566 void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable) in sdhci_enable_sdio_irq()
2574 spin_lock_irqsave(&host->lock, flags); in sdhci_enable_sdio_irq()
2576 spin_unlock_irqrestore(&host->lock, flags); in sdhci_enable_sdio_irq()
2588 spin_lock_irqsave(&host->lock, flags); in sdhci_ack_sdio_irq()
2590 spin_unlock_irqrestore(&host->lock, flags); in sdhci_ack_sdio_irq()
2593 int sdhci_start_signal_voltage_switch(struct mmc_host *mmc, in sdhci_start_signal_voltage_switch()
2598 int ret; in sdhci_start_signal_voltage_switch()
2604 if (host->version < SDHCI_SPEC_300) in sdhci_start_signal_voltage_switch()
2609 switch (ios->signal_voltage) { in sdhci_start_signal_voltage_switch()
2611 if (!(host->flags & SDHCI_SIGNALING_330)) in sdhci_start_signal_voltage_switch()
2612 return -EINVAL; in sdhci_start_signal_voltage_switch()
2617 if (!IS_ERR(mmc->supply.vqmmc)) { in sdhci_start_signal_voltage_switch()
2622 return -EIO; in sdhci_start_signal_voltage_switch()
2628 /* 3.3V regulator output should be stable within 5 ms */ in sdhci_start_signal_voltage_switch()
2633 pr_warn("%s: 3.3V regulator output did not become stable\n", in sdhci_start_signal_voltage_switch()
2636 return -EAGAIN; in sdhci_start_signal_voltage_switch()
2638 if (!(host->flags & SDHCI_SIGNALING_180)) in sdhci_start_signal_voltage_switch()
2639 return -EINVAL; in sdhci_start_signal_voltage_switch()
2640 if (!IS_ERR(mmc->supply.vqmmc)) { in sdhci_start_signal_voltage_switch()
2645 return -EIO; in sdhci_start_signal_voltage_switch()
2657 if (host->ops->voltage_switch) in sdhci_start_signal_voltage_switch()
2658 host->ops->voltage_switch(host); in sdhci_start_signal_voltage_switch()
2660 /* 1.8V regulator output should be stable within 5 ms */ in sdhci_start_signal_voltage_switch()
2665 pr_warn("%s: 1.8V regulator output did not become stable\n", in sdhci_start_signal_voltage_switch()
2668 return -EAGAIN; in sdhci_start_signal_voltage_switch()
2670 if (!(host->flags & SDHCI_SIGNALING_120)) in sdhci_start_signal_voltage_switch()
2671 return -EINVAL; in sdhci_start_signal_voltage_switch()
2672 if (!IS_ERR(mmc->supply.vqmmc)) { in sdhci_start_signal_voltage_switch()
2677 return -EIO; in sdhci_start_signal_voltage_switch()
2688 static int sdhci_card_busy(struct mmc_host *mmc) in sdhci_card_busy()
2699 static int sdhci_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios) in sdhci_prepare_hs400_tuning()
2704 spin_lock_irqsave(&host->lock, flags); in sdhci_prepare_hs400_tuning()
2705 host->flags |= SDHCI_HS400_TUNING; in sdhci_prepare_hs400_tuning()
2706 spin_unlock_irqrestore(&host->lock, flags); in sdhci_prepare_hs400_tuning()
2717 if (host->quirks2 & SDHCI_QUIRK2_TUNING_WORK_AROUND) in sdhci_start_tuning()
2738 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE); in sdhci_end_tuning()
2739 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE); in sdhci_end_tuning()
2762 mmc_send_abort_tuning(host->mmc, opcode); in sdhci_abort_tuning()
2769 * automatically) so mmc_send_tuning() will return -EIO. Also the tuning command
2775 struct mmc_host *mmc = host->mmc; in sdhci_send_tuning()
2779 u32 b = host->sdma_boundary; in sdhci_send_tuning()
2781 spin_lock_irqsave(&host->lock, flags); in sdhci_send_tuning()
2794 mmc->ios.bus_width == MMC_BUS_WIDTH_8) in sdhci_send_tuning()
2808 spin_unlock_irqrestore(&host->lock, flags); in sdhci_send_tuning()
2809 host->tuning_done = 0; in sdhci_send_tuning()
2813 host->cmd = NULL; in sdhci_send_tuning()
2817 host->tuning_done = 0; in sdhci_send_tuning()
2819 spin_unlock_irqrestore(&host->lock, flags); in sdhci_send_tuning()
2822 wait_event_timeout(host->buf_ready_int, (host->tuning_done == 1), in sdhci_send_tuning()
2828 int __sdhci_execute_tuning(struct sdhci_host *host, u32 opcode) in __sdhci_execute_tuning()
2830 int i; in __sdhci_execute_tuning()
2836 for (i = 0; i < host->tuning_loop_count; i++) { in __sdhci_execute_tuning()
2841 if (!host->tuning_done) { in __sdhci_execute_tuning()
2842 pr_debug("%s: Tuning timeout, falling back to fixed sampling clock\n", in __sdhci_execute_tuning()
2843 mmc_hostname(host->mmc)); in __sdhci_execute_tuning()
2845 return -ETIMEDOUT; in __sdhci_execute_tuning()
2849 if (host->tuning_delay > 0) in __sdhci_execute_tuning()
2850 mdelay(host->tuning_delay); in __sdhci_execute_tuning()
2861 pr_info("%s: Tuning failed, falling back to fixed sampling clock\n", in __sdhci_execute_tuning()
2862 mmc_hostname(host->mmc)); in __sdhci_execute_tuning()
2864 return -EAGAIN; in __sdhci_execute_tuning()
2868 int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode) in sdhci_execute_tuning()
2871 int err = 0; in sdhci_execute_tuning()
2872 unsigned int tuning_count = 0; in sdhci_execute_tuning()
2875 hs400_tuning = host->flags & SDHCI_HS400_TUNING; in sdhci_execute_tuning()
2877 if (host->tuning_mode == SDHCI_TUNING_MODE_1) in sdhci_execute_tuning()
2878 tuning_count = host->tuning_count; in sdhci_execute_tuning()
2887 switch (host->timing) { in sdhci_execute_tuning()
2890 err = -EINVAL; in sdhci_execute_tuning()
2895 * Periodic re-tuning for HS400 is not expected to be needed, so in sdhci_execute_tuning()
2907 if (host->flags & SDHCI_SDR50_NEEDS_TUNING) in sdhci_execute_tuning()
2915 if (host->ops->platform_execute_tuning) { in sdhci_execute_tuning()
2916 err = host->ops->platform_execute_tuning(host, opcode); in sdhci_execute_tuning()
2920 mmc->retune_period = tuning_count; in sdhci_execute_tuning()
2922 if (host->tuning_delay < 0) in sdhci_execute_tuning()
2923 host->tuning_delay = opcode == MMC_SEND_TUNING_BLOCK; in sdhci_execute_tuning()
2927 host->tuning_err = __sdhci_execute_tuning(host, opcode); in sdhci_execute_tuning()
2931 host->flags &= ~SDHCI_HS400_TUNING; in sdhci_execute_tuning()
2940 if (host->version < SDHCI_SPEC_300) in sdhci_enable_preset_value()
2947 if (host->preset_enabled != enable) { in sdhci_enable_preset_value()
2958 host->flags |= SDHCI_PV_ENABLED; in sdhci_enable_preset_value()
2960 host->flags &= ~SDHCI_PV_ENABLED; in sdhci_enable_preset_value()
2962 host->preset_enabled = enable; in sdhci_enable_preset_value()
2967 int err) in sdhci_post_req()
2969 struct mmc_data *data = mrq->data; in sdhci_post_req()
2971 if (data->host_cookie != COOKIE_UNMAPPED) in sdhci_post_req()
2972 dma_unmap_sg(mmc_dev(mmc), data->sg, data->sg_len, in sdhci_post_req()
2975 data->host_cookie = COOKIE_UNMAPPED; in sdhci_post_req()
2982 mrq->data->host_cookie = COOKIE_UNMAPPED; in sdhci_pre_req()
2985 * No pre-mapping in the pre hook if we're using the bounce buffer, in sdhci_pre_req()
2989 if (host->flags & SDHCI_REQ_USE_DMA && !host->bounce_buffer) in sdhci_pre_req()
2990 sdhci_pre_dma_transfer(host, mrq->data, COOKIE_PRE_MAPPED); in sdhci_pre_req()
2993 static void sdhci_error_out_mrqs(struct sdhci_host *host, int err) in sdhci_error_out_mrqs()
2995 if (host->data_cmd) { in sdhci_error_out_mrqs()
2996 host->data_cmd->error = err; in sdhci_error_out_mrqs()
2997 sdhci_finish_mrq(host, host->data_cmd->mrq); in sdhci_error_out_mrqs()
3000 if (host->cmd) { in sdhci_error_out_mrqs()
3001 host->cmd->error = err; in sdhci_error_out_mrqs()
3002 sdhci_finish_mrq(host, host->cmd->mrq); in sdhci_error_out_mrqs()
3010 int present; in sdhci_card_event()
3013 if (host->ops->card_event) in sdhci_card_event()
3014 host->ops->card_event(host); in sdhci_card_event()
3016 present = mmc->ops->get_cd(mmc); in sdhci_card_event()
3018 spin_lock_irqsave(&host->lock, flags); in sdhci_card_event()
3029 sdhci_error_out_mrqs(host, -ENOMEDIUM); in sdhci_card_event()
3032 spin_unlock_irqrestore(&host->lock, flags); in sdhci_card_event()
3062 int i; in sdhci_request_done()
3064 spin_lock_irqsave(&host->lock, flags); in sdhci_request_done()
3067 mrq = host->mrqs_done[i]; in sdhci_request_done()
3073 spin_unlock_irqrestore(&host->lock, flags); in sdhci_request_done()
3085 * also be in mrqs_done, otherwise host->cmd and host->data_cmd in sdhci_request_done()
3088 if (host->cmd || host->data_cmd) { in sdhci_request_done()
3089 spin_unlock_irqrestore(&host->lock, flags); in sdhci_request_done()
3094 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) in sdhci_request_done()
3096 host->ops->set_clock(host, host->clock); in sdhci_request_done()
3100 host->pending_reset = false; in sdhci_request_done()
3108 if (host->flags & SDHCI_REQ_USE_DMA) { in sdhci_request_done()
3109 struct mmc_data *data = mrq->data; in sdhci_request_done()
3111 if (host->use_external_dma && data && in sdhci_request_done()
3112 (mrq->cmd->error || data->error)) { in sdhci_request_done()
3115 host->mrqs_done[i] = NULL; in sdhci_request_done()
3116 spin_unlock_irqrestore(&host->lock, flags); in sdhci_request_done()
3118 spin_lock_irqsave(&host->lock, flags); in sdhci_request_done()
3122 if (data && data->host_cookie == COOKIE_MAPPED) { in sdhci_request_done()
3123 if (host->bounce_buffer) { in sdhci_request_done()
3129 unsigned int length = data->bytes_xfered; in sdhci_request_done()
3131 if (length > host->bounce_buffer_size) { in sdhci_request_done()
3133 mmc_hostname(host->mmc), in sdhci_request_done()
3134 host->bounce_buffer_size, in sdhci_request_done()
3135 data->bytes_xfered); in sdhci_request_done()
3137 length = host->bounce_buffer_size; in sdhci_request_done()
3140 mmc_dev(host->mmc), in sdhci_request_done()
3141 host->bounce_addr, in sdhci_request_done()
3142 host->bounce_buffer_size, in sdhci_request_done()
3144 sg_copy_from_buffer(data->sg, in sdhci_request_done()
3145 data->sg_len, in sdhci_request_done()
3146 host->bounce_buffer, in sdhci_request_done()
3151 mmc_dev(host->mmc), in sdhci_request_done()
3152 host->bounce_addr, in sdhci_request_done()
3153 host->bounce_buffer_size, in sdhci_request_done()
3158 dma_unmap_sg(mmc_dev(host->mmc), data->sg, in sdhci_request_done()
3159 data->sg_len, in sdhci_request_done()
3162 data->host_cookie = COOKIE_UNMAPPED; in sdhci_request_done()
3166 host->mrqs_done[i] = NULL; in sdhci_request_done()
3168 spin_unlock_irqrestore(&host->lock, flags); in sdhci_request_done()
3170 if (host->ops->request_done) in sdhci_request_done()
3171 host->ops->request_done(host, mrq); in sdhci_request_done()
3173 mmc_request_done(host->mmc, mrq); in sdhci_request_done()
3194 spin_lock_irqsave(&host->lock, flags); in sdhci_timeout_timer()
3196 if (host->cmd && !sdhci_data_line_cmd(host->cmd)) { in sdhci_timeout_timer()
3198 mmc_hostname(host->mmc)); in sdhci_timeout_timer()
3202 host->cmd->error = -ETIMEDOUT; in sdhci_timeout_timer()
3203 sdhci_finish_mrq(host, host->cmd->mrq); in sdhci_timeout_timer()
3206 spin_unlock_irqrestore(&host->lock, flags); in sdhci_timeout_timer()
3216 spin_lock_irqsave(&host->lock, flags); in sdhci_timeout_data_timer()
3218 if (host->data || host->data_cmd || in sdhci_timeout_data_timer()
3219 (host->cmd && sdhci_data_line_cmd(host->cmd))) { in sdhci_timeout_data_timer()
3221 mmc_hostname(host->mmc)); in sdhci_timeout_data_timer()
3225 if (host->data) { in sdhci_timeout_data_timer()
3226 host->data->error = -ETIMEDOUT; in sdhci_timeout_data_timer()
3228 queue_work(host->complete_wq, &host->complete_work); in sdhci_timeout_data_timer()
3229 } else if (host->data_cmd) { in sdhci_timeout_data_timer()
3230 host->data_cmd->error = -ETIMEDOUT; in sdhci_timeout_data_timer()
3231 sdhci_finish_mrq(host, host->data_cmd->mrq); in sdhci_timeout_data_timer()
3233 host->cmd->error = -ETIMEDOUT; in sdhci_timeout_data_timer()
3234 sdhci_finish_mrq(host, host->cmd->mrq); in sdhci_timeout_data_timer()
3238 spin_unlock_irqrestore(&host->lock, flags); in sdhci_timeout_data_timer()
3249 /* Handle auto-CMD12 error */ in sdhci_cmd_irq()
3250 if (intmask & SDHCI_INT_AUTO_CMD_ERR && host->data_cmd) { in sdhci_cmd_irq()
3251 struct mmc_request *mrq = host->data_cmd->mrq; in sdhci_cmd_irq()
3253 int data_err_bit = (auto_cmd_status & SDHCI_AUTO_CMD_TIMEOUT) ? in sdhci_cmd_irq()
3257 /* Treat auto-CMD12 error the same as data error */ in sdhci_cmd_irq()
3258 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) { in sdhci_cmd_irq()
3264 if (!host->cmd) { in sdhci_cmd_irq()
3270 if (host->pending_reset) in sdhci_cmd_irq()
3273 mmc_hostname(host->mmc), (unsigned)intmask); in sdhci_cmd_irq()
3282 host->cmd->error = -ETIMEDOUT; in sdhci_cmd_irq()
3285 host->cmd->error = -EILSEQ; in sdhci_cmd_irq()
3286 if (!mmc_op_tuning(host->cmd->opcode)) in sdhci_cmd_irq()
3290 if (host->cmd->data && in sdhci_cmd_irq()
3293 host->cmd = NULL; in sdhci_cmd_irq()
3298 __sdhci_finish_mrq(host, host->cmd->mrq); in sdhci_cmd_irq()
3302 /* Handle auto-CMD23 error */ in sdhci_cmd_irq()
3304 struct mmc_request *mrq = host->cmd->mrq; in sdhci_cmd_irq()
3306 int err = (auto_cmd_status & SDHCI_AUTO_CMD_TIMEOUT) ? in sdhci_cmd_irq()
3307 -ETIMEDOUT : in sdhci_cmd_irq()
3308 -EILSEQ; in sdhci_cmd_irq()
3313 mrq->sbc->error = err; in sdhci_cmd_irq()
3325 void *desc = host->adma_table; in sdhci_adma_show_error()
3326 dma_addr_t dma = host->adma_addr; in sdhci_adma_show_error()
3333 if (host->flags & SDHCI_USE_64_BIT_DMA) in sdhci_adma_show_error()
3336 le32_to_cpu(dma_desc->addr_hi), in sdhci_adma_show_error()
3337 le32_to_cpu(dma_desc->addr_lo), in sdhci_adma_show_error()
3338 le16_to_cpu(dma_desc->len), in sdhci_adma_show_error()
3339 le16_to_cpu(dma_desc->cmd)); in sdhci_adma_show_error()
3343 le32_to_cpu(dma_desc->addr_lo), in sdhci_adma_show_error()
3344 le16_to_cpu(dma_desc->len), in sdhci_adma_show_error()
3345 le16_to_cpu(dma_desc->cmd)); in sdhci_adma_show_error()
3347 desc += host->desc_sz; in sdhci_adma_show_error()
3348 dma += host->desc_sz; in sdhci_adma_show_error()
3350 if (dma_desc->cmd & cpu_to_le16(ADMA2_END)) in sdhci_adma_show_error()
3364 if (intmask & SDHCI_INT_DATA_AVAIL && !host->data) { in sdhci_data_irq()
3366 host->tuning_done = 1; in sdhci_data_irq()
3367 wake_up(&host->buf_ready_int); in sdhci_data_irq()
3372 if (!host->data) { in sdhci_data_irq()
3373 struct mmc_command *data_cmd = host->data_cmd; in sdhci_data_irq()
3380 if (data_cmd && (data_cmd->flags & MMC_RSP_BUSY)) { in sdhci_data_irq()
3382 host->data_cmd = NULL; in sdhci_data_irq()
3383 data_cmd->error = -ETIMEDOUT; in sdhci_data_irq()
3385 __sdhci_finish_mrq(host, data_cmd->mrq); in sdhci_data_irq()
3389 host->data_cmd = NULL; in sdhci_data_irq()
3391 * Some cards handle busy-end interrupt in sdhci_data_irq()
3395 if (host->cmd == data_cmd) in sdhci_data_irq()
3398 __sdhci_finish_mrq(host, data_cmd->mrq); in sdhci_data_irq()
3408 if (host->pending_reset) in sdhci_data_irq()
3412 mmc_hostname(host->mmc), (unsigned)intmask); in sdhci_data_irq()
3420 host->data->error = -ETIMEDOUT; in sdhci_data_irq()
3423 host->data->error = -EILSEQ; in sdhci_data_irq()
3429 host->data->error = -EILSEQ; in sdhci_data_irq()
3439 pr_err("%s: ADMA error: 0x%08x\n", mmc_hostname(host->mmc), in sdhci_data_irq()
3443 host->data->error = -EIO; in sdhci_data_irq()
3444 if (host->ops->adma_workaround) in sdhci_data_irq()
3445 host->ops->adma_workaround(host, intmask); in sdhci_data_irq()
3448 if (host->data->error) in sdhci_data_irq()
3467 dmanow = dmastart + host->data->bytes_xfered; in sdhci_data_irq()
3472 ~((dma_addr_t)SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) + in sdhci_data_irq()
3474 host->data->bytes_xfered = dmanow - dmastart; in sdhci_data_irq()
3476 &dmastart, host->data->bytes_xfered, &dmanow); in sdhci_data_irq()
3481 if (host->cmd == host->data_cmd) { in sdhci_data_irq()
3487 host->data_early = 1; in sdhci_data_irq()
3498 struct mmc_data *data = mrq->data; in sdhci_defer_done()
3500 return host->pending_reset || host->always_defer_done || in sdhci_defer_done()
3501 ((host->flags & SDHCI_REQ_USE_DMA) && data && in sdhci_defer_done()
3502 data->host_cookie == COOKIE_MAPPED); in sdhci_defer_done()
3505 static irqreturn_t sdhci_irq(int irq, void *dev_id) in sdhci_irq()
3511 int max_loops = 16; in sdhci_irq()
3512 int i; in sdhci_irq()
3514 spin_lock(&host->lock); in sdhci_irq()
3516 if (host->runtime_suspended) { in sdhci_irq()
3517 spin_unlock(&host->lock); in sdhci_irq()
3530 if (host->ops->irq) { in sdhci_irq()
3531 intmask = host->ops->irq(host, intmask); in sdhci_irq()
3556 host->ier &= ~(SDHCI_INT_CARD_INSERT | in sdhci_irq()
3558 host->ier |= present ? SDHCI_INT_CARD_REMOVE : in sdhci_irq()
3560 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE); in sdhci_irq()
3561 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE); in sdhci_irq()
3566 host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT | in sdhci_irq()
3579 mmc_hostname(host->mmc)); in sdhci_irq()
3582 mmc_retune_needed(host->mmc); in sdhci_irq()
3585 (host->ier & SDHCI_INT_CARD_INT)) { in sdhci_irq()
3587 sdio_signal_irq(host->mmc); in sdhci_irq()
3604 } while (intmask && --max_loops); in sdhci_irq()
3608 struct mmc_request *mrq = host->mrqs_done[i]; in sdhci_irq()
3617 host->mrqs_done[i] = NULL; in sdhci_irq()
3621 if (host->deferred_cmd) in sdhci_irq()
3624 spin_unlock(&host->lock); in sdhci_irq()
3631 if (host->ops->request_done) in sdhci_irq()
3632 host->ops->request_done(host, mrqs_done[i]); in sdhci_irq()
3634 mmc_request_done(host->mmc, mrqs_done[i]); in sdhci_irq()
3639 mmc_hostname(host->mmc), unexpected); in sdhci_irq()
3647 static irqreturn_t sdhci_thread_irq(int irq, void *dev_id) in sdhci_thread_irq()
3657 spin_lock_irqsave(&host->lock, flags); in sdhci_thread_irq()
3659 isr = host->thread_isr; in sdhci_thread_irq()
3660 host->thread_isr = 0; in sdhci_thread_irq()
3662 cmd = host->deferred_cmd; in sdhci_thread_irq()
3664 sdhci_finish_mrq(host, cmd->mrq); in sdhci_thread_irq()
3666 spin_unlock_irqrestore(&host->lock, flags); in sdhci_thread_irq()
3669 struct mmc_host *mmc = host->mmc; in sdhci_thread_irq()
3671 mmc->ops->card_event(mmc); in sdhci_thread_irq()
3688 return mmc_card_is_removable(host->mmc) && in sdhci_cd_irq_can_wakeup()
3689 !(host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) && in sdhci_cd_irq_can_wakeup()
3690 !mmc_can_gpio_cd(host->mmc); in sdhci_cd_irq_can_wakeup()
3695 * the Interrupt Status Enable register too. See 'Table 1-6: Wakeup Signal
3714 if (mmc_card_wake_sdio_irq(host->mmc)) { in sdhci_enable_irq_wakeups()
3729 host->irq_wake_enabled = !enable_irq_wake(host->irq); in sdhci_enable_irq_wakeups()
3731 return host->irq_wake_enabled; in sdhci_enable_irq_wakeups()
3744 disable_irq_wake(host->irq); in sdhci_disable_irq_wakeups()
3746 host->irq_wake_enabled = false; in sdhci_disable_irq_wakeups()
3749 int sdhci_suspend_host(struct sdhci_host *host) in sdhci_suspend_host()
3753 mmc_retune_timer_stop(host->mmc); in sdhci_suspend_host()
3755 if (!device_may_wakeup(mmc_dev(host->mmc)) || in sdhci_suspend_host()
3757 host->ier = 0; in sdhci_suspend_host()
3760 free_irq(host->irq, host); in sdhci_suspend_host()
3768 int sdhci_resume_host(struct sdhci_host *host) in sdhci_resume_host()
3770 struct mmc_host *mmc = host->mmc; in sdhci_resume_host()
3771 int ret = 0; in sdhci_resume_host()
3773 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) { in sdhci_resume_host()
3774 if (host->ops->enable_dma) in sdhci_resume_host()
3775 host->ops->enable_dma(host); in sdhci_resume_host()
3778 if ((mmc->pm_flags & MMC_PM_KEEP_POWER) && in sdhci_resume_host()
3779 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) { in sdhci_resume_host()
3782 host->pwr = 0; in sdhci_resume_host()
3783 host->clock = 0; in sdhci_resume_host()
3784 host->reinit_uhs = true; in sdhci_resume_host()
3785 mmc->ops->set_ios(mmc, &mmc->ios); in sdhci_resume_host()
3787 sdhci_init(host, (mmc->pm_flags & MMC_PM_KEEP_POWER)); in sdhci_resume_host()
3790 if (host->irq_wake_enabled) { in sdhci_resume_host()
3793 ret = request_threaded_irq(host->irq, sdhci_irq, in sdhci_resume_host()
3807 int sdhci_runtime_suspend_host(struct sdhci_host *host) in sdhci_runtime_suspend_host()
3811 mmc_retune_timer_stop(host->mmc); in sdhci_runtime_suspend_host()
3813 spin_lock_irqsave(&host->lock, flags); in sdhci_runtime_suspend_host()
3814 host->ier &= SDHCI_INT_CARD_INT; in sdhci_runtime_suspend_host()
3815 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE); in sdhci_runtime_suspend_host()
3816 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE); in sdhci_runtime_suspend_host()
3817 spin_unlock_irqrestore(&host->lock, flags); in sdhci_runtime_suspend_host()
3819 synchronize_hardirq(host->irq); in sdhci_runtime_suspend_host()
3821 spin_lock_irqsave(&host->lock, flags); in sdhci_runtime_suspend_host()
3822 host->runtime_suspended = true; in sdhci_runtime_suspend_host()
3823 spin_unlock_irqrestore(&host->lock, flags); in sdhci_runtime_suspend_host()
3829 int sdhci_runtime_resume_host(struct sdhci_host *host, int soft_reset) in sdhci_runtime_resume_host()
3831 struct mmc_host *mmc = host->mmc; in sdhci_runtime_resume_host()
3833 int host_flags = host->flags; in sdhci_runtime_resume_host()
3836 if (host->ops->enable_dma) in sdhci_runtime_resume_host()
3837 host->ops->enable_dma(host); in sdhci_runtime_resume_host()
3842 if (mmc->ios.power_mode != MMC_POWER_UNDEFINED && in sdhci_runtime_resume_host()
3843 mmc->ios.power_mode != MMC_POWER_OFF) { in sdhci_runtime_resume_host()
3844 /* Force clock and power re-program */ in sdhci_runtime_resume_host()
3845 host->pwr = 0; in sdhci_runtime_resume_host()
3846 host->clock = 0; in sdhci_runtime_resume_host()
3847 host->reinit_uhs = true; in sdhci_runtime_resume_host()
3848 mmc->ops->start_signal_voltage_switch(mmc, &mmc->ios); in sdhci_runtime_resume_host()
3849 mmc->ops->set_ios(mmc, &mmc->ios); in sdhci_runtime_resume_host()
3852 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) { in sdhci_runtime_resume_host()
3853 spin_lock_irqsave(&host->lock, flags); in sdhci_runtime_resume_host()
3855 spin_unlock_irqrestore(&host->lock, flags); in sdhci_runtime_resume_host()
3858 if ((mmc->caps2 & MMC_CAP2_HS400_ES) && in sdhci_runtime_resume_host()
3859 mmc->ops->hs400_enhanced_strobe) in sdhci_runtime_resume_host()
3860 mmc->ops->hs400_enhanced_strobe(mmc, &mmc->ios); in sdhci_runtime_resume_host()
3863 spin_lock_irqsave(&host->lock, flags); in sdhci_runtime_resume_host()
3865 host->runtime_suspended = false; in sdhci_runtime_resume_host()
3874 spin_unlock_irqrestore(&host->lock, flags); in sdhci_runtime_resume_host()
3894 spin_lock_irqsave(&host->lock, flags); in sdhci_cqe_enable()
3903 if (host->v4_mode && (host->caps1 & SDHCI_CAN_DO_ADMA3)) in sdhci_cqe_enable()
3905 else if (host->flags & SDHCI_USE_64_BIT_DMA) in sdhci_cqe_enable()
3911 sdhci_writew(host, SDHCI_MAKE_BLKSZ(host->sdma_boundary, 512), in sdhci_cqe_enable()
3917 host->ier = host->cqe_ier; in sdhci_cqe_enable()
3919 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE); in sdhci_cqe_enable()
3920 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE); in sdhci_cqe_enable()
3922 host->cqe_on = true; in sdhci_cqe_enable()
3925 mmc_hostname(mmc), host->ier, in sdhci_cqe_enable()
3928 spin_unlock_irqrestore(&host->lock, flags); in sdhci_cqe_enable()
3937 spin_lock_irqsave(&host->lock, flags); in sdhci_cqe_disable()
3941 host->cqe_on = false; in sdhci_cqe_disable()
3947 mmc_hostname(mmc), host->ier, in sdhci_cqe_disable()
3950 spin_unlock_irqrestore(&host->lock, flags); in sdhci_cqe_disable()
3954 bool sdhci_cqe_irq(struct sdhci_host *host, u32 intmask, int *cmd_error, in sdhci_cqe_irq()
3955 int *data_error) in sdhci_cqe_irq()
3959 if (!host->cqe_on) in sdhci_cqe_irq()
3963 *cmd_error = -EILSEQ; in sdhci_cqe_irq()
3967 *cmd_error = -ETIMEDOUT; in sdhci_cqe_irq()
3973 *data_error = -EILSEQ; in sdhci_cqe_irq()
3977 *data_error = -ETIMEDOUT; in sdhci_cqe_irq()
3980 *data_error = -EIO; in sdhci_cqe_irq()
3986 mask = intmask & host->cqe_ier; in sdhci_cqe_irq()
3991 mmc_hostname(host->mmc)); in sdhci_cqe_irq()
3993 intmask &= ~(host->cqe_ier | SDHCI_INT_ERROR); in sdhci_cqe_irq()
3997 mmc_hostname(host->mmc), intmask); in sdhci_cqe_irq()
4022 return ERR_PTR(-ENOMEM); in sdhci_alloc_host()
4025 host->mmc = mmc; in sdhci_alloc_host()
4026 host->mmc_host_ops = sdhci_ops; in sdhci_alloc_host()
4027 mmc->ops = &host->mmc_host_ops; in sdhci_alloc_host()
4029 host->flags = SDHCI_SIGNALING_330; in sdhci_alloc_host()
4031 host->cqe_ier = SDHCI_CQE_INT_MASK; in sdhci_alloc_host()
4032 host->cqe_err_ier = SDHCI_CQE_INT_ERR_MASK; in sdhci_alloc_host()
4034 host->tuning_delay = -1; in sdhci_alloc_host()
4035 host->tuning_loop_count = MAX_TUNING_LOOP; in sdhci_alloc_host()
4037 host->sdma_boundary = SDHCI_DEFAULT_BOUNDARY_ARG; in sdhci_alloc_host()
4044 host->adma_table_cnt = SDHCI_MAX_SEGS * 2 + 1; in sdhci_alloc_host()
4045 host->max_adma = 65536; in sdhci_alloc_host()
4047 host->max_timeout_count = 0xE; in sdhci_alloc_host()
4054 static int sdhci_set_dma_mask(struct sdhci_host *host) in sdhci_set_dma_mask()
4056 struct mmc_host *mmc = host->mmc; in sdhci_set_dma_mask()
4058 int ret = -EINVAL; in sdhci_set_dma_mask()
4060 if (host->quirks2 & SDHCI_QUIRK2_BROKEN_64_BIT_DMA) in sdhci_set_dma_mask()
4061 host->flags &= ~SDHCI_USE_64_BIT_DMA; in sdhci_set_dma_mask()
4063 /* Try 64-bit mask if hardware is capable of it */ in sdhci_set_dma_mask()
4064 if (host->flags & SDHCI_USE_64_BIT_DMA) { in sdhci_set_dma_mask()
4067 pr_warn("%s: Failed to set 64-bit DMA mask.\n", in sdhci_set_dma_mask()
4069 host->flags &= ~SDHCI_USE_64_BIT_DMA; in sdhci_set_dma_mask()
4073 /* 32-bit mask as default & fallback */ in sdhci_set_dma_mask()
4077 pr_warn("%s: Failed to set 32-bit DMA mask.\n", in sdhci_set_dma_mask()
4091 if (host->read_caps) in __sdhci_read_caps()
4094 host->read_caps = true; in __sdhci_read_caps()
4097 host->quirks = debug_quirks; in __sdhci_read_caps()
4100 host->quirks2 = debug_quirks2; in __sdhci_read_caps()
4104 if (host->v4_mode) in __sdhci_read_caps()
4107 device_property_read_u64(mmc_dev(host->mmc), in __sdhci_read_caps()
4108 "sdhci-caps-mask", &dt_caps_mask); in __sdhci_read_caps()
4109 device_property_read_u64(mmc_dev(host->mmc), in __sdhci_read_caps()
4110 "sdhci-caps", &dt_caps); in __sdhci_read_caps()
4113 host->version = (v & SDHCI_SPEC_VER_MASK) >> SDHCI_SPEC_VER_SHIFT; in __sdhci_read_caps()
4116 host->caps = *caps; in __sdhci_read_caps()
4118 host->caps = sdhci_readl(host, SDHCI_CAPABILITIES); in __sdhci_read_caps()
4119 host->caps &= ~lower_32_bits(dt_caps_mask); in __sdhci_read_caps()
4120 host->caps |= lower_32_bits(dt_caps); in __sdhci_read_caps()
4123 if (host->version < SDHCI_SPEC_300) in __sdhci_read_caps()
4127 host->caps1 = *caps1; in __sdhci_read_caps()
4129 host->caps1 = sdhci_readl(host, SDHCI_CAPABILITIES_1); in __sdhci_read_caps()
4130 host->caps1 &= ~upper_32_bits(dt_caps_mask); in __sdhci_read_caps()
4131 host->caps1 |= upper_32_bits(dt_caps); in __sdhci_read_caps()
4138 struct mmc_host *mmc = host->mmc; in sdhci_allocate_bounce_buffer()
4139 unsigned int max_blocks; in sdhci_allocate_bounce_buffer()
4140 unsigned int bounce_size; in sdhci_allocate_bounce_buffer()
4141 int ret; in sdhci_allocate_bounce_buffer()
4154 if (mmc->max_req_size < bounce_size) in sdhci_allocate_bounce_buffer()
4155 bounce_size = mmc->max_req_size; in sdhci_allocate_bounce_buffer()
4163 host->bounce_buffer = devm_kmalloc(mmc_dev(mmc), in sdhci_allocate_bounce_buffer()
4166 if (!host->bounce_buffer) { in sdhci_allocate_bounce_buffer()
4172 * mmc->max_segs == 1. in sdhci_allocate_bounce_buffer()
4177 host->bounce_addr = dma_map_single(mmc_dev(mmc), in sdhci_allocate_bounce_buffer()
4178 host->bounce_buffer, in sdhci_allocate_bounce_buffer()
4181 ret = dma_mapping_error(mmc_dev(mmc), host->bounce_addr); in sdhci_allocate_bounce_buffer()
4183 devm_kfree(mmc_dev(mmc), host->bounce_buffer); in sdhci_allocate_bounce_buffer()
4184 host->bounce_buffer = NULL; in sdhci_allocate_bounce_buffer()
4189 host->bounce_buffer_size = bounce_size; in sdhci_allocate_bounce_buffer()
4192 mmc->max_segs = max_blocks; in sdhci_allocate_bounce_buffer()
4193 mmc->max_seg_size = bounce_size; in sdhci_allocate_bounce_buffer()
4194 mmc->max_req_size = bounce_size; in sdhci_allocate_bounce_buffer()
4204 * version 4.10 in Capabilities Register is used as 64-bit System in sdhci_can_64bit_dma()
4207 if (host->version >= SDHCI_SPEC_410 && host->v4_mode) in sdhci_can_64bit_dma()
4208 return host->caps & SDHCI_CAN_64BIT_V4; in sdhci_can_64bit_dma()
4210 return host->caps & SDHCI_CAN_64BIT; in sdhci_can_64bit_dma()
4213 int sdhci_setup_host(struct sdhci_host *host) in sdhci_setup_host()
4217 unsigned int ocr_avail; in sdhci_setup_host()
4218 unsigned int override_timeout_clk; in sdhci_setup_host()
4220 int ret = 0; in sdhci_setup_host()
4225 return -EINVAL; in sdhci_setup_host()
4227 mmc = host->mmc; in sdhci_setup_host()
4235 if (!mmc->supply.vqmmc) { in sdhci_setup_host()
4251 override_timeout_clk = host->timeout_clk; in sdhci_setup_host()
4253 if (host->version > SDHCI_SPEC_420) { in sdhci_setup_host()
4255 mmc_hostname(mmc), host->version); in sdhci_setup_host()
4258 if (host->quirks & SDHCI_QUIRK_FORCE_DMA) in sdhci_setup_host()
4259 host->flags |= SDHCI_USE_SDMA; in sdhci_setup_host()
4260 else if (!(host->caps & SDHCI_CAN_DO_SDMA)) in sdhci_setup_host()
4263 host->flags |= SDHCI_USE_SDMA; in sdhci_setup_host()
4265 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) && in sdhci_setup_host()
4266 (host->flags & SDHCI_USE_SDMA)) { in sdhci_setup_host()
4267 DBG("Disabling DMA as it is marked broken\n"); in sdhci_setup_host()
4268 host->flags &= ~SDHCI_USE_SDMA; in sdhci_setup_host()
4271 if ((host->version >= SDHCI_SPEC_200) && in sdhci_setup_host()
4272 (host->caps & SDHCI_CAN_DO_ADMA2)) in sdhci_setup_host()
4273 host->flags |= SDHCI_USE_ADMA; in sdhci_setup_host()
4275 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) && in sdhci_setup_host()
4276 (host->flags & SDHCI_USE_ADMA)) { in sdhci_setup_host()
4277 DBG("Disabling ADMA as it is marked broken\n"); in sdhci_setup_host()
4278 host->flags &= ~SDHCI_USE_ADMA; in sdhci_setup_host()
4282 host->flags |= SDHCI_USE_64_BIT_DMA; in sdhci_setup_host()
4284 if (host->use_external_dma) { in sdhci_setup_host()
4286 if (ret == -EPROBE_DEFER) in sdhci_setup_host()
4296 host->flags &= ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA); in sdhci_setup_host()
4299 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) { in sdhci_setup_host()
4300 if (host->ops->set_dma_mask) in sdhci_setup_host()
4301 ret = host->ops->set_dma_mask(host); in sdhci_setup_host()
4305 if (!ret && host->ops->enable_dma) in sdhci_setup_host()
4306 ret = host->ops->enable_dma(host); in sdhci_setup_host()
4309 pr_warn("%s: No suitable DMA available - falling back to PIO\n", in sdhci_setup_host()
4311 host->flags &= ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA); in sdhci_setup_host()
4317 /* SDMA does not support 64-bit DMA if v4 mode not set */ in sdhci_setup_host()
4318 if ((host->flags & SDHCI_USE_64_BIT_DMA) && !host->v4_mode) in sdhci_setup_host()
4319 host->flags &= ~SDHCI_USE_SDMA; in sdhci_setup_host()
4321 if (host->flags & SDHCI_USE_ADMA) { in sdhci_setup_host()
4325 if (!(host->flags & SDHCI_USE_64_BIT_DMA)) in sdhci_setup_host()
4326 host->alloc_desc_sz = SDHCI_ADMA2_32_DESC_SZ; in sdhci_setup_host()
4327 else if (!host->alloc_desc_sz) in sdhci_setup_host()
4328 host->alloc_desc_sz = SDHCI_ADMA2_64_DESC_SZ(host); in sdhci_setup_host()
4330 host->desc_sz = host->alloc_desc_sz; in sdhci_setup_host()
4331 host->adma_table_sz = host->adma_table_cnt * host->desc_sz; in sdhci_setup_host()
4333 host->align_buffer_sz = SDHCI_MAX_SEGS * SDHCI_ADMA2_ALIGN; in sdhci_setup_host()
4335 * Use zalloc to zero the reserved high 32-bits of 128-bit in sdhci_setup_host()
4339 host->align_buffer_sz + host->adma_table_sz, in sdhci_setup_host()
4342 pr_warn("%s: Unable to allocate ADMA buffers - falling back to standard DMA\n", in sdhci_setup_host()
4344 host->flags &= ~SDHCI_USE_ADMA; in sdhci_setup_host()
4345 } else if ((dma + host->align_buffer_sz) & in sdhci_setup_host()
4346 (SDHCI_ADMA2_DESC_ALIGN - 1)) { in sdhci_setup_host()
4349 host->flags &= ~SDHCI_USE_ADMA; in sdhci_setup_host()
4350 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz + in sdhci_setup_host()
4351 host->adma_table_sz, buf, dma); in sdhci_setup_host()
4353 host->align_buffer = buf; in sdhci_setup_host()
4354 host->align_addr = dma; in sdhci_setup_host()
4356 host->adma_table = buf + host->align_buffer_sz; in sdhci_setup_host()
4357 host->adma_addr = dma + host->align_buffer_sz; in sdhci_setup_host()
4366 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) { in sdhci_setup_host()
4367 host->dma_mask = DMA_BIT_MASK(64); in sdhci_setup_host()
4368 mmc_dev(mmc)->dma_mask = &host->dma_mask; in sdhci_setup_host()
4371 if (host->version >= SDHCI_SPEC_300) in sdhci_setup_host()
4372 host->max_clk = FIELD_GET(SDHCI_CLOCK_V3_BASE_MASK, host->caps); in sdhci_setup_host()
4374 host->max_clk = FIELD_GET(SDHCI_CLOCK_BASE_MASK, host->caps); in sdhci_setup_host()
4376 host->max_clk *= 1000000; in sdhci_setup_host()
4377 if (host->max_clk == 0 || host->quirks & in sdhci_setup_host()
4379 if (!host->ops->get_max_clock) { in sdhci_setup_host()
4380 pr_err("%s: Hardware doesn't specify base clock frequency.\n", in sdhci_setup_host()
4382 ret = -ENODEV; in sdhci_setup_host()
4385 host->max_clk = host->ops->get_max_clock(host); in sdhci_setup_host()
4389 * In case of Host Controller v3.00, find out whether clock in sdhci_setup_host()
4392 host->clk_mul = FIELD_GET(SDHCI_CLOCK_MUL_MASK, host->caps1); in sdhci_setup_host()
4395 * In case the value in Clock Multiplier is 0, then programmable in sdhci_setup_host()
4396 * clock mode is not supported, otherwise the actual clock in sdhci_setup_host()
4397 * multiplier is one more than the value of Clock Multiplier in sdhci_setup_host()
4400 if (host->clk_mul) in sdhci_setup_host()
4401 host->clk_mul += 1; in sdhci_setup_host()
4406 max_clk = host->max_clk; in sdhci_setup_host()
4408 if (host->ops->get_min_clock) in sdhci_setup_host()
4409 mmc->f_min = host->ops->get_min_clock(host); in sdhci_setup_host()
4410 else if (host->version >= SDHCI_SPEC_300) { in sdhci_setup_host()
4411 if (host->clk_mul) in sdhci_setup_host()
4412 max_clk = host->max_clk * host->clk_mul; in sdhci_setup_host()
4414 * Divided Clock Mode minimum clock rate is always less than in sdhci_setup_host()
4415 * Programmable Clock Mode minimum clock rate. in sdhci_setup_host()
4417 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300; in sdhci_setup_host()
4419 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200; in sdhci_setup_host()
4421 if (!mmc->f_max || mmc->f_max > max_clk) in sdhci_setup_host()
4422 mmc->f_max = max_clk; in sdhci_setup_host()
4424 if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) { in sdhci_setup_host()
4425 host->timeout_clk = FIELD_GET(SDHCI_TIMEOUT_CLK_MASK, host->caps); in sdhci_setup_host()
4427 if (host->caps & SDHCI_TIMEOUT_CLK_UNIT) in sdhci_setup_host()
4428 host->timeout_clk *= 1000; in sdhci_setup_host()
4430 if (host->timeout_clk == 0) { in sdhci_setup_host()
4431 if (!host->ops->get_timeout_clock) { in sdhci_setup_host()
4432 pr_err("%s: Hardware doesn't specify timeout clock frequency.\n", in sdhci_setup_host()
4434 ret = -ENODEV; in sdhci_setup_host()
4438 host->timeout_clk = in sdhci_setup_host()
4439 DIV_ROUND_UP(host->ops->get_timeout_clock(host), in sdhci_setup_host()
4444 host->timeout_clk = override_timeout_clk; in sdhci_setup_host()
4446 mmc->max_busy_timeout = host->ops->get_max_timeout_count ? in sdhci_setup_host()
4447 host->ops->get_max_timeout_count(host) : 1 << 27; in sdhci_setup_host()
4448 mmc->max_busy_timeout /= host->timeout_clk; in sdhci_setup_host()
4451 if (host->quirks2 & SDHCI_QUIRK2_DISABLE_HW_TIMEOUT && in sdhci_setup_host()
4452 !host->ops->get_max_timeout_count) in sdhci_setup_host()
4453 mmc->max_busy_timeout = 0; in sdhci_setup_host()
4455 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_CMD23; in sdhci_setup_host()
4456 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD; in sdhci_setup_host()
4458 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12) in sdhci_setup_host()
4459 host->flags |= SDHCI_AUTO_CMD12; in sdhci_setup_host()
4462 * For v3 mode, Auto-CMD23 stuff only works in ADMA or PIO. in sdhci_setup_host()
4463 * For v4 mode, SDMA may use Auto-CMD23 as well. in sdhci_setup_host()
4465 if ((host->version >= SDHCI_SPEC_300) && in sdhci_setup_host()
4466 ((host->flags & SDHCI_USE_ADMA) || in sdhci_setup_host()
4467 !(host->flags & SDHCI_USE_SDMA) || host->v4_mode) && in sdhci_setup_host()
4468 !(host->quirks2 & SDHCI_QUIRK2_ACMD23_BROKEN)) { in sdhci_setup_host()
4469 host->flags |= SDHCI_AUTO_CMD23; in sdhci_setup_host()
4470 DBG("Auto-CMD23 available\n"); in sdhci_setup_host()
4472 DBG("Auto-CMD23 unavailable\n"); in sdhci_setup_host()
4476 * A controller may support 8-bit width, but the board itself in sdhci_setup_host()
4478 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in in sdhci_setup_host()
4480 * won't assume 8-bit width for hosts without that CAP. in sdhci_setup_host()
4482 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA)) in sdhci_setup_host()
4483 mmc->caps |= MMC_CAP_4_BIT_DATA; in sdhci_setup_host()
4485 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23) in sdhci_setup_host()
4486 mmc->caps &= ~MMC_CAP_CMD23; in sdhci_setup_host()
4488 if (host->caps & SDHCI_CAN_DO_HISPD) in sdhci_setup_host()
4489 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED; in sdhci_setup_host()
4491 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) && in sdhci_setup_host()
4494 mmc->caps |= MMC_CAP_NEEDS_POLL; in sdhci_setup_host()
4496 if (!IS_ERR(mmc->supply.vqmmc)) { in sdhci_setup_host()
4498 ret = regulator_enable(mmc->supply.vqmmc); in sdhci_setup_host()
4499 host->sdhci_core_to_disable_vqmmc = !ret; in sdhci_setup_host()
4503 if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000, in sdhci_setup_host()
4505 host->caps1 &= ~(SDHCI_SUPPORT_SDR104 | in sdhci_setup_host()
4510 if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 2700000, in sdhci_setup_host()
4512 host->flags &= ~SDHCI_SIGNALING_330; in sdhci_setup_host()
4517 mmc->supply.vqmmc = ERR_PTR(-EINVAL); in sdhci_setup_host()
4522 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V) { in sdhci_setup_host()
4523 host->caps1 &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 | in sdhci_setup_host()
4527 * (indicated using mmc-hs200-1_8v/mmc-hs400-1_8v dt property), in sdhci_setup_host()
4533 mmc->caps2 &= ~(MMC_CAP2_HSX00_1_8V | MMC_CAP2_HS400_ES); in sdhci_setup_host()
4534 mmc->caps &= ~(MMC_CAP_1_8V_DDR | MMC_CAP_UHS); in sdhci_setup_host()
4537 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */ in sdhci_setup_host()
4538 if (host->caps1 & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 | in sdhci_setup_host()
4540 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25; in sdhci_setup_host()
4543 if (host->caps1 & SDHCI_SUPPORT_SDR104) { in sdhci_setup_host()
4544 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50; in sdhci_setup_host()
4548 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200)) in sdhci_setup_host()
4549 mmc->caps2 |= MMC_CAP2_HS200; in sdhci_setup_host()
4550 } else if (host->caps1 & SDHCI_SUPPORT_SDR50) { in sdhci_setup_host()
4551 mmc->caps |= MMC_CAP_UHS_SDR50; in sdhci_setup_host()
4554 if (host->quirks2 & SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 && in sdhci_setup_host()
4555 (host->caps1 & SDHCI_SUPPORT_HS400)) in sdhci_setup_host()
4556 mmc->caps2 |= MMC_CAP2_HS400; in sdhci_setup_host()
4558 if ((mmc->caps2 & MMC_CAP2_HSX00_1_2V) && in sdhci_setup_host()
4559 (IS_ERR(mmc->supply.vqmmc) || in sdhci_setup_host()
4560 !regulator_is_supported_voltage(mmc->supply.vqmmc, 1100000, in sdhci_setup_host()
4562 mmc->caps2 &= ~MMC_CAP2_HSX00_1_2V; in sdhci_setup_host()
4564 if ((host->caps1 & SDHCI_SUPPORT_DDR50) && in sdhci_setup_host()
4565 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50)) in sdhci_setup_host()
4566 mmc->caps |= MMC_CAP_UHS_DDR50; in sdhci_setup_host()
4569 if (host->caps1 & SDHCI_USE_SDR50_TUNING) in sdhci_setup_host()
4570 host->flags |= SDHCI_SDR50_NEEDS_TUNING; in sdhci_setup_host()
4573 if (host->caps1 & SDHCI_DRIVER_TYPE_A) in sdhci_setup_host()
4574 mmc->caps |= MMC_CAP_DRIVER_TYPE_A; in sdhci_setup_host()
4575 if (host->caps1 & SDHCI_DRIVER_TYPE_C) in sdhci_setup_host()
4576 mmc->caps |= MMC_CAP_DRIVER_TYPE_C; in sdhci_setup_host()
4577 if (host->caps1 & SDHCI_DRIVER_TYPE_D) in sdhci_setup_host()
4578 mmc->caps |= MMC_CAP_DRIVER_TYPE_D; in sdhci_setup_host()
4580 /* Initial value for re-tuning timer count */ in sdhci_setup_host()
4581 host->tuning_count = FIELD_GET(SDHCI_RETUNING_TIMER_COUNT_MASK, in sdhci_setup_host()
4582 host->caps1); in sdhci_setup_host()
4585 * In case Re-tuning Timer is not disabled, the actual value of in sdhci_setup_host()
4586 * re-tuning timer will be 2 ^ (n - 1). in sdhci_setup_host()
4588 if (host->tuning_count) in sdhci_setup_host()
4589 host->tuning_count = 1 << (host->tuning_count - 1); in sdhci_setup_host()
4591 /* Re-tuning mode supported by the Host Controller */ in sdhci_setup_host()
4592 host->tuning_mode = FIELD_GET(SDHCI_RETUNING_MODE_MASK, host->caps1); in sdhci_setup_host()
4604 if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) { in sdhci_setup_host()
4605 int curr = regulator_get_current_limit(mmc->supply.vmmc); in sdhci_setup_host()
4620 if (host->caps & SDHCI_CAN_VDD_330) { in sdhci_setup_host()
4623 mmc->max_current_330 = FIELD_GET(SDHCI_MAX_CURRENT_330_MASK, in sdhci_setup_host()
4627 if (host->caps & SDHCI_CAN_VDD_300) { in sdhci_setup_host()
4630 mmc->max_current_300 = FIELD_GET(SDHCI_MAX_CURRENT_300_MASK, in sdhci_setup_host()
4634 if (host->caps & SDHCI_CAN_VDD_180) { in sdhci_setup_host()
4637 mmc->max_current_180 = FIELD_GET(SDHCI_MAX_CURRENT_180_MASK, in sdhci_setup_host()
4643 if (host->ocr_mask) in sdhci_setup_host()
4644 ocr_avail = host->ocr_mask; in sdhci_setup_host()
4647 if (mmc->ocr_avail) in sdhci_setup_host()
4648 ocr_avail = mmc->ocr_avail; in sdhci_setup_host()
4650 mmc->ocr_avail = ocr_avail; in sdhci_setup_host()
4651 mmc->ocr_avail_sdio = ocr_avail; in sdhci_setup_host()
4652 if (host->ocr_avail_sdio) in sdhci_setup_host()
4653 mmc->ocr_avail_sdio &= host->ocr_avail_sdio; in sdhci_setup_host()
4654 mmc->ocr_avail_sd = ocr_avail; in sdhci_setup_host()
4655 if (host->ocr_avail_sd) in sdhci_setup_host()
4656 mmc->ocr_avail_sd &= host->ocr_avail_sd; in sdhci_setup_host()
4658 mmc->ocr_avail_sd &= ~MMC_VDD_165_195; in sdhci_setup_host()
4659 mmc->ocr_avail_mmc = ocr_avail; in sdhci_setup_host()
4660 if (host->ocr_avail_mmc) in sdhci_setup_host()
4661 mmc->ocr_avail_mmc &= host->ocr_avail_mmc; in sdhci_setup_host()
4663 if (mmc->ocr_avail == 0) { in sdhci_setup_host()
4666 ret = -ENODEV; in sdhci_setup_host()
4670 if ((mmc->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | in sdhci_setup_host()
4673 (mmc->caps2 & (MMC_CAP2_HS200_1_8V_SDR | MMC_CAP2_HS400_1_8V))) in sdhci_setup_host()
4674 host->flags |= SDHCI_SIGNALING_180; in sdhci_setup_host()
4676 if (mmc->caps2 & MMC_CAP2_HSX00_1_2V) in sdhci_setup_host()
4677 host->flags |= SDHCI_SIGNALING_120; in sdhci_setup_host()
4679 spin_lock_init(&host->lock); in sdhci_setup_host()
4686 mmc->max_req_size = 524288; in sdhci_setup_host()
4692 if (host->flags & SDHCI_USE_ADMA) { in sdhci_setup_host()
4693 mmc->max_segs = SDHCI_MAX_SEGS; in sdhci_setup_host()
4694 } else if (host->flags & SDHCI_USE_SDMA) { in sdhci_setup_host()
4695 mmc->max_segs = 1; in sdhci_setup_host()
4696 mmc->max_req_size = min_t(size_t, mmc->max_req_size, in sdhci_setup_host()
4699 mmc->max_segs = SDHCI_MAX_SEGS; in sdhci_setup_host()
4707 if (host->flags & SDHCI_USE_ADMA) { in sdhci_setup_host()
4708 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC) { in sdhci_setup_host()
4709 host->max_adma = 65532; /* 32-bit alignment */ in sdhci_setup_host()
4710 mmc->max_seg_size = 65535; in sdhci_setup_host()
4715 * descriptor (16-bit field), but some controllers do in sdhci_setup_host()
4724 if (mmc->max_seg_size < PAGE_SIZE) in sdhci_setup_host()
4725 mmc->max_seg_size = PAGE_SIZE; in sdhci_setup_host()
4727 mmc->max_seg_size = 65536; in sdhci_setup_host()
4730 mmc->max_seg_size = mmc->max_req_size; in sdhci_setup_host()
4737 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) { in sdhci_setup_host()
4738 mmc->max_blk_size = 2; in sdhci_setup_host()
4740 mmc->max_blk_size = (host->caps & SDHCI_MAX_BLOCK_MASK) >> in sdhci_setup_host()
4742 if (mmc->max_blk_size >= 3) { in sdhci_setup_host()
4745 mmc->max_blk_size = 0; in sdhci_setup_host()
4749 mmc->max_blk_size = 512 << mmc->max_blk_size; in sdhci_setup_host()
4754 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535; in sdhci_setup_host()
4756 if (mmc->max_segs == 1) in sdhci_setup_host()
4757 /* This may alter mmc->*_blk_* parameters */ in sdhci_setup_host()
4763 if (host->sdhci_core_to_disable_vqmmc) in sdhci_setup_host()
4764 regulator_disable(mmc->supply.vqmmc); in sdhci_setup_host()
4766 if (host->align_buffer) in sdhci_setup_host()
4767 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz + in sdhci_setup_host()
4768 host->adma_table_sz, host->align_buffer, in sdhci_setup_host()
4769 host->align_addr); in sdhci_setup_host()
4770 host->adma_table = NULL; in sdhci_setup_host()
4771 host->align_buffer = NULL; in sdhci_setup_host()
4779 struct mmc_host *mmc = host->mmc; in sdhci_cleanup_host()
4781 if (host->sdhci_core_to_disable_vqmmc) in sdhci_cleanup_host()
4782 regulator_disable(mmc->supply.vqmmc); in sdhci_cleanup_host()
4784 if (host->align_buffer) in sdhci_cleanup_host()
4785 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz + in sdhci_cleanup_host()
4786 host->adma_table_sz, host->align_buffer, in sdhci_cleanup_host()
4787 host->align_addr); in sdhci_cleanup_host()
4789 if (host->use_external_dma) in sdhci_cleanup_host()
4792 host->adma_table = NULL; in sdhci_cleanup_host()
4793 host->align_buffer = NULL; in sdhci_cleanup_host()
4797 int __sdhci_add_host(struct sdhci_host *host) in __sdhci_add_host()
4799 unsigned int flags = WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_HIGHPRI; in __sdhci_add_host()
4800 struct mmc_host *mmc = host->mmc; in __sdhci_add_host()
4801 int ret; in __sdhci_add_host()
4803 if ((mmc->caps2 & MMC_CAP2_CQE) && in __sdhci_add_host()
4804 (host->quirks & SDHCI_QUIRK_BROKEN_CQE)) { in __sdhci_add_host()
4805 mmc->caps2 &= ~MMC_CAP2_CQE; in __sdhci_add_host()
4806 mmc->cqe_ops = NULL; in __sdhci_add_host()
4809 host->complete_wq = alloc_workqueue("sdhci", flags, 0); in __sdhci_add_host()
4810 if (!host->complete_wq) in __sdhci_add_host()
4811 return -ENOMEM; in __sdhci_add_host()
4813 INIT_WORK(&host->complete_work, sdhci_complete_work); in __sdhci_add_host()
4815 timer_setup(&host->timer, sdhci_timeout_timer, 0); in __sdhci_add_host()
4816 timer_setup(&host->data_timer, sdhci_timeout_data_timer, 0); in __sdhci_add_host()
4818 init_waitqueue_head(&host->buf_ready_int); in __sdhci_add_host()
4822 ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq, in __sdhci_add_host()
4826 mmc_hostname(mmc), host->irq, ret); in __sdhci_add_host()
4842 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)), in __sdhci_add_host()
4843 host->use_external_dma ? "External DMA" : in __sdhci_add_host()
4844 (host->flags & SDHCI_USE_ADMA) ? in __sdhci_add_host()
4845 (host->flags & SDHCI_USE_64_BIT_DMA) ? "ADMA 64-bit" : "ADMA" : in __sdhci_add_host()
4846 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO"); in __sdhci_add_host()
4858 free_irq(host->irq, host); in __sdhci_add_host()
4860 destroy_workqueue(host->complete_wq); in __sdhci_add_host()
4866 int sdhci_add_host(struct sdhci_host *host) in sdhci_add_host()
4868 int ret; in sdhci_add_host()
4887 void sdhci_remove_host(struct sdhci_host *host, int dead) in sdhci_remove_host()
4889 struct mmc_host *mmc = host->mmc; in sdhci_remove_host()
4893 spin_lock_irqsave(&host->lock, flags); in sdhci_remove_host()
4895 host->flags |= SDHCI_DEVICE_DEAD; in sdhci_remove_host()
4900 sdhci_error_out_mrqs(host, -ENOMEDIUM); in sdhci_remove_host()
4903 spin_unlock_irqrestore(&host->lock, flags); in sdhci_remove_host()
4917 free_irq(host->irq, host); in sdhci_remove_host()
4919 del_timer_sync(&host->timer); in sdhci_remove_host()
4920 del_timer_sync(&host->data_timer); in sdhci_remove_host()
4922 destroy_workqueue(host->complete_wq); in sdhci_remove_host()
4924 if (host->sdhci_core_to_disable_vqmmc) in sdhci_remove_host()
4925 regulator_disable(mmc->supply.vqmmc); in sdhci_remove_host()
4927 if (host->align_buffer) in sdhci_remove_host()
4928 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz + in sdhci_remove_host()
4929 host->adma_table_sz, host->align_buffer, in sdhci_remove_host()
4930 host->align_addr); in sdhci_remove_host()
4932 if (host->use_external_dma) in sdhci_remove_host()
4935 host->adma_table = NULL; in sdhci_remove_host()
4936 host->align_buffer = NULL; in sdhci_remove_host()
4943 mmc_free_host(host->mmc); in sdhci_free_host()
4954 static int __init sdhci_drv_init(void) in sdhci_drv_init()