1 /* Realtek USB 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 version 2
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, see <http://www.gnu.org/licenses/>.
18 * Roger Tseng <rogerable@realtek.com>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/delay.h>
24 #include <linux/platform_device.h>
25 #include <linux/usb.h>
26 #include <linux/mmc/host.h>
27 #include <linux/mmc/mmc.h>
28 #include <linux/mmc/sd.h>
29 #include <linux/mmc/sdio.h>
30 #include <linux/mmc/card.h>
31 #include <linux/scatterlist.h>
32 #include <linux/pm_runtime.h>
34 #include <linux/mfd/rtsx_usb.h>
35 #include <asm/unaligned.h>
37 #if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
38 defined(CONFIG_MMC_REALTEK_USB_MODULE))
39 #include <linux/leds.h>
40 #include <linux/workqueue.h>
41 #define RTSX_USB_USE_LEDS_CLASS
44 struct rtsx_usb_sdmmc
{
45 struct platform_device
*pdev
;
48 struct mmc_request
*mrq
;
50 struct mutex host_mutex
;
61 unsigned char power_mode
;
63 #ifdef RTSX_USB_USE_LEDS_CLASS
64 struct led_classdev led
;
66 struct work_struct led_work
;
70 static inline struct device
*sdmmc_dev(struct rtsx_usb_sdmmc
*host
)
72 return &(host
->pdev
->dev
);
75 static inline void sd_clear_error(struct rtsx_usb_sdmmc
*host
)
77 struct rtsx_ucr
*ucr
= host
->ucr
;
78 rtsx_usb_ep0_write_register(ucr
, CARD_STOP
,
80 SD_STOP
| SD_CLR_ERR
);
82 rtsx_usb_clear_dma_err(ucr
);
83 rtsx_usb_clear_fsm_err(ucr
);
87 static void sd_print_debug_regs(struct rtsx_usb_sdmmc
*host
)
89 struct rtsx_ucr
*ucr
= host
->ucr
;
92 rtsx_usb_ep0_read_register(ucr
, SD_STAT1
, &val
);
93 dev_dbg(sdmmc_dev(host
), "SD_STAT1: 0x%x\n", val
);
94 rtsx_usb_ep0_read_register(ucr
, SD_STAT2
, &val
);
95 dev_dbg(sdmmc_dev(host
), "SD_STAT2: 0x%x\n", val
);
96 rtsx_usb_ep0_read_register(ucr
, SD_BUS_STAT
, &val
);
97 dev_dbg(sdmmc_dev(host
), "SD_BUS_STAT: 0x%x\n", val
);
100 #define sd_print_debug_regs(host)
103 static int sd_read_data(struct rtsx_usb_sdmmc
*host
, struct mmc_command
*cmd
,
104 u16 byte_cnt
, u8
*buf
, int buf_len
, int timeout
)
106 struct rtsx_ucr
*ucr
= host
->ucr
;
113 rtsx_usb_init_cmd(ucr
);
115 dev_dbg(sdmmc_dev(host
), "%s: SD/MMC CMD%d\n", __func__
117 if (cmd
->opcode
== MMC_SEND_TUNING_BLOCK
)
118 trans_mode
= SD_TM_AUTO_TUNING
;
120 trans_mode
= SD_TM_NORMAL_READ
;
122 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
,
123 SD_CMD0
, 0xFF, (u8
)(cmd
->opcode
) | 0x40);
124 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
,
125 SD_CMD1
, 0xFF, (u8
)(cmd
->arg
>> 24));
126 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
,
127 SD_CMD2
, 0xFF, (u8
)(cmd
->arg
>> 16));
128 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
,
129 SD_CMD3
, 0xFF, (u8
)(cmd
->arg
>> 8));
130 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
,
131 SD_CMD4
, 0xFF, (u8
)cmd
->arg
);
133 trans_mode
= SD_TM_AUTO_READ_3
;
136 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, SD_BYTE_CNT_L
, 0xFF, (u8
)byte_cnt
);
137 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, SD_BYTE_CNT_H
,
138 0xFF, (u8
)(byte_cnt
>> 8));
139 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, SD_BLOCK_CNT_L
, 0xFF, 1);
140 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, SD_BLOCK_CNT_H
, 0xFF, 0);
142 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, SD_CFG2
, 0xFF,
143 SD_CALCULATE_CRC7
| SD_CHECK_CRC16
|
144 SD_NO_WAIT_BUSY_END
| SD_CHECK_CRC7
| SD_RSP_LEN_6
);
145 if (trans_mode
!= SD_TM_AUTO_TUNING
)
146 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
,
147 CARD_DATA_SOURCE
, 0x01, PINGPONG_BUFFER
);
149 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, SD_TRANSFER
,
150 0xFF, trans_mode
| SD_TRANSFER_START
);
151 rtsx_usb_add_cmd(ucr
, CHECK_REG_CMD
, SD_TRANSFER
,
152 SD_TRANSFER_END
, SD_TRANSFER_END
);
155 rtsx_usb_add_cmd(ucr
, READ_REG_CMD
, SD_CMD1
, 0, 0);
156 rtsx_usb_add_cmd(ucr
, READ_REG_CMD
, SD_CMD2
, 0, 0);
157 rtsx_usb_add_cmd(ucr
, READ_REG_CMD
, SD_CMD3
, 0, 0);
158 rtsx_usb_add_cmd(ucr
, READ_REG_CMD
, SD_CMD4
, 0, 0);
161 err
= rtsx_usb_send_cmd(ucr
, MODE_CR
, timeout
);
163 dev_dbg(sdmmc_dev(host
),
164 "rtsx_usb_send_cmd failed (err = %d)\n", err
);
168 err
= rtsx_usb_get_rsp(ucr
, !cmd
? 1 : 5, timeout
);
169 if (err
|| (ucr
->rsp_buf
[0] & SD_TRANSFER_ERR
)) {
170 sd_print_debug_regs(host
);
173 dev_dbg(sdmmc_dev(host
),
174 "Transfer failed (SD_TRANSFER = %02x)\n",
178 dev_dbg(sdmmc_dev(host
),
179 "rtsx_usb_get_rsp failed (err = %d)\n", err
);
186 cmd
->resp
[0] = get_unaligned_be32(ucr
->rsp_buf
+ 1);
187 dev_dbg(sdmmc_dev(host
), "cmd->resp[0] = 0x%08x\n",
191 if (buf
&& buf_len
) {
192 /* 2-byte aligned part */
193 err
= rtsx_usb_read_ppbuf(ucr
, buf
, byte_cnt
- (byte_cnt
% 2));
195 dev_dbg(sdmmc_dev(host
),
196 "rtsx_usb_read_ppbuf failed (err = %d)\n", err
);
202 return rtsx_usb_read_register(ucr
,
203 PPBUF_BASE2
+ byte_cnt
,
210 static int sd_write_data(struct rtsx_usb_sdmmc
*host
, struct mmc_command
*cmd
,
211 u16 byte_cnt
, u8
*buf
, int buf_len
, int timeout
)
213 struct rtsx_ucr
*ucr
= host
->ucr
;
220 if (buf
&& buf_len
) {
221 err
= rtsx_usb_write_ppbuf(ucr
, buf
, buf_len
);
223 dev_dbg(sdmmc_dev(host
),
224 "rtsx_usb_write_ppbuf failed (err = %d)\n",
230 trans_mode
= (cmd
!= NULL
) ? SD_TM_AUTO_WRITE_2
: SD_TM_AUTO_WRITE_3
;
231 rtsx_usb_init_cmd(ucr
);
234 dev_dbg(sdmmc_dev(host
), "%s: SD/MMC CMD%d\n", __func__
,
236 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
,
237 SD_CMD0
, 0xFF, (u8
)(cmd
->opcode
) | 0x40);
238 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
,
239 SD_CMD1
, 0xFF, (u8
)(cmd
->arg
>> 24));
240 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
,
241 SD_CMD2
, 0xFF, (u8
)(cmd
->arg
>> 16));
242 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
,
243 SD_CMD3
, 0xFF, (u8
)(cmd
->arg
>> 8));
244 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
,
245 SD_CMD4
, 0xFF, (u8
)cmd
->arg
);
248 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, SD_BYTE_CNT_L
, 0xFF, (u8
)byte_cnt
);
249 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, SD_BYTE_CNT_H
,
250 0xFF, (u8
)(byte_cnt
>> 8));
251 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, SD_BLOCK_CNT_L
, 0xFF, 1);
252 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, SD_BLOCK_CNT_H
, 0xFF, 0);
254 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, SD_CFG2
, 0xFF,
255 SD_CALCULATE_CRC7
| SD_CHECK_CRC16
|
256 SD_NO_WAIT_BUSY_END
| SD_CHECK_CRC7
| SD_RSP_LEN_6
);
257 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
,
258 CARD_DATA_SOURCE
, 0x01, PINGPONG_BUFFER
);
260 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, SD_TRANSFER
, 0xFF,
261 trans_mode
| SD_TRANSFER_START
);
262 rtsx_usb_add_cmd(ucr
, CHECK_REG_CMD
, SD_TRANSFER
,
263 SD_TRANSFER_END
, SD_TRANSFER_END
);
266 rtsx_usb_add_cmd(ucr
, READ_REG_CMD
, SD_CMD1
, 0, 0);
267 rtsx_usb_add_cmd(ucr
, READ_REG_CMD
, SD_CMD2
, 0, 0);
268 rtsx_usb_add_cmd(ucr
, READ_REG_CMD
, SD_CMD3
, 0, 0);
269 rtsx_usb_add_cmd(ucr
, READ_REG_CMD
, SD_CMD4
, 0, 0);
272 err
= rtsx_usb_send_cmd(ucr
, MODE_CR
, timeout
);
274 dev_dbg(sdmmc_dev(host
),
275 "rtsx_usb_send_cmd failed (err = %d)\n", err
);
279 err
= rtsx_usb_get_rsp(ucr
, !cmd
? 1 : 5, timeout
);
281 sd_print_debug_regs(host
);
282 dev_dbg(sdmmc_dev(host
),
283 "rtsx_usb_get_rsp failed (err = %d)\n", err
);
288 cmd
->resp
[0] = get_unaligned_be32(ucr
->rsp_buf
+ 1);
289 dev_dbg(sdmmc_dev(host
), "cmd->resp[0] = 0x%08x\n",
296 static void sd_send_cmd_get_rsp(struct rtsx_usb_sdmmc
*host
,
297 struct mmc_command
*cmd
)
299 struct rtsx_ucr
*ucr
= host
->ucr
;
300 u8 cmd_idx
= (u8
)cmd
->opcode
;
310 dev_dbg(sdmmc_dev(host
), "%s: SD/MMC CMD %d, arg = 0x%08x\n",
311 __func__
, cmd_idx
, arg
);
320 switch (mmc_resp_type(cmd
)) {
322 rsp_type
= SD_RSP_TYPE_R0
;
325 rsp_type
= SD_RSP_TYPE_R1
;
327 case MMC_RSP_R1
& ~MMC_RSP_CRC
:
328 rsp_type
= SD_RSP_TYPE_R1
| SD_NO_CHECK_CRC7
;
331 rsp_type
= SD_RSP_TYPE_R1b
;
334 rsp_type
= SD_RSP_TYPE_R2
;
337 rsp_type
= SD_RSP_TYPE_R3
;
340 dev_dbg(sdmmc_dev(host
), "cmd->flag is not valid\n");
345 if (rsp_type
== SD_RSP_TYPE_R1b
)
348 if (cmd
->opcode
== SD_SWITCH_VOLTAGE
) {
349 err
= rtsx_usb_write_register(ucr
, SD_BUS_STAT
,
350 SD_CLK_TOGGLE_EN
| SD_CLK_FORCE_STOP
,
356 rtsx_usb_init_cmd(ucr
);
358 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, SD_CMD0
, 0xFF, 0x40 | cmd_idx
);
359 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, SD_CMD1
, 0xFF, (u8
)(arg
>> 24));
360 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, SD_CMD2
, 0xFF, (u8
)(arg
>> 16));
361 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, SD_CMD3
, 0xFF, (u8
)(arg
>> 8));
362 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, SD_CMD4
, 0xFF, (u8
)arg
);
364 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, SD_CFG2
, 0xFF, rsp_type
);
365 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_DATA_SOURCE
,
366 0x01, PINGPONG_BUFFER
);
367 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, SD_TRANSFER
,
368 0xFF, SD_TM_CMD_RSP
| SD_TRANSFER_START
);
369 rtsx_usb_add_cmd(ucr
, CHECK_REG_CMD
, SD_TRANSFER
,
370 SD_TRANSFER_END
| SD_STAT_IDLE
,
371 SD_TRANSFER_END
| SD_STAT_IDLE
);
373 if (rsp_type
== SD_RSP_TYPE_R2
) {
374 /* Read data from ping-pong buffer */
375 for (i
= PPBUF_BASE2
; i
< PPBUF_BASE2
+ 16; i
++)
376 rtsx_usb_add_cmd(ucr
, READ_REG_CMD
, (u16
)i
, 0, 0);
378 } else if (rsp_type
!= SD_RSP_TYPE_R0
) {
379 /* Read data from SD_CMDx registers */
380 for (i
= SD_CMD0
; i
<= SD_CMD4
; i
++)
381 rtsx_usb_add_cmd(ucr
, READ_REG_CMD
, (u16
)i
, 0, 0);
386 rtsx_usb_add_cmd(ucr
, READ_REG_CMD
, SD_STAT1
, 0, 0);
388 err
= rtsx_usb_send_cmd(ucr
, MODE_CR
, 100);
390 dev_dbg(sdmmc_dev(host
),
391 "rtsx_usb_send_cmd error (err = %d)\n", err
);
395 err
= rtsx_usb_get_rsp(ucr
, len
, timeout
);
396 if (err
|| (ucr
->rsp_buf
[0] & SD_TRANSFER_ERR
)) {
397 sd_print_debug_regs(host
);
398 sd_clear_error(host
);
401 dev_dbg(sdmmc_dev(host
),
402 "Transfer failed (SD_TRANSFER = %02x)\n",
406 dev_dbg(sdmmc_dev(host
),
407 "rtsx_usb_get_rsp failed (err = %d)\n", err
);
413 if (rsp_type
== SD_RSP_TYPE_R0
) {
418 /* Skip result of CHECK_REG_CMD */
419 ptr
= ucr
->rsp_buf
+ 1;
421 /* Check (Start,Transmission) bit of Response */
422 if ((ptr
[0] & 0xC0) != 0) {
424 dev_dbg(sdmmc_dev(host
), "Invalid response bit\n");
429 if (!(rsp_type
& SD_NO_CHECK_CRC7
)) {
430 if (ptr
[stat_idx
] & SD_CRC7_ERR
) {
432 dev_dbg(sdmmc_dev(host
), "CRC7 error\n");
437 if (rsp_type
== SD_RSP_TYPE_R2
) {
439 * The controller offloads the last byte {CRC-7, end bit 1'b1}
440 * of response type R2. Assign dummy CRC, 0, and end bit to the
441 * byte(ptr[16], goes into the LSB of resp[3] later).
445 for (i
= 0; i
< 4; i
++) {
446 cmd
->resp
[i
] = get_unaligned_be32(ptr
+ 1 + i
* 4);
447 dev_dbg(sdmmc_dev(host
), "cmd->resp[%d] = 0x%08x\n",
451 cmd
->resp
[0] = get_unaligned_be32(ptr
+ 1);
452 dev_dbg(sdmmc_dev(host
), "cmd->resp[0] = 0x%08x\n",
460 static int sd_rw_multi(struct rtsx_usb_sdmmc
*host
, struct mmc_request
*mrq
)
462 struct rtsx_ucr
*ucr
= host
->ucr
;
463 struct mmc_data
*data
= mrq
->data
;
464 int read
= (data
->flags
& MMC_DATA_READ
) ? 1 : 0;
468 size_t data_len
= data
->blksz
* data
->blocks
;
472 dev_dbg(sdmmc_dev(host
), "%s: read %zu bytes\n",
474 cfg2
= SD_CALCULATE_CRC7
| SD_CHECK_CRC16
|
475 SD_NO_WAIT_BUSY_END
| SD_CHECK_CRC7
| SD_RSP_LEN_0
;
476 trans_mode
= SD_TM_AUTO_READ_3
;
478 dev_dbg(sdmmc_dev(host
), "%s: write %zu bytes\n",
480 cfg2
= SD_NO_CALCULATE_CRC7
| SD_CHECK_CRC16
|
481 SD_NO_WAIT_BUSY_END
| SD_NO_CHECK_CRC7
| SD_RSP_LEN_0
;
482 trans_mode
= SD_TM_AUTO_WRITE_3
;
485 rtsx_usb_init_cmd(ucr
);
487 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, SD_BYTE_CNT_L
, 0xFF, 0x00);
488 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, SD_BYTE_CNT_H
, 0xFF, 0x02);
489 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, SD_BLOCK_CNT_L
,
490 0xFF, (u8
)data
->blocks
);
491 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, SD_BLOCK_CNT_H
,
492 0xFF, (u8
)(data
->blocks
>> 8));
494 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_DATA_SOURCE
,
497 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, MC_DMA_TC3
,
498 0xFF, (u8
)(data_len
>> 24));
499 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, MC_DMA_TC2
,
500 0xFF, (u8
)(data_len
>> 16));
501 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, MC_DMA_TC1
,
502 0xFF, (u8
)(data_len
>> 8));
503 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, MC_DMA_TC0
,
507 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, MC_DMA_CTL
,
508 0x03 | DMA_PACK_SIZE_MASK
,
509 DMA_DIR_FROM_CARD
| DMA_EN
| DMA_512
);
512 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, MC_DMA_CTL
,
513 0x03 | DMA_PACK_SIZE_MASK
,
514 DMA_DIR_TO_CARD
| DMA_EN
| DMA_512
);
517 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, SD_CFG2
, 0xFF, cfg2
);
518 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, SD_TRANSFER
, 0xFF,
519 trans_mode
| SD_TRANSFER_START
);
520 rtsx_usb_add_cmd(ucr
, CHECK_REG_CMD
, SD_TRANSFER
,
521 SD_TRANSFER_END
, SD_TRANSFER_END
);
523 err
= rtsx_usb_send_cmd(ucr
, flag
, 100);
528 pipe
= usb_rcvbulkpipe(ucr
->pusb_dev
, EP_BULK_IN
);
530 pipe
= usb_sndbulkpipe(ucr
->pusb_dev
, EP_BULK_OUT
);
532 err
= rtsx_usb_transfer_data(ucr
, pipe
, data
->sg
, data_len
,
533 data
->sg_len
, NULL
, 10000);
535 dev_dbg(sdmmc_dev(host
), "rtsx_usb_transfer_data error %d\n"
537 sd_clear_error(host
);
541 return rtsx_usb_get_rsp(ucr
, 1, 2000);
544 static inline void sd_enable_initial_mode(struct rtsx_usb_sdmmc
*host
)
546 rtsx_usb_write_register(host
->ucr
, SD_CFG1
,
547 SD_CLK_DIVIDE_MASK
, SD_CLK_DIVIDE_128
);
550 static inline void sd_disable_initial_mode(struct rtsx_usb_sdmmc
*host
)
552 rtsx_usb_write_register(host
->ucr
, SD_CFG1
,
553 SD_CLK_DIVIDE_MASK
, SD_CLK_DIVIDE_0
);
556 static void sd_normal_rw(struct rtsx_usb_sdmmc
*host
,
557 struct mmc_request
*mrq
)
559 struct mmc_command
*cmd
= mrq
->cmd
;
560 struct mmc_data
*data
= mrq
->data
;
563 buf
= kzalloc(data
->blksz
, GFP_NOIO
);
565 cmd
->error
= -ENOMEM
;
569 if (data
->flags
& MMC_DATA_READ
) {
570 if (host
->initial_mode
)
571 sd_disable_initial_mode(host
);
573 cmd
->error
= sd_read_data(host
, cmd
, (u16
)data
->blksz
, buf
,
576 if (host
->initial_mode
)
577 sd_enable_initial_mode(host
);
579 sg_copy_from_buffer(data
->sg
, data
->sg_len
, buf
, data
->blksz
);
581 sg_copy_to_buffer(data
->sg
, data
->sg_len
, buf
, data
->blksz
);
583 cmd
->error
= sd_write_data(host
, cmd
, (u16
)data
->blksz
, buf
,
590 static int sd_change_phase(struct rtsx_usb_sdmmc
*host
, u8 sample_point
, int tx
)
592 struct rtsx_ucr
*ucr
= host
->ucr
;
595 dev_dbg(sdmmc_dev(host
), "%s: %s sample_point = %d\n",
596 __func__
, tx
? "TX" : "RX", sample_point
);
598 rtsx_usb_init_cmd(ucr
);
600 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CLK_DIV
, CLK_CHANGE
, CLK_CHANGE
);
603 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, SD_VPCLK0_CTL
,
606 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, SD_VPCLK1_CTL
,
609 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, SD_VPCLK0_CTL
, PHASE_NOT_RESET
, 0);
610 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, SD_VPCLK0_CTL
,
611 PHASE_NOT_RESET
, PHASE_NOT_RESET
);
612 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CLK_DIV
, CLK_CHANGE
, 0);
613 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, SD_CFG1
, SD_ASYNC_FIFO_RST
, 0);
615 err
= rtsx_usb_send_cmd(ucr
, MODE_C
, 100);
622 static inline u32
get_phase_point(u32 phase_map
, unsigned int idx
)
625 return phase_map
& (1 << idx
);
628 static int get_phase_len(u32 phase_map
, unsigned int idx
)
632 for (i
= 0; i
< MAX_PHASE
+ 1; i
++) {
633 if (get_phase_point(phase_map
, idx
+ i
) == 0)
636 return MAX_PHASE
+ 1;
639 static u8
sd_search_final_phase(struct rtsx_usb_sdmmc
*host
, u32 phase_map
)
641 int start
= 0, len
= 0;
642 int start_final
= 0, len_final
= 0;
643 u8 final_phase
= 0xFF;
645 if (phase_map
== 0) {
646 dev_dbg(sdmmc_dev(host
), "Phase: [map:%x]\n", phase_map
);
650 while (start
< MAX_PHASE
+ 1) {
651 len
= get_phase_len(phase_map
, start
);
652 if (len_final
< len
) {
656 start
+= len
? len
: 1;
659 final_phase
= (start_final
+ len_final
/ 2) & MAX_PHASE
;
660 dev_dbg(sdmmc_dev(host
), "Phase: [map:%x] [maxlen:%d] [final:%d]\n",
661 phase_map
, len_final
, final_phase
);
666 static void sd_wait_data_idle(struct rtsx_usb_sdmmc
*host
)
671 for (i
= 0; i
< 100; i
++) {
672 err
= rtsx_usb_ep0_read_register(host
->ucr
,
673 SD_DATA_STATE
, &val
);
674 if (val
& SD_DATA_IDLE
)
677 usleep_range(100, 1000);
681 static int sd_tuning_rx_cmd(struct rtsx_usb_sdmmc
*host
,
682 u8 opcode
, u8 sample_point
)
685 struct mmc_command cmd
= {0};
687 err
= sd_change_phase(host
, sample_point
, 0);
691 cmd
.opcode
= MMC_SEND_TUNING_BLOCK
;
692 err
= sd_read_data(host
, &cmd
, 0x40, NULL
, 0, 100);
694 /* Wait till SD DATA IDLE */
695 sd_wait_data_idle(host
);
696 sd_clear_error(host
);
703 static void sd_tuning_phase(struct rtsx_usb_sdmmc
*host
,
704 u8 opcode
, u16
*phase_map
)
707 u16 raw_phase_map
= 0;
709 for (i
= MAX_PHASE
; i
>= 0; i
--) {
710 err
= sd_tuning_rx_cmd(host
, opcode
, (u8
)i
);
712 raw_phase_map
|= 1 << i
;
716 *phase_map
= raw_phase_map
;
719 static int sd_tuning_rx(struct rtsx_usb_sdmmc
*host
, u8 opcode
)
722 u16 raw_phase_map
[RX_TUNING_CNT
] = {0}, phase_map
;
725 /* setting fixed default TX phase */
726 err
= sd_change_phase(host
, 0x01, 1);
728 dev_dbg(sdmmc_dev(host
), "TX phase setting failed\n");
732 /* tuning RX phase */
733 for (i
= 0; i
< RX_TUNING_CNT
; i
++) {
734 sd_tuning_phase(host
, opcode
, &(raw_phase_map
[i
]));
736 if (raw_phase_map
[i
] == 0)
741 for (i
= 0; i
< RX_TUNING_CNT
; i
++) {
742 dev_dbg(sdmmc_dev(host
), "RX raw_phase_map[%d] = 0x%04x\n",
743 i
, raw_phase_map
[i
]);
744 phase_map
&= raw_phase_map
[i
];
746 dev_dbg(sdmmc_dev(host
), "RX phase_map = 0x%04x\n", phase_map
);
749 final_phase
= sd_search_final_phase(host
, phase_map
);
750 if (final_phase
== 0xFF)
753 err
= sd_change_phase(host
, final_phase
, 0);
763 static int sdmmc_get_ro(struct mmc_host
*mmc
)
765 struct rtsx_usb_sdmmc
*host
= mmc_priv(mmc
);
766 struct rtsx_ucr
*ucr
= host
->ucr
;
770 if (host
->host_removal
)
773 mutex_lock(&ucr
->dev_mutex
);
775 /* Check SD card detect */
776 err
= rtsx_usb_get_card_status(ucr
, &val
);
778 mutex_unlock(&ucr
->dev_mutex
);
781 /* Treat failed detection as non-ro */
791 static int sdmmc_get_cd(struct mmc_host
*mmc
)
793 struct rtsx_usb_sdmmc
*host
= mmc_priv(mmc
);
794 struct rtsx_ucr
*ucr
= host
->ucr
;
798 if (host
->host_removal
)
801 mutex_lock(&ucr
->dev_mutex
);
803 /* Check SD card detect */
804 err
= rtsx_usb_get_card_status(ucr
, &val
);
806 mutex_unlock(&ucr
->dev_mutex
);
808 /* Treat failed detection as non-exist */
813 host
->card_exist
= true;
818 host
->card_exist
= false;
822 static void sdmmc_request(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
824 struct rtsx_usb_sdmmc
*host
= mmc_priv(mmc
);
825 struct rtsx_ucr
*ucr
= host
->ucr
;
826 struct mmc_command
*cmd
= mrq
->cmd
;
827 struct mmc_data
*data
= mrq
->data
;
828 unsigned int data_size
= 0;
830 dev_dbg(sdmmc_dev(host
), "%s\n", __func__
);
832 if (host
->host_removal
) {
833 cmd
->error
= -ENOMEDIUM
;
837 if ((!host
->card_exist
)) {
838 cmd
->error
= -ENOMEDIUM
;
839 goto finish_detect_card
;
843 * Reject SDIO CMDs to speed up card identification
846 if (cmd
->opcode
== SD_IO_SEND_OP_COND
||
847 cmd
->opcode
== SD_IO_RW_DIRECT
||
848 cmd
->opcode
== SD_IO_RW_EXTENDED
) {
849 cmd
->error
= -EINVAL
;
853 mutex_lock(&ucr
->dev_mutex
);
855 mutex_lock(&host
->host_mutex
);
857 mutex_unlock(&host
->host_mutex
);
860 data_size
= data
->blocks
* data
->blksz
;
863 sd_send_cmd_get_rsp(host
, cmd
);
864 } else if ((!(data_size
% 512) && cmd
->opcode
!= MMC_SEND_EXT_CSD
) ||
865 mmc_op_multi(cmd
->opcode
)) {
866 sd_send_cmd_get_rsp(host
, cmd
);
869 sd_rw_multi(host
, mrq
);
871 if (mmc_op_multi(cmd
->opcode
) && mrq
->stop
) {
872 sd_send_cmd_get_rsp(host
, mrq
->stop
);
873 rtsx_usb_write_register(ucr
, MC_FIFO_CTL
,
874 FIFO_FLUSH
, FIFO_FLUSH
);
878 sd_normal_rw(host
, mrq
);
882 if (cmd
->error
|| data
->error
)
883 data
->bytes_xfered
= 0;
885 data
->bytes_xfered
= data
->blocks
* data
->blksz
;
888 mutex_unlock(&ucr
->dev_mutex
);
893 * detect card when fail to update card existence state and
894 * speed up card removal when retry
897 dev_dbg(sdmmc_dev(host
), "cmd->error = %d\n", cmd
->error
);
901 mutex_lock(&host
->host_mutex
);
903 mutex_unlock(&host
->host_mutex
);
905 mmc_request_done(mmc
, mrq
);
908 static int sd_set_bus_width(struct rtsx_usb_sdmmc
*host
,
909 unsigned char bus_width
)
913 [MMC_BUS_WIDTH_1
] = SD_BUS_WIDTH_1BIT
,
914 [MMC_BUS_WIDTH_4
] = SD_BUS_WIDTH_4BIT
,
915 [MMC_BUS_WIDTH_8
] = SD_BUS_WIDTH_8BIT
,
918 if (bus_width
<= MMC_BUS_WIDTH_8
)
919 err
= rtsx_usb_write_register(host
->ucr
, SD_CFG1
,
920 0x03, width
[bus_width
]);
925 static int sd_pull_ctl_disable_lqfp48(struct rtsx_ucr
*ucr
)
927 rtsx_usb_init_cmd(ucr
);
929 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL1
, 0xFF, 0x55);
930 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL2
, 0xFF, 0x55);
931 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL3
, 0xFF, 0x95);
932 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL4
, 0xFF, 0x55);
933 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL5
, 0xFF, 0x55);
934 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL6
, 0xFF, 0xA5);
936 return rtsx_usb_send_cmd(ucr
, MODE_C
, 100);
939 static int sd_pull_ctl_disable_qfn24(struct rtsx_ucr
*ucr
)
941 rtsx_usb_init_cmd(ucr
);
943 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL1
, 0xFF, 0x65);
944 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL2
, 0xFF, 0x55);
945 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL3
, 0xFF, 0x95);
946 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL4
, 0xFF, 0x55);
947 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL5
, 0xFF, 0x56);
948 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL6
, 0xFF, 0x59);
950 return rtsx_usb_send_cmd(ucr
, MODE_C
, 100);
953 static int sd_pull_ctl_enable_lqfp48(struct rtsx_ucr
*ucr
)
955 rtsx_usb_init_cmd(ucr
);
957 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL1
, 0xFF, 0xAA);
958 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL2
, 0xFF, 0xAA);
959 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL3
, 0xFF, 0xA9);
960 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL4
, 0xFF, 0x55);
961 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL5
, 0xFF, 0x55);
962 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL6
, 0xFF, 0xA5);
964 return rtsx_usb_send_cmd(ucr
, MODE_C
, 100);
967 static int sd_pull_ctl_enable_qfn24(struct rtsx_ucr
*ucr
)
969 rtsx_usb_init_cmd(ucr
);
971 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL1
, 0xFF, 0xA5);
972 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL2
, 0xFF, 0x9A);
973 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL3
, 0xFF, 0xA5);
974 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL4
, 0xFF, 0x9A);
975 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL5
, 0xFF, 0x65);
976 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL6
, 0xFF, 0x5A);
978 return rtsx_usb_send_cmd(ucr
, MODE_C
, 100);
981 static int sd_power_on(struct rtsx_usb_sdmmc
*host
)
983 struct rtsx_ucr
*ucr
= host
->ucr
;
986 dev_dbg(sdmmc_dev(host
), "%s\n", __func__
);
987 rtsx_usb_init_cmd(ucr
);
988 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_SELECT
, 0x07, SD_MOD_SEL
);
989 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_SHARE_MODE
,
990 CARD_SHARE_MASK
, CARD_SHARE_SD
);
991 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_CLK_EN
,
992 SD_CLK_EN
, SD_CLK_EN
);
993 err
= rtsx_usb_send_cmd(ucr
, MODE_C
, 100);
997 if (CHECK_PKG(ucr
, LQFP48
))
998 err
= sd_pull_ctl_enable_lqfp48(ucr
);
1000 err
= sd_pull_ctl_enable_qfn24(ucr
);
1004 err
= rtsx_usb_write_register(ucr
, CARD_PWR_CTL
,
1005 POWER_MASK
, PARTIAL_POWER_ON
);
1009 usleep_range(800, 1000);
1011 rtsx_usb_init_cmd(ucr
);
1012 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PWR_CTL
,
1013 POWER_MASK
|LDO3318_PWR_MASK
, POWER_ON
|LDO_ON
);
1014 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_OE
,
1015 SD_OUTPUT_EN
, SD_OUTPUT_EN
);
1017 return rtsx_usb_send_cmd(ucr
, MODE_C
, 100);
1020 static int sd_power_off(struct rtsx_usb_sdmmc
*host
)
1022 struct rtsx_ucr
*ucr
= host
->ucr
;
1025 dev_dbg(sdmmc_dev(host
), "%s\n", __func__
);
1026 rtsx_usb_init_cmd(ucr
);
1028 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_CLK_EN
, SD_CLK_EN
, 0);
1029 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_OE
, SD_OUTPUT_EN
, 0);
1030 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PWR_CTL
,
1031 POWER_MASK
, POWER_OFF
);
1032 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PWR_CTL
,
1033 POWER_MASK
|LDO3318_PWR_MASK
, POWER_OFF
|LDO_SUSPEND
);
1035 err
= rtsx_usb_send_cmd(ucr
, MODE_C
, 100);
1039 if (CHECK_PKG(ucr
, LQFP48
))
1040 return sd_pull_ctl_disable_lqfp48(ucr
);
1041 return sd_pull_ctl_disable_qfn24(ucr
);
1044 static int sd_set_power_mode(struct rtsx_usb_sdmmc
*host
,
1045 unsigned char power_mode
)
1049 if (power_mode
!= MMC_POWER_OFF
)
1050 power_mode
= MMC_POWER_ON
;
1052 if (power_mode
== host
->power_mode
)
1055 if (power_mode
== MMC_POWER_OFF
) {
1056 err
= sd_power_off(host
);
1057 pm_runtime_put(sdmmc_dev(host
));
1059 pm_runtime_get_sync(sdmmc_dev(host
));
1060 err
= sd_power_on(host
);
1064 host
->power_mode
= power_mode
;
1069 static int sd_set_timing(struct rtsx_usb_sdmmc
*host
,
1070 unsigned char timing
, bool *ddr_mode
)
1072 struct rtsx_ucr
*ucr
= host
->ucr
;
1077 rtsx_usb_init_cmd(ucr
);
1080 case MMC_TIMING_UHS_SDR104
:
1081 case MMC_TIMING_UHS_SDR50
:
1082 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, SD_CFG1
,
1083 0x0C | SD_ASYNC_FIFO_RST
,
1084 SD_30_MODE
| SD_ASYNC_FIFO_RST
);
1085 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_CLK_SOURCE
, 0xFF,
1086 CRC_VAR_CLK0
| SD30_FIX_CLK
| SAMPLE_VAR_CLK1
);
1089 case MMC_TIMING_UHS_DDR50
:
1092 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, SD_CFG1
,
1093 0x0C | SD_ASYNC_FIFO_RST
,
1094 SD_DDR_MODE
| SD_ASYNC_FIFO_RST
);
1095 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_CLK_SOURCE
, 0xFF,
1096 CRC_VAR_CLK0
| SD30_FIX_CLK
| SAMPLE_VAR_CLK1
);
1097 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, SD_PUSH_POINT_CTL
,
1098 DDR_VAR_TX_CMD_DAT
, DDR_VAR_TX_CMD_DAT
);
1099 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, SD_SAMPLE_POINT_CTL
,
1100 DDR_VAR_RX_DAT
| DDR_VAR_RX_CMD
,
1101 DDR_VAR_RX_DAT
| DDR_VAR_RX_CMD
);
1104 case MMC_TIMING_MMC_HS
:
1105 case MMC_TIMING_SD_HS
:
1106 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, SD_CFG1
,
1108 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_CLK_SOURCE
, 0xFF,
1109 CRC_FIX_CLK
| SD30_VAR_CLK0
| SAMPLE_VAR_CLK1
);
1110 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, SD_PUSH_POINT_CTL
,
1111 SD20_TX_SEL_MASK
, SD20_TX_14_AHEAD
);
1112 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, SD_SAMPLE_POINT_CTL
,
1113 SD20_RX_SEL_MASK
, SD20_RX_14_DELAY
);
1117 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
,
1118 SD_CFG1
, 0x0C, SD_20_MODE
);
1119 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_CLK_SOURCE
, 0xFF,
1120 CRC_FIX_CLK
| SD30_VAR_CLK0
| SAMPLE_VAR_CLK1
);
1121 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
,
1122 SD_PUSH_POINT_CTL
, 0xFF, 0);
1123 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, SD_SAMPLE_POINT_CTL
,
1124 SD20_RX_SEL_MASK
, SD20_RX_POS_EDGE
);
1128 err
= rtsx_usb_send_cmd(ucr
, MODE_C
, 100);
1133 static void sdmmc_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
1135 struct rtsx_usb_sdmmc
*host
= mmc_priv(mmc
);
1136 struct rtsx_ucr
*ucr
= host
->ucr
;
1138 dev_dbg(sdmmc_dev(host
), "%s\n", __func__
);
1139 mutex_lock(&ucr
->dev_mutex
);
1141 sd_set_power_mode(host
, ios
->power_mode
);
1142 sd_set_bus_width(host
, ios
->bus_width
);
1143 sd_set_timing(host
, ios
->timing
, &host
->ddr_mode
);
1145 host
->vpclk
= false;
1146 host
->double_clk
= true;
1148 switch (ios
->timing
) {
1149 case MMC_TIMING_UHS_SDR104
:
1150 case MMC_TIMING_UHS_SDR50
:
1151 host
->ssc_depth
= SSC_DEPTH_2M
;
1153 host
->double_clk
= false;
1155 case MMC_TIMING_UHS_DDR50
:
1156 case MMC_TIMING_UHS_SDR25
:
1157 host
->ssc_depth
= SSC_DEPTH_1M
;
1160 host
->ssc_depth
= SSC_DEPTH_512K
;
1164 host
->initial_mode
= (ios
->clock
<= 1000000) ? true : false;
1165 host
->clock
= ios
->clock
;
1167 rtsx_usb_switch_clock(host
->ucr
, host
->clock
, host
->ssc_depth
,
1168 host
->initial_mode
, host
->double_clk
, host
->vpclk
);
1170 mutex_unlock(&ucr
->dev_mutex
);
1171 dev_dbg(sdmmc_dev(host
), "%s end\n", __func__
);
1174 static int sdmmc_switch_voltage(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
1176 struct rtsx_usb_sdmmc
*host
= mmc_priv(mmc
);
1177 struct rtsx_ucr
*ucr
= host
->ucr
;
1180 dev_dbg(sdmmc_dev(host
), "%s: signal_voltage = %d\n",
1181 __func__
, ios
->signal_voltage
);
1183 if (host
->host_removal
)
1186 if (ios
->signal_voltage
== MMC_SIGNAL_VOLTAGE_120
)
1189 mutex_lock(&ucr
->dev_mutex
);
1191 err
= rtsx_usb_card_exclusive_check(ucr
, RTSX_USB_SD_CARD
);
1193 mutex_unlock(&ucr
->dev_mutex
);
1197 /* Let mmc core do the busy checking, simply stop the forced-toggle
1198 * clock(while issuing CMD11) and switch voltage.
1200 rtsx_usb_init_cmd(ucr
);
1202 if (ios
->signal_voltage
== MMC_SIGNAL_VOLTAGE_330
) {
1203 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, SD_PAD_CTL
,
1204 SD_IO_USING_1V8
, SD_IO_USING_3V3
);
1205 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, LDO_POWER_CFG
,
1206 TUNE_SD18_MASK
, TUNE_SD18_3V3
);
1208 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, SD_BUS_STAT
,
1209 SD_CLK_TOGGLE_EN
| SD_CLK_FORCE_STOP
,
1211 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, SD_PAD_CTL
,
1212 SD_IO_USING_1V8
, SD_IO_USING_1V8
);
1213 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, LDO_POWER_CFG
,
1214 TUNE_SD18_MASK
, TUNE_SD18_1V8
);
1217 err
= rtsx_usb_send_cmd(ucr
, MODE_C
, 100);
1218 mutex_unlock(&ucr
->dev_mutex
);
1223 static int sdmmc_card_busy(struct mmc_host
*mmc
)
1225 struct rtsx_usb_sdmmc
*host
= mmc_priv(mmc
);
1226 struct rtsx_ucr
*ucr
= host
->ucr
;
1229 u8 mask
= SD_DAT3_STATUS
| SD_DAT2_STATUS
| SD_DAT1_STATUS
1232 dev_dbg(sdmmc_dev(host
), "%s\n", __func__
);
1234 mutex_lock(&ucr
->dev_mutex
);
1236 err
= rtsx_usb_write_register(ucr
, SD_BUS_STAT
,
1237 SD_CLK_TOGGLE_EN
| SD_CLK_FORCE_STOP
,
1244 err
= rtsx_usb_read_register(ucr
, SD_BUS_STAT
, &stat
);
1248 err
= rtsx_usb_write_register(ucr
, SD_BUS_STAT
,
1249 SD_CLK_TOGGLE_EN
| SD_CLK_FORCE_STOP
, 0);
1251 mutex_unlock(&ucr
->dev_mutex
);
1256 /* check if any pin between dat[0:3] is low */
1257 if ((stat
& mask
) != mask
)
1263 static int sdmmc_execute_tuning(struct mmc_host
*mmc
, u32 opcode
)
1265 struct rtsx_usb_sdmmc
*host
= mmc_priv(mmc
);
1266 struct rtsx_ucr
*ucr
= host
->ucr
;
1269 if (host
->host_removal
)
1272 mutex_lock(&ucr
->dev_mutex
);
1274 if (!host
->ddr_mode
)
1275 err
= sd_tuning_rx(host
, MMC_SEND_TUNING_BLOCK
);
1277 mutex_unlock(&ucr
->dev_mutex
);
1282 static const struct mmc_host_ops rtsx_usb_sdmmc_ops
= {
1283 .request
= sdmmc_request
,
1284 .set_ios
= sdmmc_set_ios
,
1285 .get_ro
= sdmmc_get_ro
,
1286 .get_cd
= sdmmc_get_cd
,
1287 .start_signal_voltage_switch
= sdmmc_switch_voltage
,
1288 .card_busy
= sdmmc_card_busy
,
1289 .execute_tuning
= sdmmc_execute_tuning
,
1292 #ifdef RTSX_USB_USE_LEDS_CLASS
1293 static void rtsx_usb_led_control(struct led_classdev
*led
,
1294 enum led_brightness brightness
)
1296 struct rtsx_usb_sdmmc
*host
= container_of(led
,
1297 struct rtsx_usb_sdmmc
, led
);
1299 if (host
->host_removal
)
1302 host
->led
.brightness
= brightness
;
1303 schedule_work(&host
->led_work
);
1306 static void rtsx_usb_update_led(struct work_struct
*work
)
1308 struct rtsx_usb_sdmmc
*host
=
1309 container_of(work
, struct rtsx_usb_sdmmc
, led_work
);
1310 struct rtsx_ucr
*ucr
= host
->ucr
;
1312 pm_runtime_get_sync(sdmmc_dev(host
));
1313 mutex_lock(&ucr
->dev_mutex
);
1315 if (host
->led
.brightness
== LED_OFF
)
1316 rtsx_usb_turn_off_led(ucr
);
1318 rtsx_usb_turn_on_led(ucr
);
1320 mutex_unlock(&ucr
->dev_mutex
);
1321 pm_runtime_put(sdmmc_dev(host
));
1325 static void rtsx_usb_init_host(struct rtsx_usb_sdmmc
*host
)
1327 struct mmc_host
*mmc
= host
->mmc
;
1329 mmc
->f_min
= 250000;
1330 mmc
->f_max
= 208000000;
1331 mmc
->ocr_avail
= MMC_VDD_32_33
| MMC_VDD_33_34
| MMC_VDD_165_195
;
1332 mmc
->caps
= MMC_CAP_4_BIT_DATA
| MMC_CAP_SD_HIGHSPEED
|
1333 MMC_CAP_MMC_HIGHSPEED
| MMC_CAP_BUS_WIDTH_TEST
|
1334 MMC_CAP_UHS_SDR12
| MMC_CAP_UHS_SDR25
| MMC_CAP_UHS_SDR50
|
1336 mmc
->caps2
= MMC_CAP2_NO_PRESCAN_POWERUP
| MMC_CAP2_FULL_PWR_CYCLE
;
1338 mmc
->max_current_330
= 400;
1339 mmc
->max_current_180
= 800;
1340 mmc
->ops
= &rtsx_usb_sdmmc_ops
;
1341 mmc
->max_segs
= 256;
1342 mmc
->max_seg_size
= 65536;
1343 mmc
->max_blk_size
= 512;
1344 mmc
->max_blk_count
= 65535;
1345 mmc
->max_req_size
= 524288;
1347 host
->power_mode
= MMC_POWER_OFF
;
1350 static int rtsx_usb_sdmmc_drv_probe(struct platform_device
*pdev
)
1352 struct mmc_host
*mmc
;
1353 struct rtsx_usb_sdmmc
*host
;
1354 struct rtsx_ucr
*ucr
;
1355 #ifdef RTSX_USB_USE_LEDS_CLASS
1359 ucr
= usb_get_intfdata(to_usb_interface(pdev
->dev
.parent
));
1363 dev_dbg(&(pdev
->dev
), ": Realtek USB SD/MMC controller found\n");
1365 mmc
= mmc_alloc_host(sizeof(*host
), &pdev
->dev
);
1369 host
= mmc_priv(mmc
);
1373 platform_set_drvdata(pdev
, host
);
1375 mutex_init(&host
->host_mutex
);
1376 rtsx_usb_init_host(host
);
1377 pm_runtime_enable(&pdev
->dev
);
1379 #ifdef RTSX_USB_USE_LEDS_CLASS
1380 snprintf(host
->led_name
, sizeof(host
->led_name
),
1381 "%s::", mmc_hostname(mmc
));
1382 host
->led
.name
= host
->led_name
;
1383 host
->led
.brightness
= LED_OFF
;
1384 host
->led
.default_trigger
= mmc_hostname(mmc
);
1385 host
->led
.brightness_set
= rtsx_usb_led_control
;
1387 err
= led_classdev_register(mmc_dev(mmc
), &host
->led
);
1389 dev_err(&(pdev
->dev
),
1390 "Failed to register LED device: %d\n", err
);
1391 INIT_WORK(&host
->led_work
, rtsx_usb_update_led
);
1399 static int rtsx_usb_sdmmc_drv_remove(struct platform_device
*pdev
)
1401 struct rtsx_usb_sdmmc
*host
= platform_get_drvdata(pdev
);
1402 struct mmc_host
*mmc
;
1408 host
->host_removal
= true;
1410 mutex_lock(&host
->host_mutex
);
1412 dev_dbg(&(pdev
->dev
),
1413 "%s: Controller removed during transfer\n",
1415 host
->mrq
->cmd
->error
= -ENOMEDIUM
;
1416 if (host
->mrq
->stop
)
1417 host
->mrq
->stop
->error
= -ENOMEDIUM
;
1418 mmc_request_done(mmc
, host
->mrq
);
1420 mutex_unlock(&host
->host_mutex
);
1422 mmc_remove_host(mmc
);
1424 #ifdef RTSX_USB_USE_LEDS_CLASS
1425 cancel_work_sync(&host
->led_work
);
1426 led_classdev_unregister(&host
->led
);
1430 pm_runtime_disable(&pdev
->dev
);
1431 platform_set_drvdata(pdev
, NULL
);
1433 dev_dbg(&(pdev
->dev
),
1434 ": Realtek USB SD/MMC module has been removed\n");
1439 static const struct platform_device_id rtsx_usb_sdmmc_ids
[] = {
1441 .name
= "rtsx_usb_sdmmc",
1446 MODULE_DEVICE_TABLE(platform
, rtsx_usb_sdmmc_ids
);
1448 static struct platform_driver rtsx_usb_sdmmc_driver
= {
1449 .probe
= rtsx_usb_sdmmc_drv_probe
,
1450 .remove
= rtsx_usb_sdmmc_drv_remove
,
1451 .id_table
= rtsx_usb_sdmmc_ids
,
1453 .name
= "rtsx_usb_sdmmc",
1456 module_platform_driver(rtsx_usb_sdmmc_driver
);
1458 MODULE_LICENSE("GPL v2");
1459 MODULE_AUTHOR("Roger Tseng <rogerable@realtek.com>");
1460 MODULE_DESCRIPTION("Realtek USB SD/MMC Card Host Driver");