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 inline void ms_set_err_code(struct rtsx_chip *chip, u8 err_code)
36 struct ms_info *ms_card = &(chip->ms_card);
38 ms_card->err_code = err_code;
41 static inline int ms_check_err_code(struct rtsx_chip *chip, u8 err_code)
43 struct ms_info *ms_card = &(chip->ms_card);
45 return (ms_card->err_code == err_code);
48 static int ms_parse_err_code(struct rtsx_chip *chip)
50 TRACE_RET(chip, STATUS_FAIL);
53 static int ms_transfer_tpc(struct rtsx_chip *chip, u8 trans_mode,
54 u8 tpc, u8 cnt, u8 cfg)
56 struct ms_info *ms_card = &(chip->ms_card);
60 dev_dbg(rtsx_dev(chip), "%s: tpc = 0x%x\n", __func__, tpc);
64 rtsx_add_cmd(chip, WRITE_REG_CMD, MS_TPC, 0xFF, tpc);
65 rtsx_add_cmd(chip, WRITE_REG_CMD, MS_BYTE_CNT, 0xFF, cnt);
66 rtsx_add_cmd(chip, WRITE_REG_CMD, MS_TRANS_CFG, 0xFF, cfg);
67 rtsx_add_cmd(chip, WRITE_REG_CMD, CARD_DATA_SOURCE,
68 0x01, PINGPONG_BUFFER);
70 rtsx_add_cmd(chip, WRITE_REG_CMD, MS_TRANSFER,
71 0xFF, MS_TRANSFER_START | trans_mode);
72 rtsx_add_cmd(chip, CHECK_REG_CMD, MS_TRANSFER,
73 MS_TRANSFER_END, MS_TRANSFER_END);
75 rtsx_add_cmd(chip, READ_REG_CMD, MS_TRANS_CFG, 0, 0);
77 retval = rtsx_send_cmd(chip, MS_CARD, 5000);
79 rtsx_clear_ms_error(chip);
80 ms_set_err_code(chip, MS_TO_ERROR);
81 TRACE_RET(chip, ms_parse_err_code(chip));
84 ptr = rtsx_get_cmd_data(chip) + 1;
86 if (!(tpc & 0x08)) { /* Read Packet */
87 if (*ptr & MS_CRC16_ERR) {
88 ms_set_err_code(chip, MS_CRC16_ERROR);
89 TRACE_RET(chip, ms_parse_err_code(chip));
91 } else { /* Write Packet */
92 if (CHK_MSPRO(ms_card) && !(*ptr & 0x80)) {
93 if (*ptr & (MS_INT_ERR | MS_INT_CMDNK)) {
94 ms_set_err_code(chip, MS_CMD_NK);
95 TRACE_RET(chip, ms_parse_err_code(chip));
100 if (*ptr & MS_RDY_TIMEOUT) {
101 rtsx_clear_ms_error(chip);
102 ms_set_err_code(chip, MS_TO_ERROR);
103 TRACE_RET(chip, ms_parse_err_code(chip));
106 return STATUS_SUCCESS;
109 static int ms_transfer_data(struct rtsx_chip *chip, u8 trans_mode,
110 u8 tpc, u16 sec_cnt, u8 cfg, int mode_2k,
111 int use_sg, void *buf, int buf_len)
114 u8 val, err_code = 0;
115 enum dma_data_direction dir;
117 if (!buf || !buf_len)
118 TRACE_RET(chip, STATUS_FAIL);
120 if (trans_mode == MS_TM_AUTO_READ) {
121 dir = DMA_FROM_DEVICE;
122 err_code = MS_FLASH_READ_ERROR;
123 } else if (trans_mode == MS_TM_AUTO_WRITE) {
125 err_code = MS_FLASH_WRITE_ERROR;
127 TRACE_RET(chip, STATUS_FAIL);
132 rtsx_add_cmd(chip, WRITE_REG_CMD, MS_TPC, 0xFF, tpc);
133 rtsx_add_cmd(chip, WRITE_REG_CMD,
134 MS_SECTOR_CNT_H, 0xFF, (u8)(sec_cnt >> 8));
135 rtsx_add_cmd(chip, WRITE_REG_CMD, MS_SECTOR_CNT_L, 0xFF, (u8)sec_cnt);
136 rtsx_add_cmd(chip, WRITE_REG_CMD, MS_TRANS_CFG, 0xFF, cfg);
139 rtsx_add_cmd(chip, WRITE_REG_CMD,
140 MS_CFG, MS_2K_SECTOR_MODE, MS_2K_SECTOR_MODE);
142 rtsx_add_cmd(chip, WRITE_REG_CMD, MS_CFG, MS_2K_SECTOR_MODE, 0);
145 trans_dma_enable(dir, chip, sec_cnt * 512, DMA_512);
147 rtsx_add_cmd(chip, WRITE_REG_CMD,
148 MS_TRANSFER, 0xFF, MS_TRANSFER_START | trans_mode);
149 rtsx_add_cmd(chip, CHECK_REG_CMD,
150 MS_TRANSFER, MS_TRANSFER_END, MS_TRANSFER_END);
152 rtsx_send_cmd_no_wait(chip);
154 retval = rtsx_transfer_data(chip, MS_CARD, buf, buf_len,
155 use_sg, dir, chip->mspro_timeout);
157 ms_set_err_code(chip, err_code);
158 if (retval == -ETIMEDOUT)
159 retval = STATUS_TIMEDOUT;
161 retval = STATUS_FAIL;
163 TRACE_RET(chip, retval);
166 RTSX_READ_REG(chip, MS_TRANS_CFG, &val);
167 if (val & (MS_INT_CMDNK | MS_INT_ERR | MS_CRC16_ERR | MS_RDY_TIMEOUT))
168 TRACE_RET(chip, STATUS_FAIL);
170 return STATUS_SUCCESS;
173 static int ms_write_bytes(struct rtsx_chip *chip,
174 u8 tpc, u8 cnt, u8 cfg, u8 *data, int data_len)
176 struct ms_info *ms_card = &(chip->ms_card);
179 if (!data || (data_len < cnt))
180 TRACE_RET(chip, STATUS_ERROR);
184 for (i = 0; i < cnt; i++) {
185 rtsx_add_cmd(chip, WRITE_REG_CMD,
186 PPBUF_BASE2 + i, 0xFF, data[i]);
189 rtsx_add_cmd(chip, WRITE_REG_CMD, PPBUF_BASE2 + i, 0xFF, 0xFF);
191 rtsx_add_cmd(chip, WRITE_REG_CMD, MS_TPC, 0xFF, tpc);
192 rtsx_add_cmd(chip, WRITE_REG_CMD, MS_BYTE_CNT, 0xFF, cnt);
193 rtsx_add_cmd(chip, WRITE_REG_CMD, MS_TRANS_CFG, 0xFF, cfg);
194 rtsx_add_cmd(chip, WRITE_REG_CMD, CARD_DATA_SOURCE,
195 0x01, PINGPONG_BUFFER);
197 rtsx_add_cmd(chip, WRITE_REG_CMD,
198 MS_TRANSFER, 0xFF, MS_TRANSFER_START | MS_TM_WRITE_BYTES);
199 rtsx_add_cmd(chip, CHECK_REG_CMD,
200 MS_TRANSFER, MS_TRANSFER_END, MS_TRANSFER_END);
202 retval = rtsx_send_cmd(chip, MS_CARD, 5000);
206 rtsx_read_register(chip, MS_TRANS_CFG, &val);
207 dev_dbg(rtsx_dev(chip), "MS_TRANS_CFG: 0x%02x\n", val);
209 rtsx_clear_ms_error(chip);
212 if (val & MS_CRC16_ERR) {
213 ms_set_err_code(chip, MS_CRC16_ERROR);
214 TRACE_RET(chip, ms_parse_err_code(chip));
217 if (CHK_MSPRO(ms_card) && !(val & 0x80)) {
218 if (val & (MS_INT_ERR | MS_INT_CMDNK)) {
219 ms_set_err_code(chip, MS_CMD_NK);
221 ms_parse_err_code(chip));
226 if (val & MS_RDY_TIMEOUT) {
227 ms_set_err_code(chip, MS_TO_ERROR);
228 TRACE_RET(chip, ms_parse_err_code(chip));
231 ms_set_err_code(chip, MS_TO_ERROR);
232 TRACE_RET(chip, ms_parse_err_code(chip));
235 return STATUS_SUCCESS;
238 static int ms_read_bytes(struct rtsx_chip *chip,
239 u8 tpc, u8 cnt, u8 cfg, u8 *data, int data_len)
241 struct ms_info *ms_card = &(chip->ms_card);
246 TRACE_RET(chip, STATUS_ERROR);
250 rtsx_add_cmd(chip, WRITE_REG_CMD, MS_TPC, 0xFF, tpc);
251 rtsx_add_cmd(chip, WRITE_REG_CMD, MS_BYTE_CNT, 0xFF, cnt);
252 rtsx_add_cmd(chip, WRITE_REG_CMD, MS_TRANS_CFG, 0xFF, cfg);
253 rtsx_add_cmd(chip, WRITE_REG_CMD, CARD_DATA_SOURCE,
254 0x01, PINGPONG_BUFFER);
256 rtsx_add_cmd(chip, WRITE_REG_CMD, MS_TRANSFER, 0xFF,
257 MS_TRANSFER_START | MS_TM_READ_BYTES);
258 rtsx_add_cmd(chip, CHECK_REG_CMD, MS_TRANSFER,
259 MS_TRANSFER_END, MS_TRANSFER_END);
261 for (i = 0; i < data_len - 1; i++)
262 rtsx_add_cmd(chip, READ_REG_CMD, PPBUF_BASE2 + i, 0, 0);
265 rtsx_add_cmd(chip, READ_REG_CMD, PPBUF_BASE2 + data_len, 0, 0);
267 rtsx_add_cmd(chip, READ_REG_CMD, PPBUF_BASE2 + data_len - 1,
270 retval = rtsx_send_cmd(chip, MS_CARD, 5000);
274 rtsx_read_register(chip, MS_TRANS_CFG, &val);
275 rtsx_clear_ms_error(chip);
278 if (val & MS_CRC16_ERR) {
279 ms_set_err_code(chip, MS_CRC16_ERROR);
280 TRACE_RET(chip, ms_parse_err_code(chip));
283 if (CHK_MSPRO(ms_card) && !(val & 0x80)) {
284 if (val & (MS_INT_ERR | MS_INT_CMDNK)) {
285 ms_set_err_code(chip, MS_CMD_NK);
287 ms_parse_err_code(chip));
292 if (val & MS_RDY_TIMEOUT) {
293 ms_set_err_code(chip, MS_TO_ERROR);
294 TRACE_RET(chip, ms_parse_err_code(chip));
297 ms_set_err_code(chip, MS_TO_ERROR);
298 TRACE_RET(chip, ms_parse_err_code(chip));
301 ptr = rtsx_get_cmd_data(chip) + 1;
303 for (i = 0; i < data_len; i++)
306 if ((tpc == PRO_READ_SHORT_DATA) && (data_len == 8)) {
307 dev_dbg(rtsx_dev(chip), "Read format progress:\n");
308 print_hex_dump_bytes(KBUILD_MODNAME ": ", DUMP_PREFIX_NONE, ptr,
312 return STATUS_SUCCESS;
315 static int ms_set_rw_reg_addr(struct rtsx_chip *chip,
316 u8 read_start, u8 read_cnt, u8 write_start, u8 write_cnt)
321 data[0] = read_start;
323 data[2] = write_start;
326 for (i = 0; i < MS_MAX_RETRY_COUNT; i++) {
327 retval = ms_write_bytes(chip, SET_RW_REG_ADRS, 4,
328 NO_WAIT_INT, data, 4);
329 if (retval == STATUS_SUCCESS)
330 return STATUS_SUCCESS;
331 rtsx_clear_ms_error(chip);
334 TRACE_RET(chip, STATUS_FAIL);
337 static int ms_send_cmd(struct rtsx_chip *chip, u8 cmd, u8 cfg)
344 return ms_write_bytes(chip, PRO_SET_CMD, 1, cfg, data, 1);
347 static int ms_set_init_para(struct rtsx_chip *chip)
349 struct ms_info *ms_card = &(chip->ms_card);
352 if (CHK_HG8BIT(ms_card)) {
354 ms_card->ms_clock = chip->asic_ms_hg_clk;
356 ms_card->ms_clock = chip->fpga_ms_hg_clk;
358 } else if (CHK_MSPRO(ms_card) || CHK_MS4BIT(ms_card)) {
360 ms_card->ms_clock = chip->asic_ms_4bit_clk;
362 ms_card->ms_clock = chip->fpga_ms_4bit_clk;
366 ms_card->ms_clock = chip->asic_ms_1bit_clk;
368 ms_card->ms_clock = chip->fpga_ms_1bit_clk;
371 retval = switch_clock(chip, ms_card->ms_clock);
372 if (retval != STATUS_SUCCESS)
373 TRACE_RET(chip, STATUS_FAIL);
375 retval = select_card(chip, MS_CARD);
376 if (retval != STATUS_SUCCESS)
377 TRACE_RET(chip, STATUS_FAIL);
379 return STATUS_SUCCESS;
382 static int ms_switch_clock(struct rtsx_chip *chip)
384 struct ms_info *ms_card = &(chip->ms_card);
387 retval = select_card(chip, MS_CARD);
388 if (retval != STATUS_SUCCESS)
389 TRACE_RET(chip, STATUS_FAIL);
391 retval = switch_clock(chip, ms_card->ms_clock);
392 if (retval != STATUS_SUCCESS)
393 TRACE_RET(chip, STATUS_FAIL);
395 return STATUS_SUCCESS;
398 static int ms_pull_ctl_disable(struct rtsx_chip *chip)
400 if (CHECK_PID(chip, 0x5208)) {
401 RTSX_WRITE_REG(chip, CARD_PULL_CTL1, 0xFF,
402 MS_D1_PD | MS_D2_PD | MS_CLK_PD | MS_D6_PD);
403 RTSX_WRITE_REG(chip, CARD_PULL_CTL2, 0xFF,
404 MS_D3_PD | MS_D0_PD | MS_BS_PD | XD_D4_PD);
405 RTSX_WRITE_REG(chip, CARD_PULL_CTL3, 0xFF,
406 MS_D7_PD | XD_CE_PD | XD_CLE_PD | XD_CD_PU);
407 RTSX_WRITE_REG(chip, CARD_PULL_CTL4, 0xFF,
408 XD_RDY_PD | SD_D3_PD | SD_D2_PD | XD_ALE_PD);
409 RTSX_WRITE_REG(chip, CARD_PULL_CTL5, 0xFF,
410 MS_INS_PU | SD_WP_PD | SD_CD_PU | SD_CMD_PD);
411 RTSX_WRITE_REG(chip, CARD_PULL_CTL6, 0xFF,
412 MS_D5_PD | MS_D4_PD);
413 } else if (CHECK_PID(chip, 0x5288)) {
414 if (CHECK_BARO_PKG(chip, QFN)) {
415 RTSX_WRITE_REG(chip, CARD_PULL_CTL1, 0xFF, 0x55);
416 RTSX_WRITE_REG(chip, CARD_PULL_CTL2, 0xFF, 0x55);
417 RTSX_WRITE_REG(chip, CARD_PULL_CTL3, 0xFF, 0x4B);
418 RTSX_WRITE_REG(chip, CARD_PULL_CTL4, 0xFF, 0x69);
422 return STATUS_SUCCESS;
425 static int ms_pull_ctl_enable(struct rtsx_chip *chip)
431 if (CHECK_PID(chip, 0x5208)) {
432 rtsx_add_cmd(chip, WRITE_REG_CMD, CARD_PULL_CTL1, 0xFF,
433 MS_D1_PD | MS_D2_PD | MS_CLK_NP | MS_D6_PD);
434 rtsx_add_cmd(chip, WRITE_REG_CMD, CARD_PULL_CTL2, 0xFF,
435 MS_D3_PD | MS_D0_PD | MS_BS_NP | XD_D4_PD);
436 rtsx_add_cmd(chip, WRITE_REG_CMD, CARD_PULL_CTL3, 0xFF,
437 MS_D7_PD | XD_CE_PD | XD_CLE_PD | XD_CD_PU);
438 rtsx_add_cmd(chip, WRITE_REG_CMD, CARD_PULL_CTL4, 0xFF,
439 XD_RDY_PD | SD_D3_PD | SD_D2_PD | XD_ALE_PD);
440 rtsx_add_cmd(chip, WRITE_REG_CMD, CARD_PULL_CTL5, 0xFF,
441 MS_INS_PU | SD_WP_PD | SD_CD_PU | SD_CMD_PD);
442 rtsx_add_cmd(chip, WRITE_REG_CMD, CARD_PULL_CTL6, 0xFF,
443 MS_D5_PD | MS_D4_PD);
444 } else if (CHECK_PID(chip, 0x5288)) {
445 if (CHECK_BARO_PKG(chip, QFN)) {
446 rtsx_add_cmd(chip, WRITE_REG_CMD,
447 CARD_PULL_CTL1, 0xFF, 0x55);
448 rtsx_add_cmd(chip, WRITE_REG_CMD,
449 CARD_PULL_CTL2, 0xFF, 0x45);
450 rtsx_add_cmd(chip, WRITE_REG_CMD,
451 CARD_PULL_CTL3, 0xFF, 0x4B);
452 rtsx_add_cmd(chip, WRITE_REG_CMD,
453 CARD_PULL_CTL4, 0xFF, 0x29);
457 retval = rtsx_send_cmd(chip, MS_CARD, 100);
459 TRACE_RET(chip, STATUS_FAIL);
461 return STATUS_SUCCESS;
464 static int ms_prepare_reset(struct rtsx_chip *chip)
466 struct ms_info *ms_card = &(chip->ms_card);
470 ms_card->ms_type = 0;
471 ms_card->check_ms_flow = 0;
472 ms_card->switch_8bit_fail = 0;
473 ms_card->delay_write.delay_write_flag = 0;
475 ms_card->pro_under_formatting = 0;
477 retval = ms_power_off_card3v3(chip);
478 if (retval != STATUS_SUCCESS)
479 TRACE_RET(chip, STATUS_FAIL);
481 if (!chip->ft2_fast_mode)
484 retval = enable_card_clock(chip, MS_CARD);
485 if (retval != STATUS_SUCCESS)
486 TRACE_RET(chip, STATUS_FAIL);
488 if (chip->asic_code) {
489 retval = ms_pull_ctl_enable(chip);
490 if (retval != STATUS_SUCCESS)
491 TRACE_RET(chip, STATUS_FAIL);
493 RTSX_WRITE_REG(chip, FPGA_PULL_CTL,
494 FPGA_MS_PULL_CTL_BIT | 0x20, 0);
497 if (!chip->ft2_fast_mode) {
498 retval = card_power_on(chip, MS_CARD);
499 if (retval != STATUS_SUCCESS)
500 TRACE_RET(chip, STATUS_FAIL);
505 if (CHECK_LUN_MODE(chip, SD_MS_2LUN))
506 oc_mask = MS_OC_NOW | MS_OC_EVER;
508 oc_mask = SD_OC_NOW | SD_OC_EVER;
510 if (chip->ocp_stat & oc_mask) {
511 dev_dbg(rtsx_dev(chip), "Over current, OCPSTAT is 0x%x\n",
513 TRACE_RET(chip, STATUS_FAIL);
518 RTSX_WRITE_REG(chip, CARD_OE, MS_OUTPUT_EN, MS_OUTPUT_EN);
520 if (chip->asic_code) {
521 RTSX_WRITE_REG(chip, MS_CFG, 0xFF,
522 SAMPLE_TIME_RISING | PUSH_TIME_DEFAULT |
523 NO_EXTEND_TOGGLE | MS_BUS_WIDTH_1);
525 RTSX_WRITE_REG(chip, MS_CFG, 0xFF,
526 SAMPLE_TIME_FALLING | PUSH_TIME_DEFAULT |
527 NO_EXTEND_TOGGLE | MS_BUS_WIDTH_1);
529 RTSX_WRITE_REG(chip, MS_TRANS_CFG,
530 0xFF, NO_WAIT_INT | NO_AUTO_READ_INT_REG);
531 RTSX_WRITE_REG(chip, CARD_STOP,
532 MS_STOP | MS_CLR_ERR, MS_STOP | MS_CLR_ERR);
534 retval = ms_set_init_para(chip);
535 if (retval != STATUS_SUCCESS)
536 TRACE_RET(chip, STATUS_FAIL);
538 return STATUS_SUCCESS;
541 static int ms_identify_media_type(struct rtsx_chip *chip, int switch_8bit_bus)
543 struct ms_info *ms_card = &(chip->ms_card);
547 retval = ms_set_rw_reg_addr(chip, Pro_StatusReg, 6, SystemParm, 1);
548 if (retval != STATUS_SUCCESS)
549 TRACE_RET(chip, STATUS_FAIL);
551 for (i = 0; i < MS_MAX_RETRY_COUNT; i++) {
552 retval = ms_transfer_tpc(chip, MS_TM_READ_BYTES, READ_REG,
554 if (retval == STATUS_SUCCESS)
557 if (i == MS_MAX_RETRY_COUNT)
558 TRACE_RET(chip, STATUS_FAIL);
560 RTSX_READ_REG(chip, PPBUF_BASE2 + 2, &val);
561 dev_dbg(rtsx_dev(chip), "Type register: 0x%x\n", val);
564 ms_card->check_ms_flow = 1;
566 TRACE_RET(chip, STATUS_FAIL);
569 RTSX_READ_REG(chip, PPBUF_BASE2 + 4, &val);
570 dev_dbg(rtsx_dev(chip), "Category register: 0x%x\n", val);
572 ms_card->check_ms_flow = 1;
573 TRACE_RET(chip, STATUS_FAIL);
576 RTSX_READ_REG(chip, PPBUF_BASE2 + 5, &val);
577 dev_dbg(rtsx_dev(chip), "Class register: 0x%x\n", val);
579 RTSX_READ_REG(chip, PPBUF_BASE2, &val);
581 chip->card_wp |= MS_CARD;
583 chip->card_wp &= ~MS_CARD;
585 } else if ((val == 0x01) || (val == 0x02) || (val == 0x03)) {
586 chip->card_wp |= MS_CARD;
588 ms_card->check_ms_flow = 1;
589 TRACE_RET(chip, STATUS_FAIL);
592 ms_card->ms_type |= TYPE_MSPRO;
594 RTSX_READ_REG(chip, PPBUF_BASE2 + 3, &val);
595 dev_dbg(rtsx_dev(chip), "IF Mode register: 0x%x\n", val);
597 ms_card->ms_type &= 0x0F;
598 } else if (val == 7) {
600 ms_card->ms_type |= MS_HG;
602 ms_card->ms_type &= 0x0F;
605 TRACE_RET(chip, STATUS_FAIL);
608 return STATUS_SUCCESS;
611 static int ms_confirm_cpu_startup(struct rtsx_chip *chip)
616 /* Confirm CPU StartUp */
619 if (detect_card_cd(chip, MS_CARD) != STATUS_SUCCESS) {
620 ms_set_err_code(chip, MS_NO_CARD);
621 TRACE_RET(chip, STATUS_FAIL);
624 for (i = 0; i < MS_MAX_RETRY_COUNT; i++) {
625 retval = ms_read_bytes(chip, GET_INT, 1,
626 NO_WAIT_INT, &val, 1);
627 if (retval == STATUS_SUCCESS)
630 if (i == MS_MAX_RETRY_COUNT)
631 TRACE_RET(chip, STATUS_FAIL);
634 TRACE_RET(chip, STATUS_FAIL);
638 } while (!(val & INT_REG_CED));
640 for (i = 0; i < MS_MAX_RETRY_COUNT; i++) {
641 retval = ms_read_bytes(chip, GET_INT, 1, NO_WAIT_INT, &val, 1);
642 if (retval == STATUS_SUCCESS)
645 if (i == MS_MAX_RETRY_COUNT)
646 TRACE_RET(chip, STATUS_FAIL);
648 if (val & INT_REG_ERR) {
649 if (val & INT_REG_CMDNK)
650 chip->card_wp |= (MS_CARD);
652 TRACE_RET(chip, STATUS_FAIL);
654 /* -- end confirm CPU startup */
656 return STATUS_SUCCESS;
659 static int ms_switch_parallel_bus(struct rtsx_chip *chip)
664 data[0] = PARALLEL_4BIT_IF;
666 for (i = 0; i < MS_MAX_RETRY_COUNT; i++) {
667 retval = ms_write_bytes(chip, WRITE_REG, 1, NO_WAIT_INT,
669 if (retval == STATUS_SUCCESS)
672 if (retval != STATUS_SUCCESS)
673 TRACE_RET(chip, STATUS_FAIL);
675 return STATUS_SUCCESS;
678 static int ms_switch_8bit_bus(struct rtsx_chip *chip)
680 struct ms_info *ms_card = &(chip->ms_card);
684 data[0] = PARALLEL_8BIT_IF;
686 for (i = 0; i < MS_MAX_RETRY_COUNT; i++) {
687 retval = ms_write_bytes(chip, WRITE_REG, 1,
688 NO_WAIT_INT, data, 2);
689 if (retval == STATUS_SUCCESS)
692 if (retval != STATUS_SUCCESS)
693 TRACE_RET(chip, STATUS_FAIL);
695 RTSX_WRITE_REG(chip, MS_CFG, 0x98,
696 MS_BUS_WIDTH_8 | SAMPLE_TIME_FALLING);
697 ms_card->ms_type |= MS_8BIT;
698 retval = ms_set_init_para(chip);
699 if (retval != STATUS_SUCCESS)
700 TRACE_RET(chip, STATUS_FAIL);
702 for (i = 0; i < MS_MAX_RETRY_COUNT; i++) {
703 retval = ms_transfer_tpc(chip, MS_TM_READ_BYTES, GET_INT,
705 if (retval != STATUS_SUCCESS)
706 TRACE_RET(chip, STATUS_FAIL);
709 return STATUS_SUCCESS;
712 static int ms_pro_reset_flow(struct rtsx_chip *chip, int switch_8bit_bus)
714 struct ms_info *ms_card = &(chip->ms_card);
717 for (i = 0; i < 3; i++) {
718 retval = ms_prepare_reset(chip);
719 if (retval != STATUS_SUCCESS)
720 TRACE_RET(chip, STATUS_FAIL);
722 retval = ms_identify_media_type(chip, switch_8bit_bus);
723 if (retval != STATUS_SUCCESS)
724 TRACE_RET(chip, STATUS_FAIL);
726 retval = ms_confirm_cpu_startup(chip);
727 if (retval != STATUS_SUCCESS)
728 TRACE_RET(chip, STATUS_FAIL);
730 retval = ms_switch_parallel_bus(chip);
731 if (retval != STATUS_SUCCESS) {
732 if (detect_card_cd(chip, MS_CARD) != STATUS_SUCCESS) {
733 ms_set_err_code(chip, MS_NO_CARD);
734 TRACE_RET(chip, STATUS_FAIL);
742 if (retval != STATUS_SUCCESS)
743 TRACE_RET(chip, STATUS_FAIL);
745 /* Switch MS-PRO into Parallel mode */
746 RTSX_WRITE_REG(chip, MS_CFG, 0x18, MS_BUS_WIDTH_4);
747 RTSX_WRITE_REG(chip, MS_CFG, PUSH_TIME_ODD, PUSH_TIME_ODD);
749 retval = ms_set_init_para(chip);
750 if (retval != STATUS_SUCCESS)
751 TRACE_RET(chip, STATUS_FAIL);
753 /* If MSPro HG Card, We shall try to switch to 8-bit bus */
754 if (CHK_MSHG(ms_card) && chip->support_ms_8bit && switch_8bit_bus) {
755 retval = ms_switch_8bit_bus(chip);
756 if (retval != STATUS_SUCCESS) {
757 ms_card->switch_8bit_fail = 1;
758 TRACE_RET(chip, STATUS_FAIL);
762 return STATUS_SUCCESS;
766 static int msxc_change_power(struct rtsx_chip *chip, u8 mode)
771 ms_cleanup_work(chip);
773 retval = ms_set_rw_reg_addr(chip, 0, 0, Pro_DataCount1, 6);
774 if (retval != STATUS_SUCCESS)
775 TRACE_RET(chip, STATUS_FAIL);
784 retval = ms_write_bytes(chip, PRO_WRITE_REG, 6, NO_WAIT_INT, buf, 6);
785 if (retval != STATUS_SUCCESS)
786 TRACE_RET(chip, STATUS_FAIL);
788 retval = ms_send_cmd(chip, XC_CHG_POWER, WAIT_INT);
789 if (retval != STATUS_SUCCESS)
790 TRACE_RET(chip, STATUS_FAIL);
792 RTSX_READ_REG(chip, MS_TRANS_CFG, buf);
793 if (buf[0] & (MS_INT_CMDNK | MS_INT_ERR))
794 TRACE_RET(chip, STATUS_FAIL);
796 return STATUS_SUCCESS;
800 static int ms_read_attribute_info(struct rtsx_chip *chip)
802 struct ms_info *ms_card = &(chip->ms_card);
804 u8 val, *buf, class_code, device_type, sub_class, data[16];
805 u16 total_blk = 0, blk_size = 0;
807 u32 xc_total_blk = 0, xc_blk_size = 0;
809 u32 sys_info_addr = 0, sys_info_size;
810 #ifdef SUPPORT_PCGL_1P18
811 u32 model_name_addr = 0, model_name_size;
812 int found_sys_info = 0, found_model_name = 0;
815 retval = ms_set_rw_reg_addr(chip, Pro_IntReg, 2, Pro_SystemParm, 7);
816 if (retval != STATUS_SUCCESS)
817 TRACE_RET(chip, STATUS_FAIL);
819 if (CHK_MS8BIT(ms_card))
820 data[0] = PARALLEL_8BIT_IF;
822 data[0] = PARALLEL_4BIT_IF;
833 for (i = 0; i < MS_MAX_RETRY_COUNT; i++) {
834 retval = ms_write_bytes(chip, PRO_WRITE_REG, 7, NO_WAIT_INT,
836 if (retval == STATUS_SUCCESS)
839 if (retval != STATUS_SUCCESS)
840 TRACE_RET(chip, STATUS_FAIL);
842 buf = kmalloc(64 * 512, GFP_KERNEL);
844 TRACE_RET(chip, STATUS_ERROR);
846 for (i = 0; i < MS_MAX_RETRY_COUNT; i++) {
847 retval = ms_send_cmd(chip, PRO_READ_ATRB, WAIT_INT);
848 if (retval != STATUS_SUCCESS)
851 retval = rtsx_read_register(chip, MS_TRANS_CFG, &val);
852 if (retval != STATUS_SUCCESS) {
854 TRACE_RET(chip, STATUS_FAIL);
856 if (!(val & MS_INT_BREQ)) {
858 TRACE_RET(chip, STATUS_FAIL);
860 retval = ms_transfer_data(chip, MS_TM_AUTO_READ,
861 PRO_READ_LONG_DATA, 0x40, WAIT_INT,
862 0, 0, buf, 64 * 512);
863 if (retval == STATUS_SUCCESS)
866 rtsx_clear_ms_error(chip);
868 if (retval != STATUS_SUCCESS) {
870 TRACE_RET(chip, STATUS_FAIL);
875 retval = rtsx_read_register(chip, MS_TRANS_CFG, &val);
876 if (retval != STATUS_SUCCESS) {
878 TRACE_RET(chip, STATUS_FAIL);
881 if ((val & MS_INT_CED) || !(val & MS_INT_BREQ))
884 retval = ms_transfer_tpc(chip, MS_TM_NORMAL_READ,
885 PRO_READ_LONG_DATA, 0, WAIT_INT);
886 if (retval != STATUS_SUCCESS) {
888 TRACE_RET(chip, STATUS_FAIL);
894 if (retval != STATUS_SUCCESS) {
896 TRACE_RET(chip, STATUS_FAIL);
899 if ((buf[0] != 0xa5) && (buf[1] != 0xc3)) {
900 /* Signature code is wrong */
902 TRACE_RET(chip, STATUS_FAIL);
905 if ((buf[4] < 1) || (buf[4] > 12)) {
907 TRACE_RET(chip, STATUS_FAIL);
910 for (i = 0; i < buf[4]; i++) {
911 int cur_addr_off = 16 + i * 12;
914 if ((buf[cur_addr_off + 8] == 0x10) ||
915 (buf[cur_addr_off + 8] == 0x13))
917 if (buf[cur_addr_off + 8] == 0x10)
920 sys_info_addr = ((u32)buf[cur_addr_off + 0] << 24) |
921 ((u32)buf[cur_addr_off + 1] << 16) |
922 ((u32)buf[cur_addr_off + 2] << 8) |
923 buf[cur_addr_off + 3];
924 sys_info_size = ((u32)buf[cur_addr_off + 4] << 24) |
925 ((u32)buf[cur_addr_off + 5] << 16) |
926 ((u32)buf[cur_addr_off + 6] << 8) |
927 buf[cur_addr_off + 7];
928 dev_dbg(rtsx_dev(chip), "sys_info_addr = 0x%x, sys_info_size = 0x%x\n",
929 sys_info_addr, sys_info_size);
930 if (sys_info_size != 96) {
932 TRACE_RET(chip, STATUS_FAIL);
934 if (sys_info_addr < 0x1A0) {
936 TRACE_RET(chip, STATUS_FAIL);
938 if ((sys_info_size + sys_info_addr) > 0x8000) {
940 TRACE_RET(chip, STATUS_FAIL);
944 if (buf[cur_addr_off + 8] == 0x13)
945 ms_card->ms_type |= MS_XC;
947 #ifdef SUPPORT_PCGL_1P18
953 #ifdef SUPPORT_PCGL_1P18
954 if (buf[cur_addr_off + 8] == 0x15) {
955 model_name_addr = ((u32)buf[cur_addr_off + 0] << 24) |
956 ((u32)buf[cur_addr_off + 1] << 16) |
957 ((u32)buf[cur_addr_off + 2] << 8) |
958 buf[cur_addr_off + 3];
959 model_name_size = ((u32)buf[cur_addr_off + 4] << 24) |
960 ((u32)buf[cur_addr_off + 5] << 16) |
961 ((u32)buf[cur_addr_off + 6] << 8) |
962 buf[cur_addr_off + 7];
963 dev_dbg(rtsx_dev(chip), "model_name_addr = 0x%x, model_name_size = 0x%x\n",
964 model_name_addr, model_name_size);
965 if (model_name_size != 48) {
967 TRACE_RET(chip, STATUS_FAIL);
969 if (model_name_addr < 0x1A0) {
971 TRACE_RET(chip, STATUS_FAIL);
973 if ((model_name_size + model_name_addr) > 0x8000) {
975 TRACE_RET(chip, STATUS_FAIL);
978 found_model_name = 1;
981 if (found_sys_info && found_model_name)
988 TRACE_RET(chip, STATUS_FAIL);
991 class_code = buf[sys_info_addr + 0];
992 device_type = buf[sys_info_addr + 56];
993 sub_class = buf[sys_info_addr + 46];
995 if (CHK_MSXC(ms_card)) {
996 xc_total_blk = ((u32)buf[sys_info_addr + 6] << 24) |
997 ((u32)buf[sys_info_addr + 7] << 16) |
998 ((u32)buf[sys_info_addr + 8] << 8) |
999 buf[sys_info_addr + 9];
1000 xc_blk_size = ((u32)buf[sys_info_addr + 32] << 24) |
1001 ((u32)buf[sys_info_addr + 33] << 16) |
1002 ((u32)buf[sys_info_addr + 34] << 8) |
1003 buf[sys_info_addr + 35];
1004 dev_dbg(rtsx_dev(chip), "xc_total_blk = 0x%x, xc_blk_size = 0x%x\n",
1005 xc_total_blk, xc_blk_size);
1007 total_blk = ((u16)buf[sys_info_addr + 6] << 8) |
1008 buf[sys_info_addr + 7];
1009 blk_size = ((u16)buf[sys_info_addr + 2] << 8) |
1010 buf[sys_info_addr + 3];
1011 dev_dbg(rtsx_dev(chip), "total_blk = 0x%x, blk_size = 0x%x\n",
1012 total_blk, blk_size);
1015 total_blk = ((u16)buf[sys_info_addr + 6] << 8) | buf[sys_info_addr + 7];
1016 blk_size = ((u16)buf[sys_info_addr + 2] << 8) | buf[sys_info_addr + 3];
1017 dev_dbg(rtsx_dev(chip), "total_blk = 0x%x, blk_size = 0x%x\n",
1018 total_blk, blk_size);
1021 dev_dbg(rtsx_dev(chip), "class_code = 0x%x, device_type = 0x%x, sub_class = 0x%x\n",
1022 class_code, device_type, sub_class);
1024 memcpy(ms_card->raw_sys_info, buf + sys_info_addr, 96);
1025 #ifdef SUPPORT_PCGL_1P18
1026 memcpy(ms_card->raw_model_name, buf + model_name_addr, 48);
1032 if (CHK_MSXC(ms_card)) {
1033 if (class_code != 0x03)
1034 TRACE_RET(chip, STATUS_FAIL);
1036 if (class_code != 0x02)
1037 TRACE_RET(chip, STATUS_FAIL);
1040 if (class_code != 0x02)
1041 TRACE_RET(chip, STATUS_FAIL);
1044 if (device_type != 0x00) {
1045 if ((device_type == 0x01) || (device_type == 0x02) ||
1046 (device_type == 0x03)) {
1047 chip->card_wp |= MS_CARD;
1049 TRACE_RET(chip, STATUS_FAIL);
1053 if (sub_class & 0xC0)
1054 TRACE_RET(chip, STATUS_FAIL);
1056 dev_dbg(rtsx_dev(chip), "class_code: 0x%x, device_type: 0x%x, sub_class: 0x%x\n",
1057 class_code, device_type, sub_class);
1060 if (CHK_MSXC(ms_card)) {
1061 chip->capacity[chip->card2lun[MS_CARD]] =
1062 ms_card->capacity = xc_total_blk * xc_blk_size;
1064 chip->capacity[chip->card2lun[MS_CARD]] =
1065 ms_card->capacity = total_blk * blk_size;
1068 ms_card->capacity = total_blk * blk_size;
1069 chip->capacity[chip->card2lun[MS_CARD]] = ms_card->capacity;
1072 return STATUS_SUCCESS;
1075 #ifdef SUPPORT_MAGIC_GATE
1076 static int mg_set_tpc_para_sub(struct rtsx_chip *chip,
1077 int type, u8 mg_entry_num);
1080 static int reset_ms_pro(struct rtsx_chip *chip)
1082 struct ms_info *ms_card = &(chip->ms_card);
1084 #ifdef XC_POWERCLASS
1085 u8 change_power_class;
1087 if (chip->ms_power_class_en & 0x02)
1088 change_power_class = 2;
1089 else if (chip->ms_power_class_en & 0x01)
1090 change_power_class = 1;
1092 change_power_class = 0;
1095 #ifdef XC_POWERCLASS
1098 retval = ms_pro_reset_flow(chip, 1);
1099 if (retval != STATUS_SUCCESS) {
1100 if (ms_card->switch_8bit_fail) {
1101 retval = ms_pro_reset_flow(chip, 0);
1102 if (retval != STATUS_SUCCESS)
1103 TRACE_RET(chip, STATUS_FAIL);
1105 TRACE_RET(chip, STATUS_FAIL);
1109 retval = ms_read_attribute_info(chip);
1110 if (retval != STATUS_SUCCESS)
1111 TRACE_RET(chip, STATUS_FAIL);
1113 #ifdef XC_POWERCLASS
1114 if (CHK_HG8BIT(ms_card))
1115 change_power_class = 0;
1117 if (change_power_class && CHK_MSXC(ms_card)) {
1118 u8 power_class_en = chip->ms_power_class_en;
1120 dev_dbg(rtsx_dev(chip), "power_class_en = 0x%x\n",
1122 dev_dbg(rtsx_dev(chip), "change_power_class = %d\n",
1123 change_power_class);
1125 if (change_power_class)
1126 power_class_en &= (1 << (change_power_class - 1));
1130 if (power_class_en) {
1131 u8 power_class_mode =
1132 (ms_card->raw_sys_info[46] & 0x18) >> 3;
1133 dev_dbg(rtsx_dev(chip), "power_class_mode = 0x%x",
1135 if (change_power_class > power_class_mode)
1136 change_power_class = power_class_mode;
1137 if (change_power_class) {
1138 retval = msxc_change_power(chip,
1139 change_power_class);
1140 if (retval != STATUS_SUCCESS) {
1141 change_power_class--;
1149 #ifdef SUPPORT_MAGIC_GATE
1150 retval = mg_set_tpc_para_sub(chip, 0, 0);
1151 if (retval != STATUS_SUCCESS)
1152 TRACE_RET(chip, STATUS_FAIL);
1155 if (CHK_HG8BIT(ms_card))
1156 chip->card_bus_width[chip->card2lun[MS_CARD]] = 8;
1158 chip->card_bus_width[chip->card2lun[MS_CARD]] = 4;
1160 return STATUS_SUCCESS;
1163 static int ms_read_status_reg(struct rtsx_chip *chip)
1168 retval = ms_set_rw_reg_addr(chip, StatusReg0, 2, 0, 0);
1169 if (retval != STATUS_SUCCESS)
1170 TRACE_RET(chip, STATUS_FAIL);
1172 retval = ms_read_bytes(chip, READ_REG, 2, NO_WAIT_INT, val, 2);
1173 if (retval != STATUS_SUCCESS)
1174 TRACE_RET(chip, STATUS_FAIL);
1176 if (val[1] & (STS_UCDT | STS_UCEX | STS_UCFG)) {
1177 ms_set_err_code(chip, MS_FLASH_READ_ERROR);
1178 TRACE_RET(chip, STATUS_FAIL);
1181 return STATUS_SUCCESS;
1185 static int ms_read_extra_data(struct rtsx_chip *chip,
1186 u16 block_addr, u8 page_num, u8 *buf, int buf_len)
1188 struct ms_info *ms_card = &(chip->ms_card);
1192 retval = ms_set_rw_reg_addr(chip, OverwriteFlag, MS_EXTRA_SIZE,
1194 if (retval != STATUS_SUCCESS)
1195 TRACE_RET(chip, STATUS_FAIL);
1197 if (CHK_MS4BIT(ms_card)) {
1198 /* Parallel interface */
1201 /* Serial interface */
1205 data[2] = (u8)(block_addr >> 8);
1206 data[3] = (u8)block_addr;
1210 for (i = 0; i < MS_MAX_RETRY_COUNT; i++) {
1211 retval = ms_write_bytes(chip, WRITE_REG, 6, NO_WAIT_INT,
1213 if (retval == STATUS_SUCCESS)
1216 if (i == MS_MAX_RETRY_COUNT)
1217 TRACE_RET(chip, STATUS_FAIL);
1219 ms_set_err_code(chip, MS_NO_ERROR);
1221 for (i = 0; i < MS_MAX_RETRY_COUNT; i++) {
1222 retval = ms_send_cmd(chip, BLOCK_READ, WAIT_INT);
1223 if (retval == STATUS_SUCCESS)
1226 if (i == MS_MAX_RETRY_COUNT)
1227 TRACE_RET(chip, STATUS_FAIL);
1229 ms_set_err_code(chip, MS_NO_ERROR);
1230 retval = ms_read_bytes(chip, GET_INT, 1, NO_WAIT_INT, &val, 1);
1231 if (retval != STATUS_SUCCESS)
1232 TRACE_RET(chip, STATUS_FAIL);
1234 if (val & INT_REG_CMDNK) {
1235 ms_set_err_code(chip, MS_CMD_NK);
1236 TRACE_RET(chip, STATUS_FAIL);
1238 if (val & INT_REG_CED) {
1239 if (val & INT_REG_ERR) {
1240 retval = ms_read_status_reg(chip);
1241 if (retval != STATUS_SUCCESS)
1242 TRACE_RET(chip, STATUS_FAIL);
1244 retval = ms_set_rw_reg_addr(chip, OverwriteFlag,
1245 MS_EXTRA_SIZE, SystemParm, 6);
1246 if (retval != STATUS_SUCCESS)
1247 TRACE_RET(chip, STATUS_FAIL);
1251 retval = ms_read_bytes(chip, READ_REG, MS_EXTRA_SIZE, NO_WAIT_INT,
1252 data, MS_EXTRA_SIZE);
1253 if (retval != STATUS_SUCCESS)
1254 TRACE_RET(chip, STATUS_FAIL);
1256 if (buf && buf_len) {
1257 if (buf_len > MS_EXTRA_SIZE)
1258 buf_len = MS_EXTRA_SIZE;
1259 memcpy(buf, data, buf_len);
1262 return STATUS_SUCCESS;
1265 static int ms_write_extra_data(struct rtsx_chip *chip,
1266 u16 block_addr, u8 page_num, u8 *buf, int buf_len)
1268 struct ms_info *ms_card = &(chip->ms_card);
1272 if (!buf || (buf_len < MS_EXTRA_SIZE))
1273 TRACE_RET(chip, STATUS_FAIL);
1275 retval = ms_set_rw_reg_addr(chip, OverwriteFlag, MS_EXTRA_SIZE,
1276 SystemParm, 6 + MS_EXTRA_SIZE);
1277 if (retval != STATUS_SUCCESS)
1278 TRACE_RET(chip, STATUS_FAIL);
1280 if (CHK_MS4BIT(ms_card))
1286 data[2] = (u8)(block_addr >> 8);
1287 data[3] = (u8)block_addr;
1291 for (i = 6; i < MS_EXTRA_SIZE + 6; i++)
1292 data[i] = buf[i - 6];
1294 retval = ms_write_bytes(chip, WRITE_REG, (6 + MS_EXTRA_SIZE),
1295 NO_WAIT_INT, data, 16);
1296 if (retval != STATUS_SUCCESS)
1297 TRACE_RET(chip, STATUS_FAIL);
1299 retval = ms_send_cmd(chip, BLOCK_WRITE, WAIT_INT);
1300 if (retval != STATUS_SUCCESS)
1301 TRACE_RET(chip, STATUS_FAIL);
1303 ms_set_err_code(chip, MS_NO_ERROR);
1304 retval = ms_read_bytes(chip, GET_INT, 1, NO_WAIT_INT, &val, 1);
1305 if (retval != STATUS_SUCCESS)
1306 TRACE_RET(chip, STATUS_FAIL);
1308 if (val & INT_REG_CMDNK) {
1309 ms_set_err_code(chip, MS_CMD_NK);
1310 TRACE_RET(chip, STATUS_FAIL);
1312 if (val & INT_REG_CED) {
1313 if (val & INT_REG_ERR) {
1314 ms_set_err_code(chip, MS_FLASH_WRITE_ERROR);
1315 TRACE_RET(chip, STATUS_FAIL);
1319 return STATUS_SUCCESS;
1323 static int ms_read_page(struct rtsx_chip *chip, u16 block_addr, u8 page_num)
1325 struct ms_info *ms_card = &(chip->ms_card);
1329 retval = ms_set_rw_reg_addr(chip, OverwriteFlag, MS_EXTRA_SIZE,
1331 if (retval != STATUS_SUCCESS)
1332 TRACE_RET(chip, STATUS_FAIL);
1334 if (CHK_MS4BIT(ms_card))
1340 data[2] = (u8)(block_addr >> 8);
1341 data[3] = (u8)block_addr;
1345 retval = ms_write_bytes(chip, WRITE_REG, 6, NO_WAIT_INT, data, 6);
1346 if (retval != STATUS_SUCCESS)
1347 TRACE_RET(chip, STATUS_FAIL);
1349 retval = ms_send_cmd(chip, BLOCK_READ, WAIT_INT);
1350 if (retval != STATUS_SUCCESS)
1351 TRACE_RET(chip, STATUS_FAIL);
1353 ms_set_err_code(chip, MS_NO_ERROR);
1354 retval = ms_read_bytes(chip, GET_INT, 1, NO_WAIT_INT, &val, 1);
1355 if (retval != STATUS_SUCCESS)
1356 TRACE_RET(chip, STATUS_FAIL);
1358 if (val & INT_REG_CMDNK) {
1359 ms_set_err_code(chip, MS_CMD_NK);
1360 TRACE_RET(chip, STATUS_FAIL);
1363 if (val & INT_REG_CED) {
1364 if (val & INT_REG_ERR) {
1365 if (!(val & INT_REG_BREQ)) {
1366 ms_set_err_code(chip, MS_FLASH_READ_ERROR);
1367 TRACE_RET(chip, STATUS_FAIL);
1369 retval = ms_read_status_reg(chip);
1370 if (retval != STATUS_SUCCESS)
1371 ms_set_err_code(chip, MS_FLASH_WRITE_ERROR);
1374 if (!(val & INT_REG_BREQ)) {
1375 ms_set_err_code(chip, MS_BREQ_ERROR);
1376 TRACE_RET(chip, STATUS_FAIL);
1381 retval = ms_transfer_tpc(chip, MS_TM_NORMAL_READ, READ_PAGE_DATA,
1383 if (retval != STATUS_SUCCESS)
1384 TRACE_RET(chip, STATUS_FAIL);
1386 if (ms_check_err_code(chip, MS_FLASH_WRITE_ERROR))
1387 TRACE_RET(chip, STATUS_FAIL);
1389 return STATUS_SUCCESS;
1393 static int ms_set_bad_block(struct rtsx_chip *chip, u16 phy_blk)
1395 struct ms_info *ms_card = &(chip->ms_card);
1397 u8 val, data[8], extra[MS_EXTRA_SIZE];
1399 retval = ms_read_extra_data(chip, phy_blk, 0, extra, MS_EXTRA_SIZE);
1400 if (retval != STATUS_SUCCESS)
1401 TRACE_RET(chip, STATUS_FAIL);
1403 retval = ms_set_rw_reg_addr(chip, OverwriteFlag, MS_EXTRA_SIZE,
1405 if (retval != STATUS_SUCCESS)
1406 TRACE_RET(chip, STATUS_FAIL);
1408 ms_set_err_code(chip, MS_NO_ERROR);
1410 if (CHK_MS4BIT(ms_card))
1416 data[2] = (u8)(phy_blk >> 8);
1417 data[3] = (u8)phy_blk;
1420 data[6] = extra[0] & 0x7F;
1423 retval = ms_write_bytes(chip, WRITE_REG, 7, NO_WAIT_INT, data, 7);
1424 if (retval != STATUS_SUCCESS)
1425 TRACE_RET(chip, STATUS_FAIL);
1427 retval = ms_send_cmd(chip, BLOCK_WRITE, WAIT_INT);
1428 if (retval != STATUS_SUCCESS)
1429 TRACE_RET(chip, STATUS_FAIL);
1431 ms_set_err_code(chip, MS_NO_ERROR);
1432 retval = ms_read_bytes(chip, GET_INT, 1, NO_WAIT_INT, &val, 1);
1433 if (retval != STATUS_SUCCESS)
1434 TRACE_RET(chip, STATUS_FAIL);
1436 if (val & INT_REG_CMDNK) {
1437 ms_set_err_code(chip, MS_CMD_NK);
1438 TRACE_RET(chip, STATUS_FAIL);
1441 if (val & INT_REG_CED) {
1442 if (val & INT_REG_ERR) {
1443 ms_set_err_code(chip, MS_FLASH_WRITE_ERROR);
1444 TRACE_RET(chip, STATUS_FAIL);
1448 return STATUS_SUCCESS;
1452 static int ms_erase_block(struct rtsx_chip *chip, u16 phy_blk)
1454 struct ms_info *ms_card = &(chip->ms_card);
1458 retval = ms_set_rw_reg_addr(chip, OverwriteFlag, MS_EXTRA_SIZE,
1460 if (retval != STATUS_SUCCESS)
1461 TRACE_RET(chip, STATUS_FAIL);
1463 ms_set_err_code(chip, MS_NO_ERROR);
1465 if (CHK_MS4BIT(ms_card))
1471 data[2] = (u8)(phy_blk >> 8);
1472 data[3] = (u8)phy_blk;
1476 retval = ms_write_bytes(chip, WRITE_REG, 6, NO_WAIT_INT, data, 6);
1477 if (retval != STATUS_SUCCESS)
1478 TRACE_RET(chip, STATUS_FAIL);
1481 retval = ms_send_cmd(chip, BLOCK_ERASE, WAIT_INT);
1482 if (retval != STATUS_SUCCESS)
1483 TRACE_RET(chip, STATUS_FAIL);
1485 ms_set_err_code(chip, MS_NO_ERROR);
1486 retval = ms_read_bytes(chip, GET_INT, 1, NO_WAIT_INT, &val, 1);
1487 if (retval != STATUS_SUCCESS)
1488 TRACE_RET(chip, STATUS_FAIL);
1490 if (val & INT_REG_CMDNK) {
1496 ms_set_err_code(chip, MS_CMD_NK);
1497 ms_set_bad_block(chip, phy_blk);
1498 TRACE_RET(chip, STATUS_FAIL);
1501 if (val & INT_REG_CED) {
1502 if (val & INT_REG_ERR) {
1503 ms_set_err_code(chip, MS_FLASH_WRITE_ERROR);
1504 TRACE_RET(chip, STATUS_FAIL);
1508 return STATUS_SUCCESS;
1512 static void ms_set_page_status(u16 log_blk, u8 type, u8 *extra, int extra_len)
1514 if (!extra || (extra_len < MS_EXTRA_SIZE))
1517 memset(extra, 0xFF, MS_EXTRA_SIZE);
1519 if (type == setPS_NG) {
1520 /* set page status as 1:NG,and block status keep 1:OK */
1523 /* set page status as 0:Data Error,and block status keep 1:OK */
1527 extra[2] = (u8)(log_blk >> 8);
1528 extra[3] = (u8)log_blk;
1531 static int ms_init_page(struct rtsx_chip *chip, u16 phy_blk, u16 log_blk,
1532 u8 start_page, u8 end_page)
1535 u8 extra[MS_EXTRA_SIZE], i;
1537 memset(extra, 0xff, MS_EXTRA_SIZE);
1539 extra[0] = 0xf8; /* Block, page OK, data erased */
1541 extra[2] = (u8)(log_blk >> 8);
1542 extra[3] = (u8)log_blk;
1544 for (i = start_page; i < end_page; i++) {
1545 if (detect_card_cd(chip, MS_CARD) != STATUS_SUCCESS) {
1546 ms_set_err_code(chip, MS_NO_CARD);
1547 TRACE_RET(chip, STATUS_FAIL);
1550 retval = ms_write_extra_data(chip, phy_blk, i,
1551 extra, MS_EXTRA_SIZE);
1552 if (retval != STATUS_SUCCESS)
1553 TRACE_RET(chip, STATUS_FAIL);
1556 return STATUS_SUCCESS;
1559 static int ms_copy_page(struct rtsx_chip *chip, u16 old_blk, u16 new_blk,
1560 u16 log_blk, u8 start_page, u8 end_page)
1562 struct ms_info *ms_card = &(chip->ms_card);
1563 int retval, rty_cnt, uncorrect_flag = 0;
1564 u8 extra[MS_EXTRA_SIZE], val, i, j, data[16];
1566 dev_dbg(rtsx_dev(chip), "Copy page from 0x%x to 0x%x, logical block is 0x%x\n",
1567 old_blk, new_blk, log_blk);
1568 dev_dbg(rtsx_dev(chip), "start_page = %d, end_page = %d\n",
1569 start_page, end_page);
1571 retval = ms_read_extra_data(chip, new_blk, 0, extra, MS_EXTRA_SIZE);
1572 if (retval != STATUS_SUCCESS)
1573 TRACE_RET(chip, STATUS_FAIL);
1575 retval = ms_read_status_reg(chip);
1576 if (retval != STATUS_SUCCESS)
1577 TRACE_RET(chip, STATUS_FAIL);
1579 RTSX_READ_REG(chip, PPBUF_BASE2, &val);
1581 if (val & BUF_FULL) {
1582 retval = ms_send_cmd(chip, CLEAR_BUF, WAIT_INT);
1583 if (retval != STATUS_SUCCESS)
1584 TRACE_RET(chip, STATUS_FAIL);
1586 retval = ms_read_bytes(chip, GET_INT, 1, NO_WAIT_INT, &val, 1);
1587 if (retval != STATUS_SUCCESS)
1588 TRACE_RET(chip, STATUS_FAIL);
1590 if (!(val & INT_REG_CED)) {
1591 ms_set_err_code(chip, MS_FLASH_WRITE_ERROR);
1592 TRACE_RET(chip, STATUS_FAIL);
1596 for (i = start_page; i < end_page; i++) {
1597 if (detect_card_cd(chip, MS_CARD) != STATUS_SUCCESS) {
1598 ms_set_err_code(chip, MS_NO_CARD);
1599 TRACE_RET(chip, STATUS_FAIL);
1602 ms_read_extra_data(chip, old_blk, i, extra, MS_EXTRA_SIZE);
1604 retval = ms_set_rw_reg_addr(chip, OverwriteFlag,
1605 MS_EXTRA_SIZE, SystemParm, 6);
1606 if (retval != STATUS_SUCCESS)
1607 TRACE_RET(chip, STATUS_FAIL);
1609 ms_set_err_code(chip, MS_NO_ERROR);
1611 if (CHK_MS4BIT(ms_card))
1617 data[2] = (u8)(old_blk >> 8);
1618 data[3] = (u8)old_blk;
1622 retval = ms_write_bytes(chip, WRITE_REG, 6, NO_WAIT_INT,
1624 if (retval != STATUS_SUCCESS)
1625 TRACE_RET(chip, STATUS_FAIL);
1627 retval = ms_send_cmd(chip, BLOCK_READ, WAIT_INT);
1628 if (retval != STATUS_SUCCESS)
1629 TRACE_RET(chip, STATUS_FAIL);
1631 ms_set_err_code(chip, MS_NO_ERROR);
1632 retval = ms_read_bytes(chip, GET_INT, 1, NO_WAIT_INT, &val, 1);
1633 if (retval != STATUS_SUCCESS)
1634 TRACE_RET(chip, STATUS_FAIL);
1636 if (val & INT_REG_CMDNK) {
1637 ms_set_err_code(chip, MS_CMD_NK);
1638 TRACE_RET(chip, STATUS_FAIL);
1641 if (val & INT_REG_CED) {
1642 if (val & INT_REG_ERR) {
1643 retval = ms_read_status_reg(chip);
1644 if (retval != STATUS_SUCCESS) {
1646 dev_dbg(rtsx_dev(chip), "Uncorrectable error\n");
1651 retval = ms_transfer_tpc(chip,
1655 if (retval != STATUS_SUCCESS)
1656 TRACE_RET(chip, STATUS_FAIL);
1658 if (uncorrect_flag) {
1659 ms_set_page_status(log_blk, setPS_NG,
1660 extra, MS_EXTRA_SIZE);
1664 ms_write_extra_data(chip, old_blk, i,
1665 extra, MS_EXTRA_SIZE);
1666 dev_dbg(rtsx_dev(chip), "page %d : extra[0] = 0x%x\n",
1668 MS_SET_BAD_BLOCK_FLG(ms_card);
1670 ms_set_page_status(log_blk, setPS_Error,
1671 extra, MS_EXTRA_SIZE);
1672 ms_write_extra_data(chip, new_blk, i,
1673 extra, MS_EXTRA_SIZE);
1677 for (rty_cnt = 0; rty_cnt < MS_MAX_RETRY_COUNT;
1679 retval = ms_transfer_tpc(
1684 if (retval == STATUS_SUCCESS)
1687 if (rty_cnt == MS_MAX_RETRY_COUNT)
1688 TRACE_RET(chip, STATUS_FAIL);
1691 if (!(val & INT_REG_BREQ)) {
1692 ms_set_err_code(chip, MS_BREQ_ERROR);
1693 TRACE_RET(chip, STATUS_FAIL);
1697 retval = ms_set_rw_reg_addr(chip, OverwriteFlag,
1698 MS_EXTRA_SIZE, SystemParm, (6 + MS_EXTRA_SIZE));
1700 ms_set_err_code(chip, MS_NO_ERROR);
1702 if (CHK_MS4BIT(ms_card))
1708 data[2] = (u8)(new_blk >> 8);
1709 data[3] = (u8)new_blk;
1713 if ((extra[0] & 0x60) != 0x60)
1719 data[6 + 2] = (u8)(log_blk >> 8);
1720 data[6 + 3] = (u8)log_blk;
1722 for (j = 4; j <= MS_EXTRA_SIZE; j++)
1725 retval = ms_write_bytes(chip, WRITE_REG, (6 + MS_EXTRA_SIZE),
1726 NO_WAIT_INT, data, 16);
1727 if (retval != STATUS_SUCCESS)
1728 TRACE_RET(chip, STATUS_FAIL);
1730 retval = ms_send_cmd(chip, BLOCK_WRITE, WAIT_INT);
1731 if (retval != STATUS_SUCCESS)
1732 TRACE_RET(chip, STATUS_FAIL);
1734 ms_set_err_code(chip, MS_NO_ERROR);
1735 retval = ms_read_bytes(chip, GET_INT, 1, NO_WAIT_INT, &val, 1);
1736 if (retval != STATUS_SUCCESS)
1737 TRACE_RET(chip, STATUS_FAIL);
1739 if (val & INT_REG_CMDNK) {
1740 ms_set_err_code(chip, MS_CMD_NK);
1741 TRACE_RET(chip, STATUS_FAIL);
1744 if (val & INT_REG_CED) {
1745 if (val & INT_REG_ERR) {
1746 ms_set_err_code(chip, MS_FLASH_WRITE_ERROR);
1747 TRACE_RET(chip, STATUS_FAIL);
1752 retval = ms_set_rw_reg_addr(chip, OverwriteFlag,
1753 MS_EXTRA_SIZE, SystemParm, 7);
1754 if (retval != STATUS_SUCCESS)
1755 TRACE_RET(chip, STATUS_FAIL);
1757 ms_set_err_code(chip, MS_NO_ERROR);
1759 if (CHK_MS4BIT(ms_card))
1765 data[2] = (u8)(old_blk >> 8);
1766 data[3] = (u8)old_blk;
1772 retval = ms_write_bytes(chip, WRITE_REG, 7,
1773 NO_WAIT_INT, data, 8);
1774 if (retval != STATUS_SUCCESS)
1775 TRACE_RET(chip, STATUS_FAIL);
1777 retval = ms_send_cmd(chip, BLOCK_WRITE, WAIT_INT);
1778 if (retval != STATUS_SUCCESS)
1779 TRACE_RET(chip, STATUS_FAIL);
1781 ms_set_err_code(chip, MS_NO_ERROR);
1782 retval = ms_read_bytes(chip, GET_INT, 1,
1783 NO_WAIT_INT, &val, 1);
1784 if (retval != STATUS_SUCCESS)
1785 TRACE_RET(chip, STATUS_FAIL);
1787 if (val & INT_REG_CMDNK) {
1788 ms_set_err_code(chip, MS_CMD_NK);
1789 TRACE_RET(chip, STATUS_FAIL);
1792 if (val & INT_REG_CED) {
1793 if (val & INT_REG_ERR) {
1794 ms_set_err_code(chip,
1795 MS_FLASH_WRITE_ERROR);
1796 TRACE_RET(chip, STATUS_FAIL);
1802 return STATUS_SUCCESS;
1806 static int reset_ms(struct rtsx_chip *chip)
1808 struct ms_info *ms_card = &(chip->ms_card);
1810 u16 i, reg_addr, block_size;
1811 u8 val, extra[MS_EXTRA_SIZE], j, *ptr;
1812 #ifndef SUPPORT_MAGIC_GATE
1816 retval = ms_prepare_reset(chip);
1817 if (retval != STATUS_SUCCESS)
1818 TRACE_RET(chip, STATUS_FAIL);
1820 ms_card->ms_type |= TYPE_MS;
1822 retval = ms_send_cmd(chip, MS_RESET, NO_WAIT_INT);
1823 if (retval != STATUS_SUCCESS)
1824 TRACE_RET(chip, STATUS_FAIL);
1826 retval = ms_read_status_reg(chip);
1827 if (retval != STATUS_SUCCESS)
1828 TRACE_RET(chip, STATUS_FAIL);
1830 RTSX_READ_REG(chip, PPBUF_BASE2, &val);
1831 if (val & WRT_PRTCT)
1832 chip->card_wp |= MS_CARD;
1834 chip->card_wp &= ~MS_CARD;
1839 /* Search Boot Block */
1840 while (i < (MAX_DEFECTIVE_BLOCK + 2)) {
1841 if (detect_card_cd(chip, MS_CARD) != STATUS_SUCCESS) {
1842 ms_set_err_code(chip, MS_NO_CARD);
1843 TRACE_RET(chip, STATUS_FAIL);
1846 retval = ms_read_extra_data(chip, i, 0, extra, MS_EXTRA_SIZE);
1847 if (retval != STATUS_SUCCESS) {
1852 if (extra[0] & BLOCK_OK) {
1853 if (!(extra[1] & NOT_BOOT_BLOCK)) {
1854 ms_card->boot_block = i;
1861 if (i == (MAX_DEFECTIVE_BLOCK + 2)) {
1862 dev_dbg(rtsx_dev(chip), "No boot block found!");
1863 TRACE_RET(chip, STATUS_FAIL);
1866 for (j = 0; j < 3; j++) {
1867 retval = ms_read_page(chip, ms_card->boot_block, j);
1868 if (retval != STATUS_SUCCESS) {
1869 if (ms_check_err_code(chip, MS_FLASH_WRITE_ERROR)) {
1870 i = ms_card->boot_block + 1;
1871 ms_set_err_code(chip, MS_NO_ERROR);
1877 retval = ms_read_page(chip, ms_card->boot_block, 0);
1878 if (retval != STATUS_SUCCESS)
1879 TRACE_RET(chip, STATUS_FAIL);
1881 /* Read MS system information as sys_info */
1882 rtsx_init_cmd(chip);
1884 for (i = 0; i < 96; i++)
1885 rtsx_add_cmd(chip, READ_REG_CMD, PPBUF_BASE2 + 0x1A0 + i, 0, 0);
1887 retval = rtsx_send_cmd(chip, MS_CARD, 100);
1889 TRACE_RET(chip, STATUS_FAIL);
1891 ptr = rtsx_get_cmd_data(chip);
1892 memcpy(ms_card->raw_sys_info, ptr, 96);
1894 /* Read useful block contents */
1895 rtsx_init_cmd(chip);
1897 rtsx_add_cmd(chip, READ_REG_CMD, HEADER_ID0, 0, 0);
1898 rtsx_add_cmd(chip, READ_REG_CMD, HEADER_ID1, 0, 0);
1900 for (reg_addr = DISABLED_BLOCK0; reg_addr <= DISABLED_BLOCK3;
1902 rtsx_add_cmd(chip, READ_REG_CMD, reg_addr, 0, 0);
1904 for (reg_addr = BLOCK_SIZE_0; reg_addr <= PAGE_SIZE_1; reg_addr++)
1905 rtsx_add_cmd(chip, READ_REG_CMD, reg_addr, 0, 0);
1907 rtsx_add_cmd(chip, READ_REG_CMD, MS_Device_Type, 0, 0);
1908 rtsx_add_cmd(chip, READ_REG_CMD, MS_4bit_Support, 0, 0);
1910 retval = rtsx_send_cmd(chip, MS_CARD, 100);
1912 TRACE_RET(chip, STATUS_FAIL);
1914 ptr = rtsx_get_cmd_data(chip);
1916 dev_dbg(rtsx_dev(chip), "Boot block data:\n");
1917 dev_dbg(rtsx_dev(chip), "%*ph\n", 16, ptr);
1920 * HEADER_ID0, HEADER_ID1
1922 if (ptr[0] != 0x00 || ptr[1] != 0x01) {
1923 i = ms_card->boot_block + 1;
1928 * PAGE_SIZE_0, PAGE_SIZE_1
1930 if (ptr[12] != 0x02 || ptr[13] != 0x00) {
1931 i = ms_card->boot_block + 1;
1935 if ((ptr[14] == 1) || (ptr[14] == 3))
1936 chip->card_wp |= MS_CARD;
1938 /* BLOCK_SIZE_0, BLOCK_SIZE_1 */
1939 block_size = ((u16)ptr[6] << 8) | ptr[7];
1940 if (block_size == 0x0010) {
1941 /* Block size 16KB */
1942 ms_card->block_shift = 5;
1943 ms_card->page_off = 0x1F;
1944 } else if (block_size == 0x0008) {
1945 /* Block size 8KB */
1946 ms_card->block_shift = 4;
1947 ms_card->page_off = 0x0F;
1950 /* BLOCK_COUNT_0, BLOCK_COUNT_1 */
1951 ms_card->total_block = ((u16)ptr[8] << 8) | ptr[9];
1953 #ifdef SUPPORT_MAGIC_GATE
1956 if (ms_card->block_shift == 4) { /* 4MB or 8MB */
1957 if (j < 2) { /* Effective block for 4MB: 0x1F0 */
1958 ms_card->capacity = 0x1EE0;
1959 } else { /* Effective block for 8MB: 0x3E0 */
1960 ms_card->capacity = 0x3DE0;
1962 } else { /* 16MB, 32MB, 64MB or 128MB */
1963 if (j < 5) { /* Effective block for 16MB: 0x3E0 */
1964 ms_card->capacity = 0x7BC0;
1965 } else if (j < 0xA) { /* Effective block for 32MB: 0x7C0 */
1966 ms_card->capacity = 0xF7C0;
1967 } else if (j < 0x11) { /* Effective block for 64MB: 0xF80 */
1968 ms_card->capacity = 0x1EF80;
1969 } else { /* Effective block for 128MB: 0x1F00 */
1970 ms_card->capacity = 0x3DF00;
1974 /* EBLOCK_COUNT_0, EBLOCK_COUNT_1 */
1975 eblock_cnt = ((u16)ptr[10] << 8) | ptr[11];
1977 ms_card->capacity = ((u32)eblock_cnt - 2) << ms_card->block_shift;
1980 chip->capacity[chip->card2lun[MS_CARD]] = ms_card->capacity;
1982 /* Switch I/F Mode */
1984 retval = ms_set_rw_reg_addr(chip, 0, 0, SystemParm, 1);
1985 if (retval != STATUS_SUCCESS)
1986 TRACE_RET(chip, STATUS_FAIL);
1988 RTSX_WRITE_REG(chip, PPBUF_BASE2, 0xFF, 0x88);
1989 RTSX_WRITE_REG(chip, PPBUF_BASE2 + 1, 0xFF, 0);
1991 retval = ms_transfer_tpc(chip, MS_TM_WRITE_BYTES, WRITE_REG, 1,
1993 if (retval != STATUS_SUCCESS)
1994 TRACE_RET(chip, STATUS_FAIL);
1996 RTSX_WRITE_REG(chip, MS_CFG, 0x58 | MS_NO_CHECK_INT,
1997 MS_BUS_WIDTH_4 | PUSH_TIME_ODD | MS_NO_CHECK_INT);
1999 ms_card->ms_type |= MS_4BIT;
2002 if (CHK_MS4BIT(ms_card))
2003 chip->card_bus_width[chip->card2lun[MS_CARD]] = 4;
2005 chip->card_bus_width[chip->card2lun[MS_CARD]] = 1;
2007 return STATUS_SUCCESS;
2010 static int ms_init_l2p_tbl(struct rtsx_chip *chip)
2012 struct ms_info *ms_card = &(chip->ms_card);
2013 int size, i, seg_no, retval;
2014 u16 defect_block, reg_addr;
2017 ms_card->segment_cnt = ms_card->total_block >> 9;
2018 dev_dbg(rtsx_dev(chip), "ms_card->segment_cnt = %d\n",
2019 ms_card->segment_cnt);
2021 size = ms_card->segment_cnt * sizeof(struct zone_entry);
2022 ms_card->segment = vzalloc(size);
2023 if (ms_card->segment == NULL)
2024 TRACE_RET(chip, STATUS_FAIL);
2026 retval = ms_read_page(chip, ms_card->boot_block, 1);
2027 if (retval != STATUS_SUCCESS)
2028 TRACE_GOTO(chip, INIT_FAIL);
2030 reg_addr = PPBUF_BASE2;
2031 for (i = 0; i < (((ms_card->total_block >> 9) * 10) + 1); i++) {
2032 retval = rtsx_read_register(chip, reg_addr++, &val1);
2033 if (retval != STATUS_SUCCESS)
2034 TRACE_GOTO(chip, INIT_FAIL);
2036 retval = rtsx_read_register(chip, reg_addr++, &val2);
2037 if (retval != STATUS_SUCCESS)
2038 TRACE_GOTO(chip, INIT_FAIL);
2040 defect_block = ((u16)val1 << 8) | val2;
2041 if (defect_block == 0xFFFF)
2044 seg_no = defect_block / 512;
2045 ms_card->segment[seg_no].defect_list[ms_card->segment[seg_no].disable_count++] = defect_block;
2048 for (i = 0; i < ms_card->segment_cnt; i++) {
2049 ms_card->segment[i].build_flag = 0;
2050 ms_card->segment[i].l2p_table = NULL;
2051 ms_card->segment[i].free_table = NULL;
2052 ms_card->segment[i].get_index = 0;
2053 ms_card->segment[i].set_index = 0;
2054 ms_card->segment[i].unused_blk_cnt = 0;
2056 dev_dbg(rtsx_dev(chip), "defective block count of segment %d is %d\n",
2057 i, ms_card->segment[i].disable_count);
2060 return STATUS_SUCCESS;
2063 if (ms_card->segment) {
2064 vfree(ms_card->segment);
2065 ms_card->segment = NULL;
2071 static u16 ms_get_l2p_tbl(struct rtsx_chip *chip, int seg_no, u16 log_off)
2073 struct ms_info *ms_card = &(chip->ms_card);
2074 struct zone_entry *segment;
2076 if (ms_card->segment == NULL)
2079 segment = &(ms_card->segment[seg_no]);
2081 if (segment->l2p_table)
2082 return segment->l2p_table[log_off];
2087 static void ms_set_l2p_tbl(struct rtsx_chip *chip,
2088 int seg_no, u16 log_off, u16 phy_blk)
2090 struct ms_info *ms_card = &(chip->ms_card);
2091 struct zone_entry *segment;
2093 if (ms_card->segment == NULL)
2096 segment = &(ms_card->segment[seg_no]);
2097 if (segment->l2p_table)
2098 segment->l2p_table[log_off] = phy_blk;
2101 static void ms_set_unused_block(struct rtsx_chip *chip, u16 phy_blk)
2103 struct ms_info *ms_card = &(chip->ms_card);
2104 struct zone_entry *segment;
2107 seg_no = (int)phy_blk >> 9;
2108 segment = &(ms_card->segment[seg_no]);
2110 segment->free_table[segment->set_index++] = phy_blk;
2111 if (segment->set_index >= MS_FREE_TABLE_CNT)
2112 segment->set_index = 0;
2114 segment->unused_blk_cnt++;
2117 static u16 ms_get_unused_block(struct rtsx_chip *chip, int seg_no)
2119 struct ms_info *ms_card = &(chip->ms_card);
2120 struct zone_entry *segment;
2123 segment = &(ms_card->segment[seg_no]);
2125 if (segment->unused_blk_cnt <= 0)
2128 phy_blk = segment->free_table[segment->get_index];
2129 segment->free_table[segment->get_index++] = 0xFFFF;
2130 if (segment->get_index >= MS_FREE_TABLE_CNT)
2131 segment->get_index = 0;
2133 segment->unused_blk_cnt--;
2138 static const unsigned short ms_start_idx[] = {0, 494, 990, 1486, 1982, 2478,
2139 2974, 3470, 3966, 4462, 4958,
2140 5454, 5950, 6446, 6942, 7438,
2143 static int ms_arbitrate_l2p(struct rtsx_chip *chip, u16 phy_blk,
2144 u16 log_off, u8 us1, u8 us2)
2146 struct ms_info *ms_card = &(chip->ms_card);
2147 struct zone_entry *segment;
2151 seg_no = (int)phy_blk >> 9;
2152 segment = &(ms_card->segment[seg_no]);
2153 tmp_blk = segment->l2p_table[log_off];
2157 if (!(chip->card_wp & MS_CARD))
2158 ms_erase_block(chip, tmp_blk);
2160 ms_set_unused_block(chip, tmp_blk);
2161 segment->l2p_table[log_off] = phy_blk;
2163 if (!(chip->card_wp & MS_CARD))
2164 ms_erase_block(chip, phy_blk);
2166 ms_set_unused_block(chip, phy_blk);
2169 if (phy_blk < tmp_blk) {
2170 if (!(chip->card_wp & MS_CARD))
2171 ms_erase_block(chip, phy_blk);
2173 ms_set_unused_block(chip, phy_blk);
2175 if (!(chip->card_wp & MS_CARD))
2176 ms_erase_block(chip, tmp_blk);
2178 ms_set_unused_block(chip, tmp_blk);
2179 segment->l2p_table[log_off] = phy_blk;
2183 return STATUS_SUCCESS;
2186 static int ms_build_l2p_tbl(struct rtsx_chip *chip, int seg_no)
2188 struct ms_info *ms_card = &(chip->ms_card);
2189 struct zone_entry *segment;
2190 int retval, table_size, disable_cnt, defect_flag, i;
2191 u16 start, end, phy_blk, log_blk, tmp_blk;
2192 u8 extra[MS_EXTRA_SIZE], us1, us2;
2194 dev_dbg(rtsx_dev(chip), "ms_build_l2p_tbl: %d\n", seg_no);
2196 if (ms_card->segment == NULL) {
2197 retval = ms_init_l2p_tbl(chip);
2198 if (retval != STATUS_SUCCESS)
2199 TRACE_RET(chip, retval);
2202 if (ms_card->segment[seg_no].build_flag) {
2203 dev_dbg(rtsx_dev(chip), "l2p table of segment %d has been built\n",
2205 return STATUS_SUCCESS;
2213 segment = &(ms_card->segment[seg_no]);
2215 if (segment->l2p_table == NULL) {
2216 segment->l2p_table = vmalloc(table_size * 2);
2217 if (segment->l2p_table == NULL)
2218 TRACE_GOTO(chip, BUILD_FAIL);
2220 memset((u8 *)(segment->l2p_table), 0xff, table_size * 2);
2222 if (segment->free_table == NULL) {
2223 segment->free_table = vmalloc(MS_FREE_TABLE_CNT * 2);
2224 if (segment->free_table == NULL)
2225 TRACE_GOTO(chip, BUILD_FAIL);
2227 memset((u8 *)(segment->free_table), 0xff, MS_FREE_TABLE_CNT * 2);
2229 start = (u16)seg_no << 9;
2230 end = (u16)(seg_no + 1) << 9;
2232 disable_cnt = segment->disable_count;
2234 segment->get_index = segment->set_index = 0;
2235 segment->unused_blk_cnt = 0;
2237 for (phy_blk = start; phy_blk < end; phy_blk++) {
2240 for (i = 0; i < segment->disable_count; i++) {
2241 if (phy_blk == segment->defect_list[i]) {
2252 retval = ms_read_extra_data(chip, phy_blk, 0,
2253 extra, MS_EXTRA_SIZE);
2254 if (retval != STATUS_SUCCESS) {
2255 dev_dbg(rtsx_dev(chip), "read extra data fail\n");
2256 ms_set_bad_block(chip, phy_blk);
2260 if (seg_no == ms_card->segment_cnt - 1) {
2261 if (!(extra[1] & NOT_TRANSLATION_TABLE)) {
2262 if (!(chip->card_wp & MS_CARD)) {
2263 retval = ms_erase_block(chip, phy_blk);
2264 if (retval != STATUS_SUCCESS)
2272 if (!(extra[0] & BLOCK_OK))
2274 if (!(extra[1] & NOT_BOOT_BLOCK))
2276 if ((extra[0] & PAGE_OK) != PAGE_OK)
2279 log_blk = ((u16)extra[2] << 8) | extra[3];
2281 if (log_blk == 0xFFFF) {
2282 if (!(chip->card_wp & MS_CARD)) {
2283 retval = ms_erase_block(chip, phy_blk);
2284 if (retval != STATUS_SUCCESS)
2287 ms_set_unused_block(chip, phy_blk);
2291 if ((log_blk < ms_start_idx[seg_no]) ||
2292 (log_blk >= ms_start_idx[seg_no+1])) {
2293 if (!(chip->card_wp & MS_CARD)) {
2294 retval = ms_erase_block(chip, phy_blk);
2295 if (retval != STATUS_SUCCESS)
2298 ms_set_unused_block(chip, phy_blk);
2302 if (segment->l2p_table[log_blk - ms_start_idx[seg_no]] == 0xFFFF) {
2303 segment->l2p_table[log_blk - ms_start_idx[seg_no]] = phy_blk;
2307 us1 = extra[0] & 0x10;
2308 tmp_blk = segment->l2p_table[log_blk - ms_start_idx[seg_no]];
2309 retval = ms_read_extra_data(chip, tmp_blk, 0,
2310 extra, MS_EXTRA_SIZE);
2311 if (retval != STATUS_SUCCESS)
2313 us2 = extra[0] & 0x10;
2315 (void)ms_arbitrate_l2p(chip, phy_blk,
2316 log_blk-ms_start_idx[seg_no], us1, us2);
2320 segment->build_flag = 1;
2322 dev_dbg(rtsx_dev(chip), "unused block count: %d\n",
2323 segment->unused_blk_cnt);
2325 /* Logical Address Confirmation Process */
2326 if (seg_no == ms_card->segment_cnt - 1) {
2327 if (segment->unused_blk_cnt < 2)
2328 chip->card_wp |= MS_CARD;
2330 if (segment->unused_blk_cnt < 1)
2331 chip->card_wp |= MS_CARD;
2334 if (chip->card_wp & MS_CARD)
2335 return STATUS_SUCCESS;
2337 for (log_blk = ms_start_idx[seg_no];
2338 log_blk < ms_start_idx[seg_no + 1]; log_blk++) {
2339 if (segment->l2p_table[log_blk-ms_start_idx[seg_no]] == 0xFFFF) {
2340 phy_blk = ms_get_unused_block(chip, seg_no);
2341 if (phy_blk == 0xFFFF) {
2342 chip->card_wp |= MS_CARD;
2343 return STATUS_SUCCESS;
2345 retval = ms_init_page(chip, phy_blk, log_blk, 0, 1);
2346 if (retval != STATUS_SUCCESS)
2347 TRACE_GOTO(chip, BUILD_FAIL);
2349 segment->l2p_table[log_blk-ms_start_idx[seg_no]] = phy_blk;
2350 if (seg_no == ms_card->segment_cnt - 1) {
2351 if (segment->unused_blk_cnt < 2) {
2352 chip->card_wp |= MS_CARD;
2353 return STATUS_SUCCESS;
2356 if (segment->unused_blk_cnt < 1) {
2357 chip->card_wp |= MS_CARD;
2358 return STATUS_SUCCESS;
2364 /* Make boot block be the first normal block */
2366 for (log_blk = 0; log_blk < 494; log_blk++) {
2367 tmp_blk = segment->l2p_table[log_blk];
2368 if (tmp_blk < ms_card->boot_block) {
2369 dev_dbg(rtsx_dev(chip), "Boot block is not the first normal block.\n");
2371 if (chip->card_wp & MS_CARD)
2374 phy_blk = ms_get_unused_block(chip, 0);
2375 retval = ms_copy_page(chip, tmp_blk, phy_blk,
2376 log_blk, 0, ms_card->page_off + 1);
2377 if (retval != STATUS_SUCCESS)
2378 TRACE_RET(chip, STATUS_FAIL);
2380 segment->l2p_table[log_blk] = phy_blk;
2382 retval = ms_set_bad_block(chip, tmp_blk);
2383 if (retval != STATUS_SUCCESS)
2384 TRACE_RET(chip, STATUS_FAIL);
2389 return STATUS_SUCCESS;
2392 segment->build_flag = 0;
2393 if (segment->l2p_table) {
2394 vfree(segment->l2p_table);
2395 segment->l2p_table = NULL;
2397 if (segment->free_table) {
2398 vfree(segment->free_table);
2399 segment->free_table = NULL;
2406 int reset_ms_card(struct rtsx_chip *chip)
2408 struct ms_info *ms_card = &(chip->ms_card);
2411 memset(ms_card, 0, sizeof(struct ms_info));
2413 retval = enable_card_clock(chip, MS_CARD);
2414 if (retval != STATUS_SUCCESS)
2415 TRACE_RET(chip, STATUS_FAIL);
2417 retval = select_card(chip, MS_CARD);
2418 if (retval != STATUS_SUCCESS)
2419 TRACE_RET(chip, STATUS_FAIL);
2421 ms_card->ms_type = 0;
2423 retval = reset_ms_pro(chip);
2424 if (retval != STATUS_SUCCESS) {
2425 if (ms_card->check_ms_flow) {
2426 retval = reset_ms(chip);
2427 if (retval != STATUS_SUCCESS)
2428 TRACE_RET(chip, STATUS_FAIL);
2430 TRACE_RET(chip, STATUS_FAIL);
2434 retval = ms_set_init_para(chip);
2435 if (retval != STATUS_SUCCESS)
2436 TRACE_RET(chip, STATUS_FAIL);
2438 if (!CHK_MSPRO(ms_card)) {
2439 /* Build table for the last segment,
2440 * to check if L2P table block exists, erasing it
2442 retval = ms_build_l2p_tbl(chip, ms_card->total_block / 512 - 1);
2443 if (retval != STATUS_SUCCESS)
2444 TRACE_RET(chip, STATUS_FAIL);
2447 dev_dbg(rtsx_dev(chip), "ms_card->ms_type = 0x%x\n", ms_card->ms_type);
2449 return STATUS_SUCCESS;
2452 static int mspro_set_rw_cmd(struct rtsx_chip *chip,
2453 u32 start_sec, u16 sec_cnt, u8 cmd)
2459 data[1] = (u8)(sec_cnt >> 8);
2460 data[2] = (u8)sec_cnt;
2461 data[3] = (u8)(start_sec >> 24);
2462 data[4] = (u8)(start_sec >> 16);
2463 data[5] = (u8)(start_sec >> 8);
2464 data[6] = (u8)start_sec;
2467 for (i = 0; i < MS_MAX_RETRY_COUNT; i++) {
2468 retval = ms_write_bytes(chip, PRO_EX_SET_CMD, 7,
2470 if (retval == STATUS_SUCCESS)
2473 if (i == MS_MAX_RETRY_COUNT)
2474 TRACE_RET(chip, STATUS_FAIL);
2476 return STATUS_SUCCESS;
2480 void mspro_stop_seq_mode(struct rtsx_chip *chip)
2482 struct ms_info *ms_card = &(chip->ms_card);
2485 if (ms_card->seq_mode) {
2486 retval = ms_switch_clock(chip);
2487 if (retval != STATUS_SUCCESS)
2490 ms_card->seq_mode = 0;
2491 ms_card->total_sec_cnt = 0;
2492 ms_send_cmd(chip, PRO_STOP, WAIT_INT);
2494 rtsx_write_register(chip, RBCTL, RB_FLUSH, RB_FLUSH);
2498 static inline int ms_auto_tune_clock(struct rtsx_chip *chip)
2500 struct ms_info *ms_card = &(chip->ms_card);
2503 if (chip->asic_code) {
2504 if (ms_card->ms_clock > 30)
2505 ms_card->ms_clock -= 20;
2507 if (ms_card->ms_clock == CLK_80)
2508 ms_card->ms_clock = CLK_60;
2509 else if (ms_card->ms_clock == CLK_60)
2510 ms_card->ms_clock = CLK_40;
2513 retval = ms_switch_clock(chip);
2514 if (retval != STATUS_SUCCESS)
2515 TRACE_RET(chip, STATUS_FAIL);
2517 return STATUS_SUCCESS;
2520 static int mspro_rw_multi_sector(struct scsi_cmnd *srb,
2521 struct rtsx_chip *chip, u32 start_sector,
2524 struct ms_info *ms_card = &(chip->ms_card);
2525 int retval, mode_2k = 0;
2527 u8 val, trans_mode, rw_tpc, rw_cmd;
2529 ms_set_err_code(chip, MS_NO_ERROR);
2531 ms_card->cleanup_counter = 0;
2533 if (CHK_MSHG(ms_card)) {
2534 if ((start_sector % 4) || (sector_cnt % 4)) {
2535 if (srb->sc_data_direction == DMA_FROM_DEVICE) {
2536 rw_tpc = PRO_READ_LONG_DATA;
2537 rw_cmd = PRO_READ_DATA;
2539 rw_tpc = PRO_WRITE_LONG_DATA;
2540 rw_cmd = PRO_WRITE_DATA;
2543 if (srb->sc_data_direction == DMA_FROM_DEVICE) {
2544 rw_tpc = PRO_READ_QUAD_DATA;
2545 rw_cmd = PRO_READ_2K_DATA;
2547 rw_tpc = PRO_WRITE_QUAD_DATA;
2548 rw_cmd = PRO_WRITE_2K_DATA;
2553 if (srb->sc_data_direction == DMA_FROM_DEVICE) {
2554 rw_tpc = PRO_READ_LONG_DATA;
2555 rw_cmd = PRO_READ_DATA;
2557 rw_tpc = PRO_WRITE_LONG_DATA;
2558 rw_cmd = PRO_WRITE_DATA;
2562 retval = ms_switch_clock(chip);
2563 if (retval != STATUS_SUCCESS)
2564 TRACE_RET(chip, STATUS_FAIL);
2566 if (srb->sc_data_direction == DMA_FROM_DEVICE)
2567 trans_mode = MS_TM_AUTO_READ;
2569 trans_mode = MS_TM_AUTO_WRITE;
2571 RTSX_READ_REG(chip, MS_TRANS_CFG, &val);
2573 if (ms_card->seq_mode) {
2574 if ((ms_card->pre_dir != srb->sc_data_direction)
2575 || ((ms_card->pre_sec_addr + ms_card->pre_sec_cnt) != start_sector)
2576 || (mode_2k && (ms_card->seq_mode & MODE_512_SEQ))
2577 || (!mode_2k && (ms_card->seq_mode & MODE_2K_SEQ))
2578 || !(val & MS_INT_BREQ)
2579 || ((ms_card->total_sec_cnt + sector_cnt) > 0xFE00)) {
2580 ms_card->seq_mode = 0;
2581 ms_card->total_sec_cnt = 0;
2582 if (val & MS_INT_BREQ) {
2583 retval = ms_send_cmd(chip, PRO_STOP, WAIT_INT);
2584 if (retval != STATUS_SUCCESS)
2585 TRACE_RET(chip, STATUS_FAIL);
2587 rtsx_write_register(chip, RBCTL, RB_FLUSH, RB_FLUSH);
2592 if (!ms_card->seq_mode) {
2593 ms_card->total_sec_cnt = 0;
2594 if (sector_cnt >= SEQ_START_CRITERIA) {
2595 if ((ms_card->capacity - start_sector) > 0xFE00)
2598 count = (u16)(ms_card->capacity - start_sector);
2600 if (count > sector_cnt) {
2602 ms_card->seq_mode = MODE_2K_SEQ;
2604 ms_card->seq_mode = MODE_512_SEQ;
2609 retval = mspro_set_rw_cmd(chip, start_sector, count, rw_cmd);
2610 if (retval != STATUS_SUCCESS) {
2611 ms_card->seq_mode = 0;
2612 TRACE_RET(chip, STATUS_FAIL);
2616 retval = ms_transfer_data(chip, trans_mode, rw_tpc, sector_cnt,
2617 WAIT_INT, mode_2k, scsi_sg_count(srb),
2618 scsi_sglist(srb), scsi_bufflen(srb));
2619 if (retval != STATUS_SUCCESS) {
2620 ms_card->seq_mode = 0;
2621 rtsx_read_register(chip, MS_TRANS_CFG, &val);
2622 rtsx_clear_ms_error(chip);
2624 if (detect_card_cd(chip, MS_CARD) != STATUS_SUCCESS) {
2625 chip->rw_need_retry = 0;
2626 dev_dbg(rtsx_dev(chip), "No card exist, exit mspro_rw_multi_sector\n");
2627 TRACE_RET(chip, STATUS_FAIL);
2630 if (val & MS_INT_BREQ)
2631 ms_send_cmd(chip, PRO_STOP, WAIT_INT);
2633 if (val & (MS_CRC16_ERR | MS_RDY_TIMEOUT)) {
2634 dev_dbg(rtsx_dev(chip), "MSPro CRC error, tune clock!\n");
2635 chip->rw_need_retry = 1;
2636 ms_auto_tune_clock(chip);
2639 TRACE_RET(chip, retval);
2642 if (ms_card->seq_mode) {
2643 ms_card->pre_sec_addr = start_sector;
2644 ms_card->pre_sec_cnt = sector_cnt;
2645 ms_card->pre_dir = srb->sc_data_direction;
2646 ms_card->total_sec_cnt += sector_cnt;
2649 return STATUS_SUCCESS;
2652 static int mspro_read_format_progress(struct rtsx_chip *chip,
2653 const int short_data_len)
2655 struct ms_info *ms_card = &(chip->ms_card);
2657 u32 total_progress, cur_progress;
2661 dev_dbg(rtsx_dev(chip), "mspro_read_format_progress, short_data_len = %d\n",
2664 retval = ms_switch_clock(chip);
2665 if (retval != STATUS_SUCCESS) {
2666 ms_card->format_status = FORMAT_FAIL;
2667 TRACE_RET(chip, STATUS_FAIL);
2670 retval = rtsx_read_register(chip, MS_TRANS_CFG, &tmp);
2671 if (retval != STATUS_SUCCESS) {
2672 ms_card->format_status = FORMAT_FAIL;
2673 TRACE_RET(chip, STATUS_FAIL);
2676 if (!(tmp & MS_INT_BREQ)) {
2677 if ((tmp & (MS_INT_CED | MS_INT_BREQ | MS_INT_CMDNK | MS_INT_ERR)) == MS_INT_CED) {
2678 ms_card->format_status = FORMAT_SUCCESS;
2679 return STATUS_SUCCESS;
2681 ms_card->format_status = FORMAT_FAIL;
2682 TRACE_RET(chip, STATUS_FAIL);
2685 if (short_data_len >= 256)
2688 cnt = (u8)short_data_len;
2690 retval = rtsx_write_register(chip, MS_CFG, MS_NO_CHECK_INT,
2692 if (retval != STATUS_SUCCESS) {
2693 ms_card->format_status = FORMAT_FAIL;
2694 TRACE_RET(chip, STATUS_FAIL);
2697 retval = ms_read_bytes(chip, PRO_READ_SHORT_DATA, cnt, WAIT_INT,
2699 if (retval != STATUS_SUCCESS) {
2700 ms_card->format_status = FORMAT_FAIL;
2701 TRACE_RET(chip, STATUS_FAIL);
2704 total_progress = (data[0] << 24) | (data[1] << 16) |
2705 (data[2] << 8) | data[3];
2706 cur_progress = (data[4] << 24) | (data[5] << 16) |
2707 (data[6] << 8) | data[7];
2709 dev_dbg(rtsx_dev(chip), "total_progress = %d, cur_progress = %d\n",
2710 total_progress, cur_progress);
2712 if (total_progress == 0) {
2713 ms_card->progress = 0;
2715 u64 ulltmp = (u64)cur_progress * (u64)65535;
2717 do_div(ulltmp, total_progress);
2718 ms_card->progress = (u16)ulltmp;
2720 dev_dbg(rtsx_dev(chip), "progress = %d\n", ms_card->progress);
2722 for (i = 0; i < 5000; i++) {
2723 retval = rtsx_read_register(chip, MS_TRANS_CFG, &tmp);
2724 if (retval != STATUS_SUCCESS) {
2725 ms_card->format_status = FORMAT_FAIL;
2726 TRACE_RET(chip, STATUS_FAIL);
2728 if (tmp & (MS_INT_CED | MS_INT_CMDNK |
2729 MS_INT_BREQ | MS_INT_ERR))
2735 retval = rtsx_write_register(chip, MS_CFG, MS_NO_CHECK_INT, 0);
2736 if (retval != STATUS_SUCCESS) {
2737 ms_card->format_status = FORMAT_FAIL;
2738 TRACE_RET(chip, STATUS_FAIL);
2742 ms_card->format_status = FORMAT_FAIL;
2743 TRACE_RET(chip, STATUS_FAIL);
2746 if (tmp & (MS_INT_CMDNK | MS_INT_ERR)) {
2747 ms_card->format_status = FORMAT_FAIL;
2748 TRACE_RET(chip, STATUS_FAIL);
2751 if (tmp & MS_INT_CED) {
2752 ms_card->format_status = FORMAT_SUCCESS;
2753 ms_card->pro_under_formatting = 0;
2754 } else if (tmp & MS_INT_BREQ) {
2755 ms_card->format_status = FORMAT_IN_PROGRESS;
2757 ms_card->format_status = FORMAT_FAIL;
2758 ms_card->pro_under_formatting = 0;
2759 TRACE_RET(chip, STATUS_FAIL);
2762 return STATUS_SUCCESS;
2765 void mspro_polling_format_status(struct rtsx_chip *chip)
2767 struct ms_info *ms_card = &(chip->ms_card);
2770 if (ms_card->pro_under_formatting &&
2771 (rtsx_get_stat(chip) != RTSX_STAT_SS)) {
2772 rtsx_set_stat(chip, RTSX_STAT_RUN);
2774 for (i = 0; i < 65535; i++) {
2775 mspro_read_format_progress(chip, MS_SHORT_DATA_LEN);
2776 if (ms_card->format_status != FORMAT_IN_PROGRESS)
2782 int mspro_format(struct scsi_cmnd *srb, struct rtsx_chip *chip,
2783 int short_data_len, int quick_format)
2785 struct ms_info *ms_card = &(chip->ms_card);
2790 retval = ms_switch_clock(chip);
2791 if (retval != STATUS_SUCCESS)
2792 TRACE_RET(chip, STATUS_FAIL);
2794 retval = ms_set_rw_reg_addr(chip, 0x00, 0x00, Pro_TPCParm, 0x01);
2795 if (retval != STATUS_SUCCESS)
2796 TRACE_RET(chip, STATUS_FAIL);
2799 switch (short_data_len) {
2815 for (i = 0; i < MS_MAX_RETRY_COUNT; i++) {
2816 retval = ms_write_bytes(chip, PRO_WRITE_REG, 1,
2817 NO_WAIT_INT, buf, 2);
2818 if (retval == STATUS_SUCCESS)
2821 if (i == MS_MAX_RETRY_COUNT)
2822 TRACE_RET(chip, STATUS_FAIL);
2829 retval = mspro_set_rw_cmd(chip, 0, para, PRO_FORMAT);
2830 if (retval != STATUS_SUCCESS)
2831 TRACE_RET(chip, STATUS_FAIL);
2833 RTSX_READ_REG(chip, MS_TRANS_CFG, &tmp);
2835 if (tmp & (MS_INT_CMDNK | MS_INT_ERR))
2836 TRACE_RET(chip, STATUS_FAIL);
2838 if ((tmp & (MS_INT_BREQ | MS_INT_CED)) == MS_INT_BREQ) {
2839 ms_card->pro_under_formatting = 1;
2840 ms_card->progress = 0;
2841 ms_card->format_status = FORMAT_IN_PROGRESS;
2842 return STATUS_SUCCESS;
2845 if (tmp & MS_INT_CED) {
2846 ms_card->pro_under_formatting = 0;
2847 ms_card->progress = 0;
2848 ms_card->format_status = FORMAT_SUCCESS;
2849 set_sense_type(chip, SCSI_LUN(srb), SENSE_TYPE_NO_SENSE);
2850 return STATUS_SUCCESS;
2853 TRACE_RET(chip, STATUS_FAIL);
2857 static int ms_read_multiple_pages(struct rtsx_chip *chip, u16 phy_blk,
2858 u16 log_blk, u8 start_page, u8 end_page,
2859 u8 *buf, unsigned int *index,
2860 unsigned int *offset)
2862 struct ms_info *ms_card = &(chip->ms_card);
2864 u8 extra[MS_EXTRA_SIZE], page_addr, val, trans_cfg, data[6];
2867 retval = ms_read_extra_data(chip, phy_blk, start_page,
2868 extra, MS_EXTRA_SIZE);
2869 if (retval == STATUS_SUCCESS) {
2870 if ((extra[1] & 0x30) != 0x30) {
2871 ms_set_err_code(chip, MS_FLASH_READ_ERROR);
2872 TRACE_RET(chip, STATUS_FAIL);
2876 retval = ms_set_rw_reg_addr(chip, OverwriteFlag, MS_EXTRA_SIZE,
2878 if (retval != STATUS_SUCCESS)
2879 TRACE_RET(chip, STATUS_FAIL);
2881 if (CHK_MS4BIT(ms_card))
2887 data[2] = (u8)(phy_blk >> 8);
2888 data[3] = (u8)phy_blk;
2890 data[5] = start_page;
2892 for (i = 0; i < MS_MAX_RETRY_COUNT; i++) {
2893 retval = ms_write_bytes(chip, WRITE_REG, 6, NO_WAIT_INT,
2895 if (retval == STATUS_SUCCESS)
2898 if (i == MS_MAX_RETRY_COUNT)
2899 TRACE_RET(chip, STATUS_FAIL);
2901 ms_set_err_code(chip, MS_NO_ERROR);
2903 retval = ms_send_cmd(chip, BLOCK_READ, WAIT_INT);
2904 if (retval != STATUS_SUCCESS)
2905 TRACE_RET(chip, STATUS_FAIL);
2909 for (page_addr = start_page; page_addr < end_page; page_addr++) {
2910 ms_set_err_code(chip, MS_NO_ERROR);
2912 if (detect_card_cd(chip, MS_CARD) != STATUS_SUCCESS) {
2913 ms_set_err_code(chip, MS_NO_CARD);
2914 TRACE_RET(chip, STATUS_FAIL);
2917 retval = ms_read_bytes(chip, GET_INT, 1, NO_WAIT_INT, &val, 1);
2918 if (retval != STATUS_SUCCESS)
2919 TRACE_RET(chip, STATUS_FAIL);
2921 if (val & INT_REG_CMDNK) {
2922 ms_set_err_code(chip, MS_CMD_NK);
2923 TRACE_RET(chip, STATUS_FAIL);
2925 if (val & INT_REG_ERR) {
2926 if (val & INT_REG_BREQ) {
2927 retval = ms_read_status_reg(chip);
2928 if (retval != STATUS_SUCCESS) {
2929 if (!(chip->card_wp & MS_CARD)) {
2931 ms_set_page_status(log_blk, setPS_NG, extra, MS_EXTRA_SIZE);
2932 ms_write_extra_data(chip, phy_blk,
2933 page_addr, extra, MS_EXTRA_SIZE);
2935 ms_set_err_code(chip, MS_FLASH_READ_ERROR);
2936 TRACE_RET(chip, STATUS_FAIL);
2939 ms_set_err_code(chip, MS_FLASH_READ_ERROR);
2940 TRACE_RET(chip, STATUS_FAIL);
2943 if (!(val & INT_REG_BREQ)) {
2944 ms_set_err_code(chip, MS_BREQ_ERROR);
2945 TRACE_RET(chip, STATUS_FAIL);
2949 if (page_addr == (end_page - 1)) {
2950 if (!(val & INT_REG_CED)) {
2951 retval = ms_send_cmd(chip, BLOCK_END, WAIT_INT);
2952 if (retval != STATUS_SUCCESS)
2953 TRACE_RET(chip, STATUS_FAIL);
2956 retval = ms_read_bytes(chip, GET_INT, 1, NO_WAIT_INT,
2958 if (retval != STATUS_SUCCESS)
2959 TRACE_RET(chip, STATUS_FAIL);
2961 if (!(val & INT_REG_CED)) {
2962 ms_set_err_code(chip, MS_FLASH_READ_ERROR);
2963 TRACE_RET(chip, STATUS_FAIL);
2966 trans_cfg = NO_WAIT_INT;
2968 trans_cfg = WAIT_INT;
2971 rtsx_init_cmd(chip);
2973 rtsx_add_cmd(chip, WRITE_REG_CMD, MS_TPC, 0xFF, READ_PAGE_DATA);
2974 rtsx_add_cmd(chip, WRITE_REG_CMD, MS_TRANS_CFG,
2976 rtsx_add_cmd(chip, WRITE_REG_CMD, CARD_DATA_SOURCE,
2979 trans_dma_enable(DMA_FROM_DEVICE, chip, 512, DMA_512);
2981 rtsx_add_cmd(chip, WRITE_REG_CMD, MS_TRANSFER, 0xFF,
2982 MS_TRANSFER_START | MS_TM_NORMAL_READ);
2983 rtsx_add_cmd(chip, CHECK_REG_CMD, MS_TRANSFER,
2984 MS_TRANSFER_END, MS_TRANSFER_END);
2986 rtsx_send_cmd_no_wait(chip);
2988 retval = rtsx_transfer_data_partial(chip, MS_CARD, ptr,
2989 512, scsi_sg_count(chip->srb),
2990 index, offset, DMA_FROM_DEVICE,
2993 if (retval == -ETIMEDOUT) {
2994 ms_set_err_code(chip, MS_TO_ERROR);
2995 rtsx_clear_ms_error(chip);
2996 TRACE_RET(chip, STATUS_TIMEDOUT);
2999 retval = rtsx_read_register(chip, MS_TRANS_CFG, &val);
3000 if (retval != STATUS_SUCCESS) {
3001 ms_set_err_code(chip, MS_TO_ERROR);
3002 rtsx_clear_ms_error(chip);
3003 TRACE_RET(chip, STATUS_TIMEDOUT);
3005 if (val & (MS_CRC16_ERR | MS_RDY_TIMEOUT)) {
3006 ms_set_err_code(chip, MS_CRC16_ERROR);
3007 rtsx_clear_ms_error(chip);
3008 TRACE_RET(chip, STATUS_FAIL);
3012 if (scsi_sg_count(chip->srb) == 0)
3016 return STATUS_SUCCESS;
3019 static int ms_write_multiple_pages(struct rtsx_chip *chip, u16 old_blk,
3020 u16 new_blk, u16 log_blk, u8 start_page,
3021 u8 end_page, u8 *buf, unsigned int *index,
3022 unsigned int *offset)
3024 struct ms_info *ms_card = &(chip->ms_card);
3026 u8 page_addr, val, data[16];
3030 retval = ms_set_rw_reg_addr(chip, OverwriteFlag, MS_EXTRA_SIZE,
3032 if (retval != STATUS_SUCCESS)
3033 TRACE_RET(chip, STATUS_FAIL);
3035 if (CHK_MS4BIT(ms_card))
3041 data[2] = (u8)(old_blk >> 8);
3042 data[3] = (u8)old_blk;
3048 retval = ms_write_bytes(chip, WRITE_REG, 7, NO_WAIT_INT,
3050 if (retval != STATUS_SUCCESS)
3051 TRACE_RET(chip, STATUS_FAIL);
3053 retval = ms_send_cmd(chip, BLOCK_WRITE, WAIT_INT);
3054 if (retval != STATUS_SUCCESS)
3055 TRACE_RET(chip, STATUS_FAIL);
3057 ms_set_err_code(chip, MS_NO_ERROR);
3058 retval = ms_transfer_tpc(chip, MS_TM_READ_BYTES, GET_INT, 1,
3060 if (retval != STATUS_SUCCESS)
3061 TRACE_RET(chip, STATUS_FAIL);
3064 retval = ms_set_rw_reg_addr(chip, OverwriteFlag, MS_EXTRA_SIZE,
3065 SystemParm, (6 + MS_EXTRA_SIZE));
3066 if (retval != STATUS_SUCCESS)
3067 TRACE_RET(chip, STATUS_FAIL);
3069 ms_set_err_code(chip, MS_NO_ERROR);
3071 if (CHK_MS4BIT(ms_card))
3077 data[2] = (u8)(new_blk >> 8);
3078 data[3] = (u8)new_blk;
3079 if ((end_page - start_page) == 1)
3084 data[5] = start_page;
3087 data[8] = (u8)(log_blk >> 8);
3088 data[9] = (u8)log_blk;
3090 for (i = 0x0A; i < 0x10; i++)
3093 for (i = 0; i < MS_MAX_RETRY_COUNT; i++) {
3094 retval = ms_write_bytes(chip, WRITE_REG, 6 + MS_EXTRA_SIZE,
3095 NO_WAIT_INT, data, 16);
3096 if (retval == STATUS_SUCCESS)
3099 if (i == MS_MAX_RETRY_COUNT)
3100 TRACE_RET(chip, STATUS_FAIL);
3102 for (i = 0; i < MS_MAX_RETRY_COUNT; i++) {
3103 retval = ms_send_cmd(chip, BLOCK_WRITE, WAIT_INT);
3104 if (retval == STATUS_SUCCESS)
3107 if (i == MS_MAX_RETRY_COUNT)
3108 TRACE_RET(chip, STATUS_FAIL);
3110 retval = ms_read_bytes(chip, GET_INT, 1, NO_WAIT_INT, &val, 1);
3111 if (retval != STATUS_SUCCESS)
3112 TRACE_RET(chip, STATUS_FAIL);
3115 for (page_addr = start_page; page_addr < end_page; page_addr++) {
3116 ms_set_err_code(chip, MS_NO_ERROR);
3118 if (detect_card_cd(chip, MS_CARD) != STATUS_SUCCESS) {
3119 ms_set_err_code(chip, MS_NO_CARD);
3120 TRACE_RET(chip, STATUS_FAIL);
3123 if (val & INT_REG_CMDNK) {
3124 ms_set_err_code(chip, MS_CMD_NK);
3125 TRACE_RET(chip, STATUS_FAIL);
3127 if (val & INT_REG_ERR) {
3128 ms_set_err_code(chip, MS_FLASH_WRITE_ERROR);
3129 TRACE_RET(chip, STATUS_FAIL);
3131 if (!(val & INT_REG_BREQ)) {
3132 ms_set_err_code(chip, MS_BREQ_ERROR);
3133 TRACE_RET(chip, STATUS_FAIL);
3138 rtsx_init_cmd(chip);
3140 rtsx_add_cmd(chip, WRITE_REG_CMD, MS_TPC,
3141 0xFF, WRITE_PAGE_DATA);
3142 rtsx_add_cmd(chip, WRITE_REG_CMD, MS_TRANS_CFG,
3144 rtsx_add_cmd(chip, WRITE_REG_CMD, CARD_DATA_SOURCE,
3147 trans_dma_enable(DMA_TO_DEVICE, chip, 512, DMA_512);
3149 rtsx_add_cmd(chip, WRITE_REG_CMD, MS_TRANSFER, 0xFF,
3150 MS_TRANSFER_START | MS_TM_NORMAL_WRITE);
3151 rtsx_add_cmd(chip, CHECK_REG_CMD, MS_TRANSFER,
3152 MS_TRANSFER_END, MS_TRANSFER_END);
3154 rtsx_send_cmd_no_wait(chip);
3156 retval = rtsx_transfer_data_partial(chip, MS_CARD, ptr,
3157 512, scsi_sg_count(chip->srb),
3158 index, offset, DMA_TO_DEVICE,
3161 ms_set_err_code(chip, MS_TO_ERROR);
3162 rtsx_clear_ms_error(chip);
3164 if (retval == -ETIMEDOUT)
3165 TRACE_RET(chip, STATUS_TIMEDOUT);
3167 TRACE_RET(chip, STATUS_FAIL);
3170 retval = ms_read_bytes(chip, GET_INT, 1, NO_WAIT_INT, &val, 1);
3171 if (retval != STATUS_SUCCESS)
3172 TRACE_RET(chip, STATUS_FAIL);
3174 if ((end_page - start_page) == 1) {
3175 if (!(val & INT_REG_CED)) {
3176 ms_set_err_code(chip, MS_FLASH_WRITE_ERROR);
3177 TRACE_RET(chip, STATUS_FAIL);
3180 if (page_addr == (end_page - 1)) {
3181 if (!(val & INT_REG_CED)) {
3182 retval = ms_send_cmd(chip, BLOCK_END,
3184 if (retval != STATUS_SUCCESS)
3185 TRACE_RET(chip, STATUS_FAIL);
3188 retval = ms_read_bytes(chip, GET_INT, 1,
3189 NO_WAIT_INT, &val, 1);
3190 if (retval != STATUS_SUCCESS)
3191 TRACE_RET(chip, STATUS_FAIL);
3194 if ((page_addr == (end_page - 1)) ||
3195 (page_addr == ms_card->page_off)) {
3196 if (!(val & INT_REG_CED)) {
3197 ms_set_err_code(chip,
3198 MS_FLASH_WRITE_ERROR);
3199 TRACE_RET(chip, STATUS_FAIL);
3204 if (scsi_sg_count(chip->srb) == 0)
3208 return STATUS_SUCCESS;
3212 static int ms_finish_write(struct rtsx_chip *chip, u16 old_blk, u16 new_blk,
3213 u16 log_blk, u8 page_off)
3215 struct ms_info *ms_card = &(chip->ms_card);
3218 retval = ms_copy_page(chip, old_blk, new_blk, log_blk,
3219 page_off, ms_card->page_off + 1);
3220 if (retval != STATUS_SUCCESS)
3221 TRACE_RET(chip, STATUS_FAIL);
3223 seg_no = old_blk >> 9;
3225 if (MS_TST_BAD_BLOCK_FLG(ms_card)) {
3226 MS_CLR_BAD_BLOCK_FLG(ms_card);
3227 ms_set_bad_block(chip, old_blk);
3229 retval = ms_erase_block(chip, old_blk);
3230 if (retval == STATUS_SUCCESS)
3231 ms_set_unused_block(chip, old_blk);
3234 ms_set_l2p_tbl(chip, seg_no, log_blk - ms_start_idx[seg_no], new_blk);
3236 return STATUS_SUCCESS;
3239 static int ms_prepare_write(struct rtsx_chip *chip, u16 old_blk, u16 new_blk,
3240 u16 log_blk, u8 start_page)
3245 retval = ms_copy_page(chip, old_blk, new_blk, log_blk,
3247 if (retval != STATUS_SUCCESS)
3248 TRACE_RET(chip, STATUS_FAIL);
3251 return STATUS_SUCCESS;
3254 #ifdef MS_DELAY_WRITE
3255 int ms_delay_write(struct rtsx_chip *chip)
3257 struct ms_info *ms_card = &(chip->ms_card);
3258 struct ms_delay_write_tag *delay_write = &(ms_card->delay_write);
3261 if (delay_write->delay_write_flag) {
3262 retval = ms_set_init_para(chip);
3263 if (retval != STATUS_SUCCESS)
3264 TRACE_RET(chip, STATUS_FAIL);
3266 delay_write->delay_write_flag = 0;
3267 retval = ms_finish_write(chip,
3268 delay_write->old_phyblock,
3269 delay_write->new_phyblock,
3270 delay_write->logblock,
3271 delay_write->pageoff);
3272 if (retval != STATUS_SUCCESS)
3273 TRACE_RET(chip, STATUS_FAIL);
3276 return STATUS_SUCCESS;
3280 static inline void ms_rw_fail(struct scsi_cmnd *srb, struct rtsx_chip *chip)
3282 if (srb->sc_data_direction == DMA_FROM_DEVICE)
3283 set_sense_type(chip, SCSI_LUN(srb),
3284 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
3286 set_sense_type(chip, SCSI_LUN(srb), SENSE_TYPE_MEDIA_WRITE_ERR);
3289 static int ms_rw_multi_sector(struct scsi_cmnd *srb, struct rtsx_chip *chip,
3290 u32 start_sector, u16 sector_cnt)
3292 struct ms_info *ms_card = &(chip->ms_card);
3293 unsigned int lun = SCSI_LUN(srb);
3295 unsigned int index = 0, offset = 0;
3296 u16 old_blk = 0, new_blk = 0, log_blk, total_sec_cnt = sector_cnt;
3297 u8 start_page, end_page = 0, page_cnt;
3299 #ifdef MS_DELAY_WRITE
3300 struct ms_delay_write_tag *delay_write = &(ms_card->delay_write);
3303 ms_set_err_code(chip, MS_NO_ERROR);
3305 ms_card->cleanup_counter = 0;
3307 ptr = (u8 *)scsi_sglist(srb);
3309 retval = ms_switch_clock(chip);
3310 if (retval != STATUS_SUCCESS) {
3311 ms_rw_fail(srb, chip);
3312 TRACE_RET(chip, STATUS_FAIL);
3315 log_blk = (u16)(start_sector >> ms_card->block_shift);
3316 start_page = (u8)(start_sector & ms_card->page_off);
3318 for (seg_no = 0; seg_no < ARRAY_SIZE(ms_start_idx) - 1; seg_no++) {
3319 if (log_blk < ms_start_idx[seg_no+1])
3323 if (ms_card->segment[seg_no].build_flag == 0) {
3324 retval = ms_build_l2p_tbl(chip, seg_no);
3325 if (retval != STATUS_SUCCESS) {
3326 chip->card_fail |= MS_CARD;
3327 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
3328 TRACE_RET(chip, STATUS_FAIL);
3332 if (srb->sc_data_direction == DMA_TO_DEVICE) {
3333 #ifdef MS_DELAY_WRITE
3334 if (delay_write->delay_write_flag &&
3335 (delay_write->logblock == log_blk) &&
3336 (start_page > delay_write->pageoff)) {
3337 delay_write->delay_write_flag = 0;
3338 retval = ms_copy_page(chip,
3339 delay_write->old_phyblock,
3340 delay_write->new_phyblock, log_blk,
3341 delay_write->pageoff, start_page);
3342 if (retval != STATUS_SUCCESS) {
3343 set_sense_type(chip, lun,
3344 SENSE_TYPE_MEDIA_WRITE_ERR);
3345 TRACE_RET(chip, STATUS_FAIL);
3347 old_blk = delay_write->old_phyblock;
3348 new_blk = delay_write->new_phyblock;
3349 } else if (delay_write->delay_write_flag &&
3350 (delay_write->logblock == log_blk) &&
3351 (start_page == delay_write->pageoff)) {
3352 delay_write->delay_write_flag = 0;
3353 old_blk = delay_write->old_phyblock;
3354 new_blk = delay_write->new_phyblock;
3356 retval = ms_delay_write(chip);
3357 if (retval != STATUS_SUCCESS) {
3358 set_sense_type(chip, lun,
3359 SENSE_TYPE_MEDIA_WRITE_ERR);
3360 TRACE_RET(chip, STATUS_FAIL);
3363 old_blk = ms_get_l2p_tbl(chip, seg_no,
3364 log_blk - ms_start_idx[seg_no]);
3365 new_blk = ms_get_unused_block(chip, seg_no);
3366 if ((old_blk == 0xFFFF) || (new_blk == 0xFFFF)) {
3367 set_sense_type(chip, lun,
3368 SENSE_TYPE_MEDIA_WRITE_ERR);
3369 TRACE_RET(chip, STATUS_FAIL);
3372 retval = ms_prepare_write(chip, old_blk, new_blk,
3373 log_blk, start_page);
3374 if (retval != STATUS_SUCCESS) {
3375 if (detect_card_cd(chip, MS_CARD) != STATUS_SUCCESS) {
3376 set_sense_type(chip, lun,
3377 SENSE_TYPE_MEDIA_NOT_PRESENT);
3378 TRACE_RET(chip, STATUS_FAIL);
3380 set_sense_type(chip, lun,
3381 SENSE_TYPE_MEDIA_WRITE_ERR);
3382 TRACE_RET(chip, STATUS_FAIL);
3384 #ifdef MS_DELAY_WRITE
3388 #ifdef MS_DELAY_WRITE
3389 retval = ms_delay_write(chip);
3390 if (retval != STATUS_SUCCESS) {
3391 if (detect_card_cd(chip, MS_CARD) != STATUS_SUCCESS) {
3392 set_sense_type(chip, lun,
3393 SENSE_TYPE_MEDIA_NOT_PRESENT);
3394 TRACE_RET(chip, STATUS_FAIL);
3396 set_sense_type(chip, lun,
3397 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
3398 TRACE_RET(chip, STATUS_FAIL);
3401 old_blk = ms_get_l2p_tbl(chip, seg_no,
3402 log_blk - ms_start_idx[seg_no]);
3403 if (old_blk == 0xFFFF) {
3404 set_sense_type(chip, lun,
3405 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
3406 TRACE_RET(chip, STATUS_FAIL);
3410 dev_dbg(rtsx_dev(chip), "seg_no = %d, old_blk = 0x%x, new_blk = 0x%x\n",
3411 seg_no, old_blk, new_blk);
3413 while (total_sec_cnt) {
3414 if ((start_page + total_sec_cnt) > (ms_card->page_off + 1))
3415 end_page = ms_card->page_off + 1;
3417 end_page = start_page + (u8)total_sec_cnt;
3419 page_cnt = end_page - start_page;
3421 dev_dbg(rtsx_dev(chip), "start_page = %d, end_page = %d, page_cnt = %d\n",
3422 start_page, end_page, page_cnt);
3424 if (srb->sc_data_direction == DMA_FROM_DEVICE) {
3425 retval = ms_read_multiple_pages(chip,
3426 old_blk, log_blk, start_page, end_page,
3427 ptr, &index, &offset);
3429 retval = ms_write_multiple_pages(chip, old_blk,
3430 new_blk, log_blk, start_page, end_page,
3431 ptr, &index, &offset);
3434 if (retval != STATUS_SUCCESS) {
3435 toggle_gpio(chip, 1);
3436 if (detect_card_cd(chip, MS_CARD) != STATUS_SUCCESS) {
3437 set_sense_type(chip, lun,
3438 SENSE_TYPE_MEDIA_NOT_PRESENT);
3439 TRACE_RET(chip, STATUS_FAIL);
3441 ms_rw_fail(srb, chip);
3442 TRACE_RET(chip, STATUS_FAIL);
3445 if (srb->sc_data_direction == DMA_TO_DEVICE) {
3446 if (end_page == (ms_card->page_off + 1)) {
3447 retval = ms_erase_block(chip, old_blk);
3448 if (retval == STATUS_SUCCESS)
3449 ms_set_unused_block(chip, old_blk);
3451 ms_set_l2p_tbl(chip, seg_no,
3452 log_blk - ms_start_idx[seg_no],
3457 total_sec_cnt -= page_cnt;
3458 if (scsi_sg_count(srb) == 0)
3459 ptr += page_cnt * 512;
3461 if (total_sec_cnt == 0)
3466 for (seg_no = 0; seg_no < ARRAY_SIZE(ms_start_idx) - 1;
3468 if (log_blk < ms_start_idx[seg_no+1])
3472 if (ms_card->segment[seg_no].build_flag == 0) {
3473 retval = ms_build_l2p_tbl(chip, seg_no);
3474 if (retval != STATUS_SUCCESS) {
3475 chip->card_fail |= MS_CARD;
3476 set_sense_type(chip, lun,
3477 SENSE_TYPE_MEDIA_NOT_PRESENT);
3478 TRACE_RET(chip, STATUS_FAIL);
3482 old_blk = ms_get_l2p_tbl(chip, seg_no,
3483 log_blk - ms_start_idx[seg_no]);
3484 if (old_blk == 0xFFFF) {
3485 ms_rw_fail(srb, chip);
3486 TRACE_RET(chip, STATUS_FAIL);
3489 if (srb->sc_data_direction == DMA_TO_DEVICE) {
3490 new_blk = ms_get_unused_block(chip, seg_no);
3491 if (new_blk == 0xFFFF) {
3492 ms_rw_fail(srb, chip);
3493 TRACE_RET(chip, STATUS_FAIL);
3497 dev_dbg(rtsx_dev(chip), "seg_no = %d, old_blk = 0x%x, new_blk = 0x%x\n",
3498 seg_no, old_blk, new_blk);
3503 if (srb->sc_data_direction == DMA_TO_DEVICE) {
3504 if (end_page < (ms_card->page_off + 1)) {
3505 #ifdef MS_DELAY_WRITE
3506 delay_write->delay_write_flag = 1;
3507 delay_write->old_phyblock = old_blk;
3508 delay_write->new_phyblock = new_blk;
3509 delay_write->logblock = log_blk;
3510 delay_write->pageoff = end_page;
3512 retval = ms_finish_write(chip, old_blk, new_blk,
3514 if (retval != STATUS_SUCCESS) {
3515 if (detect_card_cd(chip, MS_CARD) != STATUS_SUCCESS) {
3516 set_sense_type(chip, lun,
3517 SENSE_TYPE_MEDIA_NOT_PRESENT);
3518 TRACE_RET(chip, STATUS_FAIL);
3521 ms_rw_fail(srb, chip);
3522 TRACE_RET(chip, STATUS_FAIL);
3528 scsi_set_resid(srb, 0);
3530 return STATUS_SUCCESS;
3533 int ms_rw(struct scsi_cmnd *srb, struct rtsx_chip *chip,
3534 u32 start_sector, u16 sector_cnt)
3536 struct ms_info *ms_card = &(chip->ms_card);
3539 if (CHK_MSPRO(ms_card))
3540 retval = mspro_rw_multi_sector(srb, chip, start_sector,
3543 retval = ms_rw_multi_sector(srb, chip, start_sector,
3550 void ms_free_l2p_tbl(struct rtsx_chip *chip)
3552 struct ms_info *ms_card = &(chip->ms_card);
3555 if (ms_card->segment != NULL) {
3556 for (i = 0; i < ms_card->segment_cnt; i++) {
3557 if (ms_card->segment[i].l2p_table != NULL) {
3558 vfree(ms_card->segment[i].l2p_table);
3559 ms_card->segment[i].l2p_table = NULL;
3561 if (ms_card->segment[i].free_table != NULL) {
3562 vfree(ms_card->segment[i].free_table);
3563 ms_card->segment[i].free_table = NULL;
3566 vfree(ms_card->segment);
3567 ms_card->segment = NULL;
3571 #ifdef SUPPORT_MAGIC_GATE
3573 #ifdef READ_BYTES_WAIT_INT
3574 static int ms_poll_int(struct rtsx_chip *chip)
3579 rtsx_init_cmd(chip);
3581 rtsx_add_cmd(chip, CHECK_REG_CMD, MS_TRANS_CFG, MS_INT_CED, MS_INT_CED);
3583 retval = rtsx_send_cmd(chip, MS_CARD, 5000);
3584 if (retval != STATUS_SUCCESS)
3585 TRACE_RET(chip, STATUS_FAIL);
3587 val = *rtsx_get_cmd_data(chip);
3588 if (val & MS_INT_ERR)
3589 TRACE_RET(chip, STATUS_FAIL);
3591 return STATUS_SUCCESS;
3595 #ifdef MS_SAMPLE_INT_ERR
3596 static int check_ms_err(struct rtsx_chip *chip)
3601 retval = rtsx_read_register(chip, MS_TRANSFER, &val);
3602 if (retval != STATUS_SUCCESS)
3604 if (val & MS_TRANSFER_ERR)
3607 retval = rtsx_read_register(chip, MS_TRANS_CFG, &val);
3608 if (retval != STATUS_SUCCESS)
3611 if (val & (MS_INT_ERR | MS_INT_CMDNK))
3617 static int check_ms_err(struct rtsx_chip *chip)
3622 retval = rtsx_read_register(chip, MS_TRANSFER, &val);
3623 if (retval != STATUS_SUCCESS)
3625 if (val & MS_TRANSFER_ERR)
3632 static int mg_send_ex_cmd(struct rtsx_chip *chip, u8 cmd, u8 entry_num)
3643 data[6] = entry_num;
3646 for (i = 0; i < MS_MAX_RETRY_COUNT; i++) {
3647 retval = ms_write_bytes(chip, PRO_EX_SET_CMD, 7, WAIT_INT,
3649 if (retval == STATUS_SUCCESS)
3652 if (i == MS_MAX_RETRY_COUNT)
3653 TRACE_RET(chip, STATUS_FAIL);
3655 if (check_ms_err(chip)) {
3656 rtsx_clear_ms_error(chip);
3657 TRACE_RET(chip, STATUS_FAIL);
3660 return STATUS_SUCCESS;
3663 static int mg_set_tpc_para_sub(struct rtsx_chip *chip, int type,
3670 retval = ms_set_rw_reg_addr(chip, 0, 0, Pro_TPCParm, 1);
3672 retval = ms_set_rw_reg_addr(chip, 0, 0, Pro_DataCount1, 6);
3674 if (retval != STATUS_SUCCESS)
3675 TRACE_RET(chip, STATUS_FAIL);
3683 buf[5] = mg_entry_num;
3685 retval = ms_write_bytes(chip, PRO_WRITE_REG, (type == 0) ? 1 : 6,
3686 NO_WAIT_INT, buf, 6);
3687 if (retval != STATUS_SUCCESS)
3688 TRACE_RET(chip, STATUS_FAIL);
3690 return STATUS_SUCCESS;
3693 int mg_set_leaf_id(struct scsi_cmnd *srb, struct rtsx_chip *chip)
3697 unsigned int lun = SCSI_LUN(srb);
3698 u8 buf1[32], buf2[12];
3700 if (scsi_bufflen(srb) < 12) {
3701 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
3702 TRACE_RET(chip, STATUS_FAIL);
3705 ms_cleanup_work(chip);
3707 retval = ms_switch_clock(chip);
3708 if (retval != STATUS_SUCCESS)
3709 TRACE_RET(chip, STATUS_FAIL);
3711 retval = mg_send_ex_cmd(chip, MG_SET_LID, 0);
3712 if (retval != STATUS_SUCCESS) {
3713 set_sense_type(chip, lun, SENSE_TYPE_MG_KEY_FAIL_NOT_ESTAB);
3714 TRACE_RET(chip, STATUS_FAIL);
3717 memset(buf1, 0, 32);
3718 rtsx_stor_get_xfer_buf(buf2, min_t(int, 12, scsi_bufflen(srb)), srb);
3719 for (i = 0; i < 8; i++)
3720 buf1[8+i] = buf2[4+i];
3722 retval = ms_write_bytes(chip, PRO_WRITE_SHORT_DATA, 32, WAIT_INT,
3724 if (retval != STATUS_SUCCESS) {
3725 set_sense_type(chip, lun, SENSE_TYPE_MG_KEY_FAIL_NOT_ESTAB);
3726 TRACE_RET(chip, STATUS_FAIL);
3728 if (check_ms_err(chip)) {
3729 set_sense_type(chip, lun, SENSE_TYPE_MG_KEY_FAIL_NOT_ESTAB);
3730 rtsx_clear_ms_error(chip);
3731 TRACE_RET(chip, STATUS_FAIL);
3734 return STATUS_SUCCESS;
3737 int mg_get_local_EKB(struct scsi_cmnd *srb, struct rtsx_chip *chip)
3739 int retval = STATUS_FAIL;
3741 unsigned int lun = SCSI_LUN(srb);
3744 ms_cleanup_work(chip);
3746 retval = ms_switch_clock(chip);
3747 if (retval != STATUS_SUCCESS)
3748 TRACE_RET(chip, STATUS_FAIL);
3750 buf = kmalloc(1540, GFP_KERNEL);
3752 TRACE_RET(chip, STATUS_ERROR);
3759 retval = mg_send_ex_cmd(chip, MG_GET_LEKB, 0);
3760 if (retval != STATUS_SUCCESS) {
3761 set_sense_type(chip, lun, SENSE_TYPE_MG_KEY_FAIL_NOT_AUTHEN);
3762 TRACE_GOTO(chip, GetEKBFinish);
3765 retval = ms_transfer_data(chip, MS_TM_AUTO_READ, PRO_READ_LONG_DATA,
3766 3, WAIT_INT, 0, 0, buf + 4, 1536);
3767 if (retval != STATUS_SUCCESS) {
3768 set_sense_type(chip, lun, SENSE_TYPE_MG_KEY_FAIL_NOT_AUTHEN);
3769 rtsx_clear_ms_error(chip);
3770 TRACE_GOTO(chip, GetEKBFinish);
3772 if (check_ms_err(chip)) {
3773 set_sense_type(chip, lun, SENSE_TYPE_MG_KEY_FAIL_NOT_AUTHEN);
3774 rtsx_clear_ms_error(chip);
3775 TRACE_RET(chip, STATUS_FAIL);
3778 bufflen = min_t(int, 1052, scsi_bufflen(srb));
3779 rtsx_stor_set_xfer_buf(buf, bufflen, srb);
3786 int mg_chg(struct scsi_cmnd *srb, struct rtsx_chip *chip)
3788 struct ms_info *ms_card = &(chip->ms_card);
3792 unsigned int lun = SCSI_LUN(srb);
3795 ms_cleanup_work(chip);
3797 retval = ms_switch_clock(chip);
3798 if (retval != STATUS_SUCCESS)
3799 TRACE_RET(chip, STATUS_FAIL);
3801 retval = mg_send_ex_cmd(chip, MG_GET_ID, 0);
3802 if (retval != STATUS_SUCCESS) {
3803 set_sense_type(chip, lun, SENSE_TYPE_MG_INCOMPATIBLE_MEDIUM);
3804 TRACE_RET(chip, STATUS_FAIL);
3807 retval = ms_read_bytes(chip, PRO_READ_SHORT_DATA, 32, WAIT_INT,
3809 if (retval != STATUS_SUCCESS) {
3810 set_sense_type(chip, lun, SENSE_TYPE_MG_INCOMPATIBLE_MEDIUM);
3811 TRACE_RET(chip, STATUS_FAIL);
3813 if (check_ms_err(chip)) {
3814 set_sense_type(chip, lun, SENSE_TYPE_MG_INCOMPATIBLE_MEDIUM);
3815 rtsx_clear_ms_error(chip);
3816 TRACE_RET(chip, STATUS_FAIL);
3819 memcpy(ms_card->magic_gate_id, buf, 16);
3821 #ifdef READ_BYTES_WAIT_INT
3822 retval = ms_poll_int(chip);
3823 if (retval != STATUS_SUCCESS) {
3824 set_sense_type(chip, lun, SENSE_TYPE_MG_INCOMPATIBLE_MEDIUM);
3825 TRACE_RET(chip, STATUS_FAIL);
3829 retval = mg_send_ex_cmd(chip, MG_SET_RD, 0);
3830 if (retval != STATUS_SUCCESS) {
3831 set_sense_type(chip, lun, SENSE_TYPE_MG_INCOMPATIBLE_MEDIUM);
3832 TRACE_RET(chip, STATUS_FAIL);
3835 bufflen = min_t(int, 12, scsi_bufflen(srb));
3836 rtsx_stor_get_xfer_buf(buf, bufflen, srb);
3838 for (i = 0; i < 8; i++)
3841 for (i = 0; i < 24; i++)
3844 retval = ms_write_bytes(chip, PRO_WRITE_SHORT_DATA,
3845 32, WAIT_INT, buf, 32);
3846 if (retval != STATUS_SUCCESS) {
3847 set_sense_type(chip, lun, SENSE_TYPE_MG_INCOMPATIBLE_MEDIUM);
3848 TRACE_RET(chip, STATUS_FAIL);
3850 if (check_ms_err(chip)) {
3851 set_sense_type(chip, lun, SENSE_TYPE_MG_INCOMPATIBLE_MEDIUM);
3852 rtsx_clear_ms_error(chip);
3853 TRACE_RET(chip, STATUS_FAIL);
3856 ms_card->mg_auth = 0;
3858 return STATUS_SUCCESS;
3861 int mg_get_rsp_chg(struct scsi_cmnd *srb, struct rtsx_chip *chip)
3863 struct ms_info *ms_card = &(chip->ms_card);
3866 unsigned int lun = SCSI_LUN(srb);
3867 u8 buf1[32], buf2[36];
3869 ms_cleanup_work(chip);
3871 retval = ms_switch_clock(chip);
3872 if (retval != STATUS_SUCCESS)
3873 TRACE_RET(chip, STATUS_FAIL);
3875 retval = mg_send_ex_cmd(chip, MG_MAKE_RMS, 0);
3876 if (retval != STATUS_SUCCESS) {
3877 set_sense_type(chip, lun, SENSE_TYPE_MG_KEY_FAIL_NOT_AUTHEN);
3878 TRACE_RET(chip, STATUS_FAIL);
3881 retval = ms_read_bytes(chip, PRO_READ_SHORT_DATA, 32, WAIT_INT,
3883 if (retval != STATUS_SUCCESS) {
3884 set_sense_type(chip, lun, SENSE_TYPE_MG_KEY_FAIL_NOT_AUTHEN);
3885 TRACE_RET(chip, STATUS_FAIL);
3887 if (check_ms_err(chip)) {
3888 set_sense_type(chip, lun, SENSE_TYPE_MG_KEY_FAIL_NOT_AUTHEN);
3889 rtsx_clear_ms_error(chip);
3890 TRACE_RET(chip, STATUS_FAIL);
3898 memcpy(buf2 + 4, ms_card->magic_gate_id, 16);
3899 memcpy(buf2 + 20, buf1, 16);
3901 bufflen = min_t(int, 36, scsi_bufflen(srb));
3902 rtsx_stor_set_xfer_buf(buf2, bufflen, srb);
3904 #ifdef READ_BYTES_WAIT_INT
3905 retval = ms_poll_int(chip);
3906 if (retval != STATUS_SUCCESS) {
3907 set_sense_type(chip, lun, SENSE_TYPE_MG_KEY_FAIL_NOT_AUTHEN);
3908 TRACE_RET(chip, STATUS_FAIL);
3912 return STATUS_SUCCESS;
3915 int mg_rsp(struct scsi_cmnd *srb, struct rtsx_chip *chip)
3917 struct ms_info *ms_card = &(chip->ms_card);
3921 unsigned int lun = SCSI_LUN(srb);
3924 ms_cleanup_work(chip);
3926 retval = ms_switch_clock(chip);
3927 if (retval != STATUS_SUCCESS)
3928 TRACE_RET(chip, STATUS_FAIL);
3930 retval = mg_send_ex_cmd(chip, MG_MAKE_KSE, 0);
3931 if (retval != STATUS_SUCCESS) {
3932 set_sense_type(chip, lun, SENSE_TYPE_MG_KEY_FAIL_NOT_AUTHEN);
3933 TRACE_RET(chip, STATUS_FAIL);
3936 bufflen = min_t(int, 12, scsi_bufflen(srb));
3937 rtsx_stor_get_xfer_buf(buf, bufflen, srb);
3939 for (i = 0; i < 8; i++)
3942 for (i = 0; i < 24; i++)
3945 retval = ms_write_bytes(chip, PRO_WRITE_SHORT_DATA, 32, WAIT_INT,
3947 if (retval != STATUS_SUCCESS) {
3948 set_sense_type(chip, lun, SENSE_TYPE_MG_KEY_FAIL_NOT_AUTHEN);
3949 TRACE_RET(chip, STATUS_FAIL);
3951 if (check_ms_err(chip)) {
3952 set_sense_type(chip, lun, SENSE_TYPE_MG_KEY_FAIL_NOT_AUTHEN);
3953 rtsx_clear_ms_error(chip);
3954 TRACE_RET(chip, STATUS_FAIL);
3957 ms_card->mg_auth = 1;
3959 return STATUS_SUCCESS;
3962 int mg_get_ICV(struct scsi_cmnd *srb, struct rtsx_chip *chip)
3964 struct ms_info *ms_card = &(chip->ms_card);
3967 unsigned int lun = SCSI_LUN(srb);
3970 ms_cleanup_work(chip);
3972 retval = ms_switch_clock(chip);
3973 if (retval != STATUS_SUCCESS)
3974 TRACE_RET(chip, STATUS_FAIL);
3976 buf = kmalloc(1028, GFP_KERNEL);
3978 TRACE_RET(chip, STATUS_ERROR);
3985 retval = mg_send_ex_cmd(chip, MG_GET_IBD, ms_card->mg_entry_num);
3986 if (retval != STATUS_SUCCESS) {
3987 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
3988 TRACE_GOTO(chip, GetICVFinish);
3991 retval = ms_transfer_data(chip, MS_TM_AUTO_READ, PRO_READ_LONG_DATA,
3992 2, WAIT_INT, 0, 0, buf + 4, 1024);
3993 if (retval != STATUS_SUCCESS) {
3994 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
3995 rtsx_clear_ms_error(chip);
3996 TRACE_GOTO(chip, GetICVFinish);
3998 if (check_ms_err(chip)) {
3999 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
4000 rtsx_clear_ms_error(chip);
4001 TRACE_RET(chip, STATUS_FAIL);
4004 bufflen = min_t(int, 1028, scsi_bufflen(srb));
4005 rtsx_stor_set_xfer_buf(buf, bufflen, srb);
4012 int mg_set_ICV(struct scsi_cmnd *srb, struct rtsx_chip *chip)
4014 struct ms_info *ms_card = &(chip->ms_card);
4017 #ifdef MG_SET_ICV_SLOW
4020 unsigned int lun = SCSI_LUN(srb);
4023 ms_cleanup_work(chip);
4025 retval = ms_switch_clock(chip);
4026 if (retval != STATUS_SUCCESS)
4027 TRACE_RET(chip, STATUS_FAIL);
4029 buf = kmalloc(1028, GFP_KERNEL);
4031 TRACE_RET(chip, STATUS_ERROR);
4033 bufflen = min_t(int, 1028, scsi_bufflen(srb));
4034 rtsx_stor_get_xfer_buf(buf, bufflen, srb);
4036 retval = mg_send_ex_cmd(chip, MG_SET_IBD, ms_card->mg_entry_num);
4037 if (retval != STATUS_SUCCESS) {
4038 if (ms_card->mg_auth == 0) {
4039 if ((buf[5] & 0xC0) != 0)
4040 set_sense_type(chip, lun,
4041 SENSE_TYPE_MG_KEY_FAIL_NOT_ESTAB);
4043 set_sense_type(chip, lun,
4044 SENSE_TYPE_MG_WRITE_ERR);
4046 set_sense_type(chip, lun, SENSE_TYPE_MG_WRITE_ERR);
4048 TRACE_GOTO(chip, SetICVFinish);
4051 #ifdef MG_SET_ICV_SLOW
4052 for (i = 0; i < 2; i++) {
4055 rtsx_init_cmd(chip);
4057 rtsx_add_cmd(chip, WRITE_REG_CMD, MS_TPC,
4058 0xFF, PRO_WRITE_LONG_DATA);
4059 rtsx_add_cmd(chip, WRITE_REG_CMD, MS_TRANS_CFG, 0xFF, WAIT_INT);
4060 rtsx_add_cmd(chip, WRITE_REG_CMD, CARD_DATA_SOURCE,
4063 trans_dma_enable(DMA_TO_DEVICE, chip, 512, DMA_512);
4065 rtsx_add_cmd(chip, WRITE_REG_CMD, MS_TRANSFER, 0xFF,
4066 MS_TRANSFER_START | MS_TM_NORMAL_WRITE);
4067 rtsx_add_cmd(chip, CHECK_REG_CMD, MS_TRANSFER,
4068 MS_TRANSFER_END, MS_TRANSFER_END);
4070 rtsx_send_cmd_no_wait(chip);
4072 retval = rtsx_transfer_data(chip, MS_CARD, buf + 4 + i*512,
4073 512, 0, DMA_TO_DEVICE, 3000);
4074 if ((retval < 0) || check_ms_err(chip)) {
4075 rtsx_clear_ms_error(chip);
4076 if (ms_card->mg_auth == 0) {
4077 if ((buf[5] & 0xC0) != 0)
4078 set_sense_type(chip, lun, SENSE_TYPE_MG_KEY_FAIL_NOT_ESTAB);
4080 set_sense_type(chip, lun,
4081 SENSE_TYPE_MG_WRITE_ERR);
4083 set_sense_type(chip, lun,
4084 SENSE_TYPE_MG_WRITE_ERR);
4086 retval = STATUS_FAIL;
4087 TRACE_GOTO(chip, SetICVFinish);
4091 retval = ms_transfer_data(chip, MS_TM_AUTO_WRITE, PRO_WRITE_LONG_DATA,
4092 2, WAIT_INT, 0, 0, buf + 4, 1024);
4093 if ((retval != STATUS_SUCCESS) || check_ms_err(chip)) {
4094 rtsx_clear_ms_error(chip);
4095 if (ms_card->mg_auth == 0) {
4096 if ((buf[5] & 0xC0) != 0)
4097 set_sense_type(chip, lun,
4098 SENSE_TYPE_MG_KEY_FAIL_NOT_ESTAB);
4100 set_sense_type(chip, lun,
4101 SENSE_TYPE_MG_WRITE_ERR);
4103 set_sense_type(chip, lun, SENSE_TYPE_MG_WRITE_ERR);
4105 TRACE_GOTO(chip, SetICVFinish);
4114 #endif /* SUPPORT_MAGIC_GATE */
4116 void ms_cleanup_work(struct rtsx_chip *chip)
4118 struct ms_info *ms_card = &(chip->ms_card);
4120 if (CHK_MSPRO(ms_card)) {
4121 if (ms_card->seq_mode) {
4122 dev_dbg(rtsx_dev(chip), "MS Pro: stop transmission\n");
4123 mspro_stop_seq_mode(chip);
4124 ms_card->cleanup_counter = 0;
4126 if (CHK_MSHG(ms_card)) {
4127 rtsx_write_register(chip, MS_CFG,
4128 MS_2K_SECTOR_MODE, 0x00);
4131 #ifdef MS_DELAY_WRITE
4132 else if ((!CHK_MSPRO(ms_card)) && ms_card->delay_write.delay_write_flag) {
4133 dev_dbg(rtsx_dev(chip), "MS: delay write\n");
4134 ms_delay_write(chip);
4135 ms_card->cleanup_counter = 0;
4140 int ms_power_off_card3v3(struct rtsx_chip *chip)
4144 retval = disable_card_clock(chip, MS_CARD);
4145 if (retval != STATUS_SUCCESS)
4146 TRACE_RET(chip, STATUS_FAIL);
4148 if (chip->asic_code) {
4149 retval = ms_pull_ctl_disable(chip);
4150 if (retval != STATUS_SUCCESS)
4151 TRACE_RET(chip, STATUS_FAIL);
4153 RTSX_WRITE_REG(chip, FPGA_PULL_CTL,
4154 FPGA_MS_PULL_CTL_BIT | 0x20, FPGA_MS_PULL_CTL_BIT);
4156 RTSX_WRITE_REG(chip, CARD_OE, MS_OUTPUT_EN, 0);
4157 if (!chip->ft2_fast_mode) {
4158 retval = card_power_off(chip, MS_CARD);
4159 if (retval != STATUS_SUCCESS)
4160 TRACE_RET(chip, STATUS_FAIL);
4163 return STATUS_SUCCESS;
4166 int release_ms_card(struct rtsx_chip *chip)
4168 struct ms_info *ms_card = &(chip->ms_card);
4171 #ifdef MS_DELAY_WRITE
4172 ms_card->delay_write.delay_write_flag = 0;
4174 ms_card->pro_under_formatting = 0;
4176 chip->card_ready &= ~MS_CARD;
4177 chip->card_fail &= ~MS_CARD;
4178 chip->card_wp &= ~MS_CARD;
4180 ms_free_l2p_tbl(chip);
4182 memset(ms_card->raw_sys_info, 0, 96);
4183 #ifdef SUPPORT_PCGL_1P18
4184 memset(ms_card->raw_model_name, 0, 48);
4187 retval = ms_power_off_card3v3(chip);
4188 if (retval != STATUS_SUCCESS)
4189 TRACE_RET(chip, STATUS_FAIL);
4191 return STATUS_SUCCESS;