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 struct workqueue_struct
*workq
;
42 #define SDMMC_WORKQ_NAME "rtsx_pci_sdmmc_workq"
44 struct work_struct work
;
45 struct mutex host_mutex
;
54 #define SDMMC_POWER_ON 1
55 #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 dump_reg_range(struct realtek_pci_sdmmc
*host
, u16 start
, u16 end
)
77 u16 len
= end
- start
+ 1;
81 for (i
= 0; i
< len
; i
+= 8) {
83 int n
= min(8, len
- i
);
85 memset(&data
, 0, sizeof(data
));
86 for (j
= 0; j
< n
; j
++)
87 rtsx_pci_read_register(host
->pcr
, start
+ i
+ j
,
89 dev_dbg(sdmmc_dev(host
), "0x%04X(%d): %8ph\n",
94 static void sd_print_debug_regs(struct realtek_pci_sdmmc
*host
)
96 dump_reg_range(host
, 0xFDA0, 0xFDB3);
97 dump_reg_range(host
, 0xFD52, 0xFD69);
100 #define sd_print_debug_regs(host)
103 static inline int sd_get_cd_int(struct realtek_pci_sdmmc
*host
)
105 return rtsx_pci_readl(host
->pcr
, RTSX_BIPR
) & SD_EXIST
;
108 static void sd_cmd_set_sd_cmd(struct rtsx_pcr
*pcr
, struct mmc_command
*cmd
)
110 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CMD0
, 0xFF,
111 SD_CMD_START
| cmd
->opcode
);
112 rtsx_pci_write_be32(pcr
, SD_CMD1
, cmd
->arg
);
115 static void sd_cmd_set_data_len(struct rtsx_pcr
*pcr
, u16 blocks
, u16 blksz
)
117 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_BLOCK_CNT_L
, 0xFF, blocks
);
118 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_BLOCK_CNT_H
, 0xFF, blocks
>> 8);
119 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_BYTE_CNT_L
, 0xFF, blksz
);
120 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_BYTE_CNT_H
, 0xFF, blksz
>> 8);
123 static int sd_response_type(struct mmc_command
*cmd
)
125 switch (mmc_resp_type(cmd
)) {
127 return SD_RSP_TYPE_R0
;
129 return SD_RSP_TYPE_R1
;
130 case MMC_RSP_R1
& ~MMC_RSP_CRC
:
131 return SD_RSP_TYPE_R1
| SD_NO_CHECK_CRC7
;
133 return SD_RSP_TYPE_R1b
;
135 return SD_RSP_TYPE_R2
;
137 return SD_RSP_TYPE_R3
;
143 static int sd_status_index(int resp_type
)
145 if (resp_type
== SD_RSP_TYPE_R0
)
147 else if (resp_type
== SD_RSP_TYPE_R2
)
153 * sd_pre_dma_transfer - do dma_map_sg() or using cookie
155 * @pre: if called in pre_req()
157 * 0 - do dma_map_sg()
160 static int sd_pre_dma_transfer(struct realtek_pci_sdmmc
*host
,
161 struct mmc_data
*data
, bool pre
)
163 struct rtsx_pcr
*pcr
= host
->pcr
;
164 int read
= data
->flags
& MMC_DATA_READ
;
166 int using_cookie
= 0;
168 if (!pre
&& data
->host_cookie
&& data
->host_cookie
!= host
->cookie
) {
169 dev_err(sdmmc_dev(host
),
170 "error: data->host_cookie = %d, host->cookie = %d\n",
171 data
->host_cookie
, host
->cookie
);
172 data
->host_cookie
= 0;
175 if (pre
|| data
->host_cookie
!= host
->cookie
) {
176 count
= rtsx_pci_dma_map_sg(pcr
, data
->sg
, data
->sg_len
, read
);
178 count
= host
->cookie_sg_count
;
183 host
->cookie_sg_count
= count
;
184 if (++host
->cookie
< 0)
186 data
->host_cookie
= host
->cookie
;
188 host
->sg_count
= count
;
194 static void sdmmc_pre_req(struct mmc_host
*mmc
, struct mmc_request
*mrq
,
197 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
198 struct mmc_data
*data
= mrq
->data
;
200 if (data
->host_cookie
) {
201 dev_err(sdmmc_dev(host
),
202 "error: reset data->host_cookie = %d\n",
204 data
->host_cookie
= 0;
207 sd_pre_dma_transfer(host
, data
, true);
208 dev_dbg(sdmmc_dev(host
), "pre dma sg: %d\n", host
->cookie_sg_count
);
211 static void sdmmc_post_req(struct mmc_host
*mmc
, struct mmc_request
*mrq
,
214 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
215 struct rtsx_pcr
*pcr
= host
->pcr
;
216 struct mmc_data
*data
= mrq
->data
;
217 int read
= data
->flags
& MMC_DATA_READ
;
219 rtsx_pci_dma_unmap_sg(pcr
, data
->sg
, data
->sg_len
, read
);
220 data
->host_cookie
= 0;
223 static void sd_send_cmd_get_rsp(struct realtek_pci_sdmmc
*host
,
224 struct mmc_command
*cmd
)
226 struct rtsx_pcr
*pcr
= host
->pcr
;
227 u8 cmd_idx
= (u8
)cmd
->opcode
;
235 bool clock_toggled
= false;
237 dev_dbg(sdmmc_dev(host
), "%s: SD/MMC CMD %d, arg = 0x%08x\n",
238 __func__
, cmd_idx
, arg
);
240 rsp_type
= sd_response_type(cmd
);
244 stat_idx
= sd_status_index(rsp_type
);
246 if (rsp_type
== SD_RSP_TYPE_R1b
)
247 timeout
= cmd
->busy_timeout
? cmd
->busy_timeout
: 3000;
249 if (cmd
->opcode
== SD_SWITCH_VOLTAGE
) {
250 err
= rtsx_pci_write_register(pcr
, SD_BUS_STAT
,
251 0xFF, SD_CLK_TOGGLE_EN
);
255 clock_toggled
= true;
258 rtsx_pci_init_cmd(pcr
);
259 sd_cmd_set_sd_cmd(pcr
, cmd
);
260 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG2
, 0xFF, rsp_type
);
261 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_DATA_SOURCE
,
262 0x01, PINGPONG_BUFFER
);
263 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_TRANSFER
,
264 0xFF, SD_TM_CMD_RSP
| SD_TRANSFER_START
);
265 rtsx_pci_add_cmd(pcr
, CHECK_REG_CMD
, SD_TRANSFER
,
266 SD_TRANSFER_END
| SD_STAT_IDLE
,
267 SD_TRANSFER_END
| SD_STAT_IDLE
);
269 if (rsp_type
== SD_RSP_TYPE_R2
) {
270 /* Read data from ping-pong buffer */
271 for (i
= PPBUF_BASE2
; i
< PPBUF_BASE2
+ 16; i
++)
272 rtsx_pci_add_cmd(pcr
, READ_REG_CMD
, (u16
)i
, 0, 0);
273 } else if (rsp_type
!= SD_RSP_TYPE_R0
) {
274 /* Read data from SD_CMDx registers */
275 for (i
= SD_CMD0
; i
<= SD_CMD4
; i
++)
276 rtsx_pci_add_cmd(pcr
, READ_REG_CMD
, (u16
)i
, 0, 0);
279 rtsx_pci_add_cmd(pcr
, READ_REG_CMD
, SD_STAT1
, 0, 0);
281 err
= rtsx_pci_send_cmd(pcr
, timeout
);
283 sd_print_debug_regs(host
);
284 sd_clear_error(host
);
285 dev_dbg(sdmmc_dev(host
),
286 "rtsx_pci_send_cmd error (err = %d)\n", err
);
290 if (rsp_type
== SD_RSP_TYPE_R0
) {
295 /* Eliminate returned value of CHECK_REG_CMD */
296 ptr
= rtsx_pci_get_cmd_data(pcr
) + 1;
298 /* Check (Start,Transmission) bit of Response */
299 if ((ptr
[0] & 0xC0) != 0) {
301 dev_dbg(sdmmc_dev(host
), "Invalid response bit\n");
306 if (!(rsp_type
& SD_NO_CHECK_CRC7
)) {
307 if (ptr
[stat_idx
] & SD_CRC7_ERR
) {
309 dev_dbg(sdmmc_dev(host
), "CRC7 error\n");
314 if (rsp_type
== SD_RSP_TYPE_R2
) {
316 * The controller offloads the last byte {CRC-7, end bit 1'b1}
317 * of response type R2. Assign dummy CRC, 0, and end bit to the
318 * byte(ptr[16], goes into the LSB of resp[3] later).
322 for (i
= 0; i
< 4; i
++) {
323 cmd
->resp
[i
] = get_unaligned_be32(ptr
+ 1 + i
* 4);
324 dev_dbg(sdmmc_dev(host
), "cmd->resp[%d] = 0x%08x\n",
328 cmd
->resp
[0] = get_unaligned_be32(ptr
+ 1);
329 dev_dbg(sdmmc_dev(host
), "cmd->resp[0] = 0x%08x\n",
336 if (err
&& clock_toggled
)
337 rtsx_pci_write_register(pcr
, SD_BUS_STAT
,
338 SD_CLK_TOGGLE_EN
| SD_CLK_FORCE_STOP
, 0);
341 static int sd_read_data(struct realtek_pci_sdmmc
*host
, struct mmc_command
*cmd
,
342 u16 byte_cnt
, u8
*buf
, int buf_len
, int timeout
)
344 struct rtsx_pcr
*pcr
= host
->pcr
;
348 dev_dbg(sdmmc_dev(host
), "%s: SD/MMC CMD %d, arg = 0x%08x\n",
349 __func__
, cmd
->opcode
, cmd
->arg
);
354 if (cmd
->opcode
== MMC_SEND_TUNING_BLOCK
)
355 trans_mode
= SD_TM_AUTO_TUNING
;
357 trans_mode
= SD_TM_NORMAL_READ
;
359 rtsx_pci_init_cmd(pcr
);
360 sd_cmd_set_sd_cmd(pcr
, cmd
);
361 sd_cmd_set_data_len(pcr
, 1, byte_cnt
);
362 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG2
, 0xFF,
363 SD_CALCULATE_CRC7
| SD_CHECK_CRC16
|
364 SD_NO_WAIT_BUSY_END
| SD_CHECK_CRC7
| SD_RSP_LEN_6
);
365 if (trans_mode
!= SD_TM_AUTO_TUNING
)
366 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
,
367 CARD_DATA_SOURCE
, 0x01, PINGPONG_BUFFER
);
369 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_TRANSFER
,
370 0xFF, trans_mode
| SD_TRANSFER_START
);
371 rtsx_pci_add_cmd(pcr
, CHECK_REG_CMD
, SD_TRANSFER
,
372 SD_TRANSFER_END
, SD_TRANSFER_END
);
374 err
= rtsx_pci_send_cmd(pcr
, timeout
);
376 sd_print_debug_regs(host
);
377 dev_dbg(sdmmc_dev(host
),
378 "rtsx_pci_send_cmd fail (err = %d)\n", err
);
382 if (buf
&& buf_len
) {
383 err
= rtsx_pci_read_ppbuf(pcr
, buf
, buf_len
);
385 dev_dbg(sdmmc_dev(host
),
386 "rtsx_pci_read_ppbuf fail (err = %d)\n", err
);
394 static int sd_write_data(struct realtek_pci_sdmmc
*host
,
395 struct mmc_command
*cmd
, u16 byte_cnt
, u8
*buf
, int buf_len
,
398 struct rtsx_pcr
*pcr
= host
->pcr
;
401 dev_dbg(sdmmc_dev(host
), "%s: SD/MMC CMD %d, arg = 0x%08x\n",
402 __func__
, cmd
->opcode
, cmd
->arg
);
407 sd_send_cmd_get_rsp(host
, cmd
);
411 if (buf
&& buf_len
) {
412 err
= rtsx_pci_write_ppbuf(pcr
, buf
, buf_len
);
414 dev_dbg(sdmmc_dev(host
),
415 "rtsx_pci_write_ppbuf fail (err = %d)\n", err
);
420 rtsx_pci_init_cmd(pcr
);
421 sd_cmd_set_data_len(pcr
, 1, byte_cnt
);
422 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG2
, 0xFF,
423 SD_CALCULATE_CRC7
| SD_CHECK_CRC16
|
424 SD_NO_WAIT_BUSY_END
| SD_CHECK_CRC7
| SD_RSP_LEN_0
);
425 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_TRANSFER
, 0xFF,
426 SD_TRANSFER_START
| SD_TM_AUTO_WRITE_3
);
427 rtsx_pci_add_cmd(pcr
, CHECK_REG_CMD
, SD_TRANSFER
,
428 SD_TRANSFER_END
, SD_TRANSFER_END
);
430 err
= rtsx_pci_send_cmd(pcr
, timeout
);
432 sd_print_debug_regs(host
);
433 dev_dbg(sdmmc_dev(host
),
434 "rtsx_pci_send_cmd fail (err = %d)\n", err
);
441 static int sd_read_long_data(struct realtek_pci_sdmmc
*host
,
442 struct mmc_request
*mrq
)
444 struct rtsx_pcr
*pcr
= host
->pcr
;
445 struct mmc_host
*mmc
= host
->mmc
;
446 struct mmc_card
*card
= mmc
->card
;
447 struct mmc_command
*cmd
= mrq
->cmd
;
448 struct mmc_data
*data
= mrq
->data
;
449 int uhs
= mmc_card_uhs(card
);
453 size_t data_len
= data
->blksz
* data
->blocks
;
455 dev_dbg(sdmmc_dev(host
), "%s: SD/MMC CMD %d, arg = 0x%08x\n",
456 __func__
, cmd
->opcode
, cmd
->arg
);
458 resp_type
= sd_response_type(cmd
);
463 cfg2
|= SD_NO_CHECK_WAIT_CRC_TO
;
465 rtsx_pci_init_cmd(pcr
);
466 sd_cmd_set_sd_cmd(pcr
, cmd
);
467 sd_cmd_set_data_len(pcr
, data
->blocks
, data
->blksz
);
468 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, IRQSTAT0
,
469 DMA_DONE_INT
, DMA_DONE_INT
);
470 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC3
,
471 0xFF, (u8
)(data_len
>> 24));
472 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC2
,
473 0xFF, (u8
)(data_len
>> 16));
474 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC1
,
475 0xFF, (u8
)(data_len
>> 8));
476 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC0
, 0xFF, (u8
)data_len
);
477 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMACTL
,
478 0x03 | DMA_PACK_SIZE_MASK
,
479 DMA_DIR_FROM_CARD
| DMA_EN
| DMA_512
);
480 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_DATA_SOURCE
,
482 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG2
, 0xFF, cfg2
| resp_type
);
483 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_TRANSFER
, 0xFF,
484 SD_TRANSFER_START
| SD_TM_AUTO_READ_2
);
485 rtsx_pci_add_cmd(pcr
, CHECK_REG_CMD
, SD_TRANSFER
,
486 SD_TRANSFER_END
, SD_TRANSFER_END
);
487 rtsx_pci_send_cmd_no_wait(pcr
);
489 err
= rtsx_pci_dma_transfer(pcr
, data
->sg
, host
->sg_count
, 1, 10000);
491 sd_print_debug_regs(host
);
492 sd_clear_error(host
);
499 static int sd_write_long_data(struct realtek_pci_sdmmc
*host
,
500 struct mmc_request
*mrq
)
502 struct rtsx_pcr
*pcr
= host
->pcr
;
503 struct mmc_host
*mmc
= host
->mmc
;
504 struct mmc_card
*card
= mmc
->card
;
505 struct mmc_command
*cmd
= mrq
->cmd
;
506 struct mmc_data
*data
= mrq
->data
;
507 int uhs
= mmc_card_uhs(card
);
510 size_t data_len
= data
->blksz
* data
->blocks
;
512 sd_send_cmd_get_rsp(host
, cmd
);
516 dev_dbg(sdmmc_dev(host
), "%s: SD/MMC CMD %d, arg = 0x%08x\n",
517 __func__
, cmd
->opcode
, cmd
->arg
);
519 cfg2
= SD_NO_CALCULATE_CRC7
| SD_CHECK_CRC16
|
520 SD_NO_WAIT_BUSY_END
| SD_NO_CHECK_CRC7
| SD_RSP_LEN_0
;
523 cfg2
|= SD_NO_CHECK_WAIT_CRC_TO
;
525 rtsx_pci_init_cmd(pcr
);
526 sd_cmd_set_data_len(pcr
, data
->blocks
, data
->blksz
);
527 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, IRQSTAT0
,
528 DMA_DONE_INT
, DMA_DONE_INT
);
529 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC3
,
530 0xFF, (u8
)(data_len
>> 24));
531 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC2
,
532 0xFF, (u8
)(data_len
>> 16));
533 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC1
,
534 0xFF, (u8
)(data_len
>> 8));
535 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC0
, 0xFF, (u8
)data_len
);
536 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMACTL
,
537 0x03 | DMA_PACK_SIZE_MASK
,
538 DMA_DIR_TO_CARD
| DMA_EN
| DMA_512
);
539 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_DATA_SOURCE
,
541 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG2
, 0xFF, cfg2
);
542 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_TRANSFER
, 0xFF,
543 SD_TRANSFER_START
| SD_TM_AUTO_WRITE_3
);
544 rtsx_pci_add_cmd(pcr
, CHECK_REG_CMD
, SD_TRANSFER
,
545 SD_TRANSFER_END
, SD_TRANSFER_END
);
546 rtsx_pci_send_cmd_no_wait(pcr
);
547 err
= rtsx_pci_dma_transfer(pcr
, data
->sg
, host
->sg_count
, 0, 10000);
549 sd_clear_error(host
);
556 static int sd_rw_multi(struct realtek_pci_sdmmc
*host
, struct mmc_request
*mrq
)
558 struct mmc_data
*data
= mrq
->data
;
560 if (host
->sg_count
< 0) {
561 data
->error
= host
->sg_count
;
562 dev_dbg(sdmmc_dev(host
), "%s: sg_count = %d is invalid\n",
563 __func__
, host
->sg_count
);
567 if (data
->flags
& MMC_DATA_READ
)
568 return sd_read_long_data(host
, mrq
);
570 return sd_write_long_data(host
, mrq
);
573 static inline void sd_enable_initial_mode(struct realtek_pci_sdmmc
*host
)
575 rtsx_pci_write_register(host
->pcr
, SD_CFG1
,
576 SD_CLK_DIVIDE_MASK
, SD_CLK_DIVIDE_128
);
579 static inline void sd_disable_initial_mode(struct realtek_pci_sdmmc
*host
)
581 rtsx_pci_write_register(host
->pcr
, SD_CFG1
,
582 SD_CLK_DIVIDE_MASK
, SD_CLK_DIVIDE_0
);
585 static void sd_normal_rw(struct realtek_pci_sdmmc
*host
,
586 struct mmc_request
*mrq
)
588 struct mmc_command
*cmd
= mrq
->cmd
;
589 struct mmc_data
*data
= mrq
->data
;
592 buf
= kzalloc(data
->blksz
, GFP_NOIO
);
594 cmd
->error
= -ENOMEM
;
598 if (data
->flags
& MMC_DATA_READ
) {
599 if (host
->initial_mode
)
600 sd_disable_initial_mode(host
);
602 cmd
->error
= sd_read_data(host
, cmd
, (u16
)data
->blksz
, buf
,
605 if (host
->initial_mode
)
606 sd_enable_initial_mode(host
);
608 sg_copy_from_buffer(data
->sg
, data
->sg_len
, buf
, data
->blksz
);
610 sg_copy_to_buffer(data
->sg
, data
->sg_len
, buf
, data
->blksz
);
612 cmd
->error
= sd_write_data(host
, cmd
, (u16
)data
->blksz
, buf
,
619 static int sd_change_phase(struct realtek_pci_sdmmc
*host
,
620 u8 sample_point
, bool rx
)
622 struct rtsx_pcr
*pcr
= host
->pcr
;
625 dev_dbg(sdmmc_dev(host
), "%s(%s): sample_point = %d\n",
626 __func__
, rx
? "RX" : "TX", sample_point
);
628 rtsx_pci_init_cmd(pcr
);
630 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
, CHANGE_CLK
, CHANGE_CLK
);
632 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
,
633 SD_VPRX_CTL
, 0x1F, sample_point
);
635 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
,
636 SD_VPTX_CTL
, 0x1F, sample_point
);
637 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_VPCLK0_CTL
, PHASE_NOT_RESET
, 0);
638 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_VPCLK0_CTL
,
639 PHASE_NOT_RESET
, PHASE_NOT_RESET
);
640 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
, CHANGE_CLK
, 0);
641 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG1
, SD_ASYNC_FIFO_NOT_RST
, 0);
643 err
= rtsx_pci_send_cmd(pcr
, 100);
650 static inline u32
test_phase_bit(u32 phase_map
, unsigned int bit
)
652 bit
%= RTSX_PHASE_MAX
;
653 return phase_map
& (1 << bit
);
656 static int sd_get_phase_len(u32 phase_map
, unsigned int start_bit
)
660 for (i
= 0; i
< RTSX_PHASE_MAX
; i
++) {
661 if (test_phase_bit(phase_map
, start_bit
+ i
) == 0)
664 return RTSX_PHASE_MAX
;
667 static u8
sd_search_final_phase(struct realtek_pci_sdmmc
*host
, u32 phase_map
)
669 int start
= 0, len
= 0;
670 int start_final
= 0, len_final
= 0;
671 u8 final_phase
= 0xFF;
673 if (phase_map
== 0) {
674 dev_err(sdmmc_dev(host
), "phase error: [map:%x]\n", phase_map
);
678 while (start
< RTSX_PHASE_MAX
) {
679 len
= sd_get_phase_len(phase_map
, start
);
680 if (len_final
< len
) {
684 start
+= len
? len
: 1;
687 final_phase
= (start_final
+ len_final
/ 2) % RTSX_PHASE_MAX
;
688 dev_dbg(sdmmc_dev(host
), "phase: [map:%x] [maxlen:%d] [final:%d]\n",
689 phase_map
, len_final
, final_phase
);
694 static void sd_wait_data_idle(struct realtek_pci_sdmmc
*host
)
699 for (i
= 0; i
< 100; i
++) {
700 err
= rtsx_pci_read_register(host
->pcr
, SD_DATA_STATE
, &val
);
701 if (val
& SD_DATA_IDLE
)
708 static int sd_tuning_rx_cmd(struct realtek_pci_sdmmc
*host
,
709 u8 opcode
, u8 sample_point
)
712 struct mmc_command cmd
= {0};
714 err
= sd_change_phase(host
, sample_point
, true);
719 err
= sd_read_data(host
, &cmd
, 0x40, NULL
, 0, 100);
721 /* Wait till SD DATA IDLE */
722 sd_wait_data_idle(host
);
723 sd_clear_error(host
);
730 static int sd_tuning_phase(struct realtek_pci_sdmmc
*host
,
731 u8 opcode
, u32
*phase_map
)
734 u32 raw_phase_map
= 0;
736 for (i
= 0; i
< RTSX_PHASE_MAX
; i
++) {
737 err
= sd_tuning_rx_cmd(host
, opcode
, (u8
)i
);
739 raw_phase_map
|= 1 << i
;
743 *phase_map
= raw_phase_map
;
748 static int sd_tuning_rx(struct realtek_pci_sdmmc
*host
, u8 opcode
)
751 u32 raw_phase_map
[RX_TUNING_CNT
] = {0}, phase_map
;
754 for (i
= 0; i
< RX_TUNING_CNT
; i
++) {
755 err
= sd_tuning_phase(host
, opcode
, &(raw_phase_map
[i
]));
759 if (raw_phase_map
[i
] == 0)
763 phase_map
= 0xFFFFFFFF;
764 for (i
= 0; i
< RX_TUNING_CNT
; i
++) {
765 dev_dbg(sdmmc_dev(host
), "RX raw_phase_map[%d] = 0x%08x\n",
766 i
, raw_phase_map
[i
]);
767 phase_map
&= raw_phase_map
[i
];
769 dev_dbg(sdmmc_dev(host
), "RX phase_map = 0x%08x\n", phase_map
);
772 final_phase
= sd_search_final_phase(host
, phase_map
);
773 if (final_phase
== 0xFF)
776 err
= sd_change_phase(host
, final_phase
, true);
786 static inline int sdio_extblock_cmd(struct mmc_command
*cmd
,
787 struct mmc_data
*data
)
789 return (cmd
->opcode
== SD_IO_RW_EXTENDED
) && (data
->blksz
== 512);
792 static inline int sd_rw_cmd(struct mmc_command
*cmd
)
794 return mmc_op_multi(cmd
->opcode
) ||
795 (cmd
->opcode
== MMC_READ_SINGLE_BLOCK
) ||
796 (cmd
->opcode
== MMC_WRITE_BLOCK
);
799 static void sd_request(struct work_struct
*work
)
801 struct realtek_pci_sdmmc
*host
= container_of(work
,
802 struct realtek_pci_sdmmc
, work
);
803 struct rtsx_pcr
*pcr
= host
->pcr
;
805 struct mmc_host
*mmc
= host
->mmc
;
806 struct mmc_request
*mrq
= host
->mrq
;
807 struct mmc_command
*cmd
= mrq
->cmd
;
808 struct mmc_data
*data
= mrq
->data
;
810 unsigned int data_size
= 0;
813 if (host
->eject
|| !sd_get_cd_int(host
)) {
814 cmd
->error
= -ENOMEDIUM
;
818 err
= rtsx_pci_card_exclusive_check(host
->pcr
, RTSX_SD_CARD
);
824 mutex_lock(&pcr
->pcr_mutex
);
826 rtsx_pci_start_run(pcr
);
828 rtsx_pci_switch_clock(pcr
, host
->clock
, host
->ssc_depth
,
829 host
->initial_mode
, host
->double_clk
, host
->vpclk
);
830 rtsx_pci_write_register(pcr
, CARD_SELECT
, 0x07, SD_MOD_SEL
);
831 rtsx_pci_write_register(pcr
, CARD_SHARE_MODE
,
832 CARD_SHARE_MASK
, CARD_SHARE_48_SD
);
834 mutex_lock(&host
->host_mutex
);
836 mutex_unlock(&host
->host_mutex
);
839 data_size
= data
->blocks
* data
->blksz
;
842 sd_send_cmd_get_rsp(host
, cmd
);
843 } else if (sd_rw_cmd(cmd
) || sdio_extblock_cmd(cmd
, data
)) {
844 cmd
->error
= sd_rw_multi(host
, mrq
);
845 if (!host
->using_cookie
)
846 sdmmc_post_req(host
->mmc
, host
->mrq
, 0);
848 if (mmc_op_multi(cmd
->opcode
) && mrq
->stop
)
849 sd_send_cmd_get_rsp(host
, mrq
->stop
);
851 sd_normal_rw(host
, mrq
);
855 if (cmd
->error
|| data
->error
)
856 data
->bytes_xfered
= 0;
858 data
->bytes_xfered
= data
->blocks
* data
->blksz
;
861 mutex_unlock(&pcr
->pcr_mutex
);
865 dev_dbg(sdmmc_dev(host
), "CMD %d 0x%08x error(%d)\n",
866 cmd
->opcode
, cmd
->arg
, cmd
->error
);
869 mutex_lock(&host
->host_mutex
);
871 mutex_unlock(&host
->host_mutex
);
873 mmc_request_done(mmc
, mrq
);
876 static void sdmmc_request(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
878 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
879 struct mmc_data
*data
= mrq
->data
;
881 mutex_lock(&host
->host_mutex
);
883 mutex_unlock(&host
->host_mutex
);
885 if (sd_rw_cmd(mrq
->cmd
) || sdio_extblock_cmd(mrq
->cmd
, data
))
886 host
->using_cookie
= sd_pre_dma_transfer(host
, data
, false);
888 queue_work(host
->workq
, &host
->work
);
891 static int sd_set_bus_width(struct realtek_pci_sdmmc
*host
,
892 unsigned char bus_width
)
896 [MMC_BUS_WIDTH_1
] = SD_BUS_WIDTH_1BIT
,
897 [MMC_BUS_WIDTH_4
] = SD_BUS_WIDTH_4BIT
,
898 [MMC_BUS_WIDTH_8
] = SD_BUS_WIDTH_8BIT
,
901 if (bus_width
<= MMC_BUS_WIDTH_8
)
902 err
= rtsx_pci_write_register(host
->pcr
, SD_CFG1
,
903 0x03, width
[bus_width
]);
908 static int sd_power_on(struct realtek_pci_sdmmc
*host
)
910 struct rtsx_pcr
*pcr
= host
->pcr
;
913 if (host
->power_state
== SDMMC_POWER_ON
)
916 rtsx_pci_init_cmd(pcr
);
917 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_SELECT
, 0x07, SD_MOD_SEL
);
918 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_SHARE_MODE
,
919 CARD_SHARE_MASK
, CARD_SHARE_48_SD
);
920 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_CLK_EN
,
921 SD_CLK_EN
, SD_CLK_EN
);
922 err
= rtsx_pci_send_cmd(pcr
, 100);
926 err
= rtsx_pci_card_pull_ctl_enable(pcr
, RTSX_SD_CARD
);
930 err
= rtsx_pci_card_power_on(pcr
, RTSX_SD_CARD
);
934 err
= rtsx_pci_write_register(pcr
, CARD_OE
, SD_OUTPUT_EN
, SD_OUTPUT_EN
);
938 host
->power_state
= SDMMC_POWER_ON
;
942 static int sd_power_off(struct realtek_pci_sdmmc
*host
)
944 struct rtsx_pcr
*pcr
= host
->pcr
;
947 host
->power_state
= SDMMC_POWER_OFF
;
949 rtsx_pci_init_cmd(pcr
);
951 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_CLK_EN
, SD_CLK_EN
, 0);
952 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_OE
, SD_OUTPUT_EN
, 0);
954 err
= rtsx_pci_send_cmd(pcr
, 100);
958 err
= rtsx_pci_card_power_off(pcr
, RTSX_SD_CARD
);
962 return rtsx_pci_card_pull_ctl_disable(pcr
, RTSX_SD_CARD
);
965 static int sd_set_power_mode(struct realtek_pci_sdmmc
*host
,
966 unsigned char power_mode
)
970 if (power_mode
== MMC_POWER_OFF
)
971 err
= sd_power_off(host
);
973 err
= sd_power_on(host
);
978 static int sd_set_timing(struct realtek_pci_sdmmc
*host
, unsigned char timing
)
980 struct rtsx_pcr
*pcr
= host
->pcr
;
983 rtsx_pci_init_cmd(pcr
);
986 case MMC_TIMING_UHS_SDR104
:
987 case MMC_TIMING_UHS_SDR50
:
988 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG1
,
989 0x0C | SD_ASYNC_FIFO_NOT_RST
,
990 SD_30_MODE
| SD_ASYNC_FIFO_NOT_RST
);
991 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
,
992 CLK_LOW_FREQ
, CLK_LOW_FREQ
);
993 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_CLK_SOURCE
, 0xFF,
994 CRC_VAR_CLK0
| SD30_FIX_CLK
| SAMPLE_VAR_CLK1
);
995 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
, CLK_LOW_FREQ
, 0);
998 case MMC_TIMING_MMC_DDR52
:
999 case MMC_TIMING_UHS_DDR50
:
1000 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG1
,
1001 0x0C | SD_ASYNC_FIFO_NOT_RST
,
1002 SD_DDR_MODE
| SD_ASYNC_FIFO_NOT_RST
);
1003 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
,
1004 CLK_LOW_FREQ
, CLK_LOW_FREQ
);
1005 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_CLK_SOURCE
, 0xFF,
1006 CRC_VAR_CLK0
| SD30_FIX_CLK
| SAMPLE_VAR_CLK1
);
1007 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
, CLK_LOW_FREQ
, 0);
1008 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_PUSH_POINT_CTL
,
1009 DDR_VAR_TX_CMD_DAT
, DDR_VAR_TX_CMD_DAT
);
1010 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_SAMPLE_POINT_CTL
,
1011 DDR_VAR_RX_DAT
| DDR_VAR_RX_CMD
,
1012 DDR_VAR_RX_DAT
| DDR_VAR_RX_CMD
);
1015 case MMC_TIMING_MMC_HS
:
1016 case MMC_TIMING_SD_HS
:
1017 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_CFG1
,
1019 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
,
1020 CLK_LOW_FREQ
, CLK_LOW_FREQ
);
1021 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_CLK_SOURCE
, 0xFF,
1022 CRC_FIX_CLK
| SD30_VAR_CLK0
| SAMPLE_VAR_CLK1
);
1023 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
, CLK_LOW_FREQ
, 0);
1024 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_PUSH_POINT_CTL
,
1025 SD20_TX_SEL_MASK
, SD20_TX_14_AHEAD
);
1026 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_SAMPLE_POINT_CTL
,
1027 SD20_RX_SEL_MASK
, SD20_RX_14_DELAY
);
1031 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
,
1032 SD_CFG1
, 0x0C, SD_20_MODE
);
1033 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
,
1034 CLK_LOW_FREQ
, CLK_LOW_FREQ
);
1035 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_CLK_SOURCE
, 0xFF,
1036 CRC_FIX_CLK
| SD30_VAR_CLK0
| SAMPLE_VAR_CLK1
);
1037 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CLK_CTL
, CLK_LOW_FREQ
, 0);
1038 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
,
1039 SD_PUSH_POINT_CTL
, 0xFF, 0);
1040 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, SD_SAMPLE_POINT_CTL
,
1041 SD20_RX_SEL_MASK
, SD20_RX_POS_EDGE
);
1045 err
= rtsx_pci_send_cmd(pcr
, 100);
1050 static void sdmmc_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
1052 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
1053 struct rtsx_pcr
*pcr
= host
->pcr
;
1058 if (rtsx_pci_card_exclusive_check(host
->pcr
, RTSX_SD_CARD
))
1061 mutex_lock(&pcr
->pcr_mutex
);
1063 rtsx_pci_start_run(pcr
);
1065 sd_set_bus_width(host
, ios
->bus_width
);
1066 sd_set_power_mode(host
, ios
->power_mode
);
1067 sd_set_timing(host
, ios
->timing
);
1069 host
->vpclk
= false;
1070 host
->double_clk
= true;
1072 switch (ios
->timing
) {
1073 case MMC_TIMING_UHS_SDR104
:
1074 case MMC_TIMING_UHS_SDR50
:
1075 host
->ssc_depth
= RTSX_SSC_DEPTH_2M
;
1077 host
->double_clk
= false;
1079 case MMC_TIMING_MMC_DDR52
:
1080 case MMC_TIMING_UHS_DDR50
:
1081 case MMC_TIMING_UHS_SDR25
:
1082 host
->ssc_depth
= RTSX_SSC_DEPTH_1M
;
1085 host
->ssc_depth
= RTSX_SSC_DEPTH_500K
;
1089 host
->initial_mode
= (ios
->clock
<= 1000000) ? true : false;
1091 host
->clock
= ios
->clock
;
1092 rtsx_pci_switch_clock(pcr
, ios
->clock
, host
->ssc_depth
,
1093 host
->initial_mode
, host
->double_clk
, host
->vpclk
);
1095 mutex_unlock(&pcr
->pcr_mutex
);
1098 static int sdmmc_get_ro(struct mmc_host
*mmc
)
1100 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
1101 struct rtsx_pcr
*pcr
= host
->pcr
;
1108 mutex_lock(&pcr
->pcr_mutex
);
1110 rtsx_pci_start_run(pcr
);
1112 /* Check SD mechanical write-protect switch */
1113 val
= rtsx_pci_readl(pcr
, RTSX_BIPR
);
1114 dev_dbg(sdmmc_dev(host
), "%s: RTSX_BIPR = 0x%08x\n", __func__
, val
);
1115 if (val
& SD_WRITE_PROTECT
)
1118 mutex_unlock(&pcr
->pcr_mutex
);
1123 static int sdmmc_get_cd(struct mmc_host
*mmc
)
1125 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
1126 struct rtsx_pcr
*pcr
= host
->pcr
;
1133 mutex_lock(&pcr
->pcr_mutex
);
1135 rtsx_pci_start_run(pcr
);
1137 /* Check SD card detect */
1138 val
= rtsx_pci_card_exist(pcr
);
1139 dev_dbg(sdmmc_dev(host
), "%s: RTSX_BIPR = 0x%08x\n", __func__
, val
);
1143 mutex_unlock(&pcr
->pcr_mutex
);
1148 static int sd_wait_voltage_stable_1(struct realtek_pci_sdmmc
*host
)
1150 struct rtsx_pcr
*pcr
= host
->pcr
;
1154 /* Reference to Signal Voltage Switch Sequence in SD spec.
1155 * Wait for a period of time so that the card can drive SD_CMD and
1156 * SD_DAT[3:0] to low after sending back CMD11 response.
1160 /* SD_CMD, SD_DAT[3:0] should be driven to low by card;
1161 * If either one of SD_CMD,SD_DAT[3:0] is not low,
1162 * abort the voltage switch sequence;
1164 err
= rtsx_pci_read_register(pcr
, SD_BUS_STAT
, &stat
);
1168 if (stat
& (SD_CMD_STATUS
| SD_DAT3_STATUS
| SD_DAT2_STATUS
|
1169 SD_DAT1_STATUS
| SD_DAT0_STATUS
))
1172 /* Stop toggle SD clock */
1173 err
= rtsx_pci_write_register(pcr
, SD_BUS_STAT
,
1174 0xFF, SD_CLK_FORCE_STOP
);
1181 static int sd_wait_voltage_stable_2(struct realtek_pci_sdmmc
*host
)
1183 struct rtsx_pcr
*pcr
= host
->pcr
;
1187 /* Wait 1.8V output of voltage regulator in card stable */
1190 /* Toggle SD clock again */
1191 err
= rtsx_pci_write_register(pcr
, SD_BUS_STAT
, 0xFF, SD_CLK_TOGGLE_EN
);
1195 /* Wait for a period of time so that the card can drive
1196 * SD_DAT[3:0] to high at 1.8V
1200 /* SD_CMD, SD_DAT[3:0] should be pulled high by host */
1201 err
= rtsx_pci_read_register(pcr
, SD_BUS_STAT
, &stat
);
1205 mask
= SD_CMD_STATUS
| SD_DAT3_STATUS
| SD_DAT2_STATUS
|
1206 SD_DAT1_STATUS
| SD_DAT0_STATUS
;
1207 val
= SD_CMD_STATUS
| SD_DAT3_STATUS
| SD_DAT2_STATUS
|
1208 SD_DAT1_STATUS
| SD_DAT0_STATUS
;
1209 if ((stat
& mask
) != val
) {
1210 dev_dbg(sdmmc_dev(host
),
1211 "%s: SD_BUS_STAT = 0x%x\n", __func__
, stat
);
1212 rtsx_pci_write_register(pcr
, SD_BUS_STAT
,
1213 SD_CLK_TOGGLE_EN
| SD_CLK_FORCE_STOP
, 0);
1214 rtsx_pci_write_register(pcr
, CARD_CLK_EN
, 0xFF, 0);
1221 static int sdmmc_switch_voltage(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
1223 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
1224 struct rtsx_pcr
*pcr
= host
->pcr
;
1228 dev_dbg(sdmmc_dev(host
), "%s: signal_voltage = %d\n",
1229 __func__
, ios
->signal_voltage
);
1234 err
= rtsx_pci_card_exclusive_check(host
->pcr
, RTSX_SD_CARD
);
1238 mutex_lock(&pcr
->pcr_mutex
);
1240 rtsx_pci_start_run(pcr
);
1242 if (ios
->signal_voltage
== MMC_SIGNAL_VOLTAGE_330
)
1243 voltage
= OUTPUT_3V3
;
1245 voltage
= OUTPUT_1V8
;
1247 if (voltage
== OUTPUT_1V8
) {
1248 err
= sd_wait_voltage_stable_1(host
);
1253 err
= rtsx_pci_switch_output_voltage(pcr
, voltage
);
1257 if (voltage
== OUTPUT_1V8
) {
1258 err
= sd_wait_voltage_stable_2(host
);
1264 /* Stop toggle SD clock in idle */
1265 err
= rtsx_pci_write_register(pcr
, SD_BUS_STAT
,
1266 SD_CLK_TOGGLE_EN
| SD_CLK_FORCE_STOP
, 0);
1268 mutex_unlock(&pcr
->pcr_mutex
);
1273 static int sdmmc_execute_tuning(struct mmc_host
*mmc
, u32 opcode
)
1275 struct realtek_pci_sdmmc
*host
= mmc_priv(mmc
);
1276 struct rtsx_pcr
*pcr
= host
->pcr
;
1282 err
= rtsx_pci_card_exclusive_check(host
->pcr
, RTSX_SD_CARD
);
1286 mutex_lock(&pcr
->pcr_mutex
);
1288 rtsx_pci_start_run(pcr
);
1290 /* Set initial TX phase */
1291 switch (mmc
->ios
.timing
) {
1292 case MMC_TIMING_UHS_SDR104
:
1293 err
= sd_change_phase(host
, SDR104_TX_PHASE(pcr
), false);
1296 case MMC_TIMING_UHS_SDR50
:
1297 err
= sd_change_phase(host
, SDR50_TX_PHASE(pcr
), false);
1300 case MMC_TIMING_UHS_DDR50
:
1301 err
= sd_change_phase(host
, DDR50_TX_PHASE(pcr
), false);
1311 /* Tuning RX phase */
1312 if ((mmc
->ios
.timing
== MMC_TIMING_UHS_SDR104
) ||
1313 (mmc
->ios
.timing
== MMC_TIMING_UHS_SDR50
))
1314 err
= sd_tuning_rx(host
, opcode
);
1315 else if (mmc
->ios
.timing
== MMC_TIMING_UHS_DDR50
)
1316 err
= sd_change_phase(host
, DDR50_RX_PHASE(pcr
), true);
1319 mutex_unlock(&pcr
->pcr_mutex
);
1324 static const struct mmc_host_ops realtek_pci_sdmmc_ops
= {
1325 .pre_req
= sdmmc_pre_req
,
1326 .post_req
= sdmmc_post_req
,
1327 .request
= sdmmc_request
,
1328 .set_ios
= sdmmc_set_ios
,
1329 .get_ro
= sdmmc_get_ro
,
1330 .get_cd
= sdmmc_get_cd
,
1331 .start_signal_voltage_switch
= sdmmc_switch_voltage
,
1332 .execute_tuning
= sdmmc_execute_tuning
,
1335 static void init_extra_caps(struct realtek_pci_sdmmc
*host
)
1337 struct mmc_host
*mmc
= host
->mmc
;
1338 struct rtsx_pcr
*pcr
= host
->pcr
;
1340 dev_dbg(sdmmc_dev(host
), "pcr->extra_caps = 0x%x\n", pcr
->extra_caps
);
1342 if (pcr
->extra_caps
& EXTRA_CAPS_SD_SDR50
)
1343 mmc
->caps
|= MMC_CAP_UHS_SDR50
;
1344 if (pcr
->extra_caps
& EXTRA_CAPS_SD_SDR104
)
1345 mmc
->caps
|= MMC_CAP_UHS_SDR104
;
1346 if (pcr
->extra_caps
& EXTRA_CAPS_SD_DDR50
)
1347 mmc
->caps
|= MMC_CAP_UHS_DDR50
;
1348 if (pcr
->extra_caps
& EXTRA_CAPS_MMC_HSDDR
)
1349 mmc
->caps
|= MMC_CAP_1_8V_DDR
;
1350 if (pcr
->extra_caps
& EXTRA_CAPS_MMC_8BIT
)
1351 mmc
->caps
|= MMC_CAP_8_BIT_DATA
;
1354 static void realtek_init_host(struct realtek_pci_sdmmc
*host
)
1356 struct mmc_host
*mmc
= host
->mmc
;
1358 mmc
->f_min
= 250000;
1359 mmc
->f_max
= 208000000;
1360 mmc
->ocr_avail
= MMC_VDD_32_33
| MMC_VDD_33_34
| MMC_VDD_165_195
;
1361 mmc
->caps
= MMC_CAP_4_BIT_DATA
| MMC_CAP_SD_HIGHSPEED
|
1362 MMC_CAP_MMC_HIGHSPEED
| MMC_CAP_BUS_WIDTH_TEST
|
1363 MMC_CAP_UHS_SDR12
| MMC_CAP_UHS_SDR25
| MMC_CAP_ERASE
;
1364 mmc
->caps2
= MMC_CAP2_NO_PRESCAN_POWERUP
| MMC_CAP2_FULL_PWR_CYCLE
;
1365 mmc
->max_current_330
= 400;
1366 mmc
->max_current_180
= 800;
1367 mmc
->ops
= &realtek_pci_sdmmc_ops
;
1369 init_extra_caps(host
);
1371 mmc
->max_segs
= 256;
1372 mmc
->max_seg_size
= 65536;
1373 mmc
->max_blk_size
= 512;
1374 mmc
->max_blk_count
= 65535;
1375 mmc
->max_req_size
= 524288;
1378 static void rtsx_pci_sdmmc_card_event(struct platform_device
*pdev
)
1380 struct realtek_pci_sdmmc
*host
= platform_get_drvdata(pdev
);
1383 mmc_detect_change(host
->mmc
, 0);
1386 static int rtsx_pci_sdmmc_drv_probe(struct platform_device
*pdev
)
1388 struct mmc_host
*mmc
;
1389 struct realtek_pci_sdmmc
*host
;
1390 struct rtsx_pcr
*pcr
;
1391 struct pcr_handle
*handle
= pdev
->dev
.platform_data
;
1400 dev_dbg(&(pdev
->dev
), ": Realtek PCI-E SDMMC controller found\n");
1402 mmc
= mmc_alloc_host(sizeof(*host
), &pdev
->dev
);
1406 host
= mmc_priv(mmc
);
1407 host
->workq
= create_singlethread_workqueue(SDMMC_WORKQ_NAME
);
1416 host
->power_state
= SDMMC_POWER_OFF
;
1417 INIT_WORK(&host
->work
, sd_request
);
1418 platform_set_drvdata(pdev
, host
);
1419 pcr
->slots
[RTSX_SD_CARD
].p_dev
= pdev
;
1420 pcr
->slots
[RTSX_SD_CARD
].card_event
= rtsx_pci_sdmmc_card_event
;
1422 mutex_init(&host
->host_mutex
);
1424 realtek_init_host(host
);
1431 static int rtsx_pci_sdmmc_drv_remove(struct platform_device
*pdev
)
1433 struct realtek_pci_sdmmc
*host
= platform_get_drvdata(pdev
);
1434 struct rtsx_pcr
*pcr
;
1435 struct mmc_host
*mmc
;
1441 pcr
->slots
[RTSX_SD_CARD
].p_dev
= NULL
;
1442 pcr
->slots
[RTSX_SD_CARD
].card_event
= NULL
;
1445 cancel_work_sync(&host
->work
);
1447 mutex_lock(&host
->host_mutex
);
1449 dev_dbg(&(pdev
->dev
),
1450 "%s: Controller removed during transfer\n",
1453 rtsx_pci_complete_unfinished_transfer(pcr
);
1455 host
->mrq
->cmd
->error
= -ENOMEDIUM
;
1456 if (host
->mrq
->stop
)
1457 host
->mrq
->stop
->error
= -ENOMEDIUM
;
1458 mmc_request_done(mmc
, host
->mrq
);
1460 mutex_unlock(&host
->host_mutex
);
1462 mmc_remove_host(mmc
);
1465 flush_workqueue(host
->workq
);
1466 destroy_workqueue(host
->workq
);
1471 dev_dbg(&(pdev
->dev
),
1472 ": Realtek PCI-E SDMMC controller has been removed\n");
1477 static const struct platform_device_id rtsx_pci_sdmmc_ids
[] = {
1479 .name
= DRV_NAME_RTSX_PCI_SDMMC
,
1484 MODULE_DEVICE_TABLE(platform
, rtsx_pci_sdmmc_ids
);
1486 static struct platform_driver rtsx_pci_sdmmc_driver
= {
1487 .probe
= rtsx_pci_sdmmc_drv_probe
,
1488 .remove
= rtsx_pci_sdmmc_drv_remove
,
1489 .id_table
= rtsx_pci_sdmmc_ids
,
1491 .name
= DRV_NAME_RTSX_PCI_SDMMC
,
1494 module_platform_driver(rtsx_pci_sdmmc_driver
);
1496 MODULE_LICENSE("GPL");
1497 MODULE_AUTHOR("Wei WANG <wei_wang@realsil.com.cn>");
1498 MODULE_DESCRIPTION("Realtek PCI-E SD/MMC Card Host Driver");