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, sys_info_size = 0x%x\n",
1037 sys_info_addr
, sys_info_size
);
1038 if (sys_info_size
!= 96) {
1040 TRACE_RET(chip
, STATUS_FAIL
);
1042 if (sys_info_addr
< 0x1A0) {
1044 TRACE_RET(chip
, STATUS_FAIL
);
1046 if ((sys_info_size
+ sys_info_addr
) > 0x8000) {
1048 TRACE_RET(chip
, STATUS_FAIL
);
1051 if (buf
[cur_addr_off
+ 8] == 0x13)
1052 ms_card
->ms_type
|= MS_XC
;
1054 #ifdef SUPPORT_PCGL_1P18
1060 #ifdef SUPPORT_PCGL_1P18
1061 if (buf
[cur_addr_off
+ 8] == 0x15) {
1062 model_name_addr
= ((u32
) buf
[cur_addr_off
+ 0] << 24) |
1063 ((u32
) buf
[cur_addr_off
+ 1] << 16) |
1064 ((u32
) buf
[cur_addr_off
+ 2] << 8) |
1065 buf
[cur_addr_off
+ 3];
1067 ((u32
) buf
[cur_addr_off
+ 4] << 24) |
1068 ((u32
) buf
[cur_addr_off
+ 5] << 16) |
1069 ((u32
) buf
[cur_addr_off
+ 6] << 8) |
1070 buf
[cur_addr_off
+ 7];
1071 RTS51X_DEBUGP("model_name_addr = 0x%x, model_name_size = 0x%x\n",
1072 model_name_addr
, model_name_size
);
1073 if (model_name_size
!= 48) {
1075 TRACE_RET(chip
, STATUS_FAIL
);
1077 if (model_name_addr
< 0x1A0) {
1079 TRACE_RET(chip
, STATUS_FAIL
);
1081 if ((model_name_size
+ model_name_addr
) > 0x8000) {
1083 TRACE_RET(chip
, STATUS_FAIL
);
1086 found_model_name
= 1;
1089 if (found_sys_info
&& found_model_name
)
1096 TRACE_RET(chip
, STATUS_FAIL
);
1099 class_code
= buf
[sys_info_addr
+ 0];
1100 device_type
= buf
[sys_info_addr
+ 56];
1101 sub_class
= buf
[sys_info_addr
+ 46];
1103 if (CHK_MSXC(ms_card
)) {
1104 xc_total_blk
= ((u32
) buf
[sys_info_addr
+ 6] << 24) |
1105 ((u32
) buf
[sys_info_addr
+ 7] << 16) |
1106 ((u32
) buf
[sys_info_addr
+ 8] << 8) |
1107 buf
[sys_info_addr
+ 9];
1108 xc_blk_size
= ((u32
) buf
[sys_info_addr
+ 32] << 24) |
1109 ((u32
) buf
[sys_info_addr
+ 33] << 16) |
1110 ((u32
) buf
[sys_info_addr
+ 34] << 8) |
1111 buf
[sys_info_addr
+ 35];
1112 RTS51X_DEBUGP("xc_total_blk = 0x%x, xc_blk_size = 0x%x\n",
1113 xc_total_blk
, xc_blk_size
);
1116 ((u16
) buf
[sys_info_addr
+ 6] << 8) | buf
[sys_info_addr
+
1119 ((u16
) buf
[sys_info_addr
+ 2] << 8) | buf
[sys_info_addr
+
1121 RTS51X_DEBUGP("total_blk = 0x%x, blk_size = 0x%x\n", total_blk
,
1126 ((u16
) buf
[sys_info_addr
+ 6] << 8) | buf
[sys_info_addr
+ 7];
1127 blk_size
= ((u16
) buf
[sys_info_addr
+ 2] << 8) | buf
[sys_info_addr
+ 3];
1128 RTS51X_DEBUGP("total_blk = 0x%x, blk_size = 0x%x\n", total_blk
,
1132 RTS51X_DEBUGP("class_code = 0x%x, device_type = 0x%x,"
1133 "sub_class = 0x%x\n",
1134 class_code
, device_type
, sub_class
);
1136 memcpy(ms_card
->raw_sys_info
, buf
+ sys_info_addr
, 96);
1137 #ifdef SUPPORT_PCGL_1P18
1138 memcpy(ms_card
->raw_model_name
, buf
+ model_name_addr
, 48);
1143 /* Confirm System Information */
1145 if (CHK_MSXC(ms_card
)) {
1146 if (class_code
!= 0x03)
1147 TRACE_RET(chip
, STATUS_FAIL
);
1149 if (class_code
!= 0x02)
1150 TRACE_RET(chip
, STATUS_FAIL
);
1153 if (class_code
!= 0x02)
1154 TRACE_RET(chip
, STATUS_FAIL
);
1157 if (device_type
!= 0x00) {
1158 if ((device_type
== 0x01) || (device_type
== 0x02)
1159 || (device_type
== 0x03))
1160 chip
->card_wp
|= MS_CARD
;
1162 TRACE_RET(chip
, STATUS_FAIL
);
1164 if (sub_class
& 0xC0)
1165 TRACE_RET(chip
, STATUS_FAIL
);
1167 RTS51X_DEBUGP("class_code: 0x%x, device_type: 0x%x, sub_class: 0x%x\n",
1168 class_code
, device_type
, sub_class
);
1171 if (CHK_MSXC(ms_card
)) {
1172 chip
->capacity
[chip
->card2lun
[MS_CARD
]] = ms_card
->capacity
=
1173 xc_total_blk
* xc_blk_size
;
1175 chip
->capacity
[chip
->card2lun
[MS_CARD
]] = ms_card
->capacity
=
1176 total_blk
* blk_size
;
1179 chip
->capacity
[chip
->card2lun
[MS_CARD
]] = ms_card
->capacity
=
1180 total_blk
* blk_size
;
1183 return STATUS_SUCCESS
;
1186 #ifdef SUPPORT_MAGIC_GATE
1187 int mg_set_tpc_para_sub(struct rts51x_chip
*chip
, int type
, u8 mg_entry_num
);
1190 static int reset_ms_pro(struct rts51x_chip
*chip
)
1192 struct ms_info
*ms_card
= &(chip
->ms_card
);
1194 #ifdef XC_POWERCLASS
1195 u8 change_power_class
= 2;
1198 #ifdef XC_POWERCLASS
1201 retval
= ms_pro_reset_flow(chip
, 1);
1202 if (retval
!= STATUS_SUCCESS
) {
1203 if (ms_card
->switch_8bit_fail
) {
1204 retval
= ms_pro_reset_flow(chip
, 0);
1205 if (retval
!= STATUS_SUCCESS
)
1206 TRACE_RET(chip
, retval
);
1208 TRACE_RET(chip
, retval
);
1212 retval
= ms_read_attribute_info(chip
);
1213 if (retval
!= STATUS_SUCCESS
)
1214 TRACE_RET(chip
, retval
);
1215 #ifdef XC_POWERCLASS
1216 if (CHK_HG8BIT(ms_card
))
1217 change_power_class
= 0;
1219 if (change_power_class
&& CHK_MSXC(ms_card
)) {
1220 u8 power_class_mode
= (ms_card
->raw_sys_info
[46] & 0x18) >> 3;
1221 RTS51X_DEBUGP("power_class_mode = 0x%x", power_class_mode
);
1222 if (change_power_class
> power_class_mode
)
1223 change_power_class
= power_class_mode
;
1224 if (change_power_class
) {
1225 retval
= msxc_change_power(chip
, change_power_class
);
1226 if (retval
!= STATUS_SUCCESS
) {
1227 change_power_class
--;
1234 #ifdef SUPPORT_MAGIC_GATE
1235 retval
= mg_set_tpc_para_sub(chip
, 0, 0);
1236 if (retval
!= STATUS_SUCCESS
)
1237 TRACE_RET(chip
, retval
);
1240 if (CHK_HG8BIT(ms_card
))
1241 chip
->card_bus_width
[chip
->card2lun
[MS_CARD
]] = 8;
1243 chip
->card_bus_width
[chip
->card2lun
[MS_CARD
]] = 4;
1245 return STATUS_SUCCESS
;
1248 static int ms_read_status_reg(struct rts51x_chip
*chip
)
1253 retval
= ms_set_rw_reg_addr(chip
, StatusReg0
, 2, 0, 0);
1254 if (retval
!= STATUS_SUCCESS
)
1255 TRACE_RET(chip
, retval
);
1257 retval
= ms_read_bytes(chip
, READ_REG
, 2, NO_WAIT_INT
, val
, 2);
1258 if (retval
!= STATUS_SUCCESS
)
1259 TRACE_RET(chip
, retval
);
1260 if (val
[1] & (STS_UCDT
| STS_UCEX
| STS_UCFG
)) {
1261 ms_set_err_code(chip
, MS_FLASH_READ_ERROR
);
1262 TRACE_RET(chip
, STATUS_FAIL
);
1265 return STATUS_SUCCESS
;
1268 static int ms_check_boot_block(struct rts51x_chip
*chip
, u16 block_addr
)
1270 struct ms_info
*ms_card
= &(chip
->ms_card
);
1272 u8 extra
[MS_EXTRA_SIZE
], data
[10], val
= 0;
1274 if (CHK_MS4BIT(ms_card
)) {
1275 /* Parallel interface */
1278 /* Serial interface */
1283 data
[2] = (u8
) (block_addr
>> 8);
1284 data
[3] = (u8
) block_addr
;
1286 * Extra data access mode */
1290 retval
= ms_set_cmd(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
, 6,
1291 BLOCK_READ
, WAIT_INT
, data
, 6, &val
);
1292 if (retval
!= STATUS_SUCCESS
)
1293 TRACE_RET(chip
, retval
);
1295 if (val
& INT_REG_CMDNK
) {
1296 ms_set_err_code(chip
, MS_CMD_NK
);
1297 TRACE_RET(chip
, STATUS_FAIL
);
1299 if (val
& INT_REG_CED
) {
1300 if (val
& INT_REG_ERR
) {
1301 retval
= ms_read_status_reg(chip
);
1302 if (retval
!= STATUS_SUCCESS
)
1303 TRACE_RET(chip
, retval
);
1305 ms_set_rw_reg_addr(chip
, OverwriteFlag
,
1306 MS_EXTRA_SIZE
, SystemParm
, 6);
1307 if (retval
!= STATUS_SUCCESS
)
1308 TRACE_RET(chip
, retval
);
1313 ms_read_bytes(chip
, READ_REG
, MS_EXTRA_SIZE
, NO_WAIT_INT
, extra
,
1315 if (retval
!= STATUS_SUCCESS
)
1316 TRACE_RET(chip
, retval
);
1318 if (!(extra
[0] & BLOCK_OK
) || (extra
[1] & NOT_BOOT_BLOCK
))
1319 TRACE_RET(chip
, STATUS_FAIL
);
1321 return STATUS_SUCCESS
;
1324 static int ms_read_extra_data(struct rts51x_chip
*chip
,
1325 u16 block_addr
, u8 page_num
, u8
*buf
,
1328 struct ms_info
*ms_card
= &(chip
->ms_card
);
1330 u8 val
= 0, data
[10];
1332 if (CHK_MS4BIT(ms_card
)) {
1333 /* Parallel interface */
1336 /* Serial interface */
1341 data
[2] = (u8
) (block_addr
>> 8);
1342 data
[3] = (u8
) block_addr
;
1344 * Extra data access mode */
1350 ms_auto_set_cmd(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
, 6,
1351 BLOCK_READ
, WAIT_INT
, data
, 6, &val
);
1353 retval
= ms_set_cmd(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
, 6,
1354 BLOCK_READ
, WAIT_INT
, data
, 6, &val
);
1356 if (retval
!= STATUS_SUCCESS
)
1357 TRACE_RET(chip
, retval
);
1359 if (val
& INT_REG_CMDNK
) {
1360 ms_set_err_code(chip
, MS_CMD_NK
);
1361 TRACE_RET(chip
, STATUS_FAIL
);
1363 if (val
& INT_REG_CED
) {
1364 if (val
& INT_REG_ERR
) {
1365 retval
= ms_read_status_reg(chip
);
1366 if (retval
!= STATUS_SUCCESS
)
1367 TRACE_RET(chip
, retval
);
1369 ms_set_rw_reg_addr(chip
, OverwriteFlag
,
1370 MS_EXTRA_SIZE
, SystemParm
, 6);
1371 if (retval
!= STATUS_SUCCESS
)
1372 TRACE_RET(chip
, retval
);
1377 ms_read_bytes(chip
, READ_REG
, MS_EXTRA_SIZE
, NO_WAIT_INT
, data
,
1379 if (retval
!= STATUS_SUCCESS
)
1380 TRACE_RET(chip
, retval
);
1382 if (buf
&& buf_len
) {
1383 if (buf_len
> MS_EXTRA_SIZE
)
1384 buf_len
= MS_EXTRA_SIZE
;
1385 memcpy(buf
, data
, buf_len
);
1388 return STATUS_SUCCESS
;
1391 static int ms_write_extra_data(struct rts51x_chip
*chip
,
1392 u16 block_addr
, u8 page_num
, u8
*buf
,
1395 struct ms_info
*ms_card
= &(chip
->ms_card
);
1397 u8 val
= 0, data
[16];
1399 if (!buf
|| (buf_len
< MS_EXTRA_SIZE
))
1400 TRACE_RET(chip
, STATUS_FAIL
);
1402 if (CHK_MS4BIT(ms_card
)) {
1403 /* Parallel interface */
1406 /* Serial interface */
1411 data
[2] = (u8
) (block_addr
>> 8);
1412 data
[3] = (u8
) block_addr
;
1414 * Extra data access mode */
1418 for (i
= 6; i
< MS_EXTRA_SIZE
+ 6; i
++)
1419 data
[i
] = buf
[i
- 6];
1423 ms_auto_set_cmd(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
,
1424 6 + MS_EXTRA_SIZE
, BLOCK_WRITE
, WAIT_INT
, data
, 16,
1428 ms_set_cmd(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
,
1429 6 + MS_EXTRA_SIZE
, BLOCK_WRITE
, WAIT_INT
, data
, 16,
1432 if (retval
!= STATUS_SUCCESS
)
1433 TRACE_RET(chip
, retval
);
1435 if (val
& INT_REG_CMDNK
) {
1436 ms_set_err_code(chip
, MS_CMD_NK
);
1437 TRACE_RET(chip
, STATUS_FAIL
);
1439 if (val
& INT_REG_CED
) {
1440 if (val
& INT_REG_ERR
) {
1441 ms_set_err_code(chip
, MS_FLASH_WRITE_ERROR
);
1442 TRACE_RET(chip
, STATUS_FAIL
);
1446 return STATUS_SUCCESS
;
1449 static int ms_read_page(struct rts51x_chip
*chip
, u16 block_addr
, u8 page_num
)
1451 struct ms_info
*ms_card
= &(chip
->ms_card
);
1453 u8 val
= 0, data
[6];
1455 if (CHK_MS4BIT(ms_card
)) {
1456 /* Parallel interface */
1459 /* Serial interface */
1464 data
[2] = (u8
) (block_addr
>> 8);
1465 data
[3] = (u8
) block_addr
;
1467 * Single page access mode */
1471 retval
= ms_set_cmd(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
, 6,
1472 BLOCK_READ
, WAIT_INT
, data
, 6, &val
);
1473 if (retval
!= STATUS_SUCCESS
)
1474 TRACE_RET(chip
, retval
);
1476 if (val
& INT_REG_CMDNK
) {
1477 ms_set_err_code(chip
, MS_CMD_NK
);
1478 TRACE_RET(chip
, STATUS_FAIL
);
1481 if (val
& INT_REG_CED
) {
1482 if (val
& INT_REG_ERR
) {
1483 if (!(val
& INT_REG_BREQ
)) {
1484 ms_set_err_code(chip
, MS_FLASH_READ_ERROR
);
1485 TRACE_RET(chip
, STATUS_FAIL
);
1487 retval
= ms_read_status_reg(chip
);
1488 if (retval
!= STATUS_SUCCESS
)
1489 ms_set_err_code(chip
, MS_FLASH_WRITE_ERROR
);
1491 if (!(val
& INT_REG_BREQ
)) {
1492 ms_set_err_code(chip
, MS_BREQ_ERROR
);
1493 TRACE_RET(chip
, STATUS_FAIL
);
1499 ms_transfer_tpc(chip
, MS_TM_NORMAL_READ
, READ_PAGE_DATA
, 0,
1501 if (retval
!= STATUS_SUCCESS
)
1502 TRACE_RET(chip
, retval
);
1503 if (ms_check_err_code(chip
, MS_FLASH_WRITE_ERROR
))
1504 TRACE_RET(chip
, STATUS_FAIL
);
1506 return STATUS_SUCCESS
;
1509 static int ms_set_bad_block(struct rts51x_chip
*chip
, u16 phy_blk
)
1511 struct ms_info
*ms_card
= &(chip
->ms_card
);
1513 u8 val
= 0, data
[8], extra
[MS_EXTRA_SIZE
];
1515 retval
= ms_read_extra_data(chip
, phy_blk
, 0, extra
, MS_EXTRA_SIZE
);
1516 if (retval
!= STATUS_SUCCESS
)
1517 TRACE_RET(chip
, retval
);
1519 ms_set_err_code(chip
, MS_NO_ERROR
);
1521 if (CHK_MS4BIT(ms_card
)) {
1522 /* Parallel interface */
1525 /* Serial interface */
1530 data
[2] = (u8
) (phy_blk
>> 8);
1531 data
[3] = (u8
) phy_blk
;
1534 data
[6] = extra
[0] & 0x7F;
1538 retval
= ms_set_cmd(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
, 7,
1539 BLOCK_WRITE
, WAIT_INT
, data
, 7, &val
);
1541 retval
= ms_set_cmd(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
, 7,
1542 BLOCK_WRITE
, WAIT_INT
, data
, 7, &val
);
1544 if (retval
!= STATUS_SUCCESS
)
1545 TRACE_RET(chip
, retval
);
1547 if (val
& INT_REG_CMDNK
) {
1548 ms_set_err_code(chip
, MS_CMD_NK
);
1549 TRACE_RET(chip
, STATUS_FAIL
);
1552 if (val
& INT_REG_CED
) {
1553 if (val
& INT_REG_ERR
) {
1554 ms_set_err_code(chip
, MS_FLASH_WRITE_ERROR
);
1555 TRACE_RET(chip
, STATUS_FAIL
);
1559 return STATUS_SUCCESS
;
1562 static int ms_erase_block(struct rts51x_chip
*chip
, u16 phy_blk
)
1564 struct ms_info
*ms_card
= &(chip
->ms_card
);
1566 u8 val
= 0, data
[6];
1569 ms_set_rw_reg_addr(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
,
1571 if (retval
!= STATUS_SUCCESS
)
1572 TRACE_RET(chip
, retval
);
1574 ms_set_err_code(chip
, MS_NO_ERROR
);
1576 if (CHK_MS4BIT(ms_card
)) {
1577 /* Parallel interface */
1580 /* Serial interface */
1585 data
[2] = (u8
) (phy_blk
>> 8);
1586 data
[3] = (u8
) phy_blk
;
1593 ms_auto_set_cmd(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
, 6,
1594 BLOCK_ERASE
, WAIT_INT
, data
, 6, &val
);
1596 retval
= ms_set_cmd(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
, 6,
1597 BLOCK_ERASE
, WAIT_INT
, data
, 6, &val
);
1600 if (val
& INT_REG_CMDNK
) {
1605 ms_set_err_code(chip
, MS_CMD_NK
);
1606 ms_set_bad_block(chip
, phy_blk
);
1607 TRACE_RET(chip
, STATUS_FAIL
);
1610 if (val
& INT_REG_CED
) {
1611 if (val
& INT_REG_ERR
) {
1612 ms_set_err_code(chip
, MS_FLASH_WRITE_ERROR
);
1613 TRACE_RET(chip
, STATUS_FAIL
);
1617 return STATUS_SUCCESS
;
1620 static void ms_set_page_status(u16 log_blk
, u8 type
, u8
*extra
, int extra_len
)
1622 if (!extra
|| (extra_len
< MS_EXTRA_SIZE
))
1625 memset(extra
, 0xFF, MS_EXTRA_SIZE
);
1627 if (type
== setPS_NG
)
1632 extra
[2] = (u8
) (log_blk
>> 8);
1633 extra
[3] = (u8
) log_blk
;
1636 static int ms_init_page(struct rts51x_chip
*chip
, u16 phy_blk
, u16 log_blk
,
1637 u8 start_page
, u8 end_page
)
1640 u8 extra
[MS_EXTRA_SIZE
], i
;
1642 memset(extra
, 0xff, MS_EXTRA_SIZE
);
1644 extra
[0] = 0xf8; /* Block, page OK, data erased */
1646 extra
[2] = (u8
) (log_blk
>> 8);
1647 extra
[3] = (u8
) log_blk
;
1649 for (i
= start_page
; i
< end_page
; i
++) {
1650 if (monitor_card_cd(chip
, MS_CARD
) == CD_NOT_EXIST
)
1651 TRACE_RET(chip
, STATUS_FAIL
);
1654 ms_write_extra_data(chip
, phy_blk
, i
, extra
, MS_EXTRA_SIZE
);
1655 if (retval
!= STATUS_SUCCESS
)
1656 TRACE_RET(chip
, retval
);
1659 return STATUS_SUCCESS
;
1662 static int ms_copy_page(struct rts51x_chip
*chip
, u16 old_blk
, u16 new_blk
,
1663 u16 log_blk
, u8 start_page
, u8 end_page
)
1665 struct ms_info
*ms_card
= &(chip
->ms_card
);
1666 int retval
, rty_cnt
, uncorrect_flag
= 0;
1667 u8 extra
[MS_EXTRA_SIZE
], val
, i
, j
, data
[16];
1669 RTS51X_DEBUGP("Copy page from 0x%x to 0x%x, logical block is 0x%x\n",
1670 old_blk
, new_blk
, log_blk
);
1671 RTS51X_DEBUGP("start_page = %d, end_page = %d\n", start_page
,
1674 retval
= ms_read_extra_data(chip
, new_blk
, 0, extra
, MS_EXTRA_SIZE
);
1675 if (retval
!= STATUS_SUCCESS
)
1676 TRACE_RET(chip
, retval
);
1678 retval
= ms_read_status_reg(chip
);
1679 if (retval
!= STATUS_SUCCESS
)
1680 TRACE_RET(chip
, retval
);
1682 RTS51X_READ_REG(chip
, PPBUF_BASE2
, &val
);
1684 if (val
& BUF_FULL
) {
1686 retval
= ms_send_cmd(chip
, CLEAR_BUF
, WAIT_INT
);
1687 if (retval
!= STATUS_SUCCESS
)
1688 TRACE_RET(chip
, retval
);
1689 /* GET_INT Register */
1690 retval
= ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
, 1);
1691 if (retval
!= STATUS_SUCCESS
)
1692 TRACE_RET(chip
, retval
);
1694 if (!(val
& INT_REG_CED
)) {
1695 ms_set_err_code(chip
, MS_FLASH_WRITE_ERROR
);
1696 TRACE_RET(chip
, STATUS_FAIL
);
1700 for (i
= start_page
; i
< end_page
; i
++) {
1701 if (monitor_card_cd(chip
, MS_CARD
) == CD_NOT_EXIST
)
1702 TRACE_RET(chip
, STATUS_FAIL
);
1704 ms_read_extra_data(chip
, old_blk
, i
, extra
, MS_EXTRA_SIZE
);
1707 ms_set_rw_reg_addr(chip
, OverwriteFlag
, MS_EXTRA_SIZE
,
1709 if (retval
!= STATUS_SUCCESS
)
1710 TRACE_RET(chip
, retval
);
1712 ms_set_err_code(chip
, MS_NO_ERROR
);
1714 if (CHK_MS4BIT(ms_card
)) {
1715 /* Parallel interface */
1718 /* Serial interface */
1723 data
[2] = (u8
) (old_blk
>> 8);
1724 data
[3] = (u8
) old_blk
;
1729 ms_write_bytes(chip
, WRITE_REG
, 6, NO_WAIT_INT
, data
, 6);
1730 if (retval
!= STATUS_SUCCESS
)
1731 TRACE_RET(chip
, retval
);
1733 retval
= ms_send_cmd(chip
, BLOCK_READ
, WAIT_INT
);
1734 if (retval
!= STATUS_SUCCESS
)
1735 TRACE_RET(chip
, retval
);
1737 ms_set_err_code(chip
, MS_NO_ERROR
);
1738 retval
= ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
, 1);
1739 if (retval
!= STATUS_SUCCESS
)
1740 TRACE_RET(chip
, retval
);
1742 if (val
& INT_REG_CMDNK
) {
1743 ms_set_err_code(chip
, MS_CMD_NK
);
1744 TRACE_RET(chip
, STATUS_FAIL
);
1747 if (val
& INT_REG_CED
) {
1748 if (val
& INT_REG_ERR
) {
1749 retval
= ms_read_status_reg(chip
);
1750 if (retval
!= STATUS_SUCCESS
) {
1752 RTS51X_DEBUGP("Uncorrectable error\n");
1758 ms_transfer_tpc(chip
, MS_TM_NORMAL_READ
,
1759 READ_PAGE_DATA
, 0, NO_WAIT_INT
);
1760 if (retval
!= STATUS_SUCCESS
)
1761 TRACE_RET(chip
, retval
);
1762 if (uncorrect_flag
) {
1763 ms_set_page_status(log_blk
, setPS_NG
,
1764 extra
, MS_EXTRA_SIZE
);
1767 ms_write_extra_data(chip
, old_blk
, i
,
1770 RTS51X_DEBUGP("page %d : extra[0] = 0x%x\n",
1772 MS_SET_BAD_BLOCK_FLG(ms_card
);
1774 ms_set_page_status(log_blk
, setPS_Error
,
1775 extra
, MS_EXTRA_SIZE
);
1776 ms_write_extra_data(chip
, new_blk
, i
,
1777 extra
, MS_EXTRA_SIZE
);
1781 for (rty_cnt
= 0; rty_cnt
< MS_MAX_RETRY_COUNT
;
1784 ms_transfer_tpc(chip
,
1788 if (retval
== STATUS_SUCCESS
)
1791 if (rty_cnt
== MS_MAX_RETRY_COUNT
)
1792 TRACE_RET(chip
, STATUS_FAIL
);
1795 if (!(val
& INT_REG_BREQ
)) {
1796 ms_set_err_code(chip
, MS_BREQ_ERROR
);
1797 TRACE_RET(chip
, STATUS_FAIL
);
1801 retval
= ms_set_rw_reg_addr(chip
, OverwriteFlag
,
1802 MS_EXTRA_SIZE
, SystemParm
,
1803 (6 + MS_EXTRA_SIZE
));
1806 ms_set_err_code(chip
, MS_NO_ERROR
);
1808 if (CHK_MS4BIT(ms_card
)) {
1809 /* Parallel interface */
1812 /* Serial interface */
1817 data
[2] = (u8
) (new_blk
>> 8);
1818 data
[3] = (u8
) new_blk
;
1822 /* for MS check procedure */
1823 if ((extra
[0] & 0x60) != 0x60)
1829 data
[6 + 2] = (u8
) (log_blk
>> 8);
1830 data
[6 + 3] = (u8
) log_blk
;
1832 for (j
= 4; j
<= MS_EXTRA_SIZE
; j
++)
1836 ms_write_bytes(chip
, WRITE_REG
, (6 + MS_EXTRA_SIZE
),
1837 NO_WAIT_INT
, data
, 16);
1838 if (retval
!= STATUS_SUCCESS
)
1839 TRACE_RET(chip
, retval
);
1841 retval
= ms_send_cmd(chip
, BLOCK_WRITE
, WAIT_INT
);
1842 if (retval
!= STATUS_SUCCESS
)
1843 TRACE_RET(chip
, retval
);
1844 /* GET_INT Register */
1845 ms_set_err_code(chip
, MS_NO_ERROR
);
1846 retval
= ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
, 1);
1847 if (retval
!= STATUS_SUCCESS
)
1848 TRACE_RET(chip
, retval
);
1850 if (val
& INT_REG_CMDNK
) {
1851 ms_set_err_code(chip
, MS_CMD_NK
);
1852 TRACE_RET(chip
, STATUS_FAIL
);
1855 if (val
& INT_REG_CED
) {
1856 if (val
& INT_REG_ERR
) {
1857 ms_set_err_code(chip
, MS_FLASH_WRITE_ERROR
);
1858 TRACE_RET(chip
, STATUS_FAIL
);
1864 ms_set_rw_reg_addr(chip
, OverwriteFlag
,
1865 MS_EXTRA_SIZE
, SystemParm
, 7);
1866 if (retval
!= STATUS_SUCCESS
)
1867 TRACE_RET(chip
, retval
);
1869 ms_set_err_code(chip
, MS_NO_ERROR
);
1871 if (CHK_MS4BIT(ms_card
)) {
1872 /* Parallel interface */
1875 /* Serial interface */
1880 data
[2] = (u8
) (old_blk
>> 8);
1881 data
[3] = (u8
) old_blk
;
1888 ms_write_bytes(chip
, WRITE_REG
, 7, NO_WAIT_INT
,
1890 if (retval
!= STATUS_SUCCESS
)
1891 TRACE_RET(chip
, retval
);
1893 retval
= ms_send_cmd(chip
, BLOCK_WRITE
, WAIT_INT
);
1894 if (retval
!= STATUS_SUCCESS
)
1895 TRACE_RET(chip
, retval
);
1897 ms_set_err_code(chip
, MS_NO_ERROR
);
1899 ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
,
1901 if (retval
!= STATUS_SUCCESS
)
1902 TRACE_RET(chip
, retval
);
1904 if (val
& INT_REG_CMDNK
) {
1905 ms_set_err_code(chip
, MS_CMD_NK
);
1906 TRACE_RET(chip
, STATUS_FAIL
);
1909 if (val
& INT_REG_CED
) {
1910 if (val
& INT_REG_ERR
) {
1911 ms_set_err_code(chip
,
1912 MS_FLASH_WRITE_ERROR
);
1913 TRACE_RET(chip
, STATUS_FAIL
);
1919 return STATUS_SUCCESS
;
1923 static int ms_auto_copy_page(struct rts51x_chip
*chip
, u16 old_blk
, u16 new_blk
,
1924 u16 log_blk
, u8 start_page
, u8 end_page
)
1926 struct ms_info
*ms_card
= &(chip
->ms_card
);
1928 u8 page_len
, bus_width
, val
= 0;
1929 u8 extra
[MS_EXTRA_SIZE
];
1931 RTS51X_DEBUGP("Auto copy page from 0x%x to 0x%x, logical block is 0x%x\n",
1932 old_blk
, new_blk
, log_blk
);
1933 RTS51X_DEBUGP("start_page = %d, end_page = %d\n", start_page
,
1936 page_len
= end_page
- start_page
;
1938 retval
= ms_read_extra_data(chip
, new_blk
, 0, extra
, MS_EXTRA_SIZE
);
1939 if (retval
!= STATUS_SUCCESS
)
1940 TRACE_RET(chip
, retval
);
1942 retval
= ms_read_status_reg(chip
);
1943 if (retval
!= STATUS_SUCCESS
)
1944 TRACE_RET(chip
, retval
);
1946 RTS51X_READ_REG(chip
, PPBUF_BASE2
, &val
);
1948 if (val
& BUF_FULL
) {
1949 retval
= ms_send_cmd(chip
, CLEAR_BUF
, WAIT_INT
);
1950 if (retval
!= STATUS_SUCCESS
)
1951 TRACE_RET(chip
, retval
);
1953 retval
= ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
, 1);
1954 if (retval
!= STATUS_SUCCESS
)
1955 TRACE_RET(chip
, retval
);
1957 if (!(val
& INT_REG_CED
)) {
1958 ms_set_err_code(chip
, MS_FLASH_WRITE_ERROR
);
1959 TRACE_RET(chip
, STATUS_FAIL
);
1963 if (CHK_MS4BIT(ms_card
)) {
1964 /* Parallel interface */
1967 /* Serial interface */
1971 rts51x_init_cmd(chip
);
1973 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_OLD_BLOCK_0
, 0xFF, (u8
) old_blk
);
1974 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_OLD_BLOCK_1
, 0xFF,
1975 (u8
) (old_blk
>> 8));
1976 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_NEW_BLOCK_0
, 0xFF, (u8
) new_blk
);
1977 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_NEW_BLOCK_1
, 0xFF,
1978 (u8
) (new_blk
>> 8));
1979 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_LOG_BLOCK_0
, 0xFF, (u8
) log_blk
);
1980 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_LOG_BLOCK_1
, 0xFF,
1981 (u8
) (log_blk
>> 8));
1982 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_PAGE_START
, 0xFF, start_page
);
1983 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_PAGE_LENGTH
, 0xFF, page_len
);
1984 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_BUS_WIDTH
, 0xFF, bus_width
);
1986 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANSFER
, 0xFF,
1987 MS_TRANSFER_START
| MS_TM_COPY_PAGE
);
1988 rts51x_add_cmd(chip
, CHECK_REG_CMD
, MS_TRANSFER
, MS_TRANSFER_END
,
1991 retval
= rts51x_send_cmd(chip
, MODE_CR
, 100);
1992 if (retval
!= STATUS_SUCCESS
) {
1993 rts51x_clear_ms_error(chip
);
1994 TRACE_RET(chip
, retval
);
1997 retval
= rts51x_get_rsp(chip
, 1, 5000);
1999 if (CHECK_MS_TRANS_FAIL(chip
, retval
)) {
2000 rts51x_clear_ms_error(chip
);
2001 if (retval
== STATUS_TIMEDOUT
)
2002 TRACE_RET(chip
, retval
);
2003 TRACE_GOTO(chip
, Fail
);
2006 return STATUS_SUCCESS
;
2009 retval
= ms_erase_block(chip
, new_blk
);
2010 if (retval
!= STATUS_SUCCESS
)
2011 TRACE_RET(chip
, retval
);
2014 ms_copy_page(chip
, old_blk
, new_blk
, log_blk
, start_page
, end_page
);
2015 if (retval
!= STATUS_SUCCESS
)
2016 TRACE_RET(chip
, retval
);
2018 return STATUS_SUCCESS
;
2022 static int reset_ms(struct rts51x_chip
*chip
)
2024 struct ms_info
*ms_card
= &(chip
->ms_card
);
2026 u16 i
, reg_addr
, block_size
;
2028 #ifndef SUPPORT_MAGIC_GATE
2032 retval
= ms_prepare_reset(chip
);
2033 if (retval
!= STATUS_SUCCESS
)
2034 TRACE_RET(chip
, retval
);
2036 ms_card
->ms_type
|= TYPE_MS
;
2038 retval
= ms_send_cmd(chip
, MS_RESET
, NO_WAIT_INT
);
2039 if (retval
!= STATUS_SUCCESS
)
2040 TRACE_RET(chip
, retval
);
2042 retval
= ms_read_status_reg(chip
);
2043 if (retval
!= STATUS_SUCCESS
)
2044 TRACE_RET(chip
, retval
);
2046 RTS51X_READ_REG(chip
, PPBUF_BASE2
, &val
);
2047 if (val
& WRT_PRTCT
)
2048 chip
->card_wp
|= MS_CARD
;
2050 chip
->card_wp
&= ~MS_CARD
;
2055 /* Search For Boot Block */
2056 while (i
< (MAX_DEFECTIVE_BLOCK
+ 2)) {
2057 if (monitor_card_cd(chip
, MS_CARD
) == CD_NOT_EXIST
)
2058 TRACE_RET(chip
, STATUS_FAIL
);
2060 retval
= ms_check_boot_block(chip
, i
);
2061 if (retval
!= STATUS_SUCCESS
) {
2066 ms_card
->boot_block
= i
;
2070 if (i
== (MAX_DEFECTIVE_BLOCK
+ 2)) {
2071 RTS51X_DEBUGP("No boot block found!");
2072 TRACE_RET(chip
, STATUS_FAIL
);
2074 for (j
= 0; j
< 3; j
++) {
2075 retval
= ms_read_page(chip
, ms_card
->boot_block
, j
);
2076 if (retval
!= STATUS_SUCCESS
) {
2077 if (ms_check_err_code(chip
, MS_FLASH_WRITE_ERROR
)) {
2078 i
= ms_card
->boot_block
+ 1;
2079 ms_set_err_code(chip
, MS_NO_ERROR
);
2085 /* Read boot block contents */
2086 retval
= ms_read_page(chip
, ms_card
->boot_block
, 0);
2087 if (retval
!= STATUS_SUCCESS
)
2088 TRACE_RET(chip
, retval
);
2089 /* Read MS system information as sys_info */
2091 rts51x_seq_read_register(chip
, PPBUF_BASE2
+ 0x1A0, 96,
2092 ms_card
->raw_sys_info
);
2093 if (retval
!= STATUS_SUCCESS
)
2094 TRACE_RET(chip
, retval
);
2095 /* Read useful block contents */
2096 rts51x_init_cmd(chip
);
2098 rts51x_add_cmd(chip
, READ_REG_CMD
, HEADER_ID0
, 0, 0);
2099 rts51x_add_cmd(chip
, READ_REG_CMD
, HEADER_ID1
, 0, 0);
2101 for (reg_addr
= DISABLED_BLOCK0
; reg_addr
<= DISABLED_BLOCK3
;
2103 rts51x_add_cmd(chip
, READ_REG_CMD
, reg_addr
, 0, 0);
2106 for (reg_addr
= BLOCK_SIZE_0
; reg_addr
<= PAGE_SIZE_1
; reg_addr
++)
2107 rts51x_add_cmd(chip
, READ_REG_CMD
, reg_addr
, 0, 0);
2109 rts51x_add_cmd(chip
, READ_REG_CMD
, MS_Device_Type
, 0, 0);
2110 rts51x_add_cmd(chip
, READ_REG_CMD
, MS_4bit_Support
, 0, 0);
2112 retval
= rts51x_send_cmd(chip
, MODE_CR
, 100);
2113 if (retval
!= STATUS_SUCCESS
)
2114 TRACE_RET(chip
, retval
);
2116 retval
= rts51x_get_rsp(chip
, 16, 100);
2118 if (retval
!= STATUS_SUCCESS
)
2119 TRACE_RET(chip
, retval
);
2121 ptr
= rts51x_get_rsp_data(chip
);
2123 RTS51X_DEBUGP("Boot block data:\n");
2124 RTS51X_DUMP(ptr
, 16);
2126 if (ptr
[0] != 0x00 || ptr
[1] != 0x01) {
2127 i
= ms_card
->boot_block
+ 1;
2130 if (ptr
[12] != 0x02 || ptr
[13] != 0x00) {
2131 i
= ms_card
->boot_block
+ 1;
2134 if ((ptr
[14] == 1) || (ptr
[14] == 3))
2135 chip
->card_wp
|= MS_CARD
;
2136 block_size
= ((u16
) ptr
[6] << 8) | ptr
[7];
2137 if (block_size
== 0x0010) {
2138 ms_card
->block_shift
= 5;
2139 ms_card
->page_off
= 0x1F;
2140 } else if (block_size
== 0x0008) {
2141 ms_card
->block_shift
= 4;
2142 ms_card
->page_off
= 0x0F;
2144 ms_card
->total_block
= ((u16
) ptr
[8] << 8) | ptr
[9];
2146 #ifdef SUPPORT_MAGIC_GATE
2149 if (ms_card
->block_shift
== 4) {
2151 ms_card
->capacity
= 0x1EE0;
2153 ms_card
->capacity
= 0x3DE0;
2156 ms_card
->capacity
= 0x7BC0;
2158 ms_card
->capacity
= 0xF7C0;
2160 ms_card
->capacity
= 0x1EF80;
2162 ms_card
->capacity
= 0x3DF00;
2165 eblock_cnt
= ((u16
) ptr
[10] << 8) | ptr
[11];
2167 ms_card
->capacity
= ((u32
) eblock_cnt
- 2) << ms_card
->block_shift
;
2170 chip
->capacity
[chip
->card2lun
[MS_CARD
]] = ms_card
->capacity
;
2173 retval
= ms_set_rw_reg_addr(chip
, 0, 0, SystemParm
, 1);
2174 if (retval
!= STATUS_SUCCESS
)
2175 TRACE_RET(chip
, STATUS_FAIL
);
2176 RTS51X_WRITE_REG(chip
, PPBUF_BASE2
, 0xFF, 0x88);
2177 RTS51X_WRITE_REG(chip
, PPBUF_BASE2
+ 1, 0xFF, 0);
2180 ms_transfer_tpc(chip
, MS_TM_WRITE_BYTES
, WRITE_REG
, 1,
2182 if (retval
!= STATUS_SUCCESS
)
2183 TRACE_RET(chip
, STATUS_FAIL
);
2184 RTS51X_WRITE_REG(chip
, MS_CFG
, 0x58 | MS_NO_CHECK_INT
,
2185 MS_BUS_WIDTH_4
| PUSH_TIME_ODD
|
2188 ms_card
->ms_type
|= MS_4BIT
;
2191 if (CHK_MS4BIT(ms_card
))
2192 chip
->card_bus_width
[chip
->card2lun
[MS_CARD
]] = 4;
2194 chip
->card_bus_width
[chip
->card2lun
[MS_CARD
]] = 1;
2196 return STATUS_SUCCESS
;
2199 static int ms_init_l2p_tbl(struct rts51x_chip
*chip
)
2201 struct ms_info
*ms_card
= &(chip
->ms_card
);
2202 int size
, i
, seg_no
, retval
;
2203 u16 defect_block
, reg_addr
;
2206 ms_card
->segment_cnt
= ms_card
->total_block
>> 9;
2207 RTS51X_DEBUGP("ms_card->segment_cnt = %d\n", ms_card
->segment_cnt
);
2209 size
= ms_card
->segment_cnt
* sizeof(struct zone_entry
);
2210 ms_card
->segment
= vmalloc(size
);
2211 if (ms_card
->segment
== NULL
)
2212 TRACE_RET(chip
, STATUS_FAIL
);
2213 memset(ms_card
->segment
, 0, size
);
2215 retval
= ms_read_page(chip
, ms_card
->boot_block
, 1);
2216 if (retval
!= STATUS_SUCCESS
)
2217 TRACE_GOTO(chip
, INIT_FAIL
);
2219 reg_addr
= PPBUF_BASE2
;
2220 for (i
= 0; i
< (((ms_card
->total_block
>> 9) * 10) + 1); i
++) {
2221 retval
= rts51x_read_register(chip
, reg_addr
++, &val1
);
2222 if (retval
!= STATUS_SUCCESS
)
2223 TRACE_GOTO(chip
, INIT_FAIL
);
2224 retval
= rts51x_read_register(chip
, reg_addr
++, &val2
);
2225 if (retval
!= STATUS_SUCCESS
)
2226 TRACE_GOTO(chip
, INIT_FAIL
);
2228 defect_block
= ((u16
) val1
<< 8) | val2
;
2229 if (defect_block
== 0xFFFF)
2231 seg_no
= defect_block
/ 512;
2232 ms_card
->segment
[seg_no
].defect_list
[ms_card
->segment
[seg_no
].
2237 for (i
= 0; i
< ms_card
->segment_cnt
; i
++) {
2238 ms_card
->segment
[i
].build_flag
= 0;
2239 ms_card
->segment
[i
].l2p_table
= NULL
;
2240 ms_card
->segment
[i
].free_table
= NULL
;
2241 ms_card
->segment
[i
].get_index
= 0;
2242 ms_card
->segment
[i
].set_index
= 0;
2243 ms_card
->segment
[i
].unused_blk_cnt
= 0;
2245 RTS51X_DEBUGP("defective block count of segment %d is %d\n",
2246 i
, ms_card
->segment
[i
].disable_count
);
2249 return STATUS_SUCCESS
;
2252 if (ms_card
->segment
) {
2253 vfree(ms_card
->segment
);
2254 ms_card
->segment
= NULL
;
2260 static u16
ms_get_l2p_tbl(struct rts51x_chip
*chip
, int seg_no
, u16 log_off
)
2262 struct ms_info
*ms_card
= &(chip
->ms_card
);
2263 struct zone_entry
*segment
;
2265 if (ms_card
->segment
== NULL
)
2268 segment
= &(ms_card
->segment
[seg_no
]);
2270 if (segment
->l2p_table
)
2271 return segment
->l2p_table
[log_off
];
2276 static void ms_set_l2p_tbl(struct rts51x_chip
*chip
, int seg_no
, u16 log_off
,
2279 struct ms_info
*ms_card
= &(chip
->ms_card
);
2280 struct zone_entry
*segment
;
2282 if (ms_card
->segment
== NULL
)
2285 segment
= &(ms_card
->segment
[seg_no
]);
2286 if (segment
->l2p_table
)
2287 segment
->l2p_table
[log_off
] = phy_blk
;
2290 static void ms_set_unused_block(struct rts51x_chip
*chip
, u16 phy_blk
)
2292 struct ms_info
*ms_card
= &(chip
->ms_card
);
2293 struct zone_entry
*segment
;
2296 seg_no
= (int)phy_blk
>> 9;
2297 segment
= &(ms_card
->segment
[seg_no
]);
2299 segment
->free_table
[segment
->set_index
++] = phy_blk
;
2300 if (segment
->set_index
>= MS_FREE_TABLE_CNT
)
2301 segment
->set_index
= 0;
2302 segment
->unused_blk_cnt
++;
2305 static u16
ms_get_unused_block(struct rts51x_chip
*chip
, int seg_no
)
2307 struct ms_info
*ms_card
= &(chip
->ms_card
);
2308 struct zone_entry
*segment
;
2311 segment
= &(ms_card
->segment
[seg_no
]);
2313 if (segment
->unused_blk_cnt
<= 0)
2316 phy_blk
= segment
->free_table
[segment
->get_index
];
2317 segment
->free_table
[segment
->get_index
++] = 0xFFFF;
2318 if (segment
->get_index
>= MS_FREE_TABLE_CNT
)
2319 segment
->get_index
= 0;
2320 segment
->unused_blk_cnt
--;
2325 static const unsigned short ms_start_idx
[] = {
2326 0, 494, 990, 1486, 1982, 2478, 2974, 3470,
2327 3966, 4462, 4958, 5454, 5950, 6446, 6942, 7438, 7934
2330 static int ms_arbitrate_l2p(struct rts51x_chip
*chip
, u16 phy_blk
, u16 log_off
,
2333 struct ms_info
*ms_card
= &(chip
->ms_card
);
2334 struct zone_entry
*segment
;
2338 seg_no
= (int)phy_blk
>> 9;
2339 segment
= &(ms_card
->segment
[seg_no
]);
2340 tmp_blk
= segment
->l2p_table
[log_off
];
2344 if (!(chip
->card_wp
& MS_CARD
))
2345 ms_erase_block(chip
, tmp_blk
);
2346 ms_set_unused_block(chip
, tmp_blk
);
2347 segment
->l2p_table
[log_off
] = phy_blk
;
2349 if (!(chip
->card_wp
& MS_CARD
))
2350 ms_erase_block(chip
, phy_blk
);
2351 ms_set_unused_block(chip
, phy_blk
);
2354 if (phy_blk
< tmp_blk
) {
2355 if (!(chip
->card_wp
& MS_CARD
))
2356 ms_erase_block(chip
, phy_blk
);
2357 ms_set_unused_block(chip
, phy_blk
);
2359 if (!(chip
->card_wp
& MS_CARD
))
2360 ms_erase_block(chip
, tmp_blk
);
2361 ms_set_unused_block(chip
, tmp_blk
);
2362 segment
->l2p_table
[log_off
] = phy_blk
;
2366 return STATUS_SUCCESS
;
2369 static int ms_build_l2p_tbl(struct rts51x_chip
*chip
, int seg_no
)
2371 struct ms_info
*ms_card
= &(chip
->ms_card
);
2372 struct zone_entry
*segment
;
2373 int retval
, table_size
, disable_cnt
, defect_flag
, i
;
2374 u16 start
, end
, phy_blk
, log_blk
, tmp_blk
;
2375 u8 extra
[MS_EXTRA_SIZE
], us1
, us2
;
2377 RTS51X_DEBUGP("ms_build_l2p_tbl: %d\n", seg_no
);
2379 if (ms_card
->segment
== NULL
) {
2380 retval
= ms_init_l2p_tbl(chip
);
2381 if (retval
!= STATUS_SUCCESS
)
2382 TRACE_RET(chip
, retval
);
2385 if (ms_card
->segment
[seg_no
].build_flag
) {
2386 RTS51X_DEBUGP("l2p table of segment %d has been built\n",
2388 return STATUS_SUCCESS
;
2396 segment
= &(ms_card
->segment
[seg_no
]);
2398 if (segment
->l2p_table
== NULL
) {
2399 segment
->l2p_table
= vmalloc(table_size
* 2);
2400 if (segment
->l2p_table
== NULL
)
2401 TRACE_GOTO(chip
, BUILD_FAIL
);
2403 memset((u8
*) (segment
->l2p_table
), 0xff, table_size
* 2);
2405 if (segment
->free_table
== NULL
) {
2406 segment
->free_table
= vmalloc(MS_FREE_TABLE_CNT
* 2);
2407 if (segment
->free_table
== NULL
)
2408 TRACE_GOTO(chip
, BUILD_FAIL
);
2410 memset((u8
*) (segment
->free_table
), 0xff, MS_FREE_TABLE_CNT
* 2);
2412 start
= (u16
) seg_no
<< 9;
2413 end
= (u16
) (seg_no
+ 1) << 9;
2415 disable_cnt
= segment
->disable_count
;
2417 segment
->get_index
= segment
->set_index
= 0;
2418 segment
->unused_blk_cnt
= 0;
2420 for (phy_blk
= start
; phy_blk
< end
; phy_blk
++) {
2423 for (i
= 0; i
< segment
->disable_count
; i
++) {
2424 if (phy_blk
== segment
->defect_list
[i
]) {
2436 ms_read_extra_data(chip
, phy_blk
, 0, extra
, MS_EXTRA_SIZE
);
2437 if (retval
!= STATUS_SUCCESS
) {
2438 RTS51X_DEBUGP("read extra data fail\n");
2439 ms_set_bad_block(chip
, phy_blk
);
2443 if (seg_no
== ms_card
->segment_cnt
- 1) {
2444 if (!(extra
[1] & NOT_TRANSLATION_TABLE
)) {
2445 if (!(chip
->card_wp
& MS_CARD
)) {
2446 retval
= ms_erase_block(chip
, phy_blk
);
2447 if (retval
!= STATUS_SUCCESS
)
2455 if (!(extra
[0] & BLOCK_OK
))
2457 if (!(extra
[1] & NOT_BOOT_BLOCK
))
2459 if ((extra
[0] & PAGE_OK
) != PAGE_OK
)
2462 log_blk
= ((u16
) extra
[2] << 8) | extra
[3];
2464 if (log_blk
== 0xFFFF) {
2465 if (!(chip
->card_wp
& MS_CARD
)) {
2466 retval
= ms_erase_block(chip
, phy_blk
);
2467 if (retval
!= STATUS_SUCCESS
)
2470 ms_set_unused_block(chip
, phy_blk
);
2474 if ((log_blk
< ms_start_idx
[seg_no
]) ||
2475 (log_blk
>= ms_start_idx
[seg_no
+ 1])) {
2476 if (!(chip
->card_wp
& MS_CARD
)) {
2477 retval
= ms_erase_block(chip
, phy_blk
);
2478 if (retval
!= STATUS_SUCCESS
)
2481 ms_set_unused_block(chip
, phy_blk
);
2485 if (segment
->l2p_table
[log_blk
- ms_start_idx
[seg_no
]] ==
2487 segment
->l2p_table
[log_blk
- ms_start_idx
[seg_no
]] =
2492 us1
= extra
[0] & 0x10;
2493 tmp_blk
= segment
->l2p_table
[log_blk
- ms_start_idx
[seg_no
]];
2495 ms_read_extra_data(chip
, tmp_blk
, 0, extra
, MS_EXTRA_SIZE
);
2496 if (retval
!= STATUS_SUCCESS
)
2498 us2
= extra
[0] & 0x10;
2500 (void)ms_arbitrate_l2p(chip
, phy_blk
,
2501 log_blk
- ms_start_idx
[seg_no
], us1
,
2506 segment
->build_flag
= 1;
2508 RTS51X_DEBUGP("unused block count: %d\n", segment
->unused_blk_cnt
);
2510 if (seg_no
== ms_card
->segment_cnt
- 1) {
2511 if (segment
->unused_blk_cnt
< 2)
2512 chip
->card_wp
|= MS_CARD
;
2514 if (segment
->unused_blk_cnt
< 1)
2515 chip
->card_wp
|= MS_CARD
;
2518 if (chip
->card_wp
& MS_CARD
)
2519 return STATUS_SUCCESS
;
2521 for (log_blk
= ms_start_idx
[seg_no
]; log_blk
< ms_start_idx
[seg_no
+ 1];
2523 if (segment
->l2p_table
[log_blk
- ms_start_idx
[seg_no
]] ==
2525 phy_blk
= ms_get_unused_block(chip
, seg_no
);
2526 if (phy_blk
== 0xFFFF) {
2527 chip
->card_wp
|= MS_CARD
;
2528 return STATUS_SUCCESS
;
2530 retval
= ms_init_page(chip
, phy_blk
, log_blk
, 0, 1);
2531 if (retval
!= STATUS_SUCCESS
)
2532 TRACE_GOTO(chip
, BUILD_FAIL
);
2533 segment
->l2p_table
[log_blk
- ms_start_idx
[seg_no
]] =
2535 if (seg_no
== ms_card
->segment_cnt
- 1) {
2536 if (segment
->unused_blk_cnt
< 2) {
2537 chip
->card_wp
|= MS_CARD
;
2538 return STATUS_SUCCESS
;
2541 if (segment
->unused_blk_cnt
< 1) {
2542 chip
->card_wp
|= MS_CARD
;
2543 return STATUS_SUCCESS
;
2550 for (log_blk
= 0; log_blk
< 494; log_blk
++) {
2551 tmp_blk
= segment
->l2p_table
[log_blk
];
2552 if (tmp_blk
< ms_card
->boot_block
) {
2553 RTS51X_DEBUGP("Boot block is not the first normal block.\n");
2555 if (chip
->card_wp
& MS_CARD
)
2558 phy_blk
= ms_get_unused_block(chip
, 0);
2561 ms_auto_copy_page(chip
, tmp_blk
, phy_blk
,
2563 ms_card
->page_off
+ 1);
2565 retval
= ms_copy_page(chip
, tmp_blk
, phy_blk
,
2567 ms_card
->page_off
+ 1);
2569 if (retval
!= STATUS_SUCCESS
)
2570 TRACE_RET(chip
, retval
);
2572 segment
->l2p_table
[log_blk
] = phy_blk
;
2574 retval
= ms_set_bad_block(chip
, tmp_blk
);
2575 if (retval
!= STATUS_SUCCESS
)
2576 TRACE_RET(chip
, retval
);
2581 return STATUS_SUCCESS
;
2584 segment
->build_flag
= 0;
2585 if (segment
->l2p_table
) {
2586 vfree(segment
->l2p_table
);
2587 segment
->l2p_table
= NULL
;
2589 if (segment
->free_table
) {
2590 vfree(segment
->free_table
);
2591 segment
->free_table
= NULL
;
2597 int rts51x_reset_ms_card(struct rts51x_chip
*chip
)
2599 struct ms_info
*ms_card
= &(chip
->ms_card
);
2602 memset(ms_card
, 0, sizeof(struct ms_info
));
2604 rts51x_enable_card_clock(chip
, MS_CARD
);
2606 retval
= rts51x_select_card(chip
, MS_CARD
);
2607 if (retval
!= STATUS_SUCCESS
)
2608 TRACE_RET(chip
, retval
);
2610 ms_card
->ms_type
= 0;
2611 ms_card
->last_rw_int
= 0;
2613 retval
= reset_ms_pro(chip
);
2614 if (retval
!= STATUS_SUCCESS
) {
2615 if (ms_card
->check_ms_flow
) {
2616 retval
= reset_ms(chip
);
2617 if (retval
!= STATUS_SUCCESS
) {
2618 if (chip
->option
.reset_or_rw_fail_set_pad_drive
) {
2619 rts51x_write_register(chip
,
2620 CARD_DRIVE_SEL
, SD20_DRIVE_MASK
,
2623 TRACE_RET(chip
, retval
);
2626 if (chip
->option
.reset_or_rw_fail_set_pad_drive
) {
2627 rts51x_write_register(chip
, CARD_DRIVE_SEL
,
2631 TRACE_RET(chip
, retval
);
2635 retval
= ms_set_init_para(chip
);
2636 if (retval
!= STATUS_SUCCESS
)
2637 TRACE_RET(chip
, retval
);
2639 if (!CHK_MSPRO(ms_card
)) {
2640 retval
= ms_build_l2p_tbl(chip
, ms_card
->total_block
/ 512 - 1);
2641 if (retval
!= STATUS_SUCCESS
)
2642 TRACE_RET(chip
, retval
);
2645 RTS51X_DEBUGP("ms_card->ms_type = 0x%x\n", ms_card
->ms_type
);
2647 return STATUS_SUCCESS
;
2650 static int mspro_set_rw_cmd(struct rts51x_chip
*chip
, u32 start_sec
,
2651 u16 sec_cnt
, u8 cmd
)
2657 data
[1] = (u8
) (sec_cnt
>> 8);
2658 data
[2] = (u8
) sec_cnt
;
2659 data
[3] = (u8
) (start_sec
>> 24);
2660 data
[4] = (u8
) (start_sec
>> 16);
2661 data
[5] = (u8
) (start_sec
>> 8);
2662 data
[6] = (u8
) start_sec
;
2665 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
2667 ms_write_bytes(chip
, PRO_EX_SET_CMD
, 7, WAIT_INT
, data
, 8);
2668 if (retval
== STATUS_SUCCESS
)
2671 if (i
== MS_MAX_RETRY_COUNT
)
2672 TRACE_RET(chip
, STATUS_FAIL
);
2674 return STATUS_SUCCESS
;
2677 static void mspro_stop_seq_mode(struct rts51x_chip
*chip
)
2679 struct ms_info
*ms_card
= &(chip
->ms_card
);
2682 if (ms_card
->seq_mode
) {
2683 retval
= ms_switch_clock(chip
);
2684 if (retval
!= STATUS_SUCCESS
)
2687 ms_card
->seq_mode
= 0;
2688 ms_card
->total_sec_cnt
= 0;
2689 ms_card
->last_rw_int
= 0;
2690 ms_send_cmd(chip
, PRO_STOP
, WAIT_INT
);
2692 rts51x_ep0_write_register(chip
, MC_FIFO_CTL
, FIFO_FLUSH
,
2697 static inline int ms_auto_tune_clock(struct rts51x_chip
*chip
)
2699 struct ms_info
*ms_card
= &(chip
->ms_card
);
2702 if (chip
->asic_code
) {
2703 if (ms_card
->ms_clock
> 30)
2704 ms_card
->ms_clock
-= 20;
2706 if (ms_card
->ms_clock
== CLK_80
)
2707 ms_card
->ms_clock
= CLK_60
;
2708 else if (ms_card
->ms_clock
== CLK_60
)
2709 ms_card
->ms_clock
= CLK_40
;
2712 retval
= ms_switch_clock(chip
);
2713 if (retval
!= STATUS_SUCCESS
)
2714 TRACE_RET(chip
, retval
);
2716 return STATUS_SUCCESS
;
2719 static int mspro_rw_multi_sector(struct scsi_cmnd
*srb
,
2720 struct rts51x_chip
*chip
, u32 start_sector
,
2723 struct ms_info
*ms_card
= &(chip
->ms_card
);
2724 int retval
, mode_2k
= 0;
2726 u8 val
, trans_mode
, rw_tpc
, rw_cmd
;
2728 ms_set_err_code(chip
, MS_NO_ERROR
);
2730 ms_card
->counter
= 0;
2732 if (CHK_MSHG(ms_card
)) {
2733 if ((start_sector
% 4) || (sector_cnt
% 4)) {
2734 if (srb
->sc_data_direction
== DMA_FROM_DEVICE
) {
2735 rw_tpc
= PRO_READ_LONG_DATA
;
2736 rw_cmd
= PRO_READ_DATA
;
2738 rw_tpc
= PRO_WRITE_LONG_DATA
;
2739 rw_cmd
= PRO_WRITE_DATA
;
2742 if (srb
->sc_data_direction
== DMA_FROM_DEVICE
) {
2743 rw_tpc
= PRO_READ_QUAD_DATA
;
2744 rw_cmd
= PRO_READ_2K_DATA
;
2746 rw_tpc
= PRO_WRITE_QUAD_DATA
;
2747 rw_cmd
= PRO_WRITE_2K_DATA
;
2752 if (srb
->sc_data_direction
== DMA_FROM_DEVICE
) {
2753 rw_tpc
= PRO_READ_LONG_DATA
;
2754 rw_cmd
= PRO_READ_DATA
;
2756 rw_tpc
= PRO_WRITE_LONG_DATA
;
2757 rw_cmd
= PRO_WRITE_DATA
;
2761 retval
= ms_switch_clock(chip
);
2762 if (retval
!= STATUS_SUCCESS
)
2763 TRACE_RET(chip
, retval
);
2765 if (srb
->sc_data_direction
== DMA_FROM_DEVICE
)
2766 trans_mode
= MS_TM_AUTO_READ
;
2768 trans_mode
= MS_TM_AUTO_WRITE
;
2770 val
= ms_card
->last_rw_int
;
2772 if (ms_card
->seq_mode
) {
2773 if ((ms_card
->pre_dir
!= srb
->sc_data_direction
)
2774 || ((ms_card
->pre_sec_addr
+ ms_card
->pre_sec_cnt
) !=
2776 || (mode_2k
&& (ms_card
->seq_mode
& MODE_512_SEQ
))
2777 || (!mode_2k
&& (ms_card
->seq_mode
& MODE_2K_SEQ
))
2778 || !(val
& MS_INT_BREQ
)
2779 || ((ms_card
->total_sec_cnt
+ sector_cnt
) > 0xFE00)) {
2780 ms_card
->seq_mode
= 0;
2781 ms_card
->total_sec_cnt
= 0;
2782 ms_card
->last_rw_int
= 0;
2783 if (val
& MS_INT_BREQ
) {
2784 retval
= ms_send_cmd(chip
, PRO_STOP
, WAIT_INT
);
2785 if (retval
!= STATUS_SUCCESS
)
2786 TRACE_RET(chip
, retval
);
2788 rts51x_ep0_write_register(chip
, MC_FIFO_CTL
,
2789 FIFO_FLUSH
, FIFO_FLUSH
);
2794 if (!ms_card
->seq_mode
) {
2795 ms_card
->total_sec_cnt
= 0;
2796 if (sector_cnt
>= 0x80) {
2797 if ((ms_card
->capacity
- start_sector
) > 0xFE00)
2801 (u16
) (ms_card
->capacity
- start_sector
);
2802 if (count
> sector_cnt
) {
2804 ms_card
->seq_mode
|= MODE_2K_SEQ
;
2806 ms_card
->seq_mode
|= MODE_512_SEQ
;
2811 retval
= mspro_set_rw_cmd(chip
, start_sector
, count
, rw_cmd
);
2812 if (retval
!= STATUS_SUCCESS
) {
2813 ms_card
->seq_mode
= 0;
2814 TRACE_RET(chip
, retval
);
2819 ms_transfer_data(chip
, trans_mode
, rw_tpc
, sector_cnt
, WAIT_INT
,
2820 mode_2k
, scsi_sg_count(srb
), scsi_sglist(srb
),
2822 if (retval
!= STATUS_SUCCESS
) {
2823 ms_card
->seq_mode
= 0;
2824 rts51x_ep0_read_register(chip
, MS_TRANS_CFG
, &val
);
2825 rts51x_clear_ms_error(chip
);
2826 if (val
& MS_INT_BREQ
)
2827 ms_send_cmd(chip
, PRO_STOP
, WAIT_INT
);
2828 if (val
& (MS_CRC16_ERR
| MS_RDY_TIMEOUT
)) {
2829 RTS51X_DEBUGP("MSPro CRC error, tune clock!\n");
2830 ms_auto_tune_clock(chip
);
2833 TRACE_RET(chip
, retval
);
2836 ms_card
->pre_sec_addr
= start_sector
;
2837 ms_card
->pre_sec_cnt
= sector_cnt
;
2838 ms_card
->pre_dir
= srb
->sc_data_direction
;
2839 ms_card
->total_sec_cnt
+= sector_cnt
;
2841 return STATUS_SUCCESS
;
2844 static int mspro_read_format_progress(struct rts51x_chip
*chip
,
2845 const int short_data_len
)
2847 struct ms_info
*ms_card
= &(chip
->ms_card
);
2849 u32 total_progress
, cur_progress
;
2853 ms_card
->format_status
= FORMAT_FAIL
;
2855 retval
= ms_switch_clock(chip
);
2856 if (retval
!= STATUS_SUCCESS
)
2857 TRACE_RET(chip
, retval
);
2859 RTS51X_READ_REG(chip
, MS_TRANS_CFG
, &tmp
);
2861 if ((tmp
& (MS_INT_CED
| MS_INT_CMDNK
| MS_INT_ERR
)) == MS_INT_CED
) {
2862 ms_card
->format_status
= FORMAT_SUCCESS
;
2863 ms_card
->pro_under_formatting
= 0;
2864 return STATUS_SUCCESS
;
2867 ((tmp
& (MS_INT_BREQ
| MS_INT_CED
| MS_INT_CMDNK
| MS_INT_ERR
)) ==
2869 ms_card
->pro_under_formatting
= 0;
2870 TRACE_RET(chip
, STATUS_FAIL
);
2873 if (short_data_len
>= 256)
2876 cnt
= (u8
) short_data_len
;
2879 ms_read_bytes(chip
, PRO_READ_SHORT_DATA
, cnt
, WAIT_INT
, data
, 8);
2880 if (retval
!= STATUS_SUCCESS
)
2881 TRACE_RET(chip
, retval
);
2884 (data
[0] << 24) | (data
[1] << 16) | (data
[2] << 8) | data
[3];
2886 (data
[4] << 24) | (data
[5] << 16) | (data
[6] << 8) | data
[7];
2888 RTS51X_DEBUGP("total_progress = %d, cur_progress = %d\n",
2889 total_progress
, cur_progress
);
2891 if (total_progress
== 0) {
2892 ms_card
->progress
= 0;
2894 u64 ulltmp
= (u64
) cur_progress
* (u64
) 65535;
2895 do_div(ulltmp
, total_progress
);
2896 ms_card
->progress
= (u16
) ulltmp
;
2898 RTS51X_DEBUGP("progress = %d\n", ms_card
->progress
);
2900 for (i
= 0; i
< 2500; i
++) {
2901 RTS51X_READ_REG(chip
, MS_TRANS_CFG
, &tmp
);
2903 (MS_INT_CED
| MS_INT_CMDNK
| MS_INT_BREQ
| MS_INT_ERR
))
2910 TRACE_RET(chip
, STATUS_FAIL
);
2912 RTS51X_DEBUGP("MSPro format tmp:%d\n", tmp
);
2914 if (tmp
& (MS_INT_CMDNK
| MS_INT_ERR
))
2915 TRACE_RET(chip
, STATUS_FAIL
);
2916 if (tmp
& MS_INT_CED
) {
2917 ms_card
->format_status
= FORMAT_SUCCESS
;
2918 ms_card
->pro_under_formatting
= 0;
2919 } else if (tmp
& MS_INT_BREQ
) {
2920 ms_card
->format_status
= FORMAT_IN_PROGRESS
;
2922 ms_card
->format_status
= FORMAT_FAIL
;
2923 ms_card
->pro_under_formatting
= 0;
2924 TRACE_RET(chip
, STATUS_FAIL
);
2927 RTS51X_DEBUGP("MSPro format format_status:%d\n",
2928 ms_card
->format_status
);
2930 return STATUS_SUCCESS
;
2933 void rts51x_mspro_polling_format_status(struct rts51x_chip
*chip
)
2935 struct ms_info
*ms_card
= &(chip
->ms_card
);
2938 if (ms_card
->pro_under_formatting
) {
2939 for (i
= 0; i
< 65535; i
++) {
2940 mspro_read_format_progress(chip
, MS_SHORT_DATA_LEN
);
2941 if (ms_card
->format_status
!= FORMAT_IN_PROGRESS
)
2949 void rts51x_mspro_format_sense(struct rts51x_chip
*chip
, unsigned int lun
)
2951 struct ms_info
*ms_card
= &(chip
->ms_card
);
2953 if (CHK_FORMAT_STATUS(ms_card
, FORMAT_SUCCESS
)) {
2954 rts51x_set_sense_type(chip
, lun
, SENSE_TYPE_NO_SENSE
);
2955 ms_card
->pro_under_formatting
= 0;
2956 ms_card
->progress
= 0;
2957 } else if (CHK_FORMAT_STATUS(ms_card
, FORMAT_IN_PROGRESS
)) {
2958 rts51x_set_sense_data(chip
, lun
, CUR_ERR
, 0x02, 0, 0x04, 0x04,
2959 0, (u16
) (ms_card
->progress
));
2961 rts51x_set_sense_type(chip
, lun
, SENSE_TYPE_FORMAT_CMD_FAILED
);
2962 ms_card
->pro_under_formatting
= 0;
2963 ms_card
->progress
= 0;
2967 int rts51x_mspro_format(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
,
2968 int short_data_len
, int quick_format
)
2970 struct ms_info
*ms_card
= &(chip
->ms_card
);
2975 retval
= ms_switch_clock(chip
);
2976 if (retval
!= STATUS_SUCCESS
)
2977 TRACE_RET(chip
, retval
);
2979 retval
= ms_set_rw_reg_addr(chip
, 0x00, 0x00, Pro_TPCParm
, 0x01);
2980 if (retval
!= STATUS_SUCCESS
)
2981 TRACE_RET(chip
, retval
);
2984 switch (short_data_len
) {
3000 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
3002 ms_write_bytes(chip
, PRO_WRITE_REG
, 1, NO_WAIT_INT
, buf
, 2);
3003 if (retval
== STATUS_SUCCESS
)
3006 if (i
== MS_MAX_RETRY_COUNT
)
3007 TRACE_RET(chip
, STATUS_FAIL
);
3008 /* Format command */
3013 retval
= mspro_set_rw_cmd(chip
, 0, para
, PRO_FORMAT
);
3014 if (retval
!= STATUS_SUCCESS
)
3015 TRACE_RET(chip
, retval
);
3017 RTS51X_READ_REG(chip
, MS_TRANS_CFG
, &tmp
);
3018 if (tmp
& (MS_INT_CMDNK
| MS_INT_ERR
))
3019 TRACE_RET(chip
, STATUS_FAIL
);
3021 if ((tmp
& (MS_INT_BREQ
| MS_INT_CED
)) == MS_INT_BREQ
) {
3022 ms_card
->pro_under_formatting
= 1;
3023 ms_card
->progress
= 0;
3024 ms_card
->format_status
= FORMAT_IN_PROGRESS
;
3025 return STATUS_SUCCESS
;
3028 if (tmp
& MS_INT_CED
) {
3029 ms_card
->pro_under_formatting
= 0;
3030 ms_card
->progress
= 0;
3031 ms_card
->format_status
= FORMAT_SUCCESS
;
3032 rts51x_set_sense_type(chip
, SCSI_LUN(srb
), SENSE_TYPE_NO_SENSE
);
3033 return STATUS_SUCCESS
;
3036 TRACE_RET(chip
, STATUS_FAIL
);
3040 static int ms_read_multiple_pages(struct rts51x_chip
*chip
, u16 phy_blk
,
3041 u16 log_blk
, u8 start_page
, u8 end_page
,
3042 u8
*buf
, void **ptr
, unsigned int *offset
)
3044 struct ms_info
*ms_card
= &(chip
->ms_card
);
3047 u8 extra
[MS_EXTRA_SIZE
], val1
, val2
, data
[6];
3048 u8 page_cnt
= end_page
- start_page
, page_addr
, sec_cnt
;
3050 if (end_page
!= (ms_card
->page_off
+ 1))
3056 ms_read_extra_data(chip
, phy_blk
, start_page
, extra
, MS_EXTRA_SIZE
);
3057 if (retval
== STATUS_SUCCESS
) {
3058 if ((extra
[1] & 0x30) != 0x30) {
3059 ms_set_err_code(chip
, MS_FLASH_READ_ERROR
);
3060 TRACE_RET(chip
, STATUS_FAIL
);
3064 if (CHK_MS4BIT(ms_card
)) {
3065 /* Parallel interface */
3068 /* Serial interface */
3073 data
[2] = (u8
) (phy_blk
>> 8);
3074 data
[3] = (u8
) phy_blk
;
3076 * Extra data access mode */
3078 data
[5] = start_page
;
3081 ms_auto_set_cmd(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
, 6,
3082 BLOCK_READ
, WAIT_INT
, data
, 6, &val1
);
3083 if (retval
!= STATUS_SUCCESS
)
3084 TRACE_RET(chip
, retval
);
3086 rts51x_init_cmd(chip
);
3089 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_BLKEND
, SET_BLKEND
,
3092 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_BLKEND
, SET_BLKEND
, 0);
3093 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANS_CFG
, WAIT_INT
,
3095 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_SECTOR_CNT_L
, 0xFF,
3097 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_SECTOR_CNT_H
, 0xFF, 0);
3098 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TPC
, 0xFF, READ_PAGE_DATA
);
3100 rts51x_trans_dma_enable(DMA_FROM_DEVICE
, chip
, 512 * page_cnt
, DMA_512
);
3102 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANSFER
, 0xFF,
3103 MS_TRANSFER_START
| MS_TM_MULTI_READ
);
3104 rts51x_add_cmd(chip
, CHECK_REG_CMD
, MS_TRANSFER
, MS_TRANSFER_END
,
3107 retval
= rts51x_send_cmd(chip
, MODE_CDIR
| STAGE_MS_STATUS
, 100);
3108 if (retval
!= STATUS_SUCCESS
)
3109 TRACE_RET(chip
, retval
);
3112 rts51x_transfer_data_partial(chip
, RCV_BULK_PIPE(chip
), (void *)buf
,
3113 ptr
, offset
, 512 * page_cnt
,
3114 scsi_sg_count(chip
->srb
), NULL
, 2000);
3115 if (retval
!= STATUS_SUCCESS
) {
3116 rts51x_clear_ms_error(chip
);
3117 if (retval
== STATUS_TIMEDOUT
)
3118 TRACE_RET(chip
, retval
);
3119 TRACE_GOTO(chip
, Fail
);
3121 retval
= rts51x_get_rsp(chip
, 3, 200);
3122 if (CHECK_MS_TRANS_FAIL(chip
, retval
)) {
3123 rts51x_clear_ms_error(chip
);
3124 if (retval
== STATUS_TIMEDOUT
)
3125 TRACE_RET(chip
, retval
);
3126 TRACE_GOTO(chip
, Fail
);
3129 return STATUS_SUCCESS
;
3132 rts51x_init_cmd(chip
);
3134 rts51x_add_cmd(chip
, READ_REG_CMD
, MS_SECTOR_CNT_L
, 0, 0);
3136 retval
= rts51x_send_cmd(chip
, MODE_CR
| STAGE_MS_STATUS
, 100);
3137 if (retval
!= STATUS_SUCCESS
)
3138 TRACE_RET(chip
, retval
);
3140 retval
= rts51x_get_rsp(chip
, 3, 200);
3142 if (CHECK_MS_TRANS_FAIL(chip
, retval
))
3143 TRACE_RET(chip
, STATUS_FAIL
);
3145 sec_cnt
= chip
->rsp_buf
[0];
3146 RTS51X_DEBUGP("%d pages need be transferred, %d pages remained\n",
3147 (int)page_cnt
, (int)sec_cnt
);
3148 page_addr
= start_page
+ (page_cnt
- sec_cnt
);
3150 if (CHK_MS4BIT(ms_card
)) {
3151 val1
= chip
->rsp_buf
[1];
3152 RTS51X_DEBUGP("MS_TRANS_CFG: 0x%x\n", val1
);
3157 val2
= chip
->rsp_buf
[2];
3158 RTS51X_DEBUGP("GET_INT: 0x%x\n", val2
);
3160 if ((val1
& INT_CMDNK
) || (val2
& INT_REG_CMDNK
)) {
3161 ms_set_err_code(chip
, MS_CMD_NK
);
3162 TRACE_RET(chip
, STATUS_FAIL
);
3165 if ((val1
& INT_ERR
) || (val2
& INT_REG_ERR
)) {
3166 if ((val1
& INT_BREQ
) || (val2
& INT_REG_BREQ
)) {
3167 retval
= ms_read_status_reg(chip
);
3168 if (retval
!= STATUS_SUCCESS
) {
3169 if (!(chip
->card_wp
& MS_CARD
)) {
3171 ms_set_page_status(log_blk
, setPS_NG
,
3172 extra
, MS_EXTRA_SIZE
);
3173 ms_write_extra_data(chip
, phy_blk
,
3177 ms_set_err_code(chip
, MS_FLASH_READ_ERROR
);
3178 TRACE_RET(chip
, STATUS_FAIL
);
3181 ms_set_err_code(chip
, MS_FLASH_READ_ERROR
);
3182 TRACE_RET(chip
, STATUS_FAIL
);
3185 if (CHK_MS4BIT(ms_card
)) {
3186 if (!(val1
& INT_BREQ
) && !(val2
& INT_REG_BREQ
)) {
3187 ms_set_err_code(chip
, MS_BREQ_ERROR
);
3188 TRACE_RET(chip
, STATUS_FAIL
);
3191 if (!(val2
& INT_REG_BREQ
)) {
3192 ms_set_err_code(chip
, MS_BREQ_ERROR
);
3193 TRACE_RET(chip
, STATUS_FAIL
);
3198 TRACE_RET(chip
, STATUS_FAIL
);
3201 static int ms_write_multiple_pages(struct rts51x_chip
*chip
, u16 old_blk
,
3202 u16 new_blk
, u16 log_blk
, u8 start_page
,
3203 u8 end_page
, u8
*buf
, void **ptr
,
3204 unsigned int *offset
)
3206 struct ms_info
*ms_card
= &(chip
->ms_card
);
3210 u8 page_cnt
= end_page
- start_page
;
3212 if ((end_page
== (ms_card
->page_off
+ 1)) || (page_cnt
== 1))
3218 if (CHK_MS4BIT(ms_card
)) {
3219 /* Parallel interface */
3222 /* Serial interface */
3227 data
[2] = (u8
) (old_blk
>> 8);
3228 data
[3] = (u8
) old_blk
;
3235 ms_auto_set_cmd(chip
, OverwriteFlag
, MS_EXTRA_SIZE
,
3236 SystemParm
, 7, BLOCK_WRITE
, WAIT_INT
, data
,
3238 if (retval
!= STATUS_SUCCESS
)
3239 TRACE_RET(chip
, retval
);
3243 ms_set_rw_reg_addr(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
,
3244 (6 + MS_EXTRA_SIZE
));
3245 if (retval
!= STATUS_SUCCESS
)
3246 TRACE_RET(chip
, retval
);
3248 ms_set_err_code(chip
, MS_NO_ERROR
);
3250 if (CHK_MS4BIT(ms_card
)) {
3251 /* Parallel interface */
3254 /* Serial interface */
3259 data
[2] = (u8
) (new_blk
>> 8);
3260 data
[3] = (u8
) new_blk
;
3262 * Extra data access mode */
3263 if (page_cnt
== 1) {
3264 /* Single page access mode */
3267 /* Block access mode */
3270 data
[5] = start_page
;
3273 data
[8] = (u8
) (log_blk
>> 8);
3274 data
[9] = (u8
) log_blk
;
3276 for (i
= 0x0A; i
< 0x10; i
++) {
3282 ms_auto_set_cmd(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
,
3283 (6 + MS_EXTRA_SIZE
), BLOCK_WRITE
, WAIT_INT
, data
,
3285 if (retval
!= STATUS_SUCCESS
)
3286 TRACE_RET(chip
, retval
);
3288 rts51x_init_cmd(chip
);
3291 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_BLKEND
, SET_BLKEND
,
3294 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_BLKEND
, SET_BLKEND
, 0);
3295 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANS_CFG
, WAIT_INT
,
3297 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_SECTOR_CNT_L
, 0xFF,
3299 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_SECTOR_CNT_H
, 0xFF, 0);
3300 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TPC
, 0xFF, WRITE_PAGE_DATA
);
3301 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
, 0x01,
3304 rts51x_trans_dma_enable(DMA_TO_DEVICE
, chip
, 512 * page_cnt
, DMA_512
);
3306 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANSFER
, 0xFF,
3307 MS_TRANSFER_START
| MS_TM_MULTI_WRITE
);
3308 rts51x_add_cmd(chip
, CHECK_REG_CMD
, MS_TRANSFER
, MS_TRANSFER_END
,
3311 retval
= rts51x_send_cmd(chip
, MODE_CDOR
| STAGE_MS_STATUS
, 100);
3312 if (retval
!= STATUS_SUCCESS
)
3313 TRACE_RET(chip
, retval
);
3316 rts51x_transfer_data_partial(chip
, SND_BULK_PIPE(chip
), (void *)buf
,
3317 ptr
, offset
, 512 * page_cnt
,
3318 scsi_sg_count(chip
->srb
), NULL
, 2000);
3319 if (retval
!= STATUS_SUCCESS
) {
3320 rts51x_clear_ms_error(chip
);
3321 TRACE_RET(chip
, retval
);
3324 retval
= rts51x_get_rsp(chip
, 3, 2000);
3327 if (CHECK_MS_TRANS_FAIL(chip
, retval
)) {
3328 rts51x_clear_ms_error(chip
);
3329 TRACE_RET(chip
, STATUS_FAIL
);
3332 return STATUS_SUCCESS
;
3337 static int ms_read_multiple_pages(struct rts51x_chip
*chip
, u16 phy_blk
,
3338 u16 log_blk
, u8 start_page
, u8 end_page
,
3339 u8
*buf
, void **ptr
, unsigned int *offset
)
3341 struct ms_info
*ms_card
= &(chip
->ms_card
);
3343 u8 extra
[MS_EXTRA_SIZE
], page_addr
, val
, trans_cfg
, data
[6];
3346 ms_read_extra_data(chip
, phy_blk
, start_page
, extra
, MS_EXTRA_SIZE
);
3347 if (retval
== STATUS_SUCCESS
) {
3348 if ((extra
[1] & 0x30) != 0x30) {
3349 ms_set_err_code(chip
, MS_FLASH_READ_ERROR
);
3350 TRACE_RET(chip
, STATUS_FAIL
);
3355 ms_set_rw_reg_addr(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
,
3357 if (retval
!= STATUS_SUCCESS
)
3358 TRACE_RET(chip
, retval
);
3360 if (CHK_MS4BIT(ms_card
)) {
3361 /* Parallel interface */
3364 /* Serial interface */
3369 data
[2] = (u8
) (phy_blk
>> 8);
3370 data
[3] = (u8
) phy_blk
;
3372 * Extra data access mode */
3374 data
[5] = start_page
;
3376 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
3378 ms_write_bytes(chip
, WRITE_REG
, 6, NO_WAIT_INT
, data
, 6);
3379 if (retval
== STATUS_SUCCESS
)
3382 if (i
== MS_MAX_RETRY_COUNT
)
3383 TRACE_RET(chip
, STATUS_FAIL
);
3385 ms_set_err_code(chip
, MS_NO_ERROR
);
3387 retval
= ms_send_cmd(chip
, BLOCK_READ
, WAIT_INT
);
3388 if (retval
!= STATUS_SUCCESS
)
3389 TRACE_RET(chip
, retval
);
3391 for (page_addr
= start_page
; page_addr
< end_page
; page_addr
++) {
3392 ms_set_err_code(chip
, MS_NO_ERROR
);
3394 if (monitor_card_cd(chip
, MS_CARD
) == CD_NOT_EXIST
) {
3395 ms_set_err_code(chip
, MS_NO_CARD
);
3396 chip
->card_exist
&= ~MS_CARD
;
3397 chip
->card_ready
&= ~MS_CARD
;
3398 TRACE_RET(chip
, STATUS_FAIL
);
3400 /* GET_INT Register */
3401 retval
= ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
, 1);
3402 if (retval
!= STATUS_SUCCESS
)
3403 TRACE_RET(chip
, retval
);
3404 if (val
& INT_REG_CMDNK
) {
3405 ms_set_err_code(chip
, MS_CMD_NK
);
3406 TRACE_RET(chip
, STATUS_FAIL
);
3408 if (val
& INT_REG_ERR
) {
3409 if (val
& INT_REG_BREQ
) {
3410 retval
= ms_read_status_reg(chip
);
3411 if (retval
!= STATUS_SUCCESS
) {
3412 if (!(chip
->card_wp
& MS_CARD
)) {
3414 ms_set_page_status(log_blk
,
3417 ms_write_extra_data(chip
,
3419 extra
, MS_EXTRA_SIZE
);
3421 ms_set_err_code(chip
,
3422 MS_FLASH_READ_ERROR
);
3423 TRACE_RET(chip
, STATUS_FAIL
);
3426 ms_set_err_code(chip
, MS_FLASH_READ_ERROR
);
3427 TRACE_RET(chip
, STATUS_FAIL
);
3430 if (!(val
& INT_REG_BREQ
)) {
3431 ms_set_err_code(chip
, MS_BREQ_ERROR
);
3432 TRACE_RET(chip
, STATUS_FAIL
);
3436 if (page_addr
== (end_page
- 1)) {
3437 if (!(val
& INT_REG_CED
)) {
3438 retval
= ms_send_cmd(chip
, BLOCK_END
, WAIT_INT
);
3439 if (retval
!= STATUS_SUCCESS
)
3440 TRACE_RET(chip
, retval
);
3443 ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
,
3445 if (retval
!= STATUS_SUCCESS
)
3446 TRACE_RET(chip
, retval
);
3447 if (!(val
& INT_REG_CED
)) {
3448 ms_set_err_code(chip
, MS_FLASH_READ_ERROR
);
3449 TRACE_RET(chip
, STATUS_FAIL
);
3452 trans_cfg
= NO_WAIT_INT
;
3454 trans_cfg
= WAIT_INT
;
3457 rts51x_init_cmd(chip
);
3459 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TPC
, 0xFF,
3461 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANS_CFG
, 0xFF,
3464 rts51x_trans_dma_enable(DMA_FROM_DEVICE
, chip
, 512, DMA_512
);
3466 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANSFER
, 0xFF,
3467 MS_TRANSFER_START
| MS_TM_NORMAL_READ
);
3468 rts51x_add_cmd(chip
, CHECK_REG_CMD
, MS_TRANSFER
,
3469 MS_TRANSFER_END
, MS_TRANSFER_END
);
3471 retval
= rts51x_send_cmd(chip
, MODE_CDIR
, 100);
3472 if (retval
!= STATUS_SUCCESS
)
3473 TRACE_RET(chip
, retval
);
3476 rts51x_transfer_data_partial(chip
, RCV_BULK_PIPE(chip
),
3477 (void *)buf
, ptr
, offset
, 512,
3478 scsi_sg_count(chip
->srb
), NULL
,
3480 if (retval
!= STATUS_SUCCESS
) {
3481 if (retval
== STATUS_TIMEDOUT
) {
3482 ms_set_err_code(chip
, MS_TO_ERROR
);
3483 rts51x_clear_ms_error(chip
);
3484 TRACE_RET(chip
, retval
);
3488 rts51x_ep0_read_register(chip
, MS_TRANS_CFG
, &val
);
3489 if (retval
!= STATUS_SUCCESS
) {
3490 ms_set_err_code(chip
, MS_TO_ERROR
);
3491 rts51x_clear_ms_error(chip
);
3492 TRACE_RET(chip
, STATUS_FAIL
);
3494 if (val
& (MS_CRC16_ERR
| MS_RDY_TIMEOUT
)) {
3495 ms_set_err_code(chip
, MS_CRC16_ERROR
);
3496 rts51x_clear_ms_error(chip
);
3497 TRACE_RET(chip
, STATUS_FAIL
);
3501 retval
= rts51x_get_rsp(chip
, 1, 2000);
3502 if (CHECK_MS_TRANS_FAIL(chip
, retval
)) {
3503 if (retval
== STATUS_TIMEDOUT
) {
3504 ms_set_err_code(chip
, MS_TO_ERROR
);
3505 rts51x_clear_ms_error(chip
);
3506 TRACE_RET(chip
, retval
);
3510 rts51x_ep0_read_register(chip
, MS_TRANS_CFG
, &val
);
3511 if (retval
!= STATUS_SUCCESS
) {
3512 ms_set_err_code(chip
, MS_TO_ERROR
);
3513 rts51x_clear_ms_error(chip
);
3514 TRACE_RET(chip
, retval
);
3516 if (val
& (MS_CRC16_ERR
| MS_RDY_TIMEOUT
)) {
3517 ms_set_err_code(chip
, MS_CRC16_ERROR
);
3518 rts51x_clear_ms_error(chip
);
3519 TRACE_RET(chip
, STATUS_FAIL
);
3524 return STATUS_SUCCESS
;
3527 static int ms_write_multiple_pages(struct rts51x_chip
*chip
, u16 old_blk
,
3528 u16 new_blk
, u16 log_blk
, u8 start_page
,
3529 u8 end_page
, u8
*buf
, void **ptr
,
3530 unsigned int *offset
)
3532 struct ms_info
*ms_card
= &(chip
->ms_card
);
3534 u8 page_addr
, val
, data
[16];
3538 ms_set_rw_reg_addr(chip
, OverwriteFlag
, MS_EXTRA_SIZE
,
3540 if (retval
!= STATUS_SUCCESS
)
3541 TRACE_RET(chip
, retval
);
3543 if (CHK_MS4BIT(ms_card
)) {
3544 /* Parallel interface */
3547 /* Serial interface */
3552 data
[2] = (u8
) (old_blk
>> 8);
3553 data
[3] = (u8
) old_blk
;
3560 ms_write_bytes(chip
, WRITE_REG
, 7, NO_WAIT_INT
, data
, 8);
3561 if (retval
!= STATUS_SUCCESS
)
3562 TRACE_RET(chip
, retval
);
3564 retval
= ms_send_cmd(chip
, BLOCK_WRITE
, WAIT_INT
);
3565 if (retval
!= STATUS_SUCCESS
)
3566 TRACE_RET(chip
, retval
);
3567 /* GET_INT Register */
3568 ms_set_err_code(chip
, MS_NO_ERROR
);
3570 ms_transfer_tpc(chip
, MS_TM_READ_BYTES
, GET_INT
, 1,
3572 if (retval
!= STATUS_SUCCESS
)
3573 TRACE_RET(chip
, retval
);
3577 ms_set_rw_reg_addr(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
,
3578 (6 + MS_EXTRA_SIZE
));
3579 if (retval
!= STATUS_SUCCESS
)
3580 TRACE_RET(chip
, retval
);
3582 ms_set_err_code(chip
, MS_NO_ERROR
);
3584 if (CHK_MS4BIT(ms_card
)) {
3585 /* Parallel interface */
3588 /* Serial interface */
3593 data
[2] = (u8
) (new_blk
>> 8);
3594 data
[3] = (u8
) new_blk
;
3596 * Extra data access mode */
3597 if ((end_page
- start_page
) == 1) {
3598 /* Single page access mode */
3601 /* Block access mode */
3604 data
[5] = start_page
;
3607 data
[8] = (u8
) (log_blk
>> 8);
3608 data
[9] = (u8
) log_blk
;
3610 for (i
= 0x0A; i
< 0x10; i
++) {
3615 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
3617 ms_write_bytes(chip
, WRITE_REG
, 6 + MS_EXTRA_SIZE
,
3618 NO_WAIT_INT
, data
, 16);
3619 if (retval
== STATUS_SUCCESS
)
3622 if (i
== MS_MAX_RETRY_COUNT
)
3623 TRACE_RET(chip
, STATUS_FAIL
);
3625 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
3626 retval
= ms_send_cmd(chip
, BLOCK_WRITE
, WAIT_INT
);
3627 if (retval
== STATUS_SUCCESS
)
3630 if (i
== MS_MAX_RETRY_COUNT
)
3631 TRACE_RET(chip
, STATUS_FAIL
);
3632 /* GET_INT Register */
3633 retval
= ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
, 1);
3634 if (retval
!= STATUS_SUCCESS
)
3635 TRACE_RET(chip
, retval
);
3637 for (page_addr
= start_page
; page_addr
< end_page
; page_addr
++) {
3638 ms_set_err_code(chip
, MS_NO_ERROR
);
3640 if (monitor_card_cd(chip
, MS_CARD
) == CD_NOT_EXIST
) {
3641 ms_set_err_code(chip
, MS_NO_CARD
);
3642 TRACE_RET(chip
, STATUS_FAIL
);
3645 if (val
& INT_REG_CMDNK
) {
3646 ms_set_err_code(chip
, MS_CMD_NK
);
3647 TRACE_RET(chip
, STATUS_FAIL
);
3649 if (val
& INT_REG_ERR
) {
3650 ms_set_err_code(chip
, MS_FLASH_WRITE_ERROR
);
3651 TRACE_RET(chip
, STATUS_FAIL
);
3653 if (!(val
& INT_REG_BREQ
)) {
3654 ms_set_err_code(chip
, MS_BREQ_ERROR
);
3655 TRACE_RET(chip
, STATUS_FAIL
);
3660 rts51x_init_cmd(chip
);
3662 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TPC
, 0xFF,
3664 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANS_CFG
, 0xFF,
3667 rts51x_trans_dma_enable(DMA_TO_DEVICE
, chip
, 512, DMA_512
);
3669 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANSFER
, 0xFF,
3670 MS_TRANSFER_START
| MS_TM_NORMAL_WRITE
);
3671 rts51x_add_cmd(chip
, CHECK_REG_CMD
, MS_TRANSFER
,
3672 MS_TRANSFER_END
, MS_TRANSFER_END
);
3674 retval
= rts51x_send_cmd(chip
, MODE_CDOR
, 100);
3675 if (retval
!= STATUS_SUCCESS
)
3676 TRACE_RET(chip
, retval
);
3679 rts51x_transfer_data_partial(chip
, SND_BULK_PIPE(chip
),
3680 (void *)buf
, ptr
, offset
, 512,
3681 scsi_sg_count(chip
->srb
), NULL
,
3683 if (retval
!= STATUS_SUCCESS
) {
3684 ms_set_err_code(chip
, MS_TO_ERROR
);
3685 rts51x_clear_ms_error(chip
);
3687 if (retval
== STATUS_TIMEDOUT
)
3688 TRACE_RET(chip
, STATUS_TIMEDOUT
);
3690 TRACE_RET(chip
, STATUS_FAIL
);
3693 retval
= rts51x_get_rsp(chip
, 1, 2000);
3694 if (CHECK_MS_TRANS_FAIL(chip
, retval
)) {
3695 ms_set_err_code(chip
, MS_TO_ERROR
);
3696 rts51x_clear_ms_error(chip
);
3698 if (retval
== STATUS_TIMEDOUT
)
3699 TRACE_RET(chip
, STATUS_TIMEDOUT
);
3701 TRACE_RET(chip
, STATUS_FAIL
);
3703 /* GET_INT Register */
3704 retval
= ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
, 1);
3705 if (retval
!= STATUS_SUCCESS
)
3706 TRACE_RET(chip
, retval
);
3708 if ((end_page
- start_page
) == 1) {
3709 if (!(val
& INT_REG_CED
)) {
3710 /* Command can not be executed */
3711 ms_set_err_code(chip
, MS_FLASH_WRITE_ERROR
);
3712 TRACE_RET(chip
, STATUS_FAIL
);
3715 if (page_addr
== (end_page
- 1)) {
3716 if (!(val
& INT_REG_CED
)) {
3718 ms_send_cmd(chip
, BLOCK_END
,
3720 if (retval
!= STATUS_SUCCESS
)
3721 TRACE_RET(chip
, retval
);
3723 /* GET_INT Register */
3725 ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
,
3727 if (retval
!= STATUS_SUCCESS
)
3728 TRACE_RET(chip
, retval
);
3731 if ((page_addr
== (end_page
- 1))
3732 || (page_addr
== ms_card
->page_off
)) {
3733 if (!(val
& INT_REG_CED
)) {
3734 ms_set_err_code(chip
,
3735 MS_FLASH_WRITE_ERROR
);
3736 TRACE_RET(chip
, STATUS_FAIL
);
3742 return STATUS_SUCCESS
;
3746 static int ms_finish_write(struct rts51x_chip
*chip
, u16 old_blk
, u16 new_blk
,
3747 u16 log_blk
, u8 page_off
)
3749 struct ms_info
*ms_card
= &(chip
->ms_card
);
3753 retval
= ms_auto_copy_page(chip
, old_blk
, new_blk
, log_blk
,
3754 page_off
, ms_card
->page_off
+ 1);
3756 retval
= ms_copy_page(chip
, old_blk
, new_blk
, log_blk
,
3757 page_off
, ms_card
->page_off
+ 1);
3759 if (retval
!= STATUS_SUCCESS
)
3760 TRACE_RET(chip
, retval
);
3762 seg_no
= old_blk
>> 9;
3764 if (MS_TST_BAD_BLOCK_FLG(ms_card
)) {
3765 MS_CLR_BAD_BLOCK_FLG(ms_card
);
3766 ms_set_bad_block(chip
, old_blk
);
3768 retval
= ms_erase_block(chip
, old_blk
);
3769 if (retval
== STATUS_SUCCESS
)
3770 ms_set_unused_block(chip
, old_blk
);
3773 ms_set_l2p_tbl(chip
, seg_no
, log_blk
- ms_start_idx
[seg_no
], new_blk
);
3775 return STATUS_SUCCESS
;
3778 static int ms_prepare_write(struct rts51x_chip
*chip
, u16 old_blk
, u16 new_blk
,
3779 u16 log_blk
, u8 start_page
)
3786 ms_auto_copy_page(chip
, old_blk
, new_blk
, log_blk
, 0,
3790 ms_copy_page(chip
, old_blk
, new_blk
, log_blk
, 0,
3793 if (retval
!= STATUS_SUCCESS
)
3794 TRACE_RET(chip
, retval
);
3797 return STATUS_SUCCESS
;
3800 int rts51x_ms_delay_write(struct rts51x_chip
*chip
)
3802 struct ms_info
*ms_card
= &(chip
->ms_card
);
3803 struct rts51x_ms_delay_write_tag
*delay_write
= &(ms_card
->delay_write
);
3806 if (delay_write
->delay_write_flag
) {
3807 retval
= ms_set_init_para(chip
);
3808 if (retval
!= STATUS_SUCCESS
)
3809 TRACE_RET(chip
, retval
);
3811 delay_write
->delay_write_flag
= 0;
3812 retval
= ms_finish_write(chip
,
3813 delay_write
->old_phyblock
,
3814 delay_write
->new_phyblock
,
3815 delay_write
->logblock
,
3816 delay_write
->pageoff
);
3817 if (retval
!= STATUS_SUCCESS
)
3818 TRACE_RET(chip
, retval
);
3821 return STATUS_SUCCESS
;
3824 static inline void rts51x_ms_rw_fail(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
)
3826 if (srb
->sc_data_direction
== DMA_FROM_DEVICE
)
3827 rts51x_set_sense_type(chip
, SCSI_LUN(srb
),
3828 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR
);
3830 rts51x_set_sense_type(chip
, SCSI_LUN(srb
), SENSE_TYPE_MEDIA_WRITE_ERR
);
3833 static int rts51x_ms_rw_multi_sector(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
,
3834 u32 start_sector
, u16 sector_cnt
)
3836 struct ms_info
*ms_card
= &(chip
->ms_card
);
3837 unsigned int lun
= SCSI_LUN(srb
);
3839 unsigned int offset
= 0;
3840 u16 old_blk
= 0, new_blk
= 0, log_blk
, total_sec_cnt
= sector_cnt
;
3841 u8 start_page
, end_page
= 0, page_cnt
;
3844 struct rts51x_ms_delay_write_tag
*delay_write
= &(ms_card
->delay_write
);
3846 ms_set_err_code(chip
, MS_NO_ERROR
);
3848 ms_card
->counter
= 0;
3850 buf
= (u8
*) scsi_sglist(srb
);
3852 retval
= ms_switch_clock(chip
);
3853 if (retval
!= STATUS_SUCCESS
) {
3854 rts51x_ms_rw_fail(srb
, chip
);
3855 TRACE_RET(chip
, retval
);
3858 log_blk
= (u16
) (start_sector
>> ms_card
->block_shift
);
3859 start_page
= (u8
) (start_sector
& ms_card
->page_off
);
3861 for (seg_no
= 0; seg_no
< ARRAY_SIZE(ms_start_idx
) - 1; seg_no
++) {
3862 if (log_blk
< ms_start_idx
[seg_no
+ 1])
3866 if (ms_card
->segment
[seg_no
].build_flag
== 0) {
3867 retval
= ms_build_l2p_tbl(chip
, seg_no
);
3868 if (retval
!= STATUS_SUCCESS
) {
3869 chip
->card_fail
|= MS_CARD
;
3870 rts51x_set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_NOT_PRESENT
);
3871 TRACE_RET(chip
, retval
);
3875 if (srb
->sc_data_direction
== DMA_TO_DEVICE
) {
3876 if (delay_write
->delay_write_flag
&&
3877 (delay_write
->logblock
== log_blk
) &&
3878 (start_page
> delay_write
->pageoff
)) {
3879 delay_write
->delay_write_flag
= 0;
3881 retval
= ms_auto_copy_page(chip
,
3882 delay_write
->old_phyblock
,
3883 delay_write
->new_phyblock
,
3885 delay_write
->pageoff
,
3888 retval
= ms_copy_page(chip
,
3889 delay_write
->old_phyblock
,
3890 delay_write
->new_phyblock
,
3891 log_blk
, delay_write
->pageoff
,
3894 if (retval
!= STATUS_SUCCESS
) {
3895 rts51x_set_sense_type(chip
, lun
,
3896 SENSE_TYPE_MEDIA_WRITE_ERR
);
3897 TRACE_RET(chip
, retval
);
3899 old_blk
= delay_write
->old_phyblock
;
3900 new_blk
= delay_write
->new_phyblock
;
3901 } else if (delay_write
->delay_write_flag
&&
3902 (delay_write
->logblock
== log_blk
) &&
3903 (start_page
== delay_write
->pageoff
)) {
3904 delay_write
->delay_write_flag
= 0;
3905 old_blk
= delay_write
->old_phyblock
;
3906 new_blk
= delay_write
->new_phyblock
;
3908 retval
= rts51x_ms_delay_write(chip
);
3909 if (retval
!= STATUS_SUCCESS
) {
3910 rts51x_set_sense_type(chip
, lun
,
3911 SENSE_TYPE_MEDIA_WRITE_ERR
);
3912 TRACE_RET(chip
, retval
);
3915 ms_get_l2p_tbl(chip
, seg_no
,
3916 log_blk
- ms_start_idx
[seg_no
]);
3917 new_blk
= ms_get_unused_block(chip
, seg_no
);
3918 if ((old_blk
== 0xFFFF) || (new_blk
== 0xFFFF)) {
3919 rts51x_set_sense_type(chip
, lun
,
3920 SENSE_TYPE_MEDIA_WRITE_ERR
);
3921 TRACE_RET(chip
, STATUS_FAIL
);
3925 ms_prepare_write(chip
, old_blk
, new_blk
, log_blk
,
3927 if (retval
!= STATUS_SUCCESS
) {
3928 if (monitor_card_cd(chip
, MS_CARD
) ==
3930 rts51x_set_sense_type(chip
, lun
,
3931 SENSE_TYPE_MEDIA_NOT_PRESENT
);
3932 TRACE_RET(chip
, STATUS_FAIL
);
3935 rts51x_set_sense_type(chip
, lun
,
3936 SENSE_TYPE_MEDIA_WRITE_ERR
);
3937 TRACE_RET(chip
, retval
);
3941 retval
= rts51x_ms_delay_write(chip
);
3942 if (retval
!= STATUS_SUCCESS
) {
3943 if (monitor_card_cd(chip
, MS_CARD
) == CD_NOT_EXIST
) {
3944 rts51x_set_sense_type(chip
, lun
,
3945 SENSE_TYPE_MEDIA_NOT_PRESENT
);
3946 TRACE_RET(chip
, STATUS_FAIL
);
3949 rts51x_set_sense_type(chip
, lun
,
3950 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR
);
3951 TRACE_RET(chip
, retval
);
3954 ms_get_l2p_tbl(chip
, seg_no
,
3955 log_blk
- ms_start_idx
[seg_no
]);
3956 if (old_blk
== 0xFFFF) {
3957 rts51x_set_sense_type(chip
, lun
,
3958 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR
);
3959 TRACE_RET(chip
, STATUS_FAIL
);
3963 RTS51X_DEBUGP("seg_no = %d, old_blk = 0x%x, new_blk = 0x%x\n", seg_no
,
3966 while (total_sec_cnt
) {
3967 if ((start_page
+ total_sec_cnt
) > (ms_card
->page_off
+ 1))
3968 end_page
= ms_card
->page_off
+ 1;
3970 end_page
= start_page
+ (u8
) total_sec_cnt
;
3971 page_cnt
= end_page
- start_page
;
3973 RTS51X_DEBUGP("start_page = %d, end_page = %d, page_cnt = %d\n",
3974 start_page
, end_page
, page_cnt
);
3976 if (srb
->sc_data_direction
== DMA_FROM_DEVICE
)
3977 retval
= ms_read_multiple_pages(chip
,
3979 start_page
, end_page
,
3980 buf
, &ptr
, &offset
);
3982 retval
= ms_write_multiple_pages(chip
, old_blk
,
3984 start_page
, end_page
,
3985 buf
, &ptr
, &offset
);
3987 if (retval
!= STATUS_SUCCESS
) {
3988 if (monitor_card_cd(chip
, MS_CARD
) == CD_NOT_EXIST
) {
3989 rts51x_set_sense_type(chip
, lun
,
3990 SENSE_TYPE_MEDIA_NOT_PRESENT
);
3991 TRACE_RET(chip
, STATUS_FAIL
);
3994 rts51x_ms_rw_fail(srb
, chip
);
3995 TRACE_RET(chip
, retval
);
3997 /* Update L2P table if need */
3998 if (srb
->sc_data_direction
== DMA_TO_DEVICE
) {
3999 if (end_page
== (ms_card
->page_off
+ 1)) {
4000 retval
= ms_erase_block(chip
, old_blk
);
4001 if (retval
== STATUS_SUCCESS
)
4002 ms_set_unused_block(chip
, old_blk
);
4003 ms_set_l2p_tbl(chip
, seg_no
,
4004 log_blk
- ms_start_idx
[seg_no
],
4009 total_sec_cnt
-= page_cnt
;
4011 if (total_sec_cnt
== 0)
4016 for (seg_no
= 0; seg_no
< ARRAY_SIZE(ms_start_idx
) - 1;
4018 if (log_blk
< ms_start_idx
[seg_no
+ 1])
4022 if (ms_card
->segment
[seg_no
].build_flag
== 0) {
4023 retval
= ms_build_l2p_tbl(chip
, seg_no
);
4024 if (retval
!= STATUS_SUCCESS
) {
4025 chip
->card_fail
|= MS_CARD
;
4026 rts51x_set_sense_type(chip
, lun
,
4027 SENSE_TYPE_MEDIA_NOT_PRESENT
);
4028 TRACE_RET(chip
, retval
);
4033 ms_get_l2p_tbl(chip
, seg_no
,
4034 log_blk
- ms_start_idx
[seg_no
]);
4035 if (old_blk
== 0xFFFF) {
4036 rts51x_ms_rw_fail(srb
, chip
);
4037 TRACE_RET(chip
, STATUS_FAIL
);
4040 if (srb
->sc_data_direction
== DMA_TO_DEVICE
) {
4041 new_blk
= ms_get_unused_block(chip
, seg_no
);
4042 if (new_blk
== 0xFFFF) {
4043 rts51x_ms_rw_fail(srb
, chip
);
4044 TRACE_RET(chip
, STATUS_FAIL
);
4048 RTS51X_DEBUGP("seg_no = %d, old_blk = 0x%x, new_blk = 0x%x\n",
4049 seg_no
, old_blk
, new_blk
);
4054 if (srb
->sc_data_direction
== DMA_TO_DEVICE
) {
4055 if (end_page
< (ms_card
->page_off
+ 1)) {
4056 delay_write
->delay_write_flag
= 1;
4057 delay_write
->old_phyblock
= old_blk
;
4058 delay_write
->new_phyblock
= new_blk
;
4059 delay_write
->logblock
= log_blk
;
4060 delay_write
->pageoff
= end_page
;
4064 scsi_set_resid(srb
, 0);
4066 return STATUS_SUCCESS
;
4069 int rts51x_ms_rw(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
, u32 start_sector
,
4072 struct ms_info
*ms_card
= &(chip
->ms_card
);
4075 if (CHK_MSPRO(ms_card
))
4077 mspro_rw_multi_sector(srb
, chip
, start_sector
, sector_cnt
);
4080 rts51x_ms_rw_multi_sector(srb
, chip
, start_sector
, sector_cnt
);
4085 void rts51x_ms_free_l2p_tbl(struct rts51x_chip
*chip
)
4087 struct ms_info
*ms_card
= &(chip
->ms_card
);
4090 if (ms_card
->segment
!= NULL
) {
4091 for (i
= 0; i
< ms_card
->segment_cnt
; i
++) {
4092 if (ms_card
->segment
[i
].l2p_table
!= NULL
) {
4093 vfree(ms_card
->segment
[i
].l2p_table
);
4094 ms_card
->segment
[i
].l2p_table
= NULL
;
4096 if (ms_card
->segment
[i
].free_table
!= NULL
) {
4097 vfree(ms_card
->segment
[i
].free_table
);
4098 ms_card
->segment
[i
].free_table
= NULL
;
4101 vfree(ms_card
->segment
);
4102 ms_card
->segment
= NULL
;
4106 void rts51x_ms_cleanup_work(struct rts51x_chip
*chip
)
4108 struct ms_info
*ms_card
= &(chip
->ms_card
);
4110 if (CHK_MSPRO(ms_card
)) {
4111 if (ms_card
->seq_mode
) {
4112 RTS51X_DEBUGP("MS Pro: stop transmission\n");
4113 mspro_stop_seq_mode(chip
);
4114 ms_card
->counter
= 0;
4116 if (CHK_MSHG(ms_card
)) {
4118 rts51x_read_register(chip
, MS_CFG
, &value
);
4119 if (value
& MS_2K_SECTOR_MODE
)
4120 rts51x_write_register(chip
, MS_CFG
,
4121 MS_2K_SECTOR_MODE
, 0x00);
4123 } else if ((!CHK_MSPRO(ms_card
))
4124 && ms_card
->delay_write
.delay_write_flag
) {
4125 RTS51X_DEBUGP("MS: delay write\n");
4126 rts51x_ms_delay_write(chip
);
4127 ms_card
->counter
= 0;
4131 static int ms_power_off_card3v3(struct rts51x_chip
*chip
)
4135 rts51x_init_cmd(chip
);
4137 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_CLK_EN
, MS_CLK_EN
, 0);
4138 if (chip
->asic_code
)
4139 ms_pull_ctl_disable(chip
);
4141 rts51x_add_cmd(chip
, WRITE_REG_CMD
, FPGA_PULL_CTL
,
4142 FPGA_MS_PULL_CTL_BIT
| 0x20,
4143 FPGA_MS_PULL_CTL_BIT
);
4144 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_OE
, MS_OUTPUT_EN
, 0);
4145 if (!chip
->option
.FT2_fast_mode
) {
4146 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PWR_CTL
, POWER_MASK
,
4150 retval
= rts51x_send_cmd(chip
, MODE_C
, 100);
4151 if (retval
!= STATUS_SUCCESS
)
4152 TRACE_RET(chip
, retval
);
4154 return STATUS_SUCCESS
;
4157 int rts51x_release_ms_card(struct rts51x_chip
*chip
)
4159 struct ms_info
*ms_card
= &(chip
->ms_card
);
4162 RTS51X_DEBUGP("rts51x_release_ms_card\n");
4164 ms_card
->delay_write
.delay_write_flag
= 0;
4165 ms_card
->pro_under_formatting
= 0;
4167 chip
->card_ready
&= ~MS_CARD
;
4168 chip
->card_fail
&= ~MS_CARD
;
4169 chip
->card_wp
&= ~MS_CARD
;
4171 rts51x_ms_free_l2p_tbl(chip
);
4173 rts51x_write_register(chip
, SFSM_ED
, HW_CMD_STOP
, HW_CMD_STOP
);
4175 memset(ms_card
->raw_sys_info
, 0, 96);
4176 #ifdef SUPPORT_PCGL_1P18
4177 memset(ms_card
->raw_model_name
, 0, 48);
4180 retval
= ms_power_off_card3v3(chip
);
4181 if (retval
!= STATUS_SUCCESS
)
4182 TRACE_RET(chip
, retval
);
4184 return STATUS_SUCCESS
;