1 /* Realtek PCI-Express SD/MMC Card Interface driver
3 * Copyright(c) 2009 Realtek Semiconductor Corp. All rights reserved.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2, or (at your option) any
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, see <http://www.gnu.org/licenses/>.
19 * Wei WANG <wei_wang@realsil.com.cn>
20 * No. 450, Shenhu Road, Suzhou Industry Park, Suzhou, China
23 #include <linux/module.h>
24 #include <linux/highmem.h>
25 #include <linux/delay.h>
26 #include <linux/platform_device.h>
27 #include <linux/mmc/host.h>
28 #include <linux/mmc/mmc.h>
29 #include <linux/mmc/sd.h>
30 #include <linux/mmc/card.h>
31 #include <linux/mfd/rtsx_pci.h>
32 #include <asm/unaligned.h>
34 /* SD Tuning Data Structure
35 * Record continuous timing phase path
37 struct timing_phase_path {
44 struct realtek_pci_sdmmc {
45 struct platform_device *pdev;
48 struct mmc_request *mrq;
50 struct mutex host_mutex;
61 static inline struct device *sdmmc_dev(struct realtek_pci_sdmmc *host)
63 return &(host->pdev->dev);
66 static inline void sd_clear_error(struct realtek_pci_sdmmc *host)
68 rtsx_pci_write_register(host->pcr, CARD_STOP,
69 SD_STOP | SD_CLR_ERR, SD_STOP | SD_CLR_ERR);
73 static void sd_print_debug_regs(struct realtek_pci_sdmmc *host)
75 struct rtsx_pcr *pcr = host->pcr;
79 /* Print SD host internal registers */
80 rtsx_pci_init_cmd(pcr);
81 for (i = 0xFDA0; i <= 0xFDAE; i++)
82 rtsx_pci_add_cmd(pcr, READ_REG_CMD, i, 0, 0);
83 for (i = 0xFD52; i <= 0xFD69; i++)
84 rtsx_pci_add_cmd(pcr, READ_REG_CMD, i, 0, 0);
85 rtsx_pci_send_cmd(pcr, 100);
87 ptr = rtsx_pci_get_cmd_data(pcr);
88 for (i = 0xFDA0; i <= 0xFDAE; i++)
89 dev_dbg(sdmmc_dev(host), "0x%04X: 0x%02x\n", i, *(ptr++));
90 for (i = 0xFD52; i <= 0xFD69; i++)
91 dev_dbg(sdmmc_dev(host), "0x%04X: 0x%02x\n", i, *(ptr++));
94 #define sd_print_debug_regs(host)
97 static int sd_read_data(struct realtek_pci_sdmmc *host, u8 *cmd, u16 byte_cnt,
98 u8 *buf, int buf_len, int timeout)
100 struct rtsx_pcr *pcr = host->pcr;
104 dev_dbg(sdmmc_dev(host), "%s: SD/MMC CMD%d\n", __func__, cmd[0] - 0x40);
109 if ((cmd[0] & 0x3F) == MMC_SEND_TUNING_BLOCK)
110 trans_mode = SD_TM_AUTO_TUNING;
112 trans_mode = SD_TM_NORMAL_READ;
114 rtsx_pci_init_cmd(pcr);
116 for (i = 0; i < 5; i++)
117 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_CMD0 + i, 0xFF, cmd[i]);
119 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_BYTE_CNT_L, 0xFF, (u8)byte_cnt);
120 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_BYTE_CNT_H,
121 0xFF, (u8)(byte_cnt >> 8));
122 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_BLOCK_CNT_L, 0xFF, 1);
123 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_BLOCK_CNT_H, 0xFF, 0);
125 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_CFG2, 0xFF,
126 SD_CALCULATE_CRC7 | SD_CHECK_CRC16 |
127 SD_NO_WAIT_BUSY_END | SD_CHECK_CRC7 | SD_RSP_LEN_6);
128 if (trans_mode != SD_TM_AUTO_TUNING)
129 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD,
130 CARD_DATA_SOURCE, 0x01, PINGPONG_BUFFER);
132 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_TRANSFER,
133 0xFF, trans_mode | SD_TRANSFER_START);
134 rtsx_pci_add_cmd(pcr, CHECK_REG_CMD, SD_TRANSFER,
135 SD_TRANSFER_END, SD_TRANSFER_END);
137 err = rtsx_pci_send_cmd(pcr, timeout);
139 sd_print_debug_regs(host);
140 dev_dbg(sdmmc_dev(host),
141 "rtsx_pci_send_cmd fail (err = %d)\n", err);
145 if (buf && buf_len) {
146 err = rtsx_pci_read_ppbuf(pcr, buf, buf_len);
148 dev_dbg(sdmmc_dev(host),
149 "rtsx_pci_read_ppbuf fail (err = %d)\n", err);
157 static int sd_write_data(struct realtek_pci_sdmmc *host, u8 *cmd, u16 byte_cnt,
158 u8 *buf, int buf_len, int timeout)
160 struct rtsx_pcr *pcr = host->pcr;
167 if (buf && buf_len) {
168 err = rtsx_pci_write_ppbuf(pcr, buf, buf_len);
170 dev_dbg(sdmmc_dev(host),
171 "rtsx_pci_write_ppbuf fail (err = %d)\n", err);
176 trans_mode = cmd ? SD_TM_AUTO_WRITE_2 : SD_TM_AUTO_WRITE_3;
177 rtsx_pci_init_cmd(pcr);
180 dev_dbg(sdmmc_dev(host), "%s: SD/MMC CMD %d\n", __func__,
183 for (i = 0; i < 5; i++)
184 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD,
185 SD_CMD0 + i, 0xFF, cmd[i]);
188 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_BYTE_CNT_L, 0xFF, (u8)byte_cnt);
189 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_BYTE_CNT_H,
190 0xFF, (u8)(byte_cnt >> 8));
191 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_BLOCK_CNT_L, 0xFF, 1);
192 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_BLOCK_CNT_H, 0xFF, 0);
194 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_CFG2, 0xFF,
195 SD_CALCULATE_CRC7 | SD_CHECK_CRC16 |
196 SD_NO_WAIT_BUSY_END | SD_CHECK_CRC7 | SD_RSP_LEN_6);
198 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_TRANSFER, 0xFF,
199 trans_mode | SD_TRANSFER_START);
200 rtsx_pci_add_cmd(pcr, CHECK_REG_CMD, SD_TRANSFER,
201 SD_TRANSFER_END, SD_TRANSFER_END);
203 err = rtsx_pci_send_cmd(pcr, timeout);
205 sd_print_debug_regs(host);
206 dev_dbg(sdmmc_dev(host),
207 "rtsx_pci_send_cmd fail (err = %d)\n", err);
214 static void sd_send_cmd_get_rsp(struct realtek_pci_sdmmc *host,
215 struct mmc_command *cmd)
217 struct rtsx_pcr *pcr = host->pcr;
218 u8 cmd_idx = (u8)cmd->opcode;
228 dev_dbg(sdmmc_dev(host), "%s: SD/MMC CMD %d, arg = 0x%08x\n",
229 __func__, cmd_idx, arg);
238 switch (mmc_resp_type(cmd)) {
240 rsp_type = SD_RSP_TYPE_R0;
244 rsp_type = SD_RSP_TYPE_R1;
247 rsp_type = SD_RSP_TYPE_R1b;
250 rsp_type = SD_RSP_TYPE_R2;
254 rsp_type = SD_RSP_TYPE_R3;
257 dev_dbg(sdmmc_dev(host), "cmd->flag is not valid\n");
262 if (rsp_type == SD_RSP_TYPE_R1b)
265 if (cmd->opcode == SD_SWITCH_VOLTAGE) {
266 err = rtsx_pci_write_register(pcr, SD_BUS_STAT,
267 0xFF, SD_CLK_TOGGLE_EN);
272 rtsx_pci_init_cmd(pcr);
274 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_CMD0, 0xFF, 0x40 | cmd_idx);
275 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_CMD1, 0xFF, (u8)(arg >> 24));
276 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_CMD2, 0xFF, (u8)(arg >> 16));
277 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_CMD3, 0xFF, (u8)(arg >> 8));
278 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_CMD4, 0xFF, (u8)arg);
280 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_CFG2, 0xFF, rsp_type);
281 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_DATA_SOURCE,
282 0x01, PINGPONG_BUFFER);
283 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_TRANSFER,
284 0xFF, SD_TM_CMD_RSP | SD_TRANSFER_START);
285 rtsx_pci_add_cmd(pcr, CHECK_REG_CMD, SD_TRANSFER,
286 SD_TRANSFER_END | SD_STAT_IDLE,
287 SD_TRANSFER_END | SD_STAT_IDLE);
289 if (rsp_type == SD_RSP_TYPE_R2) {
290 /* Read data from ping-pong buffer */
291 for (i = PPBUF_BASE2; i < PPBUF_BASE2 + 16; i++)
292 rtsx_pci_add_cmd(pcr, READ_REG_CMD, (u16)i, 0, 0);
294 } else if (rsp_type != SD_RSP_TYPE_R0) {
295 /* Read data from SD_CMDx registers */
296 for (i = SD_CMD0; i <= SD_CMD4; i++)
297 rtsx_pci_add_cmd(pcr, READ_REG_CMD, (u16)i, 0, 0);
301 rtsx_pci_add_cmd(pcr, READ_REG_CMD, SD_STAT1, 0, 0);
303 err = rtsx_pci_send_cmd(pcr, timeout);
305 sd_print_debug_regs(host);
306 sd_clear_error(host);
307 dev_dbg(sdmmc_dev(host),
308 "rtsx_pci_send_cmd error (err = %d)\n", err);
312 if (rsp_type == SD_RSP_TYPE_R0) {
317 /* Eliminate returned value of CHECK_REG_CMD */
318 ptr = rtsx_pci_get_cmd_data(pcr) + 1;
320 /* Check (Start,Transmission) bit of Response */
321 if ((ptr[0] & 0xC0) != 0) {
323 dev_dbg(sdmmc_dev(host), "Invalid response bit\n");
328 if (!(rsp_type & SD_NO_CHECK_CRC7)) {
329 if (ptr[stat_idx] & SD_CRC7_ERR) {
331 dev_dbg(sdmmc_dev(host), "CRC7 error\n");
336 if (rsp_type == SD_RSP_TYPE_R2) {
337 for (i = 0; i < 4; i++) {
338 cmd->resp[i] = get_unaligned_be32(ptr + 1 + i * 4);
339 dev_dbg(sdmmc_dev(host), "cmd->resp[%d] = 0x%08x\n",
343 cmd->resp[0] = get_unaligned_be32(ptr + 1);
344 dev_dbg(sdmmc_dev(host), "cmd->resp[0] = 0x%08x\n",
352 static int sd_rw_multi(struct realtek_pci_sdmmc *host, struct mmc_request *mrq)
354 struct rtsx_pcr *pcr = host->pcr;
355 struct mmc_host *mmc = host->mmc;
356 struct mmc_card *card = mmc->card;
357 struct mmc_data *data = mrq->data;
358 int uhs = mmc_sd_card_uhs(card);
359 int read = (data->flags & MMC_DATA_READ) ? 1 : 0;
362 size_t data_len = data->blksz * data->blocks;
365 cfg2 = SD_CALCULATE_CRC7 | SD_CHECK_CRC16 |
366 SD_NO_WAIT_BUSY_END | SD_CHECK_CRC7 | SD_RSP_LEN_0;
367 trans_mode = SD_TM_AUTO_READ_3;
369 cfg2 = SD_NO_CALCULATE_CRC7 | SD_CHECK_CRC16 |
370 SD_NO_WAIT_BUSY_END | SD_NO_CHECK_CRC7 | SD_RSP_LEN_0;
371 trans_mode = SD_TM_AUTO_WRITE_3;
375 cfg2 |= SD_NO_CHECK_WAIT_CRC_TO;
377 rtsx_pci_init_cmd(pcr);
379 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_BYTE_CNT_L, 0xFF, 0x00);
380 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_BYTE_CNT_H, 0xFF, 0x02);
381 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_BLOCK_CNT_L,
382 0xFF, (u8)data->blocks);
383 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_BLOCK_CNT_H,
384 0xFF, (u8)(data->blocks >> 8));
385 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD,
386 CARD_DATA_SOURCE, 0x01, RING_BUFFER);
388 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, IRQSTAT0,
389 DMA_DONE_INT, DMA_DONE_INT);
390 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMATC3,
391 0xFF, (u8)(data_len >> 24));
392 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMATC2,
393 0xFF, (u8)(data_len >> 16));
394 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMATC1,
395 0xFF, (u8)(data_len >> 8));
396 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMATC0, 0xFF, (u8)data_len);
398 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMACTL,
399 0x03 | DMA_PACK_SIZE_MASK,
400 DMA_DIR_FROM_CARD | DMA_EN | DMA_512);
402 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMACTL,
403 0x03 | DMA_PACK_SIZE_MASK,
404 DMA_DIR_TO_CARD | DMA_EN | DMA_512);
407 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_DATA_SOURCE,
410 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_TRANSFER, 0xFF,
411 trans_mode | SD_TRANSFER_START);
412 rtsx_pci_add_cmd(pcr, CHECK_REG_CMD, SD_TRANSFER,
413 SD_TRANSFER_END, SD_TRANSFER_END);
415 rtsx_pci_send_cmd_no_wait(pcr);
417 err = rtsx_pci_transfer_data(pcr, data->sg, data->sg_len, read, 10000);
419 sd_clear_error(host);
426 static inline void sd_enable_initial_mode(struct realtek_pci_sdmmc *host)
428 rtsx_pci_write_register(host->pcr, SD_CFG1,
429 SD_CLK_DIVIDE_MASK, SD_CLK_DIVIDE_128);
432 static inline void sd_disable_initial_mode(struct realtek_pci_sdmmc *host)
434 rtsx_pci_write_register(host->pcr, SD_CFG1,
435 SD_CLK_DIVIDE_MASK, SD_CLK_DIVIDE_0);
438 static void sd_normal_rw(struct realtek_pci_sdmmc *host,
439 struct mmc_request *mrq)
441 struct mmc_command *cmd = mrq->cmd;
442 struct mmc_data *data = mrq->data;
445 _cmd[0] = 0x40 | (u8)cmd->opcode;
446 put_unaligned_be32(cmd->arg, (u32 *)(&_cmd[1]));
448 buf = kzalloc(data->blksz, GFP_NOIO);
450 cmd->error = -ENOMEM;
454 if (data->flags & MMC_DATA_READ) {
455 if (host->initial_mode)
456 sd_disable_initial_mode(host);
458 cmd->error = sd_read_data(host, _cmd, (u16)data->blksz, buf,
461 if (host->initial_mode)
462 sd_enable_initial_mode(host);
464 sg_copy_from_buffer(data->sg, data->sg_len, buf, data->blksz);
466 sg_copy_to_buffer(data->sg, data->sg_len, buf, data->blksz);
468 cmd->error = sd_write_data(host, _cmd, (u16)data->blksz, buf,
475 static int sd_change_phase(struct realtek_pci_sdmmc *host, u8 sample_point)
477 struct rtsx_pcr *pcr = host->pcr;
480 dev_dbg(sdmmc_dev(host), "%s: sample_point = %d\n",
481 __func__, sample_point);
483 rtsx_pci_init_cmd(pcr);
485 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CLK_CTL, CHANGE_CLK, CHANGE_CLK);
486 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_VPRX_CTL, 0x1F, sample_point);
487 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_VPCLK0_CTL, PHASE_NOT_RESET, 0);
488 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_VPCLK0_CTL,
489 PHASE_NOT_RESET, PHASE_NOT_RESET);
490 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CLK_CTL, CHANGE_CLK, 0);
491 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_CFG1, SD_ASYNC_FIFO_NOT_RST, 0);
493 err = rtsx_pci_send_cmd(pcr, 100);
500 static u8 sd_search_final_phase(struct realtek_pci_sdmmc *host, u32 phase_map)
502 struct timing_phase_path path[MAX_PHASE + 1];
503 int i, j, cont_path_cnt;
504 int new_block, max_len, final_path_idx;
505 u8 final_phase = 0xFF;
507 /* Parse phase_map, take it as a bit-ring */
511 for (i = 0; i < MAX_PHASE + 1; i++) {
512 if (phase_map & (1 << i)) {
524 /* Calculate path length and middle point */
525 int idx = cont_path_cnt - 1;
527 path[idx].end - path[idx].start + 1;
529 path[idx].start + path[idx].len / 2;
534 if (cont_path_cnt == 0) {
535 dev_dbg(sdmmc_dev(host), "No continuous phase path\n");
538 /* Calculate last continuous path length and middle point */
539 int idx = cont_path_cnt - 1;
540 path[idx].len = path[idx].end - path[idx].start + 1;
541 path[idx].mid = path[idx].start + path[idx].len / 2;
544 /* Connect the first and last continuous paths if they are adjacent */
545 if (!path[0].start && (path[cont_path_cnt - 1].end == MAX_PHASE)) {
546 /* Using negative index */
547 path[0].start = path[cont_path_cnt - 1].start - MAX_PHASE - 1;
548 path[0].len += path[cont_path_cnt - 1].len;
549 path[0].mid = path[0].start + path[0].len / 2;
550 /* Convert negative middle point index to positive one */
552 path[0].mid += MAX_PHASE + 1;
556 /* Choose the longest continuous phase path */
560 for (i = 0; i < cont_path_cnt; i++) {
561 if (path[i].len > max_len) {
562 max_len = path[i].len;
563 final_phase = (u8)path[i].mid;
567 dev_dbg(sdmmc_dev(host), "path[%d].start = %d\n",
569 dev_dbg(sdmmc_dev(host), "path[%d].end = %d\n",
571 dev_dbg(sdmmc_dev(host), "path[%d].len = %d\n",
573 dev_dbg(sdmmc_dev(host), "path[%d].mid = %d\n",
578 dev_dbg(sdmmc_dev(host), "Final chosen phase: %d\n", final_phase);
582 static void sd_wait_data_idle(struct realtek_pci_sdmmc *host)
587 for (i = 0; i < 100; i++) {
588 err = rtsx_pci_read_register(host->pcr, SD_DATA_STATE, &val);
589 if (val & SD_DATA_IDLE)
596 static int sd_tuning_rx_cmd(struct realtek_pci_sdmmc *host,
597 u8 opcode, u8 sample_point)
602 err = sd_change_phase(host, sample_point);
606 cmd[0] = 0x40 | opcode;
607 err = sd_read_data(host, cmd, 0x40, NULL, 0, 100);
609 /* Wait till SD DATA IDLE */
610 sd_wait_data_idle(host);
611 sd_clear_error(host);
618 static int sd_tuning_phase(struct realtek_pci_sdmmc *host,
619 u8 opcode, u32 *phase_map)
622 u32 raw_phase_map = 0;
624 for (i = MAX_PHASE; i >= 0; i--) {
625 err = sd_tuning_rx_cmd(host, opcode, (u8)i);
627 raw_phase_map |= 1 << i;
631 *phase_map = raw_phase_map;
636 static int sd_tuning_rx(struct realtek_pci_sdmmc *host, u8 opcode)
639 u32 raw_phase_map[RX_TUNING_CNT] = {0}, phase_map;
642 for (i = 0; i < RX_TUNING_CNT; i++) {
643 err = sd_tuning_phase(host, opcode, &(raw_phase_map[i]));
647 if (raw_phase_map[i] == 0)
651 phase_map = 0xFFFFFFFF;
652 for (i = 0; i < RX_TUNING_CNT; i++) {
653 dev_dbg(sdmmc_dev(host), "RX raw_phase_map[%d] = 0x%08x\n",
654 i, raw_phase_map[i]);
655 phase_map &= raw_phase_map[i];
657 dev_dbg(sdmmc_dev(host), "RX phase_map = 0x%08x\n", phase_map);
660 final_phase = sd_search_final_phase(host, phase_map);
661 if (final_phase == 0xFF)
664 err = sd_change_phase(host, final_phase);
674 static void sdmmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
676 struct realtek_pci_sdmmc *host = mmc_priv(mmc);
677 struct rtsx_pcr *pcr = host->pcr;
678 struct mmc_command *cmd = mrq->cmd;
679 struct mmc_data *data = mrq->data;
680 unsigned int data_size = 0;
683 cmd->error = -ENOMEDIUM;
687 mutex_lock(&pcr->pcr_mutex);
689 rtsx_pci_start_run(pcr);
691 rtsx_pci_switch_clock(pcr, host->clock, host->ssc_depth,
692 host->initial_mode, host->double_clk, host->vpclk);
693 rtsx_pci_write_register(pcr, CARD_SELECT, 0x07, SD_MOD_SEL);
694 rtsx_pci_write_register(pcr, CARD_SHARE_MODE,
695 CARD_SHARE_MASK, CARD_SHARE_48_SD);
697 mutex_lock(&host->host_mutex);
699 mutex_unlock(&host->host_mutex);
702 data_size = data->blocks * data->blksz;
704 if (!data_size || mmc_op_multi(cmd->opcode) ||
705 (cmd->opcode == MMC_READ_SINGLE_BLOCK) ||
706 (cmd->opcode == MMC_WRITE_BLOCK)) {
707 sd_send_cmd_get_rsp(host, cmd);
709 if (!cmd->error && data_size) {
710 sd_rw_multi(host, mrq);
712 if (mmc_op_multi(cmd->opcode) && mrq->stop)
713 sd_send_cmd_get_rsp(host, mrq->stop);
716 sd_normal_rw(host, mrq);
720 if (cmd->error || data->error)
721 data->bytes_xfered = 0;
723 data->bytes_xfered = data->blocks * data->blksz;
726 mutex_unlock(&pcr->pcr_mutex);
730 dev_dbg(sdmmc_dev(host), "cmd->error = %d\n", cmd->error);
732 mutex_lock(&host->host_mutex);
734 mutex_unlock(&host->host_mutex);
736 mmc_request_done(mmc, mrq);
739 static int sd_set_bus_width(struct realtek_pci_sdmmc *host,
740 unsigned char bus_width)
744 [MMC_BUS_WIDTH_1] = SD_BUS_WIDTH_1BIT,
745 [MMC_BUS_WIDTH_4] = SD_BUS_WIDTH_4BIT,
746 [MMC_BUS_WIDTH_8] = SD_BUS_WIDTH_8BIT,
749 if (bus_width <= MMC_BUS_WIDTH_8)
750 err = rtsx_pci_write_register(host->pcr, SD_CFG1,
751 0x03, width[bus_width]);
756 static int sd_power_on(struct realtek_pci_sdmmc *host)
758 struct rtsx_pcr *pcr = host->pcr;
761 rtsx_pci_init_cmd(pcr);
762 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_SELECT, 0x07, SD_MOD_SEL);
763 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_SHARE_MODE,
764 CARD_SHARE_MASK, CARD_SHARE_48_SD);
765 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_CLK_EN,
766 SD_CLK_EN, SD_CLK_EN);
767 err = rtsx_pci_send_cmd(pcr, 100);
771 err = rtsx_pci_card_pull_ctl_enable(pcr, RTSX_SD_CARD);
775 err = rtsx_pci_card_power_on(pcr, RTSX_SD_CARD);
779 err = rtsx_pci_write_register(pcr, CARD_OE, SD_OUTPUT_EN, SD_OUTPUT_EN);
786 static int sd_power_off(struct realtek_pci_sdmmc *host)
788 struct rtsx_pcr *pcr = host->pcr;
791 rtsx_pci_init_cmd(pcr);
793 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_CLK_EN, SD_CLK_EN, 0);
794 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_OE, SD_OUTPUT_EN, 0);
796 err = rtsx_pci_send_cmd(pcr, 100);
800 err = rtsx_pci_card_power_off(pcr, RTSX_SD_CARD);
804 return rtsx_pci_card_pull_ctl_disable(pcr, RTSX_SD_CARD);
807 static int sd_set_power_mode(struct realtek_pci_sdmmc *host,
808 unsigned char power_mode)
812 if (power_mode == MMC_POWER_OFF)
813 err = sd_power_off(host);
815 err = sd_power_on(host);
820 static int sd_set_timing(struct realtek_pci_sdmmc *host,
821 unsigned char timing, bool *ddr_mode)
823 struct rtsx_pcr *pcr = host->pcr;
828 rtsx_pci_init_cmd(pcr);
831 case MMC_TIMING_UHS_SDR104:
832 case MMC_TIMING_UHS_SDR50:
833 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_CFG1,
834 0x0C | SD_ASYNC_FIFO_NOT_RST,
835 SD_30_MODE | SD_ASYNC_FIFO_NOT_RST);
836 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CLK_CTL,
837 CLK_LOW_FREQ, CLK_LOW_FREQ);
838 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_CLK_SOURCE, 0xFF,
839 CRC_VAR_CLK0 | SD30_FIX_CLK | SAMPLE_VAR_CLK1);
840 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CLK_CTL, CLK_LOW_FREQ, 0);
843 case MMC_TIMING_UHS_DDR50:
846 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_CFG1,
847 0x0C | SD_ASYNC_FIFO_NOT_RST,
848 SD_DDR_MODE | SD_ASYNC_FIFO_NOT_RST);
849 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CLK_CTL,
850 CLK_LOW_FREQ, CLK_LOW_FREQ);
851 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_CLK_SOURCE, 0xFF,
852 CRC_VAR_CLK0 | SD30_FIX_CLK | SAMPLE_VAR_CLK1);
853 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CLK_CTL, CLK_LOW_FREQ, 0);
854 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_PUSH_POINT_CTL,
855 DDR_VAR_TX_CMD_DAT, DDR_VAR_TX_CMD_DAT);
856 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_SAMPLE_POINT_CTL,
857 DDR_VAR_RX_DAT | DDR_VAR_RX_CMD,
858 DDR_VAR_RX_DAT | DDR_VAR_RX_CMD);
861 case MMC_TIMING_MMC_HS:
862 case MMC_TIMING_SD_HS:
863 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_CFG1,
865 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CLK_CTL,
866 CLK_LOW_FREQ, CLK_LOW_FREQ);
867 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_CLK_SOURCE, 0xFF,
868 CRC_FIX_CLK | SD30_VAR_CLK0 | SAMPLE_VAR_CLK1);
869 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CLK_CTL, CLK_LOW_FREQ, 0);
870 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_PUSH_POINT_CTL,
871 SD20_TX_SEL_MASK, SD20_TX_14_AHEAD);
872 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_SAMPLE_POINT_CTL,
873 SD20_RX_SEL_MASK, SD20_RX_14_DELAY);
877 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD,
878 SD_CFG1, 0x0C, SD_20_MODE);
879 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CLK_CTL,
880 CLK_LOW_FREQ, CLK_LOW_FREQ);
881 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_CLK_SOURCE, 0xFF,
882 CRC_FIX_CLK | SD30_VAR_CLK0 | SAMPLE_VAR_CLK1);
883 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CLK_CTL, CLK_LOW_FREQ, 0);
884 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD,
885 SD_PUSH_POINT_CTL, 0xFF, 0);
886 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_SAMPLE_POINT_CTL,
887 SD20_RX_SEL_MASK, SD20_RX_POS_EDGE);
891 err = rtsx_pci_send_cmd(pcr, 100);
896 static void sdmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
898 struct realtek_pci_sdmmc *host = mmc_priv(mmc);
899 struct rtsx_pcr *pcr = host->pcr;
904 mutex_lock(&pcr->pcr_mutex);
906 rtsx_pci_start_run(pcr);
908 sd_set_bus_width(host, ios->bus_width);
909 sd_set_power_mode(host, ios->power_mode);
910 sd_set_timing(host, ios->timing, &host->ddr_mode);
913 host->double_clk = true;
915 switch (ios->timing) {
916 case MMC_TIMING_UHS_SDR104:
917 case MMC_TIMING_UHS_SDR50:
918 host->ssc_depth = RTSX_SSC_DEPTH_2M;
920 host->double_clk = false;
922 case MMC_TIMING_UHS_DDR50:
923 case MMC_TIMING_UHS_SDR25:
924 host->ssc_depth = RTSX_SSC_DEPTH_1M;
927 host->ssc_depth = RTSX_SSC_DEPTH_500K;
931 host->initial_mode = (ios->clock <= 1000000) ? true : false;
933 host->clock = ios->clock;
934 rtsx_pci_switch_clock(pcr, ios->clock, host->ssc_depth,
935 host->initial_mode, host->double_clk, host->vpclk);
937 mutex_unlock(&pcr->pcr_mutex);
940 static int sdmmc_get_ro(struct mmc_host *mmc)
942 struct realtek_pci_sdmmc *host = mmc_priv(mmc);
943 struct rtsx_pcr *pcr = host->pcr;
950 mutex_lock(&pcr->pcr_mutex);
952 rtsx_pci_start_run(pcr);
954 /* Check SD mechanical write-protect switch */
955 val = rtsx_pci_readl(pcr, RTSX_BIPR);
956 dev_dbg(sdmmc_dev(host), "%s: RTSX_BIPR = 0x%08x\n", __func__, val);
957 if (val & SD_WRITE_PROTECT)
960 mutex_unlock(&pcr->pcr_mutex);
965 static int sdmmc_get_cd(struct mmc_host *mmc)
967 struct realtek_pci_sdmmc *host = mmc_priv(mmc);
968 struct rtsx_pcr *pcr = host->pcr;
975 mutex_lock(&pcr->pcr_mutex);
977 rtsx_pci_start_run(pcr);
979 /* Check SD card detect */
980 val = rtsx_pci_card_exist(pcr);
981 dev_dbg(sdmmc_dev(host), "%s: RTSX_BIPR = 0x%08x\n", __func__, val);
985 mutex_unlock(&pcr->pcr_mutex);
990 static int sd_wait_voltage_stable_1(struct realtek_pci_sdmmc *host)
992 struct rtsx_pcr *pcr = host->pcr;
996 /* Reference to Signal Voltage Switch Sequence in SD spec.
997 * Wait for a period of time so that the card can drive SD_CMD and
998 * SD_DAT[3:0] to low after sending back CMD11 response.
1002 /* SD_CMD, SD_DAT[3:0] should be driven to low by card;
1003 * If either one of SD_CMD,SD_DAT[3:0] is not low,
1004 * abort the voltage switch sequence;
1006 err = rtsx_pci_read_register(pcr, SD_BUS_STAT, &stat);
1010 if (stat & (SD_CMD_STATUS | SD_DAT3_STATUS | SD_DAT2_STATUS |
1011 SD_DAT1_STATUS | SD_DAT0_STATUS))
1014 /* Stop toggle SD clock */
1015 err = rtsx_pci_write_register(pcr, SD_BUS_STAT,
1016 0xFF, SD_CLK_FORCE_STOP);
1023 static int sd_wait_voltage_stable_2(struct realtek_pci_sdmmc *host)
1025 struct rtsx_pcr *pcr = host->pcr;
1029 /* Wait 1.8V output of voltage regulator in card stable */
1032 /* Toggle SD clock again */
1033 err = rtsx_pci_write_register(pcr, SD_BUS_STAT, 0xFF, SD_CLK_TOGGLE_EN);
1037 /* Wait for a period of time so that the card can drive
1038 * SD_DAT[3:0] to high at 1.8V
1042 /* SD_CMD, SD_DAT[3:0] should be pulled high by host */
1043 err = rtsx_pci_read_register(pcr, SD_BUS_STAT, &stat);
1047 mask = SD_CMD_STATUS | SD_DAT3_STATUS | SD_DAT2_STATUS |
1048 SD_DAT1_STATUS | SD_DAT0_STATUS;
1049 val = SD_CMD_STATUS | SD_DAT3_STATUS | SD_DAT2_STATUS |
1050 SD_DAT1_STATUS | SD_DAT0_STATUS;
1051 if ((stat & mask) != val) {
1052 dev_dbg(sdmmc_dev(host),
1053 "%s: SD_BUS_STAT = 0x%x\n", __func__, stat);
1054 rtsx_pci_write_register(pcr, SD_BUS_STAT,
1055 SD_CLK_TOGGLE_EN | SD_CLK_FORCE_STOP, 0);
1056 rtsx_pci_write_register(pcr, CARD_CLK_EN, 0xFF, 0);
1063 static int sd_change_bank_voltage(struct realtek_pci_sdmmc *host, u8 voltage)
1065 struct rtsx_pcr *pcr = host->pcr;
1068 if (voltage == SD_IO_3V3) {
1069 err = rtsx_pci_write_phy_register(pcr, 0x08, 0x4FC0 | 0x24);
1072 } else if (voltage == SD_IO_1V8) {
1073 err = rtsx_pci_write_phy_register(pcr, 0x08, 0x4C40 | 0x24);
1083 static int sdmmc_switch_voltage(struct mmc_host *mmc, struct mmc_ios *ios)
1085 struct realtek_pci_sdmmc *host = mmc_priv(mmc);
1086 struct rtsx_pcr *pcr = host->pcr;
1090 dev_dbg(sdmmc_dev(host), "%s: signal_voltage = %d\n",
1091 __func__, ios->signal_voltage);
1096 mutex_lock(&pcr->pcr_mutex);
1098 rtsx_pci_start_run(pcr);
1100 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330)
1101 voltage = SD_IO_3V3;
1103 voltage = SD_IO_1V8;
1105 if (voltage == SD_IO_1V8) {
1106 err = rtsx_pci_write_register(pcr,
1107 SD30_DRIVE_SEL, 0x07, DRIVER_TYPE_B);
1111 err = sd_wait_voltage_stable_1(host);
1116 err = sd_change_bank_voltage(host, voltage);
1120 if (voltage == SD_IO_1V8) {
1121 err = sd_wait_voltage_stable_2(host);
1126 /* Stop toggle SD clock in idle */
1127 err = rtsx_pci_write_register(pcr, SD_BUS_STAT,
1128 SD_CLK_TOGGLE_EN | SD_CLK_FORCE_STOP, 0);
1131 mutex_unlock(&pcr->pcr_mutex);
1136 static int sdmmc_execute_tuning(struct mmc_host *mmc, u32 opcode)
1138 struct realtek_pci_sdmmc *host = mmc_priv(mmc);
1139 struct rtsx_pcr *pcr = host->pcr;
1145 mutex_lock(&pcr->pcr_mutex);
1147 rtsx_pci_start_run(pcr);
1149 if (!host->ddr_mode)
1150 err = sd_tuning_rx(host, MMC_SEND_TUNING_BLOCK);
1152 mutex_unlock(&pcr->pcr_mutex);
1157 static const struct mmc_host_ops realtek_pci_sdmmc_ops = {
1158 .request = sdmmc_request,
1159 .set_ios = sdmmc_set_ios,
1160 .get_ro = sdmmc_get_ro,
1161 .get_cd = sdmmc_get_cd,
1162 .start_signal_voltage_switch = sdmmc_switch_voltage,
1163 .execute_tuning = sdmmc_execute_tuning,
1167 static int rtsx_pci_sdmmc_suspend(struct platform_device *pdev,
1170 struct realtek_pci_sdmmc *host = platform_get_drvdata(pdev);
1171 struct mmc_host *mmc = host->mmc;
1174 dev_dbg(sdmmc_dev(host), "--> %s\n", __func__);
1176 err = mmc_suspend_host(mmc);
1183 static int rtsx_pci_sdmmc_resume(struct platform_device *pdev)
1185 struct realtek_pci_sdmmc *host = platform_get_drvdata(pdev);
1186 struct mmc_host *mmc = host->mmc;
1188 dev_dbg(sdmmc_dev(host), "--> %s\n", __func__);
1190 return mmc_resume_host(mmc);
1192 #else /* CONFIG_PM */
1193 #define rtsx_pci_sdmmc_suspend NULL
1194 #define rtsx_pci_sdmmc_resume NULL
1195 #endif /* CONFIG_PM */
1197 static void init_extra_caps(struct realtek_pci_sdmmc *host)
1199 struct mmc_host *mmc = host->mmc;
1200 struct rtsx_pcr *pcr = host->pcr;
1202 dev_dbg(sdmmc_dev(host), "pcr->extra_caps = 0x%x\n", pcr->extra_caps);
1204 if (pcr->extra_caps & EXTRA_CAPS_SD_SDR50)
1205 mmc->caps |= MMC_CAP_UHS_SDR50;
1206 if (pcr->extra_caps & EXTRA_CAPS_SD_SDR104)
1207 mmc->caps |= MMC_CAP_UHS_SDR104;
1208 if (pcr->extra_caps & EXTRA_CAPS_SD_DDR50)
1209 mmc->caps |= MMC_CAP_UHS_DDR50;
1210 if (pcr->extra_caps & EXTRA_CAPS_MMC_HSDDR)
1211 mmc->caps |= MMC_CAP_1_8V_DDR;
1212 if (pcr->extra_caps & EXTRA_CAPS_MMC_8BIT)
1213 mmc->caps |= MMC_CAP_8_BIT_DATA;
1216 static void realtek_init_host(struct realtek_pci_sdmmc *host)
1218 struct mmc_host *mmc = host->mmc;
1220 mmc->f_min = 250000;
1221 mmc->f_max = 208000000;
1222 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
1223 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SD_HIGHSPEED |
1224 MMC_CAP_MMC_HIGHSPEED | MMC_CAP_BUS_WIDTH_TEST |
1225 MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
1226 mmc->max_current_330 = 400;
1227 mmc->max_current_180 = 800;
1228 mmc->ops = &realtek_pci_sdmmc_ops;
1230 init_extra_caps(host);
1232 mmc->max_segs = 256;
1233 mmc->max_seg_size = 65536;
1234 mmc->max_blk_size = 512;
1235 mmc->max_blk_count = 65535;
1236 mmc->max_req_size = 524288;
1239 static void rtsx_pci_sdmmc_card_event(struct platform_device *pdev)
1241 struct realtek_pci_sdmmc *host = platform_get_drvdata(pdev);
1243 mmc_detect_change(host->mmc, 0);
1246 static int rtsx_pci_sdmmc_drv_probe(struct platform_device *pdev)
1248 struct mmc_host *mmc;
1249 struct realtek_pci_sdmmc *host;
1250 struct rtsx_pcr *pcr;
1251 struct pcr_handle *handle = pdev->dev.platform_data;
1260 dev_dbg(&(pdev->dev), ": Realtek PCI-E SDMMC controller found\n");
1262 mmc = mmc_alloc_host(sizeof(*host), &pdev->dev);
1266 host = mmc_priv(mmc);
1270 platform_set_drvdata(pdev, host);
1271 pcr->slots[RTSX_SD_CARD].p_dev = pdev;
1272 pcr->slots[RTSX_SD_CARD].card_event = rtsx_pci_sdmmc_card_event;
1274 mutex_init(&host->host_mutex);
1276 realtek_init_host(host);
1283 static int rtsx_pci_sdmmc_drv_remove(struct platform_device *pdev)
1285 struct realtek_pci_sdmmc *host = platform_get_drvdata(pdev);
1286 struct rtsx_pcr *pcr;
1287 struct mmc_host *mmc;
1293 pcr->slots[RTSX_SD_CARD].p_dev = NULL;
1294 pcr->slots[RTSX_SD_CARD].card_event = NULL;
1298 mutex_lock(&host->host_mutex);
1300 dev_dbg(&(pdev->dev),
1301 "%s: Controller removed during transfer\n",
1304 rtsx_pci_complete_unfinished_transfer(pcr);
1306 host->mrq->cmd->error = -ENOMEDIUM;
1307 if (host->mrq->stop)
1308 host->mrq->stop->error = -ENOMEDIUM;
1309 mmc_request_done(mmc, host->mrq);
1311 mutex_unlock(&host->host_mutex);
1313 mmc_remove_host(mmc);
1316 platform_set_drvdata(pdev, NULL);
1318 dev_dbg(&(pdev->dev),
1319 ": Realtek PCI-E SDMMC controller has been removed\n");
1324 static struct platform_device_id rtsx_pci_sdmmc_ids[] = {
1326 .name = DRV_NAME_RTSX_PCI_SDMMC,
1331 MODULE_DEVICE_TABLE(platform, rtsx_pci_sdmmc_ids);
1333 static struct platform_driver rtsx_pci_sdmmc_driver = {
1334 .probe = rtsx_pci_sdmmc_drv_probe,
1335 .remove = rtsx_pci_sdmmc_drv_remove,
1336 .id_table = rtsx_pci_sdmmc_ids,
1337 .suspend = rtsx_pci_sdmmc_suspend,
1338 .resume = rtsx_pci_sdmmc_resume,
1340 .owner = THIS_MODULE,
1341 .name = DRV_NAME_RTSX_PCI_SDMMC,
1344 module_platform_driver(rtsx_pci_sdmmc_driver);
1346 MODULE_LICENSE("GPL");
1347 MODULE_AUTHOR("Wei WANG <wei_wang@realsil.com.cn>");
1348 MODULE_DESCRIPTION("Realtek PCI-E SD/MMC Card Host Driver");