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>
28 #include "rtsx_transport.h"
29 #include "rtsx_scsi.h"
30 #include "rtsx_card.h"
33 #define SD_MAX_RETRY_COUNT 3
35 static u16 REG_SD_CFG1
;
36 static u16 REG_SD_CFG2
;
37 static u16 REG_SD_CFG3
;
38 static u16 REG_SD_STAT1
;
39 static u16 REG_SD_STAT2
;
40 static u16 REG_SD_BUS_STAT
;
41 static u16 REG_SD_PAD_CTL
;
42 static u16 REG_SD_SAMPLE_POINT_CTL
;
43 static u16 REG_SD_PUSH_POINT_CTL
;
44 static u16 REG_SD_CMD0
;
45 static u16 REG_SD_CMD1
;
46 static u16 REG_SD_CMD2
;
47 static u16 REG_SD_CMD3
;
48 static u16 REG_SD_CMD4
;
49 static u16 REG_SD_CMD5
;
50 static u16 REG_SD_BYTE_CNT_L
;
51 static u16 REG_SD_BYTE_CNT_H
;
52 static u16 REG_SD_BLOCK_CNT_L
;
53 static u16 REG_SD_BLOCK_CNT_H
;
54 static u16 REG_SD_TRANSFER
;
55 static u16 REG_SD_VPCLK0_CTL
;
56 static u16 REG_SD_VPCLK1_CTL
;
57 static u16 REG_SD_DCMPS0_CTL
;
58 static u16 REG_SD_DCMPS1_CTL
;
60 static inline void sd_set_err_code(struct rtsx_chip
*chip
, u8 err_code
)
62 struct sd_info
*sd_card
= &(chip
->sd_card
);
64 sd_card
->err_code
|= err_code
;
67 static inline void sd_clr_err_code(struct rtsx_chip
*chip
)
69 struct sd_info
*sd_card
= &(chip
->sd_card
);
71 sd_card
->err_code
= 0;
74 static inline int sd_check_err_code(struct rtsx_chip
*chip
, u8 err_code
)
76 struct sd_info
*sd_card
= &(chip
->sd_card
);
78 return sd_card
->err_code
& err_code
;
81 static void sd_init_reg_addr(struct rtsx_chip
*chip
)
86 REG_SD_STAT1
= 0xFD30;
90 REG_SD_SAMPLE_POINT_CTL
= 0;
91 REG_SD_PUSH_POINT_CTL
= 0;
98 REG_SD_BYTE_CNT_L
= 0xFD39;
99 REG_SD_BYTE_CNT_H
= 0xFD3A;
100 REG_SD_BLOCK_CNT_L
= 0xFD3B;
101 REG_SD_BLOCK_CNT_H
= 0xFD3C;
102 REG_SD_TRANSFER
= 0xFD32;
103 REG_SD_VPCLK0_CTL
= 0;
104 REG_SD_VPCLK1_CTL
= 0;
105 REG_SD_DCMPS0_CTL
= 0;
106 REG_SD_DCMPS1_CTL
= 0;
109 static int sd_check_data0_status(struct rtsx_chip
*chip
)
113 RTSX_READ_REG(chip
, REG_SD_STAT1
, &stat
);
115 if (!(stat
& SD_DAT0_STATUS
)) {
116 sd_set_err_code(chip
, SD_BUSY
);
117 TRACE_RET(chip
, STATUS_FAIL
);
120 return STATUS_SUCCESS
;
123 static int sd_send_cmd_get_rsp(struct rtsx_chip
*chip
, u8 cmd_idx
,
124 u32 arg
, u8 rsp_type
, u8
*rsp
, int rsp_len
)
126 struct sd_info
*sd_card
= &(chip
->sd_card
);
134 sd_clr_err_code(chip
);
136 RTSX_DEBUGP("SD/MMC CMD %d, arg = 0x%08x\n", cmd_idx
, arg
);
138 if (rsp_type
== SD_RSP_TYPE_R1b
)
145 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD0
, 0xFF, 0x40 | cmd_idx
);
146 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD1
, 0xFF, (u8
)(arg
>> 24));
147 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD2
, 0xFF, (u8
)(arg
>> 16));
148 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD3
, 0xFF, (u8
)(arg
>> 8));
149 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD4
, 0xFF, (u8
)arg
);
151 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CFG2
, 0xFF, rsp_type
);
152 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
,
153 0x01, PINGPONG_BUFFER
);
154 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_TRANSFER
,
155 0xFF, SD_TM_CMD_RSP
| SD_TRANSFER_START
);
156 rtsx_add_cmd(chip
, CHECK_REG_CMD
, REG_SD_TRANSFER
,
157 SD_TRANSFER_END
| SD_STAT_IDLE
, SD_TRANSFER_END
| SD_STAT_IDLE
);
159 if (rsp_type
== SD_RSP_TYPE_R2
) {
160 for (reg_addr
= PPBUF_BASE2
; reg_addr
< PPBUF_BASE2
+ 16;
162 rtsx_add_cmd(chip
, READ_REG_CMD
, reg_addr
, 0, 0);
165 } else if (rsp_type
!= SD_RSP_TYPE_R0
) {
166 for (reg_addr
= REG_SD_CMD0
; reg_addr
<= REG_SD_CMD4
;
168 rtsx_add_cmd(chip
, READ_REG_CMD
, reg_addr
, 0, 0);
173 rtsx_add_cmd(chip
, READ_REG_CMD
, REG_SD_STAT1
, 0, 0);
175 retval
= rtsx_send_cmd(chip
, SD_CARD
, timeout
);
179 rtsx_read_register(chip
, REG_SD_STAT1
, &val
);
180 RTSX_DEBUGP("SD_STAT1: 0x%x\n", val
);
182 rtsx_read_register(chip
, REG_SD_CFG3
, &val
);
183 RTSX_DEBUGP("SD_CFG3: 0x%x\n", val
);
185 if (retval
== -ETIMEDOUT
) {
186 if (rsp_type
& SD_WAIT_BUSY_END
) {
187 retval
= sd_check_data0_status(chip
);
188 if (retval
!= STATUS_SUCCESS
) {
189 rtsx_clear_sd_error(chip
);
190 TRACE_RET(chip
, retval
);
193 sd_set_err_code(chip
, SD_TO_ERR
);
195 retval
= STATUS_TIMEDOUT
;
197 retval
= STATUS_FAIL
;
199 rtsx_clear_sd_error(chip
);
201 TRACE_RET(chip
, retval
);
204 if (rsp_type
== SD_RSP_TYPE_R0
)
205 return STATUS_SUCCESS
;
207 ptr
= rtsx_get_cmd_data(chip
) + 1;
209 if ((ptr
[0] & 0xC0) != 0) {
210 sd_set_err_code(chip
, SD_STS_ERR
);
211 TRACE_RET(chip
, STATUS_FAIL
);
214 if (!(rsp_type
& SD_NO_CHECK_CRC7
)) {
215 if (ptr
[stat_idx
] & SD_CRC7_ERR
) {
216 if (cmd_idx
== WRITE_MULTIPLE_BLOCK
) {
217 sd_set_err_code(chip
, SD_CRC_ERR
);
218 TRACE_RET(chip
, STATUS_FAIL
);
220 if (rty_cnt
< SD_MAX_RETRY_COUNT
) {
225 sd_set_err_code(chip
, SD_CRC_ERR
);
226 TRACE_RET(chip
, STATUS_FAIL
);
231 if ((rsp_type
== SD_RSP_TYPE_R1
) || (rsp_type
== SD_RSP_TYPE_R1b
)) {
232 if ((cmd_idx
!= SEND_RELATIVE_ADDR
) &&
233 (cmd_idx
!= SEND_IF_COND
)) {
234 if (cmd_idx
!= STOP_TRANSMISSION
) {
236 TRACE_RET(chip
, STATUS_FAIL
);
238 #ifdef SUPPORT_SD_LOCK
244 RTSX_DEBUGP("ptr[1]: 0x%02x\n", ptr
[1]);
245 TRACE_RET(chip
, STATUS_FAIL
);
248 RTSX_DEBUGP("ptr[2]: 0x%02x\n", ptr
[2]);
249 TRACE_RET(chip
, STATUS_FAIL
);
252 RTSX_DEBUGP("ptr[3]: 0x%02x\n", ptr
[3]);
253 TRACE_RET(chip
, STATUS_FAIL
);
256 sd_card
->sd_data_buf_ready
= 1;
258 sd_card
->sd_data_buf_ready
= 0;
263 memcpy(rsp
, ptr
, rsp_len
);
265 return STATUS_SUCCESS
;
268 static int sd_read_data(struct rtsx_chip
*chip
,
269 u8 trans_mode
, u8
*cmd
, int cmd_len
, u16 byte_cnt
,
270 u16 blk_cnt
, u8 bus_width
, u8
*buf
, int buf_len
,
273 struct sd_info
*sd_card
= &(chip
->sd_card
);
277 sd_clr_err_code(chip
);
283 TRACE_RET(chip
, STATUS_FAIL
);
288 RTSX_DEBUGP("SD/MMC CMD %d\n", cmd
[0] - 0x40);
289 for (i
= 0; i
< (cmd_len
< 6 ? cmd_len
: 6); i
++)
290 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD0
+ i
,
293 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BYTE_CNT_L
, 0xFF,
295 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BYTE_CNT_H
, 0xFF,
296 (u8
)(byte_cnt
>> 8));
297 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BLOCK_CNT_L
, 0xFF,
299 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BLOCK_CNT_H
, 0xFF,
302 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CFG1
, 0x03, bus_width
);
304 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CFG2
, 0xFF,
305 SD_CALCULATE_CRC7
| SD_CHECK_CRC16
| SD_NO_WAIT_BUSY_END
|
306 SD_CHECK_CRC7
| SD_RSP_LEN_6
);
307 if (trans_mode
!= SD_TM_AUTO_TUNING
)
308 rtsx_add_cmd(chip
, WRITE_REG_CMD
,
309 CARD_DATA_SOURCE
, 0x01, PINGPONG_BUFFER
);
311 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_TRANSFER
, 0xFF,
312 trans_mode
| SD_TRANSFER_START
);
313 rtsx_add_cmd(chip
, CHECK_REG_CMD
, REG_SD_TRANSFER
, SD_TRANSFER_END
,
316 retval
= rtsx_send_cmd(chip
, SD_CARD
, timeout
);
318 if (retval
== -ETIMEDOUT
) {
319 sd_send_cmd_get_rsp(chip
, SEND_STATUS
, sd_card
->sd_addr
,
320 SD_RSP_TYPE_R1
, NULL
, 0);
323 TRACE_RET(chip
, STATUS_FAIL
);
326 if (buf
&& buf_len
) {
327 retval
= rtsx_read_ppbuf(chip
, buf
, buf_len
);
328 if (retval
!= STATUS_SUCCESS
)
329 TRACE_RET(chip
, STATUS_FAIL
);
332 return STATUS_SUCCESS
;
335 static int sd_write_data(struct rtsx_chip
*chip
, u8 trans_mode
,
336 u8
*cmd
, int cmd_len
, u16 byte_cnt
, u16 blk_cnt
, u8 bus_width
,
337 u8
*buf
, int buf_len
, int timeout
)
339 struct sd_info
*sd_card
= &(chip
->sd_card
);
343 sd_clr_err_code(chip
);
349 /* This function can't write data more than one page */
350 TRACE_RET(chip
, STATUS_FAIL
);
353 if (buf
&& buf_len
) {
354 retval
= rtsx_write_ppbuf(chip
, buf
, buf_len
);
355 if (retval
!= STATUS_SUCCESS
)
356 TRACE_RET(chip
, STATUS_FAIL
);
362 RTSX_DEBUGP("SD/MMC CMD %d\n", cmd
[0] - 0x40);
363 for (i
= 0; i
< (cmd_len
< 6 ? cmd_len
: 6); i
++) {
364 rtsx_add_cmd(chip
, WRITE_REG_CMD
,
365 REG_SD_CMD0
+ i
, 0xFF, cmd
[i
]);
368 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BYTE_CNT_L
, 0xFF,
370 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BYTE_CNT_H
, 0xFF,
371 (u8
)(byte_cnt
>> 8));
372 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BLOCK_CNT_L
, 0xFF,
374 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BLOCK_CNT_H
, 0xFF,
377 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CFG1
, 0x03, bus_width
);
379 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CFG2
, 0xFF,
380 SD_CALCULATE_CRC7
| SD_CHECK_CRC16
| SD_NO_WAIT_BUSY_END
|
381 SD_CHECK_CRC7
| SD_RSP_LEN_6
);
383 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_TRANSFER
, 0xFF,
384 trans_mode
| SD_TRANSFER_START
);
385 rtsx_add_cmd(chip
, CHECK_REG_CMD
, REG_SD_TRANSFER
, SD_TRANSFER_END
,
388 retval
= rtsx_send_cmd(chip
, SD_CARD
, timeout
);
390 if (retval
== -ETIMEDOUT
) {
391 sd_send_cmd_get_rsp(chip
, SEND_STATUS
,
392 sd_card
->sd_addr
, SD_RSP_TYPE_R1
, NULL
, 0);
395 TRACE_RET(chip
, STATUS_FAIL
);
398 return STATUS_SUCCESS
;
401 static int sd_check_csd(struct rtsx_chip
*chip
, char check_wp
)
403 struct sd_info
*sd_card
= &(chip
->sd_card
);
406 u8 csd_ver
, trans_speed
;
409 for (i
= 0; i
< 6; i
++) {
410 if (detect_card_cd(chip
, SD_CARD
) != STATUS_SUCCESS
) {
411 sd_set_err_code(chip
, SD_NO_CARD
);
412 TRACE_RET(chip
, STATUS_FAIL
);
415 retval
= sd_send_cmd_get_rsp(chip
, SEND_CSD
, sd_card
->sd_addr
,
416 SD_RSP_TYPE_R2
, rsp
, 16);
417 if (retval
== STATUS_SUCCESS
)
422 TRACE_RET(chip
, STATUS_FAIL
);
424 memcpy(sd_card
->raw_csd
, rsp
+ 1, 15);
426 RTSX_DEBUGP("CSD Response:\n");
427 RTSX_DUMP(sd_card
->raw_csd
, 16);
429 csd_ver
= (rsp
[1] & 0xc0) >> 6;
430 RTSX_DEBUGP("csd_ver = %d\n", csd_ver
);
432 trans_speed
= rsp
[4];
433 if ((trans_speed
& 0x07) == 0x02) {
434 if ((trans_speed
& 0xf8) >= 0x30) {
436 sd_card
->sd_clock
= 47;
438 sd_card
->sd_clock
= CLK_50
;
440 } else if ((trans_speed
& 0xf8) == 0x28) {
442 sd_card
->sd_clock
= 39;
444 sd_card
->sd_clock
= CLK_40
;
446 } else if ((trans_speed
& 0xf8) == 0x20) {
448 sd_card
->sd_clock
= 29;
450 sd_card
->sd_clock
= CLK_30
;
452 } else if ((trans_speed
& 0xf8) >= 0x10) {
454 sd_card
->sd_clock
= 23;
456 sd_card
->sd_clock
= CLK_20
;
458 } else if ((trans_speed
& 0x08) >= 0x08) {
460 sd_card
->sd_clock
= 19;
462 sd_card
->sd_clock
= CLK_20
;
464 TRACE_RET(chip
, STATUS_FAIL
);
467 TRACE_RET(chip
, STATUS_FAIL
);
470 if (CHK_MMC_SECTOR_MODE(sd_card
)) {
471 sd_card
->capacity
= 0;
473 if ((!CHK_SD_HCXC(sd_card
)) || (csd_ver
== 0)) {
474 u8 blk_size
, c_size_mult
;
476 blk_size
= rsp
[6] & 0x0F;
477 c_size
= ((u16
)(rsp
[7] & 0x03) << 10)
479 + ((u16
)(rsp
[9] & 0xC0) >> 6);
480 c_size_mult
= (u8
)((rsp
[10] & 0x03) << 1);
481 c_size_mult
+= (rsp
[11] & 0x80) >> 7;
482 sd_card
->capacity
= (((u32
)(c_size
+ 1)) *
483 (1 << (c_size_mult
+ 2)))
486 u32 total_sector
= 0;
487 total_sector
= (((u32
)rsp
[8] & 0x3f) << 16) |
488 ((u32
)rsp
[9] << 8) | (u32
)rsp
[10];
489 sd_card
->capacity
= (total_sector
+ 1) << 10;
495 chip
->card_wp
|= SD_CARD
;
497 RTSX_DEBUGP("CSD WP Status: 0x%x\n", rsp
[15]);
500 return STATUS_SUCCESS
;
503 static int sd_set_sample_push_timing(struct rtsx_chip
*chip
)
505 struct sd_info
*sd_card
= &(chip
->sd_card
);
509 if ((chip
->sd_ctl
& SD_PUSH_POINT_CTL_MASK
) == SD_PUSH_POINT_DELAY
)
512 if ((chip
->sd_ctl
& SD_SAMPLE_POINT_CTL_MASK
) == SD_SAMPLE_POINT_AUTO
) {
513 if (chip
->asic_code
) {
514 if (CHK_SD_HS(sd_card
) || CHK_MMC_52M(sd_card
)) {
526 } else if ((chip
->sd_ctl
& SD_SAMPLE_POINT_CTL_MASK
) ==
527 SD_SAMPLE_POINT_DELAY
) {
534 RTSX_WRITE_REG(chip
, REG_SD_CFG1
, 0x1C, val
);
536 return STATUS_SUCCESS
;
539 static void sd_choose_proper_clock(struct rtsx_chip
*chip
)
541 struct sd_info
*sd_card
= &(chip
->sd_card
);
543 if (CHK_SD_SDR104(sd_card
)) {
545 sd_card
->sd_clock
= chip
->asic_sd_sdr104_clk
;
547 sd_card
->sd_clock
= chip
->fpga_sd_sdr104_clk
;
549 } else if (CHK_SD_DDR50(sd_card
)) {
551 sd_card
->sd_clock
= chip
->asic_sd_ddr50_clk
;
553 sd_card
->sd_clock
= chip
->fpga_sd_ddr50_clk
;
555 } else if (CHK_SD_SDR50(sd_card
)) {
557 sd_card
->sd_clock
= chip
->asic_sd_sdr50_clk
;
559 sd_card
->sd_clock
= chip
->fpga_sd_sdr50_clk
;
561 } else if (CHK_SD_HS(sd_card
)) {
563 sd_card
->sd_clock
= chip
->asic_sd_hs_clk
;
565 sd_card
->sd_clock
= chip
->fpga_sd_hs_clk
;
567 } else if (CHK_MMC_52M(sd_card
) || CHK_MMC_DDR52(sd_card
)) {
569 sd_card
->sd_clock
= chip
->asic_mmc_52m_clk
;
571 sd_card
->sd_clock
= chip
->fpga_mmc_52m_clk
;
573 } else if (CHK_MMC_26M(sd_card
)) {
575 sd_card
->sd_clock
= 48;
577 sd_card
->sd_clock
= CLK_50
;
581 static int sd_set_clock_divider(struct rtsx_chip
*chip
, u8 clk_div
)
583 u8 mask
= 0, val
= 0;
586 if (clk_div
== SD_CLK_DIVIDE_0
)
588 else if (clk_div
== SD_CLK_DIVIDE_128
)
590 else if (clk_div
== SD_CLK_DIVIDE_256
)
593 RTSX_WRITE_REG(chip
, REG_SD_CFG1
, mask
, val
);
595 return STATUS_SUCCESS
;
598 static int sd_set_init_para(struct rtsx_chip
*chip
)
600 struct sd_info
*sd_card
= &(chip
->sd_card
);
603 retval
= sd_set_sample_push_timing(chip
);
604 if (retval
!= STATUS_SUCCESS
)
605 TRACE_RET(chip
, STATUS_FAIL
);
607 sd_choose_proper_clock(chip
);
609 retval
= switch_clock(chip
, sd_card
->sd_clock
);
610 if (retval
!= STATUS_SUCCESS
)
611 TRACE_RET(chip
, STATUS_FAIL
);
613 return STATUS_SUCCESS
;
616 int sd_select_card(struct rtsx_chip
*chip
, int select
)
618 struct sd_info
*sd_card
= &(chip
->sd_card
);
620 u8 cmd_idx
, cmd_type
;
624 cmd_idx
= SELECT_CARD
;
625 cmd_type
= SD_RSP_TYPE_R1
;
626 addr
= sd_card
->sd_addr
;
628 cmd_idx
= DESELECT_CARD
;
629 cmd_type
= SD_RSP_TYPE_R0
;
633 retval
= sd_send_cmd_get_rsp(chip
, cmd_idx
, addr
, cmd_type
, NULL
, 0);
634 if (retval
!= STATUS_SUCCESS
)
635 TRACE_RET(chip
, STATUS_FAIL
);
637 return STATUS_SUCCESS
;
640 #ifdef SUPPORT_SD_LOCK
641 static int sd_update_lock_status(struct rtsx_chip
*chip
)
643 struct sd_info
*sd_card
= &(chip
->sd_card
);
647 retval
= sd_send_cmd_get_rsp(chip
, SEND_STATUS
, sd_card
->sd_addr
,
648 SD_RSP_TYPE_R1
, rsp
, 5);
649 if (retval
!= STATUS_SUCCESS
)
650 TRACE_RET(chip
, STATUS_FAIL
);
653 sd_card
->sd_lock_status
|= SD_LOCKED
;
655 sd_card
->sd_lock_status
&= ~SD_LOCKED
;
657 RTSX_DEBUGP("sd_card->sd_lock_status = 0x%x\n",
658 sd_card
->sd_lock_status
);
661 TRACE_RET(chip
, STATUS_FAIL
);
663 return STATUS_SUCCESS
;
667 static int sd_wait_state_data_ready(struct rtsx_chip
*chip
, u8 state
,
668 u8 data_ready
, int polling_cnt
)
670 struct sd_info
*sd_card
= &(chip
->sd_card
);
674 for (i
= 0; i
< polling_cnt
; i
++) {
675 retval
= sd_send_cmd_get_rsp(chip
, SEND_STATUS
,
676 sd_card
->sd_addr
, SD_RSP_TYPE_R1
, rsp
,
678 if (retval
!= STATUS_SUCCESS
)
679 TRACE_RET(chip
, STATUS_FAIL
);
681 if (((rsp
[3] & 0x1E) == state
) &&
682 ((rsp
[3] & 0x01) == data_ready
))
683 return STATUS_SUCCESS
;
686 TRACE_RET(chip
, STATUS_FAIL
);
689 static int sd_change_bank_voltage(struct rtsx_chip
*chip
, u8 voltage
)
693 if (voltage
== SD_IO_3V3
) {
694 if (chip
->asic_code
) {
695 retval
= rtsx_write_phy_register(chip
, 0x08,
698 if (retval
!= STATUS_SUCCESS
)
699 TRACE_RET(chip
, STATUS_FAIL
);
701 RTSX_WRITE_REG(chip
, SD_PAD_CTL
, SD_IO_USING_1V8
, 0);
703 } else if (voltage
== SD_IO_1V8
) {
704 if (chip
->asic_code
) {
705 retval
= rtsx_write_phy_register(chip
, 0x08,
708 if (retval
!= STATUS_SUCCESS
)
709 TRACE_RET(chip
, STATUS_FAIL
);
711 RTSX_WRITE_REG(chip
, SD_PAD_CTL
, SD_IO_USING_1V8
,
715 TRACE_RET(chip
, STATUS_FAIL
);
718 return STATUS_SUCCESS
;
721 static int sd_voltage_switch(struct rtsx_chip
*chip
)
726 RTSX_WRITE_REG(chip
, SD_BUS_STAT
, SD_CLK_TOGGLE_EN
| SD_CLK_FORCE_STOP
,
729 retval
= sd_send_cmd_get_rsp(chip
, VOLTAGE_SWITCH
, 0, SD_RSP_TYPE_R1
,
731 if (retval
!= STATUS_SUCCESS
)
732 TRACE_RET(chip
, STATUS_FAIL
);
734 udelay(chip
->sd_voltage_switch_delay
);
736 RTSX_READ_REG(chip
, SD_BUS_STAT
, &stat
);
737 if (stat
& (SD_CMD_STATUS
| SD_DAT3_STATUS
| SD_DAT2_STATUS
|
738 SD_DAT1_STATUS
| SD_DAT0_STATUS
)) {
739 TRACE_RET(chip
, STATUS_FAIL
);
742 RTSX_WRITE_REG(chip
, SD_BUS_STAT
, 0xFF, SD_CLK_FORCE_STOP
);
743 retval
= sd_change_bank_voltage(chip
, SD_IO_1V8
);
744 if (retval
!= STATUS_SUCCESS
)
745 TRACE_RET(chip
, STATUS_FAIL
);
749 RTSX_WRITE_REG(chip
, SD_BUS_STAT
, 0xFF, SD_CLK_TOGGLE_EN
);
752 RTSX_READ_REG(chip
, SD_BUS_STAT
, &stat
);
753 if ((stat
& (SD_CMD_STATUS
| SD_DAT3_STATUS
| SD_DAT2_STATUS
|
754 SD_DAT1_STATUS
| SD_DAT0_STATUS
)) !=
755 (SD_CMD_STATUS
| SD_DAT3_STATUS
| SD_DAT2_STATUS
|
756 SD_DAT1_STATUS
| SD_DAT0_STATUS
)) {
757 RTSX_DEBUGP("SD_BUS_STAT: 0x%x\n", stat
);
758 rtsx_write_register(chip
, SD_BUS_STAT
,
759 SD_CLK_TOGGLE_EN
| SD_CLK_FORCE_STOP
, 0);
760 rtsx_write_register(chip
, CARD_CLK_EN
, 0xFF, 0);
761 TRACE_RET(chip
, STATUS_FAIL
);
764 RTSX_WRITE_REG(chip
, SD_BUS_STAT
, SD_CLK_TOGGLE_EN
| SD_CLK_FORCE_STOP
,
767 return STATUS_SUCCESS
;
770 static int sd_reset_dcm(struct rtsx_chip
*chip
, u8 tune_dir
)
772 if (tune_dir
== TUNE_RX
) {
773 RTSX_WRITE_REG(chip
, DCM_DRP_CTL
, 0xFF, DCM_RESET
| DCM_RX
);
774 RTSX_WRITE_REG(chip
, DCM_DRP_CTL
, 0xFF, DCM_RX
);
776 RTSX_WRITE_REG(chip
, DCM_DRP_CTL
, 0xFF, DCM_RESET
| DCM_TX
);
777 RTSX_WRITE_REG(chip
, DCM_DRP_CTL
, 0xFF, DCM_TX
);
780 return STATUS_SUCCESS
;
783 static int sd_change_phase(struct rtsx_chip
*chip
, u8 sample_point
, u8 tune_dir
)
785 struct sd_info
*sd_card
= &(chip
->sd_card
);
786 u16 SD_VP_CTL
, SD_DCMPS_CTL
;
791 RTSX_DEBUGP("sd_change_phase (sample_point = %d, tune_dir = %d)\n",
792 sample_point
, tune_dir
);
794 if (tune_dir
== TUNE_RX
) {
795 SD_VP_CTL
= SD_VPRX_CTL
;
796 SD_DCMPS_CTL
= SD_DCMPS_RX_CTL
;
797 if (CHK_SD_DDR50(sd_card
))
800 SD_VP_CTL
= SD_VPTX_CTL
;
801 SD_DCMPS_CTL
= SD_DCMPS_TX_CTL
;
804 if (chip
->asic_code
) {
805 RTSX_WRITE_REG(chip
, CLK_CTL
, CHANGE_CLK
, CHANGE_CLK
);
806 RTSX_WRITE_REG(chip
, SD_VP_CTL
, 0x1F, sample_point
);
807 RTSX_WRITE_REG(chip
, SD_VPCLK0_CTL
, PHASE_NOT_RESET
, 0);
808 RTSX_WRITE_REG(chip
, SD_VPCLK0_CTL
, PHASE_NOT_RESET
,
810 RTSX_WRITE_REG(chip
, CLK_CTL
, CHANGE_CLK
, 0);
812 #ifdef CONFIG_RTS5208_DEBUG
813 rtsx_read_register(chip
, SD_VP_CTL
, &val
);
814 RTSX_DEBUGP("SD_VP_CTL: 0x%x\n", val
);
815 rtsx_read_register(chip
, SD_DCMPS_CTL
, &val
);
816 RTSX_DEBUGP("SD_DCMPS_CTL: 0x%x\n", val
);
820 RTSX_WRITE_REG(chip
, SD_VP_CTL
, PHASE_CHANGE
,
823 RTSX_WRITE_REG(chip
, SD_VP_CTL
, 0xFF,
824 PHASE_CHANGE
| PHASE_NOT_RESET
| sample_point
);
826 RTSX_WRITE_REG(chip
, CLK_CTL
, CHANGE_CLK
, CHANGE_CLK
);
828 RTSX_WRITE_REG(chip
, SD_VP_CTL
, 0xFF,
829 PHASE_NOT_RESET
| sample_point
);
834 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SD_DCMPS_CTL
, DCMPS_CHANGE
,
836 rtsx_add_cmd(chip
, CHECK_REG_CMD
, SD_DCMPS_CTL
,
837 DCMPS_CHANGE_DONE
, DCMPS_CHANGE_DONE
);
838 retval
= rtsx_send_cmd(chip
, SD_CARD
, 100);
839 if (retval
!= STATUS_SUCCESS
)
840 TRACE_GOTO(chip
, Fail
);
842 val
= *rtsx_get_cmd_data(chip
);
843 if (val
& DCMPS_ERROR
)
844 TRACE_GOTO(chip
, Fail
);
846 if ((val
& DCMPS_CURRENT_PHASE
) != sample_point
)
847 TRACE_GOTO(chip
, Fail
);
849 RTSX_WRITE_REG(chip
, SD_DCMPS_CTL
, DCMPS_CHANGE
, 0);
851 RTSX_WRITE_REG(chip
, SD_VP_CTL
, PHASE_CHANGE
, 0);
853 RTSX_WRITE_REG(chip
, CLK_CTL
, CHANGE_CLK
, 0);
858 RTSX_WRITE_REG(chip
, SD_CFG1
, SD_ASYNC_FIFO_NOT_RST
, 0);
860 return STATUS_SUCCESS
;
863 #ifdef CONFIG_RTS5208_DEBUG
864 rtsx_read_register(chip
, SD_VP_CTL
, &val
);
865 RTSX_DEBUGP("SD_VP_CTL: 0x%x\n", val
);
866 rtsx_read_register(chip
, SD_DCMPS_CTL
, &val
);
867 RTSX_DEBUGP("SD_DCMPS_CTL: 0x%x\n", val
);
870 rtsx_write_register(chip
, SD_DCMPS_CTL
, DCMPS_CHANGE
, 0);
871 rtsx_write_register(chip
, SD_VP_CTL
, PHASE_CHANGE
, 0);
873 sd_reset_dcm(chip
, tune_dir
);
877 static int sd_check_spec(struct rtsx_chip
*chip
, u8 bus_width
)
879 struct sd_info
*sd_card
= &(chip
->sd_card
);
883 retval
= sd_send_cmd_get_rsp(chip
, APP_CMD
, sd_card
->sd_addr
,
884 SD_RSP_TYPE_R1
, NULL
, 0);
885 if (retval
!= STATUS_SUCCESS
)
886 TRACE_RET(chip
, STATUS_FAIL
);
888 cmd
[0] = 0x40 | SEND_SCR
;
894 retval
= sd_read_data(chip
, SD_TM_NORMAL_READ
, cmd
, 5, 8, 1, bus_width
,
896 if (retval
!= STATUS_SUCCESS
) {
897 rtsx_clear_sd_error(chip
);
898 TRACE_RET(chip
, STATUS_FAIL
);
901 memcpy(sd_card
->raw_scr
, buf
, 8);
903 if ((buf
[0] & 0x0F) == 0)
904 TRACE_RET(chip
, STATUS_FAIL
);
906 return STATUS_SUCCESS
;
909 static int sd_query_switch_result(struct rtsx_chip
*chip
, u8 func_group
,
910 u8 func_to_switch
, u8
*buf
, int buf_len
)
912 u8 support_mask
= 0, query_switch
= 0, switch_busy
= 0;
913 int support_offset
= 0, query_switch_offset
= 0, check_busy_offset
= 0;
915 if (func_group
== SD_FUNC_GROUP_1
) {
916 support_offset
= FUNCTION_GROUP1_SUPPORT_OFFSET
;
917 query_switch_offset
= FUNCTION_GROUP1_QUERY_SWITCH_OFFSET
;
918 check_busy_offset
= FUNCTION_GROUP1_CHECK_BUSY_OFFSET
;
920 switch (func_to_switch
) {
922 support_mask
= HS_SUPPORT_MASK
;
923 query_switch
= HS_QUERY_SWITCH_OK
;
924 switch_busy
= HS_SWITCH_BUSY
;
928 support_mask
= SDR50_SUPPORT_MASK
;
929 query_switch
= SDR50_QUERY_SWITCH_OK
;
930 switch_busy
= SDR50_SWITCH_BUSY
;
934 support_mask
= SDR104_SUPPORT_MASK
;
935 query_switch
= SDR104_QUERY_SWITCH_OK
;
936 switch_busy
= SDR104_SWITCH_BUSY
;
940 support_mask
= DDR50_SUPPORT_MASK
;
941 query_switch
= DDR50_QUERY_SWITCH_OK
;
942 switch_busy
= DDR50_SWITCH_BUSY
;
946 TRACE_RET(chip
, STATUS_FAIL
);
948 } else if (func_group
== SD_FUNC_GROUP_3
) {
949 support_offset
= FUNCTION_GROUP3_SUPPORT_OFFSET
;
950 query_switch_offset
= FUNCTION_GROUP3_QUERY_SWITCH_OFFSET
;
951 check_busy_offset
= FUNCTION_GROUP3_CHECK_BUSY_OFFSET
;
953 switch (func_to_switch
) {
955 support_mask
= DRIVING_TYPE_A_MASK
;
956 query_switch
= TYPE_A_QUERY_SWITCH_OK
;
957 switch_busy
= TYPE_A_SWITCH_BUSY
;
961 support_mask
= DRIVING_TYPE_C_MASK
;
962 query_switch
= TYPE_C_QUERY_SWITCH_OK
;
963 switch_busy
= TYPE_C_SWITCH_BUSY
;
967 support_mask
= DRIVING_TYPE_D_MASK
;
968 query_switch
= TYPE_D_QUERY_SWITCH_OK
;
969 switch_busy
= TYPE_D_SWITCH_BUSY
;
973 TRACE_RET(chip
, STATUS_FAIL
);
975 } else if (func_group
== SD_FUNC_GROUP_4
) {
976 support_offset
= FUNCTION_GROUP4_SUPPORT_OFFSET
;
977 query_switch_offset
= FUNCTION_GROUP4_QUERY_SWITCH_OFFSET
;
978 check_busy_offset
= FUNCTION_GROUP4_CHECK_BUSY_OFFSET
;
980 switch (func_to_switch
) {
981 case CURRENT_LIMIT_400
:
982 support_mask
= CURRENT_LIMIT_400_MASK
;
983 query_switch
= CURRENT_LIMIT_400_QUERY_SWITCH_OK
;
984 switch_busy
= CURRENT_LIMIT_400_SWITCH_BUSY
;
987 case CURRENT_LIMIT_600
:
988 support_mask
= CURRENT_LIMIT_600_MASK
;
989 query_switch
= CURRENT_LIMIT_600_QUERY_SWITCH_OK
;
990 switch_busy
= CURRENT_LIMIT_600_SWITCH_BUSY
;
993 case CURRENT_LIMIT_800
:
994 support_mask
= CURRENT_LIMIT_800_MASK
;
995 query_switch
= CURRENT_LIMIT_800_QUERY_SWITCH_OK
;
996 switch_busy
= CURRENT_LIMIT_800_SWITCH_BUSY
;
1000 TRACE_RET(chip
, STATUS_FAIL
);
1003 TRACE_RET(chip
, STATUS_FAIL
);
1006 if (func_group
== SD_FUNC_GROUP_1
) {
1007 if (!(buf
[support_offset
] & support_mask
) ||
1008 ((buf
[query_switch_offset
] & 0x0F) != query_switch
)) {
1009 TRACE_RET(chip
, STATUS_FAIL
);
1013 /* Check 'Busy Status' */
1014 if ((buf
[DATA_STRUCTURE_VER_OFFSET
] == 0x01) &&
1015 ((buf
[check_busy_offset
] & switch_busy
) == switch_busy
)) {
1016 TRACE_RET(chip
, STATUS_FAIL
);
1019 return STATUS_SUCCESS
;
1022 static int sd_check_switch_mode(struct rtsx_chip
*chip
, u8 mode
,
1023 u8 func_group
, u8 func_to_switch
, u8 bus_width
)
1025 struct sd_info
*sd_card
= &(chip
->sd_card
);
1029 RTSX_DEBUGP("sd_check_switch_mode (mode = %d, func_group = %d, func_to_switch = %d)\n",
1030 mode
, func_group
, func_to_switch
);
1032 cmd
[0] = 0x40 | SWITCH
;
1035 if (func_group
== SD_FUNC_GROUP_1
) {
1038 cmd
[4] = 0xF0 + func_to_switch
;
1039 } else if (func_group
== SD_FUNC_GROUP_3
) {
1041 cmd
[3] = 0xF0 + func_to_switch
;
1043 } else if (func_group
== SD_FUNC_GROUP_4
) {
1045 cmd
[3] = 0x0F + (func_to_switch
<< 4);
1048 cmd
[1] = SD_CHECK_MODE
;
1054 retval
= sd_read_data(chip
, SD_TM_NORMAL_READ
, cmd
, 5, 64, 1, bus_width
,
1056 if (retval
!= STATUS_SUCCESS
) {
1057 rtsx_clear_sd_error(chip
);
1058 TRACE_RET(chip
, STATUS_FAIL
);
1063 if (func_group
== NO_ARGUMENT
) {
1064 sd_card
->func_group1_mask
= buf
[0x0D];
1065 sd_card
->func_group2_mask
= buf
[0x0B];
1066 sd_card
->func_group3_mask
= buf
[0x09];
1067 sd_card
->func_group4_mask
= buf
[0x07];
1069 RTSX_DEBUGP("func_group1_mask = 0x%02x\n", buf
[0x0D]);
1070 RTSX_DEBUGP("func_group2_mask = 0x%02x\n", buf
[0x0B]);
1071 RTSX_DEBUGP("func_group3_mask = 0x%02x\n", buf
[0x09]);
1072 RTSX_DEBUGP("func_group4_mask = 0x%02x\n", buf
[0x07]);
1074 /* Maximum current consumption, check whether current is
1075 * acceptable; bit[511:496] = 0x0000 means some error happened.
1077 u16 cc
= ((u16
)buf
[0] << 8) | buf
[1];
1078 RTSX_DEBUGP("Maximum current consumption: %dmA\n", cc
);
1079 if ((cc
== 0) || (cc
> 800))
1080 TRACE_RET(chip
, STATUS_FAIL
);
1082 retval
= sd_query_switch_result(chip
, func_group
,
1083 func_to_switch
, buf
, 64);
1084 if (retval
!= STATUS_SUCCESS
)
1085 TRACE_RET(chip
, STATUS_FAIL
);
1087 if ((cc
> 400) || (func_to_switch
> CURRENT_LIMIT_400
)) {
1088 RTSX_WRITE_REG(chip
, OCPPARA2
, SD_OCP_THD_MASK
,
1089 chip
->sd_800mA_ocp_thd
);
1090 RTSX_WRITE_REG(chip
, CARD_PWR_CTL
, PMOS_STRG_MASK
,
1095 return STATUS_SUCCESS
;
1098 static u8
downgrade_switch_mode(u8 func_group
, u8 func_to_switch
)
1100 if (func_group
== SD_FUNC_GROUP_1
) {
1101 if (func_to_switch
> HS_SUPPORT
)
1104 } else if (func_group
== SD_FUNC_GROUP_4
) {
1105 if (func_to_switch
> CURRENT_LIMIT_200
)
1109 return func_to_switch
;
1112 static int sd_check_switch(struct rtsx_chip
*chip
,
1113 u8 func_group
, u8 func_to_switch
, u8 bus_width
)
1117 int switch_good
= 0;
1119 for (i
= 0; i
< 3; i
++) {
1120 if (detect_card_cd(chip
, SD_CARD
) != STATUS_SUCCESS
) {
1121 sd_set_err_code(chip
, SD_NO_CARD
);
1122 TRACE_RET(chip
, STATUS_FAIL
);
1125 retval
= sd_check_switch_mode(chip
, SD_CHECK_MODE
, func_group
,
1126 func_to_switch
, bus_width
);
1127 if (retval
== STATUS_SUCCESS
) {
1130 retval
= sd_check_switch_mode(chip
, SD_SWITCH_MODE
,
1131 func_group
, func_to_switch
, bus_width
);
1132 if (retval
== STATUS_SUCCESS
) {
1137 RTSX_READ_REG(chip
, SD_STAT1
, &stat
);
1138 if (stat
& SD_CRC16_ERR
) {
1139 RTSX_DEBUGP("SD CRC16 error when switching mode\n");
1140 TRACE_RET(chip
, STATUS_FAIL
);
1144 func_to_switch
= downgrade_switch_mode(func_group
,
1151 TRACE_RET(chip
, STATUS_FAIL
);
1153 return STATUS_SUCCESS
;
1156 static int sd_switch_function(struct rtsx_chip
*chip
, u8 bus_width
)
1158 struct sd_info
*sd_card
= &(chip
->sd_card
);
1161 u8 func_to_switch
= 0;
1163 /* Get supported functions */
1164 retval
= sd_check_switch_mode(chip
, SD_CHECK_MODE
,
1165 NO_ARGUMENT
, NO_ARGUMENT
, bus_width
);
1166 if (retval
!= STATUS_SUCCESS
)
1167 TRACE_RET(chip
, STATUS_FAIL
);
1169 sd_card
->func_group1_mask
&= ~(sd_card
->sd_switch_fail
);
1171 /* Function Group 1: Access Mode */
1172 for (i
= 0; i
< 4; i
++) {
1173 switch ((u8
)(chip
->sd_speed_prior
>> (i
*8))) {
1174 case SDR104_SUPPORT
:
1175 if ((sd_card
->func_group1_mask
& SDR104_SUPPORT_MASK
)
1176 && chip
->sdr104_en
) {
1177 func_to_switch
= SDR104_SUPPORT
;
1182 if ((sd_card
->func_group1_mask
& DDR50_SUPPORT_MASK
)
1183 && chip
->ddr50_en
) {
1184 func_to_switch
= DDR50_SUPPORT
;
1189 if ((sd_card
->func_group1_mask
& SDR50_SUPPORT_MASK
)
1190 && chip
->sdr50_en
) {
1191 func_to_switch
= SDR50_SUPPORT
;
1196 if (sd_card
->func_group1_mask
& HS_SUPPORT_MASK
)
1197 func_to_switch
= HS_SUPPORT
;
1210 RTSX_DEBUGP("SD_FUNC_GROUP_1: func_to_switch = 0x%02x", func_to_switch
);
1212 #ifdef SUPPORT_SD_LOCK
1213 if ((sd_card
->sd_lock_status
& SD_SDR_RST
)
1214 && (DDR50_SUPPORT
== func_to_switch
)
1215 && (sd_card
->func_group1_mask
& SDR50_SUPPORT_MASK
)) {
1216 func_to_switch
= SDR50_SUPPORT
;
1217 RTSX_DEBUGP("Using SDR50 instead of DDR50 for SD Lock\n");
1221 if (func_to_switch
) {
1222 retval
= sd_check_switch(chip
, SD_FUNC_GROUP_1
, func_to_switch
,
1224 if (retval
!= STATUS_SUCCESS
) {
1225 if (func_to_switch
== SDR104_SUPPORT
) {
1226 sd_card
->sd_switch_fail
= SDR104_SUPPORT_MASK
;
1227 } else if (func_to_switch
== DDR50_SUPPORT
) {
1228 sd_card
->sd_switch_fail
= SDR104_SUPPORT_MASK
|
1230 } else if (func_to_switch
== SDR50_SUPPORT
) {
1231 sd_card
->sd_switch_fail
= SDR104_SUPPORT_MASK
|
1232 DDR50_SUPPORT_MASK
| SDR50_SUPPORT_MASK
;
1234 TRACE_RET(chip
, STATUS_FAIL
);
1237 if (func_to_switch
== SDR104_SUPPORT
)
1238 SET_SD_SDR104(sd_card
);
1239 else if (func_to_switch
== DDR50_SUPPORT
)
1240 SET_SD_DDR50(sd_card
);
1241 else if (func_to_switch
== SDR50_SUPPORT
)
1242 SET_SD_SDR50(sd_card
);
1247 if (CHK_SD_DDR50(sd_card
)) {
1248 RTSX_WRITE_REG(chip
, SD_PUSH_POINT_CTL
, 0x06, 0x04);
1249 retval
= sd_set_sample_push_timing(chip
);
1250 if (retval
!= STATUS_SUCCESS
)
1251 TRACE_RET(chip
, STATUS_FAIL
);
1254 if (!func_to_switch
|| (func_to_switch
== HS_SUPPORT
)) {
1255 /* Do not try to switch current limit if the card doesn't
1256 * support UHS mode or we don't want it to support UHS mode
1258 return STATUS_SUCCESS
;
1261 /* Function Group 4: Current Limit */
1262 func_to_switch
= 0xFF;
1264 for (i
= 0; i
< 4; i
++) {
1265 switch ((u8
)(chip
->sd_current_prior
>> (i
*8))) {
1266 case CURRENT_LIMIT_800
:
1267 if (sd_card
->func_group4_mask
& CURRENT_LIMIT_800_MASK
)
1268 func_to_switch
= CURRENT_LIMIT_800
;
1272 case CURRENT_LIMIT_600
:
1273 if (sd_card
->func_group4_mask
& CURRENT_LIMIT_600_MASK
)
1274 func_to_switch
= CURRENT_LIMIT_600
;
1278 case CURRENT_LIMIT_400
:
1279 if (sd_card
->func_group4_mask
& CURRENT_LIMIT_400_MASK
)
1280 func_to_switch
= CURRENT_LIMIT_400
;
1284 case CURRENT_LIMIT_200
:
1285 if (sd_card
->func_group4_mask
& CURRENT_LIMIT_200_MASK
)
1286 func_to_switch
= CURRENT_LIMIT_200
;
1294 if (func_to_switch
!= 0xFF)
1298 RTSX_DEBUGP("SD_FUNC_GROUP_4: func_to_switch = 0x%02x", func_to_switch
);
1300 if (func_to_switch
<= CURRENT_LIMIT_800
) {
1301 retval
= sd_check_switch(chip
, SD_FUNC_GROUP_4
, func_to_switch
,
1303 if (retval
!= STATUS_SUCCESS
) {
1304 if (sd_check_err_code(chip
, SD_NO_CARD
))
1305 TRACE_RET(chip
, STATUS_FAIL
);
1307 RTSX_DEBUGP("Switch current limit finished! (%d)\n", retval
);
1310 if (CHK_SD_DDR50(sd_card
))
1311 RTSX_WRITE_REG(chip
, SD_PUSH_POINT_CTL
, 0x06, 0);
1313 return STATUS_SUCCESS
;
1316 static int sd_wait_data_idle(struct rtsx_chip
*chip
)
1318 int retval
= STATUS_TIMEDOUT
;
1322 for (i
= 0; i
< 100; i
++) {
1323 RTSX_READ_REG(chip
, SD_DATA_STATE
, &val
);
1324 if (val
& SD_DATA_IDLE
) {
1325 retval
= STATUS_SUCCESS
;
1330 RTSX_DEBUGP("SD_DATA_STATE: 0x%02x\n", val
);
1335 static int sd_sdr_tuning_rx_cmd(struct rtsx_chip
*chip
, u8 sample_point
)
1340 retval
= sd_change_phase(chip
, sample_point
, TUNE_RX
);
1341 if (retval
!= STATUS_SUCCESS
)
1342 TRACE_RET(chip
, STATUS_FAIL
);
1344 cmd
[0] = 0x40 | SEND_TUNING_PATTERN
;
1350 retval
= sd_read_data(chip
, SD_TM_AUTO_TUNING
,
1351 cmd
, 5, 0x40, 1, SD_BUS_WIDTH_4
, NULL
, 0, 100);
1352 if (retval
!= STATUS_SUCCESS
) {
1353 (void)sd_wait_data_idle(chip
);
1355 rtsx_clear_sd_error(chip
);
1356 TRACE_RET(chip
, STATUS_FAIL
);
1359 return STATUS_SUCCESS
;
1362 static int sd_ddr_tuning_rx_cmd(struct rtsx_chip
*chip
, u8 sample_point
)
1364 struct sd_info
*sd_card
= &(chip
->sd_card
);
1368 retval
= sd_change_phase(chip
, sample_point
, TUNE_RX
);
1369 if (retval
!= STATUS_SUCCESS
)
1370 TRACE_RET(chip
, STATUS_FAIL
);
1372 RTSX_DEBUGP("sd ddr tuning rx\n");
1374 retval
= sd_send_cmd_get_rsp(chip
, APP_CMD
, sd_card
->sd_addr
,
1375 SD_RSP_TYPE_R1
, NULL
, 0);
1376 if (retval
!= STATUS_SUCCESS
)
1377 TRACE_RET(chip
, STATUS_FAIL
);
1379 cmd
[0] = 0x40 | SD_STATUS
;
1385 retval
= sd_read_data(chip
, SD_TM_NORMAL_READ
,
1386 cmd
, 5, 64, 1, SD_BUS_WIDTH_4
, NULL
, 0, 100);
1387 if (retval
!= STATUS_SUCCESS
) {
1388 (void)sd_wait_data_idle(chip
);
1390 rtsx_clear_sd_error(chip
);
1391 TRACE_RET(chip
, STATUS_FAIL
);
1394 return STATUS_SUCCESS
;
1397 static int mmc_ddr_tunning_rx_cmd(struct rtsx_chip
*chip
, u8 sample_point
)
1399 struct sd_info
*sd_card
= &(chip
->sd_card
);
1401 u8 cmd
[5], bus_width
;
1403 if (CHK_MMC_8BIT(sd_card
))
1404 bus_width
= SD_BUS_WIDTH_8
;
1405 else if (CHK_MMC_4BIT(sd_card
))
1406 bus_width
= SD_BUS_WIDTH_4
;
1408 bus_width
= SD_BUS_WIDTH_1
;
1410 retval
= sd_change_phase(chip
, sample_point
, TUNE_RX
);
1411 if (retval
!= STATUS_SUCCESS
)
1412 TRACE_RET(chip
, STATUS_FAIL
);
1414 RTSX_DEBUGP("mmc ddr tuning rx\n");
1416 cmd
[0] = 0x40 | SEND_EXT_CSD
;
1422 retval
= sd_read_data(chip
, SD_TM_NORMAL_READ
,
1423 cmd
, 5, 0x200, 1, bus_width
, NULL
, 0, 100);
1424 if (retval
!= STATUS_SUCCESS
) {
1425 (void)sd_wait_data_idle(chip
);
1427 rtsx_clear_sd_error(chip
);
1428 TRACE_RET(chip
, STATUS_FAIL
);
1431 return STATUS_SUCCESS
;
1434 static int sd_sdr_tuning_tx_cmd(struct rtsx_chip
*chip
, u8 sample_point
)
1436 struct sd_info
*sd_card
= &(chip
->sd_card
);
1439 retval
= sd_change_phase(chip
, sample_point
, TUNE_TX
);
1440 if (retval
!= STATUS_SUCCESS
)
1441 TRACE_RET(chip
, STATUS_FAIL
);
1443 RTSX_WRITE_REG(chip
, SD_CFG3
, SD_RSP_80CLK_TIMEOUT_EN
,
1444 SD_RSP_80CLK_TIMEOUT_EN
);
1446 retval
= sd_send_cmd_get_rsp(chip
, SEND_STATUS
, sd_card
->sd_addr
,
1447 SD_RSP_TYPE_R1
, NULL
, 0);
1448 if (retval
!= STATUS_SUCCESS
) {
1449 if (sd_check_err_code(chip
, SD_RSP_TIMEOUT
)) {
1450 rtsx_write_register(chip
, SD_CFG3
,
1451 SD_RSP_80CLK_TIMEOUT_EN
, 0);
1452 TRACE_RET(chip
, STATUS_FAIL
);
1456 RTSX_WRITE_REG(chip
, SD_CFG3
, SD_RSP_80CLK_TIMEOUT_EN
, 0);
1458 return STATUS_SUCCESS
;
1461 static int sd_ddr_tuning_tx_cmd(struct rtsx_chip
*chip
, u8 sample_point
)
1463 struct sd_info
*sd_card
= &(chip
->sd_card
);
1465 u8 cmd
[5], bus_width
;
1467 retval
= sd_change_phase(chip
, sample_point
, TUNE_TX
);
1468 if (retval
!= STATUS_SUCCESS
)
1469 TRACE_RET(chip
, STATUS_FAIL
);
1471 if (CHK_SD(sd_card
)) {
1472 bus_width
= SD_BUS_WIDTH_4
;
1474 if (CHK_MMC_8BIT(sd_card
))
1475 bus_width
= SD_BUS_WIDTH_8
;
1476 else if (CHK_MMC_4BIT(sd_card
))
1477 bus_width
= SD_BUS_WIDTH_4
;
1479 bus_width
= SD_BUS_WIDTH_1
;
1482 retval
= sd_wait_state_data_ready(chip
, 0x08, 1, 1000);
1483 if (retval
!= STATUS_SUCCESS
)
1484 TRACE_RET(chip
, STATUS_FAIL
);
1486 RTSX_WRITE_REG(chip
, SD_CFG3
, SD_RSP_80CLK_TIMEOUT_EN
,
1487 SD_RSP_80CLK_TIMEOUT_EN
);
1489 cmd
[0] = 0x40 | PROGRAM_CSD
;
1495 retval
= sd_write_data(chip
, SD_TM_AUTO_WRITE_2
,
1496 cmd
, 5, 16, 1, bus_width
, sd_card
->raw_csd
, 16, 100);
1497 if (retval
!= STATUS_SUCCESS
) {
1498 rtsx_clear_sd_error(chip
);
1499 rtsx_write_register(chip
, SD_CFG3
, SD_RSP_80CLK_TIMEOUT_EN
, 0);
1500 TRACE_RET(chip
, STATUS_FAIL
);
1503 RTSX_WRITE_REG(chip
, SD_CFG3
, SD_RSP_80CLK_TIMEOUT_EN
, 0);
1505 sd_send_cmd_get_rsp(chip
, SEND_STATUS
, sd_card
->sd_addr
, SD_RSP_TYPE_R1
,
1508 return STATUS_SUCCESS
;
1511 static u8
sd_search_final_phase(struct rtsx_chip
*chip
, u32 phase_map
,
1514 struct sd_info
*sd_card
= &(chip
->sd_card
);
1515 struct timing_phase_path path
[MAX_PHASE
+ 1];
1516 int i
, j
, cont_path_cnt
;
1517 int new_block
, max_len
, final_path_idx
;
1518 u8 final_phase
= 0xFF;
1520 if (phase_map
== 0xFFFFFFFF) {
1521 if (tune_dir
== TUNE_RX
)
1522 final_phase
= (u8
)chip
->sd_default_rx_phase
;
1524 final_phase
= (u8
)chip
->sd_default_tx_phase
;
1532 for (i
= 0; i
< MAX_PHASE
+ 1; i
++) {
1533 if (phase_map
& (1 << i
)) {
1536 j
= cont_path_cnt
++;
1544 if (cont_path_cnt
) {
1545 int idx
= cont_path_cnt
- 1;
1546 path
[idx
].len
= path
[idx
].end
-
1547 path
[idx
].start
+ 1;
1548 path
[idx
].mid
= path
[idx
].start
+
1554 if (cont_path_cnt
== 0) {
1555 RTSX_DEBUGP("No continuous phase path\n");
1558 int idx
= cont_path_cnt
- 1;
1559 path
[idx
].len
= path
[idx
].end
- path
[idx
].start
+ 1;
1560 path
[idx
].mid
= path
[idx
].start
+ path
[idx
].len
/ 2;
1563 if ((path
[0].start
== 0) &&
1564 (path
[cont_path_cnt
- 1].end
== MAX_PHASE
)) {
1565 path
[0].start
= path
[cont_path_cnt
- 1].start
- MAX_PHASE
- 1;
1566 path
[0].len
+= path
[cont_path_cnt
- 1].len
;
1567 path
[0].mid
= path
[0].start
+ path
[0].len
/ 2;
1568 if (path
[0].mid
< 0)
1569 path
[0].mid
+= MAX_PHASE
+ 1;
1577 for (i
= 0; i
< cont_path_cnt
; i
++) {
1578 if (path
[i
].len
> max_len
) {
1579 max_len
= path
[i
].len
;
1580 final_phase
= (u8
)path
[i
].mid
;
1584 RTSX_DEBUGP("path[%d].start = %d\n", i
, path
[i
].start
);
1585 RTSX_DEBUGP("path[%d].end = %d\n", i
, path
[i
].end
);
1586 RTSX_DEBUGP("path[%d].len = %d\n", i
, path
[i
].len
);
1587 RTSX_DEBUGP("path[%d].mid = %d\n", i
, path
[i
].mid
);
1591 if (tune_dir
== TUNE_TX
) {
1592 if (CHK_SD_SDR104(sd_card
)) {
1594 int temp_mid
= (max_len
- 16) / 2;
1595 int temp_final_phase
=
1596 path
[final_path_idx
].end
-
1597 (max_len
- (6 + temp_mid
));
1599 if (temp_final_phase
< 0)
1600 final_phase
= (u8
)(temp_final_phase
+
1603 final_phase
= (u8
)temp_final_phase
;
1605 } else if (CHK_SD_SDR50(sd_card
)) {
1607 int temp_mid
= (max_len
- 13) / 2;
1608 int temp_final_phase
=
1609 path
[final_path_idx
].end
-
1610 (max_len
- (3 + temp_mid
));
1612 if (temp_final_phase
< 0)
1613 final_phase
= (u8
)(temp_final_phase
+
1616 final_phase
= (u8
)temp_final_phase
;
1622 RTSX_DEBUGP("Final chosen phase: %d\n", final_phase
);
1626 static int sd_tuning_rx(struct rtsx_chip
*chip
)
1628 struct sd_info
*sd_card
= &(chip
->sd_card
);
1631 u32 raw_phase_map
[3], phase_map
;
1633 int (*tuning_cmd
)(struct rtsx_chip
*chip
, u8 sample_point
);
1635 if (CHK_SD(sd_card
)) {
1636 if (CHK_SD_DDR50(sd_card
))
1637 tuning_cmd
= sd_ddr_tuning_rx_cmd
;
1639 tuning_cmd
= sd_sdr_tuning_rx_cmd
;
1642 if (CHK_MMC_DDR52(sd_card
))
1643 tuning_cmd
= mmc_ddr_tunning_rx_cmd
;
1645 TRACE_RET(chip
, STATUS_FAIL
);
1648 for (i
= 0; i
< 3; i
++) {
1649 raw_phase_map
[i
] = 0;
1650 for (j
= MAX_PHASE
; j
>= 0; j
--) {
1651 if (detect_card_cd(chip
, SD_CARD
) != STATUS_SUCCESS
) {
1652 sd_set_err_code(chip
, SD_NO_CARD
);
1653 TRACE_RET(chip
, STATUS_FAIL
);
1656 retval
= tuning_cmd(chip
, (u8
)j
);
1657 if (retval
== STATUS_SUCCESS
)
1658 raw_phase_map
[i
] |= 1 << j
;
1662 phase_map
= raw_phase_map
[0] & raw_phase_map
[1] & raw_phase_map
[2];
1663 for (i
= 0; i
< 3; i
++)
1664 RTSX_DEBUGP("RX raw_phase_map[%d] = 0x%08x\n", i
,
1667 RTSX_DEBUGP("RX phase_map = 0x%08x\n", phase_map
);
1669 final_phase
= sd_search_final_phase(chip
, phase_map
, TUNE_RX
);
1670 if (final_phase
== 0xFF)
1671 TRACE_RET(chip
, STATUS_FAIL
);
1673 retval
= sd_change_phase(chip
, final_phase
, TUNE_RX
);
1674 if (retval
!= STATUS_SUCCESS
)
1675 TRACE_RET(chip
, STATUS_FAIL
);
1677 return STATUS_SUCCESS
;
1680 static int sd_ddr_pre_tuning_tx(struct rtsx_chip
*chip
)
1682 struct sd_info
*sd_card
= &(chip
->sd_card
);
1688 RTSX_WRITE_REG(chip
, SD_CFG3
, SD_RSP_80CLK_TIMEOUT_EN
,
1689 SD_RSP_80CLK_TIMEOUT_EN
);
1692 for (i
= MAX_PHASE
; i
>= 0; i
--) {
1693 if (detect_card_cd(chip
, SD_CARD
) != STATUS_SUCCESS
) {
1694 sd_set_err_code(chip
, SD_NO_CARD
);
1695 rtsx_write_register(chip
, SD_CFG3
,
1696 SD_RSP_80CLK_TIMEOUT_EN
, 0);
1697 TRACE_RET(chip
, STATUS_FAIL
);
1700 retval
= sd_change_phase(chip
, (u8
)i
, TUNE_TX
);
1701 if (retval
!= STATUS_SUCCESS
)
1704 retval
= sd_send_cmd_get_rsp(chip
, SEND_STATUS
,
1705 sd_card
->sd_addr
, SD_RSP_TYPE_R1
, NULL
,
1707 if ((retval
== STATUS_SUCCESS
) ||
1708 !sd_check_err_code(chip
, SD_RSP_TIMEOUT
))
1709 phase_map
|= 1 << i
;
1712 RTSX_WRITE_REG(chip
, SD_CFG3
, SD_RSP_80CLK_TIMEOUT_EN
, 0);
1714 RTSX_DEBUGP("DDR TX pre tune phase_map = 0x%08x\n", phase_map
);
1716 final_phase
= sd_search_final_phase(chip
, phase_map
, TUNE_TX
);
1717 if (final_phase
== 0xFF)
1718 TRACE_RET(chip
, STATUS_FAIL
);
1720 retval
= sd_change_phase(chip
, final_phase
, TUNE_TX
);
1721 if (retval
!= STATUS_SUCCESS
)
1722 TRACE_RET(chip
, STATUS_FAIL
);
1724 RTSX_DEBUGP("DDR TX pre tune phase: %d\n", (int)final_phase
);
1726 return STATUS_SUCCESS
;
1729 static int sd_tuning_tx(struct rtsx_chip
*chip
)
1731 struct sd_info
*sd_card
= &(chip
->sd_card
);
1734 u32 raw_phase_map
[3], phase_map
;
1736 int (*tuning_cmd
)(struct rtsx_chip
*chip
, u8 sample_point
);
1738 if (CHK_SD(sd_card
)) {
1739 if (CHK_SD_DDR50(sd_card
))
1740 tuning_cmd
= sd_ddr_tuning_tx_cmd
;
1742 tuning_cmd
= sd_sdr_tuning_tx_cmd
;
1745 if (CHK_MMC_DDR52(sd_card
))
1746 tuning_cmd
= sd_ddr_tuning_tx_cmd
;
1748 TRACE_RET(chip
, STATUS_FAIL
);
1751 for (i
= 0; i
< 3; i
++) {
1752 raw_phase_map
[i
] = 0;
1753 for (j
= MAX_PHASE
; j
>= 0; j
--) {
1754 if (detect_card_cd(chip
, SD_CARD
) != STATUS_SUCCESS
) {
1755 sd_set_err_code(chip
, SD_NO_CARD
);
1756 rtsx_write_register(chip
, SD_CFG3
,
1757 SD_RSP_80CLK_TIMEOUT_EN
, 0);
1758 TRACE_RET(chip
, STATUS_FAIL
);
1761 retval
= tuning_cmd(chip
, (u8
)j
);
1762 if (retval
== STATUS_SUCCESS
)
1763 raw_phase_map
[i
] |= 1 << j
;
1767 phase_map
= raw_phase_map
[0] & raw_phase_map
[1] & raw_phase_map
[2];
1768 for (i
= 0; i
< 3; i
++)
1769 RTSX_DEBUGP("TX raw_phase_map[%d] = 0x%08x\n",
1770 i
, raw_phase_map
[i
]);
1772 RTSX_DEBUGP("TX phase_map = 0x%08x\n", phase_map
);
1774 final_phase
= sd_search_final_phase(chip
, phase_map
, TUNE_TX
);
1775 if (final_phase
== 0xFF)
1776 TRACE_RET(chip
, STATUS_FAIL
);
1778 retval
= sd_change_phase(chip
, final_phase
, TUNE_TX
);
1779 if (retval
!= STATUS_SUCCESS
)
1780 TRACE_RET(chip
, STATUS_FAIL
);
1782 return STATUS_SUCCESS
;
1785 static int sd_sdr_tuning(struct rtsx_chip
*chip
)
1789 retval
= sd_tuning_tx(chip
);
1790 if (retval
!= STATUS_SUCCESS
)
1791 TRACE_RET(chip
, STATUS_FAIL
);
1793 retval
= sd_tuning_rx(chip
);
1794 if (retval
!= STATUS_SUCCESS
)
1795 TRACE_RET(chip
, STATUS_FAIL
);
1797 return STATUS_SUCCESS
;
1800 static int sd_ddr_tuning(struct rtsx_chip
*chip
)
1804 if (!(chip
->sd_ctl
& SD_DDR_TX_PHASE_SET_BY_USER
)) {
1805 retval
= sd_ddr_pre_tuning_tx(chip
);
1806 if (retval
!= STATUS_SUCCESS
)
1807 TRACE_RET(chip
, STATUS_FAIL
);
1809 retval
= sd_change_phase(chip
, (u8
)chip
->sd_ddr_tx_phase
,
1811 if (retval
!= STATUS_SUCCESS
)
1812 TRACE_RET(chip
, STATUS_FAIL
);
1815 retval
= sd_tuning_rx(chip
);
1816 if (retval
!= STATUS_SUCCESS
)
1817 TRACE_RET(chip
, STATUS_FAIL
);
1819 if (!(chip
->sd_ctl
& SD_DDR_TX_PHASE_SET_BY_USER
)) {
1820 retval
= sd_tuning_tx(chip
);
1821 if (retval
!= STATUS_SUCCESS
)
1822 TRACE_RET(chip
, STATUS_FAIL
);
1825 return STATUS_SUCCESS
;
1828 static int mmc_ddr_tuning(struct rtsx_chip
*chip
)
1832 if (!(chip
->sd_ctl
& MMC_DDR_TX_PHASE_SET_BY_USER
)) {
1833 retval
= sd_ddr_pre_tuning_tx(chip
);
1834 if (retval
!= STATUS_SUCCESS
)
1835 TRACE_RET(chip
, STATUS_FAIL
);
1837 retval
= sd_change_phase(chip
, (u8
)chip
->mmc_ddr_tx_phase
,
1839 if (retval
!= STATUS_SUCCESS
)
1840 TRACE_RET(chip
, STATUS_FAIL
);
1843 retval
= sd_tuning_rx(chip
);
1844 if (retval
!= STATUS_SUCCESS
)
1845 TRACE_RET(chip
, STATUS_FAIL
);
1847 if (!(chip
->sd_ctl
& MMC_DDR_TX_PHASE_SET_BY_USER
)) {
1848 retval
= sd_tuning_tx(chip
);
1849 if (retval
!= STATUS_SUCCESS
)
1850 TRACE_RET(chip
, STATUS_FAIL
);
1853 return STATUS_SUCCESS
;
1856 int sd_switch_clock(struct rtsx_chip
*chip
)
1858 struct sd_info
*sd_card
= &(chip
->sd_card
);
1862 retval
= select_card(chip
, SD_CARD
);
1863 if (retval
!= STATUS_SUCCESS
)
1864 TRACE_RET(chip
, STATUS_FAIL
);
1866 retval
= switch_clock(chip
, sd_card
->sd_clock
);
1867 if (retval
!= STATUS_SUCCESS
)
1868 TRACE_RET(chip
, STATUS_FAIL
);
1871 if (CHK_SD(sd_card
)) {
1872 if (CHK_SD_DDR50(sd_card
))
1873 retval
= sd_ddr_tuning(chip
);
1875 retval
= sd_sdr_tuning(chip
);
1877 if (CHK_MMC_DDR52(sd_card
))
1878 retval
= mmc_ddr_tuning(chip
);
1881 if (retval
!= STATUS_SUCCESS
)
1882 TRACE_RET(chip
, STATUS_FAIL
);
1885 return STATUS_SUCCESS
;
1888 static int sd_prepare_reset(struct rtsx_chip
*chip
)
1890 struct sd_info
*sd_card
= &(chip
->sd_card
);
1893 if (chip
->asic_code
)
1894 sd_card
->sd_clock
= 29;
1896 sd_card
->sd_clock
= CLK_30
;
1898 sd_card
->sd_type
= 0;
1899 sd_card
->seq_mode
= 0;
1900 sd_card
->sd_data_buf_ready
= 0;
1901 sd_card
->capacity
= 0;
1903 #ifdef SUPPORT_SD_LOCK
1904 sd_card
->sd_lock_status
= 0;
1905 sd_card
->sd_erase_status
= 0;
1908 chip
->capacity
[chip
->card2lun
[SD_CARD
]] = 0;
1911 retval
= sd_set_init_para(chip
);
1912 if (retval
!= STATUS_SUCCESS
)
1913 TRACE_RET(chip
, retval
);
1915 RTSX_WRITE_REG(chip
, REG_SD_CFG1
, 0xFF, 0x40);
1917 RTSX_WRITE_REG(chip
, CARD_STOP
, SD_STOP
| SD_CLR_ERR
,
1918 SD_STOP
| SD_CLR_ERR
);
1920 retval
= select_card(chip
, SD_CARD
);
1921 if (retval
!= STATUS_SUCCESS
)
1922 TRACE_RET(chip
, STATUS_FAIL
);
1924 return STATUS_SUCCESS
;
1927 static int sd_pull_ctl_disable(struct rtsx_chip
*chip
)
1929 if (CHECK_PID(chip
, 0x5208)) {
1930 RTSX_WRITE_REG(chip
, CARD_PULL_CTL1
, 0xFF,
1931 XD_D3_PD
| SD_D7_PD
| SD_CLK_PD
| SD_D5_PD
);
1932 RTSX_WRITE_REG(chip
, CARD_PULL_CTL2
, 0xFF,
1933 SD_D6_PD
| SD_D0_PD
| SD_D1_PD
| XD_D5_PD
);
1934 RTSX_WRITE_REG(chip
, CARD_PULL_CTL3
, 0xFF,
1935 SD_D4_PD
| XD_CE_PD
| XD_CLE_PD
| XD_CD_PU
);
1936 RTSX_WRITE_REG(chip
, CARD_PULL_CTL4
, 0xFF,
1937 XD_RDY_PD
| SD_D3_PD
| SD_D2_PD
| XD_ALE_PD
);
1938 RTSX_WRITE_REG(chip
, CARD_PULL_CTL5
, 0xFF,
1939 MS_INS_PU
| SD_WP_PD
| SD_CD_PU
| SD_CMD_PD
);
1940 RTSX_WRITE_REG(chip
, CARD_PULL_CTL6
, 0xFF, MS_D5_PD
| MS_D4_PD
);
1941 } else if (CHECK_PID(chip
, 0x5288)) {
1942 if (CHECK_BARO_PKG(chip
, QFN
)) {
1943 RTSX_WRITE_REG(chip
, CARD_PULL_CTL1
, 0xFF, 0x55);
1944 RTSX_WRITE_REG(chip
, CARD_PULL_CTL2
, 0xFF, 0x55);
1945 RTSX_WRITE_REG(chip
, CARD_PULL_CTL3
, 0xFF, 0x4B);
1946 RTSX_WRITE_REG(chip
, CARD_PULL_CTL4
, 0xFF, 0x69);
1950 return STATUS_SUCCESS
;
1953 int sd_pull_ctl_enable(struct rtsx_chip
*chip
)
1957 rtsx_init_cmd(chip
);
1959 if (CHECK_PID(chip
, 0x5208)) {
1960 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL1
, 0xFF,
1961 XD_D3_PD
| SD_DAT7_PU
| SD_CLK_NP
| SD_D5_PU
);
1962 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL2
, 0xFF,
1963 SD_D6_PU
| SD_D0_PU
| SD_D1_PU
| XD_D5_PD
);
1964 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL3
, 0xFF,
1965 SD_D4_PU
| XD_CE_PD
| XD_CLE_PD
| XD_CD_PU
);
1966 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL4
, 0xFF,
1967 XD_RDY_PD
| SD_D3_PU
| SD_D2_PU
| XD_ALE_PD
);
1968 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL5
, 0xFF,
1969 MS_INS_PU
| SD_WP_PU
| SD_CD_PU
| SD_CMD_PU
);
1970 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL6
, 0xFF,
1971 MS_D5_PD
| MS_D4_PD
);
1972 } else if (CHECK_PID(chip
, 0x5288)) {
1973 if (CHECK_BARO_PKG(chip
, QFN
)) {
1974 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL1
, 0xFF,
1976 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL2
, 0xFF,
1978 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL3
, 0xFF,
1980 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL4
, 0xFF,
1985 retval
= rtsx_send_cmd(chip
, SD_CARD
, 100);
1987 TRACE_RET(chip
, STATUS_FAIL
);
1989 return STATUS_SUCCESS
;
1992 static int sd_init_power(struct rtsx_chip
*chip
)
1996 retval
= sd_power_off_card3v3(chip
);
1997 if (retval
!= STATUS_SUCCESS
)
1998 TRACE_RET(chip
, STATUS_FAIL
);
2000 if (!chip
->ft2_fast_mode
)
2003 retval
= enable_card_clock(chip
, SD_CARD
);
2004 if (retval
!= STATUS_SUCCESS
)
2005 TRACE_RET(chip
, STATUS_FAIL
);
2007 if (chip
->asic_code
) {
2008 retval
= sd_pull_ctl_enable(chip
);
2009 if (retval
!= STATUS_SUCCESS
)
2010 TRACE_RET(chip
, STATUS_FAIL
);
2012 RTSX_WRITE_REG(chip
, FPGA_PULL_CTL
, FPGA_SD_PULL_CTL_BIT
| 0x20,
2016 if (!chip
->ft2_fast_mode
) {
2017 retval
= card_power_on(chip
, SD_CARD
);
2018 if (retval
!= STATUS_SUCCESS
)
2019 TRACE_RET(chip
, STATUS_FAIL
);
2024 if (chip
->ocp_stat
& (SD_OC_NOW
| SD_OC_EVER
)) {
2025 RTSX_DEBUGP("Over current, OCPSTAT is 0x%x\n",
2027 TRACE_RET(chip
, STATUS_FAIL
);
2032 RTSX_WRITE_REG(chip
, CARD_OE
, SD_OUTPUT_EN
, SD_OUTPUT_EN
);
2034 return STATUS_SUCCESS
;
2037 static int sd_dummy_clock(struct rtsx_chip
*chip
)
2039 RTSX_WRITE_REG(chip
, REG_SD_CFG3
, 0x01, 0x01);
2041 RTSX_WRITE_REG(chip
, REG_SD_CFG3
, 0x01, 0);
2043 return STATUS_SUCCESS
;
2046 static int sd_read_lba0(struct rtsx_chip
*chip
)
2048 struct sd_info
*sd_card
= &(chip
->sd_card
);
2050 u8 cmd
[5], bus_width
;
2052 cmd
[0] = 0x40 | READ_SINGLE_BLOCK
;
2058 if (CHK_SD(sd_card
)) {
2059 bus_width
= SD_BUS_WIDTH_4
;
2061 if (CHK_MMC_8BIT(sd_card
))
2062 bus_width
= SD_BUS_WIDTH_8
;
2063 else if (CHK_MMC_4BIT(sd_card
))
2064 bus_width
= SD_BUS_WIDTH_4
;
2066 bus_width
= SD_BUS_WIDTH_1
;
2069 retval
= sd_read_data(chip
, SD_TM_NORMAL_READ
, cmd
,
2070 5, 512, 1, bus_width
, NULL
, 0, 100);
2071 if (retval
!= STATUS_SUCCESS
) {
2072 rtsx_clear_sd_error(chip
);
2073 TRACE_RET(chip
, STATUS_FAIL
);
2076 return STATUS_SUCCESS
;
2079 static int sd_check_wp_state(struct rtsx_chip
*chip
)
2081 struct sd_info
*sd_card
= &(chip
->sd_card
);
2087 retval
= sd_send_cmd_get_rsp(chip
, APP_CMD
,
2088 sd_card
->sd_addr
, SD_RSP_TYPE_R1
, NULL
, 0);
2089 if (retval
!= STATUS_SUCCESS
)
2090 TRACE_RET(chip
, STATUS_FAIL
);
2092 cmd
[0] = 0x40 | SD_STATUS
;
2098 retval
= sd_read_data(chip
, SD_TM_NORMAL_READ
, cmd
, 5, 64, 1,
2099 SD_BUS_WIDTH_4
, buf
, 64, 250);
2100 if (retval
!= STATUS_SUCCESS
) {
2101 rtsx_clear_sd_error(chip
);
2103 sd_send_cmd_get_rsp(chip
, SEND_STATUS
, sd_card
->sd_addr
,
2104 SD_RSP_TYPE_R1
, NULL
, 0);
2105 TRACE_RET(chip
, STATUS_FAIL
);
2108 RTSX_DEBUGP("ACMD13:\n");
2111 sd_card_type
= ((u16
)buf
[2] << 8) | buf
[3];
2112 RTSX_DEBUGP("sd_card_type = 0x%04x\n", sd_card_type
);
2113 if ((sd_card_type
== 0x0001) || (sd_card_type
== 0x0002)) {
2114 /* ROM card or OTP */
2115 chip
->card_wp
|= SD_CARD
;
2118 /* Check SD Machanical Write-Protect Switch */
2119 val
= rtsx_readl(chip
, RTSX_BIPR
);
2120 if (val
& SD_WRITE_PROTECT
)
2121 chip
->card_wp
|= SD_CARD
;
2123 return STATUS_SUCCESS
;
2126 static int reset_sd(struct rtsx_chip
*chip
)
2128 struct sd_info
*sd_card
= &(chip
->sd_card
);
2129 int retval
, i
= 0, j
= 0, k
= 0, hi_cap_flow
= 0;
2130 int sd_dont_switch
= 0;
2131 int support_1v8
= 0;
2134 u8 switch_bus_width
;
2147 #ifdef SUPPORT_SD_LOCK
2148 if (sd_card
->sd_lock_status
& SD_UNLOCK_POW_ON
)
2149 goto SD_UNLOCK_ENTRY
;
2152 retval
= sd_prepare_reset(chip
);
2153 if (retval
!= STATUS_SUCCESS
)
2154 TRACE_RET(chip
, STATUS_FAIL
);
2156 retval
= sd_dummy_clock(chip
);
2157 if (retval
!= STATUS_SUCCESS
)
2158 TRACE_RET(chip
, STATUS_FAIL
);
2160 if (CHK_SDIO_EXIST(chip
) && !CHK_SDIO_IGNORED(chip
) && try_sdio
) {
2163 for (; rty_cnt
< chip
->sdio_retry_cnt
; rty_cnt
++) {
2164 if (detect_card_cd(chip
, SD_CARD
) != STATUS_SUCCESS
) {
2165 sd_set_err_code(chip
, SD_NO_CARD
);
2166 TRACE_RET(chip
, STATUS_FAIL
);
2169 retval
= sd_send_cmd_get_rsp(chip
, IO_SEND_OP_COND
, 0,
2170 SD_RSP_TYPE_R4
, rsp
, 5);
2171 if (retval
== STATUS_SUCCESS
) {
2172 int func_num
= (rsp
[1] >> 4) & 0x07;
2174 RTSX_DEBUGP("SD_IO card (Function number: %d)!\n", func_num
);
2176 TRACE_RET(chip
, STATUS_FAIL
);
2182 sd_init_power(chip
);
2184 sd_dummy_clock(chip
);
2187 RTSX_DEBUGP("Normal card!\n");
2190 /* Start Initialization Process of SD Card */
2192 retval
= sd_send_cmd_get_rsp(chip
, GO_IDLE_STATE
, 0, SD_RSP_TYPE_R0
,
2194 if (retval
!= STATUS_SUCCESS
)
2195 TRACE_RET(chip
, STATUS_FAIL
);
2199 retval
= sd_send_cmd_get_rsp(chip
, SEND_IF_COND
, 0x000001AA,
2200 SD_RSP_TYPE_R7
, rsp
, 5);
2201 if (retval
== STATUS_SUCCESS
) {
2202 if ((rsp
[4] == 0xAA) && ((rsp
[3] & 0x0f) == 0x01)) {
2204 voltage
= SUPPORT_VOLTAGE
| 0x40000000;
2209 voltage
= SUPPORT_VOLTAGE
;
2211 retval
= sd_send_cmd_get_rsp(chip
, GO_IDLE_STATE
, 0,
2212 SD_RSP_TYPE_R0
, NULL
, 0);
2213 if (retval
!= STATUS_SUCCESS
)
2214 TRACE_RET(chip
, STATUS_FAIL
);
2220 retval
= sd_send_cmd_get_rsp(chip
, APP_CMD
, 0, SD_RSP_TYPE_R1
,
2222 if (retval
!= STATUS_SUCCESS
) {
2223 if (detect_card_cd(chip
, SD_CARD
) != STATUS_SUCCESS
) {
2224 sd_set_err_code(chip
, SD_NO_CARD
);
2225 TRACE_RET(chip
, STATUS_FAIL
);
2232 TRACE_RET(chip
, STATUS_FAIL
);
2235 retval
= sd_send_cmd_get_rsp(chip
, SD_APP_OP_COND
, voltage
,
2236 SD_RSP_TYPE_R3
, rsp
, 5);
2237 if (retval
!= STATUS_SUCCESS
) {
2242 TRACE_RET(chip
, STATUS_FAIL
);
2247 } while (!(rsp
[1] & 0x80) && (i
< 255));
2250 TRACE_RET(chip
, STATUS_FAIL
);
2254 SET_SD_HCXC(sd_card
);
2256 CLR_SD_HCXC(sd_card
);
2260 CLR_SD_HCXC(sd_card
);
2263 RTSX_DEBUGP("support_1v8 = %d\n", support_1v8
);
2266 retval
= sd_voltage_switch(chip
);
2267 if (retval
!= STATUS_SUCCESS
)
2268 TRACE_RET(chip
, STATUS_FAIL
);
2271 retval
= sd_send_cmd_get_rsp(chip
, ALL_SEND_CID
, 0, SD_RSP_TYPE_R2
,
2273 if (retval
!= STATUS_SUCCESS
)
2274 TRACE_RET(chip
, STATUS_FAIL
);
2276 for (i
= 0; i
< 3; i
++) {
2277 retval
= sd_send_cmd_get_rsp(chip
, SEND_RELATIVE_ADDR
, 0,
2278 SD_RSP_TYPE_R6
, rsp
, 5);
2279 if (retval
!= STATUS_SUCCESS
)
2280 TRACE_RET(chip
, STATUS_FAIL
);
2282 sd_card
->sd_addr
= (u32
)rsp
[1] << 24;
2283 sd_card
->sd_addr
+= (u32
)rsp
[2] << 16;
2285 if (sd_card
->sd_addr
)
2289 retval
= sd_check_csd(chip
, 1);
2290 if (retval
!= STATUS_SUCCESS
)
2291 TRACE_RET(chip
, STATUS_FAIL
);
2293 retval
= sd_select_card(chip
, 1);
2294 if (retval
!= STATUS_SUCCESS
)
2295 TRACE_RET(chip
, STATUS_FAIL
);
2297 #ifdef SUPPORT_SD_LOCK
2299 retval
= sd_update_lock_status(chip
);
2300 if (retval
!= STATUS_SUCCESS
)
2301 TRACE_RET(chip
, STATUS_FAIL
);
2303 if (sd_card
->sd_lock_status
& SD_LOCKED
) {
2304 sd_card
->sd_lock_status
|= (SD_LOCK_1BIT_MODE
| SD_PWD_EXIST
);
2305 return STATUS_SUCCESS
;
2306 } else if (!(sd_card
->sd_lock_status
& SD_UNLOCK_POW_ON
)) {
2307 sd_card
->sd_lock_status
&= ~SD_PWD_EXIST
;
2311 retval
= sd_send_cmd_get_rsp(chip
, APP_CMD
, sd_card
->sd_addr
,
2312 SD_RSP_TYPE_R1
, NULL
, 0);
2313 if (retval
!= STATUS_SUCCESS
)
2314 TRACE_RET(chip
, STATUS_FAIL
);
2316 retval
= sd_send_cmd_get_rsp(chip
, SET_CLR_CARD_DETECT
, 0,
2317 SD_RSP_TYPE_R1
, NULL
, 0);
2318 if (retval
!= STATUS_SUCCESS
)
2319 TRACE_RET(chip
, STATUS_FAIL
);
2322 retval
= sd_send_cmd_get_rsp(chip
, APP_CMD
, sd_card
->sd_addr
,
2323 SD_RSP_TYPE_R1
, NULL
, 0);
2324 if (retval
!= STATUS_SUCCESS
)
2325 TRACE_RET(chip
, STATUS_FAIL
);
2327 retval
= sd_send_cmd_get_rsp(chip
, SET_BUS_WIDTH
, 2,
2328 SD_RSP_TYPE_R1
, NULL
, 0);
2329 if (retval
!= STATUS_SUCCESS
)
2330 TRACE_RET(chip
, STATUS_FAIL
);
2332 switch_bus_width
= SD_BUS_WIDTH_4
;
2334 switch_bus_width
= SD_BUS_WIDTH_1
;
2337 retval
= sd_send_cmd_get_rsp(chip
, SET_BLOCKLEN
, 0x200, SD_RSP_TYPE_R1
,
2339 if (retval
!= STATUS_SUCCESS
)
2340 TRACE_RET(chip
, STATUS_FAIL
);
2342 retval
= sd_set_clock_divider(chip
, SD_CLK_DIVIDE_0
);
2343 if (retval
!= STATUS_SUCCESS
)
2344 TRACE_RET(chip
, STATUS_FAIL
);
2346 if (!(sd_card
->raw_csd
[4] & 0x40))
2349 if (!sd_dont_switch
) {
2351 /* Set sd_switch_fail here, because we needn't
2352 * switch to UHS mode
2354 sd_card
->sd_switch_fail
= SDR104_SUPPORT_MASK
|
2355 DDR50_SUPPORT_MASK
| SDR50_SUPPORT_MASK
;
2358 /* Check the card whether follow SD1.1 spec or higher */
2359 retval
= sd_check_spec(chip
, switch_bus_width
);
2360 if (retval
== STATUS_SUCCESS
) {
2361 retval
= sd_switch_function(chip
, switch_bus_width
);
2362 if (retval
!= STATUS_SUCCESS
) {
2363 sd_init_power(chip
);
2371 sd_init_power(chip
);
2381 retval
= sd_send_cmd_get_rsp(chip
, APP_CMD
, sd_card
->sd_addr
,
2382 SD_RSP_TYPE_R1
, NULL
, 0);
2383 if (retval
!= STATUS_SUCCESS
)
2384 TRACE_RET(chip
, STATUS_FAIL
);
2386 retval
= sd_send_cmd_get_rsp(chip
, SET_BUS_WIDTH
, 2,
2387 SD_RSP_TYPE_R1
, NULL
, 0);
2388 if (retval
!= STATUS_SUCCESS
)
2389 TRACE_RET(chip
, STATUS_FAIL
);
2392 #ifdef SUPPORT_SD_LOCK
2393 sd_card
->sd_lock_status
&= ~SD_LOCK_1BIT_MODE
;
2396 if (!sd20_mode
&& CHK_SD30_SPEED(sd_card
)) {
2399 RTSX_WRITE_REG(chip
, SD30_DRIVE_SEL
, 0x07,
2400 chip
->sd30_drive_sel_1v8
);
2402 retval
= sd_set_init_para(chip
);
2403 if (retval
!= STATUS_SUCCESS
)
2404 TRACE_RET(chip
, STATUS_FAIL
);
2406 if (CHK_SD_DDR50(sd_card
))
2407 retval
= sd_ddr_tuning(chip
);
2409 retval
= sd_sdr_tuning(chip
);
2411 if (retval
!= STATUS_SUCCESS
) {
2413 TRACE_RET(chip
, STATUS_FAIL
);
2415 retval
= sd_init_power(chip
);
2416 if (retval
!= STATUS_SUCCESS
)
2417 TRACE_RET(chip
, STATUS_FAIL
);
2425 sd_send_cmd_get_rsp(chip
, SEND_STATUS
, sd_card
->sd_addr
,
2426 SD_RSP_TYPE_R1
, NULL
, 0);
2428 if (CHK_SD_DDR50(sd_card
)) {
2429 retval
= sd_wait_state_data_ready(chip
, 0x08, 1, 1000);
2430 if (retval
!= STATUS_SUCCESS
)
2435 retval
= sd_read_lba0(chip
);
2436 if (retval
!= STATUS_SUCCESS
) {
2438 TRACE_RET(chip
, STATUS_FAIL
);
2440 retval
= sd_init_power(chip
);
2441 if (retval
!= STATUS_SUCCESS
)
2442 TRACE_RET(chip
, STATUS_FAIL
);
2452 retval
= sd_check_wp_state(chip
);
2453 if (retval
!= STATUS_SUCCESS
)
2454 TRACE_RET(chip
, STATUS_FAIL
);
2456 chip
->card_bus_width
[chip
->card2lun
[SD_CARD
]] = 4;
2458 #ifdef SUPPORT_SD_LOCK
2459 if (sd_card
->sd_lock_status
& SD_UNLOCK_POW_ON
) {
2460 RTSX_WRITE_REG(chip
, REG_SD_BLOCK_CNT_H
, 0xFF, 0x02);
2461 RTSX_WRITE_REG(chip
, REG_SD_BLOCK_CNT_L
, 0xFF, 0x00);
2465 return STATUS_SUCCESS
;
2469 static int mmc_test_switch_bus(struct rtsx_chip
*chip
, u8 width
)
2471 struct sd_info
*sd_card
= &(chip
->sd_card
);
2473 u8 buf
[8] = {0}, bus_width
, *ptr
;
2477 retval
= sd_send_cmd_get_rsp(chip
, BUSTEST_W
, 0, SD_RSP_TYPE_R1
, NULL
,
2479 if (retval
!= STATUS_SUCCESS
)
2480 TRACE_RET(chip
, SWITCH_FAIL
);
2482 if (width
== MMC_8BIT_BUS
) {
2487 bus_width
= SD_BUS_WIDTH_8
;
2492 bus_width
= SD_BUS_WIDTH_4
;
2495 retval
= rtsx_write_register(chip
, REG_SD_CFG3
, 0x02, 0x02);
2496 if (retval
!= STATUS_SUCCESS
)
2497 TRACE_RET(chip
, SWITCH_ERR
);
2499 retval
= sd_write_data(chip
, SD_TM_AUTO_WRITE_3
,
2500 NULL
, 0, byte_cnt
, 1, bus_width
, buf
, len
, 100);
2501 if (retval
!= STATUS_SUCCESS
) {
2502 rtsx_clear_sd_error(chip
);
2503 rtsx_write_register(chip
, REG_SD_CFG3
, 0x02, 0);
2504 TRACE_RET(chip
, SWITCH_ERR
);
2507 retval
= rtsx_write_register(chip
, REG_SD_CFG3
, 0x02, 0);
2508 if (retval
!= STATUS_SUCCESS
)
2509 TRACE_RET(chip
, SWITCH_ERR
);
2511 RTSX_DEBUGP("SD/MMC CMD %d\n", BUSTEST_R
);
2513 rtsx_init_cmd(chip
);
2515 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD0
, 0xFF, 0x40 | BUSTEST_R
);
2517 if (width
== MMC_8BIT_BUS
)
2518 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BYTE_CNT_L
,
2521 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BYTE_CNT_L
,
2524 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BLOCK_CNT_L
, 0xFF, 1);
2525 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BLOCK_CNT_H
, 0xFF, 0);
2527 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CFG2
, 0xFF,
2528 SD_CALCULATE_CRC7
| SD_NO_CHECK_CRC16
| SD_NO_WAIT_BUSY_END
|
2529 SD_CHECK_CRC7
| SD_RSP_LEN_6
);
2530 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
, 0x01,
2532 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_TRANSFER
, 0xFF,
2533 SD_TM_NORMAL_READ
| SD_TRANSFER_START
);
2534 rtsx_add_cmd(chip
, CHECK_REG_CMD
, REG_SD_TRANSFER
, SD_TRANSFER_END
,
2537 rtsx_add_cmd(chip
, READ_REG_CMD
, PPBUF_BASE2
, 0, 0);
2538 if (width
== MMC_8BIT_BUS
)
2539 rtsx_add_cmd(chip
, READ_REG_CMD
, PPBUF_BASE2
+ 1, 0, 0);
2541 retval
= rtsx_send_cmd(chip
, SD_CARD
, 100);
2543 rtsx_clear_sd_error(chip
);
2544 TRACE_RET(chip
, SWITCH_ERR
);
2547 ptr
= rtsx_get_cmd_data(chip
) + 1;
2549 if (width
== MMC_8BIT_BUS
) {
2550 RTSX_DEBUGP("BUSTEST_R [8bits]: 0x%02x 0x%02x\n", ptr
[0],
2552 if ((ptr
[0] == 0xAA) && (ptr
[1] == 0x55)) {
2556 if (CHK_MMC_DDR52(sd_card
))
2561 retval
= sd_send_cmd_get_rsp(chip
, SWITCH
, arg
,
2562 SD_RSP_TYPE_R1b
, rsp
, 5);
2563 if ((retval
== STATUS_SUCCESS
) &&
2564 !(rsp
[4] & MMC_SWITCH_ERR
))
2565 return SWITCH_SUCCESS
;
2568 RTSX_DEBUGP("BUSTEST_R [4bits]: 0x%02x\n", ptr
[0]);
2569 if (ptr
[0] == 0xA5) {
2573 if (CHK_MMC_DDR52(sd_card
))
2578 retval
= sd_send_cmd_get_rsp(chip
, SWITCH
, arg
,
2579 SD_RSP_TYPE_R1b
, rsp
, 5);
2580 if ((retval
== STATUS_SUCCESS
) &&
2581 !(rsp
[4] & MMC_SWITCH_ERR
))
2582 return SWITCH_SUCCESS
;
2586 TRACE_RET(chip
, SWITCH_FAIL
);
2590 static int mmc_switch_timing_bus(struct rtsx_chip
*chip
, int switch_ddr
)
2592 struct sd_info
*sd_card
= &(chip
->sd_card
);
2594 u8
*ptr
, card_type
, card_type_mask
= 0;
2596 CLR_MMC_HS(sd_card
);
2598 RTSX_DEBUGP("SD/MMC CMD %d\n", SEND_EXT_CSD
);
2600 rtsx_init_cmd(chip
);
2602 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD0
, 0xFF,
2603 0x40 | SEND_EXT_CSD
);
2604 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD1
, 0xFF, 0);
2605 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD2
, 0xFF, 0);
2606 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD3
, 0xFF, 0);
2607 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD4
, 0xFF, 0);
2609 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BYTE_CNT_L
, 0xFF, 0);
2610 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BYTE_CNT_H
, 0xFF, 2);
2611 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BLOCK_CNT_L
, 0xFF, 1);
2612 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BLOCK_CNT_H
, 0xFF, 0);
2614 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CFG2
, 0xFF,
2615 SD_CALCULATE_CRC7
| SD_CHECK_CRC16
| SD_NO_WAIT_BUSY_END
|
2616 SD_CHECK_CRC7
| SD_RSP_LEN_6
);
2617 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
, 0x01,
2619 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_TRANSFER
, 0xFF,
2620 SD_TM_NORMAL_READ
| SD_TRANSFER_START
);
2621 rtsx_add_cmd(chip
, CHECK_REG_CMD
, REG_SD_TRANSFER
, SD_TRANSFER_END
,
2624 rtsx_add_cmd(chip
, READ_REG_CMD
, PPBUF_BASE2
+ 196, 0xFF, 0);
2625 rtsx_add_cmd(chip
, READ_REG_CMD
, PPBUF_BASE2
+ 212, 0xFF, 0);
2626 rtsx_add_cmd(chip
, READ_REG_CMD
, PPBUF_BASE2
+ 213, 0xFF, 0);
2627 rtsx_add_cmd(chip
, READ_REG_CMD
, PPBUF_BASE2
+ 214, 0xFF, 0);
2628 rtsx_add_cmd(chip
, READ_REG_CMD
, PPBUF_BASE2
+ 215, 0xFF, 0);
2630 retval
= rtsx_send_cmd(chip
, SD_CARD
, 1000);
2632 if (retval
== -ETIMEDOUT
) {
2633 rtsx_clear_sd_error(chip
);
2634 sd_send_cmd_get_rsp(chip
, SEND_STATUS
, sd_card
->sd_addr
,
2635 SD_RSP_TYPE_R1
, NULL
, 0);
2637 TRACE_RET(chip
, STATUS_FAIL
);
2640 ptr
= rtsx_get_cmd_data(chip
);
2641 if (ptr
[0] & SD_TRANSFER_ERR
) {
2642 sd_send_cmd_get_rsp(chip
, SEND_STATUS
, sd_card
->sd_addr
,
2643 SD_RSP_TYPE_R1
, NULL
, 0);
2644 TRACE_RET(chip
, STATUS_FAIL
);
2647 if (CHK_MMC_SECTOR_MODE(sd_card
)) {
2648 sd_card
->capacity
= ((u32
)ptr
[5] << 24) | ((u32
)ptr
[4] << 16) |
2649 ((u32
)ptr
[3] << 8) | ((u32
)ptr
[2]);
2652 card_type_mask
= 0x03;
2653 card_type
= ptr
[1] & card_type_mask
;
2657 if (card_type
& 0x04) {
2659 SET_MMC_DDR52(sd_card
);
2661 SET_MMC_52M(sd_card
);
2662 } else if (card_type
& 0x02) {
2663 SET_MMC_52M(sd_card
);
2665 SET_MMC_26M(sd_card
);
2668 retval
= sd_send_cmd_get_rsp(chip
, SWITCH
,
2669 0x03B90100, SD_RSP_TYPE_R1b
, rsp
, 5);
2670 if ((retval
!= STATUS_SUCCESS
) || (rsp
[4] & MMC_SWITCH_ERR
))
2671 CLR_MMC_HS(sd_card
);
2674 sd_choose_proper_clock(chip
);
2675 retval
= switch_clock(chip
, sd_card
->sd_clock
);
2676 if (retval
!= STATUS_SUCCESS
)
2677 TRACE_RET(chip
, STATUS_FAIL
);
2679 /* Test Bus Procedure */
2680 retval
= mmc_test_switch_bus(chip
, MMC_8BIT_BUS
);
2681 if (retval
== SWITCH_SUCCESS
) {
2682 SET_MMC_8BIT(sd_card
);
2683 chip
->card_bus_width
[chip
->card2lun
[SD_CARD
]] = 8;
2684 #ifdef SUPPORT_SD_LOCK
2685 sd_card
->sd_lock_status
&= ~SD_LOCK_1BIT_MODE
;
2687 } else if (retval
== SWITCH_FAIL
) {
2688 retval
= mmc_test_switch_bus(chip
, MMC_4BIT_BUS
);
2689 if (retval
== SWITCH_SUCCESS
) {
2690 SET_MMC_4BIT(sd_card
);
2691 chip
->card_bus_width
[chip
->card2lun
[SD_CARD
]] = 4;
2692 #ifdef SUPPORT_SD_LOCK
2693 sd_card
->sd_lock_status
&= ~SD_LOCK_1BIT_MODE
;
2695 } else if (retval
== SWITCH_FAIL
) {
2696 CLR_MMC_8BIT(sd_card
);
2697 CLR_MMC_4BIT(sd_card
);
2699 TRACE_RET(chip
, STATUS_FAIL
);
2702 TRACE_RET(chip
, STATUS_FAIL
);
2705 return STATUS_SUCCESS
;
2709 static int reset_mmc(struct rtsx_chip
*chip
)
2711 struct sd_info
*sd_card
= &(chip
->sd_card
);
2712 int retval
, i
= 0, j
= 0, k
= 0;
2718 #ifdef SUPPORT_SD_LOCK
2719 if (sd_card
->sd_lock_status
& SD_UNLOCK_POW_ON
)
2720 goto MMC_UNLOCK_ENTRY
;
2724 retval
= sd_prepare_reset(chip
);
2725 if (retval
!= STATUS_SUCCESS
)
2726 TRACE_RET(chip
, retval
);
2731 retval
= sd_send_cmd_get_rsp(chip
, GO_IDLE_STATE
, 0, SD_RSP_TYPE_R0
,
2733 if (retval
!= STATUS_SUCCESS
)
2734 TRACE_RET(chip
, STATUS_FAIL
);
2737 if (detect_card_cd(chip
, SD_CARD
) != STATUS_SUCCESS
) {
2738 sd_set_err_code(chip
, SD_NO_CARD
);
2739 TRACE_RET(chip
, STATUS_FAIL
);
2742 retval
= sd_send_cmd_get_rsp(chip
, SEND_OP_COND
,
2743 (SUPPORT_VOLTAGE
| 0x40000000),
2744 SD_RSP_TYPE_R3
, rsp
, 5);
2745 if (retval
!= STATUS_SUCCESS
) {
2746 if (sd_check_err_code(chip
, SD_BUSY
) ||
2747 sd_check_err_code(chip
, SD_TO_ERR
)) {
2750 sd_clr_err_code(chip
);
2753 TRACE_RET(chip
, STATUS_FAIL
);
2758 sd_clr_err_code(chip
);
2761 TRACE_RET(chip
, STATUS_FAIL
);
2768 } while (!(rsp
[1] & 0x80) && (i
< 255));
2771 TRACE_RET(chip
, STATUS_FAIL
);
2773 if ((rsp
[1] & 0x60) == 0x40)
2774 SET_MMC_SECTOR_MODE(sd_card
);
2776 CLR_MMC_SECTOR_MODE(sd_card
);
2778 retval
= sd_send_cmd_get_rsp(chip
, ALL_SEND_CID
, 0, SD_RSP_TYPE_R2
,
2780 if (retval
!= STATUS_SUCCESS
)
2781 TRACE_RET(chip
, STATUS_FAIL
);
2783 sd_card
->sd_addr
= 0x00100000;
2784 retval
= sd_send_cmd_get_rsp(chip
, SET_RELATIVE_ADDR
, sd_card
->sd_addr
,
2785 SD_RSP_TYPE_R6
, rsp
, 5);
2786 if (retval
!= STATUS_SUCCESS
)
2787 TRACE_RET(chip
, STATUS_FAIL
);
2789 retval
= sd_check_csd(chip
, 1);
2790 if (retval
!= STATUS_SUCCESS
)
2791 TRACE_RET(chip
, STATUS_FAIL
);
2793 spec_ver
= (sd_card
->raw_csd
[0] & 0x3C) >> 2;
2795 retval
= sd_select_card(chip
, 1);
2796 if (retval
!= STATUS_SUCCESS
)
2797 TRACE_RET(chip
, STATUS_FAIL
);
2799 retval
= sd_send_cmd_get_rsp(chip
, SET_BLOCKLEN
, 0x200, SD_RSP_TYPE_R1
,
2801 if (retval
!= STATUS_SUCCESS
)
2802 TRACE_RET(chip
, STATUS_FAIL
);
2804 #ifdef SUPPORT_SD_LOCK
2806 retval
= sd_update_lock_status(chip
);
2807 if (retval
!= STATUS_SUCCESS
)
2808 TRACE_RET(chip
, STATUS_FAIL
);
2811 retval
= sd_set_clock_divider(chip
, SD_CLK_DIVIDE_0
);
2812 if (retval
!= STATUS_SUCCESS
)
2813 TRACE_RET(chip
, STATUS_FAIL
);
2815 chip
->card_bus_width
[chip
->card2lun
[SD_CARD
]] = 1;
2817 if (!sd_card
->mmc_dont_switch_bus
) {
2818 if (spec_ver
== 4) {
2820 retval
= mmc_switch_timing_bus(chip
, switch_ddr
);
2821 if (retval
!= STATUS_SUCCESS
) {
2822 retval
= sd_init_power(chip
);
2823 if (retval
!= STATUS_SUCCESS
)
2824 TRACE_RET(chip
, STATUS_FAIL
);
2825 sd_card
->mmc_dont_switch_bus
= 1;
2826 TRACE_GOTO(chip
, Switch_Fail
);
2830 if (CHK_MMC_SECTOR_MODE(sd_card
) && (sd_card
->capacity
== 0))
2831 TRACE_RET(chip
, STATUS_FAIL
);
2833 if (switch_ddr
&& CHK_MMC_DDR52(sd_card
)) {
2834 retval
= sd_set_init_para(chip
);
2835 if (retval
!= STATUS_SUCCESS
)
2836 TRACE_RET(chip
, STATUS_FAIL
);
2838 retval
= mmc_ddr_tuning(chip
);
2839 if (retval
!= STATUS_SUCCESS
) {
2840 retval
= sd_init_power(chip
);
2841 if (retval
!= STATUS_SUCCESS
)
2842 TRACE_RET(chip
, STATUS_FAIL
);
2845 TRACE_GOTO(chip
, Switch_Fail
);
2848 retval
= sd_wait_state_data_ready(chip
, 0x08, 1, 1000);
2849 if (retval
== STATUS_SUCCESS
) {
2850 retval
= sd_read_lba0(chip
);
2851 if (retval
!= STATUS_SUCCESS
) {
2852 retval
= sd_init_power(chip
);
2853 if (retval
!= STATUS_SUCCESS
)
2854 TRACE_RET(chip
, STATUS_FAIL
);
2857 TRACE_GOTO(chip
, Switch_Fail
);
2863 #ifdef SUPPORT_SD_LOCK
2864 if (sd_card
->sd_lock_status
& SD_UNLOCK_POW_ON
) {
2865 RTSX_WRITE_REG(chip
, REG_SD_BLOCK_CNT_H
, 0xFF, 0x02);
2866 RTSX_WRITE_REG(chip
, REG_SD_BLOCK_CNT_L
, 0xFF, 0x00);
2870 temp
= rtsx_readl(chip
, RTSX_BIPR
);
2871 if (temp
& SD_WRITE_PROTECT
)
2872 chip
->card_wp
|= SD_CARD
;
2874 return STATUS_SUCCESS
;
2877 int reset_sd_card(struct rtsx_chip
*chip
)
2879 struct sd_info
*sd_card
= &(chip
->sd_card
);
2882 sd_init_reg_addr(chip
);
2884 memset(sd_card
, 0, sizeof(struct sd_info
));
2885 chip
->capacity
[chip
->card2lun
[SD_CARD
]] = 0;
2887 retval
= enable_card_clock(chip
, SD_CARD
);
2888 if (retval
!= STATUS_SUCCESS
)
2889 TRACE_RET(chip
, STATUS_FAIL
);
2891 if (chip
->ignore_sd
&& CHK_SDIO_EXIST(chip
) &&
2892 !CHK_SDIO_IGNORED(chip
)) {
2893 if (chip
->asic_code
) {
2894 retval
= sd_pull_ctl_enable(chip
);
2895 if (retval
!= STATUS_SUCCESS
)
2896 TRACE_RET(chip
, STATUS_FAIL
);
2898 retval
= rtsx_write_register(chip
, FPGA_PULL_CTL
,
2899 FPGA_SD_PULL_CTL_BIT
| 0x20, 0);
2900 if (retval
!= STATUS_SUCCESS
)
2901 TRACE_RET(chip
, STATUS_FAIL
);
2903 retval
= card_share_mode(chip
, SD_CARD
);
2904 if (retval
!= STATUS_SUCCESS
)
2905 TRACE_RET(chip
, STATUS_FAIL
);
2908 TRACE_RET(chip
, STATUS_FAIL
);
2911 retval
= sd_init_power(chip
);
2912 if (retval
!= STATUS_SUCCESS
)
2913 TRACE_RET(chip
, STATUS_FAIL
);
2915 if (chip
->sd_ctl
& RESET_MMC_FIRST
) {
2916 retval
= reset_mmc(chip
);
2917 if (retval
!= STATUS_SUCCESS
) {
2918 if (sd_check_err_code(chip
, SD_NO_CARD
))
2919 TRACE_RET(chip
, STATUS_FAIL
);
2921 retval
= reset_sd(chip
);
2922 if (retval
!= STATUS_SUCCESS
)
2923 TRACE_RET(chip
, STATUS_FAIL
);
2926 retval
= reset_sd(chip
);
2927 if (retval
!= STATUS_SUCCESS
) {
2928 if (sd_check_err_code(chip
, SD_NO_CARD
))
2929 TRACE_RET(chip
, STATUS_FAIL
);
2932 TRACE_RET(chip
, STATUS_FAIL
);
2934 retval
= reset_mmc(chip
);
2935 if (retval
!= STATUS_SUCCESS
)
2936 TRACE_RET(chip
, STATUS_FAIL
);
2941 retval
= sd_set_clock_divider(chip
, SD_CLK_DIVIDE_0
);
2942 if (retval
!= STATUS_SUCCESS
)
2943 TRACE_RET(chip
, STATUS_FAIL
);
2945 RTSX_WRITE_REG(chip
, REG_SD_BYTE_CNT_L
, 0xFF, 0);
2946 RTSX_WRITE_REG(chip
, REG_SD_BYTE_CNT_H
, 0xFF, 2);
2948 chip
->capacity
[chip
->card2lun
[SD_CARD
]] = sd_card
->capacity
;
2950 retval
= sd_set_init_para(chip
);
2951 if (retval
!= STATUS_SUCCESS
)
2952 TRACE_RET(chip
, STATUS_FAIL
);
2954 RTSX_DEBUGP("sd_card->sd_type = 0x%x\n", sd_card
->sd_type
);
2956 return STATUS_SUCCESS
;
2959 static int reset_mmc_only(struct rtsx_chip
*chip
)
2961 struct sd_info
*sd_card
= &(chip
->sd_card
);
2964 sd_card
->sd_type
= 0;
2965 sd_card
->seq_mode
= 0;
2966 sd_card
->sd_data_buf_ready
= 0;
2967 sd_card
->capacity
= 0;
2968 sd_card
->sd_switch_fail
= 0;
2970 #ifdef SUPPORT_SD_LOCK
2971 sd_card
->sd_lock_status
= 0;
2972 sd_card
->sd_erase_status
= 0;
2975 chip
->capacity
[chip
->card2lun
[SD_CARD
]] = sd_card
->capacity
= 0;
2977 retval
= enable_card_clock(chip
, SD_CARD
);
2978 if (retval
!= STATUS_SUCCESS
)
2979 TRACE_RET(chip
, STATUS_FAIL
);
2981 retval
= sd_init_power(chip
);
2982 if (retval
!= STATUS_SUCCESS
)
2983 TRACE_RET(chip
, STATUS_FAIL
);
2985 retval
= reset_mmc(chip
);
2986 if (retval
!= STATUS_SUCCESS
)
2987 TRACE_RET(chip
, STATUS_FAIL
);
2989 retval
= sd_set_clock_divider(chip
, SD_CLK_DIVIDE_0
);
2990 if (retval
!= STATUS_SUCCESS
)
2991 TRACE_RET(chip
, STATUS_FAIL
);
2993 RTSX_WRITE_REG(chip
, REG_SD_BYTE_CNT_L
, 0xFF, 0);
2994 RTSX_WRITE_REG(chip
, REG_SD_BYTE_CNT_H
, 0xFF, 2);
2996 chip
->capacity
[chip
->card2lun
[SD_CARD
]] = sd_card
->capacity
;
2998 retval
= sd_set_init_para(chip
);
2999 if (retval
!= STATUS_SUCCESS
)
3000 TRACE_RET(chip
, STATUS_FAIL
);
3002 RTSX_DEBUGP("In reset_mmc_only, sd_card->sd_type = 0x%x\n",
3005 return STATUS_SUCCESS
;
3008 #define WAIT_DATA_READY_RTY_CNT 255
3010 static int wait_data_buf_ready(struct rtsx_chip
*chip
)
3012 struct sd_info
*sd_card
= &(chip
->sd_card
);
3015 for (i
= 0; i
< WAIT_DATA_READY_RTY_CNT
; i
++) {
3016 if (detect_card_cd(chip
, SD_CARD
) != STATUS_SUCCESS
) {
3017 sd_set_err_code(chip
, SD_NO_CARD
);
3018 TRACE_RET(chip
, STATUS_FAIL
);
3021 sd_card
->sd_data_buf_ready
= 0;
3023 retval
= sd_send_cmd_get_rsp(chip
, SEND_STATUS
,
3024 sd_card
->sd_addr
, SD_RSP_TYPE_R1
, NULL
, 0);
3025 if (retval
!= STATUS_SUCCESS
)
3026 TRACE_RET(chip
, STATUS_FAIL
);
3028 if (sd_card
->sd_data_buf_ready
) {
3029 return sd_send_cmd_get_rsp(chip
, SEND_STATUS
,
3030 sd_card
->sd_addr
, SD_RSP_TYPE_R1
, NULL
, 0);
3034 sd_set_err_code(chip
, SD_TO_ERR
);
3036 TRACE_RET(chip
, STATUS_FAIL
);
3039 void sd_stop_seq_mode(struct rtsx_chip
*chip
)
3041 struct sd_info
*sd_card
= &(chip
->sd_card
);
3044 if (sd_card
->seq_mode
) {
3045 retval
= sd_switch_clock(chip
);
3046 if (retval
!= STATUS_SUCCESS
)
3049 retval
= sd_send_cmd_get_rsp(chip
, STOP_TRANSMISSION
, 0,
3050 SD_RSP_TYPE_R1b
, NULL
, 0);
3051 if (retval
!= STATUS_SUCCESS
)
3052 sd_set_err_code(chip
, SD_STS_ERR
);
3054 retval
= sd_wait_state_data_ready(chip
, 0x08, 1, 1000);
3055 if (retval
!= STATUS_SUCCESS
)
3056 sd_set_err_code(chip
, SD_STS_ERR
);
3058 sd_card
->seq_mode
= 0;
3060 rtsx_write_register(chip
, RBCTL
, RB_FLUSH
, RB_FLUSH
);
3064 static inline int sd_auto_tune_clock(struct rtsx_chip
*chip
)
3066 struct sd_info
*sd_card
= &(chip
->sd_card
);
3069 if (chip
->asic_code
) {
3070 if (sd_card
->sd_clock
> 30)
3071 sd_card
->sd_clock
-= 20;
3073 switch (sd_card
->sd_clock
) {
3075 sd_card
->sd_clock
= CLK_150
;
3079 sd_card
->sd_clock
= CLK_120
;
3083 sd_card
->sd_clock
= CLK_100
;
3087 sd_card
->sd_clock
= CLK_80
;
3091 sd_card
->sd_clock
= CLK_60
;
3095 sd_card
->sd_clock
= CLK_50
;
3103 retval
= sd_switch_clock(chip
);
3104 if (retval
!= STATUS_SUCCESS
)
3105 TRACE_RET(chip
, STATUS_FAIL
);
3107 return STATUS_SUCCESS
;
3110 int sd_rw(struct scsi_cmnd
*srb
, struct rtsx_chip
*chip
, u32 start_sector
,
3113 struct sd_info
*sd_card
= &(chip
->sd_card
);
3118 if (srb
->sc_data_direction
== DMA_FROM_DEVICE
) {
3119 RTSX_DEBUGP("sd_rw: Read %d %s from 0x%x\n", sector_cnt
,
3120 (sector_cnt
> 1) ? "sectors" : "sector", start_sector
);
3122 RTSX_DEBUGP("sd_rw: Write %d %s to 0x%x\n", sector_cnt
,
3123 (sector_cnt
> 1) ? "sectors" : "sector", start_sector
);
3126 sd_card
->cleanup_counter
= 0;
3128 if (!(chip
->card_ready
& SD_CARD
)) {
3129 sd_card
->seq_mode
= 0;
3131 retval
= reset_sd_card(chip
);
3132 if (retval
== STATUS_SUCCESS
) {
3133 chip
->card_ready
|= SD_CARD
;
3134 chip
->card_fail
&= ~SD_CARD
;
3136 chip
->card_ready
&= ~SD_CARD
;
3137 chip
->card_fail
|= SD_CARD
;
3138 chip
->capacity
[chip
->card2lun
[SD_CARD
]] = 0;
3139 chip
->rw_need_retry
= 1;
3140 TRACE_RET(chip
, STATUS_FAIL
);
3144 if (!CHK_SD_HCXC(sd_card
) && !CHK_MMC_SECTOR_MODE(sd_card
))
3145 data_addr
= start_sector
<< 9;
3147 data_addr
= start_sector
;
3149 sd_clr_err_code(chip
);
3151 retval
= sd_switch_clock(chip
);
3152 if (retval
!= STATUS_SUCCESS
) {
3153 sd_set_err_code(chip
, SD_IO_ERR
);
3154 TRACE_GOTO(chip
, RW_FAIL
);
3157 if (sd_card
->seq_mode
&&
3158 ((sd_card
->pre_dir
!= srb
->sc_data_direction
) ||
3159 ((sd_card
->pre_sec_addr
+ sd_card
->pre_sec_cnt
) !=
3161 if ((sd_card
->pre_sec_cnt
< 0x80)
3162 && (sd_card
->pre_dir
== DMA_FROM_DEVICE
)
3163 && !CHK_SD30_SPEED(sd_card
)
3164 && !CHK_SD_HS(sd_card
)
3165 && !CHK_MMC_HS(sd_card
)) {
3166 sd_send_cmd_get_rsp(chip
, SEND_STATUS
, sd_card
->sd_addr
,
3167 SD_RSP_TYPE_R1
, NULL
, 0);
3170 retval
= sd_send_cmd_get_rsp(chip
, STOP_TRANSMISSION
,
3171 0, SD_RSP_TYPE_R1b
, NULL
, 0);
3172 if (retval
!= STATUS_SUCCESS
) {
3173 chip
->rw_need_retry
= 1;
3174 sd_set_err_code(chip
, SD_STS_ERR
);
3175 TRACE_GOTO(chip
, RW_FAIL
);
3178 sd_card
->seq_mode
= 0;
3180 retval
= rtsx_write_register(chip
, RBCTL
, RB_FLUSH
, RB_FLUSH
);
3181 if (retval
!= STATUS_SUCCESS
) {
3182 sd_set_err_code(chip
, SD_IO_ERR
);
3183 TRACE_GOTO(chip
, RW_FAIL
);
3186 if ((sd_card
->pre_sec_cnt
< 0x80)
3187 && !CHK_SD30_SPEED(sd_card
)
3188 && !CHK_SD_HS(sd_card
)
3189 && !CHK_MMC_HS(sd_card
)) {
3190 sd_send_cmd_get_rsp(chip
, SEND_STATUS
, sd_card
->sd_addr
,
3191 SD_RSP_TYPE_R1
, NULL
, 0);
3195 rtsx_init_cmd(chip
);
3197 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BYTE_CNT_L
, 0xFF, 0x00);
3198 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BYTE_CNT_H
, 0xFF, 0x02);
3199 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BLOCK_CNT_L
, 0xFF,
3201 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BLOCK_CNT_H
, 0xFF,
3202 (u8
)(sector_cnt
>> 8));
3204 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
, 0x01, RING_BUFFER
);
3206 if (CHK_MMC_8BIT(sd_card
))
3207 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CFG1
,
3208 0x03, SD_BUS_WIDTH_8
);
3209 else if (CHK_MMC_4BIT(sd_card
) || CHK_SD(sd_card
))
3210 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CFG1
,
3211 0x03, SD_BUS_WIDTH_4
);
3213 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CFG1
,
3214 0x03, SD_BUS_WIDTH_1
);
3216 if (sd_card
->seq_mode
) {
3217 cfg2
= SD_NO_CALCULATE_CRC7
| SD_CHECK_CRC16
|
3218 SD_NO_WAIT_BUSY_END
| SD_NO_CHECK_CRC7
|
3220 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CFG2
, 0xFF, cfg2
);
3222 trans_dma_enable(srb
->sc_data_direction
, chip
, sector_cnt
* 512,
3225 if (srb
->sc_data_direction
== DMA_FROM_DEVICE
) {
3226 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_TRANSFER
, 0xFF,
3227 SD_TM_AUTO_READ_3
| SD_TRANSFER_START
);
3229 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_TRANSFER
, 0xFF,
3230 SD_TM_AUTO_WRITE_3
| SD_TRANSFER_START
);
3233 rtsx_add_cmd(chip
, CHECK_REG_CMD
, REG_SD_TRANSFER
,
3234 SD_TRANSFER_END
, SD_TRANSFER_END
);
3236 rtsx_send_cmd_no_wait(chip
);
3238 if (srb
->sc_data_direction
== DMA_FROM_DEVICE
) {
3239 RTSX_DEBUGP("SD/MMC CMD %d\n", READ_MULTIPLE_BLOCK
);
3240 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD0
, 0xFF,
3241 0x40 | READ_MULTIPLE_BLOCK
);
3242 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD1
, 0xFF,
3243 (u8
)(data_addr
>> 24));
3244 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD2
, 0xFF,
3245 (u8
)(data_addr
>> 16));
3246 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD3
, 0xFF,
3247 (u8
)(data_addr
>> 8));
3248 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD4
, 0xFF,
3251 cfg2
= SD_CALCULATE_CRC7
| SD_CHECK_CRC16
|
3252 SD_NO_WAIT_BUSY_END
| SD_CHECK_CRC7
|
3254 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CFG2
, 0xFF,
3257 trans_dma_enable(srb
->sc_data_direction
, chip
,
3258 sector_cnt
* 512, DMA_512
);
3260 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_TRANSFER
, 0xFF,
3261 SD_TM_AUTO_READ_2
| SD_TRANSFER_START
);
3262 rtsx_add_cmd(chip
, CHECK_REG_CMD
, REG_SD_TRANSFER
,
3263 SD_TRANSFER_END
, SD_TRANSFER_END
);
3265 rtsx_send_cmd_no_wait(chip
);
3267 retval
= rtsx_send_cmd(chip
, SD_CARD
, 50);
3269 rtsx_clear_sd_error(chip
);
3271 chip
->rw_need_retry
= 1;
3272 sd_set_err_code(chip
, SD_TO_ERR
);
3273 TRACE_GOTO(chip
, RW_FAIL
);
3276 retval
= wait_data_buf_ready(chip
);
3277 if (retval
!= STATUS_SUCCESS
) {
3278 chip
->rw_need_retry
= 1;
3279 sd_set_err_code(chip
, SD_TO_ERR
);
3280 TRACE_GOTO(chip
, RW_FAIL
);
3283 retval
= sd_send_cmd_get_rsp(chip
, WRITE_MULTIPLE_BLOCK
,
3284 data_addr
, SD_RSP_TYPE_R1
, NULL
, 0);
3285 if (retval
!= STATUS_SUCCESS
) {
3286 chip
->rw_need_retry
= 1;
3287 TRACE_GOTO(chip
, RW_FAIL
);
3290 rtsx_init_cmd(chip
);
3292 cfg2
= SD_NO_CALCULATE_CRC7
| SD_CHECK_CRC16
|
3293 SD_NO_WAIT_BUSY_END
|
3294 SD_NO_CHECK_CRC7
| SD_RSP_LEN_0
;
3295 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CFG2
, 0xFF,
3298 trans_dma_enable(srb
->sc_data_direction
, chip
,
3299 sector_cnt
* 512, DMA_512
);
3301 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_TRANSFER
, 0xFF,
3302 SD_TM_AUTO_WRITE_3
| SD_TRANSFER_START
);
3303 rtsx_add_cmd(chip
, CHECK_REG_CMD
, REG_SD_TRANSFER
,
3304 SD_TRANSFER_END
, SD_TRANSFER_END
);
3306 rtsx_send_cmd_no_wait(chip
);
3309 sd_card
->seq_mode
= 1;
3312 retval
= rtsx_transfer_data(chip
, SD_CARD
, scsi_sglist(srb
),
3313 scsi_bufflen(srb
), scsi_sg_count(srb
),
3314 srb
->sc_data_direction
, chip
->sd_timeout
);
3319 sd_card
->seq_mode
= 0;
3321 if (retval
== -ETIMEDOUT
)
3322 err
= STATUS_TIMEDOUT
;
3326 rtsx_read_register(chip
, REG_SD_STAT1
, &stat
);
3327 rtsx_clear_sd_error(chip
);
3328 if (detect_card_cd(chip
, SD_CARD
) != STATUS_SUCCESS
) {
3329 chip
->rw_need_retry
= 0;
3330 RTSX_DEBUGP("No card exist, exit sd_rw\n");
3331 TRACE_RET(chip
, STATUS_FAIL
);
3334 chip
->rw_need_retry
= 1;
3336 retval
= sd_send_cmd_get_rsp(chip
, STOP_TRANSMISSION
, 0,
3337 SD_RSP_TYPE_R1b
, NULL
, 0);
3338 if (retval
!= STATUS_SUCCESS
) {
3339 sd_set_err_code(chip
, SD_STS_ERR
);
3340 TRACE_GOTO(chip
, RW_FAIL
);
3343 if (stat
& (SD_CRC7_ERR
| SD_CRC16_ERR
| SD_CRC_WRITE_ERR
)) {
3344 RTSX_DEBUGP("SD CRC error, tune clock!\n");
3345 sd_set_err_code(chip
, SD_CRC_ERR
);
3346 TRACE_GOTO(chip
, RW_FAIL
);
3349 if (err
== STATUS_TIMEDOUT
) {
3350 sd_set_err_code(chip
, SD_TO_ERR
);
3351 TRACE_GOTO(chip
, RW_FAIL
);
3354 TRACE_RET(chip
, err
);
3357 sd_card
->pre_sec_addr
= start_sector
;
3358 sd_card
->pre_sec_cnt
= sector_cnt
;
3359 sd_card
->pre_dir
= srb
->sc_data_direction
;
3361 return STATUS_SUCCESS
;
3364 sd_card
->seq_mode
= 0;
3366 if (detect_card_cd(chip
, SD_CARD
) != STATUS_SUCCESS
) {
3367 chip
->rw_need_retry
= 0;
3368 RTSX_DEBUGP("No card exist, exit sd_rw\n");
3369 TRACE_RET(chip
, STATUS_FAIL
);
3372 if (sd_check_err_code(chip
, SD_CRC_ERR
)) {
3373 if (CHK_MMC_4BIT(sd_card
) || CHK_MMC_8BIT(sd_card
)) {
3374 sd_card
->mmc_dont_switch_bus
= 1;
3375 reset_mmc_only(chip
);
3376 sd_card
->mmc_dont_switch_bus
= 0;
3378 sd_card
->need_retune
= 1;
3379 sd_auto_tune_clock(chip
);
3381 } else if (sd_check_err_code(chip
, SD_TO_ERR
| SD_STS_ERR
)) {
3382 retval
= reset_sd_card(chip
);
3383 if (retval
!= STATUS_SUCCESS
) {
3384 chip
->card_ready
&= ~SD_CARD
;
3385 chip
->card_fail
|= SD_CARD
;
3386 chip
->capacity
[chip
->card2lun
[SD_CARD
]] = 0;
3390 TRACE_RET(chip
, STATUS_FAIL
);
3394 int soft_reset_sd_card(struct rtsx_chip
*chip
)
3396 return reset_sd(chip
);
3399 int ext_sd_send_cmd_get_rsp(struct rtsx_chip
*chip
, u8 cmd_idx
,
3400 u32 arg
, u8 rsp_type
, u8
*rsp
, int rsp_len
, int special_check
)
3409 RTSX_DEBUGP("EXT SD/MMC CMD %d\n", cmd_idx
);
3411 if (rsp_type
== SD_RSP_TYPE_R1b
)
3416 rtsx_init_cmd(chip
);
3418 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD0
, 0xFF, 0x40 | cmd_idx
);
3419 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD1
, 0xFF, (u8
)(arg
>> 24));
3420 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD2
, 0xFF, (u8
)(arg
>> 16));
3421 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD3
, 0xFF, (u8
)(arg
>> 8));
3422 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD4
, 0xFF, (u8
)arg
);
3424 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CFG2
, 0xFF, rsp_type
);
3425 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
,
3426 0x01, PINGPONG_BUFFER
);
3427 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_TRANSFER
,
3428 0xFF, SD_TM_CMD_RSP
| SD_TRANSFER_START
);
3429 rtsx_add_cmd(chip
, CHECK_REG_CMD
, REG_SD_TRANSFER
, SD_TRANSFER_END
,
3432 if (rsp_type
== SD_RSP_TYPE_R2
) {
3433 for (reg_addr
= PPBUF_BASE2
; reg_addr
< PPBUF_BASE2
+ 16;
3435 rtsx_add_cmd(chip
, READ_REG_CMD
, reg_addr
, 0, 0);
3438 } else if (rsp_type
!= SD_RSP_TYPE_R0
) {
3439 for (reg_addr
= REG_SD_CMD0
; reg_addr
<= REG_SD_CMD4
;
3441 rtsx_add_cmd(chip
, READ_REG_CMD
, reg_addr
, 0, 0);
3445 rtsx_add_cmd(chip
, READ_REG_CMD
, REG_SD_CMD5
, 0, 0);
3447 rtsx_add_cmd(chip
, READ_REG_CMD
, REG_SD_STAT1
, 0, 0);
3449 retval
= rtsx_send_cmd(chip
, SD_CARD
, timeout
);
3451 if (retval
== -ETIMEDOUT
) {
3452 rtsx_clear_sd_error(chip
);
3454 if (rsp_type
& SD_WAIT_BUSY_END
) {
3455 retval
= sd_check_data0_status(chip
);
3456 if (retval
!= STATUS_SUCCESS
)
3457 TRACE_RET(chip
, retval
);
3459 sd_set_err_code(chip
, SD_TO_ERR
);
3462 TRACE_RET(chip
, STATUS_FAIL
);
3465 if (rsp_type
== SD_RSP_TYPE_R0
)
3466 return STATUS_SUCCESS
;
3468 ptr
= rtsx_get_cmd_data(chip
) + 1;
3470 if ((ptr
[0] & 0xC0) != 0) {
3471 sd_set_err_code(chip
, SD_STS_ERR
);
3472 TRACE_RET(chip
, STATUS_FAIL
);
3475 if (!(rsp_type
& SD_NO_CHECK_CRC7
)) {
3476 if (ptr
[stat_idx
] & SD_CRC7_ERR
) {
3477 if (cmd_idx
== WRITE_MULTIPLE_BLOCK
) {
3478 sd_set_err_code(chip
, SD_CRC_ERR
);
3479 TRACE_RET(chip
, STATUS_FAIL
);
3481 if (rty_cnt
< SD_MAX_RETRY_COUNT
) {
3486 sd_set_err_code(chip
, SD_CRC_ERR
);
3487 TRACE_RET(chip
, STATUS_FAIL
);
3492 if ((cmd_idx
== SELECT_CARD
) || (cmd_idx
== APP_CMD
) ||
3493 (cmd_idx
== SEND_STATUS
) || (cmd_idx
== STOP_TRANSMISSION
)) {
3494 if ((cmd_idx
!= STOP_TRANSMISSION
) && (special_check
== 0)) {
3496 TRACE_RET(chip
, STATUS_FAIL
);
3498 #ifdef SUPPORT_SD_LOCK
3504 TRACE_RET(chip
, STATUS_FAIL
);
3507 TRACE_RET(chip
, STATUS_FAIL
);
3509 if (cmd_idx
== SELECT_CARD
) {
3510 if (rsp_type
== SD_RSP_TYPE_R2
) {
3511 if ((ptr
[3] & 0x1E) != 0x04)
3512 TRACE_RET(chip
, STATUS_FAIL
);
3514 } else if (rsp_type
== SD_RSP_TYPE_R0
) {
3515 if ((ptr
[3] & 0x1E) != 0x03)
3516 TRACE_RET(chip
, STATUS_FAIL
);
3522 memcpy(rsp
, ptr
, rsp_len
);
3524 return STATUS_SUCCESS
;
3527 int ext_sd_get_rsp(struct rtsx_chip
*chip
, int len
, u8
*rsp
, u8 rsp_type
)
3529 int retval
, rsp_len
;
3532 if (rsp_type
== SD_RSP_TYPE_R0
)
3533 return STATUS_SUCCESS
;
3535 rtsx_init_cmd(chip
);
3537 if (rsp_type
== SD_RSP_TYPE_R2
) {
3538 for (reg_addr
= PPBUF_BASE2
; reg_addr
< PPBUF_BASE2
+ 16;
3540 rtsx_add_cmd(chip
, READ_REG_CMD
, reg_addr
, 0xFF, 0);
3543 } else if (rsp_type
!= SD_RSP_TYPE_R0
) {
3544 for (reg_addr
= REG_SD_CMD0
; reg_addr
<= REG_SD_CMD4
;
3546 rtsx_add_cmd(chip
, READ_REG_CMD
, reg_addr
, 0xFF, 0);
3550 rtsx_add_cmd(chip
, READ_REG_CMD
, REG_SD_CMD5
, 0xFF, 0);
3552 retval
= rtsx_send_cmd(chip
, SD_CARD
, 100);
3553 if (retval
!= STATUS_SUCCESS
)
3554 TRACE_RET(chip
, STATUS_FAIL
);
3557 int min_len
= (rsp_len
< len
) ? rsp_len
: len
;
3559 memcpy(rsp
, rtsx_get_cmd_data(chip
), min_len
);
3561 RTSX_DEBUGP("min_len = %d\n", min_len
);
3562 RTSX_DEBUGP("Response in cmd buf: 0x%x 0x%x 0x%x 0x%x\n",
3563 rsp
[0], rsp
[1], rsp
[2], rsp
[3]);
3566 return STATUS_SUCCESS
;
3569 int sd_pass_thru_mode(struct scsi_cmnd
*srb
, struct rtsx_chip
*chip
)
3571 struct sd_info
*sd_card
= &(chip
->sd_card
);
3572 unsigned int lun
= SCSI_LUN(srb
);
3595 sd_card
->pre_cmd_err
= 0;
3597 if (!(CHK_BIT(chip
->lun_mc
, lun
))) {
3598 SET_BIT(chip
->lun_mc
, lun
);
3599 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_CHANGE
);
3600 TRACE_RET(chip
, TRANSPORT_FAILED
);
3603 if ((0x53 != srb
->cmnd
[2]) || (0x44 != srb
->cmnd
[3]) ||
3604 (0x20 != srb
->cmnd
[4]) || (0x43 != srb
->cmnd
[5]) ||
3605 (0x61 != srb
->cmnd
[6]) || (0x72 != srb
->cmnd
[7]) ||
3606 (0x64 != srb
->cmnd
[8])) {
3607 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
3608 TRACE_RET(chip
, TRANSPORT_FAILED
);
3611 switch (srb
->cmnd
[1] & 0x0F) {
3613 sd_card
->sd_pass_thru_en
= 0;
3617 sd_card
->sd_pass_thru_en
= 1;
3621 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
3622 TRACE_RET(chip
, TRANSPORT_FAILED
);
3625 buf
[5] = (1 == CHK_SD(sd_card
)) ? 0x01 : 0x02;
3626 if (chip
->card_wp
& SD_CARD
)
3629 buf
[6] = (u8
)(sd_card
->sd_addr
>> 16);
3630 buf
[7] = (u8
)(sd_card
->sd_addr
>> 24);
3632 buf
[15] = chip
->max_lun
;
3634 len
= min_t(int, 18, scsi_bufflen(srb
));
3635 rtsx_stor_set_xfer_buf(buf
, len
, srb
);
3637 return TRANSPORT_GOOD
;
3640 static inline int get_rsp_type(struct scsi_cmnd
*srb
, u8
*rsp_type
,
3643 if (!rsp_type
|| !rsp_len
)
3646 switch (srb
->cmnd
[10]) {
3648 *rsp_type
= SD_RSP_TYPE_R0
;
3653 *rsp_type
= SD_RSP_TYPE_R1
;
3658 *rsp_type
= SD_RSP_TYPE_R1b
;
3663 *rsp_type
= SD_RSP_TYPE_R2
;
3668 *rsp_type
= SD_RSP_TYPE_R3
;
3676 return STATUS_SUCCESS
;
3679 int sd_execute_no_data(struct scsi_cmnd
*srb
, struct rtsx_chip
*chip
)
3681 struct sd_info
*sd_card
= &(chip
->sd_card
);
3682 unsigned int lun
= SCSI_LUN(srb
);
3683 int retval
, rsp_len
;
3684 u8 cmd_idx
, rsp_type
;
3685 u8 standby
= 0, acmd
= 0;
3688 if (!sd_card
->sd_pass_thru_en
) {
3689 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
3690 TRACE_RET(chip
, TRANSPORT_FAILED
);
3693 retval
= sd_switch_clock(chip
);
3694 if (retval
!= STATUS_SUCCESS
)
3695 TRACE_RET(chip
, TRANSPORT_FAILED
);
3697 if (sd_card
->pre_cmd_err
) {
3698 sd_card
->pre_cmd_err
= 0;
3699 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_CHANGE
);
3700 TRACE_RET(chip
, TRANSPORT_FAILED
);
3703 cmd_idx
= srb
->cmnd
[2] & 0x3F;
3704 if (srb
->cmnd
[1] & 0x02)
3707 if (srb
->cmnd
[1] & 0x01)
3710 arg
= ((u32
)srb
->cmnd
[3] << 24) | ((u32
)srb
->cmnd
[4] << 16) |
3711 ((u32
)srb
->cmnd
[5] << 8) | srb
->cmnd
[6];
3713 retval
= get_rsp_type(srb
, &rsp_type
, &rsp_len
);
3714 if (retval
!= STATUS_SUCCESS
) {
3715 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
3716 TRACE_RET(chip
, TRANSPORT_FAILED
);
3718 sd_card
->last_rsp_type
= rsp_type
;
3720 retval
= sd_switch_clock(chip
);
3721 if (retval
!= STATUS_SUCCESS
)
3722 TRACE_RET(chip
, TRANSPORT_FAILED
);
3724 #ifdef SUPPORT_SD_LOCK
3725 if ((sd_card
->sd_lock_status
& SD_LOCK_1BIT_MODE
) == 0) {
3726 if (CHK_MMC_8BIT(sd_card
)) {
3727 retval
= rtsx_write_register(chip
, REG_SD_CFG1
, 0x03,
3729 if (retval
!= STATUS_SUCCESS
)
3730 TRACE_RET(chip
, TRANSPORT_FAILED
);
3732 } else if (CHK_SD(sd_card
) || CHK_MMC_4BIT(sd_card
)) {
3733 retval
= rtsx_write_register(chip
, REG_SD_CFG1
, 0x03,
3735 if (retval
!= STATUS_SUCCESS
)
3736 TRACE_RET(chip
, TRANSPORT_FAILED
);
3740 retval
= rtsx_write_register(chip
, REG_SD_CFG1
, 0x03, SD_BUS_WIDTH_4
);
3741 if (retval
!= STATUS_SUCCESS
)
3742 TRACE_RET(chip
, TRANSPORT_FAILED
);
3746 retval
= sd_select_card(chip
, 0);
3747 if (retval
!= STATUS_SUCCESS
)
3748 TRACE_GOTO(chip
, SD_Execute_Cmd_Failed
);
3752 retval
= ext_sd_send_cmd_get_rsp(chip
, APP_CMD
,
3754 SD_RSP_TYPE_R1
, NULL
, 0, 0);
3755 if (retval
!= STATUS_SUCCESS
)
3756 TRACE_GOTO(chip
, SD_Execute_Cmd_Failed
);
3759 retval
= ext_sd_send_cmd_get_rsp(chip
, cmd_idx
, arg
, rsp_type
,
3760 sd_card
->rsp
, rsp_len
, 0);
3761 if (retval
!= STATUS_SUCCESS
)
3762 TRACE_GOTO(chip
, SD_Execute_Cmd_Failed
);
3765 retval
= sd_select_card(chip
, 1);
3766 if (retval
!= STATUS_SUCCESS
)
3767 TRACE_GOTO(chip
, SD_Execute_Cmd_Failed
);
3770 #ifdef SUPPORT_SD_LOCK
3771 retval
= sd_update_lock_status(chip
);
3772 if (retval
!= STATUS_SUCCESS
)
3773 TRACE_GOTO(chip
, SD_Execute_Cmd_Failed
);
3776 scsi_set_resid(srb
, 0);
3777 return TRANSPORT_GOOD
;
3779 SD_Execute_Cmd_Failed
:
3780 sd_card
->pre_cmd_err
= 1;
3781 set_sense_type(chip
, lun
, SENSE_TYPE_NO_SENSE
);
3782 release_sd_card(chip
);
3783 do_reset_sd_card(chip
);
3784 if (!(chip
->card_ready
& SD_CARD
))
3785 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_NOT_PRESENT
);
3787 TRACE_RET(chip
, TRANSPORT_FAILED
);
3790 int sd_execute_read_data(struct scsi_cmnd
*srb
, struct rtsx_chip
*chip
)
3792 struct sd_info
*sd_card
= &(chip
->sd_card
);
3793 unsigned int lun
= SCSI_LUN(srb
);
3794 int retval
, rsp_len
, i
;
3795 int cmd13_checkbit
= 0, read_err
= 0;
3796 u8 cmd_idx
, rsp_type
, bus_width
;
3797 u8 send_cmd12
= 0, standby
= 0, acmd
= 0;
3800 if (!sd_card
->sd_pass_thru_en
) {
3801 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
3802 TRACE_RET(chip
, TRANSPORT_FAILED
);
3805 if (sd_card
->pre_cmd_err
) {
3806 sd_card
->pre_cmd_err
= 0;
3807 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_CHANGE
);
3808 TRACE_RET(chip
, TRANSPORT_FAILED
);
3811 retval
= sd_switch_clock(chip
);
3812 if (retval
!= STATUS_SUCCESS
)
3813 TRACE_RET(chip
, TRANSPORT_FAILED
);
3815 cmd_idx
= srb
->cmnd
[2] & 0x3F;
3816 if (srb
->cmnd
[1] & 0x04)
3819 if (srb
->cmnd
[1] & 0x02)
3822 if (srb
->cmnd
[1] & 0x01)
3825 data_len
= ((u32
)srb
->cmnd
[7] << 16) | ((u32
)srb
->cmnd
[8]
3826 << 8) | srb
->cmnd
[9];
3828 retval
= get_rsp_type(srb
, &rsp_type
, &rsp_len
);
3829 if (retval
!= STATUS_SUCCESS
) {
3830 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
3831 TRACE_RET(chip
, TRANSPORT_FAILED
);
3833 sd_card
->last_rsp_type
= rsp_type
;
3835 retval
= sd_switch_clock(chip
);
3836 if (retval
!= STATUS_SUCCESS
)
3837 TRACE_RET(chip
, TRANSPORT_FAILED
);
3839 #ifdef SUPPORT_SD_LOCK
3840 if ((sd_card
->sd_lock_status
& SD_LOCK_1BIT_MODE
) == 0) {
3841 if (CHK_MMC_8BIT(sd_card
))
3842 bus_width
= SD_BUS_WIDTH_8
;
3843 else if (CHK_SD(sd_card
) || CHK_MMC_4BIT(sd_card
))
3844 bus_width
= SD_BUS_WIDTH_4
;
3846 bus_width
= SD_BUS_WIDTH_1
;
3848 bus_width
= SD_BUS_WIDTH_4
;
3850 RTSX_DEBUGP("bus_width = %d\n", bus_width
);
3852 bus_width
= SD_BUS_WIDTH_4
;
3855 if (data_len
< 512) {
3856 retval
= ext_sd_send_cmd_get_rsp(chip
, SET_BLOCKLEN
, data_len
,
3857 SD_RSP_TYPE_R1
, NULL
, 0, 0);
3858 if (retval
!= STATUS_SUCCESS
)
3859 TRACE_GOTO(chip
, SD_Execute_Read_Cmd_Failed
);
3863 retval
= sd_select_card(chip
, 0);
3864 if (retval
!= STATUS_SUCCESS
)
3865 TRACE_GOTO(chip
, SD_Execute_Read_Cmd_Failed
);
3869 retval
= ext_sd_send_cmd_get_rsp(chip
, APP_CMD
,
3871 SD_RSP_TYPE_R1
, NULL
, 0, 0);
3872 if (retval
!= STATUS_SUCCESS
)
3873 TRACE_GOTO(chip
, SD_Execute_Read_Cmd_Failed
);
3876 if (data_len
<= 512) {
3879 u16 byte_cnt
, blk_cnt
;
3882 byte_cnt
= ((u16
)(srb
->cmnd
[8] & 0x03) << 8) | srb
->cmnd
[9];
3885 cmd
[0] = 0x40 | cmd_idx
;
3886 cmd
[1] = srb
->cmnd
[3];
3887 cmd
[2] = srb
->cmnd
[4];
3888 cmd
[3] = srb
->cmnd
[5];
3889 cmd
[4] = srb
->cmnd
[6];
3891 buf
= kmalloc(data_len
, GFP_KERNEL
);
3893 TRACE_RET(chip
, TRANSPORT_ERROR
);
3895 retval
= sd_read_data(chip
, SD_TM_NORMAL_READ
, cmd
, 5, byte_cnt
,
3896 blk_cnt
, bus_width
, buf
, data_len
, 2000);
3897 if (retval
!= STATUS_SUCCESS
) {
3900 rtsx_clear_sd_error(chip
);
3901 TRACE_GOTO(chip
, SD_Execute_Read_Cmd_Failed
);
3904 min_len
= min(data_len
, scsi_bufflen(srb
));
3905 rtsx_stor_set_xfer_buf(buf
, min_len
, srb
);
3908 } else if (!(data_len
& 0x1FF)) {
3909 rtsx_init_cmd(chip
);
3911 trans_dma_enable(DMA_FROM_DEVICE
, chip
, data_len
, DMA_512
);
3913 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BYTE_CNT_H
, 0xFF,
3915 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BYTE_CNT_L
, 0xFF,
3917 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BLOCK_CNT_H
,
3918 0xFF, (srb
->cmnd
[7] & 0xFE) >> 1);
3919 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BLOCK_CNT_L
,
3920 0xFF, (u8
)((data_len
& 0x0001FE00) >> 9));
3922 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD0
, 0xFF,
3924 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD1
, 0xFF,
3926 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD2
, 0xFF,
3928 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD3
, 0xFF,
3930 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CMD4
, 0xFF,
3933 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CFG1
, 0x03, bus_width
);
3934 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_CFG2
, 0xFF, rsp_type
);
3936 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_TRANSFER
,
3937 0xFF, SD_TM_AUTO_READ_2
| SD_TRANSFER_START
);
3938 rtsx_add_cmd(chip
, CHECK_REG_CMD
, REG_SD_TRANSFER
,
3939 SD_TRANSFER_END
, SD_TRANSFER_END
);
3941 rtsx_send_cmd_no_wait(chip
);
3943 retval
= rtsx_transfer_data(chip
, SD_CARD
, scsi_sglist(srb
),
3944 scsi_bufflen(srb
), scsi_sg_count(srb
),
3945 DMA_FROM_DEVICE
, 10000);
3948 rtsx_clear_sd_error(chip
);
3949 TRACE_GOTO(chip
, SD_Execute_Read_Cmd_Failed
);
3953 TRACE_GOTO(chip
, SD_Execute_Read_Cmd_Failed
);
3956 retval
= ext_sd_get_rsp(chip
, rsp_len
, sd_card
->rsp
, rsp_type
);
3957 if (retval
!= STATUS_SUCCESS
)
3958 TRACE_GOTO(chip
, SD_Execute_Read_Cmd_Failed
);
3961 retval
= sd_select_card(chip
, 1);
3962 if (retval
!= STATUS_SUCCESS
)
3963 TRACE_GOTO(chip
, SD_Execute_Read_Cmd_Failed
);
3967 retval
= ext_sd_send_cmd_get_rsp(chip
, STOP_TRANSMISSION
,
3968 0, SD_RSP_TYPE_R1b
, NULL
, 0, 0);
3969 if (retval
!= STATUS_SUCCESS
)
3970 TRACE_GOTO(chip
, SD_Execute_Read_Cmd_Failed
);
3973 if (data_len
< 512) {
3974 retval
= ext_sd_send_cmd_get_rsp(chip
, SET_BLOCKLEN
, 0x200,
3975 SD_RSP_TYPE_R1
, NULL
, 0, 0);
3976 if (retval
!= STATUS_SUCCESS
)
3977 TRACE_GOTO(chip
, SD_Execute_Read_Cmd_Failed
);
3979 retval
= rtsx_write_register(chip
, SD_BYTE_CNT_H
, 0xFF, 0x02);
3980 if (retval
!= STATUS_SUCCESS
)
3981 TRACE_GOTO(chip
, SD_Execute_Read_Cmd_Failed
);
3983 retval
= rtsx_write_register(chip
, SD_BYTE_CNT_L
, 0xFF, 0x00);
3984 if (retval
!= STATUS_SUCCESS
)
3985 TRACE_GOTO(chip
, SD_Execute_Read_Cmd_Failed
);
3988 if ((srb
->cmnd
[1] & 0x02) || (srb
->cmnd
[1] & 0x04))
3991 for (i
= 0; i
< 3; i
++) {
3992 retval
= ext_sd_send_cmd_get_rsp(chip
, SEND_STATUS
,
3994 SD_RSP_TYPE_R1
, NULL
, 0,
3996 if (retval
== STATUS_SUCCESS
)
3999 if (retval
!= STATUS_SUCCESS
)
4000 TRACE_GOTO(chip
, SD_Execute_Read_Cmd_Failed
);
4002 scsi_set_resid(srb
, 0);
4003 return TRANSPORT_GOOD
;
4005 SD_Execute_Read_Cmd_Failed
:
4006 sd_card
->pre_cmd_err
= 1;
4007 set_sense_type(chip
, lun
, SENSE_TYPE_NO_SENSE
);
4009 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR
);
4011 release_sd_card(chip
);
4012 do_reset_sd_card(chip
);
4013 if (!(chip
->card_ready
& SD_CARD
))
4014 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_NOT_PRESENT
);
4016 TRACE_RET(chip
, TRANSPORT_FAILED
);
4019 int sd_execute_write_data(struct scsi_cmnd
*srb
, struct rtsx_chip
*chip
)
4021 struct sd_info
*sd_card
= &(chip
->sd_card
);
4022 unsigned int lun
= SCSI_LUN(srb
);
4023 int retval
, rsp_len
, i
;
4024 int cmd13_checkbit
= 0, write_err
= 0;
4025 u8 cmd_idx
, rsp_type
;
4026 u8 send_cmd12
= 0, standby
= 0, acmd
= 0;
4028 #ifdef SUPPORT_SD_LOCK
4029 int lock_cmd_fail
= 0;
4030 u8 sd_lock_state
= 0;
4031 u8 lock_cmd_type
= 0;
4034 if (!sd_card
->sd_pass_thru_en
) {
4035 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
4036 TRACE_RET(chip
, TRANSPORT_FAILED
);
4039 if (sd_card
->pre_cmd_err
) {
4040 sd_card
->pre_cmd_err
= 0;
4041 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_CHANGE
);
4042 TRACE_RET(chip
, TRANSPORT_FAILED
);
4045 retval
= sd_switch_clock(chip
);
4046 if (retval
!= STATUS_SUCCESS
)
4047 TRACE_RET(chip
, TRANSPORT_FAILED
);
4049 cmd_idx
= srb
->cmnd
[2] & 0x3F;
4050 if (srb
->cmnd
[1] & 0x04)
4053 if (srb
->cmnd
[1] & 0x02)
4056 if (srb
->cmnd
[1] & 0x01)
4059 data_len
= ((u32
)srb
->cmnd
[7] << 16) | ((u32
)srb
->cmnd
[8]
4060 << 8) | srb
->cmnd
[9];
4061 arg
= ((u32
)srb
->cmnd
[3] << 24) | ((u32
)srb
->cmnd
[4] << 16) |
4062 ((u32
)srb
->cmnd
[5] << 8) | srb
->cmnd
[6];
4064 #ifdef SUPPORT_SD_LOCK
4065 if (cmd_idx
== LOCK_UNLOCK
) {
4066 sd_lock_state
= sd_card
->sd_lock_status
;
4067 sd_lock_state
&= SD_LOCKED
;
4071 retval
= get_rsp_type(srb
, &rsp_type
, &rsp_len
);
4072 if (retval
!= STATUS_SUCCESS
) {
4073 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
4074 TRACE_RET(chip
, TRANSPORT_FAILED
);
4076 sd_card
->last_rsp_type
= rsp_type
;
4078 retval
= sd_switch_clock(chip
);
4079 if (retval
!= STATUS_SUCCESS
)
4080 TRACE_RET(chip
, TRANSPORT_FAILED
);
4082 #ifdef SUPPORT_SD_LOCK
4083 if ((sd_card
->sd_lock_status
& SD_LOCK_1BIT_MODE
) == 0) {
4084 if (CHK_MMC_8BIT(sd_card
)) {
4085 retval
= rtsx_write_register(chip
, REG_SD_CFG1
, 0x03,
4087 if (retval
!= STATUS_SUCCESS
)
4088 TRACE_RET(chip
, TRANSPORT_FAILED
);
4090 } else if (CHK_SD(sd_card
) || CHK_MMC_4BIT(sd_card
)) {
4091 retval
= rtsx_write_register(chip
, REG_SD_CFG1
, 0x03,
4093 if (retval
!= STATUS_SUCCESS
)
4094 TRACE_RET(chip
, TRANSPORT_FAILED
);
4098 retval
= rtsx_write_register(chip
, REG_SD_CFG1
, 0x03, SD_BUS_WIDTH_4
);
4099 if (retval
!= STATUS_SUCCESS
)
4100 TRACE_RET(chip
, TRANSPORT_FAILED
);
4103 if (data_len
< 512) {
4104 retval
= ext_sd_send_cmd_get_rsp(chip
, SET_BLOCKLEN
, data_len
,
4105 SD_RSP_TYPE_R1
, NULL
, 0, 0);
4106 if (retval
!= STATUS_SUCCESS
)
4107 TRACE_GOTO(chip
, SD_Execute_Write_Cmd_Failed
);
4111 retval
= sd_select_card(chip
, 0);
4112 if (retval
!= STATUS_SUCCESS
)
4113 TRACE_GOTO(chip
, SD_Execute_Write_Cmd_Failed
);
4117 retval
= ext_sd_send_cmd_get_rsp(chip
, APP_CMD
,
4119 SD_RSP_TYPE_R1
, NULL
, 0, 0);
4120 if (retval
!= STATUS_SUCCESS
)
4121 TRACE_GOTO(chip
, SD_Execute_Write_Cmd_Failed
);
4124 retval
= ext_sd_send_cmd_get_rsp(chip
, cmd_idx
, arg
, rsp_type
,
4125 sd_card
->rsp
, rsp_len
, 0);
4126 if (retval
!= STATUS_SUCCESS
)
4127 TRACE_GOTO(chip
, SD_Execute_Write_Cmd_Failed
);
4129 if (data_len
<= 512) {
4133 buf
= kmalloc(data_len
, GFP_KERNEL
);
4135 TRACE_RET(chip
, TRANSPORT_ERROR
);
4137 rtsx_stor_get_xfer_buf(buf
, data_len
, srb
);
4139 #ifdef SUPPORT_SD_LOCK
4140 if (cmd_idx
== LOCK_UNLOCK
)
4141 lock_cmd_type
= buf
[0] & 0x0F;
4144 if (data_len
> 256) {
4145 rtsx_init_cmd(chip
);
4146 for (i
= 0; i
< 256; i
++) {
4147 rtsx_add_cmd(chip
, WRITE_REG_CMD
,
4148 PPBUF_BASE2
+ i
, 0xFF, buf
[i
]);
4150 retval
= rtsx_send_cmd(chip
, 0, 250);
4151 if (retval
!= STATUS_SUCCESS
) {
4153 TRACE_GOTO(chip
, SD_Execute_Write_Cmd_Failed
);
4156 rtsx_init_cmd(chip
);
4157 for (i
= 256; i
< data_len
; i
++) {
4158 rtsx_add_cmd(chip
, WRITE_REG_CMD
,
4159 PPBUF_BASE2
+ i
, 0xFF, buf
[i
]);
4161 retval
= rtsx_send_cmd(chip
, 0, 250);
4162 if (retval
!= STATUS_SUCCESS
) {
4164 TRACE_GOTO(chip
, SD_Execute_Write_Cmd_Failed
);
4167 rtsx_init_cmd(chip
);
4168 for (i
= 0; i
< data_len
; i
++) {
4169 rtsx_add_cmd(chip
, WRITE_REG_CMD
,
4170 PPBUF_BASE2
+ i
, 0xFF, buf
[i
]);
4172 retval
= rtsx_send_cmd(chip
, 0, 250);
4173 if (retval
!= STATUS_SUCCESS
) {
4175 TRACE_GOTO(chip
, SD_Execute_Write_Cmd_Failed
);
4181 rtsx_init_cmd(chip
);
4183 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BYTE_CNT_H
, 0xFF,
4184 srb
->cmnd
[8] & 0x03);
4185 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BYTE_CNT_L
, 0xFF,
4187 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BLOCK_CNT_H
, 0xFF,
4189 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BLOCK_CNT_L
, 0xFF,
4191 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
, 0x01,
4194 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_TRANSFER
, 0xFF,
4195 SD_TM_AUTO_WRITE_3
| SD_TRANSFER_START
);
4196 rtsx_add_cmd(chip
, CHECK_REG_CMD
, REG_SD_TRANSFER
,
4197 SD_TRANSFER_END
, SD_TRANSFER_END
);
4199 retval
= rtsx_send_cmd(chip
, SD_CARD
, 250);
4200 } else if (!(data_len
& 0x1FF)) {
4201 rtsx_init_cmd(chip
);
4203 trans_dma_enable(DMA_TO_DEVICE
, chip
, data_len
, DMA_512
);
4205 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BYTE_CNT_H
, 0xFF,
4207 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BYTE_CNT_L
, 0xFF,
4209 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BLOCK_CNT_H
,
4210 0xFF, (srb
->cmnd
[7] & 0xFE) >> 1);
4211 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_BLOCK_CNT_L
,
4212 0xFF, (u8
)((data_len
& 0x0001FE00) >> 9));
4214 rtsx_add_cmd(chip
, WRITE_REG_CMD
, REG_SD_TRANSFER
, 0xFF,
4215 SD_TM_AUTO_WRITE_3
| SD_TRANSFER_START
);
4216 rtsx_add_cmd(chip
, CHECK_REG_CMD
, REG_SD_TRANSFER
,
4217 SD_TRANSFER_END
, SD_TRANSFER_END
);
4219 rtsx_send_cmd_no_wait(chip
);
4221 retval
= rtsx_transfer_data(chip
, SD_CARD
, scsi_sglist(srb
),
4222 scsi_bufflen(srb
), scsi_sg_count(srb
),
4223 DMA_TO_DEVICE
, 10000);
4226 TRACE_GOTO(chip
, SD_Execute_Write_Cmd_Failed
);
4231 rtsx_clear_sd_error(chip
);
4232 TRACE_GOTO(chip
, SD_Execute_Write_Cmd_Failed
);
4235 #ifdef SUPPORT_SD_LOCK
4236 if (cmd_idx
== LOCK_UNLOCK
) {
4237 if (lock_cmd_type
== SD_ERASE
) {
4238 sd_card
->sd_erase_status
= SD_UNDER_ERASING
;
4239 scsi_set_resid(srb
, 0);
4240 return TRANSPORT_GOOD
;
4243 rtsx_init_cmd(chip
);
4244 rtsx_add_cmd(chip
, CHECK_REG_CMD
, 0xFD30, 0x02, 0x02);
4246 rtsx_send_cmd(chip
, SD_CARD
, 250);
4248 retval
= sd_update_lock_status(chip
);
4249 if (retval
!= STATUS_SUCCESS
) {
4250 RTSX_DEBUGP("Lock command fail!\n");
4254 #endif /* SUPPORT_SD_LOCK */
4257 retval
= sd_select_card(chip
, 1);
4258 if (retval
!= STATUS_SUCCESS
)
4259 TRACE_GOTO(chip
, SD_Execute_Write_Cmd_Failed
);
4263 retval
= ext_sd_send_cmd_get_rsp(chip
, STOP_TRANSMISSION
,
4264 0, SD_RSP_TYPE_R1b
, NULL
, 0, 0);
4265 if (retval
!= STATUS_SUCCESS
)
4266 TRACE_GOTO(chip
, SD_Execute_Write_Cmd_Failed
);
4269 if (data_len
< 512) {
4270 retval
= ext_sd_send_cmd_get_rsp(chip
, SET_BLOCKLEN
, 0x200,
4271 SD_RSP_TYPE_R1
, NULL
, 0, 0);
4272 if (retval
!= STATUS_SUCCESS
)
4273 TRACE_GOTO(chip
, SD_Execute_Write_Cmd_Failed
);
4275 retval
= rtsx_write_register(chip
, SD_BYTE_CNT_H
, 0xFF, 0x02);
4276 if (retval
!= STATUS_SUCCESS
)
4277 TRACE_GOTO(chip
, SD_Execute_Write_Cmd_Failed
);
4279 rtsx_write_register(chip
, SD_BYTE_CNT_L
, 0xFF, 0x00);
4280 if (retval
!= STATUS_SUCCESS
)
4281 TRACE_GOTO(chip
, SD_Execute_Write_Cmd_Failed
);
4284 if ((srb
->cmnd
[1] & 0x02) || (srb
->cmnd
[1] & 0x04))
4287 for (i
= 0; i
< 3; i
++) {
4288 retval
= ext_sd_send_cmd_get_rsp(chip
, SEND_STATUS
,
4290 SD_RSP_TYPE_R1
, NULL
, 0,
4292 if (retval
== STATUS_SUCCESS
)
4295 if (retval
!= STATUS_SUCCESS
)
4296 TRACE_GOTO(chip
, SD_Execute_Write_Cmd_Failed
);
4298 #ifdef SUPPORT_SD_LOCK
4299 if (cmd_idx
== LOCK_UNLOCK
) {
4300 if (!lock_cmd_fail
) {
4301 RTSX_DEBUGP("lock_cmd_type = 0x%x\n", lock_cmd_type
);
4302 if (lock_cmd_type
& SD_CLR_PWD
)
4303 sd_card
->sd_lock_status
&= ~SD_PWD_EXIST
;
4305 if (lock_cmd_type
& SD_SET_PWD
)
4306 sd_card
->sd_lock_status
|= SD_PWD_EXIST
;
4309 RTSX_DEBUGP("sd_lock_state = 0x%x, sd_card->sd_lock_status = 0x%x\n",
4310 sd_lock_state
, sd_card
->sd_lock_status
);
4311 if (sd_lock_state
^ (sd_card
->sd_lock_status
& SD_LOCKED
)) {
4312 sd_card
->sd_lock_notify
= 1;
4313 if (sd_lock_state
) {
4314 if (sd_card
->sd_lock_status
& SD_LOCK_1BIT_MODE
) {
4315 sd_card
->sd_lock_status
|= (
4316 SD_UNLOCK_POW_ON
| SD_SDR_RST
);
4317 if (CHK_SD(sd_card
)) {
4318 retval
= reset_sd(chip
);
4319 if (retval
!= STATUS_SUCCESS
) {
4320 sd_card
->sd_lock_status
&= ~(SD_UNLOCK_POW_ON
| SD_SDR_RST
);
4321 TRACE_GOTO(chip
, SD_Execute_Write_Cmd_Failed
);
4325 sd_card
->sd_lock_status
&= ~(SD_UNLOCK_POW_ON
| SD_SDR_RST
);
4331 if (lock_cmd_fail
) {
4332 scsi_set_resid(srb
, 0);
4333 set_sense_type(chip
, lun
, SENSE_TYPE_NO_SENSE
);
4334 TRACE_RET(chip
, TRANSPORT_FAILED
);
4336 #endif /* SUPPORT_SD_LOCK */
4338 scsi_set_resid(srb
, 0);
4339 return TRANSPORT_GOOD
;
4341 SD_Execute_Write_Cmd_Failed
:
4342 sd_card
->pre_cmd_err
= 1;
4343 set_sense_type(chip
, lun
, SENSE_TYPE_NO_SENSE
);
4345 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_WRITE_ERR
);
4347 release_sd_card(chip
);
4348 do_reset_sd_card(chip
);
4349 if (!(chip
->card_ready
& SD_CARD
))
4350 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_NOT_PRESENT
);
4352 TRACE_RET(chip
, TRANSPORT_FAILED
);
4355 int sd_get_cmd_rsp(struct scsi_cmnd
*srb
, struct rtsx_chip
*chip
)
4357 struct sd_info
*sd_card
= &(chip
->sd_card
);
4358 unsigned int lun
= SCSI_LUN(srb
);
4362 if (!sd_card
->sd_pass_thru_en
) {
4363 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
4364 TRACE_RET(chip
, TRANSPORT_FAILED
);
4367 if (sd_card
->pre_cmd_err
) {
4368 sd_card
->pre_cmd_err
= 0;
4369 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_CHANGE
);
4370 TRACE_RET(chip
, TRANSPORT_FAILED
);
4373 data_len
= ((u16
)srb
->cmnd
[7] << 8) | srb
->cmnd
[8];
4375 if (sd_card
->last_rsp_type
== SD_RSP_TYPE_R0
) {
4376 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
4377 TRACE_RET(chip
, TRANSPORT_FAILED
);
4378 } else if (sd_card
->last_rsp_type
== SD_RSP_TYPE_R2
) {
4379 count
= (data_len
< 17) ? data_len
: 17;
4381 count
= (data_len
< 6) ? data_len
: 6;
4383 rtsx_stor_set_xfer_buf(sd_card
->rsp
, count
, srb
);
4385 RTSX_DEBUGP("Response length: %d\n", data_len
);
4386 RTSX_DEBUGP("Response: 0x%x 0x%x 0x%x 0x%x\n", sd_card
->rsp
[0],
4387 sd_card
->rsp
[1], sd_card
->rsp
[2], sd_card
->rsp
[3]);
4389 scsi_set_resid(srb
, 0);
4390 return TRANSPORT_GOOD
;
4393 int sd_hw_rst(struct scsi_cmnd
*srb
, struct rtsx_chip
*chip
)
4395 struct sd_info
*sd_card
= &(chip
->sd_card
);
4396 unsigned int lun
= SCSI_LUN(srb
);
4399 if (!sd_card
->sd_pass_thru_en
) {
4400 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
4401 TRACE_RET(chip
, TRANSPORT_FAILED
);
4404 if (sd_card
->pre_cmd_err
) {
4405 sd_card
->pre_cmd_err
= 0;
4406 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_CHANGE
);
4407 TRACE_RET(chip
, TRANSPORT_FAILED
);
4410 if ((0x53 != srb
->cmnd
[2]) || (0x44 != srb
->cmnd
[3]) ||
4411 (0x20 != srb
->cmnd
[4]) || (0x43 != srb
->cmnd
[5]) ||
4412 (0x61 != srb
->cmnd
[6]) || (0x72 != srb
->cmnd
[7]) ||
4413 (0x64 != srb
->cmnd
[8])) {
4414 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
4415 TRACE_RET(chip
, TRANSPORT_FAILED
);
4418 switch (srb
->cmnd
[1] & 0x0F) {
4420 #ifdef SUPPORT_SD_LOCK
4421 if (0x64 == srb
->cmnd
[9])
4422 sd_card
->sd_lock_status
|= SD_SDR_RST
;
4424 retval
= reset_sd_card(chip
);
4425 if (retval
!= STATUS_SUCCESS
) {
4426 #ifdef SUPPORT_SD_LOCK
4427 sd_card
->sd_lock_status
&= ~SD_SDR_RST
;
4429 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_NOT_PRESENT
);
4430 sd_card
->pre_cmd_err
= 1;
4431 TRACE_RET(chip
, TRANSPORT_FAILED
);
4433 #ifdef SUPPORT_SD_LOCK
4434 sd_card
->sd_lock_status
&= ~SD_SDR_RST
;
4439 retval
= soft_reset_sd_card(chip
);
4440 if (retval
!= STATUS_SUCCESS
) {
4441 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_NOT_PRESENT
);
4442 sd_card
->pre_cmd_err
= 1;
4443 TRACE_RET(chip
, TRANSPORT_FAILED
);
4448 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
4449 TRACE_RET(chip
, TRANSPORT_FAILED
);
4452 scsi_set_resid(srb
, 0);
4453 return TRANSPORT_GOOD
;
4457 void sd_cleanup_work(struct rtsx_chip
*chip
)
4459 struct sd_info
*sd_card
= &(chip
->sd_card
);
4461 if (sd_card
->seq_mode
) {
4462 RTSX_DEBUGP("SD: stop transmission\n");
4463 sd_stop_seq_mode(chip
);
4464 sd_card
->cleanup_counter
= 0;
4468 int sd_power_off_card3v3(struct rtsx_chip
*chip
)
4472 retval
= disable_card_clock(chip
, SD_CARD
);
4473 if (retval
!= STATUS_SUCCESS
)
4474 TRACE_RET(chip
, STATUS_FAIL
);
4476 RTSX_WRITE_REG(chip
, CARD_OE
, SD_OUTPUT_EN
, 0);
4478 if (!chip
->ft2_fast_mode
) {
4479 retval
= card_power_off(chip
, SD_CARD
);
4480 if (retval
!= STATUS_SUCCESS
)
4481 TRACE_RET(chip
, STATUS_FAIL
);
4486 if (chip
->asic_code
) {
4487 retval
= sd_pull_ctl_disable(chip
);
4488 if (retval
!= STATUS_SUCCESS
)
4489 TRACE_RET(chip
, STATUS_FAIL
);
4491 RTSX_WRITE_REG(chip
, FPGA_PULL_CTL
,
4492 FPGA_SD_PULL_CTL_BIT
| 0x20, FPGA_SD_PULL_CTL_BIT
);
4495 return STATUS_SUCCESS
;
4498 int release_sd_card(struct rtsx_chip
*chip
)
4500 struct sd_info
*sd_card
= &(chip
->sd_card
);
4503 RTSX_DEBUGP("release_sd_card\n");
4505 chip
->card_ready
&= ~SD_CARD
;
4506 chip
->card_fail
&= ~SD_CARD
;
4507 chip
->card_wp
&= ~SD_CARD
;
4512 #ifdef SUPPORT_SD_LOCK
4513 sd_card
->sd_lock_status
= 0;
4514 sd_card
->sd_erase_status
= 0;
4517 memset(sd_card
->raw_csd
, 0, 16);
4518 memset(sd_card
->raw_scr
, 0, 8);
4520 retval
= sd_power_off_card3v3(chip
);
4521 if (retval
!= STATUS_SUCCESS
)
4522 TRACE_RET(chip
, STATUS_FAIL
);
4524 return STATUS_SUCCESS
;