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/mmc/host.h>
28 #include <linux/mmc/mmc.h>
29 #include <linux/mmc/sd.h>
30 #include <linux/mmc/card.h>
31 #include <linux/mfd/rtsx_pci.h>
32 #include <asm/unaligned.h>
34 /* SD Tuning Data Structure
35 * Record continuous timing phase path
37 struct timing_phase_path
{
44 struct realtek_pci_sdmmc
{
45 struct platform_device
*pdev
;
48 struct mmc_request
*mrq
;
50 struct mutex host_mutex
;
59 #define SDMMC_POWER_ON 1
60 #define SDMMC_POWER_OFF 0
63 static inline struct device
*sdmmc_dev(struct realtek_pci_sdmmc
*host
)
65 return &(host
->pdev
->dev
);
68 static inline void sd_clear_error(struct realtek_pci_sdmmc
*host
)
70 rtsx_pci_write_register(host
->pcr
, CARD_STOP
,
71 SD_STOP
| SD_CLR_ERR
, SD_STOP
| SD_CLR_ERR
);
75 static void sd_print_debug_regs(struct realtek_pci_sdmmc
*host
)
77 struct rtsx_pcr
*pcr
= host
->pcr
;
81 /* Print SD host internal registers */
82 rtsx_pci_init_cmd(pcr
);
83 for (i
= 0xFDA0; i
<= 0xFDAE; i
++)
84 rtsx_pci_add_cmd(pcr
, READ_REG_CMD
, i
, 0, 0);
85 for (i
= 0xFD52; i
<= 0xFD69; i
++)
86 rtsx_pci_add_cmd(pcr
, READ_REG_CMD
, i
, 0, 0);
87 rtsx_pci_send_cmd(pcr
, 100);
89 ptr
= rtsx_pci_get_cmd_data(pcr
);
90 for (i
= 0xFDA0; i
<= 0xFDAE; i
++)
91 dev_dbg(sdmmc_dev(host
), "0x%04X: 0x%02x\n", i
, *(ptr
++));
92 for (i
= 0xFD52; i
<= 0xFD69; i
++)
93 dev_dbg(sdmmc_dev(host
), "0x%04X: 0x%02x\n", i
, *(ptr
++));
96 #define sd_print_debug_regs(host)
99 static int sd_read_data(struct realtek_pci_sdmmc
*host
, u8
*cmd
, u16 byte_cnt
,
100 u8
*buf
, int buf_len
, int timeout
)
102 struct rtsx_pcr
*pcr
= host
->pcr
;
106 dev_dbg(sdmmc_dev(host
), "%s: SD/MMC CMD%d\n", __func__
, cmd
[0] - 0x40);
111 if ((cmd
[0] & 0x3F) == MMC_SEND_TUNING_BLOCK
)
112 trans_mode
= SD_TM_AUTO_TUNING
;
114 trans_mode
= SD_TM_NORMAL_READ
;
116 rtsx_pci_init_cmd(pcr
);
118 for (i
= 0; i
< 5; i
++)
119 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CMD0
+ i
, 0xFF, cmd
[i
]);
121 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_BYTE_CNT_L
, 0xFF, (u8
)byte_cnt
);
122 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_BYTE_CNT_H
,
123 0xFF, (u8
)(byte_cnt
>> 8));
124 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_BLOCK_CNT_L
, 0xFF, 1);
125 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_BLOCK_CNT_H
, 0xFF, 0);
127 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG2
, 0xFF,
128 SD_CALCULATE_CRC7
| SD_CHECK_CRC16
|
129 SD_NO_WAIT_BUSY_END
| SD_CHECK_CRC7
| SD_RSP_LEN_6
);
130 if (trans_mode
!= SD_TM_AUTO_TUNING
)
131 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
,
132 CARD_DATA_SOURCE
, 0x01, PINGPONG_BUFFER
);
134 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_TRANSFER
,
135 0xFF, trans_mode
| SD_TRANSFER_START
);
136 rtsx_pci_add_cmd(pcr
, CHECK_REG_CMD
, SD_TRANSFER
,
137 SD_TRANSFER_END
, SD_TRANSFER_END
);
139 err
= rtsx_pci_send_cmd(pcr
, timeout
);
141 sd_print_debug_regs(host
);
142 dev_dbg(sdmmc_dev(host
),
143 "rtsx_pci_send_cmd fail (err = %d)\n", err
);
147 if (buf
&& buf_len
) {
148 err
= rtsx_pci_read_ppbuf(pcr
, buf
, buf_len
);
150 dev_dbg(sdmmc_dev(host
),
151 "rtsx_pci_read_ppbuf fail (err = %d)\n", err
);
159 static int sd_write_data(struct realtek_pci_sdmmc
*host
, u8
*cmd
, u16 byte_cnt
,
160 u8
*buf
, int buf_len
, int timeout
)
162 struct rtsx_pcr
*pcr
= host
->pcr
;
169 if (buf
&& buf_len
) {
170 err
= rtsx_pci_write_ppbuf(pcr
, buf
, buf_len
);
172 dev_dbg(sdmmc_dev(host
),
173 "rtsx_pci_write_ppbuf fail (err = %d)\n", err
);
178 trans_mode
= cmd
? SD_TM_AUTO_WRITE_2
: SD_TM_AUTO_WRITE_3
;
179 rtsx_pci_init_cmd(pcr
);
182 dev_dbg(sdmmc_dev(host
), "%s: SD/MMC CMD %d\n", __func__
,
185 for (i
= 0; i
< 5; i
++)
186 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
,
187 SD_CMD0
+ i
, 0xFF, cmd
[i
]);
190 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_BYTE_CNT_L
, 0xFF, (u8
)byte_cnt
);
191 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_BYTE_CNT_H
,
192 0xFF, (u8
)(byte_cnt
>> 8));
193 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_BLOCK_CNT_L
, 0xFF, 1);
194 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_BLOCK_CNT_H
, 0xFF, 0);
196 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG2
, 0xFF,
197 SD_CALCULATE_CRC7
| SD_CHECK_CRC16
|
198 SD_NO_WAIT_BUSY_END
| SD_CHECK_CRC7
| SD_RSP_LEN_6
);
200 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_TRANSFER
, 0xFF,
201 trans_mode
| SD_TRANSFER_START
);
202 rtsx_pci_add_cmd(pcr
, CHECK_REG_CMD
, SD_TRANSFER
,
203 SD_TRANSFER_END
, SD_TRANSFER_END
);
205 err
= rtsx_pci_send_cmd(pcr
, timeout
);
207 sd_print_debug_regs(host
);
208 dev_dbg(sdmmc_dev(host
),
209 "rtsx_pci_send_cmd fail (err = %d)\n", err
);
216 static void sd_send_cmd_get_rsp(struct realtek_pci_sdmmc
*host
,
217 struct mmc_command
*cmd
)
219 struct rtsx_pcr
*pcr
= host
->pcr
;
220 u8 cmd_idx
= (u8
)cmd
->opcode
;
229 bool clock_toggled
= false;
231 dev_dbg(sdmmc_dev(host
), "%s: SD/MMC CMD %d, arg = 0x%08x\n",
232 __func__
, cmd_idx
, arg
);
241 switch (mmc_resp_type(cmd
)) {
243 rsp_type
= SD_RSP_TYPE_R0
;
247 rsp_type
= SD_RSP_TYPE_R1
;
249 case MMC_RSP_R1
& ~MMC_RSP_CRC
:
250 rsp_type
= SD_RSP_TYPE_R1
| SD_NO_CHECK_CRC7
;
253 rsp_type
= SD_RSP_TYPE_R1b
;
256 rsp_type
= SD_RSP_TYPE_R2
;
260 rsp_type
= SD_RSP_TYPE_R3
;
263 dev_dbg(sdmmc_dev(host
), "cmd->flag is not valid\n");
268 if (rsp_type
== SD_RSP_TYPE_R1b
)
271 if (cmd
->opcode
== SD_SWITCH_VOLTAGE
) {
272 err
= rtsx_pci_write_register(pcr
, SD_BUS_STAT
,
273 0xFF, SD_CLK_TOGGLE_EN
);
277 clock_toggled
= true;
280 rtsx_pci_init_cmd(pcr
);
282 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CMD0
, 0xFF, 0x40 | cmd_idx
);
283 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CMD1
, 0xFF, (u8
)(arg
>> 24));
284 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CMD2
, 0xFF, (u8
)(arg
>> 16));
285 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CMD3
, 0xFF, (u8
)(arg
>> 8));
286 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CMD4
, 0xFF, (u8
)arg
);
288 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG2
, 0xFF, rsp_type
);
289 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_DATA_SOURCE
,
290 0x01, PINGPONG_BUFFER
);
291 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_TRANSFER
,
292 0xFF, SD_TM_CMD_RSP
| SD_TRANSFER_START
);
293 rtsx_pci_add_cmd(pcr
, CHECK_REG_CMD
, SD_TRANSFER
,
294 SD_TRANSFER_END
| SD_STAT_IDLE
,
295 SD_TRANSFER_END
| SD_STAT_IDLE
);
297 if (rsp_type
== SD_RSP_TYPE_R2
) {
298 /* Read data from ping-pong buffer */
299 for (i
= PPBUF_BASE2
; i
< PPBUF_BASE2
+ 16; i
++)
300 rtsx_pci_add_cmd(pcr
, READ_REG_CMD
, (u16
)i
, 0, 0);
302 } else if (rsp_type
!= SD_RSP_TYPE_R0
) {
303 /* Read data from SD_CMDx registers */
304 for (i
= SD_CMD0
; i
<= SD_CMD4
; i
++)
305 rtsx_pci_add_cmd(pcr
, READ_REG_CMD
, (u16
)i
, 0, 0);
309 rtsx_pci_add_cmd(pcr
, READ_REG_CMD
, SD_STAT1
, 0, 0);
311 err
= rtsx_pci_send_cmd(pcr
, timeout
);
313 sd_print_debug_regs(host
);
314 sd_clear_error(host
);
315 dev_dbg(sdmmc_dev(host
),
316 "rtsx_pci_send_cmd error (err = %d)\n", err
);
320 if (rsp_type
== SD_RSP_TYPE_R0
) {
325 /* Eliminate returned value of CHECK_REG_CMD */
326 ptr
= rtsx_pci_get_cmd_data(pcr
) + 1;
328 /* Check (Start,Transmission) bit of Response */
329 if ((ptr
[0] & 0xC0) != 0) {
331 dev_dbg(sdmmc_dev(host
), "Invalid response bit\n");
336 if (!(rsp_type
& SD_NO_CHECK_CRC7
)) {
337 if (ptr
[stat_idx
] & SD_CRC7_ERR
) {
339 dev_dbg(sdmmc_dev(host
), "CRC7 error\n");
344 if (rsp_type
== SD_RSP_TYPE_R2
) {
345 for (i
= 0; i
< 4; i
++) {
346 cmd
->resp
[i
] = get_unaligned_be32(ptr
+ 1 + i
* 4);
347 dev_dbg(sdmmc_dev(host
), "cmd->resp[%d] = 0x%08x\n",
351 cmd
->resp
[0] = get_unaligned_be32(ptr
+ 1);
352 dev_dbg(sdmmc_dev(host
), "cmd->resp[0] = 0x%08x\n",
359 if (err
&& clock_toggled
)
360 rtsx_pci_write_register(pcr
, SD_BUS_STAT
,
361 SD_CLK_TOGGLE_EN
| SD_CLK_FORCE_STOP
, 0);
364 static int sd_rw_multi(struct realtek_pci_sdmmc
*host
, struct mmc_request
*mrq
)
366 struct rtsx_pcr
*pcr
= host
->pcr
;
367 struct mmc_host
*mmc
= host
->mmc
;
368 struct mmc_card
*card
= mmc
->card
;
369 struct mmc_data
*data
= mrq
->data
;
370 int uhs
= mmc_sd_card_uhs(card
);
371 int read
= (data
->flags
& MMC_DATA_READ
) ? 1 : 0;
374 size_t data_len
= data
->blksz
* data
->blocks
;
377 cfg2
= SD_CALCULATE_CRC7
| SD_CHECK_CRC16
|
378 SD_NO_WAIT_BUSY_END
| SD_CHECK_CRC7
| SD_RSP_LEN_0
;
379 trans_mode
= SD_TM_AUTO_READ_3
;
381 cfg2
= SD_NO_CALCULATE_CRC7
| SD_CHECK_CRC16
|
382 SD_NO_WAIT_BUSY_END
| SD_NO_CHECK_CRC7
| SD_RSP_LEN_0
;
383 trans_mode
= SD_TM_AUTO_WRITE_3
;
387 cfg2
|= SD_NO_CHECK_WAIT_CRC_TO
;
389 rtsx_pci_init_cmd(pcr
);
391 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_BYTE_CNT_L
, 0xFF, 0x00);
392 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_BYTE_CNT_H
, 0xFF, 0x02);
393 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_BLOCK_CNT_L
,
394 0xFF, (u8
)data
->blocks
);
395 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_BLOCK_CNT_H
,
396 0xFF, (u8
)(data
->blocks
>> 8));
398 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, IRQSTAT0
,
399 DMA_DONE_INT
, DMA_DONE_INT
);
400 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC3
,
401 0xFF, (u8
)(data_len
>> 24));
402 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC2
,
403 0xFF, (u8
)(data_len
>> 16));
404 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC1
,
405 0xFF, (u8
)(data_len
>> 8));
406 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC0
, 0xFF, (u8
)data_len
);
408 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMACTL
,
409 0x03 | DMA_PACK_SIZE_MASK
,
410 DMA_DIR_FROM_CARD
| DMA_EN
| DMA_512
);
412 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMACTL
,
413 0x03 | DMA_PACK_SIZE_MASK
,
414 DMA_DIR_TO_CARD
| DMA_EN
| DMA_512
);
417 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_DATA_SOURCE
,
420 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG2
, 0xFF, cfg2
);
421 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_TRANSFER
, 0xFF,
422 trans_mode
| SD_TRANSFER_START
);
423 rtsx_pci_add_cmd(pcr
, CHECK_REG_CMD
, SD_TRANSFER
,
424 SD_TRANSFER_END
, SD_TRANSFER_END
);
426 rtsx_pci_send_cmd_no_wait(pcr
);
428 err
= rtsx_pci_transfer_data(pcr
, data
->sg
, data
->sg_len
, read
, 10000);
430 sd_clear_error(host
);
437 static inline void sd_enable_initial_mode(struct realtek_pci_sdmmc
*host
)
439 rtsx_pci_write_register(host
->pcr
, SD_CFG1
,
440 SD_CLK_DIVIDE_MASK
, SD_CLK_DIVIDE_128
);
443 static inline void sd_disable_initial_mode(struct realtek_pci_sdmmc
*host
)
445 rtsx_pci_write_register(host
->pcr
, SD_CFG1
,
446 SD_CLK_DIVIDE_MASK
, SD_CLK_DIVIDE_0
);
449 static void sd_normal_rw(struct realtek_pci_sdmmc
*host
,
450 struct mmc_request
*mrq
)
452 struct mmc_command
*cmd
= mrq
->cmd
;
453 struct mmc_data
*data
= mrq
->data
;
456 _cmd
[0] = 0x40 | (u8
)cmd
->opcode
;
457 put_unaligned_be32(cmd
->arg
, (u32
*)(&_cmd
[1]));
459 buf
= kzalloc(data
->blksz
, GFP_NOIO
);
461 cmd
->error
= -ENOMEM
;
465 if (data
->flags
& MMC_DATA_READ
) {
466 if (host
->initial_mode
)
467 sd_disable_initial_mode(host
);
469 cmd
->error
= sd_read_data(host
, _cmd
, (u16
)data
->blksz
, buf
,
472 if (host
->initial_mode
)
473 sd_enable_initial_mode(host
);
475 sg_copy_from_buffer(data
->sg
, data
->sg_len
, buf
, data
->blksz
);
477 sg_copy_to_buffer(data
->sg
, data
->sg_len
, buf
, data
->blksz
);
479 cmd
->error
= sd_write_data(host
, _cmd
, (u16
)data
->blksz
, buf
,
486 static int sd_change_phase(struct realtek_pci_sdmmc
*host
,
487 u8 sample_point
, bool rx
)
489 struct rtsx_pcr
*pcr
= host
->pcr
;
492 dev_dbg(sdmmc_dev(host
), "%s(%s): sample_point = %d\n",
493 __func__
, rx
? "RX" : "TX", sample_point
);
495 rtsx_pci_init_cmd(pcr
);
497 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
, CHANGE_CLK
, CHANGE_CLK
);
499 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
,
500 SD_VPRX_CTL
, 0x1F, sample_point
);
502 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
,
503 SD_VPTX_CTL
, 0x1F, sample_point
);
504 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_VPCLK0_CTL
, PHASE_NOT_RESET
, 0);
505 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_VPCLK0_CTL
,
506 PHASE_NOT_RESET
, PHASE_NOT_RESET
);
507 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
, CHANGE_CLK
, 0);
508 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG1
, SD_ASYNC_FIFO_NOT_RST
, 0);
510 err
= rtsx_pci_send_cmd(pcr
, 100);
517 static u8
sd_search_final_phase(struct realtek_pci_sdmmc
*host
, u32 phase_map
)
519 struct timing_phase_path path
[MAX_PHASE
+ 1];
520 int i
, j
, cont_path_cnt
;
521 int new_block
, max_len
, final_path_idx
;
522 u8 final_phase
= 0xFF;
524 /* Parse phase_map, take it as a bit-ring */
528 for (i
= 0; i
< MAX_PHASE
+ 1; i
++) {
529 if (phase_map
& (1 << i
)) {
541 /* Calculate path length and middle point */
542 int idx
= cont_path_cnt
- 1;
544 path
[idx
].end
- path
[idx
].start
+ 1;
546 path
[idx
].start
+ path
[idx
].len
/ 2;
551 if (cont_path_cnt
== 0) {
552 dev_dbg(sdmmc_dev(host
), "No continuous phase path\n");
555 /* Calculate last continuous path length and middle point */
556 int idx
= cont_path_cnt
- 1;
557 path
[idx
].len
= path
[idx
].end
- path
[idx
].start
+ 1;
558 path
[idx
].mid
= path
[idx
].start
+ path
[idx
].len
/ 2;
561 /* Connect the first and last continuous paths if they are adjacent */
562 if (!path
[0].start
&& (path
[cont_path_cnt
- 1].end
== MAX_PHASE
)) {
563 /* Using negative index */
564 path
[0].start
= path
[cont_path_cnt
- 1].start
- MAX_PHASE
- 1;
565 path
[0].len
+= path
[cont_path_cnt
- 1].len
;
566 path
[0].mid
= path
[0].start
+ path
[0].len
/ 2;
567 /* Convert negative middle point index to positive one */
569 path
[0].mid
+= MAX_PHASE
+ 1;
573 /* Choose the longest continuous phase path */
577 for (i
= 0; i
< cont_path_cnt
; i
++) {
578 if (path
[i
].len
> max_len
) {
579 max_len
= path
[i
].len
;
580 final_phase
= (u8
)path
[i
].mid
;
584 dev_dbg(sdmmc_dev(host
), "path[%d].start = %d\n",
586 dev_dbg(sdmmc_dev(host
), "path[%d].end = %d\n",
588 dev_dbg(sdmmc_dev(host
), "path[%d].len = %d\n",
590 dev_dbg(sdmmc_dev(host
), "path[%d].mid = %d\n",
595 dev_dbg(sdmmc_dev(host
), "Final chosen phase: %d\n", final_phase
);
599 static void sd_wait_data_idle(struct realtek_pci_sdmmc
*host
)
604 for (i
= 0; i
< 100; i
++) {
605 err
= rtsx_pci_read_register(host
->pcr
, SD_DATA_STATE
, &val
);
606 if (val
& SD_DATA_IDLE
)
613 static int sd_tuning_rx_cmd(struct realtek_pci_sdmmc
*host
,
614 u8 opcode
, u8 sample_point
)
619 err
= sd_change_phase(host
, sample_point
, true);
623 cmd
[0] = 0x40 | opcode
;
624 err
= sd_read_data(host
, cmd
, 0x40, NULL
, 0, 100);
626 /* Wait till SD DATA IDLE */
627 sd_wait_data_idle(host
);
628 sd_clear_error(host
);
635 static int sd_tuning_phase(struct realtek_pci_sdmmc
*host
,
636 u8 opcode
, u32
*phase_map
)
639 u32 raw_phase_map
= 0;
641 for (i
= MAX_PHASE
; i
>= 0; i
--) {
642 err
= sd_tuning_rx_cmd(host
, opcode
, (u8
)i
);
644 raw_phase_map
|= 1 << i
;
648 *phase_map
= raw_phase_map
;
653 static int sd_tuning_rx(struct realtek_pci_sdmmc
*host
, u8 opcode
)
656 u32 raw_phase_map
[RX_TUNING_CNT
] = {0}, phase_map
;
659 for (i
= 0; i
< RX_TUNING_CNT
; i
++) {
660 err
= sd_tuning_phase(host
, opcode
, &(raw_phase_map
[i
]));
664 if (raw_phase_map
[i
] == 0)
668 phase_map
= 0xFFFFFFFF;
669 for (i
= 0; i
< RX_TUNING_CNT
; i
++) {
670 dev_dbg(sdmmc_dev(host
), "RX raw_phase_map[%d] = 0x%08x\n",
671 i
, raw_phase_map
[i
]);
672 phase_map
&= raw_phase_map
[i
];
674 dev_dbg(sdmmc_dev(host
), "RX phase_map = 0x%08x\n", phase_map
);
677 final_phase
= sd_search_final_phase(host
, phase_map
);
678 if (final_phase
== 0xFF)
681 err
= sd_change_phase(host
, final_phase
, true);
691 static void sdmmc_request(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
693 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
694 struct rtsx_pcr
*pcr
= host
->pcr
;
695 struct mmc_command
*cmd
= mrq
->cmd
;
696 struct mmc_data
*data
= mrq
->data
;
697 unsigned int data_size
= 0;
701 cmd
->error
= -ENOMEDIUM
;
705 err
= rtsx_pci_card_exclusive_check(host
->pcr
, RTSX_SD_CARD
);
711 mutex_lock(&pcr
->pcr_mutex
);
713 rtsx_pci_start_run(pcr
);
715 rtsx_pci_switch_clock(pcr
, host
->clock
, host
->ssc_depth
,
716 host
->initial_mode
, host
->double_clk
, host
->vpclk
);
717 rtsx_pci_write_register(pcr
, CARD_SELECT
, 0x07, SD_MOD_SEL
);
718 rtsx_pci_write_register(pcr
, CARD_SHARE_MODE
,
719 CARD_SHARE_MASK
, CARD_SHARE_48_SD
);
721 mutex_lock(&host
->host_mutex
);
723 mutex_unlock(&host
->host_mutex
);
726 data_size
= data
->blocks
* data
->blksz
;
728 if (!data_size
|| mmc_op_multi(cmd
->opcode
) ||
729 (cmd
->opcode
== MMC_READ_SINGLE_BLOCK
) ||
730 (cmd
->opcode
== MMC_WRITE_BLOCK
)) {
731 sd_send_cmd_get_rsp(host
, cmd
);
733 if (!cmd
->error
&& data_size
) {
734 sd_rw_multi(host
, mrq
);
736 if (mmc_op_multi(cmd
->opcode
) && mrq
->stop
)
737 sd_send_cmd_get_rsp(host
, mrq
->stop
);
740 sd_normal_rw(host
, mrq
);
744 if (cmd
->error
|| data
->error
)
745 data
->bytes_xfered
= 0;
747 data
->bytes_xfered
= data
->blocks
* data
->blksz
;
750 mutex_unlock(&pcr
->pcr_mutex
);
754 dev_dbg(sdmmc_dev(host
), "cmd->error = %d\n", cmd
->error
);
756 mutex_lock(&host
->host_mutex
);
758 mutex_unlock(&host
->host_mutex
);
760 mmc_request_done(mmc
, mrq
);
763 static int sd_set_bus_width(struct realtek_pci_sdmmc
*host
,
764 unsigned char bus_width
)
768 [MMC_BUS_WIDTH_1
] = SD_BUS_WIDTH_1BIT
,
769 [MMC_BUS_WIDTH_4
] = SD_BUS_WIDTH_4BIT
,
770 [MMC_BUS_WIDTH_8
] = SD_BUS_WIDTH_8BIT
,
773 if (bus_width
<= MMC_BUS_WIDTH_8
)
774 err
= rtsx_pci_write_register(host
->pcr
, SD_CFG1
,
775 0x03, width
[bus_width
]);
780 static int sd_power_on(struct realtek_pci_sdmmc
*host
)
782 struct rtsx_pcr
*pcr
= host
->pcr
;
785 if (host
->power_state
== SDMMC_POWER_ON
)
788 rtsx_pci_init_cmd(pcr
);
789 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_SELECT
, 0x07, SD_MOD_SEL
);
790 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_SHARE_MODE
,
791 CARD_SHARE_MASK
, CARD_SHARE_48_SD
);
792 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_CLK_EN
,
793 SD_CLK_EN
, SD_CLK_EN
);
794 err
= rtsx_pci_send_cmd(pcr
, 100);
798 err
= rtsx_pci_card_pull_ctl_enable(pcr
, RTSX_SD_CARD
);
802 err
= rtsx_pci_card_power_on(pcr
, RTSX_SD_CARD
);
806 err
= rtsx_pci_write_register(pcr
, CARD_OE
, SD_OUTPUT_EN
, SD_OUTPUT_EN
);
810 host
->power_state
= SDMMC_POWER_ON
;
814 static int sd_power_off(struct realtek_pci_sdmmc
*host
)
816 struct rtsx_pcr
*pcr
= host
->pcr
;
819 host
->power_state
= SDMMC_POWER_OFF
;
821 rtsx_pci_init_cmd(pcr
);
823 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_CLK_EN
, SD_CLK_EN
, 0);
824 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_OE
, SD_OUTPUT_EN
, 0);
826 err
= rtsx_pci_send_cmd(pcr
, 100);
830 err
= rtsx_pci_card_power_off(pcr
, RTSX_SD_CARD
);
834 return rtsx_pci_card_pull_ctl_disable(pcr
, RTSX_SD_CARD
);
837 static int sd_set_power_mode(struct realtek_pci_sdmmc
*host
,
838 unsigned char power_mode
)
842 if (power_mode
== MMC_POWER_OFF
)
843 err
= sd_power_off(host
);
845 err
= sd_power_on(host
);
850 static int sd_set_timing(struct realtek_pci_sdmmc
*host
, unsigned char timing
)
852 struct rtsx_pcr
*pcr
= host
->pcr
;
855 rtsx_pci_init_cmd(pcr
);
858 case MMC_TIMING_UHS_SDR104
:
859 case MMC_TIMING_UHS_SDR50
:
860 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG1
,
861 0x0C | SD_ASYNC_FIFO_NOT_RST
,
862 SD_30_MODE
| SD_ASYNC_FIFO_NOT_RST
);
863 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
,
864 CLK_LOW_FREQ
, CLK_LOW_FREQ
);
865 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_CLK_SOURCE
, 0xFF,
866 CRC_VAR_CLK0
| SD30_FIX_CLK
| SAMPLE_VAR_CLK1
);
867 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
, CLK_LOW_FREQ
, 0);
870 case MMC_TIMING_UHS_DDR50
:
871 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG1
,
872 0x0C | SD_ASYNC_FIFO_NOT_RST
,
873 SD_DDR_MODE
| SD_ASYNC_FIFO_NOT_RST
);
874 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
,
875 CLK_LOW_FREQ
, CLK_LOW_FREQ
);
876 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_CLK_SOURCE
, 0xFF,
877 CRC_VAR_CLK0
| SD30_FIX_CLK
| SAMPLE_VAR_CLK1
);
878 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
, CLK_LOW_FREQ
, 0);
879 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_PUSH_POINT_CTL
,
880 DDR_VAR_TX_CMD_DAT
, DDR_VAR_TX_CMD_DAT
);
881 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_SAMPLE_POINT_CTL
,
882 DDR_VAR_RX_DAT
| DDR_VAR_RX_CMD
,
883 DDR_VAR_RX_DAT
| DDR_VAR_RX_CMD
);
886 case MMC_TIMING_MMC_HS
:
887 case MMC_TIMING_SD_HS
:
888 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG1
,
890 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
,
891 CLK_LOW_FREQ
, CLK_LOW_FREQ
);
892 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_CLK_SOURCE
, 0xFF,
893 CRC_FIX_CLK
| SD30_VAR_CLK0
| SAMPLE_VAR_CLK1
);
894 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
, CLK_LOW_FREQ
, 0);
895 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_PUSH_POINT_CTL
,
896 SD20_TX_SEL_MASK
, SD20_TX_14_AHEAD
);
897 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_SAMPLE_POINT_CTL
,
898 SD20_RX_SEL_MASK
, SD20_RX_14_DELAY
);
902 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
,
903 SD_CFG1
, 0x0C, SD_20_MODE
);
904 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
,
905 CLK_LOW_FREQ
, CLK_LOW_FREQ
);
906 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_CLK_SOURCE
, 0xFF,
907 CRC_FIX_CLK
| SD30_VAR_CLK0
| SAMPLE_VAR_CLK1
);
908 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
, CLK_LOW_FREQ
, 0);
909 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
,
910 SD_PUSH_POINT_CTL
, 0xFF, 0);
911 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_SAMPLE_POINT_CTL
,
912 SD20_RX_SEL_MASK
, SD20_RX_POS_EDGE
);
916 err
= rtsx_pci_send_cmd(pcr
, 100);
921 static void sdmmc_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
923 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
924 struct rtsx_pcr
*pcr
= host
->pcr
;
929 if (rtsx_pci_card_exclusive_check(host
->pcr
, RTSX_SD_CARD
))
932 mutex_lock(&pcr
->pcr_mutex
);
934 rtsx_pci_start_run(pcr
);
936 sd_set_bus_width(host
, ios
->bus_width
);
937 sd_set_power_mode(host
, ios
->power_mode
);
938 sd_set_timing(host
, ios
->timing
);
941 host
->double_clk
= true;
943 switch (ios
->timing
) {
944 case MMC_TIMING_UHS_SDR104
:
945 case MMC_TIMING_UHS_SDR50
:
946 host
->ssc_depth
= RTSX_SSC_DEPTH_2M
;
948 host
->double_clk
= false;
950 case MMC_TIMING_UHS_DDR50
:
951 case MMC_TIMING_UHS_SDR25
:
952 host
->ssc_depth
= RTSX_SSC_DEPTH_1M
;
955 host
->ssc_depth
= RTSX_SSC_DEPTH_500K
;
959 host
->initial_mode
= (ios
->clock
<= 1000000) ? true : false;
961 host
->clock
= ios
->clock
;
962 rtsx_pci_switch_clock(pcr
, ios
->clock
, host
->ssc_depth
,
963 host
->initial_mode
, host
->double_clk
, host
->vpclk
);
965 mutex_unlock(&pcr
->pcr_mutex
);
968 static int sdmmc_get_ro(struct mmc_host
*mmc
)
970 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
971 struct rtsx_pcr
*pcr
= host
->pcr
;
978 mutex_lock(&pcr
->pcr_mutex
);
980 rtsx_pci_start_run(pcr
);
982 /* Check SD mechanical write-protect switch */
983 val
= rtsx_pci_readl(pcr
, RTSX_BIPR
);
984 dev_dbg(sdmmc_dev(host
), "%s: RTSX_BIPR = 0x%08x\n", __func__
, val
);
985 if (val
& SD_WRITE_PROTECT
)
988 mutex_unlock(&pcr
->pcr_mutex
);
993 static int sdmmc_get_cd(struct mmc_host
*mmc
)
995 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
996 struct rtsx_pcr
*pcr
= host
->pcr
;
1003 mutex_lock(&pcr
->pcr_mutex
);
1005 rtsx_pci_start_run(pcr
);
1007 /* Check SD card detect */
1008 val
= rtsx_pci_card_exist(pcr
);
1009 dev_dbg(sdmmc_dev(host
), "%s: RTSX_BIPR = 0x%08x\n", __func__
, val
);
1013 mutex_unlock(&pcr
->pcr_mutex
);
1018 static int sd_wait_voltage_stable_1(struct realtek_pci_sdmmc
*host
)
1020 struct rtsx_pcr
*pcr
= host
->pcr
;
1024 /* Reference to Signal Voltage Switch Sequence in SD spec.
1025 * Wait for a period of time so that the card can drive SD_CMD and
1026 * SD_DAT[3:0] to low after sending back CMD11 response.
1030 /* SD_CMD, SD_DAT[3:0] should be driven to low by card;
1031 * If either one of SD_CMD,SD_DAT[3:0] is not low,
1032 * abort the voltage switch sequence;
1034 err
= rtsx_pci_read_register(pcr
, SD_BUS_STAT
, &stat
);
1038 if (stat
& (SD_CMD_STATUS
| SD_DAT3_STATUS
| SD_DAT2_STATUS
|
1039 SD_DAT1_STATUS
| SD_DAT0_STATUS
))
1042 /* Stop toggle SD clock */
1043 err
= rtsx_pci_write_register(pcr
, SD_BUS_STAT
,
1044 0xFF, SD_CLK_FORCE_STOP
);
1051 static int sd_wait_voltage_stable_2(struct realtek_pci_sdmmc
*host
)
1053 struct rtsx_pcr
*pcr
= host
->pcr
;
1057 /* Wait 1.8V output of voltage regulator in card stable */
1060 /* Toggle SD clock again */
1061 err
= rtsx_pci_write_register(pcr
, SD_BUS_STAT
, 0xFF, SD_CLK_TOGGLE_EN
);
1065 /* Wait for a period of time so that the card can drive
1066 * SD_DAT[3:0] to high at 1.8V
1070 /* SD_CMD, SD_DAT[3:0] should be pulled high by host */
1071 err
= rtsx_pci_read_register(pcr
, SD_BUS_STAT
, &stat
);
1075 mask
= SD_CMD_STATUS
| SD_DAT3_STATUS
| SD_DAT2_STATUS
|
1076 SD_DAT1_STATUS
| SD_DAT0_STATUS
;
1077 val
= SD_CMD_STATUS
| SD_DAT3_STATUS
| SD_DAT2_STATUS
|
1078 SD_DAT1_STATUS
| SD_DAT0_STATUS
;
1079 if ((stat
& mask
) != val
) {
1080 dev_dbg(sdmmc_dev(host
),
1081 "%s: SD_BUS_STAT = 0x%x\n", __func__
, stat
);
1082 rtsx_pci_write_register(pcr
, SD_BUS_STAT
,
1083 SD_CLK_TOGGLE_EN
| SD_CLK_FORCE_STOP
, 0);
1084 rtsx_pci_write_register(pcr
, CARD_CLK_EN
, 0xFF, 0);
1091 static int sdmmc_switch_voltage(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
1093 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
1094 struct rtsx_pcr
*pcr
= host
->pcr
;
1098 dev_dbg(sdmmc_dev(host
), "%s: signal_voltage = %d\n",
1099 __func__
, ios
->signal_voltage
);
1104 err
= rtsx_pci_card_exclusive_check(host
->pcr
, RTSX_SD_CARD
);
1108 mutex_lock(&pcr
->pcr_mutex
);
1110 rtsx_pci_start_run(pcr
);
1112 if (ios
->signal_voltage
== MMC_SIGNAL_VOLTAGE_330
)
1113 voltage
= OUTPUT_3V3
;
1115 voltage
= OUTPUT_1V8
;
1117 if (voltage
== OUTPUT_1V8
) {
1118 err
= sd_wait_voltage_stable_1(host
);
1123 err
= rtsx_pci_switch_output_voltage(pcr
, voltage
);
1127 if (voltage
== OUTPUT_1V8
) {
1128 err
= sd_wait_voltage_stable_2(host
);
1134 /* Stop toggle SD clock in idle */
1135 err
= rtsx_pci_write_register(pcr
, SD_BUS_STAT
,
1136 SD_CLK_TOGGLE_EN
| SD_CLK_FORCE_STOP
, 0);
1138 mutex_unlock(&pcr
->pcr_mutex
);
1143 static int sdmmc_execute_tuning(struct mmc_host
*mmc
, u32 opcode
)
1145 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
1146 struct rtsx_pcr
*pcr
= host
->pcr
;
1152 err
= rtsx_pci_card_exclusive_check(host
->pcr
, RTSX_SD_CARD
);
1156 mutex_lock(&pcr
->pcr_mutex
);
1158 rtsx_pci_start_run(pcr
);
1160 /* Set initial TX phase */
1161 switch (mmc
->ios
.timing
) {
1162 case MMC_TIMING_UHS_SDR104
:
1163 err
= sd_change_phase(host
, SDR104_TX_PHASE(pcr
), false);
1166 case MMC_TIMING_UHS_SDR50
:
1167 err
= sd_change_phase(host
, SDR50_TX_PHASE(pcr
), false);
1170 case MMC_TIMING_UHS_DDR50
:
1171 err
= sd_change_phase(host
, DDR50_TX_PHASE(pcr
), false);
1181 /* Tuning RX phase */
1182 if ((mmc
->ios
.timing
== MMC_TIMING_UHS_SDR104
) ||
1183 (mmc
->ios
.timing
== MMC_TIMING_UHS_SDR50
))
1184 err
= sd_tuning_rx(host
, opcode
);
1185 else if (mmc
->ios
.timing
== MMC_TIMING_UHS_DDR50
)
1186 err
= sd_change_phase(host
, DDR50_RX_PHASE(pcr
), true);
1189 mutex_unlock(&pcr
->pcr_mutex
);
1194 static const struct mmc_host_ops realtek_pci_sdmmc_ops
= {
1195 .request
= sdmmc_request
,
1196 .set_ios
= sdmmc_set_ios
,
1197 .get_ro
= sdmmc_get_ro
,
1198 .get_cd
= sdmmc_get_cd
,
1199 .start_signal_voltage_switch
= sdmmc_switch_voltage
,
1200 .execute_tuning
= sdmmc_execute_tuning
,
1204 static int rtsx_pci_sdmmc_suspend(struct platform_device
*pdev
,
1207 struct realtek_pci_sdmmc
*host
= platform_get_drvdata(pdev
);
1208 struct mmc_host
*mmc
= host
->mmc
;
1211 dev_dbg(sdmmc_dev(host
), "--> %s\n", __func__
);
1213 err
= mmc_suspend_host(mmc
);
1220 static int rtsx_pci_sdmmc_resume(struct platform_device
*pdev
)
1222 struct realtek_pci_sdmmc
*host
= platform_get_drvdata(pdev
);
1223 struct mmc_host
*mmc
= host
->mmc
;
1225 dev_dbg(sdmmc_dev(host
), "--> %s\n", __func__
);
1227 return mmc_resume_host(mmc
);
1229 #else /* CONFIG_PM */
1230 #define rtsx_pci_sdmmc_suspend NULL
1231 #define rtsx_pci_sdmmc_resume NULL
1232 #endif /* CONFIG_PM */
1234 static void init_extra_caps(struct realtek_pci_sdmmc
*host
)
1236 struct mmc_host
*mmc
= host
->mmc
;
1237 struct rtsx_pcr
*pcr
= host
->pcr
;
1239 dev_dbg(sdmmc_dev(host
), "pcr->extra_caps = 0x%x\n", pcr
->extra_caps
);
1241 if (pcr
->extra_caps
& EXTRA_CAPS_SD_SDR50
)
1242 mmc
->caps
|= MMC_CAP_UHS_SDR50
;
1243 if (pcr
->extra_caps
& EXTRA_CAPS_SD_SDR104
)
1244 mmc
->caps
|= MMC_CAP_UHS_SDR104
;
1245 if (pcr
->extra_caps
& EXTRA_CAPS_SD_DDR50
)
1246 mmc
->caps
|= MMC_CAP_UHS_DDR50
;
1247 if (pcr
->extra_caps
& EXTRA_CAPS_MMC_HSDDR
)
1248 mmc
->caps
|= MMC_CAP_1_8V_DDR
;
1249 if (pcr
->extra_caps
& EXTRA_CAPS_MMC_8BIT
)
1250 mmc
->caps
|= MMC_CAP_8_BIT_DATA
;
1253 static void realtek_init_host(struct realtek_pci_sdmmc
*host
)
1255 struct mmc_host
*mmc
= host
->mmc
;
1257 mmc
->f_min
= 250000;
1258 mmc
->f_max
= 208000000;
1259 mmc
->ocr_avail
= MMC_VDD_32_33
| MMC_VDD_33_34
| MMC_VDD_165_195
;
1260 mmc
->caps
= MMC_CAP_4_BIT_DATA
| MMC_CAP_SD_HIGHSPEED
|
1261 MMC_CAP_MMC_HIGHSPEED
| MMC_CAP_BUS_WIDTH_TEST
|
1262 MMC_CAP_UHS_SDR12
| MMC_CAP_UHS_SDR25
;
1263 mmc
->max_current_330
= 400;
1264 mmc
->max_current_180
= 800;
1265 mmc
->ops
= &realtek_pci_sdmmc_ops
;
1267 init_extra_caps(host
);
1269 mmc
->max_segs
= 256;
1270 mmc
->max_seg_size
= 65536;
1271 mmc
->max_blk_size
= 512;
1272 mmc
->max_blk_count
= 65535;
1273 mmc
->max_req_size
= 524288;
1276 static void rtsx_pci_sdmmc_card_event(struct platform_device
*pdev
)
1278 struct realtek_pci_sdmmc
*host
= platform_get_drvdata(pdev
);
1280 mmc_detect_change(host
->mmc
, 0);
1283 static int rtsx_pci_sdmmc_drv_probe(struct platform_device
*pdev
)
1285 struct mmc_host
*mmc
;
1286 struct realtek_pci_sdmmc
*host
;
1287 struct rtsx_pcr
*pcr
;
1288 struct pcr_handle
*handle
= pdev
->dev
.platform_data
;
1297 dev_dbg(&(pdev
->dev
), ": Realtek PCI-E SDMMC controller found\n");
1299 mmc
= mmc_alloc_host(sizeof(*host
), &pdev
->dev
);
1303 host
= mmc_priv(mmc
);
1307 host
->power_state
= SDMMC_POWER_OFF
;
1308 platform_set_drvdata(pdev
, host
);
1309 pcr
->slots
[RTSX_SD_CARD
].p_dev
= pdev
;
1310 pcr
->slots
[RTSX_SD_CARD
].card_event
= rtsx_pci_sdmmc_card_event
;
1312 mutex_init(&host
->host_mutex
);
1314 realtek_init_host(host
);
1321 static int rtsx_pci_sdmmc_drv_remove(struct platform_device
*pdev
)
1323 struct realtek_pci_sdmmc
*host
= platform_get_drvdata(pdev
);
1324 struct rtsx_pcr
*pcr
;
1325 struct mmc_host
*mmc
;
1331 pcr
->slots
[RTSX_SD_CARD
].p_dev
= NULL
;
1332 pcr
->slots
[RTSX_SD_CARD
].card_event
= NULL
;
1336 mutex_lock(&host
->host_mutex
);
1338 dev_dbg(&(pdev
->dev
),
1339 "%s: Controller removed during transfer\n",
1342 rtsx_pci_complete_unfinished_transfer(pcr
);
1344 host
->mrq
->cmd
->error
= -ENOMEDIUM
;
1345 if (host
->mrq
->stop
)
1346 host
->mrq
->stop
->error
= -ENOMEDIUM
;
1347 mmc_request_done(mmc
, host
->mrq
);
1349 mutex_unlock(&host
->host_mutex
);
1351 mmc_remove_host(mmc
);
1354 dev_dbg(&(pdev
->dev
),
1355 ": Realtek PCI-E SDMMC controller has been removed\n");
1360 static struct platform_device_id rtsx_pci_sdmmc_ids
[] = {
1362 .name
= DRV_NAME_RTSX_PCI_SDMMC
,
1367 MODULE_DEVICE_TABLE(platform
, rtsx_pci_sdmmc_ids
);
1369 static struct platform_driver rtsx_pci_sdmmc_driver
= {
1370 .probe
= rtsx_pci_sdmmc_drv_probe
,
1371 .remove
= rtsx_pci_sdmmc_drv_remove
,
1372 .id_table
= rtsx_pci_sdmmc_ids
,
1373 .suspend
= rtsx_pci_sdmmc_suspend
,
1374 .resume
= rtsx_pci_sdmmc_resume
,
1376 .owner
= THIS_MODULE
,
1377 .name
= DRV_NAME_RTSX_PCI_SDMMC
,
1380 module_platform_driver(rtsx_pci_sdmmc_driver
);
1382 MODULE_LICENSE("GPL");
1383 MODULE_AUTHOR("Wei WANG <wei_wang@realsil.com.cn>");
1384 MODULE_DESCRIPTION("Realtek PCI-E SD/MMC Card Host Driver");