1 /* Driver for Realtek PCI-Express card reader
3 * Copyright(c) 2009-2013 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 * Wei WANG (wei_wang@realsil.com.cn)
20 * Micky Ching (micky_ching@realsil.com.cn)
23 #include <linux/blkdev.h>
24 #include <linux/kthread.h>
25 #include <linux/sched.h>
26 #include <linux/vmalloc.h>
29 #include "rtsx_transport.h"
30 #include "rtsx_scsi.h"
31 #include "rtsx_card.h"
34 static int xd_build_l2p_tbl(struct rtsx_chip
*chip
, int zone_no
);
35 static int xd_init_page(struct rtsx_chip
*chip
, u32 phy_blk
, u16 logoff
,
36 u8 start_page
, u8 end_page
);
38 static inline void xd_set_err_code(struct rtsx_chip
*chip
, u8 err_code
)
40 struct xd_info
*xd_card
= &chip
->xd_card
;
42 xd_card
->err_code
= err_code
;
45 static inline int xd_check_err_code(struct rtsx_chip
*chip
, u8 err_code
)
47 struct xd_info
*xd_card
= &chip
->xd_card
;
49 return (xd_card
->err_code
== err_code
);
52 static int xd_set_init_para(struct rtsx_chip
*chip
)
54 struct xd_info
*xd_card
= &chip
->xd_card
;
58 xd_card
->xd_clock
= 47;
60 xd_card
->xd_clock
= CLK_50
;
62 retval
= switch_clock(chip
, xd_card
->xd_clock
);
63 if (retval
!= STATUS_SUCCESS
) {
68 return STATUS_SUCCESS
;
71 static int xd_switch_clock(struct rtsx_chip
*chip
)
73 struct xd_info
*xd_card
= &chip
->xd_card
;
76 retval
= select_card(chip
, XD_CARD
);
77 if (retval
!= STATUS_SUCCESS
) {
82 retval
= switch_clock(chip
, xd_card
->xd_clock
);
83 if (retval
!= STATUS_SUCCESS
) {
88 return STATUS_SUCCESS
;
91 static int xd_read_id(struct rtsx_chip
*chip
, u8 id_cmd
, u8
*id_buf
, u8 buf_len
)
98 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_DAT
, 0xFF, id_cmd
);
99 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_TRANSFER
, 0xFF,
100 XD_TRANSFER_START
| XD_READ_ID
);
101 rtsx_add_cmd(chip
, CHECK_REG_CMD
, XD_TRANSFER
, XD_TRANSFER_END
,
104 for (i
= 0; i
< 4; i
++)
105 rtsx_add_cmd(chip
, READ_REG_CMD
, (u16
)(XD_ADDRESS1
+ i
), 0, 0);
107 retval
= rtsx_send_cmd(chip
, XD_CARD
, 20);
113 ptr
= rtsx_get_cmd_data(chip
) + 1;
114 if (id_buf
&& buf_len
) {
117 memcpy(id_buf
, ptr
, buf_len
);
120 return STATUS_SUCCESS
;
123 static void xd_assign_phy_addr(struct rtsx_chip
*chip
, u32 addr
, u8 mode
)
125 struct xd_info
*xd_card
= &chip
->xd_card
;
129 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_ADDRESS0
, 0xFF, 0);
130 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_ADDRESS1
, 0xFF, (u8
)addr
);
131 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_ADDRESS2
,
132 0xFF, (u8
)(addr
>> 8));
133 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_ADDRESS3
,
134 0xFF, (u8
)(addr
>> 16));
135 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_CFG
, 0xFF,
136 xd_card
->addr_cycle
|
142 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_ADDRESS0
, 0xFF, (u8
)addr
);
143 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_ADDRESS1
,
144 0xFF, (u8
)(addr
>> 8));
145 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_ADDRESS2
,
146 0xFF, (u8
)(addr
>> 16));
147 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_CFG
, 0xFF,
148 (xd_card
->addr_cycle
- 1) | XD_CALC_ECC
|
157 static int xd_read_redundant(struct rtsx_chip
*chip
, u32 page_addr
,
158 u8
*buf
, int buf_len
)
164 xd_assign_phy_addr(chip
, page_addr
, XD_RW_ADDR
);
166 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_TRANSFER
,
167 0xFF, XD_TRANSFER_START
| XD_READ_REDUNDANT
);
168 rtsx_add_cmd(chip
, CHECK_REG_CMD
, XD_TRANSFER
,
169 XD_TRANSFER_END
, XD_TRANSFER_END
);
171 for (i
= 0; i
< 6; i
++)
172 rtsx_add_cmd(chip
, READ_REG_CMD
, (u16
)(XD_PAGE_STATUS
+ i
),
174 for (i
= 0; i
< 4; i
++)
175 rtsx_add_cmd(chip
, READ_REG_CMD
, (u16
)(XD_RESERVED0
+ i
),
177 rtsx_add_cmd(chip
, READ_REG_CMD
, XD_PARITY
, 0, 0);
179 retval
= rtsx_send_cmd(chip
, XD_CARD
, 500);
185 if (buf
&& buf_len
) {
186 u8
*ptr
= rtsx_get_cmd_data(chip
) + 1;
190 memcpy(buf
, ptr
, buf_len
);
193 return STATUS_SUCCESS
;
196 static int xd_read_data_from_ppb(struct rtsx_chip
*chip
, int offset
,
197 u8
*buf
, int buf_len
)
201 if (!buf
|| (buf_len
< 0)) {
208 for (i
= 0; i
< buf_len
; i
++)
209 rtsx_add_cmd(chip
, READ_REG_CMD
, PPBUF_BASE2
+ offset
+ i
,
212 retval
= rtsx_send_cmd(chip
, 0, 250);
214 rtsx_clear_xd_error(chip
);
219 memcpy(buf
, rtsx_get_cmd_data(chip
), buf_len
);
221 return STATUS_SUCCESS
;
224 static int xd_read_cis(struct rtsx_chip
*chip
, u32 page_addr
, u8
*buf
,
230 if (!buf
|| (buf_len
< 10)) {
237 xd_assign_phy_addr(chip
, page_addr
, XD_RW_ADDR
);
239 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
,
240 0x01, PINGPONG_BUFFER
);
241 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_PAGE_CNT
, 0xFF, 1);
242 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_CHK_DATA_STATUS
,
243 XD_AUTO_CHK_DATA_STATUS
, XD_AUTO_CHK_DATA_STATUS
);
245 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_TRANSFER
, 0xFF,
246 XD_TRANSFER_START
| XD_READ_PAGES
);
247 rtsx_add_cmd(chip
, CHECK_REG_CMD
, XD_TRANSFER
, XD_TRANSFER_END
,
250 retval
= rtsx_send_cmd(chip
, XD_CARD
, 250);
251 if (retval
== -ETIMEDOUT
) {
252 rtsx_clear_xd_error(chip
);
257 retval
= rtsx_read_register(chip
, XD_PAGE_STATUS
, ®
);
263 rtsx_clear_xd_error(chip
);
268 retval
= rtsx_read_register(chip
, XD_CTL
, ®
);
273 if (!(reg
& XD_ECC1_ERROR
) || !(reg
& XD_ECC1_UNCORRECTABLE
)) {
274 retval
= xd_read_data_from_ppb(chip
, 0, buf
, buf_len
);
275 if (retval
!= STATUS_SUCCESS
) {
279 if (reg
& XD_ECC1_ERROR
) {
280 u8 ecc_bit
, ecc_byte
;
282 retval
= rtsx_read_register(chip
, XD_ECC_BIT1
,
288 retval
= rtsx_read_register(chip
, XD_ECC_BYTE1
,
295 dev_dbg(rtsx_dev(chip
), "ECC_BIT1 = 0x%x, ECC_BYTE1 = 0x%x\n",
297 if (ecc_byte
< buf_len
) {
298 dev_dbg(rtsx_dev(chip
), "Before correct: 0x%x\n",
300 buf
[ecc_byte
] ^= (1 << ecc_bit
);
301 dev_dbg(rtsx_dev(chip
), "After correct: 0x%x\n",
305 } else if (!(reg
& XD_ECC2_ERROR
) || !(reg
& XD_ECC2_UNCORRECTABLE
)) {
306 rtsx_clear_xd_error(chip
);
308 retval
= xd_read_data_from_ppb(chip
, 256, buf
, buf_len
);
309 if (retval
!= STATUS_SUCCESS
) {
313 if (reg
& XD_ECC2_ERROR
) {
314 u8 ecc_bit
, ecc_byte
;
316 retval
= rtsx_read_register(chip
, XD_ECC_BIT2
,
322 retval
= rtsx_read_register(chip
, XD_ECC_BYTE2
,
329 dev_dbg(rtsx_dev(chip
), "ECC_BIT2 = 0x%x, ECC_BYTE2 = 0x%x\n",
331 if (ecc_byte
< buf_len
) {
332 dev_dbg(rtsx_dev(chip
), "Before correct: 0x%x\n",
334 buf
[ecc_byte
] ^= (1 << ecc_bit
);
335 dev_dbg(rtsx_dev(chip
), "After correct: 0x%x\n",
340 rtsx_clear_xd_error(chip
);
345 return STATUS_SUCCESS
;
348 static void xd_fill_pull_ctl_disable(struct rtsx_chip
*chip
)
350 if (CHECK_PID(chip
, 0x5208)) {
351 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL1
, 0xFF,
352 XD_D3_PD
| XD_D2_PD
| XD_D1_PD
| XD_D0_PD
);
353 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL2
, 0xFF,
354 XD_D7_PD
| XD_D6_PD
| XD_D5_PD
| XD_D4_PD
);
355 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL3
, 0xFF,
356 XD_WP_PD
| XD_CE_PD
| XD_CLE_PD
| XD_CD_PU
);
357 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL4
, 0xFF,
358 XD_RDY_PD
| XD_WE_PD
| XD_RE_PD
| XD_ALE_PD
);
359 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL5
, 0xFF,
360 MS_INS_PU
| SD_WP_PD
| SD_CD_PU
| SD_CMD_PD
);
361 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL6
, 0xFF,
362 MS_D5_PD
| MS_D4_PD
);
363 } else if (CHECK_PID(chip
, 0x5288)) {
364 if (CHECK_BARO_PKG(chip
, QFN
)) {
365 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL1
,
367 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL2
,
369 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL3
,
371 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL4
,
377 static void xd_fill_pull_ctl_stage1_barossa(struct rtsx_chip
*chip
)
379 if (CHECK_BARO_PKG(chip
, QFN
)) {
380 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL1
, 0xFF, 0x55);
381 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL2
, 0xFF, 0x55);
382 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL3
, 0xFF, 0x4B);
383 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL4
, 0xFF, 0x55);
387 static void xd_fill_pull_ctl_enable(struct rtsx_chip
*chip
)
389 if (CHECK_PID(chip
, 0x5208)) {
390 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL1
, 0xFF,
391 XD_D3_PD
| XD_D2_PD
| XD_D1_PD
| XD_D0_PD
);
392 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL2
, 0xFF,
393 XD_D7_PD
| XD_D6_PD
| XD_D5_PD
| XD_D4_PD
);
394 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL3
, 0xFF,
395 XD_WP_PD
| XD_CE_PU
| XD_CLE_PD
| XD_CD_PU
);
396 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL4
, 0xFF,
397 XD_RDY_PU
| XD_WE_PU
| XD_RE_PU
| XD_ALE_PD
);
398 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL5
, 0xFF,
399 MS_INS_PU
| SD_WP_PD
| SD_CD_PU
| SD_CMD_PD
);
400 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL6
, 0xFF,
401 MS_D5_PD
| MS_D4_PD
);
402 } else if (CHECK_PID(chip
, 0x5288)) {
403 if (CHECK_BARO_PKG(chip
, QFN
)) {
404 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL1
,
406 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL2
,
408 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL3
,
410 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL4
,
416 static int xd_pull_ctl_disable(struct rtsx_chip
*chip
)
420 if (CHECK_PID(chip
, 0x5208)) {
421 retval
= rtsx_write_register(chip
, CARD_PULL_CTL1
, 0xFF,
430 retval
= rtsx_write_register(chip
, CARD_PULL_CTL2
, 0xFF,
439 retval
= rtsx_write_register(chip
, CARD_PULL_CTL3
, 0xFF,
448 retval
= rtsx_write_register(chip
, CARD_PULL_CTL4
, 0xFF,
457 retval
= rtsx_write_register(chip
, CARD_PULL_CTL5
, 0xFF,
466 retval
= rtsx_write_register(chip
, CARD_PULL_CTL6
, 0xFF,
467 MS_D5_PD
| MS_D4_PD
);
472 } else if (CHECK_PID(chip
, 0x5288)) {
473 if (CHECK_BARO_PKG(chip
, QFN
)) {
474 retval
= rtsx_write_register(chip
, CARD_PULL_CTL1
,
480 retval
= rtsx_write_register(chip
, CARD_PULL_CTL2
,
486 retval
= rtsx_write_register(chip
, CARD_PULL_CTL3
,
492 retval
= rtsx_write_register(chip
, CARD_PULL_CTL4
,
501 return STATUS_SUCCESS
;
504 static int reset_xd(struct rtsx_chip
*chip
)
506 struct xd_info
*xd_card
= &chip
->xd_card
;
508 u8
*ptr
, id_buf
[4], redunt
[11];
510 retval
= select_card(chip
, XD_CARD
);
511 if (retval
!= STATUS_SUCCESS
) {
518 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_CHK_DATA_STATUS
, 0xFF,
520 if (chip
->asic_code
) {
521 if (!CHECK_PID(chip
, 0x5288))
522 xd_fill_pull_ctl_disable(chip
);
524 xd_fill_pull_ctl_stage1_barossa(chip
);
526 rtsx_add_cmd(chip
, WRITE_REG_CMD
, FPGA_PULL_CTL
, 0xFF,
527 (FPGA_XD_PULL_CTL_EN1
& FPGA_XD_PULL_CTL_EN3
) |
531 if (!chip
->ft2_fast_mode
)
532 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_INIT
,
533 XD_NO_AUTO_PWR_OFF
, 0);
535 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_OE
, XD_OUTPUT_EN
, 0);
537 retval
= rtsx_send_cmd(chip
, XD_CARD
, 100);
543 if (!chip
->ft2_fast_mode
) {
544 retval
= card_power_off(chip
, XD_CARD
);
545 if (retval
!= STATUS_SUCCESS
) {
554 if (chip
->asic_code
) {
555 xd_fill_pull_ctl_enable(chip
);
557 rtsx_add_cmd(chip
, WRITE_REG_CMD
, FPGA_PULL_CTL
, 0xFF,
558 (FPGA_XD_PULL_CTL_EN1
&
559 FPGA_XD_PULL_CTL_EN2
) |
563 retval
= rtsx_send_cmd(chip
, XD_CARD
, 100);
569 retval
= card_power_on(chip
, XD_CARD
);
570 if (retval
!= STATUS_SUCCESS
) {
577 if (chip
->ocp_stat
& (SD_OC_NOW
| SD_OC_EVER
)) {
578 dev_dbg(rtsx_dev(chip
), "Over current, OCPSTAT is 0x%x\n",
588 if (chip
->ft2_fast_mode
) {
589 if (chip
->asic_code
) {
590 xd_fill_pull_ctl_enable(chip
);
592 rtsx_add_cmd(chip
, WRITE_REG_CMD
, FPGA_PULL_CTL
, 0xFF,
593 (FPGA_XD_PULL_CTL_EN1
&
594 FPGA_XD_PULL_CTL_EN2
) |
599 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_OE
, XD_OUTPUT_EN
, XD_OUTPUT_EN
);
600 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_CTL
, XD_CE_DISEN
, XD_CE_DISEN
);
602 retval
= rtsx_send_cmd(chip
, XD_CARD
, 100);
608 if (!chip
->ft2_fast_mode
)
611 retval
= xd_set_init_para(chip
);
612 if (retval
!= STATUS_SUCCESS
) {
617 /* Read ID to check if the timing setting is right */
618 for (i
= 0; i
< 4; i
++) {
621 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_DTCTL
, 0xFF,
622 XD_TIME_SETUP_STEP
* 3 +
623 XD_TIME_RW_STEP
* (2 + i
) + XD_TIME_RWN_STEP
* i
);
624 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_CATCTL
, 0xFF,
625 XD_TIME_SETUP_STEP
* 3 +
626 XD_TIME_RW_STEP
* (4 + i
) +
627 XD_TIME_RWN_STEP
* (3 + i
));
629 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_TRANSFER
, 0xFF,
630 XD_TRANSFER_START
| XD_RESET
);
631 rtsx_add_cmd(chip
, CHECK_REG_CMD
, XD_TRANSFER
,
632 XD_TRANSFER_END
, XD_TRANSFER_END
);
634 rtsx_add_cmd(chip
, READ_REG_CMD
, XD_DAT
, 0, 0);
635 rtsx_add_cmd(chip
, READ_REG_CMD
, XD_CTL
, 0, 0);
637 retval
= rtsx_send_cmd(chip
, XD_CARD
, 100);
643 ptr
= rtsx_get_cmd_data(chip
) + 1;
645 dev_dbg(rtsx_dev(chip
), "XD_DAT: 0x%x, XD_CTL: 0x%x\n",
648 if (((ptr
[0] & READY_FLAG
) != READY_STATE
) ||
652 retval
= xd_read_id(chip
, READ_ID
, id_buf
, 4);
653 if (retval
!= STATUS_SUCCESS
) {
658 dev_dbg(rtsx_dev(chip
), "READ_ID: 0x%x 0x%x 0x%x 0x%x\n",
659 id_buf
[0], id_buf
[1], id_buf
[2], id_buf
[3]);
661 xd_card
->device_code
= id_buf
[1];
663 /* Check if the xD card is supported */
664 switch (xd_card
->device_code
) {
667 xd_card
->block_shift
= 4;
668 xd_card
->page_off
= 0x0F;
669 xd_card
->addr_cycle
= 3;
670 xd_card
->zone_cnt
= 1;
671 xd_card
->capacity
= 8000;
675 xd_card
->block_shift
= 4;
676 xd_card
->page_off
= 0x0F;
677 xd_card
->addr_cycle
= 3;
678 xd_card
->zone_cnt
= 1;
679 xd_card
->capacity
= 16000;
682 XD_PAGE_512(xd_card
);
683 xd_card
->addr_cycle
= 3;
684 xd_card
->zone_cnt
= 1;
685 xd_card
->capacity
= 32000;
688 XD_PAGE_512(xd_card
);
689 xd_card
->addr_cycle
= 3;
690 xd_card
->zone_cnt
= 2;
691 xd_card
->capacity
= 64000;
694 XD_PAGE_512(xd_card
);
695 xd_card
->addr_cycle
= 4;
696 xd_card
->zone_cnt
= 4;
697 xd_card
->capacity
= 128000;
700 XD_PAGE_512(xd_card
);
701 xd_card
->addr_cycle
= 4;
702 xd_card
->zone_cnt
= 8;
703 xd_card
->capacity
= 256000;
706 XD_PAGE_512(xd_card
);
707 xd_card
->addr_cycle
= 4;
708 xd_card
->zone_cnt
= 16;
709 xd_card
->capacity
= 512000;
712 XD_PAGE_512(xd_card
);
713 xd_card
->addr_cycle
= 4;
714 xd_card
->zone_cnt
= 32;
715 xd_card
->capacity
= 1024000;
718 XD_PAGE_512(xd_card
);
719 xd_card
->addr_cycle
= 4;
720 xd_card
->zone_cnt
= 64;
721 xd_card
->capacity
= 2048000;
724 XD_PAGE_512(xd_card
);
725 xd_card
->addr_cycle
= 4;
726 xd_card
->zone_cnt
= 128;
727 xd_card
->capacity
= 4096000;
733 /* Confirm timing setting */
734 for (j
= 0; j
< 10; j
++) {
735 retval
= xd_read_id(chip
, READ_ID
, id_buf
, 4);
736 if (retval
!= STATUS_SUCCESS
) {
741 if (id_buf
[1] != xd_card
->device_code
)
750 xd_card
->block_shift
= 0;
751 xd_card
->page_off
= 0;
752 xd_card
->addr_cycle
= 0;
753 xd_card
->capacity
= 0;
759 retval
= xd_read_id(chip
, READ_xD_ID
, id_buf
, 4);
760 if (retval
!= STATUS_SUCCESS
) {
764 dev_dbg(rtsx_dev(chip
), "READ_xD_ID: 0x%x 0x%x 0x%x 0x%x\n",
765 id_buf
[0], id_buf
[1], id_buf
[2], id_buf
[3]);
766 if (id_buf
[2] != XD_ID_CODE
) {
771 /* Search CIS block */
772 for (i
= 0; i
< 24; i
++) {
775 if (detect_card_cd(chip
, XD_CARD
) != STATUS_SUCCESS
) {
780 page_addr
= (u32
)i
<< xd_card
->block_shift
;
782 for (j
= 0; j
< 3; j
++) {
783 retval
= xd_read_redundant(chip
, page_addr
, redunt
, 11);
784 if (retval
== STATUS_SUCCESS
)
790 if (redunt
[BLOCK_STATUS
] != XD_GBLK
)
794 if (redunt
[PAGE_STATUS
] != XD_GPG
) {
795 for (j
= 1; j
<= 8; j
++) {
796 retval
= xd_read_redundant(chip
, page_addr
+ j
,
798 if (retval
== STATUS_SUCCESS
) {
799 if (redunt
[PAGE_STATUS
] == XD_GPG
)
809 if ((redunt
[BLOCK_STATUS
] == XD_GBLK
) &&
810 (redunt
[PARITY
] & XD_BA1_ALL0
)) {
815 retval
= xd_read_cis(chip
, page_addr
, buf
, 10);
816 if (retval
!= STATUS_SUCCESS
) {
821 if ((buf
[0] == 0x01) && (buf
[1] == 0x03) &&
823 (buf
[3] == 0x01) && (buf
[4] == 0xFF) &&
824 (buf
[5] == 0x18) && (buf
[6] == 0x02) &&
825 (buf
[7] == 0xDF) && (buf
[8] == 0x01) &&
827 xd_card
->cis_block
= (u16
)i
;
834 dev_dbg(rtsx_dev(chip
), "CIS block: 0x%x\n", xd_card
->cis_block
);
835 if (xd_card
->cis_block
== 0xFFFF) {
840 chip
->capacity
[chip
->card2lun
[XD_CARD
]] = xd_card
->capacity
;
842 return STATUS_SUCCESS
;
845 static int xd_check_data_blank(u8
*redunt
)
849 for (i
= 0; i
< 6; i
++) {
850 if (redunt
[PAGE_STATUS
+ i
] != 0xFF)
854 if ((redunt
[PARITY
] & (XD_ECC1_ALL1
| XD_ECC2_ALL1
))
855 != (XD_ECC1_ALL1
| XD_ECC2_ALL1
))
858 for (i
= 0; i
< 4; i
++) {
859 if (redunt
[RESERVED0
+ i
] != 0xFF)
866 static u16
xd_load_log_block_addr(u8
*redunt
)
870 if (redunt
[PARITY
] & XD_BA1_BA2_EQL
)
871 addr
= ((u16
)redunt
[BLOCK_ADDR1_H
] << 8) |
872 redunt
[BLOCK_ADDR1_L
];
873 else if (redunt
[PARITY
] & XD_BA1_VALID
)
874 addr
= ((u16
)redunt
[BLOCK_ADDR1_H
] << 8) |
875 redunt
[BLOCK_ADDR1_L
];
876 else if (redunt
[PARITY
] & XD_BA2_VALID
)
877 addr
= ((u16
)redunt
[BLOCK_ADDR2_H
] << 8) |
878 redunt
[BLOCK_ADDR2_L
];
883 static int xd_init_l2p_tbl(struct rtsx_chip
*chip
)
885 struct xd_info
*xd_card
= &chip
->xd_card
;
888 dev_dbg(rtsx_dev(chip
), "%s: zone_cnt = %d\n", __func__
,
891 if (xd_card
->zone_cnt
< 1) {
896 size
= xd_card
->zone_cnt
* sizeof(struct zone_entry
);
897 dev_dbg(rtsx_dev(chip
), "Buffer size for l2p table is %d\n", size
);
899 xd_card
->zone
= vmalloc(size
);
900 if (!xd_card
->zone
) {
905 for (i
= 0; i
< xd_card
->zone_cnt
; i
++) {
906 xd_card
->zone
[i
].build_flag
= 0;
907 xd_card
->zone
[i
].l2p_table
= NULL
;
908 xd_card
->zone
[i
].free_table
= NULL
;
909 xd_card
->zone
[i
].get_index
= 0;
910 xd_card
->zone
[i
].set_index
= 0;
911 xd_card
->zone
[i
].unused_blk_cnt
= 0;
914 return STATUS_SUCCESS
;
917 static inline void free_zone(struct zone_entry
*zone
)
922 zone
->build_flag
= 0;
925 zone
->unused_blk_cnt
= 0;
926 vfree(zone
->l2p_table
);
927 zone
->l2p_table
= NULL
;
928 vfree(zone
->free_table
);
929 zone
->free_table
= NULL
;
932 static void xd_set_unused_block(struct rtsx_chip
*chip
, u32 phy_blk
)
934 struct xd_info
*xd_card
= &chip
->xd_card
;
935 struct zone_entry
*zone
;
938 zone_no
= (int)phy_blk
>> 10;
939 if (zone_no
>= xd_card
->zone_cnt
) {
940 dev_dbg(rtsx_dev(chip
), "Set unused block to invalid zone (zone_no = %d, zone_cnt = %d)\n",
941 zone_no
, xd_card
->zone_cnt
);
944 zone
= &xd_card
->zone
[zone_no
];
946 if (!zone
->free_table
) {
947 if (xd_build_l2p_tbl(chip
, zone_no
) != STATUS_SUCCESS
)
951 if ((zone
->set_index
>= XD_FREE_TABLE_CNT
) ||
952 (zone
->set_index
< 0)) {
954 dev_dbg(rtsx_dev(chip
), "Set unused block fail, invalid set_index\n");
958 dev_dbg(rtsx_dev(chip
), "Set unused block to index %d\n",
961 zone
->free_table
[zone
->set_index
++] = (u16
)(phy_blk
& 0x3ff);
962 if (zone
->set_index
>= XD_FREE_TABLE_CNT
)
964 zone
->unused_blk_cnt
++;
967 static u32
xd_get_unused_block(struct rtsx_chip
*chip
, int zone_no
)
969 struct xd_info
*xd_card
= &chip
->xd_card
;
970 struct zone_entry
*zone
;
973 if (zone_no
>= xd_card
->zone_cnt
) {
974 dev_dbg(rtsx_dev(chip
), "Get unused block from invalid zone (zone_no = %d, zone_cnt = %d)\n",
975 zone_no
, xd_card
->zone_cnt
);
976 return BLK_NOT_FOUND
;
978 zone
= &xd_card
->zone
[zone_no
];
980 if ((zone
->unused_blk_cnt
== 0) ||
981 (zone
->set_index
== zone
->get_index
)) {
983 dev_dbg(rtsx_dev(chip
), "Get unused block fail, no unused block available\n");
984 return BLK_NOT_FOUND
;
986 if ((zone
->get_index
>= XD_FREE_TABLE_CNT
) || (zone
->get_index
< 0)) {
988 dev_dbg(rtsx_dev(chip
), "Get unused block fail, invalid get_index\n");
989 return BLK_NOT_FOUND
;
992 dev_dbg(rtsx_dev(chip
), "Get unused block from index %d\n",
995 phy_blk
= zone
->free_table
[zone
->get_index
];
996 zone
->free_table
[zone
->get_index
++] = 0xFFFF;
997 if (zone
->get_index
>= XD_FREE_TABLE_CNT
)
999 zone
->unused_blk_cnt
--;
1001 phy_blk
+= ((u32
)(zone_no
) << 10);
1005 static void xd_set_l2p_tbl(struct rtsx_chip
*chip
,
1006 int zone_no
, u16 log_off
, u16 phy_off
)
1008 struct xd_info
*xd_card
= &chip
->xd_card
;
1009 struct zone_entry
*zone
;
1011 zone
= &xd_card
->zone
[zone_no
];
1012 zone
->l2p_table
[log_off
] = phy_off
;
1015 static u32
xd_get_l2p_tbl(struct rtsx_chip
*chip
, int zone_no
, u16 log_off
)
1017 struct xd_info
*xd_card
= &chip
->xd_card
;
1018 struct zone_entry
*zone
;
1021 zone
= &xd_card
->zone
[zone_no
];
1022 if (zone
->l2p_table
[log_off
] == 0xFFFF) {
1026 #ifdef XD_DELAY_WRITE
1027 retval
= xd_delay_write(chip
);
1028 if (retval
!= STATUS_SUCCESS
) {
1029 dev_dbg(rtsx_dev(chip
), "In %s, delay write fail!\n",
1031 return BLK_NOT_FOUND
;
1035 if (zone
->unused_blk_cnt
<= 0) {
1036 dev_dbg(rtsx_dev(chip
), "No unused block!\n");
1037 return BLK_NOT_FOUND
;
1040 for (i
= 0; i
< zone
->unused_blk_cnt
; i
++) {
1041 phy_blk
= xd_get_unused_block(chip
, zone_no
);
1042 if (phy_blk
== BLK_NOT_FOUND
) {
1043 dev_dbg(rtsx_dev(chip
), "No unused block available!\n");
1044 return BLK_NOT_FOUND
;
1047 retval
= xd_init_page(chip
, phy_blk
, log_off
,
1048 0, xd_card
->page_off
+ 1);
1049 if (retval
== STATUS_SUCCESS
)
1052 if (i
>= zone
->unused_blk_cnt
) {
1053 dev_dbg(rtsx_dev(chip
), "No good unused block available!\n");
1054 return BLK_NOT_FOUND
;
1057 xd_set_l2p_tbl(chip
, zone_no
, log_off
, (u16
)(phy_blk
& 0x3FF));
1061 return (u32
)zone
->l2p_table
[log_off
] + ((u32
)(zone_no
) << 10);
1064 int reset_xd_card(struct rtsx_chip
*chip
)
1066 struct xd_info
*xd_card
= &chip
->xd_card
;
1069 memset(xd_card
, 0, sizeof(struct xd_info
));
1071 xd_card
->block_shift
= 0;
1072 xd_card
->page_off
= 0;
1073 xd_card
->addr_cycle
= 0;
1074 xd_card
->capacity
= 0;
1075 xd_card
->zone_cnt
= 0;
1076 xd_card
->cis_block
= 0xFFFF;
1077 xd_card
->delay_write
.delay_write_flag
= 0;
1079 retval
= enable_card_clock(chip
, XD_CARD
);
1080 if (retval
!= STATUS_SUCCESS
) {
1085 retval
= reset_xd(chip
);
1086 if (retval
!= STATUS_SUCCESS
) {
1091 retval
= xd_init_l2p_tbl(chip
);
1092 if (retval
!= STATUS_SUCCESS
) {
1097 return STATUS_SUCCESS
;
1100 static int xd_mark_bad_block(struct rtsx_chip
*chip
, u32 phy_blk
)
1102 struct xd_info
*xd_card
= &chip
->xd_card
;
1107 dev_dbg(rtsx_dev(chip
), "mark block 0x%x as bad block\n", phy_blk
);
1109 if (phy_blk
== BLK_NOT_FOUND
) {
1114 rtsx_init_cmd(chip
);
1116 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_PAGE_STATUS
, 0xFF, XD_GPG
);
1117 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_BLOCK_STATUS
, 0xFF, XD_LATER_BBLK
);
1118 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_BLOCK_ADDR1_H
, 0xFF, 0xFF);
1119 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_BLOCK_ADDR1_L
, 0xFF, 0xFF);
1120 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_BLOCK_ADDR2_H
, 0xFF, 0xFF);
1121 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_BLOCK_ADDR2_L
, 0xFF, 0xFF);
1122 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_RESERVED0
, 0xFF, 0xFF);
1123 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_RESERVED1
, 0xFF, 0xFF);
1124 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_RESERVED2
, 0xFF, 0xFF);
1125 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_RESERVED3
, 0xFF, 0xFF);
1127 page_addr
= phy_blk
<< xd_card
->block_shift
;
1129 xd_assign_phy_addr(chip
, page_addr
, XD_RW_ADDR
);
1131 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_PAGE_CNT
, 0xFF,
1132 xd_card
->page_off
+ 1);
1134 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_TRANSFER
, 0xFF,
1135 XD_TRANSFER_START
| XD_WRITE_REDUNDANT
);
1136 rtsx_add_cmd(chip
, CHECK_REG_CMD
, XD_TRANSFER
,
1137 XD_TRANSFER_END
, XD_TRANSFER_END
);
1139 retval
= rtsx_send_cmd(chip
, XD_CARD
, 500);
1141 rtsx_clear_xd_error(chip
);
1142 rtsx_read_register(chip
, XD_DAT
, ®
);
1143 if (reg
& PROGRAM_ERROR
)
1144 xd_set_err_code(chip
, XD_PRG_ERROR
);
1146 xd_set_err_code(chip
, XD_TO_ERROR
);
1151 return STATUS_SUCCESS
;
1154 static int xd_init_page(struct rtsx_chip
*chip
, u32 phy_blk
,
1155 u16 logoff
, u8 start_page
, u8 end_page
)
1157 struct xd_info
*xd_card
= &chip
->xd_card
;
1162 dev_dbg(rtsx_dev(chip
), "Init block 0x%x\n", phy_blk
);
1164 if (start_page
> end_page
) {
1168 if (phy_blk
== BLK_NOT_FOUND
) {
1173 rtsx_init_cmd(chip
);
1175 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_PAGE_STATUS
, 0xFF, 0xFF);
1176 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_BLOCK_STATUS
, 0xFF, 0xFF);
1177 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_BLOCK_ADDR1_H
,
1178 0xFF, (u8
)(logoff
>> 8));
1179 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_BLOCK_ADDR1_L
, 0xFF, (u8
)logoff
);
1181 page_addr
= (phy_blk
<< xd_card
->block_shift
) + start_page
;
1183 xd_assign_phy_addr(chip
, page_addr
, XD_RW_ADDR
);
1185 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_CFG
,
1186 XD_BA_TRANSFORM
, XD_BA_TRANSFORM
);
1188 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_PAGE_CNT
,
1189 0xFF, (end_page
- start_page
));
1191 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_TRANSFER
,
1192 0xFF, XD_TRANSFER_START
| XD_WRITE_REDUNDANT
);
1193 rtsx_add_cmd(chip
, CHECK_REG_CMD
, XD_TRANSFER
,
1194 XD_TRANSFER_END
, XD_TRANSFER_END
);
1196 retval
= rtsx_send_cmd(chip
, XD_CARD
, 500);
1198 rtsx_clear_xd_error(chip
);
1199 rtsx_read_register(chip
, XD_DAT
, ®
);
1200 if (reg
& PROGRAM_ERROR
) {
1201 xd_mark_bad_block(chip
, phy_blk
);
1202 xd_set_err_code(chip
, XD_PRG_ERROR
);
1204 xd_set_err_code(chip
, XD_TO_ERROR
);
1210 return STATUS_SUCCESS
;
1213 static int xd_copy_page(struct rtsx_chip
*chip
, u32 old_blk
, u32 new_blk
,
1214 u8 start_page
, u8 end_page
)
1216 struct xd_info
*xd_card
= &chip
->xd_card
;
1217 u32 old_page
, new_page
;
1221 dev_dbg(rtsx_dev(chip
), "Copy page from block 0x%x to block 0x%x\n",
1224 if (start_page
> end_page
) {
1229 if ((old_blk
== BLK_NOT_FOUND
) || (new_blk
== BLK_NOT_FOUND
)) {
1234 old_page
= (old_blk
<< xd_card
->block_shift
) + start_page
;
1235 new_page
= (new_blk
<< xd_card
->block_shift
) + start_page
;
1237 XD_CLR_BAD_NEWBLK(xd_card
);
1239 retval
= rtsx_write_register(chip
, CARD_DATA_SOURCE
, 0x01,
1246 for (i
= start_page
; i
< end_page
; i
++) {
1247 if (detect_card_cd(chip
, XD_CARD
) != STATUS_SUCCESS
) {
1248 rtsx_clear_xd_error(chip
);
1249 xd_set_err_code(chip
, XD_NO_CARD
);
1254 rtsx_init_cmd(chip
);
1256 xd_assign_phy_addr(chip
, old_page
, XD_RW_ADDR
);
1258 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_PAGE_CNT
, 0xFF, 1);
1259 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_CHK_DATA_STATUS
,
1260 XD_AUTO_CHK_DATA_STATUS
, 0);
1261 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_TRANSFER
, 0xFF,
1262 XD_TRANSFER_START
| XD_READ_PAGES
);
1263 rtsx_add_cmd(chip
, CHECK_REG_CMD
, XD_TRANSFER
,
1264 XD_TRANSFER_END
, XD_TRANSFER_END
);
1266 retval
= rtsx_send_cmd(chip
, XD_CARD
, 500);
1268 rtsx_clear_xd_error(chip
);
1270 rtsx_read_register(chip
, XD_CTL
, ®
);
1271 if (reg
& (XD_ECC1_ERROR
| XD_ECC2_ERROR
)) {
1274 if (detect_card_cd(chip
,
1275 XD_CARD
) != STATUS_SUCCESS
) {
1276 xd_set_err_code(chip
, XD_NO_CARD
);
1281 if (((reg
& (XD_ECC1_ERROR
| XD_ECC1_UNCORRECTABLE
)) ==
1282 (XD_ECC1_ERROR
| XD_ECC1_UNCORRECTABLE
)) ||
1283 ((reg
& (XD_ECC2_ERROR
| XD_ECC2_UNCORRECTABLE
)) ==
1284 (XD_ECC2_ERROR
| XD_ECC2_UNCORRECTABLE
))) {
1285 rtsx_write_register(chip
,
1289 rtsx_write_register(chip
,
1293 XD_SET_BAD_OLDBLK(xd_card
);
1294 dev_dbg(rtsx_dev(chip
), "old block 0x%x ecc error\n",
1298 xd_set_err_code(chip
, XD_TO_ERROR
);
1304 if (XD_CHK_BAD_OLDBLK(xd_card
))
1305 rtsx_clear_xd_error(chip
);
1307 rtsx_init_cmd(chip
);
1309 xd_assign_phy_addr(chip
, new_page
, XD_RW_ADDR
);
1310 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_PAGE_CNT
, 0xFF, 1);
1311 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_TRANSFER
, 0xFF,
1312 XD_TRANSFER_START
| XD_WRITE_PAGES
);
1313 rtsx_add_cmd(chip
, CHECK_REG_CMD
, XD_TRANSFER
,
1314 XD_TRANSFER_END
, XD_TRANSFER_END
);
1316 retval
= rtsx_send_cmd(chip
, XD_CARD
, 300);
1318 rtsx_clear_xd_error(chip
);
1320 rtsx_read_register(chip
, XD_DAT
, ®
);
1321 if (reg
& PROGRAM_ERROR
) {
1322 xd_mark_bad_block(chip
, new_blk
);
1323 xd_set_err_code(chip
, XD_PRG_ERROR
);
1324 XD_SET_BAD_NEWBLK(xd_card
);
1326 xd_set_err_code(chip
, XD_TO_ERROR
);
1336 return STATUS_SUCCESS
;
1339 static int xd_reset_cmd(struct rtsx_chip
*chip
)
1344 rtsx_init_cmd(chip
);
1346 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_TRANSFER
,
1347 0xFF, XD_TRANSFER_START
| XD_RESET
);
1348 rtsx_add_cmd(chip
, CHECK_REG_CMD
, XD_TRANSFER
,
1349 XD_TRANSFER_END
, XD_TRANSFER_END
);
1350 rtsx_add_cmd(chip
, READ_REG_CMD
, XD_DAT
, 0, 0);
1351 rtsx_add_cmd(chip
, READ_REG_CMD
, XD_CTL
, 0, 0);
1353 retval
= rtsx_send_cmd(chip
, XD_CARD
, 100);
1359 ptr
= rtsx_get_cmd_data(chip
) + 1;
1360 if (((ptr
[0] & READY_FLAG
) == READY_STATE
) && (ptr
[1] & XD_RDY
))
1361 return STATUS_SUCCESS
;
1367 static int xd_erase_block(struct rtsx_chip
*chip
, u32 phy_blk
)
1369 struct xd_info
*xd_card
= &chip
->xd_card
;
1374 if (phy_blk
== BLK_NOT_FOUND
) {
1379 page_addr
= phy_blk
<< xd_card
->block_shift
;
1381 for (i
= 0; i
< 3; i
++) {
1382 rtsx_init_cmd(chip
);
1384 xd_assign_phy_addr(chip
, page_addr
, XD_ERASE_ADDR
);
1386 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_TRANSFER
, 0xFF,
1387 XD_TRANSFER_START
| XD_ERASE
);
1388 rtsx_add_cmd(chip
, CHECK_REG_CMD
, XD_TRANSFER
,
1389 XD_TRANSFER_END
, XD_TRANSFER_END
);
1390 rtsx_add_cmd(chip
, READ_REG_CMD
, XD_DAT
, 0, 0);
1392 retval
= rtsx_send_cmd(chip
, XD_CARD
, 250);
1394 rtsx_clear_xd_error(chip
);
1395 rtsx_read_register(chip
, XD_DAT
, ®
);
1396 if (reg
& PROGRAM_ERROR
) {
1397 xd_mark_bad_block(chip
, phy_blk
);
1398 xd_set_err_code(chip
, XD_PRG_ERROR
);
1402 xd_set_err_code(chip
, XD_ERASE_FAIL
);
1403 retval
= xd_reset_cmd(chip
);
1404 if (retval
!= STATUS_SUCCESS
) {
1411 ptr
= rtsx_get_cmd_data(chip
) + 1;
1412 if (*ptr
& PROGRAM_ERROR
) {
1413 xd_mark_bad_block(chip
, phy_blk
);
1414 xd_set_err_code(chip
, XD_PRG_ERROR
);
1419 return STATUS_SUCCESS
;
1422 xd_mark_bad_block(chip
, phy_blk
);
1423 xd_set_err_code(chip
, XD_ERASE_FAIL
);
1428 static int xd_build_l2p_tbl(struct rtsx_chip
*chip
, int zone_no
)
1430 struct xd_info
*xd_card
= &chip
->xd_card
;
1431 struct zone_entry
*zone
;
1434 u16 max_logoff
, cur_fst_page_logoff
;
1435 u16 cur_lst_page_logoff
, ent_lst_page_logoff
;
1438 dev_dbg(rtsx_dev(chip
), "%s: %d\n", __func__
, zone_no
);
1440 if (!xd_card
->zone
) {
1441 retval
= xd_init_l2p_tbl(chip
);
1442 if (retval
!= STATUS_SUCCESS
)
1446 if (xd_card
->zone
[zone_no
].build_flag
) {
1447 dev_dbg(rtsx_dev(chip
), "l2p table of zone %d has been built\n",
1449 return STATUS_SUCCESS
;
1452 zone
= &xd_card
->zone
[zone_no
];
1454 if (!zone
->l2p_table
) {
1455 zone
->l2p_table
= vmalloc(2000);
1456 if (!zone
->l2p_table
) {
1461 memset((u8
*)(zone
->l2p_table
), 0xff, 2000);
1463 if (!zone
->free_table
) {
1464 zone
->free_table
= vmalloc(XD_FREE_TABLE_CNT
* 2);
1465 if (!zone
->free_table
) {
1470 memset((u8
*)(zone
->free_table
), 0xff, XD_FREE_TABLE_CNT
* 2);
1473 if (xd_card
->cis_block
== 0xFFFF)
1476 start
= xd_card
->cis_block
+ 1;
1477 if (XD_CHK_4MB(xd_card
)) {
1485 start
= (u32
)(zone_no
) << 10;
1486 end
= (u32
)(zone_no
+ 1) << 10;
1490 dev_dbg(rtsx_dev(chip
), "start block 0x%x, end block 0x%x\n",
1493 zone
->set_index
= 0;
1494 zone
->get_index
= 0;
1495 zone
->unused_blk_cnt
= 0;
1497 for (i
= start
; i
< end
; i
++) {
1498 u32 page_addr
= i
<< xd_card
->block_shift
;
1501 retval
= xd_read_redundant(chip
, page_addr
, redunt
, 11);
1502 if (retval
!= STATUS_SUCCESS
)
1505 if (redunt
[BLOCK_STATUS
] != 0xFF) {
1506 dev_dbg(rtsx_dev(chip
), "bad block\n");
1510 if (xd_check_data_blank(redunt
)) {
1511 dev_dbg(rtsx_dev(chip
), "blank block\n");
1512 xd_set_unused_block(chip
, i
);
1516 cur_fst_page_logoff
= xd_load_log_block_addr(redunt
);
1517 if ((cur_fst_page_logoff
== 0xFFFF) ||
1518 (cur_fst_page_logoff
> max_logoff
)) {
1519 retval
= xd_erase_block(chip
, i
);
1520 if (retval
== STATUS_SUCCESS
)
1521 xd_set_unused_block(chip
, i
);
1525 if ((zone_no
== 0) && (cur_fst_page_logoff
== 0) &&
1526 (redunt
[PAGE_STATUS
] != XD_GPG
))
1527 XD_SET_MBR_FAIL(xd_card
);
1529 if (zone
->l2p_table
[cur_fst_page_logoff
] == 0xFFFF) {
1530 zone
->l2p_table
[cur_fst_page_logoff
] = (u16
)(i
& 0x3FF);
1534 phy_block
= zone
->l2p_table
[cur_fst_page_logoff
] +
1535 ((u32
)((zone_no
) << 10));
1537 page_addr
= ((i
+ 1) << xd_card
->block_shift
) - 1;
1539 retval
= xd_read_redundant(chip
, page_addr
, redunt
, 11);
1540 if (retval
!= STATUS_SUCCESS
)
1543 cur_lst_page_logoff
= xd_load_log_block_addr(redunt
);
1544 if (cur_lst_page_logoff
== cur_fst_page_logoff
) {
1547 page_addr
= ((phy_block
+ 1) <<
1548 xd_card
->block_shift
) - 1;
1550 for (m
= 0; m
< 3; m
++) {
1551 retval
= xd_read_redundant(chip
, page_addr
,
1553 if (retval
== STATUS_SUCCESS
)
1558 zone
->l2p_table
[cur_fst_page_logoff
] =
1560 retval
= xd_erase_block(chip
, phy_block
);
1561 if (retval
== STATUS_SUCCESS
)
1562 xd_set_unused_block(chip
, phy_block
);
1566 ent_lst_page_logoff
= xd_load_log_block_addr(redunt
);
1567 if (ent_lst_page_logoff
!= cur_fst_page_logoff
) {
1568 zone
->l2p_table
[cur_fst_page_logoff
] =
1570 retval
= xd_erase_block(chip
, phy_block
);
1571 if (retval
== STATUS_SUCCESS
)
1572 xd_set_unused_block(chip
, phy_block
);
1575 retval
= xd_erase_block(chip
, i
);
1576 if (retval
== STATUS_SUCCESS
)
1577 xd_set_unused_block(chip
, i
);
1580 retval
= xd_erase_block(chip
, i
);
1581 if (retval
== STATUS_SUCCESS
)
1582 xd_set_unused_block(chip
, i
);
1586 if (XD_CHK_4MB(xd_card
))
1592 for (start
= 0; start
< end
; start
++) {
1593 if (zone
->l2p_table
[start
] == 0xFFFF)
1597 dev_dbg(rtsx_dev(chip
), "Block count %d, invalid L2P entry %d\n",
1599 dev_dbg(rtsx_dev(chip
), "Total unused block: %d\n",
1600 zone
->unused_blk_cnt
);
1602 if ((zone
->unused_blk_cnt
- i
) < 1)
1603 chip
->card_wp
|= XD_CARD
;
1605 zone
->build_flag
= 1;
1607 return STATUS_SUCCESS
;
1610 vfree(zone
->l2p_table
);
1611 zone
->l2p_table
= NULL
;
1612 vfree(zone
->free_table
);
1613 zone
->free_table
= NULL
;
1618 static int xd_send_cmd(struct rtsx_chip
*chip
, u8 cmd
)
1622 rtsx_init_cmd(chip
);
1624 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_DAT
, 0xFF, cmd
);
1625 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_TRANSFER
, 0xFF,
1626 XD_TRANSFER_START
| XD_SET_CMD
);
1627 rtsx_add_cmd(chip
, CHECK_REG_CMD
, XD_TRANSFER
,
1628 XD_TRANSFER_END
, XD_TRANSFER_END
);
1630 retval
= rtsx_send_cmd(chip
, XD_CARD
, 200);
1636 return STATUS_SUCCESS
;
1639 static int xd_read_multiple_pages(struct rtsx_chip
*chip
, u32 phy_blk
,
1640 u32 log_blk
, u8 start_page
, u8 end_page
,
1641 u8
*buf
, unsigned int *index
,
1642 unsigned int *offset
)
1644 struct xd_info
*xd_card
= &chip
->xd_card
;
1645 u32 page_addr
, new_blk
;
1647 u8 reg_val
, page_cnt
;
1648 int zone_no
, retval
, i
;
1650 if (start_page
> end_page
)
1653 page_cnt
= end_page
- start_page
;
1654 zone_no
= (int)(log_blk
/ 1000);
1655 log_off
= (u16
)(log_blk
% 1000);
1657 if ((phy_blk
& 0x3FF) == 0x3FF) {
1658 for (i
= 0; i
< 256; i
++) {
1659 page_addr
= ((u32
)i
) << xd_card
->block_shift
;
1661 retval
= xd_read_redundant(chip
, page_addr
, NULL
, 0);
1662 if (retval
== STATUS_SUCCESS
)
1665 if (detect_card_cd(chip
, XD_CARD
) != STATUS_SUCCESS
) {
1666 xd_set_err_code(chip
, XD_NO_CARD
);
1672 page_addr
= (phy_blk
<< xd_card
->block_shift
) + start_page
;
1674 rtsx_init_cmd(chip
);
1676 xd_assign_phy_addr(chip
, page_addr
, XD_RW_ADDR
);
1677 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_CFG
, XD_PPB_TO_SIE
, XD_PPB_TO_SIE
);
1678 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
, 0x01, RING_BUFFER
);
1679 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_PAGE_CNT
, 0xFF, page_cnt
);
1680 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_CHK_DATA_STATUS
,
1681 XD_AUTO_CHK_DATA_STATUS
, XD_AUTO_CHK_DATA_STATUS
);
1683 trans_dma_enable(chip
->srb
->sc_data_direction
, chip
,
1684 page_cnt
* 512, DMA_512
);
1686 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_TRANSFER
, 0xFF,
1687 XD_TRANSFER_START
| XD_READ_PAGES
);
1688 rtsx_add_cmd(chip
, CHECK_REG_CMD
, XD_TRANSFER
,
1689 XD_TRANSFER_END
| XD_PPB_EMPTY
,
1690 XD_TRANSFER_END
| XD_PPB_EMPTY
);
1692 rtsx_send_cmd_no_wait(chip
);
1694 retval
= rtsx_transfer_data_partial(chip
, XD_CARD
, buf
, page_cnt
* 512,
1695 scsi_sg_count(chip
->srb
),
1696 index
, offset
, DMA_FROM_DEVICE
,
1699 rtsx_clear_xd_error(chip
);
1701 if (retval
== -ETIMEDOUT
) {
1702 xd_set_err_code(chip
, XD_TO_ERROR
);
1710 return STATUS_SUCCESS
;
1713 retval
= rtsx_read_register(chip
, XD_PAGE_STATUS
, ®_val
);
1719 if (reg_val
!= XD_GPG
)
1720 xd_set_err_code(chip
, XD_PRG_ERROR
);
1722 retval
= rtsx_read_register(chip
, XD_CTL
, ®_val
);
1728 if (((reg_val
& (XD_ECC1_ERROR
| XD_ECC1_UNCORRECTABLE
)) ==
1729 (XD_ECC1_ERROR
| XD_ECC1_UNCORRECTABLE
)) ||
1730 ((reg_val
& (XD_ECC2_ERROR
| XD_ECC2_UNCORRECTABLE
)) ==
1731 (XD_ECC2_ERROR
| XD_ECC2_UNCORRECTABLE
))) {
1734 if (detect_card_cd(chip
, XD_CARD
) != STATUS_SUCCESS
) {
1735 xd_set_err_code(chip
, XD_NO_CARD
);
1739 xd_set_err_code(chip
, XD_ECC_ERROR
);
1741 new_blk
= xd_get_unused_block(chip
, zone_no
);
1742 if (new_blk
== NO_NEW_BLK
) {
1743 XD_CLR_BAD_OLDBLK(xd_card
);
1747 retval
= xd_copy_page(chip
, phy_blk
, new_blk
, 0,
1748 xd_card
->page_off
+ 1);
1749 if (retval
!= STATUS_SUCCESS
) {
1750 if (!XD_CHK_BAD_NEWBLK(xd_card
)) {
1751 retval
= xd_erase_block(chip
, new_blk
);
1752 if (retval
== STATUS_SUCCESS
)
1753 xd_set_unused_block(chip
, new_blk
);
1755 XD_CLR_BAD_NEWBLK(xd_card
);
1757 XD_CLR_BAD_OLDBLK(xd_card
);
1760 xd_set_l2p_tbl(chip
, zone_no
, log_off
, (u16
)(new_blk
& 0x3FF));
1761 xd_erase_block(chip
, phy_blk
);
1762 xd_mark_bad_block(chip
, phy_blk
);
1763 XD_CLR_BAD_OLDBLK(xd_card
);
1771 static int xd_finish_write(struct rtsx_chip
*chip
,
1772 u32 old_blk
, u32 new_blk
, u32 log_blk
, u8 page_off
)
1774 struct xd_info
*xd_card
= &chip
->xd_card
;
1775 int retval
, zone_no
;
1778 dev_dbg(rtsx_dev(chip
), "%s ", __func__
);
1779 dev_dbg(rtsx_dev(chip
), "old_blk = 0x%x, ", old_blk
);
1780 dev_dbg(rtsx_dev(chip
), "new_blk = 0x%x, ", new_blk
);
1781 dev_dbg(rtsx_dev(chip
), "log_blk = 0x%x\n", log_blk
);
1783 if (page_off
> xd_card
->page_off
) {
1788 zone_no
= (int)(log_blk
/ 1000);
1789 log_off
= (u16
)(log_blk
% 1000);
1791 if (old_blk
== BLK_NOT_FOUND
) {
1792 retval
= xd_init_page(chip
, new_blk
, log_off
,
1793 page_off
, xd_card
->page_off
+ 1);
1794 if (retval
!= STATUS_SUCCESS
) {
1795 retval
= xd_erase_block(chip
, new_blk
);
1796 if (retval
== STATUS_SUCCESS
)
1797 xd_set_unused_block(chip
, new_blk
);
1802 retval
= xd_copy_page(chip
, old_blk
, new_blk
,
1803 page_off
, xd_card
->page_off
+ 1);
1804 if (retval
!= STATUS_SUCCESS
) {
1805 if (!XD_CHK_BAD_NEWBLK(xd_card
)) {
1806 retval
= xd_erase_block(chip
, new_blk
);
1807 if (retval
== STATUS_SUCCESS
)
1808 xd_set_unused_block(chip
, new_blk
);
1810 XD_CLR_BAD_NEWBLK(xd_card
);
1815 retval
= xd_erase_block(chip
, old_blk
);
1816 if (retval
== STATUS_SUCCESS
) {
1817 if (XD_CHK_BAD_OLDBLK(xd_card
)) {
1818 xd_mark_bad_block(chip
, old_blk
);
1819 XD_CLR_BAD_OLDBLK(xd_card
);
1821 xd_set_unused_block(chip
, old_blk
);
1824 xd_set_err_code(chip
, XD_NO_ERROR
);
1825 XD_CLR_BAD_OLDBLK(xd_card
);
1829 xd_set_l2p_tbl(chip
, zone_no
, log_off
, (u16
)(new_blk
& 0x3FF));
1831 return STATUS_SUCCESS
;
1834 static int xd_prepare_write(struct rtsx_chip
*chip
,
1835 u32 old_blk
, u32 new_blk
, u32 log_blk
, u8 page_off
)
1839 dev_dbg(rtsx_dev(chip
), "%s, old_blk = 0x%x, new_blk = 0x%x, log_blk = 0x%x, page_off = %d\n",
1840 __func__
, old_blk
, new_blk
, log_blk
, (int)page_off
);
1843 retval
= xd_copy_page(chip
, old_blk
, new_blk
, 0, page_off
);
1844 if (retval
!= STATUS_SUCCESS
) {
1850 return STATUS_SUCCESS
;
1853 static int xd_write_multiple_pages(struct rtsx_chip
*chip
, u32 old_blk
,
1854 u32 new_blk
, u32 log_blk
, u8 start_page
,
1855 u8 end_page
, u8
*buf
, unsigned int *index
,
1856 unsigned int *offset
)
1858 struct xd_info
*xd_card
= &chip
->xd_card
;
1860 int zone_no
, retval
;
1862 u8 page_cnt
, reg_val
;
1864 dev_dbg(rtsx_dev(chip
), "%s, old_blk = 0x%x, new_blk = 0x%x, log_blk = 0x%x\n",
1865 __func__
, old_blk
, new_blk
, log_blk
);
1867 if (start_page
> end_page
)
1870 page_cnt
= end_page
- start_page
;
1871 zone_no
= (int)(log_blk
/ 1000);
1872 log_off
= (u16
)(log_blk
% 1000);
1874 page_addr
= (new_blk
<< xd_card
->block_shift
) + start_page
;
1876 retval
= xd_send_cmd(chip
, READ1_1
);
1877 if (retval
!= STATUS_SUCCESS
)
1880 rtsx_init_cmd(chip
);
1882 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_BLOCK_ADDR1_H
,
1883 0xFF, (u8
)(log_off
>> 8));
1884 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_BLOCK_ADDR1_L
, 0xFF, (u8
)log_off
);
1885 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_BLOCK_STATUS
, 0xFF, XD_GBLK
);
1886 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_PAGE_STATUS
, 0xFF, XD_GPG
);
1888 xd_assign_phy_addr(chip
, page_addr
, XD_RW_ADDR
);
1890 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_CFG
, XD_BA_TRANSFORM
,
1892 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_PAGE_CNT
, 0xFF, page_cnt
);
1893 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
, 0x01, RING_BUFFER
);
1895 trans_dma_enable(chip
->srb
->sc_data_direction
, chip
,
1896 page_cnt
* 512, DMA_512
);
1898 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_TRANSFER
,
1899 0xFF, XD_TRANSFER_START
| XD_WRITE_PAGES
);
1900 rtsx_add_cmd(chip
, CHECK_REG_CMD
, XD_TRANSFER
,
1901 XD_TRANSFER_END
, XD_TRANSFER_END
);
1903 rtsx_send_cmd_no_wait(chip
);
1905 retval
= rtsx_transfer_data_partial(chip
, XD_CARD
, buf
, page_cnt
* 512,
1906 scsi_sg_count(chip
->srb
),
1907 index
, offset
, DMA_TO_DEVICE
, chip
->xd_timeout
);
1909 rtsx_clear_xd_error(chip
);
1911 if (retval
== -ETIMEDOUT
) {
1912 xd_set_err_code(chip
, XD_TO_ERROR
);
1920 if (end_page
== (xd_card
->page_off
+ 1)) {
1921 xd_card
->delay_write
.delay_write_flag
= 0;
1923 if (old_blk
!= BLK_NOT_FOUND
) {
1924 retval
= xd_erase_block(chip
, old_blk
);
1925 if (retval
== STATUS_SUCCESS
) {
1926 if (XD_CHK_BAD_OLDBLK(xd_card
)) {
1927 xd_mark_bad_block(chip
, old_blk
);
1928 XD_CLR_BAD_OLDBLK(xd_card
);
1930 xd_set_unused_block(chip
, old_blk
);
1933 xd_set_err_code(chip
, XD_NO_ERROR
);
1934 XD_CLR_BAD_OLDBLK(xd_card
);
1937 xd_set_l2p_tbl(chip
, zone_no
, log_off
, (u16
)(new_blk
& 0x3FF));
1940 return STATUS_SUCCESS
;
1943 retval
= rtsx_read_register(chip
, XD_DAT
, ®_val
);
1948 if (reg_val
& PROGRAM_ERROR
) {
1949 xd_set_err_code(chip
, XD_PRG_ERROR
);
1950 xd_mark_bad_block(chip
, new_blk
);
1958 #ifdef XD_DELAY_WRITE
1959 int xd_delay_write(struct rtsx_chip
*chip
)
1961 struct xd_info
*xd_card
= &chip
->xd_card
;
1962 struct xd_delay_write_tag
*delay_write
= &xd_card
->delay_write
;
1965 if (delay_write
->delay_write_flag
) {
1966 dev_dbg(rtsx_dev(chip
), "%s\n", __func__
);
1967 retval
= xd_switch_clock(chip
);
1968 if (retval
!= STATUS_SUCCESS
) {
1973 delay_write
->delay_write_flag
= 0;
1974 retval
= xd_finish_write(chip
,
1975 delay_write
->old_phyblock
,
1976 delay_write
->new_phyblock
,
1977 delay_write
->logblock
,
1978 delay_write
->pageoff
);
1979 if (retval
!= STATUS_SUCCESS
) {
1985 return STATUS_SUCCESS
;
1989 int xd_rw(struct scsi_cmnd
*srb
, struct rtsx_chip
*chip
,
1990 u32 start_sector
, u16 sector_cnt
)
1992 struct xd_info
*xd_card
= &chip
->xd_card
;
1993 unsigned int lun
= SCSI_LUN(srb
);
1994 #ifdef XD_DELAY_WRITE
1995 struct xd_delay_write_tag
*delay_write
= &xd_card
->delay_write
;
1997 int retval
, zone_no
;
1998 unsigned int index
= 0, offset
= 0;
1999 u32 log_blk
, old_blk
= 0, new_blk
= 0;
2000 u16 log_off
, total_sec_cnt
= sector_cnt
;
2001 u8 start_page
, end_page
= 0, page_cnt
;
2004 xd_set_err_code(chip
, XD_NO_ERROR
);
2006 xd_card
->cleanup_counter
= 0;
2008 dev_dbg(rtsx_dev(chip
), "%s: scsi_sg_count = %d\n", __func__
,
2009 scsi_sg_count(srb
));
2011 ptr
= (u8
*)scsi_sglist(srb
);
2013 retval
= xd_switch_clock(chip
);
2014 if (retval
!= STATUS_SUCCESS
) {
2019 if (detect_card_cd(chip
, XD_CARD
) != STATUS_SUCCESS
) {
2020 chip
->card_fail
|= XD_CARD
;
2021 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_NOT_PRESENT
);
2026 log_blk
= start_sector
>> xd_card
->block_shift
;
2027 start_page
= (u8
)start_sector
& xd_card
->page_off
;
2028 zone_no
= (int)(log_blk
/ 1000);
2029 log_off
= (u16
)(log_blk
% 1000);
2031 if (xd_card
->zone
[zone_no
].build_flag
== 0) {
2032 retval
= xd_build_l2p_tbl(chip
, zone_no
);
2033 if (retval
!= STATUS_SUCCESS
) {
2034 chip
->card_fail
|= XD_CARD
;
2035 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_NOT_PRESENT
);
2041 if (srb
->sc_data_direction
== DMA_TO_DEVICE
) {
2042 #ifdef XD_DELAY_WRITE
2043 if (delay_write
->delay_write_flag
&&
2044 (delay_write
->logblock
== log_blk
) &&
2045 (start_page
> delay_write
->pageoff
)) {
2046 delay_write
->delay_write_flag
= 0;
2047 if (delay_write
->old_phyblock
!= BLK_NOT_FOUND
) {
2048 retval
= xd_copy_page(chip
,
2049 delay_write
->old_phyblock
,
2050 delay_write
->new_phyblock
,
2051 delay_write
->pageoff
,
2053 if (retval
!= STATUS_SUCCESS
) {
2054 set_sense_type(chip
, lun
,
2055 SENSE_TYPE_MEDIA_WRITE_ERR
);
2060 old_blk
= delay_write
->old_phyblock
;
2061 new_blk
= delay_write
->new_phyblock
;
2062 } else if (delay_write
->delay_write_flag
&&
2063 (delay_write
->logblock
== log_blk
) &&
2064 (start_page
== delay_write
->pageoff
)) {
2065 delay_write
->delay_write_flag
= 0;
2066 old_blk
= delay_write
->old_phyblock
;
2067 new_blk
= delay_write
->new_phyblock
;
2069 retval
= xd_delay_write(chip
);
2070 if (retval
!= STATUS_SUCCESS
) {
2071 set_sense_type(chip
, lun
,
2072 SENSE_TYPE_MEDIA_WRITE_ERR
);
2077 old_blk
= xd_get_l2p_tbl(chip
, zone_no
, log_off
);
2078 new_blk
= xd_get_unused_block(chip
, zone_no
);
2079 if ((old_blk
== BLK_NOT_FOUND
) ||
2080 (new_blk
== BLK_NOT_FOUND
)) {
2081 set_sense_type(chip
, lun
,
2082 SENSE_TYPE_MEDIA_WRITE_ERR
);
2087 retval
= xd_prepare_write(chip
, old_blk
, new_blk
,
2088 log_blk
, start_page
);
2089 if (retval
!= STATUS_SUCCESS
) {
2090 if (detect_card_cd(chip
, XD_CARD
) !=
2092 set_sense_type(chip
, lun
,
2093 SENSE_TYPE_MEDIA_NOT_PRESENT
);
2097 set_sense_type(chip
, lun
,
2098 SENSE_TYPE_MEDIA_WRITE_ERR
);
2102 #ifdef XD_DELAY_WRITE
2106 #ifdef XD_DELAY_WRITE
2107 retval
= xd_delay_write(chip
);
2108 if (retval
!= STATUS_SUCCESS
) {
2109 if (detect_card_cd(chip
, XD_CARD
) != STATUS_SUCCESS
) {
2110 set_sense_type(chip
, lun
,
2111 SENSE_TYPE_MEDIA_NOT_PRESENT
);
2115 set_sense_type(chip
, lun
,
2116 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR
);
2122 old_blk
= xd_get_l2p_tbl(chip
, zone_no
, log_off
);
2123 if (old_blk
== BLK_NOT_FOUND
) {
2124 set_sense_type(chip
, lun
,
2125 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR
);
2131 dev_dbg(rtsx_dev(chip
), "old_blk = 0x%x\n", old_blk
);
2133 while (total_sec_cnt
) {
2134 if (detect_card_cd(chip
, XD_CARD
) != STATUS_SUCCESS
) {
2135 chip
->card_fail
|= XD_CARD
;
2136 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_NOT_PRESENT
);
2141 if ((start_page
+ total_sec_cnt
) > (xd_card
->page_off
+ 1))
2142 end_page
= xd_card
->page_off
+ 1;
2144 end_page
= start_page
+ (u8
)total_sec_cnt
;
2146 page_cnt
= end_page
- start_page
;
2147 if (srb
->sc_data_direction
== DMA_FROM_DEVICE
) {
2148 retval
= xd_read_multiple_pages(chip
, old_blk
, log_blk
,
2149 start_page
, end_page
,
2150 ptr
, &index
, &offset
);
2151 if (retval
!= STATUS_SUCCESS
) {
2152 set_sense_type(chip
, lun
,
2153 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR
);
2158 retval
= xd_write_multiple_pages(chip
, old_blk
,
2160 start_page
, end_page
,
2161 ptr
, &index
, &offset
);
2162 if (retval
!= STATUS_SUCCESS
) {
2163 set_sense_type(chip
, lun
,
2164 SENSE_TYPE_MEDIA_WRITE_ERR
);
2170 total_sec_cnt
-= page_cnt
;
2171 if (scsi_sg_count(srb
) == 0)
2172 ptr
+= page_cnt
* 512;
2174 if (total_sec_cnt
== 0)
2178 zone_no
= (int)(log_blk
/ 1000);
2179 log_off
= (u16
)(log_blk
% 1000);
2181 if (xd_card
->zone
[zone_no
].build_flag
== 0) {
2182 retval
= xd_build_l2p_tbl(chip
, zone_no
);
2183 if (retval
!= STATUS_SUCCESS
) {
2184 chip
->card_fail
|= XD_CARD
;
2185 set_sense_type(chip
, lun
,
2186 SENSE_TYPE_MEDIA_NOT_PRESENT
);
2192 old_blk
= xd_get_l2p_tbl(chip
, zone_no
, log_off
);
2193 if (old_blk
== BLK_NOT_FOUND
) {
2194 if (srb
->sc_data_direction
== DMA_FROM_DEVICE
)
2195 set_sense_type(chip
, lun
,
2196 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR
);
2198 set_sense_type(chip
, lun
,
2199 SENSE_TYPE_MEDIA_WRITE_ERR
);
2205 if (srb
->sc_data_direction
== DMA_TO_DEVICE
) {
2206 new_blk
= xd_get_unused_block(chip
, zone_no
);
2207 if (new_blk
== BLK_NOT_FOUND
) {
2208 set_sense_type(chip
, lun
,
2209 SENSE_TYPE_MEDIA_WRITE_ERR
);
2218 if ((srb
->sc_data_direction
== DMA_TO_DEVICE
) &&
2219 (end_page
!= (xd_card
->page_off
+ 1))) {
2220 #ifdef XD_DELAY_WRITE
2221 delay_write
->delay_write_flag
= 1;
2222 delay_write
->old_phyblock
= old_blk
;
2223 delay_write
->new_phyblock
= new_blk
;
2224 delay_write
->logblock
= log_blk
;
2225 delay_write
->pageoff
= end_page
;
2227 if (detect_card_cd(chip
, XD_CARD
) != STATUS_SUCCESS
) {
2228 chip
->card_fail
|= XD_CARD
;
2229 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_NOT_PRESENT
);
2234 retval
= xd_finish_write(chip
, old_blk
, new_blk
,
2236 if (retval
!= STATUS_SUCCESS
) {
2237 if (detect_card_cd(chip
, XD_CARD
) != STATUS_SUCCESS
) {
2238 set_sense_type(chip
, lun
,
2239 SENSE_TYPE_MEDIA_NOT_PRESENT
);
2243 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_WRITE_ERR
);
2250 scsi_set_resid(srb
, 0);
2252 return STATUS_SUCCESS
;
2255 void xd_free_l2p_tbl(struct rtsx_chip
*chip
)
2257 struct xd_info
*xd_card
= &chip
->xd_card
;
2260 if (xd_card
->zone
) {
2261 for (i
= 0; i
< xd_card
->zone_cnt
; i
++) {
2262 vfree(xd_card
->zone
[i
].l2p_table
);
2263 xd_card
->zone
[i
].l2p_table
= NULL
;
2264 vfree(xd_card
->zone
[i
].free_table
);
2265 xd_card
->zone
[i
].free_table
= NULL
;
2267 vfree(xd_card
->zone
);
2268 xd_card
->zone
= NULL
;
2272 void xd_cleanup_work(struct rtsx_chip
*chip
)
2274 #ifdef XD_DELAY_WRITE
2275 struct xd_info
*xd_card
= &chip
->xd_card
;
2277 if (xd_card
->delay_write
.delay_write_flag
) {
2278 dev_dbg(rtsx_dev(chip
), "xD: delay write\n");
2279 xd_delay_write(chip
);
2280 xd_card
->cleanup_counter
= 0;
2285 int xd_power_off_card3v3(struct rtsx_chip
*chip
)
2289 retval
= disable_card_clock(chip
, XD_CARD
);
2290 if (retval
!= STATUS_SUCCESS
) {
2295 retval
= rtsx_write_register(chip
, CARD_OE
, XD_OUTPUT_EN
, 0);
2301 if (!chip
->ft2_fast_mode
) {
2302 retval
= card_power_off(chip
, XD_CARD
);
2303 if (retval
!= STATUS_SUCCESS
) {
2311 if (chip
->asic_code
) {
2312 retval
= xd_pull_ctl_disable(chip
);
2313 if (retval
!= STATUS_SUCCESS
) {
2318 retval
= rtsx_write_register(chip
, FPGA_PULL_CTL
, 0xFF, 0xDF);
2325 return STATUS_SUCCESS
;
2328 int release_xd_card(struct rtsx_chip
*chip
)
2330 struct xd_info
*xd_card
= &chip
->xd_card
;
2333 chip
->card_ready
&= ~XD_CARD
;
2334 chip
->card_fail
&= ~XD_CARD
;
2335 chip
->card_wp
&= ~XD_CARD
;
2337 xd_card
->delay_write
.delay_write_flag
= 0;
2339 xd_free_l2p_tbl(chip
);
2341 retval
= xd_power_off_card3v3(chip
);
2342 if (retval
!= STATUS_SUCCESS
) {
2347 return STATUS_SUCCESS
;