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>
29 #include <linux/slab.h>
34 #include "rts51x_transport.h"
35 #include "rts51x_scsi.h"
36 #include "rts51x_card.h"
37 #include "rts51x_chip.h"
42 static inline int get_rsp_type(u8 rsp_code
, u8
*rsp_type
, int *rsp_len
)
44 if (!rsp_type
|| !rsp_len
)
49 *rsp_type
= SD_RSP_TYPE_R0
; /* no response */
54 *rsp_type
= SD_RSP_TYPE_R1
; /* R1,R6(,R4,R5) */
59 *rsp_type
= SD_RSP_TYPE_R1b
; /* R1b */
64 *rsp_type
= SD_RSP_TYPE_R2
; /* R2 */
69 *rsp_type
= SD_RSP_TYPE_R3
; /* R3 */
77 return STATUS_SUCCESS
;
80 static int ext_sd_send_cmd_get_rsp(struct rts51x_chip
*chip
, u8 cmd_idx
,
81 u32 arg
, u8 rsp_type
, u8
*rsp
, int rsp_len
,
91 RTS51X_DEBUGP("EXT SD/MMC CMD %d\n", cmd_idx
);
93 if (rsp_type
== SD_RSP_TYPE_R1b
)
98 rts51x_init_cmd(chip
);
100 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CMD0
, 0xFF, 0x40 | cmd_idx
);
101 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CMD1
, 0xFF, (u8
) (arg
>> 24));
102 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CMD2
, 0xFF, (u8
) (arg
>> 16));
103 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CMD3
, 0xFF, (u8
) (arg
>> 8));
104 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CMD4
, 0xFF, (u8
) arg
);
106 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CFG2
, 0xFF, rsp_type
);
107 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
,
108 0x01, PINGPONG_BUFFER
);
109 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_TRANSFER
,
110 0xFF, SD_TM_CMD_RSP
| SD_TRANSFER_START
);
111 rts51x_add_cmd(chip
, CHECK_REG_CMD
, SD_TRANSFER
, SD_TRANSFER_END
,
114 rts51x_add_cmd(chip
, READ_REG_CMD
, SD_STAT1
, 0, 0);
116 if (CHECK_USB(chip
, USB_20
)) {
117 if (rsp_type
== SD_RSP_TYPE_R2
) {
118 for (reg_addr
= PPBUF_BASE2
;
119 reg_addr
< PPBUF_BASE2
+ 16; reg_addr
++) {
120 rts51x_add_cmd(chip
, READ_REG_CMD
, reg_addr
, 0,
124 } else if (rsp_type
!= SD_RSP_TYPE_R0
) {
125 /* Read data from SD_CMDx registers */
126 for (reg_addr
= SD_CMD0
; reg_addr
<= SD_CMD4
;
128 rts51x_add_cmd(chip
, READ_REG_CMD
, reg_addr
, 0,
135 rts51x_add_cmd(chip
, READ_REG_CMD
, SD_CMD5
, 0, 0);
140 retval
= rts51x_send_cmd(chip
, MODE_CR
, 100);
141 if (retval
!= STATUS_SUCCESS
)
142 TRACE_RET(chip
, retval
);
144 retval
= rts51x_get_rsp(chip
, len
, timeout
);
146 if (CHECK_SD_TRANS_FAIL(chip
, retval
)) {
147 rts51x_clear_sd_error(chip
);
149 if (retval
== STATUS_TIMEDOUT
) {
150 if (rsp_type
& SD_WAIT_BUSY_END
) {
151 retval
= sd_check_data0_status(chip
);
152 if (retval
!= STATUS_SUCCESS
)
153 TRACE_RET(chip
, retval
);
156 TRACE_RET(chip
, STATUS_FAIL
);
159 if (rsp_type
== SD_RSP_TYPE_R0
)
160 return STATUS_SUCCESS
;
162 if (CHECK_USB(chip
, USB_20
)) {
163 rts51x_read_rsp_buf(chip
, 2, buf
, len
- 2);
165 if (rsp_type
== SD_RSP_TYPE_R2
) {
166 reg_addr
= PPBUF_BASE2
;
173 rts51x_seq_read_register(chip
, reg_addr
,
174 (unsigned short)len
, buf
);
175 if (retval
!= STATUS_SUCCESS
)
176 TRACE_RET(chip
, retval
);
177 RTS51X_READ_REG(chip
, SD_CMD5
, buf
+ len
);
179 stat
= chip
->rsp_buf
[1];
181 if ((buf
[0] & 0xC0) != 0)
182 TRACE_RET(chip
, STATUS_FAIL
);
184 if (!(rsp_type
& SD_NO_CHECK_CRC7
)) {
185 if (stat
& SD_CRC7_ERR
) {
186 if (cmd_idx
== WRITE_MULTIPLE_BLOCK
)
187 TRACE_RET(chip
, STATUS_FAIL
);
188 if (rty_cnt
< SD_MAX_RETRY_COUNT
) {
193 TRACE_RET(chip
, STATUS_FAIL
);
198 if ((cmd_idx
== SELECT_CARD
) || (cmd_idx
== APP_CMD
) ||
199 (cmd_idx
== SEND_STATUS
) || (cmd_idx
== STOP_TRANSMISSION
)) {
200 if ((cmd_idx
!= STOP_TRANSMISSION
) && (special_check
== 0)) {
202 TRACE_RET(chip
, STATUS_FAIL
);
205 TRACE_RET(chip
, STATUS_FAIL
);
207 TRACE_RET(chip
, STATUS_FAIL
);
209 if (cmd_idx
== SELECT_CARD
) {
210 if (rsp_type
== SD_RSP_TYPE_R2
) {
211 if ((buf
[3] & 0x1E) != 0x04)
212 TRACE_RET(chip
, STATUS_FAIL
);
213 } else if (rsp_type
== SD_RSP_TYPE_R2
) {
214 if ((buf
[3] & 0x1E) != 0x03)
215 TRACE_RET(chip
, STATUS_FAIL
);
221 memcpy(rsp
, buf
, rsp_len
);
223 return STATUS_SUCCESS
;
226 static int ext_sd_get_rsp(struct rts51x_chip
*chip
, int len
,
227 u8
*rsp
, u8 rsp_type
)
232 if (rsp_type
== SD_RSP_TYPE_R0
)
233 return STATUS_SUCCESS
;
235 rts51x_init_cmd(chip
);
237 if (rsp_type
== SD_RSP_TYPE_R2
) {
238 for (reg_addr
= PPBUF_BASE2
; reg_addr
< PPBUF_BASE2
+ 16;
240 rts51x_add_cmd(chip
, READ_REG_CMD
, reg_addr
, 0xFF, 0);
243 } else if (rsp_type
!= SD_RSP_TYPE_R0
) {
244 for (reg_addr
= SD_CMD0
; reg_addr
<= SD_CMD4
; reg_addr
++)
245 rts51x_add_cmd(chip
, READ_REG_CMD
, reg_addr
, 0xFF, 0);
248 rts51x_add_cmd(chip
, READ_REG_CMD
, SD_CMD5
, 0xFF, 0);
250 retval
= rts51x_send_cmd(chip
, MODE_CR
, 100);
251 if (retval
!= STATUS_SUCCESS
)
252 TRACE_RET(chip
, retval
);
254 retval
= rts51x_get_rsp(chip
, rsp_len
, 100);
256 if (retval
!= STATUS_SUCCESS
)
257 TRACE_RET(chip
, retval
);
260 int min_len
= (rsp_len
< len
) ? rsp_len
: len
;
262 memcpy(rsp
, rts51x_get_rsp_data(chip
), min_len
);
264 RTS51X_DEBUGP("min_len = %d\n", min_len
);
265 RTS51X_DEBUGP("Response in cmd buf: 0x%x 0x%x 0x%x 0x%x\n",
266 rsp
[0], rsp
[1], rsp
[2], rsp
[3]);
269 return STATUS_SUCCESS
;
272 int ext_rts51x_sd_execute_no_data(struct rts51x_chip
*chip
, unsigned int lun
,
273 u8 cmd_idx
, u8 standby
, u8 acmd
, u8 rsp_code
,
276 struct sd_info
*sd_card
= &(chip
->sd_card
);
280 retval
= rts51x_sd_switch_clock(chip
);
281 if (retval
!= STATUS_SUCCESS
)
282 TRACE_RET(chip
, TRANSPORT_FAILED
);
284 if (sd_card
->pre_cmd_err
) {
285 sd_card
->pre_cmd_err
= 0;
286 rts51x_set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_CHANGE
);
287 TRACE_RET(chip
, TRANSPORT_FAILED
);
289 retval
= get_rsp_type(rsp_code
, &rsp_type
, &rsp_len
);
290 if (retval
!= STATUS_SUCCESS
) {
291 rts51x_set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
292 TRACE_RET(chip
, TRANSPORT_FAILED
);
294 sd_card
->last_rsp_type
= rsp_type
;
296 retval
= rts51x_sd_switch_clock(chip
);
297 if (retval
!= STATUS_SUCCESS
)
298 TRACE_RET(chip
, TRANSPORT_FAILED
);
299 /* Set H/W SD/MMC Bus Width */
300 rts51x_write_register(chip
, SD_CFG1
, 0x03, SD_BUS_WIDTH_4
);
303 retval
= rts51x_sd_select_card(chip
, 0);
304 if (retval
!= STATUS_SUCCESS
)
305 TRACE_GOTO(chip
, SD_Execute_Cmd_Failed
);
310 ext_sd_send_cmd_get_rsp(chip
, APP_CMD
, sd_card
->sd_addr
,
311 SD_RSP_TYPE_R1
, NULL
, 0, 0);
312 if (retval
!= STATUS_SUCCESS
)
313 TRACE_GOTO(chip
, SD_Execute_Cmd_Failed
);
316 retval
= ext_sd_send_cmd_get_rsp(chip
, cmd_idx
, arg
, rsp_type
,
317 sd_card
->rsp
, rsp_len
, 0);
318 if (retval
!= STATUS_SUCCESS
)
319 TRACE_GOTO(chip
, SD_Execute_Cmd_Failed
);
322 retval
= rts51x_sd_select_card(chip
, 1);
323 if (retval
!= STATUS_SUCCESS
)
324 TRACE_GOTO(chip
, SD_Execute_Cmd_Failed
);
327 return TRANSPORT_GOOD
;
329 SD_Execute_Cmd_Failed
:
330 sd_card
->pre_cmd_err
= 1;
331 rts51x_set_sense_type(chip
, lun
, SENSE_TYPE_NO_SENSE
);
332 rts51x_release_sd_card(chip
);
333 rts51x_do_rts51x_reset_sd_card(chip
);
334 if (!(chip
->card_ready
& SD_CARD
))
335 rts51x_set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_NOT_PRESENT
);
337 TRACE_RET(chip
, TRANSPORT_FAILED
);
340 int ext_rts51x_sd_execute_read_data(struct rts51x_chip
*chip
, unsigned int lun
,
341 u8 cmd_idx
, u8 cmd12
, u8 standby
,
342 u8 acmd
, u8 rsp_code
, u32 arg
, u32 data_len
,
343 void *data_buf
, unsigned int buf_len
, int use_sg
)
345 struct sd_info
*sd_card
= &(chip
->sd_card
);
346 int retval
, rsp_len
, i
;
347 int cmd13_checkbit
= 0, read_err
= 0;
348 u8 rsp_type
, bus_width
;
350 if (sd_card
->pre_cmd_err
) {
351 sd_card
->pre_cmd_err
= 0;
352 rts51x_set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_CHANGE
);
353 TRACE_RET(chip
, TRANSPORT_FAILED
);
356 retval
= rts51x_sd_switch_clock(chip
);
357 if (retval
!= STATUS_SUCCESS
)
358 TRACE_RET(chip
, STATUS_FAIL
);
359 retval
= get_rsp_type(rsp_code
, &rsp_type
, &rsp_len
);
360 if (retval
!= STATUS_SUCCESS
) {
361 rts51x_set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
362 TRACE_RET(chip
, TRANSPORT_FAILED
);
364 sd_card
->last_rsp_type
= rsp_type
;
366 retval
= rts51x_sd_switch_clock(chip
);
367 if (retval
!= STATUS_SUCCESS
)
368 TRACE_RET(chip
, TRANSPORT_FAILED
);
369 bus_width
= SD_BUS_WIDTH_4
;
371 if (data_len
< 512) {
372 retval
= ext_sd_send_cmd_get_rsp(chip
, SET_BLOCKLEN
, data_len
,
373 SD_RSP_TYPE_R1
, NULL
, 0, 0);
374 if (retval
!= STATUS_SUCCESS
)
375 TRACE_GOTO(chip
, SD_Execute_Read_Cmd_Failed
);
379 retval
= rts51x_sd_select_card(chip
, 0);
380 if (retval
!= STATUS_SUCCESS
)
381 TRACE_GOTO(chip
, SD_Execute_Read_Cmd_Failed
);
386 ext_sd_send_cmd_get_rsp(chip
, APP_CMD
, sd_card
->sd_addr
,
387 SD_RSP_TYPE_R1
, NULL
, 0, 0);
388 if (retval
!= STATUS_SUCCESS
)
389 TRACE_GOTO(chip
, SD_Execute_Read_Cmd_Failed
);
392 if (data_len
<= 512) {
395 u16 byte_cnt
, blk_cnt
;
397 unsigned int offset
= 0;
400 byte_cnt
= (u16
) (data_len
& 0x3FF);
403 cmd
[0] = 0x40 | cmd_idx
;
404 cmd
[1] = (u8
) (arg
>> 24);
405 cmd
[2] = (u8
) (arg
>> 16);
406 cmd
[3] = (u8
) (arg
>> 8);
409 buf
= kmalloc(data_len
, GFP_KERNEL
);
411 TRACE_RET(chip
, TRANSPORT_ERROR
);
413 retval
= sd_read_data(chip
, SD_TM_NORMAL_READ
, cmd
, 5, byte_cnt
,
414 blk_cnt
, bus_width
, buf
, data_len
, 2000);
415 if (retval
!= STATUS_SUCCESS
) {
418 rts51x_write_register(chip
, CARD_STOP
,
419 SD_STOP
| SD_CLR_ERR
,
420 SD_STOP
| SD_CLR_ERR
);
421 TRACE_GOTO(chip
, SD_Execute_Read_Cmd_Failed
);
424 min_len
= min(data_len
, buf_len
);
426 rts51x_access_sglist(buf
, min_len
, (void *)data_buf
,
427 &sg
, &offset
, TO_XFER_BUF
);
429 memcpy(data_buf
, buf
, min_len
);
432 } else if (!(data_len
& 0x1FF)) {
433 rts51x_init_cmd(chip
);
434 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_BYTE_CNT_H
, 0xFF, 0x02);
435 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_BYTE_CNT_L
, 0xFF, 0x00);
436 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_BLOCK_CNT_H
,
437 0xFF, (u8
) (data_len
>> 17));
438 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_BLOCK_CNT_L
,
439 0xFF, (u8
) ((data_len
& 0x0001FE00) >> 9));
440 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CMD0
, 0xFF,
442 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CMD1
, 0xFF,
444 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CMD2
, 0xFF,
446 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CMD3
, 0xFF,
448 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CMD4
, 0xFF, (u8
) arg
);
449 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CFG1
, 0x03, bus_width
);
450 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_CFG2
, 0xFF, rsp_type
);
451 rts51x_trans_dma_enable(DMA_FROM_DEVICE
, chip
, data_len
, DMA_512
);
452 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_TRANSFER
, 0xFF,
453 SD_TM_AUTO_READ_2
| SD_TRANSFER_START
);
454 rts51x_add_cmd(chip
, CHECK_REG_CMD
, SD_TRANSFER
,
455 SD_TRANSFER_END
, SD_TRANSFER_END
);
456 retval
= rts51x_send_cmd(chip
, MODE_CDIR
, 100);
457 if (retval
!= STATUS_SUCCESS
) {
459 rts51x_ep0_write_register(chip
, CARD_STOP
,
460 SD_STOP
| SD_CLR_ERR
,
461 SD_STOP
| SD_CLR_ERR
);
462 TRACE_GOTO(chip
, SD_Execute_Read_Cmd_Failed
);
466 rts51x_transfer_data_rcc(chip
, RCV_BULK_PIPE(chip
),
467 data_buf
, buf_len
, use_sg
, NULL
,
469 if (retval
!= STATUS_SUCCESS
) {
471 rts51x_ep0_write_register(chip
, CARD_STOP
,
472 SD_STOP
| SD_CLR_ERR
,
473 SD_STOP
| SD_CLR_ERR
);
474 TRACE_GOTO(chip
, SD_Execute_Read_Cmd_Failed
);
476 retval
= rts51x_get_rsp(chip
, 1, 500);
477 if (CHECK_SD_TRANS_FAIL(chip
, retval
)) {
479 rts51x_ep0_write_register(chip
, CARD_STOP
,
480 SD_STOP
| SD_CLR_ERR
,
481 SD_STOP
| SD_CLR_ERR
);
482 TRACE_GOTO(chip
, SD_Execute_Read_Cmd_Failed
);
485 TRACE_GOTO(chip
, SD_Execute_Read_Cmd_Failed
);
488 retval
= ext_sd_get_rsp(chip
, rsp_len
, sd_card
->rsp
, rsp_type
);
489 if (retval
!= STATUS_SUCCESS
)
490 TRACE_GOTO(chip
, SD_Execute_Read_Cmd_Failed
);
493 retval
= rts51x_sd_select_card(chip
, 1);
494 if (retval
!= STATUS_SUCCESS
)
495 TRACE_GOTO(chip
, SD_Execute_Read_Cmd_Failed
);
499 retval
= ext_sd_send_cmd_get_rsp(chip
, STOP_TRANSMISSION
,
500 0, SD_RSP_TYPE_R1b
, NULL
, 0,
502 if (retval
!= STATUS_SUCCESS
)
503 TRACE_GOTO(chip
, SD_Execute_Read_Cmd_Failed
);
506 if (data_len
< 512) {
507 retval
= ext_sd_send_cmd_get_rsp(chip
, SET_BLOCKLEN
, 0x200,
508 SD_RSP_TYPE_R1
, NULL
, 0, 0);
509 if (retval
!= STATUS_SUCCESS
)
510 TRACE_GOTO(chip
, SD_Execute_Read_Cmd_Failed
);
512 rts51x_write_register(chip
, SD_BYTE_CNT_H
, 0xFF, 0x02);
513 rts51x_write_register(chip
, SD_BYTE_CNT_L
, 0xFF, 0x00);
516 if (standby
|| cmd12
)
519 for (i
= 0; i
< 3; i
++) {
521 ext_sd_send_cmd_get_rsp(chip
, SEND_STATUS
, sd_card
->sd_addr
,
522 SD_RSP_TYPE_R1
, NULL
, 0,
524 if (retval
== STATUS_SUCCESS
)
527 if (retval
!= STATUS_SUCCESS
)
528 TRACE_GOTO(chip
, SD_Execute_Read_Cmd_Failed
);
530 return TRANSPORT_GOOD
;
532 SD_Execute_Read_Cmd_Failed
:
533 sd_card
->pre_cmd_err
= 1;
534 rts51x_set_sense_type(chip
, lun
, SENSE_TYPE_NO_SENSE
);
536 rts51x_set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR
);
537 rts51x_release_sd_card(chip
);
538 rts51x_do_rts51x_reset_sd_card(chip
);
539 if (!(chip
->card_ready
& SD_CARD
))
540 rts51x_set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_NOT_PRESENT
);
542 TRACE_RET(chip
, TRANSPORT_FAILED
);
545 int ext_rts51x_sd_execute_write_data(struct rts51x_chip
*chip
, unsigned int lun
,
546 u8 cmd_idx
, u8 cmd12
, u8 standby
, u8 acmd
,
547 u8 rsp_code
, u32 arg
, u32 data_len
,
548 void *data_buf
, unsigned int buf_len
, int use_sg
)
550 struct sd_info
*sd_card
= &(chip
->sd_card
);
552 int cmd13_checkbit
= 0, write_err
= 0;
556 if (sd_card
->pre_cmd_err
) {
557 sd_card
->pre_cmd_err
= 0;
558 rts51x_set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_CHANGE
);
559 TRACE_RET(chip
, TRANSPORT_FAILED
);
562 retval
= rts51x_sd_switch_clock(chip
);
563 if (retval
!= STATUS_SUCCESS
)
564 TRACE_RET(chip
, STATUS_FAIL
);
566 retval
= get_rsp_type(rsp_code
, &rsp_type
, &rsp_len
);
567 if (retval
!= STATUS_SUCCESS
) {
568 rts51x_set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
569 TRACE_RET(chip
, TRANSPORT_FAILED
);
571 sd_card
->last_rsp_type
= rsp_type
;
573 retval
= rts51x_sd_switch_clock(chip
);
574 if (retval
!= STATUS_SUCCESS
)
575 TRACE_RET(chip
, TRANSPORT_FAILED
);
576 rts51x_write_register(chip
, SD_CFG1
, 0x03, SD_BUS_WIDTH_4
);
578 if (data_len
< 512) {
579 retval
= ext_sd_send_cmd_get_rsp(chip
, SET_BLOCKLEN
, data_len
,
580 SD_RSP_TYPE_R1
, NULL
, 0, 0);
581 if (retval
!= STATUS_SUCCESS
)
582 TRACE_GOTO(chip
, SD_Execute_Write_Cmd_Failed
);
586 retval
= rts51x_sd_select_card(chip
, 0);
587 if (retval
!= STATUS_SUCCESS
)
588 TRACE_GOTO(chip
, SD_Execute_Write_Cmd_Failed
);
593 ext_sd_send_cmd_get_rsp(chip
, APP_CMD
, sd_card
->sd_addr
,
594 SD_RSP_TYPE_R1
, NULL
, 0, 0);
595 if (retval
!= STATUS_SUCCESS
)
596 TRACE_GOTO(chip
, SD_Execute_Write_Cmd_Failed
);
599 retval
= ext_sd_send_cmd_get_rsp(chip
, cmd_idx
, arg
, rsp_type
,
600 sd_card
->rsp
, rsp_len
, 0);
601 if (retval
!= STATUS_SUCCESS
)
602 TRACE_GOTO(chip
, SD_Execute_Write_Cmd_Failed
);
604 if (data_len
<= 512) {
606 unsigned int offset
= 0;
609 buf
= kmalloc(data_len
, GFP_KERNEL
);
611 TRACE_RET(chip
, TRANSPORT_ERROR
);
614 rts51x_access_sglist(buf
, data_len
, (void *)data_buf
,
615 &sg
, &offset
, FROM_XFER_BUF
);
617 memcpy(buf
, data_buf
, data_len
);
620 if (data_len
> 256) {
621 rts51x_init_cmd(chip
);
622 for (i
= 0; i
< 256; i
++) {
623 rts51x_add_cmd(chip
, WRITE_REG_CMD
,
624 (u16
) (PPBUF_BASE2
+ i
), 0xFF,
627 retval
= rts51x_send_cmd(chip
, MODE_C
, 250);
628 if (retval
!= STATUS_SUCCESS
) {
630 TRACE_GOTO(chip
, SD_Execute_Write_Cmd_Failed
);
633 rts51x_init_cmd(chip
);
634 for (i
= 256; i
< data_len
; i
++) {
635 rts51x_add_cmd(chip
, WRITE_REG_CMD
,
636 (u16
) (PPBUF_BASE2
+ i
), 0xFF,
639 retval
= rts51x_send_cmd(chip
, MODE_C
, 250);
640 if (retval
!= STATUS_SUCCESS
) {
642 TRACE_GOTO(chip
, SD_Execute_Write_Cmd_Failed
);
645 rts51x_init_cmd(chip
);
646 for (i
= 0; i
< data_len
; i
++) {
647 rts51x_add_cmd(chip
, WRITE_REG_CMD
,
648 (u16
) (PPBUF_BASE2
+ i
), 0xFF,
651 retval
= rts51x_send_cmd(chip
, MODE_C
, 250);
652 if (retval
!= STATUS_SUCCESS
) {
654 TRACE_GOTO(chip
, SD_Execute_Write_Cmd_Failed
);
660 rts51x_init_cmd(chip
);
662 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_BYTE_CNT_H
, 0xFF,
663 (u8
) ((data_len
>> 8) & 0x03));
664 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_BYTE_CNT_L
, 0xFF,
666 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_BLOCK_CNT_H
, 0xFF, 0x00);
667 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_BLOCK_CNT_L
, 0xFF, 0x01);
668 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
, 0x01,
671 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_TRANSFER
, 0xFF,
672 SD_TM_AUTO_WRITE_3
| SD_TRANSFER_START
);
673 rts51x_add_cmd(chip
, CHECK_REG_CMD
, SD_TRANSFER
,
674 SD_TRANSFER_END
, SD_TRANSFER_END
);
676 retval
= rts51x_send_cmd(chip
, MODE_CR
, 100);
677 if (retval
!= STATUS_SUCCESS
)
678 TRACE_GOTO(chip
, SD_Execute_Write_Cmd_Failed
);
680 retval
= rts51x_get_rsp(chip
, 1, 250);
681 if (CHECK_SD_TRANS_FAIL(chip
, retval
))
682 TRACE_GOTO(chip
, SD_Execute_Write_Cmd_Failed
);
683 } else if (!(data_len
& 0x1FF)) {
684 rts51x_init_cmd(chip
);
686 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_BYTE_CNT_H
, 0xFF, 0x02);
687 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_BYTE_CNT_L
, 0xFF, 0x00);
688 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_BLOCK_CNT_H
,
689 0xFF, (u8
) (data_len
>> 17));
690 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_BLOCK_CNT_L
,
691 0xFF, (u8
) ((data_len
& 0x0001FE00) >> 9));
693 rts51x_trans_dma_enable(DMA_TO_DEVICE
, chip
, data_len
, DMA_512
);
695 rts51x_add_cmd(chip
, WRITE_REG_CMD
, SD_TRANSFER
, 0xFF,
696 SD_TM_AUTO_WRITE_3
| SD_TRANSFER_START
);
697 rts51x_add_cmd(chip
, CHECK_REG_CMD
, SD_TRANSFER
,
698 SD_TRANSFER_END
, SD_TRANSFER_END
);
700 retval
= rts51x_send_cmd(chip
, MODE_CDOR
, 100);
701 if (retval
!= STATUS_SUCCESS
)
702 TRACE_GOTO(chip
, SD_Execute_Write_Cmd_Failed
);
705 rts51x_transfer_data_rcc(chip
, SND_BULK_PIPE(chip
),
706 data_buf
, buf_len
, use_sg
, NULL
,
708 if (retval
!= STATUS_SUCCESS
)
709 TRACE_GOTO(chip
, SD_Execute_Write_Cmd_Failed
);
711 retval
= rts51x_get_rsp(chip
, 1, 10000);
712 if (CHECK_SD_TRANS_FAIL(chip
, retval
))
713 TRACE_GOTO(chip
, SD_Execute_Write_Cmd_Failed
);
716 TRACE_GOTO(chip
, SD_Execute_Write_Cmd_Failed
);
721 rts51x_write_register(chip
, CARD_STOP
, SD_STOP
| SD_CLR_ERR
,
722 SD_STOP
| SD_CLR_ERR
);
723 TRACE_GOTO(chip
, SD_Execute_Write_Cmd_Failed
);
727 retval
= rts51x_sd_select_card(chip
, 1);
728 if (retval
!= STATUS_SUCCESS
)
729 TRACE_GOTO(chip
, SD_Execute_Write_Cmd_Failed
);
733 retval
= ext_sd_send_cmd_get_rsp(chip
, STOP_TRANSMISSION
,
734 0, SD_RSP_TYPE_R1b
, NULL
, 0,
736 if (retval
!= STATUS_SUCCESS
)
737 TRACE_GOTO(chip
, SD_Execute_Write_Cmd_Failed
);
740 if (data_len
< 512) {
741 retval
= ext_sd_send_cmd_get_rsp(chip
, SET_BLOCKLEN
, 0x200,
742 SD_RSP_TYPE_R1
, NULL
, 0, 0);
743 if (retval
!= STATUS_SUCCESS
)
744 TRACE_GOTO(chip
, SD_Execute_Write_Cmd_Failed
);
746 rts51x_write_register(chip
, SD_BYTE_CNT_H
, 0xFF, 0x02);
747 rts51x_write_register(chip
, SD_BYTE_CNT_L
, 0xFF, 0x00);
750 if (cmd12
|| standby
) {
751 /* There is CMD7 or CMD12 sent before CMD13 */
755 for (i
= 0; i
< 3; i
++) {
757 ext_sd_send_cmd_get_rsp(chip
, SEND_STATUS
, sd_card
->sd_addr
,
758 SD_RSP_TYPE_R1
, NULL
, 0,
760 if (retval
== STATUS_SUCCESS
)
763 if (retval
!= STATUS_SUCCESS
)
764 TRACE_GOTO(chip
, SD_Execute_Write_Cmd_Failed
);
766 return TRANSPORT_GOOD
;
768 SD_Execute_Write_Cmd_Failed
:
769 sd_card
->pre_cmd_err
= 1;
770 rts51x_set_sense_type(chip
, lun
, SENSE_TYPE_NO_SENSE
);
772 rts51x_set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_WRITE_ERR
);
773 rts51x_release_sd_card(chip
);
774 rts51x_do_rts51x_reset_sd_card(chip
);
775 if (!(chip
->card_ready
& SD_CARD
))
776 rts51x_set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_NOT_PRESENT
);
778 TRACE_RET(chip
, TRANSPORT_FAILED
);
781 int rts51x_sd_pass_thru_mode(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
)
783 struct sd_info
*sd_card
= &(chip
->sd_card
);
784 unsigned int lun
= SCSI_LUN(srb
);
791 0x00, /* Version Number */
792 0x00, /* WP | Media Type */
793 0x00, /* RCA (Low byte) */
794 0x00, /* RCA (High byte) */
802 0x00, /* Max LUN Number */
807 sd_card
->pre_cmd_err
= 0;
809 if (!(CHK_BIT(chip
->lun_mc
, lun
))) {
810 SET_BIT(chip
->lun_mc
, lun
);
811 rts51x_set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_CHANGE
);
812 TRACE_RET(chip
, TRANSPORT_FAILED
);
815 if ((0x53 != srb
->cmnd
[2]) || (0x44 != srb
->cmnd
[3])
816 || (0x20 != srb
->cmnd
[4]) || (0x43 != srb
->cmnd
[5])
817 || (0x61 != srb
->cmnd
[6]) || (0x72 != srb
->cmnd
[7])
818 || (0x64 != srb
->cmnd
[8])) {
819 rts51x_set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
820 TRACE_RET(chip
, TRANSPORT_FAILED
);
823 switch (srb
->cmnd
[1] & 0x0F) {
825 sd_card
->sd_pass_thru_en
= 0;
829 sd_card
->sd_pass_thru_en
= 1;
833 rts51x_set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
834 TRACE_RET(chip
, TRANSPORT_FAILED
);
837 /* 0x01:SD Memory Card; 0x02:Other Media; 0x03:Illegal Media; */
838 buf
[5] = (1 == CHK_SD(sd_card
)) ? 0x01 : 0x02;
839 if (chip
->card_wp
& SD_CARD
)
842 buf
[6] = (u8
) (sd_card
->sd_addr
>> 16);
843 buf
[7] = (u8
) (sd_card
->sd_addr
>> 24);
845 buf
[15] = chip
->max_lun
;
847 len
= min_t(unsigned, 18, scsi_bufflen(srb
));
848 rts51x_set_xfer_buf(buf
, len
, srb
);
850 return TRANSPORT_GOOD
;
853 int rts51x_sd_execute_no_data(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
)
855 struct sd_info
*sd_card
= &(chip
->sd_card
);
856 unsigned int lun
= SCSI_LUN(srb
);
858 u8 cmd_idx
, rsp_code
;
859 u8 standby
= 0, acmd
= 0;
862 if (!sd_card
->sd_pass_thru_en
) {
863 rts51x_set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
864 TRACE_RET(chip
, TRANSPORT_FAILED
);
867 cmd_idx
= srb
->cmnd
[2] & 0x3F;
868 if (srb
->cmnd
[1] & 0x02)
870 if (srb
->cmnd
[1] & 0x01)
873 arg
= ((u32
) srb
->cmnd
[3] << 24) | ((u32
) srb
->cmnd
[4] << 16) |
874 ((u32
) srb
->cmnd
[5] << 8) | srb
->cmnd
[6];
876 rsp_code
= srb
->cmnd
[10];
879 ext_rts51x_sd_execute_no_data(chip
, lun
, cmd_idx
, standby
, acmd
, rsp_code
,
881 scsi_set_resid(srb
, 0);
885 int rts51x_sd_execute_read_data(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
)
887 struct sd_info
*sd_card
= &(chip
->sd_card
);
889 unsigned int lun
= SCSI_LUN(srb
);
890 u8 cmd_idx
, rsp_code
, send_cmd12
= 0, standby
= 0, acmd
= 0;
893 if (!sd_card
->sd_pass_thru_en
) {
894 rts51x_set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
895 TRACE_RET(chip
, TRANSPORT_FAILED
);
898 cmd_idx
= srb
->cmnd
[2] & 0x3F;
899 if (srb
->cmnd
[1] & 0x04)
901 if (srb
->cmnd
[1] & 0x02)
903 if (srb
->cmnd
[1] & 0x01)
906 arg
= ((u32
) srb
->cmnd
[3] << 24) | ((u32
) srb
->cmnd
[4] << 16) |
907 ((u32
) srb
->cmnd
[5] << 8) | srb
->cmnd
[6];
910 ((u32
) srb
->cmnd
[7] << 16) | ((u32
) srb
->cmnd
[8] << 8) |
912 rsp_code
= srb
->cmnd
[10];
915 ext_rts51x_sd_execute_read_data(chip
, lun
, cmd_idx
, send_cmd12
, standby
,
916 acmd
, rsp_code
, arg
, data_len
,
917 scsi_sglist(srb
), scsi_bufflen(srb
),
919 scsi_set_resid(srb
, 0);
923 int rts51x_sd_execute_write_data(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
)
925 struct sd_info
*sd_card
= &(chip
->sd_card
);
927 unsigned int lun
= SCSI_LUN(srb
);
928 u8 cmd_idx
, rsp_code
, send_cmd12
= 0, standby
= 0, acmd
= 0;
931 if (!sd_card
->sd_pass_thru_en
) {
932 rts51x_set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
933 TRACE_RET(chip
, TRANSPORT_FAILED
);
936 cmd_idx
= srb
->cmnd
[2] & 0x3F;
937 if (srb
->cmnd
[1] & 0x04)
939 if (srb
->cmnd
[1] & 0x02)
941 if (srb
->cmnd
[1] & 0x01)
945 ((u32
) srb
->cmnd
[7] << 16) | ((u32
) srb
->cmnd
[8] << 8) |
948 ((u32
) srb
->cmnd
[3] << 24) | ((u32
) srb
->cmnd
[4] << 16) |
949 ((u32
) srb
->cmnd
[5] << 8) | srb
->cmnd
[6];
950 rsp_code
= srb
->cmnd
[10];
953 ext_rts51x_sd_execute_write_data(chip
, lun
, cmd_idx
, send_cmd12
, standby
,
954 acmd
, rsp_code
, arg
, data_len
,
955 scsi_sglist(srb
), scsi_bufflen(srb
),
957 scsi_set_resid(srb
, 0);
961 int rts51x_sd_get_cmd_rsp(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
)
963 struct sd_info
*sd_card
= &(chip
->sd_card
);
964 unsigned int lun
= SCSI_LUN(srb
);
968 if (!sd_card
->sd_pass_thru_en
) {
969 rts51x_set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
970 TRACE_RET(chip
, TRANSPORT_FAILED
);
973 if (sd_card
->pre_cmd_err
) {
974 sd_card
->pre_cmd_err
= 0;
975 rts51x_set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_CHANGE
);
976 TRACE_RET(chip
, TRANSPORT_FAILED
);
979 data_len
= ((u16
) srb
->cmnd
[7] << 8) | srb
->cmnd
[8];
981 if (sd_card
->last_rsp_type
== SD_RSP_TYPE_R0
) {
982 rts51x_set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
983 TRACE_RET(chip
, TRANSPORT_FAILED
);
984 } else if (sd_card
->last_rsp_type
== SD_RSP_TYPE_R2
) {
985 count
= (data_len
< 17) ? data_len
: 17;
987 count
= (data_len
< 6) ? data_len
: 6;
989 rts51x_set_xfer_buf(sd_card
->rsp
, count
, srb
);
991 RTS51X_DEBUGP("Response length: %d\n", data_len
);
992 RTS51X_DEBUGP("Response: 0x%x 0x%x 0x%x 0x%x\n",
993 sd_card
->rsp
[0], sd_card
->rsp
[1], sd_card
->rsp
[2],
996 scsi_set_resid(srb
, 0);
997 return TRANSPORT_GOOD
;
1000 int rts51x_sd_hw_rst(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
)
1002 struct sd_info
*sd_card
= &(chip
->sd_card
);
1003 unsigned int lun
= SCSI_LUN(srb
);
1006 if (!sd_card
->sd_pass_thru_en
) {
1007 rts51x_set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
1008 TRACE_RET(chip
, TRANSPORT_FAILED
);
1011 if (sd_card
->pre_cmd_err
) {
1012 sd_card
->pre_cmd_err
= 0;
1013 rts51x_set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_CHANGE
);
1014 TRACE_RET(chip
, TRANSPORT_FAILED
);
1017 if ((0x53 != srb
->cmnd
[2]) || (0x44 != srb
->cmnd
[3])
1018 || (0x20 != srb
->cmnd
[4]) || (0x43 != srb
->cmnd
[5])
1019 || (0x61 != srb
->cmnd
[6]) || (0x72 != srb
->cmnd
[7])
1020 || (0x64 != srb
->cmnd
[8])) {
1021 rts51x_set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
1022 TRACE_RET(chip
, TRANSPORT_FAILED
);
1025 switch (srb
->cmnd
[1] & 0x0F) {
1027 /* SD Card Power Off -> ON and Initialization */
1028 retval
= rts51x_reset_sd_card(chip
);
1029 if (retval
!= STATUS_SUCCESS
) {
1030 rts51x_set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_NOT_PRESENT
);
1031 sd_card
->pre_cmd_err
= 1;
1032 TRACE_RET(chip
, TRANSPORT_FAILED
);
1037 /* reset CMD(CMD0) and Initialization
1038 * (without SD Card Power Off -> ON) */
1039 retval
= reset_sd(chip
);
1040 if (retval
!= STATUS_SUCCESS
) {
1041 rts51x_set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_NOT_PRESENT
);
1042 sd_card
->pre_cmd_err
= 1;
1043 TRACE_RET(chip
, TRANSPORT_FAILED
);
1048 rts51x_set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
1049 TRACE_RET(chip
, TRANSPORT_FAILED
);
1052 scsi_set_resid(srb
, 0);
1053 return TRANSPORT_GOOD
;