28 #include <linux/sched.h>
112 sd_clr_err_code(chip);
114 RTS51X_DEBUGP(
"SD/MMC CMD %d, arg = 0x%08x\n", cmd_idx, arg);
121 rts51x_init_cmd(chip);
194 rts51x_clear_sd_error(chip);
203 rts51x_read_rsp_buf(chip, 2, buf, len - 2);
213 (
unsigned short)len, buf);
220 if ((buf[0] & 0xC0) != 0) {
271 memcpy(rsp, buf, rsp_len);
276 static inline void sd_print_debug_reg(
struct rts51x_chip *chip)
278 #ifdef CONFIG_RTS5139_DEBUG
298 sd_clr_err_code(chip);
307 rts51x_init_cmd(chip);
311 for (i = 0; i < (cmd_len < 6 ? cmd_len : 6); i++) {
318 (
u8) (byte_cnt >> 8));
321 (
u8) (blk_cnt >> 8));
344 sd_print_debug_reg(chip);
353 if (buf && buf_len) {
362 static int sd_write_data(
struct rts51x_chip *chip,
u8 trans_mode,
370 sd_clr_err_code(chip);
379 if (buf && buf_len) {
385 rts51x_init_cmd(chip);
389 for (i = 0; i < (cmd_len < 6 ? cmd_len : 6); i++) {
396 (
u8) (byte_cnt >> 8));
399 (
u8) (blk_cnt >> 8));
427 sd_print_debug_reg(chip);
439 static int sd_check_csd(
struct rts51x_chip *chip,
char check_wp)
444 u8 csd_ver, trans_speed;
447 for (i = 0; i < 6; i++) {
470 csd_ver = (rsp[1] & 0xc0) >> 6;
473 trans_speed = rsp[4];
474 if ((trans_speed & 0x07) == 0x02) {
475 if ((trans_speed & 0xf8) >= 0x30) {
480 }
else if ((trans_speed & 0xf8) == 0x28) {
485 }
else if ((trans_speed & 0xf8) == 0x20) {
490 }
else if ((trans_speed & 0xf8) >= 0x10) {
495 }
else if ((trans_speed & 0x08) >= 0x08) {
518 blk_size = rsp[6] & 0x0F;
520 c_size = ((
u16) (rsp[7] & 0x03) << 10)
521 + ((
u16) rsp[8] << 2)
522 + ((
u16) (rsp[9] & 0xC0) >> 6);
524 c_size_mult = (
u8) ((rsp[10] & 0x03) << 1);
525 c_size_mult += (rsp[11] & 0x80) >> 7;
528 (((
u32) (c_size + 1)) *
529 (1 << (c_size_mult + 2))) << (blk_size - 9);
532 u32 total_sector = 0;
533 total_sector = (((
u32) rsp[8] & 0x3f) << 16) |
534 ((
u32) rsp[9] << 8) | (
u32) rsp[10];
537 sd_card->
capacity = (total_sector + 1) << 10;
551 static int sd_set_sample_push_timing(
struct rts51x_chip *chip)
556 rts51x_init_cmd(chip);
626 static void sd_choose_proper_clock(
struct rts51x_chip *chip)
665 static int sd_set_init_para(
struct rts51x_chip *chip)
670 retval = sd_set_sample_push_timing(chip);
674 sd_choose_proper_clock(chip);
676 retval = switch_clock(chip, sd_card->
sd_clock);
700 retval = sd_send_cmd_get_rsp(chip, cmd_idx, addr, cmd_type,
NULL, 0);
707 static int sd_wait_currentstate_dataready(
struct rts51x_chip *chip,
u8 statechk,
708 u8 rdychk,
u16 pollingcnt)
715 for (i = 0; i < pollingcnt; i++) {
721 if (((rsp[3] & 0x1E) == statechk)
722 && ((rsp[3] & 0x01) == rdychk)) {
726 rts51x_clear_sd_error(chip);
734 static int sd_voltage_switch(
struct rts51x_chip *chip)
754 rts51x_init_cmd(chip);
776 rts51x_init_cmd(chip);
789 static int sd_change_phase(
struct rts51x_chip *chip,
u8 sample_point,
792 u16 SD_VP_CTL, SD_DCMPS_CTL;
796 RTS51X_DEBUGP(
"sd_change_phase (sample_point = %d, tune_dir = %d)\n",
797 sample_point, tune_dir);
815 #ifdef CONFIG_RTS5139_DEBUG
828 rts51x_init_cmd(chip);
856 #ifdef CONFIG_RTS5139_DEBUG
870 static int sd_check_spec(
struct rts51x_chip *chip,
u8 bus_width)
892 rts51x_clear_sd_error(chip);
898 if ((buf[0] & 0x0F) == 0)
904 static int sd_query_switch_result(
struct rts51x_chip *chip,
u8 func_group,
905 u8 func_to_switch,
u8 *buf,
int buf_len)
907 u8 support_mask = 0, query_switch = 0, switch_busy = 0;
908 int support_offset = 0, query_switch_offset = 0, check_busy_offset = 0;
915 switch (func_to_switch) {
948 switch (func_to_switch) {
975 switch (func_to_switch) {
1002 buf[query_switch_offset] =
1003 (buf[query_switch_offset] & 0xf0) >> 4;
1004 if (!(buf[support_offset] & support_mask) ||
1005 ((buf[query_switch_offset] & 0x0F) != query_switch))
1009 ((buf[check_busy_offset] & switch_busy) == switch_busy))
1016 u8 func_group,
u8 func_to_switch,
u8 bus_width)
1022 RTS51X_DEBUGP(
"sd_check_switch_mode (mode = %d, func_group = %d,"
1023 "func_to_switch = %d)\n", mode, func_group, func_to_switch);
1031 cmd[4] = 0xF0 + func_to_switch;
1034 cmd[3] = 0xF0 + func_to_switch;
1038 cmd[3] = 0x0F + (func_to_switch << 4);
1051 rts51x_clear_sd_error(chip);
1066 if ((buf[0] == 0) && (buf[1] == 0))
1069 sd_query_switch_result(chip, func_group, func_to_switch,
1078 static int sd_check_switch(
struct rts51x_chip *chip,
1079 u8 func_group,
u8 func_to_switch,
u8 bus_width)
1083 int switch_good = 0;
1085 for (i = 0; i < 3; i++) {
1091 retval = sd_check_switch_mode(chip,
SD_CHECK_MODE, func_group,
1092 func_to_switch, bus_width);
1097 func_group, func_to_switch, bus_width);
1121 static int sd_switch_function(
struct rts51x_chip *chip,
u8 bus_width)
1126 u8 func_to_switch = 0;
1136 for (i = 0; i < 4; i++) {
1137 switch ((
u8) (chip->
option.sd_speed_prior >> (i * 8))) {
1165 if (func_to_switch) {
1199 if (func_to_switch) {
1211 static int sd_wait_data_idle(
struct rts51x_chip *chip)
1217 for (i = 0; i < 100; i++) {
1232 static int sd_sdr_tuning_rx_cmd(
struct rts51x_chip *chip,
u8 sample_point)
1237 retval = sd_change_phase(chip, sample_point,
TUNE_RX);
1251 (
void)sd_wait_data_idle(chip);
1253 rts51x_clear_sd_error(chip);
1260 static int sd_ddr_tuning_rx_cmd(
struct rts51x_chip *chip,
u8 sample_point)
1266 retval = sd_change_phase(chip, sample_point,
TUNE_RX);
1288 (
void)sd_wait_data_idle(chip);
1290 rts51x_clear_sd_error(chip);
1297 static int mmc_ddr_tunning_rx_cmd(
struct rts51x_chip *chip,
u8 sample_point)
1310 retval = sd_change_phase(chip, sample_point,
TUNE_RX);
1323 cmd, 5, 0x200, 1, bus_width,
NULL, 0, 100);
1326 (
void)sd_wait_data_idle(chip);
1328 rts51x_clear_sd_error(chip);
1335 static int sd_sdr_tuning_tx_cmd(
struct rts51x_chip *chip,
u8 sample_point)
1340 retval = sd_change_phase(chip, sample_point,
TUNE_TX);
1363 static int sd_ddr_tuning_tx_cmd(
struct rts51x_chip *chip,
u8 sample_point)
1369 retval = sd_change_phase(chip, sample_point,
TUNE_TX);
1383 retval = sd_wait_currentstate_dataready(chip, 0x08, 1, 20);
1397 cmd, 5, 16, 1, bus_width, sd_card->
raw_csd, 16, 100);
1399 rts51x_clear_sd_error(chip);
1419 int i,
j, cont_path_cnt;
1421 u8 final_phase = 0xFF;
1424 if (phase_map == 0xffff) {
1427 final_phase = chip->
option.ddr50_tx_phase;
1429 final_phase = chip->
option.ddr50_rx_phase;
1431 "so select default phase:0x%x.\n",
1432 tune_dir, final_phase);
1435 final_phase = chip->
option.sdr50_tx_phase;
1437 final_phase = chip->
option.sdr50_rx_phase;
1439 "so select default phase:0x%x.\n",
1440 tune_dir, final_phase);
1449 if (phase_map & (1 << i)) {
1452 j = cont_path_cnt++;
1460 if (cont_path_cnt) {
1461 int idx = cont_path_cnt - 1;
1470 if (cont_path_cnt == 0) {
1474 int idx = cont_path_cnt - 1;
1480 (
path[cont_path_cnt - 1].
end == MAX_PHASE)) {
1481 path[0].start =
path[cont_path_cnt - 1].start - MAX_PHASE - 1;
1482 path[0].len +=
path[cont_path_cnt - 1].len;
1485 path[0].mid += MAX_PHASE + 1;
1491 for (i = 0; i < cont_path_cnt; i++) {
1492 if (
path[i].len > max_len) {
1493 max_len =
path[
i].len;
1506 && chip->
option.sdr50_phase_sel) {
1508 int temp_mid = (max_len - 6) / 2;
1509 int temp_final_phase =
1510 path[final_path_idx].end - (max_len -
1513 if (temp_final_phase < 0)
1514 final_phase = temp_final_phase + MAX_PHASE + 1;
1516 final_phase = (
u8) temp_final_phase;
1530 u32 raw_phase_map[3], phase_map;
1536 tuning_cmd = sd_ddr_tuning_rx_cmd;
1538 tuning_cmd = sd_sdr_tuning_rx_cmd;
1541 tuning_cmd = mmc_ddr_tunning_rx_cmd;
1546 for (i = 0; i < 3; i++) {
1547 raw_phase_map[
i] = 0;
1548 for (j = MAX_PHASE; j >= 0; j--) {
1554 retval = tuning_cmd(chip, (
u8) j);
1556 raw_phase_map[
i] |= 1 <<
j;
1562 phase_map = raw_phase_map[0] & raw_phase_map[1] & raw_phase_map[2];
1563 for (i = 0; i < 3; i++)
1568 final_phase = sd_search_final_phase(chip, phase_map,
TUNE_RX);
1569 if (final_phase == 0xFF)
1572 retval = tuning_cmd(chip, final_phase);
1579 static int sd_ddr_pre_tuning_tx(
struct rts51x_chip *chip)
1584 u8 pre_tune_tx_phase;
1585 u32 pre_tune_phase_map;
1590 pre_tune_tx_phase = 0xFF;
1591 pre_tune_phase_map = 0x0000;
1592 for (i = 0; i < MAX_PHASE + 1; i++) {
1598 retval = sd_change_phase(chip, (
u8) i,
TUNE_TX);
1607 pre_tune_phase_map |= (
u32) 1 << i;
1613 sd_search_final_phase(chip, pre_tune_phase_map,
TUNE_TX);
1614 if (pre_tune_tx_phase == 0xFF)
1617 sd_change_phase(chip, pre_tune_tx_phase,
TUNE_TX);
1618 RTS51X_DEBUGP(
"DDR TX pre tune phase: %d\n", (
int)pre_tune_tx_phase);
1628 u32 raw_phase_map[3], phase_map;
1634 tuning_cmd = sd_ddr_tuning_tx_cmd;
1636 tuning_cmd = sd_sdr_tuning_tx_cmd;
1639 tuning_cmd = sd_ddr_tuning_tx_cmd;
1644 for (i = 0; i < 3; i++) {
1645 raw_phase_map[
i] = 0;
1646 for (j = MAX_PHASE; j >= 0; j--) {
1652 retval = tuning_cmd(chip, (
u8) j);
1654 raw_phase_map[
i] |= 1 <<
j;
1660 phase_map = raw_phase_map[0] & raw_phase_map[1] & raw_phase_map[2];
1661 for (i = 0; i < 3; i++)
1666 final_phase = sd_search_final_phase(chip, phase_map,
TUNE_TX);
1667 if (final_phase == 0xFF)
1670 retval = tuning_cmd(chip, final_phase);
1677 static int sd_sdr_tuning(
struct rts51x_chip *chip)
1681 retval = sd_tuning_tx(chip);
1685 retval = sd_tuning_rx(chip);
1692 static int sd_ddr_tuning(
struct rts51x_chip *chip)
1697 retval = sd_ddr_pre_tuning_tx(chip);
1702 sd_change_phase(chip, (
u8) chip->
option.sd_ddr_tx_phase,
1708 retval = sd_tuning_rx(chip);
1713 retval = sd_tuning_tx(chip);
1721 static int mmc_ddr_tuning(
struct rts51x_chip *chip)
1726 retval = sd_ddr_pre_tuning_tx(chip);
1731 sd_change_phase(chip, (
u8) chip->
option.mmc_ddr_tx_phase,
1737 retval = sd_tuning_rx(chip);
1742 retval = sd_tuning_tx(chip);
1765 retval = switch_clock(chip, sd_card->
sd_clock);
1772 retval = sd_ddr_tuning(chip);
1774 retval = sd_sdr_tuning(chip);
1777 retval = mmc_ddr_tuning(chip);
1787 static int sd_prepare_reset(
struct rts51x_chip *chip)
1798 retval = sd_set_init_para(chip);
1802 rts51x_init_cmd(chip);
1821 static void sd_pull_ctl_disable(
struct rts51x_chip *chip)
1840 static void sd_pull_ctl_enable(
struct rts51x_chip *chip)
1859 static int sd_init_power(
struct rts51x_chip *chip)
1863 rts51x_init_cmd(chip);
1873 sd_pull_ctl_disable(chip);
1879 if (!chip->
option.FT2_fast_mode)
1886 if (!chip->
option.FT2_fast_mode) {
1887 #ifdef SD_XD_IO_FOLLOW_PWR
1889 || chip->
option.rts5129_D3318_off_enable)
1895 #ifdef SD_XD_IO_FOLLOW_PWR
1897 || chip->
option.rts5129_D3318_off_enable) {
1898 rts51x_init_cmd(chip);
1900 sd_pull_ctl_enable(chip);
1934 rts51x_init_cmd(chip);
1936 sd_pull_ctl_enable(chip);
1944 #ifdef SD_XD_IO_FOLLOW_PWR
1954 static int sd_dummy_clock(
struct rts51x_chip *chip)
1966 int retval, i = 0, j = 0,
k = 0, hi_cap_flow = 0;
1967 int sd_dont_switch = 0;
1968 int support_1v8 = 0;
1970 u8 switch_bus_width;
1984 retval = sd_prepare_reset(chip);
1988 sd_dummy_clock(chip);
2004 if ((rsp[4] == 0xAA) && ((rsp[3] & 0x0f) == 0x01)) {
2057 if (chip->
option.speed_mmc) {
2083 }
while (!(rsp[1] & 0x80) && (i < 255));
2097 support_1v8 = (rsp[1] & 0x01) ? 1 : 0;
2111 retval = sd_voltage_switch(chip);
2114 sd_init_power(chip);
2138 retval = sd_check_csd(chip, 1);
2185 if (!(sd_card->
raw_csd[4] & 0x40)) {
2190 if (!sd_dont_switch) {
2192 retval = sd_check_spec(chip, switch_bus_width);
2194 retval = sd_switch_function(chip, switch_bus_width);
2202 sd_init_power(chip);
2213 sd_init_power(chip);
2240 retval = sd_set_init_para(chip);
2245 retval = sd_ddr_tuning(chip);
2247 retval = sd_sdr_tuning(chip);
2252 sd_init_power(chip);
2257 sd_wait_currentstate_dataready(chip, 0x08, 1, 20)) {
2269 "goto SD20 mode\n");
2270 sd_init_power(chip);
2292 sd_card_type = ((
u16) buf[2] << 8) | (
u16) buf[3];
2294 if ((sd_card_type == 0x0001)
2295 || (sd_card_type == 0x0002))
2298 rts51x_clear_sd_error(chip);
2303 rts51x_clear_sd_error(chip);
2347 NULL, 0, byte_cnt, 1, bus_width, buf, len, 100);
2349 u8 val1 = 0, val2 = 0;
2352 rts51x_clear_sd_error(chip);
2353 if ((val1 & 0xE0) || val2)
2358 rts51x_init_cmd(chip);
2395 rts51x_clear_sd_error(chip);
2399 rts51x_read_rsp_buf(chip, 1, buf, 2);
2404 if ((buf[0] == 0xAA) && (buf[1] == 0x55)) {
2414 sd_send_cmd_get_rsp(chip,
SWITCH, arg,
2422 if (buf[0] == 0xA5) {
2432 sd_send_cmd_get_rsp(chip,
SWITCH, arg,
2443 static int mmc_switch_timing_bus(
struct rts51x_chip *chip)
2454 rts51x_init_cmd(chip);
2493 rts51x_clear_sd_error(chip);
2500 rts51x_read_rsp_buf(chip, 0, buf, 6);
2509 ((
u32) buf[5] << 24) | ((
u32) buf[4] << 16) |
2510 ((
u32) buf[3] << 8) | ((
u32) buf[2]);
2512 card_type_mask = 0x07;
2514 card_type_mask = 0x03;
2516 card_type = buf[1] & card_type_mask;
2521 if (card_type & 0x04)
2523 else if (card_type & 0x02)
2529 sd_send_cmd_get_rsp(chip,
SWITCH, 0x03B90100,
2534 sd_choose_proper_clock(chip);
2535 retval = switch_clock(chip, sd_card->
sd_clock);
2557 int retval, i = 0, j = 0,
k = 0;
2560 u8 change_to_ddr52 = 1;
2565 retval = sd_prepare_reset(chip);
2602 if (sd_check_err_code(chip,
SD_BUSY)
2603 || sd_check_err_code(chip,
SD_TO_ERR)) {
2606 sd_clr_err_code(chip);
2614 sd_clr_err_code(chip);
2624 }
while (!(rsp[1] & 0x80) && (i < 100));
2631 if ((rsp[1] & 0x60) == 0x40)
2643 sd_card->
sd_addr = 0x00100000;
2652 retval = sd_check_csd(chip, 1);
2656 spec_ver = (sd_card->
raw_csd[0] & 0x3C) >> 2;
2678 if (spec_ver == 4) {
2680 (
void)mmc_switch_timing_bus(chip);
2691 retval = sd_set_init_para(chip);
2694 sd_init_power(chip);
2695 change_to_ddr52 = 0;
2699 retval = mmc_ddr_tuning(chip);
2702 sd_init_power(chip);
2703 change_to_ddr52 = 0;
2708 sd_wait_currentstate_dataready(chip, 0x08, 1, 20)) {
2733 change_to_ddr52 = 0;
2735 "goto SD20 mode\n");
2736 sd_init_power(chip);
2766 sd_clear_reset_fail(chip);
2769 sd_init_power(chip);
2772 for (i = 0; i < 3; i++) {
2773 if (!chip->
option.reset_mmc_first) {
2780 sd_clear_reset_fail(chip);
2782 retval = reset_mmc(chip);
2785 retval = reset_mmc(chip);
2788 sd_clear_reset_fail(chip);
2814 sd_clear_reset_fail(chip);
2818 rts51x_init_cmd(chip);
2828 if (chip->
option.reset_or_rw_fail_set_pad_drive) {
2837 if (chip->
option.sd_send_status_en) {
2849 retval = sd_set_init_para(chip);
2858 #define WAIT_DATA_READY_RTY_CNT 255
2860 static int wait_data_buf_ready(
struct rts51x_chip *chip)
2888 static void sd_stop_seq_mode(
struct rts51x_chip *chip)
2909 static inline int sd_auto_tune_clock(
struct rts51x_chip *chip)
2946 data_addr = start_sector << 9;
2952 sd_clr_err_code(chip);
2977 TRACE_RET(chip, sd_parse_err_code(chip));
2995 rts51x_init_cmd(chip);
3002 (
u8) (sector_cnt >> 8));
3048 (
u8) (data_addr >> 24));
3050 (
u8) (data_addr >> 16));
3052 (
u8) (data_addr >> 8));
3075 rts51x_clear_sd_error(chip);
3078 TRACE_RET(chip, sd_parse_err_code(chip));
3081 retval = wait_data_buf_ready(chip);
3084 TRACE_RET(chip, sd_parse_err_code(chip));
3092 TRACE_RET(chip, sd_parse_err_code(chip));
3095 rts51x_init_cmd(chip);
3128 scsi_bufflen(srb), scsi_sg_count(srb),
3129 NULL, 10000, stageflag);
3134 sd_print_debug_reg(chip);
3139 rts51x_clear_sd_error(chip);
3151 sd_auto_tune_clock(chip);
3160 rts51x_clear_sd_error(chip);
3177 sd_stop_seq_mode(chip);
3182 static inline void sd_fill_power_off_card3v3(
struct rts51x_chip *chip)
3187 if (!chip->
option.FT2_fast_mode) {
3188 #ifdef SD_XD_IO_FOLLOW_PWR
3190 || chip->
option.rts5129_D3318_off_enable)
3204 static int sd_power_off_card3v3(
struct rts51x_chip *chip)
3208 rts51x_init_cmd(chip);
3210 sd_fill_power_off_card3v3(chip);
3215 #ifdef SD_XD_IO_FOLLOW_PWR
3216 if (!chip->
option.FT2_fast_mode)
3240 sd_power_off_card3v3(chip);
3242 rts51x_init_cmd(chip);
3244 sd_fill_power_off_card3v3(chip);
3247 sd_pull_ctl_disable(chip);