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
) {
346 * The controller offloads the last byte {CRC-7, end bit 1'b1}
347 * of response type R2. Assign dummy CRC, 0, and end bit to the
348 * byte(ptr[16], goes into the LSB of resp[3] later).
352 for (i
= 0; i
< 4; i
++) {
353 cmd
->resp
[i
] = get_unaligned_be32(ptr
+ 1 + i
* 4);
354 dev_dbg(sdmmc_dev(host
), "cmd->resp[%d] = 0x%08x\n",
358 cmd
->resp
[0] = get_unaligned_be32(ptr
+ 1);
359 dev_dbg(sdmmc_dev(host
), "cmd->resp[0] = 0x%08x\n",
366 if (err
&& clock_toggled
)
367 rtsx_pci_write_register(pcr
, SD_BUS_STAT
,
368 SD_CLK_TOGGLE_EN
| SD_CLK_FORCE_STOP
, 0);
371 static int sd_rw_multi(struct realtek_pci_sdmmc
*host
, struct mmc_request
*mrq
)
373 struct rtsx_pcr
*pcr
= host
->pcr
;
374 struct mmc_host
*mmc
= host
->mmc
;
375 struct mmc_card
*card
= mmc
->card
;
376 struct mmc_data
*data
= mrq
->data
;
377 int uhs
= mmc_sd_card_uhs(card
);
378 int read
= (data
->flags
& MMC_DATA_READ
) ? 1 : 0;
381 size_t data_len
= data
->blksz
* data
->blocks
;
384 cfg2
= SD_CALCULATE_CRC7
| SD_CHECK_CRC16
|
385 SD_NO_WAIT_BUSY_END
| SD_CHECK_CRC7
| SD_RSP_LEN_0
;
386 trans_mode
= SD_TM_AUTO_READ_3
;
388 cfg2
= SD_NO_CALCULATE_CRC7
| SD_CHECK_CRC16
|
389 SD_NO_WAIT_BUSY_END
| SD_NO_CHECK_CRC7
| SD_RSP_LEN_0
;
390 trans_mode
= SD_TM_AUTO_WRITE_3
;
394 cfg2
|= SD_NO_CHECK_WAIT_CRC_TO
;
396 rtsx_pci_init_cmd(pcr
);
398 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_BYTE_CNT_L
, 0xFF, 0x00);
399 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_BYTE_CNT_H
, 0xFF, 0x02);
400 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_BLOCK_CNT_L
,
401 0xFF, (u8
)data
->blocks
);
402 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_BLOCK_CNT_H
,
403 0xFF, (u8
)(data
->blocks
>> 8));
405 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, IRQSTAT0
,
406 DMA_DONE_INT
, DMA_DONE_INT
);
407 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC3
,
408 0xFF, (u8
)(data_len
>> 24));
409 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC2
,
410 0xFF, (u8
)(data_len
>> 16));
411 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC1
,
412 0xFF, (u8
)(data_len
>> 8));
413 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC0
, 0xFF, (u8
)data_len
);
415 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMACTL
,
416 0x03 | DMA_PACK_SIZE_MASK
,
417 DMA_DIR_FROM_CARD
| DMA_EN
| DMA_512
);
419 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMACTL
,
420 0x03 | DMA_PACK_SIZE_MASK
,
421 DMA_DIR_TO_CARD
| DMA_EN
| DMA_512
);
424 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_DATA_SOURCE
,
427 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG2
, 0xFF, cfg2
);
428 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_TRANSFER
, 0xFF,
429 trans_mode
| SD_TRANSFER_START
);
430 rtsx_pci_add_cmd(pcr
, CHECK_REG_CMD
, SD_TRANSFER
,
431 SD_TRANSFER_END
, SD_TRANSFER_END
);
433 rtsx_pci_send_cmd_no_wait(pcr
);
435 err
= rtsx_pci_transfer_data(pcr
, data
->sg
, data
->sg_len
, read
, 10000);
437 sd_clear_error(host
);
444 static inline void sd_enable_initial_mode(struct realtek_pci_sdmmc
*host
)
446 rtsx_pci_write_register(host
->pcr
, SD_CFG1
,
447 SD_CLK_DIVIDE_MASK
, SD_CLK_DIVIDE_128
);
450 static inline void sd_disable_initial_mode(struct realtek_pci_sdmmc
*host
)
452 rtsx_pci_write_register(host
->pcr
, SD_CFG1
,
453 SD_CLK_DIVIDE_MASK
, SD_CLK_DIVIDE_0
);
456 static void sd_normal_rw(struct realtek_pci_sdmmc
*host
,
457 struct mmc_request
*mrq
)
459 struct mmc_command
*cmd
= mrq
->cmd
;
460 struct mmc_data
*data
= mrq
->data
;
463 _cmd
[0] = 0x40 | (u8
)cmd
->opcode
;
464 put_unaligned_be32(cmd
->arg
, (u32
*)(&_cmd
[1]));
466 buf
= kzalloc(data
->blksz
, GFP_NOIO
);
468 cmd
->error
= -ENOMEM
;
472 if (data
->flags
& MMC_DATA_READ
) {
473 if (host
->initial_mode
)
474 sd_disable_initial_mode(host
);
476 cmd
->error
= sd_read_data(host
, _cmd
, (u16
)data
->blksz
, buf
,
479 if (host
->initial_mode
)
480 sd_enable_initial_mode(host
);
482 sg_copy_from_buffer(data
->sg
, data
->sg_len
, buf
, data
->blksz
);
484 sg_copy_to_buffer(data
->sg
, data
->sg_len
, buf
, data
->blksz
);
486 cmd
->error
= sd_write_data(host
, _cmd
, (u16
)data
->blksz
, buf
,
493 static int sd_change_phase(struct realtek_pci_sdmmc
*host
,
494 u8 sample_point
, bool rx
)
496 struct rtsx_pcr
*pcr
= host
->pcr
;
499 dev_dbg(sdmmc_dev(host
), "%s(%s): sample_point = %d\n",
500 __func__
, rx
? "RX" : "TX", sample_point
);
502 rtsx_pci_init_cmd(pcr
);
504 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
, CHANGE_CLK
, CHANGE_CLK
);
506 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
,
507 SD_VPRX_CTL
, 0x1F, sample_point
);
509 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
,
510 SD_VPTX_CTL
, 0x1F, sample_point
);
511 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_VPCLK0_CTL
, PHASE_NOT_RESET
, 0);
512 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_VPCLK0_CTL
,
513 PHASE_NOT_RESET
, PHASE_NOT_RESET
);
514 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
, CHANGE_CLK
, 0);
515 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG1
, SD_ASYNC_FIFO_NOT_RST
, 0);
517 err
= rtsx_pci_send_cmd(pcr
, 100);
524 static u8
sd_search_final_phase(struct realtek_pci_sdmmc
*host
, u32 phase_map
)
526 struct timing_phase_path path
[MAX_PHASE
+ 1];
527 int i
, j
, cont_path_cnt
;
528 int new_block
, max_len
, final_path_idx
;
529 u8 final_phase
= 0xFF;
531 /* Parse phase_map, take it as a bit-ring */
535 for (i
= 0; i
< MAX_PHASE
+ 1; i
++) {
536 if (phase_map
& (1 << i
)) {
548 /* Calculate path length and middle point */
549 int idx
= cont_path_cnt
- 1;
551 path
[idx
].end
- path
[idx
].start
+ 1;
553 path
[idx
].start
+ path
[idx
].len
/ 2;
558 if (cont_path_cnt
== 0) {
559 dev_dbg(sdmmc_dev(host
), "No continuous phase path\n");
562 /* Calculate last continuous path length and middle point */
563 int idx
= cont_path_cnt
- 1;
564 path
[idx
].len
= path
[idx
].end
- path
[idx
].start
+ 1;
565 path
[idx
].mid
= path
[idx
].start
+ path
[idx
].len
/ 2;
568 /* Connect the first and last continuous paths if they are adjacent */
569 if (!path
[0].start
&& (path
[cont_path_cnt
- 1].end
== MAX_PHASE
)) {
570 /* Using negative index */
571 path
[0].start
= path
[cont_path_cnt
- 1].start
- MAX_PHASE
- 1;
572 path
[0].len
+= path
[cont_path_cnt
- 1].len
;
573 path
[0].mid
= path
[0].start
+ path
[0].len
/ 2;
574 /* Convert negative middle point index to positive one */
576 path
[0].mid
+= MAX_PHASE
+ 1;
580 /* Choose the longest continuous phase path */
584 for (i
= 0; i
< cont_path_cnt
; i
++) {
585 if (path
[i
].len
> max_len
) {
586 max_len
= path
[i
].len
;
587 final_phase
= (u8
)path
[i
].mid
;
591 dev_dbg(sdmmc_dev(host
), "path[%d].start = %d\n",
593 dev_dbg(sdmmc_dev(host
), "path[%d].end = %d\n",
595 dev_dbg(sdmmc_dev(host
), "path[%d].len = %d\n",
597 dev_dbg(sdmmc_dev(host
), "path[%d].mid = %d\n",
602 dev_dbg(sdmmc_dev(host
), "Final chosen phase: %d\n", final_phase
);
606 static void sd_wait_data_idle(struct realtek_pci_sdmmc
*host
)
611 for (i
= 0; i
< 100; i
++) {
612 err
= rtsx_pci_read_register(host
->pcr
, SD_DATA_STATE
, &val
);
613 if (val
& SD_DATA_IDLE
)
620 static int sd_tuning_rx_cmd(struct realtek_pci_sdmmc
*host
,
621 u8 opcode
, u8 sample_point
)
626 err
= sd_change_phase(host
, sample_point
, true);
630 cmd
[0] = 0x40 | opcode
;
631 err
= sd_read_data(host
, cmd
, 0x40, NULL
, 0, 100);
633 /* Wait till SD DATA IDLE */
634 sd_wait_data_idle(host
);
635 sd_clear_error(host
);
642 static int sd_tuning_phase(struct realtek_pci_sdmmc
*host
,
643 u8 opcode
, u32
*phase_map
)
646 u32 raw_phase_map
= 0;
648 for (i
= MAX_PHASE
; i
>= 0; i
--) {
649 err
= sd_tuning_rx_cmd(host
, opcode
, (u8
)i
);
651 raw_phase_map
|= 1 << i
;
655 *phase_map
= raw_phase_map
;
660 static int sd_tuning_rx(struct realtek_pci_sdmmc
*host
, u8 opcode
)
663 u32 raw_phase_map
[RX_TUNING_CNT
] = {0}, phase_map
;
666 for (i
= 0; i
< RX_TUNING_CNT
; i
++) {
667 err
= sd_tuning_phase(host
, opcode
, &(raw_phase_map
[i
]));
671 if (raw_phase_map
[i
] == 0)
675 phase_map
= 0xFFFFFFFF;
676 for (i
= 0; i
< RX_TUNING_CNT
; i
++) {
677 dev_dbg(sdmmc_dev(host
), "RX raw_phase_map[%d] = 0x%08x\n",
678 i
, raw_phase_map
[i
]);
679 phase_map
&= raw_phase_map
[i
];
681 dev_dbg(sdmmc_dev(host
), "RX phase_map = 0x%08x\n", phase_map
);
684 final_phase
= sd_search_final_phase(host
, phase_map
);
685 if (final_phase
== 0xFF)
688 err
= sd_change_phase(host
, final_phase
, true);
698 static void sdmmc_request(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
700 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
701 struct rtsx_pcr
*pcr
= host
->pcr
;
702 struct mmc_command
*cmd
= mrq
->cmd
;
703 struct mmc_data
*data
= mrq
->data
;
704 unsigned int data_size
= 0;
708 cmd
->error
= -ENOMEDIUM
;
712 err
= rtsx_pci_card_exclusive_check(host
->pcr
, RTSX_SD_CARD
);
718 mutex_lock(&pcr
->pcr_mutex
);
720 rtsx_pci_start_run(pcr
);
722 rtsx_pci_switch_clock(pcr
, host
->clock
, host
->ssc_depth
,
723 host
->initial_mode
, host
->double_clk
, host
->vpclk
);
724 rtsx_pci_write_register(pcr
, CARD_SELECT
, 0x07, SD_MOD_SEL
);
725 rtsx_pci_write_register(pcr
, CARD_SHARE_MODE
,
726 CARD_SHARE_MASK
, CARD_SHARE_48_SD
);
728 mutex_lock(&host
->host_mutex
);
730 mutex_unlock(&host
->host_mutex
);
733 data_size
= data
->blocks
* data
->blksz
;
735 if (!data_size
|| mmc_op_multi(cmd
->opcode
) ||
736 (cmd
->opcode
== MMC_READ_SINGLE_BLOCK
) ||
737 (cmd
->opcode
== MMC_WRITE_BLOCK
)) {
738 sd_send_cmd_get_rsp(host
, cmd
);
740 if (!cmd
->error
&& data_size
) {
741 sd_rw_multi(host
, mrq
);
743 if (mmc_op_multi(cmd
->opcode
) && mrq
->stop
)
744 sd_send_cmd_get_rsp(host
, mrq
->stop
);
747 sd_normal_rw(host
, mrq
);
751 if (cmd
->error
|| data
->error
)
752 data
->bytes_xfered
= 0;
754 data
->bytes_xfered
= data
->blocks
* data
->blksz
;
757 mutex_unlock(&pcr
->pcr_mutex
);
761 dev_dbg(sdmmc_dev(host
), "cmd->error = %d\n", cmd
->error
);
763 mutex_lock(&host
->host_mutex
);
765 mutex_unlock(&host
->host_mutex
);
767 mmc_request_done(mmc
, mrq
);
770 static int sd_set_bus_width(struct realtek_pci_sdmmc
*host
,
771 unsigned char bus_width
)
775 [MMC_BUS_WIDTH_1
] = SD_BUS_WIDTH_1BIT
,
776 [MMC_BUS_WIDTH_4
] = SD_BUS_WIDTH_4BIT
,
777 [MMC_BUS_WIDTH_8
] = SD_BUS_WIDTH_8BIT
,
780 if (bus_width
<= MMC_BUS_WIDTH_8
)
781 err
= rtsx_pci_write_register(host
->pcr
, SD_CFG1
,
782 0x03, width
[bus_width
]);
787 static int sd_power_on(struct realtek_pci_sdmmc
*host
)
789 struct rtsx_pcr
*pcr
= host
->pcr
;
792 if (host
->power_state
== SDMMC_POWER_ON
)
795 rtsx_pci_init_cmd(pcr
);
796 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_SELECT
, 0x07, SD_MOD_SEL
);
797 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_SHARE_MODE
,
798 CARD_SHARE_MASK
, CARD_SHARE_48_SD
);
799 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_CLK_EN
,
800 SD_CLK_EN
, SD_CLK_EN
);
801 err
= rtsx_pci_send_cmd(pcr
, 100);
805 err
= rtsx_pci_card_pull_ctl_enable(pcr
, RTSX_SD_CARD
);
809 err
= rtsx_pci_card_power_on(pcr
, RTSX_SD_CARD
);
813 err
= rtsx_pci_write_register(pcr
, CARD_OE
, SD_OUTPUT_EN
, SD_OUTPUT_EN
);
817 host
->power_state
= SDMMC_POWER_ON
;
821 static int sd_power_off(struct realtek_pci_sdmmc
*host
)
823 struct rtsx_pcr
*pcr
= host
->pcr
;
826 host
->power_state
= SDMMC_POWER_OFF
;
828 rtsx_pci_init_cmd(pcr
);
830 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_CLK_EN
, SD_CLK_EN
, 0);
831 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_OE
, SD_OUTPUT_EN
, 0);
833 err
= rtsx_pci_send_cmd(pcr
, 100);
837 err
= rtsx_pci_card_power_off(pcr
, RTSX_SD_CARD
);
841 return rtsx_pci_card_pull_ctl_disable(pcr
, RTSX_SD_CARD
);
844 static int sd_set_power_mode(struct realtek_pci_sdmmc
*host
,
845 unsigned char power_mode
)
849 if (power_mode
== MMC_POWER_OFF
)
850 err
= sd_power_off(host
);
852 err
= sd_power_on(host
);
857 static int sd_set_timing(struct realtek_pci_sdmmc
*host
, unsigned char timing
)
859 struct rtsx_pcr
*pcr
= host
->pcr
;
862 rtsx_pci_init_cmd(pcr
);
865 case MMC_TIMING_UHS_SDR104
:
866 case MMC_TIMING_UHS_SDR50
:
867 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG1
,
868 0x0C | SD_ASYNC_FIFO_NOT_RST
,
869 SD_30_MODE
| SD_ASYNC_FIFO_NOT_RST
);
870 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
,
871 CLK_LOW_FREQ
, CLK_LOW_FREQ
);
872 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_CLK_SOURCE
, 0xFF,
873 CRC_VAR_CLK0
| SD30_FIX_CLK
| SAMPLE_VAR_CLK1
);
874 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
, CLK_LOW_FREQ
, 0);
877 case MMC_TIMING_UHS_DDR50
:
878 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG1
,
879 0x0C | SD_ASYNC_FIFO_NOT_RST
,
880 SD_DDR_MODE
| SD_ASYNC_FIFO_NOT_RST
);
881 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
,
882 CLK_LOW_FREQ
, CLK_LOW_FREQ
);
883 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_CLK_SOURCE
, 0xFF,
884 CRC_VAR_CLK0
| SD30_FIX_CLK
| SAMPLE_VAR_CLK1
);
885 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
, CLK_LOW_FREQ
, 0);
886 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_PUSH_POINT_CTL
,
887 DDR_VAR_TX_CMD_DAT
, DDR_VAR_TX_CMD_DAT
);
888 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_SAMPLE_POINT_CTL
,
889 DDR_VAR_RX_DAT
| DDR_VAR_RX_CMD
,
890 DDR_VAR_RX_DAT
| DDR_VAR_RX_CMD
);
893 case MMC_TIMING_MMC_HS
:
894 case MMC_TIMING_SD_HS
:
895 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG1
,
897 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
,
898 CLK_LOW_FREQ
, CLK_LOW_FREQ
);
899 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_CLK_SOURCE
, 0xFF,
900 CRC_FIX_CLK
| SD30_VAR_CLK0
| SAMPLE_VAR_CLK1
);
901 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
, CLK_LOW_FREQ
, 0);
902 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_PUSH_POINT_CTL
,
903 SD20_TX_SEL_MASK
, SD20_TX_14_AHEAD
);
904 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_SAMPLE_POINT_CTL
,
905 SD20_RX_SEL_MASK
, SD20_RX_14_DELAY
);
909 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
,
910 SD_CFG1
, 0x0C, SD_20_MODE
);
911 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
,
912 CLK_LOW_FREQ
, CLK_LOW_FREQ
);
913 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_CLK_SOURCE
, 0xFF,
914 CRC_FIX_CLK
| SD30_VAR_CLK0
| SAMPLE_VAR_CLK1
);
915 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
, CLK_LOW_FREQ
, 0);
916 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
,
917 SD_PUSH_POINT_CTL
, 0xFF, 0);
918 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_SAMPLE_POINT_CTL
,
919 SD20_RX_SEL_MASK
, SD20_RX_POS_EDGE
);
923 err
= rtsx_pci_send_cmd(pcr
, 100);
928 static void sdmmc_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
930 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
931 struct rtsx_pcr
*pcr
= host
->pcr
;
936 if (rtsx_pci_card_exclusive_check(host
->pcr
, RTSX_SD_CARD
))
939 mutex_lock(&pcr
->pcr_mutex
);
941 rtsx_pci_start_run(pcr
);
943 sd_set_bus_width(host
, ios
->bus_width
);
944 sd_set_power_mode(host
, ios
->power_mode
);
945 sd_set_timing(host
, ios
->timing
);
948 host
->double_clk
= true;
950 switch (ios
->timing
) {
951 case MMC_TIMING_UHS_SDR104
:
952 case MMC_TIMING_UHS_SDR50
:
953 host
->ssc_depth
= RTSX_SSC_DEPTH_2M
;
955 host
->double_clk
= false;
957 case MMC_TIMING_UHS_DDR50
:
958 case MMC_TIMING_UHS_SDR25
:
959 host
->ssc_depth
= RTSX_SSC_DEPTH_1M
;
962 host
->ssc_depth
= RTSX_SSC_DEPTH_500K
;
966 host
->initial_mode
= (ios
->clock
<= 1000000) ? true : false;
968 host
->clock
= ios
->clock
;
969 rtsx_pci_switch_clock(pcr
, ios
->clock
, host
->ssc_depth
,
970 host
->initial_mode
, host
->double_clk
, host
->vpclk
);
972 mutex_unlock(&pcr
->pcr_mutex
);
975 static int sdmmc_get_ro(struct mmc_host
*mmc
)
977 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
978 struct rtsx_pcr
*pcr
= host
->pcr
;
985 mutex_lock(&pcr
->pcr_mutex
);
987 rtsx_pci_start_run(pcr
);
989 /* Check SD mechanical write-protect switch */
990 val
= rtsx_pci_readl(pcr
, RTSX_BIPR
);
991 dev_dbg(sdmmc_dev(host
), "%s: RTSX_BIPR = 0x%08x\n", __func__
, val
);
992 if (val
& SD_WRITE_PROTECT
)
995 mutex_unlock(&pcr
->pcr_mutex
);
1000 static int sdmmc_get_cd(struct mmc_host
*mmc
)
1002 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
1003 struct rtsx_pcr
*pcr
= host
->pcr
;
1010 mutex_lock(&pcr
->pcr_mutex
);
1012 rtsx_pci_start_run(pcr
);
1014 /* Check SD card detect */
1015 val
= rtsx_pci_card_exist(pcr
);
1016 dev_dbg(sdmmc_dev(host
), "%s: RTSX_BIPR = 0x%08x\n", __func__
, val
);
1020 mutex_unlock(&pcr
->pcr_mutex
);
1025 static int sd_wait_voltage_stable_1(struct realtek_pci_sdmmc
*host
)
1027 struct rtsx_pcr
*pcr
= host
->pcr
;
1031 /* Reference to Signal Voltage Switch Sequence in SD spec.
1032 * Wait for a period of time so that the card can drive SD_CMD and
1033 * SD_DAT[3:0] to low after sending back CMD11 response.
1037 /* SD_CMD, SD_DAT[3:0] should be driven to low by card;
1038 * If either one of SD_CMD,SD_DAT[3:0] is not low,
1039 * abort the voltage switch sequence;
1041 err
= rtsx_pci_read_register(pcr
, SD_BUS_STAT
, &stat
);
1045 if (stat
& (SD_CMD_STATUS
| SD_DAT3_STATUS
| SD_DAT2_STATUS
|
1046 SD_DAT1_STATUS
| SD_DAT0_STATUS
))
1049 /* Stop toggle SD clock */
1050 err
= rtsx_pci_write_register(pcr
, SD_BUS_STAT
,
1051 0xFF, SD_CLK_FORCE_STOP
);
1058 static int sd_wait_voltage_stable_2(struct realtek_pci_sdmmc
*host
)
1060 struct rtsx_pcr
*pcr
= host
->pcr
;
1064 /* Wait 1.8V output of voltage regulator in card stable */
1067 /* Toggle SD clock again */
1068 err
= rtsx_pci_write_register(pcr
, SD_BUS_STAT
, 0xFF, SD_CLK_TOGGLE_EN
);
1072 /* Wait for a period of time so that the card can drive
1073 * SD_DAT[3:0] to high at 1.8V
1077 /* SD_CMD, SD_DAT[3:0] should be pulled high by host */
1078 err
= rtsx_pci_read_register(pcr
, SD_BUS_STAT
, &stat
);
1082 mask
= SD_CMD_STATUS
| SD_DAT3_STATUS
| SD_DAT2_STATUS
|
1083 SD_DAT1_STATUS
| SD_DAT0_STATUS
;
1084 val
= SD_CMD_STATUS
| SD_DAT3_STATUS
| SD_DAT2_STATUS
|
1085 SD_DAT1_STATUS
| SD_DAT0_STATUS
;
1086 if ((stat
& mask
) != val
) {
1087 dev_dbg(sdmmc_dev(host
),
1088 "%s: SD_BUS_STAT = 0x%x\n", __func__
, stat
);
1089 rtsx_pci_write_register(pcr
, SD_BUS_STAT
,
1090 SD_CLK_TOGGLE_EN
| SD_CLK_FORCE_STOP
, 0);
1091 rtsx_pci_write_register(pcr
, CARD_CLK_EN
, 0xFF, 0);
1098 static int sdmmc_switch_voltage(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
1100 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
1101 struct rtsx_pcr
*pcr
= host
->pcr
;
1105 dev_dbg(sdmmc_dev(host
), "%s: signal_voltage = %d\n",
1106 __func__
, ios
->signal_voltage
);
1111 err
= rtsx_pci_card_exclusive_check(host
->pcr
, RTSX_SD_CARD
);
1115 mutex_lock(&pcr
->pcr_mutex
);
1117 rtsx_pci_start_run(pcr
);
1119 if (ios
->signal_voltage
== MMC_SIGNAL_VOLTAGE_330
)
1120 voltage
= OUTPUT_3V3
;
1122 voltage
= OUTPUT_1V8
;
1124 if (voltage
== OUTPUT_1V8
) {
1125 err
= sd_wait_voltage_stable_1(host
);
1130 err
= rtsx_pci_switch_output_voltage(pcr
, voltage
);
1134 if (voltage
== OUTPUT_1V8
) {
1135 err
= sd_wait_voltage_stable_2(host
);
1141 /* Stop toggle SD clock in idle */
1142 err
= rtsx_pci_write_register(pcr
, SD_BUS_STAT
,
1143 SD_CLK_TOGGLE_EN
| SD_CLK_FORCE_STOP
, 0);
1145 mutex_unlock(&pcr
->pcr_mutex
);
1150 static int sdmmc_execute_tuning(struct mmc_host
*mmc
, u32 opcode
)
1152 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
1153 struct rtsx_pcr
*pcr
= host
->pcr
;
1159 err
= rtsx_pci_card_exclusive_check(host
->pcr
, RTSX_SD_CARD
);
1163 mutex_lock(&pcr
->pcr_mutex
);
1165 rtsx_pci_start_run(pcr
);
1167 /* Set initial TX phase */
1168 switch (mmc
->ios
.timing
) {
1169 case MMC_TIMING_UHS_SDR104
:
1170 err
= sd_change_phase(host
, SDR104_TX_PHASE(pcr
), false);
1173 case MMC_TIMING_UHS_SDR50
:
1174 err
= sd_change_phase(host
, SDR50_TX_PHASE(pcr
), false);
1177 case MMC_TIMING_UHS_DDR50
:
1178 err
= sd_change_phase(host
, DDR50_TX_PHASE(pcr
), false);
1188 /* Tuning RX phase */
1189 if ((mmc
->ios
.timing
== MMC_TIMING_UHS_SDR104
) ||
1190 (mmc
->ios
.timing
== MMC_TIMING_UHS_SDR50
))
1191 err
= sd_tuning_rx(host
, opcode
);
1192 else if (mmc
->ios
.timing
== MMC_TIMING_UHS_DDR50
)
1193 err
= sd_change_phase(host
, DDR50_RX_PHASE(pcr
), true);
1196 mutex_unlock(&pcr
->pcr_mutex
);
1201 static const struct mmc_host_ops realtek_pci_sdmmc_ops
= {
1202 .request
= sdmmc_request
,
1203 .set_ios
= sdmmc_set_ios
,
1204 .get_ro
= sdmmc_get_ro
,
1205 .get_cd
= sdmmc_get_cd
,
1206 .start_signal_voltage_switch
= sdmmc_switch_voltage
,
1207 .execute_tuning
= sdmmc_execute_tuning
,
1211 static int rtsx_pci_sdmmc_suspend(struct platform_device
*pdev
,
1214 struct realtek_pci_sdmmc
*host
= platform_get_drvdata(pdev
);
1215 struct mmc_host
*mmc
= host
->mmc
;
1218 dev_dbg(sdmmc_dev(host
), "--> %s\n", __func__
);
1220 err
= mmc_suspend_host(mmc
);
1227 static int rtsx_pci_sdmmc_resume(struct platform_device
*pdev
)
1229 struct realtek_pci_sdmmc
*host
= platform_get_drvdata(pdev
);
1230 struct mmc_host
*mmc
= host
->mmc
;
1232 dev_dbg(sdmmc_dev(host
), "--> %s\n", __func__
);
1234 return mmc_resume_host(mmc
);
1236 #else /* CONFIG_PM */
1237 #define rtsx_pci_sdmmc_suspend NULL
1238 #define rtsx_pci_sdmmc_resume NULL
1239 #endif /* CONFIG_PM */
1241 static void init_extra_caps(struct realtek_pci_sdmmc
*host
)
1243 struct mmc_host
*mmc
= host
->mmc
;
1244 struct rtsx_pcr
*pcr
= host
->pcr
;
1246 dev_dbg(sdmmc_dev(host
), "pcr->extra_caps = 0x%x\n", pcr
->extra_caps
);
1248 if (pcr
->extra_caps
& EXTRA_CAPS_SD_SDR50
)
1249 mmc
->caps
|= MMC_CAP_UHS_SDR50
;
1250 if (pcr
->extra_caps
& EXTRA_CAPS_SD_SDR104
)
1251 mmc
->caps
|= MMC_CAP_UHS_SDR104
;
1252 if (pcr
->extra_caps
& EXTRA_CAPS_SD_DDR50
)
1253 mmc
->caps
|= MMC_CAP_UHS_DDR50
;
1254 if (pcr
->extra_caps
& EXTRA_CAPS_MMC_HSDDR
)
1255 mmc
->caps
|= MMC_CAP_1_8V_DDR
;
1256 if (pcr
->extra_caps
& EXTRA_CAPS_MMC_8BIT
)
1257 mmc
->caps
|= MMC_CAP_8_BIT_DATA
;
1260 static void realtek_init_host(struct realtek_pci_sdmmc
*host
)
1262 struct mmc_host
*mmc
= host
->mmc
;
1264 mmc
->f_min
= 250000;
1265 mmc
->f_max
= 208000000;
1266 mmc
->ocr_avail
= MMC_VDD_32_33
| MMC_VDD_33_34
| MMC_VDD_165_195
;
1267 mmc
->caps
= MMC_CAP_4_BIT_DATA
| MMC_CAP_SD_HIGHSPEED
|
1268 MMC_CAP_MMC_HIGHSPEED
| MMC_CAP_BUS_WIDTH_TEST
|
1269 MMC_CAP_UHS_SDR12
| MMC_CAP_UHS_SDR25
;
1270 mmc
->max_current_330
= 400;
1271 mmc
->max_current_180
= 800;
1272 mmc
->ops
= &realtek_pci_sdmmc_ops
;
1274 init_extra_caps(host
);
1276 mmc
->max_segs
= 256;
1277 mmc
->max_seg_size
= 65536;
1278 mmc
->max_blk_size
= 512;
1279 mmc
->max_blk_count
= 65535;
1280 mmc
->max_req_size
= 524288;
1283 static void rtsx_pci_sdmmc_card_event(struct platform_device
*pdev
)
1285 struct realtek_pci_sdmmc
*host
= platform_get_drvdata(pdev
);
1287 mmc_detect_change(host
->mmc
, 0);
1290 static int rtsx_pci_sdmmc_drv_probe(struct platform_device
*pdev
)
1292 struct mmc_host
*mmc
;
1293 struct realtek_pci_sdmmc
*host
;
1294 struct rtsx_pcr
*pcr
;
1295 struct pcr_handle
*handle
= pdev
->dev
.platform_data
;
1304 dev_dbg(&(pdev
->dev
), ": Realtek PCI-E SDMMC controller found\n");
1306 mmc
= mmc_alloc_host(sizeof(*host
), &pdev
->dev
);
1310 host
= mmc_priv(mmc
);
1314 host
->power_state
= SDMMC_POWER_OFF
;
1315 platform_set_drvdata(pdev
, host
);
1316 pcr
->slots
[RTSX_SD_CARD
].p_dev
= pdev
;
1317 pcr
->slots
[RTSX_SD_CARD
].card_event
= rtsx_pci_sdmmc_card_event
;
1319 mutex_init(&host
->host_mutex
);
1321 realtek_init_host(host
);
1328 static int rtsx_pci_sdmmc_drv_remove(struct platform_device
*pdev
)
1330 struct realtek_pci_sdmmc
*host
= platform_get_drvdata(pdev
);
1331 struct rtsx_pcr
*pcr
;
1332 struct mmc_host
*mmc
;
1338 pcr
->slots
[RTSX_SD_CARD
].p_dev
= NULL
;
1339 pcr
->slots
[RTSX_SD_CARD
].card_event
= NULL
;
1343 mutex_lock(&host
->host_mutex
);
1345 dev_dbg(&(pdev
->dev
),
1346 "%s: Controller removed during transfer\n",
1349 rtsx_pci_complete_unfinished_transfer(pcr
);
1351 host
->mrq
->cmd
->error
= -ENOMEDIUM
;
1352 if (host
->mrq
->stop
)
1353 host
->mrq
->stop
->error
= -ENOMEDIUM
;
1354 mmc_request_done(mmc
, host
->mrq
);
1356 mutex_unlock(&host
->host_mutex
);
1358 mmc_remove_host(mmc
);
1361 dev_dbg(&(pdev
->dev
),
1362 ": Realtek PCI-E SDMMC controller has been removed\n");
1367 static struct platform_device_id rtsx_pci_sdmmc_ids
[] = {
1369 .name
= DRV_NAME_RTSX_PCI_SDMMC
,
1374 MODULE_DEVICE_TABLE(platform
, rtsx_pci_sdmmc_ids
);
1376 static struct platform_driver rtsx_pci_sdmmc_driver
= {
1377 .probe
= rtsx_pci_sdmmc_drv_probe
,
1378 .remove
= rtsx_pci_sdmmc_drv_remove
,
1379 .id_table
= rtsx_pci_sdmmc_ids
,
1380 .suspend
= rtsx_pci_sdmmc_suspend
,
1381 .resume
= rtsx_pci_sdmmc_resume
,
1383 .owner
= THIS_MODULE
,
1384 .name
= DRV_NAME_RTSX_PCI_SDMMC
,
1387 module_platform_driver(rtsx_pci_sdmmc_driver
);
1389 MODULE_LICENSE("GPL");
1390 MODULE_AUTHOR("Wei WANG <wei_wang@realsil.com.cn>");
1391 MODULE_DESCRIPTION("Realtek PCI-E SD/MMC Card Host Driver");