1 /* Driver for Realtek RTS51xx USB card reader
3 * Copyright(c) 2009 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 * wwang (wei_wang@realsil.com.cn)
20 * No. 450, Shenhu Road, Suzhou Industry Park, Suzhou, China
22 * Edwin Rong (edwin_rong@realsil.com.cn)
23 * No. 450, Shenhu Road, Suzhou Industry Park, Suzhou, China
26 #include <linux/blkdev.h>
27 #include <linux/kthread.h>
28 #include <linux/sched.h>
33 #include "rts51x_transport.h"
34 #include "rts51x_scsi.h"
35 #include "rts51x_card.h"
38 static inline void sd_set_reset_fail(struct rts51x_chip
*chip
, u8 err_code
)
40 struct sd_info
*sd_card
= &(chip
->sd_card
);
42 sd_card
->sd_reset_fail
|= err_code
;
45 static inline void sd_clear_reset_fail(struct rts51x_chip
*chip
)
47 struct sd_info
*sd_card
= &(chip
->sd_card
);
49 sd_card
->sd_reset_fail
= 0;
52 static inline int sd_check_reset_fail(struct rts51x_chip
*chip
, u8 err_code
)
54 struct sd_info
*sd_card
= &(chip
->sd_card
);
56 return sd_card
->sd_reset_fail
& err_code
;
59 static inline void sd_set_err_code(struct rts51x_chip
*chip
, u8 err_code
)
61 struct sd_info
*sd_card
= &(chip
->sd_card
);
63 sd_card
->err_code
|= err_code
;
66 static inline void sd_clr_err_code(struct rts51x_chip
*chip
)
68 struct sd_info
*sd_card
= &(chip
->sd_card
);
70 sd_card
->err_code
= 0;
73 static inline int sd_check_err_code(struct rts51x_chip
*chip
, u8 err_code
)
75 struct sd_info
*sd_card
= &(chip
->sd_card
);
77 return sd_card
->err_code
& err_code
;
80 static int sd_parse_err_code(struct rts51x_chip
*chip
)
82 TRACE_RET(chip
, STATUS_FAIL
);
85 int sd_check_data0_status(struct rts51x_chip
*chip
)
90 retval
= rts51x_ep0_read_register(chip
, SD_BUS_STAT
, &stat
);
91 if (retval
!= STATUS_SUCCESS
)
92 TRACE_RET(chip
, retval
);
93 if (!(stat
& SD_DAT0_STATUS
)) {
94 sd_set_err_code(chip
, SD_BUSY
);
95 TRACE_RET(chip
, STATUS_FAIL
);
98 return STATUS_SUCCESS
;
101 static int sd_send_cmd_get_rsp(struct rts51x_chip
*chip
, u8 cmd_idx
,
102 u32 arg
, u8 rsp_type
, u8
*rsp
, int rsp_len
)
104 struct sd_info
*sd_card
= &(chip
->sd_card
);
112 sd_clr_err_code(chip
);
114 RTS51X_DEBUGP("SD/MMC CMD %d, arg = 0x%08x\n", cmd_idx
, arg
);
116 if (rsp_type
== SD_RSP_TYPE_R1b
)
121 rts51x_init_cmd(chip
);
123 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CMD0
, 0xFF, 0x40 | cmd_idx
);
124 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CMD1
, 0xFF, (u8
) (arg
>> 24));
125 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CMD2
, 0xFF, (u8
) (arg
>> 16));
126 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CMD3
, 0xFF, (u8
) (arg
>> 8));
127 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CMD4
, 0xFF, (u8
) arg
);
129 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CFG2
, 0xFF, rsp_type
);
130 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
, 0x01,
132 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_TRANSFER
, 0xFF,
133 SD_TM_CMD_RSP
| SD_TRANSFER_START
);
134 rts51x_add_cmd(chip
, CHECK_REG_CMD
, SD_TRANSFER
,
135 SD_TRANSFER_END
| SD_STAT_IDLE
,
136 SD_TRANSFER_END
| SD_STAT_IDLE
);
138 rts51x_add_cmd(chip
, READ_REG_CMD
, SD_STAT1
, 0, 0);
140 if (CHECK_USB(chip
, USB_20
)) {
141 if (rsp_type
== SD_RSP_TYPE_R2
) {
142 /* Read data from ping-pong buffer */
143 for (reg_addr
= PPBUF_BASE2
;
144 reg_addr
< PPBUF_BASE2
+ 16; reg_addr
++) {
145 rts51x_add_cmd(chip
, READ_REG_CMD
, reg_addr
, 0,
149 } else if (rsp_type
!= SD_RSP_TYPE_R0
) {
150 /* Read data from SD_CMDx registers */
151 for (reg_addr
= SD_CMD0
; reg_addr
<= SD_CMD4
;
153 rts51x_add_cmd(chip
, READ_REG_CMD
, reg_addr
, 0,
164 retval
= rts51x_send_cmd(chip
, MODE_CR
, 100);
165 if (retval
!= STATUS_SUCCESS
)
166 TRACE_RET(chip
, retval
);
168 retval
= rts51x_get_rsp(chip
, len
, timeout
);
170 if (CHECK_SD_TRANS_FAIL(chip
, retval
)) {
173 rts51x_ep0_read_register(chip
, SD_STAT1
, &val
);
174 RTS51X_DEBUGP("SD_STAT1: 0x%x\n", val
);
176 rts51x_ep0_read_register(chip
, SD_STAT2
, &val
);
177 RTS51X_DEBUGP("SD_STAT2: 0x%x\n", val
);
179 if (val
& SD_RSP_80CLK_TIMEOUT
)
180 sd_set_err_code(chip
, SD_RSP_TIMEOUT
);
182 rts51x_ep0_read_register(chip
, SD_BUS_STAT
, &val
);
183 RTS51X_DEBUGP("SD_BUS_STAT: 0x%x\n", val
);
185 if (retval
== STATUS_TIMEDOUT
) {
186 if (rsp_type
& SD_WAIT_BUSY_END
) {
187 retval
= sd_check_data0_status(chip
);
188 if (retval
!= STATUS_SUCCESS
)
189 TRACE_RET(chip
, retval
);
191 sd_set_err_code(chip
, SD_TO_ERR
);
194 rts51x_clear_sd_error(chip
);
196 TRACE_RET(chip
, STATUS_FAIL
);
199 if (rsp_type
== SD_RSP_TYPE_R0
)
200 return STATUS_SUCCESS
;
202 if (CHECK_USB(chip
, USB_20
)) {
203 rts51x_read_rsp_buf(chip
, 2, buf
, len
- 2);
205 if (rsp_type
== SD_RSP_TYPE_R2
) {
206 reg_addr
= PPBUF_BASE2
;
212 retval
= rts51x_seq_read_register(chip
, reg_addr
,
213 (unsigned short)len
, buf
);
214 if (retval
!= STATUS_SUCCESS
)
215 TRACE_RET(chip
, retval
);
217 stat
= chip
->rsp_buf
[1];
219 /* Check (Start,Transmission) bit of Response */
220 if ((buf
[0] & 0xC0) != 0) {
221 sd_set_err_code(chip
, SD_STS_ERR
);
222 TRACE_RET(chip
, STATUS_FAIL
);
225 if (!(rsp_type
& SD_NO_CHECK_CRC7
)) {
226 if (stat
& SD_CRC7_ERR
) {
227 if (cmd_idx
== WRITE_MULTIPLE_BLOCK
) {
228 sd_set_err_code(chip
, SD_CRC_ERR
);
229 TRACE_RET(chip
, STATUS_FAIL
);
231 if (rty_cnt
< SD_MAX_RETRY_COUNT
) {
236 sd_set_err_code(chip
, SD_CRC_ERR
);
237 TRACE_RET(chip
, STATUS_FAIL
);
242 if ((rsp_type
== SD_RSP_TYPE_R1
) || (rsp_type
== SD_RSP_TYPE_R1b
)) {
243 if ((cmd_idx
!= SEND_RELATIVE_ADDR
)
244 && (cmd_idx
!= SEND_IF_COND
)) {
245 if (cmd_idx
!= STOP_TRANSMISSION
) {
247 TRACE_RET(chip
, STATUS_FAIL
);
249 #ifdef SUPPORT_SD_LOCK
250 /* exclude bit25 CARD_IS_LOCKED */
255 RTS51X_DEBUGP("buf[1]: 0x%02x\n", buf
[1]);
256 TRACE_RET(chip
, STATUS_FAIL
);
259 RTS51X_DEBUGP("buf[2]: 0x%02x\n", buf
[2]);
260 TRACE_RET(chip
, STATUS_FAIL
);
263 RTS51X_DEBUGP("buf[3]: 0x%02x\n", buf
[3]);
264 TRACE_RET(chip
, STATUS_FAIL
);
267 /* Get "READY_FOR_DATA" bit */
268 sd_card
->sd_data_buf_ready
= 1;
270 sd_card
->sd_data_buf_ready
= 0;
276 memcpy(rsp
, buf
, rsp_len
);
278 return STATUS_SUCCESS
;
281 static inline void sd_print_debug_reg(struct rts51x_chip
*chip
)
283 #ifdef CONFIG_RTS5139_DEBUG
286 rts51x_ep0_read_register(chip
, SD_STAT1
, &val
);
287 RTS51X_DEBUGP("SD_STAT1: 0x%x\n", val
);
288 rts51x_ep0_read_register(chip
, SD_STAT2
, &val
);
289 RTS51X_DEBUGP("SD_STAT2: 0x%x\n", val
);
290 rts51x_ep0_read_register(chip
, SD_BUS_STAT
, &val
);
291 RTS51X_DEBUGP("SD_BUS_STAT: 0x%x\n", val
);
295 int sd_read_data(struct rts51x_chip
*chip
, u8 trans_mode
, u8
*cmd
, int cmd_len
,
296 u16 byte_cnt
, u16 blk_cnt
, u8 bus_width
, u8
*buf
, int buf_len
,
299 struct sd_info
*sd_card
= &(chip
->sd_card
);
303 sd_clr_err_code(chip
);
309 /* This function can't read data more than one page */
310 TRACE_RET(chip
, STATUS_FAIL
);
312 rts51x_init_cmd(chip
);
315 RTS51X_DEBUGP("SD/MMC CMD %d\n", cmd
[0] - 0x40);
316 for (i
= 0; i
< (cmd_len
< 6 ? cmd_len
: 6); i
++) {
317 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CMD0
+ i
, 0xFF,
321 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_BYTE_CNT_L
, 0xFF, (u8
) byte_cnt
);
322 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_BYTE_CNT_H
, 0xFF,
323 (u8
) (byte_cnt
>> 8));
324 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_BLOCK_CNT_L
, 0xFF, (u8
) blk_cnt
);
325 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_BLOCK_CNT_H
, 0xFF,
326 (u8
) (blk_cnt
>> 8));
328 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CFG1
, 0x03, bus_width
);
330 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CFG2
, 0xFF,
331 SD_CALCULATE_CRC7
| SD_CHECK_CRC16
| SD_NO_WAIT_BUSY_END
332 | SD_CHECK_CRC7
| SD_RSP_LEN_6
);
333 if (trans_mode
!= SD_TM_AUTO_TUNING
) {
334 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
, 0x01,
337 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_TRANSFER
, 0xFF,
338 trans_mode
| SD_TRANSFER_START
);
339 rts51x_add_cmd(chip
, CHECK_REG_CMD
, SD_TRANSFER
, SD_TRANSFER_END
,
342 retval
= rts51x_send_cmd(chip
, MODE_CR
, 100);
343 if (retval
!= STATUS_SUCCESS
)
344 TRACE_RET(chip
, retval
);
346 retval
= rts51x_get_rsp(chip
, 1, timeout
);
348 if (CHECK_SD_TRANS_FAIL(chip
, retval
)) {
349 sd_print_debug_reg(chip
);
350 if (retval
== STATUS_TIMEDOUT
) {
351 sd_send_cmd_get_rsp(chip
, SEND_STATUS
, sd_card
->sd_addr
,
352 SD_RSP_TYPE_R1
, NULL
, 0);
355 TRACE_RET(chip
, STATUS_FAIL
);
358 if (buf
&& buf_len
) {
359 retval
= rts51x_read_ppbuf(chip
, buf
, buf_len
);
360 if (retval
!= STATUS_SUCCESS
)
361 TRACE_RET(chip
, retval
);
364 return STATUS_SUCCESS
;
367 static int sd_write_data(struct rts51x_chip
*chip
, u8 trans_mode
,
368 u8
*cmd
, int cmd_len
, u16 byte_cnt
, u16 blk_cnt
,
369 u8 bus_width
, u8
*buf
, int buf_len
, int timeout
)
371 struct sd_info
*sd_card
= &(chip
->sd_card
);
375 sd_clr_err_code(chip
);
380 /* This function can't write data more than one page */
382 TRACE_RET(chip
, STATUS_FAIL
);
384 if (buf
&& buf_len
) {
385 retval
= rts51x_write_ppbuf(chip
, buf
, buf_len
);
386 if (retval
!= STATUS_SUCCESS
)
387 TRACE_RET(chip
, retval
);
390 rts51x_init_cmd(chip
);
393 RTS51X_DEBUGP("SD/MMC CMD %d\n", cmd
[0] - 0x40);
394 for (i
= 0; i
< (cmd_len
< 6 ? cmd_len
: 6); i
++) {
395 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CMD0
+ i
, 0xFF,
399 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_BYTE_CNT_L
, 0xFF, (u8
) byte_cnt
);
400 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_BYTE_CNT_H
, 0xFF,
401 (u8
) (byte_cnt
>> 8));
402 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_BLOCK_CNT_L
, 0xFF, (u8
) blk_cnt
);
403 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_BLOCK_CNT_H
, 0xFF,
404 (u8
) (blk_cnt
>> 8));
406 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CFG1
, 0x03, bus_width
);
409 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CFG2
, 0xFF,
410 SD_CALCULATE_CRC7
| SD_CHECK_CRC16
|
411 SD_WAIT_BUSY_END
| SD_CHECK_CRC7
| SD_RSP_LEN_6
);
414 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CFG2
, 0xFF,
415 SD_CALCULATE_CRC7
| SD_CHECK_CRC16
|
416 SD_NO_WAIT_BUSY_END
| SD_CHECK_CRC7
|
420 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_TRANSFER
, 0xFF,
421 trans_mode
| SD_TRANSFER_START
);
422 rts51x_add_cmd(chip
, CHECK_REG_CMD
, SD_TRANSFER
, SD_TRANSFER_END
,
425 retval
= rts51x_send_cmd(chip
, MODE_CR
, 100);
426 if (retval
!= STATUS_SUCCESS
)
427 TRACE_RET(chip
, retval
);
429 retval
= rts51x_get_rsp(chip
, 1, timeout
);
431 if (CHECK_SD_TRANS_FAIL(chip
, retval
)) {
432 sd_print_debug_reg(chip
);
434 if (retval
== STATUS_TIMEDOUT
)
435 sd_send_cmd_get_rsp(chip
, SEND_STATUS
, sd_card
->sd_addr
,
436 SD_RSP_TYPE_R1
, NULL
, 0);
438 TRACE_RET(chip
, STATUS_FAIL
);
441 return STATUS_SUCCESS
;
444 static int sd_check_csd(struct rts51x_chip
*chip
, char check_wp
)
446 struct sd_info
*sd_card
= &(chip
->sd_card
);
449 u8 csd_ver
, trans_speed
;
452 for (i
= 0; i
< 6; i
++) {
453 if (monitor_card_cd(chip
, SD_CARD
) == CD_NOT_EXIST
) {
454 sd_set_reset_fail(chip
, SD_RESET_FAIL
);
455 TRACE_RET(chip
, STATUS_FAIL
);
459 sd_send_cmd_get_rsp(chip
, SEND_CSD
, sd_card
->sd_addr
,
460 SD_RSP_TYPE_R2
, rsp
, 16);
461 if (retval
== STATUS_SUCCESS
)
466 TRACE_RET(chip
, STATUS_FAIL
);
467 memcpy(sd_card
->raw_csd
, rsp
+ 1, 15);
469 RTS51X_READ_REG(chip
, SD_CMD5
, sd_card
->raw_csd
+ 15);
471 RTS51X_DEBUGP("CSD Response:\n");
472 RTS51X_DUMP(rsp
, 16);
474 /* Get CSD Version */
475 csd_ver
= (rsp
[1] & 0xc0) >> 6;
476 RTS51X_DEBUGP("csd_ver = %d\n", csd_ver
);
478 trans_speed
= rsp
[4];
479 if ((trans_speed
& 0x07) == 0x02) { /* 10Mbits/s */
480 if ((trans_speed
& 0xf8) >= 0x30) { /* >25Mbits/s */
482 sd_card
->sd_clock
= 46;
484 sd_card
->sd_clock
= CLK_50
;
485 } else if ((trans_speed
& 0xf8) == 0x28) { /* 20Mbits/s */
487 sd_card
->sd_clock
= 39;
489 sd_card
->sd_clock
= CLK_40
;
490 } else if ((trans_speed
& 0xf8) == 0x20) { /* 15Mbits/s */
492 sd_card
->sd_clock
= 29;
494 sd_card
->sd_clock
= CLK_30
;
495 } else if ((trans_speed
& 0xf8) >= 0x10) { /* 12Mbits/s */
497 sd_card
->sd_clock
= 23;
499 sd_card
->sd_clock
= CLK_20
;
500 } else if ((trans_speed
& 0x08) >= 0x08) { /* 10Mbits/s */
502 sd_card
->sd_clock
= 19;
504 sd_card
->sd_clock
= CLK_20
;
506 /*If this ,then slow card will use 30M clock */
507 /* TRACE_RET(chip, STATUS_FAIL); */
511 TRACE_RET(chip, STATUS_FAIL);
513 if (CHK_MMC_SECTOR_MODE(sd_card
)) {
514 sd_card
->capacity
= 0;
516 /* For High-Capacity Card, CSD_STRUCTURE always be "0x1" */
517 if ((!CHK_SD_HCXC(sd_card
)) || (csd_ver
== 0)) {
518 /* Calculate total sector according to C_SIZE,
519 * C_SIZE_MULT & READ_BL_LEN */
520 u8 blk_size
, c_size_mult
;
522 /* Get READ_BL_LEN */
523 blk_size
= rsp
[6] & 0x0F;
525 c_size
= ((u16
) (rsp
[7] & 0x03) << 10)
526 + ((u16
) rsp
[8] << 2)
527 + ((u16
) (rsp
[9] & 0xC0) >> 6);
529 c_size_mult
= (u8
) ((rsp
[10] & 0x03) << 1);
530 c_size_mult
+= (rsp
[11] & 0x80) >> 7;
531 /* Calculate total Capacity */
533 (((u32
) (c_size
+ 1)) *
534 (1 << (c_size_mult
+ 2))) << (blk_size
- 9);
536 /* High Capacity Card and Use CSD2.0 Version */
537 u32 total_sector
= 0;
538 total_sector
= (((u32
) rsp
[8] & 0x3f) << 16) |
539 ((u32
) rsp
[9] << 8) | (u32
) rsp
[10];
540 /* Total Capacity= (C_SIZE+1) *
541 * 512K Byte = (C_SIZE+1)K Sector,1K = 1024 Bytes */
542 sd_card
->capacity
= (total_sector
+ 1) << 10;
546 /* We need check Write-Protected Status by Field PERM WP or TEMP WP */
549 chip
->card_wp
|= SD_CARD
;
550 RTS51X_DEBUGP("CSD WP Status: 0x%x\n", rsp
[15]);
553 return STATUS_SUCCESS
;
556 static int sd_set_sample_push_timing(struct rts51x_chip
*chip
)
558 struct sd_info
*sd_card
= &(chip
->sd_card
);
561 rts51x_init_cmd(chip
);
563 if (CHK_SD_SDR104(sd_card
) || CHK_SD_SDR50(sd_card
)) {
564 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CFG1
,
565 0x0C | SD_ASYNC_FIFO_RST
,
566 SD_30_MODE
| SD_ASYNC_FIFO_RST
);
567 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_CLK_SOURCE
, 0xFF,
568 CRC_VAR_CLK0
| SD30_FIX_CLK
| SAMPLE_VAR_CLK1
);
569 } else if (CHK_SD_DDR50(sd_card
) || CHK_MMC_DDR52(sd_card
)) {
570 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CFG1
,
571 0x0C | SD_ASYNC_FIFO_RST
,
572 SD_DDR_MODE
| SD_ASYNC_FIFO_RST
);
573 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_CLK_SOURCE
, 0xFF,
574 CRC_VAR_CLK0
| SD30_FIX_CLK
| SAMPLE_VAR_CLK1
);
575 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_PUSH_POINT_CTL
,
576 DDR_VAR_TX_CMD_DAT
, DDR_VAR_TX_CMD_DAT
);
577 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_SAMPLE_POINT_CTL
,
578 DDR_VAR_RX_DAT
| DDR_VAR_RX_CMD
,
579 DDR_VAR_RX_DAT
| DDR_VAR_RX_CMD
);
583 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CFG1
, 0x0C, SD_20_MODE
);
584 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_CLK_SOURCE
, 0xFF,
585 CRC_FIX_CLK
| SD30_VAR_CLK0
| SAMPLE_VAR_CLK1
);
587 if ((chip
->option
.sd_ctl
& SD_PUSH_POINT_CTL_MASK
) ==
588 SD_PUSH_POINT_AUTO
) {
589 val
= SD20_TX_NEG_EDGE
;
590 } else if ((chip
->option
.sd_ctl
& SD_PUSH_POINT_CTL_MASK
) ==
591 SD_PUSH_POINT_DELAY
) {
592 val
= SD20_TX_14_AHEAD
;
594 val
= SD20_TX_NEG_EDGE
;
596 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_PUSH_POINT_CTL
,
597 SD20_TX_SEL_MASK
, val
);
599 if ((chip
->option
.sd_ctl
& SD_SAMPLE_POINT_CTL_MASK
) ==
600 SD_SAMPLE_POINT_AUTO
) {
601 if (chip
->asic_code
) {
602 if (CHK_SD_HS(sd_card
) || CHK_MMC_52M(sd_card
))
603 val
= SD20_RX_14_DELAY
;
605 val
= SD20_RX_POS_EDGE
;
607 val
= SD20_RX_14_DELAY
;
609 } else if ((chip
->option
.sd_ctl
& SD_SAMPLE_POINT_CTL_MASK
) ==
610 SD_SAMPLE_POINT_DELAY
) {
611 val
= SD20_RX_14_DELAY
;
613 val
= SD20_RX_POS_EDGE
;
615 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_SAMPLE_POINT_CTL
,
616 SD20_RX_SEL_MASK
, val
);
619 if (CHK_MMC_DDR52(sd_card
) && CHK_MMC_8BIT(sd_card
)) {
620 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_DMA1_CTL
,
621 EXTEND_DMA1_ASYNC_SIGNAL
, 0);
624 retval
= rts51x_send_cmd(chip
, MODE_C
, 100);
625 if (retval
!= STATUS_SUCCESS
)
626 TRACE_RET(chip
, retval
);
628 return STATUS_SUCCESS
;
631 static void sd_choose_proper_clock(struct rts51x_chip
*chip
)
633 struct sd_info
*sd_card
= &(chip
->sd_card
);
635 if (CHK_SD_SDR104(sd_card
)) {
637 sd_card
->sd_clock
= chip
->option
.asic_sd_sdr104_clk
;
639 sd_card
->sd_clock
= chip
->option
.fpga_sd_sdr104_clk
;
640 } else if (CHK_SD_DDR50(sd_card
)) {
642 sd_card
->sd_clock
= chip
->option
.asic_sd_ddr50_clk
;
644 sd_card
->sd_clock
= chip
->option
.fpga_sd_ddr50_clk
;
645 } else if (CHK_SD_SDR50(sd_card
)) {
647 sd_card
->sd_clock
= chip
->option
.asic_sd_sdr50_clk
;
649 sd_card
->sd_clock
= chip
->option
.fpga_sd_sdr50_clk
;
650 } else if (CHK_SD_HS(sd_card
)) {
652 sd_card
->sd_clock
= chip
->option
.asic_sd_hs_clk
;
654 sd_card
->sd_clock
= chip
->option
.fpga_sd_hs_clk
;
655 } else if (CHK_MMC_52M(sd_card
) || CHK_MMC_DDR52(sd_card
)) {
657 sd_card
->sd_clock
= chip
->option
.asic_mmc_52m_clk
;
659 sd_card
->sd_clock
= chip
->option
.fpga_mmc_52m_clk
;
660 } else if (CHK_MMC_26M(sd_card
)) {
661 if (chip
->asic_code
) {
662 sd_card
->sd_clock
= 46;
663 RTS51X_DEBUGP("Set MMC clock to 22.5MHz\n");
665 sd_card
->sd_clock
= CLK_50
;
670 static int sd_set_init_para(struct rts51x_chip
*chip
)
672 struct sd_info
*sd_card
= &(chip
->sd_card
);
675 retval
= sd_set_sample_push_timing(chip
);
676 if (retval
!= STATUS_SUCCESS
)
677 TRACE_RET(chip
, retval
);
679 sd_choose_proper_clock(chip
);
681 retval
= switch_clock(chip
, sd_card
->sd_clock
);
682 if (retval
!= STATUS_SUCCESS
)
683 TRACE_RET(chip
, retval
);
685 return STATUS_SUCCESS
;
688 int sd_select_card(struct rts51x_chip
*chip
, int select
)
690 struct sd_info
*sd_card
= &(chip
->sd_card
);
692 u8 cmd_idx
, cmd_type
;
696 cmd_idx
= SELECT_CARD
;
697 cmd_type
= SD_RSP_TYPE_R1
;
698 addr
= sd_card
->sd_addr
;
700 cmd_idx
= DESELECT_CARD
;
701 cmd_type
= SD_RSP_TYPE_R0
;
705 retval
= sd_send_cmd_get_rsp(chip
, cmd_idx
, addr
, cmd_type
, NULL
, 0);
706 if (retval
!= STATUS_SUCCESS
)
707 TRACE_RET(chip
, retval
);
709 return STATUS_SUCCESS
;
712 #ifdef SUPPORT_SD_LOCK
713 int sd_update_lock_status(struct rts51x_chip
*chip
)
715 struct sd_info
*sd_card
= &(chip
->sd_card
);
720 sd_send_cmd_get_rsp(chip
, SEND_STATUS
, sd_card
->sd_addr
,
721 SD_RSP_TYPE_R1
, rsp
, 5);
722 if (retval
!= STATUS_SUCCESS
)
723 TRACE_RET(chip
, STATUS_FAIL
);
726 sd_card
->sd_lock_status
|= SD_LOCKED
;
728 sd_card
->sd_lock_status
&= ~SD_LOCKED
;
730 RTS51X_DEBUGP("sd_card->sd_lock_status = 0x%x\n",
731 sd_card
->sd_lock_status
);
734 /* LOCK_UNLOCK_FAILED */
735 TRACE_RET(chip
, STATUS_FAIL
);
738 return STATUS_SUCCESS
;
742 int sd_wait_currentstate_dataready(struct rts51x_chip
*chip
, u8 statechk
,
743 u8 rdychk
, u16 pollingcnt
)
745 struct sd_info
*sd_card
= &(chip
->sd_card
);
750 for (i
= 0; i
< pollingcnt
; i
++) {
753 sd_send_cmd_get_rsp(chip
, SEND_STATUS
, sd_card
->sd_addr
,
754 SD_RSP_TYPE_R1
, rsp
, 5);
755 if (retval
== STATUS_SUCCESS
) {
756 if (((rsp
[3] & 0x1E) == statechk
)
757 && ((rsp
[3] & 0x01) == rdychk
)) {
758 return STATUS_SUCCESS
;
761 rts51x_clear_sd_error(chip
);
762 TRACE_RET(chip
, STATUS_FAIL
);
766 return STATUS_TIMEDOUT
;
769 static int sd_voltage_switch(struct rts51x_chip
*chip
)
774 RTS51X_WRITE_REG(chip
, SD_BUS_STAT
,
775 SD_CLK_TOGGLE_EN
| SD_CLK_FORCE_STOP
,
779 sd_send_cmd_get_rsp(chip
, VOLTAGE_SWITCH
, 0, SD_RSP_TYPE_R1
, NULL
,
781 if (retval
!= STATUS_SUCCESS
)
782 TRACE_RET(chip
, retval
);
784 RTS51X_READ_REG(chip
, SD_BUS_STAT
, &stat
);
785 if (stat
& (SD_CMD_STATUS
| SD_DAT3_STATUS
| SD_DAT2_STATUS
|
786 SD_DAT1_STATUS
| SD_DAT0_STATUS
))
787 TRACE_RET(chip
, STATUS_FAIL
);
789 rts51x_init_cmd(chip
);
790 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_BUS_STAT
, 0xFF,
792 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_PAD_CTL
, SD_IO_USING_1V8
,
795 rts51x_add_cmd(chip
, WRITE_REG_CMD
, LDO_POWER_CFG
,
796 TUNE_SD18_MASK
, TUNE_SD18_1V8
);
797 retval
= rts51x_send_cmd(chip
, MODE_C
, 100);
798 if (retval
!= STATUS_SUCCESS
)
799 TRACE_RET(chip
, retval
);
801 wait_timeout(chip
->option
.D3318_off_delay
);
803 RTS51X_WRITE_REG(chip
, SD_BUS_STAT
, 0xFF, SD_CLK_TOGGLE_EN
);
806 RTS51X_READ_REG(chip
, SD_BUS_STAT
, &stat
);
807 if ((stat
& (SD_CMD_STATUS
| SD_DAT3_STATUS
| SD_DAT2_STATUS
|
808 SD_DAT1_STATUS
| SD_DAT0_STATUS
)) !=
809 (SD_CMD_STATUS
| SD_DAT3_STATUS
| SD_DAT2_STATUS
|
810 SD_DAT1_STATUS
| SD_DAT0_STATUS
)) {
811 rts51x_init_cmd(chip
);
812 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_BUS_STAT
, 0xFF,
814 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_CLK_EN
, 0xFF, 0);
815 rts51x_send_cmd(chip
, MODE_C
, 100);
816 TRACE_RET(chip
, STATUS_FAIL
);
818 RTS51X_WRITE_REG(chip
, SD_BUS_STAT
,
819 SD_CLK_TOGGLE_EN
| SD_CLK_FORCE_STOP
, 0);
821 return STATUS_SUCCESS
;
824 static int sd_change_phase(struct rts51x_chip
*chip
, u8 sample_point
,
827 u16 SD_VP_CTL
, SD_DCMPS_CTL
;
831 RTS51X_DEBUGP("sd_change_phase (sample_point = %d, tune_dir = %d)\n",
832 sample_point
, tune_dir
);
834 if (tune_dir
== TUNE_RX
) {
835 SD_VP_CTL
= SD_VPCLK1_CTL
;
836 SD_DCMPS_CTL
= SD_DCMPS1_CTL
;
838 SD_VP_CTL
= SD_VPCLK0_CTL
;
839 SD_DCMPS_CTL
= SD_DCMPS0_CTL
;
842 if (chip
->asic_code
) {
843 RTS51X_WRITE_REG(chip
, CLK_DIV
, CLK_CHANGE
, CLK_CHANGE
);
844 RTS51X_WRITE_REG(chip
, SD_VP_CTL
, 0x1F, sample_point
);
845 RTS51X_WRITE_REG(chip
, SD_VPCLK0_CTL
, PHASE_NOT_RESET
, 0);
846 RTS51X_WRITE_REG(chip
, SD_VPCLK0_CTL
, PHASE_NOT_RESET
,
848 RTS51X_WRITE_REG(chip
, CLK_DIV
, CLK_CHANGE
, 0);
850 #ifdef CONFIG_RTS5139_DEBUG
851 RTS51X_READ_REG(chip
, SD_VP_CTL
, &val
);
852 RTS51X_DEBUGP("SD_VP_CTL: 0x%x\n", val
);
853 RTS51X_READ_REG(chip
, SD_DCMPS_CTL
, &val
);
854 RTS51X_DEBUGP("SD_DCMPS_CTL: 0x%x\n", val
);
857 RTS51X_WRITE_REG(chip
, CLK_DIV
, CLK_CHANGE
, CLK_CHANGE
);
859 RTS51X_WRITE_REG(chip
, SD_VP_CTL
, 0xFF,
860 PHASE_NOT_RESET
| sample_point
);
863 rts51x_init_cmd(chip
);
864 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_DCMPS_CTL
, DCMPS_CHANGE
,
866 rts51x_add_cmd(chip
, CHECK_REG_CMD
, SD_DCMPS_CTL
,
867 DCMPS_CHANGE_DONE
, DCMPS_CHANGE_DONE
);
868 retval
= rts51x_send_cmd(chip
, MODE_CR
, 100);
869 if (retval
!= STATUS_SUCCESS
)
870 TRACE_GOTO(chip
, Fail
);
872 retval
= rts51x_get_rsp(chip
, 1, 500);
873 if (retval
!= STATUS_SUCCESS
)
874 TRACE_GOTO(chip
, Fail
);
876 val
= chip
->rsp_buf
[0];
877 if (val
& DCMPS_ERROR
)
878 TRACE_GOTO(chip
, Fail
);
879 if ((val
& DCMPS_CURRENT_PHASE
) != sample_point
)
880 TRACE_GOTO(chip
, Fail
);
881 RTS51X_WRITE_REG(chip
, SD_DCMPS_CTL
, DCMPS_CHANGE
, 0);
882 RTS51X_WRITE_REG(chip
, CLK_DIV
, CLK_CHANGE
, 0);
886 RTS51X_WRITE_REG(chip
, SD_CFG1
, SD_ASYNC_FIFO_RST
, 0);
888 return STATUS_SUCCESS
;
891 #ifdef CONFIG_RTS5139_DEBUG
892 rts51x_ep0_read_register(chip
, SD_VP_CTL
, &val
);
893 RTS51X_DEBUGP("SD_VP_CTL: 0x%x\n", val
);
894 rts51x_ep0_read_register(chip
, SD_DCMPS_CTL
, &val
);
895 RTS51X_DEBUGP("SD_DCMPS_CTL: 0x%x\n", val
);
898 RTS51X_WRITE_REG(chip
, SD_DCMPS_CTL
, DCMPS_CHANGE
, 0);
899 RTS51X_WRITE_REG(chip
, SD_VP_CTL
, PHASE_CHANGE
, 0);
905 static int sd_check_spec(struct rts51x_chip
*chip
, u8 bus_width
)
907 struct sd_info
*sd_card
= &(chip
->sd_card
);
912 sd_send_cmd_get_rsp(chip
, APP_CMD
, sd_card
->sd_addr
, SD_RSP_TYPE_R1
,
914 if (retval
!= STATUS_SUCCESS
)
915 TRACE_RET(chip
, STATUS_FAIL
);
917 cmd
[0] = 0x40 | SEND_SCR
;
924 sd_read_data(chip
, SD_TM_NORMAL_READ
, cmd
, 5, 8, 1, bus_width
, buf
,
926 if (retval
!= STATUS_SUCCESS
) {
927 rts51x_clear_sd_error(chip
);
928 TRACE_RET(chip
, retval
);
931 memcpy(sd_card
->raw_scr
, buf
, 8);
933 if ((buf
[0] & 0x0F) == 0)
934 TRACE_RET(chip
, STATUS_FAIL
);
936 return STATUS_SUCCESS
;
939 static int sd_query_switch_result(struct rts51x_chip
*chip
, u8 func_group
,
940 u8 func_to_switch
, u8
*buf
, int buf_len
)
942 u8 support_mask
= 0, query_switch
= 0, switch_busy
= 0;
943 int support_offset
= 0, query_switch_offset
= 0, check_busy_offset
= 0;
945 if (func_group
== SD_FUNC_GROUP_1
) {
946 support_offset
= FUNCTION_GROUP1_SUPPORT_OFFSET
;
947 query_switch_offset
= FUNCTION_GROUP1_QUERY_SWITCH_OFFSET
;
948 check_busy_offset
= FUNCTION_GROUP1_CHECK_BUSY_OFFSET
;
950 switch (func_to_switch
) {
952 support_mask
= HS_SUPPORT_MASK
;
953 query_switch
= HS_QUERY_SWITCH_OK
;
954 switch_busy
= HS_SWITCH_BUSY
;
958 support_mask
= SDR50_SUPPORT_MASK
;
959 query_switch
= SDR50_QUERY_SWITCH_OK
;
960 switch_busy
= SDR50_SWITCH_BUSY
;
964 support_mask
= SDR104_SUPPORT_MASK
;
965 query_switch
= SDR104_QUERY_SWITCH_OK
;
966 switch_busy
= SDR104_SWITCH_BUSY
;
970 support_mask
= DDR50_SUPPORT_MASK
;
971 query_switch
= DDR50_QUERY_SWITCH_OK
;
972 switch_busy
= DDR50_SWITCH_BUSY
;
976 TRACE_RET(chip
, STATUS_FAIL
);
978 } else if (func_group
== SD_FUNC_GROUP_3
) {
979 support_offset
= FUNCTION_GROUP3_SUPPORT_OFFSET
;
980 query_switch_offset
= FUNCTION_GROUP3_QUERY_SWITCH_OFFSET
;
981 check_busy_offset
= FUNCTION_GROUP3_CHECK_BUSY_OFFSET
;
983 switch (func_to_switch
) {
985 support_mask
= DRIVING_TYPE_A_MASK
;
986 query_switch
= TYPE_A_QUERY_SWITCH_OK
;
987 switch_busy
= TYPE_A_SWITCH_BUSY
;
991 support_mask
= DRIVING_TYPE_C_MASK
;
992 query_switch
= TYPE_C_QUERY_SWITCH_OK
;
993 switch_busy
= TYPE_C_SWITCH_BUSY
;
997 support_mask
= DRIVING_TYPE_D_MASK
;
998 query_switch
= TYPE_D_QUERY_SWITCH_OK
;
999 switch_busy
= TYPE_D_SWITCH_BUSY
;
1003 TRACE_RET(chip
, STATUS_FAIL
);
1005 } else if (func_group
== SD_FUNC_GROUP_4
) {
1006 support_offset
= FUNCTION_GROUP4_SUPPORT_OFFSET
;
1007 query_switch_offset
= FUNCTION_GROUP4_QUERY_SWITCH_OFFSET
;
1008 check_busy_offset
= FUNCTION_GROUP4_CHECK_BUSY_OFFSET
;
1010 switch (func_to_switch
) {
1011 case CURRENT_LIMIT_400
:
1012 support_mask
= CURRENT_LIMIT_400_MASK
;
1013 query_switch
= CURRENT_LIMIT_400_QUERY_SWITCH_OK
;
1014 switch_busy
= CURRENT_LIMIT_400_SWITCH_BUSY
;
1017 case CURRENT_LIMIT_600
:
1018 support_mask
= CURRENT_LIMIT_600_MASK
;
1019 query_switch
= CURRENT_LIMIT_600_QUERY_SWITCH_OK
;
1020 switch_busy
= CURRENT_LIMIT_600_SWITCH_BUSY
;
1023 case CURRENT_LIMIT_800
:
1024 support_mask
= CURRENT_LIMIT_800_MASK
;
1025 query_switch
= CURRENT_LIMIT_800_QUERY_SWITCH_OK
;
1026 switch_busy
= CURRENT_LIMIT_800_SWITCH_BUSY
;
1030 TRACE_RET(chip
, STATUS_FAIL
);
1033 TRACE_RET(chip
, STATUS_FAIL
);
1036 if (func_group
== SD_FUNC_GROUP_4
)
1037 buf
[query_switch_offset
] =
1038 (buf
[query_switch_offset
] & 0xf0) >> 4;
1039 if (!(buf
[support_offset
] & support_mask
) ||
1040 ((buf
[query_switch_offset
] & 0x0F) != query_switch
))
1041 TRACE_RET(chip
, STATUS_FAIL
);
1043 if ((buf
[DATA_STRUCTURE_VER_OFFSET
] == 0x01) &&
1044 ((buf
[check_busy_offset
] & switch_busy
) == switch_busy
))
1045 TRACE_RET(chip
, STATUS_FAIL
);
1047 return STATUS_SUCCESS
;
1050 static int sd_check_switch_mode(struct rts51x_chip
*chip
, u8 mode
,
1051 u8 func_group
, u8 func_to_switch
, u8 bus_width
)
1053 struct sd_info
*sd_card
= &(chip
->sd_card
);
1057 RTS51X_DEBUGP("sd_check_switch_mode (mode = %d, func_group = %d,"
1058 "func_to_switch = %d)\n", mode
, func_group
, func_to_switch
);
1060 cmd
[0] = 0x40 | SWITCH
;
1063 if (func_group
== SD_FUNC_GROUP_1
) {
1066 cmd
[4] = 0xF0 + func_to_switch
;
1067 } else if (func_group
== SD_FUNC_GROUP_3
) {
1069 cmd
[3] = 0xF0 + func_to_switch
;
1071 } else if (func_group
== SD_FUNC_GROUP_4
) {
1073 cmd
[3] = 0x0F + (func_to_switch
<< 4);
1076 cmd
[1] = SD_CHECK_MODE
;
1083 sd_read_data(chip
, SD_TM_NORMAL_READ
, cmd
, 5, 64, 1, bus_width
, buf
,
1085 if (retval
!= STATUS_SUCCESS
) {
1086 rts51x_clear_sd_error(chip
);
1087 TRACE_RET(chip
, retval
);
1090 if (func_group
== NO_ARGUMENT
) {
1091 sd_card
->func_group1_mask
= buf
[0x0D];
1092 sd_card
->func_group2_mask
= buf
[0x0B];
1093 sd_card
->func_group3_mask
= buf
[0x09];
1094 sd_card
->func_group4_mask
= buf
[0x07];
1096 RTS51X_DEBUGP("func_group1_mask = 0x%02x\n", buf
[0x0D]);
1097 RTS51X_DEBUGP("func_group2_mask = 0x%02x\n", buf
[0x0B]);
1098 RTS51X_DEBUGP("func_group3_mask = 0x%02x\n", buf
[0x09]);
1099 RTS51X_DEBUGP("func_group4_mask = 0x%02x\n", buf
[0x07]);
1101 if ((buf
[0] == 0) && (buf
[1] == 0))
1102 TRACE_RET(chip
, STATUS_FAIL
);
1104 sd_query_switch_result(chip
, func_group
, func_to_switch
,
1106 if (retval
!= STATUS_SUCCESS
)
1107 TRACE_RET(chip
, retval
);
1110 return STATUS_SUCCESS
;
1113 static int sd_check_switch(struct rts51x_chip
*chip
,
1114 u8 func_group
, u8 func_to_switch
, u8 bus_width
)
1118 int switch_good
= 0;
1120 for (i
= 0; i
< 3; i
++) {
1121 if (monitor_card_cd(chip
, SD_CARD
) == CD_NOT_EXIST
) {
1122 sd_set_reset_fail(chip
, SD_RESET_FAIL
);
1123 TRACE_RET(chip
, STATUS_FAIL
);
1126 retval
= sd_check_switch_mode(chip
, SD_CHECK_MODE
, func_group
,
1127 func_to_switch
, bus_width
);
1128 if (retval
== STATUS_SUCCESS
) {
1131 retval
= sd_check_switch_mode(chip
, SD_SWITCH_MODE
,
1132 func_group
, func_to_switch
, bus_width
);
1133 if (retval
== STATUS_SUCCESS
) {
1138 RTS51X_READ_REG(chip
, SD_STAT1
, &stat
);
1140 if (stat
& SD_CRC16_ERR
) {
1141 RTS51X_DEBUGP("SD CRC16 error when switching"
1143 TRACE_RET(chip
, STATUS_FAIL
);
1151 TRACE_RET(chip
, STATUS_FAIL
);
1153 return STATUS_SUCCESS
;
1156 static int sd_switch_function(struct rts51x_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
, retval
);
1169 sd_card
->func_group1_mask
&= ~(sd_card
->sd_switch_fail
);
1171 for (i
= 0; i
< 4; i
++) {
1172 switch ((u8
) (chip
->option
.sd_speed_prior
>> (i
* 8))) {
1174 if ((sd_card
->func_group1_mask
& DDR50_SUPPORT_MASK
)
1175 && (CHECK_UHS50(chip
)))
1176 func_to_switch
= DDR50_SUPPORT
;
1180 if ((sd_card
->func_group1_mask
& SDR50_SUPPORT_MASK
)
1181 && (CHECK_UHS50(chip
)))
1182 func_to_switch
= SDR50_SUPPORT
;
1186 if (sd_card
->func_group1_mask
& HS_SUPPORT_MASK
)
1187 func_to_switch
= HS_SUPPORT
;
1197 RTS51X_DEBUGP("SD_FUNC_GROUP_1: func_to_switch = 0x%02x",
1200 #ifdef SUPPORT_SD_LOCK
1201 if ((sd_card
->sd_lock_status
& SD_SDR_RST
)
1202 && (DDR50_SUPPORT
== func_to_switch
)
1203 && (sd_card
->func_group1_mask
& SDR50_SUPPORT_MASK
)) {
1204 func_to_switch
= SDR50_SUPPORT
;
1205 RTS51X_DEBUGP("Using SDR50 instead of DDR50 for SD Lock\n");
1209 if (func_to_switch
) {
1211 sd_check_switch(chip
, SD_FUNC_GROUP_1
, func_to_switch
,
1213 if (retval
!= STATUS_SUCCESS
) {
1214 if (func_to_switch
== SDR104_SUPPORT
)
1215 sd_card
->sd_switch_fail
= SDR104_SUPPORT_MASK
;
1216 else if (func_to_switch
== DDR50_SUPPORT
)
1217 sd_card
->sd_switch_fail
= DDR50_SUPPORT_MASK
;
1218 else if (func_to_switch
== SDR50_SUPPORT
)
1219 sd_card
->sd_switch_fail
= SDR50_SUPPORT_MASK
;
1220 else if (func_to_switch
== HS_SUPPORT
)
1221 sd_card
->sd_switch_fail
= HS_SUPPORT_MASK
;
1223 TRACE_RET(chip
, retval
);
1226 if (func_to_switch
== SDR104_SUPPORT
)
1227 SET_SD_SDR104(sd_card
);
1228 else if (func_to_switch
== DDR50_SUPPORT
)
1229 SET_SD_DDR50(sd_card
);
1230 else if (func_to_switch
== SDR50_SUPPORT
)
1231 SET_SD_SDR50(sd_card
);
1236 if (CHK_SD_DDR50(sd_card
))
1237 RTS51X_WRITE_REG(chip
, SD_CFG1
, 0x0C, SD_DDR_MODE
);
1240 if (sd_card
->func_group4_mask
& CURRENT_LIMIT_400_MASK
)
1241 func_to_switch
= CURRENT_LIMIT_400
;
1243 if (func_to_switch
) {
1244 RTS51X_DEBUGP("Try to switch current_limit_400\n");
1246 sd_check_switch(chip
, SD_FUNC_GROUP_4
, func_to_switch
,
1248 RTS51X_DEBUGP("Switch current_limit_400 status: (%d)\n",
1252 return STATUS_SUCCESS
;
1255 static int sd_wait_data_idle(struct rts51x_chip
*chip
)
1257 int retval
= STATUS_TIMEDOUT
;
1261 for (i
= 0; i
< 100; i
++) {
1262 retval
= rts51x_ep0_read_register(chip
, SD_DATA_STATE
, &val
);
1263 if (retval
!= STATUS_SUCCESS
)
1264 TRACE_RET(chip
, STATUS_FAIL
);
1265 if (val
& SD_DATA_IDLE
) {
1266 retval
= STATUS_SUCCESS
;
1271 RTS51X_DEBUGP("SD_DATA_STATE: 0x%02x\n", val
);
1276 static int sd_sdr_tuning_rx_cmd(struct rts51x_chip
*chip
, u8 sample_point
)
1281 retval
= sd_change_phase(chip
, sample_point
, TUNE_RX
);
1282 if (retval
!= STATUS_SUCCESS
)
1283 TRACE_RET(chip
, retval
);
1285 cmd
[0] = 0x40 | SEND_TUNING_PATTERN
;
1291 retval
= sd_read_data(chip
, SD_TM_AUTO_TUNING
,
1292 cmd
, 5, 0x40, 1, SD_BUS_WIDTH_4
, NULL
, 0, 100);
1293 if (retval
!= STATUS_SUCCESS
) {
1294 /* Wait till SD DATA IDLE */
1295 (void)sd_wait_data_idle(chip
);
1297 rts51x_clear_sd_error(chip
);
1298 TRACE_RET(chip
, STATUS_FAIL
);
1301 return STATUS_SUCCESS
;
1304 static int sd_ddr_tuning_rx_cmd(struct rts51x_chip
*chip
, u8 sample_point
)
1306 struct sd_info
*sd_card
= &(chip
->sd_card
);
1310 retval
= sd_change_phase(chip
, sample_point
, TUNE_RX
);
1311 if (retval
!= STATUS_SUCCESS
)
1312 TRACE_RET(chip
, retval
);
1314 RTS51X_DEBUGP("sd ddr tuning rx\n");
1317 sd_send_cmd_get_rsp(chip
, APP_CMD
, sd_card
->sd_addr
, SD_RSP_TYPE_R1
,
1319 if (retval
!= STATUS_SUCCESS
)
1320 TRACE_RET(chip
, retval
);
1322 cmd
[0] = 0x40 | SD_STATUS
;
1328 retval
= sd_read_data(chip
, SD_TM_NORMAL_READ
,
1329 cmd
, 5, 64, 1, SD_BUS_WIDTH_4
, NULL
, 0, 100);
1330 if (retval
!= STATUS_SUCCESS
) {
1331 /* Wait till SD DATA IDLE */
1332 (void)sd_wait_data_idle(chip
);
1334 rts51x_clear_sd_error(chip
);
1335 TRACE_RET(chip
, STATUS_FAIL
);
1338 return STATUS_SUCCESS
;
1341 static int mmc_ddr_tunning_rx_cmd(struct rts51x_chip
*chip
, u8 sample_point
)
1343 struct sd_info
*sd_card
= &(chip
->sd_card
);
1345 u8 cmd
[5], bus_width
;
1347 if (CHK_MMC_8BIT(sd_card
))
1348 bus_width
= SD_BUS_WIDTH_8
;
1349 else if (CHK_MMC_4BIT(sd_card
))
1350 bus_width
= SD_BUS_WIDTH_4
;
1352 bus_width
= SD_BUS_WIDTH_1
;
1354 retval
= sd_change_phase(chip
, sample_point
, TUNE_RX
);
1355 if (retval
!= STATUS_SUCCESS
)
1356 TRACE_RET(chip
, retval
);
1358 RTS51X_DEBUGP("mmc ddr tuning rx\n");
1360 cmd
[0] = 0x40 | SEND_EXT_CSD
;
1366 retval
= sd_read_data(chip
, SD_TM_NORMAL_READ
,
1367 cmd
, 5, 0x200, 1, bus_width
, NULL
, 0, 100);
1368 if (retval
!= STATUS_SUCCESS
) {
1369 /* Wait till SD DATA IDLE */
1370 (void)sd_wait_data_idle(chip
);
1372 rts51x_clear_sd_error(chip
);
1373 TRACE_RET(chip
, STATUS_FAIL
);
1376 return STATUS_SUCCESS
;
1379 static int sd_sdr_tuning_tx_cmd(struct rts51x_chip
*chip
, u8 sample_point
)
1381 struct sd_info
*sd_card
= &(chip
->sd_card
);
1384 retval
= sd_change_phase(chip
, sample_point
, TUNE_TX
);
1385 if (retval
!= STATUS_SUCCESS
)
1386 TRACE_RET(chip
, retval
);
1388 RTS51X_WRITE_REG(chip
, SD_CFG3
, SD_RSP_80CLK_TIMEOUT_EN
,
1389 SD_RSP_80CLK_TIMEOUT_EN
);
1391 retval
= sd_send_cmd_get_rsp(chip
, SEND_STATUS
, sd_card
->sd_addr
,
1392 SD_RSP_TYPE_R1
, NULL
, 0);
1393 if (retval
!= STATUS_SUCCESS
) {
1394 if (sd_check_err_code(chip
, SD_RSP_TIMEOUT
)) {
1395 /* Tunning TX fail */
1396 rts51x_ep0_write_register(chip
, SD_CFG3
,
1397 SD_RSP_80CLK_TIMEOUT_EN
, 0);
1398 TRACE_RET(chip
, STATUS_FAIL
);
1402 RTS51X_WRITE_REG(chip
, SD_CFG3
, SD_RSP_80CLK_TIMEOUT_EN
, 0);
1404 return STATUS_SUCCESS
;
1407 static int sd_ddr_tuning_tx_cmd(struct rts51x_chip
*chip
, u8 sample_point
)
1409 struct sd_info
*sd_card
= &(chip
->sd_card
);
1411 u8 cmd
[5], bus_width
;
1413 retval
= sd_change_phase(chip
, sample_point
, TUNE_TX
);
1414 if (retval
!= STATUS_SUCCESS
)
1415 TRACE_RET(chip
, retval
);
1417 if (CHK_SD(sd_card
)) {
1418 bus_width
= SD_BUS_WIDTH_4
;
1420 if (CHK_MMC_8BIT(sd_card
))
1421 bus_width
= SD_BUS_WIDTH_8
;
1422 else if (CHK_MMC_4BIT(sd_card
))
1423 bus_width
= SD_BUS_WIDTH_4
;
1425 bus_width
= SD_BUS_WIDTH_1
;
1427 retval
= sd_wait_currentstate_dataready(chip
, 0x08, 1, 20);
1428 if (retval
!= STATUS_SUCCESS
)
1429 TRACE_RET(chip
, STATUS_FAIL
);
1431 RTS51X_WRITE_REG(chip
, SD_CFG3
, SD_RSP_80CLK_TIMEOUT_EN
,
1432 SD_RSP_80CLK_TIMEOUT_EN
);
1434 cmd
[0] = 0x40 | PROGRAM_CSD
;
1440 retval
= sd_write_data(chip
, SD_TM_AUTO_WRITE_2
,
1441 cmd
, 5, 16, 1, bus_width
, sd_card
->raw_csd
, 16, 100);
1442 if (retval
!= STATUS_SUCCESS
) {
1443 rts51x_clear_sd_error(chip
);
1444 /* Tunning TX fail */
1445 rts51x_ep0_write_register(chip
, SD_CFG3
,
1446 SD_RSP_80CLK_TIMEOUT_EN
, 0);
1447 TRACE_RET(chip
, STATUS_FAIL
);
1450 RTS51X_WRITE_REG(chip
, SD_CFG3
, SD_RSP_80CLK_TIMEOUT_EN
, 0);
1452 sd_send_cmd_get_rsp(chip
, SEND_STATUS
, sd_card
->sd_addr
, SD_RSP_TYPE_R1
,
1455 return STATUS_SUCCESS
;
1458 static u8
sd_search_final_phase(struct rts51x_chip
*chip
, u32 phase_map
,
1461 struct sd_info
*sd_card
= &(chip
->sd_card
);
1462 struct timing_phase_path path
[MAX_PHASE
+ 1];
1463 int i
, j
, cont_path_cnt
;
1464 int new_block
, max_len
;
1465 u8 final_phase
= 0xFF;
1468 if (phase_map
== 0xffff) {
1469 if (CHK_SD_DDR50(sd_card
)) {
1470 if (tune_dir
== TUNE_TX
)
1471 final_phase
= chip
->option
.ddr50_tx_phase
;
1473 final_phase
= chip
->option
.ddr50_rx_phase
;
1474 RTS51X_DEBUGP("DDR50 tuning dir:%d all pass,"
1475 "so select default phase:0x%x.\n",
1476 tune_dir
, final_phase
);
1478 if (tune_dir
== TUNE_TX
)
1479 final_phase
= chip
->option
.sdr50_tx_phase
;
1481 final_phase
= chip
->option
.sdr50_rx_phase
;
1482 RTS51X_DEBUGP("SDR50 tuning dir:%d all pass,"
1483 "so select default phase:0x%x.\n",
1484 tune_dir
, final_phase
);
1492 for (i
= 0; i
< MAX_PHASE
+ 1; i
++) {
1493 if (phase_map
& (1 << i
)) {
1496 j
= cont_path_cnt
++;
1504 if (cont_path_cnt
) {
1505 int idx
= cont_path_cnt
- 1;
1507 path
[idx
].end
- path
[idx
].start
+ 1;
1509 path
[idx
].start
+ path
[idx
].len
/ 2;
1514 if (cont_path_cnt
== 0) {
1515 RTS51X_DEBUGP("No continuous phase path\n");
1518 int idx
= cont_path_cnt
- 1;
1519 path
[idx
].len
= path
[idx
].end
- path
[idx
].start
+ 1;
1520 path
[idx
].mid
= path
[idx
].start
+ path
[idx
].len
/ 2;
1523 if ((path
[0].start
== 0) &&
1524 (path
[cont_path_cnt
- 1].end
== MAX_PHASE
)) {
1525 path
[0].start
= path
[cont_path_cnt
- 1].start
- MAX_PHASE
- 1;
1526 path
[0].len
+= path
[cont_path_cnt
- 1].len
;
1527 path
[0].mid
= path
[0].start
+ path
[0].len
/ 2;
1528 if (path
[0].mid
< 0)
1529 path
[0].mid
+= MAX_PHASE
+ 1;
1535 for (i
= 0; i
< cont_path_cnt
; i
++) {
1536 if (path
[i
].len
> max_len
) {
1537 max_len
= path
[i
].len
;
1538 final_phase
= (u8
) path
[i
].mid
;
1542 RTS51X_DEBUGP("path[%d].start = %d\n", i
, path
[i
].start
);
1543 RTS51X_DEBUGP("path[%d].end = %d\n", i
, path
[i
].end
);
1544 RTS51X_DEBUGP("path[%d].len = %d\n", i
, path
[i
].len
);
1545 RTS51X_DEBUGP("path[%d].mid = %d\n", i
, path
[i
].mid
);
1546 RTS51X_DEBUGP("\n");
1549 if ((tune_dir
== TUNE_TX
) && (CHK_SD_SDR50(sd_card
))
1550 && chip
->option
.sdr50_phase_sel
) {
1552 int temp_mid
= (max_len
- 6) / 2;
1553 int temp_final_phase
=
1554 path
[final_path_idx
].end
- (max_len
-
1557 if (temp_final_phase
< 0)
1558 final_phase
= temp_final_phase
+ MAX_PHASE
+ 1;
1560 final_phase
= (u8
) temp_final_phase
;
1565 RTS51X_DEBUGP("Final choosen phase: %d\n", final_phase
);
1569 static int sd_tuning_rx(struct rts51x_chip
*chip
)
1571 struct sd_info
*sd_card
= &(chip
->sd_card
);
1574 u32 raw_phase_map
[3], phase_map
;
1576 int (*tuning_cmd
) (struct rts51x_chip
*chip
, u8 sample_point
);
1578 if (CHK_SD(sd_card
)) {
1579 if (CHK_SD_DDR50(sd_card
))
1580 tuning_cmd
= sd_ddr_tuning_rx_cmd
;
1582 tuning_cmd
= sd_sdr_tuning_rx_cmd
;
1584 if (CHK_MMC_DDR52(sd_card
))
1585 tuning_cmd
= mmc_ddr_tunning_rx_cmd
;
1587 TRACE_RET(chip
, STATUS_FAIL
);
1590 for (i
= 0; i
< 3; i
++) {
1591 raw_phase_map
[i
] = 0;
1592 for (j
= MAX_PHASE
; j
>= 0; j
--) {
1593 if (monitor_card_cd(chip
, SD_CARD
) == CD_NOT_EXIST
) {
1594 sd_set_reset_fail(chip
, SD_RESET_FAIL
);
1595 TRACE_RET(chip
, STATUS_FAIL
);
1598 retval
= tuning_cmd(chip
, (u8
) j
);
1599 if (retval
== STATUS_SUCCESS
)
1600 raw_phase_map
[i
] |= 1 << j
;
1602 RTS51X_DEBUGP("Tuning phase %d fail\n", j
);
1606 phase_map
= raw_phase_map
[0] & raw_phase_map
[1] & raw_phase_map
[2];
1607 for (i
= 0; i
< 3; i
++)
1608 RTS51X_DEBUGP("RX raw_phase_map[%d] = 0x%04x\n", i
,
1610 RTS51X_DEBUGP("RX phase_map = 0x%04x\n", phase_map
);
1612 final_phase
= sd_search_final_phase(chip
, phase_map
, TUNE_RX
);
1613 if (final_phase
== 0xFF)
1614 TRACE_RET(chip
, STATUS_FAIL
);
1616 retval
= tuning_cmd(chip
, final_phase
);
1617 if (retval
!= STATUS_SUCCESS
)
1618 TRACE_RET(chip
, retval
);
1620 return STATUS_SUCCESS
;
1623 static int sd_ddr_pre_tuning_tx(struct rts51x_chip
*chip
)
1625 struct sd_info
*sd_card
= &(chip
->sd_card
);
1628 u8 pre_tune_tx_phase
;
1629 u32 pre_tune_phase_map
;
1631 RTS51X_WRITE_REG(chip
, SD_CFG3
, SD_RSP_80CLK_TIMEOUT_EN
,
1632 SD_RSP_80CLK_TIMEOUT_EN
);
1634 pre_tune_tx_phase
= 0xFF;
1635 pre_tune_phase_map
= 0x0000;
1636 for (i
= 0; i
< MAX_PHASE
+ 1; i
++) {
1637 if (monitor_card_cd(chip
, SD_CARD
) == CD_NOT_EXIST
) {
1638 sd_set_reset_fail(chip
, SD_RESET_FAIL
);
1639 TRACE_RET(chip
, STATUS_FAIL
);
1642 retval
= sd_change_phase(chip
, (u8
) i
, TUNE_TX
);
1643 if (retval
!= STATUS_SUCCESS
)
1644 TRACE_RET(chip
, retval
);
1647 sd_send_cmd_get_rsp(chip
, SEND_STATUS
, sd_card
->sd_addr
,
1648 SD_RSP_TYPE_R1
, NULL
, 0);
1649 if ((retval
== STATUS_SUCCESS
)
1650 || !sd_check_err_code(chip
, SD_RSP_TIMEOUT
))
1651 pre_tune_phase_map
|= (u32
) 1 << i
;
1654 RTS51X_WRITE_REG(chip
, SD_CFG3
, SD_RSP_80CLK_TIMEOUT_EN
, 0);
1657 sd_search_final_phase(chip
, pre_tune_phase_map
, TUNE_TX
);
1658 if (pre_tune_tx_phase
== 0xFF)
1659 TRACE_RET(chip
, STATUS_FAIL
);
1661 sd_change_phase(chip
, pre_tune_tx_phase
, TUNE_TX
);
1662 RTS51X_DEBUGP("DDR TX pre tune phase: %d\n", (int)pre_tune_tx_phase
);
1664 return STATUS_SUCCESS
;
1667 static int sd_tuning_tx(struct rts51x_chip
*chip
)
1669 struct sd_info
*sd_card
= &(chip
->sd_card
);
1672 u32 raw_phase_map
[3], phase_map
;
1674 int (*tuning_cmd
) (struct rts51x_chip
*chip
, u8 sample_point
);
1676 if (CHK_SD(sd_card
)) {
1677 if (CHK_SD_DDR50(sd_card
))
1678 tuning_cmd
= sd_ddr_tuning_tx_cmd
;
1680 tuning_cmd
= sd_sdr_tuning_tx_cmd
;
1682 if (CHK_MMC_DDR52(sd_card
))
1683 tuning_cmd
= sd_ddr_tuning_tx_cmd
;
1685 TRACE_RET(chip
, STATUS_FAIL
);
1688 for (i
= 0; i
< 3; i
++) {
1689 raw_phase_map
[i
] = 0;
1690 for (j
= MAX_PHASE
; j
>= 0; j
--) {
1691 if (monitor_card_cd(chip
, SD_CARD
) == CD_NOT_EXIST
) {
1692 sd_set_reset_fail(chip
, SD_RESET_FAIL
);
1693 TRACE_RET(chip
, STATUS_FAIL
);
1696 retval
= tuning_cmd(chip
, (u8
) j
);
1697 if (retval
== STATUS_SUCCESS
)
1698 raw_phase_map
[i
] |= 1 << j
;
1700 RTS51X_DEBUGP("Tuning phase %d fail\n", j
);
1704 phase_map
= raw_phase_map
[0] & raw_phase_map
[1] & raw_phase_map
[2];
1705 for (i
= 0; i
< 3; i
++)
1706 RTS51X_DEBUGP("TX raw_phase_map[%d] = 0x%04x\n", i
,
1708 RTS51X_DEBUGP("TX phase_map = 0x%04x\n", phase_map
);
1710 final_phase
= sd_search_final_phase(chip
, phase_map
, TUNE_TX
);
1711 if (final_phase
== 0xFF)
1712 TRACE_RET(chip
, STATUS_FAIL
);
1714 retval
= tuning_cmd(chip
, final_phase
);
1715 if (retval
!= STATUS_SUCCESS
)
1716 TRACE_RET(chip
, retval
);
1718 return STATUS_SUCCESS
;
1721 static int sd_sdr_tuning(struct rts51x_chip
*chip
)
1725 retval
= sd_tuning_tx(chip
);
1726 if (retval
!= STATUS_SUCCESS
)
1727 TRACE_RET(chip
, retval
);
1729 retval
= sd_tuning_rx(chip
);
1730 if (retval
!= STATUS_SUCCESS
)
1731 TRACE_RET(chip
, retval
);
1733 return STATUS_SUCCESS
;
1736 static int sd_ddr_tuning(struct rts51x_chip
*chip
)
1740 if (!(chip
->option
.sd_ctl
& SD_DDR_TX_PHASE_SET_BY_USER
)) {
1741 retval
= sd_ddr_pre_tuning_tx(chip
);
1742 if (retval
!= STATUS_SUCCESS
)
1743 TRACE_RET(chip
, retval
);
1746 sd_change_phase(chip
, (u8
) chip
->option
.sd_ddr_tx_phase
,
1748 if (retval
!= STATUS_SUCCESS
)
1749 TRACE_RET(chip
, retval
);
1752 retval
= sd_tuning_rx(chip
);
1753 if (retval
!= STATUS_SUCCESS
)
1754 TRACE_RET(chip
, retval
);
1756 if (!(chip
->option
.sd_ctl
& SD_DDR_TX_PHASE_SET_BY_USER
)) {
1757 retval
= sd_tuning_tx(chip
);
1758 if (retval
!= STATUS_SUCCESS
)
1759 TRACE_RET(chip
, retval
);
1762 return STATUS_SUCCESS
;
1765 static int mmc_ddr_tuning(struct rts51x_chip
*chip
)
1769 if (!(chip
->option
.sd_ctl
& MMC_DDR_TX_PHASE_SET_BY_USER
)) {
1770 retval
= sd_ddr_pre_tuning_tx(chip
);
1771 if (retval
!= STATUS_SUCCESS
)
1772 TRACE_RET(chip
, retval
);
1775 sd_change_phase(chip
, (u8
) chip
->option
.mmc_ddr_tx_phase
,
1777 if (retval
!= STATUS_SUCCESS
)
1778 TRACE_RET(chip
, retval
);
1781 retval
= sd_tuning_rx(chip
);
1782 if (retval
!= STATUS_SUCCESS
)
1783 TRACE_RET(chip
, retval
);
1785 if (!(chip
->option
.sd_ctl
& MMC_DDR_TX_PHASE_SET_BY_USER
)) {
1786 retval
= sd_tuning_tx(chip
);
1787 if (retval
!= STATUS_SUCCESS
)
1788 TRACE_RET(chip
, retval
);
1791 return STATUS_SUCCESS
;
1794 int sd_switch_clock(struct rts51x_chip
*chip
)
1796 struct sd_info
*sd_card
= &(chip
->sd_card
);
1800 retval
= rts51x_select_card(chip
, SD_CARD
);
1801 if (retval
!= STATUS_SUCCESS
)
1802 TRACE_RET(chip
, retval
);
1804 if (CHK_SD30_SPEED(sd_card
) || CHK_MMC_DDR52(sd_card
)) {
1805 if (sd_card
->sd_clock
!= chip
->cur_clk
)
1809 retval
= switch_clock(chip
, sd_card
->sd_clock
);
1810 if (retval
!= STATUS_SUCCESS
)
1811 TRACE_RET(chip
, retval
);
1814 if (CHK_SD(sd_card
)) {
1815 if (CHK_SD_DDR50(sd_card
))
1816 retval
= sd_ddr_tuning(chip
);
1818 retval
= sd_sdr_tuning(chip
);
1820 if (CHK_MMC_DDR52(sd_card
))
1821 retval
= mmc_ddr_tuning(chip
);
1824 if (retval
!= STATUS_SUCCESS
)
1825 TRACE_RET(chip
, retval
);
1828 return STATUS_SUCCESS
;
1831 static int sd_prepare_reset(struct rts51x_chip
*chip
)
1833 struct sd_info
*sd_card
= &(chip
->sd_card
);
1836 if (chip
->asic_code
)
1837 sd_card
->sd_clock
= 29;
1839 sd_card
->sd_clock
= CLK_30
;
1842 retval
= sd_set_init_para(chip
);
1843 if (retval
!= STATUS_SUCCESS
)
1844 TRACE_RET(chip
, retval
);
1846 rts51x_init_cmd(chip
);
1848 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CFG1
, 0xFF,
1849 SD_CLK_DIVIDE_128
| SD_20_MODE
| SD_BUS_WIDTH_1
);
1850 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_SAMPLE_POINT_CTL
, 0xFF,
1852 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_PUSH_POINT_CTL
, 0xFF, 0);
1854 retval
= rts51x_send_cmd(chip
, MODE_C
, 100);
1855 if (retval
!= STATUS_SUCCESS
)
1856 TRACE_RET(chip
, retval
);
1858 retval
= rts51x_select_card(chip
, SD_CARD
);
1859 if (retval
!= STATUS_SUCCESS
)
1860 TRACE_RET(chip
, retval
);
1862 return STATUS_SUCCESS
;
1865 static void sd_pull_ctl_disable(struct rts51x_chip
*chip
)
1867 if (CHECK_PKG(chip
, LQFP48
)) {
1868 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL1
, 0xFF, 0x55);
1869 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL2
, 0xFF, 0x55);
1870 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL3
, 0xFF, 0x95);
1871 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL4
, 0xFF, 0x55);
1872 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL5
, 0xFF, 0x55);
1873 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL6
, 0xFF, 0xA5);
1875 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL1
, 0xFF, 0x65);
1876 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL2
, 0xFF, 0x55);
1877 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL3
, 0xFF, 0x95);
1878 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL4
, 0xFF, 0x55);
1879 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL5
, 0xFF, 0x56);
1880 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL6
, 0xFF, 0x59);
1884 static void sd_pull_ctl_enable(struct rts51x_chip
*chip
)
1886 if (CHECK_PKG(chip
, LQFP48
)) {
1887 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL1
, 0xFF, 0xAA);
1888 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL2
, 0xFF, 0xAA);
1889 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL3
, 0xFF, 0xA9);
1890 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL4
, 0xFF, 0x55);
1891 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL5
, 0xFF, 0x55);
1892 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL6
, 0xFF, 0xA5);
1894 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL1
, 0xFF, 0xA5);
1895 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL2
, 0xFF, 0x9A);
1896 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL3
, 0xFF, 0xA5);
1897 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL4
, 0xFF, 0x9A);
1898 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL5
, 0xFF, 0x65);
1899 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL6
, 0xFF, 0x5A);
1903 static int sd_init_power(struct rts51x_chip
*chip
)
1907 rts51x_init_cmd(chip
);
1909 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PWR_CTL
, LDO3318_PWR_MASK
,
1911 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_PAD_CTL
, SD_IO_USING_1V8
,
1913 if (chip
->asic_code
)
1914 rts51x_add_cmd(chip
, WRITE_REG_CMD
, LDO_POWER_CFG
,
1915 TUNE_SD18_MASK
, TUNE_SD18_3V3
);
1916 if (chip
->asic_code
)
1917 sd_pull_ctl_disable(chip
);
1919 rts51x_add_cmd(chip
, WRITE_REG_CMD
, FPGA_PULL_CTL
,
1920 FPGA_SD_PULL_CTL_BIT
| 0x20,
1921 FPGA_SD_PULL_CTL_BIT
);
1922 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_OE
, SD_OUTPUT_EN
, 0);
1923 if (!chip
->option
.FT2_fast_mode
)
1924 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PWR_CTL
, POWER_MASK
,
1927 retval
= rts51x_send_cmd(chip
, MODE_C
, 100);
1928 if (retval
!= STATUS_SUCCESS
)
1929 TRACE_RET(chip
, retval
);
1930 if (!chip
->option
.FT2_fast_mode
) {
1931 #ifdef SD_XD_IO_FOLLOW_PWR
1932 if (CHECK_PKG(chip
, LQFP48
)
1933 || chip
->option
.rts5129_D3318_off_enable
)
1934 rts51x_write_register(chip
, CARD_PWR_CTL
,
1939 #ifdef SD_XD_IO_FOLLOW_PWR
1940 if (CHECK_PKG(chip
, LQFP48
)
1941 || chip
->option
.rts5129_D3318_off_enable
) {
1942 rts51x_init_cmd(chip
);
1943 if (chip
->asic_code
)
1944 sd_pull_ctl_enable(chip
);
1946 rts51x_add_cmd(chip
, WRITE_REG_CMD
,
1948 FPGA_SD_PULL_CTL_BIT
| 0x20, 0);
1949 retval
= rts51x_send_cmd(chip
, MODE_C
, 100);
1950 if (retval
!= STATUS_SUCCESS
)
1951 TRACE_RET(chip
, retval
);
1953 if (chip
->asic_code
)
1954 rts51x_write_register(chip
, CARD_PULL_CTL6
,
1960 retval
= card_power_on(chip
, SD_CARD
);
1961 if (retval
!= STATUS_SUCCESS
)
1962 TRACE_RET(chip
, retval
);
1967 rts51x_get_card_status(chip
, &(chip
->card_status
));
1968 chip
->ocp_stat
= (chip
->card_status
>> 4) & 0x03;
1970 if (chip
->ocp_stat
& (MS_OCP_NOW
| MS_OCP_EVER
)) {
1971 RTS51X_DEBUGP("Over current, OCPSTAT is 0x%x\n",
1973 TRACE_RET(chip
, STATUS_FAIL
);
1978 rts51x_init_cmd(chip
);
1979 if (chip
->asic_code
) {
1980 sd_pull_ctl_enable(chip
);
1982 rts51x_add_cmd(chip
, WRITE_REG_CMD
, FPGA_PULL_CTL
,
1983 FPGA_SD_PULL_CTL_BIT
| 0x20, 0);
1985 retval
= rts51x_send_cmd(chip
, MODE_C
, 100);
1986 if (retval
!= STATUS_SUCCESS
)
1987 TRACE_RET(chip
, retval
);
1988 #ifdef SD_XD_IO_FOLLOW_PWR
1989 rts51x_write_register(chip
, CARD_INT_PEND
, XD_INT
| MS_INT
| SD_INT
,
1990 XD_INT
| MS_INT
| SD_INT
);
1993 RTS51X_WRITE_REG(chip
, CARD_OE
, SD_OUTPUT_EN
, SD_OUTPUT_EN
);
1995 return STATUS_SUCCESS
;
1998 static int sd_dummy_clock(struct rts51x_chip
*chip
)
2000 RTS51X_WRITE_REG(chip
, SD_BUS_STAT
, SD_CLK_TOGGLE_EN
, SD_CLK_TOGGLE_EN
);
2002 RTS51X_WRITE_REG(chip
, SD_BUS_STAT
, SD_CLK_TOGGLE_EN
, 0x00);
2004 return STATUS_SUCCESS
;
2007 int reset_sd(struct rts51x_chip
*chip
)
2009 struct sd_info
*sd_card
= &(chip
->sd_card
);
2010 int retval
, i
= 0, j
= 0, k
= 0, hi_cap_flow
= 0;
2011 int sd_dont_switch
= 0;
2012 int support_1v8
= 0;
2014 u8 switch_bus_width
;
2020 CLR_RETRY_SD20_MODE(sd_card
);
2027 #ifdef SUPPORT_SD_LOCK
2028 if (sd_card
->sd_lock_status
& SD_UNLOCK_POW_ON
)
2029 goto SD_UNLOCK_ENTRY
;
2032 retval
= sd_prepare_reset(chip
);
2033 if (retval
!= STATUS_SUCCESS
)
2034 TRACE_RET(chip
, retval
);
2036 sd_dummy_clock(chip
);
2038 /* Start Initialization Process of SD Card */
2041 sd_send_cmd_get_rsp(chip
, GO_IDLE_STATE
, 0, SD_RSP_TYPE_R0
, NULL
,
2043 if (retval
!= STATUS_SUCCESS
)
2044 TRACE_RET(chip
, retval
);
2049 sd_send_cmd_get_rsp(chip
, SEND_IF_COND
, 0x000001AA, SD_RSP_TYPE_R7
,
2051 if (retval
== STATUS_SUCCESS
) {
2052 if ((rsp
[4] == 0xAA) && ((rsp
[3] & 0x0f) == 0x01)) {
2054 if (CHK_RETRY_SD20_MODE(sd_card
)) {
2057 SUPPORT_HIGH_AND_EXTENDED_CAPACITY
;
2061 SUPPORT_HIGH_AND_EXTENDED_CAPACITY
|
2062 SUPPORT_MAX_POWER_PERMANCE
| SUPPORT_1V8
;
2068 voltage
= SUPPORT_VOLTAGE
;
2071 sd_send_cmd_get_rsp(chip
, GO_IDLE_STATE
, 0, SD_RSP_TYPE_R0
,
2073 if (retval
!= STATUS_SUCCESS
)
2074 TRACE_RET(chip
, retval
);
2082 rts51x_read_register(chip
, CARD_INT_PEND
, &temp
);
2083 RTS51X_DEBUGP("CARD_INT_PEND:%x\n", temp
);
2084 if (temp
& SD_INT
) {
2085 chip
->reset_need_retry
= 1;
2086 rts51x_write_register(chip
, CARD_INT_PEND
,
2087 XD_INT
| SD_INT
| MS_INT
,
2088 XD_INT
| SD_INT
| MS_INT
);
2089 sd_set_reset_fail(chip
, SD_RESET_FAIL
);
2090 TRACE_RET(chip
, STATUS_FAIL
);
2096 sd_send_cmd_get_rsp(chip
, APP_CMD
, 0, SD_RSP_TYPE_R1
, NULL
,
2098 if (retval
!= STATUS_SUCCESS
) {
2099 if (monitor_card_cd(chip
, SD_CARD
) == CD_NOT_EXIST
) {
2100 sd_set_reset_fail(chip
, SD_RESET_FAIL
);
2101 TRACE_RET(chip
, STATUS_FAIL
);
2105 if (chip
->option
.speed_mmc
) {
2109 TRACE_RET(chip
, STATUS_FAIL
);
2114 TRACE_RET(chip
, STATUS_FAIL
);
2119 sd_send_cmd_get_rsp(chip
, SD_APP_OP_COND
, voltage
,
2120 SD_RSP_TYPE_R3
, rsp
, 5);
2121 if (retval
!= STATUS_SUCCESS
) {
2126 TRACE_RET(chip
, STATUS_FAIL
);
2131 } while (!(rsp
[1] & 0x80) && (i
< 255)); /* Not complete power on */
2135 TRACE_RET(chip
, STATUS_FAIL
);
2140 SET_SD_HCXC(sd_card
);
2142 CLR_SD_HCXC(sd_card
);
2143 if (!CHK_RETRY_SD20_MODE(sd_card
)) {
2144 if ((CHK_SD_HCXC(sd_card
)) && (CHECK_UHS50(chip
))) {
2145 support_1v8
= (rsp
[1] & 0x01) ? 1 : 0;
2146 RTS51X_DEBUGP("support_1v8 = %d\n",
2151 CLR_SD_HCXC(sd_card
);
2155 /* CMD11: Switch Voltage */
2156 if (support_1v8
&& CHECK_UHS50(chip
)
2157 && !(((u8
) chip
->option
.sd_speed_prior
& SDR104_SUPPORT
) ==
2159 retval
= sd_voltage_switch(chip
);
2160 if (retval
!= STATUS_SUCCESS
) {
2161 SET_RETRY_SD20_MODE(sd_card
);
2162 sd_init_power(chip
);
2163 RTS51X_DEBUGP("1.8v switch fail\n");
2170 sd_send_cmd_get_rsp(chip
, ALL_SEND_CID
, 0, SD_RSP_TYPE_R2
, NULL
, 0);
2171 if (retval
!= STATUS_SUCCESS
)
2172 TRACE_RET(chip
, retval
);
2176 sd_send_cmd_get_rsp(chip
, SEND_RELATIVE_ADDR
, 0, SD_RSP_TYPE_R6
,
2178 if (retval
!= STATUS_SUCCESS
)
2179 TRACE_RET(chip
, retval
);
2181 sd_card
->sd_addr
= (u32
) rsp
[1] << 24;
2182 sd_card
->sd_addr
+= (u32
) rsp
[2] << 16;
2184 /* Get CSD register for Calculating Timing,Capacity,
2185 * Check CSD to determaine as if this is the SD ROM card */
2186 retval
= sd_check_csd(chip
, 1);
2187 if (retval
!= STATUS_SUCCESS
)
2188 TRACE_RET(chip
, retval
);
2189 /* Select SD card */
2190 retval
= sd_select_card(chip
, 1);
2191 if (retval
!= STATUS_SUCCESS
)
2192 TRACE_RET(chip
, retval
);
2193 #ifdef SUPPORT_SD_LOCK
2195 /* Get SD lock status */
2196 retval
= sd_update_lock_status(chip
);
2197 if (retval
!= STATUS_SUCCESS
)
2198 TRACE_RET(chip
, STATUS_FAIL
);
2200 if (sd_card
->sd_lock_status
& SD_LOCKED
) {
2201 sd_card
->sd_lock_status
|= (SD_LOCK_1BIT_MODE
| SD_PWD_EXIST
);
2202 return STATUS_SUCCESS
;
2203 } else if (!(sd_card
->sd_lock_status
& SD_UNLOCK_POW_ON
)) {
2204 sd_card
->sd_lock_status
&= ~SD_PWD_EXIST
;
2210 sd_send_cmd_get_rsp(chip
, APP_CMD
, sd_card
->sd_addr
, SD_RSP_TYPE_R1
,
2212 if (retval
!= STATUS_SUCCESS
)
2213 TRACE_RET(chip
, retval
);
2216 sd_send_cmd_get_rsp(chip
, SET_CLR_CARD_DETECT
, 0, SD_RSP_TYPE_R1
,
2218 if (retval
!= STATUS_SUCCESS
)
2219 TRACE_RET(chip
, retval
);
2224 sd_send_cmd_get_rsp(chip
, APP_CMD
, sd_card
->sd_addr
,
2225 SD_RSP_TYPE_R1
, NULL
, 0);
2226 if (retval
!= STATUS_SUCCESS
)
2227 TRACE_RET(chip
, retval
);
2228 /* Enable 4 bit data bus */
2230 sd_send_cmd_get_rsp(chip
, SET_BUS_WIDTH
, 2, SD_RSP_TYPE_R1
,
2232 if (retval
!= STATUS_SUCCESS
)
2233 TRACE_RET(chip
, retval
);
2234 switch_bus_width
= SD_BUS_WIDTH_4
;
2236 switch_bus_width
= SD_BUS_WIDTH_1
;
2239 /* Set block length 512 bytes for all block commands */
2240 retval
= sd_send_cmd_get_rsp(chip
, SET_BLOCKLEN
,
2241 0x200, SD_RSP_TYPE_R1
, NULL
, 0);
2242 if (retval
!= STATUS_SUCCESS
)
2243 TRACE_RET(chip
, retval
);
2245 RTS51X_WRITE_REG(chip
, SD_CFG1
, SD_CLK_DIVIDE_MASK
, SD_CLK_DIVIDE_0
);
2247 if (!(sd_card
->raw_csd
[4] & 0x40)) {
2249 RTS51X_DEBUGP("Not support class ten\n");
2252 if (!sd_dont_switch
) {
2253 /* Check the card whether flow SD1.1 spec or higher */
2254 retval
= sd_check_spec(chip
, switch_bus_width
);
2255 if (retval
== STATUS_SUCCESS
) {
2256 retval
= sd_switch_function(chip
, switch_bus_width
);
2257 if (retval
!= STATUS_SUCCESS
) {
2258 if ((sd_card
->sd_switch_fail
==
2259 SDR104_SUPPORT_MASK
)
2260 || (sd_card
->sd_switch_fail
==
2262 || (sd_card
->sd_switch_fail
==
2263 SDR50_SUPPORT_MASK
)) {
2264 sd_init_power(chip
);
2265 SET_RETRY_SD20_MODE(sd_card
);
2266 } else if (sd_card
->sd_switch_fail
==
2274 SET_RETRY_SD20_MODE(sd_card
);
2275 sd_init_power(chip
);
2286 sd_send_cmd_get_rsp(chip
, APP_CMD
, sd_card
->sd_addr
,
2287 SD_RSP_TYPE_R1
, NULL
, 0);
2288 if (retval
!= STATUS_SUCCESS
)
2289 TRACE_RET(chip
, retval
);
2290 /* Enable 4 bit data bus */
2292 sd_send_cmd_get_rsp(chip
, SET_BUS_WIDTH
, 2, SD_RSP_TYPE_R1
,
2294 if (retval
!= STATUS_SUCCESS
)
2295 TRACE_RET(chip
, retval
);
2297 #ifdef SUPPORT_SD_LOCK
2298 /* clear 1 bit mode status */
2299 sd_card
->sd_lock_status
&= ~SD_LOCK_1BIT_MODE
;
2302 if (CHK_SD30_SPEED(sd_card
)) {
2303 rts51x_write_register(chip
, SD30_DRIVE_SEL
, SD30_DRIVE_MASK
,
2306 retval
= sd_set_init_para(chip
);
2307 if (retval
!= STATUS_SUCCESS
)
2308 TRACE_RET(chip
, retval
);
2310 if (CHK_SD_DDR50(sd_card
))
2311 retval
= sd_ddr_tuning(chip
);
2313 retval
= sd_sdr_tuning(chip
);
2315 if (retval
!= STATUS_SUCCESS
) {
2316 SET_RETRY_SD20_MODE(sd_card
);
2317 RTS51X_DEBUGP("tuning phase fail,goto SD20 mode\n");
2318 sd_init_power(chip
);
2319 CLR_SD30_SPEED(sd_card
);
2322 if (STATUS_SUCCESS
==
2323 sd_wait_currentstate_dataready(chip
, 0x08, 1, 20)) {
2324 cmd
[0] = 0x40 | READ_SINGLE_BLOCK
;
2330 sd_read_data(chip
, SD_TM_NORMAL_READ
, cmd
, 5, 512,
2331 1, SD_BUS_WIDTH_4
, NULL
, 0, 600);
2332 if (retval
!= STATUS_SUCCESS
) {
2333 SET_RETRY_SD20_MODE(sd_card
);
2334 RTS51X_DEBUGP("read lba0 fail,"
2335 "goto SD20 mode\n");
2336 sd_init_power(chip
);
2337 CLR_SD30_SPEED(sd_card
);
2342 sd_send_cmd_get_rsp(chip
, SEND_STATUS
, sd_card
->sd_addr
, SD_RSP_TYPE_R1
,
2345 retval
= sd_send_cmd_get_rsp(chip
, APP_CMD
, sd_card
->sd_addr
,
2346 SD_RSP_TYPE_R1
, NULL
, 0);
2347 if (retval
== STATUS_SUCCESS
) {
2349 cmd
[0] = 0x40 | SEND_STATUS
;
2355 sd_read_data(chip
, SD_TM_NORMAL_READ
, cmd
, 5, 64, 1,
2356 SD_BUS_WIDTH_4
, buf
, 64, 600);
2357 if (ret
== STATUS_SUCCESS
) {
2358 sd_card_type
= ((u16
) buf
[2] << 8) | (u16
) buf
[3];
2359 RTS51X_DEBUGP("sd_card_type:0x%4x\n", sd_card_type
);
2360 if ((sd_card_type
== 0x0001)
2361 || (sd_card_type
== 0x0002))
2362 chip
->card_wp
|= SD_CARD
;
2364 rts51x_clear_sd_error(chip
);
2365 sd_send_cmd_get_rsp(chip
, SEND_STATUS
, sd_card
->sd_addr
,
2366 SD_RSP_TYPE_R1
, NULL
, 0);
2369 rts51x_clear_sd_error(chip
);
2370 sd_send_cmd_get_rsp(chip
, SEND_STATUS
, sd_card
->sd_addr
,
2371 SD_RSP_TYPE_R1
, NULL
, 0);
2374 /* Check SD Machanical Write-Protect Switch */
2375 retval
= rts51x_get_card_status(chip
, &(chip
->card_status
));
2376 if (retval
!= STATUS_SUCCESS
)
2377 TRACE_RET(chip
, retval
);
2378 if (chip
->card_status
& SD_WP
)
2379 chip
->card_wp
|= SD_CARD
;
2381 chip
->card_bus_width
[chip
->card2lun
[SD_CARD
]] = 4;
2383 #ifdef SUPPORT_SD_LOCK
2384 if (sd_card
->sd_lock_status
& SD_UNLOCK_POW_ON
) {
2385 rts51x_init_cmd(chip
);
2387 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_BLOCK_CNT_H
, 0xFF, 0x02);
2388 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_BLOCK_CNT_L
, 0xFF, 0x00);
2390 retval
= rts51x_send_cmd(chip
, MODE_C
, 100);
2391 if (retval
!= STATUS_SUCCESS
)
2392 TRACE_RET(chip
, retval
);
2396 return STATUS_SUCCESS
;
2399 static int mmc_test_switch_bus(struct rts51x_chip
*chip
, u8 width
)
2401 struct sd_info
*sd_card
= &(chip
->sd_card
);
2403 u8 buf
[8] = { 0 }, bus_width
;
2408 sd_send_cmd_get_rsp(chip
, BUSTEST_W
, 0, SD_RSP_TYPE_R1
, NULL
, 0);
2409 if (retval
!= STATUS_SUCCESS
)
2410 TRACE_RET(chip
, retval
);
2412 if (width
== MMC_8BIT_BUS
) {
2417 bus_width
= SD_BUS_WIDTH_8
;
2422 bus_width
= SD_BUS_WIDTH_4
;
2425 retval
= sd_write_data(chip
, SD_TM_AUTO_WRITE_3
,
2426 NULL
, 0, byte_cnt
, 1, bus_width
, buf
, len
, 100);
2427 if (retval
!= STATUS_SUCCESS
) {
2428 u8 val1
= 0, val2
= 0;
2429 rts51x_ep0_read_register(chip
, SD_STAT1
, &val1
);
2430 rts51x_ep0_read_register(chip
, SD_STAT2
, &val2
);
2431 rts51x_clear_sd_error(chip
);
2432 if ((val1
& 0xE0) || val2
)
2433 TRACE_RET(chip
, STATUS_FAIL
);
2435 RTS51X_DEBUGP("SD/MMC CMD %d\n", BUSTEST_R
);
2437 rts51x_init_cmd(chip
);
2440 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CMD0
, 0xFF, 0x40 | BUSTEST_R
);
2442 if (width
== MMC_8BIT_BUS
)
2443 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_BYTE_CNT_L
, 0xFF, 0x08);
2445 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_BYTE_CNT_L
, 0xFF, 0x04);
2447 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_BLOCK_CNT_L
, 0xFF, 1);
2448 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_BLOCK_CNT_H
, 0xFF, 0);
2450 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CFG2
, 0xFF,
2451 SD_CALCULATE_CRC7
| SD_NO_CHECK_CRC16
|
2452 SD_NO_WAIT_BUSY_END
| SD_CHECK_CRC7
| SD_RSP_LEN_6
);
2453 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
, 0x01,
2455 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_TRANSFER
, 0xFF,
2456 SD_TM_NORMAL_READ
| SD_TRANSFER_START
);
2457 rts51x_add_cmd(chip
, CHECK_REG_CMD
, SD_TRANSFER
, SD_TRANSFER_END
,
2460 rts51x_add_cmd(chip
, READ_REG_CMD
, PPBUF_BASE2
, 0, 0);
2461 if (width
== MMC_8BIT_BUS
) {
2463 rts51x_add_cmd(chip
, READ_REG_CMD
, PPBUF_BASE2
+ 1, 0, 0);
2468 retval
= rts51x_send_cmd(chip
, MODE_CR
, 100);
2469 if (retval
!= STATUS_SUCCESS
)
2470 TRACE_RET(chip
, retval
);
2472 retval
= rts51x_get_rsp(chip
, len
, 100);
2473 if (CHECK_SD_TRANS_FAIL(chip
, retval
)) {
2474 rts51x_clear_sd_error(chip
);
2475 TRACE_RET(chip
, STATUS_FAIL
);
2478 rts51x_read_rsp_buf(chip
, 1, buf
, 2);
2480 if (width
== MMC_8BIT_BUS
) {
2481 RTS51X_DEBUGP("BUSTEST_R [8bits]: 0x%02x 0x%02x\n",
2483 if ((buf
[0] == 0xAA) && (buf
[1] == 0x55)) {
2487 if (CHK_MMC_DDR52(sd_card
))
2491 /* Switch MMC to 8-bit mode */
2493 sd_send_cmd_get_rsp(chip
, SWITCH
, arg
,
2494 SD_RSP_TYPE_R1b
, rsp
, 5);
2495 if ((retval
== STATUS_SUCCESS
)
2496 && !(rsp
[4] & MMC_SWITCH_ERR
))
2497 return STATUS_SUCCESS
;
2500 RTS51X_DEBUGP("BUSTEST_R [4bits]: 0x%02x\n", buf
[0]);
2501 if (buf
[0] == 0xA5) {
2505 if (CHK_MMC_DDR52(sd_card
))
2509 /* Switch MMC to 4-bit mode */
2511 sd_send_cmd_get_rsp(chip
, SWITCH
, arg
,
2512 SD_RSP_TYPE_R1b
, rsp
, 5);
2513 if ((retval
== STATUS_SUCCESS
)
2514 && !(rsp
[4] & MMC_SWITCH_ERR
))
2515 return STATUS_SUCCESS
;
2519 TRACE_RET(chip
, STATUS_FAIL
);
2522 static int mmc_switch_timing_bus(struct rts51x_chip
*chip
)
2524 struct sd_info
*sd_card
= &(chip
->sd_card
);
2526 u8 card_type
, card_type_mask
= 0;
2529 CLR_MMC_HS(sd_card
);
2531 RTS51X_DEBUGP("SD/MMC CMD %d\n", SEND_EXT_CSD
);
2533 rts51x_init_cmd(chip
);
2535 /* SEND_EXT_CSD command */
2536 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CMD0
, 0xFF,
2537 0x40 | SEND_EXT_CSD
);
2538 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CMD1
, 0xFF, 0);
2539 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CMD2
, 0xFF, 0);
2540 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CMD3
, 0xFF, 0);
2541 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CMD4
, 0xFF, 0);
2543 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_BYTE_CNT_L
, 0xFF, 0);
2544 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_BYTE_CNT_H
, 0xFF, 2);
2545 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_BLOCK_CNT_L
, 0xFF, 1);
2546 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_BLOCK_CNT_H
, 0xFF, 0);
2548 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CFG2
, 0xFF,
2549 SD_CALCULATE_CRC7
| SD_CHECK_CRC16
| SD_NO_WAIT_BUSY_END
2550 | SD_CHECK_CRC7
| SD_RSP_LEN_6
);
2551 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
, 0x01,
2553 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_TRANSFER
, 0xFF,
2554 SD_TM_NORMAL_READ
| SD_TRANSFER_START
);
2555 rts51x_add_cmd(chip
, CHECK_REG_CMD
, SD_TRANSFER
, SD_TRANSFER_END
,
2558 rts51x_add_cmd(chip
, READ_REG_CMD
, PPBUF_BASE2
+ 196, 0xFF, 0);
2559 rts51x_add_cmd(chip
, READ_REG_CMD
, PPBUF_BASE2
+ 212, 0xFF, 0);
2560 rts51x_add_cmd(chip
, READ_REG_CMD
, PPBUF_BASE2
+ 213, 0xFF, 0);
2561 rts51x_add_cmd(chip
, READ_REG_CMD
, PPBUF_BASE2
+ 214, 0xFF, 0);
2562 rts51x_add_cmd(chip
, READ_REG_CMD
, PPBUF_BASE2
+ 215, 0xFF, 0);
2564 retval
= rts51x_send_cmd(chip
, MODE_CR
, 100);
2565 if (retval
!= STATUS_SUCCESS
)
2566 TRACE_RET(chip
, retval
);
2568 retval
= rts51x_get_rsp(chip
, 6, 1000);
2570 if (CHECK_SD_TRANS_FAIL(chip
, retval
)) {
2571 if (retval
== STATUS_TIMEDOUT
) {
2572 rts51x_clear_sd_error(chip
);
2573 sd_send_cmd_get_rsp(chip
, SEND_STATUS
, sd_card
->sd_addr
,
2574 SD_RSP_TYPE_R1
, NULL
, 0);
2576 TRACE_RET(chip
, STATUS_FAIL
);
2579 rts51x_read_rsp_buf(chip
, 0, buf
, 6);
2581 if (buf
[0] & SD_TRANSFER_ERR
) {
2582 sd_send_cmd_get_rsp(chip
, SEND_STATUS
, sd_card
->sd_addr
,
2583 SD_RSP_TYPE_R1
, NULL
, 0);
2584 TRACE_RET(chip
, STATUS_FAIL
);
2586 if (CHK_MMC_SECTOR_MODE(sd_card
))
2588 ((u32
) buf
[5] << 24) | ((u32
) buf
[4] << 16) |
2589 ((u32
) buf
[3] << 8) | ((u32
) buf
[2]);
2590 #ifdef SUPPORT_SD_LOCK
2591 if (!(sd_card
->sd_lock_status
& SD_SDR_RST
) && CHECK_UHS50(chip
))
2592 card_type_mask
= 0x07;
2594 card_type_mask
= 0x03;
2596 if (CHECK_UHS50(chip
))
2597 card_type_mask
= 0x07;
2599 card_type_mask
= 0x03;
2602 card_type
= buf
[1] & card_type_mask
;
2604 /* CARD TYPE FIELD = DDR52MHz, 52MHz or 26MHz */
2607 if (card_type
& 0x04)
2608 SET_MMC_DDR52(sd_card
);
2609 else if (card_type
& 0x02)
2610 SET_MMC_52M(sd_card
);
2612 SET_MMC_26M(sd_card
);
2615 sd_send_cmd_get_rsp(chip
, SWITCH
, 0x03B90100,
2616 SD_RSP_TYPE_R1b
, rsp
, 5);
2617 if ((retval
!= STATUS_SUCCESS
) || (rsp
[4] & MMC_SWITCH_ERR
))
2618 CLR_MMC_HS(sd_card
);
2620 sd_choose_proper_clock(chip
);
2621 retval
= switch_clock(chip
, sd_card
->sd_clock
);
2622 if (retval
!= STATUS_SUCCESS
)
2623 TRACE_RET(chip
, retval
);
2625 /* Test Bus Procedure */
2626 if (mmc_test_switch_bus(chip
, MMC_8BIT_BUS
) == STATUS_SUCCESS
) {
2627 SET_MMC_8BIT(sd_card
);
2628 chip
->card_bus_width
[chip
->card2lun
[SD_CARD
]] = 8;
2629 #ifdef SUPPORT_SD_LOCK
2630 sd_card
->sd_lock_status
&= ~SD_LOCK_1BIT_MODE
;
2632 } else if (mmc_test_switch_bus(chip
, MMC_4BIT_BUS
) == STATUS_SUCCESS
) {
2633 SET_MMC_4BIT(sd_card
);
2634 chip
->card_bus_width
[chip
->card2lun
[SD_CARD
]] = 4;
2635 #ifdef SUPPORT_SD_LOCK
2636 sd_card
->sd_lock_status
&= ~SD_LOCK_1BIT_MODE
;
2639 CLR_MMC_8BIT(sd_card
);
2640 CLR_MMC_4BIT(sd_card
);
2643 return STATUS_SUCCESS
;
2646 static int reset_mmc(struct rts51x_chip
*chip
)
2648 struct sd_info
*sd_card
= &(chip
->sd_card
);
2649 int retval
, i
= 0, j
= 0, k
= 0;
2652 u8 change_to_ddr52
= 1;
2655 #ifdef SUPPORT_SD_LOCK
2656 if (sd_card
->sd_lock_status
& SD_UNLOCK_POW_ON
)
2657 goto MMC_UNLOCK_ENTRY
;
2662 retval
= sd_prepare_reset(chip
);
2663 if (retval
!= STATUS_SUCCESS
)
2664 TRACE_RET(chip
, retval
);
2670 sd_send_cmd_get_rsp(chip
, GO_IDLE_STATE
, 0, SD_RSP_TYPE_R0
, NULL
,
2672 if (retval
!= STATUS_SUCCESS
)
2673 TRACE_RET(chip
, retval
);
2678 rts51x_read_register(chip
, CARD_INT_PEND
, &temp
);
2679 if (temp
& SD_INT
) {
2680 chip
->reset_need_retry
= 1;
2681 rts51x_write_register(chip
, CARD_INT_PEND
,
2682 XD_INT
| SD_INT
| MS_INT
,
2683 XD_INT
| SD_INT
| MS_INT
);
2684 sd_set_reset_fail(chip
, MMC_RESET_FAIL
);
2685 TRACE_RET(chip
, STATUS_FAIL
);
2690 retval
= sd_send_cmd_get_rsp(chip
, SEND_OP_COND
,
2691 (SUPPORT_VOLTAGE
| 0x40000000),
2692 SD_RSP_TYPE_R3
, rsp
, 5);
2693 if (retval
!= STATUS_SUCCESS
) {
2694 if (monitor_card_cd(chip
, SD_CARD
) == CD_NOT_EXIST
) {
2695 sd_set_reset_fail(chip
, MMC_RESET_FAIL
);
2696 TRACE_RET(chip
, STATUS_FAIL
);
2699 if (sd_check_err_code(chip
, SD_BUSY
)
2700 || sd_check_err_code(chip
, SD_TO_ERR
)) {
2703 sd_clr_err_code(chip
);
2706 TRACE_RET(chip
, STATUS_FAIL
);
2711 sd_clr_err_code(chip
);
2714 TRACE_RET(chip
, STATUS_FAIL
);
2721 } while (!(rsp
[1] & 0x80) && (i
< 100)); /* Not complete power on */
2725 TRACE_RET(chip
, STATUS_FAIL
);
2728 if ((rsp
[1] & 0x60) == 0x40)
2729 SET_MMC_SECTOR_MODE(sd_card
);
2731 CLR_MMC_SECTOR_MODE(sd_card
);
2735 sd_send_cmd_get_rsp(chip
, ALL_SEND_CID
, 0, SD_RSP_TYPE_R2
, NULL
, 0);
2736 if (retval
!= STATUS_SUCCESS
)
2737 TRACE_RET(chip
, retval
);
2740 sd_card
->sd_addr
= 0x00100000;
2742 sd_send_cmd_get_rsp(chip
, SET_RELATIVE_ADDR
, sd_card
->sd_addr
,
2743 SD_RSP_TYPE_R6
, rsp
, 5);
2744 if (retval
!= STATUS_SUCCESS
)
2745 TRACE_RET(chip
, retval
);
2747 /* Get CSD register for Calculating Timing,Capacity
2748 * Check CSD to determaine as if this is the SD ROM card */
2749 retval
= sd_check_csd(chip
, 1);
2750 if (retval
!= STATUS_SUCCESS
)
2751 TRACE_RET(chip
, retval
);
2752 /* Get MMC Spec_Ver in the CSD register */
2753 spec_ver
= (sd_card
->raw_csd
[0] & 0x3C) >> 2;
2755 /* Select MMC card */
2756 retval
= sd_select_card(chip
, 1);
2757 if (retval
!= STATUS_SUCCESS
)
2758 TRACE_RET(chip
, retval
);
2760 /* Set block length 512 bytes for all block commands */
2762 sd_send_cmd_get_rsp(chip
, SET_BLOCKLEN
, 0x200, SD_RSP_TYPE_R1
, NULL
,
2764 if (retval
!= STATUS_SUCCESS
)
2765 TRACE_RET(chip
, retval
);
2766 #ifdef SUPPORT_SD_LOCK
2768 /* Get SD lock status */
2769 retval
= sd_update_lock_status(chip
);
2770 if (retval
!= STATUS_SUCCESS
)
2771 TRACE_RET(chip
, STATUS_FAIL
);
2774 RTS51X_WRITE_REG(chip
, SD_CFG1
, SD_CLK_DIVIDE_MASK
, SD_CLK_DIVIDE_0
);
2776 if (chip
->ic_version
< 2)
2777 rts51x_write_register(chip
, SD30_DRIVE_SEL
, SD30_DRIVE_MASK
,
2779 rts51x_write_register(chip
, CARD_DRIVE_SEL
, SD20_DRIVE_MASK
, DRIVE_8mA
);
2781 chip
->card_bus_width
[chip
->card2lun
[SD_CARD
]] = 1;
2782 if (spec_ver
== 4) {
2784 (void)mmc_switch_timing_bus(chip
);
2787 if (CHK_MMC_SECTOR_MODE(sd_card
) && (sd_card
->capacity
== 0))
2788 TRACE_RET(chip
, STATUS_FAIL
);
2790 if (CHK_MMC_DDR52(sd_card
) && change_to_ddr52
) {
2791 /* Card is extracted while identifying */
2792 if (monitor_card_cd(chip
, SD_CARD
) == CD_NOT_EXIST
)
2793 TRACE_RET(chip
, STATUS_FAIL
);
2795 retval
= sd_set_init_para(chip
);
2796 if (retval
!= STATUS_SUCCESS
) {
2797 CLR_MMC_DDR52(sd_card
);
2798 sd_init_power(chip
);
2799 change_to_ddr52
= 0;
2803 retval
= mmc_ddr_tuning(chip
);
2804 if (retval
!= STATUS_SUCCESS
) {
2805 CLR_MMC_DDR52(sd_card
);
2806 sd_init_power(chip
);
2807 change_to_ddr52
= 0;
2811 if (STATUS_SUCCESS
==
2812 sd_wait_currentstate_dataready(chip
, 0x08, 1, 20)) {
2813 cmd
[0] = 0x40 | READ_SINGLE_BLOCK
;
2818 if (CHK_MMC_8BIT(sd_card
)) {
2820 sd_read_data(chip
, SD_TM_NORMAL_READ
, cmd
,
2821 5, 512, 1, SD_BUS_WIDTH_8
,
2823 } else if (CHK_MMC_4BIT(sd_card
)) {
2825 sd_read_data(chip
, SD_TM_NORMAL_READ
, cmd
,
2826 5, 512, 1, SD_BUS_WIDTH_4
,
2830 sd_read_data(chip
, SD_TM_NORMAL_READ
, cmd
,
2831 5, 512, 1, SD_BUS_WIDTH_1
,
2835 if (retval
!= STATUS_SUCCESS
) {
2836 CLR_MMC_DDR52(sd_card
);
2837 change_to_ddr52
= 0;
2838 RTS51X_DEBUGP("read lba0 fail,"
2839 "goto SD20 mode\n");
2840 sd_init_power(chip
);
2845 #ifdef SUPPORT_SD_LOCK
2846 if (sd_card
->sd_lock_status
& SD_UNLOCK_POW_ON
) {
2847 rts51x_init_cmd(chip
);
2849 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_BLOCK_CNT_H
, 0xFF, 0x02);
2850 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_BLOCK_CNT_L
, 0xFF, 0x00);
2852 retval
= rts51x_send_cmd(chip
, MODE_C
, 100);
2853 if (retval
!= STATUS_SUCCESS
)
2854 TRACE_RET(chip
, retval
);
2858 retval
= rts51x_get_card_status(chip
, &(chip
->card_status
));
2859 if (retval
!= STATUS_SUCCESS
)
2860 TRACE_RET(chip
, retval
);
2861 if (chip
->card_status
& SD_WP
)
2862 chip
->card_wp
|= SD_CARD
;
2864 return STATUS_SUCCESS
;
2867 int reset_sd_card(struct rts51x_chip
*chip
)
2869 struct sd_info
*sd_card
= &(chip
->sd_card
);
2873 memset(sd_card
, 0, sizeof(struct sd_info
));
2875 /* Init variables */
2876 sd_card
->sd_type
= 0;
2877 sd_card
->seq_mode
= 0;
2878 sd_card
->sd_data_buf_ready
= 0;
2879 sd_card
->capacity
= 0;
2880 sd_card
->sd_switch_fail
= 0;
2882 #ifdef SUPPORT_SD_LOCK
2883 sd_card
->sd_lock_status
= 0;
2884 sd_card
->sd_erase_status
= 0;
2887 sd_clear_reset_fail(chip
);
2888 enable_card_clock(chip
, SD_CARD
);
2890 sd_init_power(chip
);
2892 chip
->reset_need_retry
= 0;
2893 for (i
= 0; i
< 3; i
++) {
2894 if (!chip
->option
.reset_mmc_first
) { /* reset sd first */
2895 retval
= reset_sd(chip
);
2896 if (retval
!= STATUS_SUCCESS
) {
2897 /* Switch SD bus to 3V3 signal */
2898 RTS51X_WRITE_REG(chip
, SD_PAD_CTL
,
2899 SD_IO_USING_1V8
, 0);
2900 if (sd_check_reset_fail(chip
, SD_RESET_FAIL
))
2901 sd_clear_reset_fail(chip
);
2903 retval
= reset_mmc(chip
);
2905 } else { /* reset MMC first */
2906 retval
= reset_mmc(chip
);
2907 if (retval
!= STATUS_SUCCESS
) {
2908 if (sd_check_reset_fail(chip
, MMC_RESET_FAIL
)) {
2909 sd_clear_reset_fail(chip
);
2911 retval
= reset_sd(chip
);
2912 if (retval
!= STATUS_SUCCESS
) {
2915 RTS51X_WRITE_REG(chip
,
2917 SD_IO_USING_1V8
, 0);
2923 if ((retval
== STATUS_SUCCESS
) || (!chip
->reset_need_retry
)) {
2924 /* if reset success or don't need retry,then break */
2927 if (monitor_card_cd(chip
, SD_CARD
) == CD_NOT_EXIST
) {
2928 /* card is extracted */
2931 RTS51X_DEBUGP("retry reset sd card,%d\n", i
);
2932 chip
->reset_need_retry
= 0;
2935 sd_clear_reset_fail(chip
);
2936 chip
->reset_need_retry
= 0;
2938 if (retval
== STATUS_SUCCESS
) {
2939 rts51x_init_cmd(chip
);
2940 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CFG1
, SD_CLK_DIVIDE_MASK
,
2942 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_BYTE_CNT_L
, 0xFF, 0);
2943 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_BYTE_CNT_H
, 0xFF, 2);
2944 retval
= rts51x_send_cmd(chip
, MODE_C
, 100);
2945 if (retval
!= STATUS_SUCCESS
)
2946 TRACE_RET(chip
, retval
);
2948 chip
->capacity
[chip
->card2lun
[SD_CARD
]] = sd_card
->capacity
= 0;
2949 if (chip
->option
.reset_or_rw_fail_set_pad_drive
) {
2950 rts51x_write_register(chip
, CARD_DRIVE_SEL
,
2951 SD20_DRIVE_MASK
, DRIVE_8mA
);
2953 TRACE_RET(chip
, STATUS_FAIL
);
2956 chip
->capacity
[chip
->card2lun
[SD_CARD
]] = sd_card
->capacity
;
2958 if (chip
->option
.sd_send_status_en
) {
2959 sd_card
->sd_send_status_en
= 1;
2961 if (sd_card
->capacity
> 0x20000) { /* 64MB */
2962 sd_card
->sd_send_status_en
= 0;
2964 sd_card
->sd_send_status_en
= 1;
2967 RTS51X_DEBUGP("sd_card->sd_send_status = %d\n",
2968 sd_card
->sd_send_status_en
);
2970 retval
= sd_set_init_para(chip
);
2971 if (retval
!= STATUS_SUCCESS
)
2972 TRACE_RET(chip
, retval
);
2974 RTS51X_DEBUGP("sd_card->sd_type = 0x%x\n", sd_card
->sd_type
);
2976 return STATUS_SUCCESS
;
2979 #define WAIT_DATA_READY_RTY_CNT 255
2981 static int wait_data_buf_ready(struct rts51x_chip
*chip
)
2983 struct sd_info
*sd_card
= &(chip
->sd_card
);
2986 for (i
= 0; i
< WAIT_DATA_READY_RTY_CNT
; i
++) {
2987 if (monitor_card_cd(chip
, SD_CARD
) == CD_NOT_EXIST
)
2988 TRACE_RET(chip
, STATUS_FAIL
);
2990 sd_card
->sd_data_buf_ready
= 0;
2992 retval
= sd_send_cmd_get_rsp(chip
, SEND_STATUS
,
2993 sd_card
->sd_addr
, SD_RSP_TYPE_R1
,
2995 if (retval
!= STATUS_SUCCESS
)
2996 TRACE_RET(chip
, retval
);
2998 if (sd_card
->sd_data_buf_ready
)
2999 return sd_send_cmd_get_rsp(chip
, SEND_STATUS
,
3001 SD_RSP_TYPE_R1
, NULL
, 0);
3004 sd_set_err_code(chip
, SD_TO_ERR
);
3006 TRACE_RET(chip
, STATUS_FAIL
);
3009 void sd_stop_seq_mode(struct rts51x_chip
*chip
)
3011 struct sd_info
*sd_card
= &(chip
->sd_card
);
3014 if (sd_card
->seq_mode
) {
3015 retval
= sd_switch_clock(chip
);
3016 if (retval
!= STATUS_SUCCESS
)
3019 retval
= sd_send_cmd_get_rsp(chip
, STOP_TRANSMISSION
, 0,
3020 SD_RSP_TYPE_R1b
, NULL
, 0);
3021 if (retval
!= STATUS_SUCCESS
)
3022 sd_set_err_code(chip
, SD_STS_ERR
);
3023 sd_card
->seq_mode
= 0;
3025 rts51x_ep0_write_register(chip
, MC_FIFO_CTL
, FIFO_FLUSH
,
3030 static inline int sd_auto_tune_clock(struct rts51x_chip
*chip
)
3032 struct sd_info
*sd_card
= &(chip
->sd_card
);
3035 if (chip
->asic_code
) {
3036 if (sd_card
->sd_clock
> 30)
3037 sd_card
->sd_clock
-= 20;
3039 if (sd_card
->sd_clock
== CLK_100
)
3040 sd_card
->sd_clock
= CLK_80
;
3041 else if (sd_card
->sd_clock
== CLK_80
)
3042 sd_card
->sd_clock
= CLK_60
;
3043 else if (sd_card
->sd_clock
== CLK_60
)
3044 sd_card
->sd_clock
= CLK_50
;
3047 retval
= sd_switch_clock(chip
);
3048 if (retval
!= STATUS_SUCCESS
)
3049 TRACE_RET(chip
, retval
);
3051 return STATUS_SUCCESS
;
3054 int sd_rw(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
, u32 start_sector
,
3057 struct sd_info
*sd_card
= &(chip
->sd_card
);
3064 sd_card
->counter
= 0;
3066 if (!CHK_SD_HCXC(sd_card
) && !CHK_MMC_SECTOR_MODE(sd_card
))
3067 data_addr
= start_sector
<< 9;
3069 data_addr
= start_sector
;
3071 RTS51X_DEBUGP("sd_rw, data_addr = 0x%x\n", data_addr
);
3073 sd_clr_err_code(chip
);
3075 retval
= sd_switch_clock(chip
);
3076 if (retval
!= STATUS_SUCCESS
)
3077 TRACE_RET(chip
, retval
);
3079 if (sd_card
->seq_mode
&& ((sd_card
->pre_dir
!= srb
->sc_data_direction
)
3081 ((sd_card
->pre_sec_addr
+
3082 sd_card
->pre_sec_cnt
) != start_sector
))) {
3083 if ((sd_card
->pre_dir
== DMA_FROM_DEVICE
)
3084 && !CHK_SD30_SPEED(sd_card
)
3085 && !CHK_SD_HS(sd_card
)
3086 && !CHK_MMC_HS(sd_card
)
3087 && sd_card
->sd_send_status_en
) {
3088 sd_send_cmd_get_rsp(chip
, SEND_STATUS
,
3089 sd_card
->sd_addr
, SD_RSP_TYPE_R1
,
3094 sd_send_cmd_get_rsp(chip
, STOP_TRANSMISSION
, 0,
3095 SD_RSP_TYPE_R1b
, NULL
, 0);
3096 if (retval
!= STATUS_SUCCESS
) {
3097 sd_set_err_code(chip
, SD_STS_ERR
);
3098 TRACE_RET(chip
, sd_parse_err_code(chip
));
3101 sd_card
->seq_mode
= 0;
3103 RTS51X_WRITE_REG(chip
, MC_FIFO_CTL
, FIFO_FLUSH
, FIFO_FLUSH
);
3105 if (!CHK_SD30_SPEED(sd_card
)
3106 && !CHK_SD_HS(sd_card
)
3107 && !CHK_MMC_HS(sd_card
)
3108 && sd_card
->sd_send_status_en
) {
3109 /* random rw, so pre_sec_cnt < 0x80 */
3110 sd_send_cmd_get_rsp(chip
, SEND_STATUS
,
3111 sd_card
->sd_addr
, SD_RSP_TYPE_R1
,
3116 rts51x_init_cmd(chip
);
3118 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_BYTE_CNT_L
, 0xFF, 0x00);
3119 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_BYTE_CNT_H
, 0xFF, 0x02);
3120 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_BLOCK_CNT_L
, 0xFF,
3122 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_BLOCK_CNT_H
, 0xFF,
3123 (u8
) (sector_cnt
>> 8));
3125 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
, 0x01,
3128 if (CHK_MMC_8BIT(sd_card
))
3129 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CFG1
, 0x03,
3131 else if (CHK_MMC_4BIT(sd_card
) || CHK_SD(sd_card
))
3132 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CFG1
, 0x03,
3135 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CFG1
, 0x03,
3138 if (sd_card
->seq_mode
) {
3139 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CFG2
, 0xFF,
3140 SD_NO_CALCULATE_CRC7
| SD_CHECK_CRC16
|
3141 SD_NO_WAIT_BUSY_END
| SD_NO_CHECK_CRC7
|
3144 trans_dma_enable(srb
->sc_data_direction
, chip
, sector_cnt
* 512,
3147 if (srb
->sc_data_direction
== DMA_FROM_DEVICE
) {
3149 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_TRANSFER
, 0xFF,
3150 SD_TM_AUTO_READ_3
| SD_TRANSFER_START
);
3153 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_TRANSFER
, 0xFF,
3154 SD_TM_AUTO_WRITE_3
| SD_TRANSFER_START
);
3157 rts51x_add_cmd(chip
, CHECK_REG_CMD
, SD_TRANSFER
,
3158 SD_TRANSFER_END
, SD_TRANSFER_END
);
3160 retval
= rts51x_send_cmd(chip
, flag
, 100);
3161 if (retval
!= STATUS_SUCCESS
)
3162 TRACE_RET(chip
, retval
);
3164 if (srb
->sc_data_direction
== DMA_FROM_DEVICE
) {
3165 RTS51X_DEBUGP("SD/MMC CMD %d\n", READ_MULTIPLE_BLOCK
);
3166 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CMD0
, 0xFF,
3167 0x40 | READ_MULTIPLE_BLOCK
);
3168 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CMD1
, 0xFF,
3169 (u8
) (data_addr
>> 24));
3170 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CMD2
, 0xFF,
3171 (u8
) (data_addr
>> 16));
3172 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CMD3
, 0xFF,
3173 (u8
) (data_addr
>> 8));
3174 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CMD4
, 0xFF,
3177 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CFG2
, 0xFF,
3178 SD_CALCULATE_CRC7
| SD_CHECK_CRC16
|
3179 SD_NO_WAIT_BUSY_END
| SD_CHECK_CRC7
|
3182 trans_dma_enable(srb
->sc_data_direction
, chip
,
3183 sector_cnt
* 512, DMA_512
);
3185 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_TRANSFER
, 0xFF,
3186 SD_TM_AUTO_READ_2
| SD_TRANSFER_START
);
3187 rts51x_add_cmd(chip
, CHECK_REG_CMD
, SD_TRANSFER
,
3188 SD_TRANSFER_END
, SD_TRANSFER_END
);
3190 retval
= rts51x_send_cmd(chip
, MODE_CDIR
, 100);
3191 if (retval
!= STATUS_SUCCESS
)
3192 TRACE_RET(chip
, retval
);
3194 retval
= rts51x_send_cmd(chip
, MODE_C
, 50);
3195 if (retval
!= STATUS_SUCCESS
) {
3196 rts51x_clear_sd_error(chip
);
3198 sd_set_err_code(chip
, SD_TO_ERR
);
3199 TRACE_RET(chip
, sd_parse_err_code(chip
));
3202 retval
= wait_data_buf_ready(chip
);
3203 if (retval
!= STATUS_SUCCESS
) {
3204 sd_set_err_code(chip
, SD_TO_ERR
);
3205 TRACE_RET(chip
, sd_parse_err_code(chip
));
3208 retval
= sd_send_cmd_get_rsp(chip
, WRITE_MULTIPLE_BLOCK
,
3209 data_addr
, SD_RSP_TYPE_R1
,
3211 if (retval
!= STATUS_SUCCESS
) {
3212 sd_set_err_code(chip
, SD_CRC_ERR
);
3213 TRACE_RET(chip
, sd_parse_err_code(chip
));
3216 rts51x_init_cmd(chip
);
3218 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CFG2
, 0xFF,
3219 SD_NO_CALCULATE_CRC7
| SD_CHECK_CRC16
|
3220 SD_NO_WAIT_BUSY_END
| SD_NO_CHECK_CRC7
|
3223 trans_dma_enable(srb
->sc_data_direction
, chip
,
3224 sector_cnt
* 512, DMA_512
);
3226 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_TRANSFER
, 0xFF,
3227 SD_TM_AUTO_WRITE_3
| SD_TRANSFER_START
);
3228 rts51x_add_cmd(chip
, CHECK_REG_CMD
, SD_TRANSFER
,
3229 SD_TRANSFER_END
, SD_TRANSFER_END
);
3231 retval
= rts51x_send_cmd(chip
, MODE_CDOR
, 100);
3232 if (retval
!= STATUS_SUCCESS
)
3233 TRACE_RET(chip
, retval
);
3236 sd_card
->seq_mode
= 1;
3239 if (srb
->sc_data_direction
== DMA_FROM_DEVICE
) {
3240 pipe
= RCV_BULK_PIPE(chip
);
3241 stageflag
= STAGE_DI
;
3243 pipe
= SND_BULK_PIPE(chip
);
3244 stageflag
= STAGE_DO
;
3248 rts51x_transfer_data_rcc(chip
, pipe
, scsi_sglist(srb
),
3249 scsi_bufflen(srb
), scsi_sg_count(srb
),
3250 NULL
, 10000, stageflag
);
3251 if (retval
!= STATUS_SUCCESS
) {
3255 sd_print_debug_reg(chip
);
3257 rts51x_ep0_read_register(chip
, SD_STAT1
, &stat
);
3258 RTS51X_DEBUGP("SD_STAT1: 0x%x\n", stat
);
3260 rts51x_clear_sd_error(chip
);
3263 sd_send_cmd_get_rsp(chip
, STOP_TRANSMISSION
, 0,
3264 SD_RSP_TYPE_R1b
, NULL
, 0);
3265 if (retval
!= STATUS_SUCCESS
) {
3266 sd_set_err_code(chip
, SD_STS_ERR
);
3267 TRACE_RET(chip
, retval
);
3270 if (stat
& (SD_CRC7_ERR
| SD_CRC16_ERR
| SD_CRC_WRITE_ERR
)) {
3271 RTS51X_DEBUGP("SD CRC error, tune clock!\n");
3272 sd_auto_tune_clock(chip
);
3275 sd_card
->seq_mode
= 0;
3277 TRACE_RET(chip
, err
);
3279 retval
= rts51x_get_rsp(chip
, 1, 2000);
3280 if (CHECK_SD_TRANS_FAIL(chip
, retval
)) {
3281 rts51x_clear_sd_error(chip
);
3282 TRACE_RET(chip
, STATUS_FAIL
);
3285 sd_card
->pre_sec_addr
= start_sector
;
3286 sd_card
->pre_sec_cnt
= sector_cnt
;
3287 sd_card
->pre_dir
= srb
->sc_data_direction
;
3289 return STATUS_SUCCESS
;
3292 void sd_cleanup_work(struct rts51x_chip
*chip
)
3294 struct sd_info
*sd_card
= &(chip
->sd_card
);
3296 if (sd_card
->seq_mode
) {
3297 RTS51X_DEBUGP("SD: stop transmission\n");
3298 sd_stop_seq_mode(chip
);
3299 sd_card
->counter
= 0;
3303 inline void sd_fill_power_off_card3v3(struct rts51x_chip
*chip
)
3305 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_CLK_EN
, SD_CLK_EN
, 0);
3307 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_OE
, SD_OUTPUT_EN
, 0);
3308 if (!chip
->option
.FT2_fast_mode
) {
3309 #ifdef SD_XD_IO_FOLLOW_PWR
3310 if (CHECK_PKG(chip
, LQFP48
)
3311 || chip
->option
.rts5129_D3318_off_enable
)
3312 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PWR_CTL
,
3313 POWER_MASK
| LDO_OFF
,
3314 POWER_OFF
| LDO_OFF
);
3316 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PWR_CTL
,
3317 POWER_MASK
, POWER_OFF
);
3319 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PWR_CTL
, POWER_MASK
,
3325 int sd_power_off_card3v3(struct rts51x_chip
*chip
)
3329 rts51x_init_cmd(chip
);
3331 sd_fill_power_off_card3v3(chip
);
3333 retval
= rts51x_send_cmd(chip
, MODE_C
, 100);
3334 if (retval
!= STATUS_SUCCESS
)
3335 TRACE_RET(chip
, retval
);
3336 #ifdef SD_XD_IO_FOLLOW_PWR
3337 if (!chip
->option
.FT2_fast_mode
)
3338 wait_timeout(chip
->option
.D3318_off_delay
);
3341 return STATUS_SUCCESS
;
3344 int release_sd_card(struct rts51x_chip
*chip
)
3346 struct sd_info
*sd_card
= &(chip
->sd_card
);
3349 RTS51X_DEBUGP("elease_sd_card\n");
3351 chip
->card_ready
&= ~SD_CARD
;
3352 chip
->card_fail
&= ~SD_CARD
;
3353 chip
->card_wp
&= ~SD_CARD
;
3355 #ifdef SUPPORT_SD_LOCK
3356 sd_card
->sd_lock_status
= 0;
3357 sd_card
->sd_erase_status
= 0;
3360 memset(sd_card
->raw_csd
, 0, 16);
3361 memset(sd_card
->raw_scr
, 0, 8);
3363 rts51x_write_register(chip
, SFSM_ED
, HW_CMD_STOP
, HW_CMD_STOP
);
3364 rts51x_write_register(chip
, SD_PAD_CTL
, SD_IO_USING_1V8
, 0);
3365 if (CHECK_PKG(chip
, LQFP48
) || chip
->option
.rts5129_D3318_off_enable
)
3366 sd_power_off_card3v3(chip
);
3368 rts51x_init_cmd(chip
);
3369 if (!(CHECK_PKG(chip
, LQFP48
) || chip
->option
.rts5129_D3318_off_enable
))
3370 sd_fill_power_off_card3v3(chip
);
3372 if (chip
->asic_code
)
3373 sd_pull_ctl_disable(chip
);
3375 rts51x_add_cmd(chip
, WRITE_REG_CMD
, FPGA_PULL_CTL
,
3376 FPGA_SD_PULL_CTL_BIT
| 0x20,
3377 FPGA_SD_PULL_CTL_BIT
);
3379 /* Switch LDO3318 to 3.3V */
3380 rts51x_add_cmd(chip
, WRITE_REG_CMD
, LDO_POWER_CFG
, TUNE_SD18_MASK
,
3383 if (CHK_MMC_DDR52(sd_card
) && CHK_MMC_8BIT(sd_card
))
3384 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_DMA1_CTL
,
3385 EXTEND_DMA1_ASYNC_SIGNAL
,
3386 EXTEND_DMA1_ASYNC_SIGNAL
);
3387 if (CHK_SD30_SPEED(sd_card
) || CHK_MMC(sd_card
))
3388 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD30_DRIVE_SEL
,
3389 SD30_DRIVE_MASK
, chip
->option
.sd30_pad_drive
);
3390 /* Suspend LDO3318 */
3391 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PWR_CTL
, LDO3318_PWR_MASK
,
3394 retval
= rts51x_send_cmd(chip
, MODE_C
, 100);
3395 if (retval
!= STATUS_SUCCESS
)
3396 TRACE_RET(chip
, retval
);
3399 return STATUS_SUCCESS
;