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>
30 #include <linux/vmalloc.h>
35 #include "rts51x_transport.h"
36 #include "rts51x_scsi.h"
37 #include "rts51x_card.h"
40 static inline void ms_set_err_code(struct rts51x_chip
*chip
, u8 err_code
)
42 struct ms_info
*ms_card
= &(chip
->ms_card
);
44 ms_card
->err_code
= err_code
;
47 static inline int ms_check_err_code(struct rts51x_chip
*chip
, u8 err_code
)
49 struct ms_info
*ms_card
= &(chip
->ms_card
);
51 return ms_card
->err_code
== err_code
;
54 static int ms_parse_err_code(struct rts51x_chip
*chip
)
56 TRACE_RET(chip
, STATUS_FAIL
);
59 static int ms_transfer_tpc(struct rts51x_chip
*chip
, u8 trans_mode
, u8 tpc
,
62 struct ms_info
*ms_card
= &(chip
->ms_card
);
65 RTS51X_DEBUGP("ms_transfer_tpc: tpc = 0x%x\n", tpc
);
67 rts51x_init_cmd(chip
);
69 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TPC
, 0xFF, tpc
);
70 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_BYTE_CNT
, 0xFF, cnt
);
71 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANS_CFG
, 0xFF, cfg
);
72 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
, 0x01,
75 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANSFER
, 0xFF,
76 MS_TRANSFER_START
| trans_mode
);
77 rts51x_add_cmd(chip
, CHECK_REG_CMD
, MS_TRANSFER
, MS_TRANSFER_END
,
80 rts51x_add_cmd(chip
, READ_REG_CMD
, MS_TRANS_CFG
, 0, 0);
82 retval
= rts51x_send_cmd(chip
, MODE_CR
, 100);
83 if (retval
!= STATUS_SUCCESS
)
84 TRACE_RET(chip
, retval
);
86 retval
= rts51x_get_rsp(chip
, 2, 5000);
87 if (CHECK_MS_TRANS_FAIL(chip
, retval
)) {
88 rts51x_clear_ms_error(chip
);
89 ms_set_err_code(chip
, MS_TO_ERROR
);
90 TRACE_RET(chip
, ms_parse_err_code(chip
));
93 if (!(tpc
& 0x08)) { /* Read Packet */
94 /* Check CRC16 & Ready Timeout */
95 if (chip
->rsp_buf
[1] & MS_CRC16_ERR
) {
96 ms_set_err_code(chip
, MS_CRC16_ERROR
);
97 TRACE_RET(chip
, ms_parse_err_code(chip
));
99 } else { /* Write Packet */
100 if (CHK_MSPRO(ms_card
) && !(chip
->rsp_buf
[1] & 0x80)) {
101 if (chip
->rsp_buf
[1] & (MS_INT_ERR
| MS_INT_CMDNK
)) {
102 ms_set_err_code(chip
, MS_CMD_NK
);
103 TRACE_RET(chip
, ms_parse_err_code(chip
));
108 /* Check Timeout of Ready Signal */
109 if (chip
->rsp_buf
[1] & MS_RDY_TIMEOUT
) {
110 rts51x_clear_ms_error(chip
);
111 ms_set_err_code(chip
, MS_TO_ERROR
);
112 TRACE_RET(chip
, ms_parse_err_code(chip
));
115 return STATUS_SUCCESS
;
118 int ms_transfer_data(struct rts51x_chip
*chip
, u8 trans_mode
, u8 tpc
,
119 u16 sec_cnt
, u8 cfg
, int mode_2k
, int use_sg
, void *buf
,
122 struct ms_info
*ms_card
= &(chip
->ms_card
);
124 u8 val
, err_code
= 0, flag
= 0;
125 enum dma_data_direction dir
;
128 if (!buf
|| !buf_len
)
129 TRACE_RET(chip
, STATUS_FAIL
);
131 if (trans_mode
== MS_TM_AUTO_READ
) {
132 pipe
= RCV_BULK_PIPE(chip
);
133 dir
= DMA_FROM_DEVICE
;
135 err_code
= MS_FLASH_READ_ERROR
;
136 } else if (trans_mode
== MS_TM_AUTO_WRITE
) {
137 pipe
= SND_BULK_PIPE(chip
);
140 err_code
= MS_FLASH_WRITE_ERROR
;
142 TRACE_RET(chip
, STATUS_FAIL
);
145 rts51x_init_cmd(chip
);
147 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TPC
, 0xFF, tpc
);
148 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_SECTOR_CNT_H
, 0xFF,
149 (u8
) (sec_cnt
>> 8));
150 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_SECTOR_CNT_L
, 0xFF,
152 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANS_CFG
, 0xFF, cfg
);
153 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
, 0x01,
157 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_CFG
, MS_2K_SECTOR_MODE
,
160 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_CFG
, MS_2K_SECTOR_MODE
,
163 rts51x_trans_dma_enable(dir
, chip
, sec_cnt
* 512, DMA_512
);
165 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANSFER
, 0xFF,
166 MS_TRANSFER_START
| trans_mode
);
167 rts51x_add_cmd(chip
, CHECK_REG_CMD
, MS_TRANSFER
, MS_TRANSFER_END
,
170 retval
= rts51x_send_cmd(chip
, flag
| STAGE_MS_STATUS
, 100);
171 if (retval
!= STATUS_SUCCESS
)
172 TRACE_RET(chip
, retval
);
175 rts51x_transfer_data_rcc(chip
, pipe
, buf
, buf_len
, use_sg
, NULL
,
177 if (retval
!= STATUS_SUCCESS
) {
178 ms_set_err_code(chip
, err_code
);
179 rts51x_clear_ms_error(chip
);
180 TRACE_RET(chip
, retval
);
183 retval
= rts51x_get_rsp(chip
, 3, 15000);
184 if (CHECK_MS_TRANS_FAIL(chip
, retval
)) {
185 ms_set_err_code(chip
, err_code
);
186 rts51x_clear_ms_error(chip
);
187 TRACE_RET(chip
, STATUS_FAIL
);
190 ms_card
->last_rw_int
= val
= chip
->rsp_buf
[1];
191 if (val
& (MS_INT_CMDNK
| MS_INT_ERR
| MS_CRC16_ERR
| MS_RDY_TIMEOUT
))
192 TRACE_RET(chip
, STATUS_FAIL
);
194 return STATUS_SUCCESS
;
197 int ms_write_bytes(struct rts51x_chip
*chip
, u8 tpc
, u8 cnt
, u8 cfg
, u8
*data
,
200 struct ms_info
*ms_card
= &(chip
->ms_card
);
203 if (!data
|| (data_len
< cnt
))
204 TRACE_RET(chip
, STATUS_ERROR
);
206 rts51x_init_cmd(chip
);
208 for (i
= 0; i
< cnt
; i
++) {
209 rts51x_add_cmd(chip
, WRITE_REG_CMD
, PPBUF_BASE2
+ i
, 0xFF,
213 rts51x_add_cmd(chip
, WRITE_REG_CMD
, PPBUF_BASE2
+ i
,
216 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TPC
, 0xFF, tpc
);
217 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_BYTE_CNT
, 0xFF, cnt
);
218 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANS_CFG
, 0xFF, cfg
);
219 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
, 0x01,
222 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANSFER
, 0xFF,
223 MS_TRANSFER_START
| MS_TM_WRITE_BYTES
);
224 rts51x_add_cmd(chip
, CHECK_REG_CMD
, MS_TRANSFER
, MS_TRANSFER_END
,
227 retval
= rts51x_send_cmd(chip
, MODE_CR
, 100);
228 if (retval
!= STATUS_SUCCESS
)
229 TRACE_RET(chip
, retval
);
231 retval
= rts51x_get_rsp(chip
, 1, 5000);
232 if (CHECK_MS_TRANS_FAIL(chip
, retval
)) {
235 rts51x_ep0_read_register(chip
, MS_TRANS_CFG
, &val
);
236 RTS51X_DEBUGP("MS_TRANS_CFG: 0x%02x\n", val
);
238 rts51x_clear_ms_error(chip
);
240 if (!(tpc
& 0x08)) { /* Read Packet */
241 /* Check CRC16 & Ready Timeout */
242 if (val
& MS_CRC16_ERR
) {
243 ms_set_err_code(chip
, MS_CRC16_ERROR
);
244 TRACE_RET(chip
, ms_parse_err_code(chip
));
246 } else { /* Write Packet */
247 if (CHK_MSPRO(ms_card
) && !(val
& 0x80)) {
248 if (val
& (MS_INT_ERR
| MS_INT_CMDNK
)) {
249 ms_set_err_code(chip
, MS_CMD_NK
);
251 ms_parse_err_code(chip
));
256 /* Check Timeout of Ready Signal */
257 if (val
& MS_RDY_TIMEOUT
) {
258 ms_set_err_code(chip
, MS_TO_ERROR
);
259 TRACE_RET(chip
, ms_parse_err_code(chip
));
262 ms_set_err_code(chip
, MS_TO_ERROR
);
263 TRACE_RET(chip
, ms_parse_err_code(chip
));
266 return STATUS_SUCCESS
;
269 int ms_read_bytes(struct rts51x_chip
*chip
, u8 tpc
, u8 cnt
, u8 cfg
, u8
*data
,
272 struct ms_info
*ms_card
= &(chip
->ms_card
);
276 TRACE_RET(chip
, STATUS_ERROR
);
278 rts51x_init_cmd(chip
);
280 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TPC
, 0xFF, tpc
);
281 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_BYTE_CNT
, 0xFF, cnt
);
282 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANS_CFG
, 0xFF, cfg
);
283 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
, 0x01,
286 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANSFER
, 0xFF,
287 MS_TRANSFER_START
| MS_TM_READ_BYTES
);
288 rts51x_add_cmd(chip
, CHECK_REG_CMD
, MS_TRANSFER
, MS_TRANSFER_END
,
291 for (i
= 0; i
< data_len
- 1; i
++)
292 rts51x_add_cmd(chip
, READ_REG_CMD
, PPBUF_BASE2
+ i
, 0, 0);
295 rts51x_add_cmd(chip
, READ_REG_CMD
, PPBUF_BASE2
+ data_len
, 0,
298 rts51x_add_cmd(chip
, READ_REG_CMD
, PPBUF_BASE2
+ data_len
- 1,
301 retval
= rts51x_send_cmd(chip
, MODE_CR
, 100);
302 if (retval
!= STATUS_SUCCESS
)
303 TRACE_RET(chip
, retval
);
305 retval
= rts51x_get_rsp(chip
, data_len
+ 1, 5000);
306 if (CHECK_MS_TRANS_FAIL(chip
, retval
)) {
309 rts51x_ep0_read_register(chip
, MS_TRANS_CFG
, &val
);
310 RTS51X_DEBUGP("MS_TRANS_CFG: 0x%02x\n", val
);
312 rts51x_clear_ms_error(chip
);
314 if (!(tpc
& 0x08)) { /* Read Packet */
315 /* Check CRC16 & Ready Timeout */
316 if (val
& MS_CRC16_ERR
) {
317 ms_set_err_code(chip
, MS_CRC16_ERROR
);
318 TRACE_RET(chip
, ms_parse_err_code(chip
));
320 } else { /* Write Packet */
321 if (CHK_MSPRO(ms_card
) && !(val
& 0x80)) {
322 if (val
& (MS_INT_ERR
| MS_INT_CMDNK
)) {
323 ms_set_err_code(chip
, MS_CMD_NK
);
325 ms_parse_err_code(chip
));
330 /* Check Timeout of Ready Signal */
331 if (val
& MS_RDY_TIMEOUT
) {
332 ms_set_err_code(chip
, MS_TO_ERROR
);
333 TRACE_RET(chip
, ms_parse_err_code(chip
));
336 ms_set_err_code(chip
, MS_TO_ERROR
);
337 TRACE_RET(chip
, ms_parse_err_code(chip
));
340 rts51x_read_rsp_buf(chip
, 1, data
, data_len
);
342 return STATUS_SUCCESS
;
345 int ms_set_rw_reg_addr(struct rts51x_chip
*chip
,
346 u8 read_start
, u8 read_cnt
, u8 write_start
, u8 write_cnt
)
351 data
[0] = read_start
;
353 data
[2] = write_start
;
356 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
358 ms_write_bytes(chip
, SET_RW_REG_ADRS
, 4, NO_WAIT_INT
, data
,
360 if (retval
== STATUS_SUCCESS
)
361 return STATUS_SUCCESS
;
362 rts51x_clear_ms_error(chip
);
365 TRACE_RET(chip
, STATUS_FAIL
);
368 static int ms_send_cmd(struct rts51x_chip
*chip
, u8 cmd
, u8 cfg
)
375 return ms_write_bytes(chip
, PRO_SET_CMD
, 1, cfg
, data
, 1);
378 static int ms_set_cmd(struct rts51x_chip
*chip
,
379 u8 read_start
, u8 read_count
,
380 u8 write_start
, u8 write_count
,
381 u8 cmd
, u8 cfg
, u8
*data
, int data_len
, u8
*int_stat
)
386 if (!data
|| (data_len
<= 0) || (data_len
> 128)) {
387 RTS51X_DEBUGP("ms_set_cmd (data_len = %d)\n", data_len
);
388 TRACE_RET(chip
, STATUS_FAIL
);
392 ms_set_rw_reg_addr(chip
, read_start
, read_count
, write_start
,
394 if (retval
!= STATUS_SUCCESS
)
395 TRACE_RET(chip
, retval
);
397 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
399 ms_write_bytes(chip
, WRITE_REG
, write_count
, NO_WAIT_INT
,
401 if (retval
== STATUS_SUCCESS
)
404 if (i
== MS_MAX_RETRY_COUNT
)
405 TRACE_RET(chip
, STATUS_FAIL
);
407 ms_set_err_code(chip
, MS_NO_ERROR
);
409 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
410 retval
= ms_send_cmd(chip
, cmd
, WAIT_INT
);
411 if (retval
== STATUS_SUCCESS
)
414 if (i
== MS_MAX_RETRY_COUNT
)
415 TRACE_RET(chip
, STATUS_FAIL
);
416 /* GET_INT Register */
417 ms_set_err_code(chip
, MS_NO_ERROR
);
418 retval
= ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
, 1);
419 if (retval
!= STATUS_SUCCESS
)
420 TRACE_RET(chip
, retval
);
425 return STATUS_SUCCESS
;
429 static int ms_auto_set_cmd(struct rts51x_chip
*chip
,
430 u8 read_start
, u8 read_count
,
431 u8 write_start
, u8 write_count
,
432 u8 cmd
, u8 cfg
, u8
*data
, int data_len
,
438 if (!data
|| (data_len
<= 0) || (data_len
> 128)) {
439 RTS51X_DEBUGP("ms_auto_set_cmd (data_len = %d)\n", data_len
);
440 TRACE_RET(chip
, STATUS_FAIL
);
443 rts51x_init_cmd(chip
);
445 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_READ_START
, 0xFF, read_start
);
446 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_READ_COUNT
, 0xFF, read_count
);
447 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_WRITE_START
, 0xFF, write_start
);
448 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_WRITE_COUNT
, 0xFF, write_count
);
449 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_COMMAND
, 0xFF, cmd
);
451 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANS_CFG
, 0xFF, cfg
);
452 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
, 0x01,
455 for (i
= 0; i
< data_len
; i
++) {
456 rts51x_add_cmd(chip
, WRITE_REG_CMD
, PPBUF_BASE2
+ i
, 0xFF,
460 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANSFER
, 0xFF,
461 MS_TRANSFER_START
| MS_TM_SET_CMD
);
462 rts51x_add_cmd(chip
, CHECK_REG_CMD
, MS_TRANSFER
, MS_TRANSFER_END
,
465 retval
= rts51x_send_cmd(chip
, MODE_CR
| STAGE_MS_STATUS
, 100);
466 if (retval
!= STATUS_SUCCESS
)
467 TRACE_RET(chip
, retval
);
469 retval
= rts51x_get_rsp(chip
, 3, 5000);
471 if (CHECK_MS_TRANS_FAIL(chip
, retval
)) {
472 rts51x_clear_ms_error(chip
);
473 TRACE_RET(chip
, STATUS_FAIL
);
477 *int_stat
= chip
->rsp_buf
[2];
479 return STATUS_SUCCESS
;
483 static int ms_set_init_para(struct rts51x_chip
*chip
)
485 struct ms_info
*ms_card
= &(chip
->ms_card
);
488 if (CHK_HG8BIT(ms_card
)) {
490 ms_card
->ms_clock
= chip
->option
.asic_ms_hg_clk
;
492 ms_card
->ms_clock
= chip
->option
.fpga_ms_hg_clk
;
493 } else if (CHK_MSPRO(ms_card
) || CHK_MS4BIT(ms_card
)) {
495 ms_card
->ms_clock
= chip
->option
.asic_ms_4bit_clk
;
497 ms_card
->ms_clock
= chip
->option
.fpga_ms_4bit_clk
;
500 ms_card
->ms_clock
= 38;
502 ms_card
->ms_clock
= CLK_40
;
505 retval
= switch_clock(chip
, ms_card
->ms_clock
);
506 if (retval
!= STATUS_SUCCESS
)
507 TRACE_RET(chip
, retval
);
509 retval
= rts51x_select_card(chip
, MS_CARD
);
510 if (retval
!= STATUS_SUCCESS
)
511 TRACE_RET(chip
, retval
);
513 return STATUS_SUCCESS
;
516 int ms_switch_clock(struct rts51x_chip
*chip
)
518 struct ms_info
*ms_card
= &(chip
->ms_card
);
521 retval
= rts51x_select_card(chip
, MS_CARD
);
522 if (retval
!= STATUS_SUCCESS
)
523 TRACE_RET(chip
, retval
);
525 retval
= switch_clock(chip
, ms_card
->ms_clock
);
526 if (retval
!= STATUS_SUCCESS
)
527 TRACE_RET(chip
, retval
);
529 return STATUS_SUCCESS
;
532 static void ms_pull_ctl_disable(struct rts51x_chip
*chip
)
534 if (CHECK_PKG(chip
, LQFP48
)) {
535 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL1
, 0xFF, 0x55);
536 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL2
, 0xFF, 0x55);
537 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL3
, 0xFF, 0x95);
538 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL4
, 0xFF, 0x55);
539 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL5
, 0xFF, 0x55);
540 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL6
, 0xFF, 0xA5);
542 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL1
, 0xFF, 0x65);
543 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL2
, 0xFF, 0x55);
544 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL3
, 0xFF, 0x95);
545 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL4
, 0xFF, 0x55);
546 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL5
, 0xFF, 0x56);
547 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL6
, 0xFF, 0x59);
551 static void ms_pull_ctl_enable(struct rts51x_chip
*chip
)
553 if (CHECK_PKG(chip
, LQFP48
)) {
554 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL1
, 0xFF, 0x55);
555 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL2
, 0xFF, 0x55);
556 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL3
, 0xFF, 0x95);
557 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL4
, 0xFF, 0x55);
558 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL5
, 0xFF, 0x55);
559 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL6
, 0xFF, 0xA5);
561 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL1
, 0xFF, 0x65);
562 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL2
, 0xFF, 0x55);
563 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL3
, 0xFF, 0x95);
564 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL4
, 0xFF, 0x55);
565 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL5
, 0xFF, 0x55);
566 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL6
, 0xFF, 0x59);
570 static int ms_prepare_reset(struct rts51x_chip
*chip
)
572 struct ms_info
*ms_card
= &(chip
->ms_card
);
575 ms_card
->ms_type
= 0;
576 ms_card
->check_ms_flow
= 0;
577 ms_card
->switch_8bit_fail
= 0;
578 ms_card
->delay_write
.delay_write_flag
= 0;
580 ms_card
->pro_under_formatting
= 0;
582 rts51x_init_cmd(chip
);
584 if (chip
->asic_code
) {
585 ms_pull_ctl_enable(chip
);
587 rts51x_add_cmd(chip
, WRITE_REG_CMD
, FPGA_PULL_CTL
,
588 FPGA_MS_PULL_CTL_BIT
| 0x20, 0);
590 /* Tri-state MS output */
591 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_OE
, MS_OUTPUT_EN
, 0);
593 if (!chip
->option
.FT2_fast_mode
) {
594 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PWR_CTL
, POWER_MASK
,
598 retval
= rts51x_send_cmd(chip
, MODE_C
, 100);
599 if (retval
!= STATUS_SUCCESS
)
600 TRACE_RET(chip
, retval
);
602 if (!chip
->option
.FT2_fast_mode
) {
605 rts51x_card_power_on(chip
, MS_CARD
);
609 rts51x_get_card_status(chip
, &(chip
->card_status
));
611 chip
->ocp_stat
= (chip
->card_status
>> 4) & 0x03;
613 if (chip
->ocp_stat
& (MS_OCP_NOW
| MS_OCP_EVER
)) {
614 RTS51X_DEBUGP("Over current, OCPSTAT is 0x%x\n",
616 TRACE_RET(chip
, STATUS_FAIL
);
621 rts51x_init_cmd(chip
);
623 /* Enable MS Output */
624 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_OE
, MS_OUTPUT_EN
,
627 /* Reset Registers */
629 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_CFG
, 0xFF,
630 SAMPLE_TIME_RISING
| PUSH_TIME_DEFAULT
|
631 NO_EXTEND_TOGGLE
| MS_BUS_WIDTH_1
);
633 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_CFG
, 0xFF,
634 SAMPLE_TIME_FALLING
| PUSH_TIME_DEFAULT
|
635 NO_EXTEND_TOGGLE
| MS_BUS_WIDTH_1
);
636 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANS_CFG
, 0xFF,
637 NO_WAIT_INT
| NO_AUTO_READ_INT_REG
);
639 retval
= rts51x_send_cmd(chip
, MODE_C
, 100);
640 if (retval
!= STATUS_SUCCESS
)
641 TRACE_RET(chip
, retval
);
643 return ms_set_init_para(chip
);
646 static int ms_identify_media_type(struct rts51x_chip
*chip
, int switch_8bit_bus
)
648 struct ms_info
*ms_card
= &(chip
->ms_card
);
652 retval
= ms_set_rw_reg_addr(chip
, Pro_StatusReg
, 6, SystemParm
, 1);
653 if (retval
!= STATUS_SUCCESS
)
654 TRACE_RET(chip
, retval
);
656 /* Get Register form MS-PRO card */
657 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
659 ms_transfer_tpc(chip
, MS_TM_READ_BYTES
, READ_REG
, 6,
661 if (retval
== STATUS_SUCCESS
)
664 if (i
== MS_MAX_RETRY_COUNT
)
665 TRACE_RET(chip
, STATUS_FAIL
);
667 RTS51X_READ_REG(chip
, PPBUF_BASE2
+ 2, &val
);
668 RTS51X_DEBUGP("Type register: 0x%x\n", val
);
671 ms_card
->check_ms_flow
= 1;
672 TRACE_RET(chip
, STATUS_FAIL
);
674 /* Category Register */
675 RTS51X_READ_REG(chip
, PPBUF_BASE2
+ 4, &val
);
676 RTS51X_DEBUGP("Category register: 0x%x\n", val
);
678 ms_card
->check_ms_flow
= 1;
679 TRACE_RET(chip
, STATUS_FAIL
);
682 RTS51X_READ_REG(chip
, PPBUF_BASE2
+ 5, &val
);
683 RTS51X_DEBUGP("Class register: 0x%x\n", val
);
685 RTS51X_READ_REG(chip
, PPBUF_BASE2
, &val
);
687 chip
->card_wp
|= MS_CARD
;
689 chip
->card_wp
&= ~MS_CARD
;
690 } else if ((val
== 0x01) || (val
== 0x02) || (val
== 0x03)) {
691 chip
->card_wp
|= MS_CARD
;
693 ms_card
->check_ms_flow
= 1;
694 TRACE_RET(chip
, STATUS_FAIL
);
697 ms_card
->ms_type
|= TYPE_MSPRO
;
699 /* Check MSPro-HG Card, use IF Mode Register to distinguish */
700 RTS51X_READ_REG(chip
, PPBUF_BASE2
+ 3, &val
);
701 RTS51X_DEBUGP("IF Mode register: 0x%x\n", val
);
703 ms_card
->ms_type
&= 0x0F;
704 } else if (val
== 7) {
706 ms_card
->ms_type
|= MS_HG
;
708 ms_card
->ms_type
&= 0x0F;
710 TRACE_RET(chip
, STATUS_FAIL
);
713 /* end Procedure to identify Media Type */
714 return STATUS_SUCCESS
;
717 static int ms_confirm_cpu_startup(struct rts51x_chip
*chip
)
722 /* Confirm CPU StartUp */
725 if (monitor_card_cd(chip
, MS_CARD
) == CD_NOT_EXIST
)
726 TRACE_RET(chip
, STATUS_FAIL
);
728 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
730 ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
,
732 if (retval
== STATUS_SUCCESS
)
735 if (i
== MS_MAX_RETRY_COUNT
)
736 TRACE_RET(chip
, STATUS_FAIL
);
739 TRACE_RET(chip
, STATUS_FAIL
);
742 } while (!(val
& INT_REG_CED
));
744 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
745 retval
= ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
, 1);
746 if (retval
== STATUS_SUCCESS
)
749 if (i
== MS_MAX_RETRY_COUNT
)
750 TRACE_RET(chip
, STATUS_FAIL
);
752 if (val
& INT_REG_ERR
) {
753 if (val
& INT_REG_CMDNK
) { /* CMDNK = 1 */
754 chip
->card_wp
|= (MS_CARD
);
755 } else { /* CMDNK = 0 */
756 TRACE_RET(chip
, STATUS_FAIL
);
759 /*-- end confirm CPU startup */
761 return STATUS_SUCCESS
;
764 static int ms_switch_parallel_bus(struct rts51x_chip
*chip
)
769 data
[0] = PARALLEL_4BIT_IF
;
771 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
773 ms_write_bytes(chip
, WRITE_REG
, 1, NO_WAIT_INT
, data
, 2);
774 if (retval
== STATUS_SUCCESS
)
777 if (retval
!= STATUS_SUCCESS
)
778 TRACE_RET(chip
, retval
);
780 return STATUS_SUCCESS
;
783 static int ms_switch_8bit_bus(struct rts51x_chip
*chip
)
785 struct ms_info
*ms_card
= &(chip
->ms_card
);
789 data
[0] = PARALLEL_8BIT_IF
;
791 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
793 ms_write_bytes(chip
, WRITE_REG
, 1, NO_WAIT_INT
, data
, 2);
794 if (retval
== STATUS_SUCCESS
)
797 if (retval
!= STATUS_SUCCESS
)
798 TRACE_RET(chip
, STATUS_FAIL
);
800 RTS51X_WRITE_REG(chip
, MS_CFG
, 0x98,
801 MS_BUS_WIDTH_8
| SAMPLE_TIME_FALLING
);
802 ms_card
->ms_type
|= MS_8BIT
;
804 retval
= ms_set_init_para(chip
);
805 if (retval
!= STATUS_SUCCESS
)
806 TRACE_RET(chip
, retval
);
808 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
810 ms_transfer_tpc(chip
, MS_TM_READ_BYTES
, GET_INT
, 1,
812 if (retval
!= STATUS_SUCCESS
)
813 TRACE_RET(chip
, retval
);
816 return STATUS_SUCCESS
;
819 static int ms_pro_reset_flow(struct rts51x_chip
*chip
, int switch_8bit_bus
)
821 struct ms_info
*ms_card
= &(chip
->ms_card
);
824 for (i
= 0; i
< 3; i
++) {
825 retval
= ms_prepare_reset(chip
);
826 if (retval
!= STATUS_SUCCESS
)
827 TRACE_RET(chip
, retval
);
829 retval
= ms_identify_media_type(chip
, switch_8bit_bus
);
830 if (retval
!= STATUS_SUCCESS
)
831 TRACE_RET(chip
, retval
);
833 retval
= ms_confirm_cpu_startup(chip
);
834 if (retval
!= STATUS_SUCCESS
)
835 TRACE_RET(chip
, retval
);
837 retval
= ms_switch_parallel_bus(chip
);
838 if (retval
!= STATUS_SUCCESS
) {
839 if (monitor_card_cd(chip
, MS_CARD
) == CD_NOT_EXIST
)
840 TRACE_RET(chip
, STATUS_FAIL
);
847 if (retval
!= STATUS_SUCCESS
)
848 TRACE_RET(chip
, retval
);
850 RTS51X_WRITE_REG(chip
, MS_CFG
, 0x18, MS_BUS_WIDTH_4
);
852 RTS51X_WRITE_REG(chip
, MS_CFG
, PUSH_TIME_ODD
, PUSH_TIME_ODD
);
854 retval
= ms_set_init_para(chip
);
855 if (retval
!= STATUS_SUCCESS
)
856 TRACE_RET(chip
, retval
);
858 if (CHK_MSHG(ms_card
) && switch_8bit_bus
) {
859 retval
= ms_switch_8bit_bus(chip
);
860 if (retval
!= STATUS_SUCCESS
) {
861 ms_card
->switch_8bit_fail
= 1;
862 TRACE_RET(chip
, retval
);
866 return STATUS_SUCCESS
;
870 static int msxc_change_power(struct rts51x_chip
*chip
, u8 mode
)
875 rts51x_ms_cleanup_work(chip
);
877 /* Set Parameter Register */
878 retval
= ms_set_rw_reg_addr(chip
, 0, 0, Pro_DataCount1
, 6);
879 if (retval
!= STATUS_SUCCESS
)
880 TRACE_RET(chip
, retval
);
889 retval
= ms_write_bytes(chip
, PRO_WRITE_REG
, 6, NO_WAIT_INT
, buf
, 6);
890 if (retval
!= STATUS_SUCCESS
)
891 TRACE_RET(chip
, retval
);
893 retval
= ms_send_cmd(chip
, XC_CHG_POWER
, WAIT_INT
);
894 if (retval
!= STATUS_SUCCESS
)
895 TRACE_RET(chip
, retval
);
897 RTS51X_READ_REG(chip
, MS_TRANS_CFG
, buf
);
898 if (buf
[0] & (MS_INT_CMDNK
| MS_INT_ERR
))
899 TRACE_RET(chip
, STATUS_FAIL
);
901 return STATUS_SUCCESS
;
905 static int ms_read_attribute_info(struct rts51x_chip
*chip
)
907 struct ms_info
*ms_card
= &(chip
->ms_card
);
909 u8 val
, *buf
, class_code
, device_type
, sub_class
, data
[16];
910 u16 total_blk
= 0, blk_size
= 0;
912 u32 xc_total_blk
= 0, xc_blk_size
= 0;
914 u32 sys_info_addr
= 0, sys_info_size
;
915 #ifdef SUPPORT_PCGL_1P18
916 u32 model_name_addr
= 0, model_name_size
;
917 int found_sys_info
= 0, found_model_name
= 0;
920 retval
= ms_set_rw_reg_addr(chip
, Pro_IntReg
, 2, Pro_SystemParm
, 7);
921 if (retval
!= STATUS_SUCCESS
)
922 TRACE_RET(chip
, retval
);
924 if (CHK_MS8BIT(ms_card
))
925 data
[0] = PARALLEL_8BIT_IF
;
927 data
[0] = PARALLEL_4BIT_IF
;
934 /* Start address 0 */
938 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
940 ms_write_bytes(chip
, PRO_WRITE_REG
, 7, NO_WAIT_INT
, data
,
942 if (retval
== STATUS_SUCCESS
)
945 if (retval
!= STATUS_SUCCESS
)
946 TRACE_RET(chip
, retval
);
948 buf
= kmalloc(64 * 512, GFP_KERNEL
);
950 TRACE_RET(chip
, STATUS_NOMEM
);
952 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
953 retval
= ms_send_cmd(chip
, PRO_READ_ATRB
, WAIT_INT
);
954 if (retval
!= STATUS_SUCCESS
)
957 retval
= rts51x_read_register(chip
, MS_TRANS_CFG
, &val
);
958 if (retval
!= STATUS_SUCCESS
) {
960 TRACE_RET(chip
, STATUS_FAIL
);
962 if (!(val
& MS_INT_BREQ
)) {
964 TRACE_RET(chip
, STATUS_FAIL
);
968 ms_transfer_data(chip
, MS_TM_AUTO_READ
, PRO_READ_LONG_DATA
,
969 0x40, WAIT_INT
, 0, 0, buf
, 64 * 512);
970 if (retval
== STATUS_SUCCESS
)
973 rts51x_clear_ms_error(chip
);
975 if (retval
!= STATUS_SUCCESS
) {
977 TRACE_RET(chip
, retval
);
982 retval
= rts51x_read_register(chip
, MS_TRANS_CFG
, &val
);
983 if (retval
!= STATUS_SUCCESS
) {
985 TRACE_RET(chip
, retval
);
988 if ((val
& MS_INT_CED
) || !(val
& MS_INT_BREQ
))
992 ms_transfer_tpc(chip
, MS_TM_NORMAL_READ
, PRO_READ_LONG_DATA
,
994 if (retval
!= STATUS_SUCCESS
) {
996 TRACE_RET(chip
, retval
);
1002 if (retval
!= STATUS_SUCCESS
) {
1004 TRACE_RET(chip
, retval
);
1007 if ((buf
[0] != 0xa5) && (buf
[1] != 0xc3)) {
1008 /* Signature code is wrong */
1010 TRACE_RET(chip
, STATUS_FAIL
);
1013 if ((buf
[4] < 1) || (buf
[4] > 12)) {
1015 TRACE_RET(chip
, STATUS_FAIL
);
1018 for (i
= 0; i
< buf
[4]; i
++) {
1019 int cur_addr_off
= 16 + i
* 12;
1022 if ((buf
[cur_addr_off
+ 8] == 0x10)
1023 || (buf
[cur_addr_off
+ 8] == 0x13)) {
1025 if (buf
[cur_addr_off
+ 8] == 0x10) {
1027 sys_info_addr
= ((u32
) buf
[cur_addr_off
+ 0] << 24) |
1028 ((u32
) buf
[cur_addr_off
+ 1] << 16) |
1029 ((u32
) buf
[cur_addr_off
+ 2] << 8) |
1030 buf
[cur_addr_off
+ 3];
1032 ((u32
) buf
[cur_addr_off
+ 4] << 24) |
1033 ((u32
) buf
[cur_addr_off
+ 5] << 16) |
1034 ((u32
) buf
[cur_addr_off
+ 6] << 8) |
1035 buf
[cur_addr_off
+ 7];
1036 RTS51X_DEBUGP("sys_info_addr = 0x%x,"
1037 "sys_info_size = 0x%x\n",
1038 sys_info_addr
, sys_info_size
);
1039 if (sys_info_size
!= 96) {
1041 TRACE_RET(chip
, STATUS_FAIL
);
1043 if (sys_info_addr
< 0x1A0) {
1045 TRACE_RET(chip
, STATUS_FAIL
);
1047 if ((sys_info_size
+ sys_info_addr
) > 0x8000) {
1049 TRACE_RET(chip
, STATUS_FAIL
);
1052 if (buf
[cur_addr_off
+ 8] == 0x13)
1053 ms_card
->ms_type
|= MS_XC
;
1055 #ifdef SUPPORT_PCGL_1P18
1061 #ifdef SUPPORT_PCGL_1P18
1062 if (buf
[cur_addr_off
+ 8] == 0x15) {
1063 model_name_addr
= ((u32
) buf
[cur_addr_off
+ 0] << 24) |
1064 ((u32
) buf
[cur_addr_off
+ 1] << 16) |
1065 ((u32
) buf
[cur_addr_off
+ 2] << 8) |
1066 buf
[cur_addr_off
+ 3];
1068 ((u32
) buf
[cur_addr_off
+ 4] << 24) |
1069 ((u32
) buf
[cur_addr_off
+ 5] << 16) |
1070 ((u32
) buf
[cur_addr_off
+ 6] << 8) |
1071 buf
[cur_addr_off
+ 7];
1072 RTS51X_DEBUGP("model_name_addr = 0x%x,"
1073 "model_name_size = 0x%x\n",
1074 model_name_addr
, model_name_size
);
1075 if (model_name_size
!= 48) {
1077 TRACE_RET(chip
, STATUS_FAIL
);
1079 if (model_name_addr
< 0x1A0) {
1081 TRACE_RET(chip
, STATUS_FAIL
);
1083 if ((model_name_size
+ model_name_addr
) > 0x8000) {
1085 TRACE_RET(chip
, STATUS_FAIL
);
1088 found_model_name
= 1;
1091 if (found_sys_info
&& found_model_name
)
1098 TRACE_RET(chip
, STATUS_FAIL
);
1101 class_code
= buf
[sys_info_addr
+ 0];
1102 device_type
= buf
[sys_info_addr
+ 56];
1103 sub_class
= buf
[sys_info_addr
+ 46];
1105 if (CHK_MSXC(ms_card
)) {
1106 xc_total_blk
= ((u32
) buf
[sys_info_addr
+ 6] << 24) |
1107 ((u32
) buf
[sys_info_addr
+ 7] << 16) |
1108 ((u32
) buf
[sys_info_addr
+ 8] << 8) |
1109 buf
[sys_info_addr
+ 9];
1110 xc_blk_size
= ((u32
) buf
[sys_info_addr
+ 32] << 24) |
1111 ((u32
) buf
[sys_info_addr
+ 33] << 16) |
1112 ((u32
) buf
[sys_info_addr
+ 34] << 8) |
1113 buf
[sys_info_addr
+ 35];
1114 RTS51X_DEBUGP("xc_total_blk = 0x%x, xc_blk_size = 0x%x\n",
1115 xc_total_blk
, xc_blk_size
);
1118 ((u16
) buf
[sys_info_addr
+ 6] << 8) | buf
[sys_info_addr
+
1121 ((u16
) buf
[sys_info_addr
+ 2] << 8) | buf
[sys_info_addr
+
1123 RTS51X_DEBUGP("total_blk = 0x%x, blk_size = 0x%x\n", total_blk
,
1128 ((u16
) buf
[sys_info_addr
+ 6] << 8) | buf
[sys_info_addr
+ 7];
1129 blk_size
= ((u16
) buf
[sys_info_addr
+ 2] << 8) | buf
[sys_info_addr
+ 3];
1130 RTS51X_DEBUGP("total_blk = 0x%x, blk_size = 0x%x\n", total_blk
,
1134 RTS51X_DEBUGP("class_code = 0x%x, device_type = 0x%x,"
1135 "sub_class = 0x%x\n",
1136 class_code
, device_type
, sub_class
);
1138 memcpy(ms_card
->raw_sys_info
, buf
+ sys_info_addr
, 96);
1139 #ifdef SUPPORT_PCGL_1P18
1140 memcpy(ms_card
->raw_model_name
, buf
+ model_name_addr
, 48);
1145 /* Confirm System Information */
1147 if (CHK_MSXC(ms_card
)) {
1148 if (class_code
!= 0x03)
1149 TRACE_RET(chip
, STATUS_FAIL
);
1151 if (class_code
!= 0x02)
1152 TRACE_RET(chip
, STATUS_FAIL
);
1155 if (class_code
!= 0x02)
1156 TRACE_RET(chip
, STATUS_FAIL
);
1159 if (device_type
!= 0x00) {
1160 if ((device_type
== 0x01) || (device_type
== 0x02)
1161 || (device_type
== 0x03))
1162 chip
->card_wp
|= MS_CARD
;
1164 TRACE_RET(chip
, STATUS_FAIL
);
1166 if (sub_class
& 0xC0)
1167 TRACE_RET(chip
, STATUS_FAIL
);
1169 RTS51X_DEBUGP("class_code: 0x%x, device_type: 0x%x, sub_class: 0x%x\n",
1170 class_code
, device_type
, sub_class
);
1173 if (CHK_MSXC(ms_card
)) {
1174 chip
->capacity
[chip
->card2lun
[MS_CARD
]] = ms_card
->capacity
=
1175 xc_total_blk
* xc_blk_size
;
1177 chip
->capacity
[chip
->card2lun
[MS_CARD
]] = ms_card
->capacity
=
1178 total_blk
* blk_size
;
1181 chip
->capacity
[chip
->card2lun
[MS_CARD
]] = ms_card
->capacity
=
1182 total_blk
* blk_size
;
1185 return STATUS_SUCCESS
;
1188 #ifdef SUPPORT_MAGIC_GATE
1189 int mg_set_tpc_para_sub(struct rts51x_chip
*chip
, int type
, u8 mg_entry_num
);
1192 static int reset_ms_pro(struct rts51x_chip
*chip
)
1194 struct ms_info
*ms_card
= &(chip
->ms_card
);
1196 #ifdef XC_POWERCLASS
1197 u8 change_power_class
= 2;
1200 #ifdef XC_POWERCLASS
1203 retval
= ms_pro_reset_flow(chip
, 1);
1204 if (retval
!= STATUS_SUCCESS
) {
1205 if (ms_card
->switch_8bit_fail
) {
1206 retval
= ms_pro_reset_flow(chip
, 0);
1207 if (retval
!= STATUS_SUCCESS
)
1208 TRACE_RET(chip
, retval
);
1210 TRACE_RET(chip
, retval
);
1214 retval
= ms_read_attribute_info(chip
);
1215 if (retval
!= STATUS_SUCCESS
)
1216 TRACE_RET(chip
, retval
);
1217 #ifdef XC_POWERCLASS
1218 if (CHK_HG8BIT(ms_card
))
1219 change_power_class
= 0;
1221 if (change_power_class
&& CHK_MSXC(ms_card
)) {
1222 u8 power_class_mode
= (ms_card
->raw_sys_info
[46] & 0x18) >> 3;
1223 RTS51X_DEBUGP("power_class_mode = 0x%x", power_class_mode
);
1224 if (change_power_class
> power_class_mode
)
1225 change_power_class
= power_class_mode
;
1226 if (change_power_class
) {
1227 retval
= msxc_change_power(chip
, change_power_class
);
1228 if (retval
!= STATUS_SUCCESS
) {
1229 change_power_class
--;
1236 #ifdef SUPPORT_MAGIC_GATE
1237 retval
= mg_set_tpc_para_sub(chip
, 0, 0);
1238 if (retval
!= STATUS_SUCCESS
)
1239 TRACE_RET(chip
, retval
);
1242 if (CHK_HG8BIT(ms_card
))
1243 chip
->card_bus_width
[chip
->card2lun
[MS_CARD
]] = 8;
1245 chip
->card_bus_width
[chip
->card2lun
[MS_CARD
]] = 4;
1247 return STATUS_SUCCESS
;
1250 static int ms_read_status_reg(struct rts51x_chip
*chip
)
1255 retval
= ms_set_rw_reg_addr(chip
, StatusReg0
, 2, 0, 0);
1256 if (retval
!= STATUS_SUCCESS
)
1257 TRACE_RET(chip
, retval
);
1259 retval
= ms_read_bytes(chip
, READ_REG
, 2, NO_WAIT_INT
, val
, 2);
1260 if (retval
!= STATUS_SUCCESS
)
1261 TRACE_RET(chip
, retval
);
1262 if (val
[1] & (STS_UCDT
| STS_UCEX
| STS_UCFG
)) {
1263 ms_set_err_code(chip
, MS_FLASH_READ_ERROR
);
1264 TRACE_RET(chip
, STATUS_FAIL
);
1267 return STATUS_SUCCESS
;
1270 static int ms_check_boot_block(struct rts51x_chip
*chip
, u16 block_addr
)
1272 struct ms_info
*ms_card
= &(chip
->ms_card
);
1274 u8 extra
[MS_EXTRA_SIZE
], data
[10], val
= 0;
1276 if (CHK_MS4BIT(ms_card
)) {
1277 /* Parallel interface */
1280 /* Serial interface */
1285 data
[2] = (u8
) (block_addr
>> 8);
1286 data
[3] = (u8
) block_addr
;
1288 * Extra data access mode */
1292 retval
= ms_set_cmd(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
, 6,
1293 BLOCK_READ
, WAIT_INT
, data
, 6, &val
);
1294 if (retval
!= STATUS_SUCCESS
)
1295 TRACE_RET(chip
, retval
);
1297 if (val
& INT_REG_CMDNK
) {
1298 ms_set_err_code(chip
, MS_CMD_NK
);
1299 TRACE_RET(chip
, STATUS_FAIL
);
1301 if (val
& INT_REG_CED
) {
1302 if (val
& INT_REG_ERR
) {
1303 retval
= ms_read_status_reg(chip
);
1304 if (retval
!= STATUS_SUCCESS
)
1305 TRACE_RET(chip
, retval
);
1307 ms_set_rw_reg_addr(chip
, OverwriteFlag
,
1308 MS_EXTRA_SIZE
, SystemParm
, 6);
1309 if (retval
!= STATUS_SUCCESS
)
1310 TRACE_RET(chip
, retval
);
1315 ms_read_bytes(chip
, READ_REG
, MS_EXTRA_SIZE
, NO_WAIT_INT
, extra
,
1317 if (retval
!= STATUS_SUCCESS
)
1318 TRACE_RET(chip
, retval
);
1320 if (!(extra
[0] & BLOCK_OK
) || (extra
[1] & NOT_BOOT_BLOCK
))
1321 TRACE_RET(chip
, STATUS_FAIL
);
1323 return STATUS_SUCCESS
;
1326 static int ms_read_extra_data(struct rts51x_chip
*chip
,
1327 u16 block_addr
, u8 page_num
, u8
*buf
,
1330 struct ms_info
*ms_card
= &(chip
->ms_card
);
1332 u8 val
= 0, data
[10];
1334 if (CHK_MS4BIT(ms_card
)) {
1335 /* Parallel interface */
1338 /* Serial interface */
1343 data
[2] = (u8
) (block_addr
>> 8);
1344 data
[3] = (u8
) block_addr
;
1346 * Extra data access mode */
1352 ms_auto_set_cmd(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
, 6,
1353 BLOCK_READ
, WAIT_INT
, data
, 6, &val
);
1355 retval
= ms_set_cmd(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
, 6,
1356 BLOCK_READ
, WAIT_INT
, data
, 6, &val
);
1358 if (retval
!= STATUS_SUCCESS
)
1359 TRACE_RET(chip
, retval
);
1361 if (val
& INT_REG_CMDNK
) {
1362 ms_set_err_code(chip
, MS_CMD_NK
);
1363 TRACE_RET(chip
, STATUS_FAIL
);
1365 if (val
& INT_REG_CED
) {
1366 if (val
& INT_REG_ERR
) {
1367 retval
= ms_read_status_reg(chip
);
1368 if (retval
!= STATUS_SUCCESS
)
1369 TRACE_RET(chip
, retval
);
1371 ms_set_rw_reg_addr(chip
, OverwriteFlag
,
1372 MS_EXTRA_SIZE
, SystemParm
, 6);
1373 if (retval
!= STATUS_SUCCESS
)
1374 TRACE_RET(chip
, retval
);
1379 ms_read_bytes(chip
, READ_REG
, MS_EXTRA_SIZE
, NO_WAIT_INT
, data
,
1381 if (retval
!= STATUS_SUCCESS
)
1382 TRACE_RET(chip
, retval
);
1384 if (buf
&& buf_len
) {
1385 if (buf_len
> MS_EXTRA_SIZE
)
1386 buf_len
= MS_EXTRA_SIZE
;
1387 memcpy(buf
, data
, buf_len
);
1390 return STATUS_SUCCESS
;
1393 static int ms_write_extra_data(struct rts51x_chip
*chip
,
1394 u16 block_addr
, u8 page_num
, u8
*buf
,
1397 struct ms_info
*ms_card
= &(chip
->ms_card
);
1399 u8 val
= 0, data
[16];
1401 if (!buf
|| (buf_len
< MS_EXTRA_SIZE
))
1402 TRACE_RET(chip
, STATUS_FAIL
);
1404 if (CHK_MS4BIT(ms_card
)) {
1405 /* Parallel interface */
1408 /* Serial interface */
1413 data
[2] = (u8
) (block_addr
>> 8);
1414 data
[3] = (u8
) block_addr
;
1416 * Extra data access mode */
1420 for (i
= 6; i
< MS_EXTRA_SIZE
+ 6; i
++)
1421 data
[i
] = buf
[i
- 6];
1425 ms_auto_set_cmd(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
,
1426 6 + MS_EXTRA_SIZE
, BLOCK_WRITE
, WAIT_INT
, data
, 16,
1430 ms_set_cmd(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
,
1431 6 + MS_EXTRA_SIZE
, BLOCK_WRITE
, WAIT_INT
, data
, 16,
1434 if (retval
!= STATUS_SUCCESS
)
1435 TRACE_RET(chip
, retval
);
1437 if (val
& INT_REG_CMDNK
) {
1438 ms_set_err_code(chip
, MS_CMD_NK
);
1439 TRACE_RET(chip
, STATUS_FAIL
);
1441 if (val
& INT_REG_CED
) {
1442 if (val
& INT_REG_ERR
) {
1443 ms_set_err_code(chip
, MS_FLASH_WRITE_ERROR
);
1444 TRACE_RET(chip
, STATUS_FAIL
);
1448 return STATUS_SUCCESS
;
1451 static int ms_read_page(struct rts51x_chip
*chip
, u16 block_addr
, u8 page_num
)
1453 struct ms_info
*ms_card
= &(chip
->ms_card
);
1455 u8 val
= 0, data
[6];
1457 if (CHK_MS4BIT(ms_card
)) {
1458 /* Parallel interface */
1461 /* Serial interface */
1466 data
[2] = (u8
) (block_addr
>> 8);
1467 data
[3] = (u8
) block_addr
;
1469 * Single page access mode */
1473 retval
= ms_set_cmd(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
, 6,
1474 BLOCK_READ
, WAIT_INT
, data
, 6, &val
);
1475 if (retval
!= STATUS_SUCCESS
)
1476 TRACE_RET(chip
, retval
);
1478 if (val
& INT_REG_CMDNK
) {
1479 ms_set_err_code(chip
, MS_CMD_NK
);
1480 TRACE_RET(chip
, STATUS_FAIL
);
1483 if (val
& INT_REG_CED
) {
1484 if (val
& INT_REG_ERR
) {
1485 if (!(val
& INT_REG_BREQ
)) {
1486 ms_set_err_code(chip
, MS_FLASH_READ_ERROR
);
1487 TRACE_RET(chip
, STATUS_FAIL
);
1489 retval
= ms_read_status_reg(chip
);
1490 if (retval
!= STATUS_SUCCESS
)
1491 ms_set_err_code(chip
, MS_FLASH_WRITE_ERROR
);
1493 if (!(val
& INT_REG_BREQ
)) {
1494 ms_set_err_code(chip
, MS_BREQ_ERROR
);
1495 TRACE_RET(chip
, STATUS_FAIL
);
1501 ms_transfer_tpc(chip
, MS_TM_NORMAL_READ
, READ_PAGE_DATA
, 0,
1503 if (retval
!= STATUS_SUCCESS
)
1504 TRACE_RET(chip
, retval
);
1505 if (ms_check_err_code(chip
, MS_FLASH_WRITE_ERROR
))
1506 TRACE_RET(chip
, STATUS_FAIL
);
1508 return STATUS_SUCCESS
;
1511 static int ms_set_bad_block(struct rts51x_chip
*chip
, u16 phy_blk
)
1513 struct ms_info
*ms_card
= &(chip
->ms_card
);
1515 u8 val
= 0, data
[8], extra
[MS_EXTRA_SIZE
];
1517 retval
= ms_read_extra_data(chip
, phy_blk
, 0, extra
, MS_EXTRA_SIZE
);
1518 if (retval
!= STATUS_SUCCESS
)
1519 TRACE_RET(chip
, retval
);
1521 ms_set_err_code(chip
, MS_NO_ERROR
);
1523 if (CHK_MS4BIT(ms_card
)) {
1524 /* Parallel interface */
1527 /* Serial interface */
1532 data
[2] = (u8
) (phy_blk
>> 8);
1533 data
[3] = (u8
) phy_blk
;
1536 data
[6] = extra
[0] & 0x7F;
1540 retval
= ms_set_cmd(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
, 7,
1541 BLOCK_WRITE
, WAIT_INT
, data
, 7, &val
);
1543 retval
= ms_set_cmd(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
, 7,
1544 BLOCK_WRITE
, WAIT_INT
, data
, 7, &val
);
1546 if (retval
!= STATUS_SUCCESS
)
1547 TRACE_RET(chip
, retval
);
1549 if (val
& INT_REG_CMDNK
) {
1550 ms_set_err_code(chip
, MS_CMD_NK
);
1551 TRACE_RET(chip
, STATUS_FAIL
);
1554 if (val
& INT_REG_CED
) {
1555 if (val
& INT_REG_ERR
) {
1556 ms_set_err_code(chip
, MS_FLASH_WRITE_ERROR
);
1557 TRACE_RET(chip
, STATUS_FAIL
);
1561 return STATUS_SUCCESS
;
1564 static int ms_erase_block(struct rts51x_chip
*chip
, u16 phy_blk
)
1566 struct ms_info
*ms_card
= &(chip
->ms_card
);
1568 u8 val
= 0, data
[6];
1571 ms_set_rw_reg_addr(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
,
1573 if (retval
!= STATUS_SUCCESS
)
1574 TRACE_RET(chip
, retval
);
1576 ms_set_err_code(chip
, MS_NO_ERROR
);
1578 if (CHK_MS4BIT(ms_card
)) {
1579 /* Parallel interface */
1582 /* Serial interface */
1587 data
[2] = (u8
) (phy_blk
>> 8);
1588 data
[3] = (u8
) phy_blk
;
1595 ms_auto_set_cmd(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
, 6,
1596 BLOCK_ERASE
, WAIT_INT
, data
, 6, &val
);
1598 retval
= ms_set_cmd(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
, 6,
1599 BLOCK_ERASE
, WAIT_INT
, data
, 6, &val
);
1602 if (val
& INT_REG_CMDNK
) {
1607 ms_set_err_code(chip
, MS_CMD_NK
);
1608 ms_set_bad_block(chip
, phy_blk
);
1609 TRACE_RET(chip
, STATUS_FAIL
);
1612 if (val
& INT_REG_CED
) {
1613 if (val
& INT_REG_ERR
) {
1614 ms_set_err_code(chip
, MS_FLASH_WRITE_ERROR
);
1615 TRACE_RET(chip
, STATUS_FAIL
);
1619 return STATUS_SUCCESS
;
1622 static void ms_set_page_status(u16 log_blk
, u8 type
, u8
*extra
, int extra_len
)
1624 if (!extra
|| (extra_len
< MS_EXTRA_SIZE
))
1627 memset(extra
, 0xFF, MS_EXTRA_SIZE
);
1629 if (type
== setPS_NG
)
1634 extra
[2] = (u8
) (log_blk
>> 8);
1635 extra
[3] = (u8
) log_blk
;
1638 static int ms_init_page(struct rts51x_chip
*chip
, u16 phy_blk
, u16 log_blk
,
1639 u8 start_page
, u8 end_page
)
1642 u8 extra
[MS_EXTRA_SIZE
], i
;
1644 memset(extra
, 0xff, MS_EXTRA_SIZE
);
1646 extra
[0] = 0xf8; /* Block, page OK, data erased */
1648 extra
[2] = (u8
) (log_blk
>> 8);
1649 extra
[3] = (u8
) log_blk
;
1651 for (i
= start_page
; i
< end_page
; i
++) {
1652 if (monitor_card_cd(chip
, MS_CARD
) == CD_NOT_EXIST
)
1653 TRACE_RET(chip
, STATUS_FAIL
);
1656 ms_write_extra_data(chip
, phy_blk
, i
, extra
, MS_EXTRA_SIZE
);
1657 if (retval
!= STATUS_SUCCESS
)
1658 TRACE_RET(chip
, retval
);
1661 return STATUS_SUCCESS
;
1664 static int ms_copy_page(struct rts51x_chip
*chip
, u16 old_blk
, u16 new_blk
,
1665 u16 log_blk
, u8 start_page
, u8 end_page
)
1667 struct ms_info
*ms_card
= &(chip
->ms_card
);
1668 int retval
, rty_cnt
, uncorrect_flag
= 0;
1669 u8 extra
[MS_EXTRA_SIZE
], val
, i
, j
, data
[16];
1671 RTS51X_DEBUGP("Copy page from 0x%x to 0x%x, logical block is 0x%x\n",
1672 old_blk
, new_blk
, log_blk
);
1673 RTS51X_DEBUGP("start_page = %d, end_page = %d\n", start_page
,
1676 retval
= ms_read_extra_data(chip
, new_blk
, 0, extra
, MS_EXTRA_SIZE
);
1677 if (retval
!= STATUS_SUCCESS
)
1678 TRACE_RET(chip
, retval
);
1680 retval
= ms_read_status_reg(chip
);
1681 if (retval
!= STATUS_SUCCESS
)
1682 TRACE_RET(chip
, retval
);
1684 RTS51X_READ_REG(chip
, PPBUF_BASE2
, &val
);
1686 if (val
& BUF_FULL
) {
1688 retval
= ms_send_cmd(chip
, CLEAR_BUF
, WAIT_INT
);
1689 if (retval
!= STATUS_SUCCESS
)
1690 TRACE_RET(chip
, retval
);
1691 /* GET_INT Register */
1692 retval
= ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
, 1);
1693 if (retval
!= STATUS_SUCCESS
)
1694 TRACE_RET(chip
, retval
);
1696 if (!(val
& INT_REG_CED
)) {
1697 ms_set_err_code(chip
, MS_FLASH_WRITE_ERROR
);
1698 TRACE_RET(chip
, STATUS_FAIL
);
1702 for (i
= start_page
; i
< end_page
; i
++) {
1703 if (monitor_card_cd(chip
, MS_CARD
) == CD_NOT_EXIST
)
1704 TRACE_RET(chip
, STATUS_FAIL
);
1706 ms_read_extra_data(chip
, old_blk
, i
, extra
, MS_EXTRA_SIZE
);
1709 ms_set_rw_reg_addr(chip
, OverwriteFlag
, MS_EXTRA_SIZE
,
1711 if (retval
!= STATUS_SUCCESS
)
1712 TRACE_RET(chip
, retval
);
1714 ms_set_err_code(chip
, MS_NO_ERROR
);
1716 if (CHK_MS4BIT(ms_card
)) {
1717 /* Parallel interface */
1720 /* Serial interface */
1725 data
[2] = (u8
) (old_blk
>> 8);
1726 data
[3] = (u8
) old_blk
;
1731 ms_write_bytes(chip
, WRITE_REG
, 6, NO_WAIT_INT
, data
, 6);
1732 if (retval
!= STATUS_SUCCESS
)
1733 TRACE_RET(chip
, retval
);
1735 retval
= ms_send_cmd(chip
, BLOCK_READ
, WAIT_INT
);
1736 if (retval
!= STATUS_SUCCESS
)
1737 TRACE_RET(chip
, retval
);
1739 ms_set_err_code(chip
, MS_NO_ERROR
);
1740 retval
= ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
, 1);
1741 if (retval
!= STATUS_SUCCESS
)
1742 TRACE_RET(chip
, retval
);
1744 if (val
& INT_REG_CMDNK
) {
1745 ms_set_err_code(chip
, MS_CMD_NK
);
1746 TRACE_RET(chip
, STATUS_FAIL
);
1749 if (val
& INT_REG_CED
) {
1750 if (val
& INT_REG_ERR
) {
1751 retval
= ms_read_status_reg(chip
);
1752 if (retval
!= STATUS_SUCCESS
) {
1754 RTS51X_DEBUGP("Uncorrectable"
1761 ms_transfer_tpc(chip
, MS_TM_NORMAL_READ
,
1762 READ_PAGE_DATA
, 0, NO_WAIT_INT
);
1763 if (retval
!= STATUS_SUCCESS
)
1764 TRACE_RET(chip
, retval
);
1765 if (uncorrect_flag
) {
1766 ms_set_page_status(log_blk
, setPS_NG
,
1767 extra
, MS_EXTRA_SIZE
);
1770 ms_write_extra_data(chip
, old_blk
, i
,
1773 RTS51X_DEBUGP("page %d :"
1774 "extra[0] = 0x%x\n",
1776 MS_SET_BAD_BLOCK_FLG(ms_card
);
1778 ms_set_page_status(log_blk
, setPS_Error
,
1779 extra
, MS_EXTRA_SIZE
);
1780 ms_write_extra_data(chip
, new_blk
, i
,
1781 extra
, MS_EXTRA_SIZE
);
1785 for (rty_cnt
= 0; rty_cnt
< MS_MAX_RETRY_COUNT
;
1788 ms_transfer_tpc(chip
,
1792 if (retval
== STATUS_SUCCESS
)
1795 if (rty_cnt
== MS_MAX_RETRY_COUNT
)
1796 TRACE_RET(chip
, STATUS_FAIL
);
1799 if (!(val
& INT_REG_BREQ
)) {
1800 ms_set_err_code(chip
, MS_BREQ_ERROR
);
1801 TRACE_RET(chip
, STATUS_FAIL
);
1805 retval
= ms_set_rw_reg_addr(chip
, OverwriteFlag
,
1806 MS_EXTRA_SIZE
, SystemParm
,
1807 (6 + MS_EXTRA_SIZE
));
1810 ms_set_err_code(chip
, MS_NO_ERROR
);
1812 if (CHK_MS4BIT(ms_card
)) {
1813 /* Parallel interface */
1816 /* Serial interface */
1821 data
[2] = (u8
) (new_blk
>> 8);
1822 data
[3] = (u8
) new_blk
;
1826 /* for MS check procedure */
1827 if ((extra
[0] & 0x60) != 0x60)
1833 data
[6 + 2] = (u8
) (log_blk
>> 8);
1834 data
[6 + 3] = (u8
) log_blk
;
1836 for (j
= 4; j
<= MS_EXTRA_SIZE
; j
++)
1840 ms_write_bytes(chip
, WRITE_REG
, (6 + MS_EXTRA_SIZE
),
1841 NO_WAIT_INT
, data
, 16);
1842 if (retval
!= STATUS_SUCCESS
)
1843 TRACE_RET(chip
, retval
);
1845 retval
= ms_send_cmd(chip
, BLOCK_WRITE
, WAIT_INT
);
1846 if (retval
!= STATUS_SUCCESS
)
1847 TRACE_RET(chip
, retval
);
1848 /* GET_INT Register */
1849 ms_set_err_code(chip
, MS_NO_ERROR
);
1850 retval
= ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
, 1);
1851 if (retval
!= STATUS_SUCCESS
)
1852 TRACE_RET(chip
, retval
);
1854 if (val
& INT_REG_CMDNK
) {
1855 ms_set_err_code(chip
, MS_CMD_NK
);
1856 TRACE_RET(chip
, STATUS_FAIL
);
1859 if (val
& INT_REG_CED
) {
1860 if (val
& INT_REG_ERR
) {
1861 ms_set_err_code(chip
, MS_FLASH_WRITE_ERROR
);
1862 TRACE_RET(chip
, STATUS_FAIL
);
1868 ms_set_rw_reg_addr(chip
, OverwriteFlag
,
1869 MS_EXTRA_SIZE
, SystemParm
, 7);
1870 if (retval
!= STATUS_SUCCESS
)
1871 TRACE_RET(chip
, retval
);
1873 ms_set_err_code(chip
, MS_NO_ERROR
);
1875 if (CHK_MS4BIT(ms_card
)) {
1876 /* Parallel interface */
1879 /* Serial interface */
1884 data
[2] = (u8
) (old_blk
>> 8);
1885 data
[3] = (u8
) old_blk
;
1892 ms_write_bytes(chip
, WRITE_REG
, 7, NO_WAIT_INT
,
1894 if (retval
!= STATUS_SUCCESS
)
1895 TRACE_RET(chip
, retval
);
1897 retval
= ms_send_cmd(chip
, BLOCK_WRITE
, WAIT_INT
);
1898 if (retval
!= STATUS_SUCCESS
)
1899 TRACE_RET(chip
, retval
);
1901 ms_set_err_code(chip
, MS_NO_ERROR
);
1903 ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
,
1905 if (retval
!= STATUS_SUCCESS
)
1906 TRACE_RET(chip
, retval
);
1908 if (val
& INT_REG_CMDNK
) {
1909 ms_set_err_code(chip
, MS_CMD_NK
);
1910 TRACE_RET(chip
, STATUS_FAIL
);
1913 if (val
& INT_REG_CED
) {
1914 if (val
& INT_REG_ERR
) {
1915 ms_set_err_code(chip
,
1916 MS_FLASH_WRITE_ERROR
);
1917 TRACE_RET(chip
, STATUS_FAIL
);
1923 return STATUS_SUCCESS
;
1927 static int ms_auto_copy_page(struct rts51x_chip
*chip
, u16 old_blk
, u16 new_blk
,
1928 u16 log_blk
, u8 start_page
, u8 end_page
)
1930 struct ms_info
*ms_card
= &(chip
->ms_card
);
1932 u8 page_len
, bus_width
, val
= 0;
1933 u8 extra
[MS_EXTRA_SIZE
];
1935 RTS51X_DEBUGP("Auto copy page from 0x%x to 0x%x,"
1936 "logical block is 0x%x\n",
1937 old_blk
, new_blk
, log_blk
);
1938 RTS51X_DEBUGP("start_page = %d, end_page = %d\n", start_page
,
1941 page_len
= end_page
- start_page
;
1943 retval
= ms_read_extra_data(chip
, new_blk
, 0, extra
, MS_EXTRA_SIZE
);
1944 if (retval
!= STATUS_SUCCESS
)
1945 TRACE_RET(chip
, retval
);
1947 retval
= ms_read_status_reg(chip
);
1948 if (retval
!= STATUS_SUCCESS
)
1949 TRACE_RET(chip
, retval
);
1951 RTS51X_READ_REG(chip
, PPBUF_BASE2
, &val
);
1953 if (val
& BUF_FULL
) {
1954 retval
= ms_send_cmd(chip
, CLEAR_BUF
, WAIT_INT
);
1955 if (retval
!= STATUS_SUCCESS
)
1956 TRACE_RET(chip
, retval
);
1958 retval
= ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
, 1);
1959 if (retval
!= STATUS_SUCCESS
)
1960 TRACE_RET(chip
, retval
);
1962 if (!(val
& INT_REG_CED
)) {
1963 ms_set_err_code(chip
, MS_FLASH_WRITE_ERROR
);
1964 TRACE_RET(chip
, STATUS_FAIL
);
1968 if (CHK_MS4BIT(ms_card
)) {
1969 /* Parallel interface */
1972 /* Serial interface */
1976 rts51x_init_cmd(chip
);
1978 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_OLD_BLOCK_0
, 0xFF, (u8
) old_blk
);
1979 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_OLD_BLOCK_1
, 0xFF,
1980 (u8
) (old_blk
>> 8));
1981 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_NEW_BLOCK_0
, 0xFF, (u8
) new_blk
);
1982 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_NEW_BLOCK_1
, 0xFF,
1983 (u8
) (new_blk
>> 8));
1984 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_LOG_BLOCK_0
, 0xFF, (u8
) log_blk
);
1985 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_LOG_BLOCK_1
, 0xFF,
1986 (u8
) (log_blk
>> 8));
1987 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_PAGE_START
, 0xFF, start_page
);
1988 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_PAGE_LENGTH
, 0xFF, page_len
);
1989 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_BUS_WIDTH
, 0xFF, bus_width
);
1991 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANSFER
, 0xFF,
1992 MS_TRANSFER_START
| MS_TM_COPY_PAGE
);
1993 rts51x_add_cmd(chip
, CHECK_REG_CMD
, MS_TRANSFER
, MS_TRANSFER_END
,
1996 retval
= rts51x_send_cmd(chip
, MODE_CR
, 100);
1997 if (retval
!= STATUS_SUCCESS
) {
1998 rts51x_clear_ms_error(chip
);
1999 TRACE_RET(chip
, retval
);
2002 retval
= rts51x_get_rsp(chip
, 1, 5000);
2004 if (CHECK_MS_TRANS_FAIL(chip
, retval
)) {
2005 rts51x_clear_ms_error(chip
);
2006 if (retval
== STATUS_TIMEDOUT
)
2007 TRACE_RET(chip
, retval
);
2008 TRACE_GOTO(chip
, Fail
);
2011 return STATUS_SUCCESS
;
2014 retval
= ms_erase_block(chip
, new_blk
);
2015 if (retval
!= STATUS_SUCCESS
)
2016 TRACE_RET(chip
, retval
);
2019 ms_copy_page(chip
, old_blk
, new_blk
, log_blk
, start_page
, end_page
);
2020 if (retval
!= STATUS_SUCCESS
)
2021 TRACE_RET(chip
, retval
);
2023 return STATUS_SUCCESS
;
2027 static int reset_ms(struct rts51x_chip
*chip
)
2029 struct ms_info
*ms_card
= &(chip
->ms_card
);
2031 u16 i
, reg_addr
, block_size
;
2033 #ifndef SUPPORT_MAGIC_GATE
2037 retval
= ms_prepare_reset(chip
);
2038 if (retval
!= STATUS_SUCCESS
)
2039 TRACE_RET(chip
, retval
);
2041 ms_card
->ms_type
|= TYPE_MS
;
2043 retval
= ms_send_cmd(chip
, MS_RESET
, NO_WAIT_INT
);
2044 if (retval
!= STATUS_SUCCESS
)
2045 TRACE_RET(chip
, retval
);
2047 retval
= ms_read_status_reg(chip
);
2048 if (retval
!= STATUS_SUCCESS
)
2049 TRACE_RET(chip
, retval
);
2051 RTS51X_READ_REG(chip
, PPBUF_BASE2
, &val
);
2052 if (val
& WRT_PRTCT
)
2053 chip
->card_wp
|= MS_CARD
;
2055 chip
->card_wp
&= ~MS_CARD
;
2060 /* Search For Boot Block */
2061 while (i
< (MAX_DEFECTIVE_BLOCK
+ 2)) {
2062 if (monitor_card_cd(chip
, MS_CARD
) == CD_NOT_EXIST
)
2063 TRACE_RET(chip
, STATUS_FAIL
);
2065 retval
= ms_check_boot_block(chip
, i
);
2066 if (retval
!= STATUS_SUCCESS
) {
2071 ms_card
->boot_block
= i
;
2075 if (i
== (MAX_DEFECTIVE_BLOCK
+ 2)) {
2076 RTS51X_DEBUGP("No boot block found!");
2077 TRACE_RET(chip
, STATUS_FAIL
);
2079 for (j
= 0; j
< 3; j
++) {
2080 retval
= ms_read_page(chip
, ms_card
->boot_block
, j
);
2081 if (retval
!= STATUS_SUCCESS
) {
2082 if (ms_check_err_code(chip
, MS_FLASH_WRITE_ERROR
)) {
2083 i
= ms_card
->boot_block
+ 1;
2084 ms_set_err_code(chip
, MS_NO_ERROR
);
2090 /* Read boot block contents */
2091 retval
= ms_read_page(chip
, ms_card
->boot_block
, 0);
2092 if (retval
!= STATUS_SUCCESS
)
2093 TRACE_RET(chip
, retval
);
2094 /* Read MS system information as sys_info */
2096 rts51x_seq_read_register(chip
, PPBUF_BASE2
+ 0x1A0, 96,
2097 ms_card
->raw_sys_info
);
2098 if (retval
!= STATUS_SUCCESS
)
2099 TRACE_RET(chip
, retval
);
2100 /* Read useful block contents */
2101 rts51x_init_cmd(chip
);
2103 rts51x_add_cmd(chip
, READ_REG_CMD
, HEADER_ID0
, 0, 0);
2104 rts51x_add_cmd(chip
, READ_REG_CMD
, HEADER_ID1
, 0, 0);
2106 for (reg_addr
= DISABLED_BLOCK0
; reg_addr
<= DISABLED_BLOCK3
;
2108 rts51x_add_cmd(chip
, READ_REG_CMD
, reg_addr
, 0, 0);
2111 for (reg_addr
= BLOCK_SIZE_0
; reg_addr
<= PAGE_SIZE_1
; reg_addr
++)
2112 rts51x_add_cmd(chip
, READ_REG_CMD
, reg_addr
, 0, 0);
2114 rts51x_add_cmd(chip
, READ_REG_CMD
, MS_Device_Type
, 0, 0);
2115 rts51x_add_cmd(chip
, READ_REG_CMD
, MS_4bit_Support
, 0, 0);
2117 retval
= rts51x_send_cmd(chip
, MODE_CR
, 100);
2118 if (retval
!= STATUS_SUCCESS
)
2119 TRACE_RET(chip
, retval
);
2121 retval
= rts51x_get_rsp(chip
, 16, 100);
2123 if (retval
!= STATUS_SUCCESS
)
2124 TRACE_RET(chip
, retval
);
2126 ptr
= rts51x_get_rsp_data(chip
);
2128 RTS51X_DEBUGP("Boot block data:\n");
2129 RTS51X_DUMP(ptr
, 16);
2131 if (ptr
[0] != 0x00 || ptr
[1] != 0x01) {
2132 i
= ms_card
->boot_block
+ 1;
2135 if (ptr
[12] != 0x02 || ptr
[13] != 0x00) {
2136 i
= ms_card
->boot_block
+ 1;
2139 if ((ptr
[14] == 1) || (ptr
[14] == 3))
2140 chip
->card_wp
|= MS_CARD
;
2141 block_size
= ((u16
) ptr
[6] << 8) | ptr
[7];
2142 if (block_size
== 0x0010) {
2143 ms_card
->block_shift
= 5;
2144 ms_card
->page_off
= 0x1F;
2145 } else if (block_size
== 0x0008) {
2146 ms_card
->block_shift
= 4;
2147 ms_card
->page_off
= 0x0F;
2149 ms_card
->total_block
= ((u16
) ptr
[8] << 8) | ptr
[9];
2151 #ifdef SUPPORT_MAGIC_GATE
2154 if (ms_card
->block_shift
== 4) {
2156 ms_card
->capacity
= 0x1EE0;
2158 ms_card
->capacity
= 0x3DE0;
2161 ms_card
->capacity
= 0x7BC0;
2163 ms_card
->capacity
= 0xF7C0;
2165 ms_card
->capacity
= 0x1EF80;
2167 ms_card
->capacity
= 0x3DF00;
2170 eblock_cnt
= ((u16
) ptr
[10] << 8) | ptr
[11];
2172 ms_card
->capacity
= ((u32
) eblock_cnt
- 2) << ms_card
->block_shift
;
2175 chip
->capacity
[chip
->card2lun
[MS_CARD
]] = ms_card
->capacity
;
2178 retval
= ms_set_rw_reg_addr(chip
, 0, 0, SystemParm
, 1);
2179 if (retval
!= STATUS_SUCCESS
)
2180 TRACE_RET(chip
, STATUS_FAIL
);
2181 RTS51X_WRITE_REG(chip
, PPBUF_BASE2
, 0xFF, 0x88);
2182 RTS51X_WRITE_REG(chip
, PPBUF_BASE2
+ 1, 0xFF, 0);
2185 ms_transfer_tpc(chip
, MS_TM_WRITE_BYTES
, WRITE_REG
, 1,
2187 if (retval
!= STATUS_SUCCESS
)
2188 TRACE_RET(chip
, STATUS_FAIL
);
2189 RTS51X_WRITE_REG(chip
, MS_CFG
, 0x58 | MS_NO_CHECK_INT
,
2190 MS_BUS_WIDTH_4
| PUSH_TIME_ODD
|
2193 ms_card
->ms_type
|= MS_4BIT
;
2196 if (CHK_MS4BIT(ms_card
))
2197 chip
->card_bus_width
[chip
->card2lun
[MS_CARD
]] = 4;
2199 chip
->card_bus_width
[chip
->card2lun
[MS_CARD
]] = 1;
2201 return STATUS_SUCCESS
;
2204 static int ms_init_l2p_tbl(struct rts51x_chip
*chip
)
2206 struct ms_info
*ms_card
= &(chip
->ms_card
);
2207 int size
, i
, seg_no
, retval
;
2208 u16 defect_block
, reg_addr
;
2211 ms_card
->segment_cnt
= ms_card
->total_block
>> 9;
2212 RTS51X_DEBUGP("ms_card->segment_cnt = %d\n", ms_card
->segment_cnt
);
2214 size
= ms_card
->segment_cnt
* sizeof(struct zone_entry
);
2215 ms_card
->segment
= vmalloc(size
);
2216 if (ms_card
->segment
== NULL
)
2217 TRACE_RET(chip
, STATUS_FAIL
);
2218 memset(ms_card
->segment
, 0, size
);
2220 retval
= ms_read_page(chip
, ms_card
->boot_block
, 1);
2221 if (retval
!= STATUS_SUCCESS
)
2222 TRACE_GOTO(chip
, INIT_FAIL
);
2224 reg_addr
= PPBUF_BASE2
;
2225 for (i
= 0; i
< (((ms_card
->total_block
>> 9) * 10) + 1); i
++) {
2226 retval
= rts51x_read_register(chip
, reg_addr
++, &val1
);
2227 if (retval
!= STATUS_SUCCESS
)
2228 TRACE_GOTO(chip
, INIT_FAIL
);
2229 retval
= rts51x_read_register(chip
, reg_addr
++, &val2
);
2230 if (retval
!= STATUS_SUCCESS
)
2231 TRACE_GOTO(chip
, INIT_FAIL
);
2233 defect_block
= ((u16
) val1
<< 8) | val2
;
2234 if (defect_block
== 0xFFFF)
2236 seg_no
= defect_block
/ 512;
2237 ms_card
->segment
[seg_no
].defect_list
[ms_card
->segment
[seg_no
].
2242 for (i
= 0; i
< ms_card
->segment_cnt
; i
++) {
2243 ms_card
->segment
[i
].build_flag
= 0;
2244 ms_card
->segment
[i
].l2p_table
= NULL
;
2245 ms_card
->segment
[i
].free_table
= NULL
;
2246 ms_card
->segment
[i
].get_index
= 0;
2247 ms_card
->segment
[i
].set_index
= 0;
2248 ms_card
->segment
[i
].unused_blk_cnt
= 0;
2250 RTS51X_DEBUGP("defective block count of segment %d is %d\n",
2251 i
, ms_card
->segment
[i
].disable_count
);
2254 return STATUS_SUCCESS
;
2257 if (ms_card
->segment
) {
2258 vfree(ms_card
->segment
);
2259 ms_card
->segment
= NULL
;
2265 static u16
ms_get_l2p_tbl(struct rts51x_chip
*chip
, int seg_no
, u16 log_off
)
2267 struct ms_info
*ms_card
= &(chip
->ms_card
);
2268 struct zone_entry
*segment
;
2270 if (ms_card
->segment
== NULL
)
2273 segment
= &(ms_card
->segment
[seg_no
]);
2275 if (segment
->l2p_table
)
2276 return segment
->l2p_table
[log_off
];
2281 static void ms_set_l2p_tbl(struct rts51x_chip
*chip
, int seg_no
, u16 log_off
,
2284 struct ms_info
*ms_card
= &(chip
->ms_card
);
2285 struct zone_entry
*segment
;
2287 if (ms_card
->segment
== NULL
)
2290 segment
= &(ms_card
->segment
[seg_no
]);
2291 if (segment
->l2p_table
)
2292 segment
->l2p_table
[log_off
] = phy_blk
;
2295 static void ms_set_unused_block(struct rts51x_chip
*chip
, u16 phy_blk
)
2297 struct ms_info
*ms_card
= &(chip
->ms_card
);
2298 struct zone_entry
*segment
;
2301 seg_no
= (int)phy_blk
>> 9;
2302 segment
= &(ms_card
->segment
[seg_no
]);
2304 segment
->free_table
[segment
->set_index
++] = phy_blk
;
2305 if (segment
->set_index
>= MS_FREE_TABLE_CNT
)
2306 segment
->set_index
= 0;
2307 segment
->unused_blk_cnt
++;
2310 static u16
ms_get_unused_block(struct rts51x_chip
*chip
, int seg_no
)
2312 struct ms_info
*ms_card
= &(chip
->ms_card
);
2313 struct zone_entry
*segment
;
2316 segment
= &(ms_card
->segment
[seg_no
]);
2318 if (segment
->unused_blk_cnt
<= 0)
2321 phy_blk
= segment
->free_table
[segment
->get_index
];
2322 segment
->free_table
[segment
->get_index
++] = 0xFFFF;
2323 if (segment
->get_index
>= MS_FREE_TABLE_CNT
)
2324 segment
->get_index
= 0;
2325 segment
->unused_blk_cnt
--;
2330 static const unsigned short ms_start_idx
[] = {
2331 0, 494, 990, 1486, 1982, 2478, 2974, 3470,
2332 3966, 4462, 4958, 5454, 5950, 6446, 6942, 7438, 7934
2335 static int ms_arbitrate_l2p(struct rts51x_chip
*chip
, u16 phy_blk
, u16 log_off
,
2338 struct ms_info
*ms_card
= &(chip
->ms_card
);
2339 struct zone_entry
*segment
;
2343 seg_no
= (int)phy_blk
>> 9;
2344 segment
= &(ms_card
->segment
[seg_no
]);
2345 tmp_blk
= segment
->l2p_table
[log_off
];
2349 if (!(chip
->card_wp
& MS_CARD
))
2350 ms_erase_block(chip
, tmp_blk
);
2351 ms_set_unused_block(chip
, tmp_blk
);
2352 segment
->l2p_table
[log_off
] = phy_blk
;
2354 if (!(chip
->card_wp
& MS_CARD
))
2355 ms_erase_block(chip
, phy_blk
);
2356 ms_set_unused_block(chip
, phy_blk
);
2359 if (phy_blk
< tmp_blk
) {
2360 if (!(chip
->card_wp
& MS_CARD
))
2361 ms_erase_block(chip
, phy_blk
);
2362 ms_set_unused_block(chip
, phy_blk
);
2364 if (!(chip
->card_wp
& MS_CARD
))
2365 ms_erase_block(chip
, tmp_blk
);
2366 ms_set_unused_block(chip
, tmp_blk
);
2367 segment
->l2p_table
[log_off
] = phy_blk
;
2371 return STATUS_SUCCESS
;
2374 static int ms_build_l2p_tbl(struct rts51x_chip
*chip
, int seg_no
)
2376 struct ms_info
*ms_card
= &(chip
->ms_card
);
2377 struct zone_entry
*segment
;
2378 int retval
, table_size
, disable_cnt
, defect_flag
, i
;
2379 u16 start
, end
, phy_blk
, log_blk
, tmp_blk
;
2380 u8 extra
[MS_EXTRA_SIZE
], us1
, us2
;
2382 RTS51X_DEBUGP("ms_build_l2p_tbl: %d\n", seg_no
);
2384 if (ms_card
->segment
== NULL
) {
2385 retval
= ms_init_l2p_tbl(chip
);
2386 if (retval
!= STATUS_SUCCESS
)
2387 TRACE_RET(chip
, retval
);
2390 if (ms_card
->segment
[seg_no
].build_flag
) {
2391 RTS51X_DEBUGP("l2p table of segment %d has been built\n",
2393 return STATUS_SUCCESS
;
2401 segment
= &(ms_card
->segment
[seg_no
]);
2403 if (segment
->l2p_table
== NULL
) {
2404 segment
->l2p_table
= vmalloc(table_size
* 2);
2405 if (segment
->l2p_table
== NULL
)
2406 TRACE_GOTO(chip
, BUILD_FAIL
);
2408 memset((u8
*) (segment
->l2p_table
), 0xff, table_size
* 2);
2410 if (segment
->free_table
== NULL
) {
2411 segment
->free_table
= vmalloc(MS_FREE_TABLE_CNT
* 2);
2412 if (segment
->free_table
== NULL
)
2413 TRACE_GOTO(chip
, BUILD_FAIL
);
2415 memset((u8
*) (segment
->free_table
), 0xff, MS_FREE_TABLE_CNT
* 2);
2417 start
= (u16
) seg_no
<< 9;
2418 end
= (u16
) (seg_no
+ 1) << 9;
2420 disable_cnt
= segment
->disable_count
;
2422 segment
->get_index
= segment
->set_index
= 0;
2423 segment
->unused_blk_cnt
= 0;
2425 for (phy_blk
= start
; phy_blk
< end
; phy_blk
++) {
2428 for (i
= 0; i
< segment
->disable_count
; i
++) {
2429 if (phy_blk
== segment
->defect_list
[i
]) {
2441 ms_read_extra_data(chip
, phy_blk
, 0, extra
, MS_EXTRA_SIZE
);
2442 if (retval
!= STATUS_SUCCESS
) {
2443 RTS51X_DEBUGP("read extra data fail\n");
2444 ms_set_bad_block(chip
, phy_blk
);
2448 if (seg_no
== ms_card
->segment_cnt
- 1) {
2449 if (!(extra
[1] & NOT_TRANSLATION_TABLE
)) {
2450 if (!(chip
->card_wp
& MS_CARD
)) {
2451 retval
= ms_erase_block(chip
, phy_blk
);
2452 if (retval
!= STATUS_SUCCESS
)
2460 if (!(extra
[0] & BLOCK_OK
))
2462 if (!(extra
[1] & NOT_BOOT_BLOCK
))
2464 if ((extra
[0] & PAGE_OK
) != PAGE_OK
)
2467 log_blk
= ((u16
) extra
[2] << 8) | extra
[3];
2469 if (log_blk
== 0xFFFF) {
2470 if (!(chip
->card_wp
& MS_CARD
)) {
2471 retval
= ms_erase_block(chip
, phy_blk
);
2472 if (retval
!= STATUS_SUCCESS
)
2475 ms_set_unused_block(chip
, phy_blk
);
2479 if ((log_blk
< ms_start_idx
[seg_no
]) ||
2480 (log_blk
>= ms_start_idx
[seg_no
+ 1])) {
2481 if (!(chip
->card_wp
& MS_CARD
)) {
2482 retval
= ms_erase_block(chip
, phy_blk
);
2483 if (retval
!= STATUS_SUCCESS
)
2486 ms_set_unused_block(chip
, phy_blk
);
2490 if (segment
->l2p_table
[log_blk
- ms_start_idx
[seg_no
]] ==
2492 segment
->l2p_table
[log_blk
- ms_start_idx
[seg_no
]] =
2497 us1
= extra
[0] & 0x10;
2498 tmp_blk
= segment
->l2p_table
[log_blk
- ms_start_idx
[seg_no
]];
2500 ms_read_extra_data(chip
, tmp_blk
, 0, extra
, MS_EXTRA_SIZE
);
2501 if (retval
!= STATUS_SUCCESS
)
2503 us2
= extra
[0] & 0x10;
2505 (void)ms_arbitrate_l2p(chip
, phy_blk
,
2506 log_blk
- ms_start_idx
[seg_no
], us1
,
2511 segment
->build_flag
= 1;
2513 RTS51X_DEBUGP("unused block count: %d\n", segment
->unused_blk_cnt
);
2515 if (seg_no
== ms_card
->segment_cnt
- 1) {
2516 if (segment
->unused_blk_cnt
< 2)
2517 chip
->card_wp
|= MS_CARD
;
2519 if (segment
->unused_blk_cnt
< 1)
2520 chip
->card_wp
|= MS_CARD
;
2523 if (chip
->card_wp
& MS_CARD
)
2524 return STATUS_SUCCESS
;
2526 for (log_blk
= ms_start_idx
[seg_no
]; log_blk
< ms_start_idx
[seg_no
+ 1];
2528 if (segment
->l2p_table
[log_blk
- ms_start_idx
[seg_no
]] ==
2530 phy_blk
= ms_get_unused_block(chip
, seg_no
);
2531 if (phy_blk
== 0xFFFF) {
2532 chip
->card_wp
|= MS_CARD
;
2533 return STATUS_SUCCESS
;
2535 retval
= ms_init_page(chip
, phy_blk
, log_blk
, 0, 1);
2536 if (retval
!= STATUS_SUCCESS
)
2537 TRACE_GOTO(chip
, BUILD_FAIL
);
2538 segment
->l2p_table
[log_blk
- ms_start_idx
[seg_no
]] =
2540 if (seg_no
== ms_card
->segment_cnt
- 1) {
2541 if (segment
->unused_blk_cnt
< 2) {
2542 chip
->card_wp
|= MS_CARD
;
2543 return STATUS_SUCCESS
;
2546 if (segment
->unused_blk_cnt
< 1) {
2547 chip
->card_wp
|= MS_CARD
;
2548 return STATUS_SUCCESS
;
2555 for (log_blk
= 0; log_blk
< 494; log_blk
++) {
2556 tmp_blk
= segment
->l2p_table
[log_blk
];
2557 if (tmp_blk
< ms_card
->boot_block
) {
2558 RTS51X_DEBUGP("Boot block is not the first"
2561 if (chip
->card_wp
& MS_CARD
)
2564 phy_blk
= ms_get_unused_block(chip
, 0);
2567 ms_auto_copy_page(chip
, tmp_blk
, phy_blk
,
2569 ms_card
->page_off
+ 1);
2571 retval
= ms_copy_page(chip
, tmp_blk
, phy_blk
,
2573 ms_card
->page_off
+ 1);
2575 if (retval
!= STATUS_SUCCESS
)
2576 TRACE_RET(chip
, retval
);
2578 segment
->l2p_table
[log_blk
] = phy_blk
;
2580 retval
= ms_set_bad_block(chip
, tmp_blk
);
2581 if (retval
!= STATUS_SUCCESS
)
2582 TRACE_RET(chip
, retval
);
2587 return STATUS_SUCCESS
;
2590 segment
->build_flag
= 0;
2591 if (segment
->l2p_table
) {
2592 vfree(segment
->l2p_table
);
2593 segment
->l2p_table
= NULL
;
2595 if (segment
->free_table
) {
2596 vfree(segment
->free_table
);
2597 segment
->free_table
= NULL
;
2603 int rts51x_reset_ms_card(struct rts51x_chip
*chip
)
2605 struct ms_info
*ms_card
= &(chip
->ms_card
);
2608 memset(ms_card
, 0, sizeof(struct ms_info
));
2610 rts51x_enable_card_clock(chip
, MS_CARD
);
2612 retval
= rts51x_select_card(chip
, MS_CARD
);
2613 if (retval
!= STATUS_SUCCESS
)
2614 TRACE_RET(chip
, retval
);
2616 ms_card
->ms_type
= 0;
2617 ms_card
->last_rw_int
= 0;
2619 retval
= reset_ms_pro(chip
);
2620 if (retval
!= STATUS_SUCCESS
) {
2621 if (ms_card
->check_ms_flow
) {
2622 retval
= reset_ms(chip
);
2623 if (retval
!= STATUS_SUCCESS
) {
2624 if (chip
->option
.reset_or_rw_fail_set_pad_drive
) {
2625 rts51x_write_register(chip
,
2626 CARD_DRIVE_SEL
, SD20_DRIVE_MASK
,
2629 TRACE_RET(chip
, retval
);
2632 if (chip
->option
.reset_or_rw_fail_set_pad_drive
) {
2633 rts51x_write_register(chip
, CARD_DRIVE_SEL
,
2637 TRACE_RET(chip
, retval
);
2641 retval
= ms_set_init_para(chip
);
2642 if (retval
!= STATUS_SUCCESS
)
2643 TRACE_RET(chip
, retval
);
2645 if (!CHK_MSPRO(ms_card
)) {
2646 retval
= ms_build_l2p_tbl(chip
, ms_card
->total_block
/ 512 - 1);
2647 if (retval
!= STATUS_SUCCESS
)
2648 TRACE_RET(chip
, retval
);
2651 RTS51X_DEBUGP("ms_card->ms_type = 0x%x\n", ms_card
->ms_type
);
2653 return STATUS_SUCCESS
;
2656 static int mspro_set_rw_cmd(struct rts51x_chip
*chip
, u32 start_sec
,
2657 u16 sec_cnt
, u8 cmd
)
2663 data
[1] = (u8
) (sec_cnt
>> 8);
2664 data
[2] = (u8
) sec_cnt
;
2665 data
[3] = (u8
) (start_sec
>> 24);
2666 data
[4] = (u8
) (start_sec
>> 16);
2667 data
[5] = (u8
) (start_sec
>> 8);
2668 data
[6] = (u8
) start_sec
;
2671 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
2673 ms_write_bytes(chip
, PRO_EX_SET_CMD
, 7, WAIT_INT
, data
, 8);
2674 if (retval
== STATUS_SUCCESS
)
2677 if (i
== MS_MAX_RETRY_COUNT
)
2678 TRACE_RET(chip
, STATUS_FAIL
);
2680 return STATUS_SUCCESS
;
2683 static void mspro_stop_seq_mode(struct rts51x_chip
*chip
)
2685 struct ms_info
*ms_card
= &(chip
->ms_card
);
2688 if (ms_card
->seq_mode
) {
2689 retval
= ms_switch_clock(chip
);
2690 if (retval
!= STATUS_SUCCESS
)
2693 ms_card
->seq_mode
= 0;
2694 ms_card
->total_sec_cnt
= 0;
2695 ms_card
->last_rw_int
= 0;
2696 ms_send_cmd(chip
, PRO_STOP
, WAIT_INT
);
2698 rts51x_ep0_write_register(chip
, MC_FIFO_CTL
, FIFO_FLUSH
,
2703 static inline int ms_auto_tune_clock(struct rts51x_chip
*chip
)
2705 struct ms_info
*ms_card
= &(chip
->ms_card
);
2708 if (chip
->asic_code
) {
2709 if (ms_card
->ms_clock
> 30)
2710 ms_card
->ms_clock
-= 20;
2712 if (ms_card
->ms_clock
== CLK_80
)
2713 ms_card
->ms_clock
= CLK_60
;
2714 else if (ms_card
->ms_clock
== CLK_60
)
2715 ms_card
->ms_clock
= CLK_40
;
2718 retval
= ms_switch_clock(chip
);
2719 if (retval
!= STATUS_SUCCESS
)
2720 TRACE_RET(chip
, retval
);
2722 return STATUS_SUCCESS
;
2725 static int mspro_rw_multi_sector(struct scsi_cmnd
*srb
,
2726 struct rts51x_chip
*chip
, u32 start_sector
,
2729 struct ms_info
*ms_card
= &(chip
->ms_card
);
2730 int retval
, mode_2k
= 0;
2732 u8 val
, trans_mode
, rw_tpc
, rw_cmd
;
2734 ms_set_err_code(chip
, MS_NO_ERROR
);
2736 ms_card
->counter
= 0;
2738 if (CHK_MSHG(ms_card
)) {
2739 if ((start_sector
% 4) || (sector_cnt
% 4)) {
2740 if (srb
->sc_data_direction
== DMA_FROM_DEVICE
) {
2741 rw_tpc
= PRO_READ_LONG_DATA
;
2742 rw_cmd
= PRO_READ_DATA
;
2744 rw_tpc
= PRO_WRITE_LONG_DATA
;
2745 rw_cmd
= PRO_WRITE_DATA
;
2748 if (srb
->sc_data_direction
== DMA_FROM_DEVICE
) {
2749 rw_tpc
= PRO_READ_QUAD_DATA
;
2750 rw_cmd
= PRO_READ_2K_DATA
;
2752 rw_tpc
= PRO_WRITE_QUAD_DATA
;
2753 rw_cmd
= PRO_WRITE_2K_DATA
;
2758 if (srb
->sc_data_direction
== DMA_FROM_DEVICE
) {
2759 rw_tpc
= PRO_READ_LONG_DATA
;
2760 rw_cmd
= PRO_READ_DATA
;
2762 rw_tpc
= PRO_WRITE_LONG_DATA
;
2763 rw_cmd
= PRO_WRITE_DATA
;
2767 retval
= ms_switch_clock(chip
);
2768 if (retval
!= STATUS_SUCCESS
)
2769 TRACE_RET(chip
, retval
);
2771 if (srb
->sc_data_direction
== DMA_FROM_DEVICE
)
2772 trans_mode
= MS_TM_AUTO_READ
;
2774 trans_mode
= MS_TM_AUTO_WRITE
;
2776 val
= ms_card
->last_rw_int
;
2778 if (ms_card
->seq_mode
) {
2779 if ((ms_card
->pre_dir
!= srb
->sc_data_direction
)
2780 || ((ms_card
->pre_sec_addr
+ ms_card
->pre_sec_cnt
) !=
2782 || (mode_2k
&& (ms_card
->seq_mode
& MODE_512_SEQ
))
2783 || (!mode_2k
&& (ms_card
->seq_mode
& MODE_2K_SEQ
))
2784 || !(val
& MS_INT_BREQ
)
2785 || ((ms_card
->total_sec_cnt
+ sector_cnt
) > 0xFE00)) {
2786 ms_card
->seq_mode
= 0;
2787 ms_card
->total_sec_cnt
= 0;
2788 ms_card
->last_rw_int
= 0;
2789 if (val
& MS_INT_BREQ
) {
2790 retval
= ms_send_cmd(chip
, PRO_STOP
, WAIT_INT
);
2791 if (retval
!= STATUS_SUCCESS
)
2792 TRACE_RET(chip
, retval
);
2794 rts51x_ep0_write_register(chip
, MC_FIFO_CTL
,
2795 FIFO_FLUSH
, FIFO_FLUSH
);
2800 if (!ms_card
->seq_mode
) {
2801 ms_card
->total_sec_cnt
= 0;
2802 if (sector_cnt
>= 0x80) {
2803 if ((ms_card
->capacity
- start_sector
) > 0xFE00)
2807 (u16
) (ms_card
->capacity
- start_sector
);
2808 if (count
> sector_cnt
) {
2810 ms_card
->seq_mode
|= MODE_2K_SEQ
;
2812 ms_card
->seq_mode
|= MODE_512_SEQ
;
2817 retval
= mspro_set_rw_cmd(chip
, start_sector
, count
, rw_cmd
);
2818 if (retval
!= STATUS_SUCCESS
) {
2819 ms_card
->seq_mode
= 0;
2820 TRACE_RET(chip
, retval
);
2825 ms_transfer_data(chip
, trans_mode
, rw_tpc
, sector_cnt
, WAIT_INT
,
2826 mode_2k
, scsi_sg_count(srb
), scsi_sglist(srb
),
2828 if (retval
!= STATUS_SUCCESS
) {
2829 ms_card
->seq_mode
= 0;
2830 rts51x_ep0_read_register(chip
, MS_TRANS_CFG
, &val
);
2831 rts51x_clear_ms_error(chip
);
2832 if (val
& MS_INT_BREQ
)
2833 ms_send_cmd(chip
, PRO_STOP
, WAIT_INT
);
2834 if (val
& (MS_CRC16_ERR
| MS_RDY_TIMEOUT
)) {
2835 RTS51X_DEBUGP("MSPro CRC error, tune clock!\n");
2836 ms_auto_tune_clock(chip
);
2839 TRACE_RET(chip
, retval
);
2842 ms_card
->pre_sec_addr
= start_sector
;
2843 ms_card
->pre_sec_cnt
= sector_cnt
;
2844 ms_card
->pre_dir
= srb
->sc_data_direction
;
2845 ms_card
->total_sec_cnt
+= sector_cnt
;
2847 return STATUS_SUCCESS
;
2850 static int mspro_read_format_progress(struct rts51x_chip
*chip
,
2851 const int short_data_len
)
2853 struct ms_info
*ms_card
= &(chip
->ms_card
);
2855 u32 total_progress
, cur_progress
;
2859 ms_card
->format_status
= FORMAT_FAIL
;
2861 retval
= ms_switch_clock(chip
);
2862 if (retval
!= STATUS_SUCCESS
)
2863 TRACE_RET(chip
, retval
);
2865 RTS51X_READ_REG(chip
, MS_TRANS_CFG
, &tmp
);
2867 if ((tmp
& (MS_INT_CED
| MS_INT_CMDNK
| MS_INT_ERR
)) == MS_INT_CED
) {
2868 ms_card
->format_status
= FORMAT_SUCCESS
;
2869 ms_card
->pro_under_formatting
= 0;
2870 return STATUS_SUCCESS
;
2873 ((tmp
& (MS_INT_BREQ
| MS_INT_CED
| MS_INT_CMDNK
| MS_INT_ERR
)) ==
2875 ms_card
->pro_under_formatting
= 0;
2876 TRACE_RET(chip
, STATUS_FAIL
);
2879 if (short_data_len
>= 256)
2882 cnt
= (u8
) short_data_len
;
2885 ms_read_bytes(chip
, PRO_READ_SHORT_DATA
, cnt
, WAIT_INT
, data
, 8);
2886 if (retval
!= STATUS_SUCCESS
)
2887 TRACE_RET(chip
, retval
);
2890 (data
[0] << 24) | (data
[1] << 16) | (data
[2] << 8) | data
[3];
2892 (data
[4] << 24) | (data
[5] << 16) | (data
[6] << 8) | data
[7];
2894 RTS51X_DEBUGP("total_progress = %d, cur_progress = %d\n",
2895 total_progress
, cur_progress
);
2897 if (total_progress
== 0) {
2898 ms_card
->progress
= 0;
2900 u64 ulltmp
= (u64
) cur_progress
* (u64
) 65535;
2901 do_div(ulltmp
, total_progress
);
2902 ms_card
->progress
= (u16
) ulltmp
;
2904 RTS51X_DEBUGP("progress = %d\n", ms_card
->progress
);
2906 for (i
= 0; i
< 2500; i
++) {
2907 RTS51X_READ_REG(chip
, MS_TRANS_CFG
, &tmp
);
2909 (MS_INT_CED
| MS_INT_CMDNK
| MS_INT_BREQ
| MS_INT_ERR
))
2916 TRACE_RET(chip
, STATUS_FAIL
);
2918 RTS51X_DEBUGP("MSPro format tmp:%d\n", tmp
);
2920 if (tmp
& (MS_INT_CMDNK
| MS_INT_ERR
))
2921 TRACE_RET(chip
, STATUS_FAIL
);
2922 if (tmp
& MS_INT_CED
) {
2923 ms_card
->format_status
= FORMAT_SUCCESS
;
2924 ms_card
->pro_under_formatting
= 0;
2925 } else if (tmp
& MS_INT_BREQ
) {
2926 ms_card
->format_status
= FORMAT_IN_PROGRESS
;
2928 ms_card
->format_status
= FORMAT_FAIL
;
2929 ms_card
->pro_under_formatting
= 0;
2930 TRACE_RET(chip
, STATUS_FAIL
);
2933 RTS51X_DEBUGP("MSPro format format_status:%d\n",
2934 ms_card
->format_status
);
2936 return STATUS_SUCCESS
;
2939 void rts51x_mspro_polling_format_status(struct rts51x_chip
*chip
)
2941 struct ms_info
*ms_card
= &(chip
->ms_card
);
2944 if (ms_card
->pro_under_formatting
) {
2945 for (i
= 0; i
< 65535; i
++) {
2946 mspro_read_format_progress(chip
, MS_SHORT_DATA_LEN
);
2947 if (ms_card
->format_status
!= FORMAT_IN_PROGRESS
)
2955 void rts51x_mspro_format_sense(struct rts51x_chip
*chip
, unsigned int lun
)
2957 struct ms_info
*ms_card
= &(chip
->ms_card
);
2959 if (CHK_FORMAT_STATUS(ms_card
, FORMAT_SUCCESS
)) {
2960 rts51x_set_sense_type(chip
, lun
, SENSE_TYPE_NO_SENSE
);
2961 ms_card
->pro_under_formatting
= 0;
2962 ms_card
->progress
= 0;
2963 } else if (CHK_FORMAT_STATUS(ms_card
, FORMAT_IN_PROGRESS
)) {
2964 rts51x_set_sense_data(chip
, lun
, CUR_ERR
, 0x02, 0, 0x04, 0x04,
2965 0, (u16
) (ms_card
->progress
));
2967 rts51x_set_sense_type(chip
, lun
, SENSE_TYPE_FORMAT_CMD_FAILED
);
2968 ms_card
->pro_under_formatting
= 0;
2969 ms_card
->progress
= 0;
2973 int rts51x_mspro_format(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
,
2974 int short_data_len
, int quick_format
)
2976 struct ms_info
*ms_card
= &(chip
->ms_card
);
2981 retval
= ms_switch_clock(chip
);
2982 if (retval
!= STATUS_SUCCESS
)
2983 TRACE_RET(chip
, retval
);
2985 retval
= ms_set_rw_reg_addr(chip
, 0x00, 0x00, Pro_TPCParm
, 0x01);
2986 if (retval
!= STATUS_SUCCESS
)
2987 TRACE_RET(chip
, retval
);
2990 switch (short_data_len
) {
3006 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
3008 ms_write_bytes(chip
, PRO_WRITE_REG
, 1, NO_WAIT_INT
, buf
, 2);
3009 if (retval
== STATUS_SUCCESS
)
3012 if (i
== MS_MAX_RETRY_COUNT
)
3013 TRACE_RET(chip
, STATUS_FAIL
);
3014 /* Format command */
3019 retval
= mspro_set_rw_cmd(chip
, 0, para
, PRO_FORMAT
);
3020 if (retval
!= STATUS_SUCCESS
)
3021 TRACE_RET(chip
, retval
);
3023 RTS51X_READ_REG(chip
, MS_TRANS_CFG
, &tmp
);
3024 if (tmp
& (MS_INT_CMDNK
| MS_INT_ERR
))
3025 TRACE_RET(chip
, STATUS_FAIL
);
3027 if ((tmp
& (MS_INT_BREQ
| MS_INT_CED
)) == MS_INT_BREQ
) {
3028 ms_card
->pro_under_formatting
= 1;
3029 ms_card
->progress
= 0;
3030 ms_card
->format_status
= FORMAT_IN_PROGRESS
;
3031 return STATUS_SUCCESS
;
3034 if (tmp
& MS_INT_CED
) {
3035 ms_card
->pro_under_formatting
= 0;
3036 ms_card
->progress
= 0;
3037 ms_card
->format_status
= FORMAT_SUCCESS
;
3038 rts51x_set_sense_type(chip
, SCSI_LUN(srb
), SENSE_TYPE_NO_SENSE
);
3039 return STATUS_SUCCESS
;
3042 TRACE_RET(chip
, STATUS_FAIL
);
3046 static int ms_read_multiple_pages(struct rts51x_chip
*chip
, u16 phy_blk
,
3047 u16 log_blk
, u8 start_page
, u8 end_page
,
3048 u8
*buf
, void **ptr
, unsigned int *offset
)
3050 struct ms_info
*ms_card
= &(chip
->ms_card
);
3053 u8 extra
[MS_EXTRA_SIZE
], val1
, val2
, data
[6];
3054 u8 page_cnt
= end_page
- start_page
, page_addr
, sec_cnt
;
3056 if (end_page
!= (ms_card
->page_off
+ 1))
3062 ms_read_extra_data(chip
, phy_blk
, start_page
, extra
, MS_EXTRA_SIZE
);
3063 if (retval
== STATUS_SUCCESS
) {
3064 if ((extra
[1] & 0x30) != 0x30) {
3065 ms_set_err_code(chip
, MS_FLASH_READ_ERROR
);
3066 TRACE_RET(chip
, STATUS_FAIL
);
3070 if (CHK_MS4BIT(ms_card
)) {
3071 /* Parallel interface */
3074 /* Serial interface */
3079 data
[2] = (u8
) (phy_blk
>> 8);
3080 data
[3] = (u8
) phy_blk
;
3082 * Extra data access mode */
3084 data
[5] = start_page
;
3087 ms_auto_set_cmd(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
, 6,
3088 BLOCK_READ
, WAIT_INT
, data
, 6, &val1
);
3089 if (retval
!= STATUS_SUCCESS
)
3090 TRACE_RET(chip
, retval
);
3092 rts51x_init_cmd(chip
);
3095 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_BLKEND
, SET_BLKEND
,
3098 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_BLKEND
, SET_BLKEND
, 0);
3099 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANS_CFG
, WAIT_INT
,
3101 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_SECTOR_CNT_L
, 0xFF,
3103 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_SECTOR_CNT_H
, 0xFF, 0);
3104 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TPC
, 0xFF, READ_PAGE_DATA
);
3106 rts51x_trans_dma_enable(DMA_FROM_DEVICE
, chip
, 512 * page_cnt
, DMA_512
);
3108 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANSFER
, 0xFF,
3109 MS_TRANSFER_START
| MS_TM_MULTI_READ
);
3110 rts51x_add_cmd(chip
, CHECK_REG_CMD
, MS_TRANSFER
, MS_TRANSFER_END
,
3113 retval
= rts51x_send_cmd(chip
, MODE_CDIR
| STAGE_MS_STATUS
, 100);
3114 if (retval
!= STATUS_SUCCESS
)
3115 TRACE_RET(chip
, retval
);
3118 rts51x_transfer_data_partial(chip
, RCV_BULK_PIPE(chip
), (void *)buf
,
3119 ptr
, offset
, 512 * page_cnt
,
3120 scsi_sg_count(chip
->srb
), NULL
, 2000);
3121 if (retval
!= STATUS_SUCCESS
) {
3122 rts51x_clear_ms_error(chip
);
3123 if (retval
== STATUS_TIMEDOUT
)
3124 TRACE_RET(chip
, retval
);
3125 TRACE_GOTO(chip
, Fail
);
3127 retval
= rts51x_get_rsp(chip
, 3, 200);
3128 if (CHECK_MS_TRANS_FAIL(chip
, retval
)) {
3129 rts51x_clear_ms_error(chip
);
3130 if (retval
== STATUS_TIMEDOUT
)
3131 TRACE_RET(chip
, retval
);
3132 TRACE_GOTO(chip
, Fail
);
3135 return STATUS_SUCCESS
;
3138 rts51x_init_cmd(chip
);
3140 rts51x_add_cmd(chip
, READ_REG_CMD
, MS_SECTOR_CNT_L
, 0, 0);
3142 retval
= rts51x_send_cmd(chip
, MODE_CR
| STAGE_MS_STATUS
, 100);
3143 if (retval
!= STATUS_SUCCESS
)
3144 TRACE_RET(chip
, retval
);
3146 retval
= rts51x_get_rsp(chip
, 3, 200);
3148 if (CHECK_MS_TRANS_FAIL(chip
, retval
))
3149 TRACE_RET(chip
, STATUS_FAIL
);
3151 sec_cnt
= chip
->rsp_buf
[0];
3152 RTS51X_DEBUGP("%d pages need be transferred, %d pages remained\n",
3153 (int)page_cnt
, (int)sec_cnt
);
3154 page_addr
= start_page
+ (page_cnt
- sec_cnt
);
3156 if (CHK_MS4BIT(ms_card
)) {
3157 val1
= chip
->rsp_buf
[1];
3158 RTS51X_DEBUGP("MS_TRANS_CFG: 0x%x\n", val1
);
3163 val2
= chip
->rsp_buf
[2];
3164 RTS51X_DEBUGP("GET_INT: 0x%x\n", val2
);
3166 if ((val1
& INT_CMDNK
) || (val2
& INT_REG_CMDNK
)) {
3167 ms_set_err_code(chip
, MS_CMD_NK
);
3168 TRACE_RET(chip
, STATUS_FAIL
);
3171 if ((val1
& INT_ERR
) || (val2
& INT_REG_ERR
)) {
3172 if ((val1
& INT_BREQ
) || (val2
& INT_REG_BREQ
)) {
3173 retval
= ms_read_status_reg(chip
);
3174 if (retval
!= STATUS_SUCCESS
) {
3175 if (!(chip
->card_wp
& MS_CARD
)) {
3177 ms_set_page_status(log_blk
, setPS_NG
,
3178 extra
, MS_EXTRA_SIZE
);
3179 ms_write_extra_data(chip
, phy_blk
,
3183 ms_set_err_code(chip
, MS_FLASH_READ_ERROR
);
3184 TRACE_RET(chip
, STATUS_FAIL
);
3187 ms_set_err_code(chip
, MS_FLASH_READ_ERROR
);
3188 TRACE_RET(chip
, STATUS_FAIL
);
3191 if (CHK_MS4BIT(ms_card
)) {
3192 if (!(val1
& INT_BREQ
) && !(val2
& INT_REG_BREQ
)) {
3193 ms_set_err_code(chip
, MS_BREQ_ERROR
);
3194 TRACE_RET(chip
, STATUS_FAIL
);
3197 if (!(val2
& INT_REG_BREQ
)) {
3198 ms_set_err_code(chip
, MS_BREQ_ERROR
);
3199 TRACE_RET(chip
, STATUS_FAIL
);
3204 TRACE_RET(chip
, STATUS_FAIL
);
3207 static int ms_write_multiple_pages(struct rts51x_chip
*chip
, u16 old_blk
,
3208 u16 new_blk
, u16 log_blk
, u8 start_page
,
3209 u8 end_page
, u8
*buf
, void **ptr
,
3210 unsigned int *offset
)
3212 struct ms_info
*ms_card
= &(chip
->ms_card
);
3216 u8 page_cnt
= end_page
- start_page
;
3218 if ((end_page
== (ms_card
->page_off
+ 1)) || (page_cnt
== 1))
3224 if (CHK_MS4BIT(ms_card
)) {
3225 /* Parallel interface */
3228 /* Serial interface */
3233 data
[2] = (u8
) (old_blk
>> 8);
3234 data
[3] = (u8
) old_blk
;
3241 ms_auto_set_cmd(chip
, OverwriteFlag
, MS_EXTRA_SIZE
,
3242 SystemParm
, 7, BLOCK_WRITE
, WAIT_INT
, data
,
3244 if (retval
!= STATUS_SUCCESS
)
3245 TRACE_RET(chip
, retval
);
3249 ms_set_rw_reg_addr(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
,
3250 (6 + MS_EXTRA_SIZE
));
3251 if (retval
!= STATUS_SUCCESS
)
3252 TRACE_RET(chip
, retval
);
3254 ms_set_err_code(chip
, MS_NO_ERROR
);
3256 if (CHK_MS4BIT(ms_card
)) {
3257 /* Parallel interface */
3260 /* Serial interface */
3265 data
[2] = (u8
) (new_blk
>> 8);
3266 data
[3] = (u8
) new_blk
;
3268 * Extra data access mode */
3269 if (page_cnt
== 1) {
3270 /* Single page access mode */
3273 /* Block access mode */
3276 data
[5] = start_page
;
3279 data
[8] = (u8
) (log_blk
>> 8);
3280 data
[9] = (u8
) log_blk
;
3282 for (i
= 0x0A; i
< 0x10; i
++) {
3288 ms_auto_set_cmd(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
,
3289 (6 + MS_EXTRA_SIZE
), BLOCK_WRITE
, WAIT_INT
, data
,
3291 if (retval
!= STATUS_SUCCESS
)
3292 TRACE_RET(chip
, retval
);
3294 rts51x_init_cmd(chip
);
3297 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_BLKEND
, SET_BLKEND
,
3300 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_BLKEND
, SET_BLKEND
, 0);
3301 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANS_CFG
, WAIT_INT
,
3303 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_SECTOR_CNT_L
, 0xFF,
3305 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_SECTOR_CNT_H
, 0xFF, 0);
3306 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TPC
, 0xFF, WRITE_PAGE_DATA
);
3307 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
, 0x01,
3310 rts51x_trans_dma_enable(DMA_TO_DEVICE
, chip
, 512 * page_cnt
, DMA_512
);
3312 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANSFER
, 0xFF,
3313 MS_TRANSFER_START
| MS_TM_MULTI_WRITE
);
3314 rts51x_add_cmd(chip
, CHECK_REG_CMD
, MS_TRANSFER
, MS_TRANSFER_END
,
3317 retval
= rts51x_send_cmd(chip
, MODE_CDOR
| STAGE_MS_STATUS
, 100);
3318 if (retval
!= STATUS_SUCCESS
)
3319 TRACE_RET(chip
, retval
);
3322 rts51x_transfer_data_partial(chip
, SND_BULK_PIPE(chip
), (void *)buf
,
3323 ptr
, offset
, 512 * page_cnt
,
3324 scsi_sg_count(chip
->srb
), NULL
, 2000);
3325 if (retval
!= STATUS_SUCCESS
) {
3326 rts51x_clear_ms_error(chip
);
3327 TRACE_RET(chip
, retval
);
3330 retval
= rts51x_get_rsp(chip
, 3, 2000);
3333 if (CHECK_MS_TRANS_FAIL(chip
, retval
)) {
3334 rts51x_clear_ms_error(chip
);
3335 TRACE_RET(chip
, STATUS_FAIL
);
3338 return STATUS_SUCCESS
;
3343 static int ms_read_multiple_pages(struct rts51x_chip
*chip
, u16 phy_blk
,
3344 u16 log_blk
, u8 start_page
, u8 end_page
,
3345 u8
*buf
, void **ptr
, unsigned int *offset
)
3347 struct ms_info
*ms_card
= &(chip
->ms_card
);
3349 u8 extra
[MS_EXTRA_SIZE
], page_addr
, val
, trans_cfg
, data
[6];
3352 ms_read_extra_data(chip
, phy_blk
, start_page
, extra
, MS_EXTRA_SIZE
);
3353 if (retval
== STATUS_SUCCESS
) {
3354 if ((extra
[1] & 0x30) != 0x30) {
3355 ms_set_err_code(chip
, MS_FLASH_READ_ERROR
);
3356 TRACE_RET(chip
, STATUS_FAIL
);
3361 ms_set_rw_reg_addr(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
,
3363 if (retval
!= STATUS_SUCCESS
)
3364 TRACE_RET(chip
, retval
);
3366 if (CHK_MS4BIT(ms_card
)) {
3367 /* Parallel interface */
3370 /* Serial interface */
3375 data
[2] = (u8
) (phy_blk
>> 8);
3376 data
[3] = (u8
) phy_blk
;
3378 * Extra data access mode */
3380 data
[5] = start_page
;
3382 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
3384 ms_write_bytes(chip
, WRITE_REG
, 6, NO_WAIT_INT
, data
, 6);
3385 if (retval
== STATUS_SUCCESS
)
3388 if (i
== MS_MAX_RETRY_COUNT
)
3389 TRACE_RET(chip
, STATUS_FAIL
);
3391 ms_set_err_code(chip
, MS_NO_ERROR
);
3393 retval
= ms_send_cmd(chip
, BLOCK_READ
, WAIT_INT
);
3394 if (retval
!= STATUS_SUCCESS
)
3395 TRACE_RET(chip
, retval
);
3397 for (page_addr
= start_page
; page_addr
< end_page
; page_addr
++) {
3398 ms_set_err_code(chip
, MS_NO_ERROR
);
3400 if (monitor_card_cd(chip
, MS_CARD
) == CD_NOT_EXIST
) {
3401 ms_set_err_code(chip
, MS_NO_CARD
);
3402 chip
->card_exist
&= ~MS_CARD
;
3403 chip
->card_ready
&= ~MS_CARD
;
3404 TRACE_RET(chip
, STATUS_FAIL
);
3406 /* GET_INT Register */
3407 retval
= ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
, 1);
3408 if (retval
!= STATUS_SUCCESS
)
3409 TRACE_RET(chip
, retval
);
3410 if (val
& INT_REG_CMDNK
) {
3411 ms_set_err_code(chip
, MS_CMD_NK
);
3412 TRACE_RET(chip
, STATUS_FAIL
);
3414 if (val
& INT_REG_ERR
) {
3415 if (val
& INT_REG_BREQ
) {
3416 retval
= ms_read_status_reg(chip
);
3417 if (retval
!= STATUS_SUCCESS
) {
3418 if (!(chip
->card_wp
& MS_CARD
)) {
3420 ms_set_page_status(log_blk
,
3423 ms_write_extra_data(chip
,
3425 extra
, MS_EXTRA_SIZE
);
3427 ms_set_err_code(chip
,
3428 MS_FLASH_READ_ERROR
);
3429 TRACE_RET(chip
, STATUS_FAIL
);
3432 ms_set_err_code(chip
, MS_FLASH_READ_ERROR
);
3433 TRACE_RET(chip
, STATUS_FAIL
);
3436 if (!(val
& INT_REG_BREQ
)) {
3437 ms_set_err_code(chip
, MS_BREQ_ERROR
);
3438 TRACE_RET(chip
, STATUS_FAIL
);
3442 if (page_addr
== (end_page
- 1)) {
3443 if (!(val
& INT_REG_CED
)) {
3444 retval
= ms_send_cmd(chip
, BLOCK_END
, WAIT_INT
);
3445 if (retval
!= STATUS_SUCCESS
)
3446 TRACE_RET(chip
, retval
);
3449 ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
,
3451 if (retval
!= STATUS_SUCCESS
)
3452 TRACE_RET(chip
, retval
);
3453 if (!(val
& INT_REG_CED
)) {
3454 ms_set_err_code(chip
, MS_FLASH_READ_ERROR
);
3455 TRACE_RET(chip
, STATUS_FAIL
);
3458 trans_cfg
= NO_WAIT_INT
;
3460 trans_cfg
= WAIT_INT
;
3463 rts51x_init_cmd(chip
);
3465 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TPC
, 0xFF,
3467 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANS_CFG
, 0xFF,
3470 rts51x_trans_dma_enable(DMA_FROM_DEVICE
, chip
, 512, DMA_512
);
3472 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANSFER
, 0xFF,
3473 MS_TRANSFER_START
| MS_TM_NORMAL_READ
);
3474 rts51x_add_cmd(chip
, CHECK_REG_CMD
, MS_TRANSFER
,
3475 MS_TRANSFER_END
, MS_TRANSFER_END
);
3477 retval
= rts51x_send_cmd(chip
, MODE_CDIR
, 100);
3478 if (retval
!= STATUS_SUCCESS
)
3479 TRACE_RET(chip
, retval
);
3482 rts51x_transfer_data_partial(chip
, RCV_BULK_PIPE(chip
),
3483 (void *)buf
, ptr
, offset
, 512,
3484 scsi_sg_count(chip
->srb
), NULL
,
3486 if (retval
!= STATUS_SUCCESS
) {
3487 if (retval
== STATUS_TIMEDOUT
) {
3488 ms_set_err_code(chip
, MS_TO_ERROR
);
3489 rts51x_clear_ms_error(chip
);
3490 TRACE_RET(chip
, retval
);
3494 rts51x_ep0_read_register(chip
, MS_TRANS_CFG
, &val
);
3495 if (retval
!= STATUS_SUCCESS
) {
3496 ms_set_err_code(chip
, MS_TO_ERROR
);
3497 rts51x_clear_ms_error(chip
);
3498 TRACE_RET(chip
, STATUS_FAIL
);
3500 if (val
& (MS_CRC16_ERR
| MS_RDY_TIMEOUT
)) {
3501 ms_set_err_code(chip
, MS_CRC16_ERROR
);
3502 rts51x_clear_ms_error(chip
);
3503 TRACE_RET(chip
, STATUS_FAIL
);
3507 retval
= rts51x_get_rsp(chip
, 1, 2000);
3508 if (CHECK_MS_TRANS_FAIL(chip
, retval
)) {
3509 if (retval
== STATUS_TIMEDOUT
) {
3510 ms_set_err_code(chip
, MS_TO_ERROR
);
3511 rts51x_clear_ms_error(chip
);
3512 TRACE_RET(chip
, retval
);
3516 rts51x_ep0_read_register(chip
, MS_TRANS_CFG
, &val
);
3517 if (retval
!= STATUS_SUCCESS
) {
3518 ms_set_err_code(chip
, MS_TO_ERROR
);
3519 rts51x_clear_ms_error(chip
);
3520 TRACE_RET(chip
, retval
);
3522 if (val
& (MS_CRC16_ERR
| MS_RDY_TIMEOUT
)) {
3523 ms_set_err_code(chip
, MS_CRC16_ERROR
);
3524 rts51x_clear_ms_error(chip
);
3525 TRACE_RET(chip
, STATUS_FAIL
);
3530 return STATUS_SUCCESS
;
3533 static int ms_write_multiple_pages(struct rts51x_chip
*chip
, u16 old_blk
,
3534 u16 new_blk
, u16 log_blk
, u8 start_page
,
3535 u8 end_page
, u8
*buf
, void **ptr
,
3536 unsigned int *offset
)
3538 struct ms_info
*ms_card
= &(chip
->ms_card
);
3540 u8 page_addr
, val
, data
[16];
3544 ms_set_rw_reg_addr(chip
, OverwriteFlag
, MS_EXTRA_SIZE
,
3546 if (retval
!= STATUS_SUCCESS
)
3547 TRACE_RET(chip
, retval
);
3549 if (CHK_MS4BIT(ms_card
)) {
3550 /* Parallel interface */
3553 /* Serial interface */
3558 data
[2] = (u8
) (old_blk
>> 8);
3559 data
[3] = (u8
) old_blk
;
3566 ms_write_bytes(chip
, WRITE_REG
, 7, NO_WAIT_INT
, data
, 8);
3567 if (retval
!= STATUS_SUCCESS
)
3568 TRACE_RET(chip
, retval
);
3570 retval
= ms_send_cmd(chip
, BLOCK_WRITE
, WAIT_INT
);
3571 if (retval
!= STATUS_SUCCESS
)
3572 TRACE_RET(chip
, retval
);
3573 /* GET_INT Register */
3574 ms_set_err_code(chip
, MS_NO_ERROR
);
3576 ms_transfer_tpc(chip
, MS_TM_READ_BYTES
, GET_INT
, 1,
3578 if (retval
!= STATUS_SUCCESS
)
3579 TRACE_RET(chip
, retval
);
3583 ms_set_rw_reg_addr(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
,
3584 (6 + MS_EXTRA_SIZE
));
3585 if (retval
!= STATUS_SUCCESS
)
3586 TRACE_RET(chip
, retval
);
3588 ms_set_err_code(chip
, MS_NO_ERROR
);
3590 if (CHK_MS4BIT(ms_card
)) {
3591 /* Parallel interface */
3594 /* Serial interface */
3599 data
[2] = (u8
) (new_blk
>> 8);
3600 data
[3] = (u8
) new_blk
;
3602 * Extra data access mode */
3603 if ((end_page
- start_page
) == 1) {
3604 /* Single page access mode */
3607 /* Block access mode */
3610 data
[5] = start_page
;
3613 data
[8] = (u8
) (log_blk
>> 8);
3614 data
[9] = (u8
) log_blk
;
3616 for (i
= 0x0A; i
< 0x10; i
++) {
3621 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
3623 ms_write_bytes(chip
, WRITE_REG
, 6 + MS_EXTRA_SIZE
,
3624 NO_WAIT_INT
, data
, 16);
3625 if (retval
== STATUS_SUCCESS
)
3628 if (i
== MS_MAX_RETRY_COUNT
)
3629 TRACE_RET(chip
, STATUS_FAIL
);
3631 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
3632 retval
= ms_send_cmd(chip
, BLOCK_WRITE
, WAIT_INT
);
3633 if (retval
== STATUS_SUCCESS
)
3636 if (i
== MS_MAX_RETRY_COUNT
)
3637 TRACE_RET(chip
, STATUS_FAIL
);
3638 /* GET_INT Register */
3639 retval
= ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
, 1);
3640 if (retval
!= STATUS_SUCCESS
)
3641 TRACE_RET(chip
, retval
);
3643 for (page_addr
= start_page
; page_addr
< end_page
; page_addr
++) {
3644 ms_set_err_code(chip
, MS_NO_ERROR
);
3646 if (monitor_card_cd(chip
, MS_CARD
) == CD_NOT_EXIST
) {
3647 ms_set_err_code(chip
, MS_NO_CARD
);
3648 TRACE_RET(chip
, STATUS_FAIL
);
3651 if (val
& INT_REG_CMDNK
) {
3652 ms_set_err_code(chip
, MS_CMD_NK
);
3653 TRACE_RET(chip
, STATUS_FAIL
);
3655 if (val
& INT_REG_ERR
) {
3656 ms_set_err_code(chip
, MS_FLASH_WRITE_ERROR
);
3657 TRACE_RET(chip
, STATUS_FAIL
);
3659 if (!(val
& INT_REG_BREQ
)) {
3660 ms_set_err_code(chip
, MS_BREQ_ERROR
);
3661 TRACE_RET(chip
, STATUS_FAIL
);
3666 rts51x_init_cmd(chip
);
3668 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TPC
, 0xFF,
3670 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANS_CFG
, 0xFF,
3673 rts51x_trans_dma_enable(DMA_TO_DEVICE
, chip
, 512, DMA_512
);
3675 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANSFER
, 0xFF,
3676 MS_TRANSFER_START
| MS_TM_NORMAL_WRITE
);
3677 rts51x_add_cmd(chip
, CHECK_REG_CMD
, MS_TRANSFER
,
3678 MS_TRANSFER_END
, MS_TRANSFER_END
);
3680 retval
= rts51x_send_cmd(chip
, MODE_CDOR
, 100);
3681 if (retval
!= STATUS_SUCCESS
)
3682 TRACE_RET(chip
, retval
);
3685 rts51x_transfer_data_partial(chip
, SND_BULK_PIPE(chip
),
3686 (void *)buf
, ptr
, offset
, 512,
3687 scsi_sg_count(chip
->srb
), NULL
,
3689 if (retval
!= STATUS_SUCCESS
) {
3690 ms_set_err_code(chip
, MS_TO_ERROR
);
3691 rts51x_clear_ms_error(chip
);
3693 if (retval
== STATUS_TIMEDOUT
)
3694 TRACE_RET(chip
, STATUS_TIMEDOUT
);
3696 TRACE_RET(chip
, STATUS_FAIL
);
3699 retval
= rts51x_get_rsp(chip
, 1, 2000);
3700 if (CHECK_MS_TRANS_FAIL(chip
, retval
)) {
3701 ms_set_err_code(chip
, MS_TO_ERROR
);
3702 rts51x_clear_ms_error(chip
);
3704 if (retval
== STATUS_TIMEDOUT
)
3705 TRACE_RET(chip
, STATUS_TIMEDOUT
);
3707 TRACE_RET(chip
, STATUS_FAIL
);
3709 /* GET_INT Register */
3710 retval
= ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
, 1);
3711 if (retval
!= STATUS_SUCCESS
)
3712 TRACE_RET(chip
, retval
);
3714 if ((end_page
- start_page
) == 1) {
3715 if (!(val
& INT_REG_CED
)) {
3716 /* Command can not be executed */
3717 ms_set_err_code(chip
, MS_FLASH_WRITE_ERROR
);
3718 TRACE_RET(chip
, STATUS_FAIL
);
3721 if (page_addr
== (end_page
- 1)) {
3722 if (!(val
& INT_REG_CED
)) {
3724 ms_send_cmd(chip
, BLOCK_END
,
3726 if (retval
!= STATUS_SUCCESS
)
3727 TRACE_RET(chip
, retval
);
3729 /* GET_INT Register */
3731 ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
,
3733 if (retval
!= STATUS_SUCCESS
)
3734 TRACE_RET(chip
, retval
);
3737 if ((page_addr
== (end_page
- 1))
3738 || (page_addr
== ms_card
->page_off
)) {
3739 if (!(val
& INT_REG_CED
)) {
3740 ms_set_err_code(chip
,
3741 MS_FLASH_WRITE_ERROR
);
3742 TRACE_RET(chip
, STATUS_FAIL
);
3748 return STATUS_SUCCESS
;
3752 static int ms_finish_write(struct rts51x_chip
*chip
, u16 old_blk
, u16 new_blk
,
3753 u16 log_blk
, u8 page_off
)
3755 struct ms_info
*ms_card
= &(chip
->ms_card
);
3759 retval
= ms_auto_copy_page(chip
, old_blk
, new_blk
, log_blk
,
3760 page_off
, ms_card
->page_off
+ 1);
3762 retval
= ms_copy_page(chip
, old_blk
, new_blk
, log_blk
,
3763 page_off
, ms_card
->page_off
+ 1);
3765 if (retval
!= STATUS_SUCCESS
)
3766 TRACE_RET(chip
, retval
);
3768 seg_no
= old_blk
>> 9;
3770 if (MS_TST_BAD_BLOCK_FLG(ms_card
)) {
3771 MS_CLR_BAD_BLOCK_FLG(ms_card
);
3772 ms_set_bad_block(chip
, old_blk
);
3774 retval
= ms_erase_block(chip
, old_blk
);
3775 if (retval
== STATUS_SUCCESS
)
3776 ms_set_unused_block(chip
, old_blk
);
3779 ms_set_l2p_tbl(chip
, seg_no
, log_blk
- ms_start_idx
[seg_no
], new_blk
);
3781 return STATUS_SUCCESS
;
3784 static int ms_prepare_write(struct rts51x_chip
*chip
, u16 old_blk
, u16 new_blk
,
3785 u16 log_blk
, u8 start_page
)
3792 ms_auto_copy_page(chip
, old_blk
, new_blk
, log_blk
, 0,
3796 ms_copy_page(chip
, old_blk
, new_blk
, log_blk
, 0,
3799 if (retval
!= STATUS_SUCCESS
)
3800 TRACE_RET(chip
, retval
);
3803 return STATUS_SUCCESS
;
3806 int rts51x_ms_delay_write(struct rts51x_chip
*chip
)
3808 struct ms_info
*ms_card
= &(chip
->ms_card
);
3809 struct rts51x_ms_delay_write_tag
*delay_write
= &(ms_card
->delay_write
);
3812 if (delay_write
->delay_write_flag
) {
3813 retval
= ms_set_init_para(chip
);
3814 if (retval
!= STATUS_SUCCESS
)
3815 TRACE_RET(chip
, retval
);
3817 delay_write
->delay_write_flag
= 0;
3818 retval
= ms_finish_write(chip
,
3819 delay_write
->old_phyblock
,
3820 delay_write
->new_phyblock
,
3821 delay_write
->logblock
,
3822 delay_write
->pageoff
);
3823 if (retval
!= STATUS_SUCCESS
)
3824 TRACE_RET(chip
, retval
);
3827 return STATUS_SUCCESS
;
3830 static inline void rts51x_ms_rw_fail(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
)
3832 if (srb
->sc_data_direction
== DMA_FROM_DEVICE
)
3833 rts51x_set_sense_type(chip
, SCSI_LUN(srb
),
3834 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR
);
3836 rts51x_set_sense_type(chip
, SCSI_LUN(srb
), SENSE_TYPE_MEDIA_WRITE_ERR
);
3839 static int rts51x_ms_rw_multi_sector(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
,
3840 u32 start_sector
, u16 sector_cnt
)
3842 struct ms_info
*ms_card
= &(chip
->ms_card
);
3843 unsigned int lun
= SCSI_LUN(srb
);
3845 unsigned int offset
= 0;
3846 u16 old_blk
= 0, new_blk
= 0, log_blk
, total_sec_cnt
= sector_cnt
;
3847 u8 start_page
, end_page
= 0, page_cnt
;
3850 struct rts51x_ms_delay_write_tag
*delay_write
= &(ms_card
->delay_write
);
3852 ms_set_err_code(chip
, MS_NO_ERROR
);
3854 ms_card
->counter
= 0;
3856 buf
= (u8
*) scsi_sglist(srb
);
3858 retval
= ms_switch_clock(chip
);
3859 if (retval
!= STATUS_SUCCESS
) {
3860 rts51x_ms_rw_fail(srb
, chip
);
3861 TRACE_RET(chip
, retval
);
3864 log_blk
= (u16
) (start_sector
>> ms_card
->block_shift
);
3865 start_page
= (u8
) (start_sector
& ms_card
->page_off
);
3867 for (seg_no
= 0; seg_no
< ARRAY_SIZE(ms_start_idx
) - 1; seg_no
++) {
3868 if (log_blk
< ms_start_idx
[seg_no
+ 1])
3872 if (ms_card
->segment
[seg_no
].build_flag
== 0) {
3873 retval
= ms_build_l2p_tbl(chip
, seg_no
);
3874 if (retval
!= STATUS_SUCCESS
) {
3875 chip
->card_fail
|= MS_CARD
;
3876 rts51x_set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_NOT_PRESENT
);
3877 TRACE_RET(chip
, retval
);
3881 if (srb
->sc_data_direction
== DMA_TO_DEVICE
) {
3882 if (delay_write
->delay_write_flag
&&
3883 (delay_write
->logblock
== log_blk
) &&
3884 (start_page
> delay_write
->pageoff
)) {
3885 delay_write
->delay_write_flag
= 0;
3887 retval
= ms_auto_copy_page(chip
,
3888 delay_write
->old_phyblock
,
3889 delay_write
->new_phyblock
,
3891 delay_write
->pageoff
,
3894 retval
= ms_copy_page(chip
,
3895 delay_write
->old_phyblock
,
3896 delay_write
->new_phyblock
,
3897 log_blk
, delay_write
->pageoff
,
3900 if (retval
!= STATUS_SUCCESS
) {
3901 rts51x_set_sense_type(chip
, lun
,
3902 SENSE_TYPE_MEDIA_WRITE_ERR
);
3903 TRACE_RET(chip
, retval
);
3905 old_blk
= delay_write
->old_phyblock
;
3906 new_blk
= delay_write
->new_phyblock
;
3907 } else if (delay_write
->delay_write_flag
&&
3908 (delay_write
->logblock
== log_blk
) &&
3909 (start_page
== delay_write
->pageoff
)) {
3910 delay_write
->delay_write_flag
= 0;
3911 old_blk
= delay_write
->old_phyblock
;
3912 new_blk
= delay_write
->new_phyblock
;
3914 retval
= rts51x_ms_delay_write(chip
);
3915 if (retval
!= STATUS_SUCCESS
) {
3916 rts51x_set_sense_type(chip
, lun
,
3917 SENSE_TYPE_MEDIA_WRITE_ERR
);
3918 TRACE_RET(chip
, retval
);
3921 ms_get_l2p_tbl(chip
, seg_no
,
3922 log_blk
- ms_start_idx
[seg_no
]);
3923 new_blk
= ms_get_unused_block(chip
, seg_no
);
3924 if ((old_blk
== 0xFFFF) || (new_blk
== 0xFFFF)) {
3925 rts51x_set_sense_type(chip
, lun
,
3926 SENSE_TYPE_MEDIA_WRITE_ERR
);
3927 TRACE_RET(chip
, STATUS_FAIL
);
3931 ms_prepare_write(chip
, old_blk
, new_blk
, log_blk
,
3933 if (retval
!= STATUS_SUCCESS
) {
3934 if (monitor_card_cd(chip
, MS_CARD
) ==
3936 rts51x_set_sense_type(chip
, lun
,
3937 SENSE_TYPE_MEDIA_NOT_PRESENT
);
3938 TRACE_RET(chip
, STATUS_FAIL
);
3941 rts51x_set_sense_type(chip
, lun
,
3942 SENSE_TYPE_MEDIA_WRITE_ERR
);
3943 TRACE_RET(chip
, retval
);
3947 retval
= rts51x_ms_delay_write(chip
);
3948 if (retval
!= STATUS_SUCCESS
) {
3949 if (monitor_card_cd(chip
, MS_CARD
) == CD_NOT_EXIST
) {
3950 rts51x_set_sense_type(chip
, lun
,
3951 SENSE_TYPE_MEDIA_NOT_PRESENT
);
3952 TRACE_RET(chip
, STATUS_FAIL
);
3955 rts51x_set_sense_type(chip
, lun
,
3956 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR
);
3957 TRACE_RET(chip
, retval
);
3960 ms_get_l2p_tbl(chip
, seg_no
,
3961 log_blk
- ms_start_idx
[seg_no
]);
3962 if (old_blk
== 0xFFFF) {
3963 rts51x_set_sense_type(chip
, lun
,
3964 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR
);
3965 TRACE_RET(chip
, STATUS_FAIL
);
3969 RTS51X_DEBUGP("seg_no = %d, old_blk = 0x%x, new_blk = 0x%x\n", seg_no
,
3972 while (total_sec_cnt
) {
3973 if ((start_page
+ total_sec_cnt
) > (ms_card
->page_off
+ 1))
3974 end_page
= ms_card
->page_off
+ 1;
3976 end_page
= start_page
+ (u8
) total_sec_cnt
;
3977 page_cnt
= end_page
- start_page
;
3979 RTS51X_DEBUGP("start_page = %d, end_page = %d,"
3981 start_page
, end_page
, page_cnt
);
3983 if (srb
->sc_data_direction
== DMA_FROM_DEVICE
)
3984 retval
= ms_read_multiple_pages(chip
,
3986 start_page
, end_page
,
3987 buf
, &ptr
, &offset
);
3989 retval
= ms_write_multiple_pages(chip
, old_blk
,
3991 start_page
, end_page
,
3992 buf
, &ptr
, &offset
);
3994 if (retval
!= STATUS_SUCCESS
) {
3995 if (monitor_card_cd(chip
, MS_CARD
) == CD_NOT_EXIST
) {
3996 rts51x_set_sense_type(chip
, lun
,
3997 SENSE_TYPE_MEDIA_NOT_PRESENT
);
3998 TRACE_RET(chip
, STATUS_FAIL
);
4001 rts51x_ms_rw_fail(srb
, chip
);
4002 TRACE_RET(chip
, retval
);
4004 /* Update L2P table if need */
4005 if (srb
->sc_data_direction
== DMA_TO_DEVICE
) {
4006 if (end_page
== (ms_card
->page_off
+ 1)) {
4007 retval
= ms_erase_block(chip
, old_blk
);
4008 if (retval
== STATUS_SUCCESS
)
4009 ms_set_unused_block(chip
, old_blk
);
4010 ms_set_l2p_tbl(chip
, seg_no
,
4011 log_blk
- ms_start_idx
[seg_no
],
4016 total_sec_cnt
-= page_cnt
;
4018 if (total_sec_cnt
== 0)
4023 for (seg_no
= 0; seg_no
< ARRAY_SIZE(ms_start_idx
) - 1;
4025 if (log_blk
< ms_start_idx
[seg_no
+ 1])
4029 if (ms_card
->segment
[seg_no
].build_flag
== 0) {
4030 retval
= ms_build_l2p_tbl(chip
, seg_no
);
4031 if (retval
!= STATUS_SUCCESS
) {
4032 chip
->card_fail
|= MS_CARD
;
4033 rts51x_set_sense_type(chip
, lun
,
4034 SENSE_TYPE_MEDIA_NOT_PRESENT
);
4035 TRACE_RET(chip
, retval
);
4040 ms_get_l2p_tbl(chip
, seg_no
,
4041 log_blk
- ms_start_idx
[seg_no
]);
4042 if (old_blk
== 0xFFFF) {
4043 rts51x_ms_rw_fail(srb
, chip
);
4044 TRACE_RET(chip
, STATUS_FAIL
);
4047 if (srb
->sc_data_direction
== DMA_TO_DEVICE
) {
4048 new_blk
= ms_get_unused_block(chip
, seg_no
);
4049 if (new_blk
== 0xFFFF) {
4050 rts51x_ms_rw_fail(srb
, chip
);
4051 TRACE_RET(chip
, STATUS_FAIL
);
4055 RTS51X_DEBUGP("seg_no = %d, old_blk = 0x%x, new_blk = 0x%x\n",
4056 seg_no
, old_blk
, new_blk
);
4061 if (srb
->sc_data_direction
== DMA_TO_DEVICE
) {
4062 if (end_page
< (ms_card
->page_off
+ 1)) {
4063 delay_write
->delay_write_flag
= 1;
4064 delay_write
->old_phyblock
= old_blk
;
4065 delay_write
->new_phyblock
= new_blk
;
4066 delay_write
->logblock
= log_blk
;
4067 delay_write
->pageoff
= end_page
;
4071 scsi_set_resid(srb
, 0);
4073 return STATUS_SUCCESS
;
4076 int rts51x_ms_rw(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
, u32 start_sector
,
4079 struct ms_info
*ms_card
= &(chip
->ms_card
);
4082 if (CHK_MSPRO(ms_card
))
4084 mspro_rw_multi_sector(srb
, chip
, start_sector
, sector_cnt
);
4087 rts51x_ms_rw_multi_sector(srb
, chip
, start_sector
, sector_cnt
);
4092 void rts51x_ms_free_l2p_tbl(struct rts51x_chip
*chip
)
4094 struct ms_info
*ms_card
= &(chip
->ms_card
);
4097 if (ms_card
->segment
!= NULL
) {
4098 for (i
= 0; i
< ms_card
->segment_cnt
; i
++) {
4099 if (ms_card
->segment
[i
].l2p_table
!= NULL
) {
4100 vfree(ms_card
->segment
[i
].l2p_table
);
4101 ms_card
->segment
[i
].l2p_table
= NULL
;
4103 if (ms_card
->segment
[i
].free_table
!= NULL
) {
4104 vfree(ms_card
->segment
[i
].free_table
);
4105 ms_card
->segment
[i
].free_table
= NULL
;
4108 vfree(ms_card
->segment
);
4109 ms_card
->segment
= NULL
;
4113 void rts51x_ms_cleanup_work(struct rts51x_chip
*chip
)
4115 struct ms_info
*ms_card
= &(chip
->ms_card
);
4117 if (CHK_MSPRO(ms_card
)) {
4118 if (ms_card
->seq_mode
) {
4119 RTS51X_DEBUGP("MS Pro: stop transmission\n");
4120 mspro_stop_seq_mode(chip
);
4121 ms_card
->counter
= 0;
4123 if (CHK_MSHG(ms_card
)) {
4125 rts51x_read_register(chip
, MS_CFG
, &value
);
4126 if (value
& MS_2K_SECTOR_MODE
)
4127 rts51x_write_register(chip
, MS_CFG
,
4128 MS_2K_SECTOR_MODE
, 0x00);
4130 } else if ((!CHK_MSPRO(ms_card
))
4131 && ms_card
->delay_write
.delay_write_flag
) {
4132 RTS51X_DEBUGP("MS: delay write\n");
4133 rts51x_ms_delay_write(chip
);
4134 ms_card
->counter
= 0;
4138 static int ms_power_off_card3v3(struct rts51x_chip
*chip
)
4142 rts51x_init_cmd(chip
);
4144 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_CLK_EN
, MS_CLK_EN
, 0);
4145 if (chip
->asic_code
)
4146 ms_pull_ctl_disable(chip
);
4148 rts51x_add_cmd(chip
, WRITE_REG_CMD
, FPGA_PULL_CTL
,
4149 FPGA_MS_PULL_CTL_BIT
| 0x20,
4150 FPGA_MS_PULL_CTL_BIT
);
4151 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_OE
, MS_OUTPUT_EN
, 0);
4152 if (!chip
->option
.FT2_fast_mode
) {
4153 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PWR_CTL
, POWER_MASK
,
4157 retval
= rts51x_send_cmd(chip
, MODE_C
, 100);
4158 if (retval
!= STATUS_SUCCESS
)
4159 TRACE_RET(chip
, retval
);
4161 return STATUS_SUCCESS
;
4164 int rts51x_release_ms_card(struct rts51x_chip
*chip
)
4166 struct ms_info
*ms_card
= &(chip
->ms_card
);
4169 RTS51X_DEBUGP("rts51x_release_ms_card\n");
4171 ms_card
->delay_write
.delay_write_flag
= 0;
4172 ms_card
->pro_under_formatting
= 0;
4174 chip
->card_ready
&= ~MS_CARD
;
4175 chip
->card_fail
&= ~MS_CARD
;
4176 chip
->card_wp
&= ~MS_CARD
;
4178 rts51x_ms_free_l2p_tbl(chip
);
4180 rts51x_write_register(chip
, SFSM_ED
, HW_CMD_STOP
, HW_CMD_STOP
);
4182 memset(ms_card
->raw_sys_info
, 0, 96);
4183 #ifdef SUPPORT_PCGL_1P18
4184 memset(ms_card
->raw_model_name
, 0, 48);
4187 retval
= ms_power_off_card3v3(chip
);
4188 if (retval
!= STATUS_SUCCESS
)
4189 TRACE_RET(chip
, retval
);
4191 return STATUS_SUCCESS
;