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
) {
67 return STATUS_SUCCESS
;
70 static int xd_switch_clock(struct rtsx_chip
*chip
)
72 struct xd_info
*xd_card
= &chip
->xd_card
;
75 retval
= select_card(chip
, XD_CARD
);
76 if (retval
!= STATUS_SUCCESS
) {
80 retval
= switch_clock(chip
, xd_card
->xd_clock
);
81 if (retval
!= STATUS_SUCCESS
) {
85 return STATUS_SUCCESS
;
88 static int xd_read_id(struct rtsx_chip
*chip
, u8 id_cmd
, u8
*id_buf
, u8 buf_len
)
95 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_DAT
, 0xFF, id_cmd
);
96 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_TRANSFER
, 0xFF,
97 XD_TRANSFER_START
| XD_READ_ID
);
98 rtsx_add_cmd(chip
, CHECK_REG_CMD
, XD_TRANSFER
, XD_TRANSFER_END
,
101 for (i
= 0; i
< 4; i
++)
102 rtsx_add_cmd(chip
, READ_REG_CMD
, (u16
)(XD_ADDRESS1
+ i
), 0, 0);
104 retval
= rtsx_send_cmd(chip
, XD_CARD
, 20);
109 ptr
= rtsx_get_cmd_data(chip
) + 1;
110 if (id_buf
&& buf_len
) {
113 memcpy(id_buf
, ptr
, buf_len
);
116 return STATUS_SUCCESS
;
119 static void xd_assign_phy_addr(struct rtsx_chip
*chip
, u32 addr
, u8 mode
)
121 struct xd_info
*xd_card
= &chip
->xd_card
;
125 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_ADDRESS0
, 0xFF, 0);
126 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_ADDRESS1
, 0xFF, (u8
)addr
);
127 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_ADDRESS2
,
128 0xFF, (u8
)(addr
>> 8));
129 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_ADDRESS3
,
130 0xFF, (u8
)(addr
>> 16));
131 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_CFG
, 0xFF,
132 xd_card
->addr_cycle
|
138 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_ADDRESS0
, 0xFF, (u8
)addr
);
139 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_ADDRESS1
,
140 0xFF, (u8
)(addr
>> 8));
141 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_ADDRESS2
,
142 0xFF, (u8
)(addr
>> 16));
143 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_CFG
, 0xFF,
144 (xd_card
->addr_cycle
- 1) | XD_CALC_ECC
|
153 static int xd_read_redundant(struct rtsx_chip
*chip
, u32 page_addr
,
154 u8
*buf
, int buf_len
)
160 xd_assign_phy_addr(chip
, page_addr
, XD_RW_ADDR
);
162 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_TRANSFER
,
163 0xFF, XD_TRANSFER_START
| XD_READ_REDUNDANT
);
164 rtsx_add_cmd(chip
, CHECK_REG_CMD
, XD_TRANSFER
,
165 XD_TRANSFER_END
, XD_TRANSFER_END
);
167 for (i
= 0; i
< 6; i
++)
168 rtsx_add_cmd(chip
, READ_REG_CMD
, (u16
)(XD_PAGE_STATUS
+ i
),
170 for (i
= 0; i
< 4; i
++)
171 rtsx_add_cmd(chip
, READ_REG_CMD
, (u16
)(XD_RESERVED0
+ i
),
173 rtsx_add_cmd(chip
, READ_REG_CMD
, XD_PARITY
, 0, 0);
175 retval
= rtsx_send_cmd(chip
, XD_CARD
, 500);
180 if (buf
&& buf_len
) {
181 u8
*ptr
= rtsx_get_cmd_data(chip
) + 1;
185 memcpy(buf
, ptr
, buf_len
);
188 return STATUS_SUCCESS
;
191 static int xd_read_data_from_ppb(struct rtsx_chip
*chip
, int offset
,
192 u8
*buf
, int buf_len
)
196 if (!buf
|| (buf_len
< 0)) {
202 for (i
= 0; i
< buf_len
; i
++)
203 rtsx_add_cmd(chip
, READ_REG_CMD
, PPBUF_BASE2
+ offset
+ i
,
206 retval
= rtsx_send_cmd(chip
, 0, 250);
208 rtsx_clear_xd_error(chip
);
212 memcpy(buf
, rtsx_get_cmd_data(chip
), buf_len
);
214 return STATUS_SUCCESS
;
217 static int xd_read_cis(struct rtsx_chip
*chip
, u32 page_addr
, u8
*buf
,
223 if (!buf
|| (buf_len
< 10)) {
229 xd_assign_phy_addr(chip
, page_addr
, XD_RW_ADDR
);
231 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
,
232 0x01, PINGPONG_BUFFER
);
233 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_PAGE_CNT
, 0xFF, 1);
234 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_CHK_DATA_STATUS
,
235 XD_AUTO_CHK_DATA_STATUS
, XD_AUTO_CHK_DATA_STATUS
);
237 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_TRANSFER
, 0xFF,
238 XD_TRANSFER_START
| XD_READ_PAGES
);
239 rtsx_add_cmd(chip
, CHECK_REG_CMD
, XD_TRANSFER
, XD_TRANSFER_END
,
242 retval
= rtsx_send_cmd(chip
, XD_CARD
, 250);
243 if (retval
== -ETIMEDOUT
) {
244 rtsx_clear_xd_error(chip
);
248 retval
= rtsx_read_register(chip
, XD_PAGE_STATUS
, ®
);
253 rtsx_clear_xd_error(chip
);
257 retval
= rtsx_read_register(chip
, XD_CTL
, ®
);
261 if (!(reg
& XD_ECC1_ERROR
) || !(reg
& XD_ECC1_UNCORRECTABLE
)) {
262 retval
= xd_read_data_from_ppb(chip
, 0, buf
, buf_len
);
263 if (retval
!= STATUS_SUCCESS
) {
266 if (reg
& XD_ECC1_ERROR
) {
267 u8 ecc_bit
, ecc_byte
;
269 retval
= rtsx_read_register(chip
, XD_ECC_BIT1
,
274 retval
= rtsx_read_register(chip
, XD_ECC_BYTE1
,
280 dev_dbg(rtsx_dev(chip
), "ECC_BIT1 = 0x%x, ECC_BYTE1 = 0x%x\n",
282 if (ecc_byte
< buf_len
) {
283 dev_dbg(rtsx_dev(chip
), "Before correct: 0x%x\n",
285 buf
[ecc_byte
] ^= (1 << ecc_bit
);
286 dev_dbg(rtsx_dev(chip
), "After correct: 0x%x\n",
290 } else if (!(reg
& XD_ECC2_ERROR
) || !(reg
& XD_ECC2_UNCORRECTABLE
)) {
291 rtsx_clear_xd_error(chip
);
293 retval
= xd_read_data_from_ppb(chip
, 256, buf
, buf_len
);
294 if (retval
!= STATUS_SUCCESS
) {
297 if (reg
& XD_ECC2_ERROR
) {
298 u8 ecc_bit
, ecc_byte
;
300 retval
= rtsx_read_register(chip
, XD_ECC_BIT2
,
305 retval
= rtsx_read_register(chip
, XD_ECC_BYTE2
,
311 dev_dbg(rtsx_dev(chip
), "ECC_BIT2 = 0x%x, ECC_BYTE2 = 0x%x\n",
313 if (ecc_byte
< buf_len
) {
314 dev_dbg(rtsx_dev(chip
), "Before correct: 0x%x\n",
316 buf
[ecc_byte
] ^= (1 << ecc_bit
);
317 dev_dbg(rtsx_dev(chip
), "After correct: 0x%x\n",
322 rtsx_clear_xd_error(chip
);
326 return STATUS_SUCCESS
;
329 static void xd_fill_pull_ctl_disable(struct rtsx_chip
*chip
)
331 if (CHECK_PID(chip
, 0x5208)) {
332 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL1
, 0xFF,
333 XD_D3_PD
| XD_D2_PD
| XD_D1_PD
| XD_D0_PD
);
334 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL2
, 0xFF,
335 XD_D7_PD
| XD_D6_PD
| XD_D5_PD
| XD_D4_PD
);
336 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL3
, 0xFF,
337 XD_WP_PD
| XD_CE_PD
| XD_CLE_PD
| XD_CD_PU
);
338 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL4
, 0xFF,
339 XD_RDY_PD
| XD_WE_PD
| XD_RE_PD
| XD_ALE_PD
);
340 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL5
, 0xFF,
341 MS_INS_PU
| SD_WP_PD
| SD_CD_PU
| SD_CMD_PD
);
342 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL6
, 0xFF,
343 MS_D5_PD
| MS_D4_PD
);
344 } else if (CHECK_PID(chip
, 0x5288)) {
345 if (CHECK_BARO_PKG(chip
, QFN
)) {
346 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL1
,
348 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL2
,
350 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL3
,
352 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL4
,
358 static void xd_fill_pull_ctl_stage1_barossa(struct rtsx_chip
*chip
)
360 if (CHECK_BARO_PKG(chip
, QFN
)) {
361 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL1
, 0xFF, 0x55);
362 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL2
, 0xFF, 0x55);
363 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL3
, 0xFF, 0x4B);
364 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL4
, 0xFF, 0x55);
368 static void xd_fill_pull_ctl_enable(struct rtsx_chip
*chip
)
370 if (CHECK_PID(chip
, 0x5208)) {
371 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL1
, 0xFF,
372 XD_D3_PD
| XD_D2_PD
| XD_D1_PD
| XD_D0_PD
);
373 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL2
, 0xFF,
374 XD_D7_PD
| XD_D6_PD
| XD_D5_PD
| XD_D4_PD
);
375 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL3
, 0xFF,
376 XD_WP_PD
| XD_CE_PU
| XD_CLE_PD
| XD_CD_PU
);
377 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL4
, 0xFF,
378 XD_RDY_PU
| XD_WE_PU
| XD_RE_PU
| XD_ALE_PD
);
379 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL5
, 0xFF,
380 MS_INS_PU
| SD_WP_PD
| SD_CD_PU
| SD_CMD_PD
);
381 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL6
, 0xFF,
382 MS_D5_PD
| MS_D4_PD
);
383 } else if (CHECK_PID(chip
, 0x5288)) {
384 if (CHECK_BARO_PKG(chip
, QFN
)) {
385 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL1
,
387 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL2
,
389 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL3
,
391 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL4
,
397 static int xd_pull_ctl_disable(struct rtsx_chip
*chip
)
401 if (CHECK_PID(chip
, 0x5208)) {
402 retval
= rtsx_write_register(chip
, CARD_PULL_CTL1
, 0xFF,
410 retval
= rtsx_write_register(chip
, CARD_PULL_CTL2
, 0xFF,
418 retval
= rtsx_write_register(chip
, CARD_PULL_CTL3
, 0xFF,
426 retval
= rtsx_write_register(chip
, CARD_PULL_CTL4
, 0xFF,
434 retval
= rtsx_write_register(chip
, CARD_PULL_CTL5
, 0xFF,
442 retval
= rtsx_write_register(chip
, CARD_PULL_CTL6
, 0xFF,
443 MS_D5_PD
| MS_D4_PD
);
447 } else if (CHECK_PID(chip
, 0x5288)) {
448 if (CHECK_BARO_PKG(chip
, QFN
)) {
449 retval
= rtsx_write_register(chip
, CARD_PULL_CTL1
,
454 retval
= rtsx_write_register(chip
, CARD_PULL_CTL2
,
459 retval
= rtsx_write_register(chip
, CARD_PULL_CTL3
,
464 retval
= rtsx_write_register(chip
, CARD_PULL_CTL4
,
472 return STATUS_SUCCESS
;
475 static int reset_xd(struct rtsx_chip
*chip
)
477 struct xd_info
*xd_card
= &chip
->xd_card
;
479 u8
*ptr
, id_buf
[4], redunt
[11];
481 retval
= select_card(chip
, XD_CARD
);
482 if (retval
!= STATUS_SUCCESS
) {
488 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_CHK_DATA_STATUS
, 0xFF,
490 if (chip
->asic_code
) {
491 if (!CHECK_PID(chip
, 0x5288))
492 xd_fill_pull_ctl_disable(chip
);
494 xd_fill_pull_ctl_stage1_barossa(chip
);
496 rtsx_add_cmd(chip
, WRITE_REG_CMD
, FPGA_PULL_CTL
, 0xFF,
497 (FPGA_XD_PULL_CTL_EN1
& FPGA_XD_PULL_CTL_EN3
) |
501 if (!chip
->ft2_fast_mode
)
502 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_INIT
,
503 XD_NO_AUTO_PWR_OFF
, 0);
505 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_OE
, XD_OUTPUT_EN
, 0);
507 retval
= rtsx_send_cmd(chip
, XD_CARD
, 100);
512 if (!chip
->ft2_fast_mode
) {
513 retval
= card_power_off(chip
, XD_CARD
);
514 if (retval
!= STATUS_SUCCESS
) {
522 if (chip
->asic_code
) {
523 xd_fill_pull_ctl_enable(chip
);
525 rtsx_add_cmd(chip
, WRITE_REG_CMD
, FPGA_PULL_CTL
, 0xFF,
526 (FPGA_XD_PULL_CTL_EN1
&
527 FPGA_XD_PULL_CTL_EN2
) |
531 retval
= rtsx_send_cmd(chip
, XD_CARD
, 100);
536 retval
= card_power_on(chip
, XD_CARD
);
537 if (retval
!= STATUS_SUCCESS
) {
543 if (chip
->ocp_stat
& (SD_OC_NOW
| SD_OC_EVER
)) {
544 dev_dbg(rtsx_dev(chip
), "Over current, OCPSTAT is 0x%x\n",
553 if (chip
->ft2_fast_mode
) {
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
) |
564 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_OE
, XD_OUTPUT_EN
, XD_OUTPUT_EN
);
565 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_CTL
, XD_CE_DISEN
, XD_CE_DISEN
);
567 retval
= rtsx_send_cmd(chip
, XD_CARD
, 100);
572 if (!chip
->ft2_fast_mode
)
575 retval
= xd_set_init_para(chip
);
576 if (retval
!= STATUS_SUCCESS
) {
580 /* Read ID to check if the timing setting is right */
581 for (i
= 0; i
< 4; i
++) {
584 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_DTCTL
, 0xFF,
585 XD_TIME_SETUP_STEP
* 3 +
586 XD_TIME_RW_STEP
* (2 + i
) + XD_TIME_RWN_STEP
* i
);
587 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_CATCTL
, 0xFF,
588 XD_TIME_SETUP_STEP
* 3 +
589 XD_TIME_RW_STEP
* (4 + i
) +
590 XD_TIME_RWN_STEP
* (3 + i
));
592 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_TRANSFER
, 0xFF,
593 XD_TRANSFER_START
| XD_RESET
);
594 rtsx_add_cmd(chip
, CHECK_REG_CMD
, XD_TRANSFER
,
595 XD_TRANSFER_END
, XD_TRANSFER_END
);
597 rtsx_add_cmd(chip
, READ_REG_CMD
, XD_DAT
, 0, 0);
598 rtsx_add_cmd(chip
, READ_REG_CMD
, XD_CTL
, 0, 0);
600 retval
= rtsx_send_cmd(chip
, XD_CARD
, 100);
605 ptr
= rtsx_get_cmd_data(chip
) + 1;
607 dev_dbg(rtsx_dev(chip
), "XD_DAT: 0x%x, XD_CTL: 0x%x\n",
610 if (((ptr
[0] & READY_FLAG
) != READY_STATE
) ||
614 retval
= xd_read_id(chip
, READ_ID
, id_buf
, 4);
615 if (retval
!= STATUS_SUCCESS
) {
619 dev_dbg(rtsx_dev(chip
), "READ_ID: 0x%x 0x%x 0x%x 0x%x\n",
620 id_buf
[0], id_buf
[1], id_buf
[2], id_buf
[3]);
622 xd_card
->device_code
= id_buf
[1];
624 /* Check if the xD card is supported */
625 switch (xd_card
->device_code
) {
628 xd_card
->block_shift
= 4;
629 xd_card
->page_off
= 0x0F;
630 xd_card
->addr_cycle
= 3;
631 xd_card
->zone_cnt
= 1;
632 xd_card
->capacity
= 8000;
636 xd_card
->block_shift
= 4;
637 xd_card
->page_off
= 0x0F;
638 xd_card
->addr_cycle
= 3;
639 xd_card
->zone_cnt
= 1;
640 xd_card
->capacity
= 16000;
643 XD_PAGE_512(xd_card
);
644 xd_card
->addr_cycle
= 3;
645 xd_card
->zone_cnt
= 1;
646 xd_card
->capacity
= 32000;
649 XD_PAGE_512(xd_card
);
650 xd_card
->addr_cycle
= 3;
651 xd_card
->zone_cnt
= 2;
652 xd_card
->capacity
= 64000;
655 XD_PAGE_512(xd_card
);
656 xd_card
->addr_cycle
= 4;
657 xd_card
->zone_cnt
= 4;
658 xd_card
->capacity
= 128000;
661 XD_PAGE_512(xd_card
);
662 xd_card
->addr_cycle
= 4;
663 xd_card
->zone_cnt
= 8;
664 xd_card
->capacity
= 256000;
667 XD_PAGE_512(xd_card
);
668 xd_card
->addr_cycle
= 4;
669 xd_card
->zone_cnt
= 16;
670 xd_card
->capacity
= 512000;
673 XD_PAGE_512(xd_card
);
674 xd_card
->addr_cycle
= 4;
675 xd_card
->zone_cnt
= 32;
676 xd_card
->capacity
= 1024000;
679 XD_PAGE_512(xd_card
);
680 xd_card
->addr_cycle
= 4;
681 xd_card
->zone_cnt
= 64;
682 xd_card
->capacity
= 2048000;
685 XD_PAGE_512(xd_card
);
686 xd_card
->addr_cycle
= 4;
687 xd_card
->zone_cnt
= 128;
688 xd_card
->capacity
= 4096000;
694 /* Confirm timing setting */
695 for (j
= 0; j
< 10; j
++) {
696 retval
= xd_read_id(chip
, READ_ID
, id_buf
, 4);
697 if (retval
!= STATUS_SUCCESS
) {
701 if (id_buf
[1] != xd_card
->device_code
)
710 xd_card
->block_shift
= 0;
711 xd_card
->page_off
= 0;
712 xd_card
->addr_cycle
= 0;
713 xd_card
->capacity
= 0;
718 retval
= xd_read_id(chip
, READ_xD_ID
, id_buf
, 4);
719 if (retval
!= STATUS_SUCCESS
) {
722 dev_dbg(rtsx_dev(chip
), "READ_xD_ID: 0x%x 0x%x 0x%x 0x%x\n",
723 id_buf
[0], id_buf
[1], id_buf
[2], id_buf
[3]);
724 if (id_buf
[2] != XD_ID_CODE
) {
728 /* Search CIS block */
729 for (i
= 0; i
< 24; i
++) {
732 if (detect_card_cd(chip
, XD_CARD
) != STATUS_SUCCESS
) {
736 page_addr
= (u32
)i
<< xd_card
->block_shift
;
738 for (j
= 0; j
< 3; j
++) {
739 retval
= xd_read_redundant(chip
, page_addr
, redunt
, 11);
740 if (retval
== STATUS_SUCCESS
)
746 if (redunt
[BLOCK_STATUS
] != XD_GBLK
)
750 if (redunt
[PAGE_STATUS
] != XD_GPG
) {
751 for (j
= 1; j
<= 8; j
++) {
752 retval
= xd_read_redundant(chip
, page_addr
+ j
,
754 if (retval
== STATUS_SUCCESS
) {
755 if (redunt
[PAGE_STATUS
] == XD_GPG
)
765 if ((redunt
[BLOCK_STATUS
] == XD_GBLK
) &&
766 (redunt
[PARITY
] & XD_BA1_ALL0
)) {
771 retval
= xd_read_cis(chip
, page_addr
, buf
, 10);
772 if (retval
!= STATUS_SUCCESS
) {
776 if ((buf
[0] == 0x01) && (buf
[1] == 0x03) &&
778 (buf
[3] == 0x01) && (buf
[4] == 0xFF) &&
779 (buf
[5] == 0x18) && (buf
[6] == 0x02) &&
780 (buf
[7] == 0xDF) && (buf
[8] == 0x01) &&
782 xd_card
->cis_block
= (u16
)i
;
789 dev_dbg(rtsx_dev(chip
), "CIS block: 0x%x\n", xd_card
->cis_block
);
790 if (xd_card
->cis_block
== 0xFFFF)
793 chip
->capacity
[chip
->card2lun
[XD_CARD
]] = xd_card
->capacity
;
795 return STATUS_SUCCESS
;
798 static int xd_check_data_blank(u8
*redunt
)
802 for (i
= 0; i
< 6; i
++) {
803 if (redunt
[PAGE_STATUS
+ i
] != 0xFF)
807 if ((redunt
[PARITY
] & (XD_ECC1_ALL1
| XD_ECC2_ALL1
))
808 != (XD_ECC1_ALL1
| XD_ECC2_ALL1
))
811 for (i
= 0; i
< 4; i
++) {
812 if (redunt
[RESERVED0
+ i
] != 0xFF)
819 static u16
xd_load_log_block_addr(u8
*redunt
)
823 if (redunt
[PARITY
] & XD_BA1_BA2_EQL
)
824 addr
= ((u16
)redunt
[BLOCK_ADDR1_H
] << 8) |
825 redunt
[BLOCK_ADDR1_L
];
826 else if (redunt
[PARITY
] & XD_BA1_VALID
)
827 addr
= ((u16
)redunt
[BLOCK_ADDR1_H
] << 8) |
828 redunt
[BLOCK_ADDR1_L
];
829 else if (redunt
[PARITY
] & XD_BA2_VALID
)
830 addr
= ((u16
)redunt
[BLOCK_ADDR2_H
] << 8) |
831 redunt
[BLOCK_ADDR2_L
];
836 static int xd_init_l2p_tbl(struct rtsx_chip
*chip
)
838 struct xd_info
*xd_card
= &chip
->xd_card
;
841 dev_dbg(rtsx_dev(chip
), "%s: zone_cnt = %d\n", __func__
,
844 if (xd_card
->zone_cnt
< 1) {
848 size
= xd_card
->zone_cnt
* sizeof(struct zone_entry
);
849 dev_dbg(rtsx_dev(chip
), "Buffer size for l2p table is %d\n", size
);
851 xd_card
->zone
= vmalloc(size
);
852 if (!xd_card
->zone
) {
856 for (i
= 0; i
< xd_card
->zone_cnt
; i
++) {
857 xd_card
->zone
[i
].build_flag
= 0;
858 xd_card
->zone
[i
].l2p_table
= NULL
;
859 xd_card
->zone
[i
].free_table
= NULL
;
860 xd_card
->zone
[i
].get_index
= 0;
861 xd_card
->zone
[i
].set_index
= 0;
862 xd_card
->zone
[i
].unused_blk_cnt
= 0;
865 return STATUS_SUCCESS
;
868 static inline void free_zone(struct zone_entry
*zone
)
873 zone
->build_flag
= 0;
876 zone
->unused_blk_cnt
= 0;
877 vfree(zone
->l2p_table
);
878 zone
->l2p_table
= NULL
;
879 vfree(zone
->free_table
);
880 zone
->free_table
= NULL
;
883 static void xd_set_unused_block(struct rtsx_chip
*chip
, u32 phy_blk
)
885 struct xd_info
*xd_card
= &chip
->xd_card
;
886 struct zone_entry
*zone
;
889 zone_no
= (int)phy_blk
>> 10;
890 if (zone_no
>= xd_card
->zone_cnt
) {
891 dev_dbg(rtsx_dev(chip
), "Set unused block to invalid zone (zone_no = %d, zone_cnt = %d)\n",
892 zone_no
, xd_card
->zone_cnt
);
895 zone
= &xd_card
->zone
[zone_no
];
897 if (!zone
->free_table
) {
898 if (xd_build_l2p_tbl(chip
, zone_no
) != STATUS_SUCCESS
)
902 if ((zone
->set_index
>= XD_FREE_TABLE_CNT
) ||
903 (zone
->set_index
< 0)) {
905 dev_dbg(rtsx_dev(chip
), "Set unused block fail, invalid set_index\n");
909 dev_dbg(rtsx_dev(chip
), "Set unused block to index %d\n",
912 zone
->free_table
[zone
->set_index
++] = (u16
)(phy_blk
& 0x3ff);
913 if (zone
->set_index
>= XD_FREE_TABLE_CNT
)
915 zone
->unused_blk_cnt
++;
918 static u32
xd_get_unused_block(struct rtsx_chip
*chip
, int zone_no
)
920 struct xd_info
*xd_card
= &chip
->xd_card
;
921 struct zone_entry
*zone
;
924 if (zone_no
>= xd_card
->zone_cnt
) {
925 dev_dbg(rtsx_dev(chip
), "Get unused block from invalid zone (zone_no = %d, zone_cnt = %d)\n",
926 zone_no
, xd_card
->zone_cnt
);
927 return BLK_NOT_FOUND
;
929 zone
= &xd_card
->zone
[zone_no
];
931 if ((zone
->unused_blk_cnt
== 0) ||
932 (zone
->set_index
== zone
->get_index
)) {
934 dev_dbg(rtsx_dev(chip
), "Get unused block fail, no unused block available\n");
935 return BLK_NOT_FOUND
;
937 if ((zone
->get_index
>= XD_FREE_TABLE_CNT
) || (zone
->get_index
< 0)) {
939 dev_dbg(rtsx_dev(chip
), "Get unused block fail, invalid get_index\n");
940 return BLK_NOT_FOUND
;
943 dev_dbg(rtsx_dev(chip
), "Get unused block from index %d\n",
946 phy_blk
= zone
->free_table
[zone
->get_index
];
947 zone
->free_table
[zone
->get_index
++] = 0xFFFF;
948 if (zone
->get_index
>= XD_FREE_TABLE_CNT
)
950 zone
->unused_blk_cnt
--;
952 phy_blk
+= ((u32
)(zone_no
) << 10);
956 static void xd_set_l2p_tbl(struct rtsx_chip
*chip
,
957 int zone_no
, u16 log_off
, u16 phy_off
)
959 struct xd_info
*xd_card
= &chip
->xd_card
;
960 struct zone_entry
*zone
;
962 zone
= &xd_card
->zone
[zone_no
];
963 zone
->l2p_table
[log_off
] = phy_off
;
966 static u32
xd_get_l2p_tbl(struct rtsx_chip
*chip
, int zone_no
, u16 log_off
)
968 struct xd_info
*xd_card
= &chip
->xd_card
;
969 struct zone_entry
*zone
;
972 zone
= &xd_card
->zone
[zone_no
];
973 if (zone
->l2p_table
[log_off
] == 0xFFFF) {
977 #ifdef XD_DELAY_WRITE
978 retval
= xd_delay_write(chip
);
979 if (retval
!= STATUS_SUCCESS
) {
980 dev_dbg(rtsx_dev(chip
), "In %s, delay write fail!\n",
982 return BLK_NOT_FOUND
;
986 if (zone
->unused_blk_cnt
<= 0) {
987 dev_dbg(rtsx_dev(chip
), "No unused block!\n");
988 return BLK_NOT_FOUND
;
991 for (i
= 0; i
< zone
->unused_blk_cnt
; i
++) {
992 phy_blk
= xd_get_unused_block(chip
, zone_no
);
993 if (phy_blk
== BLK_NOT_FOUND
) {
994 dev_dbg(rtsx_dev(chip
), "No unused block available!\n");
995 return BLK_NOT_FOUND
;
998 retval
= xd_init_page(chip
, phy_blk
, log_off
,
999 0, xd_card
->page_off
+ 1);
1000 if (retval
== STATUS_SUCCESS
)
1003 if (i
>= zone
->unused_blk_cnt
) {
1004 dev_dbg(rtsx_dev(chip
), "No good unused block available!\n");
1005 return BLK_NOT_FOUND
;
1008 xd_set_l2p_tbl(chip
, zone_no
, log_off
, (u16
)(phy_blk
& 0x3FF));
1012 return (u32
)zone
->l2p_table
[log_off
] + ((u32
)(zone_no
) << 10);
1015 int reset_xd_card(struct rtsx_chip
*chip
)
1017 struct xd_info
*xd_card
= &chip
->xd_card
;
1020 memset(xd_card
, 0, sizeof(struct xd_info
));
1022 xd_card
->block_shift
= 0;
1023 xd_card
->page_off
= 0;
1024 xd_card
->addr_cycle
= 0;
1025 xd_card
->capacity
= 0;
1026 xd_card
->zone_cnt
= 0;
1027 xd_card
->cis_block
= 0xFFFF;
1028 xd_card
->delay_write
.delay_write_flag
= 0;
1030 retval
= enable_card_clock(chip
, XD_CARD
);
1031 if (retval
!= STATUS_SUCCESS
) {
1035 retval
= reset_xd(chip
);
1036 if (retval
!= STATUS_SUCCESS
) {
1040 retval
= xd_init_l2p_tbl(chip
);
1041 if (retval
!= STATUS_SUCCESS
) {
1045 return STATUS_SUCCESS
;
1048 static int xd_mark_bad_block(struct rtsx_chip
*chip
, u32 phy_blk
)
1050 struct xd_info
*xd_card
= &chip
->xd_card
;
1055 dev_dbg(rtsx_dev(chip
), "mark block 0x%x as bad block\n", phy_blk
);
1057 if (phy_blk
== BLK_NOT_FOUND
) {
1061 rtsx_init_cmd(chip
);
1063 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_PAGE_STATUS
, 0xFF, XD_GPG
);
1064 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_BLOCK_STATUS
, 0xFF, XD_LATER_BBLK
);
1065 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_BLOCK_ADDR1_H
, 0xFF, 0xFF);
1066 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_BLOCK_ADDR1_L
, 0xFF, 0xFF);
1067 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_BLOCK_ADDR2_H
, 0xFF, 0xFF);
1068 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_BLOCK_ADDR2_L
, 0xFF, 0xFF);
1069 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_RESERVED0
, 0xFF, 0xFF);
1070 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_RESERVED1
, 0xFF, 0xFF);
1071 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_RESERVED2
, 0xFF, 0xFF);
1072 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_RESERVED3
, 0xFF, 0xFF);
1074 page_addr
= phy_blk
<< xd_card
->block_shift
;
1076 xd_assign_phy_addr(chip
, page_addr
, XD_RW_ADDR
);
1078 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_PAGE_CNT
, 0xFF,
1079 xd_card
->page_off
+ 1);
1081 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_TRANSFER
, 0xFF,
1082 XD_TRANSFER_START
| XD_WRITE_REDUNDANT
);
1083 rtsx_add_cmd(chip
, CHECK_REG_CMD
, XD_TRANSFER
,
1084 XD_TRANSFER_END
, XD_TRANSFER_END
);
1086 retval
= rtsx_send_cmd(chip
, XD_CARD
, 500);
1088 rtsx_clear_xd_error(chip
);
1089 rtsx_read_register(chip
, XD_DAT
, ®
);
1090 if (reg
& PROGRAM_ERROR
)
1091 xd_set_err_code(chip
, XD_PRG_ERROR
);
1093 xd_set_err_code(chip
, XD_TO_ERROR
);
1097 return STATUS_SUCCESS
;
1100 static int xd_init_page(struct rtsx_chip
*chip
, u32 phy_blk
,
1101 u16 logoff
, u8 start_page
, u8 end_page
)
1103 struct xd_info
*xd_card
= &chip
->xd_card
;
1108 dev_dbg(rtsx_dev(chip
), "Init block 0x%x\n", phy_blk
);
1110 if (start_page
> end_page
) {
1113 if (phy_blk
== BLK_NOT_FOUND
) {
1117 rtsx_init_cmd(chip
);
1119 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_PAGE_STATUS
, 0xFF, 0xFF);
1120 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_BLOCK_STATUS
, 0xFF, 0xFF);
1121 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_BLOCK_ADDR1_H
,
1122 0xFF, (u8
)(logoff
>> 8));
1123 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_BLOCK_ADDR1_L
, 0xFF, (u8
)logoff
);
1125 page_addr
= (phy_blk
<< xd_card
->block_shift
) + start_page
;
1127 xd_assign_phy_addr(chip
, page_addr
, XD_RW_ADDR
);
1129 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_CFG
,
1130 XD_BA_TRANSFORM
, XD_BA_TRANSFORM
);
1132 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_PAGE_CNT
,
1133 0xFF, (end_page
- start_page
));
1135 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_TRANSFER
,
1136 0xFF, XD_TRANSFER_START
| XD_WRITE_REDUNDANT
);
1137 rtsx_add_cmd(chip
, CHECK_REG_CMD
, XD_TRANSFER
,
1138 XD_TRANSFER_END
, XD_TRANSFER_END
);
1140 retval
= rtsx_send_cmd(chip
, XD_CARD
, 500);
1142 rtsx_clear_xd_error(chip
);
1143 rtsx_read_register(chip
, XD_DAT
, ®
);
1144 if (reg
& PROGRAM_ERROR
) {
1145 xd_mark_bad_block(chip
, phy_blk
);
1146 xd_set_err_code(chip
, XD_PRG_ERROR
);
1148 xd_set_err_code(chip
, XD_TO_ERROR
);
1153 return STATUS_SUCCESS
;
1156 static int xd_copy_page(struct rtsx_chip
*chip
, u32 old_blk
, u32 new_blk
,
1157 u8 start_page
, u8 end_page
)
1159 struct xd_info
*xd_card
= &chip
->xd_card
;
1160 u32 old_page
, new_page
;
1164 dev_dbg(rtsx_dev(chip
), "Copy page from block 0x%x to block 0x%x\n",
1167 if (start_page
> end_page
) {
1171 if ((old_blk
== BLK_NOT_FOUND
) || (new_blk
== BLK_NOT_FOUND
)) {
1175 old_page
= (old_blk
<< xd_card
->block_shift
) + start_page
;
1176 new_page
= (new_blk
<< xd_card
->block_shift
) + start_page
;
1178 XD_CLR_BAD_NEWBLK(xd_card
);
1180 retval
= rtsx_write_register(chip
, CARD_DATA_SOURCE
, 0x01,
1186 for (i
= start_page
; i
< end_page
; i
++) {
1187 if (detect_card_cd(chip
, XD_CARD
) != STATUS_SUCCESS
) {
1188 rtsx_clear_xd_error(chip
);
1189 xd_set_err_code(chip
, XD_NO_CARD
);
1193 rtsx_init_cmd(chip
);
1195 xd_assign_phy_addr(chip
, old_page
, XD_RW_ADDR
);
1197 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_PAGE_CNT
, 0xFF, 1);
1198 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_CHK_DATA_STATUS
,
1199 XD_AUTO_CHK_DATA_STATUS
, 0);
1200 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_TRANSFER
, 0xFF,
1201 XD_TRANSFER_START
| XD_READ_PAGES
);
1202 rtsx_add_cmd(chip
, CHECK_REG_CMD
, XD_TRANSFER
,
1203 XD_TRANSFER_END
, XD_TRANSFER_END
);
1205 retval
= rtsx_send_cmd(chip
, XD_CARD
, 500);
1207 rtsx_clear_xd_error(chip
);
1209 rtsx_read_register(chip
, XD_CTL
, ®
);
1210 if (reg
& (XD_ECC1_ERROR
| XD_ECC2_ERROR
)) {
1213 if (detect_card_cd(chip
,
1214 XD_CARD
) != STATUS_SUCCESS
) {
1215 xd_set_err_code(chip
, XD_NO_CARD
);
1219 if (((reg
& (XD_ECC1_ERROR
| XD_ECC1_UNCORRECTABLE
)) ==
1220 (XD_ECC1_ERROR
| XD_ECC1_UNCORRECTABLE
)) ||
1221 ((reg
& (XD_ECC2_ERROR
| XD_ECC2_UNCORRECTABLE
)) ==
1222 (XD_ECC2_ERROR
| XD_ECC2_UNCORRECTABLE
))) {
1223 rtsx_write_register(chip
,
1227 rtsx_write_register(chip
,
1231 XD_SET_BAD_OLDBLK(xd_card
);
1232 dev_dbg(rtsx_dev(chip
), "old block 0x%x ecc error\n",
1236 xd_set_err_code(chip
, XD_TO_ERROR
);
1241 if (XD_CHK_BAD_OLDBLK(xd_card
))
1242 rtsx_clear_xd_error(chip
);
1244 rtsx_init_cmd(chip
);
1246 xd_assign_phy_addr(chip
, new_page
, XD_RW_ADDR
);
1247 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_PAGE_CNT
, 0xFF, 1);
1248 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_TRANSFER
, 0xFF,
1249 XD_TRANSFER_START
| XD_WRITE_PAGES
);
1250 rtsx_add_cmd(chip
, CHECK_REG_CMD
, XD_TRANSFER
,
1251 XD_TRANSFER_END
, XD_TRANSFER_END
);
1253 retval
= rtsx_send_cmd(chip
, XD_CARD
, 300);
1255 rtsx_clear_xd_error(chip
);
1257 rtsx_read_register(chip
, XD_DAT
, ®
);
1258 if (reg
& PROGRAM_ERROR
) {
1259 xd_mark_bad_block(chip
, new_blk
);
1260 xd_set_err_code(chip
, XD_PRG_ERROR
);
1261 XD_SET_BAD_NEWBLK(xd_card
);
1263 xd_set_err_code(chip
, XD_TO_ERROR
);
1272 return STATUS_SUCCESS
;
1275 static int xd_reset_cmd(struct rtsx_chip
*chip
)
1280 rtsx_init_cmd(chip
);
1282 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_TRANSFER
,
1283 0xFF, XD_TRANSFER_START
| XD_RESET
);
1284 rtsx_add_cmd(chip
, CHECK_REG_CMD
, XD_TRANSFER
,
1285 XD_TRANSFER_END
, XD_TRANSFER_END
);
1286 rtsx_add_cmd(chip
, READ_REG_CMD
, XD_DAT
, 0, 0);
1287 rtsx_add_cmd(chip
, READ_REG_CMD
, XD_CTL
, 0, 0);
1289 retval
= rtsx_send_cmd(chip
, XD_CARD
, 100);
1294 ptr
= rtsx_get_cmd_data(chip
) + 1;
1295 if (((ptr
[0] & READY_FLAG
) == READY_STATE
) && (ptr
[1] & XD_RDY
))
1296 return STATUS_SUCCESS
;
1301 static int xd_erase_block(struct rtsx_chip
*chip
, u32 phy_blk
)
1303 struct xd_info
*xd_card
= &chip
->xd_card
;
1308 if (phy_blk
== BLK_NOT_FOUND
) {
1312 page_addr
= phy_blk
<< xd_card
->block_shift
;
1314 for (i
= 0; i
< 3; i
++) {
1315 rtsx_init_cmd(chip
);
1317 xd_assign_phy_addr(chip
, page_addr
, XD_ERASE_ADDR
);
1319 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_TRANSFER
, 0xFF,
1320 XD_TRANSFER_START
| XD_ERASE
);
1321 rtsx_add_cmd(chip
, CHECK_REG_CMD
, XD_TRANSFER
,
1322 XD_TRANSFER_END
, XD_TRANSFER_END
);
1323 rtsx_add_cmd(chip
, READ_REG_CMD
, XD_DAT
, 0, 0);
1325 retval
= rtsx_send_cmd(chip
, XD_CARD
, 250);
1327 rtsx_clear_xd_error(chip
);
1328 rtsx_read_register(chip
, XD_DAT
, ®
);
1329 if (reg
& PROGRAM_ERROR
) {
1330 xd_mark_bad_block(chip
, phy_blk
);
1331 xd_set_err_code(chip
, XD_PRG_ERROR
);
1334 xd_set_err_code(chip
, XD_ERASE_FAIL
);
1335 retval
= xd_reset_cmd(chip
);
1336 if (retval
!= STATUS_SUCCESS
) {
1342 ptr
= rtsx_get_cmd_data(chip
) + 1;
1343 if (*ptr
& PROGRAM_ERROR
) {
1344 xd_mark_bad_block(chip
, phy_blk
);
1345 xd_set_err_code(chip
, XD_PRG_ERROR
);
1349 return STATUS_SUCCESS
;
1352 xd_mark_bad_block(chip
, phy_blk
);
1353 xd_set_err_code(chip
, XD_ERASE_FAIL
);
1357 static int xd_build_l2p_tbl(struct rtsx_chip
*chip
, int zone_no
)
1359 struct xd_info
*xd_card
= &chip
->xd_card
;
1360 struct zone_entry
*zone
;
1363 u16 max_logoff
, cur_fst_page_logoff
;
1364 u16 cur_lst_page_logoff
, ent_lst_page_logoff
;
1367 dev_dbg(rtsx_dev(chip
), "%s: %d\n", __func__
, zone_no
);
1369 if (!xd_card
->zone
) {
1370 retval
= xd_init_l2p_tbl(chip
);
1371 if (retval
!= STATUS_SUCCESS
)
1375 if (xd_card
->zone
[zone_no
].build_flag
) {
1376 dev_dbg(rtsx_dev(chip
), "l2p table of zone %d has been built\n",
1378 return STATUS_SUCCESS
;
1381 zone
= &xd_card
->zone
[zone_no
];
1383 if (!zone
->l2p_table
) {
1384 zone
->l2p_table
= vmalloc(2000);
1385 if (!zone
->l2p_table
) {
1389 memset((u8
*)(zone
->l2p_table
), 0xff, 2000);
1391 if (!zone
->free_table
) {
1392 zone
->free_table
= vmalloc(XD_FREE_TABLE_CNT
* 2);
1393 if (!zone
->free_table
) {
1397 memset((u8
*)(zone
->free_table
), 0xff, XD_FREE_TABLE_CNT
* 2);
1400 if (xd_card
->cis_block
== 0xFFFF)
1403 start
= xd_card
->cis_block
+ 1;
1404 if (XD_CHK_4MB(xd_card
)) {
1412 start
= (u32
)(zone_no
) << 10;
1413 end
= (u32
)(zone_no
+ 1) << 10;
1417 dev_dbg(rtsx_dev(chip
), "start block 0x%x, end block 0x%x\n",
1420 zone
->set_index
= 0;
1421 zone
->get_index
= 0;
1422 zone
->unused_blk_cnt
= 0;
1424 for (i
= start
; i
< end
; i
++) {
1425 u32 page_addr
= i
<< xd_card
->block_shift
;
1428 retval
= xd_read_redundant(chip
, page_addr
, redunt
, 11);
1429 if (retval
!= STATUS_SUCCESS
)
1432 if (redunt
[BLOCK_STATUS
] != 0xFF) {
1433 dev_dbg(rtsx_dev(chip
), "bad block\n");
1437 if (xd_check_data_blank(redunt
)) {
1438 dev_dbg(rtsx_dev(chip
), "blank block\n");
1439 xd_set_unused_block(chip
, i
);
1443 cur_fst_page_logoff
= xd_load_log_block_addr(redunt
);
1444 if ((cur_fst_page_logoff
== 0xFFFF) ||
1445 (cur_fst_page_logoff
> max_logoff
)) {
1446 retval
= xd_erase_block(chip
, i
);
1447 if (retval
== STATUS_SUCCESS
)
1448 xd_set_unused_block(chip
, i
);
1452 if ((zone_no
== 0) && (cur_fst_page_logoff
== 0) &&
1453 (redunt
[PAGE_STATUS
] != XD_GPG
))
1454 XD_SET_MBR_FAIL(xd_card
);
1456 if (zone
->l2p_table
[cur_fst_page_logoff
] == 0xFFFF) {
1457 zone
->l2p_table
[cur_fst_page_logoff
] = (u16
)(i
& 0x3FF);
1461 phy_block
= 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
) {
1474 page_addr
= ((phy_block
+ 1) <<
1475 xd_card
->block_shift
) - 1;
1477 for (m
= 0; m
< 3; m
++) {
1478 retval
= xd_read_redundant(chip
, page_addr
,
1480 if (retval
== STATUS_SUCCESS
)
1485 zone
->l2p_table
[cur_fst_page_logoff
] =
1487 retval
= xd_erase_block(chip
, phy_block
);
1488 if (retval
== STATUS_SUCCESS
)
1489 xd_set_unused_block(chip
, phy_block
);
1493 ent_lst_page_logoff
= xd_load_log_block_addr(redunt
);
1494 if (ent_lst_page_logoff
!= cur_fst_page_logoff
) {
1495 zone
->l2p_table
[cur_fst_page_logoff
] =
1497 retval
= xd_erase_block(chip
, phy_block
);
1498 if (retval
== STATUS_SUCCESS
)
1499 xd_set_unused_block(chip
, phy_block
);
1502 retval
= xd_erase_block(chip
, i
);
1503 if (retval
== STATUS_SUCCESS
)
1504 xd_set_unused_block(chip
, i
);
1507 retval
= xd_erase_block(chip
, i
);
1508 if (retval
== STATUS_SUCCESS
)
1509 xd_set_unused_block(chip
, i
);
1513 if (XD_CHK_4MB(xd_card
))
1519 for (start
= 0; start
< end
; start
++) {
1520 if (zone
->l2p_table
[start
] == 0xFFFF)
1524 dev_dbg(rtsx_dev(chip
), "Block count %d, invalid L2P entry %d\n",
1526 dev_dbg(rtsx_dev(chip
), "Total unused block: %d\n",
1527 zone
->unused_blk_cnt
);
1529 if ((zone
->unused_blk_cnt
- i
) < 1)
1530 chip
->card_wp
|= XD_CARD
;
1532 zone
->build_flag
= 1;
1534 return STATUS_SUCCESS
;
1537 vfree(zone
->l2p_table
);
1538 zone
->l2p_table
= NULL
;
1539 vfree(zone
->free_table
);
1540 zone
->free_table
= NULL
;
1545 static int xd_send_cmd(struct rtsx_chip
*chip
, u8 cmd
)
1549 rtsx_init_cmd(chip
);
1551 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_DAT
, 0xFF, cmd
);
1552 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_TRANSFER
, 0xFF,
1553 XD_TRANSFER_START
| XD_SET_CMD
);
1554 rtsx_add_cmd(chip
, CHECK_REG_CMD
, XD_TRANSFER
,
1555 XD_TRANSFER_END
, XD_TRANSFER_END
);
1557 retval
= rtsx_send_cmd(chip
, XD_CARD
, 200);
1562 return STATUS_SUCCESS
;
1565 static int xd_read_multiple_pages(struct rtsx_chip
*chip
, u32 phy_blk
,
1566 u32 log_blk
, u8 start_page
, u8 end_page
,
1567 u8
*buf
, unsigned int *index
,
1568 unsigned int *offset
)
1570 struct xd_info
*xd_card
= &chip
->xd_card
;
1571 u32 page_addr
, new_blk
;
1573 u8 reg_val
, page_cnt
;
1574 int zone_no
, retval
, i
;
1576 if (start_page
> end_page
)
1579 page_cnt
= end_page
- start_page
;
1580 zone_no
= (int)(log_blk
/ 1000);
1581 log_off
= (u16
)(log_blk
% 1000);
1583 if ((phy_blk
& 0x3FF) == 0x3FF) {
1584 for (i
= 0; i
< 256; i
++) {
1585 page_addr
= ((u32
)i
) << xd_card
->block_shift
;
1587 retval
= xd_read_redundant(chip
, page_addr
, NULL
, 0);
1588 if (retval
== STATUS_SUCCESS
)
1591 if (detect_card_cd(chip
, XD_CARD
) != STATUS_SUCCESS
) {
1592 xd_set_err_code(chip
, XD_NO_CARD
);
1598 page_addr
= (phy_blk
<< xd_card
->block_shift
) + start_page
;
1600 rtsx_init_cmd(chip
);
1602 xd_assign_phy_addr(chip
, page_addr
, XD_RW_ADDR
);
1603 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_CFG
, XD_PPB_TO_SIE
, XD_PPB_TO_SIE
);
1604 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
, 0x01, RING_BUFFER
);
1605 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_PAGE_CNT
, 0xFF, page_cnt
);
1606 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_CHK_DATA_STATUS
,
1607 XD_AUTO_CHK_DATA_STATUS
, XD_AUTO_CHK_DATA_STATUS
);
1609 trans_dma_enable(chip
->srb
->sc_data_direction
, chip
,
1610 page_cnt
* 512, DMA_512
);
1612 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_TRANSFER
, 0xFF,
1613 XD_TRANSFER_START
| XD_READ_PAGES
);
1614 rtsx_add_cmd(chip
, CHECK_REG_CMD
, XD_TRANSFER
,
1615 XD_TRANSFER_END
| XD_PPB_EMPTY
,
1616 XD_TRANSFER_END
| XD_PPB_EMPTY
);
1618 rtsx_send_cmd_no_wait(chip
);
1620 retval
= rtsx_transfer_data_partial(chip
, XD_CARD
, buf
, page_cnt
* 512,
1621 scsi_sg_count(chip
->srb
),
1622 index
, offset
, DMA_FROM_DEVICE
,
1625 rtsx_clear_xd_error(chip
);
1627 if (retval
== -ETIMEDOUT
) {
1628 xd_set_err_code(chip
, XD_TO_ERROR
);
1635 return STATUS_SUCCESS
;
1638 retval
= rtsx_read_register(chip
, XD_PAGE_STATUS
, ®_val
);
1643 if (reg_val
!= XD_GPG
)
1644 xd_set_err_code(chip
, XD_PRG_ERROR
);
1646 retval
= rtsx_read_register(chip
, XD_CTL
, ®_val
);
1651 if (((reg_val
& (XD_ECC1_ERROR
| XD_ECC1_UNCORRECTABLE
)) ==
1652 (XD_ECC1_ERROR
| XD_ECC1_UNCORRECTABLE
)) ||
1653 ((reg_val
& (XD_ECC2_ERROR
| XD_ECC2_UNCORRECTABLE
)) ==
1654 (XD_ECC2_ERROR
| XD_ECC2_UNCORRECTABLE
))) {
1657 if (detect_card_cd(chip
, XD_CARD
) != STATUS_SUCCESS
) {
1658 xd_set_err_code(chip
, XD_NO_CARD
);
1662 xd_set_err_code(chip
, XD_ECC_ERROR
);
1664 new_blk
= xd_get_unused_block(chip
, zone_no
);
1665 if (new_blk
== NO_NEW_BLK
) {
1666 XD_CLR_BAD_OLDBLK(xd_card
);
1670 retval
= xd_copy_page(chip
, phy_blk
, new_blk
, 0,
1671 xd_card
->page_off
+ 1);
1672 if (retval
!= STATUS_SUCCESS
) {
1673 if (!XD_CHK_BAD_NEWBLK(xd_card
)) {
1674 retval
= xd_erase_block(chip
, new_blk
);
1675 if (retval
== STATUS_SUCCESS
)
1676 xd_set_unused_block(chip
, new_blk
);
1678 XD_CLR_BAD_NEWBLK(xd_card
);
1680 XD_CLR_BAD_OLDBLK(xd_card
);
1683 xd_set_l2p_tbl(chip
, zone_no
, log_off
, (u16
)(new_blk
& 0x3FF));
1684 xd_erase_block(chip
, phy_blk
);
1685 xd_mark_bad_block(chip
, phy_blk
);
1686 XD_CLR_BAD_OLDBLK(xd_card
);
1693 static int xd_finish_write(struct rtsx_chip
*chip
,
1694 u32 old_blk
, u32 new_blk
, u32 log_blk
, u8 page_off
)
1696 struct xd_info
*xd_card
= &chip
->xd_card
;
1697 int retval
, zone_no
;
1700 dev_dbg(rtsx_dev(chip
), "%s ", __func__
);
1701 dev_dbg(rtsx_dev(chip
), "old_blk = 0x%x, ", old_blk
);
1702 dev_dbg(rtsx_dev(chip
), "new_blk = 0x%x, ", new_blk
);
1703 dev_dbg(rtsx_dev(chip
), "log_blk = 0x%x\n", log_blk
);
1705 if (page_off
> xd_card
->page_off
) {
1709 zone_no
= (int)(log_blk
/ 1000);
1710 log_off
= (u16
)(log_blk
% 1000);
1712 if (old_blk
== BLK_NOT_FOUND
) {
1713 retval
= xd_init_page(chip
, new_blk
, log_off
,
1714 page_off
, xd_card
->page_off
+ 1);
1715 if (retval
!= STATUS_SUCCESS
) {
1716 retval
= xd_erase_block(chip
, new_blk
);
1717 if (retval
== STATUS_SUCCESS
)
1718 xd_set_unused_block(chip
, new_blk
);
1722 retval
= xd_copy_page(chip
, old_blk
, new_blk
,
1723 page_off
, xd_card
->page_off
+ 1);
1724 if (retval
!= STATUS_SUCCESS
) {
1725 if (!XD_CHK_BAD_NEWBLK(xd_card
)) {
1726 retval
= xd_erase_block(chip
, new_blk
);
1727 if (retval
== STATUS_SUCCESS
)
1728 xd_set_unused_block(chip
, new_blk
);
1730 XD_CLR_BAD_NEWBLK(xd_card
);
1734 retval
= xd_erase_block(chip
, old_blk
);
1735 if (retval
== STATUS_SUCCESS
) {
1736 if (XD_CHK_BAD_OLDBLK(xd_card
)) {
1737 xd_mark_bad_block(chip
, old_blk
);
1738 XD_CLR_BAD_OLDBLK(xd_card
);
1740 xd_set_unused_block(chip
, old_blk
);
1743 xd_set_err_code(chip
, XD_NO_ERROR
);
1744 XD_CLR_BAD_OLDBLK(xd_card
);
1748 xd_set_l2p_tbl(chip
, zone_no
, log_off
, (u16
)(new_blk
& 0x3FF));
1750 return STATUS_SUCCESS
;
1753 static int xd_prepare_write(struct rtsx_chip
*chip
,
1754 u32 old_blk
, u32 new_blk
, u32 log_blk
, u8 page_off
)
1758 dev_dbg(rtsx_dev(chip
), "%s, old_blk = 0x%x, new_blk = 0x%x, log_blk = 0x%x, page_off = %d\n",
1759 __func__
, old_blk
, new_blk
, log_blk
, (int)page_off
);
1762 retval
= xd_copy_page(chip
, old_blk
, new_blk
, 0, page_off
);
1763 if (retval
!= STATUS_SUCCESS
) {
1768 return STATUS_SUCCESS
;
1771 static int xd_write_multiple_pages(struct rtsx_chip
*chip
, u32 old_blk
,
1772 u32 new_blk
, u32 log_blk
, u8 start_page
,
1773 u8 end_page
, u8
*buf
, unsigned int *index
,
1774 unsigned int *offset
)
1776 struct xd_info
*xd_card
= &chip
->xd_card
;
1778 int zone_no
, retval
;
1780 u8 page_cnt
, reg_val
;
1782 dev_dbg(rtsx_dev(chip
), "%s, old_blk = 0x%x, new_blk = 0x%x, log_blk = 0x%x\n",
1783 __func__
, old_blk
, new_blk
, log_blk
);
1785 if (start_page
> end_page
)
1788 page_cnt
= end_page
- start_page
;
1789 zone_no
= (int)(log_blk
/ 1000);
1790 log_off
= (u16
)(log_blk
% 1000);
1792 page_addr
= (new_blk
<< xd_card
->block_shift
) + start_page
;
1794 retval
= xd_send_cmd(chip
, READ1_1
);
1795 if (retval
!= STATUS_SUCCESS
)
1798 rtsx_init_cmd(chip
);
1800 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_BLOCK_ADDR1_H
,
1801 0xFF, (u8
)(log_off
>> 8));
1802 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_BLOCK_ADDR1_L
, 0xFF, (u8
)log_off
);
1803 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_BLOCK_STATUS
, 0xFF, XD_GBLK
);
1804 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_PAGE_STATUS
, 0xFF, XD_GPG
);
1806 xd_assign_phy_addr(chip
, page_addr
, XD_RW_ADDR
);
1808 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_CFG
, XD_BA_TRANSFORM
,
1810 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_PAGE_CNT
, 0xFF, page_cnt
);
1811 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
, 0x01, RING_BUFFER
);
1813 trans_dma_enable(chip
->srb
->sc_data_direction
, chip
,
1814 page_cnt
* 512, DMA_512
);
1816 rtsx_add_cmd(chip
, WRITE_REG_CMD
, XD_TRANSFER
,
1817 0xFF, XD_TRANSFER_START
| XD_WRITE_PAGES
);
1818 rtsx_add_cmd(chip
, CHECK_REG_CMD
, XD_TRANSFER
,
1819 XD_TRANSFER_END
, XD_TRANSFER_END
);
1821 rtsx_send_cmd_no_wait(chip
);
1823 retval
= rtsx_transfer_data_partial(chip
, XD_CARD
, buf
, page_cnt
* 512,
1824 scsi_sg_count(chip
->srb
),
1825 index
, offset
, DMA_TO_DEVICE
, chip
->xd_timeout
);
1827 rtsx_clear_xd_error(chip
);
1829 if (retval
== -ETIMEDOUT
) {
1830 xd_set_err_code(chip
, XD_TO_ERROR
);
1837 if (end_page
== (xd_card
->page_off
+ 1)) {
1838 xd_card
->delay_write
.delay_write_flag
= 0;
1840 if (old_blk
!= BLK_NOT_FOUND
) {
1841 retval
= xd_erase_block(chip
, old_blk
);
1842 if (retval
== STATUS_SUCCESS
) {
1843 if (XD_CHK_BAD_OLDBLK(xd_card
)) {
1844 xd_mark_bad_block(chip
, old_blk
);
1845 XD_CLR_BAD_OLDBLK(xd_card
);
1847 xd_set_unused_block(chip
, old_blk
);
1850 xd_set_err_code(chip
, XD_NO_ERROR
);
1851 XD_CLR_BAD_OLDBLK(xd_card
);
1854 xd_set_l2p_tbl(chip
, zone_no
, log_off
, (u16
)(new_blk
& 0x3FF));
1857 return STATUS_SUCCESS
;
1860 retval
= rtsx_read_register(chip
, XD_DAT
, ®_val
);
1864 if (reg_val
& PROGRAM_ERROR
) {
1865 xd_set_err_code(chip
, XD_PRG_ERROR
);
1866 xd_mark_bad_block(chip
, new_blk
);
1873 #ifdef XD_DELAY_WRITE
1874 int xd_delay_write(struct rtsx_chip
*chip
)
1876 struct xd_info
*xd_card
= &chip
->xd_card
;
1877 struct xd_delay_write_tag
*delay_write
= &xd_card
->delay_write
;
1880 if (delay_write
->delay_write_flag
) {
1881 dev_dbg(rtsx_dev(chip
), "%s\n", __func__
);
1882 retval
= xd_switch_clock(chip
);
1883 if (retval
!= STATUS_SUCCESS
) {
1887 delay_write
->delay_write_flag
= 0;
1888 retval
= xd_finish_write(chip
,
1889 delay_write
->old_phyblock
,
1890 delay_write
->new_phyblock
,
1891 delay_write
->logblock
,
1892 delay_write
->pageoff
);
1893 if (retval
!= STATUS_SUCCESS
) {
1898 return STATUS_SUCCESS
;
1902 int xd_rw(struct scsi_cmnd
*srb
, struct rtsx_chip
*chip
,
1903 u32 start_sector
, u16 sector_cnt
)
1905 struct xd_info
*xd_card
= &chip
->xd_card
;
1906 unsigned int lun
= SCSI_LUN(srb
);
1907 #ifdef XD_DELAY_WRITE
1908 struct xd_delay_write_tag
*delay_write
= &xd_card
->delay_write
;
1910 int retval
, zone_no
;
1911 unsigned int index
= 0, offset
= 0;
1912 u32 log_blk
, old_blk
= 0, new_blk
= 0;
1913 u16 log_off
, total_sec_cnt
= sector_cnt
;
1914 u8 start_page
, end_page
= 0, page_cnt
;
1917 xd_set_err_code(chip
, XD_NO_ERROR
);
1919 xd_card
->cleanup_counter
= 0;
1921 dev_dbg(rtsx_dev(chip
), "%s: scsi_sg_count = %d\n", __func__
,
1922 scsi_sg_count(srb
));
1924 ptr
= (u8
*)scsi_sglist(srb
);
1926 retval
= xd_switch_clock(chip
);
1927 if (retval
!= STATUS_SUCCESS
) {
1931 if (detect_card_cd(chip
, XD_CARD
) != STATUS_SUCCESS
) {
1932 chip
->card_fail
|= XD_CARD
;
1933 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_NOT_PRESENT
);
1937 log_blk
= start_sector
>> xd_card
->block_shift
;
1938 start_page
= (u8
)start_sector
& xd_card
->page_off
;
1939 zone_no
= (int)(log_blk
/ 1000);
1940 log_off
= (u16
)(log_blk
% 1000);
1942 if (xd_card
->zone
[zone_no
].build_flag
== 0) {
1943 retval
= xd_build_l2p_tbl(chip
, zone_no
);
1944 if (retval
!= STATUS_SUCCESS
) {
1945 chip
->card_fail
|= XD_CARD
;
1946 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_NOT_PRESENT
);
1951 if (srb
->sc_data_direction
== DMA_TO_DEVICE
) {
1952 #ifdef XD_DELAY_WRITE
1953 if (delay_write
->delay_write_flag
&&
1954 (delay_write
->logblock
== log_blk
) &&
1955 (start_page
> delay_write
->pageoff
)) {
1956 delay_write
->delay_write_flag
= 0;
1957 if (delay_write
->old_phyblock
!= BLK_NOT_FOUND
) {
1958 retval
= xd_copy_page(chip
,
1959 delay_write
->old_phyblock
,
1960 delay_write
->new_phyblock
,
1961 delay_write
->pageoff
,
1963 if (retval
!= STATUS_SUCCESS
) {
1964 set_sense_type(chip
, lun
,
1965 SENSE_TYPE_MEDIA_WRITE_ERR
);
1969 old_blk
= delay_write
->old_phyblock
;
1970 new_blk
= delay_write
->new_phyblock
;
1971 } else if (delay_write
->delay_write_flag
&&
1972 (delay_write
->logblock
== log_blk
) &&
1973 (start_page
== delay_write
->pageoff
)) {
1974 delay_write
->delay_write_flag
= 0;
1975 old_blk
= delay_write
->old_phyblock
;
1976 new_blk
= delay_write
->new_phyblock
;
1978 retval
= xd_delay_write(chip
);
1979 if (retval
!= STATUS_SUCCESS
) {
1980 set_sense_type(chip
, lun
,
1981 SENSE_TYPE_MEDIA_WRITE_ERR
);
1985 old_blk
= xd_get_l2p_tbl(chip
, zone_no
, log_off
);
1986 new_blk
= xd_get_unused_block(chip
, zone_no
);
1987 if ((old_blk
== BLK_NOT_FOUND
) ||
1988 (new_blk
== BLK_NOT_FOUND
)) {
1989 set_sense_type(chip
, lun
,
1990 SENSE_TYPE_MEDIA_WRITE_ERR
);
1994 retval
= xd_prepare_write(chip
, old_blk
, new_blk
,
1995 log_blk
, start_page
);
1996 if (retval
!= STATUS_SUCCESS
) {
1997 if (detect_card_cd(chip
, XD_CARD
) !=
1999 set_sense_type(chip
, lun
,
2000 SENSE_TYPE_MEDIA_NOT_PRESENT
);
2003 set_sense_type(chip
, lun
,
2004 SENSE_TYPE_MEDIA_WRITE_ERR
);
2007 #ifdef XD_DELAY_WRITE
2011 #ifdef XD_DELAY_WRITE
2012 retval
= xd_delay_write(chip
);
2013 if (retval
!= STATUS_SUCCESS
) {
2014 if (detect_card_cd(chip
, XD_CARD
) != STATUS_SUCCESS
) {
2015 set_sense_type(chip
, lun
,
2016 SENSE_TYPE_MEDIA_NOT_PRESENT
);
2019 set_sense_type(chip
, lun
,
2020 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR
);
2025 old_blk
= xd_get_l2p_tbl(chip
, zone_no
, log_off
);
2026 if (old_blk
== BLK_NOT_FOUND
) {
2027 set_sense_type(chip
, lun
,
2028 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR
);
2033 dev_dbg(rtsx_dev(chip
), "old_blk = 0x%x\n", old_blk
);
2035 while (total_sec_cnt
) {
2036 if (detect_card_cd(chip
, XD_CARD
) != STATUS_SUCCESS
) {
2037 chip
->card_fail
|= XD_CARD
;
2038 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_NOT_PRESENT
);
2042 if ((start_page
+ total_sec_cnt
) > (xd_card
->page_off
+ 1))
2043 end_page
= xd_card
->page_off
+ 1;
2045 end_page
= start_page
+ (u8
)total_sec_cnt
;
2047 page_cnt
= end_page
- start_page
;
2048 if (srb
->sc_data_direction
== DMA_FROM_DEVICE
) {
2049 retval
= xd_read_multiple_pages(chip
, old_blk
, log_blk
,
2050 start_page
, end_page
,
2051 ptr
, &index
, &offset
);
2052 if (retval
!= STATUS_SUCCESS
) {
2053 set_sense_type(chip
, lun
,
2054 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR
);
2058 retval
= xd_write_multiple_pages(chip
, old_blk
,
2060 start_page
, end_page
,
2061 ptr
, &index
, &offset
);
2062 if (retval
!= STATUS_SUCCESS
) {
2063 set_sense_type(chip
, lun
,
2064 SENSE_TYPE_MEDIA_WRITE_ERR
);
2069 total_sec_cnt
-= page_cnt
;
2070 if (scsi_sg_count(srb
) == 0)
2071 ptr
+= page_cnt
* 512;
2073 if (total_sec_cnt
== 0)
2077 zone_no
= (int)(log_blk
/ 1000);
2078 log_off
= (u16
)(log_blk
% 1000);
2080 if (xd_card
->zone
[zone_no
].build_flag
== 0) {
2081 retval
= xd_build_l2p_tbl(chip
, zone_no
);
2082 if (retval
!= STATUS_SUCCESS
) {
2083 chip
->card_fail
|= XD_CARD
;
2084 set_sense_type(chip
, lun
,
2085 SENSE_TYPE_MEDIA_NOT_PRESENT
);
2090 old_blk
= xd_get_l2p_tbl(chip
, zone_no
, log_off
);
2091 if (old_blk
== BLK_NOT_FOUND
) {
2092 if (srb
->sc_data_direction
== DMA_FROM_DEVICE
)
2093 set_sense_type(chip
, lun
,
2094 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR
);
2096 set_sense_type(chip
, lun
,
2097 SENSE_TYPE_MEDIA_WRITE_ERR
);
2102 if (srb
->sc_data_direction
== DMA_TO_DEVICE
) {
2103 new_blk
= xd_get_unused_block(chip
, zone_no
);
2104 if (new_blk
== BLK_NOT_FOUND
) {
2105 set_sense_type(chip
, lun
,
2106 SENSE_TYPE_MEDIA_WRITE_ERR
);
2114 if ((srb
->sc_data_direction
== DMA_TO_DEVICE
) &&
2115 (end_page
!= (xd_card
->page_off
+ 1))) {
2116 #ifdef XD_DELAY_WRITE
2117 delay_write
->delay_write_flag
= 1;
2118 delay_write
->old_phyblock
= old_blk
;
2119 delay_write
->new_phyblock
= new_blk
;
2120 delay_write
->logblock
= log_blk
;
2121 delay_write
->pageoff
= end_page
;
2123 if (detect_card_cd(chip
, XD_CARD
) != STATUS_SUCCESS
) {
2124 chip
->card_fail
|= XD_CARD
;
2125 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_NOT_PRESENT
);
2129 retval
= xd_finish_write(chip
, old_blk
, new_blk
,
2131 if (retval
!= STATUS_SUCCESS
) {
2132 if (detect_card_cd(chip
, XD_CARD
) != STATUS_SUCCESS
) {
2133 set_sense_type(chip
, lun
,
2134 SENSE_TYPE_MEDIA_NOT_PRESENT
);
2137 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_WRITE_ERR
);
2143 scsi_set_resid(srb
, 0);
2145 return STATUS_SUCCESS
;
2148 void xd_free_l2p_tbl(struct rtsx_chip
*chip
)
2150 struct xd_info
*xd_card
= &chip
->xd_card
;
2153 if (xd_card
->zone
) {
2154 for (i
= 0; i
< xd_card
->zone_cnt
; i
++) {
2155 vfree(xd_card
->zone
[i
].l2p_table
);
2156 xd_card
->zone
[i
].l2p_table
= NULL
;
2157 vfree(xd_card
->zone
[i
].free_table
);
2158 xd_card
->zone
[i
].free_table
= NULL
;
2160 vfree(xd_card
->zone
);
2161 xd_card
->zone
= NULL
;
2165 void xd_cleanup_work(struct rtsx_chip
*chip
)
2167 #ifdef XD_DELAY_WRITE
2168 struct xd_info
*xd_card
= &chip
->xd_card
;
2170 if (xd_card
->delay_write
.delay_write_flag
) {
2171 dev_dbg(rtsx_dev(chip
), "xD: delay write\n");
2172 xd_delay_write(chip
);
2173 xd_card
->cleanup_counter
= 0;
2178 int xd_power_off_card3v3(struct rtsx_chip
*chip
)
2182 retval
= disable_card_clock(chip
, XD_CARD
);
2183 if (retval
!= STATUS_SUCCESS
) {
2187 retval
= rtsx_write_register(chip
, CARD_OE
, XD_OUTPUT_EN
, 0);
2192 if (!chip
->ft2_fast_mode
) {
2193 retval
= card_power_off(chip
, XD_CARD
);
2194 if (retval
!= STATUS_SUCCESS
) {
2201 if (chip
->asic_code
) {
2202 retval
= xd_pull_ctl_disable(chip
);
2203 if (retval
!= STATUS_SUCCESS
) {
2207 retval
= rtsx_write_register(chip
, FPGA_PULL_CTL
, 0xFF, 0xDF);
2213 return STATUS_SUCCESS
;
2216 int release_xd_card(struct rtsx_chip
*chip
)
2218 struct xd_info
*xd_card
= &chip
->xd_card
;
2221 chip
->card_ready
&= ~XD_CARD
;
2222 chip
->card_fail
&= ~XD_CARD
;
2223 chip
->card_wp
&= ~XD_CARD
;
2225 xd_card
->delay_write
.delay_write_flag
= 0;
2227 xd_free_l2p_tbl(chip
);
2229 retval
= xd_power_off_card3v3(chip
);
2230 if (retval
!= STATUS_SUCCESS
) {
2234 return STATUS_SUCCESS
;