1 /* Driver for Realtek PCI-Express card reader
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)
20 * Micky Ching (micky_ching@realsil.com.cn)
23 #include <linux/blkdev.h>
24 #include <linux/kthread.h>
25 #include <linux/sched.h>
30 #define SD_MAX_RETRY_COUNT 3
32 static u16 REG_SD_CFG1
;
33 static u16 REG_SD_CFG2
;
34 static u16 REG_SD_CFG3
;
35 static u16 REG_SD_STAT1
;
36 static u16 REG_SD_STAT2
;
37 static u16 REG_SD_BUS_STAT
;
38 static u16 REG_SD_PAD_CTL
;
39 static u16 REG_SD_SAMPLE_POINT_CTL
;
40 static u16 REG_SD_PUSH_POINT_CTL
;
41 static u16 REG_SD_CMD0
;
42 static u16 REG_SD_CMD1
;
43 static u16 REG_SD_CMD2
;
44 static u16 REG_SD_CMD3
;
45 static u16 REG_SD_CMD4
;
46 static u16 REG_SD_CMD5
;
47 static u16 REG_SD_BYTE_CNT_L
;
48 static u16 REG_SD_BYTE_CNT_H
;
49 static u16 REG_SD_BLOCK_CNT_L
;
50 static u16 REG_SD_BLOCK_CNT_H
;
51 static u16 REG_SD_TRANSFER
;
52 static u16 REG_SD_VPCLK0_CTL
;
53 static u16 REG_SD_VPCLK1_CTL
;
54 static u16 REG_SD_DCMPS0_CTL
;
55 static u16 REG_SD_DCMPS1_CTL
;
57 static inline void sd_set_err_code(struct rtsx_chip
*chip
, u8 err_code
)
59 struct sd_info
*sd_card
= &chip
->sd_card
;
61 sd_card
->err_code
|= err_code
;
64 static inline void sd_clr_err_code(struct rtsx_chip
*chip
)
66 struct sd_info
*sd_card
= &chip
->sd_card
;
68 sd_card
->err_code
= 0;
71 static inline int sd_check_err_code(struct rtsx_chip
*chip
, u8 err_code
)
73 struct sd_info
*sd_card
= &chip
->sd_card
;
75 return sd_card
->err_code
& err_code
;
78 static void sd_init_reg_addr(struct rtsx_chip
*chip
)
83 REG_SD_STAT1
= 0xFD30;
87 REG_SD_SAMPLE_POINT_CTL
= 0;
88 REG_SD_PUSH_POINT_CTL
= 0;
95 REG_SD_BYTE_CNT_L
= 0xFD39;
96 REG_SD_BYTE_CNT_H
= 0xFD3A;
97 REG_SD_BLOCK_CNT_L
= 0xFD3B;
98 REG_SD_BLOCK_CNT_H
= 0xFD3C;
99 REG_SD_TRANSFER
= 0xFD32;
100 REG_SD_VPCLK0_CTL
= 0;
101 REG_SD_VPCLK1_CTL
= 0;
102 REG_SD_DCMPS0_CTL
= 0;
103 REG_SD_DCMPS1_CTL
= 0;
106 static int sd_check_data0_status(struct rtsx_chip
*chip
)
111 retval
= rtsx_read_register(chip
, REG_SD_STAT1
, &stat
);
116 if (!(stat
& SD_DAT0_STATUS
)) {
117 sd_set_err_code(chip
, SD_BUSY
);
121 return STATUS_SUCCESS
;
124 static int sd_send_cmd_get_rsp(struct rtsx_chip
*chip
, u8 cmd_idx
,
125 u32 arg
, u8 rsp_type
, u8
*rsp
, int rsp_len
)
127 struct sd_info
*sd_card
= &chip
->sd_card
;
135 sd_clr_err_code(chip
);
137 dev_dbg(rtsx_dev(chip
), "SD/MMC CMD %d, arg = 0x%08x\n", cmd_idx
, arg
);
139 if (rsp_type
== SD_RSP_TYPE_R1b
)
146 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD0
, 0xFF, 0x40 | cmd_idx
);
147 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD1
, 0xFF, (u8
)(arg
>> 24));
148 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD2
, 0xFF, (u8
)(arg
>> 16));
149 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD3
, 0xFF, (u8
)(arg
>> 8));
150 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD4
, 0xFF, (u8
)arg
);
152 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CFG2
, 0xFF, rsp_type
);
153 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
,
154 0x01, PINGPONG_BUFFER
);
155 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_TRANSFER
,
156 0xFF, SD_TM_CMD_RSP
| SD_TRANSFER_START
);
157 rtsx_add_cmd(chip
, CHECK_REG_CMD
, REG_SD_TRANSFER
,
158 SD_TRANSFER_END
| SD_STAT_IDLE
, SD_TRANSFER_END
|
161 if (rsp_type
== SD_RSP_TYPE_R2
) {
162 for (reg_addr
= PPBUF_BASE2
; reg_addr
< PPBUF_BASE2
+ 16;
164 rtsx_add_cmd(chip
, READ_REG_CMD
, reg_addr
, 0, 0);
167 } else if (rsp_type
!= SD_RSP_TYPE_R0
) {
168 for (reg_addr
= REG_SD_CMD0
; reg_addr
<= REG_SD_CMD4
;
170 rtsx_add_cmd(chip
, READ_REG_CMD
, reg_addr
, 0, 0);
175 rtsx_add_cmd(chip
, READ_REG_CMD
, REG_SD_STAT1
, 0, 0);
177 retval
= rtsx_send_cmd(chip
, SD_CARD
, timeout
);
181 rtsx_read_register(chip
, REG_SD_STAT1
, &val
);
182 dev_dbg(rtsx_dev(chip
), "SD_STAT1: 0x%x\n", val
);
184 rtsx_read_register(chip
, REG_SD_CFG3
, &val
);
185 dev_dbg(rtsx_dev(chip
), "SD_CFG3: 0x%x\n", val
);
187 if (retval
== -ETIMEDOUT
) {
188 if (rsp_type
& SD_WAIT_BUSY_END
) {
189 retval
= sd_check_data0_status(chip
);
190 if (retval
!= STATUS_SUCCESS
) {
191 rtsx_clear_sd_error(chip
);
195 sd_set_err_code(chip
, SD_TO_ERR
);
197 retval
= STATUS_TIMEDOUT
;
199 retval
= STATUS_FAIL
;
201 rtsx_clear_sd_error(chip
);
206 if (rsp_type
== SD_RSP_TYPE_R0
)
207 return STATUS_SUCCESS
;
209 ptr
= rtsx_get_cmd_data(chip
) + 1;
211 if ((ptr
[0] & 0xC0) != 0) {
212 sd_set_err_code(chip
, SD_STS_ERR
);
216 if (!(rsp_type
& SD_NO_CHECK_CRC7
)) {
217 if (ptr
[stat_idx
] & SD_CRC7_ERR
) {
218 if (cmd_idx
== WRITE_MULTIPLE_BLOCK
) {
219 sd_set_err_code(chip
, SD_CRC_ERR
);
222 if (rty_cnt
< SD_MAX_RETRY_COUNT
) {
227 sd_set_err_code(chip
, SD_CRC_ERR
);
233 if ((rsp_type
== SD_RSP_TYPE_R1
) || (rsp_type
== SD_RSP_TYPE_R1b
)) {
234 if ((cmd_idx
!= SEND_RELATIVE_ADDR
) &&
235 (cmd_idx
!= SEND_IF_COND
)) {
236 if (cmd_idx
!= STOP_TRANSMISSION
) {
241 #ifdef SUPPORT_SD_LOCK
246 dev_dbg(rtsx_dev(chip
), "ptr[1]: 0x%02x\n",
251 dev_dbg(rtsx_dev(chip
), "ptr[2]: 0x%02x\n",
256 dev_dbg(rtsx_dev(chip
), "ptr[3]: 0x%02x\n",
261 sd_card
->sd_data_buf_ready
= 1;
263 sd_card
->sd_data_buf_ready
= 0;
268 memcpy(rsp
, ptr
, rsp_len
);
270 return STATUS_SUCCESS
;
273 static int sd_read_data(struct rtsx_chip
*chip
,
274 u8 trans_mode
, u8
*cmd
, int cmd_len
, u16 byte_cnt
,
275 u16 blk_cnt
, u8 bus_width
, u8
*buf
, int buf_len
,
278 struct sd_info
*sd_card
= &chip
->sd_card
;
282 sd_clr_err_code(chip
);
294 dev_dbg(rtsx_dev(chip
), "SD/MMC CMD %d\n", cmd
[0] - 0x40);
295 for (i
= 0; i
< (min(cmd_len
, 6)); i
++)
296 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD0
+ i
,
299 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BYTE_CNT_L
, 0xFF,
301 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BYTE_CNT_H
, 0xFF,
302 (u8
)(byte_cnt
>> 8));
303 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BLOCK_CNT_L
, 0xFF,
305 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BLOCK_CNT_H
, 0xFF,
308 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CFG1
, 0x03, bus_width
);
310 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CFG2
, 0xFF,
311 SD_CALCULATE_CRC7
| SD_CHECK_CRC16
| SD_NO_WAIT_BUSY_END
|
312 SD_CHECK_CRC7
| SD_RSP_LEN_6
);
313 if (trans_mode
!= SD_TM_AUTO_TUNING
)
314 rtsx_add_cmd(chip
, WRITE_REG_CMD
,
315 CARD_DATA_SOURCE
, 0x01, PINGPONG_BUFFER
);
317 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_TRANSFER
, 0xFF,
318 trans_mode
| SD_TRANSFER_START
);
319 rtsx_add_cmd(chip
, CHECK_REG_CMD
, REG_SD_TRANSFER
, SD_TRANSFER_END
,
322 retval
= rtsx_send_cmd(chip
, SD_CARD
, timeout
);
324 if (retval
== -ETIMEDOUT
) {
325 sd_send_cmd_get_rsp(chip
, SEND_STATUS
, sd_card
->sd_addr
,
326 SD_RSP_TYPE_R1
, NULL
, 0);
332 if (buf
&& buf_len
) {
333 retval
= rtsx_read_ppbuf(chip
, buf
, buf_len
);
334 if (retval
!= STATUS_SUCCESS
) {
339 return STATUS_SUCCESS
;
342 static int sd_write_data(struct rtsx_chip
*chip
, u8 trans_mode
,
343 u8
*cmd
, int cmd_len
, u16 byte_cnt
, u16 blk_cnt
,
344 u8 bus_width
, u8
*buf
, int buf_len
, int timeout
)
346 struct sd_info
*sd_card
= &chip
->sd_card
;
350 sd_clr_err_code(chip
);
356 /* This function can't write data more than one page */
360 if (buf
&& buf_len
) {
361 retval
= rtsx_write_ppbuf(chip
, buf
, buf_len
);
362 if (retval
!= STATUS_SUCCESS
) {
370 dev_dbg(rtsx_dev(chip
), "SD/MMC CMD %d\n", cmd
[0] - 0x40);
371 for (i
= 0; i
< (min(cmd_len
, 6)); i
++) {
372 rtsx_add_cmd(chip
, WRITE_REG_CMD
,
373 REG_SD_CMD0
+ i
, 0xFF, cmd
[i
]);
376 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BYTE_CNT_L
, 0xFF,
378 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BYTE_CNT_H
, 0xFF,
379 (u8
)(byte_cnt
>> 8));
380 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BLOCK_CNT_L
, 0xFF,
382 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BLOCK_CNT_H
, 0xFF,
385 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CFG1
, 0x03, bus_width
);
387 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CFG2
, 0xFF,
388 SD_CALCULATE_CRC7
| SD_CHECK_CRC16
| SD_NO_WAIT_BUSY_END
|
389 SD_CHECK_CRC7
| SD_RSP_LEN_6
);
391 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_TRANSFER
, 0xFF,
392 trans_mode
| SD_TRANSFER_START
);
393 rtsx_add_cmd(chip
, CHECK_REG_CMD
, REG_SD_TRANSFER
, SD_TRANSFER_END
,
396 retval
= rtsx_send_cmd(chip
, SD_CARD
, timeout
);
398 if (retval
== -ETIMEDOUT
) {
399 sd_send_cmd_get_rsp(chip
, SEND_STATUS
, sd_card
->sd_addr
,
400 SD_RSP_TYPE_R1
, NULL
, 0);
406 return STATUS_SUCCESS
;
409 static int sd_check_csd(struct rtsx_chip
*chip
, char check_wp
)
411 struct sd_info
*sd_card
= &chip
->sd_card
;
414 u8 csd_ver
, trans_speed
;
417 for (i
= 0; i
< 6; i
++) {
418 if (detect_card_cd(chip
, SD_CARD
) != STATUS_SUCCESS
) {
419 sd_set_err_code(chip
, SD_NO_CARD
);
423 retval
= sd_send_cmd_get_rsp(chip
, SEND_CSD
, sd_card
->sd_addr
,
424 SD_RSP_TYPE_R2
, rsp
, 16);
425 if (retval
== STATUS_SUCCESS
)
433 memcpy(sd_card
->raw_csd
, rsp
+ 1, 15);
435 dev_dbg(rtsx_dev(chip
), "CSD Response:\n");
436 dev_dbg(rtsx_dev(chip
), "%*ph\n", 16, sd_card
->raw_csd
);
438 csd_ver
= (rsp
[1] & 0xc0) >> 6;
439 dev_dbg(rtsx_dev(chip
), "csd_ver = %d\n", csd_ver
);
441 trans_speed
= rsp
[4];
442 if ((trans_speed
& 0x07) == 0x02) {
443 if ((trans_speed
& 0xf8) >= 0x30) {
445 sd_card
->sd_clock
= 47;
447 sd_card
->sd_clock
= CLK_50
;
449 } else if ((trans_speed
& 0xf8) == 0x28) {
451 sd_card
->sd_clock
= 39;
453 sd_card
->sd_clock
= CLK_40
;
455 } else if ((trans_speed
& 0xf8) == 0x20) {
457 sd_card
->sd_clock
= 29;
459 sd_card
->sd_clock
= CLK_30
;
461 } else if ((trans_speed
& 0xf8) >= 0x10) {
463 sd_card
->sd_clock
= 23;
465 sd_card
->sd_clock
= CLK_20
;
467 } else if ((trans_speed
& 0x08) >= 0x08) {
469 sd_card
->sd_clock
= 19;
471 sd_card
->sd_clock
= CLK_20
;
479 if (CHK_MMC_SECTOR_MODE(sd_card
)) {
480 sd_card
->capacity
= 0;
482 if ((!CHK_SD_HCXC(sd_card
)) || (csd_ver
== 0)) {
483 u8 blk_size
, c_size_mult
;
486 blk_size
= rsp
[6] & 0x0F;
487 c_size
= ((u16
)(rsp
[7] & 0x03) << 10)
489 + ((u16
)(rsp
[9] & 0xC0) >> 6);
490 c_size_mult
= (u8
)((rsp
[10] & 0x03) << 1);
491 c_size_mult
+= (rsp
[11] & 0x80) >> 7;
492 sd_card
->capacity
= (((u32
)(c_size
+ 1)) *
493 (1 << (c_size_mult
+ 2)))
496 u32 total_sector
= 0;
498 total_sector
= (((u32
)rsp
[8] & 0x3f) << 16) |
499 ((u32
)rsp
[9] << 8) | (u32
)rsp
[10];
500 sd_card
->capacity
= (total_sector
+ 1) << 10;
506 chip
->card_wp
|= SD_CARD
;
508 dev_dbg(rtsx_dev(chip
), "CSD WP Status: 0x%x\n", rsp
[15]);
511 return STATUS_SUCCESS
;
514 static int sd_set_sample_push_timing(struct rtsx_chip
*chip
)
517 struct sd_info
*sd_card
= &chip
->sd_card
;
520 if ((chip
->sd_ctl
& SD_PUSH_POINT_CTL_MASK
) == SD_PUSH_POINT_DELAY
)
523 if ((chip
->sd_ctl
& SD_SAMPLE_POINT_CTL_MASK
) == SD_SAMPLE_POINT_AUTO
) {
524 if (chip
->asic_code
) {
525 if (CHK_SD_HS(sd_card
) || CHK_MMC_52M(sd_card
)) {
537 } else if ((chip
->sd_ctl
& SD_SAMPLE_POINT_CTL_MASK
) ==
538 SD_SAMPLE_POINT_DELAY
) {
545 retval
= rtsx_write_register(chip
, REG_SD_CFG1
, 0x1C, val
);
550 return STATUS_SUCCESS
;
553 static void sd_choose_proper_clock(struct rtsx_chip
*chip
)
555 struct sd_info
*sd_card
= &chip
->sd_card
;
557 if (CHK_SD_SDR104(sd_card
)) {
559 sd_card
->sd_clock
= chip
->asic_sd_sdr104_clk
;
561 sd_card
->sd_clock
= chip
->fpga_sd_sdr104_clk
;
563 } else if (CHK_SD_DDR50(sd_card
)) {
565 sd_card
->sd_clock
= chip
->asic_sd_ddr50_clk
;
567 sd_card
->sd_clock
= chip
->fpga_sd_ddr50_clk
;
569 } else if (CHK_SD_SDR50(sd_card
)) {
571 sd_card
->sd_clock
= chip
->asic_sd_sdr50_clk
;
573 sd_card
->sd_clock
= chip
->fpga_sd_sdr50_clk
;
575 } else if (CHK_SD_HS(sd_card
)) {
577 sd_card
->sd_clock
= chip
->asic_sd_hs_clk
;
579 sd_card
->sd_clock
= chip
->fpga_sd_hs_clk
;
581 } else if (CHK_MMC_52M(sd_card
) || CHK_MMC_DDR52(sd_card
)) {
583 sd_card
->sd_clock
= chip
->asic_mmc_52m_clk
;
585 sd_card
->sd_clock
= chip
->fpga_mmc_52m_clk
;
587 } else if (CHK_MMC_26M(sd_card
)) {
589 sd_card
->sd_clock
= 48;
591 sd_card
->sd_clock
= CLK_50
;
595 static int sd_set_clock_divider(struct rtsx_chip
*chip
, u8 clk_div
)
598 u8 mask
= 0, val
= 0;
601 if (clk_div
== SD_CLK_DIVIDE_0
)
603 else if (clk_div
== SD_CLK_DIVIDE_128
)
605 else if (clk_div
== SD_CLK_DIVIDE_256
)
608 retval
= rtsx_write_register(chip
, REG_SD_CFG1
, mask
, val
);
613 return STATUS_SUCCESS
;
616 static int sd_set_init_para(struct rtsx_chip
*chip
)
618 struct sd_info
*sd_card
= &chip
->sd_card
;
621 retval
= sd_set_sample_push_timing(chip
);
622 if (retval
!= STATUS_SUCCESS
) {
626 sd_choose_proper_clock(chip
);
628 retval
= switch_clock(chip
, sd_card
->sd_clock
);
629 if (retval
!= STATUS_SUCCESS
) {
633 return STATUS_SUCCESS
;
636 int sd_select_card(struct rtsx_chip
*chip
, int select
)
638 struct sd_info
*sd_card
= &chip
->sd_card
;
640 u8 cmd_idx
, cmd_type
;
644 cmd_idx
= SELECT_CARD
;
645 cmd_type
= SD_RSP_TYPE_R1
;
646 addr
= sd_card
->sd_addr
;
648 cmd_idx
= DESELECT_CARD
;
649 cmd_type
= SD_RSP_TYPE_R0
;
653 retval
= sd_send_cmd_get_rsp(chip
, cmd_idx
, addr
, cmd_type
, NULL
, 0);
654 if (retval
!= STATUS_SUCCESS
) {
658 return STATUS_SUCCESS
;
661 #ifdef SUPPORT_SD_LOCK
662 static int sd_update_lock_status(struct rtsx_chip
*chip
)
664 struct sd_info
*sd_card
= &chip
->sd_card
;
668 retval
= sd_send_cmd_get_rsp(chip
, SEND_STATUS
, sd_card
->sd_addr
,
669 SD_RSP_TYPE_R1
, rsp
, 5);
670 if (retval
!= STATUS_SUCCESS
) {
675 sd_card
->sd_lock_status
|= SD_LOCKED
;
677 sd_card
->sd_lock_status
&= ~SD_LOCKED
;
679 dev_dbg(rtsx_dev(chip
), "sd_card->sd_lock_status = 0x%x\n",
680 sd_card
->sd_lock_status
);
686 return STATUS_SUCCESS
;
690 static int sd_wait_state_data_ready(struct rtsx_chip
*chip
, u8 state
,
691 u8 data_ready
, int polling_cnt
)
693 struct sd_info
*sd_card
= &chip
->sd_card
;
697 for (i
= 0; i
< polling_cnt
; i
++) {
698 retval
= sd_send_cmd_get_rsp(chip
, SEND_STATUS
,
699 sd_card
->sd_addr
, SD_RSP_TYPE_R1
,
701 if (retval
!= STATUS_SUCCESS
) {
705 if (((rsp
[3] & 0x1E) == state
) &&
706 ((rsp
[3] & 0x01) == data_ready
))
707 return STATUS_SUCCESS
;
713 static int sd_change_bank_voltage(struct rtsx_chip
*chip
, u8 voltage
)
717 if (voltage
== SD_IO_3V3
) {
718 if (chip
->asic_code
) {
719 retval
= rtsx_write_phy_register(chip
, 0x08,
722 if (retval
!= STATUS_SUCCESS
) {
726 retval
= rtsx_write_register(chip
, SD_PAD_CTL
,
732 } else if (voltage
== SD_IO_1V8
) {
733 if (chip
->asic_code
) {
734 retval
= rtsx_write_phy_register(chip
, 0x08,
737 if (retval
!= STATUS_SUCCESS
) {
741 retval
= rtsx_write_register(chip
, SD_PAD_CTL
,
752 return STATUS_SUCCESS
;
755 static int sd_voltage_switch(struct rtsx_chip
*chip
)
760 retval
= rtsx_write_register(chip
, SD_BUS_STAT
,
761 SD_CLK_TOGGLE_EN
| SD_CLK_FORCE_STOP
,
767 retval
= sd_send_cmd_get_rsp(chip
, VOLTAGE_SWITCH
, 0, SD_RSP_TYPE_R1
,
769 if (retval
!= STATUS_SUCCESS
) {
773 udelay(chip
->sd_voltage_switch_delay
);
775 retval
= rtsx_read_register(chip
, SD_BUS_STAT
, &stat
);
779 if (stat
& (SD_CMD_STATUS
| SD_DAT3_STATUS
| SD_DAT2_STATUS
|
780 SD_DAT1_STATUS
| SD_DAT0_STATUS
)) {
784 retval
= rtsx_write_register(chip
, SD_BUS_STAT
, 0xFF,
789 retval
= sd_change_bank_voltage(chip
, SD_IO_1V8
);
790 if (retval
!= STATUS_SUCCESS
) {
796 retval
= rtsx_write_register(chip
, SD_BUS_STAT
, 0xFF,
803 retval
= rtsx_read_register(chip
, SD_BUS_STAT
, &stat
);
807 if ((stat
& (SD_CMD_STATUS
| SD_DAT3_STATUS
| SD_DAT2_STATUS
|
808 SD_DAT1_STATUS
| SD_DAT0_STATUS
)) !=
809 (SD_CMD_STATUS
| SD_DAT3_STATUS
| SD_DAT2_STATUS
|
810 SD_DAT1_STATUS
| SD_DAT0_STATUS
)) {
811 dev_dbg(rtsx_dev(chip
), "SD_BUS_STAT: 0x%x\n", stat
);
812 rtsx_write_register(chip
, SD_BUS_STAT
, SD_CLK_TOGGLE_EN
|
813 SD_CLK_FORCE_STOP
, 0);
814 rtsx_write_register(chip
, CARD_CLK_EN
, 0xFF, 0);
818 retval
= rtsx_write_register(chip
, SD_BUS_STAT
,
819 SD_CLK_TOGGLE_EN
| SD_CLK_FORCE_STOP
, 0);
824 return STATUS_SUCCESS
;
827 static int sd_reset_dcm(struct rtsx_chip
*chip
, u8 tune_dir
)
831 if (tune_dir
== TUNE_RX
) {
832 retval
= rtsx_write_register(chip
, DCM_DRP_CTL
, 0xFF,
837 retval
= rtsx_write_register(chip
, DCM_DRP_CTL
, 0xFF, DCM_RX
);
842 retval
= rtsx_write_register(chip
, DCM_DRP_CTL
, 0xFF,
847 retval
= rtsx_write_register(chip
, DCM_DRP_CTL
, 0xFF, DCM_TX
);
853 return STATUS_SUCCESS
;
856 static int sd_change_phase(struct rtsx_chip
*chip
, u8 sample_point
, u8 tune_dir
)
858 struct sd_info
*sd_card
= &chip
->sd_card
;
859 u16 SD_VP_CTL
, SD_DCMPS_CTL
;
864 dev_dbg(rtsx_dev(chip
), "%s (sample_point = %d, tune_dir = %d)\n",
865 __func__
, sample_point
, tune_dir
);
867 if (tune_dir
== TUNE_RX
) {
868 SD_VP_CTL
= SD_VPRX_CTL
;
869 SD_DCMPS_CTL
= SD_DCMPS_RX_CTL
;
870 if (CHK_SD_DDR50(sd_card
))
873 SD_VP_CTL
= SD_VPTX_CTL
;
874 SD_DCMPS_CTL
= SD_DCMPS_TX_CTL
;
877 if (chip
->asic_code
) {
878 retval
= rtsx_write_register(chip
, CLK_CTL
, CHANGE_CLK
,
883 retval
= rtsx_write_register(chip
, SD_VP_CTL
, 0x1F,
888 retval
= rtsx_write_register(chip
, SD_VPCLK0_CTL
,
893 retval
= rtsx_write_register(chip
, SD_VPCLK0_CTL
,
894 PHASE_NOT_RESET
, PHASE_NOT_RESET
);
898 retval
= rtsx_write_register(chip
, CLK_CTL
, CHANGE_CLK
, 0);
903 rtsx_read_register(chip
, SD_VP_CTL
, &val
);
904 dev_dbg(rtsx_dev(chip
), "SD_VP_CTL: 0x%x\n", val
);
905 rtsx_read_register(chip
, SD_DCMPS_CTL
, &val
);
906 dev_dbg(rtsx_dev(chip
), "SD_DCMPS_CTL: 0x%x\n", val
);
909 retval
= rtsx_write_register(chip
, SD_VP_CTL
,
916 retval
= rtsx_write_register(chip
, SD_VP_CTL
, 0xFF,
924 retval
= rtsx_write_register(chip
, CLK_CTL
,
925 CHANGE_CLK
, CHANGE_CLK
);
930 retval
= rtsx_write_register(chip
, SD_VP_CTL
, 0xFF,
940 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SD_DCMPS_CTL
, DCMPS_CHANGE
,
942 rtsx_add_cmd(chip
, CHECK_REG_CMD
, SD_DCMPS_CTL
,
943 DCMPS_CHANGE_DONE
, DCMPS_CHANGE_DONE
);
944 retval
= rtsx_send_cmd(chip
, SD_CARD
, 100);
945 if (retval
!= STATUS_SUCCESS
) {
949 val
= *rtsx_get_cmd_data(chip
);
950 if (val
& DCMPS_ERROR
) {
954 if ((val
& DCMPS_CURRENT_PHASE
) != sample_point
) {
958 retval
= rtsx_write_register(chip
, SD_DCMPS_CTL
,
964 retval
= rtsx_write_register(chip
, SD_VP_CTL
,
970 retval
= rtsx_write_register(chip
, CLK_CTL
,
980 retval
= rtsx_write_register(chip
, SD_CFG1
, SD_ASYNC_FIFO_NOT_RST
, 0);
985 return STATUS_SUCCESS
;
988 rtsx_read_register(chip
, SD_VP_CTL
, &val
);
989 dev_dbg(rtsx_dev(chip
), "SD_VP_CTL: 0x%x\n", val
);
990 rtsx_read_register(chip
, SD_DCMPS_CTL
, &val
);
991 dev_dbg(rtsx_dev(chip
), "SD_DCMPS_CTL: 0x%x\n", val
);
993 rtsx_write_register(chip
, SD_DCMPS_CTL
, DCMPS_CHANGE
, 0);
994 rtsx_write_register(chip
, SD_VP_CTL
, PHASE_CHANGE
, 0);
996 sd_reset_dcm(chip
, tune_dir
);
1000 static int sd_check_spec(struct rtsx_chip
*chip
, u8 bus_width
)
1002 struct sd_info
*sd_card
= &chip
->sd_card
;
1006 retval
= sd_send_cmd_get_rsp(chip
, APP_CMD
, sd_card
->sd_addr
,
1007 SD_RSP_TYPE_R1
, NULL
, 0);
1008 if (retval
!= STATUS_SUCCESS
) {
1012 cmd
[0] = 0x40 | SEND_SCR
;
1018 retval
= sd_read_data(chip
, SD_TM_NORMAL_READ
, cmd
, 5, 8, 1, bus_width
,
1020 if (retval
!= STATUS_SUCCESS
) {
1021 rtsx_clear_sd_error(chip
);
1025 memcpy(sd_card
->raw_scr
, buf
, 8);
1027 if ((buf
[0] & 0x0F) == 0) {
1031 return STATUS_SUCCESS
;
1034 static int sd_query_switch_result(struct rtsx_chip
*chip
, u8 func_group
,
1035 u8 func_to_switch
, u8
*buf
, int buf_len
)
1037 u8 support_mask
= 0, query_switch
= 0, switch_busy
= 0;
1038 int support_offset
= 0, query_switch_offset
= 0, check_busy_offset
= 0;
1040 if (func_group
== SD_FUNC_GROUP_1
) {
1041 support_offset
= FUNCTION_GROUP1_SUPPORT_OFFSET
;
1042 query_switch_offset
= FUNCTION_GROUP1_QUERY_SWITCH_OFFSET
;
1043 check_busy_offset
= FUNCTION_GROUP1_CHECK_BUSY_OFFSET
;
1045 switch (func_to_switch
) {
1047 support_mask
= HS_SUPPORT_MASK
;
1048 query_switch
= HS_QUERY_SWITCH_OK
;
1049 switch_busy
= HS_SWITCH_BUSY
;
1053 support_mask
= SDR50_SUPPORT_MASK
;
1054 query_switch
= SDR50_QUERY_SWITCH_OK
;
1055 switch_busy
= SDR50_SWITCH_BUSY
;
1058 case SDR104_SUPPORT
:
1059 support_mask
= SDR104_SUPPORT_MASK
;
1060 query_switch
= SDR104_QUERY_SWITCH_OK
;
1061 switch_busy
= SDR104_SWITCH_BUSY
;
1065 support_mask
= DDR50_SUPPORT_MASK
;
1066 query_switch
= DDR50_QUERY_SWITCH_OK
;
1067 switch_busy
= DDR50_SWITCH_BUSY
;
1073 } else if (func_group
== SD_FUNC_GROUP_3
) {
1074 support_offset
= FUNCTION_GROUP3_SUPPORT_OFFSET
;
1075 query_switch_offset
= FUNCTION_GROUP3_QUERY_SWITCH_OFFSET
;
1076 check_busy_offset
= FUNCTION_GROUP3_CHECK_BUSY_OFFSET
;
1078 switch (func_to_switch
) {
1079 case DRIVING_TYPE_A
:
1080 support_mask
= DRIVING_TYPE_A_MASK
;
1081 query_switch
= TYPE_A_QUERY_SWITCH_OK
;
1082 switch_busy
= TYPE_A_SWITCH_BUSY
;
1085 case DRIVING_TYPE_C
:
1086 support_mask
= DRIVING_TYPE_C_MASK
;
1087 query_switch
= TYPE_C_QUERY_SWITCH_OK
;
1088 switch_busy
= TYPE_C_SWITCH_BUSY
;
1091 case DRIVING_TYPE_D
:
1092 support_mask
= DRIVING_TYPE_D_MASK
;
1093 query_switch
= TYPE_D_QUERY_SWITCH_OK
;
1094 switch_busy
= TYPE_D_SWITCH_BUSY
;
1100 } else if (func_group
== SD_FUNC_GROUP_4
) {
1101 support_offset
= FUNCTION_GROUP4_SUPPORT_OFFSET
;
1102 query_switch_offset
= FUNCTION_GROUP4_QUERY_SWITCH_OFFSET
;
1103 check_busy_offset
= FUNCTION_GROUP4_CHECK_BUSY_OFFSET
;
1105 switch (func_to_switch
) {
1106 case CURRENT_LIMIT_400
:
1107 support_mask
= CURRENT_LIMIT_400_MASK
;
1108 query_switch
= CURRENT_LIMIT_400_QUERY_SWITCH_OK
;
1109 switch_busy
= CURRENT_LIMIT_400_SWITCH_BUSY
;
1112 case CURRENT_LIMIT_600
:
1113 support_mask
= CURRENT_LIMIT_600_MASK
;
1114 query_switch
= CURRENT_LIMIT_600_QUERY_SWITCH_OK
;
1115 switch_busy
= CURRENT_LIMIT_600_SWITCH_BUSY
;
1118 case CURRENT_LIMIT_800
:
1119 support_mask
= CURRENT_LIMIT_800_MASK
;
1120 query_switch
= CURRENT_LIMIT_800_QUERY_SWITCH_OK
;
1121 switch_busy
= CURRENT_LIMIT_800_SWITCH_BUSY
;
1131 if (func_group
== SD_FUNC_GROUP_1
) {
1132 if (!(buf
[support_offset
] & support_mask
) ||
1133 ((buf
[query_switch_offset
] & 0x0F) != query_switch
)) {
1138 /* Check 'Busy Status' */
1139 if ((buf
[DATA_STRUCTURE_VER_OFFSET
] == 0x01) &&
1140 ((buf
[check_busy_offset
] & switch_busy
) == switch_busy
)) {
1144 return STATUS_SUCCESS
;
1147 static int sd_check_switch_mode(struct rtsx_chip
*chip
, u8 mode
, u8 func_group
,
1148 u8 func_to_switch
, u8 bus_width
)
1150 struct sd_info
*sd_card
= &chip
->sd_card
;
1154 dev_dbg(rtsx_dev(chip
), "%s (mode = %d, func_group = %d, func_to_switch = %d)\n",
1155 __func__
, mode
, func_group
, func_to_switch
);
1157 cmd
[0] = 0x40 | SWITCH
;
1160 if (func_group
== SD_FUNC_GROUP_1
) {
1163 cmd
[4] = 0xF0 + func_to_switch
;
1164 } else if (func_group
== SD_FUNC_GROUP_3
) {
1166 cmd
[3] = 0xF0 + func_to_switch
;
1168 } else if (func_group
== SD_FUNC_GROUP_4
) {
1170 cmd
[3] = 0x0F + (func_to_switch
<< 4);
1173 cmd
[1] = SD_CHECK_MODE
;
1179 retval
= sd_read_data(chip
, SD_TM_NORMAL_READ
, cmd
, 5, 64, 1, bus_width
,
1181 if (retval
!= STATUS_SUCCESS
) {
1182 rtsx_clear_sd_error(chip
);
1186 dev_dbg(rtsx_dev(chip
), "%*ph\n", 64, buf
);
1188 if (func_group
== NO_ARGUMENT
) {
1189 sd_card
->func_group1_mask
= buf
[0x0D];
1190 sd_card
->func_group2_mask
= buf
[0x0B];
1191 sd_card
->func_group3_mask
= buf
[0x09];
1192 sd_card
->func_group4_mask
= buf
[0x07];
1194 dev_dbg(rtsx_dev(chip
), "func_group1_mask = 0x%02x\n",
1196 dev_dbg(rtsx_dev(chip
), "func_group2_mask = 0x%02x\n",
1198 dev_dbg(rtsx_dev(chip
), "func_group3_mask = 0x%02x\n",
1200 dev_dbg(rtsx_dev(chip
), "func_group4_mask = 0x%02x\n",
1203 /* Maximum current consumption, check whether current is
1204 * acceptable; bit[511:496] = 0x0000 means some error happened.
1206 u16 cc
= ((u16
)buf
[0] << 8) | buf
[1];
1208 dev_dbg(rtsx_dev(chip
), "Maximum current consumption: %dmA\n",
1210 if ((cc
== 0) || (cc
> 800)) {
1214 retval
= sd_query_switch_result(chip
, func_group
,
1215 func_to_switch
, buf
, 64);
1216 if (retval
!= STATUS_SUCCESS
) {
1220 if ((cc
> 400) || (func_to_switch
> CURRENT_LIMIT_400
)) {
1221 retval
= rtsx_write_register(chip
, OCPPARA2
,
1223 chip
->sd_800mA_ocp_thd
);
1227 retval
= rtsx_write_register(chip
, CARD_PWR_CTL
,
1236 return STATUS_SUCCESS
;
1239 static u8
downgrade_switch_mode(u8 func_group
, u8 func_to_switch
)
1241 if (func_group
== SD_FUNC_GROUP_1
) {
1242 if (func_to_switch
> HS_SUPPORT
)
1245 } else if (func_group
== SD_FUNC_GROUP_4
) {
1246 if (func_to_switch
> CURRENT_LIMIT_200
)
1250 return func_to_switch
;
1253 static int sd_check_switch(struct rtsx_chip
*chip
,
1254 u8 func_group
, u8 func_to_switch
, u8 bus_width
)
1258 bool switch_good
= false;
1260 for (i
= 0; i
< 3; i
++) {
1261 if (detect_card_cd(chip
, SD_CARD
) != STATUS_SUCCESS
) {
1262 sd_set_err_code(chip
, SD_NO_CARD
);
1266 retval
= sd_check_switch_mode(chip
, SD_CHECK_MODE
, func_group
,
1267 func_to_switch
, bus_width
);
1268 if (retval
== STATUS_SUCCESS
) {
1271 retval
= sd_check_switch_mode(chip
, SD_SWITCH_MODE
,
1275 if (retval
== STATUS_SUCCESS
) {
1280 retval
= rtsx_read_register(chip
, SD_STAT1
, &stat
);
1284 if (stat
& SD_CRC16_ERR
) {
1285 dev_dbg(rtsx_dev(chip
), "SD CRC16 error when switching mode\n");
1290 func_to_switch
= downgrade_switch_mode(func_group
,
1300 return STATUS_SUCCESS
;
1303 static int sd_switch_function(struct rtsx_chip
*chip
, u8 bus_width
)
1305 struct sd_info
*sd_card
= &chip
->sd_card
;
1308 u8 func_to_switch
= 0;
1310 /* Get supported functions */
1311 retval
= sd_check_switch_mode(chip
, SD_CHECK_MODE
, NO_ARGUMENT
,
1312 NO_ARGUMENT
, bus_width
);
1313 if (retval
!= STATUS_SUCCESS
) {
1317 sd_card
->func_group1_mask
&= ~(sd_card
->sd_switch_fail
);
1319 /* Function Group 1: Access Mode */
1320 for (i
= 0; i
< 4; i
++) {
1321 switch ((u8
)(chip
->sd_speed_prior
>> (i
* 8))) {
1322 case SDR104_SUPPORT
:
1323 if ((sd_card
->func_group1_mask
& SDR104_SUPPORT_MASK
) &&
1325 func_to_switch
= SDR104_SUPPORT
;
1330 if ((sd_card
->func_group1_mask
& DDR50_SUPPORT_MASK
) &&
1332 func_to_switch
= DDR50_SUPPORT
;
1337 if ((sd_card
->func_group1_mask
& SDR50_SUPPORT_MASK
) &&
1339 func_to_switch
= SDR50_SUPPORT
;
1344 if (sd_card
->func_group1_mask
& HS_SUPPORT_MASK
)
1345 func_to_switch
= HS_SUPPORT
;
1356 dev_dbg(rtsx_dev(chip
), "SD_FUNC_GROUP_1: func_to_switch = 0x%02x",
1359 #ifdef SUPPORT_SD_LOCK
1360 if ((sd_card
->sd_lock_status
& SD_SDR_RST
) &&
1361 (func_to_switch
== DDR50_SUPPORT
) &&
1362 (sd_card
->func_group1_mask
& SDR50_SUPPORT_MASK
)) {
1363 func_to_switch
= SDR50_SUPPORT
;
1364 dev_dbg(rtsx_dev(chip
), "Using SDR50 instead of DDR50 for SD Lock\n");
1368 if (func_to_switch
) {
1369 retval
= sd_check_switch(chip
, SD_FUNC_GROUP_1
, func_to_switch
,
1371 if (retval
!= STATUS_SUCCESS
) {
1372 if (func_to_switch
== SDR104_SUPPORT
) {
1373 sd_card
->sd_switch_fail
= SDR104_SUPPORT_MASK
;
1374 } else if (func_to_switch
== DDR50_SUPPORT
) {
1375 sd_card
->sd_switch_fail
= SDR104_SUPPORT_MASK
|
1377 } else if (func_to_switch
== SDR50_SUPPORT
) {
1378 sd_card
->sd_switch_fail
= SDR104_SUPPORT_MASK
|
1379 DDR50_SUPPORT_MASK
| SDR50_SUPPORT_MASK
;
1384 if (func_to_switch
== SDR104_SUPPORT
)
1385 SET_SD_SDR104(sd_card
);
1386 else if (func_to_switch
== DDR50_SUPPORT
)
1387 SET_SD_DDR50(sd_card
);
1388 else if (func_to_switch
== SDR50_SUPPORT
)
1389 SET_SD_SDR50(sd_card
);
1394 if (CHK_SD_DDR50(sd_card
)) {
1395 retval
= rtsx_write_register(chip
, SD_PUSH_POINT_CTL
, 0x06,
1400 retval
= sd_set_sample_push_timing(chip
);
1401 if (retval
!= STATUS_SUCCESS
) {
1406 if (!func_to_switch
|| (func_to_switch
== HS_SUPPORT
)) {
1407 /* Do not try to switch current limit if the card doesn't
1408 * support UHS mode or we don't want it to support UHS mode
1410 return STATUS_SUCCESS
;
1413 /* Function Group 4: Current Limit */
1414 func_to_switch
= 0xFF;
1416 for (i
= 0; i
< 4; i
++) {
1417 switch ((u8
)(chip
->sd_current_prior
>> (i
* 8))) {
1418 case CURRENT_LIMIT_800
:
1419 if (sd_card
->func_group4_mask
& CURRENT_LIMIT_800_MASK
)
1420 func_to_switch
= CURRENT_LIMIT_800
;
1424 case CURRENT_LIMIT_600
:
1425 if (sd_card
->func_group4_mask
& CURRENT_LIMIT_600_MASK
)
1426 func_to_switch
= CURRENT_LIMIT_600
;
1430 case CURRENT_LIMIT_400
:
1431 if (sd_card
->func_group4_mask
& CURRENT_LIMIT_400_MASK
)
1432 func_to_switch
= CURRENT_LIMIT_400
;
1436 case CURRENT_LIMIT_200
:
1437 if (sd_card
->func_group4_mask
& CURRENT_LIMIT_200_MASK
)
1438 func_to_switch
= CURRENT_LIMIT_200
;
1446 if (func_to_switch
!= 0xFF)
1450 dev_dbg(rtsx_dev(chip
), "SD_FUNC_GROUP_4: func_to_switch = 0x%02x",
1453 if (func_to_switch
<= CURRENT_LIMIT_800
) {
1454 retval
= sd_check_switch(chip
, SD_FUNC_GROUP_4
, func_to_switch
,
1456 if (retval
!= STATUS_SUCCESS
) {
1457 if (sd_check_err_code(chip
, SD_NO_CARD
)) {
1461 dev_dbg(rtsx_dev(chip
), "Switch current limit finished! (%d)\n",
1465 if (CHK_SD_DDR50(sd_card
)) {
1466 retval
= rtsx_write_register(chip
, SD_PUSH_POINT_CTL
, 0x06, 0);
1472 return STATUS_SUCCESS
;
1475 static int sd_wait_data_idle(struct rtsx_chip
*chip
)
1477 int retval
= STATUS_TIMEDOUT
;
1481 for (i
= 0; i
< 100; i
++) {
1482 retval
= rtsx_read_register(chip
, SD_DATA_STATE
, &val
);
1486 if (val
& SD_DATA_IDLE
) {
1487 retval
= STATUS_SUCCESS
;
1492 dev_dbg(rtsx_dev(chip
), "SD_DATA_STATE: 0x%02x\n", val
);
1497 static int sd_sdr_tuning_rx_cmd(struct rtsx_chip
*chip
, u8 sample_point
)
1502 retval
= sd_change_phase(chip
, sample_point
, TUNE_RX
);
1503 if (retval
!= STATUS_SUCCESS
) {
1507 cmd
[0] = 0x40 | SEND_TUNING_PATTERN
;
1513 retval
= sd_read_data(chip
, SD_TM_AUTO_TUNING
, cmd
, 5, 0x40, 1,
1514 SD_BUS_WIDTH_4
, NULL
, 0, 100);
1515 if (retval
!= STATUS_SUCCESS
) {
1516 (void)sd_wait_data_idle(chip
);
1518 rtsx_clear_sd_error(chip
);
1522 return STATUS_SUCCESS
;
1525 static int sd_ddr_tuning_rx_cmd(struct rtsx_chip
*chip
, u8 sample_point
)
1527 struct sd_info
*sd_card
= &chip
->sd_card
;
1531 retval
= sd_change_phase(chip
, sample_point
, TUNE_RX
);
1532 if (retval
!= STATUS_SUCCESS
) {
1536 dev_dbg(rtsx_dev(chip
), "sd ddr tuning rx\n");
1538 retval
= sd_send_cmd_get_rsp(chip
, APP_CMD
, sd_card
->sd_addr
,
1539 SD_RSP_TYPE_R1
, NULL
, 0);
1540 if (retval
!= STATUS_SUCCESS
) {
1544 cmd
[0] = 0x40 | SD_STATUS
;
1550 retval
= sd_read_data(chip
, SD_TM_NORMAL_READ
, cmd
, 5, 64, 1,
1551 SD_BUS_WIDTH_4
, NULL
, 0, 100);
1552 if (retval
!= STATUS_SUCCESS
) {
1553 (void)sd_wait_data_idle(chip
);
1555 rtsx_clear_sd_error(chip
);
1559 return STATUS_SUCCESS
;
1562 static int mmc_ddr_tuning_rx_cmd(struct rtsx_chip
*chip
, u8 sample_point
)
1564 struct sd_info
*sd_card
= &chip
->sd_card
;
1566 u8 cmd
[5], bus_width
;
1568 if (CHK_MMC_8BIT(sd_card
))
1569 bus_width
= SD_BUS_WIDTH_8
;
1570 else if (CHK_MMC_4BIT(sd_card
))
1571 bus_width
= SD_BUS_WIDTH_4
;
1573 bus_width
= SD_BUS_WIDTH_1
;
1575 retval
= sd_change_phase(chip
, sample_point
, TUNE_RX
);
1576 if (retval
!= STATUS_SUCCESS
) {
1580 dev_dbg(rtsx_dev(chip
), "mmc ddr tuning rx\n");
1582 cmd
[0] = 0x40 | SEND_EXT_CSD
;
1588 retval
= sd_read_data(chip
, SD_TM_NORMAL_READ
, cmd
, 5, 0x200, 1,
1589 bus_width
, NULL
, 0, 100);
1590 if (retval
!= STATUS_SUCCESS
) {
1591 (void)sd_wait_data_idle(chip
);
1593 rtsx_clear_sd_error(chip
);
1597 return STATUS_SUCCESS
;
1600 static int sd_sdr_tuning_tx_cmd(struct rtsx_chip
*chip
, u8 sample_point
)
1602 struct sd_info
*sd_card
= &chip
->sd_card
;
1605 retval
= sd_change_phase(chip
, sample_point
, TUNE_TX
);
1606 if (retval
!= STATUS_SUCCESS
) {
1610 retval
= rtsx_write_register(chip
, SD_CFG3
, SD_RSP_80CLK_TIMEOUT_EN
,
1611 SD_RSP_80CLK_TIMEOUT_EN
);
1616 retval
= sd_send_cmd_get_rsp(chip
, SEND_STATUS
, sd_card
->sd_addr
,
1617 SD_RSP_TYPE_R1
, NULL
, 0);
1618 if (retval
!= STATUS_SUCCESS
) {
1619 if (sd_check_err_code(chip
, SD_RSP_TIMEOUT
)) {
1620 rtsx_write_register(chip
, SD_CFG3
,
1621 SD_RSP_80CLK_TIMEOUT_EN
, 0);
1626 retval
= rtsx_write_register(chip
, SD_CFG3
, SD_RSP_80CLK_TIMEOUT_EN
,
1632 return STATUS_SUCCESS
;
1635 static int sd_ddr_tuning_tx_cmd(struct rtsx_chip
*chip
, u8 sample_point
)
1637 struct sd_info
*sd_card
= &chip
->sd_card
;
1639 u8 cmd
[5], bus_width
;
1641 retval
= sd_change_phase(chip
, sample_point
, TUNE_TX
);
1642 if (retval
!= STATUS_SUCCESS
) {
1646 if (CHK_SD(sd_card
)) {
1647 bus_width
= SD_BUS_WIDTH_4
;
1649 if (CHK_MMC_8BIT(sd_card
))
1650 bus_width
= SD_BUS_WIDTH_8
;
1651 else if (CHK_MMC_4BIT(sd_card
))
1652 bus_width
= SD_BUS_WIDTH_4
;
1654 bus_width
= SD_BUS_WIDTH_1
;
1657 retval
= sd_wait_state_data_ready(chip
, 0x08, 1, 1000);
1658 if (retval
!= STATUS_SUCCESS
) {
1662 retval
= rtsx_write_register(chip
, SD_CFG3
, SD_RSP_80CLK_TIMEOUT_EN
,
1663 SD_RSP_80CLK_TIMEOUT_EN
);
1668 cmd
[0] = 0x40 | PROGRAM_CSD
;
1674 retval
= sd_write_data(chip
, SD_TM_AUTO_WRITE_2
, cmd
, 5, 16, 1,
1675 bus_width
, sd_card
->raw_csd
, 16, 100);
1676 if (retval
!= STATUS_SUCCESS
) {
1677 rtsx_clear_sd_error(chip
);
1678 rtsx_write_register(chip
, SD_CFG3
, SD_RSP_80CLK_TIMEOUT_EN
, 0);
1682 retval
= rtsx_write_register(chip
, SD_CFG3
, SD_RSP_80CLK_TIMEOUT_EN
,
1688 sd_send_cmd_get_rsp(chip
, SEND_STATUS
, sd_card
->sd_addr
, SD_RSP_TYPE_R1
,
1691 return STATUS_SUCCESS
;
1694 static u8
sd_search_final_phase(struct rtsx_chip
*chip
, u32 phase_map
,
1697 struct sd_info
*sd_card
= &chip
->sd_card
;
1698 struct timing_phase_path path
[MAX_PHASE
+ 1];
1699 int i
, j
, cont_path_cnt
;
1701 int max_len
, final_path_idx
;
1702 u8 final_phase
= 0xFF;
1704 if (phase_map
== 0xFFFFFFFF) {
1705 if (tune_dir
== TUNE_RX
)
1706 final_phase
= (u8
)chip
->sd_default_rx_phase
;
1708 final_phase
= (u8
)chip
->sd_default_tx_phase
;
1716 for (i
= 0; i
< MAX_PHASE
+ 1; i
++) {
1717 if (phase_map
& (1 << i
)) {
1720 j
= cont_path_cnt
++;
1728 if (cont_path_cnt
) {
1729 int idx
= cont_path_cnt
- 1;
1731 path
[idx
].len
= path
[idx
].end
-
1732 path
[idx
].start
+ 1;
1733 path
[idx
].mid
= path
[idx
].start
+
1739 if (cont_path_cnt
== 0) {
1740 dev_dbg(rtsx_dev(chip
), "No continuous phase path\n");
1743 int idx
= cont_path_cnt
- 1;
1745 path
[idx
].len
= path
[idx
].end
- path
[idx
].start
+ 1;
1746 path
[idx
].mid
= path
[idx
].start
+ path
[idx
].len
/ 2;
1749 if ((path
[0].start
== 0) &&
1750 (path
[cont_path_cnt
- 1].end
== MAX_PHASE
)) {
1751 path
[0].start
= path
[cont_path_cnt
- 1].start
- MAX_PHASE
- 1;
1752 path
[0].len
+= path
[cont_path_cnt
- 1].len
;
1753 path
[0].mid
= path
[0].start
+ path
[0].len
/ 2;
1754 if (path
[0].mid
< 0)
1755 path
[0].mid
+= MAX_PHASE
+ 1;
1763 for (i
= 0; i
< cont_path_cnt
; i
++) {
1764 if (path
[i
].len
> max_len
) {
1765 max_len
= path
[i
].len
;
1766 final_phase
= (u8
)path
[i
].mid
;
1770 dev_dbg(rtsx_dev(chip
), "path[%d].start = %d\n",
1772 dev_dbg(rtsx_dev(chip
), "path[%d].end = %d\n", i
, path
[i
].end
);
1773 dev_dbg(rtsx_dev(chip
), "path[%d].len = %d\n", i
, path
[i
].len
);
1774 dev_dbg(rtsx_dev(chip
), "path[%d].mid = %d\n", i
, path
[i
].mid
);
1775 dev_dbg(rtsx_dev(chip
), "\n");
1778 if (tune_dir
== TUNE_TX
) {
1779 if (CHK_SD_SDR104(sd_card
)) {
1781 int temp_mid
= (max_len
- 16) / 2;
1782 int temp_final_phase
=
1783 path
[final_path_idx
].end
-
1784 (max_len
- (6 + temp_mid
));
1786 if (temp_final_phase
< 0)
1787 final_phase
= (u8
)(temp_final_phase
+
1790 final_phase
= (u8
)temp_final_phase
;
1792 } else if (CHK_SD_SDR50(sd_card
)) {
1794 int temp_mid
= (max_len
- 13) / 2;
1795 int temp_final_phase
=
1796 path
[final_path_idx
].end
-
1797 (max_len
- (3 + temp_mid
));
1799 if (temp_final_phase
< 0)
1800 final_phase
= (u8
)(temp_final_phase
+
1803 final_phase
= (u8
)temp_final_phase
;
1809 dev_dbg(rtsx_dev(chip
), "Final chosen phase: %d\n", final_phase
);
1813 static int sd_tuning_rx(struct rtsx_chip
*chip
)
1815 struct sd_info
*sd_card
= &chip
->sd_card
;
1818 u32 raw_phase_map
[3], phase_map
;
1820 int (*tuning_cmd
)(struct rtsx_chip
*chip
, u8 sample_point
);
1822 if (CHK_SD(sd_card
)) {
1823 if (CHK_SD_DDR50(sd_card
))
1824 tuning_cmd
= sd_ddr_tuning_rx_cmd
;
1826 tuning_cmd
= sd_sdr_tuning_rx_cmd
;
1829 if (CHK_MMC_DDR52(sd_card
)) {
1830 tuning_cmd
= mmc_ddr_tuning_rx_cmd
;
1836 for (i
= 0; i
< 3; i
++) {
1837 raw_phase_map
[i
] = 0;
1838 for (j
= MAX_PHASE
; j
>= 0; j
--) {
1839 if (detect_card_cd(chip
, SD_CARD
) != STATUS_SUCCESS
) {
1840 sd_set_err_code(chip
, SD_NO_CARD
);
1844 retval
= tuning_cmd(chip
, (u8
)j
);
1845 if (retval
== STATUS_SUCCESS
)
1846 raw_phase_map
[i
] |= 1 << j
;
1850 phase_map
= raw_phase_map
[0] & raw_phase_map
[1] & raw_phase_map
[2];
1851 for (i
= 0; i
< 3; i
++)
1852 dev_dbg(rtsx_dev(chip
), "RX raw_phase_map[%d] = 0x%08x\n",
1853 i
, raw_phase_map
[i
]);
1855 dev_dbg(rtsx_dev(chip
), "RX phase_map = 0x%08x\n", phase_map
);
1857 final_phase
= sd_search_final_phase(chip
, phase_map
, TUNE_RX
);
1858 if (final_phase
== 0xFF) {
1862 retval
= sd_change_phase(chip
, final_phase
, TUNE_RX
);
1863 if (retval
!= STATUS_SUCCESS
) {
1867 return STATUS_SUCCESS
;
1870 static int sd_ddr_pre_tuning_tx(struct rtsx_chip
*chip
)
1872 struct sd_info
*sd_card
= &chip
->sd_card
;
1878 retval
= rtsx_write_register(chip
, SD_CFG3
, SD_RSP_80CLK_TIMEOUT_EN
,
1879 SD_RSP_80CLK_TIMEOUT_EN
);
1885 for (i
= MAX_PHASE
; i
>= 0; i
--) {
1886 if (detect_card_cd(chip
, SD_CARD
) != STATUS_SUCCESS
) {
1887 sd_set_err_code(chip
, SD_NO_CARD
);
1888 rtsx_write_register(chip
, SD_CFG3
,
1889 SD_RSP_80CLK_TIMEOUT_EN
, 0);
1893 retval
= sd_change_phase(chip
, (u8
)i
, TUNE_TX
);
1894 if (retval
!= STATUS_SUCCESS
)
1897 retval
= sd_send_cmd_get_rsp(chip
, SEND_STATUS
,
1898 sd_card
->sd_addr
, SD_RSP_TYPE_R1
,
1900 if ((retval
== STATUS_SUCCESS
) ||
1901 !sd_check_err_code(chip
, SD_RSP_TIMEOUT
))
1902 phase_map
|= 1 << i
;
1905 retval
= rtsx_write_register(chip
, SD_CFG3
, SD_RSP_80CLK_TIMEOUT_EN
,
1911 dev_dbg(rtsx_dev(chip
), "DDR TX pre tune phase_map = 0x%08x\n",
1914 final_phase
= sd_search_final_phase(chip
, phase_map
, TUNE_TX
);
1915 if (final_phase
== 0xFF) {
1919 retval
= sd_change_phase(chip
, final_phase
, TUNE_TX
);
1920 if (retval
!= STATUS_SUCCESS
) {
1924 dev_dbg(rtsx_dev(chip
), "DDR TX pre tune phase: %d\n",
1927 return STATUS_SUCCESS
;
1930 static int sd_tuning_tx(struct rtsx_chip
*chip
)
1932 struct sd_info
*sd_card
= &chip
->sd_card
;
1935 u32 raw_phase_map
[3], phase_map
;
1937 int (*tuning_cmd
)(struct rtsx_chip
*chip
, u8 sample_point
);
1939 if (CHK_SD(sd_card
)) {
1940 if (CHK_SD_DDR50(sd_card
))
1941 tuning_cmd
= sd_ddr_tuning_tx_cmd
;
1943 tuning_cmd
= sd_sdr_tuning_tx_cmd
;
1946 if (CHK_MMC_DDR52(sd_card
)) {
1947 tuning_cmd
= sd_ddr_tuning_tx_cmd
;
1953 for (i
= 0; i
< 3; i
++) {
1954 raw_phase_map
[i
] = 0;
1955 for (j
= MAX_PHASE
; j
>= 0; j
--) {
1956 if (detect_card_cd(chip
, SD_CARD
) != STATUS_SUCCESS
) {
1957 sd_set_err_code(chip
, SD_NO_CARD
);
1958 rtsx_write_register(chip
, SD_CFG3
,
1959 SD_RSP_80CLK_TIMEOUT_EN
, 0);
1963 retval
= tuning_cmd(chip
, (u8
)j
);
1964 if (retval
== STATUS_SUCCESS
)
1965 raw_phase_map
[i
] |= 1 << j
;
1969 phase_map
= raw_phase_map
[0] & raw_phase_map
[1] & raw_phase_map
[2];
1970 for (i
= 0; i
< 3; i
++)
1971 dev_dbg(rtsx_dev(chip
), "TX raw_phase_map[%d] = 0x%08x\n",
1972 i
, raw_phase_map
[i
]);
1974 dev_dbg(rtsx_dev(chip
), "TX phase_map = 0x%08x\n", phase_map
);
1976 final_phase
= sd_search_final_phase(chip
, phase_map
, TUNE_TX
);
1977 if (final_phase
== 0xFF) {
1981 retval
= sd_change_phase(chip
, final_phase
, TUNE_TX
);
1982 if (retval
!= STATUS_SUCCESS
) {
1986 return STATUS_SUCCESS
;
1989 static int sd_sdr_tuning(struct rtsx_chip
*chip
)
1993 retval
= sd_tuning_tx(chip
);
1994 if (retval
!= STATUS_SUCCESS
) {
1998 retval
= sd_tuning_rx(chip
);
1999 if (retval
!= STATUS_SUCCESS
) {
2003 return STATUS_SUCCESS
;
2006 static int sd_ddr_tuning(struct rtsx_chip
*chip
)
2010 if (!(chip
->sd_ctl
& SD_DDR_TX_PHASE_SET_BY_USER
)) {
2011 retval
= sd_ddr_pre_tuning_tx(chip
);
2012 if (retval
!= STATUS_SUCCESS
) {
2016 retval
= sd_change_phase(chip
, (u8
)chip
->sd_ddr_tx_phase
,
2018 if (retval
!= STATUS_SUCCESS
) {
2023 retval
= sd_tuning_rx(chip
);
2024 if (retval
!= STATUS_SUCCESS
) {
2028 if (!(chip
->sd_ctl
& SD_DDR_TX_PHASE_SET_BY_USER
)) {
2029 retval
= sd_tuning_tx(chip
);
2030 if (retval
!= STATUS_SUCCESS
) {
2035 return STATUS_SUCCESS
;
2038 static int mmc_ddr_tuning(struct rtsx_chip
*chip
)
2042 if (!(chip
->sd_ctl
& MMC_DDR_TX_PHASE_SET_BY_USER
)) {
2043 retval
= sd_ddr_pre_tuning_tx(chip
);
2044 if (retval
!= STATUS_SUCCESS
) {
2048 retval
= sd_change_phase(chip
, (u8
)chip
->mmc_ddr_tx_phase
,
2050 if (retval
!= STATUS_SUCCESS
) {
2055 retval
= sd_tuning_rx(chip
);
2056 if (retval
!= STATUS_SUCCESS
) {
2060 if (!(chip
->sd_ctl
& MMC_DDR_TX_PHASE_SET_BY_USER
)) {
2061 retval
= sd_tuning_tx(chip
);
2062 if (retval
!= STATUS_SUCCESS
) {
2067 return STATUS_SUCCESS
;
2070 int sd_switch_clock(struct rtsx_chip
*chip
)
2072 struct sd_info
*sd_card
= &chip
->sd_card
;
2076 retval
= select_card(chip
, SD_CARD
);
2077 if (retval
!= STATUS_SUCCESS
) {
2081 retval
= switch_clock(chip
, sd_card
->sd_clock
);
2082 if (retval
!= STATUS_SUCCESS
) {
2087 if (CHK_SD(sd_card
)) {
2088 if (CHK_SD_DDR50(sd_card
))
2089 retval
= sd_ddr_tuning(chip
);
2091 retval
= sd_sdr_tuning(chip
);
2093 if (CHK_MMC_DDR52(sd_card
))
2094 retval
= mmc_ddr_tuning(chip
);
2097 if (retval
!= STATUS_SUCCESS
) {
2102 return STATUS_SUCCESS
;
2105 static int sd_prepare_reset(struct rtsx_chip
*chip
)
2107 struct sd_info
*sd_card
= &chip
->sd_card
;
2110 if (chip
->asic_code
)
2111 sd_card
->sd_clock
= 29;
2113 sd_card
->sd_clock
= CLK_30
;
2115 sd_card
->sd_type
= 0;
2116 sd_card
->seq_mode
= 0;
2117 sd_card
->sd_data_buf_ready
= 0;
2118 sd_card
->capacity
= 0;
2120 #ifdef SUPPORT_SD_LOCK
2121 sd_card
->sd_lock_status
= 0;
2122 sd_card
->sd_erase_status
= 0;
2125 chip
->capacity
[chip
->card2lun
[SD_CARD
]] = 0;
2128 retval
= sd_set_init_para(chip
);
2129 if (retval
!= STATUS_SUCCESS
) {
2133 retval
= rtsx_write_register(chip
, REG_SD_CFG1
, 0xFF, 0x40);
2138 retval
= rtsx_write_register(chip
, CARD_STOP
, SD_STOP
| SD_CLR_ERR
,
2139 SD_STOP
| SD_CLR_ERR
);
2144 retval
= select_card(chip
, SD_CARD
);
2145 if (retval
!= STATUS_SUCCESS
) {
2149 return STATUS_SUCCESS
;
2152 static int sd_pull_ctl_disable(struct rtsx_chip
*chip
)
2156 if (CHECK_PID(chip
, 0x5208)) {
2157 retval
= rtsx_write_register(chip
, CARD_PULL_CTL1
, 0xFF,
2158 XD_D3_PD
| SD_D7_PD
| SD_CLK_PD
|
2163 retval
= rtsx_write_register(chip
, CARD_PULL_CTL2
, 0xFF,
2164 SD_D6_PD
| SD_D0_PD
| SD_D1_PD
|
2169 retval
= rtsx_write_register(chip
, CARD_PULL_CTL3
, 0xFF,
2170 SD_D4_PD
| XD_CE_PD
| XD_CLE_PD
|
2175 retval
= rtsx_write_register(chip
, CARD_PULL_CTL4
, 0xFF,
2176 XD_RDY_PD
| SD_D3_PD
| SD_D2_PD
|
2181 retval
= rtsx_write_register(chip
, CARD_PULL_CTL5
, 0xFF,
2182 MS_INS_PU
| SD_WP_PD
| SD_CD_PU
|
2187 retval
= rtsx_write_register(chip
, CARD_PULL_CTL6
, 0xFF,
2188 MS_D5_PD
| MS_D4_PD
);
2192 } else if (CHECK_PID(chip
, 0x5288)) {
2193 if (CHECK_BARO_PKG(chip
, QFN
)) {
2194 retval
= rtsx_write_register(chip
, CARD_PULL_CTL1
,
2199 retval
= rtsx_write_register(chip
, CARD_PULL_CTL2
,
2204 retval
= rtsx_write_register(chip
, CARD_PULL_CTL3
,
2209 retval
= rtsx_write_register(chip
, CARD_PULL_CTL4
,
2217 return STATUS_SUCCESS
;
2220 int sd_pull_ctl_enable(struct rtsx_chip
*chip
)
2224 rtsx_init_cmd(chip
);
2226 if (CHECK_PID(chip
, 0x5208)) {
2227 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL1
, 0xFF,
2228 XD_D3_PD
| SD_DAT7_PU
| SD_CLK_NP
| SD_D5_PU
);
2229 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL2
, 0xFF,
2230 SD_D6_PU
| SD_D0_PU
| SD_D1_PU
| XD_D5_PD
);
2231 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL3
, 0xFF,
2232 SD_D4_PU
| XD_CE_PD
| XD_CLE_PD
| XD_CD_PU
);
2233 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL4
, 0xFF,
2234 XD_RDY_PD
| SD_D3_PU
| SD_D2_PU
| XD_ALE_PD
);
2235 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL5
, 0xFF,
2236 MS_INS_PU
| SD_WP_PU
| SD_CD_PU
| SD_CMD_PU
);
2237 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL6
, 0xFF,
2238 MS_D5_PD
| MS_D4_PD
);
2239 } else if (CHECK_PID(chip
, 0x5288)) {
2240 if (CHECK_BARO_PKG(chip
, QFN
)) {
2241 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL1
, 0xFF,
2243 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL2
, 0xFF,
2245 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL3
, 0xFF,
2247 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL4
, 0xFF,
2252 retval
= rtsx_send_cmd(chip
, SD_CARD
, 100);
2257 return STATUS_SUCCESS
;
2260 static int sd_init_power(struct rtsx_chip
*chip
)
2264 retval
= sd_power_off_card3v3(chip
);
2265 if (retval
!= STATUS_SUCCESS
) {
2269 if (!chip
->ft2_fast_mode
)
2272 retval
= enable_card_clock(chip
, SD_CARD
);
2273 if (retval
!= STATUS_SUCCESS
) {
2277 if (chip
->asic_code
) {
2278 retval
= sd_pull_ctl_enable(chip
);
2279 if (retval
!= STATUS_SUCCESS
) {
2283 retval
= rtsx_write_register(chip
, FPGA_PULL_CTL
,
2284 FPGA_SD_PULL_CTL_BIT
| 0x20, 0);
2290 if (!chip
->ft2_fast_mode
) {
2291 retval
= card_power_on(chip
, SD_CARD
);
2292 if (retval
!= STATUS_SUCCESS
) {
2299 if (chip
->ocp_stat
& (SD_OC_NOW
| SD_OC_EVER
)) {
2300 dev_dbg(rtsx_dev(chip
), "Over current, OCPSTAT is 0x%x\n",
2307 retval
= rtsx_write_register(chip
, CARD_OE
, SD_OUTPUT_EN
,
2313 return STATUS_SUCCESS
;
2316 static int sd_dummy_clock(struct rtsx_chip
*chip
)
2320 retval
= rtsx_write_register(chip
, REG_SD_CFG3
, 0x01, 0x01);
2325 retval
= rtsx_write_register(chip
, REG_SD_CFG3
, 0x01, 0);
2330 return STATUS_SUCCESS
;
2333 static int sd_read_lba0(struct rtsx_chip
*chip
)
2335 struct sd_info
*sd_card
= &chip
->sd_card
;
2337 u8 cmd
[5], bus_width
;
2339 cmd
[0] = 0x40 | READ_SINGLE_BLOCK
;
2345 if (CHK_SD(sd_card
)) {
2346 bus_width
= SD_BUS_WIDTH_4
;
2348 if (CHK_MMC_8BIT(sd_card
))
2349 bus_width
= SD_BUS_WIDTH_8
;
2350 else if (CHK_MMC_4BIT(sd_card
))
2351 bus_width
= SD_BUS_WIDTH_4
;
2353 bus_width
= SD_BUS_WIDTH_1
;
2356 retval
= sd_read_data(chip
, SD_TM_NORMAL_READ
, cmd
, 5, 512, 1,
2357 bus_width
, NULL
, 0, 100);
2358 if (retval
!= STATUS_SUCCESS
) {
2359 rtsx_clear_sd_error(chip
);
2363 return STATUS_SUCCESS
;
2366 static int sd_check_wp_state(struct rtsx_chip
*chip
)
2368 struct sd_info
*sd_card
= &chip
->sd_card
;
2374 retval
= sd_send_cmd_get_rsp(chip
, APP_CMD
, sd_card
->sd_addr
,
2375 SD_RSP_TYPE_R1
, NULL
, 0);
2376 if (retval
!= STATUS_SUCCESS
) {
2380 cmd
[0] = 0x40 | SD_STATUS
;
2386 retval
= sd_read_data(chip
, SD_TM_NORMAL_READ
, cmd
, 5, 64, 1,
2387 SD_BUS_WIDTH_4
, buf
, 64, 250);
2388 if (retval
!= STATUS_SUCCESS
) {
2389 rtsx_clear_sd_error(chip
);
2391 sd_send_cmd_get_rsp(chip
, SEND_STATUS
, sd_card
->sd_addr
,
2392 SD_RSP_TYPE_R1
, NULL
, 0);
2396 dev_dbg(rtsx_dev(chip
), "ACMD13:\n");
2397 dev_dbg(rtsx_dev(chip
), "%*ph\n", 64, buf
);
2399 sd_card_type
= ((u16
)buf
[2] << 8) | buf
[3];
2400 dev_dbg(rtsx_dev(chip
), "sd_card_type = 0x%04x\n", sd_card_type
);
2401 if ((sd_card_type
== 0x0001) || (sd_card_type
== 0x0002)) {
2402 /* ROM card or OTP */
2403 chip
->card_wp
|= SD_CARD
;
2406 /* Check SD Machanical Write-Protect Switch */
2407 val
= rtsx_readl(chip
, RTSX_BIPR
);
2408 if (val
& SD_WRITE_PROTECT
)
2409 chip
->card_wp
|= SD_CARD
;
2411 return STATUS_SUCCESS
;
2414 static int reset_sd(struct rtsx_chip
*chip
)
2416 struct sd_info
*sd_card
= &chip
->sd_card
;
2417 bool hi_cap_flow
= false;
2418 int retval
, i
= 0, j
= 0, k
= 0;
2419 bool sd_dont_switch
= false;
2420 bool support_1v8
= false;
2421 bool try_sdio
= true;
2423 u8 switch_bus_width
;
2425 bool sd20_mode
= false;
2434 hi_cap_flow
= false;
2436 #ifdef SUPPORT_SD_LOCK
2437 if (sd_card
->sd_lock_status
& SD_UNLOCK_POW_ON
)
2438 goto SD_UNLOCK_ENTRY
;
2441 retval
= sd_prepare_reset(chip
);
2442 if (retval
!= STATUS_SUCCESS
)
2445 retval
= sd_dummy_clock(chip
);
2446 if (retval
!= STATUS_SUCCESS
)
2449 if (CHK_SDIO_EXIST(chip
) && !CHK_SDIO_IGNORED(chip
) && try_sdio
) {
2452 for (; rty_cnt
< chip
->sdio_retry_cnt
; rty_cnt
++) {
2453 if (detect_card_cd(chip
, SD_CARD
) != STATUS_SUCCESS
) {
2454 sd_set_err_code(chip
, SD_NO_CARD
);
2458 retval
= sd_send_cmd_get_rsp(chip
, IO_SEND_OP_COND
, 0,
2459 SD_RSP_TYPE_R4
, rsp
, 5);
2460 if (retval
== STATUS_SUCCESS
) {
2461 int func_num
= (rsp
[1] >> 4) & 0x07;
2464 dev_dbg(rtsx_dev(chip
), "SD_IO card (Function number: %d)!\n",
2473 sd_init_power(chip
);
2475 sd_dummy_clock(chip
);
2478 dev_dbg(rtsx_dev(chip
), "Normal card!\n");
2481 /* Start Initialization Process of SD Card */
2483 retval
= sd_send_cmd_get_rsp(chip
, GO_IDLE_STATE
, 0, SD_RSP_TYPE_R0
,
2485 if (retval
!= STATUS_SUCCESS
)
2490 retval
= sd_send_cmd_get_rsp(chip
, SEND_IF_COND
, 0x000001AA,
2491 SD_RSP_TYPE_R7
, rsp
, 5);
2492 if (retval
== STATUS_SUCCESS
) {
2493 if ((rsp
[4] == 0xAA) && ((rsp
[3] & 0x0f) == 0x01)) {
2495 voltage
= SUPPORT_VOLTAGE
| 0x40000000;
2500 voltage
= SUPPORT_VOLTAGE
;
2502 retval
= sd_send_cmd_get_rsp(chip
, GO_IDLE_STATE
, 0,
2503 SD_RSP_TYPE_R0
, NULL
, 0);
2504 if (retval
!= STATUS_SUCCESS
)
2511 retval
= sd_send_cmd_get_rsp(chip
, APP_CMD
, 0, SD_RSP_TYPE_R1
,
2513 if (retval
!= STATUS_SUCCESS
) {
2514 if (detect_card_cd(chip
, SD_CARD
) != STATUS_SUCCESS
) {
2515 sd_set_err_code(chip
, SD_NO_CARD
);
2526 retval
= sd_send_cmd_get_rsp(chip
, SD_APP_OP_COND
, voltage
,
2527 SD_RSP_TYPE_R3
, rsp
, 5);
2528 if (retval
!= STATUS_SUCCESS
) {
2538 } while (!(rsp
[1] & 0x80) && (i
< 255));
2545 SET_SD_HCXC(sd_card
);
2547 CLR_SD_HCXC(sd_card
);
2549 support_1v8
= false;
2551 CLR_SD_HCXC(sd_card
);
2552 support_1v8
= false;
2554 dev_dbg(rtsx_dev(chip
), "support_1v8 = %d\n", support_1v8
);
2557 retval
= sd_voltage_switch(chip
);
2558 if (retval
!= STATUS_SUCCESS
)
2562 retval
= sd_send_cmd_get_rsp(chip
, ALL_SEND_CID
, 0, SD_RSP_TYPE_R2
,
2564 if (retval
!= STATUS_SUCCESS
)
2567 for (i
= 0; i
< 3; i
++) {
2568 retval
= sd_send_cmd_get_rsp(chip
, SEND_RELATIVE_ADDR
, 0,
2569 SD_RSP_TYPE_R6
, rsp
, 5);
2570 if (retval
!= STATUS_SUCCESS
)
2573 sd_card
->sd_addr
= (u32
)rsp
[1] << 24;
2574 sd_card
->sd_addr
+= (u32
)rsp
[2] << 16;
2576 if (sd_card
->sd_addr
)
2580 retval
= sd_check_csd(chip
, 1);
2581 if (retval
!= STATUS_SUCCESS
)
2584 retval
= sd_select_card(chip
, 1);
2585 if (retval
!= STATUS_SUCCESS
)
2588 #ifdef SUPPORT_SD_LOCK
2590 retval
= sd_update_lock_status(chip
);
2591 if (retval
!= STATUS_SUCCESS
)
2594 if (sd_card
->sd_lock_status
& SD_LOCKED
) {
2595 sd_card
->sd_lock_status
|= (SD_LOCK_1BIT_MODE
| SD_PWD_EXIST
);
2596 return STATUS_SUCCESS
;
2597 } else if (!(sd_card
->sd_lock_status
& SD_UNLOCK_POW_ON
)) {
2598 sd_card
->sd_lock_status
&= ~SD_PWD_EXIST
;
2602 retval
= sd_send_cmd_get_rsp(chip
, APP_CMD
, sd_card
->sd_addr
,
2603 SD_RSP_TYPE_R1
, NULL
, 0);
2604 if (retval
!= STATUS_SUCCESS
)
2607 retval
= sd_send_cmd_get_rsp(chip
, SET_CLR_CARD_DETECT
, 0,
2608 SD_RSP_TYPE_R1
, NULL
, 0);
2609 if (retval
!= STATUS_SUCCESS
)
2613 retval
= sd_send_cmd_get_rsp(chip
, APP_CMD
, sd_card
->sd_addr
,
2614 SD_RSP_TYPE_R1
, NULL
, 0);
2615 if (retval
!= STATUS_SUCCESS
)
2618 retval
= sd_send_cmd_get_rsp(chip
, SET_BUS_WIDTH
, 2,
2619 SD_RSP_TYPE_R1
, NULL
, 0);
2620 if (retval
!= STATUS_SUCCESS
)
2623 switch_bus_width
= SD_BUS_WIDTH_4
;
2625 switch_bus_width
= SD_BUS_WIDTH_1
;
2628 retval
= sd_send_cmd_get_rsp(chip
, SET_BLOCKLEN
, 0x200, SD_RSP_TYPE_R1
,
2630 if (retval
!= STATUS_SUCCESS
)
2633 retval
= sd_set_clock_divider(chip
, SD_CLK_DIVIDE_0
);
2634 if (retval
!= STATUS_SUCCESS
)
2637 if (!(sd_card
->raw_csd
[4] & 0x40))
2638 sd_dont_switch
= true;
2640 if (!sd_dont_switch
) {
2642 /* Set sd_switch_fail here, because we needn't
2643 * switch to UHS mode
2645 sd_card
->sd_switch_fail
= SDR104_SUPPORT_MASK
|
2646 DDR50_SUPPORT_MASK
| SDR50_SUPPORT_MASK
;
2649 /* Check the card whether follow SD1.1 spec or higher */
2650 retval
= sd_check_spec(chip
, switch_bus_width
);
2651 if (retval
== STATUS_SUCCESS
) {
2652 retval
= sd_switch_function(chip
, switch_bus_width
);
2653 if (retval
!= STATUS_SUCCESS
) {
2654 sd_init_power(chip
);
2655 sd_dont_switch
= true;
2662 sd_init_power(chip
);
2663 sd_dont_switch
= true;
2672 retval
= sd_send_cmd_get_rsp(chip
, APP_CMD
, sd_card
->sd_addr
,
2673 SD_RSP_TYPE_R1
, NULL
, 0);
2674 if (retval
!= STATUS_SUCCESS
)
2677 retval
= sd_send_cmd_get_rsp(chip
, SET_BUS_WIDTH
, 2,
2678 SD_RSP_TYPE_R1
, NULL
, 0);
2679 if (retval
!= STATUS_SUCCESS
)
2683 #ifdef SUPPORT_SD_LOCK
2684 sd_card
->sd_lock_status
&= ~SD_LOCK_1BIT_MODE
;
2687 if (!sd20_mode
&& CHK_SD30_SPEED(sd_card
)) {
2690 retval
= rtsx_write_register(chip
, SD30_DRIVE_SEL
, 0x07,
2691 chip
->sd30_drive_sel_1v8
);
2696 retval
= sd_set_init_para(chip
);
2697 if (retval
!= STATUS_SUCCESS
)
2700 if (CHK_SD_DDR50(sd_card
))
2701 retval
= sd_ddr_tuning(chip
);
2703 retval
= sd_sdr_tuning(chip
);
2705 if (retval
!= STATUS_SUCCESS
) {
2709 retval
= sd_init_power(chip
);
2710 if (retval
!= STATUS_SUCCESS
)
2719 sd_send_cmd_get_rsp(chip
, SEND_STATUS
, sd_card
->sd_addr
,
2720 SD_RSP_TYPE_R1
, NULL
, 0);
2722 if (CHK_SD_DDR50(sd_card
)) {
2723 retval
= sd_wait_state_data_ready(chip
, 0x08, 1, 1000);
2724 if (retval
!= STATUS_SUCCESS
)
2729 retval
= sd_read_lba0(chip
);
2730 if (retval
!= STATUS_SUCCESS
) {
2734 retval
= sd_init_power(chip
);
2735 if (retval
!= STATUS_SUCCESS
)
2746 retval
= sd_check_wp_state(chip
);
2747 if (retval
!= STATUS_SUCCESS
)
2750 chip
->card_bus_width
[chip
->card2lun
[SD_CARD
]] = 4;
2752 #ifdef SUPPORT_SD_LOCK
2753 if (sd_card
->sd_lock_status
& SD_UNLOCK_POW_ON
) {
2754 retval
= rtsx_write_register(chip
, REG_SD_BLOCK_CNT_H
, 0xFF,
2759 retval
= rtsx_write_register(chip
, REG_SD_BLOCK_CNT_L
, 0xFF,
2767 return STATUS_SUCCESS
;
2773 static int mmc_test_switch_bus(struct rtsx_chip
*chip
, u8 width
)
2775 struct sd_info
*sd_card
= &chip
->sd_card
;
2777 u8 buf
[8] = {0}, bus_width
, *ptr
;
2781 retval
= sd_send_cmd_get_rsp(chip
, BUSTEST_W
, 0, SD_RSP_TYPE_R1
, NULL
,
2783 if (retval
!= STATUS_SUCCESS
) {
2787 if (width
== MMC_8BIT_BUS
) {
2792 bus_width
= SD_BUS_WIDTH_8
;
2797 bus_width
= SD_BUS_WIDTH_4
;
2800 retval
= rtsx_write_register(chip
, REG_SD_CFG3
, 0x02, 0x02);
2801 if (retval
!= STATUS_SUCCESS
) {
2805 retval
= sd_write_data(chip
, SD_TM_AUTO_WRITE_3
, NULL
, 0, byte_cnt
, 1,
2806 bus_width
, buf
, len
, 100);
2807 if (retval
!= STATUS_SUCCESS
) {
2808 rtsx_clear_sd_error(chip
);
2809 rtsx_write_register(chip
, REG_SD_CFG3
, 0x02, 0);
2813 retval
= rtsx_write_register(chip
, REG_SD_CFG3
, 0x02, 0);
2814 if (retval
!= STATUS_SUCCESS
) {
2818 dev_dbg(rtsx_dev(chip
), "SD/MMC CMD %d\n", BUSTEST_R
);
2820 rtsx_init_cmd(chip
);
2822 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD0
, 0xFF, 0x40 | BUSTEST_R
);
2824 if (width
== MMC_8BIT_BUS
)
2825 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BYTE_CNT_L
,
2828 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BYTE_CNT_L
,
2831 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BLOCK_CNT_L
, 0xFF, 1);
2832 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BLOCK_CNT_H
, 0xFF, 0);
2834 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CFG2
, 0xFF, SD_CALCULATE_CRC7
|
2835 SD_NO_CHECK_CRC16
| SD_NO_WAIT_BUSY_END
|
2836 SD_CHECK_CRC7
| SD_RSP_LEN_6
);
2837 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
, 0x01,
2839 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_TRANSFER
, 0xFF,
2840 SD_TM_NORMAL_READ
| SD_TRANSFER_START
);
2841 rtsx_add_cmd(chip
, CHECK_REG_CMD
, REG_SD_TRANSFER
, SD_TRANSFER_END
,
2844 rtsx_add_cmd(chip
, READ_REG_CMD
, PPBUF_BASE2
, 0, 0);
2845 if (width
== MMC_8BIT_BUS
)
2846 rtsx_add_cmd(chip
, READ_REG_CMD
, PPBUF_BASE2
+ 1, 0, 0);
2848 retval
= rtsx_send_cmd(chip
, SD_CARD
, 100);
2850 rtsx_clear_sd_error(chip
);
2854 ptr
= rtsx_get_cmd_data(chip
) + 1;
2856 if (width
== MMC_8BIT_BUS
) {
2857 dev_dbg(rtsx_dev(chip
), "BUSTEST_R [8bits]: 0x%02x 0x%02x\n",
2859 if ((ptr
[0] == 0xAA) && (ptr
[1] == 0x55)) {
2863 if (CHK_MMC_DDR52(sd_card
))
2868 retval
= sd_send_cmd_get_rsp(chip
, SWITCH
, arg
,
2869 SD_RSP_TYPE_R1b
, rsp
, 5);
2870 if ((retval
== STATUS_SUCCESS
) &&
2871 !(rsp
[4] & MMC_SWITCH_ERR
))
2872 return SWITCH_SUCCESS
;
2875 dev_dbg(rtsx_dev(chip
), "BUSTEST_R [4bits]: 0x%02x\n", ptr
[0]);
2876 if (ptr
[0] == 0xA5) {
2880 if (CHK_MMC_DDR52(sd_card
))
2885 retval
= sd_send_cmd_get_rsp(chip
, SWITCH
, arg
,
2886 SD_RSP_TYPE_R1b
, rsp
, 5);
2887 if ((retval
== STATUS_SUCCESS
) &&
2888 !(rsp
[4] & MMC_SWITCH_ERR
))
2889 return SWITCH_SUCCESS
;
2896 static int mmc_switch_timing_bus(struct rtsx_chip
*chip
, bool switch_ddr
)
2898 struct sd_info
*sd_card
= &chip
->sd_card
;
2900 u8
*ptr
, card_type
, card_type_mask
= 0;
2902 CLR_MMC_HS(sd_card
);
2904 dev_dbg(rtsx_dev(chip
), "SD/MMC CMD %d\n", SEND_EXT_CSD
);
2906 rtsx_init_cmd(chip
);
2908 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD0
, 0xFF,
2909 0x40 | SEND_EXT_CSD
);
2910 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD1
, 0xFF, 0);
2911 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD2
, 0xFF, 0);
2912 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD3
, 0xFF, 0);
2913 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD4
, 0xFF, 0);
2915 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BYTE_CNT_L
, 0xFF, 0);
2916 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BYTE_CNT_H
, 0xFF, 2);
2917 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BLOCK_CNT_L
, 0xFF, 1);
2918 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BLOCK_CNT_H
, 0xFF, 0);
2920 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CFG2
, 0xFF,
2921 SD_CALCULATE_CRC7
| SD_CHECK_CRC16
| SD_NO_WAIT_BUSY_END
|
2922 SD_CHECK_CRC7
| SD_RSP_LEN_6
);
2923 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
, 0x01,
2925 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_TRANSFER
, 0xFF,
2926 SD_TM_NORMAL_READ
| SD_TRANSFER_START
);
2927 rtsx_add_cmd(chip
, CHECK_REG_CMD
, REG_SD_TRANSFER
, SD_TRANSFER_END
,
2930 rtsx_add_cmd(chip
, READ_REG_CMD
, PPBUF_BASE2
+ 196, 0xFF, 0);
2931 rtsx_add_cmd(chip
, READ_REG_CMD
, PPBUF_BASE2
+ 212, 0xFF, 0);
2932 rtsx_add_cmd(chip
, READ_REG_CMD
, PPBUF_BASE2
+ 213, 0xFF, 0);
2933 rtsx_add_cmd(chip
, READ_REG_CMD
, PPBUF_BASE2
+ 214, 0xFF, 0);
2934 rtsx_add_cmd(chip
, READ_REG_CMD
, PPBUF_BASE2
+ 215, 0xFF, 0);
2936 retval
= rtsx_send_cmd(chip
, SD_CARD
, 1000);
2938 if (retval
== -ETIMEDOUT
) {
2939 rtsx_clear_sd_error(chip
);
2940 sd_send_cmd_get_rsp(chip
, SEND_STATUS
, sd_card
->sd_addr
,
2941 SD_RSP_TYPE_R1
, NULL
, 0);
2946 ptr
= rtsx_get_cmd_data(chip
);
2947 if (ptr
[0] & SD_TRANSFER_ERR
) {
2948 sd_send_cmd_get_rsp(chip
, SEND_STATUS
, sd_card
->sd_addr
,
2949 SD_RSP_TYPE_R1
, NULL
, 0);
2953 if (CHK_MMC_SECTOR_MODE(sd_card
)) {
2954 sd_card
->capacity
= ((u32
)ptr
[5] << 24) | ((u32
)ptr
[4] << 16) |
2955 ((u32
)ptr
[3] << 8) | ((u32
)ptr
[2]);
2958 card_type_mask
= 0x03;
2959 card_type
= ptr
[1] & card_type_mask
;
2963 if (card_type
& 0x04) {
2965 SET_MMC_DDR52(sd_card
);
2967 SET_MMC_52M(sd_card
);
2968 } else if (card_type
& 0x02) {
2969 SET_MMC_52M(sd_card
);
2971 SET_MMC_26M(sd_card
);
2974 retval
= sd_send_cmd_get_rsp(chip
, SWITCH
, 0x03B90100,
2975 SD_RSP_TYPE_R1b
, rsp
, 5);
2976 if ((retval
!= STATUS_SUCCESS
) || (rsp
[4] & MMC_SWITCH_ERR
))
2977 CLR_MMC_HS(sd_card
);
2980 sd_choose_proper_clock(chip
);
2981 retval
= switch_clock(chip
, sd_card
->sd_clock
);
2982 if (retval
!= STATUS_SUCCESS
) {
2986 /* Test Bus Procedure */
2987 retval
= mmc_test_switch_bus(chip
, MMC_8BIT_BUS
);
2988 if (retval
== SWITCH_SUCCESS
) {
2989 SET_MMC_8BIT(sd_card
);
2990 chip
->card_bus_width
[chip
->card2lun
[SD_CARD
]] = 8;
2991 #ifdef SUPPORT_SD_LOCK
2992 sd_card
->sd_lock_status
&= ~SD_LOCK_1BIT_MODE
;
2994 } else if (retval
== SWITCH_FAIL
) {
2995 retval
= mmc_test_switch_bus(chip
, MMC_4BIT_BUS
);
2996 if (retval
== SWITCH_SUCCESS
) {
2997 SET_MMC_4BIT(sd_card
);
2998 chip
->card_bus_width
[chip
->card2lun
[SD_CARD
]] = 4;
2999 #ifdef SUPPORT_SD_LOCK
3000 sd_card
->sd_lock_status
&= ~SD_LOCK_1BIT_MODE
;
3002 } else if (retval
== SWITCH_FAIL
) {
3003 CLR_MMC_8BIT(sd_card
);
3004 CLR_MMC_4BIT(sd_card
);
3012 return STATUS_SUCCESS
;
3015 static int reset_mmc(struct rtsx_chip
*chip
)
3017 struct sd_info
*sd_card
= &chip
->sd_card
;
3018 int retval
, i
= 0, j
= 0, k
= 0;
3019 bool switch_ddr
= true;
3024 #ifdef SUPPORT_SD_LOCK
3025 if (sd_card
->sd_lock_status
& SD_UNLOCK_POW_ON
)
3026 goto MMC_UNLOCK_ENTRY
;
3030 retval
= sd_prepare_reset(chip
);
3031 if (retval
!= STATUS_SUCCESS
) {
3038 retval
= sd_send_cmd_get_rsp(chip
, GO_IDLE_STATE
, 0, SD_RSP_TYPE_R0
,
3040 if (retval
!= STATUS_SUCCESS
) {
3045 if (detect_card_cd(chip
, SD_CARD
) != STATUS_SUCCESS
) {
3046 sd_set_err_code(chip
, SD_NO_CARD
);
3050 retval
= sd_send_cmd_get_rsp(chip
, SEND_OP_COND
,
3051 (SUPPORT_VOLTAGE
| 0x40000000),
3052 SD_RSP_TYPE_R3
, rsp
, 5);
3053 if (retval
!= STATUS_SUCCESS
) {
3054 if (sd_check_err_code(chip
, SD_BUSY
) ||
3055 sd_check_err_code(chip
, SD_TO_ERR
)) {
3058 sd_clr_err_code(chip
);
3066 sd_clr_err_code(chip
);
3076 } while (!(rsp
[1] & 0x80) && (i
< 255));
3082 if ((rsp
[1] & 0x60) == 0x40)
3083 SET_MMC_SECTOR_MODE(sd_card
);
3085 CLR_MMC_SECTOR_MODE(sd_card
);
3087 retval
= sd_send_cmd_get_rsp(chip
, ALL_SEND_CID
, 0, SD_RSP_TYPE_R2
,
3089 if (retval
!= STATUS_SUCCESS
) {
3093 sd_card
->sd_addr
= 0x00100000;
3094 retval
= sd_send_cmd_get_rsp(chip
, SET_RELATIVE_ADDR
, sd_card
->sd_addr
,
3095 SD_RSP_TYPE_R6
, rsp
, 5);
3096 if (retval
!= STATUS_SUCCESS
) {
3100 retval
= sd_check_csd(chip
, 1);
3101 if (retval
!= STATUS_SUCCESS
) {
3105 spec_ver
= (sd_card
->raw_csd
[0] & 0x3C) >> 2;
3107 retval
= sd_select_card(chip
, 1);
3108 if (retval
!= STATUS_SUCCESS
) {
3112 retval
= sd_send_cmd_get_rsp(chip
, SET_BLOCKLEN
, 0x200, SD_RSP_TYPE_R1
,
3114 if (retval
!= STATUS_SUCCESS
) {
3118 #ifdef SUPPORT_SD_LOCK
3120 retval
= sd_update_lock_status(chip
);
3121 if (retval
!= STATUS_SUCCESS
) {
3126 retval
= sd_set_clock_divider(chip
, SD_CLK_DIVIDE_0
);
3127 if (retval
!= STATUS_SUCCESS
) {
3131 chip
->card_bus_width
[chip
->card2lun
[SD_CARD
]] = 1;
3133 if (!sd_card
->mmc_dont_switch_bus
) {
3134 if (spec_ver
== 4) {
3136 retval
= mmc_switch_timing_bus(chip
, switch_ddr
);
3137 if (retval
!= STATUS_SUCCESS
) {
3138 retval
= sd_init_power(chip
);
3139 if (retval
!= STATUS_SUCCESS
) {
3142 sd_card
->mmc_dont_switch_bus
= 1;
3147 if (CHK_MMC_SECTOR_MODE(sd_card
) && (sd_card
->capacity
== 0)) {
3151 if (switch_ddr
&& CHK_MMC_DDR52(sd_card
)) {
3152 retval
= sd_set_init_para(chip
);
3153 if (retval
!= STATUS_SUCCESS
) {
3157 retval
= mmc_ddr_tuning(chip
);
3158 if (retval
!= STATUS_SUCCESS
) {
3159 retval
= sd_init_power(chip
);
3160 if (retval
!= STATUS_SUCCESS
) {
3168 retval
= sd_wait_state_data_ready(chip
, 0x08, 1, 1000);
3169 if (retval
== STATUS_SUCCESS
) {
3170 retval
= sd_read_lba0(chip
);
3171 if (retval
!= STATUS_SUCCESS
) {
3172 retval
= sd_init_power(chip
);
3173 if (retval
!= STATUS_SUCCESS
) {
3184 #ifdef SUPPORT_SD_LOCK
3185 if (sd_card
->sd_lock_status
& SD_UNLOCK_POW_ON
) {
3186 retval
= rtsx_write_register(chip
, REG_SD_BLOCK_CNT_H
, 0xFF,
3191 retval
= rtsx_write_register(chip
, REG_SD_BLOCK_CNT_L
, 0xFF,
3199 temp
= rtsx_readl(chip
, RTSX_BIPR
);
3200 if (temp
& SD_WRITE_PROTECT
)
3201 chip
->card_wp
|= SD_CARD
;
3203 return STATUS_SUCCESS
;
3206 int reset_sd_card(struct rtsx_chip
*chip
)
3208 struct sd_info
*sd_card
= &chip
->sd_card
;
3211 sd_init_reg_addr(chip
);
3213 memset(sd_card
, 0, sizeof(struct sd_info
));
3214 chip
->capacity
[chip
->card2lun
[SD_CARD
]] = 0;
3216 retval
= enable_card_clock(chip
, SD_CARD
);
3217 if (retval
!= STATUS_SUCCESS
) {
3221 if (chip
->ignore_sd
&& CHK_SDIO_EXIST(chip
) &&
3222 !CHK_SDIO_IGNORED(chip
)) {
3223 if (chip
->asic_code
) {
3224 retval
= sd_pull_ctl_enable(chip
);
3225 if (retval
!= STATUS_SUCCESS
) {
3229 retval
= rtsx_write_register(chip
, FPGA_PULL_CTL
,
3230 FPGA_SD_PULL_CTL_BIT
|
3232 if (retval
!= STATUS_SUCCESS
) {
3236 retval
= card_share_mode(chip
, SD_CARD
);
3237 if (retval
!= STATUS_SUCCESS
) {
3245 retval
= sd_init_power(chip
);
3246 if (retval
!= STATUS_SUCCESS
) {
3250 if (chip
->sd_ctl
& RESET_MMC_FIRST
) {
3251 retval
= reset_mmc(chip
);
3252 if (retval
!= STATUS_SUCCESS
) {
3253 if (sd_check_err_code(chip
, SD_NO_CARD
)) {
3257 retval
= reset_sd(chip
);
3258 if (retval
!= STATUS_SUCCESS
) {
3263 retval
= reset_sd(chip
);
3264 if (retval
!= STATUS_SUCCESS
) {
3265 if (sd_check_err_code(chip
, SD_NO_CARD
)) {
3272 retval
= reset_mmc(chip
);
3273 if (retval
!= STATUS_SUCCESS
) {
3279 retval
= sd_set_clock_divider(chip
, SD_CLK_DIVIDE_0
);
3280 if (retval
!= STATUS_SUCCESS
) {
3284 retval
= rtsx_write_register(chip
, REG_SD_BYTE_CNT_L
, 0xFF, 0);
3288 retval
= rtsx_write_register(chip
, REG_SD_BYTE_CNT_H
, 0xFF, 2);
3293 chip
->capacity
[chip
->card2lun
[SD_CARD
]] = sd_card
->capacity
;
3295 retval
= sd_set_init_para(chip
);
3296 if (retval
!= STATUS_SUCCESS
) {
3300 dev_dbg(rtsx_dev(chip
), "sd_card->sd_type = 0x%x\n", sd_card
->sd_type
);
3302 return STATUS_SUCCESS
;
3305 static int reset_mmc_only(struct rtsx_chip
*chip
)
3307 struct sd_info
*sd_card
= &chip
->sd_card
;
3310 sd_card
->sd_type
= 0;
3311 sd_card
->seq_mode
= 0;
3312 sd_card
->sd_data_buf_ready
= 0;
3313 sd_card
->capacity
= 0;
3314 sd_card
->sd_switch_fail
= 0;
3316 #ifdef SUPPORT_SD_LOCK
3317 sd_card
->sd_lock_status
= 0;
3318 sd_card
->sd_erase_status
= 0;
3321 chip
->capacity
[chip
->card2lun
[SD_CARD
]] = sd_card
->capacity
= 0;
3323 retval
= enable_card_clock(chip
, SD_CARD
);
3324 if (retval
!= STATUS_SUCCESS
) {
3328 retval
= sd_init_power(chip
);
3329 if (retval
!= STATUS_SUCCESS
) {
3333 retval
= reset_mmc(chip
);
3334 if (retval
!= STATUS_SUCCESS
) {
3338 retval
= sd_set_clock_divider(chip
, SD_CLK_DIVIDE_0
);
3339 if (retval
!= STATUS_SUCCESS
) {
3343 retval
= rtsx_write_register(chip
, REG_SD_BYTE_CNT_L
, 0xFF, 0);
3347 retval
= rtsx_write_register(chip
, REG_SD_BYTE_CNT_H
, 0xFF, 2);
3352 chip
->capacity
[chip
->card2lun
[SD_CARD
]] = sd_card
->capacity
;
3354 retval
= sd_set_init_para(chip
);
3355 if (retval
!= STATUS_SUCCESS
) {
3359 dev_dbg(rtsx_dev(chip
), "In %s, sd_card->sd_type = 0x%x\n",
3360 __func__
, sd_card
->sd_type
);
3362 return STATUS_SUCCESS
;
3365 #define WAIT_DATA_READY_RTY_CNT 255
3367 static int wait_data_buf_ready(struct rtsx_chip
*chip
)
3369 struct sd_info
*sd_card
= &chip
->sd_card
;
3372 for (i
= 0; i
< WAIT_DATA_READY_RTY_CNT
; i
++) {
3373 if (detect_card_cd(chip
, SD_CARD
) != STATUS_SUCCESS
) {
3374 sd_set_err_code(chip
, SD_NO_CARD
);
3378 sd_card
->sd_data_buf_ready
= 0;
3380 retval
= sd_send_cmd_get_rsp(chip
, SEND_STATUS
,
3381 sd_card
->sd_addr
, SD_RSP_TYPE_R1
,
3383 if (retval
!= STATUS_SUCCESS
) {
3387 if (sd_card
->sd_data_buf_ready
) {
3388 return sd_send_cmd_get_rsp(chip
, SEND_STATUS
,
3389 sd_card
->sd_addr
, SD_RSP_TYPE_R1
, NULL
, 0);
3393 sd_set_err_code(chip
, SD_TO_ERR
);
3398 void sd_stop_seq_mode(struct rtsx_chip
*chip
)
3400 struct sd_info
*sd_card
= &chip
->sd_card
;
3403 if (sd_card
->seq_mode
) {
3404 retval
= sd_switch_clock(chip
);
3405 if (retval
!= STATUS_SUCCESS
)
3408 retval
= sd_send_cmd_get_rsp(chip
, STOP_TRANSMISSION
, 0,
3409 SD_RSP_TYPE_R1b
, NULL
, 0);
3410 if (retval
!= STATUS_SUCCESS
)
3411 sd_set_err_code(chip
, SD_STS_ERR
);
3413 retval
= sd_wait_state_data_ready(chip
, 0x08, 1, 1000);
3414 if (retval
!= STATUS_SUCCESS
)
3415 sd_set_err_code(chip
, SD_STS_ERR
);
3417 sd_card
->seq_mode
= 0;
3419 rtsx_write_register(chip
, RBCTL
, RB_FLUSH
, RB_FLUSH
);
3423 static inline int sd_auto_tune_clock(struct rtsx_chip
*chip
)
3425 struct sd_info
*sd_card
= &chip
->sd_card
;
3428 if (chip
->asic_code
) {
3429 if (sd_card
->sd_clock
> 30)
3430 sd_card
->sd_clock
-= 20;
3432 switch (sd_card
->sd_clock
) {
3434 sd_card
->sd_clock
= CLK_150
;
3438 sd_card
->sd_clock
= CLK_120
;
3442 sd_card
->sd_clock
= CLK_100
;
3446 sd_card
->sd_clock
= CLK_80
;
3450 sd_card
->sd_clock
= CLK_60
;
3454 sd_card
->sd_clock
= CLK_50
;
3462 retval
= sd_switch_clock(chip
);
3463 if (retval
!= STATUS_SUCCESS
) {
3467 return STATUS_SUCCESS
;
3470 int sd_rw(struct scsi_cmnd
*srb
, struct rtsx_chip
*chip
, u32 start_sector
,
3473 struct sd_info
*sd_card
= &chip
->sd_card
;
3478 if (srb
->sc_data_direction
== DMA_FROM_DEVICE
) {
3479 dev_dbg(rtsx_dev(chip
), "%s: Read %d %s from 0x%x\n", __func__
,
3480 sector_cnt
, (sector_cnt
> 1) ? "sectors" : "sector",
3483 dev_dbg(rtsx_dev(chip
), "%s: Write %d %s to 0x%x\n", __func__
,
3484 sector_cnt
, (sector_cnt
> 1) ? "sectors" : "sector",
3488 sd_card
->cleanup_counter
= 0;
3490 if (!(chip
->card_ready
& SD_CARD
)) {
3491 sd_card
->seq_mode
= 0;
3493 retval
= reset_sd_card(chip
);
3494 if (retval
== STATUS_SUCCESS
) {
3495 chip
->card_ready
|= SD_CARD
;
3496 chip
->card_fail
&= ~SD_CARD
;
3498 chip
->card_ready
&= ~SD_CARD
;
3499 chip
->card_fail
|= SD_CARD
;
3500 chip
->capacity
[chip
->card2lun
[SD_CARD
]] = 0;
3501 chip
->rw_need_retry
= 1;
3506 if (!CHK_SD_HCXC(sd_card
) && !CHK_MMC_SECTOR_MODE(sd_card
))
3507 data_addr
= start_sector
<< 9;
3509 data_addr
= start_sector
;
3511 sd_clr_err_code(chip
);
3513 retval
= sd_switch_clock(chip
);
3514 if (retval
!= STATUS_SUCCESS
) {
3515 sd_set_err_code(chip
, SD_IO_ERR
);
3519 if (sd_card
->seq_mode
&&
3520 ((sd_card
->pre_dir
!= srb
->sc_data_direction
) ||
3521 ((sd_card
->pre_sec_addr
+ sd_card
->pre_sec_cnt
) !=
3523 if ((sd_card
->pre_sec_cnt
< 0x80) &&
3524 (sd_card
->pre_dir
== DMA_FROM_DEVICE
) &&
3525 !CHK_SD30_SPEED(sd_card
) &&
3526 !CHK_SD_HS(sd_card
) &&
3527 !CHK_MMC_HS(sd_card
)) {
3528 sd_send_cmd_get_rsp(chip
, SEND_STATUS
, sd_card
->sd_addr
,
3529 SD_RSP_TYPE_R1
, NULL
, 0);
3532 retval
= sd_send_cmd_get_rsp(chip
, STOP_TRANSMISSION
, 0,
3533 SD_RSP_TYPE_R1b
, NULL
, 0);
3534 if (retval
!= STATUS_SUCCESS
) {
3535 chip
->rw_need_retry
= 1;
3536 sd_set_err_code(chip
, SD_STS_ERR
);
3540 sd_card
->seq_mode
= 0;
3542 retval
= rtsx_write_register(chip
, RBCTL
, RB_FLUSH
, RB_FLUSH
);
3543 if (retval
!= STATUS_SUCCESS
) {
3544 sd_set_err_code(chip
, SD_IO_ERR
);
3548 if ((sd_card
->pre_sec_cnt
< 0x80) &&
3549 !CHK_SD30_SPEED(sd_card
) &&
3550 !CHK_SD_HS(sd_card
) &&
3551 !CHK_MMC_HS(sd_card
)) {
3552 sd_send_cmd_get_rsp(chip
, SEND_STATUS
, sd_card
->sd_addr
,
3553 SD_RSP_TYPE_R1
, NULL
, 0);
3557 rtsx_init_cmd(chip
);
3559 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BYTE_CNT_L
, 0xFF, 0x00);
3560 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BYTE_CNT_H
, 0xFF, 0x02);
3561 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BLOCK_CNT_L
, 0xFF,
3563 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BLOCK_CNT_H
, 0xFF,
3564 (u8
)(sector_cnt
>> 8));
3566 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
, 0x01, RING_BUFFER
);
3568 if (CHK_MMC_8BIT(sd_card
))
3569 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CFG1
,
3570 0x03, SD_BUS_WIDTH_8
);
3571 else if (CHK_MMC_4BIT(sd_card
) || CHK_SD(sd_card
))
3572 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CFG1
,
3573 0x03, SD_BUS_WIDTH_4
);
3575 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CFG1
,
3576 0x03, SD_BUS_WIDTH_1
);
3578 if (sd_card
->seq_mode
) {
3579 cfg2
= SD_NO_CALCULATE_CRC7
| SD_CHECK_CRC16
|
3580 SD_NO_WAIT_BUSY_END
| SD_NO_CHECK_CRC7
|
3582 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CFG2
, 0xFF, cfg2
);
3584 trans_dma_enable(srb
->sc_data_direction
, chip
, sector_cnt
* 512,
3587 if (srb
->sc_data_direction
== DMA_FROM_DEVICE
) {
3588 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_TRANSFER
, 0xFF,
3589 SD_TM_AUTO_READ_3
| SD_TRANSFER_START
);
3591 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_TRANSFER
, 0xFF,
3592 SD_TM_AUTO_WRITE_3
| SD_TRANSFER_START
);
3595 rtsx_add_cmd(chip
, CHECK_REG_CMD
, REG_SD_TRANSFER
,
3596 SD_TRANSFER_END
, SD_TRANSFER_END
);
3598 rtsx_send_cmd_no_wait(chip
);
3600 if (srb
->sc_data_direction
== DMA_FROM_DEVICE
) {
3601 dev_dbg(rtsx_dev(chip
), "SD/MMC CMD %d\n",
3602 READ_MULTIPLE_BLOCK
);
3603 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD0
, 0xFF,
3604 0x40 | READ_MULTIPLE_BLOCK
);
3605 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD1
, 0xFF,
3606 (u8
)(data_addr
>> 24));
3607 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD2
, 0xFF,
3608 (u8
)(data_addr
>> 16));
3609 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD3
, 0xFF,
3610 (u8
)(data_addr
>> 8));
3611 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD4
, 0xFF,
3614 cfg2
= SD_CALCULATE_CRC7
| SD_CHECK_CRC16
|
3615 SD_NO_WAIT_BUSY_END
| SD_CHECK_CRC7
|
3617 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CFG2
, 0xFF,
3620 trans_dma_enable(srb
->sc_data_direction
, chip
,
3621 sector_cnt
* 512, DMA_512
);
3623 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_TRANSFER
, 0xFF,
3624 SD_TM_AUTO_READ_2
| SD_TRANSFER_START
);
3625 rtsx_add_cmd(chip
, CHECK_REG_CMD
, REG_SD_TRANSFER
,
3626 SD_TRANSFER_END
, SD_TRANSFER_END
);
3628 rtsx_send_cmd_no_wait(chip
);
3630 retval
= rtsx_send_cmd(chip
, SD_CARD
, 50);
3632 rtsx_clear_sd_error(chip
);
3634 chip
->rw_need_retry
= 1;
3635 sd_set_err_code(chip
, SD_TO_ERR
);
3639 retval
= wait_data_buf_ready(chip
);
3640 if (retval
!= STATUS_SUCCESS
) {
3641 chip
->rw_need_retry
= 1;
3642 sd_set_err_code(chip
, SD_TO_ERR
);
3646 retval
= sd_send_cmd_get_rsp(chip
, WRITE_MULTIPLE_BLOCK
,
3647 data_addr
, SD_RSP_TYPE_R1
,
3649 if (retval
!= STATUS_SUCCESS
) {
3650 chip
->rw_need_retry
= 1;
3654 rtsx_init_cmd(chip
);
3656 cfg2
= SD_NO_CALCULATE_CRC7
| SD_CHECK_CRC16
|
3657 SD_NO_WAIT_BUSY_END
|
3658 SD_NO_CHECK_CRC7
| SD_RSP_LEN_0
;
3659 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CFG2
, 0xFF,
3662 trans_dma_enable(srb
->sc_data_direction
, chip
,
3663 sector_cnt
* 512, DMA_512
);
3665 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_TRANSFER
, 0xFF,
3666 SD_TM_AUTO_WRITE_3
| SD_TRANSFER_START
);
3667 rtsx_add_cmd(chip
, CHECK_REG_CMD
, REG_SD_TRANSFER
,
3668 SD_TRANSFER_END
, SD_TRANSFER_END
);
3670 rtsx_send_cmd_no_wait(chip
);
3673 sd_card
->seq_mode
= 1;
3676 retval
= rtsx_transfer_data(chip
, SD_CARD
, scsi_sglist(srb
),
3677 scsi_bufflen(srb
), scsi_sg_count(srb
),
3678 srb
->sc_data_direction
, chip
->sd_timeout
);
3683 sd_card
->seq_mode
= 0;
3685 if (retval
== -ETIMEDOUT
)
3686 err
= STATUS_TIMEDOUT
;
3690 rtsx_read_register(chip
, REG_SD_STAT1
, &stat
);
3691 rtsx_clear_sd_error(chip
);
3692 if (detect_card_cd(chip
, SD_CARD
) != STATUS_SUCCESS
) {
3693 chip
->rw_need_retry
= 0;
3694 dev_dbg(rtsx_dev(chip
), "No card exist, exit %s\n",
3699 chip
->rw_need_retry
= 1;
3701 retval
= sd_send_cmd_get_rsp(chip
, STOP_TRANSMISSION
, 0,
3702 SD_RSP_TYPE_R1b
, NULL
, 0);
3703 if (retval
!= STATUS_SUCCESS
) {
3704 sd_set_err_code(chip
, SD_STS_ERR
);
3708 if (stat
& (SD_CRC7_ERR
| SD_CRC16_ERR
| SD_CRC_WRITE_ERR
)) {
3709 dev_dbg(rtsx_dev(chip
), "SD CRC error, tune clock!\n");
3710 sd_set_err_code(chip
, SD_CRC_ERR
);
3714 if (err
== STATUS_TIMEDOUT
) {
3715 sd_set_err_code(chip
, SD_TO_ERR
);
3722 sd_card
->pre_sec_addr
= start_sector
;
3723 sd_card
->pre_sec_cnt
= sector_cnt
;
3724 sd_card
->pre_dir
= srb
->sc_data_direction
;
3726 return STATUS_SUCCESS
;
3729 sd_card
->seq_mode
= 0;
3731 if (detect_card_cd(chip
, SD_CARD
) != STATUS_SUCCESS
) {
3732 chip
->rw_need_retry
= 0;
3733 dev_dbg(rtsx_dev(chip
), "No card exist, exit %s\n", __func__
);
3737 if (sd_check_err_code(chip
, SD_CRC_ERR
)) {
3738 if (CHK_MMC_4BIT(sd_card
) || CHK_MMC_8BIT(sd_card
)) {
3739 sd_card
->mmc_dont_switch_bus
= 1;
3740 reset_mmc_only(chip
);
3741 sd_card
->mmc_dont_switch_bus
= 0;
3743 sd_card
->need_retune
= 1;
3744 sd_auto_tune_clock(chip
);
3746 } else if (sd_check_err_code(chip
, SD_TO_ERR
| SD_STS_ERR
)) {
3747 retval
= reset_sd_card(chip
);
3748 if (retval
!= STATUS_SUCCESS
) {
3749 chip
->card_ready
&= ~SD_CARD
;
3750 chip
->card_fail
|= SD_CARD
;
3751 chip
->capacity
[chip
->card2lun
[SD_CARD
]] = 0;
3759 int soft_reset_sd_card(struct rtsx_chip
*chip
)
3761 return reset_sd(chip
);
3764 int ext_sd_send_cmd_get_rsp(struct rtsx_chip
*chip
, u8 cmd_idx
, u32 arg
,
3765 u8 rsp_type
, u8
*rsp
, int rsp_len
,
3775 dev_dbg(rtsx_dev(chip
), "EXT SD/MMC CMD %d\n", cmd_idx
);
3777 if (rsp_type
== SD_RSP_TYPE_R1b
)
3782 rtsx_init_cmd(chip
);
3784 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD0
, 0xFF, 0x40 | cmd_idx
);
3785 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD1
, 0xFF, (u8
)(arg
>> 24));
3786 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD2
, 0xFF, (u8
)(arg
>> 16));
3787 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD3
, 0xFF, (u8
)(arg
>> 8));
3788 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD4
, 0xFF, (u8
)arg
);
3790 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CFG2
, 0xFF, rsp_type
);
3791 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
,
3792 0x01, PINGPONG_BUFFER
);
3793 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_TRANSFER
,
3794 0xFF, SD_TM_CMD_RSP
| SD_TRANSFER_START
);
3795 rtsx_add_cmd(chip
, CHECK_REG_CMD
, REG_SD_TRANSFER
, SD_TRANSFER_END
,
3798 if (rsp_type
== SD_RSP_TYPE_R2
) {
3799 for (reg_addr
= PPBUF_BASE2
; reg_addr
< PPBUF_BASE2
+ 16;
3801 rtsx_add_cmd(chip
, READ_REG_CMD
, reg_addr
, 0, 0);
3804 } else if (rsp_type
!= SD_RSP_TYPE_R0
) {
3805 for (reg_addr
= REG_SD_CMD0
; reg_addr
<= REG_SD_CMD4
;
3807 rtsx_add_cmd(chip
, READ_REG_CMD
, reg_addr
, 0, 0);
3811 rtsx_add_cmd(chip
, READ_REG_CMD
, REG_SD_CMD5
, 0, 0);
3813 rtsx_add_cmd(chip
, READ_REG_CMD
, REG_SD_STAT1
, 0, 0);
3815 retval
= rtsx_send_cmd(chip
, SD_CARD
, timeout
);
3817 if (retval
== -ETIMEDOUT
) {
3818 rtsx_clear_sd_error(chip
);
3820 if (rsp_type
& SD_WAIT_BUSY_END
) {
3821 retval
= sd_check_data0_status(chip
);
3822 if (retval
!= STATUS_SUCCESS
) {
3826 sd_set_err_code(chip
, SD_TO_ERR
);
3832 if (rsp_type
== SD_RSP_TYPE_R0
)
3833 return STATUS_SUCCESS
;
3835 ptr
= rtsx_get_cmd_data(chip
) + 1;
3837 if ((ptr
[0] & 0xC0) != 0) {
3838 sd_set_err_code(chip
, SD_STS_ERR
);
3842 if (!(rsp_type
& SD_NO_CHECK_CRC7
)) {
3843 if (ptr
[stat_idx
] & SD_CRC7_ERR
) {
3844 if (cmd_idx
== WRITE_MULTIPLE_BLOCK
) {
3845 sd_set_err_code(chip
, SD_CRC_ERR
);
3848 if (rty_cnt
< SD_MAX_RETRY_COUNT
) {
3853 sd_set_err_code(chip
, SD_CRC_ERR
);
3859 if ((cmd_idx
== SELECT_CARD
) || (cmd_idx
== APP_CMD
) ||
3860 (cmd_idx
== SEND_STATUS
) || (cmd_idx
== STOP_TRANSMISSION
)) {
3861 if ((cmd_idx
!= STOP_TRANSMISSION
) && !special_check
) {
3862 if (ptr
[1] & 0x80) {
3866 #ifdef SUPPORT_SD_LOCK
3867 if (ptr
[1] & 0x7D) {
3869 if (ptr
[1] & 0x7F) {
3873 if (ptr
[2] & 0xF8) {
3877 if (cmd_idx
== SELECT_CARD
) {
3878 if (rsp_type
== SD_RSP_TYPE_R2
) {
3879 if ((ptr
[3] & 0x1E) != 0x04) {
3887 memcpy(rsp
, ptr
, rsp_len
);
3889 return STATUS_SUCCESS
;
3892 int ext_sd_get_rsp(struct rtsx_chip
*chip
, int len
, u8
*rsp
, u8 rsp_type
)
3894 int retval
, rsp_len
;
3897 if (rsp_type
== SD_RSP_TYPE_R0
)
3898 return STATUS_SUCCESS
;
3900 rtsx_init_cmd(chip
);
3902 if (rsp_type
== SD_RSP_TYPE_R2
) {
3903 for (reg_addr
= PPBUF_BASE2
; reg_addr
< PPBUF_BASE2
+ 16;
3905 rtsx_add_cmd(chip
, READ_REG_CMD
, reg_addr
, 0xFF, 0);
3908 } else if (rsp_type
!= SD_RSP_TYPE_R0
) {
3909 for (reg_addr
= REG_SD_CMD0
; reg_addr
<= REG_SD_CMD4
;
3911 rtsx_add_cmd(chip
, READ_REG_CMD
, reg_addr
, 0xFF, 0);
3915 rtsx_add_cmd(chip
, READ_REG_CMD
, REG_SD_CMD5
, 0xFF, 0);
3917 retval
= rtsx_send_cmd(chip
, SD_CARD
, 100);
3918 if (retval
!= STATUS_SUCCESS
) {
3923 int min_len
= (rsp_len
< len
) ? rsp_len
: len
;
3925 memcpy(rsp
, rtsx_get_cmd_data(chip
), min_len
);
3927 dev_dbg(rtsx_dev(chip
), "min_len = %d\n", min_len
);
3928 dev_dbg(rtsx_dev(chip
), "Response in cmd buf: 0x%x 0x%x 0x%x 0x%x\n",
3929 rsp
[0], rsp
[1], rsp
[2], rsp
[3]);
3932 return STATUS_SUCCESS
;
3935 int sd_pass_thru_mode(struct scsi_cmnd
*srb
, struct rtsx_chip
*chip
)
3937 struct sd_info
*sd_card
= &chip
->sd_card
;
3938 unsigned int lun
= SCSI_LUN(srb
);
3961 sd_card
->pre_cmd_err
= 0;
3963 if (!(CHK_BIT(chip
->lun_mc
, lun
))) {
3964 SET_BIT(chip
->lun_mc
, lun
);
3965 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_CHANGE
);
3966 return TRANSPORT_FAILED
;
3969 if ((srb
->cmnd
[2] != 0x53) || (srb
->cmnd
[3] != 0x44) ||
3970 (srb
->cmnd
[4] != 0x20) || (srb
->cmnd
[5] != 0x43) ||
3971 (srb
->cmnd
[6] != 0x61) || (srb
->cmnd
[7] != 0x72) ||
3972 (srb
->cmnd
[8] != 0x64)) {
3973 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
3974 return TRANSPORT_FAILED
;
3977 switch (srb
->cmnd
[1] & 0x0F) {
3979 sd_card
->sd_pass_thru_en
= 0;
3983 sd_card
->sd_pass_thru_en
= 1;
3987 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
3988 return TRANSPORT_FAILED
;
3991 buf
[5] = (CHK_SD(sd_card
) == 1) ? 0x01 : 0x02;
3992 if (chip
->card_wp
& SD_CARD
)
3995 buf
[6] = (u8
)(sd_card
->sd_addr
>> 16);
3996 buf
[7] = (u8
)(sd_card
->sd_addr
>> 24);
3998 buf
[15] = chip
->max_lun
;
4000 len
= min_t(int, 18, scsi_bufflen(srb
));
4001 rtsx_stor_set_xfer_buf(buf
, len
, srb
);
4003 return TRANSPORT_GOOD
;
4006 static inline int get_rsp_type(struct scsi_cmnd
*srb
, u8
*rsp_type
,
4009 if (!rsp_type
|| !rsp_len
)
4012 switch (srb
->cmnd
[10]) {
4014 *rsp_type
= SD_RSP_TYPE_R0
;
4019 *rsp_type
= SD_RSP_TYPE_R1
;
4024 *rsp_type
= SD_RSP_TYPE_R1b
;
4029 *rsp_type
= SD_RSP_TYPE_R2
;
4034 *rsp_type
= SD_RSP_TYPE_R3
;
4042 return STATUS_SUCCESS
;
4045 int sd_execute_no_data(struct scsi_cmnd
*srb
, struct rtsx_chip
*chip
)
4047 struct sd_info
*sd_card
= &chip
->sd_card
;
4048 unsigned int lun
= SCSI_LUN(srb
);
4049 int retval
, rsp_len
;
4050 u8 cmd_idx
, rsp_type
;
4051 bool standby
= false, acmd
= false;
4054 if (!sd_card
->sd_pass_thru_en
) {
4055 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
4056 return TRANSPORT_FAILED
;
4059 retval
= sd_switch_clock(chip
);
4060 if (retval
!= STATUS_SUCCESS
) {
4061 return TRANSPORT_FAILED
;
4064 if (sd_card
->pre_cmd_err
) {
4065 sd_card
->pre_cmd_err
= 0;
4066 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_CHANGE
);
4067 return TRANSPORT_FAILED
;
4070 cmd_idx
= srb
->cmnd
[2] & 0x3F;
4071 if (srb
->cmnd
[1] & 0x02)
4074 if (srb
->cmnd
[1] & 0x01)
4077 arg
= ((u32
)srb
->cmnd
[3] << 24) | ((u32
)srb
->cmnd
[4] << 16) |
4078 ((u32
)srb
->cmnd
[5] << 8) | srb
->cmnd
[6];
4080 retval
= get_rsp_type(srb
, &rsp_type
, &rsp_len
);
4081 if (retval
!= STATUS_SUCCESS
) {
4082 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
4083 return TRANSPORT_FAILED
;
4085 sd_card
->last_rsp_type
= rsp_type
;
4087 retval
= sd_switch_clock(chip
);
4088 if (retval
!= STATUS_SUCCESS
) {
4089 return TRANSPORT_FAILED
;
4092 #ifdef SUPPORT_SD_LOCK
4093 if ((sd_card
->sd_lock_status
& SD_LOCK_1BIT_MODE
) == 0) {
4094 if (CHK_MMC_8BIT(sd_card
)) {
4095 retval
= rtsx_write_register(chip
, REG_SD_CFG1
, 0x03,
4097 if (retval
!= STATUS_SUCCESS
) {
4098 return TRANSPORT_FAILED
;
4101 } else if (CHK_SD(sd_card
) || CHK_MMC_4BIT(sd_card
)) {
4102 retval
= rtsx_write_register(chip
, REG_SD_CFG1
, 0x03,
4104 if (retval
!= STATUS_SUCCESS
) {
4105 return TRANSPORT_FAILED
;
4110 retval
= rtsx_write_register(chip
, REG_SD_CFG1
, 0x03, SD_BUS_WIDTH_4
);
4111 if (retval
!= STATUS_SUCCESS
) {
4112 return TRANSPORT_FAILED
;
4117 retval
= sd_select_card(chip
, 0);
4118 if (retval
!= STATUS_SUCCESS
) {
4119 goto sd_execute_cmd_failed
;
4124 retval
= ext_sd_send_cmd_get_rsp(chip
, APP_CMD
,
4126 SD_RSP_TYPE_R1
, NULL
, 0,
4128 if (retval
!= STATUS_SUCCESS
) {
4129 goto sd_execute_cmd_failed
;
4133 retval
= ext_sd_send_cmd_get_rsp(chip
, cmd_idx
, arg
, rsp_type
,
4134 sd_card
->rsp
, rsp_len
, false);
4135 if (retval
!= STATUS_SUCCESS
) {
4136 goto sd_execute_cmd_failed
;
4140 retval
= sd_select_card(chip
, 1);
4141 if (retval
!= STATUS_SUCCESS
) {
4142 goto sd_execute_cmd_failed
;
4146 #ifdef SUPPORT_SD_LOCK
4147 retval
= sd_update_lock_status(chip
);
4148 if (retval
!= STATUS_SUCCESS
) {
4149 goto sd_execute_cmd_failed
;
4153 scsi_set_resid(srb
, 0);
4154 return TRANSPORT_GOOD
;
4156 sd_execute_cmd_failed
:
4157 sd_card
->pre_cmd_err
= 1;
4158 set_sense_type(chip
, lun
, SENSE_TYPE_NO_SENSE
);
4159 release_sd_card(chip
);
4160 do_reset_sd_card(chip
);
4161 if (!(chip
->card_ready
& SD_CARD
))
4162 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_NOT_PRESENT
);
4164 return TRANSPORT_FAILED
;
4167 int sd_execute_read_data(struct scsi_cmnd
*srb
, struct rtsx_chip
*chip
)
4169 struct sd_info
*sd_card
= &chip
->sd_card
;
4170 unsigned int lun
= SCSI_LUN(srb
);
4171 int retval
, rsp_len
, i
;
4172 bool read_err
= false, cmd13_checkbit
= false;
4173 u8 cmd_idx
, rsp_type
, bus_width
;
4174 bool standby
= false, send_cmd12
= false, acmd
= false;
4177 if (!sd_card
->sd_pass_thru_en
) {
4178 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
4179 return TRANSPORT_FAILED
;
4182 if (sd_card
->pre_cmd_err
) {
4183 sd_card
->pre_cmd_err
= 0;
4184 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_CHANGE
);
4185 return TRANSPORT_FAILED
;
4188 retval
= sd_switch_clock(chip
);
4189 if (retval
!= STATUS_SUCCESS
) {
4190 return TRANSPORT_FAILED
;
4193 cmd_idx
= srb
->cmnd
[2] & 0x3F;
4194 if (srb
->cmnd
[1] & 0x04)
4197 if (srb
->cmnd
[1] & 0x02)
4200 if (srb
->cmnd
[1] & 0x01)
4203 data_len
= ((u32
)srb
->cmnd
[7] << 16) | ((u32
)srb
->cmnd
[8]
4204 << 8) | srb
->cmnd
[9];
4206 retval
= get_rsp_type(srb
, &rsp_type
, &rsp_len
);
4207 if (retval
!= STATUS_SUCCESS
) {
4208 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
4209 return TRANSPORT_FAILED
;
4211 sd_card
->last_rsp_type
= rsp_type
;
4213 retval
= sd_switch_clock(chip
);
4214 if (retval
!= STATUS_SUCCESS
) {
4215 return TRANSPORT_FAILED
;
4218 #ifdef SUPPORT_SD_LOCK
4219 if ((sd_card
->sd_lock_status
& SD_LOCK_1BIT_MODE
) == 0) {
4220 if (CHK_MMC_8BIT(sd_card
))
4221 bus_width
= SD_BUS_WIDTH_8
;
4222 else if (CHK_SD(sd_card
) || CHK_MMC_4BIT(sd_card
))
4223 bus_width
= SD_BUS_WIDTH_4
;
4225 bus_width
= SD_BUS_WIDTH_1
;
4227 bus_width
= SD_BUS_WIDTH_4
;
4229 dev_dbg(rtsx_dev(chip
), "bus_width = %d\n", bus_width
);
4231 bus_width
= SD_BUS_WIDTH_4
;
4234 if (data_len
< 512) {
4235 retval
= ext_sd_send_cmd_get_rsp(chip
, SET_BLOCKLEN
, data_len
,
4236 SD_RSP_TYPE_R1
, NULL
, 0,
4238 if (retval
!= STATUS_SUCCESS
) {
4239 goto sd_execute_read_cmd_failed
;
4244 retval
= sd_select_card(chip
, 0);
4245 if (retval
!= STATUS_SUCCESS
) {
4246 goto sd_execute_read_cmd_failed
;
4251 retval
= ext_sd_send_cmd_get_rsp(chip
, APP_CMD
,
4253 SD_RSP_TYPE_R1
, NULL
, 0,
4255 if (retval
!= STATUS_SUCCESS
) {
4256 goto sd_execute_read_cmd_failed
;
4260 if (data_len
<= 512) {
4263 u16 byte_cnt
, blk_cnt
;
4266 byte_cnt
= ((u16
)(srb
->cmnd
[8] & 0x03) << 8) | srb
->cmnd
[9];
4269 cmd
[0] = 0x40 | cmd_idx
;
4270 cmd
[1] = srb
->cmnd
[3];
4271 cmd
[2] = srb
->cmnd
[4];
4272 cmd
[3] = srb
->cmnd
[5];
4273 cmd
[4] = srb
->cmnd
[6];
4275 buf
= kmalloc(data_len
, GFP_KERNEL
);
4277 return TRANSPORT_ERROR
;
4280 retval
= sd_read_data(chip
, SD_TM_NORMAL_READ
, cmd
, 5, byte_cnt
,
4281 blk_cnt
, bus_width
, buf
, data_len
, 2000);
4282 if (retval
!= STATUS_SUCCESS
) {
4285 rtsx_clear_sd_error(chip
);
4286 goto sd_execute_read_cmd_failed
;
4289 min_len
= min(data_len
, scsi_bufflen(srb
));
4290 rtsx_stor_set_xfer_buf(buf
, min_len
, srb
);
4293 } else if (!(data_len
& 0x1FF)) {
4294 rtsx_init_cmd(chip
);
4296 trans_dma_enable(DMA_FROM_DEVICE
, chip
, data_len
, DMA_512
);
4298 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BYTE_CNT_H
, 0xFF,
4300 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BYTE_CNT_L
, 0xFF,
4302 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BLOCK_CNT_H
,
4303 0xFF, (srb
->cmnd
[7] & 0xFE) >> 1);
4304 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BLOCK_CNT_L
,
4305 0xFF, (u8
)((data_len
& 0x0001FE00) >> 9));
4307 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD0
, 0xFF,
4309 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD1
, 0xFF,
4311 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD2
, 0xFF,
4313 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD3
, 0xFF,
4315 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD4
, 0xFF,
4318 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CFG1
, 0x03, bus_width
);
4319 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CFG2
, 0xFF, rsp_type
);
4321 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_TRANSFER
,
4322 0xFF, SD_TM_AUTO_READ_2
| SD_TRANSFER_START
);
4323 rtsx_add_cmd(chip
, CHECK_REG_CMD
, REG_SD_TRANSFER
,
4324 SD_TRANSFER_END
, SD_TRANSFER_END
);
4326 rtsx_send_cmd_no_wait(chip
);
4328 retval
= rtsx_transfer_data(chip
, SD_CARD
, scsi_sglist(srb
),
4331 DMA_FROM_DEVICE
, 10000);
4334 rtsx_clear_sd_error(chip
);
4335 goto sd_execute_read_cmd_failed
;
4339 goto sd_execute_read_cmd_failed
;
4342 retval
= ext_sd_get_rsp(chip
, rsp_len
, sd_card
->rsp
, rsp_type
);
4343 if (retval
!= STATUS_SUCCESS
) {
4344 goto sd_execute_read_cmd_failed
;
4348 retval
= sd_select_card(chip
, 1);
4349 if (retval
!= STATUS_SUCCESS
) {
4350 goto sd_execute_read_cmd_failed
;
4355 retval
= ext_sd_send_cmd_get_rsp(chip
, STOP_TRANSMISSION
, 0,
4356 SD_RSP_TYPE_R1b
, NULL
, 0,
4358 if (retval
!= STATUS_SUCCESS
) {
4359 goto sd_execute_read_cmd_failed
;
4363 if (data_len
< 512) {
4364 retval
= ext_sd_send_cmd_get_rsp(chip
, SET_BLOCKLEN
, 0x200,
4365 SD_RSP_TYPE_R1
, NULL
, 0,
4367 if (retval
!= STATUS_SUCCESS
) {
4368 goto sd_execute_read_cmd_failed
;
4371 retval
= rtsx_write_register(chip
, SD_BYTE_CNT_H
, 0xFF, 0x02);
4372 if (retval
!= STATUS_SUCCESS
) {
4373 goto sd_execute_read_cmd_failed
;
4376 retval
= rtsx_write_register(chip
, SD_BYTE_CNT_L
, 0xFF, 0x00);
4377 if (retval
!= STATUS_SUCCESS
) {
4378 goto sd_execute_read_cmd_failed
;
4382 if ((srb
->cmnd
[1] & 0x02) || (srb
->cmnd
[1] & 0x04))
4383 cmd13_checkbit
= true;
4385 for (i
= 0; i
< 3; i
++) {
4386 retval
= ext_sd_send_cmd_get_rsp(chip
, SEND_STATUS
,
4388 SD_RSP_TYPE_R1
, NULL
, 0,
4390 if (retval
== STATUS_SUCCESS
)
4393 if (retval
!= STATUS_SUCCESS
) {
4394 goto sd_execute_read_cmd_failed
;
4397 scsi_set_resid(srb
, 0);
4398 return TRANSPORT_GOOD
;
4400 sd_execute_read_cmd_failed
:
4401 sd_card
->pre_cmd_err
= 1;
4402 set_sense_type(chip
, lun
, SENSE_TYPE_NO_SENSE
);
4404 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR
);
4406 release_sd_card(chip
);
4407 do_reset_sd_card(chip
);
4408 if (!(chip
->card_ready
& SD_CARD
))
4409 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_NOT_PRESENT
);
4411 return TRANSPORT_FAILED
;
4414 int sd_execute_write_data(struct scsi_cmnd
*srb
, struct rtsx_chip
*chip
)
4416 struct sd_info
*sd_card
= &chip
->sd_card
;
4417 unsigned int lun
= SCSI_LUN(srb
);
4418 int retval
, rsp_len
, i
;
4419 bool write_err
= false, cmd13_checkbit
= false;
4420 u8 cmd_idx
, rsp_type
;
4421 bool standby
= false, send_cmd12
= false, acmd
= false;
4423 #ifdef SUPPORT_SD_LOCK
4424 int lock_cmd_fail
= 0;
4425 u8 sd_lock_state
= 0;
4426 u8 lock_cmd_type
= 0;
4429 if (!sd_card
->sd_pass_thru_en
) {
4430 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
4431 return TRANSPORT_FAILED
;
4434 if (sd_card
->pre_cmd_err
) {
4435 sd_card
->pre_cmd_err
= 0;
4436 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_CHANGE
);
4437 return TRANSPORT_FAILED
;
4440 retval
= sd_switch_clock(chip
);
4441 if (retval
!= STATUS_SUCCESS
) {
4442 return TRANSPORT_FAILED
;
4445 cmd_idx
= srb
->cmnd
[2] & 0x3F;
4446 if (srb
->cmnd
[1] & 0x04)
4449 if (srb
->cmnd
[1] & 0x02)
4452 if (srb
->cmnd
[1] & 0x01)
4455 data_len
= ((u32
)srb
->cmnd
[7] << 16) | ((u32
)srb
->cmnd
[8]
4456 << 8) | srb
->cmnd
[9];
4457 arg
= ((u32
)srb
->cmnd
[3] << 24) | ((u32
)srb
->cmnd
[4] << 16) |
4458 ((u32
)srb
->cmnd
[5] << 8) | srb
->cmnd
[6];
4460 #ifdef SUPPORT_SD_LOCK
4461 if (cmd_idx
== LOCK_UNLOCK
) {
4462 sd_lock_state
= sd_card
->sd_lock_status
;
4463 sd_lock_state
&= SD_LOCKED
;
4467 retval
= get_rsp_type(srb
, &rsp_type
, &rsp_len
);
4468 if (retval
!= STATUS_SUCCESS
) {
4469 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
4470 return TRANSPORT_FAILED
;
4472 sd_card
->last_rsp_type
= rsp_type
;
4474 retval
= sd_switch_clock(chip
);
4475 if (retval
!= STATUS_SUCCESS
) {
4476 return TRANSPORT_FAILED
;
4479 #ifdef SUPPORT_SD_LOCK
4480 if ((sd_card
->sd_lock_status
& SD_LOCK_1BIT_MODE
) == 0) {
4481 if (CHK_MMC_8BIT(sd_card
)) {
4482 retval
= rtsx_write_register(chip
, REG_SD_CFG1
, 0x03,
4484 if (retval
!= STATUS_SUCCESS
) {
4485 return TRANSPORT_FAILED
;
4488 } else if (CHK_SD(sd_card
) || CHK_MMC_4BIT(sd_card
)) {
4489 retval
= rtsx_write_register(chip
, REG_SD_CFG1
, 0x03,
4491 if (retval
!= STATUS_SUCCESS
) {
4492 return TRANSPORT_FAILED
;
4497 retval
= rtsx_write_register(chip
, REG_SD_CFG1
, 0x03, SD_BUS_WIDTH_4
);
4498 if (retval
!= STATUS_SUCCESS
) {
4499 return TRANSPORT_FAILED
;
4503 if (data_len
< 512) {
4504 retval
= ext_sd_send_cmd_get_rsp(chip
, SET_BLOCKLEN
, data_len
,
4505 SD_RSP_TYPE_R1
, NULL
, 0,
4507 if (retval
!= STATUS_SUCCESS
) {
4508 goto sd_execute_write_cmd_failed
;
4513 retval
= sd_select_card(chip
, 0);
4514 if (retval
!= STATUS_SUCCESS
) {
4515 goto sd_execute_write_cmd_failed
;
4520 retval
= ext_sd_send_cmd_get_rsp(chip
, APP_CMD
,
4522 SD_RSP_TYPE_R1
, NULL
, 0,
4524 if (retval
!= STATUS_SUCCESS
) {
4525 goto sd_execute_write_cmd_failed
;
4529 retval
= ext_sd_send_cmd_get_rsp(chip
, cmd_idx
, arg
, rsp_type
,
4530 sd_card
->rsp
, rsp_len
, false);
4531 if (retval
!= STATUS_SUCCESS
) {
4532 goto sd_execute_write_cmd_failed
;
4535 if (data_len
<= 512) {
4539 buf
= kmalloc(data_len
, GFP_KERNEL
);
4541 return TRANSPORT_ERROR
;
4544 rtsx_stor_get_xfer_buf(buf
, data_len
, srb
);
4546 #ifdef SUPPORT_SD_LOCK
4547 if (cmd_idx
== LOCK_UNLOCK
)
4548 lock_cmd_type
= buf
[0] & 0x0F;
4551 if (data_len
> 256) {
4552 rtsx_init_cmd(chip
);
4553 for (i
= 0; i
< 256; i
++) {
4554 rtsx_add_cmd(chip
, WRITE_REG_CMD
,
4555 PPBUF_BASE2
+ i
, 0xFF, buf
[i
]);
4557 retval
= rtsx_send_cmd(chip
, 0, 250);
4558 if (retval
!= STATUS_SUCCESS
) {
4560 goto sd_execute_write_cmd_failed
;
4563 rtsx_init_cmd(chip
);
4564 for (i
= 256; i
< data_len
; i
++) {
4565 rtsx_add_cmd(chip
, WRITE_REG_CMD
,
4566 PPBUF_BASE2
+ i
, 0xFF, buf
[i
]);
4568 retval
= rtsx_send_cmd(chip
, 0, 250);
4569 if (retval
!= STATUS_SUCCESS
) {
4571 goto sd_execute_write_cmd_failed
;
4574 rtsx_init_cmd(chip
);
4575 for (i
= 0; i
< data_len
; i
++) {
4576 rtsx_add_cmd(chip
, WRITE_REG_CMD
,
4577 PPBUF_BASE2
+ i
, 0xFF, buf
[i
]);
4579 retval
= rtsx_send_cmd(chip
, 0, 250);
4580 if (retval
!= STATUS_SUCCESS
) {
4582 goto sd_execute_write_cmd_failed
;
4588 rtsx_init_cmd(chip
);
4590 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BYTE_CNT_H
, 0xFF,
4591 srb
->cmnd
[8] & 0x03);
4592 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BYTE_CNT_L
, 0xFF,
4594 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BLOCK_CNT_H
, 0xFF,
4596 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BLOCK_CNT_L
, 0xFF,
4598 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
, 0x01,
4601 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_TRANSFER
, 0xFF,
4602 SD_TM_AUTO_WRITE_3
| SD_TRANSFER_START
);
4603 rtsx_add_cmd(chip
, CHECK_REG_CMD
, REG_SD_TRANSFER
,
4604 SD_TRANSFER_END
, SD_TRANSFER_END
);
4606 retval
= rtsx_send_cmd(chip
, SD_CARD
, 250);
4607 } else if (!(data_len
& 0x1FF)) {
4608 rtsx_init_cmd(chip
);
4610 trans_dma_enable(DMA_TO_DEVICE
, chip
, data_len
, DMA_512
);
4612 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BYTE_CNT_H
, 0xFF,
4614 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BYTE_CNT_L
, 0xFF,
4616 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BLOCK_CNT_H
,
4617 0xFF, (srb
->cmnd
[7] & 0xFE) >> 1);
4618 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BLOCK_CNT_L
,
4619 0xFF, (u8
)((data_len
& 0x0001FE00) >> 9));
4621 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_TRANSFER
, 0xFF,
4622 SD_TM_AUTO_WRITE_3
| SD_TRANSFER_START
);
4623 rtsx_add_cmd(chip
, CHECK_REG_CMD
, REG_SD_TRANSFER
,
4624 SD_TRANSFER_END
, SD_TRANSFER_END
);
4626 rtsx_send_cmd_no_wait(chip
);
4628 retval
= rtsx_transfer_data(chip
, SD_CARD
, scsi_sglist(srb
),
4631 DMA_TO_DEVICE
, 10000);
4634 goto sd_execute_write_cmd_failed
;
4639 rtsx_clear_sd_error(chip
);
4640 goto sd_execute_write_cmd_failed
;
4643 #ifdef SUPPORT_SD_LOCK
4644 if (cmd_idx
== LOCK_UNLOCK
) {
4645 if (lock_cmd_type
== SD_ERASE
) {
4646 sd_card
->sd_erase_status
= SD_UNDER_ERASING
;
4647 scsi_set_resid(srb
, 0);
4648 return TRANSPORT_GOOD
;
4651 rtsx_init_cmd(chip
);
4652 rtsx_add_cmd(chip
, CHECK_REG_CMD
, 0xFD30, 0x02, 0x02);
4654 rtsx_send_cmd(chip
, SD_CARD
, 250);
4656 retval
= sd_update_lock_status(chip
);
4657 if (retval
!= STATUS_SUCCESS
) {
4658 dev_dbg(rtsx_dev(chip
), "Lock command fail!\n");
4662 #endif /* SUPPORT_SD_LOCK */
4665 retval
= sd_select_card(chip
, 1);
4666 if (retval
!= STATUS_SUCCESS
) {
4667 goto sd_execute_write_cmd_failed
;
4672 retval
= ext_sd_send_cmd_get_rsp(chip
, STOP_TRANSMISSION
, 0,
4673 SD_RSP_TYPE_R1b
, NULL
, 0,
4675 if (retval
!= STATUS_SUCCESS
) {
4676 goto sd_execute_write_cmd_failed
;
4680 if (data_len
< 512) {
4681 retval
= ext_sd_send_cmd_get_rsp(chip
, SET_BLOCKLEN
, 0x200,
4682 SD_RSP_TYPE_R1
, NULL
, 0,
4684 if (retval
!= STATUS_SUCCESS
) {
4685 goto sd_execute_write_cmd_failed
;
4688 retval
= rtsx_write_register(chip
, SD_BYTE_CNT_H
, 0xFF, 0x02);
4689 if (retval
!= STATUS_SUCCESS
) {
4690 goto sd_execute_write_cmd_failed
;
4693 retval
= rtsx_write_register(chip
, SD_BYTE_CNT_L
, 0xFF, 0x00);
4694 if (retval
!= STATUS_SUCCESS
) {
4695 goto sd_execute_write_cmd_failed
;
4699 if ((srb
->cmnd
[1] & 0x02) || (srb
->cmnd
[1] & 0x04))
4700 cmd13_checkbit
= true;
4702 for (i
= 0; i
< 3; i
++) {
4703 retval
= ext_sd_send_cmd_get_rsp(chip
, SEND_STATUS
,
4705 SD_RSP_TYPE_R1
, NULL
, 0,
4707 if (retval
== STATUS_SUCCESS
)
4710 if (retval
!= STATUS_SUCCESS
) {
4711 goto sd_execute_write_cmd_failed
;
4714 #ifdef SUPPORT_SD_LOCK
4715 if (cmd_idx
== LOCK_UNLOCK
) {
4716 if (!lock_cmd_fail
) {
4717 dev_dbg(rtsx_dev(chip
), "lock_cmd_type = 0x%x\n",
4719 if (lock_cmd_type
& SD_CLR_PWD
)
4720 sd_card
->sd_lock_status
&= ~SD_PWD_EXIST
;
4722 if (lock_cmd_type
& SD_SET_PWD
)
4723 sd_card
->sd_lock_status
|= SD_PWD_EXIST
;
4726 dev_dbg(rtsx_dev(chip
), "sd_lock_state = 0x%x, sd_card->sd_lock_status = 0x%x\n",
4727 sd_lock_state
, sd_card
->sd_lock_status
);
4728 if (sd_lock_state
^ (sd_card
->sd_lock_status
& SD_LOCKED
)) {
4729 sd_card
->sd_lock_notify
= 1;
4730 if (sd_lock_state
) {
4731 if (sd_card
->sd_lock_status
& SD_LOCK_1BIT_MODE
) {
4732 sd_card
->sd_lock_status
|= (
4733 SD_UNLOCK_POW_ON
| SD_SDR_RST
);
4734 if (CHK_SD(sd_card
)) {
4735 retval
= reset_sd(chip
);
4736 if (retval
!= STATUS_SUCCESS
) {
4737 sd_card
->sd_lock_status
&= ~(SD_UNLOCK_POW_ON
| SD_SDR_RST
);
4738 goto sd_execute_write_cmd_failed
;
4742 sd_card
->sd_lock_status
&= ~(SD_UNLOCK_POW_ON
| SD_SDR_RST
);
4748 if (lock_cmd_fail
) {
4749 scsi_set_resid(srb
, 0);
4750 set_sense_type(chip
, lun
, SENSE_TYPE_NO_SENSE
);
4751 return TRANSPORT_FAILED
;
4753 #endif /* SUPPORT_SD_LOCK */
4755 scsi_set_resid(srb
, 0);
4756 return TRANSPORT_GOOD
;
4758 sd_execute_write_cmd_failed
:
4759 sd_card
->pre_cmd_err
= 1;
4760 set_sense_type(chip
, lun
, SENSE_TYPE_NO_SENSE
);
4762 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_WRITE_ERR
);
4764 release_sd_card(chip
);
4765 do_reset_sd_card(chip
);
4766 if (!(chip
->card_ready
& SD_CARD
))
4767 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_NOT_PRESENT
);
4769 return TRANSPORT_FAILED
;
4772 int sd_get_cmd_rsp(struct scsi_cmnd
*srb
, struct rtsx_chip
*chip
)
4774 struct sd_info
*sd_card
= &chip
->sd_card
;
4775 unsigned int lun
= SCSI_LUN(srb
);
4779 if (!sd_card
->sd_pass_thru_en
) {
4780 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
4781 return TRANSPORT_FAILED
;
4784 if (sd_card
->pre_cmd_err
) {
4785 sd_card
->pre_cmd_err
= 0;
4786 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_CHANGE
);
4787 return TRANSPORT_FAILED
;
4790 data_len
= ((u16
)srb
->cmnd
[7] << 8) | srb
->cmnd
[8];
4792 if (sd_card
->last_rsp_type
== SD_RSP_TYPE_R0
) {
4793 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
4794 return TRANSPORT_FAILED
;
4795 } else if (sd_card
->last_rsp_type
== SD_RSP_TYPE_R2
) {
4796 count
= (data_len
< 17) ? data_len
: 17;
4798 count
= (data_len
< 6) ? data_len
: 6;
4800 rtsx_stor_set_xfer_buf(sd_card
->rsp
, count
, srb
);
4802 dev_dbg(rtsx_dev(chip
), "Response length: %d\n", data_len
);
4803 dev_dbg(rtsx_dev(chip
), "Response: 0x%x 0x%x 0x%x 0x%x\n",
4804 sd_card
->rsp
[0], sd_card
->rsp
[1],
4805 sd_card
->rsp
[2], sd_card
->rsp
[3]);
4807 scsi_set_resid(srb
, 0);
4808 return TRANSPORT_GOOD
;
4811 int sd_hw_rst(struct scsi_cmnd
*srb
, struct rtsx_chip
*chip
)
4813 struct sd_info
*sd_card
= &chip
->sd_card
;
4814 unsigned int lun
= SCSI_LUN(srb
);
4817 if (!sd_card
->sd_pass_thru_en
) {
4818 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
4819 return TRANSPORT_FAILED
;
4822 if (sd_card
->pre_cmd_err
) {
4823 sd_card
->pre_cmd_err
= 0;
4824 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_CHANGE
);
4825 return TRANSPORT_FAILED
;
4828 if ((srb
->cmnd
[2] != 0x53) || (srb
->cmnd
[3] != 0x44) ||
4829 (srb
->cmnd
[4] != 0x20) || (srb
->cmnd
[5] != 0x43) ||
4830 (srb
->cmnd
[6] != 0x61) || (srb
->cmnd
[7] != 0x72) ||
4831 (srb
->cmnd
[8] != 0x64)) {
4832 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
4833 return TRANSPORT_FAILED
;
4836 switch (srb
->cmnd
[1] & 0x0F) {
4838 #ifdef SUPPORT_SD_LOCK
4839 if (srb
->cmnd
[9] == 0x64)
4840 sd_card
->sd_lock_status
|= SD_SDR_RST
;
4842 retval
= reset_sd_card(chip
);
4843 if (retval
!= STATUS_SUCCESS
) {
4844 #ifdef SUPPORT_SD_LOCK
4845 sd_card
->sd_lock_status
&= ~SD_SDR_RST
;
4847 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_NOT_PRESENT
);
4848 sd_card
->pre_cmd_err
= 1;
4849 return TRANSPORT_FAILED
;
4851 #ifdef SUPPORT_SD_LOCK
4852 sd_card
->sd_lock_status
&= ~SD_SDR_RST
;
4857 retval
= soft_reset_sd_card(chip
);
4858 if (retval
!= STATUS_SUCCESS
) {
4859 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_NOT_PRESENT
);
4860 sd_card
->pre_cmd_err
= 1;
4861 return TRANSPORT_FAILED
;
4866 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
4867 return TRANSPORT_FAILED
;
4870 scsi_set_resid(srb
, 0);
4871 return TRANSPORT_GOOD
;
4875 void sd_cleanup_work(struct rtsx_chip
*chip
)
4877 struct sd_info
*sd_card
= &chip
->sd_card
;
4879 if (sd_card
->seq_mode
) {
4880 dev_dbg(rtsx_dev(chip
), "SD: stop transmission\n");
4881 sd_stop_seq_mode(chip
);
4882 sd_card
->cleanup_counter
= 0;
4886 int sd_power_off_card3v3(struct rtsx_chip
*chip
)
4890 retval
= disable_card_clock(chip
, SD_CARD
);
4891 if (retval
!= STATUS_SUCCESS
) {
4895 retval
= rtsx_write_register(chip
, CARD_OE
, SD_OUTPUT_EN
, 0);
4900 if (!chip
->ft2_fast_mode
) {
4901 retval
= card_power_off(chip
, SD_CARD
);
4902 if (retval
!= STATUS_SUCCESS
) {
4909 if (chip
->asic_code
) {
4910 retval
= sd_pull_ctl_disable(chip
);
4911 if (retval
!= STATUS_SUCCESS
) {
4915 retval
= rtsx_write_register(chip
, FPGA_PULL_CTL
,
4916 FPGA_SD_PULL_CTL_BIT
| 0x20,
4917 FPGA_SD_PULL_CTL_BIT
);
4923 return STATUS_SUCCESS
;
4926 int release_sd_card(struct rtsx_chip
*chip
)
4928 struct sd_info
*sd_card
= &chip
->sd_card
;
4931 chip
->card_ready
&= ~SD_CARD
;
4932 chip
->card_fail
&= ~SD_CARD
;
4933 chip
->card_wp
&= ~SD_CARD
;
4938 #ifdef SUPPORT_SD_LOCK
4939 sd_card
->sd_lock_status
= 0;
4940 sd_card
->sd_erase_status
= 0;
4943 memset(sd_card
->raw_csd
, 0, 16);
4944 memset(sd_card
->raw_scr
, 0, 8);
4946 retval
= sd_power_off_card3v3(chip
);
4947 if (retval
!= STATUS_SUCCESS
) {
4951 return STATUS_SUCCESS
;