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/vmalloc.h>
29 #include <linux/sched.h>
30 #include <linux/slab.h>
31 #include <linux/vmalloc.h>
36 #include "rts51x_transport.h"
37 #include "rts51x_scsi.h"
38 #include "rts51x_card.h"
41 static inline void ms_set_err_code(struct rts51x_chip
*chip
, u8 err_code
)
43 struct ms_info
*ms_card
= &(chip
->ms_card
);
45 ms_card
->err_code
= err_code
;
48 static inline int ms_check_err_code(struct rts51x_chip
*chip
, u8 err_code
)
50 struct ms_info
*ms_card
= &(chip
->ms_card
);
52 return (ms_card
->err_code
== err_code
);
55 static int ms_parse_err_code(struct rts51x_chip
*chip
)
57 TRACE_RET(chip
, STATUS_FAIL
);
60 static int ms_transfer_tpc(struct rts51x_chip
*chip
, u8 trans_mode
, u8 tpc
,
63 struct ms_info
*ms_card
= &(chip
->ms_card
);
66 RTS51X_DEBUGP("ms_transfer_tpc: tpc = 0x%x\n", tpc
);
68 rts51x_init_cmd(chip
);
70 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TPC
, 0xFF, tpc
);
71 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_BYTE_CNT
, 0xFF, cnt
);
72 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANS_CFG
, 0xFF, cfg
);
73 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
, 0x01,
76 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANSFER
, 0xFF,
77 MS_TRANSFER_START
| trans_mode
);
78 rts51x_add_cmd(chip
, CHECK_REG_CMD
, MS_TRANSFER
, MS_TRANSFER_END
,
81 rts51x_add_cmd(chip
, READ_REG_CMD
, MS_TRANS_CFG
, 0, 0);
83 retval
= rts51x_send_cmd(chip
, MODE_CR
, 100);
84 if (retval
!= STATUS_SUCCESS
)
85 TRACE_RET(chip
, retval
);
87 retval
= rts51x_get_rsp(chip
, 2, 5000);
88 if (CHECK_MS_TRANS_FAIL(chip
, retval
)) {
89 rts51x_clear_ms_error(chip
);
90 ms_set_err_code(chip
, MS_TO_ERROR
);
91 TRACE_RET(chip
, ms_parse_err_code(chip
));
94 if (!(tpc
& 0x08)) { /* Read Packet */
95 /* Check CRC16 & Ready Timeout */
96 if (chip
->rsp_buf
[1] & MS_CRC16_ERR
) {
97 ms_set_err_code(chip
, MS_CRC16_ERROR
);
98 TRACE_RET(chip
, ms_parse_err_code(chip
));
100 } else { /* Write Packet */
101 if (CHK_MSPRO(ms_card
) && !(chip
->rsp_buf
[1] & 0x80)) {
102 if (chip
->rsp_buf
[1] & (MS_INT_ERR
| MS_INT_CMDNK
)) {
103 ms_set_err_code(chip
, MS_CMD_NK
);
104 TRACE_RET(chip
, ms_parse_err_code(chip
));
109 /* Check Timeout of Ready Signal */
110 if (chip
->rsp_buf
[1] & MS_RDY_TIMEOUT
) {
111 rts51x_clear_ms_error(chip
);
112 ms_set_err_code(chip
, MS_TO_ERROR
);
113 TRACE_RET(chip
, ms_parse_err_code(chip
));
116 return STATUS_SUCCESS
;
119 int ms_transfer_data(struct rts51x_chip
*chip
, u8 trans_mode
, u8 tpc
,
120 u16 sec_cnt
, u8 cfg
, int mode_2k
, int use_sg
, void *buf
,
123 struct ms_info
*ms_card
= &(chip
->ms_card
);
125 u8 val
, err_code
= 0, flag
= 0;
126 enum dma_data_direction dir
;
129 if (!buf
|| !buf_len
)
130 TRACE_RET(chip
, STATUS_FAIL
);
132 if (trans_mode
== MS_TM_AUTO_READ
) {
133 pipe
= RCV_BULK_PIPE(chip
);
134 dir
= DMA_FROM_DEVICE
;
136 err_code
= MS_FLASH_READ_ERROR
;
137 } else if (trans_mode
== MS_TM_AUTO_WRITE
) {
138 pipe
= SND_BULK_PIPE(chip
);
141 err_code
= MS_FLASH_WRITE_ERROR
;
143 TRACE_RET(chip
, STATUS_FAIL
);
146 rts51x_init_cmd(chip
);
148 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TPC
, 0xFF, tpc
);
149 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_SECTOR_CNT_H
, 0xFF,
150 (u8
) (sec_cnt
>> 8));
151 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_SECTOR_CNT_L
, 0xFF,
153 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANS_CFG
, 0xFF, cfg
);
154 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
, 0x01,
158 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_CFG
, MS_2K_SECTOR_MODE
,
161 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_CFG
, MS_2K_SECTOR_MODE
,
164 trans_dma_enable(dir
, chip
, sec_cnt
* 512, DMA_512
);
166 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANSFER
, 0xFF,
167 MS_TRANSFER_START
| trans_mode
);
168 rts51x_add_cmd(chip
, CHECK_REG_CMD
, MS_TRANSFER
, MS_TRANSFER_END
,
171 retval
= rts51x_send_cmd(chip
, flag
| STAGE_MS_STATUS
, 100);
172 if (retval
!= STATUS_SUCCESS
)
173 TRACE_RET(chip
, retval
);
176 rts51x_transfer_data_rcc(chip
, pipe
, buf
, buf_len
, use_sg
, NULL
,
178 if (retval
!= STATUS_SUCCESS
) {
179 ms_set_err_code(chip
, err_code
);
180 rts51x_clear_ms_error(chip
);
181 TRACE_RET(chip
, retval
);
184 retval
= rts51x_get_rsp(chip
, 3, 15000);
185 if (CHECK_MS_TRANS_FAIL(chip
, retval
)) {
186 ms_set_err_code(chip
, err_code
);
187 rts51x_clear_ms_error(chip
);
188 TRACE_RET(chip
, STATUS_FAIL
);
191 ms_card
->last_rw_int
= val
= chip
->rsp_buf
[1];
192 if (val
& (MS_INT_CMDNK
| MS_INT_ERR
| MS_CRC16_ERR
| MS_RDY_TIMEOUT
))
193 TRACE_RET(chip
, STATUS_FAIL
);
195 return STATUS_SUCCESS
;
198 int ms_write_bytes(struct rts51x_chip
*chip
, u8 tpc
, u8 cnt
, u8 cfg
, u8
*data
,
201 struct ms_info
*ms_card
= &(chip
->ms_card
);
204 if (!data
|| (data_len
< cnt
))
205 TRACE_RET(chip
, STATUS_ERROR
);
207 rts51x_init_cmd(chip
);
209 for (i
= 0; i
< cnt
; i
++) {
210 rts51x_add_cmd(chip
, WRITE_REG_CMD
, PPBUF_BASE2
+ i
, 0xFF,
214 rts51x_add_cmd(chip
, WRITE_REG_CMD
, PPBUF_BASE2
+ i
,
217 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TPC
, 0xFF, tpc
);
218 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_BYTE_CNT
, 0xFF, cnt
);
219 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANS_CFG
, 0xFF, cfg
);
220 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
, 0x01,
223 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANSFER
, 0xFF,
224 MS_TRANSFER_START
| MS_TM_WRITE_BYTES
);
225 rts51x_add_cmd(chip
, CHECK_REG_CMD
, MS_TRANSFER
, MS_TRANSFER_END
,
228 retval
= rts51x_send_cmd(chip
, MODE_CR
, 100);
229 if (retval
!= STATUS_SUCCESS
)
230 TRACE_RET(chip
, retval
);
232 retval
= rts51x_get_rsp(chip
, 1, 5000);
233 if (CHECK_MS_TRANS_FAIL(chip
, retval
)) {
236 rts51x_ep0_read_register(chip
, MS_TRANS_CFG
, &val
);
237 RTS51X_DEBUGP("MS_TRANS_CFG: 0x%02x\n", val
);
239 rts51x_clear_ms_error(chip
);
241 if (!(tpc
& 0x08)) { /* Read Packet */
242 /* Check CRC16 & Ready Timeout */
243 if (val
& MS_CRC16_ERR
) {
244 ms_set_err_code(chip
, MS_CRC16_ERROR
);
245 TRACE_RET(chip
, ms_parse_err_code(chip
));
247 } else { /* Write Packet */
248 if (CHK_MSPRO(ms_card
) && !(val
& 0x80)) {
249 if (val
& (MS_INT_ERR
| MS_INT_CMDNK
)) {
250 ms_set_err_code(chip
, MS_CMD_NK
);
252 ms_parse_err_code(chip
));
257 /* Check Timeout of Ready Signal */
258 if (val
& MS_RDY_TIMEOUT
) {
259 ms_set_err_code(chip
, MS_TO_ERROR
);
260 TRACE_RET(chip
, ms_parse_err_code(chip
));
263 ms_set_err_code(chip
, MS_TO_ERROR
);
264 TRACE_RET(chip
, ms_parse_err_code(chip
));
267 return STATUS_SUCCESS
;
270 int ms_read_bytes(struct rts51x_chip
*chip
, u8 tpc
, u8 cnt
, u8 cfg
, u8
*data
,
273 struct ms_info
*ms_card
= &(chip
->ms_card
);
277 TRACE_RET(chip
, STATUS_ERROR
);
279 rts51x_init_cmd(chip
);
281 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TPC
, 0xFF, tpc
);
282 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_BYTE_CNT
, 0xFF, cnt
);
283 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANS_CFG
, 0xFF, cfg
);
284 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
, 0x01,
287 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANSFER
, 0xFF,
288 MS_TRANSFER_START
| MS_TM_READ_BYTES
);
289 rts51x_add_cmd(chip
, CHECK_REG_CMD
, MS_TRANSFER
, MS_TRANSFER_END
,
292 for (i
= 0; i
< data_len
- 1; i
++)
293 rts51x_add_cmd(chip
, READ_REG_CMD
, PPBUF_BASE2
+ i
, 0, 0);
296 rts51x_add_cmd(chip
, READ_REG_CMD
, PPBUF_BASE2
+ data_len
, 0,
299 rts51x_add_cmd(chip
, READ_REG_CMD
, PPBUF_BASE2
+ data_len
- 1,
302 retval
= rts51x_send_cmd(chip
, MODE_CR
, 100);
303 if (retval
!= STATUS_SUCCESS
)
304 TRACE_RET(chip
, retval
);
306 retval
= rts51x_get_rsp(chip
, data_len
+ 1, 5000);
307 if (CHECK_MS_TRANS_FAIL(chip
, retval
)) {
310 rts51x_ep0_read_register(chip
, MS_TRANS_CFG
, &val
);
311 RTS51X_DEBUGP("MS_TRANS_CFG: 0x%02x\n", val
);
313 rts51x_clear_ms_error(chip
);
315 if (!(tpc
& 0x08)) { /* Read Packet */
316 /* Check CRC16 & Ready Timeout */
317 if (val
& MS_CRC16_ERR
) {
318 ms_set_err_code(chip
, MS_CRC16_ERROR
);
319 TRACE_RET(chip
, ms_parse_err_code(chip
));
321 } else { /* Write Packet */
322 if (CHK_MSPRO(ms_card
) && !(val
& 0x80)) {
323 if (val
& (MS_INT_ERR
| MS_INT_CMDNK
)) {
324 ms_set_err_code(chip
, MS_CMD_NK
);
326 ms_parse_err_code(chip
));
331 /* Check Timeout of Ready Signal */
332 if (val
& MS_RDY_TIMEOUT
) {
333 ms_set_err_code(chip
, MS_TO_ERROR
);
334 TRACE_RET(chip
, ms_parse_err_code(chip
));
337 ms_set_err_code(chip
, MS_TO_ERROR
);
338 TRACE_RET(chip
, ms_parse_err_code(chip
));
341 rts51x_read_rsp_buf(chip
, 1, data
, data_len
);
343 return STATUS_SUCCESS
;
346 int ms_set_rw_reg_addr(struct rts51x_chip
*chip
,
347 u8 read_start
, u8 read_cnt
, u8 write_start
, u8 write_cnt
)
352 data
[0] = read_start
;
354 data
[2] = write_start
;
357 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
359 ms_write_bytes(chip
, SET_RW_REG_ADRS
, 4, NO_WAIT_INT
, data
,
361 if (retval
== STATUS_SUCCESS
)
362 return STATUS_SUCCESS
;
363 rts51x_clear_ms_error(chip
);
366 TRACE_RET(chip
, STATUS_FAIL
);
369 static int ms_send_cmd(struct rts51x_chip
*chip
, u8 cmd
, u8 cfg
)
376 return ms_write_bytes(chip
, PRO_SET_CMD
, 1, cfg
, data
, 1);
379 static int ms_set_cmd(struct rts51x_chip
*chip
,
380 u8 read_start
, u8 read_count
,
381 u8 write_start
, u8 write_count
,
382 u8 cmd
, u8 cfg
, u8
*data
, int data_len
, u8
*int_stat
)
387 if (!data
|| (data_len
<= 0) || (data_len
> 128)) {
388 RTS51X_DEBUGP("ms_set_cmd (data_len = %d)\n", data_len
);
389 TRACE_RET(chip
, STATUS_FAIL
);
393 ms_set_rw_reg_addr(chip
, read_start
, read_count
, write_start
,
395 if (retval
!= STATUS_SUCCESS
)
396 TRACE_RET(chip
, retval
);
398 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
400 ms_write_bytes(chip
, WRITE_REG
, write_count
, NO_WAIT_INT
,
402 if (retval
== STATUS_SUCCESS
)
405 if (i
== MS_MAX_RETRY_COUNT
)
406 TRACE_RET(chip
, STATUS_FAIL
);
408 ms_set_err_code(chip
, MS_NO_ERROR
);
410 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
411 retval
= ms_send_cmd(chip
, cmd
, WAIT_INT
);
412 if (retval
== STATUS_SUCCESS
)
415 if (i
== MS_MAX_RETRY_COUNT
)
416 TRACE_RET(chip
, STATUS_FAIL
);
417 /* GET_INT Register */
418 ms_set_err_code(chip
, MS_NO_ERROR
);
419 retval
= ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
, 1);
420 if (retval
!= STATUS_SUCCESS
)
421 TRACE_RET(chip
, retval
);
426 return STATUS_SUCCESS
;
430 static int ms_auto_set_cmd(struct rts51x_chip
*chip
,
431 u8 read_start
, u8 read_count
,
432 u8 write_start
, u8 write_count
,
433 u8 cmd
, u8 cfg
, u8
*data
, int data_len
,
439 if (!data
|| (data_len
<= 0) || (data_len
> 128)) {
440 RTS51X_DEBUGP("ms_auto_set_cmd (data_len = %d)\n", data_len
);
441 TRACE_RET(chip
, STATUS_FAIL
);
444 rts51x_init_cmd(chip
);
446 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_READ_START
, 0xFF, read_start
);
447 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_READ_COUNT
, 0xFF, read_count
);
448 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_WRITE_START
, 0xFF, write_start
);
449 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_WRITE_COUNT
, 0xFF, write_count
);
450 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_COMMAND
, 0xFF, cmd
);
452 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANS_CFG
, 0xFF, cfg
);
453 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
, 0x01,
456 for (i
= 0; i
< data_len
; i
++) {
457 rts51x_add_cmd(chip
, WRITE_REG_CMD
, PPBUF_BASE2
+ i
, 0xFF,
461 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANSFER
, 0xFF,
462 MS_TRANSFER_START
| MS_TM_SET_CMD
);
463 rts51x_add_cmd(chip
, CHECK_REG_CMD
, MS_TRANSFER
, MS_TRANSFER_END
,
466 retval
= rts51x_send_cmd(chip
, MODE_CR
| STAGE_MS_STATUS
, 100);
467 if (retval
!= STATUS_SUCCESS
)
468 TRACE_RET(chip
, retval
);
470 retval
= rts51x_get_rsp(chip
, 3, 5000);
472 if (CHECK_MS_TRANS_FAIL(chip
, retval
)) {
473 rts51x_clear_ms_error(chip
);
474 TRACE_RET(chip
, STATUS_FAIL
);
478 *int_stat
= chip
->rsp_buf
[2];
480 return STATUS_SUCCESS
;
484 static int ms_set_init_para(struct rts51x_chip
*chip
)
486 struct ms_info
*ms_card
= &(chip
->ms_card
);
489 if (CHK_HG8BIT(ms_card
)) {
491 ms_card
->ms_clock
= chip
->option
.asic_ms_hg_clk
;
493 ms_card
->ms_clock
= chip
->option
.fpga_ms_hg_clk
;
494 } else if (CHK_MSPRO(ms_card
) || CHK_MS4BIT(ms_card
)) {
496 ms_card
->ms_clock
= chip
->option
.asic_ms_4bit_clk
;
498 ms_card
->ms_clock
= chip
->option
.fpga_ms_4bit_clk
;
501 ms_card
->ms_clock
= 38;
503 ms_card
->ms_clock
= CLK_40
;
506 retval
= switch_clock(chip
, ms_card
->ms_clock
);
507 if (retval
!= STATUS_SUCCESS
)
508 TRACE_RET(chip
, retval
);
510 retval
= rts51x_select_card(chip
, MS_CARD
);
511 if (retval
!= STATUS_SUCCESS
)
512 TRACE_RET(chip
, retval
);
514 return STATUS_SUCCESS
;
517 int ms_switch_clock(struct rts51x_chip
*chip
)
519 struct ms_info
*ms_card
= &(chip
->ms_card
);
522 retval
= rts51x_select_card(chip
, MS_CARD
);
523 if (retval
!= STATUS_SUCCESS
)
524 TRACE_RET(chip
, retval
);
526 retval
= switch_clock(chip
, ms_card
->ms_clock
);
527 if (retval
!= STATUS_SUCCESS
)
528 TRACE_RET(chip
, retval
);
530 return STATUS_SUCCESS
;
533 static void ms_pull_ctl_disable(struct rts51x_chip
*chip
)
535 if (CHECK_PKG(chip
, LQFP48
)) {
536 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL1
, 0xFF, 0x55);
537 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL2
, 0xFF, 0x55);
538 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL3
, 0xFF, 0x95);
539 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL4
, 0xFF, 0x55);
540 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL5
, 0xFF, 0x55);
541 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL6
, 0xFF, 0xA5);
543 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL1
, 0xFF, 0x65);
544 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL2
, 0xFF, 0x55);
545 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL3
, 0xFF, 0x95);
546 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL4
, 0xFF, 0x55);
547 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL5
, 0xFF, 0x56);
548 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL6
, 0xFF, 0x59);
552 static void ms_pull_ctl_enable(struct rts51x_chip
*chip
)
554 if (CHECK_PKG(chip
, LQFP48
)) {
555 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL1
, 0xFF, 0x55);
556 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL2
, 0xFF, 0x55);
557 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL3
, 0xFF, 0x95);
558 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL4
, 0xFF, 0x55);
559 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL5
, 0xFF, 0x55);
560 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL6
, 0xFF, 0xA5);
562 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL1
, 0xFF, 0x65);
563 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL2
, 0xFF, 0x55);
564 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL3
, 0xFF, 0x95);
565 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL4
, 0xFF, 0x55);
566 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL5
, 0xFF, 0x55);
567 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL6
, 0xFF, 0x59);
571 static int ms_prepare_reset(struct rts51x_chip
*chip
)
573 struct ms_info
*ms_card
= &(chip
->ms_card
);
576 ms_card
->ms_type
= 0;
577 ms_card
->check_ms_flow
= 0;
578 ms_card
->switch_8bit_fail
= 0;
579 ms_card
->delay_write
.delay_write_flag
= 0;
581 ms_card
->pro_under_formatting
= 0;
583 rts51x_init_cmd(chip
);
585 if (chip
->asic_code
) {
586 ms_pull_ctl_enable(chip
);
588 rts51x_add_cmd(chip
, WRITE_REG_CMD
, FPGA_PULL_CTL
,
589 FPGA_MS_PULL_CTL_BIT
| 0x20, 0);
591 /* Tri-state MS output */
592 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_OE
, MS_OUTPUT_EN
, 0);
594 if (!chip
->option
.FT2_fast_mode
) {
595 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PWR_CTL
, POWER_MASK
,
599 retval
= rts51x_send_cmd(chip
, MODE_C
, 100);
600 if (retval
!= STATUS_SUCCESS
)
601 TRACE_RET(chip
, retval
);
603 if (!chip
->option
.FT2_fast_mode
) {
606 card_power_on(chip
, MS_CARD
);
610 rts51x_get_card_status(chip
, &(chip
->card_status
));
612 chip
->ocp_stat
= (chip
->card_status
>> 4) & 0x03;
614 if (chip
->ocp_stat
& (MS_OCP_NOW
| MS_OCP_EVER
)) {
615 RTS51X_DEBUGP("Over current, OCPSTAT is 0x%x\n",
617 TRACE_RET(chip
, STATUS_FAIL
);
622 rts51x_init_cmd(chip
);
624 /* Enable MS Output */
625 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_OE
, MS_OUTPUT_EN
,
628 /* Reset Registers */
630 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_CFG
, 0xFF,
631 SAMPLE_TIME_RISING
| PUSH_TIME_DEFAULT
|
632 NO_EXTEND_TOGGLE
| MS_BUS_WIDTH_1
);
634 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_CFG
, 0xFF,
635 SAMPLE_TIME_FALLING
| PUSH_TIME_DEFAULT
|
636 NO_EXTEND_TOGGLE
| MS_BUS_WIDTH_1
);
637 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANS_CFG
, 0xFF,
638 NO_WAIT_INT
| NO_AUTO_READ_INT_REG
);
640 retval
= rts51x_send_cmd(chip
, MODE_C
, 100);
641 if (retval
!= STATUS_SUCCESS
)
642 TRACE_RET(chip
, retval
);
644 return ms_set_init_para(chip
);
647 static int ms_identify_media_type(struct rts51x_chip
*chip
, int switch_8bit_bus
)
649 struct ms_info
*ms_card
= &(chip
->ms_card
);
653 retval
= ms_set_rw_reg_addr(chip
, Pro_StatusReg
, 6, SystemParm
, 1);
654 if (retval
!= STATUS_SUCCESS
)
655 TRACE_RET(chip
, retval
);
657 /* Get Register form MS-PRO card */
658 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
660 ms_transfer_tpc(chip
, MS_TM_READ_BYTES
, READ_REG
, 6,
662 if (retval
== STATUS_SUCCESS
)
665 if (i
== MS_MAX_RETRY_COUNT
)
666 TRACE_RET(chip
, STATUS_FAIL
);
668 RTS51X_READ_REG(chip
, PPBUF_BASE2
+ 2, &val
);
669 RTS51X_DEBUGP("Type register: 0x%x\n", val
);
672 ms_card
->check_ms_flow
= 1;
673 TRACE_RET(chip
, STATUS_FAIL
);
675 /* Category Register */
676 RTS51X_READ_REG(chip
, PPBUF_BASE2
+ 4, &val
);
677 RTS51X_DEBUGP("Category register: 0x%x\n", val
);
679 ms_card
->check_ms_flow
= 1;
680 TRACE_RET(chip
, STATUS_FAIL
);
683 RTS51X_READ_REG(chip
, PPBUF_BASE2
+ 5, &val
);
684 RTS51X_DEBUGP("Class register: 0x%x\n", val
);
686 RTS51X_READ_REG(chip
, PPBUF_BASE2
, &val
);
688 chip
->card_wp
|= MS_CARD
;
690 chip
->card_wp
&= ~MS_CARD
;
691 } else if ((val
== 0x01) || (val
== 0x02) || (val
== 0x03)) {
692 chip
->card_wp
|= MS_CARD
;
694 ms_card
->check_ms_flow
= 1;
695 TRACE_RET(chip
, STATUS_FAIL
);
698 ms_card
->ms_type
|= TYPE_MSPRO
;
700 /* Check MSPro-HG Card, use IF Mode Register to distinguish */
701 RTS51X_READ_REG(chip
, PPBUF_BASE2
+ 3, &val
);
702 RTS51X_DEBUGP("IF Mode register: 0x%x\n", val
);
704 ms_card
->ms_type
&= 0x0F;
705 } else if (val
== 7) {
707 ms_card
->ms_type
|= MS_HG
;
709 ms_card
->ms_type
&= 0x0F;
711 TRACE_RET(chip
, STATUS_FAIL
);
714 /* end Procedure to identify Media Type */
715 return STATUS_SUCCESS
;
718 static int ms_confirm_cpu_startup(struct rts51x_chip
*chip
)
723 /* Confirm CPU StartUp */
726 if (monitor_card_cd(chip
, MS_CARD
) == CD_NOT_EXIST
)
727 TRACE_RET(chip
, STATUS_FAIL
);
729 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
731 ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
,
733 if (retval
== STATUS_SUCCESS
)
736 if (i
== MS_MAX_RETRY_COUNT
)
737 TRACE_RET(chip
, STATUS_FAIL
);
740 TRACE_RET(chip
, STATUS_FAIL
);
743 } while (!(val
& INT_REG_CED
));
745 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
746 retval
= ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
, 1);
747 if (retval
== STATUS_SUCCESS
)
750 if (i
== MS_MAX_RETRY_COUNT
)
751 TRACE_RET(chip
, STATUS_FAIL
);
753 if (val
& INT_REG_ERR
) {
754 if (val
& INT_REG_CMDNK
) { /* CMDNK = 1 */
755 chip
->card_wp
|= (MS_CARD
);
756 } else { /* CMDNK = 0 */
757 TRACE_RET(chip
, STATUS_FAIL
);
760 /*-- end confirm CPU startup */
762 return STATUS_SUCCESS
;
765 static int ms_switch_parallel_bus(struct rts51x_chip
*chip
)
770 data
[0] = PARALLEL_4BIT_IF
;
772 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
774 ms_write_bytes(chip
, WRITE_REG
, 1, NO_WAIT_INT
, data
, 2);
775 if (retval
== STATUS_SUCCESS
)
778 if (retval
!= STATUS_SUCCESS
)
779 TRACE_RET(chip
, retval
);
781 return STATUS_SUCCESS
;
784 static int ms_switch_8bit_bus(struct rts51x_chip
*chip
)
786 struct ms_info
*ms_card
= &(chip
->ms_card
);
790 data
[0] = PARALLEL_8BIT_IF
;
792 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
794 ms_write_bytes(chip
, WRITE_REG
, 1, NO_WAIT_INT
, data
, 2);
795 if (retval
== STATUS_SUCCESS
)
798 if (retval
!= STATUS_SUCCESS
)
799 TRACE_RET(chip
, STATUS_FAIL
);
801 RTS51X_WRITE_REG(chip
, MS_CFG
, 0x98,
802 MS_BUS_WIDTH_8
| SAMPLE_TIME_FALLING
);
803 ms_card
->ms_type
|= MS_8BIT
;
805 retval
= ms_set_init_para(chip
);
806 if (retval
!= STATUS_SUCCESS
)
807 TRACE_RET(chip
, retval
);
809 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
811 ms_transfer_tpc(chip
, MS_TM_READ_BYTES
, GET_INT
, 1,
813 if (retval
!= STATUS_SUCCESS
)
814 TRACE_RET(chip
, retval
);
817 return STATUS_SUCCESS
;
820 static int ms_pro_reset_flow(struct rts51x_chip
*chip
, int switch_8bit_bus
)
822 struct ms_info
*ms_card
= &(chip
->ms_card
);
825 for (i
= 0; i
< 3; i
++) {
826 retval
= ms_prepare_reset(chip
);
827 if (retval
!= STATUS_SUCCESS
)
828 TRACE_RET(chip
, retval
);
830 retval
= ms_identify_media_type(chip
, switch_8bit_bus
);
831 if (retval
!= STATUS_SUCCESS
)
832 TRACE_RET(chip
, retval
);
834 retval
= ms_confirm_cpu_startup(chip
);
835 if (retval
!= STATUS_SUCCESS
)
836 TRACE_RET(chip
, retval
);
838 retval
= ms_switch_parallel_bus(chip
);
839 if (retval
!= STATUS_SUCCESS
) {
840 if (monitor_card_cd(chip
, MS_CARD
) == CD_NOT_EXIST
)
841 TRACE_RET(chip
, STATUS_FAIL
);
848 if (retval
!= STATUS_SUCCESS
)
849 TRACE_RET(chip
, retval
);
851 RTS51X_WRITE_REG(chip
, MS_CFG
, 0x18, MS_BUS_WIDTH_4
);
853 RTS51X_WRITE_REG(chip
, MS_CFG
, PUSH_TIME_ODD
, PUSH_TIME_ODD
);
855 retval
= ms_set_init_para(chip
);
856 if (retval
!= STATUS_SUCCESS
)
857 TRACE_RET(chip
, retval
);
859 if (CHK_MSHG(ms_card
) && switch_8bit_bus
) {
860 retval
= ms_switch_8bit_bus(chip
);
861 if (retval
!= STATUS_SUCCESS
) {
862 ms_card
->switch_8bit_fail
= 1;
863 TRACE_RET(chip
, retval
);
867 return STATUS_SUCCESS
;
871 static int msxc_change_power(struct rts51x_chip
*chip
, u8 mode
)
876 ms_cleanup_work(chip
);
878 /* Set Parameter Register */
879 retval
= ms_set_rw_reg_addr(chip
, 0, 0, Pro_DataCount1
, 6);
880 if (retval
!= STATUS_SUCCESS
)
881 TRACE_RET(chip
, retval
);
890 retval
= ms_write_bytes(chip
, PRO_WRITE_REG
, 6, NO_WAIT_INT
, buf
, 6);
891 if (retval
!= STATUS_SUCCESS
)
892 TRACE_RET(chip
, retval
);
894 retval
= ms_send_cmd(chip
, XC_CHG_POWER
, WAIT_INT
);
895 if (retval
!= STATUS_SUCCESS
)
896 TRACE_RET(chip
, retval
);
898 RTS51X_READ_REG(chip
, MS_TRANS_CFG
, buf
);
899 if (buf
[0] & (MS_INT_CMDNK
| MS_INT_ERR
))
900 TRACE_RET(chip
, STATUS_FAIL
);
902 return STATUS_SUCCESS
;
906 static int ms_read_attribute_info(struct rts51x_chip
*chip
)
908 struct ms_info
*ms_card
= &(chip
->ms_card
);
910 u8 val
, *buf
, class_code
, device_type
, sub_class
, data
[16];
911 u16 total_blk
= 0, blk_size
= 0;
913 u32 xc_total_blk
= 0, xc_blk_size
= 0;
915 u32 sys_info_addr
= 0, sys_info_size
;
916 #ifdef SUPPORT_PCGL_1P18
917 u32 model_name_addr
= 0, model_name_size
;
918 int found_sys_info
= 0, found_model_name
= 0;
921 retval
= ms_set_rw_reg_addr(chip
, Pro_IntReg
, 2, Pro_SystemParm
, 7);
922 if (retval
!= STATUS_SUCCESS
)
923 TRACE_RET(chip
, retval
);
925 if (CHK_MS8BIT(ms_card
))
926 data
[0] = PARALLEL_8BIT_IF
;
928 data
[0] = PARALLEL_4BIT_IF
;
935 /* Start address 0 */
939 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
941 ms_write_bytes(chip
, PRO_WRITE_REG
, 7, NO_WAIT_INT
, data
,
943 if (retval
== STATUS_SUCCESS
)
946 if (retval
!= STATUS_SUCCESS
)
947 TRACE_RET(chip
, retval
);
949 buf
= kmalloc(64 * 512, GFP_KERNEL
);
951 TRACE_RET(chip
, STATUS_NOMEM
);
953 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
954 retval
= ms_send_cmd(chip
, PRO_READ_ATRB
, WAIT_INT
);
955 if (retval
!= STATUS_SUCCESS
)
958 retval
= rts51x_read_register(chip
, MS_TRANS_CFG
, &val
);
959 if (retval
!= STATUS_SUCCESS
) {
961 TRACE_RET(chip
, STATUS_FAIL
);
963 if (!(val
& MS_INT_BREQ
)) {
965 TRACE_RET(chip
, STATUS_FAIL
);
969 ms_transfer_data(chip
, MS_TM_AUTO_READ
, PRO_READ_LONG_DATA
,
970 0x40, WAIT_INT
, 0, 0, buf
, 64 * 512);
971 if (retval
== STATUS_SUCCESS
)
974 rts51x_clear_ms_error(chip
);
976 if (retval
!= STATUS_SUCCESS
) {
978 TRACE_RET(chip
, retval
);
983 retval
= rts51x_read_register(chip
, MS_TRANS_CFG
, &val
);
984 if (retval
!= STATUS_SUCCESS
) {
986 TRACE_RET(chip
, retval
);
989 if ((val
& MS_INT_CED
) || !(val
& MS_INT_BREQ
))
993 ms_transfer_tpc(chip
, MS_TM_NORMAL_READ
, PRO_READ_LONG_DATA
,
995 if (retval
!= STATUS_SUCCESS
) {
997 TRACE_RET(chip
, retval
);
1003 if (retval
!= STATUS_SUCCESS
) {
1005 TRACE_RET(chip
, retval
);
1008 if ((buf
[0] != 0xa5) && (buf
[1] != 0xc3)) {
1009 /* Signature code is wrong */
1011 TRACE_RET(chip
, STATUS_FAIL
);
1014 if ((buf
[4] < 1) || (buf
[4] > 12)) {
1016 TRACE_RET(chip
, STATUS_FAIL
);
1019 for (i
= 0; i
< buf
[4]; i
++) {
1020 int cur_addr_off
= 16 + i
* 12;
1023 if ((buf
[cur_addr_off
+ 8] == 0x10)
1024 || (buf
[cur_addr_off
+ 8] == 0x13)) {
1026 if (buf
[cur_addr_off
+ 8] == 0x10) {
1028 sys_info_addr
= ((u32
) buf
[cur_addr_off
+ 0] << 24) |
1029 ((u32
) buf
[cur_addr_off
+ 1] << 16) |
1030 ((u32
) buf
[cur_addr_off
+ 2] << 8) |
1031 buf
[cur_addr_off
+ 3];
1033 ((u32
) buf
[cur_addr_off
+ 4] << 24) |
1034 ((u32
) buf
[cur_addr_off
+ 5] << 16) |
1035 ((u32
) buf
[cur_addr_off
+ 6] << 8) |
1036 buf
[cur_addr_off
+ 7];
1037 RTS51X_DEBUGP("sys_info_addr = 0x%x,"
1038 "sys_info_size = 0x%x\n",
1039 sys_info_addr
, sys_info_size
);
1040 if (sys_info_size
!= 96) {
1042 TRACE_RET(chip
, STATUS_FAIL
);
1044 if (sys_info_addr
< 0x1A0) {
1046 TRACE_RET(chip
, STATUS_FAIL
);
1048 if ((sys_info_size
+ sys_info_addr
) > 0x8000) {
1050 TRACE_RET(chip
, STATUS_FAIL
);
1053 if (buf
[cur_addr_off
+ 8] == 0x13)
1054 ms_card
->ms_type
|= MS_XC
;
1056 #ifdef SUPPORT_PCGL_1P18
1062 #ifdef SUPPORT_PCGL_1P18
1063 if (buf
[cur_addr_off
+ 8] == 0x15) {
1064 model_name_addr
= ((u32
) buf
[cur_addr_off
+ 0] << 24) |
1065 ((u32
) buf
[cur_addr_off
+ 1] << 16) |
1066 ((u32
) buf
[cur_addr_off
+ 2] << 8) |
1067 buf
[cur_addr_off
+ 3];
1069 ((u32
) buf
[cur_addr_off
+ 4] << 24) |
1070 ((u32
) buf
[cur_addr_off
+ 5] << 16) |
1071 ((u32
) buf
[cur_addr_off
+ 6] << 8) |
1072 buf
[cur_addr_off
+ 7];
1073 RTS51X_DEBUGP("model_name_addr = 0x%x,"
1074 "model_name_size = 0x%x\n",
1075 model_name_addr
, model_name_size
);
1076 if (model_name_size
!= 48) {
1078 TRACE_RET(chip
, STATUS_FAIL
);
1080 if (model_name_addr
< 0x1A0) {
1082 TRACE_RET(chip
, STATUS_FAIL
);
1084 if ((model_name_size
+ model_name_addr
) > 0x8000) {
1086 TRACE_RET(chip
, STATUS_FAIL
);
1089 found_model_name
= 1;
1092 if (found_sys_info
&& found_model_name
)
1099 TRACE_RET(chip
, STATUS_FAIL
);
1102 class_code
= buf
[sys_info_addr
+ 0];
1103 device_type
= buf
[sys_info_addr
+ 56];
1104 sub_class
= buf
[sys_info_addr
+ 46];
1106 if (CHK_MSXC(ms_card
)) {
1107 xc_total_blk
= ((u32
) buf
[sys_info_addr
+ 6] << 24) |
1108 ((u32
) buf
[sys_info_addr
+ 7] << 16) |
1109 ((u32
) buf
[sys_info_addr
+ 8] << 8) |
1110 buf
[sys_info_addr
+ 9];
1111 xc_blk_size
= ((u32
) buf
[sys_info_addr
+ 32] << 24) |
1112 ((u32
) buf
[sys_info_addr
+ 33] << 16) |
1113 ((u32
) buf
[sys_info_addr
+ 34] << 8) |
1114 buf
[sys_info_addr
+ 35];
1115 RTS51X_DEBUGP("xc_total_blk = 0x%x, xc_blk_size = 0x%x\n",
1116 xc_total_blk
, xc_blk_size
);
1119 ((u16
) buf
[sys_info_addr
+ 6] << 8) | buf
[sys_info_addr
+
1122 ((u16
) buf
[sys_info_addr
+ 2] << 8) | buf
[sys_info_addr
+
1124 RTS51X_DEBUGP("total_blk = 0x%x, blk_size = 0x%x\n", total_blk
,
1129 ((u16
) buf
[sys_info_addr
+ 6] << 8) | buf
[sys_info_addr
+ 7];
1130 blk_size
= ((u16
) buf
[sys_info_addr
+ 2] << 8) | buf
[sys_info_addr
+ 3];
1131 RTS51X_DEBUGP("total_blk = 0x%x, blk_size = 0x%x\n", total_blk
,
1135 RTS51X_DEBUGP("class_code = 0x%x, device_type = 0x%x,"
1136 "sub_class = 0x%x\n",
1137 class_code
, device_type
, sub_class
);
1139 memcpy(ms_card
->raw_sys_info
, buf
+ sys_info_addr
, 96);
1140 #ifdef SUPPORT_PCGL_1P18
1141 memcpy(ms_card
->raw_model_name
, buf
+ model_name_addr
, 48);
1146 /* Confirm System Information */
1148 if (CHK_MSXC(ms_card
)) {
1149 if (class_code
!= 0x03)
1150 TRACE_RET(chip
, STATUS_FAIL
);
1152 if (class_code
!= 0x02)
1153 TRACE_RET(chip
, STATUS_FAIL
);
1156 if (class_code
!= 0x02)
1157 TRACE_RET(chip
, STATUS_FAIL
);
1160 if (device_type
!= 0x00) {
1161 if ((device_type
== 0x01) || (device_type
== 0x02)
1162 || (device_type
== 0x03))
1163 chip
->card_wp
|= MS_CARD
;
1165 TRACE_RET(chip
, STATUS_FAIL
);
1167 if (sub_class
& 0xC0)
1168 TRACE_RET(chip
, STATUS_FAIL
);
1170 RTS51X_DEBUGP("class_code: 0x%x, device_type: 0x%x, sub_class: 0x%x\n",
1171 class_code
, device_type
, sub_class
);
1174 if (CHK_MSXC(ms_card
)) {
1175 chip
->capacity
[chip
->card2lun
[MS_CARD
]] = ms_card
->capacity
=
1176 xc_total_blk
* xc_blk_size
;
1178 chip
->capacity
[chip
->card2lun
[MS_CARD
]] = ms_card
->capacity
=
1179 total_blk
* blk_size
;
1182 chip
->capacity
[chip
->card2lun
[MS_CARD
]] = ms_card
->capacity
=
1183 total_blk
* blk_size
;
1186 return STATUS_SUCCESS
;
1189 #ifdef SUPPORT_MAGIC_GATE
1190 int mg_set_tpc_para_sub(struct rts51x_chip
*chip
, int type
, u8 mg_entry_num
);
1193 static int reset_ms_pro(struct rts51x_chip
*chip
)
1195 struct ms_info
*ms_card
= &(chip
->ms_card
);
1197 #ifdef XC_POWERCLASS
1198 u8 change_power_class
= 2;
1201 #ifdef XC_POWERCLASS
1204 retval
= ms_pro_reset_flow(chip
, 1);
1205 if (retval
!= STATUS_SUCCESS
) {
1206 if (ms_card
->switch_8bit_fail
) {
1207 retval
= ms_pro_reset_flow(chip
, 0);
1208 if (retval
!= STATUS_SUCCESS
)
1209 TRACE_RET(chip
, retval
);
1211 TRACE_RET(chip
, retval
);
1215 retval
= ms_read_attribute_info(chip
);
1216 if (retval
!= STATUS_SUCCESS
)
1217 TRACE_RET(chip
, retval
);
1218 #ifdef XC_POWERCLASS
1219 if (CHK_HG8BIT(ms_card
))
1220 change_power_class
= 0;
1222 if (change_power_class
&& CHK_MSXC(ms_card
)) {
1223 u8 power_class_mode
= (ms_card
->raw_sys_info
[46] & 0x18) >> 3;
1224 RTS51X_DEBUGP("power_class_mode = 0x%x", power_class_mode
);
1225 if (change_power_class
> power_class_mode
)
1226 change_power_class
= power_class_mode
;
1227 if (change_power_class
) {
1228 retval
= msxc_change_power(chip
, change_power_class
);
1229 if (retval
!= STATUS_SUCCESS
) {
1230 change_power_class
--;
1237 #ifdef SUPPORT_MAGIC_GATE
1238 retval
= mg_set_tpc_para_sub(chip
, 0, 0);
1239 if (retval
!= STATUS_SUCCESS
)
1240 TRACE_RET(chip
, retval
);
1243 if (CHK_HG8BIT(ms_card
))
1244 chip
->card_bus_width
[chip
->card2lun
[MS_CARD
]] = 8;
1246 chip
->card_bus_width
[chip
->card2lun
[MS_CARD
]] = 4;
1248 return STATUS_SUCCESS
;
1251 static int ms_read_status_reg(struct rts51x_chip
*chip
)
1256 retval
= ms_set_rw_reg_addr(chip
, StatusReg0
, 2, 0, 0);
1257 if (retval
!= STATUS_SUCCESS
)
1258 TRACE_RET(chip
, retval
);
1260 retval
= ms_read_bytes(chip
, READ_REG
, 2, NO_WAIT_INT
, val
, 2);
1261 if (retval
!= STATUS_SUCCESS
)
1262 TRACE_RET(chip
, retval
);
1263 if (val
[1] & (STS_UCDT
| STS_UCEX
| STS_UCFG
)) {
1264 ms_set_err_code(chip
, MS_FLASH_READ_ERROR
);
1265 TRACE_RET(chip
, STATUS_FAIL
);
1268 return STATUS_SUCCESS
;
1271 static int ms_check_boot_block(struct rts51x_chip
*chip
, u16 block_addr
)
1273 struct ms_info
*ms_card
= &(chip
->ms_card
);
1275 u8 extra
[MS_EXTRA_SIZE
], data
[10], val
= 0;
1277 if (CHK_MS4BIT(ms_card
)) {
1278 /* Parallel interface */
1281 /* Serial interface */
1286 data
[2] = (u8
) (block_addr
>> 8);
1287 data
[3] = (u8
) block_addr
;
1289 * Extra data access mode */
1293 retval
= ms_set_cmd(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
, 6,
1294 BLOCK_READ
, WAIT_INT
, data
, 6, &val
);
1295 if (retval
!= STATUS_SUCCESS
)
1296 TRACE_RET(chip
, retval
);
1298 if (val
& INT_REG_CMDNK
) {
1299 ms_set_err_code(chip
, MS_CMD_NK
);
1300 TRACE_RET(chip
, STATUS_FAIL
);
1302 if (val
& INT_REG_CED
) {
1303 if (val
& INT_REG_ERR
) {
1304 retval
= ms_read_status_reg(chip
);
1305 if (retval
!= STATUS_SUCCESS
)
1306 TRACE_RET(chip
, retval
);
1308 ms_set_rw_reg_addr(chip
, OverwriteFlag
,
1309 MS_EXTRA_SIZE
, SystemParm
, 6);
1310 if (retval
!= STATUS_SUCCESS
)
1311 TRACE_RET(chip
, retval
);
1316 ms_read_bytes(chip
, READ_REG
, MS_EXTRA_SIZE
, NO_WAIT_INT
, extra
,
1318 if (retval
!= STATUS_SUCCESS
)
1319 TRACE_RET(chip
, retval
);
1321 if (!(extra
[0] & BLOCK_OK
) || (extra
[1] & NOT_BOOT_BLOCK
))
1322 TRACE_RET(chip
, STATUS_FAIL
);
1324 return STATUS_SUCCESS
;
1327 static int ms_read_extra_data(struct rts51x_chip
*chip
,
1328 u16 block_addr
, u8 page_num
, u8
*buf
,
1331 struct ms_info
*ms_card
= &(chip
->ms_card
);
1333 u8 val
= 0, data
[10];
1335 if (CHK_MS4BIT(ms_card
)) {
1336 /* Parallel interface */
1339 /* Serial interface */
1344 data
[2] = (u8
) (block_addr
>> 8);
1345 data
[3] = (u8
) block_addr
;
1347 * Extra data access mode */
1353 ms_auto_set_cmd(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
, 6,
1354 BLOCK_READ
, WAIT_INT
, data
, 6, &val
);
1356 retval
= ms_set_cmd(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
, 6,
1357 BLOCK_READ
, WAIT_INT
, data
, 6, &val
);
1359 if (retval
!= STATUS_SUCCESS
)
1360 TRACE_RET(chip
, retval
);
1362 if (val
& INT_REG_CMDNK
) {
1363 ms_set_err_code(chip
, MS_CMD_NK
);
1364 TRACE_RET(chip
, STATUS_FAIL
);
1366 if (val
& INT_REG_CED
) {
1367 if (val
& INT_REG_ERR
) {
1368 retval
= ms_read_status_reg(chip
);
1369 if (retval
!= STATUS_SUCCESS
)
1370 TRACE_RET(chip
, retval
);
1372 ms_set_rw_reg_addr(chip
, OverwriteFlag
,
1373 MS_EXTRA_SIZE
, SystemParm
, 6);
1374 if (retval
!= STATUS_SUCCESS
)
1375 TRACE_RET(chip
, retval
);
1380 ms_read_bytes(chip
, READ_REG
, MS_EXTRA_SIZE
, NO_WAIT_INT
, data
,
1382 if (retval
!= STATUS_SUCCESS
)
1383 TRACE_RET(chip
, retval
);
1385 if (buf
&& buf_len
) {
1386 if (buf_len
> MS_EXTRA_SIZE
)
1387 buf_len
= MS_EXTRA_SIZE
;
1388 memcpy(buf
, data
, buf_len
);
1391 return STATUS_SUCCESS
;
1394 static int ms_write_extra_data(struct rts51x_chip
*chip
,
1395 u16 block_addr
, u8 page_num
, u8
*buf
,
1398 struct ms_info
*ms_card
= &(chip
->ms_card
);
1400 u8 val
= 0, data
[16];
1402 if (!buf
|| (buf_len
< MS_EXTRA_SIZE
))
1403 TRACE_RET(chip
, STATUS_FAIL
);
1405 if (CHK_MS4BIT(ms_card
)) {
1406 /* Parallel interface */
1409 /* Serial interface */
1414 data
[2] = (u8
) (block_addr
>> 8);
1415 data
[3] = (u8
) block_addr
;
1417 * Extra data access mode */
1421 for (i
= 6; i
< MS_EXTRA_SIZE
+ 6; i
++)
1422 data
[i
] = buf
[i
- 6];
1426 ms_auto_set_cmd(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
,
1427 6 + MS_EXTRA_SIZE
, BLOCK_WRITE
, WAIT_INT
, data
, 16,
1431 ms_set_cmd(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
,
1432 6 + MS_EXTRA_SIZE
, BLOCK_WRITE
, WAIT_INT
, data
, 16,
1435 if (retval
!= STATUS_SUCCESS
)
1436 TRACE_RET(chip
, retval
);
1438 if (val
& INT_REG_CMDNK
) {
1439 ms_set_err_code(chip
, MS_CMD_NK
);
1440 TRACE_RET(chip
, STATUS_FAIL
);
1442 if (val
& INT_REG_CED
) {
1443 if (val
& INT_REG_ERR
) {
1444 ms_set_err_code(chip
, MS_FLASH_WRITE_ERROR
);
1445 TRACE_RET(chip
, STATUS_FAIL
);
1449 return STATUS_SUCCESS
;
1452 static int ms_read_page(struct rts51x_chip
*chip
, u16 block_addr
, u8 page_num
)
1454 struct ms_info
*ms_card
= &(chip
->ms_card
);
1456 u8 val
= 0, data
[6];
1458 if (CHK_MS4BIT(ms_card
)) {
1459 /* Parallel interface */
1462 /* Serial interface */
1467 data
[2] = (u8
) (block_addr
>> 8);
1468 data
[3] = (u8
) block_addr
;
1470 * Single page access mode */
1474 retval
= ms_set_cmd(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
, 6,
1475 BLOCK_READ
, WAIT_INT
, data
, 6, &val
);
1476 if (retval
!= STATUS_SUCCESS
)
1477 TRACE_RET(chip
, retval
);
1479 if (val
& INT_REG_CMDNK
) {
1480 ms_set_err_code(chip
, MS_CMD_NK
);
1481 TRACE_RET(chip
, STATUS_FAIL
);
1484 if (val
& INT_REG_CED
) {
1485 if (val
& INT_REG_ERR
) {
1486 if (!(val
& INT_REG_BREQ
)) {
1487 ms_set_err_code(chip
, MS_FLASH_READ_ERROR
);
1488 TRACE_RET(chip
, STATUS_FAIL
);
1490 retval
= ms_read_status_reg(chip
);
1491 if (retval
!= STATUS_SUCCESS
)
1492 ms_set_err_code(chip
, MS_FLASH_WRITE_ERROR
);
1494 if (!(val
& INT_REG_BREQ
)) {
1495 ms_set_err_code(chip
, MS_BREQ_ERROR
);
1496 TRACE_RET(chip
, STATUS_FAIL
);
1502 ms_transfer_tpc(chip
, MS_TM_NORMAL_READ
, READ_PAGE_DATA
, 0,
1504 if (retval
!= STATUS_SUCCESS
)
1505 TRACE_RET(chip
, retval
);
1506 if (ms_check_err_code(chip
, MS_FLASH_WRITE_ERROR
))
1507 TRACE_RET(chip
, STATUS_FAIL
);
1509 return STATUS_SUCCESS
;
1512 static int ms_set_bad_block(struct rts51x_chip
*chip
, u16 phy_blk
)
1514 struct ms_info
*ms_card
= &(chip
->ms_card
);
1516 u8 val
= 0, data
[8], extra
[MS_EXTRA_SIZE
];
1518 retval
= ms_read_extra_data(chip
, phy_blk
, 0, extra
, MS_EXTRA_SIZE
);
1519 if (retval
!= STATUS_SUCCESS
)
1520 TRACE_RET(chip
, retval
);
1522 ms_set_err_code(chip
, MS_NO_ERROR
);
1524 if (CHK_MS4BIT(ms_card
)) {
1525 /* Parallel interface */
1528 /* Serial interface */
1533 data
[2] = (u8
) (phy_blk
>> 8);
1534 data
[3] = (u8
) phy_blk
;
1537 data
[6] = extra
[0] & 0x7F;
1541 retval
= ms_set_cmd(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
, 7,
1542 BLOCK_WRITE
, WAIT_INT
, data
, 7, &val
);
1544 retval
= ms_set_cmd(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
, 7,
1545 BLOCK_WRITE
, WAIT_INT
, data
, 7, &val
);
1547 if (retval
!= STATUS_SUCCESS
)
1548 TRACE_RET(chip
, retval
);
1550 if (val
& INT_REG_CMDNK
) {
1551 ms_set_err_code(chip
, MS_CMD_NK
);
1552 TRACE_RET(chip
, STATUS_FAIL
);
1555 if (val
& INT_REG_CED
) {
1556 if (val
& INT_REG_ERR
) {
1557 ms_set_err_code(chip
, MS_FLASH_WRITE_ERROR
);
1558 TRACE_RET(chip
, STATUS_FAIL
);
1562 return STATUS_SUCCESS
;
1565 static int ms_erase_block(struct rts51x_chip
*chip
, u16 phy_blk
)
1567 struct ms_info
*ms_card
= &(chip
->ms_card
);
1569 u8 val
= 0, data
[6];
1572 ms_set_rw_reg_addr(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
,
1574 if (retval
!= STATUS_SUCCESS
)
1575 TRACE_RET(chip
, retval
);
1577 ms_set_err_code(chip
, MS_NO_ERROR
);
1579 if (CHK_MS4BIT(ms_card
)) {
1580 /* Parallel interface */
1583 /* Serial interface */
1588 data
[2] = (u8
) (phy_blk
>> 8);
1589 data
[3] = (u8
) phy_blk
;
1596 ms_auto_set_cmd(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
, 6,
1597 BLOCK_ERASE
, WAIT_INT
, data
, 6, &val
);
1599 retval
= ms_set_cmd(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
, 6,
1600 BLOCK_ERASE
, WAIT_INT
, data
, 6, &val
);
1603 if (val
& INT_REG_CMDNK
) {
1608 ms_set_err_code(chip
, MS_CMD_NK
);
1609 ms_set_bad_block(chip
, phy_blk
);
1610 TRACE_RET(chip
, STATUS_FAIL
);
1613 if (val
& INT_REG_CED
) {
1614 if (val
& INT_REG_ERR
) {
1615 ms_set_err_code(chip
, MS_FLASH_WRITE_ERROR
);
1616 TRACE_RET(chip
, STATUS_FAIL
);
1620 return STATUS_SUCCESS
;
1623 static void ms_set_page_status(u16 log_blk
, u8 type
, u8
*extra
, int extra_len
)
1625 if (!extra
|| (extra_len
< MS_EXTRA_SIZE
))
1628 memset(extra
, 0xFF, MS_EXTRA_SIZE
);
1630 if (type
== setPS_NG
)
1635 extra
[2] = (u8
) (log_blk
>> 8);
1636 extra
[3] = (u8
) log_blk
;
1639 static int ms_init_page(struct rts51x_chip
*chip
, u16 phy_blk
, u16 log_blk
,
1640 u8 start_page
, u8 end_page
)
1643 u8 extra
[MS_EXTRA_SIZE
], i
;
1645 memset(extra
, 0xff, MS_EXTRA_SIZE
);
1647 extra
[0] = 0xf8; /* Block, page OK, data erased */
1649 extra
[2] = (u8
) (log_blk
>> 8);
1650 extra
[3] = (u8
) log_blk
;
1652 for (i
= start_page
; i
< end_page
; i
++) {
1653 if (monitor_card_cd(chip
, MS_CARD
) == CD_NOT_EXIST
)
1654 TRACE_RET(chip
, STATUS_FAIL
);
1657 ms_write_extra_data(chip
, phy_blk
, i
, extra
, MS_EXTRA_SIZE
);
1658 if (retval
!= STATUS_SUCCESS
)
1659 TRACE_RET(chip
, retval
);
1662 return STATUS_SUCCESS
;
1665 static int ms_copy_page(struct rts51x_chip
*chip
, u16 old_blk
, u16 new_blk
,
1666 u16 log_blk
, u8 start_page
, u8 end_page
)
1668 struct ms_info
*ms_card
= &(chip
->ms_card
);
1669 int retval
, rty_cnt
, uncorrect_flag
= 0;
1670 u8 extra
[MS_EXTRA_SIZE
], val
, i
, j
, data
[16];
1672 RTS51X_DEBUGP("Copy page from 0x%x to 0x%x, logical block is 0x%x\n",
1673 old_blk
, new_blk
, log_blk
);
1674 RTS51X_DEBUGP("start_page = %d, end_page = %d\n", start_page
,
1677 retval
= ms_read_extra_data(chip
, new_blk
, 0, extra
, MS_EXTRA_SIZE
);
1678 if (retval
!= STATUS_SUCCESS
)
1679 TRACE_RET(chip
, retval
);
1681 retval
= ms_read_status_reg(chip
);
1682 if (retval
!= STATUS_SUCCESS
)
1683 TRACE_RET(chip
, retval
);
1685 RTS51X_READ_REG(chip
, PPBUF_BASE2
, &val
);
1687 if (val
& BUF_FULL
) {
1689 retval
= ms_send_cmd(chip
, CLEAR_BUF
, WAIT_INT
);
1690 if (retval
!= STATUS_SUCCESS
)
1691 TRACE_RET(chip
, retval
);
1692 /* GET_INT Register */
1693 retval
= ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
, 1);
1694 if (retval
!= STATUS_SUCCESS
)
1695 TRACE_RET(chip
, retval
);
1697 if (!(val
& INT_REG_CED
)) {
1698 ms_set_err_code(chip
, MS_FLASH_WRITE_ERROR
);
1699 TRACE_RET(chip
, STATUS_FAIL
);
1703 for (i
= start_page
; i
< end_page
; i
++) {
1704 if (monitor_card_cd(chip
, MS_CARD
) == CD_NOT_EXIST
)
1705 TRACE_RET(chip
, STATUS_FAIL
);
1707 ms_read_extra_data(chip
, old_blk
, i
, extra
, MS_EXTRA_SIZE
);
1710 ms_set_rw_reg_addr(chip
, OverwriteFlag
, MS_EXTRA_SIZE
,
1712 if (retval
!= STATUS_SUCCESS
)
1713 TRACE_RET(chip
, retval
);
1715 ms_set_err_code(chip
, MS_NO_ERROR
);
1717 if (CHK_MS4BIT(ms_card
)) {
1718 /* Parallel interface */
1721 /* Serial interface */
1726 data
[2] = (u8
) (old_blk
>> 8);
1727 data
[3] = (u8
) old_blk
;
1732 ms_write_bytes(chip
, WRITE_REG
, 6, NO_WAIT_INT
, data
, 6);
1733 if (retval
!= STATUS_SUCCESS
)
1734 TRACE_RET(chip
, retval
);
1736 retval
= ms_send_cmd(chip
, BLOCK_READ
, WAIT_INT
);
1737 if (retval
!= STATUS_SUCCESS
)
1738 TRACE_RET(chip
, retval
);
1740 ms_set_err_code(chip
, MS_NO_ERROR
);
1741 retval
= ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
, 1);
1742 if (retval
!= STATUS_SUCCESS
)
1743 TRACE_RET(chip
, retval
);
1745 if (val
& INT_REG_CMDNK
) {
1746 ms_set_err_code(chip
, MS_CMD_NK
);
1747 TRACE_RET(chip
, STATUS_FAIL
);
1750 if (val
& INT_REG_CED
) {
1751 if (val
& INT_REG_ERR
) {
1752 retval
= ms_read_status_reg(chip
);
1753 if (retval
!= STATUS_SUCCESS
) {
1755 RTS51X_DEBUGP("Uncorrectable"
1762 ms_transfer_tpc(chip
, MS_TM_NORMAL_READ
,
1763 READ_PAGE_DATA
, 0, NO_WAIT_INT
);
1764 if (retval
!= STATUS_SUCCESS
)
1765 TRACE_RET(chip
, retval
);
1766 if (uncorrect_flag
) {
1767 ms_set_page_status(log_blk
, setPS_NG
,
1768 extra
, MS_EXTRA_SIZE
);
1771 ms_write_extra_data(chip
, old_blk
, i
,
1774 RTS51X_DEBUGP("page %d :"
1775 "extra[0] = 0x%x\n",
1777 MS_SET_BAD_BLOCK_FLG(ms_card
);
1779 ms_set_page_status(log_blk
, setPS_Error
,
1780 extra
, MS_EXTRA_SIZE
);
1781 ms_write_extra_data(chip
, new_blk
, i
,
1782 extra
, MS_EXTRA_SIZE
);
1786 for (rty_cnt
= 0; rty_cnt
< MS_MAX_RETRY_COUNT
;
1789 ms_transfer_tpc(chip
,
1793 if (retval
== STATUS_SUCCESS
)
1796 if (rty_cnt
== MS_MAX_RETRY_COUNT
)
1797 TRACE_RET(chip
, STATUS_FAIL
);
1800 if (!(val
& INT_REG_BREQ
)) {
1801 ms_set_err_code(chip
, MS_BREQ_ERROR
);
1802 TRACE_RET(chip
, STATUS_FAIL
);
1806 retval
= ms_set_rw_reg_addr(chip
, OverwriteFlag
,
1807 MS_EXTRA_SIZE
, SystemParm
,
1808 (6 + MS_EXTRA_SIZE
));
1811 ms_set_err_code(chip
, MS_NO_ERROR
);
1813 if (CHK_MS4BIT(ms_card
)) {
1814 /* Parallel interface */
1817 /* Serial interface */
1822 data
[2] = (u8
) (new_blk
>> 8);
1823 data
[3] = (u8
) new_blk
;
1827 /* for MS check procedure */
1828 if ((extra
[0] & 0x60) != 0x60)
1834 data
[6 + 2] = (u8
) (log_blk
>> 8);
1835 data
[6 + 3] = (u8
) log_blk
;
1837 for (j
= 4; j
<= MS_EXTRA_SIZE
; j
++)
1841 ms_write_bytes(chip
, WRITE_REG
, (6 + MS_EXTRA_SIZE
),
1842 NO_WAIT_INT
, data
, 16);
1843 if (retval
!= STATUS_SUCCESS
)
1844 TRACE_RET(chip
, retval
);
1846 retval
= ms_send_cmd(chip
, BLOCK_WRITE
, WAIT_INT
);
1847 if (retval
!= STATUS_SUCCESS
)
1848 TRACE_RET(chip
, retval
);
1849 /* GET_INT Register */
1850 ms_set_err_code(chip
, MS_NO_ERROR
);
1851 retval
= ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
, 1);
1852 if (retval
!= STATUS_SUCCESS
)
1853 TRACE_RET(chip
, retval
);
1855 if (val
& INT_REG_CMDNK
) {
1856 ms_set_err_code(chip
, MS_CMD_NK
);
1857 TRACE_RET(chip
, STATUS_FAIL
);
1860 if (val
& INT_REG_CED
) {
1861 if (val
& INT_REG_ERR
) {
1862 ms_set_err_code(chip
, MS_FLASH_WRITE_ERROR
);
1863 TRACE_RET(chip
, STATUS_FAIL
);
1869 ms_set_rw_reg_addr(chip
, OverwriteFlag
,
1870 MS_EXTRA_SIZE
, SystemParm
, 7);
1871 if (retval
!= STATUS_SUCCESS
)
1872 TRACE_RET(chip
, retval
);
1874 ms_set_err_code(chip
, MS_NO_ERROR
);
1876 if (CHK_MS4BIT(ms_card
)) {
1877 /* Parallel interface */
1880 /* Serial interface */
1885 data
[2] = (u8
) (old_blk
>> 8);
1886 data
[3] = (u8
) old_blk
;
1893 ms_write_bytes(chip
, WRITE_REG
, 7, NO_WAIT_INT
,
1895 if (retval
!= STATUS_SUCCESS
)
1896 TRACE_RET(chip
, retval
);
1898 retval
= ms_send_cmd(chip
, BLOCK_WRITE
, WAIT_INT
);
1899 if (retval
!= STATUS_SUCCESS
)
1900 TRACE_RET(chip
, retval
);
1902 ms_set_err_code(chip
, MS_NO_ERROR
);
1904 ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
,
1906 if (retval
!= STATUS_SUCCESS
)
1907 TRACE_RET(chip
, retval
);
1909 if (val
& INT_REG_CMDNK
) {
1910 ms_set_err_code(chip
, MS_CMD_NK
);
1911 TRACE_RET(chip
, STATUS_FAIL
);
1914 if (val
& INT_REG_CED
) {
1915 if (val
& INT_REG_ERR
) {
1916 ms_set_err_code(chip
,
1917 MS_FLASH_WRITE_ERROR
);
1918 TRACE_RET(chip
, STATUS_FAIL
);
1924 return STATUS_SUCCESS
;
1928 static int ms_auto_copy_page(struct rts51x_chip
*chip
, u16 old_blk
, u16 new_blk
,
1929 u16 log_blk
, u8 start_page
, u8 end_page
)
1931 struct ms_info
*ms_card
= &(chip
->ms_card
);
1933 u8 page_len
, bus_width
, val
= 0;
1934 u8 extra
[MS_EXTRA_SIZE
];
1936 RTS51X_DEBUGP("Auto copy page from 0x%x to 0x%x,"
1937 "logical block is 0x%x\n",
1938 old_blk
, new_blk
, log_blk
);
1939 RTS51X_DEBUGP("start_page = %d, end_page = %d\n", start_page
,
1942 page_len
= end_page
- start_page
;
1944 retval
= ms_read_extra_data(chip
, new_blk
, 0, extra
, MS_EXTRA_SIZE
);
1945 if (retval
!= STATUS_SUCCESS
)
1946 TRACE_RET(chip
, retval
);
1948 retval
= ms_read_status_reg(chip
);
1949 if (retval
!= STATUS_SUCCESS
)
1950 TRACE_RET(chip
, retval
);
1952 RTS51X_READ_REG(chip
, PPBUF_BASE2
, &val
);
1954 if (val
& BUF_FULL
) {
1955 retval
= ms_send_cmd(chip
, CLEAR_BUF
, WAIT_INT
);
1956 if (retval
!= STATUS_SUCCESS
)
1957 TRACE_RET(chip
, retval
);
1959 retval
= ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
, 1);
1960 if (retval
!= STATUS_SUCCESS
)
1961 TRACE_RET(chip
, retval
);
1963 if (!(val
& INT_REG_CED
)) {
1964 ms_set_err_code(chip
, MS_FLASH_WRITE_ERROR
);
1965 TRACE_RET(chip
, STATUS_FAIL
);
1969 if (CHK_MS4BIT(ms_card
)) {
1970 /* Parallel interface */
1973 /* Serial interface */
1977 rts51x_init_cmd(chip
);
1979 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_OLD_BLOCK_0
, 0xFF, (u8
) old_blk
);
1980 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_OLD_BLOCK_1
, 0xFF,
1981 (u8
) (old_blk
>> 8));
1982 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_NEW_BLOCK_0
, 0xFF, (u8
) new_blk
);
1983 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_NEW_BLOCK_1
, 0xFF,
1984 (u8
) (new_blk
>> 8));
1985 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_LOG_BLOCK_0
, 0xFF, (u8
) log_blk
);
1986 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_LOG_BLOCK_1
, 0xFF,
1987 (u8
) (log_blk
>> 8));
1988 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_PAGE_START
, 0xFF, start_page
);
1989 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_PAGE_LENGTH
, 0xFF, page_len
);
1990 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_BUS_WIDTH
, 0xFF, bus_width
);
1992 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANSFER
, 0xFF,
1993 MS_TRANSFER_START
| MS_TM_COPY_PAGE
);
1994 rts51x_add_cmd(chip
, CHECK_REG_CMD
, MS_TRANSFER
, MS_TRANSFER_END
,
1997 retval
= rts51x_send_cmd(chip
, MODE_CR
, 100);
1998 if (retval
!= STATUS_SUCCESS
) {
1999 rts51x_clear_ms_error(chip
);
2000 TRACE_RET(chip
, retval
);
2003 retval
= rts51x_get_rsp(chip
, 1, 5000);
2005 if (CHECK_MS_TRANS_FAIL(chip
, retval
)) {
2006 rts51x_clear_ms_error(chip
);
2007 if (retval
== STATUS_TIMEDOUT
)
2008 TRACE_RET(chip
, retval
);
2009 TRACE_GOTO(chip
, Fail
);
2012 return STATUS_SUCCESS
;
2015 retval
= ms_erase_block(chip
, new_blk
);
2016 if (retval
!= STATUS_SUCCESS
)
2017 TRACE_RET(chip
, retval
);
2020 ms_copy_page(chip
, old_blk
, new_blk
, log_blk
, start_page
, end_page
);
2021 if (retval
!= STATUS_SUCCESS
)
2022 TRACE_RET(chip
, retval
);
2024 return STATUS_SUCCESS
;
2028 static int reset_ms(struct rts51x_chip
*chip
)
2030 struct ms_info
*ms_card
= &(chip
->ms_card
);
2032 u16 i
, reg_addr
, block_size
;
2034 #ifndef SUPPORT_MAGIC_GATE
2038 retval
= ms_prepare_reset(chip
);
2039 if (retval
!= STATUS_SUCCESS
)
2040 TRACE_RET(chip
, retval
);
2042 ms_card
->ms_type
|= TYPE_MS
;
2044 retval
= ms_send_cmd(chip
, MS_RESET
, NO_WAIT_INT
);
2045 if (retval
!= STATUS_SUCCESS
)
2046 TRACE_RET(chip
, retval
);
2048 retval
= ms_read_status_reg(chip
);
2049 if (retval
!= STATUS_SUCCESS
)
2050 TRACE_RET(chip
, retval
);
2052 RTS51X_READ_REG(chip
, PPBUF_BASE2
, &val
);
2053 if (val
& WRT_PRTCT
)
2054 chip
->card_wp
|= MS_CARD
;
2056 chip
->card_wp
&= ~MS_CARD
;
2061 /* Search For Boot Block */
2062 while (i
< (MAX_DEFECTIVE_BLOCK
+ 2)) {
2063 if (monitor_card_cd(chip
, MS_CARD
) == CD_NOT_EXIST
)
2064 TRACE_RET(chip
, STATUS_FAIL
);
2066 retval
= ms_check_boot_block(chip
, i
);
2067 if (retval
!= STATUS_SUCCESS
) {
2072 ms_card
->boot_block
= i
;
2076 if (i
== (MAX_DEFECTIVE_BLOCK
+ 2)) {
2077 RTS51X_DEBUGP("No boot block found!");
2078 TRACE_RET(chip
, STATUS_FAIL
);
2080 for (j
= 0; j
< 3; j
++) {
2081 retval
= ms_read_page(chip
, ms_card
->boot_block
, j
);
2082 if (retval
!= STATUS_SUCCESS
) {
2083 if (ms_check_err_code(chip
, MS_FLASH_WRITE_ERROR
)) {
2084 i
= ms_card
->boot_block
+ 1;
2085 ms_set_err_code(chip
, MS_NO_ERROR
);
2091 /* Read boot block contents */
2092 retval
= ms_read_page(chip
, ms_card
->boot_block
, 0);
2093 if (retval
!= STATUS_SUCCESS
)
2094 TRACE_RET(chip
, retval
);
2095 /* Read MS system information as sys_info */
2097 rts51x_seq_read_register(chip
, PPBUF_BASE2
+ 0x1A0, 96,
2098 ms_card
->raw_sys_info
);
2099 if (retval
!= STATUS_SUCCESS
)
2100 TRACE_RET(chip
, retval
);
2101 /* Read useful block contents */
2102 rts51x_init_cmd(chip
);
2104 rts51x_add_cmd(chip
, READ_REG_CMD
, HEADER_ID0
, 0, 0);
2105 rts51x_add_cmd(chip
, READ_REG_CMD
, HEADER_ID1
, 0, 0);
2107 for (reg_addr
= DISABLED_BLOCK0
; reg_addr
<= DISABLED_BLOCK3
;
2109 rts51x_add_cmd(chip
, READ_REG_CMD
, reg_addr
, 0, 0);
2112 for (reg_addr
= BLOCK_SIZE_0
; reg_addr
<= PAGE_SIZE_1
; reg_addr
++)
2113 rts51x_add_cmd(chip
, READ_REG_CMD
, reg_addr
, 0, 0);
2115 rts51x_add_cmd(chip
, READ_REG_CMD
, MS_Device_Type
, 0, 0);
2116 rts51x_add_cmd(chip
, READ_REG_CMD
, MS_4bit_Support
, 0, 0);
2118 retval
= rts51x_send_cmd(chip
, MODE_CR
, 100);
2119 if (retval
!= STATUS_SUCCESS
)
2120 TRACE_RET(chip
, retval
);
2122 retval
= rts51x_get_rsp(chip
, 16, 100);
2124 if (retval
!= STATUS_SUCCESS
)
2125 TRACE_RET(chip
, retval
);
2127 ptr
= rts51x_get_rsp_data(chip
);
2129 RTS51X_DEBUGP("Boot block data:\n");
2130 RTS51X_DUMP(ptr
, 16);
2132 if (ptr
[0] != 0x00 || ptr
[1] != 0x01) {
2133 i
= ms_card
->boot_block
+ 1;
2136 if (ptr
[12] != 0x02 || ptr
[13] != 0x00) {
2137 i
= ms_card
->boot_block
+ 1;
2140 if ((ptr
[14] == 1) || (ptr
[14] == 3))
2141 chip
->card_wp
|= MS_CARD
;
2142 block_size
= ((u16
) ptr
[6] << 8) | ptr
[7];
2143 if (block_size
== 0x0010) {
2144 ms_card
->block_shift
= 5;
2145 ms_card
->page_off
= 0x1F;
2146 } else if (block_size
== 0x0008) {
2147 ms_card
->block_shift
= 4;
2148 ms_card
->page_off
= 0x0F;
2150 ms_card
->total_block
= ((u16
) ptr
[8] << 8) | ptr
[9];
2152 #ifdef SUPPORT_MAGIC_GATE
2155 if (ms_card
->block_shift
== 4) {
2157 ms_card
->capacity
= 0x1EE0;
2159 ms_card
->capacity
= 0x3DE0;
2162 ms_card
->capacity
= 0x7BC0;
2164 ms_card
->capacity
= 0xF7C0;
2166 ms_card
->capacity
= 0x1EF80;
2168 ms_card
->capacity
= 0x3DF00;
2171 eblock_cnt
= ((u16
) ptr
[10] << 8) | ptr
[11];
2173 ms_card
->capacity
= ((u32
) eblock_cnt
- 2) << ms_card
->block_shift
;
2176 chip
->capacity
[chip
->card2lun
[MS_CARD
]] = ms_card
->capacity
;
2179 retval
= ms_set_rw_reg_addr(chip
, 0, 0, SystemParm
, 1);
2180 if (retval
!= STATUS_SUCCESS
)
2181 TRACE_RET(chip
, STATUS_FAIL
);
2182 RTS51X_WRITE_REG(chip
, PPBUF_BASE2
, 0xFF, 0x88);
2183 RTS51X_WRITE_REG(chip
, PPBUF_BASE2
+ 1, 0xFF, 0);
2186 ms_transfer_tpc(chip
, MS_TM_WRITE_BYTES
, WRITE_REG
, 1,
2188 if (retval
!= STATUS_SUCCESS
)
2189 TRACE_RET(chip
, STATUS_FAIL
);
2190 RTS51X_WRITE_REG(chip
, MS_CFG
, 0x58 | MS_NO_CHECK_INT
,
2191 MS_BUS_WIDTH_4
| PUSH_TIME_ODD
|
2194 ms_card
->ms_type
|= MS_4BIT
;
2197 if (CHK_MS4BIT(ms_card
))
2198 chip
->card_bus_width
[chip
->card2lun
[MS_CARD
]] = 4;
2200 chip
->card_bus_width
[chip
->card2lun
[MS_CARD
]] = 1;
2202 return STATUS_SUCCESS
;
2205 static int ms_init_l2p_tbl(struct rts51x_chip
*chip
)
2207 struct ms_info
*ms_card
= &(chip
->ms_card
);
2208 int size
, i
, seg_no
, retval
;
2209 u16 defect_block
, reg_addr
;
2212 ms_card
->segment_cnt
= ms_card
->total_block
>> 9;
2213 RTS51X_DEBUGP("ms_card->segment_cnt = %d\n", ms_card
->segment_cnt
);
2215 size
= ms_card
->segment_cnt
* sizeof(struct zone_entry
);
2216 ms_card
->segment
= vmalloc(size
);
2217 if (ms_card
->segment
== NULL
)
2218 TRACE_RET(chip
, STATUS_FAIL
);
2219 memset(ms_card
->segment
, 0, size
);
2221 retval
= ms_read_page(chip
, ms_card
->boot_block
, 1);
2222 if (retval
!= STATUS_SUCCESS
)
2223 TRACE_GOTO(chip
, INIT_FAIL
);
2225 reg_addr
= PPBUF_BASE2
;
2226 for (i
= 0; i
< (((ms_card
->total_block
>> 9) * 10) + 1); i
++) {
2227 retval
= rts51x_read_register(chip
, reg_addr
++, &val1
);
2228 if (retval
!= STATUS_SUCCESS
)
2229 TRACE_GOTO(chip
, INIT_FAIL
);
2230 retval
= rts51x_read_register(chip
, reg_addr
++, &val2
);
2231 if (retval
!= STATUS_SUCCESS
)
2232 TRACE_GOTO(chip
, INIT_FAIL
);
2234 defect_block
= ((u16
) val1
<< 8) | val2
;
2235 if (defect_block
== 0xFFFF)
2237 seg_no
= defect_block
/ 512;
2238 ms_card
->segment
[seg_no
].defect_list
[ms_card
->segment
[seg_no
].
2243 for (i
= 0; i
< ms_card
->segment_cnt
; i
++) {
2244 ms_card
->segment
[i
].build_flag
= 0;
2245 ms_card
->segment
[i
].l2p_table
= NULL
;
2246 ms_card
->segment
[i
].free_table
= NULL
;
2247 ms_card
->segment
[i
].get_index
= 0;
2248 ms_card
->segment
[i
].set_index
= 0;
2249 ms_card
->segment
[i
].unused_blk_cnt
= 0;
2251 RTS51X_DEBUGP("defective block count of segment %d is %d\n",
2252 i
, ms_card
->segment
[i
].disable_count
);
2255 return STATUS_SUCCESS
;
2258 if (ms_card
->segment
) {
2259 vfree(ms_card
->segment
);
2260 ms_card
->segment
= NULL
;
2266 static u16
ms_get_l2p_tbl(struct rts51x_chip
*chip
, int seg_no
, u16 log_off
)
2268 struct ms_info
*ms_card
= &(chip
->ms_card
);
2269 struct zone_entry
*segment
;
2271 if (ms_card
->segment
== NULL
)
2274 segment
= &(ms_card
->segment
[seg_no
]);
2276 if (segment
->l2p_table
)
2277 return segment
->l2p_table
[log_off
];
2282 static void ms_set_l2p_tbl(struct rts51x_chip
*chip
, int seg_no
, u16 log_off
,
2285 struct ms_info
*ms_card
= &(chip
->ms_card
);
2286 struct zone_entry
*segment
;
2288 if (ms_card
->segment
== NULL
)
2291 segment
= &(ms_card
->segment
[seg_no
]);
2292 if (segment
->l2p_table
)
2293 segment
->l2p_table
[log_off
] = phy_blk
;
2296 static void ms_set_unused_block(struct rts51x_chip
*chip
, u16 phy_blk
)
2298 struct ms_info
*ms_card
= &(chip
->ms_card
);
2299 struct zone_entry
*segment
;
2302 seg_no
= (int)phy_blk
>> 9;
2303 segment
= &(ms_card
->segment
[seg_no
]);
2305 segment
->free_table
[segment
->set_index
++] = phy_blk
;
2306 if (segment
->set_index
>= MS_FREE_TABLE_CNT
)
2307 segment
->set_index
= 0;
2308 segment
->unused_blk_cnt
++;
2311 static u16
ms_get_unused_block(struct rts51x_chip
*chip
, int seg_no
)
2313 struct ms_info
*ms_card
= &(chip
->ms_card
);
2314 struct zone_entry
*segment
;
2317 segment
= &(ms_card
->segment
[seg_no
]);
2319 if (segment
->unused_blk_cnt
<= 0)
2322 phy_blk
= segment
->free_table
[segment
->get_index
];
2323 segment
->free_table
[segment
->get_index
++] = 0xFFFF;
2324 if (segment
->get_index
>= MS_FREE_TABLE_CNT
)
2325 segment
->get_index
= 0;
2326 segment
->unused_blk_cnt
--;
2331 static const unsigned short ms_start_idx
[] = {
2332 0, 494, 990, 1486, 1982, 2478, 2974, 3470,
2333 3966, 4462, 4958, 5454, 5950, 6446, 6942, 7438, 7934
2336 static int ms_arbitrate_l2p(struct rts51x_chip
*chip
, u16 phy_blk
, u16 log_off
,
2339 struct ms_info
*ms_card
= &(chip
->ms_card
);
2340 struct zone_entry
*segment
;
2344 seg_no
= (int)phy_blk
>> 9;
2345 segment
= &(ms_card
->segment
[seg_no
]);
2346 tmp_blk
= segment
->l2p_table
[log_off
];
2350 if (!(chip
->card_wp
& MS_CARD
))
2351 ms_erase_block(chip
, tmp_blk
);
2352 ms_set_unused_block(chip
, tmp_blk
);
2353 segment
->l2p_table
[log_off
] = phy_blk
;
2355 if (!(chip
->card_wp
& MS_CARD
))
2356 ms_erase_block(chip
, phy_blk
);
2357 ms_set_unused_block(chip
, phy_blk
);
2360 if (phy_blk
< tmp_blk
) {
2361 if (!(chip
->card_wp
& MS_CARD
))
2362 ms_erase_block(chip
, phy_blk
);
2363 ms_set_unused_block(chip
, phy_blk
);
2365 if (!(chip
->card_wp
& MS_CARD
))
2366 ms_erase_block(chip
, tmp_blk
);
2367 ms_set_unused_block(chip
, tmp_blk
);
2368 segment
->l2p_table
[log_off
] = phy_blk
;
2372 return STATUS_SUCCESS
;
2375 static int ms_build_l2p_tbl(struct rts51x_chip
*chip
, int seg_no
)
2377 struct ms_info
*ms_card
= &(chip
->ms_card
);
2378 struct zone_entry
*segment
;
2379 int retval
, table_size
, disable_cnt
, defect_flag
, i
;
2380 u16 start
, end
, phy_blk
, log_blk
, tmp_blk
;
2381 u8 extra
[MS_EXTRA_SIZE
], us1
, us2
;
2383 RTS51X_DEBUGP("ms_build_l2p_tbl: %d\n", seg_no
);
2385 if (ms_card
->segment
== NULL
) {
2386 retval
= ms_init_l2p_tbl(chip
);
2387 if (retval
!= STATUS_SUCCESS
)
2388 TRACE_RET(chip
, retval
);
2391 if (ms_card
->segment
[seg_no
].build_flag
) {
2392 RTS51X_DEBUGP("l2p table of segment %d has been built\n",
2394 return STATUS_SUCCESS
;
2402 segment
= &(ms_card
->segment
[seg_no
]);
2404 if (segment
->l2p_table
== NULL
) {
2405 segment
->l2p_table
= vmalloc(table_size
* 2);
2406 if (segment
->l2p_table
== NULL
)
2407 TRACE_GOTO(chip
, BUILD_FAIL
);
2409 memset((u8
*) (segment
->l2p_table
), 0xff, table_size
* 2);
2411 if (segment
->free_table
== NULL
) {
2412 segment
->free_table
= vmalloc(MS_FREE_TABLE_CNT
* 2);
2413 if (segment
->free_table
== NULL
)
2414 TRACE_GOTO(chip
, BUILD_FAIL
);
2416 memset((u8
*) (segment
->free_table
), 0xff, MS_FREE_TABLE_CNT
* 2);
2418 start
= (u16
) seg_no
<< 9;
2419 end
= (u16
) (seg_no
+ 1) << 9;
2421 disable_cnt
= segment
->disable_count
;
2423 segment
->get_index
= segment
->set_index
= 0;
2424 segment
->unused_blk_cnt
= 0;
2426 for (phy_blk
= start
; phy_blk
< end
; phy_blk
++) {
2429 for (i
= 0; i
< segment
->disable_count
; i
++) {
2430 if (phy_blk
== segment
->defect_list
[i
]) {
2442 ms_read_extra_data(chip
, phy_blk
, 0, extra
, MS_EXTRA_SIZE
);
2443 if (retval
!= STATUS_SUCCESS
) {
2444 RTS51X_DEBUGP("read extra data fail\n");
2445 ms_set_bad_block(chip
, phy_blk
);
2449 if (seg_no
== ms_card
->segment_cnt
- 1) {
2450 if (!(extra
[1] & NOT_TRANSLATION_TABLE
)) {
2451 if (!(chip
->card_wp
& MS_CARD
)) {
2452 retval
= ms_erase_block(chip
, phy_blk
);
2453 if (retval
!= STATUS_SUCCESS
)
2461 if (!(extra
[0] & BLOCK_OK
))
2463 if (!(extra
[1] & NOT_BOOT_BLOCK
))
2465 if ((extra
[0] & PAGE_OK
) != PAGE_OK
)
2468 log_blk
= ((u16
) extra
[2] << 8) | extra
[3];
2470 if (log_blk
== 0xFFFF) {
2471 if (!(chip
->card_wp
& MS_CARD
)) {
2472 retval
= ms_erase_block(chip
, phy_blk
);
2473 if (retval
!= STATUS_SUCCESS
)
2476 ms_set_unused_block(chip
, phy_blk
);
2480 if ((log_blk
< ms_start_idx
[seg_no
]) ||
2481 (log_blk
>= ms_start_idx
[seg_no
+ 1])) {
2482 if (!(chip
->card_wp
& MS_CARD
)) {
2483 retval
= ms_erase_block(chip
, phy_blk
);
2484 if (retval
!= STATUS_SUCCESS
)
2487 ms_set_unused_block(chip
, phy_blk
);
2491 if (segment
->l2p_table
[log_blk
- ms_start_idx
[seg_no
]] ==
2493 segment
->l2p_table
[log_blk
- ms_start_idx
[seg_no
]] =
2498 us1
= extra
[0] & 0x10;
2499 tmp_blk
= segment
->l2p_table
[log_blk
- ms_start_idx
[seg_no
]];
2501 ms_read_extra_data(chip
, tmp_blk
, 0, extra
, MS_EXTRA_SIZE
);
2502 if (retval
!= STATUS_SUCCESS
)
2504 us2
= extra
[0] & 0x10;
2506 (void)ms_arbitrate_l2p(chip
, phy_blk
,
2507 log_blk
- ms_start_idx
[seg_no
], us1
,
2512 segment
->build_flag
= 1;
2514 RTS51X_DEBUGP("unused block count: %d\n", segment
->unused_blk_cnt
);
2516 if (seg_no
== ms_card
->segment_cnt
- 1) {
2517 if (segment
->unused_blk_cnt
< 2)
2518 chip
->card_wp
|= MS_CARD
;
2520 if (segment
->unused_blk_cnt
< 1)
2521 chip
->card_wp
|= MS_CARD
;
2524 if (chip
->card_wp
& MS_CARD
)
2525 return STATUS_SUCCESS
;
2527 for (log_blk
= ms_start_idx
[seg_no
]; log_blk
< ms_start_idx
[seg_no
+ 1];
2529 if (segment
->l2p_table
[log_blk
- ms_start_idx
[seg_no
]] ==
2531 phy_blk
= ms_get_unused_block(chip
, seg_no
);
2532 if (phy_blk
== 0xFFFF) {
2533 chip
->card_wp
|= MS_CARD
;
2534 return STATUS_SUCCESS
;
2536 retval
= ms_init_page(chip
, phy_blk
, log_blk
, 0, 1);
2537 if (retval
!= STATUS_SUCCESS
)
2538 TRACE_GOTO(chip
, BUILD_FAIL
);
2539 segment
->l2p_table
[log_blk
- ms_start_idx
[seg_no
]] =
2541 if (seg_no
== ms_card
->segment_cnt
- 1) {
2542 if (segment
->unused_blk_cnt
< 2) {
2543 chip
->card_wp
|= MS_CARD
;
2544 return STATUS_SUCCESS
;
2547 if (segment
->unused_blk_cnt
< 1) {
2548 chip
->card_wp
|= MS_CARD
;
2549 return STATUS_SUCCESS
;
2556 for (log_blk
= 0; log_blk
< 494; log_blk
++) {
2557 tmp_blk
= segment
->l2p_table
[log_blk
];
2558 if (tmp_blk
< ms_card
->boot_block
) {
2559 RTS51X_DEBUGP("Boot block is not the first"
2562 if (chip
->card_wp
& MS_CARD
)
2565 phy_blk
= ms_get_unused_block(chip
, 0);
2568 ms_auto_copy_page(chip
, tmp_blk
, phy_blk
,
2570 ms_card
->page_off
+ 1);
2572 retval
= ms_copy_page(chip
, tmp_blk
, phy_blk
,
2574 ms_card
->page_off
+ 1);
2576 if (retval
!= STATUS_SUCCESS
)
2577 TRACE_RET(chip
, retval
);
2579 segment
->l2p_table
[log_blk
] = phy_blk
;
2581 retval
= ms_set_bad_block(chip
, tmp_blk
);
2582 if (retval
!= STATUS_SUCCESS
)
2583 TRACE_RET(chip
, retval
);
2588 return STATUS_SUCCESS
;
2591 segment
->build_flag
= 0;
2592 if (segment
->l2p_table
) {
2593 vfree(segment
->l2p_table
);
2594 segment
->l2p_table
= NULL
;
2596 if (segment
->free_table
) {
2597 vfree(segment
->free_table
);
2598 segment
->free_table
= NULL
;
2604 int reset_ms_card(struct rts51x_chip
*chip
)
2606 struct ms_info
*ms_card
= &(chip
->ms_card
);
2609 memset(ms_card
, 0, sizeof(struct ms_info
));
2611 enable_card_clock(chip
, MS_CARD
);
2613 retval
= rts51x_select_card(chip
, MS_CARD
);
2614 if (retval
!= STATUS_SUCCESS
)
2615 TRACE_RET(chip
, retval
);
2617 ms_card
->ms_type
= 0;
2618 ms_card
->last_rw_int
= 0;
2620 retval
= reset_ms_pro(chip
);
2621 if (retval
!= STATUS_SUCCESS
) {
2622 if (ms_card
->check_ms_flow
) {
2623 retval
= reset_ms(chip
);
2624 if (retval
!= STATUS_SUCCESS
) {
2625 if (chip
->option
.reset_or_rw_fail_set_pad_drive
) {
2626 rts51x_write_register(chip
,
2627 CARD_DRIVE_SEL
, SD20_DRIVE_MASK
,
2630 TRACE_RET(chip
, retval
);
2633 if (chip
->option
.reset_or_rw_fail_set_pad_drive
) {
2634 rts51x_write_register(chip
, CARD_DRIVE_SEL
,
2638 TRACE_RET(chip
, retval
);
2642 retval
= ms_set_init_para(chip
);
2643 if (retval
!= STATUS_SUCCESS
)
2644 TRACE_RET(chip
, retval
);
2646 if (!CHK_MSPRO(ms_card
)) {
2647 retval
= ms_build_l2p_tbl(chip
, ms_card
->total_block
/ 512 - 1);
2648 if (retval
!= STATUS_SUCCESS
)
2649 TRACE_RET(chip
, retval
);
2652 RTS51X_DEBUGP("ms_card->ms_type = 0x%x\n", ms_card
->ms_type
);
2654 return STATUS_SUCCESS
;
2657 static int mspro_set_rw_cmd(struct rts51x_chip
*chip
, u32 start_sec
,
2658 u16 sec_cnt
, u8 cmd
)
2664 data
[1] = (u8
) (sec_cnt
>> 8);
2665 data
[2] = (u8
) sec_cnt
;
2666 data
[3] = (u8
) (start_sec
>> 24);
2667 data
[4] = (u8
) (start_sec
>> 16);
2668 data
[5] = (u8
) (start_sec
>> 8);
2669 data
[6] = (u8
) start_sec
;
2672 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
2674 ms_write_bytes(chip
, PRO_EX_SET_CMD
, 7, WAIT_INT
, data
, 8);
2675 if (retval
== STATUS_SUCCESS
)
2678 if (i
== MS_MAX_RETRY_COUNT
)
2679 TRACE_RET(chip
, STATUS_FAIL
);
2681 return STATUS_SUCCESS
;
2684 void mspro_stop_seq_mode(struct rts51x_chip
*chip
)
2686 struct ms_info
*ms_card
= &(chip
->ms_card
);
2689 if (ms_card
->seq_mode
) {
2690 retval
= ms_switch_clock(chip
);
2691 if (retval
!= STATUS_SUCCESS
)
2694 ms_card
->seq_mode
= 0;
2695 ms_card
->total_sec_cnt
= 0;
2696 ms_card
->last_rw_int
= 0;
2697 ms_send_cmd(chip
, PRO_STOP
, WAIT_INT
);
2699 rts51x_ep0_write_register(chip
, MC_FIFO_CTL
, FIFO_FLUSH
,
2704 static inline int ms_auto_tune_clock(struct rts51x_chip
*chip
)
2706 struct ms_info
*ms_card
= &(chip
->ms_card
);
2709 if (chip
->asic_code
) {
2710 if (ms_card
->ms_clock
> 30)
2711 ms_card
->ms_clock
-= 20;
2713 if (ms_card
->ms_clock
== CLK_80
)
2714 ms_card
->ms_clock
= CLK_60
;
2715 else if (ms_card
->ms_clock
== CLK_60
)
2716 ms_card
->ms_clock
= CLK_40
;
2719 retval
= ms_switch_clock(chip
);
2720 if (retval
!= STATUS_SUCCESS
)
2721 TRACE_RET(chip
, retval
);
2723 return STATUS_SUCCESS
;
2726 static int mspro_rw_multi_sector(struct scsi_cmnd
*srb
,
2727 struct rts51x_chip
*chip
, u32 start_sector
,
2730 struct ms_info
*ms_card
= &(chip
->ms_card
);
2731 int retval
, mode_2k
= 0;
2733 u8 val
, trans_mode
, rw_tpc
, rw_cmd
;
2735 ms_set_err_code(chip
, MS_NO_ERROR
);
2737 ms_card
->counter
= 0;
2739 if (CHK_MSHG(ms_card
)) {
2740 if ((start_sector
% 4) || (sector_cnt
% 4)) {
2741 if (srb
->sc_data_direction
== DMA_FROM_DEVICE
) {
2742 rw_tpc
= PRO_READ_LONG_DATA
;
2743 rw_cmd
= PRO_READ_DATA
;
2745 rw_tpc
= PRO_WRITE_LONG_DATA
;
2746 rw_cmd
= PRO_WRITE_DATA
;
2749 if (srb
->sc_data_direction
== DMA_FROM_DEVICE
) {
2750 rw_tpc
= PRO_READ_QUAD_DATA
;
2751 rw_cmd
= PRO_READ_2K_DATA
;
2753 rw_tpc
= PRO_WRITE_QUAD_DATA
;
2754 rw_cmd
= PRO_WRITE_2K_DATA
;
2759 if (srb
->sc_data_direction
== DMA_FROM_DEVICE
) {
2760 rw_tpc
= PRO_READ_LONG_DATA
;
2761 rw_cmd
= PRO_READ_DATA
;
2763 rw_tpc
= PRO_WRITE_LONG_DATA
;
2764 rw_cmd
= PRO_WRITE_DATA
;
2768 retval
= ms_switch_clock(chip
);
2769 if (retval
!= STATUS_SUCCESS
)
2770 TRACE_RET(chip
, retval
);
2772 if (srb
->sc_data_direction
== DMA_FROM_DEVICE
)
2773 trans_mode
= MS_TM_AUTO_READ
;
2775 trans_mode
= MS_TM_AUTO_WRITE
;
2777 val
= ms_card
->last_rw_int
;
2779 if (ms_card
->seq_mode
) {
2780 if ((ms_card
->pre_dir
!= srb
->sc_data_direction
)
2781 || ((ms_card
->pre_sec_addr
+ ms_card
->pre_sec_cnt
) !=
2783 || (mode_2k
&& (ms_card
->seq_mode
& MODE_512_SEQ
))
2784 || (!mode_2k
&& (ms_card
->seq_mode
& MODE_2K_SEQ
))
2785 || !(val
& MS_INT_BREQ
)
2786 || ((ms_card
->total_sec_cnt
+ sector_cnt
) > 0xFE00)) {
2787 ms_card
->seq_mode
= 0;
2788 ms_card
->total_sec_cnt
= 0;
2789 ms_card
->last_rw_int
= 0;
2790 if (val
& MS_INT_BREQ
) {
2791 retval
= ms_send_cmd(chip
, PRO_STOP
, WAIT_INT
);
2792 if (retval
!= STATUS_SUCCESS
)
2793 TRACE_RET(chip
, retval
);
2795 rts51x_ep0_write_register(chip
, MC_FIFO_CTL
,
2796 FIFO_FLUSH
, FIFO_FLUSH
);
2801 if (!ms_card
->seq_mode
) {
2802 ms_card
->total_sec_cnt
= 0;
2803 if (sector_cnt
>= 0x80) {
2804 if ((ms_card
->capacity
- start_sector
) > 0xFE00)
2808 (u16
) (ms_card
->capacity
- start_sector
);
2809 if (count
> sector_cnt
) {
2811 ms_card
->seq_mode
|= MODE_2K_SEQ
;
2813 ms_card
->seq_mode
|= MODE_512_SEQ
;
2818 retval
= mspro_set_rw_cmd(chip
, start_sector
, count
, rw_cmd
);
2819 if (retval
!= STATUS_SUCCESS
) {
2820 ms_card
->seq_mode
= 0;
2821 TRACE_RET(chip
, retval
);
2826 ms_transfer_data(chip
, trans_mode
, rw_tpc
, sector_cnt
, WAIT_INT
,
2827 mode_2k
, scsi_sg_count(srb
), scsi_sglist(srb
),
2829 if (retval
!= STATUS_SUCCESS
) {
2830 ms_card
->seq_mode
= 0;
2831 rts51x_ep0_read_register(chip
, MS_TRANS_CFG
, &val
);
2832 rts51x_clear_ms_error(chip
);
2833 if (val
& MS_INT_BREQ
)
2834 ms_send_cmd(chip
, PRO_STOP
, WAIT_INT
);
2835 if (val
& (MS_CRC16_ERR
| MS_RDY_TIMEOUT
)) {
2836 RTS51X_DEBUGP("MSPro CRC error, tune clock!\n");
2837 ms_auto_tune_clock(chip
);
2840 TRACE_RET(chip
, retval
);
2843 ms_card
->pre_sec_addr
= start_sector
;
2844 ms_card
->pre_sec_cnt
= sector_cnt
;
2845 ms_card
->pre_dir
= srb
->sc_data_direction
;
2846 ms_card
->total_sec_cnt
+= sector_cnt
;
2848 return STATUS_SUCCESS
;
2851 static int mspro_read_format_progress(struct rts51x_chip
*chip
,
2852 const int short_data_len
)
2854 struct ms_info
*ms_card
= &(chip
->ms_card
);
2856 u32 total_progress
, cur_progress
;
2860 ms_card
->format_status
= FORMAT_FAIL
;
2862 retval
= ms_switch_clock(chip
);
2863 if (retval
!= STATUS_SUCCESS
)
2864 TRACE_RET(chip
, retval
);
2866 RTS51X_READ_REG(chip
, MS_TRANS_CFG
, &tmp
);
2868 if ((tmp
& (MS_INT_CED
| MS_INT_CMDNK
| MS_INT_ERR
)) == MS_INT_CED
) {
2869 ms_card
->format_status
= FORMAT_SUCCESS
;
2870 ms_card
->pro_under_formatting
= 0;
2871 return STATUS_SUCCESS
;
2874 ((tmp
& (MS_INT_BREQ
| MS_INT_CED
| MS_INT_CMDNK
| MS_INT_ERR
)) ==
2876 ms_card
->pro_under_formatting
= 0;
2877 TRACE_RET(chip
, STATUS_FAIL
);
2880 if (short_data_len
>= 256)
2883 cnt
= (u8
) short_data_len
;
2886 ms_read_bytes(chip
, PRO_READ_SHORT_DATA
, cnt
, WAIT_INT
, data
, 8);
2887 if (retval
!= STATUS_SUCCESS
)
2888 TRACE_RET(chip
, retval
);
2891 (data
[0] << 24) | (data
[1] << 16) | (data
[2] << 8) | data
[3];
2893 (data
[4] << 24) | (data
[5] << 16) | (data
[6] << 8) | data
[7];
2895 RTS51X_DEBUGP("total_progress = %d, cur_progress = %d\n",
2896 total_progress
, cur_progress
);
2898 if (total_progress
== 0) {
2899 ms_card
->progress
= 0;
2901 u64 ulltmp
= (u64
) cur_progress
* (u64
) 65535;
2902 do_div(ulltmp
, total_progress
);
2903 ms_card
->progress
= (u16
) ulltmp
;
2905 RTS51X_DEBUGP("progress = %d\n", ms_card
->progress
);
2907 for (i
= 0; i
< 2500; i
++) {
2908 RTS51X_READ_REG(chip
, MS_TRANS_CFG
, &tmp
);
2910 (MS_INT_CED
| MS_INT_CMDNK
| MS_INT_BREQ
| MS_INT_ERR
))
2917 TRACE_RET(chip
, STATUS_FAIL
);
2919 RTS51X_DEBUGP("MSPro format tmp:%d\n", tmp
);
2921 if (tmp
& (MS_INT_CMDNK
| MS_INT_ERR
))
2922 TRACE_RET(chip
, STATUS_FAIL
);
2923 if (tmp
& MS_INT_CED
) {
2924 ms_card
->format_status
= FORMAT_SUCCESS
;
2925 ms_card
->pro_under_formatting
= 0;
2926 } else if (tmp
& MS_INT_BREQ
) {
2927 ms_card
->format_status
= FORMAT_IN_PROGRESS
;
2929 ms_card
->format_status
= FORMAT_FAIL
;
2930 ms_card
->pro_under_formatting
= 0;
2931 TRACE_RET(chip
, STATUS_FAIL
);
2934 RTS51X_DEBUGP("MSPro format format_status:%d\n",
2935 ms_card
->format_status
);
2937 return STATUS_SUCCESS
;
2940 void mspro_polling_format_status(struct rts51x_chip
*chip
)
2942 struct ms_info
*ms_card
= &(chip
->ms_card
);
2945 if (ms_card
->pro_under_formatting
) {
2946 for (i
= 0; i
< 65535; i
++) {
2947 mspro_read_format_progress(chip
, MS_SHORT_DATA_LEN
);
2948 if (ms_card
->format_status
!= FORMAT_IN_PROGRESS
)
2956 void mspro_format_sense(struct rts51x_chip
*chip
, unsigned int lun
)
2958 struct ms_info
*ms_card
= &(chip
->ms_card
);
2960 if (CHK_FORMAT_STATUS(ms_card
, FORMAT_SUCCESS
)) {
2961 set_sense_type(chip
, lun
, SENSE_TYPE_NO_SENSE
);
2962 ms_card
->pro_under_formatting
= 0;
2963 ms_card
->progress
= 0;
2964 } else if (CHK_FORMAT_STATUS(ms_card
, FORMAT_IN_PROGRESS
)) {
2965 set_sense_data(chip
, lun
, CUR_ERR
, 0x02, 0, 0x04, 0x04,
2966 0, (u16
) (ms_card
->progress
));
2968 set_sense_type(chip
, lun
, SENSE_TYPE_FORMAT_CMD_FAILED
);
2969 ms_card
->pro_under_formatting
= 0;
2970 ms_card
->progress
= 0;
2974 int mspro_format(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
,
2975 int short_data_len
, int quick_format
)
2977 struct ms_info
*ms_card
= &(chip
->ms_card
);
2982 retval
= ms_switch_clock(chip
);
2983 if (retval
!= STATUS_SUCCESS
)
2984 TRACE_RET(chip
, retval
);
2986 retval
= ms_set_rw_reg_addr(chip
, 0x00, 0x00, Pro_TPCParm
, 0x01);
2987 if (retval
!= STATUS_SUCCESS
)
2988 TRACE_RET(chip
, retval
);
2991 switch (short_data_len
) {
3007 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
3009 ms_write_bytes(chip
, PRO_WRITE_REG
, 1, NO_WAIT_INT
, buf
, 2);
3010 if (retval
== STATUS_SUCCESS
)
3013 if (i
== MS_MAX_RETRY_COUNT
)
3014 TRACE_RET(chip
, STATUS_FAIL
);
3015 /* Format command */
3020 retval
= mspro_set_rw_cmd(chip
, 0, para
, PRO_FORMAT
);
3021 if (retval
!= STATUS_SUCCESS
)
3022 TRACE_RET(chip
, retval
);
3024 RTS51X_READ_REG(chip
, MS_TRANS_CFG
, &tmp
);
3025 if (tmp
& (MS_INT_CMDNK
| MS_INT_ERR
))
3026 TRACE_RET(chip
, STATUS_FAIL
);
3028 if ((tmp
& (MS_INT_BREQ
| MS_INT_CED
)) == MS_INT_BREQ
) {
3029 ms_card
->pro_under_formatting
= 1;
3030 ms_card
->progress
= 0;
3031 ms_card
->format_status
= FORMAT_IN_PROGRESS
;
3032 return STATUS_SUCCESS
;
3035 if (tmp
& MS_INT_CED
) {
3036 ms_card
->pro_under_formatting
= 0;
3037 ms_card
->progress
= 0;
3038 ms_card
->format_status
= FORMAT_SUCCESS
;
3039 set_sense_type(chip
, SCSI_LUN(srb
), SENSE_TYPE_NO_SENSE
);
3040 return STATUS_SUCCESS
;
3043 TRACE_RET(chip
, STATUS_FAIL
);
3047 static int ms_read_multiple_pages(struct rts51x_chip
*chip
, u16 phy_blk
,
3048 u16 log_blk
, u8 start_page
, u8 end_page
,
3049 u8
*buf
, void **ptr
, unsigned int *offset
)
3051 struct ms_info
*ms_card
= &(chip
->ms_card
);
3054 u8 extra
[MS_EXTRA_SIZE
], val1
, val2
, data
[6];
3055 u8 page_cnt
= end_page
- start_page
, page_addr
, sec_cnt
;
3057 if (end_page
!= (ms_card
->page_off
+ 1))
3063 ms_read_extra_data(chip
, phy_blk
, start_page
, extra
, MS_EXTRA_SIZE
);
3064 if (retval
== STATUS_SUCCESS
) {
3065 if ((extra
[1] & 0x30) != 0x30) {
3066 ms_set_err_code(chip
, MS_FLASH_READ_ERROR
);
3067 TRACE_RET(chip
, STATUS_FAIL
);
3071 if (CHK_MS4BIT(ms_card
)) {
3072 /* Parallel interface */
3075 /* Serial interface */
3080 data
[2] = (u8
) (phy_blk
>> 8);
3081 data
[3] = (u8
) phy_blk
;
3083 * Extra data access mode */
3085 data
[5] = start_page
;
3088 ms_auto_set_cmd(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
, 6,
3089 BLOCK_READ
, WAIT_INT
, data
, 6, &val1
);
3090 if (retval
!= STATUS_SUCCESS
)
3091 TRACE_RET(chip
, retval
);
3093 rts51x_init_cmd(chip
);
3096 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_BLKEND
, SET_BLKEND
,
3099 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_BLKEND
, SET_BLKEND
, 0);
3100 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANS_CFG
, WAIT_INT
,
3102 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_SECTOR_CNT_L
, 0xFF,
3104 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_SECTOR_CNT_H
, 0xFF, 0);
3105 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TPC
, 0xFF, READ_PAGE_DATA
);
3107 trans_dma_enable(DMA_FROM_DEVICE
, chip
, 512 * page_cnt
, DMA_512
);
3109 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANSFER
, 0xFF,
3110 MS_TRANSFER_START
| MS_TM_MULTI_READ
);
3111 rts51x_add_cmd(chip
, CHECK_REG_CMD
, MS_TRANSFER
, MS_TRANSFER_END
,
3114 retval
= rts51x_send_cmd(chip
, MODE_CDIR
| STAGE_MS_STATUS
, 100);
3115 if (retval
!= STATUS_SUCCESS
)
3116 TRACE_RET(chip
, retval
);
3119 rts51x_transfer_data_partial(chip
, RCV_BULK_PIPE(chip
), (void *)buf
,
3120 ptr
, offset
, 512 * page_cnt
,
3121 scsi_sg_count(chip
->srb
), NULL
, 2000);
3122 if (retval
!= STATUS_SUCCESS
) {
3123 rts51x_clear_ms_error(chip
);
3124 if (retval
== STATUS_TIMEDOUT
)
3125 TRACE_RET(chip
, retval
);
3126 TRACE_GOTO(chip
, Fail
);
3128 retval
= rts51x_get_rsp(chip
, 3, 200);
3129 if (CHECK_MS_TRANS_FAIL(chip
, retval
)) {
3130 rts51x_clear_ms_error(chip
);
3131 if (retval
== STATUS_TIMEDOUT
)
3132 TRACE_RET(chip
, retval
);
3133 TRACE_GOTO(chip
, Fail
);
3136 return STATUS_SUCCESS
;
3139 rts51x_init_cmd(chip
);
3141 rts51x_add_cmd(chip
, READ_REG_CMD
, MS_SECTOR_CNT_L
, 0, 0);
3143 retval
= rts51x_send_cmd(chip
, MODE_CR
| STAGE_MS_STATUS
, 100);
3144 if (retval
!= STATUS_SUCCESS
)
3145 TRACE_RET(chip
, retval
);
3147 retval
= rts51x_get_rsp(chip
, 3, 200);
3149 if (CHECK_MS_TRANS_FAIL(chip
, retval
))
3150 TRACE_RET(chip
, STATUS_FAIL
);
3152 sec_cnt
= chip
->rsp_buf
[0];
3153 RTS51X_DEBUGP("%d pages need be trasferred, %d pages remained\n",
3154 (int)page_cnt
, (int)sec_cnt
);
3155 page_addr
= start_page
+ (page_cnt
- sec_cnt
);
3157 if (CHK_MS4BIT(ms_card
)) {
3158 val1
= chip
->rsp_buf
[1];
3159 RTS51X_DEBUGP("MS_TRANS_CFG: 0x%x\n", val1
);
3164 val2
= chip
->rsp_buf
[2];
3165 RTS51X_DEBUGP("GET_INT: 0x%x\n", val2
);
3167 if ((val1
& INT_CMDNK
) || (val2
& INT_REG_CMDNK
)) {
3168 ms_set_err_code(chip
, MS_CMD_NK
);
3169 TRACE_RET(chip
, STATUS_FAIL
);
3172 if ((val1
& INT_ERR
) || (val2
& INT_REG_ERR
)) {
3173 if ((val1
& INT_BREQ
) || (val2
& INT_REG_BREQ
)) {
3174 retval
= ms_read_status_reg(chip
);
3175 if (retval
!= STATUS_SUCCESS
) {
3176 if (!(chip
->card_wp
& MS_CARD
)) {
3178 ms_set_page_status(log_blk
, setPS_NG
,
3179 extra
, MS_EXTRA_SIZE
);
3180 ms_write_extra_data(chip
, phy_blk
,
3184 ms_set_err_code(chip
, MS_FLASH_READ_ERROR
);
3185 TRACE_RET(chip
, STATUS_FAIL
);
3188 ms_set_err_code(chip
, MS_FLASH_READ_ERROR
);
3189 TRACE_RET(chip
, STATUS_FAIL
);
3192 if (CHK_MS4BIT(ms_card
)) {
3193 if (!(val1
& INT_BREQ
) && !(val2
& INT_REG_BREQ
)) {
3194 ms_set_err_code(chip
, MS_BREQ_ERROR
);
3195 TRACE_RET(chip
, STATUS_FAIL
);
3198 if (!(val2
& INT_REG_BREQ
)) {
3199 ms_set_err_code(chip
, MS_BREQ_ERROR
);
3200 TRACE_RET(chip
, STATUS_FAIL
);
3205 TRACE_RET(chip
, STATUS_FAIL
);
3208 static int ms_write_multiple_pages(struct rts51x_chip
*chip
, u16 old_blk
,
3209 u16 new_blk
, u16 log_blk
, u8 start_page
,
3210 u8 end_page
, u8
*buf
, void **ptr
,
3211 unsigned int *offset
)
3213 struct ms_info
*ms_card
= &(chip
->ms_card
);
3217 u8 page_cnt
= end_page
- start_page
;
3219 if ((end_page
== (ms_card
->page_off
+ 1)) || (page_cnt
== 1))
3225 if (CHK_MS4BIT(ms_card
)) {
3226 /* Parallel interface */
3229 /* Serial interface */
3234 data
[2] = (u8
) (old_blk
>> 8);
3235 data
[3] = (u8
) old_blk
;
3242 ms_auto_set_cmd(chip
, OverwriteFlag
, MS_EXTRA_SIZE
,
3243 SystemParm
, 7, BLOCK_WRITE
, WAIT_INT
, data
,
3245 if (retval
!= STATUS_SUCCESS
)
3246 TRACE_RET(chip
, retval
);
3250 ms_set_rw_reg_addr(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
,
3251 (6 + MS_EXTRA_SIZE
));
3252 if (retval
!= STATUS_SUCCESS
)
3253 TRACE_RET(chip
, retval
);
3255 ms_set_err_code(chip
, MS_NO_ERROR
);
3257 if (CHK_MS4BIT(ms_card
)) {
3258 /* Parallel interface */
3261 /* Serial interface */
3266 data
[2] = (u8
) (new_blk
>> 8);
3267 data
[3] = (u8
) new_blk
;
3269 * Extra data access mode */
3270 if (page_cnt
== 1) {
3271 /* Single page access mode */
3274 /* Block access mode */
3277 data
[5] = start_page
;
3280 data
[8] = (u8
) (log_blk
>> 8);
3281 data
[9] = (u8
) log_blk
;
3283 for (i
= 0x0A; i
< 0x10; i
++) {
3289 ms_auto_set_cmd(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
,
3290 (6 + MS_EXTRA_SIZE
), BLOCK_WRITE
, WAIT_INT
, data
,
3292 if (retval
!= STATUS_SUCCESS
)
3293 TRACE_RET(chip
, retval
);
3295 rts51x_init_cmd(chip
);
3298 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_BLKEND
, SET_BLKEND
,
3301 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_BLKEND
, SET_BLKEND
, 0);
3302 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANS_CFG
, WAIT_INT
,
3304 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_SECTOR_CNT_L
, 0xFF,
3306 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_SECTOR_CNT_H
, 0xFF, 0);
3307 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TPC
, 0xFF, WRITE_PAGE_DATA
);
3308 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
, 0x01,
3311 trans_dma_enable(DMA_TO_DEVICE
, chip
, 512 * page_cnt
, DMA_512
);
3313 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANSFER
, 0xFF,
3314 MS_TRANSFER_START
| MS_TM_MULTI_WRITE
);
3315 rts51x_add_cmd(chip
, CHECK_REG_CMD
, MS_TRANSFER
, MS_TRANSFER_END
,
3318 retval
= rts51x_send_cmd(chip
, MODE_CDOR
| STAGE_MS_STATUS
, 100);
3319 if (retval
!= STATUS_SUCCESS
)
3320 TRACE_RET(chip
, retval
);
3323 rts51x_transfer_data_partial(chip
, SND_BULK_PIPE(chip
), (void *)buf
,
3324 ptr
, offset
, 512 * page_cnt
,
3325 scsi_sg_count(chip
->srb
), NULL
, 2000);
3326 if (retval
!= STATUS_SUCCESS
) {
3327 rts51x_clear_ms_error(chip
);
3328 TRACE_RET(chip
, retval
);
3331 retval
= rts51x_get_rsp(chip
, 3, 2000);
3334 if (CHECK_MS_TRANS_FAIL(chip
, retval
)) {
3335 rts51x_clear_ms_error(chip
);
3336 TRACE_RET(chip
, STATUS_FAIL
);
3339 return STATUS_SUCCESS
;
3344 static int ms_read_multiple_pages(struct rts51x_chip
*chip
, u16 phy_blk
,
3345 u16 log_blk
, u8 start_page
, u8 end_page
,
3346 u8
*buf
, void **ptr
, unsigned int *offset
)
3348 struct ms_info
*ms_card
= &(chip
->ms_card
);
3350 u8 extra
[MS_EXTRA_SIZE
], page_addr
, val
, trans_cfg
, data
[6];
3353 ms_read_extra_data(chip
, phy_blk
, start_page
, extra
, MS_EXTRA_SIZE
);
3354 if (retval
== STATUS_SUCCESS
) {
3355 if ((extra
[1] & 0x30) != 0x30) {
3356 ms_set_err_code(chip
, MS_FLASH_READ_ERROR
);
3357 TRACE_RET(chip
, STATUS_FAIL
);
3362 ms_set_rw_reg_addr(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
,
3364 if (retval
!= STATUS_SUCCESS
)
3365 TRACE_RET(chip
, retval
);
3367 if (CHK_MS4BIT(ms_card
)) {
3368 /* Parallel interface */
3371 /* Serial interface */
3376 data
[2] = (u8
) (phy_blk
>> 8);
3377 data
[3] = (u8
) phy_blk
;
3379 * Extra data access mode */
3381 data
[5] = start_page
;
3383 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
3385 ms_write_bytes(chip
, WRITE_REG
, 6, NO_WAIT_INT
, data
, 6);
3386 if (retval
== STATUS_SUCCESS
)
3389 if (i
== MS_MAX_RETRY_COUNT
)
3390 TRACE_RET(chip
, STATUS_FAIL
);
3392 ms_set_err_code(chip
, MS_NO_ERROR
);
3394 retval
= ms_send_cmd(chip
, BLOCK_READ
, WAIT_INT
);
3395 if (retval
!= STATUS_SUCCESS
)
3396 TRACE_RET(chip
, retval
);
3398 for (page_addr
= start_page
; page_addr
< end_page
; page_addr
++) {
3399 ms_set_err_code(chip
, MS_NO_ERROR
);
3401 if (monitor_card_cd(chip
, MS_CARD
) == CD_NOT_EXIST
) {
3402 ms_set_err_code(chip
, MS_NO_CARD
);
3403 chip
->card_exist
&= ~MS_CARD
;
3404 chip
->card_ready
&= ~MS_CARD
;
3405 TRACE_RET(chip
, STATUS_FAIL
);
3407 /* GET_INT Register */
3408 retval
= ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
, 1);
3409 if (retval
!= STATUS_SUCCESS
)
3410 TRACE_RET(chip
, retval
);
3411 if (val
& INT_REG_CMDNK
) {
3412 ms_set_err_code(chip
, MS_CMD_NK
);
3413 TRACE_RET(chip
, STATUS_FAIL
);
3415 if (val
& INT_REG_ERR
) {
3416 if (val
& INT_REG_BREQ
) {
3417 retval
= ms_read_status_reg(chip
);
3418 if (retval
!= STATUS_SUCCESS
) {
3419 if (!(chip
->card_wp
& MS_CARD
)) {
3421 ms_set_page_status(log_blk
,
3424 ms_write_extra_data(chip
,
3426 extra
, MS_EXTRA_SIZE
);
3428 ms_set_err_code(chip
,
3429 MS_FLASH_READ_ERROR
);
3430 TRACE_RET(chip
, STATUS_FAIL
);
3433 ms_set_err_code(chip
, MS_FLASH_READ_ERROR
);
3434 TRACE_RET(chip
, STATUS_FAIL
);
3437 if (!(val
& INT_REG_BREQ
)) {
3438 ms_set_err_code(chip
, MS_BREQ_ERROR
);
3439 TRACE_RET(chip
, STATUS_FAIL
);
3443 if (page_addr
== (end_page
- 1)) {
3444 if (!(val
& INT_REG_CED
)) {
3445 retval
= ms_send_cmd(chip
, BLOCK_END
, WAIT_INT
);
3446 if (retval
!= STATUS_SUCCESS
)
3447 TRACE_RET(chip
, retval
);
3450 ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
,
3452 if (retval
!= STATUS_SUCCESS
)
3453 TRACE_RET(chip
, retval
);
3454 if (!(val
& INT_REG_CED
)) {
3455 ms_set_err_code(chip
, MS_FLASH_READ_ERROR
);
3456 TRACE_RET(chip
, STATUS_FAIL
);
3459 trans_cfg
= NO_WAIT_INT
;
3461 trans_cfg
= WAIT_INT
;
3464 rts51x_init_cmd(chip
);
3466 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TPC
, 0xFF,
3468 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANS_CFG
, 0xFF,
3471 trans_dma_enable(DMA_FROM_DEVICE
, chip
, 512, DMA_512
);
3473 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANSFER
, 0xFF,
3474 MS_TRANSFER_START
| MS_TM_NORMAL_READ
);
3475 rts51x_add_cmd(chip
, CHECK_REG_CMD
, MS_TRANSFER
,
3476 MS_TRANSFER_END
, MS_TRANSFER_END
);
3478 retval
= rts51x_send_cmd(chip
, MODE_CDIR
, 100);
3479 if (retval
!= STATUS_SUCCESS
)
3480 TRACE_RET(chip
, retval
);
3483 rts51x_transfer_data_partial(chip
, RCV_BULK_PIPE(chip
),
3484 (void *)buf
, ptr
, offset
, 512,
3485 scsi_sg_count(chip
->srb
), NULL
,
3487 if (retval
!= STATUS_SUCCESS
) {
3488 if (retval
== STATUS_TIMEDOUT
) {
3489 ms_set_err_code(chip
, MS_TO_ERROR
);
3490 rts51x_clear_ms_error(chip
);
3491 TRACE_RET(chip
, retval
);
3495 rts51x_ep0_read_register(chip
, MS_TRANS_CFG
, &val
);
3496 if (retval
!= STATUS_SUCCESS
) {
3497 ms_set_err_code(chip
, MS_TO_ERROR
);
3498 rts51x_clear_ms_error(chip
);
3499 TRACE_RET(chip
, STATUS_FAIL
);
3501 if (val
& (MS_CRC16_ERR
| MS_RDY_TIMEOUT
)) {
3502 ms_set_err_code(chip
, MS_CRC16_ERROR
);
3503 rts51x_clear_ms_error(chip
);
3504 TRACE_RET(chip
, STATUS_FAIL
);
3508 retval
= rts51x_get_rsp(chip
, 1, 2000);
3509 if (CHECK_MS_TRANS_FAIL(chip
, retval
)) {
3510 if (retval
== STATUS_TIMEDOUT
) {
3511 ms_set_err_code(chip
, MS_TO_ERROR
);
3512 rts51x_clear_ms_error(chip
);
3513 TRACE_RET(chip
, retval
);
3517 rts51x_ep0_read_register(chip
, MS_TRANS_CFG
, &val
);
3518 if (retval
!= STATUS_SUCCESS
) {
3519 ms_set_err_code(chip
, MS_TO_ERROR
);
3520 rts51x_clear_ms_error(chip
);
3521 TRACE_RET(chip
, retval
);
3523 if (val
& (MS_CRC16_ERR
| MS_RDY_TIMEOUT
)) {
3524 ms_set_err_code(chip
, MS_CRC16_ERROR
);
3525 rts51x_clear_ms_error(chip
);
3526 TRACE_RET(chip
, STATUS_FAIL
);
3531 return STATUS_SUCCESS
;
3534 static int ms_write_multiple_pages(struct rts51x_chip
*chip
, u16 old_blk
,
3535 u16 new_blk
, u16 log_blk
, u8 start_page
,
3536 u8 end_page
, u8
*buf
, void **ptr
,
3537 unsigned int *offset
)
3539 struct ms_info
*ms_card
= &(chip
->ms_card
);
3541 u8 page_addr
, val
, data
[16];
3545 ms_set_rw_reg_addr(chip
, OverwriteFlag
, MS_EXTRA_SIZE
,
3547 if (retval
!= STATUS_SUCCESS
)
3548 TRACE_RET(chip
, retval
);
3550 if (CHK_MS4BIT(ms_card
)) {
3551 /* Parallel interface */
3554 /* Serial interface */
3559 data
[2] = (u8
) (old_blk
>> 8);
3560 data
[3] = (u8
) old_blk
;
3567 ms_write_bytes(chip
, WRITE_REG
, 7, NO_WAIT_INT
, data
, 8);
3568 if (retval
!= STATUS_SUCCESS
)
3569 TRACE_RET(chip
, retval
);
3571 retval
= ms_send_cmd(chip
, BLOCK_WRITE
, WAIT_INT
);
3572 if (retval
!= STATUS_SUCCESS
)
3573 TRACE_RET(chip
, retval
);
3574 /* GET_INT Register */
3575 ms_set_err_code(chip
, MS_NO_ERROR
);
3577 ms_transfer_tpc(chip
, MS_TM_READ_BYTES
, GET_INT
, 1,
3579 if (retval
!= STATUS_SUCCESS
)
3580 TRACE_RET(chip
, retval
);
3584 ms_set_rw_reg_addr(chip
, OverwriteFlag
, MS_EXTRA_SIZE
, SystemParm
,
3585 (6 + MS_EXTRA_SIZE
));
3586 if (retval
!= STATUS_SUCCESS
)
3587 TRACE_RET(chip
, retval
);
3589 ms_set_err_code(chip
, MS_NO_ERROR
);
3591 if (CHK_MS4BIT(ms_card
)) {
3592 /* Parallel interface */
3595 /* Serial interface */
3600 data
[2] = (u8
) (new_blk
>> 8);
3601 data
[3] = (u8
) new_blk
;
3603 * Extra data access mode */
3604 if ((end_page
- start_page
) == 1) {
3605 /* Single page access mode */
3608 /* Block access mode */
3611 data
[5] = start_page
;
3614 data
[8] = (u8
) (log_blk
>> 8);
3615 data
[9] = (u8
) log_blk
;
3617 for (i
= 0x0A; i
< 0x10; i
++) {
3622 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
3624 ms_write_bytes(chip
, WRITE_REG
, 6 + MS_EXTRA_SIZE
,
3625 NO_WAIT_INT
, data
, 16);
3626 if (retval
== STATUS_SUCCESS
)
3629 if (i
== MS_MAX_RETRY_COUNT
)
3630 TRACE_RET(chip
, STATUS_FAIL
);
3632 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
3633 retval
= ms_send_cmd(chip
, BLOCK_WRITE
, WAIT_INT
);
3634 if (retval
== STATUS_SUCCESS
)
3637 if (i
== MS_MAX_RETRY_COUNT
)
3638 TRACE_RET(chip
, STATUS_FAIL
);
3639 /* GET_INT Register */
3640 retval
= ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
, 1);
3641 if (retval
!= STATUS_SUCCESS
)
3642 TRACE_RET(chip
, retval
);
3644 for (page_addr
= start_page
; page_addr
< end_page
; page_addr
++) {
3645 ms_set_err_code(chip
, MS_NO_ERROR
);
3647 if (monitor_card_cd(chip
, MS_CARD
) == CD_NOT_EXIST
) {
3648 ms_set_err_code(chip
, MS_NO_CARD
);
3649 TRACE_RET(chip
, STATUS_FAIL
);
3652 if (val
& INT_REG_CMDNK
) {
3653 ms_set_err_code(chip
, MS_CMD_NK
);
3654 TRACE_RET(chip
, STATUS_FAIL
);
3656 if (val
& INT_REG_ERR
) {
3657 ms_set_err_code(chip
, MS_FLASH_WRITE_ERROR
);
3658 TRACE_RET(chip
, STATUS_FAIL
);
3660 if (!(val
& INT_REG_BREQ
)) {
3661 ms_set_err_code(chip
, MS_BREQ_ERROR
);
3662 TRACE_RET(chip
, STATUS_FAIL
);
3667 rts51x_init_cmd(chip
);
3669 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TPC
, 0xFF,
3671 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANS_CFG
, 0xFF,
3674 trans_dma_enable(DMA_TO_DEVICE
, chip
, 512, DMA_512
);
3676 rts51x_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANSFER
, 0xFF,
3677 MS_TRANSFER_START
| MS_TM_NORMAL_WRITE
);
3678 rts51x_add_cmd(chip
, CHECK_REG_CMD
, MS_TRANSFER
,
3679 MS_TRANSFER_END
, MS_TRANSFER_END
);
3681 retval
= rts51x_send_cmd(chip
, MODE_CDOR
, 100);
3682 if (retval
!= STATUS_SUCCESS
)
3683 TRACE_RET(chip
, retval
);
3686 rts51x_transfer_data_partial(chip
, SND_BULK_PIPE(chip
),
3687 (void *)buf
, ptr
, offset
, 512,
3688 scsi_sg_count(chip
->srb
), NULL
,
3690 if (retval
!= STATUS_SUCCESS
) {
3691 ms_set_err_code(chip
, MS_TO_ERROR
);
3692 rts51x_clear_ms_error(chip
);
3694 if (retval
== STATUS_TIMEDOUT
)
3695 TRACE_RET(chip
, STATUS_TIMEDOUT
);
3697 TRACE_RET(chip
, STATUS_FAIL
);
3700 retval
= rts51x_get_rsp(chip
, 1, 2000);
3701 if (CHECK_MS_TRANS_FAIL(chip
, retval
)) {
3702 ms_set_err_code(chip
, MS_TO_ERROR
);
3703 rts51x_clear_ms_error(chip
);
3705 if (retval
== STATUS_TIMEDOUT
)
3706 TRACE_RET(chip
, STATUS_TIMEDOUT
);
3708 TRACE_RET(chip
, STATUS_FAIL
);
3710 /* GET_INT Register */
3711 retval
= ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
, 1);
3712 if (retval
!= STATUS_SUCCESS
)
3713 TRACE_RET(chip
, retval
);
3715 if ((end_page
- start_page
) == 1) {
3716 if (!(val
& INT_REG_CED
)) {
3717 /* Command can not be executed */
3718 ms_set_err_code(chip
, MS_FLASH_WRITE_ERROR
);
3719 TRACE_RET(chip
, STATUS_FAIL
);
3722 if (page_addr
== (end_page
- 1)) {
3723 if (!(val
& INT_REG_CED
)) {
3725 ms_send_cmd(chip
, BLOCK_END
,
3727 if (retval
!= STATUS_SUCCESS
)
3728 TRACE_RET(chip
, retval
);
3730 /* GET_INT Register */
3732 ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
,
3734 if (retval
!= STATUS_SUCCESS
)
3735 TRACE_RET(chip
, retval
);
3738 if ((page_addr
== (end_page
- 1))
3739 || (page_addr
== ms_card
->page_off
)) {
3740 if (!(val
& INT_REG_CED
)) {
3741 ms_set_err_code(chip
,
3742 MS_FLASH_WRITE_ERROR
);
3743 TRACE_RET(chip
, STATUS_FAIL
);
3749 return STATUS_SUCCESS
;
3753 static int ms_finish_write(struct rts51x_chip
*chip
, u16 old_blk
, u16 new_blk
,
3754 u16 log_blk
, u8 page_off
)
3756 struct ms_info
*ms_card
= &(chip
->ms_card
);
3760 retval
= ms_auto_copy_page(chip
, old_blk
, new_blk
, log_blk
,
3761 page_off
, ms_card
->page_off
+ 1);
3763 retval
= ms_copy_page(chip
, old_blk
, new_blk
, log_blk
,
3764 page_off
, ms_card
->page_off
+ 1);
3766 if (retval
!= STATUS_SUCCESS
)
3767 TRACE_RET(chip
, retval
);
3769 seg_no
= old_blk
>> 9;
3771 if (MS_TST_BAD_BLOCK_FLG(ms_card
)) {
3772 MS_CLR_BAD_BLOCK_FLG(ms_card
);
3773 ms_set_bad_block(chip
, old_blk
);
3775 retval
= ms_erase_block(chip
, old_blk
);
3776 if (retval
== STATUS_SUCCESS
)
3777 ms_set_unused_block(chip
, old_blk
);
3780 ms_set_l2p_tbl(chip
, seg_no
, log_blk
- ms_start_idx
[seg_no
], new_blk
);
3782 return STATUS_SUCCESS
;
3785 static int ms_prepare_write(struct rts51x_chip
*chip
, u16 old_blk
, u16 new_blk
,
3786 u16 log_blk
, u8 start_page
)
3793 ms_auto_copy_page(chip
, old_blk
, new_blk
, log_blk
, 0,
3797 ms_copy_page(chip
, old_blk
, new_blk
, log_blk
, 0,
3800 if (retval
!= STATUS_SUCCESS
)
3801 TRACE_RET(chip
, retval
);
3804 return STATUS_SUCCESS
;
3807 int ms_delay_write(struct rts51x_chip
*chip
)
3809 struct ms_info
*ms_card
= &(chip
->ms_card
);
3810 struct ms_delay_write_tag
*delay_write
= &(ms_card
->delay_write
);
3813 if (delay_write
->delay_write_flag
) {
3814 retval
= ms_set_init_para(chip
);
3815 if (retval
!= STATUS_SUCCESS
)
3816 TRACE_RET(chip
, retval
);
3818 delay_write
->delay_write_flag
= 0;
3819 retval
= ms_finish_write(chip
,
3820 delay_write
->old_phyblock
,
3821 delay_write
->new_phyblock
,
3822 delay_write
->logblock
,
3823 delay_write
->pageoff
);
3824 if (retval
!= STATUS_SUCCESS
)
3825 TRACE_RET(chip
, retval
);
3828 return STATUS_SUCCESS
;
3831 static inline void ms_rw_fail(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
)
3833 if (srb
->sc_data_direction
== DMA_FROM_DEVICE
)
3834 set_sense_type(chip
, SCSI_LUN(srb
),
3835 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR
);
3837 set_sense_type(chip
, SCSI_LUN(srb
), SENSE_TYPE_MEDIA_WRITE_ERR
);
3840 static int ms_rw_multi_sector(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
,
3841 u32 start_sector
, u16 sector_cnt
)
3843 struct ms_info
*ms_card
= &(chip
->ms_card
);
3844 unsigned int lun
= SCSI_LUN(srb
);
3846 unsigned int offset
= 0;
3847 u16 old_blk
= 0, new_blk
= 0, log_blk
, total_sec_cnt
= sector_cnt
;
3848 u8 start_page
, end_page
= 0, page_cnt
;
3851 struct ms_delay_write_tag
*delay_write
= &(ms_card
->delay_write
);
3853 ms_set_err_code(chip
, MS_NO_ERROR
);
3855 ms_card
->counter
= 0;
3857 buf
= (u8
*) scsi_sglist(srb
);
3859 retval
= ms_switch_clock(chip
);
3860 if (retval
!= STATUS_SUCCESS
) {
3861 ms_rw_fail(srb
, chip
);
3862 TRACE_RET(chip
, retval
);
3865 log_blk
= (u16
) (start_sector
>> ms_card
->block_shift
);
3866 start_page
= (u8
) (start_sector
& ms_card
->page_off
);
3868 for (seg_no
= 0; seg_no
< sizeof(ms_start_idx
) / 2; seg_no
++) {
3869 if (log_blk
< ms_start_idx
[seg_no
+ 1])
3873 if (ms_card
->segment
[seg_no
].build_flag
== 0) {
3874 retval
= ms_build_l2p_tbl(chip
, seg_no
);
3875 if (retval
!= STATUS_SUCCESS
) {
3876 chip
->card_fail
|= MS_CARD
;
3877 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_NOT_PRESENT
);
3878 TRACE_RET(chip
, retval
);
3882 if (srb
->sc_data_direction
== DMA_TO_DEVICE
) {
3883 if (delay_write
->delay_write_flag
&&
3884 (delay_write
->logblock
== log_blk
) &&
3885 (start_page
> delay_write
->pageoff
)) {
3886 delay_write
->delay_write_flag
= 0;
3888 retval
= ms_auto_copy_page(chip
,
3889 delay_write
->old_phyblock
,
3890 delay_write
->new_phyblock
,
3892 delay_write
->pageoff
,
3895 retval
= ms_copy_page(chip
,
3896 delay_write
->old_phyblock
,
3897 delay_write
->new_phyblock
,
3898 log_blk
, delay_write
->pageoff
,
3901 if (retval
!= STATUS_SUCCESS
) {
3902 set_sense_type(chip
, lun
,
3903 SENSE_TYPE_MEDIA_WRITE_ERR
);
3904 TRACE_RET(chip
, retval
);
3906 old_blk
= delay_write
->old_phyblock
;
3907 new_blk
= delay_write
->new_phyblock
;
3908 } else if (delay_write
->delay_write_flag
&&
3909 (delay_write
->logblock
== log_blk
) &&
3910 (start_page
== delay_write
->pageoff
)) {
3911 delay_write
->delay_write_flag
= 0;
3912 old_blk
= delay_write
->old_phyblock
;
3913 new_blk
= delay_write
->new_phyblock
;
3915 retval
= ms_delay_write(chip
);
3916 if (retval
!= STATUS_SUCCESS
) {
3917 set_sense_type(chip
, lun
,
3918 SENSE_TYPE_MEDIA_WRITE_ERR
);
3919 TRACE_RET(chip
, retval
);
3922 ms_get_l2p_tbl(chip
, seg_no
,
3923 log_blk
- ms_start_idx
[seg_no
]);
3924 new_blk
= ms_get_unused_block(chip
, seg_no
);
3925 if ((old_blk
== 0xFFFF) || (new_blk
== 0xFFFF)) {
3926 set_sense_type(chip
, lun
,
3927 SENSE_TYPE_MEDIA_WRITE_ERR
);
3928 TRACE_RET(chip
, STATUS_FAIL
);
3932 ms_prepare_write(chip
, old_blk
, new_blk
, log_blk
,
3934 if (retval
!= STATUS_SUCCESS
) {
3935 if (monitor_card_cd(chip
, MS_CARD
) ==
3937 set_sense_type(chip
, lun
,
3938 SENSE_TYPE_MEDIA_NOT_PRESENT
);
3939 TRACE_RET(chip
, STATUS_FAIL
);
3942 set_sense_type(chip
, lun
,
3943 SENSE_TYPE_MEDIA_WRITE_ERR
);
3944 TRACE_RET(chip
, retval
);
3948 retval
= ms_delay_write(chip
);
3949 if (retval
!= STATUS_SUCCESS
) {
3950 if (monitor_card_cd(chip
, MS_CARD
) == CD_NOT_EXIST
) {
3951 set_sense_type(chip
, lun
,
3952 SENSE_TYPE_MEDIA_NOT_PRESENT
);
3953 TRACE_RET(chip
, STATUS_FAIL
);
3956 set_sense_type(chip
, lun
,
3957 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR
);
3958 TRACE_RET(chip
, retval
);
3961 ms_get_l2p_tbl(chip
, seg_no
,
3962 log_blk
- ms_start_idx
[seg_no
]);
3963 if (old_blk
== 0xFFFF) {
3964 set_sense_type(chip
, lun
,
3965 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR
);
3966 TRACE_RET(chip
, STATUS_FAIL
);
3970 RTS51X_DEBUGP("seg_no = %d, old_blk = 0x%x, new_blk = 0x%x\n", seg_no
,
3973 while (total_sec_cnt
) {
3974 if ((start_page
+ total_sec_cnt
) > (ms_card
->page_off
+ 1))
3975 end_page
= ms_card
->page_off
+ 1;
3977 end_page
= start_page
+ (u8
) total_sec_cnt
;
3978 page_cnt
= end_page
- start_page
;
3980 RTS51X_DEBUGP("start_page = %d, end_page = %d,"
3982 start_page
, end_page
, page_cnt
);
3984 if (srb
->sc_data_direction
== DMA_FROM_DEVICE
)
3985 retval
= ms_read_multiple_pages(chip
,
3987 start_page
, end_page
,
3988 buf
, &ptr
, &offset
);
3990 retval
= ms_write_multiple_pages(chip
, old_blk
,
3992 start_page
, end_page
,
3993 buf
, &ptr
, &offset
);
3995 if (retval
!= STATUS_SUCCESS
) {
3996 if (monitor_card_cd(chip
, MS_CARD
) == CD_NOT_EXIST
) {
3997 set_sense_type(chip
, lun
,
3998 SENSE_TYPE_MEDIA_NOT_PRESENT
);
3999 TRACE_RET(chip
, STATUS_FAIL
);
4002 ms_rw_fail(srb
, chip
);
4003 TRACE_RET(chip
, retval
);
4005 /* Update L2P table if need */
4006 if (srb
->sc_data_direction
== DMA_TO_DEVICE
) {
4007 if (end_page
== (ms_card
->page_off
+ 1)) {
4008 retval
= ms_erase_block(chip
, old_blk
);
4009 if (retval
== STATUS_SUCCESS
)
4010 ms_set_unused_block(chip
, old_blk
);
4011 ms_set_l2p_tbl(chip
, seg_no
,
4012 log_blk
- ms_start_idx
[seg_no
],
4017 total_sec_cnt
-= page_cnt
;
4019 if (total_sec_cnt
== 0)
4024 for (seg_no
= 0; seg_no
< sizeof(ms_start_idx
) / 2; seg_no
++) {
4025 if (log_blk
< ms_start_idx
[seg_no
+ 1])
4029 if (ms_card
->segment
[seg_no
].build_flag
== 0) {
4030 retval
= ms_build_l2p_tbl(chip
, seg_no
);
4031 if (retval
!= STATUS_SUCCESS
) {
4032 chip
->card_fail
|= MS_CARD
;
4033 set_sense_type(chip
, lun
,
4034 SENSE_TYPE_MEDIA_NOT_PRESENT
);
4035 TRACE_RET(chip
, retval
);
4040 ms_get_l2p_tbl(chip
, seg_no
,
4041 log_blk
- ms_start_idx
[seg_no
]);
4042 if (old_blk
== 0xFFFF) {
4043 ms_rw_fail(srb
, chip
);
4044 TRACE_RET(chip
, STATUS_FAIL
);
4047 if (srb
->sc_data_direction
== DMA_TO_DEVICE
) {
4048 new_blk
= ms_get_unused_block(chip
, seg_no
);
4049 if (new_blk
== 0xFFFF) {
4050 ms_rw_fail(srb
, chip
);
4051 TRACE_RET(chip
, STATUS_FAIL
);
4055 RTS51X_DEBUGP("seg_no = %d, old_blk = 0x%x, new_blk = 0x%x\n",
4056 seg_no
, old_blk
, new_blk
);
4061 if (srb
->sc_data_direction
== DMA_TO_DEVICE
) {
4062 if (end_page
< (ms_card
->page_off
+ 1)) {
4063 delay_write
->delay_write_flag
= 1;
4064 delay_write
->old_phyblock
= old_blk
;
4065 delay_write
->new_phyblock
= new_blk
;
4066 delay_write
->logblock
= log_blk
;
4067 delay_write
->pageoff
= end_page
;
4071 scsi_set_resid(srb
, 0);
4073 return STATUS_SUCCESS
;
4076 int ms_rw(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
, u32 start_sector
,
4079 struct ms_info
*ms_card
= &(chip
->ms_card
);
4082 if (CHK_MSPRO(ms_card
))
4084 mspro_rw_multi_sector(srb
, chip
, start_sector
, sector_cnt
);
4087 ms_rw_multi_sector(srb
, chip
, start_sector
, sector_cnt
);
4092 void ms_free_l2p_tbl(struct rts51x_chip
*chip
)
4094 struct ms_info
*ms_card
= &(chip
->ms_card
);
4097 if (ms_card
->segment
!= NULL
) {
4098 for (i
= 0; i
< ms_card
->segment_cnt
; i
++) {
4099 if (ms_card
->segment
[i
].l2p_table
!= NULL
) {
4100 vfree(ms_card
->segment
[i
].l2p_table
);
4101 ms_card
->segment
[i
].l2p_table
= NULL
;
4103 if (ms_card
->segment
[i
].free_table
!= NULL
) {
4104 vfree(ms_card
->segment
[i
].free_table
);
4105 ms_card
->segment
[i
].free_table
= NULL
;
4108 vfree(ms_card
->segment
);
4109 ms_card
->segment
= NULL
;
4113 void ms_cleanup_work(struct rts51x_chip
*chip
)
4115 struct ms_info
*ms_card
= &(chip
->ms_card
);
4117 if (CHK_MSPRO(ms_card
)) {
4118 if (ms_card
->seq_mode
) {
4119 RTS51X_DEBUGP("MS Pro: stop transmission\n");
4120 mspro_stop_seq_mode(chip
);
4121 ms_card
->counter
= 0;
4123 if (CHK_MSHG(ms_card
)) {
4125 rts51x_read_register(chip
, MS_CFG
, &value
);
4126 if (value
& MS_2K_SECTOR_MODE
)
4127 rts51x_write_register(chip
, MS_CFG
,
4128 MS_2K_SECTOR_MODE
, 0x00);
4130 } else if ((!CHK_MSPRO(ms_card
))
4131 && ms_card
->delay_write
.delay_write_flag
) {
4132 RTS51X_DEBUGP("MS: delay write\n");
4133 ms_delay_write(chip
);
4134 ms_card
->counter
= 0;
4138 int ms_power_off_card3v3(struct rts51x_chip
*chip
)
4142 rts51x_init_cmd(chip
);
4144 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_CLK_EN
, MS_CLK_EN
, 0);
4145 if (chip
->asic_code
)
4146 ms_pull_ctl_disable(chip
);
4148 rts51x_add_cmd(chip
, WRITE_REG_CMD
, FPGA_PULL_CTL
,
4149 FPGA_MS_PULL_CTL_BIT
| 0x20,
4150 FPGA_MS_PULL_CTL_BIT
);
4151 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_OE
, MS_OUTPUT_EN
, 0);
4152 if (!chip
->option
.FT2_fast_mode
) {
4153 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PWR_CTL
, POWER_MASK
,
4157 retval
= rts51x_send_cmd(chip
, MODE_C
, 100);
4158 if (retval
!= STATUS_SUCCESS
)
4159 TRACE_RET(chip
, retval
);
4161 return STATUS_SUCCESS
;
4164 int release_ms_card(struct rts51x_chip
*chip
)
4166 struct ms_info
*ms_card
= &(chip
->ms_card
);
4169 RTS51X_DEBUGP("release_ms_card\n");
4171 ms_card
->delay_write
.delay_write_flag
= 0;
4172 ms_card
->pro_under_formatting
= 0;
4174 chip
->card_ready
&= ~MS_CARD
;
4175 chip
->card_fail
&= ~MS_CARD
;
4176 chip
->card_wp
&= ~MS_CARD
;
4178 ms_free_l2p_tbl(chip
);
4180 rts51x_write_register(chip
, SFSM_ED
, HW_CMD_STOP
, HW_CMD_STOP
);
4182 memset(ms_card
->raw_sys_info
, 0, 96);
4183 #ifdef SUPPORT_PCGL_1P18
4184 memset(ms_card
->raw_model_name
, 0, 48);
4187 retval
= ms_power_off_card3v3(chip
);
4188 if (retval
!= STATUS_SUCCESS
)
4189 TRACE_RET(chip
, retval
);
4191 return STATUS_SUCCESS
;