25 #include <linux/sched.h>
45 return (ms_card->
err_code == err_code);
48 static int ms_parse_err_code(
struct rtsx_chip *chip)
75 rtsx_clear_ms_error(chip);
88 if (
CHK_MSPRO(ms_card) && !(*ptr & 0x80)) {
97 rtsx_clear_ms_error(chip);
106 u8 cfg,
int mode_2k,
int use_sg,
void *
buf,
int buf_len)
109 u8 val, err_code = 0;
112 if (!buf || !buf_len)
152 ms_set_err_code(chip, err_code);
174 if (!data || (data_len < cnt))
179 for (i = 0; i <
cnt; i++) {
203 rtsx_clear_ms_error(chip);
206 if (val & MS_CRC16_ERR) {
208 TRACE_RET(chip, ms_parse_err_code(chip));
211 if (
CHK_MSPRO(ms_card) && !(val & 0x80)) {
214 TRACE_RET(chip, ms_parse_err_code(chip));
219 if (val & MS_RDY_TIMEOUT) {
221 TRACE_RET(chip, ms_parse_err_code(chip));
225 TRACE_RET(chip, ms_parse_err_code(chip));
250 for (i = 0; i < data_len - 1; i++)
263 rtsx_clear_ms_error(chip);
266 if (val & MS_CRC16_ERR) {
268 TRACE_RET(chip, ms_parse_err_code(chip));
271 if (
CHK_MSPRO(ms_card) && !(val & 0x80)) {
274 TRACE_RET(chip, ms_parse_err_code(chip));
279 if (val & MS_RDY_TIMEOUT) {
281 TRACE_RET(chip, ms_parse_err_code(chip));
285 TRACE_RET(chip, ms_parse_err_code(chip));
302 u8 read_start,
u8 read_cnt,
u8 write_start,
u8 write_cnt)
307 data[0] = read_start;
309 data[2] = write_start;
317 rtsx_clear_ms_error(chip);
333 static int ms_set_init_para(
struct rtsx_chip *chip)
357 retval = switch_clock(chip, ms_card->
ms_clock);
377 retval = switch_clock(chip, ms_card->
ms_clock);
384 static int ms_pull_ctl_disable(
struct rtsx_chip *chip)
415 static int ms_pull_ctl_enable(
struct rtsx_chip *chip)
458 static int ms_prepare_reset(
struct rtsx_chip *chip)
483 retval = ms_pull_ctl_enable(chip);
525 retval = ms_set_init_para(chip);
532 static int ms_identify_media_type(
struct rtsx_chip *chip,
int switch_8bit_bus)
547 if (i == MS_MAX_RETRY_COUNT)
575 }
else if ((val == 0x01) || (val == 0x02) || (val == 0x03)) {
588 }
else if (val == 7) {
601 static int ms_confirm_cpu_startup(
struct rtsx_chip *chip)
619 if (i == MS_MAX_RETRY_COUNT)
634 if (i == MS_MAX_RETRY_COUNT)
648 static int ms_switch_parallel_bus(
struct rtsx_chip *chip)
666 static int ms_switch_8bit_bus(
struct rtsx_chip *chip)
684 retval = ms_set_init_para(chip);
697 static int ms_pro_reset_flow(
struct rtsx_chip *chip,
int switch_8bit_bus)
702 for (i = 0; i < 3; i++) {
703 retval = ms_prepare_reset(chip);
707 retval = ms_identify_media_type(chip, switch_8bit_bus);
711 retval = ms_confirm_cpu_startup(chip);
715 retval = ms_switch_parallel_bus(chip);
734 retval = ms_set_init_para(chip);
740 retval = ms_switch_8bit_bus(chip);
785 static int ms_read_attribute_info(
struct rtsx_chip *chip)
792 u32 xc_total_blk = 0, xc_blk_size = 0;
794 u32 sys_info_addr = 0, sys_info_size;
795 #ifdef SUPPORT_PCGL_1P18
796 u32 model_name_addr = 0, model_name_size;
797 int found_sys_info = 0, found_model_name = 0;
845 0x40,
WAIT_INT, 0, 0, buf, 64 * 512);
849 rtsx_clear_ms_error(chip);
864 if ((val &
MS_INT_CED) || !(val & MS_INT_BREQ))
881 if ((buf[0] != 0xa5) && (buf[1] != 0xc3)) {
887 if ((buf[4] < 1) || (buf[4] > 12)) {
892 for (i = 0; i < buf[4]; i++) {
893 int cur_addr_off = 16 + i * 12;
896 if ((buf[cur_addr_off + 8] == 0x10) || (buf[cur_addr_off + 8] == 0x13))
898 if (buf[cur_addr_off + 8] == 0x10)
901 sys_info_addr = ((
u32)buf[cur_addr_off + 0] << 24) |
902 ((
u32)buf[cur_addr_off + 1] << 16) |
903 ((
u32)buf[cur_addr_off + 2] << 8) | buf[cur_addr_off + 3];
904 sys_info_size = ((
u32)buf[cur_addr_off + 4] << 24) |
905 ((
u32)buf[cur_addr_off + 5] << 16) |
906 ((
u32)buf[cur_addr_off + 6] << 8) | buf[cur_addr_off + 7];
907 RTSX_DEBUGP(
"sys_info_addr = 0x%x, sys_info_size = 0x%x\n",
908 sys_info_addr, sys_info_size);
909 if (sys_info_size != 96) {
913 if (sys_info_addr < 0x1A0) {
917 if ((sys_info_size + sys_info_addr) > 0x8000) {
923 if (buf[cur_addr_off + 8] == 0x13)
926 #ifdef SUPPORT_PCGL_1P18
932 #ifdef SUPPORT_PCGL_1P18
933 if (buf[cur_addr_off + 8] == 0x15) {
934 model_name_addr = ((
u32)buf[cur_addr_off + 0] << 24) |
935 ((
u32)buf[cur_addr_off + 1] << 16) |
936 ((
u32)buf[cur_addr_off + 2] << 8) | buf[cur_addr_off + 3];
937 model_name_size = ((
u32)buf[cur_addr_off + 4] << 24) |
938 ((
u32)buf[cur_addr_off + 5] << 16) |
939 ((
u32)buf[cur_addr_off + 6] << 8) | buf[cur_addr_off + 7];
940 RTSX_DEBUGP(
"model_name_addr = 0x%x, model_name_size = 0x%x\n",
941 model_name_addr, model_name_size);
942 if (model_name_size != 48) {
946 if (model_name_addr < 0x1A0) {
950 if ((model_name_size + model_name_addr) > 0x8000) {
955 found_model_name = 1;
958 if (found_sys_info && found_model_name)
968 class_code = buf[sys_info_addr + 0];
969 device_type = buf[sys_info_addr + 56];
970 sub_class = buf[sys_info_addr + 46];
973 xc_total_blk = ((
u32)buf[sys_info_addr + 6] << 24) |
974 ((
u32)buf[sys_info_addr + 7] << 16) |
975 ((
u32)buf[sys_info_addr + 8] << 8) |
976 buf[sys_info_addr + 9];
977 xc_blk_size = ((
u32)buf[sys_info_addr + 32] << 24) |
978 ((
u32)buf[sys_info_addr + 33] << 16) |
979 ((
u32)buf[sys_info_addr + 34] << 8) |
980 buf[sys_info_addr + 35];
981 RTSX_DEBUGP(
"xc_total_blk = 0x%x, xc_blk_size = 0x%x\n", xc_total_blk, xc_blk_size);
983 total_blk = ((
u16)buf[sys_info_addr + 6] << 8) | buf[sys_info_addr + 7];
984 blk_size = ((
u16)buf[sys_info_addr + 2] << 8) | buf[sys_info_addr + 3];
988 total_blk = ((
u16)buf[sys_info_addr + 6] << 8) | buf[sys_info_addr + 7];
989 blk_size = ((
u16)buf[sys_info_addr + 2] << 8) | buf[sys_info_addr + 3];
993 RTSX_DEBUGP(
"class_code = 0x%x, device_type = 0x%x, sub_class = 0x%x\n",
994 class_code, device_type, sub_class);
997 #ifdef SUPPORT_PCGL_1P18
1005 if (class_code != 0x03)
1008 if (class_code != 0x02)
1012 if (class_code != 0x02)
1016 if (device_type != 0x00) {
1017 if ((device_type == 0x01) || (device_type == 0x02) ||
1018 (device_type == 0x03)) {
1025 if (sub_class & 0xC0)
1028 RTSX_DEBUGP(
"class_code: 0x%x, device_type: 0x%x, sub_class: 0x%x\n",
1029 class_code, device_type, sub_class);
1034 ms_card->
capacity = xc_total_blk * xc_blk_size;
1046 #ifdef SUPPORT_MAGIC_GATE
1050 static int reset_ms_pro(
struct rtsx_chip *chip)
1054 #ifdef XC_POWERCLASS
1055 u8 change_power_class;
1058 change_power_class = 2;
1060 change_power_class = 1;
1062 change_power_class = 0;
1065 #ifdef XC_POWERCLASS
1068 retval = ms_pro_reset_flow(chip, 1);
1071 retval = ms_pro_reset_flow(chip, 0);
1079 retval = ms_read_attribute_info(chip);
1083 #ifdef XC_POWERCLASS
1085 change_power_class = 0;
1087 if (change_power_class &&
CHK_MSXC(ms_card)) {
1090 RTSX_DEBUGP(
"power_class_en = 0x%x\n", power_class_en);
1091 RTSX_DEBUGP(
"change_power_class = %d\n", change_power_class);
1093 if (change_power_class)
1094 power_class_en &= (1 << (change_power_class - 1));
1098 if (power_class_en) {
1100 RTSX_DEBUGP(
"power_class_mode = 0x%x", power_class_mode);
1101 if (change_power_class > power_class_mode)
1102 change_power_class = power_class_mode;
1103 if (change_power_class) {
1104 retval = msxc_change_power(chip, change_power_class);
1106 change_power_class--;
1114 #ifdef SUPPORT_MAGIC_GATE
1128 static int ms_read_status_reg(
struct rtsx_chip *chip)
1150 static int ms_read_extra_data(
struct rtsx_chip *chip,
1151 u16 block_addr,
u8 page_num,
u8 *buf,
int buf_len)
1169 data[2] = (
u8)(block_addr >> 8);
1170 data[3] = (
u8)block_addr;
1179 if (i == MS_MAX_RETRY_COUNT)
1189 if (i == MS_MAX_RETRY_COUNT)
1201 if (val & INT_REG_CED) {
1202 if (val & INT_REG_ERR) {
1203 retval = ms_read_status_reg(chip);
1217 if (buf && buf_len) {
1220 memcpy(buf, data, buf_len);
1226 static int ms_write_extra_data(
struct rtsx_chip *chip,
1227 u16 block_addr,
u8 page_num,
u8 *buf,
int buf_len)
1246 data[2] = (
u8)(block_addr >> 8);
1247 data[3] = (
u8)block_addr;
1252 data[i] = buf[i - 6];
1267 if (val & INT_REG_CMDNK) {
1271 if (val & INT_REG_CED) {
1272 if (val & INT_REG_ERR) {
1282 static int ms_read_page(
struct rtsx_chip *chip,
u16 block_addr,
u8 page_num)
1298 data[2] = (
u8)(block_addr >> 8);
1299 data[3] = (
u8)block_addr;
1316 if (val & INT_REG_CMDNK) {
1321 if (val & INT_REG_CED) {
1322 if (val & INT_REG_ERR) {
1327 retval = ms_read_status_reg(chip);
1332 if (!(val & INT_REG_BREQ)) {
1350 static int ms_set_bad_block(
struct rtsx_chip *chip,
u16 phy_blk)
1356 retval = ms_read_extra_data(chip, phy_blk, 0, extra, MS_EXTRA_SIZE);
1372 data[2] = (
u8)(phy_blk >> 8);
1373 data[3] = (
u8)phy_blk;
1376 data[6] = extra[0] & 0x7F;
1392 if (val & INT_REG_CMDNK) {
1397 if (val & INT_REG_CED) {
1398 if (val & INT_REG_ERR) {
1408 static int ms_erase_block(
struct rtsx_chip *chip,
u16 phy_blk)
1426 data[2] = (
u8)(phy_blk >> 8);
1427 data[3] = (
u8)phy_blk;
1445 if (val & INT_REG_CMDNK) {
1452 ms_set_bad_block(chip, phy_blk);
1456 if (val & INT_REG_CED) {
1457 if (val & INT_REG_ERR) {
1467 static void ms_set_page_status(
u16 log_blk,
u8 type,
u8 *extra,
int extra_len)
1469 if (!extra || (extra_len < MS_EXTRA_SIZE))
1472 memset(extra, 0xFF, MS_EXTRA_SIZE);
1482 extra[2] = (
u8)(log_blk >> 8);
1483 extra[3] = (
u8)log_blk;
1486 static int ms_init_page(
struct rtsx_chip *chip,
u16 phy_blk,
u16 log_blk,
u8 start_page,
u8 end_page)
1491 memset(extra, 0xff, MS_EXTRA_SIZE);
1495 extra[2] = (
u8)(log_blk >> 8);
1496 extra[3] = (
u8)log_blk;
1498 for (i = start_page; i < end_page; i++) {
1504 retval = ms_write_extra_data(chip, phy_blk, i, extra, MS_EXTRA_SIZE);
1512 static int ms_copy_page(
struct rtsx_chip *chip,
u16 old_blk,
u16 new_blk,
1513 u16 log_blk,
u8 start_page,
u8 end_page)
1516 int retval, rty_cnt, uncorrect_flag = 0;
1519 RTSX_DEBUGP(
"Copy page from 0x%x to 0x%x, logical block is 0x%x\n",
1520 old_blk, new_blk, log_blk);
1521 RTSX_DEBUGP(
"start_page = %d, end_page = %d\n", start_page, end_page);
1523 retval = ms_read_extra_data(chip, new_blk, 0, extra, MS_EXTRA_SIZE);
1527 retval = ms_read_status_reg(chip);
1542 if (!(val & INT_REG_CED)) {
1548 for (i = start_page; i < end_page; i++) {
1554 ms_read_extra_data(chip, old_blk, i, extra, MS_EXTRA_SIZE);
1568 data[2] = (
u8)(old_blk >> 8);
1569 data[3] = (
u8)old_blk;
1586 if (val & INT_REG_CMDNK) {
1591 if (val & INT_REG_CED) {
1592 if (val & INT_REG_ERR) {
1593 retval = ms_read_status_reg(chip);
1605 if (uncorrect_flag) {
1606 ms_set_page_status(log_blk,
setPS_NG, extra, MS_EXTRA_SIZE);
1610 ms_write_extra_data(chip, old_blk, i, extra, MS_EXTRA_SIZE);
1611 RTSX_DEBUGP(
"page %d : extra[0] = 0x%x\n", i, extra[0]);
1614 ms_set_page_status(log_blk,
setPS_Error, extra, MS_EXTRA_SIZE);
1615 ms_write_extra_data(chip, new_blk, i, extra, MS_EXTRA_SIZE);
1625 if (rty_cnt == MS_MAX_RETRY_COUNT)
1629 if (!(val & INT_REG_BREQ)) {
1636 MS_EXTRA_SIZE,
SystemParm, (6+MS_EXTRA_SIZE));
1646 data[2] = (
u8)(new_blk >> 8);
1647 data[3] = (
u8)new_blk;
1651 if ((extra[0] & 0x60) != 0x60)
1657 data[6 + 2] = (
u8)(log_blk >> 8);
1658 data[6 + 3] = (
u8)log_blk;
1676 if (val & INT_REG_CMDNK) {
1681 if (val & INT_REG_CED) {
1682 if (val & INT_REG_ERR) {
1701 data[2] = (
u8)(old_blk >> 8);
1702 data[3] = (
u8)old_blk;
1721 if (val & INT_REG_CMDNK) {
1726 if (val & INT_REG_CED) {
1727 if (val & INT_REG_ERR) {
1739 static int reset_ms(
struct rtsx_chip *chip)
1745 #ifndef SUPPORT_MAGIC_GATE
1749 retval = ms_prepare_reset(chip);
1759 retval = ms_read_status_reg(chip);
1779 retval = ms_read_extra_data(chip, i, 0, extra, MS_EXTRA_SIZE);
1799 for (j = 0; j < 3; j++) {
1800 retval = ms_read_page(chip, ms_card->
boot_block, j);
1810 retval = ms_read_page(chip, ms_card->
boot_block, 0);
1817 for (i = 0; i < 96; i++)
1854 if (ptr[0] != 0x00 || ptr[1] != 0x01) {
1862 if (ptr[12] != 0x02 || ptr[13] != 0x00) {
1867 if ((ptr[14] == 1) || (ptr[14] == 3))
1871 block_size = ((
u16)ptr[6] << 8) | ptr[7];
1872 if (block_size == 0x0010) {
1876 }
else if (block_size == 0x0008) {
1885 #ifdef SUPPORT_MAGIC_GATE
1897 }
else if (j < 0xA) {
1899 }
else if (j < 0x11) {
1907 eblock_cnt = ((
u16)ptr[10] << 8) | ptr[11];
1941 static int ms_init_l2p_tbl(
struct rtsx_chip *chip)
1956 retval = ms_read_page(chip, ms_card->
boot_block, 1);
1961 for (i = 0; i < (((ms_card->
total_block >> 9) * 10) + 1); i++) {
1970 defect_block = ((
u16)val1 << 8) | val2;
1971 if (defect_block == 0xFFFF)
1974 seg_no = defect_block / 512;
1975 ms_card->
segment[seg_no].defect_list[ms_card->
segment[seg_no].disable_count++] = defect_block;
1979 ms_card->
segment[
i].build_flag = 0;
1984 ms_card->
segment[
i].unused_blk_cnt = 0;
1986 RTSX_DEBUGP(
"defective block count of segment %d is %d\n",
1987 i, ms_card->
segment[i].disable_count);
2001 static u16 ms_get_l2p_tbl(
struct rtsx_chip *chip,
int seg_no,
u16 log_off)
2009 segment = &(ms_card->
segment[seg_no]);
2017 static void ms_set_l2p_tbl(
struct rtsx_chip *chip,
int seg_no,
u16 log_off,
u16 phy_blk)
2025 segment = &(ms_card->
segment[seg_no]);
2030 static void ms_set_unused_block(
struct rtsx_chip *chip,
u16 phy_blk)
2036 seg_no = (
int)phy_blk >> 9;
2037 segment = &(ms_card->
segment[seg_no]);
2046 static u16 ms_get_unused_block(
struct rtsx_chip *chip,
int seg_no)
2052 segment = &(ms_card->
segment[seg_no]);
2067 static const unsigned short ms_start_idx[] = {0, 494, 990, 1486, 1982, 2478, 2974, 3470,
2068 3966, 4462, 4958, 5454, 5950, 6446, 6942, 7438, 7934};
2070 static int ms_arbitrate_l2p(
struct rtsx_chip *chip,
u16 phy_blk,
u16 log_off,
u8 us1,
u8 us2)
2077 seg_no = (
int)phy_blk >> 9;
2078 segment = &(ms_card->
segment[seg_no]);
2084 ms_erase_block(chip, tmp_blk);
2086 ms_set_unused_block(chip, tmp_blk);
2090 ms_erase_block(chip, phy_blk);
2092 ms_set_unused_block(chip, phy_blk);
2095 if (phy_blk < tmp_blk) {
2097 ms_erase_block(chip, phy_blk);
2099 ms_set_unused_block(chip, phy_blk);
2102 ms_erase_block(chip, tmp_blk);
2104 ms_set_unused_block(chip, tmp_blk);
2112 static int ms_build_l2p_tbl(
struct rtsx_chip *chip,
int seg_no)
2116 int retval, table_size, disable_cnt, defect_flag,
i;
2123 retval = ms_init_l2p_tbl(chip);
2128 if (ms_card->
segment[seg_no].build_flag) {
2129 RTSX_DEBUGP(
"l2p table of segment %d has been built\n", seg_no);
2138 segment = &(ms_card->
segment[seg_no]);
2154 start = (
u16)seg_no << 9;
2155 end = (
u16)(seg_no + 1) << 9;
2162 for (phy_blk = start; phy_blk <
end; phy_blk++) {
2177 retval = ms_read_extra_data(chip, phy_blk, 0, extra, MS_EXTRA_SIZE);
2180 ms_set_bad_block(chip, phy_blk);
2187 retval = ms_erase_block(chip, phy_blk);
2196 if (!(extra[0] & BLOCK_OK))
2200 if ((extra[0] &
PAGE_OK) != PAGE_OK)
2203 log_blk = ((
u16)extra[2] << 8) | extra[3];
2205 if (log_blk == 0xFFFF) {
2207 retval = ms_erase_block(chip, phy_blk);
2211 ms_set_unused_block(chip, phy_blk);
2215 if ((log_blk < ms_start_idx[seg_no]) ||
2216 (log_blk >= ms_start_idx[seg_no+1])) {
2218 retval = ms_erase_block(chip, phy_blk);
2222 ms_set_unused_block(chip, phy_blk);
2226 if (segment->
l2p_table[log_blk - ms_start_idx[seg_no]] == 0xFFFF) {
2227 segment->
l2p_table[log_blk - ms_start_idx[seg_no]] = phy_blk;
2231 us1 = extra[0] & 0x10;
2232 tmp_blk = segment->
l2p_table[log_blk - ms_start_idx[seg_no]];
2233 retval = ms_read_extra_data(chip, tmp_blk, 0, extra, MS_EXTRA_SIZE);
2236 us2 = extra[0] & 0x10;
2238 (
void)ms_arbitrate_l2p(chip, phy_blk, log_blk-ms_start_idx[seg_no], us1, us2);
2258 for (log_blk = ms_start_idx[seg_no]; log_blk < ms_start_idx[seg_no + 1]; log_blk++) {
2259 if (segment->
l2p_table[log_blk-ms_start_idx[seg_no]] == 0xFFFF) {
2260 phy_blk = ms_get_unused_block(chip, seg_no);
2261 if (phy_blk == 0xFFFF) {
2265 retval = ms_init_page(chip, phy_blk, log_blk, 0, 1);
2269 segment->
l2p_table[log_blk-ms_start_idx[seg_no]] = phy_blk;
2286 for (log_blk = 0; log_blk < 494; log_blk++) {
2289 RTSX_DEBUGP(
"Boot block is not the first normal block.\n");
2294 phy_blk = ms_get_unused_block(chip, 0);
2295 retval = ms_copy_page(chip, tmp_blk, phy_blk,
2296 log_blk, 0, ms_card->
page_off + 1);
2302 retval = ms_set_bad_block(chip, tmp_blk);
2343 retval = reset_ms_pro(chip);
2346 retval = reset_ms(chip);
2354 retval = ms_set_init_para(chip);
2362 retval = ms_build_l2p_tbl(chip, ms_card->
total_block / 512 - 1);
2378 data[1] = (
u8)(sec_cnt >> 8);
2379 data[2] = (
u8)sec_cnt;
2380 data[3] = (
u8)(start_sec >> 24);
2381 data[4] = (
u8)(start_sec >> 16);
2382 data[5] = (
u8)(start_sec >> 8);
2383 data[6] = (
u8)start_sec;
2391 if (i == MS_MAX_RETRY_COUNT)
2418 static inline int ms_auto_tune_clock(
struct rtsx_chip *chip)
2447 u8 val, trans_mode, rw_tpc, rw_cmd;
2454 if ((start_sector % 4) || (sector_cnt % 4)) {
2498 || !(val & MS_INT_BREQ)
2502 if (val & MS_INT_BREQ) {
2515 if ((ms_card->
capacity - start_sector) > 0xFE00)
2520 if (count > sector_cnt) {
2529 retval = mspro_set_rw_cmd(chip, start_sector, count, rw_cmd);
2537 scsi_sg_count(srb), scsi_sglist(srb), scsi_bufflen(srb));
2541 rtsx_clear_ms_error(chip);
2545 RTSX_DEBUGP(
"No card exist, exit mspro_rw_multi_sector\n");
2549 if (val & MS_INT_BREQ)
2552 if (val & (MS_CRC16_ERR | MS_RDY_TIMEOUT)) {
2555 ms_auto_tune_clock(chip);
2571 static int mspro_read_format_progress(
struct rtsx_chip *chip,
const int short_data_len)
2575 u32 total_progress, cur_progress;
2579 RTSX_DEBUGP(
"mspro_read_format_progress, short_data_len = %d\n", short_data_len);
2593 if (!(tmp & MS_INT_BREQ)) {
2602 if (short_data_len >= 256)
2605 cnt = (
u8)short_data_len;
2619 total_progress = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3];
2620 cur_progress = (data[4] << 24) | (data[5] << 16) | (data[6] << 8) | data[7];
2622 RTSX_DEBUGP(
"total_progress = %d, cur_progress = %d\n",
2623 total_progress, cur_progress);
2625 if (total_progress == 0) {
2628 u64 ulltmp = (
u64)cur_progress * (
u64)65535;
2629 do_div(ulltmp, total_progress);
2634 for (i = 0; i < 5000; i++) {
2662 if (tmp & MS_INT_CED) {
2665 }
else if (tmp & MS_INT_BREQ) {
2684 for (i = 0; i < 65535; i++) {
2712 switch (short_data_len) {
2733 if (i == MS_MAX_RETRY_COUNT)
2741 retval = mspro_set_rw_cmd(chip, 0, para,
PRO_FORMAT);
2750 if ((tmp & (MS_INT_BREQ | MS_INT_CED)) == MS_INT_BREQ) {
2757 if (tmp & MS_INT_CED) {
2769 static int ms_read_multiple_pages(
struct rtsx_chip *chip,
u16 phy_blk,
u16 log_blk,
2777 retval = ms_read_extra_data(chip, phy_blk, start_page, extra, MS_EXTRA_SIZE);
2779 if ((extra[1] & 0x30) != 0x30) {
2795 data[2] = (
u8)(phy_blk >> 8);
2796 data[3] = (
u8)phy_blk;
2798 data[5] = start_page;
2805 if (i == MS_MAX_RETRY_COUNT)
2816 for (page_addr = start_page; page_addr < end_page; page_addr++) {
2828 if (val & INT_REG_CMDNK) {
2832 if (val & INT_REG_ERR) {
2833 if (val & INT_REG_BREQ) {
2834 retval = ms_read_status_reg(chip);
2838 ms_set_page_status(log_blk,
setPS_NG, extra, MS_EXTRA_SIZE);
2839 ms_write_extra_data(chip, phy_blk,
2840 page_addr, extra, MS_EXTRA_SIZE);
2850 if (!(val & INT_REG_BREQ)) {
2856 if (page_addr == (end_page - 1)) {
2857 if (!(val & INT_REG_CED)) {
2867 if (!(val & INT_REG_CED)) {
2896 rtsx_clear_ms_error(chip);
2903 rtsx_clear_ms_error(chip);
2906 if (val & (MS_CRC16_ERR | MS_RDY_TIMEOUT)) {
2908 rtsx_clear_ms_error(chip);
2913 if (scsi_sg_count(chip->
srb) == 0)
2920 static int ms_write_multiple_pages(
struct rtsx_chip *chip,
u16 old_blk,
u16 new_blk,
2921 u16 log_blk,
u8 start_page,
u8 end_page,
u8 *buf,
2922 unsigned int *index,
unsigned int *offset)
2926 u8 page_addr,
val, data[16];
2940 data[2] = (
u8)(old_blk >> 8);
2941 data[3] = (
u8)old_blk;
2973 data[2] = (
u8)(new_blk >> 8);
2974 data[3] = (
u8)new_blk;
2975 if ((end_page - start_page) == 1)
2980 data[5] = start_page;
2983 data[8] = (
u8)(log_blk >> 8);
2984 data[9] = (
u8)log_blk;
2986 for (i = 0x0A; i < 0x10; i++)
2994 if (i == MS_MAX_RETRY_COUNT)
3002 if (i == MS_MAX_RETRY_COUNT)
3010 for (page_addr = start_page; page_addr < end_page; page_addr++) {
3018 if (val & INT_REG_CMDNK) {
3022 if (val & INT_REG_ERR) {
3026 if (!(val & INT_REG_BREQ)) {
3051 rtsx_clear_ms_error(chip);
3063 if ((end_page - start_page) == 1) {
3064 if (!(val & INT_REG_CED)) {
3069 if (page_addr == (end_page - 1)) {
3070 if (!(val & INT_REG_CED)) {
3081 if ((page_addr == (end_page - 1)) || (page_addr == ms_card->
page_off)) {
3082 if (!(val & INT_REG_CED)) {
3089 if (scsi_sg_count(chip->
srb) == 0)
3097 static int ms_finish_write(
struct rtsx_chip *chip,
u16 old_blk,
u16 new_blk,
3103 retval = ms_copy_page(chip, old_blk, new_blk, log_blk,
3108 seg_no = old_blk >> 9;
3112 ms_set_bad_block(chip, old_blk);
3114 retval = ms_erase_block(chip, old_blk);
3116 ms_set_unused_block(chip, old_blk);
3119 ms_set_l2p_tbl(chip, seg_no, log_blk - ms_start_idx[seg_no], new_blk);
3124 static int ms_prepare_write(
struct rtsx_chip *chip,
u16 old_blk,
u16 new_blk,
3125 u16 log_blk,
u8 start_page)
3130 retval = ms_copy_page(chip, old_blk, new_blk, log_blk, 0, start_page);
3138 #ifdef MS_DELAY_WRITE
3146 retval = ms_set_init_para(chip);
3151 retval = ms_finish_write(chip,
3175 unsigned int index = 0, offset = 0;
3177 u8 start_page, end_page = 0, page_cnt;
3179 #ifdef MS_DELAY_WRITE
3187 ptr = (
u8 *)scsi_sglist(srb);
3191 ms_rw_fail(srb, chip);
3196 start_page = (
u8)(start_sector & ms_card->
page_off);
3198 for (seg_no = 0; seg_no <
ARRAY_SIZE(ms_start_idx) - 1; seg_no++) {
3199 if (log_blk < ms_start_idx[seg_no+1])
3203 if (ms_card->
segment[seg_no].build_flag == 0) {
3204 retval = ms_build_l2p_tbl(chip, seg_no);
3213 #ifdef MS_DELAY_WRITE
3215 (delay_write->
logblock == log_blk) &&
3216 (start_page > delay_write->
pageoff)) {
3218 retval = ms_copy_page(chip,
3221 delay_write->
pageoff, start_page);
3229 (delay_write->
logblock == log_blk) &&
3230 (start_page == delay_write->
pageoff)) {
3241 old_blk = ms_get_l2p_tbl(chip, seg_no, log_blk - ms_start_idx[seg_no]);
3242 new_blk = ms_get_unused_block(chip, seg_no);
3243 if ((old_blk == 0xFFFF) || (new_blk == 0xFFFF)) {
3248 retval = ms_prepare_write(chip, old_blk, new_blk, log_blk, start_page);
3257 #ifdef MS_DELAY_WRITE
3261 #ifdef MS_DELAY_WRITE
3272 old_blk = ms_get_l2p_tbl(chip, seg_no, log_blk - ms_start_idx[seg_no]);
3273 if (old_blk == 0xFFFF) {
3279 RTSX_DEBUGP(
"seg_no = %d, old_blk = 0x%x, new_blk = 0x%x\n", seg_no, old_blk, new_blk);
3281 while (total_sec_cnt) {
3282 if ((start_page + total_sec_cnt) > (ms_card->
page_off + 1))
3285 end_page = start_page + (
u8)total_sec_cnt;
3287 page_cnt = end_page - start_page;
3289 RTSX_DEBUGP(
"start_page = %d, end_page = %d, page_cnt = %d\n",
3290 start_page, end_page, page_cnt);
3293 retval = ms_read_multiple_pages(chip,
3294 old_blk, log_blk, start_page, end_page,
3295 ptr, &index, &offset);
3297 retval = ms_write_multiple_pages(chip, old_blk,
3298 new_blk, log_blk, start_page, end_page,
3299 ptr, &index, &offset);
3308 ms_rw_fail(srb, chip);
3313 if (end_page == (ms_card->
page_off + 1)) {
3314 retval = ms_erase_block(chip, old_blk);
3316 ms_set_unused_block(chip, old_blk);
3318 ms_set_l2p_tbl(chip, seg_no, log_blk - ms_start_idx[seg_no], new_blk);
3322 total_sec_cnt -= page_cnt;
3323 if (scsi_sg_count(srb) == 0)
3324 ptr += page_cnt * 512;
3326 if (total_sec_cnt == 0)
3331 for (seg_no = 0; seg_no <
ARRAY_SIZE(ms_start_idx) - 1;
3333 if (log_blk < ms_start_idx[seg_no+1])
3337 if (ms_card->
segment[seg_no].build_flag == 0) {
3338 retval = ms_build_l2p_tbl(chip, seg_no);
3346 old_blk = ms_get_l2p_tbl(chip, seg_no, log_blk - ms_start_idx[seg_no]);
3347 if (old_blk == 0xFFFF) {
3348 ms_rw_fail(srb, chip);
3353 new_blk = ms_get_unused_block(chip, seg_no);
3354 if (new_blk == 0xFFFF) {
3355 ms_rw_fail(srb, chip);
3360 RTSX_DEBUGP(
"seg_no = %d, old_blk = 0x%x, new_blk = 0x%x\n", seg_no, old_blk, new_blk);
3366 if (end_page < (ms_card->
page_off + 1)) {
3367 #ifdef MS_DELAY_WRITE
3372 delay_write->
pageoff = end_page;
3374 retval = ms_finish_write(chip, old_blk, new_blk, log_blk, end_page);
3381 ms_rw_fail(srb, chip);
3388 scsi_set_resid(srb, 0);
3399 retval = mspro_rw_multi_sector(srb, chip, start_sector, sector_cnt);
3401 retval = ms_rw_multi_sector(srb, chip, start_sector, sector_cnt);
3428 #ifdef SUPPORT_MAGIC_GATE
3430 #ifdef READ_BYTES_WAIT_INT
3431 static int ms_poll_int(
struct rtsx_chip *chip)
3452 #ifdef MS_SAMPLE_INT_ERR
3453 static int check_ms_err(
struct rtsx_chip *chip)
3474 static int check_ms_err(
struct rtsx_chip *chip)
3482 if (val & MS_TRANSFER_ERR)
3489 static int mg_send_ex_cmd(
struct rtsx_chip *chip,
u8 cmd,
u8 entry_num)
3500 data[6] = entry_num;
3508 if (i == MS_MAX_RETRY_COUNT)
3511 if (check_ms_err(chip)) {
3512 rtsx_clear_ms_error(chip);
3554 u8 buf1[32], buf2[12];
3558 if (scsi_bufflen(srb) < 12) {
3569 retval = mg_send_ex_cmd(chip,
MG_SET_LID, 0);
3577 for (i = 0; i < 8; i++)
3578 buf1[8+i] = buf2[4+i];
3585 if (check_ms_err(chip)) {
3587 rtsx_clear_ms_error(chip);
3628 rtsx_clear_ms_error(chip);
3631 if (check_ms_err(chip)) {
3633 rtsx_clear_ms_error(chip);
3637 bufflen =
min(1052, (
int)scsi_bufflen(srb));
3662 retval = mg_send_ex_cmd(chip,
MG_GET_ID, 0);
3673 if (check_ms_err(chip)) {
3675 rtsx_clear_ms_error(chip);
3681 #ifdef READ_BYTES_WAIT_INT
3682 retval = ms_poll_int(chip);
3689 retval = mg_send_ex_cmd(chip,
MG_SET_RD, 0);
3695 bufflen =
min(12, (
int)scsi_bufflen(srb));
3698 for (i = 0; i < 8; i++)
3701 for (i = 0; i < 24; i++)
3710 if (check_ms_err(chip)) {
3712 rtsx_clear_ms_error(chip);
3727 u8 buf1[32], buf2[36];
3748 if (check_ms_err(chip)) {
3750 rtsx_clear_ms_error(chip);
3760 memcpy(buf2 + 20, buf1, 16);
3762 bufflen =
min(36, (
int)scsi_bufflen(srb));
3765 #ifdef READ_BYTES_WAIT_INT
3766 retval = ms_poll_int(chip);
3799 bufflen =
min(12, (
int)scsi_bufflen(srb));
3802 for (i = 0; i < 8; i++)
3805 for (i = 0; i < 24; i++)
3813 if (check_ms_err(chip)) {
3815 rtsx_clear_ms_error(chip);
3859 rtsx_clear_ms_error(chip);
3862 if (check_ms_err(chip)) {
3864 rtsx_clear_ms_error(chip);
3868 bufflen =
min(1028, (
int)scsi_bufflen(srb));
3881 #ifdef MG_SET_ICV_SLOW
3899 bufflen =
min(1028, (
int)scsi_bufflen(srb));
3905 if ((buf[5] & 0xC0) != 0)
3915 #ifdef MG_SET_ICV_SLOW
3916 for (i = 0; i < 2; i++) {
3934 if ((retval < 0) || check_ms_err(chip)) {
3935 rtsx_clear_ms_error(chip);
3937 if ((buf[5] & 0xC0) != 0)
3952 rtsx_clear_ms_error(chip);
3954 if ((buf[5] & 0xC0) != 0)
3987 #ifdef MS_DELAY_WRITE
4005 retval = ms_pull_ctl_disable(chip);
4029 #ifdef MS_DELAY_WRITE
4041 #ifdef SUPPORT_PCGL_1P18