Lines Matching +full:mmc +full:- +full:host

1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Realtek PCI-Express SD/MMC Card Interface driver
4 * Copyright(c) 2009-2013 Realtek Semiconductor Corp. All rights reserved.
17 #include <linux/mmc/host.h>
18 #include <linux/mmc/mmc.h>
19 #include <linux/mmc/sd.h>
20 #include <linux/mmc/sdio.h>
21 #include <linux/mmc/card.h>
29 struct mmc_host *mmc; member
49 static int sdmmc_init_sd_express(struct mmc_host *mmc, struct mmc_ios *ios);
51 static inline struct device *sdmmc_dev(struct realtek_pci_sdmmc *host) in sdmmc_dev() argument
53 return &(host->pdev->dev); in sdmmc_dev()
56 static inline void sd_clear_error(struct realtek_pci_sdmmc *host) in sd_clear_error() argument
58 rtsx_pci_write_register(host->pcr, CARD_STOP, in sd_clear_error()
63 static void dump_reg_range(struct realtek_pci_sdmmc *host, u16 start, u16 end) in dump_reg_range() argument
65 u16 len = end - start + 1; in dump_reg_range()
71 int n = min(8, len - i); in dump_reg_range()
75 rtsx_pci_read_register(host->pcr, start + i + j, in dump_reg_range()
77 dev_dbg(sdmmc_dev(host), "0x%04X(%d): %8ph\n", in dump_reg_range()
82 static void sd_print_debug_regs(struct realtek_pci_sdmmc *host) in sd_print_debug_regs() argument
84 dump_reg_range(host, 0xFDA0, 0xFDB3); in sd_print_debug_regs()
85 dump_reg_range(host, 0xFD52, 0xFD69); in sd_print_debug_regs()
88 #define sd_print_debug_regs(host) argument
91 static inline int sd_get_cd_int(struct realtek_pci_sdmmc *host) in sd_get_cd_int() argument
93 return rtsx_pci_readl(host->pcr, RTSX_BIPR) & SD_EXIST; in sd_get_cd_int()
99 SD_CMD_START | cmd->opcode); in sd_cmd_set_sd_cmd()
100 rtsx_pci_write_be32(pcr, SD_CMD1, cmd->arg); in sd_cmd_set_sd_cmd()
127 return -EINVAL; in sd_response_type()
141 * sd_pre_dma_transfer - do dma_map_sg() or using cookie
145 * 0 - do dma_map_sg()
146 * 1 - using cookie
148 static int sd_pre_dma_transfer(struct realtek_pci_sdmmc *host, in sd_pre_dma_transfer() argument
151 struct rtsx_pcr *pcr = host->pcr; in sd_pre_dma_transfer()
152 int read = data->flags & MMC_DATA_READ; in sd_pre_dma_transfer()
156 if (!pre && data->host_cookie && data->host_cookie != host->cookie) { in sd_pre_dma_transfer()
157 dev_err(sdmmc_dev(host), in sd_pre_dma_transfer()
158 "error: data->host_cookie = %d, host->cookie = %d\n", in sd_pre_dma_transfer()
159 data->host_cookie, host->cookie); in sd_pre_dma_transfer()
160 data->host_cookie = 0; in sd_pre_dma_transfer()
163 if (pre || data->host_cookie != host->cookie) { in sd_pre_dma_transfer()
164 count = rtsx_pci_dma_map_sg(pcr, data->sg, data->sg_len, read); in sd_pre_dma_transfer()
166 count = host->cookie_sg_count; in sd_pre_dma_transfer()
171 host->cookie_sg_count = count; in sd_pre_dma_transfer()
172 if (++host->cookie < 0) in sd_pre_dma_transfer()
173 host->cookie = 1; in sd_pre_dma_transfer()
174 data->host_cookie = host->cookie; in sd_pre_dma_transfer()
176 host->sg_count = count; in sd_pre_dma_transfer()
182 static void sdmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq) in sdmmc_pre_req() argument
184 struct realtek_pci_sdmmc *host = mmc_priv(mmc); in sdmmc_pre_req() local
185 struct mmc_data *data = mrq->data; in sdmmc_pre_req()
187 if (data->host_cookie) { in sdmmc_pre_req()
188 dev_err(sdmmc_dev(host), in sdmmc_pre_req()
189 "error: reset data->host_cookie = %d\n", in sdmmc_pre_req()
190 data->host_cookie); in sdmmc_pre_req()
191 data->host_cookie = 0; in sdmmc_pre_req()
194 sd_pre_dma_transfer(host, data, true); in sdmmc_pre_req()
195 dev_dbg(sdmmc_dev(host), "pre dma sg: %d\n", host->cookie_sg_count); in sdmmc_pre_req()
198 static void sdmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq, in sdmmc_post_req() argument
201 struct realtek_pci_sdmmc *host = mmc_priv(mmc); in sdmmc_post_req() local
202 struct rtsx_pcr *pcr = host->pcr; in sdmmc_post_req()
203 struct mmc_data *data = mrq->data; in sdmmc_post_req()
204 int read = data->flags & MMC_DATA_READ; in sdmmc_post_req()
206 rtsx_pci_dma_unmap_sg(pcr, data->sg, data->sg_len, read); in sdmmc_post_req()
207 data->host_cookie = 0; in sdmmc_post_req()
210 static void sd_send_cmd_get_rsp(struct realtek_pci_sdmmc *host, in sd_send_cmd_get_rsp() argument
213 struct rtsx_pcr *pcr = host->pcr; in sd_send_cmd_get_rsp()
214 u8 cmd_idx = (u8)cmd->opcode; in sd_send_cmd_get_rsp()
215 u32 arg = cmd->arg; in sd_send_cmd_get_rsp()
224 dev_dbg(sdmmc_dev(host), "%s: SD/MMC CMD %d, arg = 0x%08x\n", in sd_send_cmd_get_rsp()
234 timeout = cmd->busy_timeout ? cmd->busy_timeout : 3000; in sd_send_cmd_get_rsp()
236 if (cmd->opcode == SD_SWITCH_VOLTAGE) { in sd_send_cmd_get_rsp()
257 /* Read data from ping-pong buffer */ in sd_send_cmd_get_rsp()
270 sd_print_debug_regs(host); in sd_send_cmd_get_rsp()
271 sd_clear_error(host); in sd_send_cmd_get_rsp()
272 dev_dbg(sdmmc_dev(host), in sd_send_cmd_get_rsp()
287 err = -EILSEQ; in sd_send_cmd_get_rsp()
288 dev_dbg(sdmmc_dev(host), "Invalid response bit\n"); in sd_send_cmd_get_rsp()
295 err = -EILSEQ; in sd_send_cmd_get_rsp()
296 dev_dbg(sdmmc_dev(host), "CRC7 error\n"); in sd_send_cmd_get_rsp()
303 * The controller offloads the last byte {CRC-7, end bit 1'b1} in sd_send_cmd_get_rsp()
310 cmd->resp[i] = get_unaligned_be32(ptr + 1 + i * 4); in sd_send_cmd_get_rsp()
311 dev_dbg(sdmmc_dev(host), "cmd->resp[%d] = 0x%08x\n", in sd_send_cmd_get_rsp()
312 i, cmd->resp[i]); in sd_send_cmd_get_rsp()
315 cmd->resp[0] = get_unaligned_be32(ptr + 1); in sd_send_cmd_get_rsp()
316 dev_dbg(sdmmc_dev(host), "cmd->resp[0] = 0x%08x\n", in sd_send_cmd_get_rsp()
317 cmd->resp[0]); in sd_send_cmd_get_rsp()
321 cmd->error = err; in sd_send_cmd_get_rsp()
328 static int sd_read_data(struct realtek_pci_sdmmc *host, struct mmc_command *cmd, in sd_read_data() argument
331 struct rtsx_pcr *pcr = host->pcr; in sd_read_data()
335 dev_dbg(sdmmc_dev(host), "%s: SD/MMC CMD %d, arg = 0x%08x\n", in sd_read_data()
336 __func__, cmd->opcode, cmd->arg); in sd_read_data()
341 if (cmd->opcode == MMC_SEND_TUNING_BLOCK) in sd_read_data()
363 sd_print_debug_regs(host); in sd_read_data()
364 dev_dbg(sdmmc_dev(host), in sd_read_data()
372 dev_dbg(sdmmc_dev(host), in sd_read_data()
381 static int sd_write_data(struct realtek_pci_sdmmc *host, in sd_write_data() argument
385 struct rtsx_pcr *pcr = host->pcr; in sd_write_data()
388 dev_dbg(sdmmc_dev(host), "%s: SD/MMC CMD %d, arg = 0x%08x\n", in sd_write_data()
389 __func__, cmd->opcode, cmd->arg); in sd_write_data()
394 sd_send_cmd_get_rsp(host, cmd); in sd_write_data()
395 if (cmd->error) in sd_write_data()
396 return cmd->error; in sd_write_data()
401 dev_dbg(sdmmc_dev(host), in sd_write_data()
419 sd_print_debug_regs(host); in sd_write_data()
420 dev_dbg(sdmmc_dev(host), in sd_write_data()
428 static int sd_read_long_data(struct realtek_pci_sdmmc *host, in sd_read_long_data() argument
431 struct rtsx_pcr *pcr = host->pcr; in sd_read_long_data()
432 struct mmc_host *mmc = host->mmc; in sd_read_long_data() local
433 struct mmc_card *card = mmc->card; in sd_read_long_data()
434 struct mmc_command *cmd = mrq->cmd; in sd_read_long_data()
435 struct mmc_data *data = mrq->data; in sd_read_long_data()
440 size_t data_len = data->blksz * data->blocks; in sd_read_long_data()
442 dev_dbg(sdmmc_dev(host), "%s: SD/MMC CMD %d, arg = 0x%08x\n", in sd_read_long_data()
443 __func__, cmd->opcode, cmd->arg); in sd_read_long_data()
454 sd_cmd_set_data_len(pcr, data->blocks, data->blksz); in sd_read_long_data()
476 err = rtsx_pci_dma_transfer(pcr, data->sg, host->sg_count, 1, 10000); in sd_read_long_data()
478 sd_print_debug_regs(host); in sd_read_long_data()
479 sd_clear_error(host); in sd_read_long_data()
486 static int sd_write_long_data(struct realtek_pci_sdmmc *host, in sd_write_long_data() argument
489 struct rtsx_pcr *pcr = host->pcr; in sd_write_long_data()
490 struct mmc_host *mmc = host->mmc; in sd_write_long_data() local
491 struct mmc_card *card = mmc->card; in sd_write_long_data()
492 struct mmc_command *cmd = mrq->cmd; in sd_write_long_data()
493 struct mmc_data *data = mrq->data; in sd_write_long_data()
497 size_t data_len = data->blksz * data->blocks; in sd_write_long_data()
499 sd_send_cmd_get_rsp(host, cmd); in sd_write_long_data()
500 if (cmd->error) in sd_write_long_data()
501 return cmd->error; in sd_write_long_data()
503 dev_dbg(sdmmc_dev(host), "%s: SD/MMC CMD %d, arg = 0x%08x\n", in sd_write_long_data()
504 __func__, cmd->opcode, cmd->arg); in sd_write_long_data()
513 sd_cmd_set_data_len(pcr, data->blocks, data->blksz); in sd_write_long_data()
534 err = rtsx_pci_dma_transfer(pcr, data->sg, host->sg_count, 0, 10000); in sd_write_long_data()
536 sd_clear_error(host); in sd_write_long_data()
543 static inline void sd_enable_initial_mode(struct realtek_pci_sdmmc *host) in sd_enable_initial_mode() argument
545 rtsx_pci_write_register(host->pcr, SD_CFG1, in sd_enable_initial_mode()
549 static inline void sd_disable_initial_mode(struct realtek_pci_sdmmc *host) in sd_disable_initial_mode() argument
551 rtsx_pci_write_register(host->pcr, SD_CFG1, in sd_disable_initial_mode()
555 static int sd_rw_multi(struct realtek_pci_sdmmc *host, struct mmc_request *mrq) in sd_rw_multi() argument
557 struct mmc_data *data = mrq->data; in sd_rw_multi()
560 if (host->sg_count < 0) { in sd_rw_multi()
561 data->error = host->sg_count; in sd_rw_multi()
562 dev_dbg(sdmmc_dev(host), "%s: sg_count = %d is invalid\n", in sd_rw_multi()
563 __func__, host->sg_count); in sd_rw_multi()
564 return data->error; in sd_rw_multi()
567 if (data->flags & MMC_DATA_READ) { in sd_rw_multi()
568 if (host->initial_mode) in sd_rw_multi()
569 sd_disable_initial_mode(host); in sd_rw_multi()
571 err = sd_read_long_data(host, mrq); in sd_rw_multi()
573 if (host->initial_mode) in sd_rw_multi()
574 sd_enable_initial_mode(host); in sd_rw_multi()
579 return sd_write_long_data(host, mrq); in sd_rw_multi()
582 static void sd_normal_rw(struct realtek_pci_sdmmc *host, in sd_normal_rw() argument
585 struct mmc_command *cmd = mrq->cmd; in sd_normal_rw()
586 struct mmc_data *data = mrq->data; in sd_normal_rw()
589 buf = kzalloc(data->blksz, GFP_NOIO); in sd_normal_rw()
591 cmd->error = -ENOMEM; in sd_normal_rw()
595 if (data->flags & MMC_DATA_READ) { in sd_normal_rw()
596 if (host->initial_mode) in sd_normal_rw()
597 sd_disable_initial_mode(host); in sd_normal_rw()
599 cmd->error = sd_read_data(host, cmd, (u16)data->blksz, buf, in sd_normal_rw()
600 data->blksz, 200); in sd_normal_rw()
602 if (host->initial_mode) in sd_normal_rw()
603 sd_enable_initial_mode(host); in sd_normal_rw()
605 sg_copy_from_buffer(data->sg, data->sg_len, buf, data->blksz); in sd_normal_rw()
607 sg_copy_to_buffer(data->sg, data->sg_len, buf, data->blksz); in sd_normal_rw()
609 cmd->error = sd_write_data(host, cmd, (u16)data->blksz, buf, in sd_normal_rw()
610 data->blksz, 200); in sd_normal_rw()
616 static int sd_change_phase(struct realtek_pci_sdmmc *host, in sd_change_phase() argument
619 struct rtsx_pcr *pcr = host->pcr; in sd_change_phase()
621 dev_dbg(sdmmc_dev(host), "%s(%s): sample_point = %d\n", in sd_change_phase()
660 static u8 sd_search_final_phase(struct realtek_pci_sdmmc *host, u32 phase_map) in sd_search_final_phase() argument
667 dev_err(sdmmc_dev(host), "phase error: [map:%x]\n", phase_map); in sd_search_final_phase()
681 dev_dbg(sdmmc_dev(host), "phase: [map:%x] [maxlen:%d] [final:%d]\n", in sd_search_final_phase()
687 static void sd_wait_data_idle(struct realtek_pci_sdmmc *host) in sd_wait_data_idle() argument
693 rtsx_pci_read_register(host->pcr, SD_DATA_STATE, &val); in sd_wait_data_idle()
701 static int sd_tuning_rx_cmd(struct realtek_pci_sdmmc *host, in sd_tuning_rx_cmd() argument
706 struct rtsx_pcr *pcr = host->pcr; in sd_tuning_rx_cmd()
708 sd_change_phase(host, sample_point, true); in sd_tuning_rx_cmd()
714 err = sd_read_data(host, &cmd, 0x40, NULL, 0, 100); in sd_tuning_rx_cmd()
717 sd_wait_data_idle(host); in sd_tuning_rx_cmd()
718 sd_clear_error(host); in sd_tuning_rx_cmd()
728 static int sd_tuning_phase(struct realtek_pci_sdmmc *host, in sd_tuning_phase() argument
735 err = sd_tuning_rx_cmd(host, opcode, (u8)i); in sd_tuning_phase()
746 static int sd_tuning_rx(struct realtek_pci_sdmmc *host, u8 opcode) in sd_tuning_rx() argument
753 err = sd_tuning_phase(host, opcode, &(raw_phase_map[i])); in sd_tuning_rx()
763 dev_dbg(sdmmc_dev(host), "RX raw_phase_map[%d] = 0x%08x\n", in sd_tuning_rx()
767 dev_dbg(sdmmc_dev(host), "RX phase_map = 0x%08x\n", phase_map); in sd_tuning_rx()
770 final_phase = sd_search_final_phase(host, phase_map); in sd_tuning_rx()
772 return -EINVAL; in sd_tuning_rx()
774 err = sd_change_phase(host, final_phase, true); in sd_tuning_rx()
778 return -EINVAL; in sd_tuning_rx()
787 return (cmd->opcode == SD_IO_RW_EXTENDED) && (data->blksz == 512); in sdio_extblock_cmd()
792 return mmc_op_multi(cmd->opcode) || in sd_rw_cmd()
793 (cmd->opcode == MMC_READ_SINGLE_BLOCK) || in sd_rw_cmd()
794 (cmd->opcode == MMC_WRITE_BLOCK); in sd_rw_cmd()
799 struct realtek_pci_sdmmc *host = container_of(work, in sd_request() local
801 struct rtsx_pcr *pcr = host->pcr; in sd_request()
803 struct mmc_host *mmc = host->mmc; in sd_request() local
804 struct mmc_request *mrq = host->mrq; in sd_request()
805 struct mmc_command *cmd = mrq->cmd; in sd_request()
806 struct mmc_data *data = mrq->data; in sd_request()
811 if (host->eject || !sd_get_cd_int(host)) { in sd_request()
812 cmd->error = -ENOMEDIUM; in sd_request()
816 err = rtsx_pci_card_exclusive_check(host->pcr, RTSX_SD_CARD); in sd_request()
818 cmd->error = err; in sd_request()
822 mutex_lock(&pcr->pcr_mutex); in sd_request()
826 rtsx_pci_switch_clock(pcr, host->clock, host->ssc_depth, in sd_request()
827 host->initial_mode, host->double_clk, host->vpclk); in sd_request()
832 mutex_lock(&host->host_mutex); in sd_request()
833 host->mrq = mrq; in sd_request()
834 mutex_unlock(&host->host_mutex); in sd_request()
836 if (mrq->data) in sd_request()
837 data_size = data->blocks * data->blksz; in sd_request()
840 sd_send_cmd_get_rsp(host, cmd); in sd_request()
842 cmd->error = sd_rw_multi(host, mrq); in sd_request()
843 if (!host->using_cookie) in sd_request()
844 sdmmc_post_req(host->mmc, host->mrq, 0); in sd_request()
846 if (mmc_op_multi(cmd->opcode) && mrq->stop) in sd_request()
847 sd_send_cmd_get_rsp(host, mrq->stop); in sd_request()
849 sd_normal_rw(host, mrq); in sd_request()
852 if (mrq->data) { in sd_request()
853 if (cmd->error || data->error) in sd_request()
854 data->bytes_xfered = 0; in sd_request()
856 data->bytes_xfered = data->blocks * data->blksz; in sd_request()
859 mutex_unlock(&pcr->pcr_mutex); in sd_request()
862 if (cmd->error) { in sd_request()
863 dev_dbg(sdmmc_dev(host), "CMD %d 0x%08x error(%d)\n", in sd_request()
864 cmd->opcode, cmd->arg, cmd->error); in sd_request()
867 mutex_lock(&host->host_mutex); in sd_request()
868 host->mrq = NULL; in sd_request()
869 mutex_unlock(&host->host_mutex); in sd_request()
871 mmc_request_done(mmc, mrq); in sd_request()
874 static void sdmmc_request(struct mmc_host *mmc, struct mmc_request *mrq) in sdmmc_request() argument
876 struct realtek_pci_sdmmc *host = mmc_priv(mmc); in sdmmc_request() local
877 struct mmc_data *data = mrq->data; in sdmmc_request()
879 mutex_lock(&host->host_mutex); in sdmmc_request()
880 host->mrq = mrq; in sdmmc_request()
881 mutex_unlock(&host->host_mutex); in sdmmc_request()
883 if (sd_rw_cmd(mrq->cmd) || sdio_extblock_cmd(mrq->cmd, data)) in sdmmc_request()
884 host->using_cookie = sd_pre_dma_transfer(host, data, false); in sdmmc_request()
886 schedule_work(&host->work); in sdmmc_request()
889 static int sd_set_bus_width(struct realtek_pci_sdmmc *host, in sd_set_bus_width() argument
900 err = rtsx_pci_write_register(host->pcr, SD_CFG1, in sd_set_bus_width()
906 static int sd_power_on(struct realtek_pci_sdmmc *host, unsigned char power_mode) in sd_power_on() argument
908 struct rtsx_pcr *pcr = host->pcr; in sd_power_on()
909 struct mmc_host *mmc = host->mmc; in sd_power_on() local
914 if (host->prev_power_state == MMC_POWER_ON) in sd_power_on()
917 if (host->prev_power_state == MMC_POWER_UP) { in sd_power_on()
958 sdmmc_init_sd_express(mmc, NULL); in sd_power_on()
961 if (pcr->extra_caps & EXTRA_CAPS_SD_EXPRESS) in sd_power_on()
962 mmc->caps2 |= MMC_CAP2_SD_EXP | MMC_CAP2_SD_EXP_1_2V; in sd_power_on()
966 * in read-only mode. in sd_power_on()
970 pcr->extra_caps &= ~EXTRA_CAPS_SD_EXPRESS; in sd_power_on()
971 mmc->caps2 &= ~(MMC_CAP2_SD_EXP | MMC_CAP2_SD_EXP_1_2V); in sd_power_on()
976 host->prev_power_state = power_mode; in sd_power_on()
980 static int sd_power_off(struct realtek_pci_sdmmc *host) in sd_power_off() argument
982 struct rtsx_pcr *pcr = host->pcr; in sd_power_off()
985 host->prev_power_state = MMC_POWER_OFF; in sd_power_off()
1003 static int sd_set_power_mode(struct realtek_pci_sdmmc *host, in sd_set_power_mode() argument
1009 err = sd_power_off(host); in sd_set_power_mode()
1011 err = sd_power_on(host, power_mode); in sd_set_power_mode()
1016 static int sd_set_timing(struct realtek_pci_sdmmc *host, unsigned char timing) in sd_set_timing() argument
1018 struct rtsx_pcr *pcr = host->pcr; in sd_set_timing()
1088 static void sdmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) in sdmmc_set_ios() argument
1090 struct realtek_pci_sdmmc *host = mmc_priv(mmc); in sdmmc_set_ios() local
1091 struct rtsx_pcr *pcr = host->pcr; in sdmmc_set_ios()
1093 if (host->eject) in sdmmc_set_ios()
1096 if (rtsx_pci_card_exclusive_check(host->pcr, RTSX_SD_CARD)) in sdmmc_set_ios()
1099 mutex_lock(&pcr->pcr_mutex); in sdmmc_set_ios()
1103 sd_set_bus_width(host, ios->bus_width); in sdmmc_set_ios()
1104 sd_set_power_mode(host, ios->power_mode); in sdmmc_set_ios()
1105 sd_set_timing(host, ios->timing); in sdmmc_set_ios()
1107 host->vpclk = false; in sdmmc_set_ios()
1108 host->double_clk = true; in sdmmc_set_ios()
1110 switch (ios->timing) { in sdmmc_set_ios()
1113 host->ssc_depth = RTSX_SSC_DEPTH_2M; in sdmmc_set_ios()
1114 host->vpclk = true; in sdmmc_set_ios()
1115 host->double_clk = false; in sdmmc_set_ios()
1120 host->ssc_depth = RTSX_SSC_DEPTH_1M; in sdmmc_set_ios()
1123 host->ssc_depth = RTSX_SSC_DEPTH_500K; in sdmmc_set_ios()
1127 host->initial_mode = (ios->clock <= 1000000) ? true : false; in sdmmc_set_ios()
1129 host->clock = ios->clock; in sdmmc_set_ios()
1130 rtsx_pci_switch_clock(pcr, ios->clock, host->ssc_depth, in sdmmc_set_ios()
1131 host->initial_mode, host->double_clk, host->vpclk); in sdmmc_set_ios()
1133 mutex_unlock(&pcr->pcr_mutex); in sdmmc_set_ios()
1136 static int sdmmc_get_ro(struct mmc_host *mmc) in sdmmc_get_ro() argument
1138 struct realtek_pci_sdmmc *host = mmc_priv(mmc); in sdmmc_get_ro() local
1139 struct rtsx_pcr *pcr = host->pcr; in sdmmc_get_ro()
1143 if (host->eject) in sdmmc_get_ro()
1144 return -ENOMEDIUM; in sdmmc_get_ro()
1146 mutex_lock(&pcr->pcr_mutex); in sdmmc_get_ro()
1150 /* Check SD mechanical write-protect switch */ in sdmmc_get_ro()
1152 dev_dbg(sdmmc_dev(host), "%s: RTSX_BIPR = 0x%08x\n", __func__, val); in sdmmc_get_ro()
1156 mutex_unlock(&pcr->pcr_mutex); in sdmmc_get_ro()
1161 static int sdmmc_get_cd(struct mmc_host *mmc) in sdmmc_get_cd() argument
1163 struct realtek_pci_sdmmc *host = mmc_priv(mmc); in sdmmc_get_cd() local
1164 struct rtsx_pcr *pcr = host->pcr; in sdmmc_get_cd()
1168 if (host->eject) in sdmmc_get_cd()
1171 mutex_lock(&pcr->pcr_mutex); in sdmmc_get_cd()
1177 dev_dbg(sdmmc_dev(host), "%s: RTSX_BIPR = 0x%08x\n", __func__, val); in sdmmc_get_cd()
1181 mutex_unlock(&pcr->pcr_mutex); in sdmmc_get_cd()
1186 static int sd_wait_voltage_stable_1(struct realtek_pci_sdmmc *host) in sd_wait_voltage_stable_1() argument
1188 struct rtsx_pcr *pcr = host->pcr; in sd_wait_voltage_stable_1()
1208 return -EINVAL; in sd_wait_voltage_stable_1()
1219 static int sd_wait_voltage_stable_2(struct realtek_pci_sdmmc *host) in sd_wait_voltage_stable_2() argument
1221 struct rtsx_pcr *pcr = host->pcr; in sd_wait_voltage_stable_2()
1238 /* SD_CMD, SD_DAT[3:0] should be pulled high by host */ in sd_wait_voltage_stable_2()
1248 dev_dbg(sdmmc_dev(host), in sd_wait_voltage_stable_2()
1253 return -EINVAL; in sd_wait_voltage_stable_2()
1259 static int sdmmc_switch_voltage(struct mmc_host *mmc, struct mmc_ios *ios) in sdmmc_switch_voltage() argument
1261 struct realtek_pci_sdmmc *host = mmc_priv(mmc); in sdmmc_switch_voltage() local
1262 struct rtsx_pcr *pcr = host->pcr; in sdmmc_switch_voltage()
1266 dev_dbg(sdmmc_dev(host), "%s: signal_voltage = %d\n", in sdmmc_switch_voltage()
1267 __func__, ios->signal_voltage); in sdmmc_switch_voltage()
1269 if (host->eject) in sdmmc_switch_voltage()
1270 return -ENOMEDIUM; in sdmmc_switch_voltage()
1272 err = rtsx_pci_card_exclusive_check(host->pcr, RTSX_SD_CARD); in sdmmc_switch_voltage()
1276 mutex_lock(&pcr->pcr_mutex); in sdmmc_switch_voltage()
1280 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) in sdmmc_switch_voltage()
1286 err = sd_wait_voltage_stable_1(host); in sdmmc_switch_voltage()
1296 err = sd_wait_voltage_stable_2(host); in sdmmc_switch_voltage()
1306 mutex_unlock(&pcr->pcr_mutex); in sdmmc_switch_voltage()
1311 static int sdmmc_execute_tuning(struct mmc_host *mmc, u32 opcode) in sdmmc_execute_tuning() argument
1313 struct realtek_pci_sdmmc *host = mmc_priv(mmc); in sdmmc_execute_tuning() local
1314 struct rtsx_pcr *pcr = host->pcr; in sdmmc_execute_tuning()
1317 if (host->eject) in sdmmc_execute_tuning()
1318 return -ENOMEDIUM; in sdmmc_execute_tuning()
1320 err = rtsx_pci_card_exclusive_check(host->pcr, RTSX_SD_CARD); in sdmmc_execute_tuning()
1324 mutex_lock(&pcr->pcr_mutex); in sdmmc_execute_tuning()
1329 switch (mmc->ios.timing) { in sdmmc_execute_tuning()
1331 err = sd_change_phase(host, SDR104_TX_PHASE(pcr), false); in sdmmc_execute_tuning()
1335 err = sd_change_phase(host, SDR50_TX_PHASE(pcr), false); in sdmmc_execute_tuning()
1339 err = sd_change_phase(host, DDR50_TX_PHASE(pcr), false); in sdmmc_execute_tuning()
1350 if ((mmc->ios.timing == MMC_TIMING_UHS_SDR104) || in sdmmc_execute_tuning()
1351 (mmc->ios.timing == MMC_TIMING_UHS_SDR50)) in sdmmc_execute_tuning()
1352 err = sd_tuning_rx(host, opcode); in sdmmc_execute_tuning()
1353 else if (mmc->ios.timing == MMC_TIMING_UHS_DDR50) in sdmmc_execute_tuning()
1354 err = sd_change_phase(host, DDR50_RX_PHASE(pcr), true); in sdmmc_execute_tuning()
1357 mutex_unlock(&pcr->pcr_mutex); in sdmmc_execute_tuning()
1362 static int sdmmc_init_sd_express(struct mmc_host *mmc, struct mmc_ios *ios) in sdmmc_init_sd_express() argument
1365 struct realtek_pci_sdmmc *host = mmc_priv(mmc); in sdmmc_init_sd_express() local
1366 struct rtsx_pcr *pcr = host->pcr; in sdmmc_init_sd_express()
1369 pcie_capability_clear_and_set_word(pcr->pci, PCI_EXP_LNKCTL2, in sdmmc_init_sd_express()
1371 pci_write_config_byte(pcr->pci, 0x80e, 0x02); in sdmmc_init_sd_express()
1372 pcie_capability_clear_and_set_word(pcr->pci, PCI_EXP_LNKCTL2, in sdmmc_init_sd_express()
1386 pcr->option.sd_800mA_ocp_thd); in sdmmc_init_sd_express()
1388 if (pcr->ops->disable_auto_blink) in sdmmc_init_sd_express()
1389 pcr->ops->disable_auto_blink(pcr); in sdmmc_init_sd_express()
1398 pcr->hw_param.interrupt_en &= ~(SD_INT_EN); in sdmmc_init_sd_express()
1399 rtsx_pci_writel(pcr, RTSX_BIER, pcr->hw_param.interrupt_en); in sdmmc_init_sd_express()
1411 host->eject = true; in sdmmc_init_sd_express()
1427 static void init_extra_caps(struct realtek_pci_sdmmc *host) in init_extra_caps() argument
1429 struct mmc_host *mmc = host->mmc; in init_extra_caps() local
1430 struct rtsx_pcr *pcr = host->pcr; in init_extra_caps()
1432 dev_dbg(sdmmc_dev(host), "pcr->extra_caps = 0x%x\n", pcr->extra_caps); in init_extra_caps()
1434 if (pcr->extra_caps & EXTRA_CAPS_SD_SDR50) in init_extra_caps()
1435 mmc->caps |= MMC_CAP_UHS_SDR50; in init_extra_caps()
1436 if (pcr->extra_caps & EXTRA_CAPS_SD_SDR104) in init_extra_caps()
1437 mmc->caps |= MMC_CAP_UHS_SDR104; in init_extra_caps()
1438 if (pcr->extra_caps & EXTRA_CAPS_SD_DDR50) in init_extra_caps()
1439 mmc->caps |= MMC_CAP_UHS_DDR50; in init_extra_caps()
1440 if (pcr->extra_caps & EXTRA_CAPS_MMC_HSDDR) in init_extra_caps()
1441 mmc->caps |= MMC_CAP_1_8V_DDR; in init_extra_caps()
1442 if (pcr->extra_caps & EXTRA_CAPS_MMC_8BIT) in init_extra_caps()
1443 mmc->caps |= MMC_CAP_8_BIT_DATA; in init_extra_caps()
1444 if (pcr->extra_caps & EXTRA_CAPS_NO_MMC) in init_extra_caps()
1445 mmc->caps2 |= MMC_CAP2_NO_MMC; in init_extra_caps()
1446 if (pcr->extra_caps & EXTRA_CAPS_SD_EXPRESS) in init_extra_caps()
1447 mmc->caps2 |= MMC_CAP2_SD_EXP | MMC_CAP2_SD_EXP_1_2V; in init_extra_caps()
1450 static void realtek_init_host(struct realtek_pci_sdmmc *host) in realtek_init_host() argument
1452 struct mmc_host *mmc = host->mmc; in realtek_init_host() local
1453 struct rtsx_pcr *pcr = host->pcr; in realtek_init_host()
1455 mmc->f_min = 250000; in realtek_init_host()
1456 mmc->f_max = 208000000; in realtek_init_host()
1457 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195; in realtek_init_host()
1458 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SD_HIGHSPEED | in realtek_init_host()
1461 if (pcr->rtd3_en) in realtek_init_host()
1462 mmc->caps = mmc->caps | MMC_CAP_AGGRESSIVE_PM; in realtek_init_host()
1463 mmc->caps2 = MMC_CAP2_NO_PRESCAN_POWERUP | MMC_CAP2_FULL_PWR_CYCLE | in realtek_init_host()
1465 mmc->max_current_330 = 400; in realtek_init_host()
1466 mmc->max_current_180 = 800; in realtek_init_host()
1467 mmc->ops = &realtek_pci_sdmmc_ops; in realtek_init_host()
1469 init_extra_caps(host); in realtek_init_host()
1471 mmc->max_segs = 256; in realtek_init_host()
1472 mmc->max_seg_size = 65536; in realtek_init_host()
1473 mmc->max_blk_size = 512; in realtek_init_host()
1474 mmc->max_blk_count = 65535; in realtek_init_host()
1475 mmc->max_req_size = 524288; in realtek_init_host()
1480 struct realtek_pci_sdmmc *host = platform_get_drvdata(pdev); in rtsx_pci_sdmmc_card_event() local
1482 host->cookie = -1; in rtsx_pci_sdmmc_card_event()
1483 mmc_detect_change(host->mmc, 0); in rtsx_pci_sdmmc_card_event()
1488 struct mmc_host *mmc; in rtsx_pci_sdmmc_drv_probe() local
1489 struct realtek_pci_sdmmc *host; in rtsx_pci_sdmmc_drv_probe() local
1491 struct pcr_handle *handle = pdev->dev.platform_data; in rtsx_pci_sdmmc_drv_probe()
1495 return -ENXIO; in rtsx_pci_sdmmc_drv_probe()
1497 pcr = handle->pcr; in rtsx_pci_sdmmc_drv_probe()
1499 return -ENXIO; in rtsx_pci_sdmmc_drv_probe()
1501 dev_dbg(&(pdev->dev), ": Realtek PCI-E SDMMC controller found\n"); in rtsx_pci_sdmmc_drv_probe()
1503 mmc = mmc_alloc_host(sizeof(*host), &pdev->dev); in rtsx_pci_sdmmc_drv_probe()
1504 if (!mmc) in rtsx_pci_sdmmc_drv_probe()
1505 return -ENOMEM; in rtsx_pci_sdmmc_drv_probe()
1507 host = mmc_priv(mmc); in rtsx_pci_sdmmc_drv_probe()
1508 host->pcr = pcr; in rtsx_pci_sdmmc_drv_probe()
1509 mmc->ios.power_delay_ms = 5; in rtsx_pci_sdmmc_drv_probe()
1510 host->mmc = mmc; in rtsx_pci_sdmmc_drv_probe()
1511 host->pdev = pdev; in rtsx_pci_sdmmc_drv_probe()
1512 host->cookie = -1; in rtsx_pci_sdmmc_drv_probe()
1513 host->prev_power_state = MMC_POWER_OFF; in rtsx_pci_sdmmc_drv_probe()
1514 INIT_WORK(&host->work, sd_request); in rtsx_pci_sdmmc_drv_probe()
1515 platform_set_drvdata(pdev, host); in rtsx_pci_sdmmc_drv_probe()
1516 pcr->slots[RTSX_SD_CARD].p_dev = pdev; in rtsx_pci_sdmmc_drv_probe()
1517 pcr->slots[RTSX_SD_CARD].card_event = rtsx_pci_sdmmc_card_event; in rtsx_pci_sdmmc_drv_probe()
1519 mutex_init(&host->host_mutex); in rtsx_pci_sdmmc_drv_probe()
1521 realtek_init_host(host); in rtsx_pci_sdmmc_drv_probe()
1523 pm_runtime_no_callbacks(&pdev->dev); in rtsx_pci_sdmmc_drv_probe()
1524 pm_runtime_set_active(&pdev->dev); in rtsx_pci_sdmmc_drv_probe()
1525 pm_runtime_enable(&pdev->dev); in rtsx_pci_sdmmc_drv_probe()
1526 pm_runtime_set_autosuspend_delay(&pdev->dev, 200); in rtsx_pci_sdmmc_drv_probe()
1527 pm_runtime_mark_last_busy(&pdev->dev); in rtsx_pci_sdmmc_drv_probe()
1528 pm_runtime_use_autosuspend(&pdev->dev); in rtsx_pci_sdmmc_drv_probe()
1530 ret = mmc_add_host(mmc); in rtsx_pci_sdmmc_drv_probe()
1532 pm_runtime_dont_use_autosuspend(&pdev->dev); in rtsx_pci_sdmmc_drv_probe()
1533 pm_runtime_disable(&pdev->dev); in rtsx_pci_sdmmc_drv_probe()
1534 mmc_free_host(mmc); in rtsx_pci_sdmmc_drv_probe()
1543 struct realtek_pci_sdmmc *host = platform_get_drvdata(pdev); in rtsx_pci_sdmmc_drv_remove() local
1545 struct mmc_host *mmc; in rtsx_pci_sdmmc_drv_remove() local
1547 pcr = host->pcr; in rtsx_pci_sdmmc_drv_remove()
1548 pcr->slots[RTSX_SD_CARD].p_dev = NULL; in rtsx_pci_sdmmc_drv_remove()
1549 pcr->slots[RTSX_SD_CARD].card_event = NULL; in rtsx_pci_sdmmc_drv_remove()
1550 mmc = host->mmc; in rtsx_pci_sdmmc_drv_remove()
1552 cancel_work_sync(&host->work); in rtsx_pci_sdmmc_drv_remove()
1554 mutex_lock(&host->host_mutex); in rtsx_pci_sdmmc_drv_remove()
1555 if (host->mrq) { in rtsx_pci_sdmmc_drv_remove()
1556 dev_dbg(&(pdev->dev), in rtsx_pci_sdmmc_drv_remove()
1558 mmc_hostname(mmc)); in rtsx_pci_sdmmc_drv_remove()
1562 host->mrq->cmd->error = -ENOMEDIUM; in rtsx_pci_sdmmc_drv_remove()
1563 if (host->mrq->stop) in rtsx_pci_sdmmc_drv_remove()
1564 host->mrq->stop->error = -ENOMEDIUM; in rtsx_pci_sdmmc_drv_remove()
1565 mmc_request_done(mmc, host->mrq); in rtsx_pci_sdmmc_drv_remove()
1567 mutex_unlock(&host->host_mutex); in rtsx_pci_sdmmc_drv_remove()
1569 mmc_remove_host(mmc); in rtsx_pci_sdmmc_drv_remove()
1570 host->eject = true; in rtsx_pci_sdmmc_drv_remove()
1572 flush_work(&host->work); in rtsx_pci_sdmmc_drv_remove()
1574 pm_runtime_dont_use_autosuspend(&pdev->dev); in rtsx_pci_sdmmc_drv_remove()
1575 pm_runtime_disable(&pdev->dev); in rtsx_pci_sdmmc_drv_remove()
1577 mmc_free_host(mmc); in rtsx_pci_sdmmc_drv_remove()
1579 dev_dbg(&(pdev->dev), in rtsx_pci_sdmmc_drv_remove()
1580 ": Realtek PCI-E SDMMC controller has been removed\n"); in rtsx_pci_sdmmc_drv_remove()
1605 MODULE_DESCRIPTION("Realtek PCI-E SD/MMC Card Host Driver");