1 /* Realtek PCI-Express SD/MMC Card Interface driver
3 * Copyright(c) 2009-2013 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>
22 #include <linux/module.h>
23 #include <linux/slab.h>
24 #include <linux/highmem.h>
25 #include <linux/delay.h>
26 #include <linux/platform_device.h>
27 #include <linux/workqueue.h>
28 #include <linux/mmc/host.h>
29 #include <linux/mmc/mmc.h>
30 #include <linux/mmc/sd.h>
31 #include <linux/mmc/sdio.h>
32 #include <linux/mmc/card.h>
33 #include <linux/mfd/rtsx_pci.h>
34 #include <asm/unaligned.h>
36 struct realtek_pci_sdmmc
{
37 struct platform_device
*pdev
;
40 struct mmc_request
*mrq
;
41 #define SDMMC_WORKQ_NAME "rtsx_pci_sdmmc_workq"
43 struct work_struct work
;
44 struct mutex host_mutex
;
53 #define SDMMC_POWER_ON 1
54 #define SDMMC_POWER_OFF 0
62 static inline struct device
*sdmmc_dev(struct realtek_pci_sdmmc
*host
)
64 return &(host
->pdev
->dev
);
67 static inline void sd_clear_error(struct realtek_pci_sdmmc
*host
)
69 rtsx_pci_write_register(host
->pcr
, CARD_STOP
,
70 SD_STOP
| SD_CLR_ERR
, SD_STOP
| SD_CLR_ERR
);
74 static void dump_reg_range(struct realtek_pci_sdmmc
*host
, u16 start
, u16 end
)
76 u16 len
= end
- start
+ 1;
80 for (i
= 0; i
< len
; i
+= 8) {
82 int n
= min(8, len
- i
);
84 memset(&data
, 0, sizeof(data
));
85 for (j
= 0; j
< n
; j
++)
86 rtsx_pci_read_register(host
->pcr
, start
+ i
+ j
,
88 dev_dbg(sdmmc_dev(host
), "0x%04X(%d): %8ph\n",
93 static void sd_print_debug_regs(struct realtek_pci_sdmmc
*host
)
95 dump_reg_range(host
, 0xFDA0, 0xFDB3);
96 dump_reg_range(host
, 0xFD52, 0xFD69);
99 #define sd_print_debug_regs(host)
102 static inline int sd_get_cd_int(struct realtek_pci_sdmmc
*host
)
104 return rtsx_pci_readl(host
->pcr
, RTSX_BIPR
) & SD_EXIST
;
107 static void sd_cmd_set_sd_cmd(struct rtsx_pcr
*pcr
, struct mmc_command
*cmd
)
109 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CMD0
, 0xFF,
110 SD_CMD_START
| cmd
->opcode
);
111 rtsx_pci_write_be32(pcr
, SD_CMD1
, cmd
->arg
);
114 static void sd_cmd_set_data_len(struct rtsx_pcr
*pcr
, u16 blocks
, u16 blksz
)
116 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_BLOCK_CNT_L
, 0xFF, blocks
);
117 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_BLOCK_CNT_H
, 0xFF, blocks
>> 8);
118 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_BYTE_CNT_L
, 0xFF, blksz
);
119 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_BYTE_CNT_H
, 0xFF, blksz
>> 8);
122 static int sd_response_type(struct mmc_command
*cmd
)
124 switch (mmc_resp_type(cmd
)) {
126 return SD_RSP_TYPE_R0
;
128 return SD_RSP_TYPE_R1
;
129 case MMC_RSP_R1_NO_CRC
:
130 return SD_RSP_TYPE_R1
| SD_NO_CHECK_CRC7
;
132 return SD_RSP_TYPE_R1b
;
134 return SD_RSP_TYPE_R2
;
136 return SD_RSP_TYPE_R3
;
142 static int sd_status_index(int resp_type
)
144 if (resp_type
== SD_RSP_TYPE_R0
)
146 else if (resp_type
== SD_RSP_TYPE_R2
)
152 * sd_pre_dma_transfer - do dma_map_sg() or using cookie
154 * @pre: if called in pre_req()
156 * 0 - do dma_map_sg()
159 static int sd_pre_dma_transfer(struct realtek_pci_sdmmc
*host
,
160 struct mmc_data
*data
, bool pre
)
162 struct rtsx_pcr
*pcr
= host
->pcr
;
163 int read
= data
->flags
& MMC_DATA_READ
;
165 int using_cookie
= 0;
167 if (!pre
&& data
->host_cookie
&& data
->host_cookie
!= host
->cookie
) {
168 dev_err(sdmmc_dev(host
),
169 "error: data->host_cookie = %d, host->cookie = %d\n",
170 data
->host_cookie
, host
->cookie
);
171 data
->host_cookie
= 0;
174 if (pre
|| data
->host_cookie
!= host
->cookie
) {
175 count
= rtsx_pci_dma_map_sg(pcr
, data
->sg
, data
->sg_len
, read
);
177 count
= host
->cookie_sg_count
;
182 host
->cookie_sg_count
= count
;
183 if (++host
->cookie
< 0)
185 data
->host_cookie
= host
->cookie
;
187 host
->sg_count
= count
;
193 static void sdmmc_pre_req(struct mmc_host
*mmc
, struct mmc_request
*mrq
,
196 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
197 struct mmc_data
*data
= mrq
->data
;
199 if (data
->host_cookie
) {
200 dev_err(sdmmc_dev(host
),
201 "error: reset data->host_cookie = %d\n",
203 data
->host_cookie
= 0;
206 sd_pre_dma_transfer(host
, data
, true);
207 dev_dbg(sdmmc_dev(host
), "pre dma sg: %d\n", host
->cookie_sg_count
);
210 static void sdmmc_post_req(struct mmc_host
*mmc
, struct mmc_request
*mrq
,
213 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
214 struct rtsx_pcr
*pcr
= host
->pcr
;
215 struct mmc_data
*data
= mrq
->data
;
216 int read
= data
->flags
& MMC_DATA_READ
;
218 rtsx_pci_dma_unmap_sg(pcr
, data
->sg
, data
->sg_len
, read
);
219 data
->host_cookie
= 0;
222 static void sd_send_cmd_get_rsp(struct realtek_pci_sdmmc
*host
,
223 struct mmc_command
*cmd
)
225 struct rtsx_pcr
*pcr
= host
->pcr
;
226 u8 cmd_idx
= (u8
)cmd
->opcode
;
234 bool clock_toggled
= false;
236 dev_dbg(sdmmc_dev(host
), "%s: SD/MMC CMD %d, arg = 0x%08x\n",
237 __func__
, cmd_idx
, arg
);
239 rsp_type
= sd_response_type(cmd
);
243 stat_idx
= sd_status_index(rsp_type
);
245 if (rsp_type
== SD_RSP_TYPE_R1b
)
246 timeout
= cmd
->busy_timeout
? cmd
->busy_timeout
: 3000;
248 if (cmd
->opcode
== SD_SWITCH_VOLTAGE
) {
249 err
= rtsx_pci_write_register(pcr
, SD_BUS_STAT
,
250 0xFF, SD_CLK_TOGGLE_EN
);
254 clock_toggled
= true;
257 rtsx_pci_init_cmd(pcr
);
258 sd_cmd_set_sd_cmd(pcr
, cmd
);
259 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG2
, 0xFF, rsp_type
);
260 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_DATA_SOURCE
,
261 0x01, PINGPONG_BUFFER
);
262 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_TRANSFER
,
263 0xFF, SD_TM_CMD_RSP
| SD_TRANSFER_START
);
264 rtsx_pci_add_cmd(pcr
, CHECK_REG_CMD
, SD_TRANSFER
,
265 SD_TRANSFER_END
| SD_STAT_IDLE
,
266 SD_TRANSFER_END
| SD_STAT_IDLE
);
268 if (rsp_type
== SD_RSP_TYPE_R2
) {
269 /* Read data from ping-pong buffer */
270 for (i
= PPBUF_BASE2
; i
< PPBUF_BASE2
+ 16; i
++)
271 rtsx_pci_add_cmd(pcr
, READ_REG_CMD
, (u16
)i
, 0, 0);
272 } else if (rsp_type
!= SD_RSP_TYPE_R0
) {
273 /* Read data from SD_CMDx registers */
274 for (i
= SD_CMD0
; i
<= SD_CMD4
; i
++)
275 rtsx_pci_add_cmd(pcr
, READ_REG_CMD
, (u16
)i
, 0, 0);
278 rtsx_pci_add_cmd(pcr
, READ_REG_CMD
, SD_STAT1
, 0, 0);
280 err
= rtsx_pci_send_cmd(pcr
, timeout
);
282 sd_print_debug_regs(host
);
283 sd_clear_error(host
);
284 dev_dbg(sdmmc_dev(host
),
285 "rtsx_pci_send_cmd error (err = %d)\n", err
);
289 if (rsp_type
== SD_RSP_TYPE_R0
) {
294 /* Eliminate returned value of CHECK_REG_CMD */
295 ptr
= rtsx_pci_get_cmd_data(pcr
) + 1;
297 /* Check (Start,Transmission) bit of Response */
298 if ((ptr
[0] & 0xC0) != 0) {
300 dev_dbg(sdmmc_dev(host
), "Invalid response bit\n");
305 if (!(rsp_type
& SD_NO_CHECK_CRC7
)) {
306 if (ptr
[stat_idx
] & SD_CRC7_ERR
) {
308 dev_dbg(sdmmc_dev(host
), "CRC7 error\n");
313 if (rsp_type
== SD_RSP_TYPE_R2
) {
315 * The controller offloads the last byte {CRC-7, end bit 1'b1}
316 * of response type R2. Assign dummy CRC, 0, and end bit to the
317 * byte(ptr[16], goes into the LSB of resp[3] later).
321 for (i
= 0; i
< 4; i
++) {
322 cmd
->resp
[i
] = get_unaligned_be32(ptr
+ 1 + i
* 4);
323 dev_dbg(sdmmc_dev(host
), "cmd->resp[%d] = 0x%08x\n",
327 cmd
->resp
[0] = get_unaligned_be32(ptr
+ 1);
328 dev_dbg(sdmmc_dev(host
), "cmd->resp[0] = 0x%08x\n",
335 if (err
&& clock_toggled
)
336 rtsx_pci_write_register(pcr
, SD_BUS_STAT
,
337 SD_CLK_TOGGLE_EN
| SD_CLK_FORCE_STOP
, 0);
340 static int sd_read_data(struct realtek_pci_sdmmc
*host
, struct mmc_command
*cmd
,
341 u16 byte_cnt
, u8
*buf
, int buf_len
, int timeout
)
343 struct rtsx_pcr
*pcr
= host
->pcr
;
347 dev_dbg(sdmmc_dev(host
), "%s: SD/MMC CMD %d, arg = 0x%08x\n",
348 __func__
, cmd
->opcode
, cmd
->arg
);
353 if (cmd
->opcode
== MMC_SEND_TUNING_BLOCK
)
354 trans_mode
= SD_TM_AUTO_TUNING
;
356 trans_mode
= SD_TM_NORMAL_READ
;
358 rtsx_pci_init_cmd(pcr
);
359 sd_cmd_set_sd_cmd(pcr
, cmd
);
360 sd_cmd_set_data_len(pcr
, 1, byte_cnt
);
361 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG2
, 0xFF,
362 SD_CALCULATE_CRC7
| SD_CHECK_CRC16
|
363 SD_NO_WAIT_BUSY_END
| SD_CHECK_CRC7
| SD_RSP_LEN_6
);
364 if (trans_mode
!= SD_TM_AUTO_TUNING
)
365 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
,
366 CARD_DATA_SOURCE
, 0x01, PINGPONG_BUFFER
);
368 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_TRANSFER
,
369 0xFF, trans_mode
| SD_TRANSFER_START
);
370 rtsx_pci_add_cmd(pcr
, CHECK_REG_CMD
, SD_TRANSFER
,
371 SD_TRANSFER_END
, SD_TRANSFER_END
);
373 err
= rtsx_pci_send_cmd(pcr
, timeout
);
375 sd_print_debug_regs(host
);
376 dev_dbg(sdmmc_dev(host
),
377 "rtsx_pci_send_cmd fail (err = %d)\n", err
);
381 if (buf
&& buf_len
) {
382 err
= rtsx_pci_read_ppbuf(pcr
, buf
, buf_len
);
384 dev_dbg(sdmmc_dev(host
),
385 "rtsx_pci_read_ppbuf fail (err = %d)\n", err
);
393 static int sd_write_data(struct realtek_pci_sdmmc
*host
,
394 struct mmc_command
*cmd
, u16 byte_cnt
, u8
*buf
, int buf_len
,
397 struct rtsx_pcr
*pcr
= host
->pcr
;
400 dev_dbg(sdmmc_dev(host
), "%s: SD/MMC CMD %d, arg = 0x%08x\n",
401 __func__
, cmd
->opcode
, cmd
->arg
);
406 sd_send_cmd_get_rsp(host
, cmd
);
410 if (buf
&& buf_len
) {
411 err
= rtsx_pci_write_ppbuf(pcr
, buf
, buf_len
);
413 dev_dbg(sdmmc_dev(host
),
414 "rtsx_pci_write_ppbuf fail (err = %d)\n", err
);
419 rtsx_pci_init_cmd(pcr
);
420 sd_cmd_set_data_len(pcr
, 1, byte_cnt
);
421 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG2
, 0xFF,
422 SD_CALCULATE_CRC7
| SD_CHECK_CRC16
|
423 SD_NO_WAIT_BUSY_END
| SD_CHECK_CRC7
| SD_RSP_LEN_0
);
424 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_TRANSFER
, 0xFF,
425 SD_TRANSFER_START
| SD_TM_AUTO_WRITE_3
);
426 rtsx_pci_add_cmd(pcr
, CHECK_REG_CMD
, SD_TRANSFER
,
427 SD_TRANSFER_END
, SD_TRANSFER_END
);
429 err
= rtsx_pci_send_cmd(pcr
, timeout
);
431 sd_print_debug_regs(host
);
432 dev_dbg(sdmmc_dev(host
),
433 "rtsx_pci_send_cmd fail (err = %d)\n", err
);
440 static int sd_read_long_data(struct realtek_pci_sdmmc
*host
,
441 struct mmc_request
*mrq
)
443 struct rtsx_pcr
*pcr
= host
->pcr
;
444 struct mmc_host
*mmc
= host
->mmc
;
445 struct mmc_card
*card
= mmc
->card
;
446 struct mmc_command
*cmd
= mrq
->cmd
;
447 struct mmc_data
*data
= mrq
->data
;
448 int uhs
= mmc_card_uhs(card
);
452 size_t data_len
= data
->blksz
* data
->blocks
;
454 dev_dbg(sdmmc_dev(host
), "%s: SD/MMC CMD %d, arg = 0x%08x\n",
455 __func__
, cmd
->opcode
, cmd
->arg
);
457 resp_type
= sd_response_type(cmd
);
462 cfg2
|= SD_NO_CHECK_WAIT_CRC_TO
;
464 rtsx_pci_init_cmd(pcr
);
465 sd_cmd_set_sd_cmd(pcr
, cmd
);
466 sd_cmd_set_data_len(pcr
, data
->blocks
, data
->blksz
);
467 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, IRQSTAT0
,
468 DMA_DONE_INT
, DMA_DONE_INT
);
469 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC3
,
470 0xFF, (u8
)(data_len
>> 24));
471 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC2
,
472 0xFF, (u8
)(data_len
>> 16));
473 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC1
,
474 0xFF, (u8
)(data_len
>> 8));
475 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC0
, 0xFF, (u8
)data_len
);
476 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMACTL
,
477 0x03 | DMA_PACK_SIZE_MASK
,
478 DMA_DIR_FROM_CARD
| DMA_EN
| DMA_512
);
479 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_DATA_SOURCE
,
481 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG2
, 0xFF, cfg2
| resp_type
);
482 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_TRANSFER
, 0xFF,
483 SD_TRANSFER_START
| SD_TM_AUTO_READ_2
);
484 rtsx_pci_add_cmd(pcr
, CHECK_REG_CMD
, SD_TRANSFER
,
485 SD_TRANSFER_END
, SD_TRANSFER_END
);
486 rtsx_pci_send_cmd_no_wait(pcr
);
488 err
= rtsx_pci_dma_transfer(pcr
, data
->sg
, host
->sg_count
, 1, 10000);
490 sd_print_debug_regs(host
);
491 sd_clear_error(host
);
498 static int sd_write_long_data(struct realtek_pci_sdmmc
*host
,
499 struct mmc_request
*mrq
)
501 struct rtsx_pcr
*pcr
= host
->pcr
;
502 struct mmc_host
*mmc
= host
->mmc
;
503 struct mmc_card
*card
= mmc
->card
;
504 struct mmc_command
*cmd
= mrq
->cmd
;
505 struct mmc_data
*data
= mrq
->data
;
506 int uhs
= mmc_card_uhs(card
);
509 size_t data_len
= data
->blksz
* data
->blocks
;
511 sd_send_cmd_get_rsp(host
, cmd
);
515 dev_dbg(sdmmc_dev(host
), "%s: SD/MMC CMD %d, arg = 0x%08x\n",
516 __func__
, cmd
->opcode
, cmd
->arg
);
518 cfg2
= SD_NO_CALCULATE_CRC7
| SD_CHECK_CRC16
|
519 SD_NO_WAIT_BUSY_END
| SD_NO_CHECK_CRC7
| SD_RSP_LEN_0
;
522 cfg2
|= SD_NO_CHECK_WAIT_CRC_TO
;
524 rtsx_pci_init_cmd(pcr
);
525 sd_cmd_set_data_len(pcr
, data
->blocks
, data
->blksz
);
526 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, IRQSTAT0
,
527 DMA_DONE_INT
, DMA_DONE_INT
);
528 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC3
,
529 0xFF, (u8
)(data_len
>> 24));
530 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC2
,
531 0xFF, (u8
)(data_len
>> 16));
532 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC1
,
533 0xFF, (u8
)(data_len
>> 8));
534 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC0
, 0xFF, (u8
)data_len
);
535 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMACTL
,
536 0x03 | DMA_PACK_SIZE_MASK
,
537 DMA_DIR_TO_CARD
| DMA_EN
| DMA_512
);
538 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_DATA_SOURCE
,
540 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG2
, 0xFF, cfg2
);
541 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_TRANSFER
, 0xFF,
542 SD_TRANSFER_START
| SD_TM_AUTO_WRITE_3
);
543 rtsx_pci_add_cmd(pcr
, CHECK_REG_CMD
, SD_TRANSFER
,
544 SD_TRANSFER_END
, SD_TRANSFER_END
);
545 rtsx_pci_send_cmd_no_wait(pcr
);
546 err
= rtsx_pci_dma_transfer(pcr
, data
->sg
, host
->sg_count
, 0, 10000);
548 sd_clear_error(host
);
555 static int sd_rw_multi(struct realtek_pci_sdmmc
*host
, struct mmc_request
*mrq
)
557 struct mmc_data
*data
= mrq
->data
;
559 if (host
->sg_count
< 0) {
560 data
->error
= host
->sg_count
;
561 dev_dbg(sdmmc_dev(host
), "%s: sg_count = %d is invalid\n",
562 __func__
, host
->sg_count
);
566 if (data
->flags
& MMC_DATA_READ
)
567 return sd_read_long_data(host
, mrq
);
569 return sd_write_long_data(host
, mrq
);
572 static inline void sd_enable_initial_mode(struct realtek_pci_sdmmc
*host
)
574 rtsx_pci_write_register(host
->pcr
, SD_CFG1
,
575 SD_CLK_DIVIDE_MASK
, SD_CLK_DIVIDE_128
);
578 static inline void sd_disable_initial_mode(struct realtek_pci_sdmmc
*host
)
580 rtsx_pci_write_register(host
->pcr
, SD_CFG1
,
581 SD_CLK_DIVIDE_MASK
, SD_CLK_DIVIDE_0
);
584 static void sd_normal_rw(struct realtek_pci_sdmmc
*host
,
585 struct mmc_request
*mrq
)
587 struct mmc_command
*cmd
= mrq
->cmd
;
588 struct mmc_data
*data
= mrq
->data
;
591 buf
= kzalloc(data
->blksz
, GFP_NOIO
);
593 cmd
->error
= -ENOMEM
;
597 if (data
->flags
& MMC_DATA_READ
) {
598 if (host
->initial_mode
)
599 sd_disable_initial_mode(host
);
601 cmd
->error
= sd_read_data(host
, cmd
, (u16
)data
->blksz
, buf
,
604 if (host
->initial_mode
)
605 sd_enable_initial_mode(host
);
607 sg_copy_from_buffer(data
->sg
, data
->sg_len
, buf
, data
->blksz
);
609 sg_copy_to_buffer(data
->sg
, data
->sg_len
, buf
, data
->blksz
);
611 cmd
->error
= sd_write_data(host
, cmd
, (u16
)data
->blksz
, buf
,
618 static int sd_change_phase(struct realtek_pci_sdmmc
*host
,
619 u8 sample_point
, bool rx
)
621 struct rtsx_pcr
*pcr
= host
->pcr
;
624 dev_dbg(sdmmc_dev(host
), "%s(%s): sample_point = %d\n",
625 __func__
, rx
? "RX" : "TX", sample_point
);
627 rtsx_pci_init_cmd(pcr
);
629 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
, CHANGE_CLK
, CHANGE_CLK
);
631 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
,
632 SD_VPRX_CTL
, 0x1F, sample_point
);
634 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
,
635 SD_VPTX_CTL
, 0x1F, sample_point
);
636 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_VPCLK0_CTL
, PHASE_NOT_RESET
, 0);
637 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_VPCLK0_CTL
,
638 PHASE_NOT_RESET
, PHASE_NOT_RESET
);
639 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
, CHANGE_CLK
, 0);
640 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG1
, SD_ASYNC_FIFO_NOT_RST
, 0);
642 err
= rtsx_pci_send_cmd(pcr
, 100);
649 static inline u32
test_phase_bit(u32 phase_map
, unsigned int bit
)
651 bit
%= RTSX_PHASE_MAX
;
652 return phase_map
& (1 << bit
);
655 static int sd_get_phase_len(u32 phase_map
, unsigned int start_bit
)
659 for (i
= 0; i
< RTSX_PHASE_MAX
; i
++) {
660 if (test_phase_bit(phase_map
, start_bit
+ i
) == 0)
663 return RTSX_PHASE_MAX
;
666 static u8
sd_search_final_phase(struct realtek_pci_sdmmc
*host
, u32 phase_map
)
668 int start
= 0, len
= 0;
669 int start_final
= 0, len_final
= 0;
670 u8 final_phase
= 0xFF;
672 if (phase_map
== 0) {
673 dev_err(sdmmc_dev(host
), "phase error: [map:%x]\n", phase_map
);
677 while (start
< RTSX_PHASE_MAX
) {
678 len
= sd_get_phase_len(phase_map
, start
);
679 if (len_final
< len
) {
683 start
+= len
? len
: 1;
686 final_phase
= (start_final
+ len_final
/ 2) % RTSX_PHASE_MAX
;
687 dev_dbg(sdmmc_dev(host
), "phase: [map:%x] [maxlen:%d] [final:%d]\n",
688 phase_map
, len_final
, final_phase
);
693 static void sd_wait_data_idle(struct realtek_pci_sdmmc
*host
)
698 for (i
= 0; i
< 100; i
++) {
699 err
= rtsx_pci_read_register(host
->pcr
, SD_DATA_STATE
, &val
);
700 if (val
& SD_DATA_IDLE
)
707 static int sd_tuning_rx_cmd(struct realtek_pci_sdmmc
*host
,
708 u8 opcode
, u8 sample_point
)
711 struct mmc_command cmd
= {0};
713 err
= sd_change_phase(host
, sample_point
, true);
718 err
= sd_read_data(host
, &cmd
, 0x40, NULL
, 0, 100);
720 /* Wait till SD DATA IDLE */
721 sd_wait_data_idle(host
);
722 sd_clear_error(host
);
729 static int sd_tuning_phase(struct realtek_pci_sdmmc
*host
,
730 u8 opcode
, u32
*phase_map
)
733 u32 raw_phase_map
= 0;
735 for (i
= 0; i
< RTSX_PHASE_MAX
; i
++) {
736 err
= sd_tuning_rx_cmd(host
, opcode
, (u8
)i
);
738 raw_phase_map
|= 1 << i
;
742 *phase_map
= raw_phase_map
;
747 static int sd_tuning_rx(struct realtek_pci_sdmmc
*host
, u8 opcode
)
750 u32 raw_phase_map
[RX_TUNING_CNT
] = {0}, phase_map
;
753 for (i
= 0; i
< RX_TUNING_CNT
; i
++) {
754 err
= sd_tuning_phase(host
, opcode
, &(raw_phase_map
[i
]));
758 if (raw_phase_map
[i
] == 0)
762 phase_map
= 0xFFFFFFFF;
763 for (i
= 0; i
< RX_TUNING_CNT
; i
++) {
764 dev_dbg(sdmmc_dev(host
), "RX raw_phase_map[%d] = 0x%08x\n",
765 i
, raw_phase_map
[i
]);
766 phase_map
&= raw_phase_map
[i
];
768 dev_dbg(sdmmc_dev(host
), "RX phase_map = 0x%08x\n", phase_map
);
771 final_phase
= sd_search_final_phase(host
, phase_map
);
772 if (final_phase
== 0xFF)
775 err
= sd_change_phase(host
, final_phase
, true);
785 static inline int sdio_extblock_cmd(struct mmc_command
*cmd
,
786 struct mmc_data
*data
)
788 return (cmd
->opcode
== SD_IO_RW_EXTENDED
) && (data
->blksz
== 512);
791 static inline int sd_rw_cmd(struct mmc_command
*cmd
)
793 return mmc_op_multi(cmd
->opcode
) ||
794 (cmd
->opcode
== MMC_READ_SINGLE_BLOCK
) ||
795 (cmd
->opcode
== MMC_WRITE_BLOCK
);
798 static void sd_request(struct work_struct
*work
)
800 struct realtek_pci_sdmmc
*host
= container_of(work
,
801 struct realtek_pci_sdmmc
, work
);
802 struct rtsx_pcr
*pcr
= host
->pcr
;
804 struct mmc_host
*mmc
= host
->mmc
;
805 struct mmc_request
*mrq
= host
->mrq
;
806 struct mmc_command
*cmd
= mrq
->cmd
;
807 struct mmc_data
*data
= mrq
->data
;
809 unsigned int data_size
= 0;
812 if (host
->eject
|| !sd_get_cd_int(host
)) {
813 cmd
->error
= -ENOMEDIUM
;
817 err
= rtsx_pci_card_exclusive_check(host
->pcr
, RTSX_SD_CARD
);
823 mutex_lock(&pcr
->pcr_mutex
);
825 rtsx_pci_start_run(pcr
);
827 rtsx_pci_switch_clock(pcr
, host
->clock
, host
->ssc_depth
,
828 host
->initial_mode
, host
->double_clk
, host
->vpclk
);
829 rtsx_pci_write_register(pcr
, CARD_SELECT
, 0x07, SD_MOD_SEL
);
830 rtsx_pci_write_register(pcr
, CARD_SHARE_MODE
,
831 CARD_SHARE_MASK
, CARD_SHARE_48_SD
);
833 mutex_lock(&host
->host_mutex
);
835 mutex_unlock(&host
->host_mutex
);
838 data_size
= data
->blocks
* data
->blksz
;
841 sd_send_cmd_get_rsp(host
, cmd
);
842 } else if (sd_rw_cmd(cmd
) || sdio_extblock_cmd(cmd
, data
)) {
843 cmd
->error
= sd_rw_multi(host
, mrq
);
844 if (!host
->using_cookie
)
845 sdmmc_post_req(host
->mmc
, host
->mrq
, 0);
847 if (mmc_op_multi(cmd
->opcode
) && mrq
->stop
)
848 sd_send_cmd_get_rsp(host
, mrq
->stop
);
850 sd_normal_rw(host
, mrq
);
854 if (cmd
->error
|| data
->error
)
855 data
->bytes_xfered
= 0;
857 data
->bytes_xfered
= data
->blocks
* data
->blksz
;
860 mutex_unlock(&pcr
->pcr_mutex
);
864 dev_dbg(sdmmc_dev(host
), "CMD %d 0x%08x error(%d)\n",
865 cmd
->opcode
, cmd
->arg
, cmd
->error
);
868 mutex_lock(&host
->host_mutex
);
870 mutex_unlock(&host
->host_mutex
);
872 mmc_request_done(mmc
, mrq
);
875 static void sdmmc_request(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
877 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
878 struct mmc_data
*data
= mrq
->data
;
880 mutex_lock(&host
->host_mutex
);
882 mutex_unlock(&host
->host_mutex
);
884 if (sd_rw_cmd(mrq
->cmd
) || sdio_extblock_cmd(mrq
->cmd
, data
))
885 host
->using_cookie
= sd_pre_dma_transfer(host
, data
, false);
887 schedule_work(&host
->work
);
890 static int sd_set_bus_width(struct realtek_pci_sdmmc
*host
,
891 unsigned char bus_width
)
895 [MMC_BUS_WIDTH_1
] = SD_BUS_WIDTH_1BIT
,
896 [MMC_BUS_WIDTH_4
] = SD_BUS_WIDTH_4BIT
,
897 [MMC_BUS_WIDTH_8
] = SD_BUS_WIDTH_8BIT
,
900 if (bus_width
<= MMC_BUS_WIDTH_8
)
901 err
= rtsx_pci_write_register(host
->pcr
, SD_CFG1
,
902 0x03, width
[bus_width
]);
907 static int sd_power_on(struct realtek_pci_sdmmc
*host
)
909 struct rtsx_pcr
*pcr
= host
->pcr
;
912 if (host
->power_state
== SDMMC_POWER_ON
)
915 rtsx_pci_init_cmd(pcr
);
916 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_SELECT
, 0x07, SD_MOD_SEL
);
917 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_SHARE_MODE
,
918 CARD_SHARE_MASK
, CARD_SHARE_48_SD
);
919 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_CLK_EN
,
920 SD_CLK_EN
, SD_CLK_EN
);
921 err
= rtsx_pci_send_cmd(pcr
, 100);
925 err
= rtsx_pci_card_pull_ctl_enable(pcr
, RTSX_SD_CARD
);
929 err
= rtsx_pci_card_power_on(pcr
, RTSX_SD_CARD
);
933 err
= rtsx_pci_write_register(pcr
, CARD_OE
, SD_OUTPUT_EN
, SD_OUTPUT_EN
);
937 host
->power_state
= SDMMC_POWER_ON
;
941 static int sd_power_off(struct realtek_pci_sdmmc
*host
)
943 struct rtsx_pcr
*pcr
= host
->pcr
;
946 host
->power_state
= SDMMC_POWER_OFF
;
948 rtsx_pci_init_cmd(pcr
);
950 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_CLK_EN
, SD_CLK_EN
, 0);
951 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_OE
, SD_OUTPUT_EN
, 0);
953 err
= rtsx_pci_send_cmd(pcr
, 100);
957 err
= rtsx_pci_card_power_off(pcr
, RTSX_SD_CARD
);
961 return rtsx_pci_card_pull_ctl_disable(pcr
, RTSX_SD_CARD
);
964 static int sd_set_power_mode(struct realtek_pci_sdmmc
*host
,
965 unsigned char power_mode
)
969 if (power_mode
== MMC_POWER_OFF
)
970 err
= sd_power_off(host
);
972 err
= sd_power_on(host
);
977 static int sd_set_timing(struct realtek_pci_sdmmc
*host
, unsigned char timing
)
979 struct rtsx_pcr
*pcr
= host
->pcr
;
982 rtsx_pci_init_cmd(pcr
);
985 case MMC_TIMING_UHS_SDR104
:
986 case MMC_TIMING_UHS_SDR50
:
987 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG1
,
988 0x0C | SD_ASYNC_FIFO_NOT_RST
,
989 SD_30_MODE
| SD_ASYNC_FIFO_NOT_RST
);
990 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
,
991 CLK_LOW_FREQ
, CLK_LOW_FREQ
);
992 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_CLK_SOURCE
, 0xFF,
993 CRC_VAR_CLK0
| SD30_FIX_CLK
| SAMPLE_VAR_CLK1
);
994 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
, CLK_LOW_FREQ
, 0);
997 case MMC_TIMING_MMC_DDR52
:
998 case MMC_TIMING_UHS_DDR50
:
999 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG1
,
1000 0x0C | SD_ASYNC_FIFO_NOT_RST
,
1001 SD_DDR_MODE
| SD_ASYNC_FIFO_NOT_RST
);
1002 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
,
1003 CLK_LOW_FREQ
, CLK_LOW_FREQ
);
1004 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_CLK_SOURCE
, 0xFF,
1005 CRC_VAR_CLK0
| SD30_FIX_CLK
| SAMPLE_VAR_CLK1
);
1006 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
, CLK_LOW_FREQ
, 0);
1007 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_PUSH_POINT_CTL
,
1008 DDR_VAR_TX_CMD_DAT
, DDR_VAR_TX_CMD_DAT
);
1009 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_SAMPLE_POINT_CTL
,
1010 DDR_VAR_RX_DAT
| DDR_VAR_RX_CMD
,
1011 DDR_VAR_RX_DAT
| DDR_VAR_RX_CMD
);
1014 case MMC_TIMING_MMC_HS
:
1015 case MMC_TIMING_SD_HS
:
1016 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG1
,
1018 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
,
1019 CLK_LOW_FREQ
, CLK_LOW_FREQ
);
1020 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_CLK_SOURCE
, 0xFF,
1021 CRC_FIX_CLK
| SD30_VAR_CLK0
| SAMPLE_VAR_CLK1
);
1022 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
, CLK_LOW_FREQ
, 0);
1023 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_PUSH_POINT_CTL
,
1024 SD20_TX_SEL_MASK
, SD20_TX_14_AHEAD
);
1025 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_SAMPLE_POINT_CTL
,
1026 SD20_RX_SEL_MASK
, SD20_RX_14_DELAY
);
1030 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
,
1031 SD_CFG1
, 0x0C, SD_20_MODE
);
1032 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
,
1033 CLK_LOW_FREQ
, CLK_LOW_FREQ
);
1034 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_CLK_SOURCE
, 0xFF,
1035 CRC_FIX_CLK
| SD30_VAR_CLK0
| SAMPLE_VAR_CLK1
);
1036 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
, CLK_LOW_FREQ
, 0);
1037 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
,
1038 SD_PUSH_POINT_CTL
, 0xFF, 0);
1039 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_SAMPLE_POINT_CTL
,
1040 SD20_RX_SEL_MASK
, SD20_RX_POS_EDGE
);
1044 err
= rtsx_pci_send_cmd(pcr
, 100);
1049 static void sdmmc_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
1051 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
1052 struct rtsx_pcr
*pcr
= host
->pcr
;
1057 if (rtsx_pci_card_exclusive_check(host
->pcr
, RTSX_SD_CARD
))
1060 mutex_lock(&pcr
->pcr_mutex
);
1062 rtsx_pci_start_run(pcr
);
1064 sd_set_bus_width(host
, ios
->bus_width
);
1065 sd_set_power_mode(host
, ios
->power_mode
);
1066 sd_set_timing(host
, ios
->timing
);
1068 host
->vpclk
= false;
1069 host
->double_clk
= true;
1071 switch (ios
->timing
) {
1072 case MMC_TIMING_UHS_SDR104
:
1073 case MMC_TIMING_UHS_SDR50
:
1074 host
->ssc_depth
= RTSX_SSC_DEPTH_2M
;
1076 host
->double_clk
= false;
1078 case MMC_TIMING_MMC_DDR52
:
1079 case MMC_TIMING_UHS_DDR50
:
1080 case MMC_TIMING_UHS_SDR25
:
1081 host
->ssc_depth
= RTSX_SSC_DEPTH_1M
;
1084 host
->ssc_depth
= RTSX_SSC_DEPTH_500K
;
1088 host
->initial_mode
= (ios
->clock
<= 1000000) ? true : false;
1090 host
->clock
= ios
->clock
;
1091 rtsx_pci_switch_clock(pcr
, ios
->clock
, host
->ssc_depth
,
1092 host
->initial_mode
, host
->double_clk
, host
->vpclk
);
1094 mutex_unlock(&pcr
->pcr_mutex
);
1097 static int sdmmc_get_ro(struct mmc_host
*mmc
)
1099 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
1100 struct rtsx_pcr
*pcr
= host
->pcr
;
1107 mutex_lock(&pcr
->pcr_mutex
);
1109 rtsx_pci_start_run(pcr
);
1111 /* Check SD mechanical write-protect switch */
1112 val
= rtsx_pci_readl(pcr
, RTSX_BIPR
);
1113 dev_dbg(sdmmc_dev(host
), "%s: RTSX_BIPR = 0x%08x\n", __func__
, val
);
1114 if (val
& SD_WRITE_PROTECT
)
1117 mutex_unlock(&pcr
->pcr_mutex
);
1122 static int sdmmc_get_cd(struct mmc_host
*mmc
)
1124 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
1125 struct rtsx_pcr
*pcr
= host
->pcr
;
1132 mutex_lock(&pcr
->pcr_mutex
);
1134 rtsx_pci_start_run(pcr
);
1136 /* Check SD card detect */
1137 val
= rtsx_pci_card_exist(pcr
);
1138 dev_dbg(sdmmc_dev(host
), "%s: RTSX_BIPR = 0x%08x\n", __func__
, val
);
1142 mutex_unlock(&pcr
->pcr_mutex
);
1147 static int sd_wait_voltage_stable_1(struct realtek_pci_sdmmc
*host
)
1149 struct rtsx_pcr
*pcr
= host
->pcr
;
1153 /* Reference to Signal Voltage Switch Sequence in SD spec.
1154 * Wait for a period of time so that the card can drive SD_CMD and
1155 * SD_DAT[3:0] to low after sending back CMD11 response.
1159 /* SD_CMD, SD_DAT[3:0] should be driven to low by card;
1160 * If either one of SD_CMD,SD_DAT[3:0] is not low,
1161 * abort the voltage switch sequence;
1163 err
= rtsx_pci_read_register(pcr
, SD_BUS_STAT
, &stat
);
1167 if (stat
& (SD_CMD_STATUS
| SD_DAT3_STATUS
| SD_DAT2_STATUS
|
1168 SD_DAT1_STATUS
| SD_DAT0_STATUS
))
1171 /* Stop toggle SD clock */
1172 err
= rtsx_pci_write_register(pcr
, SD_BUS_STAT
,
1173 0xFF, SD_CLK_FORCE_STOP
);
1180 static int sd_wait_voltage_stable_2(struct realtek_pci_sdmmc
*host
)
1182 struct rtsx_pcr
*pcr
= host
->pcr
;
1186 /* Wait 1.8V output of voltage regulator in card stable */
1189 /* Toggle SD clock again */
1190 err
= rtsx_pci_write_register(pcr
, SD_BUS_STAT
, 0xFF, SD_CLK_TOGGLE_EN
);
1194 /* Wait for a period of time so that the card can drive
1195 * SD_DAT[3:0] to high at 1.8V
1199 /* SD_CMD, SD_DAT[3:0] should be pulled high by host */
1200 err
= rtsx_pci_read_register(pcr
, SD_BUS_STAT
, &stat
);
1204 mask
= SD_CMD_STATUS
| SD_DAT3_STATUS
| SD_DAT2_STATUS
|
1205 SD_DAT1_STATUS
| SD_DAT0_STATUS
;
1206 val
= SD_CMD_STATUS
| SD_DAT3_STATUS
| SD_DAT2_STATUS
|
1207 SD_DAT1_STATUS
| SD_DAT0_STATUS
;
1208 if ((stat
& mask
) != val
) {
1209 dev_dbg(sdmmc_dev(host
),
1210 "%s: SD_BUS_STAT = 0x%x\n", __func__
, stat
);
1211 rtsx_pci_write_register(pcr
, SD_BUS_STAT
,
1212 SD_CLK_TOGGLE_EN
| SD_CLK_FORCE_STOP
, 0);
1213 rtsx_pci_write_register(pcr
, CARD_CLK_EN
, 0xFF, 0);
1220 static int sdmmc_switch_voltage(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
1222 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
1223 struct rtsx_pcr
*pcr
= host
->pcr
;
1227 dev_dbg(sdmmc_dev(host
), "%s: signal_voltage = %d\n",
1228 __func__
, ios
->signal_voltage
);
1233 err
= rtsx_pci_card_exclusive_check(host
->pcr
, RTSX_SD_CARD
);
1237 mutex_lock(&pcr
->pcr_mutex
);
1239 rtsx_pci_start_run(pcr
);
1241 if (ios
->signal_voltage
== MMC_SIGNAL_VOLTAGE_330
)
1242 voltage
= OUTPUT_3V3
;
1244 voltage
= OUTPUT_1V8
;
1246 if (voltage
== OUTPUT_1V8
) {
1247 err
= sd_wait_voltage_stable_1(host
);
1252 err
= rtsx_pci_switch_output_voltage(pcr
, voltage
);
1256 if (voltage
== OUTPUT_1V8
) {
1257 err
= sd_wait_voltage_stable_2(host
);
1263 /* Stop toggle SD clock in idle */
1264 err
= rtsx_pci_write_register(pcr
, SD_BUS_STAT
,
1265 SD_CLK_TOGGLE_EN
| SD_CLK_FORCE_STOP
, 0);
1267 mutex_unlock(&pcr
->pcr_mutex
);
1272 static int sdmmc_execute_tuning(struct mmc_host
*mmc
, u32 opcode
)
1274 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
1275 struct rtsx_pcr
*pcr
= host
->pcr
;
1281 err
= rtsx_pci_card_exclusive_check(host
->pcr
, RTSX_SD_CARD
);
1285 mutex_lock(&pcr
->pcr_mutex
);
1287 rtsx_pci_start_run(pcr
);
1289 /* Set initial TX phase */
1290 switch (mmc
->ios
.timing
) {
1291 case MMC_TIMING_UHS_SDR104
:
1292 err
= sd_change_phase(host
, SDR104_TX_PHASE(pcr
), false);
1295 case MMC_TIMING_UHS_SDR50
:
1296 err
= sd_change_phase(host
, SDR50_TX_PHASE(pcr
), false);
1299 case MMC_TIMING_UHS_DDR50
:
1300 err
= sd_change_phase(host
, DDR50_TX_PHASE(pcr
), false);
1310 /* Tuning RX phase */
1311 if ((mmc
->ios
.timing
== MMC_TIMING_UHS_SDR104
) ||
1312 (mmc
->ios
.timing
== MMC_TIMING_UHS_SDR50
))
1313 err
= sd_tuning_rx(host
, opcode
);
1314 else if (mmc
->ios
.timing
== MMC_TIMING_UHS_DDR50
)
1315 err
= sd_change_phase(host
, DDR50_RX_PHASE(pcr
), true);
1318 mutex_unlock(&pcr
->pcr_mutex
);
1323 static const struct mmc_host_ops realtek_pci_sdmmc_ops
= {
1324 .pre_req
= sdmmc_pre_req
,
1325 .post_req
= sdmmc_post_req
,
1326 .request
= sdmmc_request
,
1327 .set_ios
= sdmmc_set_ios
,
1328 .get_ro
= sdmmc_get_ro
,
1329 .get_cd
= sdmmc_get_cd
,
1330 .start_signal_voltage_switch
= sdmmc_switch_voltage
,
1331 .execute_tuning
= sdmmc_execute_tuning
,
1334 static void init_extra_caps(struct realtek_pci_sdmmc
*host
)
1336 struct mmc_host
*mmc
= host
->mmc
;
1337 struct rtsx_pcr
*pcr
= host
->pcr
;
1339 dev_dbg(sdmmc_dev(host
), "pcr->extra_caps = 0x%x\n", pcr
->extra_caps
);
1341 if (pcr
->extra_caps
& EXTRA_CAPS_SD_SDR50
)
1342 mmc
->caps
|= MMC_CAP_UHS_SDR50
;
1343 if (pcr
->extra_caps
& EXTRA_CAPS_SD_SDR104
)
1344 mmc
->caps
|= MMC_CAP_UHS_SDR104
;
1345 if (pcr
->extra_caps
& EXTRA_CAPS_SD_DDR50
)
1346 mmc
->caps
|= MMC_CAP_UHS_DDR50
;
1347 if (pcr
->extra_caps
& EXTRA_CAPS_MMC_HSDDR
)
1348 mmc
->caps
|= MMC_CAP_1_8V_DDR
;
1349 if (pcr
->extra_caps
& EXTRA_CAPS_MMC_8BIT
)
1350 mmc
->caps
|= MMC_CAP_8_BIT_DATA
;
1353 static void realtek_init_host(struct realtek_pci_sdmmc
*host
)
1355 struct mmc_host
*mmc
= host
->mmc
;
1357 mmc
->f_min
= 250000;
1358 mmc
->f_max
= 208000000;
1359 mmc
->ocr_avail
= MMC_VDD_32_33
| MMC_VDD_33_34
| MMC_VDD_165_195
;
1360 mmc
->caps
= MMC_CAP_4_BIT_DATA
| MMC_CAP_SD_HIGHSPEED
|
1361 MMC_CAP_MMC_HIGHSPEED
| MMC_CAP_BUS_WIDTH_TEST
|
1362 MMC_CAP_UHS_SDR12
| MMC_CAP_UHS_SDR25
| MMC_CAP_ERASE
;
1363 mmc
->caps2
= MMC_CAP2_NO_PRESCAN_POWERUP
| MMC_CAP2_FULL_PWR_CYCLE
;
1364 mmc
->max_current_330
= 400;
1365 mmc
->max_current_180
= 800;
1366 mmc
->ops
= &realtek_pci_sdmmc_ops
;
1368 init_extra_caps(host
);
1370 mmc
->max_segs
= 256;
1371 mmc
->max_seg_size
= 65536;
1372 mmc
->max_blk_size
= 512;
1373 mmc
->max_blk_count
= 65535;
1374 mmc
->max_req_size
= 524288;
1377 static void rtsx_pci_sdmmc_card_event(struct platform_device
*pdev
)
1379 struct realtek_pci_sdmmc
*host
= platform_get_drvdata(pdev
);
1382 mmc_detect_change(host
->mmc
, 0);
1385 static int rtsx_pci_sdmmc_drv_probe(struct platform_device
*pdev
)
1387 struct mmc_host
*mmc
;
1388 struct realtek_pci_sdmmc
*host
;
1389 struct rtsx_pcr
*pcr
;
1390 struct pcr_handle
*handle
= pdev
->dev
.platform_data
;
1399 dev_dbg(&(pdev
->dev
), ": Realtek PCI-E SDMMC controller found\n");
1401 mmc
= mmc_alloc_host(sizeof(*host
), &pdev
->dev
);
1405 host
= mmc_priv(mmc
);
1410 host
->power_state
= SDMMC_POWER_OFF
;
1411 INIT_WORK(&host
->work
, sd_request
);
1412 platform_set_drvdata(pdev
, host
);
1413 pcr
->slots
[RTSX_SD_CARD
].p_dev
= pdev
;
1414 pcr
->slots
[RTSX_SD_CARD
].card_event
= rtsx_pci_sdmmc_card_event
;
1416 mutex_init(&host
->host_mutex
);
1418 realtek_init_host(host
);
1425 static int rtsx_pci_sdmmc_drv_remove(struct platform_device
*pdev
)
1427 struct realtek_pci_sdmmc
*host
= platform_get_drvdata(pdev
);
1428 struct rtsx_pcr
*pcr
;
1429 struct mmc_host
*mmc
;
1435 pcr
->slots
[RTSX_SD_CARD
].p_dev
= NULL
;
1436 pcr
->slots
[RTSX_SD_CARD
].card_event
= NULL
;
1439 cancel_work_sync(&host
->work
);
1441 mutex_lock(&host
->host_mutex
);
1443 dev_dbg(&(pdev
->dev
),
1444 "%s: Controller removed during transfer\n",
1447 rtsx_pci_complete_unfinished_transfer(pcr
);
1449 host
->mrq
->cmd
->error
= -ENOMEDIUM
;
1450 if (host
->mrq
->stop
)
1451 host
->mrq
->stop
->error
= -ENOMEDIUM
;
1452 mmc_request_done(mmc
, host
->mrq
);
1454 mutex_unlock(&host
->host_mutex
);
1456 mmc_remove_host(mmc
);
1459 flush_work(&host
->work
);
1463 dev_dbg(&(pdev
->dev
),
1464 ": Realtek PCI-E SDMMC controller has been removed\n");
1469 static const struct platform_device_id rtsx_pci_sdmmc_ids
[] = {
1471 .name
= DRV_NAME_RTSX_PCI_SDMMC
,
1476 MODULE_DEVICE_TABLE(platform
, rtsx_pci_sdmmc_ids
);
1478 static struct platform_driver rtsx_pci_sdmmc_driver
= {
1479 .probe
= rtsx_pci_sdmmc_drv_probe
,
1480 .remove
= rtsx_pci_sdmmc_drv_remove
,
1481 .id_table
= rtsx_pci_sdmmc_ids
,
1483 .name
= DRV_NAME_RTSX_PCI_SDMMC
,
1486 module_platform_driver(rtsx_pci_sdmmc_driver
);
1488 MODULE_LICENSE("GPL");
1489 MODULE_AUTHOR("Wei WANG <wei_wang@realsil.com.cn>");
1490 MODULE_DESCRIPTION("Realtek PCI-E SD/MMC Card Host Driver");