25 #include <linux/sched.h>
34 static int xd_build_l2p_tbl(
struct rtsx_chip *
chip,
int zone_no);
48 return (xd_card->
err_code == err_code);
51 static int xd_set_init_para(
struct rtsx_chip *chip)
61 retval = switch_clock(chip, xd_card->
xd_clock);
68 static int xd_switch_clock(
struct rtsx_chip *chip)
77 retval = switch_clock(chip, xd_card->
xd_clock);
84 static int xd_read_id(
struct rtsx_chip *chip,
u8 id_cmd,
u8 *id_buf,
u8 buf_len)
95 for (i = 0; i < 4; i++)
103 if (id_buf && buf_len) {
106 memcpy(id_buf, ptr, buf_len);
139 static int xd_read_redundant(
struct rtsx_chip *chip,
u32 page_addr,
u8 *
buf,
int buf_len)
145 xd_assign_phy_addr(chip, page_addr,
XD_RW_ADDR);
150 for (i = 0; i < 6; i++)
152 for (i = 0; i < 4; i++)
160 if (buf && buf_len) {
165 memcpy(buf, ptr, buf_len);
171 static int xd_read_data_from_ppb(
struct rtsx_chip *chip,
int offset,
u8 *buf,
int buf_len)
175 if (!buf || (buf_len < 0))
180 for (i = 0; i < buf_len; i++)
185 rtsx_clear_xd_error(chip);
194 static int xd_read_cis(
struct rtsx_chip *chip,
u32 page_addr,
u8 *buf,
int buf_len)
199 if (!buf || (buf_len < 10))
204 xd_assign_phy_addr(chip, page_addr,
XD_RW_ADDR);
215 rtsx_clear_xd_error(chip);
221 rtsx_clear_xd_error(chip);
227 retval = xd_read_data_from_ppb(chip, 0, buf, buf_len);
230 if (reg & XD_ECC1_ERROR) {
231 u8 ecc_bit, ecc_byte;
236 RTSX_DEBUGP(
"ECC_BIT1 = 0x%x, ECC_BYTE1 = 0x%x\n", ecc_bit, ecc_byte);
237 if (ecc_byte < buf_len) {
238 RTSX_DEBUGP(
"Before correct: 0x%x\n", buf[ecc_byte]);
239 buf[ecc_byte] ^= (1 << ecc_bit);
240 RTSX_DEBUGP(
"After correct: 0x%x\n", buf[ecc_byte]);
244 rtsx_clear_xd_error(chip);
246 retval = xd_read_data_from_ppb(chip, 256, buf, buf_len);
249 if (reg & XD_ECC2_ERROR) {
250 u8 ecc_bit, ecc_byte;
255 RTSX_DEBUGP(
"ECC_BIT2 = 0x%x, ECC_BYTE2 = 0x%x\n", ecc_bit, ecc_byte);
256 if (ecc_byte < buf_len) {
257 RTSX_DEBUGP(
"Before correct: 0x%x\n", buf[ecc_byte]);
258 buf[ecc_byte] ^= (1 << ecc_bit);
259 RTSX_DEBUGP(
"After correct: 0x%x\n", buf[ecc_byte]);
263 rtsx_clear_xd_error(chip);
270 static void xd_fill_pull_ctl_disable(
struct rtsx_chip *chip)
301 static void xd_fill_pull_ctl_stage1_barossa(
struct rtsx_chip *chip)
311 static void xd_fill_pull_ctl_enable(
struct rtsx_chip *chip)
342 static int xd_pull_ctl_disable(
struct rtsx_chip *chip)
375 static void xd_clear_dma_buffer(
struct rtsx_chip *chip)
443 static int reset_xd(
struct rtsx_chip *chip)
447 u8 *
ptr, id_buf[4], redunt[11];
458 xd_fill_pull_ctl_disable(chip);
460 xd_fill_pull_ctl_stage1_barossa(chip);
488 xd_fill_pull_ctl_enable(chip);
515 xd_fill_pull_ctl_enable(chip);
532 retval = xd_set_init_para(chip);
537 for (i = 0; i < 4; i++) {
557 RTSX_DEBUGP(
"XD_DAT: 0x%x, XD_CTL: 0x%x\n", ptr[0], ptr[1]);
562 retval = xd_read_id(chip,
READ_ID, id_buf, 4);
567 id_buf[0], id_buf[1], id_buf[2], id_buf[3]);
642 for (j = 0; j < 10; j++) {
643 retval = xd_read_id(chip,
READ_ID, id_buf, 4);
664 retval = xd_read_id(chip,
READ_xD_ID, id_buf, 4);
668 id_buf[0], id_buf[1], id_buf[2], id_buf[3]);
673 for (i = 0; i < 24; i++) {
681 for (j = 0; j < 3; j++) {
682 retval = xd_read_redundant(chip, page_addr, redunt, 11);
694 for (j = 1; j <= 8; j++) {
695 retval = xd_read_redundant(chip, page_addr + j, redunt, 11);
712 retval = xd_read_cis(chip, page_addr, buf, 10);
716 if ((buf[0] == 0x01) && (buf[1] == 0x03) && (buf[2] == 0xD9)
717 && (buf[3] == 0x01) && (buf[4] == 0xFF)
718 && (buf[5] == 0x18) && (buf[6] == 0x02)
719 && (buf[7] == 0xDF) && (buf[8] == 0x01)
720 && (buf[9] == 0x20)) {
737 static int xd_check_data_blank(
u8 *redunt)
741 for (i = 0; i < 6; i++) {
750 for (i = 0; i < 4; i++) {
758 static u16 xd_load_log_block_addr(
u8 *redunt)
772 static int xd_init_l2p_tbl(
struct rtsx_chip *chip)
783 RTSX_DEBUGP(
"Buffer size for l2p table is %d\n", size);
789 for (i = 0; i < xd_card->
zone_cnt; i++) {
790 xd_card->
zone[
i].build_flag = 0;
793 xd_card->
zone[
i].get_index = 0;
794 xd_card->
zone[
i].set_index = 0;
795 xd_card->
zone[
i].unused_blk_cnt = 0;
822 static void xd_set_unused_block(
struct rtsx_chip *chip,
u32 phy_blk)
828 zone_no = (
int)phy_blk >> 10;
830 RTSX_DEBUGP(
"Set unused block to invalid zone (zone_no = %d, zone_cnt = %d)\n",
834 zone = &(xd_card->
zone[zone_no]);
844 RTSX_DEBUGP(
"Set unused block fail, invalid set_index\n");
856 static u32 xd_get_unused_block(
struct rtsx_chip *chip,
int zone_no)
863 RTSX_DEBUGP(
"Get unused block from invalid zone (zone_no = %d, zone_cnt = %d)\n",
867 zone = &(xd_card->
zone[zone_no]);
871 RTSX_DEBUGP(
"Get unused block fail, no unused block available\n");
876 RTSX_DEBUGP(
"Get unused block fail, invalid get_index\n");
888 phy_blk += ((
u32)(zone_no) << 10);
892 static void xd_set_l2p_tbl(
struct rtsx_chip *chip,
int zone_no,
u16 log_off,
u16 phy_off)
897 zone = &(xd_card->
zone[zone_no]);
901 static u32 xd_get_l2p_tbl(
struct rtsx_chip *chip,
int zone_no,
u16 log_off)
907 zone = &(xd_card->
zone[zone_no]);
908 if (zone->
l2p_table[log_off] == 0xFFFF) {
912 #ifdef XD_DELAY_WRITE
915 RTSX_DEBUGP(
"In xd_get_l2p_tbl, delay write fail!\n");
926 phy_blk = xd_get_unused_block(chip, zone_no);
932 retval = xd_init_page(chip, phy_blk, log_off, 0, xd_card->
page_off + 1);
941 xd_set_l2p_tbl(chip, zone_no, log_off, (
u16)(phy_blk & 0x3FF));
967 retval = reset_xd(chip);
971 retval = xd_init_l2p_tbl(chip);
978 static int xd_mark_bad_block(
struct rtsx_chip *chip,
u32 phy_blk)
985 RTSX_DEBUGP(
"mark block 0x%x as bad block\n", phy_blk);
1005 xd_assign_phy_addr(chip, page_addr,
XD_RW_ADDR);
1014 rtsx_clear_xd_error(chip);
1026 static int xd_init_page(
struct rtsx_chip *chip,
u32 phy_blk,
u16 logoff,
u8 start_page,
u8 end_page)
1035 if (start_page > end_page)
1047 page_addr = (phy_blk << xd_card->
block_shift) + start_page;
1049 xd_assign_phy_addr(chip, page_addr,
XD_RW_ADDR);
1060 rtsx_clear_xd_error(chip);
1063 xd_mark_bad_block(chip, phy_blk);
1074 static int xd_copy_page(
struct rtsx_chip *chip,
u32 old_blk,
u32 new_blk,
u8 start_page,
u8 end_page)
1077 u32 old_page, new_page;
1081 RTSX_DEBUGP(
"Copy page from block 0x%x to block 0x%x\n", old_blk, new_blk);
1083 if (start_page > end_page)
1089 old_page = (old_blk << xd_card->
block_shift) + start_page;
1090 new_page = (new_blk << xd_card->
block_shift) + start_page;
1096 for (i = start_page; i < end_page; i++) {
1098 rtsx_clear_xd_error(chip);
1105 xd_assign_phy_addr(chip, old_page,
XD_RW_ADDR);
1114 rtsx_clear_xd_error(chip);
1117 if (reg & (XD_ECC1_ERROR | XD_ECC2_ERROR)) {
1125 if (((reg & (XD_ECC1_ERROR | XD_ECC1_UNCORRECTABLE)) ==
1126 (XD_ECC1_ERROR | XD_ECC1_UNCORRECTABLE))
1127 || ((reg & (XD_ECC2_ERROR | XD_ECC2_UNCORRECTABLE)) ==
1128 (XD_ECC2_ERROR | XD_ECC2_UNCORRECTABLE))) {
1132 RTSX_DEBUGP(
"old block 0x%x ecc error\n", old_blk);
1141 rtsx_clear_xd_error(chip);
1145 xd_assign_phy_addr(chip, new_page,
XD_RW_ADDR);
1153 rtsx_clear_xd_error(chip);
1157 xd_mark_bad_block(chip, new_blk);
1173 static int xd_reset_cmd(
struct rtsx_chip *chip)
1190 if (((ptr[0] & READY_FLAG) ==
READY_STATE) && (ptr[1] & XD_RDY))
1196 static int xd_erase_block(
struct rtsx_chip *chip,
u32 phy_blk)
1208 for (i = 0; i < 3; i++) {
1219 rtsx_clear_xd_error(chip);
1222 xd_mark_bad_block(chip, phy_blk);
1228 retval = xd_reset_cmd(chip);
1235 if (*ptr & PROGRAM_ERROR) {
1236 xd_mark_bad_block(chip, phy_blk);
1244 xd_mark_bad_block(chip, phy_blk);
1250 static int xd_build_l2p_tbl(
struct rtsx_chip *chip,
int zone_no)
1256 u16 max_logoff, cur_fst_page_logoff, cur_lst_page_logoff, ent_lst_page_logoff;
1262 retval = xd_init_l2p_tbl(chip);
1267 if (xd_card->
zone[zone_no].build_flag) {
1268 RTSX_DEBUGP(
"l2p table of zone %d has been built\n", zone_no);
1272 zone = &(xd_card->
zone[zone_no]);
1301 start = (
u32)(zone_no) << 10;
1302 end = (
u32)(zone_no + 1) << 10;
1306 RTSX_DEBUGP(
"start block 0x%x, end block 0x%x\n", start, end);
1311 for (i = start; i <
end; i++) {
1315 retval = xd_read_redundant(chip, page_addr, redunt, 11);
1324 if (xd_check_data_blank(redunt)) {
1326 xd_set_unused_block(chip, i);
1330 cur_fst_page_logoff = xd_load_log_block_addr(redunt);
1331 if ((cur_fst_page_logoff == 0xFFFF) || (cur_fst_page_logoff > max_logoff)) {
1332 retval = xd_erase_block(chip, i);
1334 xd_set_unused_block(chip, i);
1338 if ((zone_no == 0) && (cur_fst_page_logoff == 0) && (redunt[
PAGE_STATUS] !=
XD_GPG))
1341 if (zone->
l2p_table[cur_fst_page_logoff] == 0xFFFF) {
1342 zone->
l2p_table[cur_fst_page_logoff] = (
u16)(i & 0x3FF);
1346 phy_block = zone->
l2p_table[cur_fst_page_logoff] + ((
u32)((zone_no) << 10));
1348 page_addr = ((i + 1) << xd_card->
block_shift) - 1;
1350 retval = xd_read_redundant(chip, page_addr, redunt, 11);
1354 cur_lst_page_logoff = xd_load_log_block_addr(redunt);
1355 if (cur_lst_page_logoff == cur_fst_page_logoff) {
1358 page_addr = ((phy_block + 1) << xd_card->
block_shift) - 1;
1360 for (m = 0; m < 3; m++) {
1361 retval = xd_read_redundant(chip, page_addr, redunt, 11);
1367 zone->
l2p_table[cur_fst_page_logoff] = (
u16)(i & 0x3FF);
1368 retval = xd_erase_block(chip, phy_block);
1370 xd_set_unused_block(chip, phy_block);
1374 ent_lst_page_logoff = xd_load_log_block_addr(redunt);
1375 if (ent_lst_page_logoff != cur_fst_page_logoff) {
1376 zone->
l2p_table[cur_fst_page_logoff] = (
u16)(i & 0x3FF);
1377 retval = xd_erase_block(chip, phy_block);
1379 xd_set_unused_block(chip, phy_block);
1382 retval = xd_erase_block(chip, i);
1384 xd_set_unused_block(chip, i);
1387 retval = xd_erase_block(chip, i);
1389 xd_set_unused_block(chip, i);
1399 for (start = 0; start <
end; start++) {
1404 RTSX_DEBUGP(
"Block count %d, invalid L2P entry %d\n", end, i);
1444 static int xd_read_multiple_pages(
struct rtsx_chip *chip,
u32 phy_blk,
u32 log_blk,
1445 u8 start_page,
u8 end_page,
u8 *buf,
unsigned int *
index,
unsigned int *offset)
1448 u32 page_addr, new_blk;
1453 if (start_page > end_page)
1456 page_cnt = end_page - start_page;
1457 zone_no = (
int)(log_blk / 1000);
1458 log_off = (
u16)(log_blk % 1000);
1460 if ((phy_blk & 0x3FF) == 0x3FF) {
1461 for (i = 0; i < 256; i++) {
1464 retval = xd_read_redundant(chip, page_addr,
NULL, 0);
1475 page_addr = (phy_blk << xd_card->
block_shift) + start_page;
1479 xd_assign_phy_addr(chip, page_addr,
XD_RW_ADDR);
1497 rtsx_clear_xd_error(chip);
1498 xd_clear_dma_buffer(chip);
1518 if (((reg_val & (XD_ECC1_ERROR | XD_ECC1_UNCORRECTABLE))
1519 == (XD_ECC1_ERROR | XD_ECC1_UNCORRECTABLE))
1520 || ((reg_val & (XD_ECC2_ERROR | XD_ECC2_UNCORRECTABLE))
1521 == (XD_ECC2_ERROR | XD_ECC2_UNCORRECTABLE))) {
1531 new_blk = xd_get_unused_block(chip, zone_no);
1537 retval = xd_copy_page(chip, phy_blk, new_blk, 0, xd_card->
page_off + 1);
1540 retval = xd_erase_block(chip, new_blk);
1542 xd_set_unused_block(chip, new_blk);
1549 xd_set_l2p_tbl(chip, zone_no, log_off, (
u16)(new_blk & 0x3FF));
1550 xd_erase_block(chip, phy_blk);
1551 xd_mark_bad_block(chip, phy_blk);
1558 static int xd_finish_write(
struct rtsx_chip *chip,
1565 RTSX_DEBUGP(
"xd_finish_write, old_blk = 0x%x, new_blk = 0x%x, log_blk = 0x%x\n",
1566 old_blk, new_blk, log_blk);
1571 zone_no = (
int)(log_blk / 1000);
1572 log_off = (
u16)(log_blk % 1000);
1575 retval = xd_init_page(chip, new_blk, log_off,
1578 retval = xd_erase_block(chip, new_blk);
1580 xd_set_unused_block(chip, new_blk);
1584 retval = xd_copy_page(chip, old_blk, new_blk,
1588 retval = xd_erase_block(chip, new_blk);
1590 xd_set_unused_block(chip, new_blk);
1596 retval = xd_erase_block(chip, old_blk);
1599 xd_mark_bad_block(chip, old_blk);
1602 xd_set_unused_block(chip, old_blk);
1610 xd_set_l2p_tbl(chip, zone_no, log_off, (
u16)(new_blk & 0x3FF));
1615 static int xd_prepare_write(
struct rtsx_chip *chip,
1616 u32 old_blk,
u32 new_blk,
u32 log_blk,
u8 page_off)
1620 RTSX_DEBUGP(
"%s, old_blk = 0x%x, new_blk = 0x%x, log_blk = 0x%x, page_off = %d\n",
1621 __func__, old_blk, new_blk, log_blk, (
int)page_off);
1624 retval = xd_copy_page(chip, old_blk, new_blk, 0, page_off);
1633 static int xd_write_multiple_pages(
struct rtsx_chip *chip,
u32 old_blk,
u32 new_blk,
u32 log_blk,
1634 u8 start_page,
u8 end_page,
u8 *buf,
unsigned int *index,
unsigned int *offset)
1642 RTSX_DEBUGP(
"%s, old_blk = 0x%x, new_blk = 0x%x, log_blk = 0x%x\n",
1643 __func__, old_blk, new_blk, log_blk);
1645 if (start_page > end_page)
1648 page_cnt = end_page - start_page;
1649 zone_no = (
int)(log_blk / 1000);
1650 log_off = (
u16)(log_blk % 1000);
1652 page_addr = (new_blk << xd_card->
block_shift) + start_page;
1654 retval = xd_send_cmd(chip,
READ1_1);
1665 xd_assign_phy_addr(chip, page_addr,
XD_RW_ADDR);
1681 rtsx_clear_xd_error(chip);
1691 if (end_page == (xd_card->
page_off + 1)) {
1695 retval = xd_erase_block(chip, old_blk);
1698 xd_mark_bad_block(chip, old_blk);
1701 xd_set_unused_block(chip, old_blk);
1708 xd_set_l2p_tbl(chip, zone_no, log_off, (
u16)(new_blk & 0x3FF));
1717 xd_mark_bad_block(chip, new_blk);
1723 #ifdef XD_DELAY_WRITE
1732 retval = xd_switch_clock(chip);
1737 retval = xd_finish_write(chip,
1752 #ifdef XD_DELAY_WRITE
1756 unsigned int index = 0, offset = 0;
1757 u32 log_blk, old_blk = 0, new_blk = 0;
1758 u16 log_off, total_sec_cnt = sector_cnt;
1759 u8 start_page, end_page = 0, page_cnt;
1766 RTSX_DEBUGP(
"xd_rw: scsi_sg_count = %d\n", scsi_sg_count(srb));
1768 ptr = (
u8 *)scsi_sglist(srb);
1770 retval = xd_switch_clock(chip);
1782 start_page = (
u8)start_sector & xd_card->
page_off;
1783 zone_no = (
int)(log_blk / 1000);
1784 log_off = (
u16)(log_blk % 1000);
1786 if (xd_card->
zone[zone_no].build_flag == 0) {
1787 retval = xd_build_l2p_tbl(chip, zone_no);
1796 #ifdef XD_DELAY_WRITE
1798 (delay_write->
logblock == log_blk) &&
1799 (start_page > delay_write->
pageoff)) {
1802 retval = xd_copy_page(chip,
1805 delay_write->
pageoff, start_page);
1814 (delay_write->
logblock == log_blk) &&
1815 (start_page == delay_write->
pageoff)) {
1826 old_blk = xd_get_l2p_tbl(chip, zone_no, log_off);
1827 new_blk = xd_get_unused_block(chip, zone_no);
1833 retval = xd_prepare_write(chip, old_blk, new_blk, log_blk, start_page);
1842 #ifdef XD_DELAY_WRITE
1846 #ifdef XD_DELAY_WRITE
1858 old_blk = xd_get_l2p_tbl(chip, zone_no, log_off);
1867 while (total_sec_cnt) {
1874 if ((start_page + total_sec_cnt) > (xd_card->
page_off + 1))
1877 end_page = start_page + (
u8)total_sec_cnt;
1879 page_cnt = end_page - start_page;
1881 retval = xd_read_multiple_pages(chip, old_blk, log_blk,
1882 start_page, end_page, ptr, &index, &offset);
1888 retval = xd_write_multiple_pages(chip, old_blk, new_blk, log_blk,
1889 start_page, end_page, ptr, &index, &offset);
1896 total_sec_cnt -= page_cnt;
1897 if (scsi_sg_count(srb) == 0)
1898 ptr += page_cnt * 512;
1900 if (total_sec_cnt == 0)
1904 zone_no = (
int)(log_blk / 1000);
1905 log_off = (
u16)(log_blk % 1000);
1907 if (xd_card->
zone[zone_no].build_flag == 0) {
1908 retval = xd_build_l2p_tbl(chip, zone_no);
1916 old_blk = xd_get_l2p_tbl(chip, zone_no, log_off);
1927 new_blk = xd_get_unused_block(chip, zone_no);
1938 (end_page != (xd_card->
page_off + 1))) {
1939 #ifdef XD_DELAY_WRITE
1944 delay_write->
pageoff = end_page;
1952 retval = xd_finish_write(chip, old_blk, new_blk, log_blk, end_page);
1964 scsi_set_resid(srb, 0);
1975 for (i = 0; i < xd_card->
zone_cnt; i++) {
1976 if (xd_card->
zone[i].l2p_table !=
NULL) {
1980 if (xd_card->
zone[i].free_table !=
NULL) {
1992 #ifdef XD_DELAY_WRITE
2022 retval = xd_pull_ctl_disable(chip);