1 /* Driver for Realtek RTS51xx USB card reader
3 * Copyright(c) 2009 Realtek Semiconductor Corp. All rights reserved.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2, or (at your option) any
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, see <http://www.gnu.org/licenses/>.
19 * wwang (wei_wang@realsil.com.cn)
20 * No. 450, Shenhu Road, Suzhou Industry Park, Suzhou, China
22 * Edwin Rong (edwin_rong@realsil.com.cn)
23 * No. 450, Shenhu Road, Suzhou Industry Park, Suzhou, China
26 #include <linux/blkdev.h>
27 #include <linux/kthread.h>
28 #include <linux/sched.h>
29 #include <linux/vmalloc.h>
34 #include "rts51x_transport.h"
35 #include "rts51x_scsi.h"
36 #include "rts51x_card.h"
39 static int xd_build_l2p_tbl(struct rts51x_chip
*chip
, int zone_no
);
40 static int xd_init_page(struct rts51x_chip
*chip
, u32 phy_blk
, u16 logoff
,
41 u8 start_page
, u8 end_page
);
43 static inline void xd_set_err_code(struct rts51x_chip
*chip
, u8 err_code
)
45 struct xd_info
*xd_card
= &(chip
->xd_card
);
47 xd_card
->err_code
= err_code
;
50 static inline int xd_check_err_code(struct rts51x_chip
*chip
, u8 err_code
)
52 struct xd_info
*xd_card
= &(chip
->xd_card
);
54 return (xd_card
->err_code
== err_code
);
57 static int xd_set_init_para(struct rts51x_chip
*chip
)
59 struct xd_info
*xd_card
= &(chip
->xd_card
);
63 xd_card
->xd_clock
= 47;
65 xd_card
->xd_clock
= CLK_50
;
67 retval
= switch_clock(chip
, xd_card
->xd_clock
);
68 if (retval
!= STATUS_SUCCESS
)
69 TRACE_RET(chip
, retval
);
71 return STATUS_SUCCESS
;
74 static int xd_switch_clock(struct rts51x_chip
*chip
)
76 struct xd_info
*xd_card
= &(chip
->xd_card
);
79 retval
= rts51x_select_card(chip
, XD_CARD
);
80 if (retval
!= STATUS_SUCCESS
)
81 TRACE_RET(chip
, retval
);
83 retval
= switch_clock(chip
, xd_card
->xd_clock
);
84 if (retval
!= STATUS_SUCCESS
)
85 TRACE_RET(chip
, retval
);
87 return STATUS_SUCCESS
;
90 static int xd_read_id(struct rts51x_chip
*chip
, u8 id_cmd
, u8
*id_buf
,
95 rts51x_init_cmd(chip
);
97 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_DAT
, 0xFF, id_cmd
);
98 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_TRANSFER
, 0xFF,
99 XD_TRANSFER_START
| XD_READ_ID
);
100 rts51x_add_cmd(chip
, CHECK_REG_CMD
, XD_TRANSFER
, XD_TRANSFER_END
,
103 for (i
= 0; i
< 4; i
++) {
104 rts51x_add_cmd(chip
, READ_REG_CMD
, (u16
) (XD_ADDRESS1
+ i
), 0,
108 retval
= rts51x_send_cmd(chip
, MODE_CR
, 20);
109 if (retval
!= STATUS_SUCCESS
)
110 TRACE_RET(chip
, retval
);
112 retval
= rts51x_get_rsp(chip
, 5, 20);
114 if (retval
!= STATUS_SUCCESS
) {
115 rts51x_clear_xd_error(chip
);
116 TRACE_RET(chip
, retval
);
119 if (id_buf
&& buf_len
) {
122 rts51x_read_rsp_buf(chip
, 1, id_buf
, buf_len
);
125 return STATUS_SUCCESS
;
128 static void xd_assign_phy_addr(struct rts51x_chip
*chip
, u32 addr
, u8 mode
)
130 struct xd_info
*xd_card
= &(chip
->xd_card
);
134 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_ADDRESS0
, 0xFF, 0);
135 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_ADDRESS1
, 0xFF,
137 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_ADDRESS2
, 0xFF,
139 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_ADDRESS3
, 0xFF,
141 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_CFG
, 0xFF,
142 xd_card
->addr_cycle
| XD_CALC_ECC
|
147 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_ADDRESS0
, 0xFF,
149 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_ADDRESS1
, 0xFF,
151 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_ADDRESS2
, 0xFF,
153 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_CFG
, 0xFF,
154 (xd_card
->addr_cycle
- 1) |
155 XD_CALC_ECC
| XD_BA_NO_TRANSFORM
);
163 static int xd_read_redundant(struct rts51x_chip
*chip
, u32 page_addr
, u8
*buf
,
168 rts51x_init_cmd(chip
);
170 xd_assign_phy_addr(chip
, page_addr
, XD_RW_ADDR
);
172 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_TRANSFER
, 0xFF,
173 XD_TRANSFER_START
| XD_READ_REDUNDANT
);
174 rts51x_add_cmd(chip
, CHECK_REG_CMD
, XD_TRANSFER
, XD_TRANSFER_END
,
177 for (i
= 0; i
< 6; i
++) {
178 rts51x_add_cmd(chip
, READ_REG_CMD
, (u16
) (XD_PAGE_STATUS
+ i
),
181 for (i
= 0; i
< 4; i
++) {
182 rts51x_add_cmd(chip
, READ_REG_CMD
, (u16
) (XD_RESERVED0
+ i
), 0,
185 rts51x_add_cmd(chip
, READ_REG_CMD
, XD_PARITY
, 0, 0);
187 retval
= rts51x_send_cmd(chip
, MODE_CR
, 100);
188 if (retval
!= STATUS_SUCCESS
)
189 TRACE_RET(chip
, retval
);
191 retval
= rts51x_get_rsp(chip
, 11, 500);
193 if (retval
!= STATUS_SUCCESS
) {
194 rts51x_clear_xd_error(chip
);
195 TRACE_RET(chip
, retval
);
198 if (buf
&& buf_len
) {
201 rts51x_read_rsp_buf(chip
, 1, buf
, buf_len
);
204 return STATUS_SUCCESS
;
207 static int xd_read_data_from_ppb(struct rts51x_chip
*chip
, int offset
, u8
*buf
,
212 if (!buf
|| (buf_len
<= 0))
213 TRACE_RET(chip
, STATUS_FAIL
);
215 rts51x_init_cmd(chip
);
217 for (i
= 0; i
< buf_len
; i
++) {
218 rts51x_add_cmd(chip
, READ_REG_CMD
, PPBUF_BASE2
+ offset
+ i
, 0,
222 retval
= rts51x_send_cmd(chip
, MODE_CR
, 100);
223 if (retval
!= STATUS_SUCCESS
)
224 TRACE_RET(chip
, retval
);
226 retval
= rts51x_get_rsp(chip
, buf_len
, 200);
227 if (retval
!= STATUS_SUCCESS
)
228 TRACE_RET(chip
, retval
);
230 rts51x_read_rsp_buf(chip
, 0, buf
, buf_len
);
232 return STATUS_SUCCESS
;
235 static int xd_read_cis(struct rts51x_chip
*chip
, u32 page_addr
, u8
*buf
,
241 if (!buf
|| (buf_len
< 10))
242 TRACE_RET(chip
, STATUS_FAIL
);
244 rts51x_init_cmd(chip
);
246 xd_assign_phy_addr(chip
, page_addr
, XD_RW_ADDR
);
248 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
, 0x01,
250 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_PAGE_CNT
, 0xFF, 1);
251 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_CHK_DATA_STATUS
,
252 XD_AUTO_CHK_DATA_STATUS
, XD_AUTO_CHK_DATA_STATUS
);
254 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_TRANSFER
, 0xFF,
255 XD_TRANSFER_START
| XD_READ_PAGES
);
256 rts51x_add_cmd(chip
, CHECK_REG_CMD
, XD_TRANSFER
,
257 XD_TRANSFER_END
| XD_PPB_EMPTY
,
258 XD_TRANSFER_END
| XD_PPB_EMPTY
);
260 retval
= rts51x_send_cmd(chip
, MODE_CR
, 100);
261 if (retval
!= STATUS_SUCCESS
)
262 TRACE_RET(chip
, retval
);
264 retval
= rts51x_get_rsp(chip
, 1, 500);
265 if (retval
== STATUS_TIMEDOUT
) {
266 rts51x_clear_xd_error(chip
);
267 TRACE_RET(chip
, retval
);
270 RTS51X_READ_REG(chip
, XD_PAGE_STATUS
, ®
);
272 rts51x_clear_xd_error(chip
);
273 TRACE_RET(chip
, STATUS_FAIL
);
276 RTS51X_READ_REG(chip
, XD_CTL
, ®
);
278 if (!(reg
& XD_ECC1_ERROR
) || !(reg
& XD_ECC1_UNCORRECTABLE
)) {
279 retval
= xd_read_data_from_ppb(chip
, 0, buf
, buf_len
);
280 if (retval
!= STATUS_SUCCESS
)
281 TRACE_RET(chip
, retval
);
282 if (reg
& XD_ECC1_ERROR
) { /* correctable error */
283 u8 ecc_bit
, ecc_byte
;
285 RTS51X_READ_REG(chip
, XD_ECC_BIT1
, &ecc_bit
);
286 RTS51X_READ_REG(chip
, XD_ECC_BYTE1
, &ecc_byte
);
288 RTS51X_DEBUGP("ECC_BIT1 = 0x%x, ECC_BYTE1 = 0x%x\n",
290 if (ecc_byte
< buf_len
) {
291 RTS51X_DEBUGP("Before correct: 0x%x\n",
293 buf
[ecc_byte
] ^= (1 << ecc_bit
);
294 RTS51X_DEBUGP("After correct: 0x%x\n",
298 } else if (!(reg
& XD_ECC2_ERROR
) || !(reg
& XD_ECC2_UNCORRECTABLE
)) {
299 RTS51X_WRITE_REG(chip
, CARD_STOP
, XD_STOP
| XD_CLR_ERR
,
300 XD_STOP
| XD_CLR_ERR
);
302 retval
= xd_read_data_from_ppb(chip
, 256, buf
, buf_len
);
303 if (retval
!= STATUS_SUCCESS
)
304 TRACE_RET(chip
, retval
);
305 if (reg
& XD_ECC2_ERROR
) {
306 u8 ecc_bit
, ecc_byte
;
308 RTS51X_READ_REG(chip
, XD_ECC_BIT2
, &ecc_bit
);
309 RTS51X_READ_REG(chip
, XD_ECC_BYTE2
, &ecc_byte
);
311 RTS51X_DEBUGP("ECC_BIT2 = 0x%x, ECC_BYTE2 = 0x%x\n",
313 if (ecc_byte
< buf_len
) {
314 RTS51X_DEBUGP("Before correct: 0x%x\n",
316 buf
[ecc_byte
] ^= (1 << ecc_bit
);
317 RTS51X_DEBUGP("After correct: 0x%x\n",
322 rts51x_clear_xd_error(chip
);
323 TRACE_RET(chip
, STATUS_FAIL
);
326 return STATUS_SUCCESS
;
329 static void xd_pull_ctl_disable(struct rts51x_chip
*chip
)
331 if (CHECK_PKG(chip
, LQFP48
)) {
332 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL1
, 0xFF, 0x55);
333 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL2
, 0xFF, 0x55);
334 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL3
, 0xFF, 0x95);
335 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL4
, 0xFF, 0x55);
336 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL5
, 0xFF, 0x55);
337 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL6
, 0xFF, 0xA5);
339 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL1
, 0xFF, 0x65);
340 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL2
, 0xFF, 0x55);
341 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL3
, 0xFF, 0x95);
342 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL4
, 0xFF, 0x55);
343 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL5
, 0xFF, 0x56);
344 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL6
, 0xFF, 0x59);
348 static void xd_pull_ctl_enable(struct rts51x_chip
*chip
)
350 if (CHECK_PKG(chip
, LQFP48
)) {
351 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL1
, 0xFF, 0xAA);
352 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL2
, 0xFF, 0x55);
353 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL3
, 0xFF, 0x95);
354 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL4
, 0xFF, 0x55);
355 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL5
, 0xFF, 0x55);
356 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL6
, 0xFF, 0xA5);
358 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL1
, 0xFF, 0xA5);
359 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL2
, 0xFF, 0x59);
360 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL3
, 0xFF, 0x95);
361 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL4
, 0xFF, 0x55);
362 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL5
, 0xFF, 0x55);
363 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL6
, 0xFF, 0x59);
367 static int reset_xd(struct rts51x_chip
*chip
)
369 struct xd_info
*xd_card
= &(chip
->xd_card
);
371 u8 id_buf
[4], redunt
[11];
373 retval
= rts51x_select_card(chip
, XD_CARD
);
374 if (retval
!= STATUS_SUCCESS
)
375 TRACE_RET(chip
, STATUS_FAIL
);
377 rts51x_init_cmd(chip
);
379 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_CHK_DATA_STATUS
, 0xFF,
382 xd_pull_ctl_disable(chip
);
384 rts51x_add_cmd(chip
, WRITE_REG_CMD
, FPGA_PULL_CTL
, 0xFF,
385 (FPGA_XD_PULL_CTL_EN1
& FPGA_XD_PULL_CTL_EN3
));
387 if (!chip
->option
.FT2_fast_mode
) {
388 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_INIT
, XD_NO_AUTO_PWR_OFF
,
390 if (CHECK_PKG(chip
, LQFP48
) ||
391 chip
->option
.rts5129_D3318_off_enable
) {
392 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PWR_CTL
,
394 DV3318_AUTO_PWR_OFF
);
397 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_OE
, XD_OUTPUT_EN
, 0);
398 if (!chip
->option
.FT2_fast_mode
) {
399 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PWR_CTL
, POWER_MASK
,
403 retval
= rts51x_send_cmd(chip
, MODE_C
, 100);
404 if (retval
!= STATUS_SUCCESS
)
405 TRACE_RET(chip
, retval
);
406 if (!chip
->option
.FT2_fast_mode
) {
407 #ifdef SD_XD_IO_FOLLOW_PWR
408 if (CHECK_PKG(chip
, LQFP48
)
409 || chip
->option
.rts5129_D3318_off_enable
) {
410 rts51x_write_register(chip
, CARD_PWR_CTL
,
417 #ifdef SD_XD_IO_FOLLOW_PWR
418 if (CHECK_PKG(chip
, LQFP48
)
419 || chip
->option
.rts5129_D3318_off_enable
) {
420 rts51x_init_cmd(chip
);
421 if (chip
->asic_code
) {
422 xd_pull_ctl_enable(chip
);
424 rts51x_add_cmd(chip
, WRITE_REG_CMD
,
426 (FPGA_XD_PULL_CTL_EN1
&
427 FPGA_XD_PULL_CTL_EN2
));
429 retval
= rts51x_send_cmd(chip
, MODE_C
, 100);
430 if (retval
!= STATUS_SUCCESS
)
431 TRACE_RET(chip
, STATUS_FAIL
);
435 retval
= card_power_on(chip
, XD_CARD
);
436 if (retval
!= STATUS_SUCCESS
)
437 TRACE_RET(chip
, retval
);
440 rts51x_get_card_status(chip
, &(chip
->card_status
));
441 chip
->ocp_stat
= (chip
->card_status
>> 4) & 0x03;
443 if (chip
->ocp_stat
& (MS_OCP_NOW
| MS_OCP_EVER
)) {
444 RTS51X_DEBUGP("Over current, OCPSTAT is 0x%x\n",
446 TRACE_RET(chip
, STATUS_FAIL
);
451 rts51x_init_cmd(chip
);
454 xd_pull_ctl_enable(chip
);
456 rts51x_add_cmd(chip
, WRITE_REG_CMD
, FPGA_PULL_CTL
, 0xFF,
457 (FPGA_XD_PULL_CTL_EN1
& FPGA_XD_PULL_CTL_EN2
));
458 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_OE
, XD_OUTPUT_EN
,
460 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_CTL
, XD_CE_DISEN
, XD_CE_DISEN
);
462 retval
= rts51x_send_cmd(chip
, MODE_C
, 100);
463 if (retval
!= STATUS_SUCCESS
)
464 TRACE_RET(chip
, STATUS_FAIL
);
466 if (!chip
->option
.FT2_fast_mode
)
469 retval
= xd_set_init_para(chip
);
470 if (retval
!= STATUS_SUCCESS
)
471 TRACE_RET(chip
, STATUS_FAIL
);
472 /* Read ID to check if the timing setting is right */
473 for (i
= 0; i
< 4; i
++) {
476 if (monitor_card_cd(chip
, XD_CARD
) == CD_NOT_EXIST
)
477 TRACE_RET(chip
, STATUS_FAIL
);
479 rts51x_init_cmd(chip
);
480 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_DTCTL
, 0xFF,
481 XD_TIME_SETUP_STEP
* 3 + XD_TIME_RW_STEP
*
482 (2 + i
+ chip
->option
.xd_rw_step
)
483 + XD_TIME_RWN_STEP
* (i
+ chip
->option
.xd_rwn_step
));
484 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_CATCTL
, 0xFF,
485 XD_TIME_SETUP_STEP
* 3 + XD_TIME_RW_STEP
* (4 +
486 i
) + XD_TIME_RWN_STEP
* (3 + i
));
488 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_TRANSFER
, 0xFF,
489 XD_TRANSFER_START
| XD_RESET
);
490 rts51x_add_cmd(chip
, CHECK_REG_CMD
, XD_TRANSFER
,
491 XD_TRANSFER_END
, XD_TRANSFER_END
);
493 rts51x_add_cmd(chip
, READ_REG_CMD
, XD_DAT
, 0, 0);
494 rts51x_add_cmd(chip
, READ_REG_CMD
, XD_CTL
, 0, 0);
496 retval
= rts51x_send_cmd(chip
, MODE_CR
, 100);
497 if (retval
!= STATUS_SUCCESS
) {
498 rts51x_clear_xd_error(chip
);
499 TRACE_RET(chip
, retval
);
502 retval
= rts51x_get_rsp(chip
, 3, 100);
503 if (retval
!= STATUS_SUCCESS
) {
504 rts51x_clear_xd_error(chip
);
505 TRACE_RET(chip
, retval
);
508 xd_dat
= chip
->rsp_buf
[1];
509 xd_ctl
= chip
->rsp_buf
[2];
510 RTS51X_DEBUGP("XD_DAT: 0x%x, XD_CTL: 0x%x\n", xd_dat
, xd_ctl
);
512 if (((xd_dat
& READY_FLAG
) != READY_STATE
)
513 || !(xd_ctl
& XD_RDY
))
516 retval
= xd_read_id(chip
, READ_ID
, id_buf
, 4);
517 if (retval
!= STATUS_SUCCESS
)
518 TRACE_RET(chip
, retval
);
520 RTS51X_DEBUGP("READ_ID: 0x%x 0x%x 0x%x 0x%x\n",
521 id_buf
[0], id_buf
[1], id_buf
[2], id_buf
[3]);
523 xd_card
->device_code
= id_buf
[1];
525 switch (xd_card
->device_code
) {
528 xd_card
->block_shift
= 4; /* 16 pages per block */
529 xd_card
->page_off
= 0x0F;
530 xd_card
->addr_cycle
= 3;
531 xd_card
->zone_cnt
= 1;
532 xd_card
->capacity
= 8000; /* 500 * 2 ^ 4 */
536 xd_card
->block_shift
= 4;
537 xd_card
->page_off
= 0x0F;
538 xd_card
->addr_cycle
= 3;
539 xd_card
->zone_cnt
= 1;
540 xd_card
->capacity
= 16000; /* 1000 * 2 ^ 4 */
543 XD_PAGE_512(xd_card
); /* 32 pages per block */
544 xd_card
->addr_cycle
= 3;
545 xd_card
->zone_cnt
= 1;
546 xd_card
->capacity
= 32000; /* 1000 * 2 ^ 5 */
549 XD_PAGE_512(xd_card
);
550 xd_card
->addr_cycle
= 3;
551 xd_card
->zone_cnt
= 2;
552 xd_card
->capacity
= 64000; /* 2000 * 2 ^ 5 */
555 XD_PAGE_512(xd_card
);
556 xd_card
->addr_cycle
= 4;
557 xd_card
->zone_cnt
= 4;
558 xd_card
->capacity
= 128000; /* 4000 * 2 ^ 5 */
561 XD_PAGE_512(xd_card
);
562 xd_card
->addr_cycle
= 4;
563 xd_card
->zone_cnt
= 8;
564 xd_card
->capacity
= 256000; /* 8000 * 2 ^ 5 */
567 XD_PAGE_512(xd_card
);
568 xd_card
->addr_cycle
= 4;
569 xd_card
->zone_cnt
= 16;
570 xd_card
->capacity
= 512000; /* 16000 * 2 ^ 5 */
573 XD_PAGE_512(xd_card
);
574 xd_card
->addr_cycle
= 4;
575 xd_card
->zone_cnt
= 32;
576 xd_card
->capacity
= 1024000; /* 32000 * 2 ^ 5 */
579 XD_PAGE_512(xd_card
);
580 xd_card
->addr_cycle
= 4;
581 xd_card
->zone_cnt
= 64;
582 xd_card
->capacity
= 2048000; /* 64000 * 2 ^ 5 */
585 XD_PAGE_512(xd_card
);
586 xd_card
->addr_cycle
= 4;
587 xd_card
->zone_cnt
= 128;
588 xd_card
->capacity
= 4096000; /* 128000 * 2 ^ 5 */
594 /* Confirm timing setting */
595 for (j
= 0; j
< 10; j
++) {
596 retval
= xd_read_id(chip
, READ_ID
, id_buf
, 4);
597 if (retval
!= STATUS_SUCCESS
)
598 TRACE_RET(chip
, retval
);
600 if (id_buf
[1] != xd_card
->device_code
)
604 /* Current timing pass */
610 xd_card
->block_shift
= 0;
611 xd_card
->page_off
= 0;
612 xd_card
->addr_cycle
= 0;
613 xd_card
->capacity
= 0;
615 TRACE_RET(chip
, STATUS_FAIL
);
618 retval
= xd_read_id(chip
, READ_xD_ID
, id_buf
, 4);
619 if (retval
!= STATUS_SUCCESS
)
620 TRACE_RET(chip
, retval
);
621 RTS51X_DEBUGP("READ_xD_ID: 0x%x 0x%x 0x%x 0x%x\n",
622 id_buf
[0], id_buf
[1], id_buf
[2], id_buf
[3]);
623 if (id_buf
[2] != XD_ID_CODE
)
624 TRACE_RET(chip
, STATUS_FAIL
);
626 /* Search CIS block */
627 for (i
= 0; i
< 24; i
++) {
630 if (monitor_card_cd(chip
, XD_CARD
) == CD_NOT_EXIST
)
631 TRACE_RET(chip
, STATUS_FAIL
);
633 page_addr
= (u32
) i
<< xd_card
->block_shift
;
635 for (j
= 0; j
< 3; j
++) {
636 retval
= xd_read_redundant(chip
, page_addr
, redunt
, 11);
637 if (retval
== STATUS_SUCCESS
)
643 if (redunt
[BLOCK_STATUS
] != XD_GBLK
)
647 /* Check page status */
648 if (redunt
[PAGE_STATUS
] != XD_GPG
) {
649 for (j
= 1; j
<= 8; j
++) {
651 xd_read_redundant(chip
, page_addr
+ j
,
653 if (retval
== STATUS_SUCCESS
) {
654 if (redunt
[PAGE_STATUS
] == XD_GPG
)
663 if ((redunt
[BLOCK_STATUS
] == XD_GBLK
)
664 && (redunt
[PARITY
] & XD_BA1_ALL0
)) {
669 retval
= xd_read_cis(chip
, page_addr
, buf
, 10);
670 if (retval
!= STATUS_SUCCESS
)
671 TRACE_RET(chip
, retval
);
673 if ((buf
[0] == 0x01) && (buf
[1] == 0x03)
675 && (buf
[3] == 0x01) && (buf
[4] == 0xFF)
676 && (buf
[5] == 0x18) && (buf
[6] == 0x02)
677 && (buf
[7] == 0xDF) && (buf
[8] == 0x01)
678 && (buf
[9] == 0x20)) {
679 xd_card
->cis_block
= (u16
) i
;
686 RTS51X_DEBUGP("CIS block: 0x%x\n", xd_card
->cis_block
);
687 if (xd_card
->cis_block
== 0xFFFF)
688 TRACE_RET(chip
, STATUS_FAIL
);
690 chip
->capacity
[chip
->card2lun
[XD_CARD
]] = xd_card
->capacity
;
692 return STATUS_SUCCESS
;
695 static int xd_check_data_blank(u8
*redunt
)
699 for (i
= 0; i
< 6; i
++) {
700 if (redunt
[PAGE_STATUS
+ i
] != 0xFF)
704 if ((redunt
[PARITY
] & (XD_ECC1_ALL1
| XD_ECC2_ALL1
)) !=
705 (XD_ECC1_ALL1
| XD_ECC2_ALL1
))
708 for (i
= 0; i
< 4; i
++) {
709 if (redunt
[RESERVED0
+ i
] != 0xFF)
716 static u16
xd_load_log_block_addr(u8
*redunt
)
720 if (redunt
[PARITY
] & XD_BA1_BA2_EQL
)
722 ((u16
) redunt
[BLOCK_ADDR1_H
] << 8) | redunt
[BLOCK_ADDR1_L
];
723 else if (redunt
[PARITY
] & XD_BA1_VALID
)
725 ((u16
) redunt
[BLOCK_ADDR1_H
] << 8) | redunt
[BLOCK_ADDR1_L
];
726 else if (redunt
[PARITY
] & XD_BA2_VALID
)
728 ((u16
) redunt
[BLOCK_ADDR2_H
] << 8) | redunt
[BLOCK_ADDR2_L
];
733 static int xd_init_l2p_tbl(struct rts51x_chip
*chip
)
735 struct xd_info
*xd_card
= &(chip
->xd_card
);
738 RTS51X_DEBUGP("xd_init_l2p_tbl: zone_cnt = %d\n", xd_card
->zone_cnt
);
740 if (xd_card
->zone_cnt
< 1)
741 TRACE_RET(chip
, STATUS_FAIL
);
743 size
= xd_card
->zone_cnt
* sizeof(struct zone_entry
);
744 RTS51X_DEBUGP("Buffer size for l2p table is %d\n", size
);
746 xd_card
->zone
= vmalloc(size
);
748 TRACE_RET(chip
, STATUS_NOMEM
);
750 for (i
= 0; i
< xd_card
->zone_cnt
; i
++) {
751 xd_card
->zone
[i
].build_flag
= 0;
752 xd_card
->zone
[i
].l2p_table
= NULL
;
753 xd_card
->zone
[i
].free_table
= NULL
;
754 xd_card
->zone
[i
].get_index
= 0;
755 xd_card
->zone
[i
].set_index
= 0;
756 xd_card
->zone
[i
].unused_blk_cnt
= 0;
759 return STATUS_SUCCESS
;
762 static inline void free_zone(struct zone_entry
*zone
)
764 RTS51X_DEBUGP("free_zone\n");
767 zone
->build_flag
= 0;
770 zone
->unused_blk_cnt
= 0;
771 if (zone
->l2p_table
) {
772 vfree(zone
->l2p_table
);
773 zone
->l2p_table
= NULL
;
775 if (zone
->free_table
) {
776 vfree(zone
->free_table
);
777 zone
->free_table
= NULL
;
781 static void xd_set_unused_block(struct rts51x_chip
*chip
, u32 phy_blk
)
783 struct xd_info
*xd_card
= &(chip
->xd_card
);
784 struct zone_entry
*zone
;
787 zone_no
= (int)phy_blk
>> 10;
788 if (zone_no
>= xd_card
->zone_cnt
) {
789 RTS51X_DEBUGP("Set unused block to invalid zone"
790 "(zone_no = %d, zone_cnt = %d)\n",
791 zone_no
, xd_card
->zone_cnt
);
794 zone
= &(xd_card
->zone
[zone_no
]);
796 if (zone
->free_table
== NULL
) {
797 if (xd_build_l2p_tbl(chip
, zone_no
) != STATUS_SUCCESS
)
801 if ((zone
->set_index
>= XD_FREE_TABLE_CNT
)
802 || (zone
->set_index
< 0)) {
804 RTS51X_DEBUGP("Set unused block fail, invalid set_index\n");
808 RTS51X_DEBUGP("Set unused block to index %d\n", zone
->set_index
);
810 zone
->free_table
[zone
->set_index
++] = (u16
) (phy_blk
& 0x3ff);
811 if (zone
->set_index
>= XD_FREE_TABLE_CNT
)
813 zone
->unused_blk_cnt
++;
816 static u32
xd_get_unused_block(struct rts51x_chip
*chip
, int zone_no
)
818 struct xd_info
*xd_card
= &(chip
->xd_card
);
819 struct zone_entry
*zone
;
822 if (zone_no
>= xd_card
->zone_cnt
) {
823 RTS51X_DEBUGP("Get unused block from invalid zone"
824 "(zone_no = %d, zone_cnt = %d)\n",
825 zone_no
, xd_card
->zone_cnt
);
826 TRACE_RET(chip
, BLK_NOT_FOUND
);
828 zone
= &(xd_card
->zone
[zone_no
]);
830 if ((zone
->unused_blk_cnt
== 0) ||
831 (zone
->set_index
== zone
->get_index
)) {
833 RTS51X_DEBUGP("Get unused block fail,"
834 "no unused block available\n");
835 TRACE_RET(chip
, BLK_NOT_FOUND
);
837 if ((zone
->get_index
>= XD_FREE_TABLE_CNT
) || (zone
->get_index
< 0)) {
839 RTS51X_DEBUGP("Get unused block fail, invalid get_index\n");
840 TRACE_RET(chip
, BLK_NOT_FOUND
);
843 RTS51X_DEBUGP("Get unused block from index %d\n", zone
->get_index
);
845 phy_blk
= zone
->free_table
[zone
->get_index
];
846 zone
->free_table
[zone
->get_index
++] = 0xFFFF;
847 if (zone
->get_index
>= XD_FREE_TABLE_CNT
)
849 zone
->unused_blk_cnt
--;
851 phy_blk
+= ((u32
) (zone_no
) << 10);
855 static void xd_set_l2p_tbl(struct rts51x_chip
*chip
, int zone_no
, u16 log_off
,
858 struct xd_info
*xd_card
= &(chip
->xd_card
);
859 struct zone_entry
*zone
;
861 zone
= &(xd_card
->zone
[zone_no
]);
862 zone
->l2p_table
[log_off
] = phy_off
;
865 static u32
xd_get_l2p_tbl(struct rts51x_chip
*chip
, int zone_no
, u16 log_off
)
867 struct xd_info
*xd_card
= &(chip
->xd_card
);
868 struct zone_entry
*zone
;
871 zone
= &(xd_card
->zone
[zone_no
]);
872 if (zone
->l2p_table
[log_off
] == 0xFFFF) {
876 retval
= xd_delay_write(chip
);
877 if (retval
!= STATUS_SUCCESS
) {
878 RTS51X_DEBUGP("In xd_get_l2p_tbl,"
879 "delay write fail!\n");
880 TRACE_RET(chip
, BLK_NOT_FOUND
);
883 if (zone
->unused_blk_cnt
<= 0) {
884 RTS51X_DEBUGP("No unused block!\n");
885 TRACE_RET(chip
, BLK_NOT_FOUND
);
888 for (i
= 0; i
< zone
->unused_blk_cnt
; i
++) {
889 phy_blk
= xd_get_unused_block(chip
, zone_no
);
890 if (phy_blk
== BLK_NOT_FOUND
) {
891 RTS51X_DEBUGP("No unused block available!\n");
892 TRACE_RET(chip
, BLK_NOT_FOUND
);
896 xd_init_page(chip
, phy_blk
, log_off
, 0,
897 xd_card
->page_off
+ 1);
898 if (retval
== STATUS_SUCCESS
)
901 if (i
>= zone
->unused_blk_cnt
) {
902 RTS51X_DEBUGP("No good unused block available!\n");
903 TRACE_RET(chip
, BLK_NOT_FOUND
);
906 xd_set_l2p_tbl(chip
, zone_no
, log_off
, (u16
) (phy_blk
& 0x3FF));
910 return (u32
) zone
->l2p_table
[log_off
] + ((u32
) (zone_no
) << 10);
913 int reset_xd_card(struct rts51x_chip
*chip
)
915 struct xd_info
*xd_card
= &(chip
->xd_card
);
918 memset(xd_card
, 0, sizeof(struct xd_info
));
920 xd_card
->block_shift
= 0;
921 xd_card
->page_off
= 0;
922 xd_card
->addr_cycle
= 0;
923 xd_card
->capacity
= 0;
924 xd_card
->zone_cnt
= 0;
925 xd_card
->cis_block
= 0xFFFF;
926 xd_card
->delay_write
.delay_write_flag
= 0;
928 enable_card_clock(chip
, XD_CARD
);
930 retval
= reset_xd(chip
);
931 if (retval
!= STATUS_SUCCESS
) {
932 if (chip
->option
.reset_or_rw_fail_set_pad_drive
) {
933 rts51x_write_register(chip
, CARD_DRIVE_SEL
,
934 SD20_DRIVE_MASK
, DRIVE_8mA
);
936 TRACE_RET(chip
, retval
);
939 retval
= xd_init_l2p_tbl(chip
);
940 if (retval
!= STATUS_SUCCESS
)
941 TRACE_RET(chip
, retval
);
943 return STATUS_SUCCESS
;
946 static int xd_mark_bad_block(struct rts51x_chip
*chip
, u32 phy_blk
)
948 struct xd_info
*xd_card
= &(chip
->xd_card
);
953 RTS51X_DEBUGP("mark block 0x%x as bad block\n", phy_blk
);
955 if (phy_blk
== BLK_NOT_FOUND
)
956 TRACE_RET(chip
, STATUS_FAIL
);
958 rts51x_init_cmd(chip
);
960 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_PAGE_STATUS
, 0xFF, XD_GPG
);
961 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_BLOCK_STATUS
, 0xFF,
963 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_BLOCK_ADDR1_H
, 0xFF, 0xFF);
964 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_BLOCK_ADDR1_L
, 0xFF, 0xFF);
965 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_BLOCK_ADDR2_H
, 0xFF, 0xFF);
966 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_BLOCK_ADDR2_L
, 0xFF, 0xFF);
967 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_RESERVED0
, 0xFF, 0xFF);
968 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_RESERVED1
, 0xFF, 0xFF);
969 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_RESERVED2
, 0xFF, 0xFF);
970 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_RESERVED3
, 0xFF, 0xFF);
972 page_addr
= phy_blk
<< xd_card
->block_shift
;
974 xd_assign_phy_addr(chip
, page_addr
, XD_RW_ADDR
);
976 /* Specify page count */
977 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_PAGE_CNT
, 0xFF,
978 xd_card
->page_off
+ 1);
980 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_TRANSFER
, 0xFF,
981 XD_TRANSFER_START
| XD_WRITE_REDUNDANT
);
982 rts51x_add_cmd(chip
, CHECK_REG_CMD
, XD_TRANSFER
, XD_TRANSFER_END
,
985 retval
= rts51x_send_cmd(chip
, MODE_CR
, 100);
986 if (retval
!= STATUS_SUCCESS
)
987 TRACE_RET(chip
, STATUS_FAIL
);
989 retval
= rts51x_get_rsp(chip
, 1, 100);
991 if (retval
!= STATUS_SUCCESS
) {
992 rts51x_clear_xd_error(chip
);
993 rts51x_ep0_read_register(chip
, XD_DAT
, ®
);
994 if (reg
& PROGRAM_ERROR
)
995 xd_set_err_code(chip
, XD_PRG_ERROR
);
997 xd_set_err_code(chip
, XD_TO_ERROR
);
998 TRACE_RET(chip
, STATUS_FAIL
);
1001 return STATUS_SUCCESS
;
1004 static int xd_init_page(struct rts51x_chip
*chip
, u32 phy_blk
, u16 logoff
,
1005 u8 start_page
, u8 end_page
)
1007 struct xd_info
*xd_card
= &(chip
->xd_card
);
1012 RTS51X_DEBUGP("Init block 0x%x\n", phy_blk
);
1014 if (start_page
> end_page
)
1015 TRACE_RET(chip
, STATUS_FAIL
);
1016 if (phy_blk
== BLK_NOT_FOUND
)
1017 TRACE_RET(chip
, STATUS_FAIL
);
1019 rts51x_init_cmd(chip
);
1021 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_PAGE_STATUS
, 0xFF, 0xFF);
1022 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_BLOCK_STATUS
, 0xFF, 0xFF);
1023 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_BLOCK_ADDR1_H
, 0xFF,
1024 (u8
) (logoff
>> 8));
1025 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_BLOCK_ADDR1_L
, 0xFF,
1028 page_addr
= (phy_blk
<< xd_card
->block_shift
) + start_page
;
1030 xd_assign_phy_addr(chip
, page_addr
, XD_RW_ADDR
);
1032 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_CFG
, XD_BA_TRANSFORM
,
1035 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_PAGE_CNT
, 0xFF,
1036 (end_page
- start_page
));
1038 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_TRANSFER
, 0xFF,
1039 XD_TRANSFER_START
| XD_WRITE_REDUNDANT
);
1040 rts51x_add_cmd(chip
, CHECK_REG_CMD
, XD_TRANSFER
, XD_TRANSFER_END
,
1043 retval
= rts51x_send_cmd(chip
, MODE_CR
, 100);
1044 if (retval
!= STATUS_SUCCESS
)
1045 TRACE_RET(chip
, STATUS_FAIL
);
1047 retval
= rts51x_get_rsp(chip
, 1, 500);
1049 if (retval
!= STATUS_SUCCESS
) {
1050 rts51x_clear_xd_error(chip
);
1051 rts51x_ep0_read_register(chip
, XD_DAT
, ®
);
1052 if (reg
& PROGRAM_ERROR
) {
1053 xd_mark_bad_block(chip
, phy_blk
);
1054 xd_set_err_code(chip
, XD_PRG_ERROR
);
1056 xd_set_err_code(chip
, XD_TO_ERROR
);
1058 TRACE_RET(chip
, STATUS_FAIL
);
1061 return STATUS_SUCCESS
;
1064 static int xd_copy_page(struct rts51x_chip
*chip
,
1065 u32 old_blk
, u32 new_blk
, u8 start_page
, u8 end_page
)
1067 struct xd_info
*xd_card
= &(chip
->xd_card
);
1068 u32 old_page
, new_page
;
1072 RTS51X_DEBUGP("Copy page from block 0x%x to block 0x%x\n", old_blk
,
1075 if (start_page
> end_page
)
1076 TRACE_RET(chip
, STATUS_FAIL
);
1078 if ((old_blk
== BLK_NOT_FOUND
) || (new_blk
== BLK_NOT_FOUND
))
1079 TRACE_RET(chip
, STATUS_FAIL
);
1081 old_page
= (old_blk
<< xd_card
->block_shift
) + start_page
;
1082 new_page
= (new_blk
<< xd_card
->block_shift
) + start_page
;
1084 XD_CLR_BAD_NEWBLK(xd_card
);
1086 RTS51X_WRITE_REG(chip
, CARD_DATA_SOURCE
, 0x01, PINGPONG_BUFFER
);
1088 for (i
= start_page
; i
< end_page
; i
++) {
1089 if (monitor_card_cd(chip
, XD_CARD
) == CD_NOT_EXIST
) {
1090 RTS51X_WRITE_REG(chip
, CARD_STOP
, XD_STOP
| XD_CLR_ERR
,
1091 XD_STOP
| XD_CLR_ERR
);
1092 xd_set_err_code(chip
, XD_NO_CARD
);
1093 TRACE_RET(chip
, STATUS_FAIL
);
1096 rts51x_init_cmd(chip
);
1098 xd_assign_phy_addr(chip
, old_page
, XD_RW_ADDR
);
1100 /* Single page read */
1101 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_PAGE_CNT
, 0xFF, 1);
1102 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_CHK_DATA_STATUS
,
1103 XD_AUTO_CHK_DATA_STATUS
, 0);
1104 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_TRANSFER
, 0xFF,
1105 XD_TRANSFER_START
| XD_READ_PAGES
);
1106 rts51x_add_cmd(chip
, CHECK_REG_CMD
, XD_TRANSFER
,
1107 XD_TRANSFER_END
, XD_TRANSFER_END
);
1109 retval
= rts51x_send_cmd(chip
, MODE_CR
| STAGE_XD_STATUS
, 100);
1110 if (retval
!= STATUS_SUCCESS
)
1111 TRACE_RET(chip
, retval
);
1113 retval
= rts51x_get_rsp(chip
, 4, 500);
1114 if ((retval
!= STATUS_SUCCESS
) ||
1115 (chip
->rsp_buf
[2] & (XD_ECC1_ERROR
| XD_ECC2_ERROR
))) {
1116 rts51x_clear_xd_error(chip
);
1118 rts51x_ep0_read_register(chip
, XD_CTL
, ®
);
1119 if (reg
& (XD_ECC1_ERROR
| XD_ECC2_ERROR
)) {
1122 if (monitor_card_cd(chip
, XD_CARD
) ==
1124 xd_set_err_code(chip
, XD_NO_CARD
);
1125 TRACE_RET(chip
, STATUS_FAIL
);
1129 (XD_ECC1_ERROR
| XD_ECC1_UNCORRECTABLE
))
1130 == (XD_ECC1_ERROR
| XD_ECC1_UNCORRECTABLE
))
1131 || ((reg
& (XD_ECC2_ERROR
|
1132 XD_ECC2_UNCORRECTABLE
)) ==
1133 (XD_ECC2_ERROR
| XD_ECC2_UNCORRECTABLE
))) {
1134 RTS51X_WRITE_REG(chip
, XD_PAGE_STATUS
,
1136 RTS51X_WRITE_REG(chip
, XD_BLOCK_STATUS
,
1138 XD_SET_BAD_OLDBLK(xd_card
);
1139 RTS51X_DEBUGP("old block 0x%x"
1140 "ecc error\n", old_blk
);
1143 xd_set_err_code(chip
, XD_TO_ERROR
);
1144 TRACE_RET(chip
, STATUS_FAIL
);
1147 if (XD_CHK_BAD_OLDBLK(xd_card
))
1148 rts51x_clear_xd_error(chip
);
1150 rts51x_init_cmd(chip
);
1152 xd_assign_phy_addr(chip
, new_page
, XD_RW_ADDR
);
1153 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_PAGE_CNT
, 0xFF, 1);
1154 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_TRANSFER
, 0xFF,
1155 XD_TRANSFER_START
| XD_WRITE_PAGES
);
1156 rts51x_add_cmd(chip
, CHECK_REG_CMD
, XD_TRANSFER
,
1157 XD_TRANSFER_END
, XD_TRANSFER_END
);
1159 retval
= rts51x_send_cmd(chip
, MODE_CR
, 100);
1160 if (retval
!= STATUS_SUCCESS
)
1161 TRACE_RET(chip
, retval
);
1163 retval
= rts51x_get_rsp(chip
, 1, 300);
1164 if (retval
!= STATUS_SUCCESS
) {
1165 rts51x_clear_xd_error(chip
);
1167 rts51x_ep0_read_register(chip
, XD_DAT
, ®
);
1168 if (reg
& PROGRAM_ERROR
) {
1169 xd_mark_bad_block(chip
, new_blk
);
1170 xd_set_err_code(chip
, XD_PRG_ERROR
);
1171 XD_SET_BAD_NEWBLK(xd_card
);
1173 xd_set_err_code(chip
, XD_TO_ERROR
);
1175 TRACE_RET(chip
, retval
);
1182 return STATUS_SUCCESS
;
1186 static int xd_auto_copy_page(struct rts51x_chip
*chip
,
1187 u32 old_blk
, u32 new_blk
,
1188 u8 start_page
, u8 end_page
)
1190 struct xd_info
*xd_card
= &(chip
->xd_card
);
1191 u32 old_page
, new_page
;
1195 RTS51X_DEBUGP("Auto copy page from block 0x%x to block 0x%x\n",
1198 if (start_page
> end_page
)
1199 TRACE_RET(chip
, STATUS_FAIL
);
1201 page_count
= end_page
- start_page
;
1203 if ((old_blk
== BLK_NOT_FOUND
) || (new_blk
== BLK_NOT_FOUND
))
1204 TRACE_RET(chip
, STATUS_FAIL
);
1206 old_page
= (old_blk
<< xd_card
->block_shift
) + start_page
;
1207 new_page
= (new_blk
<< xd_card
->block_shift
) + start_page
;
1209 XD_CLR_BAD_NEWBLK(xd_card
);
1211 rts51x_init_cmd(chip
);
1213 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_CP_WAITTIME
, 0x03, WAIT_FF
);
1214 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_CP_PAGELEN
, 0xFF, page_count
);
1216 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_CP_READADDR0
, 0xFF, 0);
1217 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_CP_READADDR1
, 0xFF,
1219 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_CP_READADDR2
, 0xFF,
1220 (u8
) (old_page
>> 8));
1221 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_CP_READADDR3
, 0xFF,
1222 (u8
) (old_page
>> 16));
1223 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_CP_READADDR4
, 0xFF, 0);
1225 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_CP_WRITEADDR0
, 0xFF, 0);
1226 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_CP_WRITEADDR1
, 0xFF,
1228 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_CP_WRITEADDR2
, 0xFF,
1229 (u8
) (new_page
>> 8));
1230 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_CP_WRITEADDR3
, 0xFF,
1231 (u8
) (new_page
>> 16));
1232 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_CP_WRITEADDR4
, 0xFF, 0);
1234 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
, 0x01,
1237 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_CFG
,
1238 XD_BA_TRANSFORM
| XD_ADDR_MASK
, 0 | xd_card
->addr_cycle
);
1240 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_CHK_DATA_STATUS
,
1241 XD_AUTO_CHK_DATA_STATUS
, 0);
1242 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_TRANSFER
, 0xFF,
1243 XD_TRANSFER_START
| XD_COPY_PAGES
);
1244 rts51x_add_cmd(chip
, CHECK_REG_CMD
, XD_TRANSFER
, XD_TRANSFER_END
,
1247 retval
= rts51x_send_cmd(chip
, MODE_CR
, 100);
1248 if (retval
!= STATUS_SUCCESS
) {
1249 rts51x_clear_xd_error(chip
);
1250 TRACE_GOTO(chip
, Copy_Fail
);
1253 retval
= rts51x_get_rsp(chip
, 1, 800);
1254 if (retval
!= STATUS_SUCCESS
) {
1255 rts51x_clear_xd_error(chip
);
1256 TRACE_GOTO(chip
, Copy_Fail
);
1259 return STATUS_SUCCESS
;
1262 retval
= xd_copy_page(chip
, old_blk
, new_blk
, start_page
, end_page
);
1263 if (retval
!= STATUS_SUCCESS
)
1264 TRACE_RET(chip
, retval
);
1266 return STATUS_SUCCESS
;
1270 static int xd_reset_cmd(struct rts51x_chip
*chip
)
1275 rts51x_init_cmd(chip
);
1277 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_TRANSFER
, 0xFF,
1278 XD_TRANSFER_START
| XD_RESET
);
1279 rts51x_add_cmd(chip
, CHECK_REG_CMD
, XD_TRANSFER
, XD_TRANSFER_END
,
1281 rts51x_add_cmd(chip
, READ_REG_CMD
, XD_DAT
, 0, 0);
1282 rts51x_add_cmd(chip
, READ_REG_CMD
, XD_CTL
, 0, 0);
1284 retval
= rts51x_send_cmd(chip
, MODE_CR
, 100);
1285 if (retval
!= STATUS_SUCCESS
)
1286 TRACE_RET(chip
, retval
);
1288 retval
= rts51x_get_rsp(chip
, 3, 100);
1289 if (retval
!= STATUS_SUCCESS
) {
1290 rts51x_clear_xd_error(chip
);
1291 TRACE_RET(chip
, retval
);
1294 xd_dat
= chip
->rsp_buf
[1];
1295 xd_ctl
= chip
->rsp_buf
[2];
1296 if (((xd_dat
& READY_FLAG
) == READY_STATE
) && (xd_ctl
& XD_RDY
))
1297 return STATUS_SUCCESS
;
1299 TRACE_RET(chip
, STATUS_FAIL
);
1302 static int xd_erase_block(struct rts51x_chip
*chip
, u32 phy_blk
)
1304 struct xd_info
*xd_card
= &(chip
->xd_card
);
1309 if (phy_blk
== BLK_NOT_FOUND
)
1310 TRACE_RET(chip
, STATUS_FAIL
);
1312 page_addr
= phy_blk
<< xd_card
->block_shift
;
1314 for (i
= 0; i
< 3; i
++) {
1315 rts51x_init_cmd(chip
);
1317 xd_assign_phy_addr(chip
, page_addr
, XD_ERASE_ADDR
);
1319 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_TRANSFER
, 0xFF,
1320 XD_TRANSFER_START
| XD_ERASE
);
1321 rts51x_add_cmd(chip
, CHECK_REG_CMD
, XD_TRANSFER
,
1322 XD_TRANSFER_END
, XD_TRANSFER_END
);
1323 rts51x_add_cmd(chip
, READ_REG_CMD
, XD_DAT
, 0, 0);
1325 retval
= rts51x_send_cmd(chip
, MODE_CR
, 100);
1326 if (retval
!= STATUS_SUCCESS
)
1327 TRACE_RET(chip
, retval
);
1329 retval
= rts51x_get_rsp(chip
, 2, 300);
1330 if (retval
!= STATUS_SUCCESS
) {
1331 rts51x_clear_xd_error(chip
);
1332 rts51x_ep0_read_register(chip
, XD_DAT
, ®
);
1333 if (reg
& PROGRAM_ERROR
) {
1334 xd_mark_bad_block(chip
, phy_blk
);
1335 xd_set_err_code(chip
, XD_PRG_ERROR
);
1336 TRACE_RET(chip
, STATUS_FAIL
);
1338 xd_set_err_code(chip
, XD_ERASE_FAIL
);
1340 retval
= xd_reset_cmd(chip
);
1341 if (retval
!= STATUS_SUCCESS
)
1342 TRACE_RET(chip
, retval
);
1345 xd_dat
= chip
->rsp_buf
[1];
1346 if (xd_dat
& PROGRAM_ERROR
) {
1347 xd_mark_bad_block(chip
, phy_blk
);
1348 xd_set_err_code(chip
, XD_PRG_ERROR
);
1349 TRACE_RET(chip
, STATUS_FAIL
);
1352 return STATUS_SUCCESS
;
1355 xd_mark_bad_block(chip
, phy_blk
);
1356 xd_set_err_code(chip
, XD_ERASE_FAIL
);
1357 TRACE_RET(chip
, STATUS_FAIL
);
1360 static int xd_build_l2p_tbl(struct rts51x_chip
*chip
, int zone_no
)
1362 struct xd_info
*xd_card
= &(chip
->xd_card
);
1363 struct zone_entry
*zone
;
1366 u16 max_logoff
, cur_fst_page_logoff
, cur_lst_page_logoff
,
1367 ent_lst_page_logoff
;
1370 RTS51X_DEBUGP("xd_build_l2p_tbl: %d\n", zone_no
);
1372 if (xd_card
->zone
== NULL
) {
1373 retval
= xd_init_l2p_tbl(chip
);
1374 if (retval
!= STATUS_SUCCESS
)
1375 TRACE_RET(chip
, retval
);
1378 if (xd_card
->zone
[zone_no
].build_flag
) {
1379 RTS51X_DEBUGP("l2p table of zone %d has been built\n",
1381 return STATUS_SUCCESS
;
1384 zone
= &(xd_card
->zone
[zone_no
]);
1386 if (zone
->l2p_table
== NULL
) {
1387 zone
->l2p_table
= vmalloc(2000);
1388 if (zone
->l2p_table
== NULL
)
1389 TRACE_GOTO(chip
, Build_Fail
);
1391 memset((u8
*) (zone
->l2p_table
), 0xff, 2000);
1393 if (zone
->free_table
== NULL
) {
1394 zone
->free_table
= vmalloc(XD_FREE_TABLE_CNT
* 2);
1395 if (zone
->free_table
== NULL
)
1396 TRACE_GOTO(chip
, Build_Fail
);
1398 memset((u8
*) (zone
->free_table
), 0xff, XD_FREE_TABLE_CNT
* 2);
1401 if (xd_card
->cis_block
== 0xFFFF)
1404 start
= xd_card
->cis_block
+ 1;
1405 if (XD_CHK_4MB(xd_card
)) {
1413 start
= (u32
) (zone_no
) << 10;
1414 end
= (u32
) (zone_no
+ 1) << 10;
1418 RTS51X_DEBUGP("start block 0x%x, end block 0x%x\n", start
, end
);
1420 zone
->set_index
= zone
->get_index
= 0;
1421 zone
->unused_blk_cnt
= 0;
1423 for (i
= start
; i
< end
; i
++) {
1424 u32 page_addr
= i
<< xd_card
->block_shift
;
1427 retval
= xd_read_redundant(chip
, page_addr
, redunt
, 11);
1428 if (retval
!= STATUS_SUCCESS
)
1431 if (redunt
[BLOCK_STATUS
] != 0xFF) {
1432 RTS51X_DEBUGP("bad block\n");
1436 if (xd_check_data_blank(redunt
)) {
1437 RTS51X_DEBUGP("blank block\n");
1438 xd_set_unused_block(chip
, i
);
1442 cur_fst_page_logoff
= xd_load_log_block_addr(redunt
);
1443 if ((cur_fst_page_logoff
== 0xFFFF)
1444 || (cur_fst_page_logoff
> max_logoff
)) {
1445 retval
= xd_erase_block(chip
, i
);
1446 if (retval
== STATUS_SUCCESS
)
1447 xd_set_unused_block(chip
, i
);
1450 if ((zone_no
== 0) && (cur_fst_page_logoff
== 0)
1451 && (redunt
[PAGE_STATUS
] != XD_GPG
))
1452 XD_SET_MBR_FAIL(xd_card
);
1454 if (zone
->l2p_table
[cur_fst_page_logoff
] == 0xFFFF) {
1455 zone
->l2p_table
[cur_fst_page_logoff
] =
1461 zone
->l2p_table
[cur_fst_page_logoff
] +
1462 ((u32
) ((zone_no
) << 10));
1464 page_addr
= ((i
+ 1) << xd_card
->block_shift
) - 1;
1466 retval
= xd_read_redundant(chip
, page_addr
, redunt
, 11);
1467 if (retval
!= STATUS_SUCCESS
)
1470 cur_lst_page_logoff
= xd_load_log_block_addr(redunt
);
1471 if (cur_lst_page_logoff
== cur_fst_page_logoff
) {
1475 ((phy_block
+ 1) << xd_card
->block_shift
) - 1;
1477 for (m
= 0; m
< 3; m
++) {
1479 xd_read_redundant(chip
, page_addr
, redunt
,
1481 if (retval
== STATUS_SUCCESS
)
1486 zone
->l2p_table
[cur_fst_page_logoff
] =
1488 retval
= xd_erase_block(chip
, phy_block
);
1489 if (retval
== STATUS_SUCCESS
)
1490 xd_set_unused_block(chip
, phy_block
);
1494 ent_lst_page_logoff
= xd_load_log_block_addr(redunt
);
1495 if (ent_lst_page_logoff
!= cur_fst_page_logoff
) {
1496 zone
->l2p_table
[cur_fst_page_logoff
] =
1498 retval
= xd_erase_block(chip
, phy_block
);
1499 if (retval
== STATUS_SUCCESS
)
1500 xd_set_unused_block(chip
, phy_block
);
1503 retval
= xd_erase_block(chip
, i
);
1504 if (retval
== STATUS_SUCCESS
)
1505 xd_set_unused_block(chip
, i
);
1508 retval
= xd_erase_block(chip
, i
);
1509 if (retval
== STATUS_SUCCESS
)
1510 xd_set_unused_block(chip
, i
);
1514 if (XD_CHK_4MB(xd_card
))
1520 for (start
= 0; start
< end
; start
++) {
1521 if (zone
->l2p_table
[start
] == 0xFFFF)
1525 RTS51X_DEBUGP("Block count %d, invalid L2P entry %d\n", end
, i
);
1526 RTS51X_DEBUGP("Total unused block: %d\n", zone
->unused_blk_cnt
);
1528 if ((zone
->unused_blk_cnt
- i
) < 1)
1529 chip
->card_wp
|= XD_CARD
;
1531 zone
->build_flag
= 1;
1533 return STATUS_SUCCESS
;
1536 if (zone
->l2p_table
) {
1537 vfree(zone
->l2p_table
);
1538 zone
->l2p_table
= NULL
;
1540 if (zone
->free_table
) {
1541 vfree(zone
->free_table
);
1542 zone
->free_table
= NULL
;
1548 static int xd_send_cmd(struct rts51x_chip
*chip
, u8 cmd
)
1552 rts51x_init_cmd(chip
);
1554 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_DAT
, 0xFF, cmd
);
1555 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_TRANSFER
, 0xFF,
1556 XD_TRANSFER_START
| XD_SET_CMD
);
1557 rts51x_add_cmd(chip
, CHECK_REG_CMD
, XD_TRANSFER
, XD_TRANSFER_END
,
1560 retval
= rts51x_send_cmd(chip
, MODE_CR
, 100);
1561 if (retval
!= STATUS_SUCCESS
)
1562 TRACE_RET(chip
, retval
);
1564 retval
= rts51x_get_rsp(chip
, 1, 200);
1565 if (retval
!= STATUS_SUCCESS
) {
1566 rts51x_clear_xd_error(chip
);
1567 TRACE_RET(chip
, retval
);
1570 return STATUS_SUCCESS
;
1573 static int xd_read_multiple_pages(struct rts51x_chip
*chip
, u32 phy_blk
,
1574 u32 log_blk
, u8 start_page
, u8 end_page
,
1575 u8
*buf
, void **ptr
, unsigned int *offset
)
1577 struct xd_info
*xd_card
= &(chip
->xd_card
);
1578 u32 page_addr
, new_blk
;
1580 u8 reg_val
, page_cnt
;
1581 int zone_no
, retval
, i
;
1583 if (start_page
> end_page
)
1584 TRACE_RET(chip
, STATUS_FAIL
);
1586 page_cnt
= end_page
- start_page
;
1587 zone_no
= (int)(log_blk
/ 1000);
1588 log_off
= (u16
) (log_blk
% 1000);
1590 if ((phy_blk
& 0x3FF) == 0x3FF) {
1591 for (i
= 0; i
< 256; i
++) {
1592 page_addr
= ((u32
) i
) << xd_card
->block_shift
;
1594 retval
= xd_read_redundant(chip
, page_addr
, NULL
, 0);
1595 if (retval
== STATUS_SUCCESS
)
1598 if (monitor_card_cd(chip
, XD_CARD
) == CD_NOT_EXIST
) {
1599 xd_set_err_code(chip
, XD_NO_CARD
);
1600 TRACE_RET(chip
, STATUS_FAIL
);
1605 page_addr
= (phy_blk
<< xd_card
->block_shift
) + start_page
;
1607 rts51x_init_cmd(chip
);
1609 xd_assign_phy_addr(chip
, page_addr
, XD_RW_ADDR
);
1611 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_CFG
, XD_PPB_TO_SIE
,
1613 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
, 0x01,
1615 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_PAGE_CNT
, 0xFF, page_cnt
);
1616 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_CHK_DATA_STATUS
,
1617 XD_AUTO_CHK_DATA_STATUS
, XD_AUTO_CHK_DATA_STATUS
);
1619 trans_dma_enable(chip
->srb
->sc_data_direction
, chip
, page_cnt
* 512,
1622 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_TRANSFER
, 0xFF,
1623 XD_TRANSFER_START
| XD_READ_PAGES
);
1624 rts51x_add_cmd(chip
, CHECK_REG_CMD
, XD_TRANSFER
,
1625 XD_TRANSFER_END
| XD_PPB_EMPTY
,
1626 XD_TRANSFER_END
| XD_PPB_EMPTY
);
1628 retval
= rts51x_send_cmd(chip
, MODE_CDIR
, 100);
1629 if (retval
!= STATUS_SUCCESS
)
1630 TRACE_RET(chip
, retval
);
1633 rts51x_transfer_data_partial(chip
, RCV_BULK_PIPE(chip
), (void *)buf
,
1634 ptr
, offset
, page_cnt
* 512,
1635 scsi_sg_count(chip
->srb
), NULL
, 2000);
1636 if (retval
!= STATUS_SUCCESS
) {
1637 rts51x_clear_xd_error(chip
);
1639 if (retval
== STATUS_TIMEDOUT
) {
1640 xd_set_err_code(chip
, XD_TO_ERROR
);
1641 TRACE_RET(chip
, retval
);
1643 TRACE_GOTO(chip
, Fail
);
1646 retval
= rts51x_get_rsp(chip
, 1, 200);
1647 if (retval
!= STATUS_SUCCESS
) {
1648 rts51x_clear_xd_error(chip
);
1650 if (retval
== STATUS_TIMEDOUT
) {
1651 xd_set_err_code(chip
, XD_TO_ERROR
);
1652 TRACE_RET(chip
, retval
);
1654 TRACE_GOTO(chip
, Fail
);
1658 return STATUS_SUCCESS
;
1661 rts51x_ep0_read_register(chip
, XD_PAGE_STATUS
, ®_val
);
1662 RTS51X_DEBUGP("XD_PAGE_STATUS: 0x%x\n", reg_val
);
1664 if (reg_val
!= XD_GPG
)
1665 xd_set_err_code(chip
, XD_PRG_ERROR
);
1667 rts51x_ep0_read_register(chip
, XD_CTL
, ®_val
);
1668 RTS51X_DEBUGP("XD_CTL: 0x%x\n", reg_val
);
1670 /* Handle uncorrectable ECC error */
1671 if (((reg_val
& (XD_ECC1_ERROR
| XD_ECC1_UNCORRECTABLE
))
1672 == (XD_ECC1_ERROR
| XD_ECC1_UNCORRECTABLE
))
1673 || ((reg_val
& (XD_ECC2_ERROR
| XD_ECC2_UNCORRECTABLE
))
1674 == (XD_ECC2_ERROR
| XD_ECC2_UNCORRECTABLE
))) {
1677 if (monitor_card_cd(chip
, XD_CARD
) == CD_NOT_EXIST
) {
1678 xd_set_err_code(chip
, XD_NO_CARD
);
1679 TRACE_RET(chip
, STATUS_FAIL
);
1682 xd_set_err_code(chip
, XD_ECC_ERROR
);
1684 new_blk
= xd_get_unused_block(chip
, zone_no
);
1685 if (new_blk
== NO_NEW_BLK
) {
1686 XD_CLR_BAD_OLDBLK(xd_card
);
1687 TRACE_RET(chip
, STATUS_FAIL
);
1691 xd_auto_copy_page(chip
, phy_blk
, new_blk
, 0,
1692 xd_card
->page_off
+ 1);
1695 xd_copy_page(chip
, phy_blk
, new_blk
, 0,
1696 xd_card
->page_off
+ 1);
1698 if (retval
!= STATUS_SUCCESS
) {
1699 if (!XD_CHK_BAD_NEWBLK(xd_card
)) {
1700 retval
= xd_erase_block(chip
, new_blk
);
1701 if (retval
== STATUS_SUCCESS
)
1702 xd_set_unused_block(chip
, new_blk
);
1704 XD_CLR_BAD_NEWBLK(xd_card
);
1706 XD_CLR_BAD_OLDBLK(xd_card
);
1707 TRACE_RET(chip
, STATUS_FAIL
);
1709 xd_set_l2p_tbl(chip
, zone_no
, log_off
, (u16
) (new_blk
& 0x3FF));
1710 xd_erase_block(chip
, phy_blk
);
1711 xd_mark_bad_block(chip
, phy_blk
);
1712 XD_CLR_BAD_OLDBLK(xd_card
);
1715 TRACE_RET(chip
, STATUS_FAIL
);
1718 static int xd_finish_write(struct rts51x_chip
*chip
,
1719 u32 old_blk
, u32 new_blk
, u32 log_blk
, u8 page_off
)
1721 struct xd_info
*xd_card
= &(chip
->xd_card
);
1722 int retval
, zone_no
;
1725 RTS51X_DEBUGP("xd_finish_write, old_blk = 0x%x, new_blk = 0x%x,"
1726 "log_blk = 0x%x\n", old_blk
, new_blk
, log_blk
);
1728 if (page_off
> xd_card
->page_off
)
1729 TRACE_RET(chip
, STATUS_FAIL
);
1731 zone_no
= (int)(log_blk
/ 1000);
1732 log_off
= (u16
) (log_blk
% 1000);
1734 if (old_blk
== BLK_NOT_FOUND
) {
1735 retval
= xd_init_page(chip
, new_blk
, log_off
,
1736 page_off
, xd_card
->page_off
+ 1);
1737 if (retval
!= STATUS_SUCCESS
) {
1738 retval
= xd_erase_block(chip
, new_blk
);
1739 if (retval
== STATUS_SUCCESS
)
1740 xd_set_unused_block(chip
, new_blk
);
1741 TRACE_RET(chip
, STATUS_FAIL
);
1745 retval
= xd_auto_copy_page(chip
, old_blk
, new_blk
,
1746 page_off
, xd_card
->page_off
+ 1);
1748 retval
= xd_copy_page(chip
, old_blk
, new_blk
,
1749 page_off
, xd_card
->page_off
+ 1);
1751 if (retval
!= STATUS_SUCCESS
) {
1752 if (!XD_CHK_BAD_NEWBLK(xd_card
)) {
1753 retval
= xd_erase_block(chip
, new_blk
);
1754 if (retval
== STATUS_SUCCESS
)
1755 xd_set_unused_block(chip
, new_blk
);
1757 XD_CLR_BAD_NEWBLK(xd_card
);
1758 TRACE_RET(chip
, STATUS_FAIL
);
1761 retval
= xd_erase_block(chip
, old_blk
);
1762 if (retval
== STATUS_SUCCESS
) {
1763 if (XD_CHK_BAD_OLDBLK(xd_card
)) {
1764 xd_mark_bad_block(chip
, old_blk
);
1765 XD_CLR_BAD_OLDBLK(xd_card
);
1767 /* Add source block to unused block */
1768 xd_set_unused_block(chip
, old_blk
);
1771 xd_set_err_code(chip
, XD_NO_ERROR
);
1772 XD_CLR_BAD_OLDBLK(xd_card
);
1776 /* Add target block to L2P table */
1777 xd_set_l2p_tbl(chip
, zone_no
, log_off
, (u16
) (new_blk
& 0x3FF));
1779 return STATUS_SUCCESS
;
1782 static int xd_prepare_write(struct rts51x_chip
*chip
,
1783 u32 old_blk
, u32 new_blk
, u32 log_blk
, u8 page_off
)
1787 RTS51X_DEBUGP("xd_prepare_write, old_blk = 0x%x, new_blk = 0x%x,"
1788 "log_blk = 0x%x, page_off = %d\n",
1789 old_blk
, new_blk
, log_blk
, (int)page_off
);
1793 retval
= xd_auto_copy_page(chip
, old_blk
, new_blk
, 0, page_off
);
1795 retval
= xd_copy_page(chip
, old_blk
, new_blk
, 0, page_off
);
1797 if (retval
!= STATUS_SUCCESS
)
1798 TRACE_RET(chip
, retval
);
1801 return STATUS_SUCCESS
;
1804 static int xd_write_multiple_pages(struct rts51x_chip
*chip
, u32 old_blk
,
1805 u32 new_blk
, u32 log_blk
, u8 start_page
,
1806 u8 end_page
, u8
*buf
, void **ptr
,
1807 unsigned int *offset
)
1809 struct xd_info
*xd_card
= &(chip
->xd_card
);
1811 int zone_no
, retval
;
1813 u8 page_cnt
, reg_val
;
1815 RTS51X_DEBUGP("xd_write_multiple_pages, old_blk = 0x%x,"
1816 "new_blk = 0x%x, log_blk = 0x%x\n",
1817 old_blk
, new_blk
, log_blk
);
1819 if (start_page
> end_page
)
1820 TRACE_RET(chip
, STATUS_FAIL
);
1822 page_cnt
= end_page
- start_page
;
1823 zone_no
= (int)(log_blk
/ 1000);
1824 log_off
= (u16
) (log_blk
% 1000);
1826 page_addr
= (new_blk
<< xd_card
->block_shift
) + start_page
;
1828 /* Send index command */
1829 retval
= xd_send_cmd(chip
, READ1_1
);
1830 if (retval
!= STATUS_SUCCESS
)
1831 TRACE_RET(chip
, retval
);
1833 rts51x_init_cmd(chip
);
1835 /* Prepare redundant field */
1836 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_BLOCK_ADDR1_H
, 0xFF,
1837 (u8
) (log_off
>> 8));
1838 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_BLOCK_ADDR1_L
, 0xFF,
1840 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_BLOCK_STATUS
, 0xFF, XD_GBLK
);
1841 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_PAGE_STATUS
, 0xFF, XD_GPG
);
1843 xd_assign_phy_addr(chip
, page_addr
, XD_RW_ADDR
);
1845 /* Transform the block address by hardware */
1846 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_CFG
, XD_BA_TRANSFORM
,
1849 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_PAGE_CNT
, 0xFF, page_cnt
);
1850 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
, 0x01,
1853 trans_dma_enable(chip
->srb
->sc_data_direction
, chip
, page_cnt
* 512,
1856 rts51x_add_cmd(chip
, WRITE_REG_CMD
, XD_TRANSFER
, 0xFF,
1857 XD_TRANSFER_START
| XD_WRITE_PAGES
);
1858 rts51x_add_cmd(chip
, CHECK_REG_CMD
, XD_TRANSFER
, XD_TRANSFER_END
,
1861 retval
= rts51x_send_cmd(chip
, MODE_CDOR
, 100);
1862 if (retval
!= STATUS_SUCCESS
)
1863 TRACE_RET(chip
, retval
);
1866 rts51x_transfer_data_partial(chip
, SND_BULK_PIPE(chip
), (void *)buf
,
1867 ptr
, offset
, page_cnt
* 512,
1868 scsi_sg_count(chip
->srb
), NULL
, 2000);
1869 if (retval
!= STATUS_SUCCESS
) {
1870 rts51x_clear_xd_error(chip
);
1872 if (retval
== STATUS_TIMEDOUT
) {
1873 xd_set_err_code(chip
, XD_TO_ERROR
);
1874 TRACE_RET(chip
, retval
);
1876 TRACE_GOTO(chip
, Fail
);
1879 retval
= rts51x_get_rsp(chip
, 1, 200);
1880 if (retval
!= STATUS_SUCCESS
) {
1881 rts51x_clear_xd_error(chip
);
1883 if (retval
== STATUS_TIMEDOUT
) {
1884 xd_set_err_code(chip
, XD_TO_ERROR
);
1885 TRACE_RET(chip
, retval
);
1887 TRACE_GOTO(chip
, Fail
);
1891 if (end_page
== (xd_card
->page_off
+ 1)) {
1892 xd_card
->delay_write
.delay_write_flag
= 0;
1894 if (old_blk
!= BLK_NOT_FOUND
) {
1895 retval
= xd_erase_block(chip
, old_blk
);
1896 if (retval
== STATUS_SUCCESS
) {
1897 if (XD_CHK_BAD_OLDBLK(xd_card
)) {
1898 xd_mark_bad_block(chip
, old_blk
);
1899 XD_CLR_BAD_OLDBLK(xd_card
);
1901 xd_set_unused_block(chip
, old_blk
);
1904 xd_set_err_code(chip
, XD_NO_ERROR
);
1905 XD_CLR_BAD_OLDBLK(xd_card
);
1908 xd_set_l2p_tbl(chip
, zone_no
, log_off
, (u16
) (new_blk
& 0x3FF));
1911 return STATUS_SUCCESS
;
1914 rts51x_ep0_read_register(chip
, XD_DAT
, ®_val
);
1915 RTS51X_DEBUGP("XD_DAT: 0x%x\n", reg_val
);
1917 if (reg_val
& PROGRAM_ERROR
) {
1918 xd_set_err_code(chip
, XD_PRG_ERROR
);
1919 xd_mark_bad_block(chip
, new_blk
);
1922 TRACE_RET(chip
, STATUS_FAIL
);
1925 int xd_delay_write(struct rts51x_chip
*chip
)
1927 struct xd_info
*xd_card
= &(chip
->xd_card
);
1928 struct xd_delay_write_tag
*delay_write
= &(xd_card
->delay_write
);
1931 if (delay_write
->delay_write_flag
) {
1932 RTS51X_DEBUGP("xd_delay_write\n");
1933 retval
= xd_switch_clock(chip
);
1934 if (retval
!= STATUS_SUCCESS
)
1935 TRACE_RET(chip
, retval
);
1937 delay_write
->delay_write_flag
= 0;
1938 retval
= xd_finish_write(chip
,
1939 delay_write
->old_phyblock
,
1940 delay_write
->new_phyblock
,
1941 delay_write
->logblock
,
1942 delay_write
->pageoff
);
1943 if (retval
!= STATUS_SUCCESS
)
1944 TRACE_RET(chip
, retval
);
1947 return STATUS_SUCCESS
;
1950 int xd_rw(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
, u32 start_sector
,
1953 struct xd_info
*xd_card
= &(chip
->xd_card
);
1954 unsigned int lun
= SCSI_LUN(srb
);
1955 struct xd_delay_write_tag
*delay_write
= &(xd_card
->delay_write
);
1956 int retval
, zone_no
;
1957 u32 log_blk
, old_blk
= 0, new_blk
= 0;
1958 u16 log_off
, total_sec_cnt
= sector_cnt
;
1959 u8 start_page
, end_page
= 0, page_cnt
;
1962 unsigned int offset
= 0;
1964 xd_set_err_code(chip
, XD_NO_ERROR
);
1966 xd_card
->counter
= 0;
1968 RTS51X_DEBUGP("xd_rw: scsi_bufflen = %d, scsi_sg_count = %d\n",
1969 scsi_bufflen(srb
), scsi_sg_count(srb
));
1970 RTS51X_DEBUGP("Data direction: %s\n",
1971 (srb
->sc_data_direction
==
1972 DMA_TO_DEVICE
) ? "write" : "read");
1974 buf
= (u8
*) scsi_sglist(srb
);
1976 retval
= xd_switch_clock(chip
);
1977 if (retval
!= STATUS_SUCCESS
)
1978 TRACE_RET(chip
, retval
);
1980 log_blk
= start_sector
>> xd_card
->block_shift
;
1981 start_page
= (u8
) start_sector
& xd_card
->page_off
;
1982 zone_no
= (int)(log_blk
/ 1000);
1983 log_off
= (u16
) (log_blk
% 1000);
1985 RTS51X_DEBUGP("log_blk = 0x%x\n", log_blk
);
1987 if (xd_card
->zone
[zone_no
].build_flag
== 0) {
1988 retval
= xd_build_l2p_tbl(chip
, zone_no
);
1989 if (retval
!= STATUS_SUCCESS
) {
1990 chip
->card_fail
|= XD_CARD
;
1991 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_NOT_PRESENT
);
1992 TRACE_RET(chip
, retval
);
1996 if (srb
->sc_data_direction
== DMA_TO_DEVICE
) {
1997 if (delay_write
->delay_write_flag
&&
1998 (delay_write
->logblock
== log_blk
) &&
1999 (start_page
> delay_write
->pageoff
)) {
2000 delay_write
->delay_write_flag
= 0;
2001 if (delay_write
->old_phyblock
!= BLK_NOT_FOUND
) {
2003 retval
= xd_auto_copy_page(chip
,
2004 delay_write
->old_phyblock
,
2005 delay_write
->new_phyblock
,
2006 delay_write
->pageoff
, start_page
);
2008 retval
= xd_copy_page(chip
,
2009 delay_write
->old_phyblock
,
2010 delay_write
->new_phyblock
,
2011 delay_write
->pageoff
,
2014 if (retval
!= STATUS_SUCCESS
) {
2015 set_sense_type(chip
, lun
,
2016 SENSE_TYPE_MEDIA_WRITE_ERR
);
2017 TRACE_RET(chip
, retval
);
2020 old_blk
= delay_write
->old_phyblock
;
2021 new_blk
= delay_write
->new_phyblock
;
2022 } else if (delay_write
->delay_write_flag
&&
2023 (delay_write
->logblock
== log_blk
) &&
2024 (start_page
== delay_write
->pageoff
)) {
2025 delay_write
->delay_write_flag
= 0;
2026 old_blk
= delay_write
->old_phyblock
;
2027 new_blk
= delay_write
->new_phyblock
;
2029 retval
= xd_delay_write(chip
);
2030 if (retval
!= STATUS_SUCCESS
) {
2031 set_sense_type(chip
, lun
,
2032 SENSE_TYPE_MEDIA_WRITE_ERR
);
2033 TRACE_RET(chip
, retval
);
2035 old_blk
= xd_get_l2p_tbl(chip
, zone_no
, log_off
);
2036 new_blk
= xd_get_unused_block(chip
, zone_no
);
2037 if ((old_blk
== BLK_NOT_FOUND
)
2038 || (new_blk
== BLK_NOT_FOUND
)) {
2039 set_sense_type(chip
, lun
,
2040 SENSE_TYPE_MEDIA_WRITE_ERR
);
2041 TRACE_RET(chip
, retval
);
2045 xd_prepare_write(chip
, old_blk
, new_blk
, log_blk
,
2047 if (retval
!= STATUS_SUCCESS
) {
2048 if (monitor_card_cd(chip
, XD_CARD
) ==
2050 set_sense_type(chip
, lun
,
2051 SENSE_TYPE_MEDIA_NOT_PRESENT
);
2052 TRACE_RET(chip
, STATUS_FAIL
);
2054 set_sense_type(chip
, lun
,
2055 SENSE_TYPE_MEDIA_WRITE_ERR
);
2056 TRACE_RET(chip
, retval
);
2060 retval
= xd_delay_write(chip
);
2061 if (retval
!= STATUS_SUCCESS
) {
2062 if (monitor_card_cd(chip
, XD_CARD
) == CD_NOT_EXIST
) {
2063 set_sense_type(chip
, lun
,
2064 SENSE_TYPE_MEDIA_NOT_PRESENT
);
2065 TRACE_RET(chip
, STATUS_FAIL
);
2067 set_sense_type(chip
, lun
,
2068 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR
);
2069 TRACE_RET(chip
, retval
);
2072 old_blk
= xd_get_l2p_tbl(chip
, zone_no
, log_off
);
2073 if (old_blk
== BLK_NOT_FOUND
) {
2074 set_sense_type(chip
, lun
,
2075 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR
);
2076 TRACE_RET(chip
, STATUS_FAIL
);
2080 RTS51X_DEBUGP("old_blk = 0x%x\n", old_blk
);
2081 if (srb
->sc_data_direction
== DMA_TO_DEVICE
)
2082 RTS51X_DEBUGP("new_blk = 0x%x\n", new_blk
);
2084 while (total_sec_cnt
) {
2085 if ((start_page
+ total_sec_cnt
) > (xd_card
->page_off
+ 1))
2086 end_page
= xd_card
->page_off
+ 1;
2088 end_page
= start_page
+ (u8
) total_sec_cnt
;
2089 page_cnt
= end_page
- start_page
;
2090 if (srb
->sc_data_direction
== DMA_FROM_DEVICE
) {
2091 retval
= xd_read_multiple_pages(chip
, old_blk
, log_blk
,
2092 start_page
, end_page
,
2093 buf
, &ptr
, &offset
);
2094 if (retval
!= STATUS_SUCCESS
) {
2095 set_sense_type(chip
, lun
,
2096 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR
);
2097 TRACE_RET(chip
, STATUS_FAIL
);
2101 xd_write_multiple_pages(chip
, old_blk
, new_blk
,
2102 log_blk
, start_page
,
2103 end_page
, buf
, &ptr
,
2105 if (retval
!= STATUS_SUCCESS
) {
2106 set_sense_type(chip
, lun
,
2107 SENSE_TYPE_MEDIA_WRITE_ERR
);
2108 TRACE_RET(chip
, STATUS_FAIL
);
2112 total_sec_cnt
-= page_cnt
;
2114 if (total_sec_cnt
== 0)
2118 zone_no
= (int)(log_blk
/ 1000);
2119 log_off
= (u16
) (log_blk
% 1000);
2121 if (xd_card
->zone
[zone_no
].build_flag
== 0) {
2122 retval
= xd_build_l2p_tbl(chip
, zone_no
);
2123 if (retval
!= STATUS_SUCCESS
) {
2124 chip
->card_fail
|= XD_CARD
;
2125 set_sense_type(chip
, lun
,
2126 SENSE_TYPE_MEDIA_NOT_PRESENT
);
2127 TRACE_RET(chip
, retval
);
2131 old_blk
= xd_get_l2p_tbl(chip
, zone_no
, log_off
);
2132 if (old_blk
== BLK_NOT_FOUND
) {
2133 if (srb
->sc_data_direction
== DMA_FROM_DEVICE
) {
2134 set_sense_type(chip
, lun
,
2135 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR
);
2137 set_sense_type(chip
, lun
,
2138 SENSE_TYPE_MEDIA_WRITE_ERR
);
2140 TRACE_RET(chip
, STATUS_FAIL
);
2143 if (srb
->sc_data_direction
== DMA_TO_DEVICE
) {
2144 new_blk
= xd_get_unused_block(chip
, zone_no
);
2145 if (new_blk
== BLK_NOT_FOUND
) {
2146 set_sense_type(chip
, lun
,
2147 SENSE_TYPE_MEDIA_WRITE_ERR
);
2148 TRACE_RET(chip
, STATUS_FAIL
);
2155 if ((srb
->sc_data_direction
== DMA_TO_DEVICE
) &&
2156 (end_page
!= (xd_card
->page_off
+ 1))) {
2157 delay_write
->delay_write_flag
= 1;
2158 delay_write
->old_phyblock
= old_blk
;
2159 delay_write
->new_phyblock
= new_blk
;
2160 delay_write
->logblock
= log_blk
;
2161 delay_write
->pageoff
= end_page
;
2164 scsi_set_resid(srb
, 0);
2166 return STATUS_SUCCESS
;
2169 void xd_free_l2p_tbl(struct rts51x_chip
*chip
)
2171 struct xd_info
*xd_card
= &(chip
->xd_card
);
2174 if (xd_card
->zone
!= NULL
) {
2175 for (i
= 0; i
< xd_card
->zone_cnt
; i
++) {
2176 if (xd_card
->zone
[i
].l2p_table
!= NULL
) {
2177 vfree(xd_card
->zone
[i
].l2p_table
);
2178 xd_card
->zone
[i
].l2p_table
= NULL
;
2180 if (xd_card
->zone
[i
].free_table
!= NULL
) {
2181 vfree(xd_card
->zone
[i
].free_table
);
2182 xd_card
->zone
[i
].free_table
= NULL
;
2185 vfree(xd_card
->zone
);
2186 xd_card
->zone
= NULL
;
2190 void xd_cleanup_work(struct rts51x_chip
*chip
)
2192 struct xd_info
*xd_card
= &(chip
->xd_card
);
2194 if (xd_card
->delay_write
.delay_write_flag
) {
2195 RTS51X_DEBUGP("xD: delay write\n");
2196 xd_delay_write(chip
);
2197 xd_card
->counter
= 0;
2201 int xd_power_off_card3v3(struct rts51x_chip
*chip
)
2205 rts51x_init_cmd(chip
);
2207 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_CLK_EN
, XD_CLK_EN
, 0);
2209 if (chip
->asic_code
)
2210 xd_pull_ctl_disable(chip
);
2212 rts51x_add_cmd(chip
, WRITE_REG_CMD
, FPGA_PULL_CTL
, 0xFF, 0xDF);
2213 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_OE
, XD_OUTPUT_EN
, 0);
2214 if (!chip
->option
.FT2_fast_mode
) {
2215 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PWR_CTL
, POWER_MASK
,
2217 if (CHECK_PKG(chip
, LQFP48
)
2218 || chip
->option
.rts5129_D3318_off_enable
)
2219 rts51x_add_cmd(chip
, WRITE_REG_CMD
, CARD_PWR_CTL
,
2220 DV3318_AUTO_PWR_OFF
, 0);
2223 retval
= rts51x_send_cmd(chip
, MODE_C
, 100);
2224 if (retval
!= STATUS_SUCCESS
)
2225 TRACE_RET(chip
, retval
);
2227 return STATUS_SUCCESS
;
2230 int release_xd_card(struct rts51x_chip
*chip
)
2232 struct xd_info
*xd_card
= &(chip
->xd_card
);
2235 RTS51X_DEBUGP("elease_xd_card\n");
2237 chip
->card_ready
&= ~XD_CARD
;
2238 chip
->card_fail
&= ~XD_CARD
;
2239 chip
->card_wp
&= ~XD_CARD
;
2241 xd_card
->delay_write
.delay_write_flag
= 0;
2243 xd_free_l2p_tbl(chip
);
2245 rts51x_write_register(chip
, SFSM_ED
, HW_CMD_STOP
, HW_CMD_STOP
);
2247 retval
= xd_power_off_card3v3(chip
);
2248 if (retval
!= STATUS_SUCCESS
)
2249 TRACE_RET(chip
, retval
);
2251 if (chip
->asic_code
&& CHECK_PKG(chip
, QFN24
))
2254 return STATUS_SUCCESS
;