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/pci.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/highmem.h>
14 #include <linux/delay.h>
15 #include <linux/platform_device.h>
16 #include <linux/workqueue.h>
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>
22 #include <linux/rtsx_pci.h>
23 #include <linux/unaligned.h>
24 #include <linux/pm_runtime.h>
26 struct realtek_pci_sdmmc
{
27 struct platform_device
*pdev
;
30 struct mmc_request
*mrq
;
31 #define SDMMC_WORKQ_NAME "rtsx_pci_sdmmc_workq"
33 struct work_struct work
;
34 struct mutex host_mutex
;
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
)
53 return &(host
->pdev
->dev
);
56 static inline void sd_clear_error(struct realtek_pci_sdmmc
*host
)
58 rtsx_pci_write_register(host
->pcr
, CARD_STOP
,
59 SD_STOP
| SD_CLR_ERR
, SD_STOP
| SD_CLR_ERR
);
63 static void dump_reg_range(struct realtek_pci_sdmmc
*host
, u16 start
, u16 end
)
65 u16 len
= end
- start
+ 1;
69 for (i
= 0; i
< len
; i
+= 8) {
71 int n
= min(8, len
- i
);
73 memset(&data
, 0, sizeof(data
));
74 for (j
= 0; j
< n
; j
++)
75 rtsx_pci_read_register(host
->pcr
, start
+ i
+ j
,
77 dev_dbg(sdmmc_dev(host
), "0x%04X(%d): %8ph\n",
82 static void sd_print_debug_regs(struct realtek_pci_sdmmc
*host
)
84 dump_reg_range(host
, 0xFDA0, 0xFDB3);
85 dump_reg_range(host
, 0xFD52, 0xFD69);
88 #define sd_print_debug_regs(host)
91 static inline int sd_get_cd_int(struct realtek_pci_sdmmc
*host
)
93 return rtsx_pci_readl(host
->pcr
, RTSX_BIPR
) & SD_EXIST
;
96 static void sd_cmd_set_sd_cmd(struct rtsx_pcr
*pcr
, struct mmc_command
*cmd
)
98 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CMD0
, 0xFF,
99 SD_CMD_START
| cmd
->opcode
);
100 rtsx_pci_write_be32(pcr
, SD_CMD1
, cmd
->arg
);
103 static void sd_cmd_set_data_len(struct rtsx_pcr
*pcr
, u16 blocks
, u16 blksz
)
105 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_BLOCK_CNT_L
, 0xFF, blocks
);
106 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_BLOCK_CNT_H
, 0xFF, blocks
>> 8);
107 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_BYTE_CNT_L
, 0xFF, blksz
);
108 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_BYTE_CNT_H
, 0xFF, blksz
>> 8);
111 static int sd_response_type(struct mmc_command
*cmd
)
113 switch (mmc_resp_type(cmd
)) {
115 return SD_RSP_TYPE_R0
;
117 return SD_RSP_TYPE_R1
;
118 case MMC_RSP_R1_NO_CRC
:
119 return SD_RSP_TYPE_R1
| SD_NO_CHECK_CRC7
;
121 return SD_RSP_TYPE_R1b
;
123 return SD_RSP_TYPE_R2
;
125 return SD_RSP_TYPE_R3
;
131 static int sd_status_index(int resp_type
)
133 if (resp_type
== SD_RSP_TYPE_R0
)
135 else if (resp_type
== SD_RSP_TYPE_R2
)
141 * sd_pre_dma_transfer - do dma_map_sg() or using cookie
143 * @pre: if called in pre_req()
145 * 0 - do dma_map_sg()
148 static int sd_pre_dma_transfer(struct realtek_pci_sdmmc
*host
,
149 struct mmc_data
*data
, bool pre
)
151 struct rtsx_pcr
*pcr
= host
->pcr
;
152 int read
= data
->flags
& MMC_DATA_READ
;
154 int using_cookie
= 0;
156 if (!pre
&& data
->host_cookie
&& data
->host_cookie
!= host
->cookie
) {
157 dev_err(sdmmc_dev(host
),
158 "error: data->host_cookie = %d, host->cookie = %d\n",
159 data
->host_cookie
, host
->cookie
);
160 data
->host_cookie
= 0;
163 if (pre
|| data
->host_cookie
!= host
->cookie
) {
164 count
= rtsx_pci_dma_map_sg(pcr
, data
->sg
, data
->sg_len
, read
);
166 count
= host
->cookie_sg_count
;
171 host
->cookie_sg_count
= count
;
172 if (++host
->cookie
< 0)
174 data
->host_cookie
= host
->cookie
;
176 host
->sg_count
= count
;
182 static void sdmmc_pre_req(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
184 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
185 struct mmc_data
*data
= mrq
->data
;
187 if (data
->host_cookie
) {
188 dev_err(sdmmc_dev(host
),
189 "error: reset data->host_cookie = %d\n",
191 data
->host_cookie
= 0;
194 sd_pre_dma_transfer(host
, data
, true);
195 dev_dbg(sdmmc_dev(host
), "pre dma sg: %d\n", host
->cookie_sg_count
);
198 static void sdmmc_post_req(struct mmc_host
*mmc
, struct mmc_request
*mrq
,
201 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
202 struct rtsx_pcr
*pcr
= host
->pcr
;
203 struct mmc_data
*data
= mrq
->data
;
204 int read
= data
->flags
& MMC_DATA_READ
;
206 rtsx_pci_dma_unmap_sg(pcr
, data
->sg
, data
->sg_len
, read
);
207 data
->host_cookie
= 0;
210 static void sd_send_cmd_get_rsp(struct realtek_pci_sdmmc
*host
,
211 struct mmc_command
*cmd
)
213 struct rtsx_pcr
*pcr
= host
->pcr
;
214 u8 cmd_idx
= (u8
)cmd
->opcode
;
222 bool clock_toggled
= false;
224 dev_dbg(sdmmc_dev(host
), "%s: SD/MMC CMD %d, arg = 0x%08x\n",
225 __func__
, cmd_idx
, arg
);
227 rsp_type
= sd_response_type(cmd
);
231 stat_idx
= sd_status_index(rsp_type
);
233 if (rsp_type
== SD_RSP_TYPE_R1b
)
234 timeout
= cmd
->busy_timeout
? cmd
->busy_timeout
: 3000;
236 if (cmd
->opcode
== SD_SWITCH_VOLTAGE
) {
237 err
= rtsx_pci_write_register(pcr
, SD_BUS_STAT
,
238 0xFF, SD_CLK_TOGGLE_EN
);
242 clock_toggled
= true;
245 rtsx_pci_init_cmd(pcr
);
246 sd_cmd_set_sd_cmd(pcr
, cmd
);
247 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG2
, 0xFF, rsp_type
);
248 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_DATA_SOURCE
,
249 0x01, PINGPONG_BUFFER
);
250 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_TRANSFER
,
251 0xFF, SD_TM_CMD_RSP
| SD_TRANSFER_START
);
252 rtsx_pci_add_cmd(pcr
, CHECK_REG_CMD
, SD_TRANSFER
,
253 SD_TRANSFER_END
| SD_STAT_IDLE
,
254 SD_TRANSFER_END
| SD_STAT_IDLE
);
256 if (rsp_type
== SD_RSP_TYPE_R2
) {
257 /* Read data from ping-pong buffer */
258 for (i
= PPBUF_BASE2
; i
< PPBUF_BASE2
+ 16; i
++)
259 rtsx_pci_add_cmd(pcr
, READ_REG_CMD
, (u16
)i
, 0, 0);
260 } else if (rsp_type
!= SD_RSP_TYPE_R0
) {
261 /* Read data from SD_CMDx registers */
262 for (i
= SD_CMD0
; i
<= SD_CMD4
; i
++)
263 rtsx_pci_add_cmd(pcr
, READ_REG_CMD
, (u16
)i
, 0, 0);
266 rtsx_pci_add_cmd(pcr
, READ_REG_CMD
, SD_STAT1
, 0, 0);
268 err
= rtsx_pci_send_cmd(pcr
, timeout
);
270 sd_print_debug_regs(host
);
271 sd_clear_error(host
);
272 dev_dbg(sdmmc_dev(host
),
273 "rtsx_pci_send_cmd error (err = %d)\n", err
);
277 if (rsp_type
== SD_RSP_TYPE_R0
) {
282 /* Eliminate returned value of CHECK_REG_CMD */
283 ptr
= rtsx_pci_get_cmd_data(pcr
) + 1;
285 /* Check (Start,Transmission) bit of Response */
286 if ((ptr
[0] & 0xC0) != 0) {
288 dev_dbg(sdmmc_dev(host
), "Invalid response bit\n");
293 if (!(rsp_type
& SD_NO_CHECK_CRC7
)) {
294 if (ptr
[stat_idx
] & SD_CRC7_ERR
) {
296 dev_dbg(sdmmc_dev(host
), "CRC7 error\n");
301 if (rsp_type
== SD_RSP_TYPE_R2
) {
303 * The controller offloads the last byte {CRC-7, end bit 1'b1}
304 * of response type R2. Assign dummy CRC, 0, and end bit to the
305 * byte(ptr[16], goes into the LSB of resp[3] later).
309 for (i
= 0; i
< 4; i
++) {
310 cmd
->resp
[i
] = get_unaligned_be32(ptr
+ 1 + i
* 4);
311 dev_dbg(sdmmc_dev(host
), "cmd->resp[%d] = 0x%08x\n",
315 cmd
->resp
[0] = get_unaligned_be32(ptr
+ 1);
316 dev_dbg(sdmmc_dev(host
), "cmd->resp[0] = 0x%08x\n",
323 if (err
&& clock_toggled
)
324 rtsx_pci_write_register(pcr
, SD_BUS_STAT
,
325 SD_CLK_TOGGLE_EN
| SD_CLK_FORCE_STOP
, 0);
328 static int sd_read_data(struct realtek_pci_sdmmc
*host
, struct mmc_command
*cmd
,
329 u16 byte_cnt
, u8
*buf
, int buf_len
, int timeout
)
331 struct rtsx_pcr
*pcr
= host
->pcr
;
335 dev_dbg(sdmmc_dev(host
), "%s: SD/MMC CMD %d, arg = 0x%08x\n",
336 __func__
, cmd
->opcode
, cmd
->arg
);
341 if (cmd
->opcode
== MMC_SEND_TUNING_BLOCK
)
342 trans_mode
= SD_TM_AUTO_TUNING
;
344 trans_mode
= SD_TM_NORMAL_READ
;
346 rtsx_pci_init_cmd(pcr
);
347 sd_cmd_set_sd_cmd(pcr
, cmd
);
348 sd_cmd_set_data_len(pcr
, 1, byte_cnt
);
349 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG2
, 0xFF,
350 SD_CALCULATE_CRC7
| SD_CHECK_CRC16
|
351 SD_NO_WAIT_BUSY_END
| SD_CHECK_CRC7
| SD_RSP_LEN_6
);
352 if (trans_mode
!= SD_TM_AUTO_TUNING
)
353 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
,
354 CARD_DATA_SOURCE
, 0x01, PINGPONG_BUFFER
);
356 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_TRANSFER
,
357 0xFF, trans_mode
| SD_TRANSFER_START
);
358 rtsx_pci_add_cmd(pcr
, CHECK_REG_CMD
, SD_TRANSFER
,
359 SD_TRANSFER_END
, SD_TRANSFER_END
);
361 err
= rtsx_pci_send_cmd(pcr
, timeout
);
363 sd_print_debug_regs(host
);
364 dev_dbg(sdmmc_dev(host
),
365 "rtsx_pci_send_cmd fail (err = %d)\n", err
);
369 if (buf
&& buf_len
) {
370 err
= rtsx_pci_read_ppbuf(pcr
, buf
, buf_len
);
372 dev_dbg(sdmmc_dev(host
),
373 "rtsx_pci_read_ppbuf fail (err = %d)\n", err
);
381 static int sd_write_data(struct realtek_pci_sdmmc
*host
,
382 struct mmc_command
*cmd
, u16 byte_cnt
, u8
*buf
, int buf_len
,
385 struct rtsx_pcr
*pcr
= host
->pcr
;
388 dev_dbg(sdmmc_dev(host
), "%s: SD/MMC CMD %d, arg = 0x%08x\n",
389 __func__
, cmd
->opcode
, cmd
->arg
);
394 sd_send_cmd_get_rsp(host
, cmd
);
398 if (buf
&& buf_len
) {
399 err
= rtsx_pci_write_ppbuf(pcr
, buf
, buf_len
);
401 dev_dbg(sdmmc_dev(host
),
402 "rtsx_pci_write_ppbuf fail (err = %d)\n", err
);
407 rtsx_pci_init_cmd(pcr
);
408 sd_cmd_set_data_len(pcr
, 1, byte_cnt
);
409 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG2
, 0xFF,
410 SD_CALCULATE_CRC7
| SD_CHECK_CRC16
|
411 SD_NO_WAIT_BUSY_END
| SD_CHECK_CRC7
| SD_RSP_LEN_0
);
412 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_TRANSFER
, 0xFF,
413 SD_TRANSFER_START
| SD_TM_AUTO_WRITE_3
);
414 rtsx_pci_add_cmd(pcr
, CHECK_REG_CMD
, SD_TRANSFER
,
415 SD_TRANSFER_END
, SD_TRANSFER_END
);
417 err
= rtsx_pci_send_cmd(pcr
, timeout
);
419 sd_print_debug_regs(host
);
420 dev_dbg(sdmmc_dev(host
),
421 "rtsx_pci_send_cmd fail (err = %d)\n", err
);
428 static int sd_read_long_data(struct realtek_pci_sdmmc
*host
,
429 struct mmc_request
*mrq
)
431 struct rtsx_pcr
*pcr
= host
->pcr
;
432 struct mmc_host
*mmc
= host
->mmc
;
433 struct mmc_card
*card
= mmc
->card
;
434 struct mmc_command
*cmd
= mrq
->cmd
;
435 struct mmc_data
*data
= mrq
->data
;
436 int uhs
= mmc_card_uhs(card
);
440 size_t data_len
= data
->blksz
* data
->blocks
;
442 dev_dbg(sdmmc_dev(host
), "%s: SD/MMC CMD %d, arg = 0x%08x\n",
443 __func__
, cmd
->opcode
, cmd
->arg
);
445 resp_type
= sd_response_type(cmd
);
450 cfg2
|= SD_NO_CHECK_WAIT_CRC_TO
;
452 rtsx_pci_init_cmd(pcr
);
453 sd_cmd_set_sd_cmd(pcr
, cmd
);
454 sd_cmd_set_data_len(pcr
, data
->blocks
, data
->blksz
);
455 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, IRQSTAT0
,
456 DMA_DONE_INT
, DMA_DONE_INT
);
457 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC3
,
458 0xFF, (u8
)(data_len
>> 24));
459 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC2
,
460 0xFF, (u8
)(data_len
>> 16));
461 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC1
,
462 0xFF, (u8
)(data_len
>> 8));
463 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC0
, 0xFF, (u8
)data_len
);
464 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMACTL
,
465 0x03 | DMA_PACK_SIZE_MASK
,
466 DMA_DIR_FROM_CARD
| DMA_EN
| DMA_512
);
467 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_DATA_SOURCE
,
469 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG2
, 0xFF, cfg2
| resp_type
);
470 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_TRANSFER
, 0xFF,
471 SD_TRANSFER_START
| SD_TM_AUTO_READ_2
);
472 rtsx_pci_add_cmd(pcr
, CHECK_REG_CMD
, SD_TRANSFER
,
473 SD_TRANSFER_END
, SD_TRANSFER_END
);
474 rtsx_pci_send_cmd_no_wait(pcr
);
476 err
= rtsx_pci_dma_transfer(pcr
, data
->sg
, host
->sg_count
, 1, 10000);
478 sd_print_debug_regs(host
);
479 sd_clear_error(host
);
486 static int sd_write_long_data(struct realtek_pci_sdmmc
*host
,
487 struct mmc_request
*mrq
)
489 struct rtsx_pcr
*pcr
= host
->pcr
;
490 struct mmc_host
*mmc
= host
->mmc
;
491 struct mmc_card
*card
= mmc
->card
;
492 struct mmc_command
*cmd
= mrq
->cmd
;
493 struct mmc_data
*data
= mrq
->data
;
494 int uhs
= mmc_card_uhs(card
);
497 size_t data_len
= data
->blksz
* data
->blocks
;
499 sd_send_cmd_get_rsp(host
, cmd
);
503 dev_dbg(sdmmc_dev(host
), "%s: SD/MMC CMD %d, arg = 0x%08x\n",
504 __func__
, cmd
->opcode
, cmd
->arg
);
506 cfg2
= SD_NO_CALCULATE_CRC7
| SD_CHECK_CRC16
|
507 SD_NO_WAIT_BUSY_END
| SD_NO_CHECK_CRC7
| SD_RSP_LEN_0
;
510 cfg2
|= SD_NO_CHECK_WAIT_CRC_TO
;
512 rtsx_pci_init_cmd(pcr
);
513 sd_cmd_set_data_len(pcr
, data
->blocks
, data
->blksz
);
514 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, IRQSTAT0
,
515 DMA_DONE_INT
, DMA_DONE_INT
);
516 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC3
,
517 0xFF, (u8
)(data_len
>> 24));
518 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC2
,
519 0xFF, (u8
)(data_len
>> 16));
520 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC1
,
521 0xFF, (u8
)(data_len
>> 8));
522 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC0
, 0xFF, (u8
)data_len
);
523 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMACTL
,
524 0x03 | DMA_PACK_SIZE_MASK
,
525 DMA_DIR_TO_CARD
| DMA_EN
| DMA_512
);
526 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_DATA_SOURCE
,
528 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG2
, 0xFF, cfg2
);
529 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_TRANSFER
, 0xFF,
530 SD_TRANSFER_START
| SD_TM_AUTO_WRITE_3
);
531 rtsx_pci_add_cmd(pcr
, CHECK_REG_CMD
, SD_TRANSFER
,
532 SD_TRANSFER_END
, SD_TRANSFER_END
);
533 rtsx_pci_send_cmd_no_wait(pcr
);
534 err
= rtsx_pci_dma_transfer(pcr
, data
->sg
, host
->sg_count
, 0, 10000);
536 sd_clear_error(host
);
543 static inline void sd_enable_initial_mode(struct realtek_pci_sdmmc
*host
)
545 rtsx_pci_write_register(host
->pcr
, SD_CFG1
,
546 SD_CLK_DIVIDE_MASK
, SD_CLK_DIVIDE_128
);
549 static inline void sd_disable_initial_mode(struct realtek_pci_sdmmc
*host
)
551 rtsx_pci_write_register(host
->pcr
, SD_CFG1
,
552 SD_CLK_DIVIDE_MASK
, SD_CLK_DIVIDE_0
);
555 static int sd_rw_multi(struct realtek_pci_sdmmc
*host
, struct mmc_request
*mrq
)
557 struct mmc_data
*data
= mrq
->data
;
560 if (host
->sg_count
< 0) {
561 data
->error
= host
->sg_count
;
562 dev_dbg(sdmmc_dev(host
), "%s: sg_count = %d is invalid\n",
563 __func__
, host
->sg_count
);
567 if (data
->flags
& MMC_DATA_READ
) {
568 if (host
->initial_mode
)
569 sd_disable_initial_mode(host
);
571 err
= sd_read_long_data(host
, mrq
);
573 if (host
->initial_mode
)
574 sd_enable_initial_mode(host
);
579 return sd_write_long_data(host
, mrq
);
582 static void sd_normal_rw(struct realtek_pci_sdmmc
*host
,
583 struct mmc_request
*mrq
)
585 struct mmc_command
*cmd
= mrq
->cmd
;
586 struct mmc_data
*data
= mrq
->data
;
589 buf
= kzalloc(data
->blksz
, GFP_NOIO
);
591 cmd
->error
= -ENOMEM
;
595 if (data
->flags
& MMC_DATA_READ
) {
596 if (host
->initial_mode
)
597 sd_disable_initial_mode(host
);
599 cmd
->error
= sd_read_data(host
, cmd
, (u16
)data
->blksz
, buf
,
602 if (host
->initial_mode
)
603 sd_enable_initial_mode(host
);
605 sg_copy_from_buffer(data
->sg
, data
->sg_len
, buf
, data
->blksz
);
607 sg_copy_to_buffer(data
->sg
, data
->sg_len
, buf
, data
->blksz
);
609 cmd
->error
= sd_write_data(host
, cmd
, (u16
)data
->blksz
, buf
,
616 static int sd_change_phase(struct realtek_pci_sdmmc
*host
,
617 u8 sample_point
, bool rx
)
619 struct rtsx_pcr
*pcr
= host
->pcr
;
621 dev_dbg(sdmmc_dev(host
), "%s(%s): sample_point = %d\n",
622 __func__
, rx
? "RX" : "TX", sample_point
);
624 rtsx_pci_write_register(pcr
, CLK_CTL
, CHANGE_CLK
, CHANGE_CLK
);
626 SD_VP_CTL
= SD_VPRX_CTL
;
627 rtsx_pci_write_register(pcr
, SD_VPRX_CTL
,
628 PHASE_SELECT_MASK
, sample_point
);
630 SD_VP_CTL
= SD_VPTX_CTL
;
631 rtsx_pci_write_register(pcr
, SD_VPTX_CTL
,
632 PHASE_SELECT_MASK
, sample_point
);
634 rtsx_pci_write_register(pcr
, SD_VP_CTL
, PHASE_NOT_RESET
, 0);
635 rtsx_pci_write_register(pcr
, SD_VP_CTL
, PHASE_NOT_RESET
,
637 rtsx_pci_write_register(pcr
, CLK_CTL
, CHANGE_CLK
, 0);
638 rtsx_pci_write_register(pcr
, SD_CFG1
, SD_ASYNC_FIFO_NOT_RST
, 0);
643 static inline u32
test_phase_bit(u32 phase_map
, unsigned int bit
)
645 bit
%= RTSX_PHASE_MAX
;
646 return phase_map
& (1 << bit
);
649 static int sd_get_phase_len(u32 phase_map
, unsigned int start_bit
)
653 for (i
= 0; i
< RTSX_PHASE_MAX
; i
++) {
654 if (test_phase_bit(phase_map
, start_bit
+ i
) == 0)
657 return RTSX_PHASE_MAX
;
660 static u8
sd_search_final_phase(struct realtek_pci_sdmmc
*host
, u32 phase_map
)
662 int start
= 0, len
= 0;
663 int start_final
= 0, len_final
= 0;
664 u8 final_phase
= 0xFF;
666 if (phase_map
== 0) {
667 dev_err(sdmmc_dev(host
), "phase error: [map:%x]\n", phase_map
);
671 while (start
< RTSX_PHASE_MAX
) {
672 len
= sd_get_phase_len(phase_map
, start
);
673 if (len_final
< len
) {
677 start
+= len
? len
: 1;
680 final_phase
= (start_final
+ len_final
/ 2) % RTSX_PHASE_MAX
;
681 dev_dbg(sdmmc_dev(host
), "phase: [map:%x] [maxlen:%d] [final:%d]\n",
682 phase_map
, len_final
, final_phase
);
687 static void sd_wait_data_idle(struct realtek_pci_sdmmc
*host
)
692 for (i
= 0; i
< 100; i
++) {
693 rtsx_pci_read_register(host
->pcr
, SD_DATA_STATE
, &val
);
694 if (val
& SD_DATA_IDLE
)
701 static int sd_tuning_rx_cmd(struct realtek_pci_sdmmc
*host
,
702 u8 opcode
, u8 sample_point
)
705 struct mmc_command cmd
= {};
706 struct rtsx_pcr
*pcr
= host
->pcr
;
708 sd_change_phase(host
, sample_point
, true);
710 rtsx_pci_write_register(pcr
, SD_CFG3
, SD_RSP_80CLK_TIMEOUT_EN
,
711 SD_RSP_80CLK_TIMEOUT_EN
);
714 err
= sd_read_data(host
, &cmd
, 0x40, NULL
, 0, 100);
716 /* Wait till SD DATA IDLE */
717 sd_wait_data_idle(host
);
718 sd_clear_error(host
);
719 rtsx_pci_write_register(pcr
, SD_CFG3
,
720 SD_RSP_80CLK_TIMEOUT_EN
, 0);
724 rtsx_pci_write_register(pcr
, SD_CFG3
, SD_RSP_80CLK_TIMEOUT_EN
, 0);
728 static int sd_tuning_phase(struct realtek_pci_sdmmc
*host
,
729 u8 opcode
, u32
*phase_map
)
732 u32 raw_phase_map
= 0;
734 for (i
= 0; i
< RTSX_PHASE_MAX
; i
++) {
735 err
= sd_tuning_rx_cmd(host
, opcode
, (u8
)i
);
737 raw_phase_map
|= 1 << i
;
741 *phase_map
= raw_phase_map
;
746 static int sd_tuning_rx(struct realtek_pci_sdmmc
*host
, u8 opcode
)
749 u32 raw_phase_map
[RX_TUNING_CNT
] = {0}, phase_map
;
752 for (i
= 0; i
< RX_TUNING_CNT
; i
++) {
753 err
= sd_tuning_phase(host
, opcode
, &(raw_phase_map
[i
]));
757 if (raw_phase_map
[i
] == 0)
761 phase_map
= 0xFFFFFFFF;
762 for (i
= 0; i
< RX_TUNING_CNT
; i
++) {
763 dev_dbg(sdmmc_dev(host
), "RX raw_phase_map[%d] = 0x%08x\n",
764 i
, raw_phase_map
[i
]);
765 phase_map
&= raw_phase_map
[i
];
767 dev_dbg(sdmmc_dev(host
), "RX phase_map = 0x%08x\n", phase_map
);
770 final_phase
= sd_search_final_phase(host
, phase_map
);
771 if (final_phase
== 0xFF)
774 err
= sd_change_phase(host
, final_phase
, true);
784 static inline int sdio_extblock_cmd(struct mmc_command
*cmd
,
785 struct mmc_data
*data
)
787 return (cmd
->opcode
== SD_IO_RW_EXTENDED
) && (data
->blksz
== 512);
790 static inline int sd_rw_cmd(struct mmc_command
*cmd
)
792 return mmc_op_multi(cmd
->opcode
) ||
793 (cmd
->opcode
== MMC_READ_SINGLE_BLOCK
) ||
794 (cmd
->opcode
== MMC_WRITE_BLOCK
);
797 static void sd_request(struct work_struct
*work
)
799 struct realtek_pci_sdmmc
*host
= container_of(work
,
800 struct realtek_pci_sdmmc
, work
);
801 struct rtsx_pcr
*pcr
= host
->pcr
;
803 struct mmc_host
*mmc
= host
->mmc
;
804 struct mmc_request
*mrq
= host
->mrq
;
805 struct mmc_command
*cmd
= mrq
->cmd
;
806 struct mmc_data
*data
= mrq
->data
;
808 unsigned int data_size
= 0;
811 if (host
->eject
|| !sd_get_cd_int(host
)) {
812 cmd
->error
= -ENOMEDIUM
;
816 err
= rtsx_pci_card_exclusive_check(host
->pcr
, RTSX_SD_CARD
);
822 mutex_lock(&pcr
->pcr_mutex
);
824 rtsx_pci_start_run(pcr
);
826 rtsx_pci_switch_clock(pcr
, host
->clock
, host
->ssc_depth
,
827 host
->initial_mode
, host
->double_clk
, host
->vpclk
);
828 rtsx_pci_write_register(pcr
, CARD_SELECT
, 0x07, SD_MOD_SEL
);
829 rtsx_pci_write_register(pcr
, CARD_SHARE_MODE
,
830 CARD_SHARE_MASK
, CARD_SHARE_48_SD
);
832 mutex_lock(&host
->host_mutex
);
834 mutex_unlock(&host
->host_mutex
);
837 data_size
= data
->blocks
* data
->blksz
;
840 sd_send_cmd_get_rsp(host
, cmd
);
841 } else if (sd_rw_cmd(cmd
) || sdio_extblock_cmd(cmd
, data
)) {
842 cmd
->error
= sd_rw_multi(host
, mrq
);
843 if (!host
->using_cookie
)
844 sdmmc_post_req(host
->mmc
, host
->mrq
, 0);
846 if (mmc_op_multi(cmd
->opcode
) && mrq
->stop
)
847 sd_send_cmd_get_rsp(host
, mrq
->stop
);
849 sd_normal_rw(host
, mrq
);
853 if (cmd
->error
|| data
->error
)
854 data
->bytes_xfered
= 0;
856 data
->bytes_xfered
= data
->blocks
* data
->blksz
;
859 mutex_unlock(&pcr
->pcr_mutex
);
863 dev_dbg(sdmmc_dev(host
), "CMD %d 0x%08x error(%d)\n",
864 cmd
->opcode
, cmd
->arg
, cmd
->error
);
867 mutex_lock(&host
->host_mutex
);
869 mutex_unlock(&host
->host_mutex
);
871 mmc_request_done(mmc
, mrq
);
874 static void sdmmc_request(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
876 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
877 struct mmc_data
*data
= mrq
->data
;
879 mutex_lock(&host
->host_mutex
);
881 mutex_unlock(&host
->host_mutex
);
883 if (sd_rw_cmd(mrq
->cmd
) || sdio_extblock_cmd(mrq
->cmd
, data
))
884 host
->using_cookie
= sd_pre_dma_transfer(host
, data
, false);
886 schedule_work(&host
->work
);
889 static int sd_set_bus_width(struct realtek_pci_sdmmc
*host
,
890 unsigned char bus_width
)
894 [MMC_BUS_WIDTH_1
] = SD_BUS_WIDTH_1BIT
,
895 [MMC_BUS_WIDTH_4
] = SD_BUS_WIDTH_4BIT
,
896 [MMC_BUS_WIDTH_8
] = SD_BUS_WIDTH_8BIT
,
899 if (bus_width
<= MMC_BUS_WIDTH_8
)
900 err
= rtsx_pci_write_register(host
->pcr
, SD_CFG1
,
901 0x03, width
[bus_width
]);
906 static int sd_power_on(struct realtek_pci_sdmmc
*host
, unsigned char power_mode
)
908 struct rtsx_pcr
*pcr
= host
->pcr
;
909 struct mmc_host
*mmc
= host
->mmc
;
914 if (host
->prev_power_state
== MMC_POWER_ON
)
917 if (host
->prev_power_state
== MMC_POWER_UP
) {
918 rtsx_pci_write_register(pcr
, SD_BUS_STAT
, SD_CLK_TOGGLE_EN
, 0);
924 rtsx_pci_init_cmd(pcr
);
925 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_SELECT
, 0x07, SD_MOD_SEL
);
926 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_SHARE_MODE
,
927 CARD_SHARE_MASK
, CARD_SHARE_48_SD
);
928 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_CLK_EN
,
929 SD_CLK_EN
, SD_CLK_EN
);
930 err
= rtsx_pci_send_cmd(pcr
, 100);
934 err
= rtsx_pci_card_pull_ctl_enable(pcr
, RTSX_SD_CARD
);
938 err
= rtsx_pci_card_power_on(pcr
, RTSX_SD_CARD
);
944 err
= rtsx_pci_write_register(pcr
, CARD_OE
, SD_OUTPUT_EN
, SD_OUTPUT_EN
);
948 /* send at least 74 clocks */
949 rtsx_pci_write_register(pcr
, SD_BUS_STAT
, SD_CLK_TOGGLE_EN
, SD_CLK_TOGGLE_EN
);
951 if ((PCI_PID(pcr
) == PID_5261
) || (PCI_PID(pcr
) == PID_5264
)) {
953 * If test mode is set switch to SD Express mandatorily,
954 * this is only for factory testing.
956 rtsx_pci_read_register(pcr
, RTS5261_FW_CFG_INFO0
, &test_mode
);
957 if (test_mode
& RTS5261_FW_EXPRESS_TEST_MASK
) {
958 sdmmc_init_sd_express(mmc
, NULL
);
961 if (pcr
->extra_caps
& EXTRA_CAPS_SD_EXPRESS
)
962 mmc
->caps2
|= MMC_CAP2_SD_EXP
| MMC_CAP2_SD_EXP_1_2V
;
964 * HW read wp status when resuming from S3/S4,
965 * and then picks SD legacy interface if it's set
968 val
= rtsx_pci_readl(pcr
, RTSX_BIPR
);
969 if (val
& SD_WRITE_PROTECT
) {
970 pcr
->extra_caps
&= ~EXTRA_CAPS_SD_EXPRESS
;
971 mmc
->caps2
&= ~(MMC_CAP2_SD_EXP
| MMC_CAP2_SD_EXP_1_2V
);
976 host
->prev_power_state
= power_mode
;
980 static int sd_power_off(struct realtek_pci_sdmmc
*host
)
982 struct rtsx_pcr
*pcr
= host
->pcr
;
985 host
->prev_power_state
= MMC_POWER_OFF
;
987 rtsx_pci_init_cmd(pcr
);
989 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_CLK_EN
, SD_CLK_EN
, 0);
990 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_OE
, SD_OUTPUT_EN
, 0);
992 err
= rtsx_pci_send_cmd(pcr
, 100);
996 err
= rtsx_pci_card_power_off(pcr
, RTSX_SD_CARD
);
1000 return rtsx_pci_card_pull_ctl_disable(pcr
, RTSX_SD_CARD
);
1003 static int sd_set_power_mode(struct realtek_pci_sdmmc
*host
,
1004 unsigned char power_mode
)
1008 if (power_mode
== MMC_POWER_OFF
)
1009 err
= sd_power_off(host
);
1011 err
= sd_power_on(host
, power_mode
);
1016 static int sd_set_timing(struct realtek_pci_sdmmc
*host
, unsigned char timing
)
1018 struct rtsx_pcr
*pcr
= host
->pcr
;
1021 rtsx_pci_init_cmd(pcr
);
1024 case MMC_TIMING_UHS_SDR104
:
1025 case MMC_TIMING_UHS_SDR50
:
1026 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG1
,
1027 0x0C | SD_ASYNC_FIFO_NOT_RST
,
1028 SD_30_MODE
| SD_ASYNC_FIFO_NOT_RST
);
1029 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
,
1030 CLK_LOW_FREQ
, CLK_LOW_FREQ
);
1031 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_CLK_SOURCE
, 0xFF,
1032 CRC_VAR_CLK0
| SD30_FIX_CLK
| SAMPLE_VAR_CLK1
);
1033 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
, CLK_LOW_FREQ
, 0);
1036 case MMC_TIMING_MMC_DDR52
:
1037 case MMC_TIMING_UHS_DDR50
:
1038 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG1
,
1039 0x0C | SD_ASYNC_FIFO_NOT_RST
,
1040 SD_DDR_MODE
| SD_ASYNC_FIFO_NOT_RST
);
1041 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
,
1042 CLK_LOW_FREQ
, CLK_LOW_FREQ
);
1043 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_CLK_SOURCE
, 0xFF,
1044 CRC_VAR_CLK0
| SD30_FIX_CLK
| SAMPLE_VAR_CLK1
);
1045 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
, CLK_LOW_FREQ
, 0);
1046 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_PUSH_POINT_CTL
,
1047 DDR_VAR_TX_CMD_DAT
, DDR_VAR_TX_CMD_DAT
);
1048 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_SAMPLE_POINT_CTL
,
1049 DDR_VAR_RX_DAT
| DDR_VAR_RX_CMD
,
1050 DDR_VAR_RX_DAT
| DDR_VAR_RX_CMD
);
1053 case MMC_TIMING_MMC_HS
:
1054 case MMC_TIMING_SD_HS
:
1055 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG1
,
1057 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
,
1058 CLK_LOW_FREQ
, CLK_LOW_FREQ
);
1059 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_CLK_SOURCE
, 0xFF,
1060 CRC_FIX_CLK
| SD30_VAR_CLK0
| SAMPLE_VAR_CLK1
);
1061 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
, CLK_LOW_FREQ
, 0);
1062 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_PUSH_POINT_CTL
,
1063 SD20_TX_SEL_MASK
, SD20_TX_14_AHEAD
);
1064 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_SAMPLE_POINT_CTL
,
1065 SD20_RX_SEL_MASK
, SD20_RX_14_DELAY
);
1069 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
,
1070 SD_CFG1
, 0x0C, SD_20_MODE
);
1071 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
,
1072 CLK_LOW_FREQ
, CLK_LOW_FREQ
);
1073 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_CLK_SOURCE
, 0xFF,
1074 CRC_FIX_CLK
| SD30_VAR_CLK0
| SAMPLE_VAR_CLK1
);
1075 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
, CLK_LOW_FREQ
, 0);
1076 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
,
1077 SD_PUSH_POINT_CTL
, 0xFF, 0);
1078 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_SAMPLE_POINT_CTL
,
1079 SD20_RX_SEL_MASK
, SD20_RX_POS_EDGE
);
1083 err
= rtsx_pci_send_cmd(pcr
, 100);
1088 static void sdmmc_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
1090 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
1091 struct rtsx_pcr
*pcr
= host
->pcr
;
1096 if (rtsx_pci_card_exclusive_check(host
->pcr
, RTSX_SD_CARD
))
1099 mutex_lock(&pcr
->pcr_mutex
);
1101 rtsx_pci_start_run(pcr
);
1103 sd_set_bus_width(host
, ios
->bus_width
);
1104 sd_set_power_mode(host
, ios
->power_mode
);
1105 sd_set_timing(host
, ios
->timing
);
1107 host
->vpclk
= false;
1108 host
->double_clk
= true;
1110 switch (ios
->timing
) {
1111 case MMC_TIMING_UHS_SDR104
:
1112 case MMC_TIMING_UHS_SDR50
:
1113 host
->ssc_depth
= RTSX_SSC_DEPTH_2M
;
1115 host
->double_clk
= false;
1117 case MMC_TIMING_MMC_DDR52
:
1118 case MMC_TIMING_UHS_DDR50
:
1119 case MMC_TIMING_UHS_SDR25
:
1120 host
->ssc_depth
= RTSX_SSC_DEPTH_1M
;
1123 host
->ssc_depth
= RTSX_SSC_DEPTH_500K
;
1127 host
->initial_mode
= (ios
->clock
<= 1000000) ? true : false;
1129 host
->clock
= ios
->clock
;
1130 rtsx_pci_switch_clock(pcr
, ios
->clock
, host
->ssc_depth
,
1131 host
->initial_mode
, host
->double_clk
, host
->vpclk
);
1133 mutex_unlock(&pcr
->pcr_mutex
);
1136 static int sdmmc_get_ro(struct mmc_host
*mmc
)
1138 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
1139 struct rtsx_pcr
*pcr
= host
->pcr
;
1146 mutex_lock(&pcr
->pcr_mutex
);
1148 rtsx_pci_start_run(pcr
);
1150 /* Check SD mechanical write-protect switch */
1151 val
= rtsx_pci_readl(pcr
, RTSX_BIPR
);
1152 dev_dbg(sdmmc_dev(host
), "%s: RTSX_BIPR = 0x%08x\n", __func__
, val
);
1153 if (val
& SD_WRITE_PROTECT
)
1156 mutex_unlock(&pcr
->pcr_mutex
);
1161 static int sdmmc_get_cd(struct mmc_host
*mmc
)
1163 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
1164 struct rtsx_pcr
*pcr
= host
->pcr
;
1171 mutex_lock(&pcr
->pcr_mutex
);
1173 rtsx_pci_start_run(pcr
);
1175 /* Check SD card detect */
1176 val
= rtsx_pci_card_exist(pcr
);
1177 dev_dbg(sdmmc_dev(host
), "%s: RTSX_BIPR = 0x%08x\n", __func__
, val
);
1181 mutex_unlock(&pcr
->pcr_mutex
);
1186 static int sd_wait_voltage_stable_1(struct realtek_pci_sdmmc
*host
)
1188 struct rtsx_pcr
*pcr
= host
->pcr
;
1192 /* Reference to Signal Voltage Switch Sequence in SD spec.
1193 * Wait for a period of time so that the card can drive SD_CMD and
1194 * SD_DAT[3:0] to low after sending back CMD11 response.
1198 /* SD_CMD, SD_DAT[3:0] should be driven to low by card;
1199 * If either one of SD_CMD,SD_DAT[3:0] is not low,
1200 * abort the voltage switch sequence;
1202 err
= rtsx_pci_read_register(pcr
, SD_BUS_STAT
, &stat
);
1206 if (stat
& (SD_CMD_STATUS
| SD_DAT3_STATUS
| SD_DAT2_STATUS
|
1207 SD_DAT1_STATUS
| SD_DAT0_STATUS
))
1210 /* Stop toggle SD clock */
1211 err
= rtsx_pci_write_register(pcr
, SD_BUS_STAT
,
1212 0xFF, SD_CLK_FORCE_STOP
);
1219 static int sd_wait_voltage_stable_2(struct realtek_pci_sdmmc
*host
)
1221 struct rtsx_pcr
*pcr
= host
->pcr
;
1225 /* Wait 1.8V output of voltage regulator in card stable */
1228 /* Toggle SD clock again */
1229 err
= rtsx_pci_write_register(pcr
, SD_BUS_STAT
, 0xFF, SD_CLK_TOGGLE_EN
);
1233 /* Wait for a period of time so that the card can drive
1234 * SD_DAT[3:0] to high at 1.8V
1238 /* SD_CMD, SD_DAT[3:0] should be pulled high by host */
1239 err
= rtsx_pci_read_register(pcr
, SD_BUS_STAT
, &stat
);
1243 mask
= SD_CMD_STATUS
| SD_DAT3_STATUS
| SD_DAT2_STATUS
|
1244 SD_DAT1_STATUS
| SD_DAT0_STATUS
;
1245 val
= SD_CMD_STATUS
| SD_DAT3_STATUS
| SD_DAT2_STATUS
|
1246 SD_DAT1_STATUS
| SD_DAT0_STATUS
;
1247 if ((stat
& mask
) != val
) {
1248 dev_dbg(sdmmc_dev(host
),
1249 "%s: SD_BUS_STAT = 0x%x\n", __func__
, stat
);
1250 rtsx_pci_write_register(pcr
, SD_BUS_STAT
,
1251 SD_CLK_TOGGLE_EN
| SD_CLK_FORCE_STOP
, 0);
1252 rtsx_pci_write_register(pcr
, CARD_CLK_EN
, 0xFF, 0);
1259 static int sdmmc_switch_voltage(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
1261 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
1262 struct rtsx_pcr
*pcr
= host
->pcr
;
1266 dev_dbg(sdmmc_dev(host
), "%s: signal_voltage = %d\n",
1267 __func__
, ios
->signal_voltage
);
1272 err
= rtsx_pci_card_exclusive_check(host
->pcr
, RTSX_SD_CARD
);
1276 mutex_lock(&pcr
->pcr_mutex
);
1278 rtsx_pci_start_run(pcr
);
1280 if (ios
->signal_voltage
== MMC_SIGNAL_VOLTAGE_330
)
1281 voltage
= OUTPUT_3V3
;
1283 voltage
= OUTPUT_1V8
;
1285 if (voltage
== OUTPUT_1V8
) {
1286 err
= sd_wait_voltage_stable_1(host
);
1291 err
= rtsx_pci_switch_output_voltage(pcr
, voltage
);
1295 if (voltage
== OUTPUT_1V8
) {
1296 err
= sd_wait_voltage_stable_2(host
);
1302 /* Stop toggle SD clock in idle */
1303 err
= rtsx_pci_write_register(pcr
, SD_BUS_STAT
,
1304 SD_CLK_TOGGLE_EN
| SD_CLK_FORCE_STOP
, 0);
1306 mutex_unlock(&pcr
->pcr_mutex
);
1311 static int sdmmc_execute_tuning(struct mmc_host
*mmc
, u32 opcode
)
1313 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
1314 struct rtsx_pcr
*pcr
= host
->pcr
;
1320 err
= rtsx_pci_card_exclusive_check(host
->pcr
, RTSX_SD_CARD
);
1324 mutex_lock(&pcr
->pcr_mutex
);
1326 rtsx_pci_start_run(pcr
);
1328 /* Set initial TX phase */
1329 switch (mmc
->ios
.timing
) {
1330 case MMC_TIMING_UHS_SDR104
:
1331 err
= sd_change_phase(host
, SDR104_TX_PHASE(pcr
), false);
1334 case MMC_TIMING_UHS_SDR50
:
1335 err
= sd_change_phase(host
, SDR50_TX_PHASE(pcr
), false);
1338 case MMC_TIMING_UHS_DDR50
:
1339 err
= sd_change_phase(host
, DDR50_TX_PHASE(pcr
), false);
1349 /* Tuning RX phase */
1350 if ((mmc
->ios
.timing
== MMC_TIMING_UHS_SDR104
) ||
1351 (mmc
->ios
.timing
== MMC_TIMING_UHS_SDR50
))
1352 err
= sd_tuning_rx(host
, opcode
);
1353 else if (mmc
->ios
.timing
== MMC_TIMING_UHS_DDR50
)
1354 err
= sd_change_phase(host
, DDR50_RX_PHASE(pcr
), true);
1357 mutex_unlock(&pcr
->pcr_mutex
);
1362 static int sdmmc_init_sd_express(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
1365 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
1366 struct rtsx_pcr
*pcr
= host
->pcr
;
1368 if (PCI_PID(pcr
) == PID_5264
) {
1369 pcie_capability_clear_and_set_word(pcr
->pci
, PCI_EXP_LNKCTL2
,
1370 PCI_EXP_LNKCTL2_TLS
, PCI_EXP_LNKCTL2_TLS_2_5GT
);
1371 pci_write_config_byte(pcr
->pci
, 0x80e, 0x02);
1372 pcie_capability_clear_and_set_word(pcr
->pci
, PCI_EXP_LNKCTL2
,
1373 PCI_EXP_LNKCTL2_TLS
, PCI_EXP_LNKCTL2_TLS_5_0GT
);
1376 /* Set relink_time for changing to PCIe card */
1377 relink_time
= 0x8FFF;
1379 rtsx_pci_write_register(pcr
, 0xFF01, 0xFF, relink_time
);
1380 rtsx_pci_write_register(pcr
, 0xFF02, 0xFF, relink_time
>> 8);
1381 rtsx_pci_write_register(pcr
, 0xFF03, 0x01, relink_time
>> 16);
1383 rtsx_pci_write_register(pcr
, PETXCFG
, 0x80, 0x80);
1384 rtsx_pci_write_register(pcr
, LDO_VCC_CFG0
,
1385 RTS5261_LDO1_OCP_THD_MASK
,
1386 pcr
->option
.sd_800mA_ocp_thd
);
1388 if (pcr
->ops
->disable_auto_blink
)
1389 pcr
->ops
->disable_auto_blink(pcr
);
1391 if (PCI_PID(pcr
) == PID_5264
) {
1392 rtsx_pci_write_register(pcr
, RTS5264_AUTOLOAD_CFG2
,
1393 RTS5264_CHIP_RST_N_SEL
, RTS5264_CHIP_RST_N_SEL
);
1394 rtsx_pci_write_register(pcr
, GPIO_CTL
, 0x02, 0x00);
1397 /* For PCIe/NVMe mode can't enter delink issue */
1398 pcr
->hw_param
.interrupt_en
&= ~(SD_INT_EN
);
1399 rtsx_pci_writel(pcr
, RTSX_BIER
, pcr
->hw_param
.interrupt_en
);
1401 rtsx_pci_write_register(pcr
, RTS5260_AUTOLOAD_CFG4
,
1402 RTS5261_AUX_CLK_16M_EN
, RTS5261_AUX_CLK_16M_EN
);
1403 rtsx_pci_write_register(pcr
, RTS5261_FW_CFG0
,
1404 RTS5261_FW_ENTER_EXPRESS
, RTS5261_FW_ENTER_EXPRESS
);
1405 rtsx_pci_write_register(pcr
, RTS5261_FW_CFG1
,
1406 RTS5261_MCU_CLOCK_GATING
, RTS5261_MCU_CLOCK_GATING
);
1407 rtsx_pci_write_register(pcr
, RTS5261_FW_CFG1
,
1408 RTS5261_MCU_BUS_SEL_MASK
| RTS5261_MCU_CLOCK_SEL_MASK
1409 | RTS5261_DRIVER_ENABLE_FW
,
1410 RTS5261_MCU_CLOCK_SEL_16M
| RTS5261_DRIVER_ENABLE_FW
);
1415 static const struct mmc_host_ops realtek_pci_sdmmc_ops
= {
1416 .pre_req
= sdmmc_pre_req
,
1417 .post_req
= sdmmc_post_req
,
1418 .request
= sdmmc_request
,
1419 .set_ios
= sdmmc_set_ios
,
1420 .get_ro
= sdmmc_get_ro
,
1421 .get_cd
= sdmmc_get_cd
,
1422 .start_signal_voltage_switch
= sdmmc_switch_voltage
,
1423 .execute_tuning
= sdmmc_execute_tuning
,
1424 .init_sd_express
= sdmmc_init_sd_express
,
1427 static void init_extra_caps(struct realtek_pci_sdmmc
*host
)
1429 struct mmc_host
*mmc
= host
->mmc
;
1430 struct rtsx_pcr
*pcr
= host
->pcr
;
1432 dev_dbg(sdmmc_dev(host
), "pcr->extra_caps = 0x%x\n", pcr
->extra_caps
);
1434 if (pcr
->extra_caps
& EXTRA_CAPS_SD_SDR50
)
1435 mmc
->caps
|= MMC_CAP_UHS_SDR50
;
1436 if (pcr
->extra_caps
& EXTRA_CAPS_SD_SDR104
)
1437 mmc
->caps
|= MMC_CAP_UHS_SDR104
;
1438 if (pcr
->extra_caps
& EXTRA_CAPS_SD_DDR50
)
1439 mmc
->caps
|= MMC_CAP_UHS_DDR50
;
1440 if (pcr
->extra_caps
& EXTRA_CAPS_MMC_HSDDR
)
1441 mmc
->caps
|= MMC_CAP_1_8V_DDR
;
1442 if (pcr
->extra_caps
& EXTRA_CAPS_MMC_8BIT
)
1443 mmc
->caps
|= MMC_CAP_8_BIT_DATA
;
1444 if (pcr
->extra_caps
& EXTRA_CAPS_NO_MMC
)
1445 mmc
->caps2
|= MMC_CAP2_NO_MMC
;
1446 if (pcr
->extra_caps
& EXTRA_CAPS_SD_EXPRESS
)
1447 mmc
->caps2
|= MMC_CAP2_SD_EXP
| MMC_CAP2_SD_EXP_1_2V
;
1450 static void realtek_init_host(struct realtek_pci_sdmmc
*host
)
1452 struct mmc_host
*mmc
= host
->mmc
;
1453 struct rtsx_pcr
*pcr
= host
->pcr
;
1455 mmc
->f_min
= 250000;
1456 mmc
->f_max
= 208000000;
1457 mmc
->ocr_avail
= MMC_VDD_32_33
| MMC_VDD_33_34
| MMC_VDD_165_195
;
1458 mmc
->caps
= MMC_CAP_4_BIT_DATA
| MMC_CAP_SD_HIGHSPEED
|
1459 MMC_CAP_MMC_HIGHSPEED
| MMC_CAP_BUS_WIDTH_TEST
|
1460 MMC_CAP_UHS_SDR12
| MMC_CAP_UHS_SDR25
;
1462 mmc
->caps
= mmc
->caps
| MMC_CAP_AGGRESSIVE_PM
;
1463 mmc
->caps2
= MMC_CAP2_NO_PRESCAN_POWERUP
| MMC_CAP2_FULL_PWR_CYCLE
|
1465 mmc
->max_current_330
= 400;
1466 mmc
->max_current_180
= 800;
1467 mmc
->ops
= &realtek_pci_sdmmc_ops
;
1469 init_extra_caps(host
);
1471 mmc
->max_segs
= 256;
1472 mmc
->max_seg_size
= 65536;
1473 mmc
->max_blk_size
= 512;
1474 mmc
->max_blk_count
= 65535;
1475 mmc
->max_req_size
= 524288;
1478 static void rtsx_pci_sdmmc_card_event(struct platform_device
*pdev
)
1480 struct realtek_pci_sdmmc
*host
= platform_get_drvdata(pdev
);
1483 mmc_detect_change(host
->mmc
, 0);
1486 static int rtsx_pci_sdmmc_drv_probe(struct platform_device
*pdev
)
1488 struct mmc_host
*mmc
;
1489 struct realtek_pci_sdmmc
*host
;
1490 struct rtsx_pcr
*pcr
;
1491 struct pcr_handle
*handle
= pdev
->dev
.platform_data
;
1501 dev_dbg(&(pdev
->dev
), ": Realtek PCI-E SDMMC controller found\n");
1503 mmc
= mmc_alloc_host(sizeof(*host
), &pdev
->dev
);
1507 host
= mmc_priv(mmc
);
1509 mmc
->ios
.power_delay_ms
= 5;
1513 host
->prev_power_state
= MMC_POWER_OFF
;
1514 INIT_WORK(&host
->work
, sd_request
);
1515 platform_set_drvdata(pdev
, host
);
1516 pcr
->slots
[RTSX_SD_CARD
].p_dev
= pdev
;
1517 pcr
->slots
[RTSX_SD_CARD
].card_event
= rtsx_pci_sdmmc_card_event
;
1519 mutex_init(&host
->host_mutex
);
1521 realtek_init_host(host
);
1523 pm_runtime_no_callbacks(&pdev
->dev
);
1524 pm_runtime_set_active(&pdev
->dev
);
1525 pm_runtime_enable(&pdev
->dev
);
1526 pm_runtime_set_autosuspend_delay(&pdev
->dev
, 200);
1527 pm_runtime_mark_last_busy(&pdev
->dev
);
1528 pm_runtime_use_autosuspend(&pdev
->dev
);
1530 ret
= mmc_add_host(mmc
);
1532 pm_runtime_dont_use_autosuspend(&pdev
->dev
);
1533 pm_runtime_disable(&pdev
->dev
);
1541 static void rtsx_pci_sdmmc_drv_remove(struct platform_device
*pdev
)
1543 struct realtek_pci_sdmmc
*host
= platform_get_drvdata(pdev
);
1544 struct rtsx_pcr
*pcr
;
1545 struct mmc_host
*mmc
;
1548 pcr
->slots
[RTSX_SD_CARD
].p_dev
= NULL
;
1549 pcr
->slots
[RTSX_SD_CARD
].card_event
= NULL
;
1552 cancel_work_sync(&host
->work
);
1554 mutex_lock(&host
->host_mutex
);
1556 dev_dbg(&(pdev
->dev
),
1557 "%s: Controller removed during transfer\n",
1560 rtsx_pci_complete_unfinished_transfer(pcr
);
1562 host
->mrq
->cmd
->error
= -ENOMEDIUM
;
1563 if (host
->mrq
->stop
)
1564 host
->mrq
->stop
->error
= -ENOMEDIUM
;
1565 mmc_request_done(mmc
, host
->mrq
);
1567 mutex_unlock(&host
->host_mutex
);
1569 mmc_remove_host(mmc
);
1572 flush_work(&host
->work
);
1574 pm_runtime_dont_use_autosuspend(&pdev
->dev
);
1575 pm_runtime_disable(&pdev
->dev
);
1579 dev_dbg(&(pdev
->dev
),
1580 ": Realtek PCI-E SDMMC controller has been removed\n");
1583 static const struct platform_device_id rtsx_pci_sdmmc_ids
[] = {
1585 .name
= DRV_NAME_RTSX_PCI_SDMMC
,
1590 MODULE_DEVICE_TABLE(platform
, rtsx_pci_sdmmc_ids
);
1592 static struct platform_driver rtsx_pci_sdmmc_driver
= {
1593 .probe
= rtsx_pci_sdmmc_drv_probe
,
1594 .remove
= rtsx_pci_sdmmc_drv_remove
,
1595 .id_table
= rtsx_pci_sdmmc_ids
,
1597 .name
= DRV_NAME_RTSX_PCI_SDMMC
,
1598 .probe_type
= PROBE_PREFER_ASYNCHRONOUS
,
1601 module_platform_driver(rtsx_pci_sdmmc_driver
);
1603 MODULE_LICENSE("GPL");
1604 MODULE_AUTHOR("Wei WANG <wei_wang@realsil.com.cn>");
1605 MODULE_DESCRIPTION("Realtek PCI-E SD/MMC Card Host Driver");