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.
7 * Wei WANG <wei_wang@realsil.com.cn>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/highmem.h>
13 #include <linux/delay.h>
14 #include <linux/platform_device.h>
15 #include <linux/workqueue.h>
16 #include <linux/mmc/host.h>
17 #include <linux/mmc/mmc.h>
18 #include <linux/mmc/sd.h>
19 #include <linux/mmc/sdio.h>
20 #include <linux/mmc/card.h>
21 #include <linux/rtsx_pci.h>
22 #include <asm/unaligned.h>
23 #include <linux/pm_runtime.h>
25 struct realtek_pci_sdmmc
{
26 struct platform_device
*pdev
;
29 struct mmc_request
*mrq
;
30 #define SDMMC_WORKQ_NAME "rtsx_pci_sdmmc_workq"
32 struct work_struct work
;
33 struct mutex host_mutex
;
42 #define SDMMC_POWER_ON 1
43 #define SDMMC_POWER_OFF 0
51 static int sdmmc_init_sd_express(struct mmc_host
*mmc
, struct mmc_ios
*ios
);
53 static inline struct device
*sdmmc_dev(struct realtek_pci_sdmmc
*host
)
55 return &(host
->pdev
->dev
);
58 static inline void sd_clear_error(struct realtek_pci_sdmmc
*host
)
60 rtsx_pci_write_register(host
->pcr
, CARD_STOP
,
61 SD_STOP
| SD_CLR_ERR
, SD_STOP
| SD_CLR_ERR
);
65 static void dump_reg_range(struct realtek_pci_sdmmc
*host
, u16 start
, u16 end
)
67 u16 len
= end
- start
+ 1;
71 for (i
= 0; i
< len
; i
+= 8) {
73 int n
= min(8, len
- i
);
75 memset(&data
, 0, sizeof(data
));
76 for (j
= 0; j
< n
; j
++)
77 rtsx_pci_read_register(host
->pcr
, start
+ i
+ j
,
79 dev_dbg(sdmmc_dev(host
), "0x%04X(%d): %8ph\n",
84 static void sd_print_debug_regs(struct realtek_pci_sdmmc
*host
)
86 dump_reg_range(host
, 0xFDA0, 0xFDB3);
87 dump_reg_range(host
, 0xFD52, 0xFD69);
90 #define sd_print_debug_regs(host)
93 static inline int sd_get_cd_int(struct realtek_pci_sdmmc
*host
)
95 return rtsx_pci_readl(host
->pcr
, RTSX_BIPR
) & SD_EXIST
;
98 static void sd_cmd_set_sd_cmd(struct rtsx_pcr
*pcr
, struct mmc_command
*cmd
)
100 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CMD0
, 0xFF,
101 SD_CMD_START
| cmd
->opcode
);
102 rtsx_pci_write_be32(pcr
, SD_CMD1
, cmd
->arg
);
105 static void sd_cmd_set_data_len(struct rtsx_pcr
*pcr
, u16 blocks
, u16 blksz
)
107 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_BLOCK_CNT_L
, 0xFF, blocks
);
108 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_BLOCK_CNT_H
, 0xFF, blocks
>> 8);
109 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_BYTE_CNT_L
, 0xFF, blksz
);
110 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_BYTE_CNT_H
, 0xFF, blksz
>> 8);
113 static int sd_response_type(struct mmc_command
*cmd
)
115 switch (mmc_resp_type(cmd
)) {
117 return SD_RSP_TYPE_R0
;
119 return SD_RSP_TYPE_R1
;
120 case MMC_RSP_R1_NO_CRC
:
121 return SD_RSP_TYPE_R1
| SD_NO_CHECK_CRC7
;
123 return SD_RSP_TYPE_R1b
;
125 return SD_RSP_TYPE_R2
;
127 return SD_RSP_TYPE_R3
;
133 static int sd_status_index(int resp_type
)
135 if (resp_type
== SD_RSP_TYPE_R0
)
137 else if (resp_type
== SD_RSP_TYPE_R2
)
143 * sd_pre_dma_transfer - do dma_map_sg() or using cookie
145 * @pre: if called in pre_req()
147 * 0 - do dma_map_sg()
150 static int sd_pre_dma_transfer(struct realtek_pci_sdmmc
*host
,
151 struct mmc_data
*data
, bool pre
)
153 struct rtsx_pcr
*pcr
= host
->pcr
;
154 int read
= data
->flags
& MMC_DATA_READ
;
156 int using_cookie
= 0;
158 if (!pre
&& data
->host_cookie
&& data
->host_cookie
!= host
->cookie
) {
159 dev_err(sdmmc_dev(host
),
160 "error: data->host_cookie = %d, host->cookie = %d\n",
161 data
->host_cookie
, host
->cookie
);
162 data
->host_cookie
= 0;
165 if (pre
|| data
->host_cookie
!= host
->cookie
) {
166 count
= rtsx_pci_dma_map_sg(pcr
, data
->sg
, data
->sg_len
, read
);
168 count
= host
->cookie_sg_count
;
173 host
->cookie_sg_count
= count
;
174 if (++host
->cookie
< 0)
176 data
->host_cookie
= host
->cookie
;
178 host
->sg_count
= count
;
184 static void sdmmc_pre_req(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
186 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
187 struct mmc_data
*data
= mrq
->data
;
189 if (data
->host_cookie
) {
190 dev_err(sdmmc_dev(host
),
191 "error: reset data->host_cookie = %d\n",
193 data
->host_cookie
= 0;
196 sd_pre_dma_transfer(host
, data
, true);
197 dev_dbg(sdmmc_dev(host
), "pre dma sg: %d\n", host
->cookie_sg_count
);
200 static void sdmmc_post_req(struct mmc_host
*mmc
, struct mmc_request
*mrq
,
203 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
204 struct rtsx_pcr
*pcr
= host
->pcr
;
205 struct mmc_data
*data
= mrq
->data
;
206 int read
= data
->flags
& MMC_DATA_READ
;
208 rtsx_pci_dma_unmap_sg(pcr
, data
->sg
, data
->sg_len
, read
);
209 data
->host_cookie
= 0;
212 static void sd_send_cmd_get_rsp(struct realtek_pci_sdmmc
*host
,
213 struct mmc_command
*cmd
)
215 struct rtsx_pcr
*pcr
= host
->pcr
;
216 u8 cmd_idx
= (u8
)cmd
->opcode
;
224 bool clock_toggled
= false;
226 dev_dbg(sdmmc_dev(host
), "%s: SD/MMC CMD %d, arg = 0x%08x\n",
227 __func__
, cmd_idx
, arg
);
229 rsp_type
= sd_response_type(cmd
);
233 stat_idx
= sd_status_index(rsp_type
);
235 if (rsp_type
== SD_RSP_TYPE_R1b
)
236 timeout
= cmd
->busy_timeout
? cmd
->busy_timeout
: 3000;
238 if (cmd
->opcode
== SD_SWITCH_VOLTAGE
) {
239 err
= rtsx_pci_write_register(pcr
, SD_BUS_STAT
,
240 0xFF, SD_CLK_TOGGLE_EN
);
244 clock_toggled
= true;
247 rtsx_pci_init_cmd(pcr
);
248 sd_cmd_set_sd_cmd(pcr
, cmd
);
249 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG2
, 0xFF, rsp_type
);
250 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_DATA_SOURCE
,
251 0x01, PINGPONG_BUFFER
);
252 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_TRANSFER
,
253 0xFF, SD_TM_CMD_RSP
| SD_TRANSFER_START
);
254 rtsx_pci_add_cmd(pcr
, CHECK_REG_CMD
, SD_TRANSFER
,
255 SD_TRANSFER_END
| SD_STAT_IDLE
,
256 SD_TRANSFER_END
| SD_STAT_IDLE
);
258 if (rsp_type
== SD_RSP_TYPE_R2
) {
259 /* Read data from ping-pong buffer */
260 for (i
= PPBUF_BASE2
; i
< PPBUF_BASE2
+ 16; i
++)
261 rtsx_pci_add_cmd(pcr
, READ_REG_CMD
, (u16
)i
, 0, 0);
262 } else if (rsp_type
!= SD_RSP_TYPE_R0
) {
263 /* Read data from SD_CMDx registers */
264 for (i
= SD_CMD0
; i
<= SD_CMD4
; i
++)
265 rtsx_pci_add_cmd(pcr
, READ_REG_CMD
, (u16
)i
, 0, 0);
268 rtsx_pci_add_cmd(pcr
, READ_REG_CMD
, SD_STAT1
, 0, 0);
270 err
= rtsx_pci_send_cmd(pcr
, timeout
);
272 sd_print_debug_regs(host
);
273 sd_clear_error(host
);
274 dev_dbg(sdmmc_dev(host
),
275 "rtsx_pci_send_cmd error (err = %d)\n", err
);
279 if (rsp_type
== SD_RSP_TYPE_R0
) {
284 /* Eliminate returned value of CHECK_REG_CMD */
285 ptr
= rtsx_pci_get_cmd_data(pcr
) + 1;
287 /* Check (Start,Transmission) bit of Response */
288 if ((ptr
[0] & 0xC0) != 0) {
290 dev_dbg(sdmmc_dev(host
), "Invalid response bit\n");
295 if (!(rsp_type
& SD_NO_CHECK_CRC7
)) {
296 if (ptr
[stat_idx
] & SD_CRC7_ERR
) {
298 dev_dbg(sdmmc_dev(host
), "CRC7 error\n");
303 if (rsp_type
== SD_RSP_TYPE_R2
) {
305 * The controller offloads the last byte {CRC-7, end bit 1'b1}
306 * of response type R2. Assign dummy CRC, 0, and end bit to the
307 * byte(ptr[16], goes into the LSB of resp[3] later).
311 for (i
= 0; i
< 4; i
++) {
312 cmd
->resp
[i
] = get_unaligned_be32(ptr
+ 1 + i
* 4);
313 dev_dbg(sdmmc_dev(host
), "cmd->resp[%d] = 0x%08x\n",
317 cmd
->resp
[0] = get_unaligned_be32(ptr
+ 1);
318 dev_dbg(sdmmc_dev(host
), "cmd->resp[0] = 0x%08x\n",
325 if (err
&& clock_toggled
)
326 rtsx_pci_write_register(pcr
, SD_BUS_STAT
,
327 SD_CLK_TOGGLE_EN
| SD_CLK_FORCE_STOP
, 0);
330 static int sd_read_data(struct realtek_pci_sdmmc
*host
, struct mmc_command
*cmd
,
331 u16 byte_cnt
, u8
*buf
, int buf_len
, int timeout
)
333 struct rtsx_pcr
*pcr
= host
->pcr
;
337 dev_dbg(sdmmc_dev(host
), "%s: SD/MMC CMD %d, arg = 0x%08x\n",
338 __func__
, cmd
->opcode
, cmd
->arg
);
343 if (cmd
->opcode
== MMC_SEND_TUNING_BLOCK
)
344 trans_mode
= SD_TM_AUTO_TUNING
;
346 trans_mode
= SD_TM_NORMAL_READ
;
348 rtsx_pci_init_cmd(pcr
);
349 sd_cmd_set_sd_cmd(pcr
, cmd
);
350 sd_cmd_set_data_len(pcr
, 1, byte_cnt
);
351 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG2
, 0xFF,
352 SD_CALCULATE_CRC7
| SD_CHECK_CRC16
|
353 SD_NO_WAIT_BUSY_END
| SD_CHECK_CRC7
| SD_RSP_LEN_6
);
354 if (trans_mode
!= SD_TM_AUTO_TUNING
)
355 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
,
356 CARD_DATA_SOURCE
, 0x01, PINGPONG_BUFFER
);
358 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_TRANSFER
,
359 0xFF, trans_mode
| SD_TRANSFER_START
);
360 rtsx_pci_add_cmd(pcr
, CHECK_REG_CMD
, SD_TRANSFER
,
361 SD_TRANSFER_END
, SD_TRANSFER_END
);
363 err
= rtsx_pci_send_cmd(pcr
, timeout
);
365 sd_print_debug_regs(host
);
366 dev_dbg(sdmmc_dev(host
),
367 "rtsx_pci_send_cmd fail (err = %d)\n", err
);
371 if (buf
&& buf_len
) {
372 err
= rtsx_pci_read_ppbuf(pcr
, buf
, buf_len
);
374 dev_dbg(sdmmc_dev(host
),
375 "rtsx_pci_read_ppbuf fail (err = %d)\n", err
);
383 static int sd_write_data(struct realtek_pci_sdmmc
*host
,
384 struct mmc_command
*cmd
, u16 byte_cnt
, u8
*buf
, int buf_len
,
387 struct rtsx_pcr
*pcr
= host
->pcr
;
390 dev_dbg(sdmmc_dev(host
), "%s: SD/MMC CMD %d, arg = 0x%08x\n",
391 __func__
, cmd
->opcode
, cmd
->arg
);
396 sd_send_cmd_get_rsp(host
, cmd
);
400 if (buf
&& buf_len
) {
401 err
= rtsx_pci_write_ppbuf(pcr
, buf
, buf_len
);
403 dev_dbg(sdmmc_dev(host
),
404 "rtsx_pci_write_ppbuf fail (err = %d)\n", err
);
409 rtsx_pci_init_cmd(pcr
);
410 sd_cmd_set_data_len(pcr
, 1, byte_cnt
);
411 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG2
, 0xFF,
412 SD_CALCULATE_CRC7
| SD_CHECK_CRC16
|
413 SD_NO_WAIT_BUSY_END
| SD_CHECK_CRC7
| SD_RSP_LEN_0
);
414 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_TRANSFER
, 0xFF,
415 SD_TRANSFER_START
| SD_TM_AUTO_WRITE_3
);
416 rtsx_pci_add_cmd(pcr
, CHECK_REG_CMD
, SD_TRANSFER
,
417 SD_TRANSFER_END
, SD_TRANSFER_END
);
419 err
= rtsx_pci_send_cmd(pcr
, timeout
);
421 sd_print_debug_regs(host
);
422 dev_dbg(sdmmc_dev(host
),
423 "rtsx_pci_send_cmd fail (err = %d)\n", err
);
430 static int sd_read_long_data(struct realtek_pci_sdmmc
*host
,
431 struct mmc_request
*mrq
)
433 struct rtsx_pcr
*pcr
= host
->pcr
;
434 struct mmc_host
*mmc
= host
->mmc
;
435 struct mmc_card
*card
= mmc
->card
;
436 struct mmc_command
*cmd
= mrq
->cmd
;
437 struct mmc_data
*data
= mrq
->data
;
438 int uhs
= mmc_card_uhs(card
);
442 size_t data_len
= data
->blksz
* data
->blocks
;
444 dev_dbg(sdmmc_dev(host
), "%s: SD/MMC CMD %d, arg = 0x%08x\n",
445 __func__
, cmd
->opcode
, cmd
->arg
);
447 resp_type
= sd_response_type(cmd
);
452 cfg2
|= SD_NO_CHECK_WAIT_CRC_TO
;
454 rtsx_pci_init_cmd(pcr
);
455 sd_cmd_set_sd_cmd(pcr
, cmd
);
456 sd_cmd_set_data_len(pcr
, data
->blocks
, data
->blksz
);
457 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, IRQSTAT0
,
458 DMA_DONE_INT
, DMA_DONE_INT
);
459 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC3
,
460 0xFF, (u8
)(data_len
>> 24));
461 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC2
,
462 0xFF, (u8
)(data_len
>> 16));
463 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC1
,
464 0xFF, (u8
)(data_len
>> 8));
465 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC0
, 0xFF, (u8
)data_len
);
466 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMACTL
,
467 0x03 | DMA_PACK_SIZE_MASK
,
468 DMA_DIR_FROM_CARD
| DMA_EN
| DMA_512
);
469 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_DATA_SOURCE
,
471 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG2
, 0xFF, cfg2
| resp_type
);
472 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_TRANSFER
, 0xFF,
473 SD_TRANSFER_START
| SD_TM_AUTO_READ_2
);
474 rtsx_pci_add_cmd(pcr
, CHECK_REG_CMD
, SD_TRANSFER
,
475 SD_TRANSFER_END
, SD_TRANSFER_END
);
476 rtsx_pci_send_cmd_no_wait(pcr
);
478 err
= rtsx_pci_dma_transfer(pcr
, data
->sg
, host
->sg_count
, 1, 10000);
480 sd_print_debug_regs(host
);
481 sd_clear_error(host
);
488 static int sd_write_long_data(struct realtek_pci_sdmmc
*host
,
489 struct mmc_request
*mrq
)
491 struct rtsx_pcr
*pcr
= host
->pcr
;
492 struct mmc_host
*mmc
= host
->mmc
;
493 struct mmc_card
*card
= mmc
->card
;
494 struct mmc_command
*cmd
= mrq
->cmd
;
495 struct mmc_data
*data
= mrq
->data
;
496 int uhs
= mmc_card_uhs(card
);
499 size_t data_len
= data
->blksz
* data
->blocks
;
501 sd_send_cmd_get_rsp(host
, cmd
);
505 dev_dbg(sdmmc_dev(host
), "%s: SD/MMC CMD %d, arg = 0x%08x\n",
506 __func__
, cmd
->opcode
, cmd
->arg
);
508 cfg2
= SD_NO_CALCULATE_CRC7
| SD_CHECK_CRC16
|
509 SD_NO_WAIT_BUSY_END
| SD_NO_CHECK_CRC7
| SD_RSP_LEN_0
;
512 cfg2
|= SD_NO_CHECK_WAIT_CRC_TO
;
514 rtsx_pci_init_cmd(pcr
);
515 sd_cmd_set_data_len(pcr
, data
->blocks
, data
->blksz
);
516 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, IRQSTAT0
,
517 DMA_DONE_INT
, DMA_DONE_INT
);
518 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC3
,
519 0xFF, (u8
)(data_len
>> 24));
520 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC2
,
521 0xFF, (u8
)(data_len
>> 16));
522 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC1
,
523 0xFF, (u8
)(data_len
>> 8));
524 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC0
, 0xFF, (u8
)data_len
);
525 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMACTL
,
526 0x03 | DMA_PACK_SIZE_MASK
,
527 DMA_DIR_TO_CARD
| DMA_EN
| DMA_512
);
528 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_DATA_SOURCE
,
530 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG2
, 0xFF, cfg2
);
531 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_TRANSFER
, 0xFF,
532 SD_TRANSFER_START
| SD_TM_AUTO_WRITE_3
);
533 rtsx_pci_add_cmd(pcr
, CHECK_REG_CMD
, SD_TRANSFER
,
534 SD_TRANSFER_END
, SD_TRANSFER_END
);
535 rtsx_pci_send_cmd_no_wait(pcr
);
536 err
= rtsx_pci_dma_transfer(pcr
, data
->sg
, host
->sg_count
, 0, 10000);
538 sd_clear_error(host
);
545 static int sd_rw_multi(struct realtek_pci_sdmmc
*host
, struct mmc_request
*mrq
)
547 struct mmc_data
*data
= mrq
->data
;
549 if (host
->sg_count
< 0) {
550 data
->error
= host
->sg_count
;
551 dev_dbg(sdmmc_dev(host
), "%s: sg_count = %d is invalid\n",
552 __func__
, host
->sg_count
);
556 if (data
->flags
& MMC_DATA_READ
)
557 return sd_read_long_data(host
, mrq
);
559 return sd_write_long_data(host
, mrq
);
562 static inline void sd_enable_initial_mode(struct realtek_pci_sdmmc
*host
)
564 rtsx_pci_write_register(host
->pcr
, SD_CFG1
,
565 SD_CLK_DIVIDE_MASK
, SD_CLK_DIVIDE_128
);
568 static inline void sd_disable_initial_mode(struct realtek_pci_sdmmc
*host
)
570 rtsx_pci_write_register(host
->pcr
, SD_CFG1
,
571 SD_CLK_DIVIDE_MASK
, SD_CLK_DIVIDE_0
);
574 static void sd_normal_rw(struct realtek_pci_sdmmc
*host
,
575 struct mmc_request
*mrq
)
577 struct mmc_command
*cmd
= mrq
->cmd
;
578 struct mmc_data
*data
= mrq
->data
;
581 buf
= kzalloc(data
->blksz
, GFP_NOIO
);
583 cmd
->error
= -ENOMEM
;
587 if (data
->flags
& MMC_DATA_READ
) {
588 if (host
->initial_mode
)
589 sd_disable_initial_mode(host
);
591 cmd
->error
= sd_read_data(host
, cmd
, (u16
)data
->blksz
, buf
,
594 if (host
->initial_mode
)
595 sd_enable_initial_mode(host
);
597 sg_copy_from_buffer(data
->sg
, data
->sg_len
, buf
, data
->blksz
);
599 sg_copy_to_buffer(data
->sg
, data
->sg_len
, buf
, data
->blksz
);
601 cmd
->error
= sd_write_data(host
, cmd
, (u16
)data
->blksz
, buf
,
608 static int sd_change_phase(struct realtek_pci_sdmmc
*host
,
609 u8 sample_point
, bool rx
)
611 struct rtsx_pcr
*pcr
= host
->pcr
;
613 dev_dbg(sdmmc_dev(host
), "%s(%s): sample_point = %d\n",
614 __func__
, rx
? "RX" : "TX", sample_point
);
616 rtsx_pci_write_register(pcr
, CLK_CTL
, CHANGE_CLK
, CHANGE_CLK
);
618 SD_VP_CTL
= SD_VPRX_CTL
;
619 rtsx_pci_write_register(pcr
, SD_VPRX_CTL
,
620 PHASE_SELECT_MASK
, sample_point
);
622 SD_VP_CTL
= SD_VPTX_CTL
;
623 rtsx_pci_write_register(pcr
, SD_VPTX_CTL
,
624 PHASE_SELECT_MASK
, sample_point
);
626 rtsx_pci_write_register(pcr
, SD_VP_CTL
, PHASE_NOT_RESET
, 0);
627 rtsx_pci_write_register(pcr
, SD_VP_CTL
, PHASE_NOT_RESET
,
629 rtsx_pci_write_register(pcr
, CLK_CTL
, CHANGE_CLK
, 0);
630 rtsx_pci_write_register(pcr
, SD_CFG1
, SD_ASYNC_FIFO_NOT_RST
, 0);
635 static inline u32
test_phase_bit(u32 phase_map
, unsigned int bit
)
637 bit
%= RTSX_PHASE_MAX
;
638 return phase_map
& (1 << bit
);
641 static int sd_get_phase_len(u32 phase_map
, unsigned int start_bit
)
645 for (i
= 0; i
< RTSX_PHASE_MAX
; i
++) {
646 if (test_phase_bit(phase_map
, start_bit
+ i
) == 0)
649 return RTSX_PHASE_MAX
;
652 static u8
sd_search_final_phase(struct realtek_pci_sdmmc
*host
, u32 phase_map
)
654 int start
= 0, len
= 0;
655 int start_final
= 0, len_final
= 0;
656 u8 final_phase
= 0xFF;
658 if (phase_map
== 0) {
659 dev_err(sdmmc_dev(host
), "phase error: [map:%x]\n", phase_map
);
663 while (start
< RTSX_PHASE_MAX
) {
664 len
= sd_get_phase_len(phase_map
, start
);
665 if (len_final
< len
) {
669 start
+= len
? len
: 1;
672 final_phase
= (start_final
+ len_final
/ 2) % RTSX_PHASE_MAX
;
673 dev_dbg(sdmmc_dev(host
), "phase: [map:%x] [maxlen:%d] [final:%d]\n",
674 phase_map
, len_final
, final_phase
);
679 static void sd_wait_data_idle(struct realtek_pci_sdmmc
*host
)
684 for (i
= 0; i
< 100; i
++) {
685 rtsx_pci_read_register(host
->pcr
, SD_DATA_STATE
, &val
);
686 if (val
& SD_DATA_IDLE
)
693 static int sd_tuning_rx_cmd(struct realtek_pci_sdmmc
*host
,
694 u8 opcode
, u8 sample_point
)
697 struct mmc_command cmd
= {};
698 struct rtsx_pcr
*pcr
= host
->pcr
;
700 sd_change_phase(host
, sample_point
, true);
702 rtsx_pci_write_register(pcr
, SD_CFG3
, SD_RSP_80CLK_TIMEOUT_EN
,
703 SD_RSP_80CLK_TIMEOUT_EN
);
706 err
= sd_read_data(host
, &cmd
, 0x40, NULL
, 0, 100);
708 /* Wait till SD DATA IDLE */
709 sd_wait_data_idle(host
);
710 sd_clear_error(host
);
711 rtsx_pci_write_register(pcr
, SD_CFG3
,
712 SD_RSP_80CLK_TIMEOUT_EN
, 0);
716 rtsx_pci_write_register(pcr
, SD_CFG3
, SD_RSP_80CLK_TIMEOUT_EN
, 0);
720 static int sd_tuning_phase(struct realtek_pci_sdmmc
*host
,
721 u8 opcode
, u32
*phase_map
)
724 u32 raw_phase_map
= 0;
726 for (i
= 0; i
< RTSX_PHASE_MAX
; i
++) {
727 err
= sd_tuning_rx_cmd(host
, opcode
, (u8
)i
);
729 raw_phase_map
|= 1 << i
;
733 *phase_map
= raw_phase_map
;
738 static int sd_tuning_rx(struct realtek_pci_sdmmc
*host
, u8 opcode
)
741 u32 raw_phase_map
[RX_TUNING_CNT
] = {0}, phase_map
;
744 for (i
= 0; i
< RX_TUNING_CNT
; i
++) {
745 err
= sd_tuning_phase(host
, opcode
, &(raw_phase_map
[i
]));
749 if (raw_phase_map
[i
] == 0)
753 phase_map
= 0xFFFFFFFF;
754 for (i
= 0; i
< RX_TUNING_CNT
; i
++) {
755 dev_dbg(sdmmc_dev(host
), "RX raw_phase_map[%d] = 0x%08x\n",
756 i
, raw_phase_map
[i
]);
757 phase_map
&= raw_phase_map
[i
];
759 dev_dbg(sdmmc_dev(host
), "RX phase_map = 0x%08x\n", phase_map
);
762 final_phase
= sd_search_final_phase(host
, phase_map
);
763 if (final_phase
== 0xFF)
766 err
= sd_change_phase(host
, final_phase
, true);
776 static inline int sdio_extblock_cmd(struct mmc_command
*cmd
,
777 struct mmc_data
*data
)
779 return (cmd
->opcode
== SD_IO_RW_EXTENDED
) && (data
->blksz
== 512);
782 static inline int sd_rw_cmd(struct mmc_command
*cmd
)
784 return mmc_op_multi(cmd
->opcode
) ||
785 (cmd
->opcode
== MMC_READ_SINGLE_BLOCK
) ||
786 (cmd
->opcode
== MMC_WRITE_BLOCK
);
789 static void sd_request(struct work_struct
*work
)
791 struct realtek_pci_sdmmc
*host
= container_of(work
,
792 struct realtek_pci_sdmmc
, work
);
793 struct rtsx_pcr
*pcr
= host
->pcr
;
795 struct mmc_host
*mmc
= host
->mmc
;
796 struct mmc_request
*mrq
= host
->mrq
;
797 struct mmc_command
*cmd
= mrq
->cmd
;
798 struct mmc_data
*data
= mrq
->data
;
800 unsigned int data_size
= 0;
803 if (host
->eject
|| !sd_get_cd_int(host
)) {
804 cmd
->error
= -ENOMEDIUM
;
808 err
= rtsx_pci_card_exclusive_check(host
->pcr
, RTSX_SD_CARD
);
814 mutex_lock(&pcr
->pcr_mutex
);
816 rtsx_pci_start_run(pcr
);
818 rtsx_pci_switch_clock(pcr
, host
->clock
, host
->ssc_depth
,
819 host
->initial_mode
, host
->double_clk
, host
->vpclk
);
820 rtsx_pci_write_register(pcr
, CARD_SELECT
, 0x07, SD_MOD_SEL
);
821 rtsx_pci_write_register(pcr
, CARD_SHARE_MODE
,
822 CARD_SHARE_MASK
, CARD_SHARE_48_SD
);
824 mutex_lock(&host
->host_mutex
);
826 mutex_unlock(&host
->host_mutex
);
829 data_size
= data
->blocks
* data
->blksz
;
832 sd_send_cmd_get_rsp(host
, cmd
);
833 } else if (sd_rw_cmd(cmd
) || sdio_extblock_cmd(cmd
, data
)) {
834 cmd
->error
= sd_rw_multi(host
, mrq
);
835 if (!host
->using_cookie
)
836 sdmmc_post_req(host
->mmc
, host
->mrq
, 0);
838 if (mmc_op_multi(cmd
->opcode
) && mrq
->stop
)
839 sd_send_cmd_get_rsp(host
, mrq
->stop
);
841 sd_normal_rw(host
, mrq
);
845 if (cmd
->error
|| data
->error
)
846 data
->bytes_xfered
= 0;
848 data
->bytes_xfered
= data
->blocks
* data
->blksz
;
851 mutex_unlock(&pcr
->pcr_mutex
);
855 dev_dbg(sdmmc_dev(host
), "CMD %d 0x%08x error(%d)\n",
856 cmd
->opcode
, cmd
->arg
, cmd
->error
);
859 mutex_lock(&host
->host_mutex
);
861 mutex_unlock(&host
->host_mutex
);
863 mmc_request_done(mmc
, mrq
);
866 static void sdmmc_request(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
868 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
869 struct mmc_data
*data
= mrq
->data
;
871 mutex_lock(&host
->host_mutex
);
873 mutex_unlock(&host
->host_mutex
);
875 if (sd_rw_cmd(mrq
->cmd
) || sdio_extblock_cmd(mrq
->cmd
, data
))
876 host
->using_cookie
= sd_pre_dma_transfer(host
, data
, false);
878 schedule_work(&host
->work
);
881 static int sd_set_bus_width(struct realtek_pci_sdmmc
*host
,
882 unsigned char bus_width
)
886 [MMC_BUS_WIDTH_1
] = SD_BUS_WIDTH_1BIT
,
887 [MMC_BUS_WIDTH_4
] = SD_BUS_WIDTH_4BIT
,
888 [MMC_BUS_WIDTH_8
] = SD_BUS_WIDTH_8BIT
,
891 if (bus_width
<= MMC_BUS_WIDTH_8
)
892 err
= rtsx_pci_write_register(host
->pcr
, SD_CFG1
,
893 0x03, width
[bus_width
]);
898 static int sd_power_on(struct realtek_pci_sdmmc
*host
)
900 struct rtsx_pcr
*pcr
= host
->pcr
;
901 struct mmc_host
*mmc
= host
->mmc
;
906 if (host
->power_state
== SDMMC_POWER_ON
)
909 rtsx_pci_init_cmd(pcr
);
910 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_SELECT
, 0x07, SD_MOD_SEL
);
911 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_SHARE_MODE
,
912 CARD_SHARE_MASK
, CARD_SHARE_48_SD
);
913 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_CLK_EN
,
914 SD_CLK_EN
, SD_CLK_EN
);
915 err
= rtsx_pci_send_cmd(pcr
, 100);
919 err
= rtsx_pci_card_pull_ctl_enable(pcr
, RTSX_SD_CARD
);
923 err
= rtsx_pci_card_power_on(pcr
, RTSX_SD_CARD
);
927 err
= rtsx_pci_write_register(pcr
, CARD_OE
, SD_OUTPUT_EN
, SD_OUTPUT_EN
);
931 if (PCI_PID(pcr
) == PID_5261
) {
933 * If test mode is set switch to SD Express mandatorily,
934 * this is only for factory testing.
936 rtsx_pci_read_register(pcr
, RTS5261_FW_CFG_INFO0
, &test_mode
);
937 if (test_mode
& RTS5261_FW_EXPRESS_TEST_MASK
) {
938 sdmmc_init_sd_express(mmc
, NULL
);
941 if (pcr
->extra_caps
& EXTRA_CAPS_SD_EXPRESS
)
942 mmc
->caps2
|= MMC_CAP2_SD_EXP
| MMC_CAP2_SD_EXP_1_2V
;
944 * HW read wp status when resuming from S3/S4,
945 * and then picks SD legacy interface if it's set
948 val
= rtsx_pci_readl(pcr
, RTSX_BIPR
);
949 if (val
& SD_WRITE_PROTECT
) {
950 pcr
->extra_caps
&= ~EXTRA_CAPS_SD_EXPRESS
;
951 mmc
->caps2
&= ~(MMC_CAP2_SD_EXP
| MMC_CAP2_SD_EXP_1_2V
);
955 host
->power_state
= SDMMC_POWER_ON
;
959 static int sd_power_off(struct realtek_pci_sdmmc
*host
)
961 struct rtsx_pcr
*pcr
= host
->pcr
;
964 host
->power_state
= SDMMC_POWER_OFF
;
966 rtsx_pci_init_cmd(pcr
);
968 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_CLK_EN
, SD_CLK_EN
, 0);
969 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_OE
, SD_OUTPUT_EN
, 0);
971 err
= rtsx_pci_send_cmd(pcr
, 100);
975 err
= rtsx_pci_card_power_off(pcr
, RTSX_SD_CARD
);
979 return rtsx_pci_card_pull_ctl_disable(pcr
, RTSX_SD_CARD
);
982 static int sd_set_power_mode(struct realtek_pci_sdmmc
*host
,
983 unsigned char power_mode
)
987 if (power_mode
== MMC_POWER_OFF
)
988 err
= sd_power_off(host
);
990 err
= sd_power_on(host
);
995 static int sd_set_timing(struct realtek_pci_sdmmc
*host
, unsigned char timing
)
997 struct rtsx_pcr
*pcr
= host
->pcr
;
1000 rtsx_pci_init_cmd(pcr
);
1003 case MMC_TIMING_UHS_SDR104
:
1004 case MMC_TIMING_UHS_SDR50
:
1005 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG1
,
1006 0x0C | SD_ASYNC_FIFO_NOT_RST
,
1007 SD_30_MODE
| SD_ASYNC_FIFO_NOT_RST
);
1008 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
,
1009 CLK_LOW_FREQ
, CLK_LOW_FREQ
);
1010 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_CLK_SOURCE
, 0xFF,
1011 CRC_VAR_CLK0
| SD30_FIX_CLK
| SAMPLE_VAR_CLK1
);
1012 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
, CLK_LOW_FREQ
, 0);
1015 case MMC_TIMING_MMC_DDR52
:
1016 case MMC_TIMING_UHS_DDR50
:
1017 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG1
,
1018 0x0C | SD_ASYNC_FIFO_NOT_RST
,
1019 SD_DDR_MODE
| SD_ASYNC_FIFO_NOT_RST
);
1020 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
,
1021 CLK_LOW_FREQ
, CLK_LOW_FREQ
);
1022 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_CLK_SOURCE
, 0xFF,
1023 CRC_VAR_CLK0
| SD30_FIX_CLK
| SAMPLE_VAR_CLK1
);
1024 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
, CLK_LOW_FREQ
, 0);
1025 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_PUSH_POINT_CTL
,
1026 DDR_VAR_TX_CMD_DAT
, DDR_VAR_TX_CMD_DAT
);
1027 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_SAMPLE_POINT_CTL
,
1028 DDR_VAR_RX_DAT
| DDR_VAR_RX_CMD
,
1029 DDR_VAR_RX_DAT
| DDR_VAR_RX_CMD
);
1032 case MMC_TIMING_MMC_HS
:
1033 case MMC_TIMING_SD_HS
:
1034 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG1
,
1036 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
,
1037 CLK_LOW_FREQ
, CLK_LOW_FREQ
);
1038 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_CLK_SOURCE
, 0xFF,
1039 CRC_FIX_CLK
| SD30_VAR_CLK0
| SAMPLE_VAR_CLK1
);
1040 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
, CLK_LOW_FREQ
, 0);
1041 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_PUSH_POINT_CTL
,
1042 SD20_TX_SEL_MASK
, SD20_TX_14_AHEAD
);
1043 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_SAMPLE_POINT_CTL
,
1044 SD20_RX_SEL_MASK
, SD20_RX_14_DELAY
);
1048 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
,
1049 SD_CFG1
, 0x0C, SD_20_MODE
);
1050 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
,
1051 CLK_LOW_FREQ
, CLK_LOW_FREQ
);
1052 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_CLK_SOURCE
, 0xFF,
1053 CRC_FIX_CLK
| SD30_VAR_CLK0
| SAMPLE_VAR_CLK1
);
1054 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
, CLK_LOW_FREQ
, 0);
1055 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
,
1056 SD_PUSH_POINT_CTL
, 0xFF, 0);
1057 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_SAMPLE_POINT_CTL
,
1058 SD20_RX_SEL_MASK
, SD20_RX_POS_EDGE
);
1062 err
= rtsx_pci_send_cmd(pcr
, 100);
1067 static void sdmmc_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
1069 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
1070 struct rtsx_pcr
*pcr
= host
->pcr
;
1075 if (rtsx_pci_card_exclusive_check(host
->pcr
, RTSX_SD_CARD
))
1078 mutex_lock(&pcr
->pcr_mutex
);
1080 rtsx_pci_start_run(pcr
);
1082 sd_set_bus_width(host
, ios
->bus_width
);
1083 sd_set_power_mode(host
, ios
->power_mode
);
1084 sd_set_timing(host
, ios
->timing
);
1086 host
->vpclk
= false;
1087 host
->double_clk
= true;
1089 switch (ios
->timing
) {
1090 case MMC_TIMING_UHS_SDR104
:
1091 case MMC_TIMING_UHS_SDR50
:
1092 host
->ssc_depth
= RTSX_SSC_DEPTH_2M
;
1094 host
->double_clk
= false;
1096 case MMC_TIMING_MMC_DDR52
:
1097 case MMC_TIMING_UHS_DDR50
:
1098 case MMC_TIMING_UHS_SDR25
:
1099 host
->ssc_depth
= RTSX_SSC_DEPTH_1M
;
1102 host
->ssc_depth
= RTSX_SSC_DEPTH_500K
;
1106 host
->initial_mode
= (ios
->clock
<= 1000000) ? true : false;
1108 host
->clock
= ios
->clock
;
1109 rtsx_pci_switch_clock(pcr
, ios
->clock
, host
->ssc_depth
,
1110 host
->initial_mode
, host
->double_clk
, host
->vpclk
);
1112 mutex_unlock(&pcr
->pcr_mutex
);
1115 static int sdmmc_get_ro(struct mmc_host
*mmc
)
1117 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
1118 struct rtsx_pcr
*pcr
= host
->pcr
;
1125 mutex_lock(&pcr
->pcr_mutex
);
1127 rtsx_pci_start_run(pcr
);
1129 /* Check SD mechanical write-protect switch */
1130 val
= rtsx_pci_readl(pcr
, RTSX_BIPR
);
1131 dev_dbg(sdmmc_dev(host
), "%s: RTSX_BIPR = 0x%08x\n", __func__
, val
);
1132 if (val
& SD_WRITE_PROTECT
)
1135 mutex_unlock(&pcr
->pcr_mutex
);
1140 static int sdmmc_get_cd(struct mmc_host
*mmc
)
1142 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
1143 struct rtsx_pcr
*pcr
= host
->pcr
;
1150 mutex_lock(&pcr
->pcr_mutex
);
1152 rtsx_pci_start_run(pcr
);
1154 /* Check SD card detect */
1155 val
= rtsx_pci_card_exist(pcr
);
1156 dev_dbg(sdmmc_dev(host
), "%s: RTSX_BIPR = 0x%08x\n", __func__
, val
);
1160 mutex_unlock(&pcr
->pcr_mutex
);
1165 static int sd_wait_voltage_stable_1(struct realtek_pci_sdmmc
*host
)
1167 struct rtsx_pcr
*pcr
= host
->pcr
;
1171 /* Reference to Signal Voltage Switch Sequence in SD spec.
1172 * Wait for a period of time so that the card can drive SD_CMD and
1173 * SD_DAT[3:0] to low after sending back CMD11 response.
1177 /* SD_CMD, SD_DAT[3:0] should be driven to low by card;
1178 * If either one of SD_CMD,SD_DAT[3:0] is not low,
1179 * abort the voltage switch sequence;
1181 err
= rtsx_pci_read_register(pcr
, SD_BUS_STAT
, &stat
);
1185 if (stat
& (SD_CMD_STATUS
| SD_DAT3_STATUS
| SD_DAT2_STATUS
|
1186 SD_DAT1_STATUS
| SD_DAT0_STATUS
))
1189 /* Stop toggle SD clock */
1190 err
= rtsx_pci_write_register(pcr
, SD_BUS_STAT
,
1191 0xFF, SD_CLK_FORCE_STOP
);
1198 static int sd_wait_voltage_stable_2(struct realtek_pci_sdmmc
*host
)
1200 struct rtsx_pcr
*pcr
= host
->pcr
;
1204 /* Wait 1.8V output of voltage regulator in card stable */
1207 /* Toggle SD clock again */
1208 err
= rtsx_pci_write_register(pcr
, SD_BUS_STAT
, 0xFF, SD_CLK_TOGGLE_EN
);
1212 /* Wait for a period of time so that the card can drive
1213 * SD_DAT[3:0] to high at 1.8V
1217 /* SD_CMD, SD_DAT[3:0] should be pulled high by host */
1218 err
= rtsx_pci_read_register(pcr
, SD_BUS_STAT
, &stat
);
1222 mask
= SD_CMD_STATUS
| SD_DAT3_STATUS
| SD_DAT2_STATUS
|
1223 SD_DAT1_STATUS
| SD_DAT0_STATUS
;
1224 val
= SD_CMD_STATUS
| SD_DAT3_STATUS
| SD_DAT2_STATUS
|
1225 SD_DAT1_STATUS
| SD_DAT0_STATUS
;
1226 if ((stat
& mask
) != val
) {
1227 dev_dbg(sdmmc_dev(host
),
1228 "%s: SD_BUS_STAT = 0x%x\n", __func__
, stat
);
1229 rtsx_pci_write_register(pcr
, SD_BUS_STAT
,
1230 SD_CLK_TOGGLE_EN
| SD_CLK_FORCE_STOP
, 0);
1231 rtsx_pci_write_register(pcr
, CARD_CLK_EN
, 0xFF, 0);
1238 static int sdmmc_switch_voltage(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
1240 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
1241 struct rtsx_pcr
*pcr
= host
->pcr
;
1245 dev_dbg(sdmmc_dev(host
), "%s: signal_voltage = %d\n",
1246 __func__
, ios
->signal_voltage
);
1251 err
= rtsx_pci_card_exclusive_check(host
->pcr
, RTSX_SD_CARD
);
1255 mutex_lock(&pcr
->pcr_mutex
);
1257 rtsx_pci_start_run(pcr
);
1259 if (ios
->signal_voltage
== MMC_SIGNAL_VOLTAGE_330
)
1260 voltage
= OUTPUT_3V3
;
1262 voltage
= OUTPUT_1V8
;
1264 if (voltage
== OUTPUT_1V8
) {
1265 err
= sd_wait_voltage_stable_1(host
);
1270 err
= rtsx_pci_switch_output_voltage(pcr
, voltage
);
1274 if (voltage
== OUTPUT_1V8
) {
1275 err
= sd_wait_voltage_stable_2(host
);
1281 /* Stop toggle SD clock in idle */
1282 err
= rtsx_pci_write_register(pcr
, SD_BUS_STAT
,
1283 SD_CLK_TOGGLE_EN
| SD_CLK_FORCE_STOP
, 0);
1285 mutex_unlock(&pcr
->pcr_mutex
);
1290 static int sdmmc_execute_tuning(struct mmc_host
*mmc
, u32 opcode
)
1292 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
1293 struct rtsx_pcr
*pcr
= host
->pcr
;
1299 err
= rtsx_pci_card_exclusive_check(host
->pcr
, RTSX_SD_CARD
);
1303 mutex_lock(&pcr
->pcr_mutex
);
1305 rtsx_pci_start_run(pcr
);
1307 /* Set initial TX phase */
1308 switch (mmc
->ios
.timing
) {
1309 case MMC_TIMING_UHS_SDR104
:
1310 err
= sd_change_phase(host
, SDR104_TX_PHASE(pcr
), false);
1313 case MMC_TIMING_UHS_SDR50
:
1314 err
= sd_change_phase(host
, SDR50_TX_PHASE(pcr
), false);
1317 case MMC_TIMING_UHS_DDR50
:
1318 err
= sd_change_phase(host
, DDR50_TX_PHASE(pcr
), false);
1328 /* Tuning RX phase */
1329 if ((mmc
->ios
.timing
== MMC_TIMING_UHS_SDR104
) ||
1330 (mmc
->ios
.timing
== MMC_TIMING_UHS_SDR50
))
1331 err
= sd_tuning_rx(host
, opcode
);
1332 else if (mmc
->ios
.timing
== MMC_TIMING_UHS_DDR50
)
1333 err
= sd_change_phase(host
, DDR50_RX_PHASE(pcr
), true);
1336 mutex_unlock(&pcr
->pcr_mutex
);
1341 static int sdmmc_init_sd_express(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
1344 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
1345 struct rtsx_pcr
*pcr
= host
->pcr
;
1347 /* Set relink_time for changing to PCIe card */
1348 relink_time
= 0x8FFF;
1350 rtsx_pci_write_register(pcr
, 0xFF01, 0xFF, relink_time
);
1351 rtsx_pci_write_register(pcr
, 0xFF02, 0xFF, relink_time
>> 8);
1352 rtsx_pci_write_register(pcr
, 0xFF03, 0x01, relink_time
>> 16);
1354 rtsx_pci_write_register(pcr
, PETXCFG
, 0x80, 0x80);
1355 rtsx_pci_write_register(pcr
, LDO_VCC_CFG0
,
1356 RTS5261_LDO1_OCP_THD_MASK
,
1357 pcr
->option
.sd_800mA_ocp_thd
);
1359 if (pcr
->ops
->disable_auto_blink
)
1360 pcr
->ops
->disable_auto_blink(pcr
);
1362 /* For PCIe/NVMe mode can't enter delink issue */
1363 pcr
->hw_param
.interrupt_en
&= ~(SD_INT_EN
);
1364 rtsx_pci_writel(pcr
, RTSX_BIER
, pcr
->hw_param
.interrupt_en
);
1366 rtsx_pci_write_register(pcr
, RTS5260_AUTOLOAD_CFG4
,
1367 RTS5261_AUX_CLK_16M_EN
, RTS5261_AUX_CLK_16M_EN
);
1368 rtsx_pci_write_register(pcr
, RTS5261_FW_CFG0
,
1369 RTS5261_FW_ENTER_EXPRESS
, RTS5261_FW_ENTER_EXPRESS
);
1370 rtsx_pci_write_register(pcr
, RTS5261_FW_CFG1
,
1371 RTS5261_MCU_CLOCK_GATING
, RTS5261_MCU_CLOCK_GATING
);
1372 rtsx_pci_write_register(pcr
, RTS5261_FW_CFG1
,
1373 RTS5261_MCU_BUS_SEL_MASK
| RTS5261_MCU_CLOCK_SEL_MASK
1374 | RTS5261_DRIVER_ENABLE_FW
,
1375 RTS5261_MCU_CLOCK_SEL_16M
| RTS5261_DRIVER_ENABLE_FW
);
1380 static const struct mmc_host_ops realtek_pci_sdmmc_ops
= {
1381 .pre_req
= sdmmc_pre_req
,
1382 .post_req
= sdmmc_post_req
,
1383 .request
= sdmmc_request
,
1384 .set_ios
= sdmmc_set_ios
,
1385 .get_ro
= sdmmc_get_ro
,
1386 .get_cd
= sdmmc_get_cd
,
1387 .start_signal_voltage_switch
= sdmmc_switch_voltage
,
1388 .execute_tuning
= sdmmc_execute_tuning
,
1389 .init_sd_express
= sdmmc_init_sd_express
,
1392 static void init_extra_caps(struct realtek_pci_sdmmc
*host
)
1394 struct mmc_host
*mmc
= host
->mmc
;
1395 struct rtsx_pcr
*pcr
= host
->pcr
;
1397 dev_dbg(sdmmc_dev(host
), "pcr->extra_caps = 0x%x\n", pcr
->extra_caps
);
1399 if (pcr
->extra_caps
& EXTRA_CAPS_SD_SDR50
)
1400 mmc
->caps
|= MMC_CAP_UHS_SDR50
;
1401 if (pcr
->extra_caps
& EXTRA_CAPS_SD_SDR104
)
1402 mmc
->caps
|= MMC_CAP_UHS_SDR104
;
1403 if (pcr
->extra_caps
& EXTRA_CAPS_SD_DDR50
)
1404 mmc
->caps
|= MMC_CAP_UHS_DDR50
;
1405 if (pcr
->extra_caps
& EXTRA_CAPS_MMC_HSDDR
)
1406 mmc
->caps
|= MMC_CAP_1_8V_DDR
;
1407 if (pcr
->extra_caps
& EXTRA_CAPS_MMC_8BIT
)
1408 mmc
->caps
|= MMC_CAP_8_BIT_DATA
;
1409 if (pcr
->extra_caps
& EXTRA_CAPS_NO_MMC
)
1410 mmc
->caps2
|= MMC_CAP2_NO_MMC
;
1411 if (pcr
->extra_caps
& EXTRA_CAPS_SD_EXPRESS
)
1412 mmc
->caps2
|= MMC_CAP2_SD_EXP
| MMC_CAP2_SD_EXP_1_2V
;
1415 static void realtek_init_host(struct realtek_pci_sdmmc
*host
)
1417 struct mmc_host
*mmc
= host
->mmc
;
1418 struct rtsx_pcr
*pcr
= host
->pcr
;
1420 mmc
->f_min
= 250000;
1421 mmc
->f_max
= 208000000;
1422 mmc
->ocr_avail
= MMC_VDD_32_33
| MMC_VDD_33_34
| MMC_VDD_165_195
;
1423 mmc
->caps
= MMC_CAP_4_BIT_DATA
| MMC_CAP_SD_HIGHSPEED
|
1424 MMC_CAP_MMC_HIGHSPEED
| MMC_CAP_BUS_WIDTH_TEST
|
1425 MMC_CAP_UHS_SDR12
| MMC_CAP_UHS_SDR25
;
1427 mmc
->caps
= mmc
->caps
| MMC_CAP_AGGRESSIVE_PM
;
1428 mmc
->caps2
= MMC_CAP2_NO_PRESCAN_POWERUP
| MMC_CAP2_FULL_PWR_CYCLE
;
1429 mmc
->max_current_330
= 400;
1430 mmc
->max_current_180
= 800;
1431 mmc
->ops
= &realtek_pci_sdmmc_ops
;
1433 init_extra_caps(host
);
1435 mmc
->max_segs
= 256;
1436 mmc
->max_seg_size
= 65536;
1437 mmc
->max_blk_size
= 512;
1438 mmc
->max_blk_count
= 65535;
1439 mmc
->max_req_size
= 524288;
1442 static void rtsx_pci_sdmmc_card_event(struct platform_device
*pdev
)
1444 struct realtek_pci_sdmmc
*host
= platform_get_drvdata(pdev
);
1447 mmc_detect_change(host
->mmc
, 0);
1450 static int rtsx_pci_sdmmc_drv_probe(struct platform_device
*pdev
)
1452 struct mmc_host
*mmc
;
1453 struct realtek_pci_sdmmc
*host
;
1454 struct rtsx_pcr
*pcr
;
1455 struct pcr_handle
*handle
= pdev
->dev
.platform_data
;
1464 dev_dbg(&(pdev
->dev
), ": Realtek PCI-E SDMMC controller found\n");
1466 mmc
= mmc_alloc_host(sizeof(*host
), &pdev
->dev
);
1470 host
= mmc_priv(mmc
);
1475 host
->power_state
= SDMMC_POWER_OFF
;
1476 INIT_WORK(&host
->work
, sd_request
);
1477 platform_set_drvdata(pdev
, host
);
1478 pcr
->slots
[RTSX_SD_CARD
].p_dev
= pdev
;
1479 pcr
->slots
[RTSX_SD_CARD
].card_event
= rtsx_pci_sdmmc_card_event
;
1481 mutex_init(&host
->host_mutex
);
1483 realtek_init_host(host
);
1486 pm_runtime_set_autosuspend_delay(&pdev
->dev
, 5000);
1487 pm_runtime_use_autosuspend(&pdev
->dev
);
1488 pm_runtime_enable(&pdev
->dev
);
1497 static int rtsx_pci_sdmmc_drv_remove(struct platform_device
*pdev
)
1499 struct realtek_pci_sdmmc
*host
= platform_get_drvdata(pdev
);
1500 struct rtsx_pcr
*pcr
;
1501 struct mmc_host
*mmc
;
1507 pcr
->slots
[RTSX_SD_CARD
].p_dev
= NULL
;
1508 pcr
->slots
[RTSX_SD_CARD
].card_event
= NULL
;
1512 pm_runtime_dont_use_autosuspend(&pdev
->dev
);
1513 pm_runtime_disable(&pdev
->dev
);
1516 cancel_work_sync(&host
->work
);
1518 mutex_lock(&host
->host_mutex
);
1520 dev_dbg(&(pdev
->dev
),
1521 "%s: Controller removed during transfer\n",
1524 rtsx_pci_complete_unfinished_transfer(pcr
);
1526 host
->mrq
->cmd
->error
= -ENOMEDIUM
;
1527 if (host
->mrq
->stop
)
1528 host
->mrq
->stop
->error
= -ENOMEDIUM
;
1529 mmc_request_done(mmc
, host
->mrq
);
1531 mutex_unlock(&host
->host_mutex
);
1533 mmc_remove_host(mmc
);
1536 flush_work(&host
->work
);
1540 dev_dbg(&(pdev
->dev
),
1541 ": Realtek PCI-E SDMMC controller has been removed\n");
1546 static const struct platform_device_id rtsx_pci_sdmmc_ids
[] = {
1548 .name
= DRV_NAME_RTSX_PCI_SDMMC
,
1553 MODULE_DEVICE_TABLE(platform
, rtsx_pci_sdmmc_ids
);
1555 static struct platform_driver rtsx_pci_sdmmc_driver
= {
1556 .probe
= rtsx_pci_sdmmc_drv_probe
,
1557 .remove
= rtsx_pci_sdmmc_drv_remove
,
1558 .id_table
= rtsx_pci_sdmmc_ids
,
1560 .name
= DRV_NAME_RTSX_PCI_SDMMC
,
1561 .probe_type
= PROBE_PREFER_ASYNCHRONOUS
,
1564 module_platform_driver(rtsx_pci_sdmmc_driver
);
1566 MODULE_LICENSE("GPL");
1567 MODULE_AUTHOR("Wei WANG <wei_wang@realsil.com.cn>");
1568 MODULE_DESCRIPTION("Realtek PCI-E SD/MMC Card Host Driver");