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>
31 static inline void ms_set_err_code(struct rtsx_chip
*chip
, u8 err_code
)
33 struct ms_info
*ms_card
= &chip
->ms_card
;
35 ms_card
->err_code
= err_code
;
38 static inline int ms_check_err_code(struct rtsx_chip
*chip
, u8 err_code
)
40 struct ms_info
*ms_card
= &chip
->ms_card
;
42 return (ms_card
->err_code
== err_code
);
45 static int ms_parse_err_code(struct rtsx_chip
*chip
)
51 static int ms_transfer_tpc(struct rtsx_chip
*chip
, u8 trans_mode
,
52 u8 tpc
, u8 cnt
, u8 cfg
)
54 struct ms_info
*ms_card
= &chip
->ms_card
;
58 dev_dbg(rtsx_dev(chip
), "%s: tpc = 0x%x\n", __func__
, tpc
);
62 rtsx_add_cmd(chip
, WRITE_REG_CMD
, MS_TPC
, 0xFF, tpc
);
63 rtsx_add_cmd(chip
, WRITE_REG_CMD
, MS_BYTE_CNT
, 0xFF, cnt
);
64 rtsx_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANS_CFG
, 0xFF, cfg
);
65 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
,
66 0x01, PINGPONG_BUFFER
);
68 rtsx_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANSFER
,
69 0xFF, MS_TRANSFER_START
| trans_mode
);
70 rtsx_add_cmd(chip
, CHECK_REG_CMD
, MS_TRANSFER
,
71 MS_TRANSFER_END
, MS_TRANSFER_END
);
73 rtsx_add_cmd(chip
, READ_REG_CMD
, MS_TRANS_CFG
, 0, 0);
75 retval
= rtsx_send_cmd(chip
, MS_CARD
, 5000);
77 rtsx_clear_ms_error(chip
);
78 ms_set_err_code(chip
, MS_TO_ERROR
);
80 return ms_parse_err_code(chip
);
83 ptr
= rtsx_get_cmd_data(chip
) + 1;
85 if (!(tpc
& 0x08)) { /* Read Packet */
86 if (*ptr
& MS_CRC16_ERR
) {
87 ms_set_err_code(chip
, MS_CRC16_ERROR
);
89 return 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
);
96 return ms_parse_err_code(chip
);
101 if (*ptr
& MS_RDY_TIMEOUT
) {
102 rtsx_clear_ms_error(chip
);
103 ms_set_err_code(chip
, MS_TO_ERROR
);
105 return ms_parse_err_code(chip
);
108 return STATUS_SUCCESS
;
111 static int ms_transfer_data(struct rtsx_chip
*chip
, u8 trans_mode
,
112 u8 tpc
, u16 sec_cnt
, u8 cfg
, bool mode_2k
,
113 int use_sg
, void *buf
, int buf_len
)
116 u8 val
, err_code
= 0;
117 enum dma_data_direction dir
;
119 if (!buf
|| !buf_len
) {
124 if (trans_mode
== MS_TM_AUTO_READ
) {
125 dir
= DMA_FROM_DEVICE
;
126 err_code
= MS_FLASH_READ_ERROR
;
127 } else if (trans_mode
== MS_TM_AUTO_WRITE
) {
129 err_code
= MS_FLASH_WRITE_ERROR
;
137 rtsx_add_cmd(chip
, WRITE_REG_CMD
, MS_TPC
, 0xFF, tpc
);
138 rtsx_add_cmd(chip
, WRITE_REG_CMD
,
139 MS_SECTOR_CNT_H
, 0xFF, (u8
)(sec_cnt
>> 8));
140 rtsx_add_cmd(chip
, WRITE_REG_CMD
, MS_SECTOR_CNT_L
, 0xFF, (u8
)sec_cnt
);
141 rtsx_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANS_CFG
, 0xFF, cfg
);
144 rtsx_add_cmd(chip
, WRITE_REG_CMD
,
145 MS_CFG
, MS_2K_SECTOR_MODE
, MS_2K_SECTOR_MODE
);
147 rtsx_add_cmd(chip
, WRITE_REG_CMD
, MS_CFG
, MS_2K_SECTOR_MODE
, 0);
150 trans_dma_enable(dir
, chip
, sec_cnt
* 512, DMA_512
);
152 rtsx_add_cmd(chip
, WRITE_REG_CMD
,
153 MS_TRANSFER
, 0xFF, MS_TRANSFER_START
| trans_mode
);
154 rtsx_add_cmd(chip
, CHECK_REG_CMD
,
155 MS_TRANSFER
, MS_TRANSFER_END
, MS_TRANSFER_END
);
157 rtsx_send_cmd_no_wait(chip
);
159 retval
= rtsx_transfer_data(chip
, MS_CARD
, buf
, buf_len
,
160 use_sg
, dir
, chip
->mspro_timeout
);
162 ms_set_err_code(chip
, err_code
);
163 if (retval
== -ETIMEDOUT
)
164 retval
= STATUS_TIMEDOUT
;
166 retval
= STATUS_FAIL
;
172 retval
= rtsx_read_register(chip
, MS_TRANS_CFG
, &val
);
177 if (val
& (MS_INT_CMDNK
| MS_INT_ERR
| MS_CRC16_ERR
| MS_RDY_TIMEOUT
)) {
182 return STATUS_SUCCESS
;
185 static int ms_write_bytes(struct rtsx_chip
*chip
,
186 u8 tpc
, u8 cnt
, u8 cfg
, u8
*data
, int data_len
)
188 struct ms_info
*ms_card
= &chip
->ms_card
;
191 if (!data
|| (data_len
< cnt
)) {
198 for (i
= 0; i
< cnt
; i
++) {
199 rtsx_add_cmd(chip
, WRITE_REG_CMD
,
200 PPBUF_BASE2
+ i
, 0xFF, data
[i
]);
203 rtsx_add_cmd(chip
, WRITE_REG_CMD
, PPBUF_BASE2
+ i
, 0xFF, 0xFF);
205 rtsx_add_cmd(chip
, WRITE_REG_CMD
, MS_TPC
, 0xFF, tpc
);
206 rtsx_add_cmd(chip
, WRITE_REG_CMD
, MS_BYTE_CNT
, 0xFF, cnt
);
207 rtsx_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANS_CFG
, 0xFF, cfg
);
208 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
,
209 0x01, PINGPONG_BUFFER
);
211 rtsx_add_cmd(chip
, WRITE_REG_CMD
,
212 MS_TRANSFER
, 0xFF, MS_TRANSFER_START
| MS_TM_WRITE_BYTES
);
213 rtsx_add_cmd(chip
, CHECK_REG_CMD
,
214 MS_TRANSFER
, MS_TRANSFER_END
, MS_TRANSFER_END
);
216 retval
= rtsx_send_cmd(chip
, MS_CARD
, 5000);
220 rtsx_read_register(chip
, MS_TRANS_CFG
, &val
);
221 dev_dbg(rtsx_dev(chip
), "MS_TRANS_CFG: 0x%02x\n", val
);
223 rtsx_clear_ms_error(chip
);
226 if (val
& MS_CRC16_ERR
) {
227 ms_set_err_code(chip
, MS_CRC16_ERROR
);
229 return ms_parse_err_code(chip
);
232 if (CHK_MSPRO(ms_card
) && !(val
& 0x80)) {
233 if (val
& (MS_INT_ERR
| MS_INT_CMDNK
)) {
234 ms_set_err_code(chip
, MS_CMD_NK
);
236 return ms_parse_err_code(chip
);
241 if (val
& MS_RDY_TIMEOUT
) {
242 ms_set_err_code(chip
, MS_TO_ERROR
);
244 return ms_parse_err_code(chip
);
247 ms_set_err_code(chip
, MS_TO_ERROR
);
249 return ms_parse_err_code(chip
);
252 return STATUS_SUCCESS
;
255 static int ms_read_bytes(struct rtsx_chip
*chip
,
256 u8 tpc
, u8 cnt
, u8 cfg
, u8
*data
, int data_len
)
258 struct ms_info
*ms_card
= &chip
->ms_card
;
269 rtsx_add_cmd(chip
, WRITE_REG_CMD
, MS_TPC
, 0xFF, tpc
);
270 rtsx_add_cmd(chip
, WRITE_REG_CMD
, MS_BYTE_CNT
, 0xFF, cnt
);
271 rtsx_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANS_CFG
, 0xFF, cfg
);
272 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
,
273 0x01, PINGPONG_BUFFER
);
275 rtsx_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANSFER
, 0xFF,
276 MS_TRANSFER_START
| MS_TM_READ_BYTES
);
277 rtsx_add_cmd(chip
, CHECK_REG_CMD
, MS_TRANSFER
,
278 MS_TRANSFER_END
, MS_TRANSFER_END
);
280 for (i
= 0; i
< data_len
- 1; i
++)
281 rtsx_add_cmd(chip
, READ_REG_CMD
, PPBUF_BASE2
+ i
, 0, 0);
284 rtsx_add_cmd(chip
, READ_REG_CMD
, PPBUF_BASE2
+ data_len
, 0, 0);
286 rtsx_add_cmd(chip
, READ_REG_CMD
, PPBUF_BASE2
+ data_len
- 1,
289 retval
= rtsx_send_cmd(chip
, MS_CARD
, 5000);
293 rtsx_read_register(chip
, MS_TRANS_CFG
, &val
);
294 rtsx_clear_ms_error(chip
);
297 if (val
& MS_CRC16_ERR
) {
298 ms_set_err_code(chip
, MS_CRC16_ERROR
);
300 return ms_parse_err_code(chip
);
303 if (CHK_MSPRO(ms_card
) && !(val
& 0x80)) {
304 if (val
& (MS_INT_ERR
| MS_INT_CMDNK
)) {
305 ms_set_err_code(chip
, MS_CMD_NK
);
307 return ms_parse_err_code(chip
);
312 if (val
& MS_RDY_TIMEOUT
) {
313 ms_set_err_code(chip
, MS_TO_ERROR
);
315 return ms_parse_err_code(chip
);
318 ms_set_err_code(chip
, MS_TO_ERROR
);
320 return ms_parse_err_code(chip
);
323 ptr
= rtsx_get_cmd_data(chip
) + 1;
325 for (i
= 0; i
< data_len
; i
++)
328 if ((tpc
== PRO_READ_SHORT_DATA
) && (data_len
== 8)) {
329 dev_dbg(rtsx_dev(chip
), "Read format progress:\n");
330 print_hex_dump_bytes(KBUILD_MODNAME
": ", DUMP_PREFIX_NONE
, ptr
,
334 return STATUS_SUCCESS
;
337 static int ms_set_rw_reg_addr(struct rtsx_chip
*chip
, u8 read_start
,
338 u8 read_cnt
, u8 write_start
, u8 write_cnt
)
343 data
[0] = read_start
;
345 data
[2] = write_start
;
348 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
349 retval
= ms_write_bytes(chip
, SET_RW_REG_ADRS
, 4,
350 NO_WAIT_INT
, data
, 4);
351 if (retval
== STATUS_SUCCESS
)
352 return STATUS_SUCCESS
;
353 rtsx_clear_ms_error(chip
);
360 static int ms_send_cmd(struct rtsx_chip
*chip
, u8 cmd
, u8 cfg
)
367 return ms_write_bytes(chip
, PRO_SET_CMD
, 1, cfg
, data
, 1);
370 static int ms_set_init_para(struct rtsx_chip
*chip
)
372 struct ms_info
*ms_card
= &chip
->ms_card
;
375 if (CHK_HG8BIT(ms_card
)) {
377 ms_card
->ms_clock
= chip
->asic_ms_hg_clk
;
379 ms_card
->ms_clock
= chip
->fpga_ms_hg_clk
;
381 } else if (CHK_MSPRO(ms_card
) || CHK_MS4BIT(ms_card
)) {
383 ms_card
->ms_clock
= chip
->asic_ms_4bit_clk
;
385 ms_card
->ms_clock
= chip
->fpga_ms_4bit_clk
;
389 ms_card
->ms_clock
= chip
->asic_ms_1bit_clk
;
391 ms_card
->ms_clock
= chip
->fpga_ms_1bit_clk
;
394 retval
= switch_clock(chip
, ms_card
->ms_clock
);
395 if (retval
!= STATUS_SUCCESS
) {
400 retval
= select_card(chip
, MS_CARD
);
401 if (retval
!= STATUS_SUCCESS
) {
406 return STATUS_SUCCESS
;
409 static int ms_switch_clock(struct rtsx_chip
*chip
)
411 struct ms_info
*ms_card
= &chip
->ms_card
;
414 retval
= select_card(chip
, MS_CARD
);
415 if (retval
!= STATUS_SUCCESS
) {
420 retval
= switch_clock(chip
, ms_card
->ms_clock
);
421 if (retval
!= STATUS_SUCCESS
) {
426 return STATUS_SUCCESS
;
429 static int ms_pull_ctl_disable(struct rtsx_chip
*chip
)
433 if (CHECK_PID(chip
, 0x5208)) {
434 retval
= rtsx_write_register(chip
, CARD_PULL_CTL1
, 0xFF,
435 MS_D1_PD
| MS_D2_PD
| MS_CLK_PD
|
441 retval
= rtsx_write_register(chip
, CARD_PULL_CTL2
, 0xFF,
442 MS_D3_PD
| MS_D0_PD
| MS_BS_PD
|
448 retval
= rtsx_write_register(chip
, CARD_PULL_CTL3
, 0xFF,
449 MS_D7_PD
| XD_CE_PD
| XD_CLE_PD
|
455 retval
= rtsx_write_register(chip
, CARD_PULL_CTL4
, 0xFF,
456 XD_RDY_PD
| SD_D3_PD
| SD_D2_PD
|
462 retval
= rtsx_write_register(chip
, CARD_PULL_CTL5
, 0xFF,
463 MS_INS_PU
| SD_WP_PD
| SD_CD_PU
|
469 retval
= rtsx_write_register(chip
, CARD_PULL_CTL6
, 0xFF,
470 MS_D5_PD
| MS_D4_PD
);
475 } else if (CHECK_PID(chip
, 0x5288)) {
476 if (CHECK_BARO_PKG(chip
, QFN
)) {
477 retval
= rtsx_write_register(chip
, CARD_PULL_CTL1
,
483 retval
= rtsx_write_register(chip
, CARD_PULL_CTL2
,
489 retval
= rtsx_write_register(chip
, CARD_PULL_CTL3
,
495 retval
= rtsx_write_register(chip
, CARD_PULL_CTL4
,
504 return STATUS_SUCCESS
;
507 static int ms_pull_ctl_enable(struct rtsx_chip
*chip
)
513 if (CHECK_PID(chip
, 0x5208)) {
514 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL1
, 0xFF,
515 MS_D1_PD
| MS_D2_PD
| MS_CLK_NP
| MS_D6_PD
);
516 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL2
, 0xFF,
517 MS_D3_PD
| MS_D0_PD
| MS_BS_NP
| XD_D4_PD
);
518 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL3
, 0xFF,
519 MS_D7_PD
| XD_CE_PD
| XD_CLE_PD
| XD_CD_PU
);
520 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL4
, 0xFF,
521 XD_RDY_PD
| SD_D3_PD
| SD_D2_PD
| XD_ALE_PD
);
522 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL5
, 0xFF,
523 MS_INS_PU
| SD_WP_PD
| SD_CD_PU
| SD_CMD_PD
);
524 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_PULL_CTL6
, 0xFF,
525 MS_D5_PD
| MS_D4_PD
);
526 } else if (CHECK_PID(chip
, 0x5288)) {
527 if (CHECK_BARO_PKG(chip
, QFN
)) {
528 rtsx_add_cmd(chip
, WRITE_REG_CMD
,
529 CARD_PULL_CTL1
, 0xFF, 0x55);
530 rtsx_add_cmd(chip
, WRITE_REG_CMD
,
531 CARD_PULL_CTL2
, 0xFF, 0x45);
532 rtsx_add_cmd(chip
, WRITE_REG_CMD
,
533 CARD_PULL_CTL3
, 0xFF, 0x4B);
534 rtsx_add_cmd(chip
, WRITE_REG_CMD
,
535 CARD_PULL_CTL4
, 0xFF, 0x29);
539 retval
= rtsx_send_cmd(chip
, MS_CARD
, 100);
545 return STATUS_SUCCESS
;
548 static int ms_prepare_reset(struct rtsx_chip
*chip
)
550 struct ms_info
*ms_card
= &chip
->ms_card
;
554 ms_card
->ms_type
= 0;
555 ms_card
->check_ms_flow
= 0;
556 ms_card
->switch_8bit_fail
= 0;
557 ms_card
->delay_write
.delay_write_flag
= 0;
559 ms_card
->pro_under_formatting
= 0;
561 retval
= ms_power_off_card3v3(chip
);
562 if (retval
!= STATUS_SUCCESS
) {
567 if (!chip
->ft2_fast_mode
)
570 retval
= enable_card_clock(chip
, MS_CARD
);
571 if (retval
!= STATUS_SUCCESS
) {
576 if (chip
->asic_code
) {
577 retval
= ms_pull_ctl_enable(chip
);
578 if (retval
!= STATUS_SUCCESS
) {
583 retval
= rtsx_write_register(chip
, FPGA_PULL_CTL
,
584 FPGA_MS_PULL_CTL_BIT
| 0x20, 0);
591 if (!chip
->ft2_fast_mode
) {
592 retval
= card_power_on(chip
, MS_CARD
);
593 if (retval
!= STATUS_SUCCESS
) {
601 if (CHECK_LUN_MODE(chip
, SD_MS_2LUN
))
602 oc_mask
= MS_OC_NOW
| MS_OC_EVER
;
604 oc_mask
= SD_OC_NOW
| SD_OC_EVER
;
606 if (chip
->ocp_stat
& oc_mask
) {
607 dev_dbg(rtsx_dev(chip
), "Over current, OCPSTAT is 0x%x\n",
615 retval
= rtsx_write_register(chip
, CARD_OE
, MS_OUTPUT_EN
,
622 if (chip
->asic_code
) {
623 retval
= rtsx_write_register(chip
, MS_CFG
, 0xFF,
633 retval
= rtsx_write_register(chip
, MS_CFG
, 0xFF,
634 SAMPLE_TIME_FALLING
|
643 retval
= rtsx_write_register(chip
, MS_TRANS_CFG
, 0xFF,
644 NO_WAIT_INT
| NO_AUTO_READ_INT_REG
);
649 retval
= rtsx_write_register(chip
, CARD_STOP
, MS_STOP
| MS_CLR_ERR
,
650 MS_STOP
| MS_CLR_ERR
);
656 retval
= ms_set_init_para(chip
);
657 if (retval
!= STATUS_SUCCESS
) {
662 return STATUS_SUCCESS
;
665 static int ms_identify_media_type(struct rtsx_chip
*chip
, int switch_8bit_bus
)
667 struct ms_info
*ms_card
= &chip
->ms_card
;
671 retval
= ms_set_rw_reg_addr(chip
, Pro_StatusReg
, 6, SystemParm
, 1);
672 if (retval
!= STATUS_SUCCESS
) {
677 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
678 retval
= ms_transfer_tpc(chip
, MS_TM_READ_BYTES
, READ_REG
,
680 if (retval
== STATUS_SUCCESS
)
683 if (i
== MS_MAX_RETRY_COUNT
) {
688 retval
= rtsx_read_register(chip
, PPBUF_BASE2
+ 2, &val
);
693 dev_dbg(rtsx_dev(chip
), "Type register: 0x%x\n", val
);
696 ms_card
->check_ms_flow
= 1;
702 retval
= rtsx_read_register(chip
, PPBUF_BASE2
+ 4, &val
);
707 dev_dbg(rtsx_dev(chip
), "Category register: 0x%x\n", val
);
709 ms_card
->check_ms_flow
= 1;
714 retval
= rtsx_read_register(chip
, PPBUF_BASE2
+ 5, &val
);
719 dev_dbg(rtsx_dev(chip
), "Class register: 0x%x\n", val
);
721 retval
= rtsx_read_register(chip
, PPBUF_BASE2
, &val
);
727 chip
->card_wp
|= MS_CARD
;
729 chip
->card_wp
&= ~MS_CARD
;
731 } else if ((val
== 0x01) || (val
== 0x02) || (val
== 0x03)) {
732 chip
->card_wp
|= MS_CARD
;
734 ms_card
->check_ms_flow
= 1;
739 ms_card
->ms_type
|= TYPE_MSPRO
;
741 retval
= rtsx_read_register(chip
, PPBUF_BASE2
+ 3, &val
);
746 dev_dbg(rtsx_dev(chip
), "IF Mode register: 0x%x\n", val
);
748 ms_card
->ms_type
&= 0x0F;
749 } else if (val
== 7) {
751 ms_card
->ms_type
|= MS_HG
;
753 ms_card
->ms_type
&= 0x0F;
760 return STATUS_SUCCESS
;
763 static int ms_confirm_cpu_startup(struct rtsx_chip
*chip
)
768 /* Confirm CPU StartUp */
771 if (detect_card_cd(chip
, MS_CARD
) != STATUS_SUCCESS
) {
772 ms_set_err_code(chip
, MS_NO_CARD
);
777 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
778 retval
= ms_read_bytes(chip
, GET_INT
, 1,
779 NO_WAIT_INT
, &val
, 1);
780 if (retval
== STATUS_SUCCESS
)
783 if (i
== MS_MAX_RETRY_COUNT
) {
795 } while (!(val
& INT_REG_CED
));
797 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
798 retval
= ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
, 1);
799 if (retval
== STATUS_SUCCESS
)
802 if (i
== MS_MAX_RETRY_COUNT
) {
807 if (val
& INT_REG_ERR
) {
808 if (val
& INT_REG_CMDNK
) {
809 chip
->card_wp
|= (MS_CARD
);
815 /* -- end confirm CPU startup */
817 return STATUS_SUCCESS
;
820 static int ms_switch_parallel_bus(struct rtsx_chip
*chip
)
825 data
[0] = PARALLEL_4BIT_IF
;
827 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
828 retval
= ms_write_bytes(chip
, WRITE_REG
, 1, NO_WAIT_INT
,
830 if (retval
== STATUS_SUCCESS
)
833 if (retval
!= STATUS_SUCCESS
) {
838 return STATUS_SUCCESS
;
841 static int ms_switch_8bit_bus(struct rtsx_chip
*chip
)
843 struct ms_info
*ms_card
= &chip
->ms_card
;
847 data
[0] = PARALLEL_8BIT_IF
;
849 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
850 retval
= ms_write_bytes(chip
, WRITE_REG
, 1,
851 NO_WAIT_INT
, data
, 2);
852 if (retval
== STATUS_SUCCESS
)
855 if (retval
!= STATUS_SUCCESS
) {
860 retval
= rtsx_write_register(chip
, MS_CFG
, 0x98,
861 MS_BUS_WIDTH_8
| SAMPLE_TIME_FALLING
);
866 ms_card
->ms_type
|= MS_8BIT
;
867 retval
= ms_set_init_para(chip
);
868 if (retval
!= STATUS_SUCCESS
) {
873 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
874 retval
= ms_transfer_tpc(chip
, MS_TM_READ_BYTES
, GET_INT
,
876 if (retval
!= STATUS_SUCCESS
) {
882 return STATUS_SUCCESS
;
885 static int ms_pro_reset_flow(struct rtsx_chip
*chip
, int switch_8bit_bus
)
887 struct ms_info
*ms_card
= &chip
->ms_card
;
890 for (i
= 0; i
< 3; i
++) {
891 retval
= ms_prepare_reset(chip
);
892 if (retval
!= STATUS_SUCCESS
) {
897 retval
= ms_identify_media_type(chip
, switch_8bit_bus
);
898 if (retval
!= STATUS_SUCCESS
) {
903 retval
= ms_confirm_cpu_startup(chip
);
904 if (retval
!= STATUS_SUCCESS
) {
909 retval
= ms_switch_parallel_bus(chip
);
910 if (retval
!= STATUS_SUCCESS
) {
911 if (detect_card_cd(chip
, MS_CARD
) != STATUS_SUCCESS
) {
912 ms_set_err_code(chip
, MS_NO_CARD
);
922 if (retval
!= STATUS_SUCCESS
) {
927 /* Switch MS-PRO into Parallel mode */
928 retval
= rtsx_write_register(chip
, MS_CFG
, 0x18, MS_BUS_WIDTH_4
);
933 retval
= rtsx_write_register(chip
, MS_CFG
, PUSH_TIME_ODD
,
940 retval
= ms_set_init_para(chip
);
941 if (retval
!= STATUS_SUCCESS
) {
946 /* If MSPro HG Card, We shall try to switch to 8-bit bus */
947 if (CHK_MSHG(ms_card
) && chip
->support_ms_8bit
&& switch_8bit_bus
) {
948 retval
= ms_switch_8bit_bus(chip
);
949 if (retval
!= STATUS_SUCCESS
) {
950 ms_card
->switch_8bit_fail
= 1;
956 return STATUS_SUCCESS
;
960 static int msxc_change_power(struct rtsx_chip
*chip
, u8 mode
)
965 ms_cleanup_work(chip
);
967 retval
= ms_set_rw_reg_addr(chip
, 0, 0, Pro_DataCount1
, 6);
968 if (retval
!= STATUS_SUCCESS
) {
980 retval
= ms_write_bytes(chip
, PRO_WRITE_REG
, 6, NO_WAIT_INT
, buf
, 6);
981 if (retval
!= STATUS_SUCCESS
) {
986 retval
= ms_send_cmd(chip
, XC_CHG_POWER
, WAIT_INT
);
987 if (retval
!= STATUS_SUCCESS
) {
992 retval
= rtsx_read_register(chip
, MS_TRANS_CFG
, buf
);
997 if (buf
[0] & (MS_INT_CMDNK
| MS_INT_ERR
)) {
1002 return STATUS_SUCCESS
;
1006 static int ms_read_attribute_info(struct rtsx_chip
*chip
)
1008 struct ms_info
*ms_card
= &chip
->ms_card
;
1010 u8 val
, *buf
, class_code
, device_type
, sub_class
, data
[16];
1011 u16 total_blk
= 0, blk_size
= 0;
1013 u32 xc_total_blk
= 0, xc_blk_size
= 0;
1015 u32 sys_info_addr
= 0, sys_info_size
;
1016 #ifdef SUPPORT_PCGL_1P18
1017 u32 model_name_addr
= 0, model_name_size
;
1018 int found_sys_info
= 0, found_model_name
= 0;
1021 retval
= ms_set_rw_reg_addr(chip
, Pro_IntReg
, 2, Pro_SystemParm
, 7);
1022 if (retval
!= STATUS_SUCCESS
) {
1027 if (CHK_MS8BIT(ms_card
))
1028 data
[0] = PARALLEL_8BIT_IF
;
1030 data
[0] = PARALLEL_4BIT_IF
;
1041 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
1042 retval
= ms_write_bytes(chip
, PRO_WRITE_REG
, 7, NO_WAIT_INT
,
1044 if (retval
== STATUS_SUCCESS
)
1047 if (retval
!= STATUS_SUCCESS
) {
1052 buf
= kmalloc(64 * 512, GFP_KERNEL
);
1055 return STATUS_ERROR
;
1058 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
1059 retval
= ms_send_cmd(chip
, PRO_READ_ATRB
, WAIT_INT
);
1060 if (retval
!= STATUS_SUCCESS
)
1063 retval
= rtsx_read_register(chip
, MS_TRANS_CFG
, &val
);
1064 if (retval
!= STATUS_SUCCESS
) {
1069 if (!(val
& MS_INT_BREQ
)) {
1074 retval
= ms_transfer_data(chip
, MS_TM_AUTO_READ
,
1075 PRO_READ_LONG_DATA
, 0x40, WAIT_INT
,
1076 0, 0, buf
, 64 * 512);
1077 if (retval
== STATUS_SUCCESS
)
1080 rtsx_clear_ms_error(chip
);
1082 if (retval
!= STATUS_SUCCESS
) {
1090 retval
= rtsx_read_register(chip
, MS_TRANS_CFG
, &val
);
1091 if (retval
!= STATUS_SUCCESS
) {
1097 if ((val
& MS_INT_CED
) || !(val
& MS_INT_BREQ
))
1100 retval
= ms_transfer_tpc(chip
, MS_TM_NORMAL_READ
,
1101 PRO_READ_LONG_DATA
, 0, WAIT_INT
);
1102 if (retval
!= STATUS_SUCCESS
) {
1111 if ((buf
[0] != 0xa5) && (buf
[1] != 0xc3)) {
1112 /* Signature code is wrong */
1118 if ((buf
[4] < 1) || (buf
[4] > 12)) {
1124 for (i
= 0; i
< buf
[4]; i
++) {
1125 int cur_addr_off
= 16 + i
* 12;
1128 if ((buf
[cur_addr_off
+ 8] == 0x10) ||
1129 (buf
[cur_addr_off
+ 8] == 0x13)) {
1131 if (buf
[cur_addr_off
+ 8] == 0x10) {
1133 sys_info_addr
= ((u32
)buf
[cur_addr_off
+ 0] << 24) |
1134 ((u32
)buf
[cur_addr_off
+ 1] << 16) |
1135 ((u32
)buf
[cur_addr_off
+ 2] << 8) |
1136 buf
[cur_addr_off
+ 3];
1137 sys_info_size
= ((u32
)buf
[cur_addr_off
+ 4] << 24) |
1138 ((u32
)buf
[cur_addr_off
+ 5] << 16) |
1139 ((u32
)buf
[cur_addr_off
+ 6] << 8) |
1140 buf
[cur_addr_off
+ 7];
1141 dev_dbg(rtsx_dev(chip
), "sys_info_addr = 0x%x, sys_info_size = 0x%x\n",
1142 sys_info_addr
, sys_info_size
);
1143 if (sys_info_size
!= 96) {
1148 if (sys_info_addr
< 0x1A0) {
1153 if ((sys_info_size
+ sys_info_addr
) > 0x8000) {
1160 if (buf
[cur_addr_off
+ 8] == 0x13)
1161 ms_card
->ms_type
|= MS_XC
;
1163 #ifdef SUPPORT_PCGL_1P18
1169 #ifdef SUPPORT_PCGL_1P18
1170 if (buf
[cur_addr_off
+ 8] == 0x15) {
1171 model_name_addr
= ((u32
)buf
[cur_addr_off
+ 0] << 24) |
1172 ((u32
)buf
[cur_addr_off
+ 1] << 16) |
1173 ((u32
)buf
[cur_addr_off
+ 2] << 8) |
1174 buf
[cur_addr_off
+ 3];
1175 model_name_size
= ((u32
)buf
[cur_addr_off
+ 4] << 24) |
1176 ((u32
)buf
[cur_addr_off
+ 5] << 16) |
1177 ((u32
)buf
[cur_addr_off
+ 6] << 8) |
1178 buf
[cur_addr_off
+ 7];
1179 dev_dbg(rtsx_dev(chip
), "model_name_addr = 0x%x, model_name_size = 0x%x\n",
1180 model_name_addr
, model_name_size
);
1181 if (model_name_size
!= 48) {
1186 if (model_name_addr
< 0x1A0) {
1191 if ((model_name_size
+ model_name_addr
) > 0x8000) {
1197 found_model_name
= 1;
1200 if (found_sys_info
&& found_model_name
)
1211 class_code
= buf
[sys_info_addr
+ 0];
1212 device_type
= buf
[sys_info_addr
+ 56];
1213 sub_class
= buf
[sys_info_addr
+ 46];
1215 if (CHK_MSXC(ms_card
)) {
1216 xc_total_blk
= ((u32
)buf
[sys_info_addr
+ 6] << 24) |
1217 ((u32
)buf
[sys_info_addr
+ 7] << 16) |
1218 ((u32
)buf
[sys_info_addr
+ 8] << 8) |
1219 buf
[sys_info_addr
+ 9];
1220 xc_blk_size
= ((u32
)buf
[sys_info_addr
+ 32] << 24) |
1221 ((u32
)buf
[sys_info_addr
+ 33] << 16) |
1222 ((u32
)buf
[sys_info_addr
+ 34] << 8) |
1223 buf
[sys_info_addr
+ 35];
1224 dev_dbg(rtsx_dev(chip
), "xc_total_blk = 0x%x, xc_blk_size = 0x%x\n",
1225 xc_total_blk
, xc_blk_size
);
1227 total_blk
= ((u16
)buf
[sys_info_addr
+ 6] << 8) |
1228 buf
[sys_info_addr
+ 7];
1229 blk_size
= ((u16
)buf
[sys_info_addr
+ 2] << 8) |
1230 buf
[sys_info_addr
+ 3];
1231 dev_dbg(rtsx_dev(chip
), "total_blk = 0x%x, blk_size = 0x%x\n",
1232 total_blk
, blk_size
);
1235 total_blk
= ((u16
)buf
[sys_info_addr
+ 6] << 8) | buf
[sys_info_addr
+ 7];
1236 blk_size
= ((u16
)buf
[sys_info_addr
+ 2] << 8) | buf
[sys_info_addr
+ 3];
1237 dev_dbg(rtsx_dev(chip
), "total_blk = 0x%x, blk_size = 0x%x\n",
1238 total_blk
, blk_size
);
1241 dev_dbg(rtsx_dev(chip
), "class_code = 0x%x, device_type = 0x%x, sub_class = 0x%x\n",
1242 class_code
, device_type
, sub_class
);
1244 memcpy(ms_card
->raw_sys_info
, buf
+ sys_info_addr
, 96);
1245 #ifdef SUPPORT_PCGL_1P18
1246 memcpy(ms_card
->raw_model_name
, buf
+ model_name_addr
, 48);
1252 if (CHK_MSXC(ms_card
)) {
1253 if (class_code
!= 0x03) {
1258 if (class_code
!= 0x02) {
1264 if (class_code
!= 0x02) {
1270 if (device_type
!= 0x00) {
1271 if ((device_type
== 0x01) || (device_type
== 0x02) ||
1272 (device_type
== 0x03)) {
1273 chip
->card_wp
|= MS_CARD
;
1280 if (sub_class
& 0xC0) {
1285 dev_dbg(rtsx_dev(chip
), "class_code: 0x%x, device_type: 0x%x, sub_class: 0x%x\n",
1286 class_code
, device_type
, sub_class
);
1289 if (CHK_MSXC(ms_card
)) {
1290 chip
->capacity
[chip
->card2lun
[MS_CARD
]] =
1291 ms_card
->capacity
= xc_total_blk
* xc_blk_size
;
1293 chip
->capacity
[chip
->card2lun
[MS_CARD
]] =
1294 ms_card
->capacity
= total_blk
* blk_size
;
1297 ms_card
->capacity
= total_blk
* blk_size
;
1298 chip
->capacity
[chip
->card2lun
[MS_CARD
]] = ms_card
->capacity
;
1301 return STATUS_SUCCESS
;
1304 #ifdef SUPPORT_MAGIC_GATE
1305 static int mg_set_tpc_para_sub(struct rtsx_chip
*chip
,
1306 int type
, u8 mg_entry_num
);
1309 static int reset_ms_pro(struct rtsx_chip
*chip
)
1311 struct ms_info
*ms_card
= &chip
->ms_card
;
1313 #ifdef XC_POWERCLASS
1314 u8 change_power_class
;
1316 if (chip
->ms_power_class_en
& 0x02)
1317 change_power_class
= 2;
1318 else if (chip
->ms_power_class_en
& 0x01)
1319 change_power_class
= 1;
1321 change_power_class
= 0;
1324 #ifdef XC_POWERCLASS
1327 retval
= ms_pro_reset_flow(chip
, 1);
1328 if (retval
!= STATUS_SUCCESS
) {
1329 if (ms_card
->switch_8bit_fail
) {
1330 retval
= ms_pro_reset_flow(chip
, 0);
1331 if (retval
!= STATUS_SUCCESS
) {
1341 retval
= ms_read_attribute_info(chip
);
1342 if (retval
!= STATUS_SUCCESS
) {
1347 #ifdef XC_POWERCLASS
1348 if (CHK_HG8BIT(ms_card
))
1349 change_power_class
= 0;
1351 if (change_power_class
&& CHK_MSXC(ms_card
)) {
1352 u8 power_class_en
= chip
->ms_power_class_en
;
1354 dev_dbg(rtsx_dev(chip
), "power_class_en = 0x%x\n",
1356 dev_dbg(rtsx_dev(chip
), "change_power_class = %d\n",
1357 change_power_class
);
1359 if (change_power_class
)
1360 power_class_en
&= (1 << (change_power_class
- 1));
1364 if (power_class_en
) {
1365 u8 power_class_mode
=
1366 (ms_card
->raw_sys_info
[46] & 0x18) >> 3;
1367 dev_dbg(rtsx_dev(chip
), "power_class_mode = 0x%x",
1369 if (change_power_class
> power_class_mode
)
1370 change_power_class
= power_class_mode
;
1371 if (change_power_class
) {
1372 retval
= msxc_change_power(chip
,
1373 change_power_class
);
1374 if (retval
!= STATUS_SUCCESS
) {
1375 change_power_class
--;
1383 #ifdef SUPPORT_MAGIC_GATE
1384 retval
= mg_set_tpc_para_sub(chip
, 0, 0);
1385 if (retval
!= STATUS_SUCCESS
) {
1391 if (CHK_HG8BIT(ms_card
))
1392 chip
->card_bus_width
[chip
->card2lun
[MS_CARD
]] = 8;
1394 chip
->card_bus_width
[chip
->card2lun
[MS_CARD
]] = 4;
1396 return STATUS_SUCCESS
;
1399 static int ms_read_status_reg(struct rtsx_chip
*chip
)
1404 retval
= ms_set_rw_reg_addr(chip
, StatusReg0
, 2, 0, 0);
1405 if (retval
!= STATUS_SUCCESS
) {
1410 retval
= ms_read_bytes(chip
, READ_REG
, 2, NO_WAIT_INT
, val
, 2);
1411 if (retval
!= STATUS_SUCCESS
) {
1416 if (val
[1] & (STS_UCDT
| STS_UCEX
| STS_UCFG
)) {
1417 ms_set_err_code(chip
, MS_FLASH_READ_ERROR
);
1422 return STATUS_SUCCESS
;
1425 static int ms_read_extra_data(struct rtsx_chip
*chip
,
1426 u16 block_addr
, u8 page_num
, u8
*buf
, int buf_len
)
1428 struct ms_info
*ms_card
= &chip
->ms_card
;
1432 retval
= ms_set_rw_reg_addr(chip
, OverwriteFlag
, MS_EXTRA_SIZE
,
1434 if (retval
!= STATUS_SUCCESS
) {
1439 if (CHK_MS4BIT(ms_card
)) {
1440 /* Parallel interface */
1443 /* Serial interface */
1447 data
[2] = (u8
)(block_addr
>> 8);
1448 data
[3] = (u8
)block_addr
;
1452 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
1453 retval
= ms_write_bytes(chip
, WRITE_REG
, 6, NO_WAIT_INT
,
1455 if (retval
== STATUS_SUCCESS
)
1458 if (i
== MS_MAX_RETRY_COUNT
) {
1463 ms_set_err_code(chip
, MS_NO_ERROR
);
1465 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
1466 retval
= ms_send_cmd(chip
, BLOCK_READ
, WAIT_INT
);
1467 if (retval
== STATUS_SUCCESS
)
1470 if (i
== MS_MAX_RETRY_COUNT
) {
1475 ms_set_err_code(chip
, MS_NO_ERROR
);
1476 retval
= ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
, 1);
1477 if (retval
!= STATUS_SUCCESS
) {
1482 if (val
& INT_REG_CMDNK
) {
1483 ms_set_err_code(chip
, MS_CMD_NK
);
1487 if (val
& INT_REG_CED
) {
1488 if (val
& INT_REG_ERR
) {
1489 retval
= ms_read_status_reg(chip
);
1490 if (retval
!= STATUS_SUCCESS
) {
1495 retval
= ms_set_rw_reg_addr(chip
, OverwriteFlag
,
1496 MS_EXTRA_SIZE
, SystemParm
,
1498 if (retval
!= STATUS_SUCCESS
) {
1505 retval
= ms_read_bytes(chip
, READ_REG
, MS_EXTRA_SIZE
, NO_WAIT_INT
,
1506 data
, MS_EXTRA_SIZE
);
1507 if (retval
!= STATUS_SUCCESS
) {
1512 if (buf
&& buf_len
) {
1513 if (buf_len
> MS_EXTRA_SIZE
)
1514 buf_len
= MS_EXTRA_SIZE
;
1515 memcpy(buf
, data
, buf_len
);
1518 return STATUS_SUCCESS
;
1521 static int ms_write_extra_data(struct rtsx_chip
*chip
, u16 block_addr
,
1522 u8 page_num
, u8
*buf
, int buf_len
)
1524 struct ms_info
*ms_card
= &chip
->ms_card
;
1528 if (!buf
|| (buf_len
< MS_EXTRA_SIZE
)) {
1533 retval
= ms_set_rw_reg_addr(chip
, OverwriteFlag
, MS_EXTRA_SIZE
,
1534 SystemParm
, 6 + MS_EXTRA_SIZE
);
1535 if (retval
!= STATUS_SUCCESS
) {
1540 if (CHK_MS4BIT(ms_card
))
1546 data
[2] = (u8
)(block_addr
>> 8);
1547 data
[3] = (u8
)block_addr
;
1551 for (i
= 6; i
< MS_EXTRA_SIZE
+ 6; i
++)
1552 data
[i
] = buf
[i
- 6];
1554 retval
= ms_write_bytes(chip
, WRITE_REG
, (6 + MS_EXTRA_SIZE
),
1555 NO_WAIT_INT
, data
, 16);
1556 if (retval
!= STATUS_SUCCESS
) {
1561 retval
= ms_send_cmd(chip
, BLOCK_WRITE
, WAIT_INT
);
1562 if (retval
!= STATUS_SUCCESS
) {
1567 ms_set_err_code(chip
, MS_NO_ERROR
);
1568 retval
= ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
, 1);
1569 if (retval
!= STATUS_SUCCESS
) {
1574 if (val
& INT_REG_CMDNK
) {
1575 ms_set_err_code(chip
, MS_CMD_NK
);
1579 if (val
& INT_REG_CED
) {
1580 if (val
& INT_REG_ERR
) {
1581 ms_set_err_code(chip
, MS_FLASH_WRITE_ERROR
);
1587 return STATUS_SUCCESS
;
1590 static int ms_read_page(struct rtsx_chip
*chip
, u16 block_addr
, u8 page_num
)
1592 struct ms_info
*ms_card
= &chip
->ms_card
;
1596 retval
= ms_set_rw_reg_addr(chip
, OverwriteFlag
, MS_EXTRA_SIZE
,
1598 if (retval
!= STATUS_SUCCESS
) {
1603 if (CHK_MS4BIT(ms_card
))
1609 data
[2] = (u8
)(block_addr
>> 8);
1610 data
[3] = (u8
)block_addr
;
1614 retval
= ms_write_bytes(chip
, WRITE_REG
, 6, NO_WAIT_INT
, data
, 6);
1615 if (retval
!= STATUS_SUCCESS
) {
1620 retval
= ms_send_cmd(chip
, BLOCK_READ
, WAIT_INT
);
1621 if (retval
!= STATUS_SUCCESS
) {
1626 ms_set_err_code(chip
, MS_NO_ERROR
);
1627 retval
= ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
, 1);
1628 if (retval
!= STATUS_SUCCESS
) {
1633 if (val
& INT_REG_CMDNK
) {
1634 ms_set_err_code(chip
, MS_CMD_NK
);
1639 if (val
& INT_REG_CED
) {
1640 if (val
& INT_REG_ERR
) {
1641 if (!(val
& INT_REG_BREQ
)) {
1642 ms_set_err_code(chip
, MS_FLASH_READ_ERROR
);
1646 retval
= ms_read_status_reg(chip
);
1647 if (retval
!= STATUS_SUCCESS
)
1648 ms_set_err_code(chip
, MS_FLASH_WRITE_ERROR
);
1651 if (!(val
& INT_REG_BREQ
)) {
1652 ms_set_err_code(chip
, MS_BREQ_ERROR
);
1659 retval
= ms_transfer_tpc(chip
, MS_TM_NORMAL_READ
, READ_PAGE_DATA
,
1661 if (retval
!= STATUS_SUCCESS
) {
1666 if (ms_check_err_code(chip
, MS_FLASH_WRITE_ERROR
)) {
1671 return STATUS_SUCCESS
;
1674 static int ms_set_bad_block(struct rtsx_chip
*chip
, u16 phy_blk
)
1676 struct ms_info
*ms_card
= &chip
->ms_card
;
1678 u8 val
, data
[8], extra
[MS_EXTRA_SIZE
];
1680 retval
= ms_read_extra_data(chip
, phy_blk
, 0, extra
, MS_EXTRA_SIZE
);
1681 if (retval
!= STATUS_SUCCESS
) {
1686 retval
= ms_set_rw_reg_addr(chip
, OverwriteFlag
, MS_EXTRA_SIZE
,
1688 if (retval
!= STATUS_SUCCESS
) {
1693 ms_set_err_code(chip
, MS_NO_ERROR
);
1695 if (CHK_MS4BIT(ms_card
))
1701 data
[2] = (u8
)(phy_blk
>> 8);
1702 data
[3] = (u8
)phy_blk
;
1705 data
[6] = extra
[0] & 0x7F;
1708 retval
= ms_write_bytes(chip
, WRITE_REG
, 7, NO_WAIT_INT
, data
, 7);
1709 if (retval
!= STATUS_SUCCESS
) {
1714 retval
= ms_send_cmd(chip
, BLOCK_WRITE
, WAIT_INT
);
1715 if (retval
!= STATUS_SUCCESS
) {
1720 ms_set_err_code(chip
, MS_NO_ERROR
);
1721 retval
= ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
, 1);
1722 if (retval
!= STATUS_SUCCESS
) {
1727 if (val
& INT_REG_CMDNK
) {
1728 ms_set_err_code(chip
, MS_CMD_NK
);
1733 if (val
& INT_REG_CED
) {
1734 if (val
& INT_REG_ERR
) {
1735 ms_set_err_code(chip
, MS_FLASH_WRITE_ERROR
);
1741 return STATUS_SUCCESS
;
1744 static int ms_erase_block(struct rtsx_chip
*chip
, u16 phy_blk
)
1746 struct ms_info
*ms_card
= &chip
->ms_card
;
1750 retval
= ms_set_rw_reg_addr(chip
, OverwriteFlag
, MS_EXTRA_SIZE
,
1752 if (retval
!= STATUS_SUCCESS
) {
1757 ms_set_err_code(chip
, MS_NO_ERROR
);
1759 if (CHK_MS4BIT(ms_card
))
1765 data
[2] = (u8
)(phy_blk
>> 8);
1766 data
[3] = (u8
)phy_blk
;
1770 retval
= ms_write_bytes(chip
, WRITE_REG
, 6, NO_WAIT_INT
, data
, 6);
1771 if (retval
!= STATUS_SUCCESS
) {
1777 retval
= ms_send_cmd(chip
, BLOCK_ERASE
, WAIT_INT
);
1778 if (retval
!= STATUS_SUCCESS
) {
1783 ms_set_err_code(chip
, MS_NO_ERROR
);
1784 retval
= ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
, 1);
1785 if (retval
!= STATUS_SUCCESS
) {
1790 if (val
& INT_REG_CMDNK
) {
1796 ms_set_err_code(chip
, MS_CMD_NK
);
1797 ms_set_bad_block(chip
, phy_blk
);
1802 if (val
& INT_REG_CED
) {
1803 if (val
& INT_REG_ERR
) {
1804 ms_set_err_code(chip
, MS_FLASH_WRITE_ERROR
);
1810 return STATUS_SUCCESS
;
1813 static void ms_set_page_status(u16 log_blk
, u8 type
, u8
*extra
, int extra_len
)
1815 if (!extra
|| (extra_len
< MS_EXTRA_SIZE
))
1818 memset(extra
, 0xFF, MS_EXTRA_SIZE
);
1820 if (type
== setPS_NG
) {
1821 /* set page status as 1:NG,and block status keep 1:OK */
1824 /* set page status as 0:Data Error,and block status keep 1:OK */
1828 extra
[2] = (u8
)(log_blk
>> 8);
1829 extra
[3] = (u8
)log_blk
;
1832 static int ms_init_page(struct rtsx_chip
*chip
, u16 phy_blk
, u16 log_blk
,
1833 u8 start_page
, u8 end_page
)
1836 u8 extra
[MS_EXTRA_SIZE
], i
;
1838 memset(extra
, 0xff, MS_EXTRA_SIZE
);
1840 extra
[0] = 0xf8; /* Block, page OK, data erased */
1842 extra
[2] = (u8
)(log_blk
>> 8);
1843 extra
[3] = (u8
)log_blk
;
1845 for (i
= start_page
; i
< end_page
; i
++) {
1846 if (detect_card_cd(chip
, MS_CARD
) != STATUS_SUCCESS
) {
1847 ms_set_err_code(chip
, MS_NO_CARD
);
1852 retval
= ms_write_extra_data(chip
, phy_blk
, i
,
1853 extra
, MS_EXTRA_SIZE
);
1854 if (retval
!= STATUS_SUCCESS
) {
1860 return STATUS_SUCCESS
;
1863 static int ms_copy_page(struct rtsx_chip
*chip
, u16 old_blk
, u16 new_blk
,
1864 u16 log_blk
, u8 start_page
, u8 end_page
)
1866 struct ms_info
*ms_card
= &chip
->ms_card
;
1867 bool uncorrect_flag
= false;
1868 int retval
, rty_cnt
;
1869 u8 extra
[MS_EXTRA_SIZE
], val
, i
, j
, data
[16];
1871 dev_dbg(rtsx_dev(chip
), "Copy page from 0x%x to 0x%x, logical block is 0x%x\n",
1872 old_blk
, new_blk
, log_blk
);
1873 dev_dbg(rtsx_dev(chip
), "start_page = %d, end_page = %d\n",
1874 start_page
, end_page
);
1876 retval
= ms_read_extra_data(chip
, new_blk
, 0, extra
, MS_EXTRA_SIZE
);
1877 if (retval
!= STATUS_SUCCESS
) {
1882 retval
= ms_read_status_reg(chip
);
1883 if (retval
!= STATUS_SUCCESS
) {
1888 retval
= rtsx_read_register(chip
, PPBUF_BASE2
, &val
);
1894 if (val
& BUF_FULL
) {
1895 retval
= ms_send_cmd(chip
, CLEAR_BUF
, WAIT_INT
);
1896 if (retval
!= STATUS_SUCCESS
) {
1901 retval
= ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
, 1);
1902 if (retval
!= STATUS_SUCCESS
) {
1907 if (!(val
& INT_REG_CED
)) {
1908 ms_set_err_code(chip
, MS_FLASH_WRITE_ERROR
);
1914 for (i
= start_page
; i
< end_page
; i
++) {
1915 if (detect_card_cd(chip
, MS_CARD
) != STATUS_SUCCESS
) {
1916 ms_set_err_code(chip
, MS_NO_CARD
);
1921 ms_read_extra_data(chip
, old_blk
, i
, extra
, MS_EXTRA_SIZE
);
1923 retval
= ms_set_rw_reg_addr(chip
, OverwriteFlag
,
1924 MS_EXTRA_SIZE
, SystemParm
, 6);
1925 if (retval
!= STATUS_SUCCESS
) {
1930 ms_set_err_code(chip
, MS_NO_ERROR
);
1932 if (CHK_MS4BIT(ms_card
))
1938 data
[2] = (u8
)(old_blk
>> 8);
1939 data
[3] = (u8
)old_blk
;
1943 retval
= ms_write_bytes(chip
, WRITE_REG
, 6, NO_WAIT_INT
,
1945 if (retval
!= STATUS_SUCCESS
) {
1950 retval
= ms_send_cmd(chip
, BLOCK_READ
, WAIT_INT
);
1951 if (retval
!= STATUS_SUCCESS
) {
1956 ms_set_err_code(chip
, MS_NO_ERROR
);
1957 retval
= ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
, 1);
1958 if (retval
!= STATUS_SUCCESS
) {
1963 if (val
& INT_REG_CMDNK
) {
1964 ms_set_err_code(chip
, MS_CMD_NK
);
1969 if (val
& INT_REG_CED
) {
1970 if (val
& INT_REG_ERR
) {
1971 retval
= ms_read_status_reg(chip
);
1972 if (retval
!= STATUS_SUCCESS
) {
1973 uncorrect_flag
= true;
1974 dev_dbg(rtsx_dev(chip
), "Uncorrectable error\n");
1976 uncorrect_flag
= false;
1979 retval
= ms_transfer_tpc(chip
,
1983 if (retval
!= STATUS_SUCCESS
) {
1988 if (uncorrect_flag
) {
1989 ms_set_page_status(log_blk
, setPS_NG
,
1995 ms_write_extra_data(chip
, old_blk
, i
,
1998 dev_dbg(rtsx_dev(chip
), "page %d : extra[0] = 0x%x\n",
2000 MS_SET_BAD_BLOCK_FLG(ms_card
);
2002 ms_set_page_status(log_blk
, setPS_Error
,
2005 ms_write_extra_data(chip
, new_blk
, i
,
2011 for (rty_cnt
= 0; rty_cnt
< MS_MAX_RETRY_COUNT
;
2013 retval
= ms_transfer_tpc(
2018 if (retval
== STATUS_SUCCESS
)
2021 if (rty_cnt
== MS_MAX_RETRY_COUNT
) {
2027 if (!(val
& INT_REG_BREQ
)) {
2028 ms_set_err_code(chip
, MS_BREQ_ERROR
);
2034 retval
= ms_set_rw_reg_addr(chip
, OverwriteFlag
, MS_EXTRA_SIZE
,
2035 SystemParm
, (6 + MS_EXTRA_SIZE
));
2037 ms_set_err_code(chip
, MS_NO_ERROR
);
2039 if (CHK_MS4BIT(ms_card
))
2045 data
[2] = (u8
)(new_blk
>> 8);
2046 data
[3] = (u8
)new_blk
;
2050 if ((extra
[0] & 0x60) != 0x60)
2056 data
[6 + 2] = (u8
)(log_blk
>> 8);
2057 data
[6 + 3] = (u8
)log_blk
;
2059 for (j
= 4; j
<= MS_EXTRA_SIZE
; j
++)
2062 retval
= ms_write_bytes(chip
, WRITE_REG
, (6 + MS_EXTRA_SIZE
),
2063 NO_WAIT_INT
, data
, 16);
2064 if (retval
!= STATUS_SUCCESS
) {
2069 retval
= ms_send_cmd(chip
, BLOCK_WRITE
, WAIT_INT
);
2070 if (retval
!= STATUS_SUCCESS
) {
2075 ms_set_err_code(chip
, MS_NO_ERROR
);
2076 retval
= ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
, 1);
2077 if (retval
!= STATUS_SUCCESS
) {
2082 if (val
& INT_REG_CMDNK
) {
2083 ms_set_err_code(chip
, MS_CMD_NK
);
2088 if (val
& INT_REG_CED
) {
2089 if (val
& INT_REG_ERR
) {
2090 ms_set_err_code(chip
, MS_FLASH_WRITE_ERROR
);
2097 retval
= ms_set_rw_reg_addr(chip
, OverwriteFlag
,
2098 MS_EXTRA_SIZE
, SystemParm
,
2100 if (retval
!= STATUS_SUCCESS
) {
2105 ms_set_err_code(chip
, MS_NO_ERROR
);
2107 if (CHK_MS4BIT(ms_card
))
2113 data
[2] = (u8
)(old_blk
>> 8);
2114 data
[3] = (u8
)old_blk
;
2120 retval
= ms_write_bytes(chip
, WRITE_REG
, 7,
2121 NO_WAIT_INT
, data
, 8);
2122 if (retval
!= STATUS_SUCCESS
) {
2127 retval
= ms_send_cmd(chip
, BLOCK_WRITE
, WAIT_INT
);
2128 if (retval
!= STATUS_SUCCESS
) {
2133 ms_set_err_code(chip
, MS_NO_ERROR
);
2134 retval
= ms_read_bytes(chip
, GET_INT
, 1,
2135 NO_WAIT_INT
, &val
, 1);
2136 if (retval
!= STATUS_SUCCESS
) {
2141 if (val
& INT_REG_CMDNK
) {
2142 ms_set_err_code(chip
, MS_CMD_NK
);
2147 if (val
& INT_REG_CED
) {
2148 if (val
& INT_REG_ERR
) {
2149 ms_set_err_code(chip
,
2150 MS_FLASH_WRITE_ERROR
);
2158 return STATUS_SUCCESS
;
2161 static int reset_ms(struct rtsx_chip
*chip
)
2163 struct ms_info
*ms_card
= &chip
->ms_card
;
2165 u16 i
, reg_addr
, block_size
;
2166 u8 val
, extra
[MS_EXTRA_SIZE
], j
, *ptr
;
2167 #ifndef SUPPORT_MAGIC_GATE
2171 retval
= ms_prepare_reset(chip
);
2172 if (retval
!= STATUS_SUCCESS
) {
2177 ms_card
->ms_type
|= TYPE_MS
;
2179 retval
= ms_send_cmd(chip
, MS_RESET
, NO_WAIT_INT
);
2180 if (retval
!= STATUS_SUCCESS
) {
2185 retval
= ms_read_status_reg(chip
);
2186 if (retval
!= STATUS_SUCCESS
) {
2191 retval
= rtsx_read_register(chip
, PPBUF_BASE2
, &val
);
2196 if (val
& WRT_PRTCT
)
2197 chip
->card_wp
|= MS_CARD
;
2199 chip
->card_wp
&= ~MS_CARD
;
2204 /* Search Boot Block */
2205 while (i
< (MAX_DEFECTIVE_BLOCK
+ 2)) {
2206 if (detect_card_cd(chip
, MS_CARD
) != STATUS_SUCCESS
) {
2207 ms_set_err_code(chip
, MS_NO_CARD
);
2212 retval
= ms_read_extra_data(chip
, i
, 0, extra
, MS_EXTRA_SIZE
);
2213 if (retval
!= STATUS_SUCCESS
) {
2218 if (extra
[0] & BLOCK_OK
) {
2219 if (!(extra
[1] & NOT_BOOT_BLOCK
)) {
2220 ms_card
->boot_block
= i
;
2227 if (i
== (MAX_DEFECTIVE_BLOCK
+ 2)) {
2228 dev_dbg(rtsx_dev(chip
), "No boot block found!");
2233 for (j
= 0; j
< 3; j
++) {
2234 retval
= ms_read_page(chip
, ms_card
->boot_block
, j
);
2235 if (retval
!= STATUS_SUCCESS
) {
2236 if (ms_check_err_code(chip
, MS_FLASH_WRITE_ERROR
)) {
2237 i
= ms_card
->boot_block
+ 1;
2238 ms_set_err_code(chip
, MS_NO_ERROR
);
2244 retval
= ms_read_page(chip
, ms_card
->boot_block
, 0);
2245 if (retval
!= STATUS_SUCCESS
) {
2250 /* Read MS system information as sys_info */
2251 rtsx_init_cmd(chip
);
2253 for (i
= 0; i
< 96; i
++)
2254 rtsx_add_cmd(chip
, READ_REG_CMD
, PPBUF_BASE2
+ 0x1A0 + i
, 0, 0);
2256 retval
= rtsx_send_cmd(chip
, MS_CARD
, 100);
2262 ptr
= rtsx_get_cmd_data(chip
);
2263 memcpy(ms_card
->raw_sys_info
, ptr
, 96);
2265 /* Read useful block contents */
2266 rtsx_init_cmd(chip
);
2268 rtsx_add_cmd(chip
, READ_REG_CMD
, HEADER_ID0
, 0, 0);
2269 rtsx_add_cmd(chip
, READ_REG_CMD
, HEADER_ID1
, 0, 0);
2271 for (reg_addr
= DISABLED_BLOCK0
; reg_addr
<= DISABLED_BLOCK3
;
2273 rtsx_add_cmd(chip
, READ_REG_CMD
, reg_addr
, 0, 0);
2275 for (reg_addr
= BLOCK_SIZE_0
; reg_addr
<= PAGE_SIZE_1
; reg_addr
++)
2276 rtsx_add_cmd(chip
, READ_REG_CMD
, reg_addr
, 0, 0);
2278 rtsx_add_cmd(chip
, READ_REG_CMD
, MS_Device_Type
, 0, 0);
2279 rtsx_add_cmd(chip
, READ_REG_CMD
, MS_4bit_Support
, 0, 0);
2281 retval
= rtsx_send_cmd(chip
, MS_CARD
, 100);
2287 ptr
= rtsx_get_cmd_data(chip
);
2289 dev_dbg(rtsx_dev(chip
), "Boot block data:\n");
2290 dev_dbg(rtsx_dev(chip
), "%*ph\n", 16, ptr
);
2293 * HEADER_ID0, HEADER_ID1
2295 if (ptr
[0] != 0x00 || ptr
[1] != 0x01) {
2296 i
= ms_card
->boot_block
+ 1;
2301 * PAGE_SIZE_0, PAGE_SIZE_1
2303 if (ptr
[12] != 0x02 || ptr
[13] != 0x00) {
2304 i
= ms_card
->boot_block
+ 1;
2308 if ((ptr
[14] == 1) || (ptr
[14] == 3))
2309 chip
->card_wp
|= MS_CARD
;
2311 /* BLOCK_SIZE_0, BLOCK_SIZE_1 */
2312 block_size
= ((u16
)ptr
[6] << 8) | ptr
[7];
2313 if (block_size
== 0x0010) {
2314 /* Block size 16KB */
2315 ms_card
->block_shift
= 5;
2316 ms_card
->page_off
= 0x1F;
2317 } else if (block_size
== 0x0008) {
2318 /* Block size 8KB */
2319 ms_card
->block_shift
= 4;
2320 ms_card
->page_off
= 0x0F;
2323 /* BLOCK_COUNT_0, BLOCK_COUNT_1 */
2324 ms_card
->total_block
= ((u16
)ptr
[8] << 8) | ptr
[9];
2326 #ifdef SUPPORT_MAGIC_GATE
2329 if (ms_card
->block_shift
== 4) { /* 4MB or 8MB */
2330 if (j
< 2) { /* Effective block for 4MB: 0x1F0 */
2331 ms_card
->capacity
= 0x1EE0;
2332 } else { /* Effective block for 8MB: 0x3E0 */
2333 ms_card
->capacity
= 0x3DE0;
2335 } else { /* 16MB, 32MB, 64MB or 128MB */
2336 if (j
< 5) { /* Effective block for 16MB: 0x3E0 */
2337 ms_card
->capacity
= 0x7BC0;
2338 } else if (j
< 0xA) { /* Effective block for 32MB: 0x7C0 */
2339 ms_card
->capacity
= 0xF7C0;
2340 } else if (j
< 0x11) { /* Effective block for 64MB: 0xF80 */
2341 ms_card
->capacity
= 0x1EF80;
2342 } else { /* Effective block for 128MB: 0x1F00 */
2343 ms_card
->capacity
= 0x3DF00;
2347 /* EBLOCK_COUNT_0, EBLOCK_COUNT_1 */
2348 eblock_cnt
= ((u16
)ptr
[10] << 8) | ptr
[11];
2350 ms_card
->capacity
= ((u32
)eblock_cnt
- 2) << ms_card
->block_shift
;
2353 chip
->capacity
[chip
->card2lun
[MS_CARD
]] = ms_card
->capacity
;
2355 /* Switch I/F Mode */
2357 retval
= ms_set_rw_reg_addr(chip
, 0, 0, SystemParm
, 1);
2358 if (retval
!= STATUS_SUCCESS
) {
2363 retval
= rtsx_write_register(chip
, PPBUF_BASE2
, 0xFF, 0x88);
2368 retval
= rtsx_write_register(chip
, PPBUF_BASE2
+ 1, 0xFF, 0);
2374 retval
= ms_transfer_tpc(chip
, MS_TM_WRITE_BYTES
, WRITE_REG
, 1,
2376 if (retval
!= STATUS_SUCCESS
) {
2381 retval
= rtsx_write_register(chip
, MS_CFG
,
2382 0x58 | MS_NO_CHECK_INT
,
2391 ms_card
->ms_type
|= MS_4BIT
;
2394 if (CHK_MS4BIT(ms_card
))
2395 chip
->card_bus_width
[chip
->card2lun
[MS_CARD
]] = 4;
2397 chip
->card_bus_width
[chip
->card2lun
[MS_CARD
]] = 1;
2399 return STATUS_SUCCESS
;
2402 static int ms_init_l2p_tbl(struct rtsx_chip
*chip
)
2404 struct ms_info
*ms_card
= &chip
->ms_card
;
2405 int size
, i
, seg_no
, retval
;
2406 u16 defect_block
, reg_addr
;
2409 ms_card
->segment_cnt
= ms_card
->total_block
>> 9;
2410 dev_dbg(rtsx_dev(chip
), "ms_card->segment_cnt = %d\n",
2411 ms_card
->segment_cnt
);
2413 size
= ms_card
->segment_cnt
* sizeof(struct zone_entry
);
2414 ms_card
->segment
= vzalloc(size
);
2415 if (!ms_card
->segment
) {
2420 retval
= ms_read_page(chip
, ms_card
->boot_block
, 1);
2421 if (retval
!= STATUS_SUCCESS
) {
2426 reg_addr
= PPBUF_BASE2
;
2427 for (i
= 0; i
< (((ms_card
->total_block
>> 9) * 10) + 1); i
++) {
2430 retval
= rtsx_read_register(chip
, reg_addr
++, &val1
);
2431 if (retval
!= STATUS_SUCCESS
) {
2436 retval
= rtsx_read_register(chip
, reg_addr
++, &val2
);
2437 if (retval
!= STATUS_SUCCESS
) {
2442 defect_block
= ((u16
)val1
<< 8) | val2
;
2443 if (defect_block
== 0xFFFF)
2446 seg_no
= defect_block
/ 512;
2448 block_no
= ms_card
->segment
[seg_no
].disable_count
++;
2449 ms_card
->segment
[seg_no
].defect_list
[block_no
] = defect_block
;
2452 for (i
= 0; i
< ms_card
->segment_cnt
; i
++) {
2453 ms_card
->segment
[i
].build_flag
= 0;
2454 ms_card
->segment
[i
].l2p_table
= NULL
;
2455 ms_card
->segment
[i
].free_table
= NULL
;
2456 ms_card
->segment
[i
].get_index
= 0;
2457 ms_card
->segment
[i
].set_index
= 0;
2458 ms_card
->segment
[i
].unused_blk_cnt
= 0;
2460 dev_dbg(rtsx_dev(chip
), "defective block count of segment %d is %d\n",
2461 i
, ms_card
->segment
[i
].disable_count
);
2464 return STATUS_SUCCESS
;
2467 vfree(ms_card
->segment
);
2468 ms_card
->segment
= NULL
;
2473 static u16
ms_get_l2p_tbl(struct rtsx_chip
*chip
, int seg_no
, u16 log_off
)
2475 struct ms_info
*ms_card
= &chip
->ms_card
;
2476 struct zone_entry
*segment
;
2478 if (!ms_card
->segment
)
2481 segment
= &ms_card
->segment
[seg_no
];
2483 if (segment
->l2p_table
)
2484 return segment
->l2p_table
[log_off
];
2489 static void ms_set_l2p_tbl(struct rtsx_chip
*chip
,
2490 int seg_no
, u16 log_off
, u16 phy_blk
)
2492 struct ms_info
*ms_card
= &chip
->ms_card
;
2493 struct zone_entry
*segment
;
2495 if (!ms_card
->segment
)
2498 segment
= &ms_card
->segment
[seg_no
];
2499 if (segment
->l2p_table
)
2500 segment
->l2p_table
[log_off
] = phy_blk
;
2503 static void ms_set_unused_block(struct rtsx_chip
*chip
, u16 phy_blk
)
2505 struct ms_info
*ms_card
= &chip
->ms_card
;
2506 struct zone_entry
*segment
;
2509 seg_no
= (int)phy_blk
>> 9;
2510 segment
= &ms_card
->segment
[seg_no
];
2512 segment
->free_table
[segment
->set_index
++] = phy_blk
;
2513 if (segment
->set_index
>= MS_FREE_TABLE_CNT
)
2514 segment
->set_index
= 0;
2516 segment
->unused_blk_cnt
++;
2519 static u16
ms_get_unused_block(struct rtsx_chip
*chip
, int seg_no
)
2521 struct ms_info
*ms_card
= &chip
->ms_card
;
2522 struct zone_entry
*segment
;
2525 segment
= &ms_card
->segment
[seg_no
];
2527 if (segment
->unused_blk_cnt
<= 0)
2530 phy_blk
= segment
->free_table
[segment
->get_index
];
2531 segment
->free_table
[segment
->get_index
++] = 0xFFFF;
2532 if (segment
->get_index
>= MS_FREE_TABLE_CNT
)
2533 segment
->get_index
= 0;
2535 segment
->unused_blk_cnt
--;
2540 static const unsigned short ms_start_idx
[] = {0, 494, 990, 1486, 1982, 2478,
2541 2974, 3470, 3966, 4462, 4958,
2542 5454, 5950, 6446, 6942, 7438,
2545 static int ms_arbitrate_l2p(struct rtsx_chip
*chip
, u16 phy_blk
,
2546 u16 log_off
, u8 us1
, u8 us2
)
2548 struct ms_info
*ms_card
= &chip
->ms_card
;
2549 struct zone_entry
*segment
;
2553 seg_no
= (int)phy_blk
>> 9;
2554 segment
= &ms_card
->segment
[seg_no
];
2555 tmp_blk
= segment
->l2p_table
[log_off
];
2559 if (!(chip
->card_wp
& MS_CARD
))
2560 ms_erase_block(chip
, tmp_blk
);
2562 ms_set_unused_block(chip
, tmp_blk
);
2563 segment
->l2p_table
[log_off
] = phy_blk
;
2565 if (!(chip
->card_wp
& MS_CARD
))
2566 ms_erase_block(chip
, phy_blk
);
2568 ms_set_unused_block(chip
, phy_blk
);
2571 if (phy_blk
< tmp_blk
) {
2572 if (!(chip
->card_wp
& MS_CARD
))
2573 ms_erase_block(chip
, phy_blk
);
2575 ms_set_unused_block(chip
, phy_blk
);
2577 if (!(chip
->card_wp
& MS_CARD
))
2578 ms_erase_block(chip
, tmp_blk
);
2580 ms_set_unused_block(chip
, tmp_blk
);
2581 segment
->l2p_table
[log_off
] = phy_blk
;
2585 return STATUS_SUCCESS
;
2588 static int ms_build_l2p_tbl(struct rtsx_chip
*chip
, int seg_no
)
2590 struct ms_info
*ms_card
= &chip
->ms_card
;
2591 struct zone_entry
*segment
;
2593 int retval
, table_size
, disable_cnt
, i
;
2594 u16 start
, end
, phy_blk
, log_blk
, tmp_blk
, idx
;
2595 u8 extra
[MS_EXTRA_SIZE
], us1
, us2
;
2597 dev_dbg(rtsx_dev(chip
), "%s: %d\n", __func__
, seg_no
);
2599 if (!ms_card
->segment
) {
2600 retval
= ms_init_l2p_tbl(chip
);
2601 if (retval
!= STATUS_SUCCESS
) {
2607 if (ms_card
->segment
[seg_no
].build_flag
) {
2608 dev_dbg(rtsx_dev(chip
), "l2p table of segment %d has been built\n",
2610 return STATUS_SUCCESS
;
2618 segment
= &ms_card
->segment
[seg_no
];
2620 if (!segment
->l2p_table
) {
2621 segment
->l2p_table
= vmalloc(table_size
* 2);
2622 if (!segment
->l2p_table
) {
2627 memset((u8
*)(segment
->l2p_table
), 0xff, table_size
* 2);
2629 if (!segment
->free_table
) {
2630 segment
->free_table
= vmalloc(MS_FREE_TABLE_CNT
* 2);
2631 if (!segment
->free_table
) {
2636 memset((u8
*)(segment
->free_table
), 0xff, MS_FREE_TABLE_CNT
* 2);
2638 start
= (u16
)seg_no
<< 9;
2639 end
= (u16
)(seg_no
+ 1) << 9;
2641 disable_cnt
= segment
->disable_count
;
2643 segment
->get_index
= 0;
2644 segment
->set_index
= 0;
2645 segment
->unused_blk_cnt
= 0;
2647 for (phy_blk
= start
; phy_blk
< end
; phy_blk
++) {
2649 defect_flag
= false;
2650 for (i
= 0; i
< segment
->disable_count
; i
++) {
2651 if (phy_blk
== segment
->defect_list
[i
]) {
2662 retval
= ms_read_extra_data(chip
, phy_blk
, 0,
2663 extra
, MS_EXTRA_SIZE
);
2664 if (retval
!= STATUS_SUCCESS
) {
2665 dev_dbg(rtsx_dev(chip
), "read extra data fail\n");
2666 ms_set_bad_block(chip
, phy_blk
);
2670 if (seg_no
== ms_card
->segment_cnt
- 1) {
2671 if (!(extra
[1] & NOT_TRANSLATION_TABLE
)) {
2672 if (!(chip
->card_wp
& MS_CARD
)) {
2673 retval
= ms_erase_block(chip
, phy_blk
);
2674 if (retval
!= STATUS_SUCCESS
)
2682 if (!(extra
[0] & BLOCK_OK
))
2684 if (!(extra
[1] & NOT_BOOT_BLOCK
))
2686 if ((extra
[0] & PAGE_OK
) != PAGE_OK
)
2689 log_blk
= ((u16
)extra
[2] << 8) | extra
[3];
2691 if (log_blk
== 0xFFFF) {
2692 if (!(chip
->card_wp
& MS_CARD
)) {
2693 retval
= ms_erase_block(chip
, phy_blk
);
2694 if (retval
!= STATUS_SUCCESS
)
2697 ms_set_unused_block(chip
, phy_blk
);
2701 if ((log_blk
< ms_start_idx
[seg_no
]) ||
2702 (log_blk
>= ms_start_idx
[seg_no
+ 1])) {
2703 if (!(chip
->card_wp
& MS_CARD
)) {
2704 retval
= ms_erase_block(chip
, phy_blk
);
2705 if (retval
!= STATUS_SUCCESS
)
2708 ms_set_unused_block(chip
, phy_blk
);
2712 idx
= log_blk
- ms_start_idx
[seg_no
];
2714 if (segment
->l2p_table
[idx
] == 0xFFFF) {
2715 segment
->l2p_table
[idx
] = phy_blk
;
2719 us1
= extra
[0] & 0x10;
2720 tmp_blk
= segment
->l2p_table
[idx
];
2721 retval
= ms_read_extra_data(chip
, tmp_blk
, 0,
2722 extra
, MS_EXTRA_SIZE
);
2723 if (retval
!= STATUS_SUCCESS
)
2725 us2
= extra
[0] & 0x10;
2727 (void)ms_arbitrate_l2p(chip
, phy_blk
,
2728 log_blk
- ms_start_idx
[seg_no
], us1
, us2
);
2732 segment
->build_flag
= 1;
2734 dev_dbg(rtsx_dev(chip
), "unused block count: %d\n",
2735 segment
->unused_blk_cnt
);
2737 /* Logical Address Confirmation Process */
2738 if (seg_no
== ms_card
->segment_cnt
- 1) {
2739 if (segment
->unused_blk_cnt
< 2)
2740 chip
->card_wp
|= MS_CARD
;
2742 if (segment
->unused_blk_cnt
< 1)
2743 chip
->card_wp
|= MS_CARD
;
2746 if (chip
->card_wp
& MS_CARD
)
2747 return STATUS_SUCCESS
;
2749 for (log_blk
= ms_start_idx
[seg_no
];
2750 log_blk
< ms_start_idx
[seg_no
+ 1]; log_blk
++) {
2751 idx
= log_blk
- ms_start_idx
[seg_no
];
2752 if (segment
->l2p_table
[idx
] == 0xFFFF) {
2753 phy_blk
= ms_get_unused_block(chip
, seg_no
);
2754 if (phy_blk
== 0xFFFF) {
2755 chip
->card_wp
|= MS_CARD
;
2756 return STATUS_SUCCESS
;
2758 retval
= ms_init_page(chip
, phy_blk
, log_blk
, 0, 1);
2759 if (retval
!= STATUS_SUCCESS
) {
2764 segment
->l2p_table
[idx
] = phy_blk
;
2765 if (seg_no
== ms_card
->segment_cnt
- 1) {
2766 if (segment
->unused_blk_cnt
< 2) {
2767 chip
->card_wp
|= MS_CARD
;
2768 return STATUS_SUCCESS
;
2771 if (segment
->unused_blk_cnt
< 1) {
2772 chip
->card_wp
|= MS_CARD
;
2773 return STATUS_SUCCESS
;
2779 /* Make boot block be the first normal block */
2781 for (log_blk
= 0; log_blk
< 494; log_blk
++) {
2782 tmp_blk
= segment
->l2p_table
[log_blk
];
2783 if (tmp_blk
< ms_card
->boot_block
) {
2784 dev_dbg(rtsx_dev(chip
), "Boot block is not the first normal block.\n");
2786 if (chip
->card_wp
& MS_CARD
)
2789 phy_blk
= ms_get_unused_block(chip
, 0);
2790 retval
= ms_copy_page(chip
, tmp_blk
, phy_blk
,
2792 ms_card
->page_off
+ 1);
2793 if (retval
!= STATUS_SUCCESS
) {
2798 segment
->l2p_table
[log_blk
] = phy_blk
;
2800 retval
= ms_set_bad_block(chip
, tmp_blk
);
2801 if (retval
!= STATUS_SUCCESS
) {
2809 return STATUS_SUCCESS
;
2812 segment
->build_flag
= 0;
2813 vfree(segment
->l2p_table
);
2814 segment
->l2p_table
= NULL
;
2815 vfree(segment
->free_table
);
2816 segment
->free_table
= NULL
;
2821 int reset_ms_card(struct rtsx_chip
*chip
)
2823 struct ms_info
*ms_card
= &chip
->ms_card
;
2826 memset(ms_card
, 0, sizeof(struct ms_info
));
2828 retval
= enable_card_clock(chip
, MS_CARD
);
2829 if (retval
!= STATUS_SUCCESS
) {
2834 retval
= select_card(chip
, MS_CARD
);
2835 if (retval
!= STATUS_SUCCESS
) {
2840 ms_card
->ms_type
= 0;
2842 retval
= reset_ms_pro(chip
);
2843 if (retval
!= STATUS_SUCCESS
) {
2844 if (ms_card
->check_ms_flow
) {
2845 retval
= reset_ms(chip
);
2846 if (retval
!= STATUS_SUCCESS
) {
2856 retval
= ms_set_init_para(chip
);
2857 if (retval
!= STATUS_SUCCESS
) {
2862 if (!CHK_MSPRO(ms_card
)) {
2863 /* Build table for the last segment,
2864 * to check if L2P table block exists, erasing it
2866 retval
= ms_build_l2p_tbl(chip
, ms_card
->total_block
/ 512 - 1);
2867 if (retval
!= STATUS_SUCCESS
) {
2873 dev_dbg(rtsx_dev(chip
), "ms_card->ms_type = 0x%x\n", ms_card
->ms_type
);
2875 return STATUS_SUCCESS
;
2878 static int mspro_set_rw_cmd(struct rtsx_chip
*chip
,
2879 u32 start_sec
, u16 sec_cnt
, u8 cmd
)
2885 data
[1] = (u8
)(sec_cnt
>> 8);
2886 data
[2] = (u8
)sec_cnt
;
2887 data
[3] = (u8
)(start_sec
>> 24);
2888 data
[4] = (u8
)(start_sec
>> 16);
2889 data
[5] = (u8
)(start_sec
>> 8);
2890 data
[6] = (u8
)start_sec
;
2893 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
2894 retval
= ms_write_bytes(chip
, PRO_EX_SET_CMD
, 7,
2896 if (retval
== STATUS_SUCCESS
)
2899 if (i
== MS_MAX_RETRY_COUNT
) {
2904 return STATUS_SUCCESS
;
2907 void mspro_stop_seq_mode(struct rtsx_chip
*chip
)
2909 struct ms_info
*ms_card
= &chip
->ms_card
;
2912 if (ms_card
->seq_mode
) {
2913 retval
= ms_switch_clock(chip
);
2914 if (retval
!= STATUS_SUCCESS
)
2917 ms_card
->seq_mode
= 0;
2918 ms_card
->total_sec_cnt
= 0;
2919 ms_send_cmd(chip
, PRO_STOP
, WAIT_INT
);
2921 rtsx_write_register(chip
, RBCTL
, RB_FLUSH
, RB_FLUSH
);
2925 static inline int ms_auto_tune_clock(struct rtsx_chip
*chip
)
2927 struct ms_info
*ms_card
= &chip
->ms_card
;
2930 if (chip
->asic_code
) {
2931 if (ms_card
->ms_clock
> 30)
2932 ms_card
->ms_clock
-= 20;
2934 if (ms_card
->ms_clock
== CLK_80
)
2935 ms_card
->ms_clock
= CLK_60
;
2936 else if (ms_card
->ms_clock
== CLK_60
)
2937 ms_card
->ms_clock
= CLK_40
;
2940 retval
= ms_switch_clock(chip
);
2941 if (retval
!= STATUS_SUCCESS
) {
2946 return STATUS_SUCCESS
;
2949 static int mspro_rw_multi_sector(struct scsi_cmnd
*srb
,
2950 struct rtsx_chip
*chip
, u32 start_sector
,
2953 struct ms_info
*ms_card
= &chip
->ms_card
;
2954 bool mode_2k
= false;
2957 u8 val
, trans_mode
, rw_tpc
, rw_cmd
;
2959 ms_set_err_code(chip
, MS_NO_ERROR
);
2961 ms_card
->cleanup_counter
= 0;
2963 if (CHK_MSHG(ms_card
)) {
2964 if ((start_sector
% 4) || (sector_cnt
% 4)) {
2965 if (srb
->sc_data_direction
== DMA_FROM_DEVICE
) {
2966 rw_tpc
= PRO_READ_LONG_DATA
;
2967 rw_cmd
= PRO_READ_DATA
;
2969 rw_tpc
= PRO_WRITE_LONG_DATA
;
2970 rw_cmd
= PRO_WRITE_DATA
;
2973 if (srb
->sc_data_direction
== DMA_FROM_DEVICE
) {
2974 rw_tpc
= PRO_READ_QUAD_DATA
;
2975 rw_cmd
= PRO_READ_2K_DATA
;
2977 rw_tpc
= PRO_WRITE_QUAD_DATA
;
2978 rw_cmd
= PRO_WRITE_2K_DATA
;
2983 if (srb
->sc_data_direction
== DMA_FROM_DEVICE
) {
2984 rw_tpc
= PRO_READ_LONG_DATA
;
2985 rw_cmd
= PRO_READ_DATA
;
2987 rw_tpc
= PRO_WRITE_LONG_DATA
;
2988 rw_cmd
= PRO_WRITE_DATA
;
2992 retval
= ms_switch_clock(chip
);
2993 if (retval
!= STATUS_SUCCESS
) {
2998 if (srb
->sc_data_direction
== DMA_FROM_DEVICE
)
2999 trans_mode
= MS_TM_AUTO_READ
;
3001 trans_mode
= MS_TM_AUTO_WRITE
;
3003 retval
= rtsx_read_register(chip
, MS_TRANS_CFG
, &val
);
3009 if (ms_card
->seq_mode
) {
3010 if ((ms_card
->pre_dir
!= srb
->sc_data_direction
) ||
3011 ((ms_card
->pre_sec_addr
+ ms_card
->pre_sec_cnt
) !=
3013 (mode_2k
&& (ms_card
->seq_mode
& MODE_512_SEQ
)) ||
3014 (!mode_2k
&& (ms_card
->seq_mode
& MODE_2K_SEQ
)) ||
3015 !(val
& MS_INT_BREQ
) ||
3016 ((ms_card
->total_sec_cnt
+ sector_cnt
) > 0xFE00)) {
3017 ms_card
->seq_mode
= 0;
3018 ms_card
->total_sec_cnt
= 0;
3019 if (val
& MS_INT_BREQ
) {
3020 retval
= ms_send_cmd(chip
, PRO_STOP
, WAIT_INT
);
3021 if (retval
!= STATUS_SUCCESS
) {
3026 rtsx_write_register(chip
, RBCTL
, RB_FLUSH
,
3032 if (!ms_card
->seq_mode
) {
3033 ms_card
->total_sec_cnt
= 0;
3034 if (sector_cnt
>= SEQ_START_CRITERIA
) {
3035 if ((ms_card
->capacity
- start_sector
) > 0xFE00)
3038 count
= (u16
)(ms_card
->capacity
- start_sector
);
3040 if (count
> sector_cnt
) {
3042 ms_card
->seq_mode
= MODE_2K_SEQ
;
3044 ms_card
->seq_mode
= MODE_512_SEQ
;
3049 retval
= mspro_set_rw_cmd(chip
, start_sector
, count
, rw_cmd
);
3050 if (retval
!= STATUS_SUCCESS
) {
3051 ms_card
->seq_mode
= 0;
3057 retval
= ms_transfer_data(chip
, trans_mode
, rw_tpc
, sector_cnt
,
3058 WAIT_INT
, mode_2k
, scsi_sg_count(srb
),
3059 scsi_sglist(srb
), scsi_bufflen(srb
));
3060 if (retval
!= STATUS_SUCCESS
) {
3061 ms_card
->seq_mode
= 0;
3062 rtsx_read_register(chip
, MS_TRANS_CFG
, &val
);
3063 rtsx_clear_ms_error(chip
);
3065 if (detect_card_cd(chip
, MS_CARD
) != STATUS_SUCCESS
) {
3066 chip
->rw_need_retry
= 0;
3067 dev_dbg(rtsx_dev(chip
), "No card exist, exit %s\n",
3073 if (val
& MS_INT_BREQ
)
3074 ms_send_cmd(chip
, PRO_STOP
, WAIT_INT
);
3076 if (val
& (MS_CRC16_ERR
| MS_RDY_TIMEOUT
)) {
3077 dev_dbg(rtsx_dev(chip
), "MSPro CRC error, tune clock!\n");
3078 chip
->rw_need_retry
= 1;
3079 ms_auto_tune_clock(chip
);
3086 if (ms_card
->seq_mode
) {
3087 ms_card
->pre_sec_addr
= start_sector
;
3088 ms_card
->pre_sec_cnt
= sector_cnt
;
3089 ms_card
->pre_dir
= srb
->sc_data_direction
;
3090 ms_card
->total_sec_cnt
+= sector_cnt
;
3093 return STATUS_SUCCESS
;
3096 static int mspro_read_format_progress(struct rtsx_chip
*chip
,
3097 const int short_data_len
)
3099 struct ms_info
*ms_card
= &chip
->ms_card
;
3101 u32 total_progress
, cur_progress
;
3105 dev_dbg(rtsx_dev(chip
), "%s, short_data_len = %d\n", __func__
,
3108 retval
= ms_switch_clock(chip
);
3109 if (retval
!= STATUS_SUCCESS
) {
3110 ms_card
->format_status
= FORMAT_FAIL
;
3115 retval
= rtsx_read_register(chip
, MS_TRANS_CFG
, &tmp
);
3116 if (retval
!= STATUS_SUCCESS
) {
3117 ms_card
->format_status
= FORMAT_FAIL
;
3122 if (!(tmp
& MS_INT_BREQ
)) {
3123 if ((tmp
& (MS_INT_CED
| MS_INT_BREQ
| MS_INT_CMDNK
|
3124 MS_INT_ERR
)) == MS_INT_CED
) {
3125 ms_card
->format_status
= FORMAT_SUCCESS
;
3126 return STATUS_SUCCESS
;
3128 ms_card
->format_status
= FORMAT_FAIL
;
3133 if (short_data_len
>= 256)
3136 cnt
= (u8
)short_data_len
;
3138 retval
= rtsx_write_register(chip
, MS_CFG
, MS_NO_CHECK_INT
,
3140 if (retval
!= STATUS_SUCCESS
) {
3141 ms_card
->format_status
= FORMAT_FAIL
;
3146 retval
= ms_read_bytes(chip
, PRO_READ_SHORT_DATA
, cnt
, WAIT_INT
,
3148 if (retval
!= STATUS_SUCCESS
) {
3149 ms_card
->format_status
= FORMAT_FAIL
;
3154 total_progress
= (data
[0] << 24) | (data
[1] << 16) |
3155 (data
[2] << 8) | data
[3];
3156 cur_progress
= (data
[4] << 24) | (data
[5] << 16) |
3157 (data
[6] << 8) | data
[7];
3159 dev_dbg(rtsx_dev(chip
), "total_progress = %d, cur_progress = %d\n",
3160 total_progress
, cur_progress
);
3162 if (total_progress
== 0) {
3163 ms_card
->progress
= 0;
3165 u64 ulltmp
= (u64
)cur_progress
* (u64
)65535;
3167 do_div(ulltmp
, total_progress
);
3168 ms_card
->progress
= (u16
)ulltmp
;
3170 dev_dbg(rtsx_dev(chip
), "progress = %d\n", ms_card
->progress
);
3172 for (i
= 0; i
< 5000; i
++) {
3173 retval
= rtsx_read_register(chip
, MS_TRANS_CFG
, &tmp
);
3174 if (retval
!= STATUS_SUCCESS
) {
3175 ms_card
->format_status
= FORMAT_FAIL
;
3179 if (tmp
& (MS_INT_CED
| MS_INT_CMDNK
|
3180 MS_INT_BREQ
| MS_INT_ERR
))
3186 retval
= rtsx_write_register(chip
, MS_CFG
, MS_NO_CHECK_INT
, 0);
3187 if (retval
!= STATUS_SUCCESS
) {
3188 ms_card
->format_status
= FORMAT_FAIL
;
3194 ms_card
->format_status
= FORMAT_FAIL
;
3199 if (tmp
& (MS_INT_CMDNK
| MS_INT_ERR
)) {
3200 ms_card
->format_status
= FORMAT_FAIL
;
3205 if (tmp
& MS_INT_CED
) {
3206 ms_card
->format_status
= FORMAT_SUCCESS
;
3207 ms_card
->pro_under_formatting
= 0;
3208 } else if (tmp
& MS_INT_BREQ
) {
3209 ms_card
->format_status
= FORMAT_IN_PROGRESS
;
3211 ms_card
->format_status
= FORMAT_FAIL
;
3212 ms_card
->pro_under_formatting
= 0;
3217 return STATUS_SUCCESS
;
3220 void mspro_polling_format_status(struct rtsx_chip
*chip
)
3222 struct ms_info
*ms_card
= &chip
->ms_card
;
3225 if (ms_card
->pro_under_formatting
&&
3226 (rtsx_get_stat(chip
) != RTSX_STAT_SS
)) {
3227 rtsx_set_stat(chip
, RTSX_STAT_RUN
);
3229 for (i
= 0; i
< 65535; i
++) {
3230 mspro_read_format_progress(chip
, MS_SHORT_DATA_LEN
);
3231 if (ms_card
->format_status
!= FORMAT_IN_PROGRESS
)
3237 int mspro_format(struct scsi_cmnd
*srb
, struct rtsx_chip
*chip
,
3238 int short_data_len
, bool quick_format
)
3240 struct ms_info
*ms_card
= &chip
->ms_card
;
3245 retval
= ms_switch_clock(chip
);
3246 if (retval
!= STATUS_SUCCESS
) {
3251 retval
= ms_set_rw_reg_addr(chip
, 0x00, 0x00, Pro_TPCParm
, 0x01);
3252 if (retval
!= STATUS_SUCCESS
) {
3258 switch (short_data_len
) {
3274 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
3275 retval
= ms_write_bytes(chip
, PRO_WRITE_REG
, 1,
3276 NO_WAIT_INT
, buf
, 2);
3277 if (retval
== STATUS_SUCCESS
)
3280 if (i
== MS_MAX_RETRY_COUNT
) {
3290 retval
= mspro_set_rw_cmd(chip
, 0, para
, PRO_FORMAT
);
3291 if (retval
!= STATUS_SUCCESS
) {
3296 retval
= rtsx_read_register(chip
, MS_TRANS_CFG
, &tmp
);
3302 if (tmp
& (MS_INT_CMDNK
| MS_INT_ERR
)) {
3307 if ((tmp
& (MS_INT_BREQ
| MS_INT_CED
)) == MS_INT_BREQ
) {
3308 ms_card
->pro_under_formatting
= 1;
3309 ms_card
->progress
= 0;
3310 ms_card
->format_status
= FORMAT_IN_PROGRESS
;
3311 return STATUS_SUCCESS
;
3314 if (tmp
& MS_INT_CED
) {
3315 ms_card
->pro_under_formatting
= 0;
3316 ms_card
->progress
= 0;
3317 ms_card
->format_status
= FORMAT_SUCCESS
;
3318 set_sense_type(chip
, SCSI_LUN(srb
), SENSE_TYPE_NO_SENSE
);
3319 return STATUS_SUCCESS
;
3326 static int ms_read_multiple_pages(struct rtsx_chip
*chip
, u16 phy_blk
,
3327 u16 log_blk
, u8 start_page
, u8 end_page
,
3328 u8
*buf
, unsigned int *index
,
3329 unsigned int *offset
)
3331 struct ms_info
*ms_card
= &chip
->ms_card
;
3333 u8 extra
[MS_EXTRA_SIZE
], page_addr
, val
, trans_cfg
, data
[6];
3336 retval
= ms_read_extra_data(chip
, phy_blk
, start_page
,
3337 extra
, MS_EXTRA_SIZE
);
3338 if (retval
== STATUS_SUCCESS
) {
3339 if ((extra
[1] & 0x30) != 0x30) {
3340 ms_set_err_code(chip
, MS_FLASH_READ_ERROR
);
3346 retval
= ms_set_rw_reg_addr(chip
, OverwriteFlag
, MS_EXTRA_SIZE
,
3348 if (retval
!= STATUS_SUCCESS
) {
3353 if (CHK_MS4BIT(ms_card
))
3359 data
[2] = (u8
)(phy_blk
>> 8);
3360 data
[3] = (u8
)phy_blk
;
3362 data
[5] = start_page
;
3364 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
3365 retval
= ms_write_bytes(chip
, WRITE_REG
, 6, NO_WAIT_INT
,
3367 if (retval
== STATUS_SUCCESS
)
3370 if (i
== MS_MAX_RETRY_COUNT
) {
3375 ms_set_err_code(chip
, MS_NO_ERROR
);
3377 retval
= ms_send_cmd(chip
, BLOCK_READ
, WAIT_INT
);
3378 if (retval
!= STATUS_SUCCESS
) {
3385 for (page_addr
= start_page
; page_addr
< end_page
; page_addr
++) {
3386 ms_set_err_code(chip
, MS_NO_ERROR
);
3388 if (detect_card_cd(chip
, MS_CARD
) != STATUS_SUCCESS
) {
3389 ms_set_err_code(chip
, MS_NO_CARD
);
3394 retval
= ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
, 1);
3395 if (retval
!= STATUS_SUCCESS
) {
3400 if (val
& INT_REG_CMDNK
) {
3401 ms_set_err_code(chip
, MS_CMD_NK
);
3405 if (val
& INT_REG_ERR
) {
3406 if (val
& INT_REG_BREQ
) {
3407 retval
= ms_read_status_reg(chip
);
3408 if (retval
!= STATUS_SUCCESS
) {
3409 if (!(chip
->card_wp
& MS_CARD
)) {
3420 ms_set_err_code(chip
,
3421 MS_FLASH_READ_ERROR
);
3426 ms_set_err_code(chip
, MS_FLASH_READ_ERROR
);
3431 if (!(val
& INT_REG_BREQ
)) {
3432 ms_set_err_code(chip
, MS_BREQ_ERROR
);
3438 if (page_addr
== (end_page
- 1)) {
3439 if (!(val
& INT_REG_CED
)) {
3440 retval
= ms_send_cmd(chip
, BLOCK_END
, WAIT_INT
);
3441 if (retval
!= STATUS_SUCCESS
) {
3447 retval
= ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
,
3449 if (retval
!= STATUS_SUCCESS
) {
3454 if (!(val
& INT_REG_CED
)) {
3455 ms_set_err_code(chip
, MS_FLASH_READ_ERROR
);
3460 trans_cfg
= NO_WAIT_INT
;
3462 trans_cfg
= WAIT_INT
;
3465 rtsx_init_cmd(chip
);
3467 rtsx_add_cmd(chip
, WRITE_REG_CMD
, MS_TPC
, 0xFF, READ_PAGE_DATA
);
3468 rtsx_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANS_CFG
,
3470 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
,
3473 trans_dma_enable(DMA_FROM_DEVICE
, chip
, 512, DMA_512
);
3475 rtsx_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANSFER
, 0xFF,
3476 MS_TRANSFER_START
| MS_TM_NORMAL_READ
);
3477 rtsx_add_cmd(chip
, CHECK_REG_CMD
, MS_TRANSFER
,
3478 MS_TRANSFER_END
, MS_TRANSFER_END
);
3480 rtsx_send_cmd_no_wait(chip
);
3482 retval
= rtsx_transfer_data_partial(chip
, MS_CARD
, ptr
, 512,
3483 scsi_sg_count(chip
->srb
),
3488 if (retval
== -ETIMEDOUT
) {
3489 ms_set_err_code(chip
, MS_TO_ERROR
);
3490 rtsx_clear_ms_error(chip
);
3492 return STATUS_TIMEDOUT
;
3495 retval
= rtsx_read_register(chip
, MS_TRANS_CFG
, &val
);
3496 if (retval
!= STATUS_SUCCESS
) {
3497 ms_set_err_code(chip
, MS_TO_ERROR
);
3498 rtsx_clear_ms_error(chip
);
3500 return STATUS_TIMEDOUT
;
3502 if (val
& (MS_CRC16_ERR
| MS_RDY_TIMEOUT
)) {
3503 ms_set_err_code(chip
, MS_CRC16_ERROR
);
3504 rtsx_clear_ms_error(chip
);
3510 if (scsi_sg_count(chip
->srb
) == 0)
3514 return STATUS_SUCCESS
;
3517 static int ms_write_multiple_pages(struct rtsx_chip
*chip
, u16 old_blk
,
3518 u16 new_blk
, u16 log_blk
, u8 start_page
,
3519 u8 end_page
, u8
*buf
, unsigned int *index
,
3520 unsigned int *offset
)
3522 struct ms_info
*ms_card
= &chip
->ms_card
;
3524 u8 page_addr
, val
, data
[16];
3528 retval
= ms_set_rw_reg_addr(chip
, OverwriteFlag
, MS_EXTRA_SIZE
,
3530 if (retval
!= STATUS_SUCCESS
) {
3535 if (CHK_MS4BIT(ms_card
))
3541 data
[2] = (u8
)(old_blk
>> 8);
3542 data
[3] = (u8
)old_blk
;
3548 retval
= ms_write_bytes(chip
, WRITE_REG
, 7, NO_WAIT_INT
,
3550 if (retval
!= STATUS_SUCCESS
) {
3555 retval
= ms_send_cmd(chip
, BLOCK_WRITE
, WAIT_INT
);
3556 if (retval
!= STATUS_SUCCESS
) {
3561 ms_set_err_code(chip
, MS_NO_ERROR
);
3562 retval
= ms_transfer_tpc(chip
, MS_TM_READ_BYTES
, GET_INT
, 1,
3564 if (retval
!= STATUS_SUCCESS
) {
3570 retval
= ms_set_rw_reg_addr(chip
, OverwriteFlag
, MS_EXTRA_SIZE
,
3571 SystemParm
, (6 + MS_EXTRA_SIZE
));
3572 if (retval
!= STATUS_SUCCESS
) {
3577 ms_set_err_code(chip
, MS_NO_ERROR
);
3579 if (CHK_MS4BIT(ms_card
))
3585 data
[2] = (u8
)(new_blk
>> 8);
3586 data
[3] = (u8
)new_blk
;
3587 if ((end_page
- start_page
) == 1)
3592 data
[5] = start_page
;
3595 data
[8] = (u8
)(log_blk
>> 8);
3596 data
[9] = (u8
)log_blk
;
3598 for (i
= 0x0A; i
< 0x10; i
++)
3601 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
3602 retval
= ms_write_bytes(chip
, WRITE_REG
, 6 + MS_EXTRA_SIZE
,
3603 NO_WAIT_INT
, data
, 16);
3604 if (retval
== STATUS_SUCCESS
)
3607 if (i
== MS_MAX_RETRY_COUNT
) {
3612 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
3613 retval
= ms_send_cmd(chip
, BLOCK_WRITE
, WAIT_INT
);
3614 if (retval
== STATUS_SUCCESS
)
3617 if (i
== MS_MAX_RETRY_COUNT
) {
3622 retval
= ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
, 1);
3623 if (retval
!= STATUS_SUCCESS
) {
3629 for (page_addr
= start_page
; page_addr
< end_page
; page_addr
++) {
3630 ms_set_err_code(chip
, MS_NO_ERROR
);
3632 if (detect_card_cd(chip
, MS_CARD
) != STATUS_SUCCESS
) {
3633 ms_set_err_code(chip
, MS_NO_CARD
);
3638 if (val
& INT_REG_CMDNK
) {
3639 ms_set_err_code(chip
, MS_CMD_NK
);
3643 if (val
& INT_REG_ERR
) {
3644 ms_set_err_code(chip
, MS_FLASH_WRITE_ERROR
);
3648 if (!(val
& INT_REG_BREQ
)) {
3649 ms_set_err_code(chip
, MS_BREQ_ERROR
);
3656 rtsx_init_cmd(chip
);
3658 rtsx_add_cmd(chip
, WRITE_REG_CMD
, MS_TPC
,
3659 0xFF, WRITE_PAGE_DATA
);
3660 rtsx_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANS_CFG
,
3662 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
,
3665 trans_dma_enable(DMA_TO_DEVICE
, chip
, 512, DMA_512
);
3667 rtsx_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANSFER
, 0xFF,
3668 MS_TRANSFER_START
| MS_TM_NORMAL_WRITE
);
3669 rtsx_add_cmd(chip
, CHECK_REG_CMD
, MS_TRANSFER
,
3670 MS_TRANSFER_END
, MS_TRANSFER_END
);
3672 rtsx_send_cmd_no_wait(chip
);
3674 retval
= rtsx_transfer_data_partial(chip
, MS_CARD
, ptr
, 512,
3675 scsi_sg_count(chip
->srb
),
3680 ms_set_err_code(chip
, MS_TO_ERROR
);
3681 rtsx_clear_ms_error(chip
);
3683 if (retval
== -ETIMEDOUT
) {
3685 return STATUS_TIMEDOUT
;
3691 retval
= ms_read_bytes(chip
, GET_INT
, 1, NO_WAIT_INT
, &val
, 1);
3692 if (retval
!= STATUS_SUCCESS
) {
3697 if ((end_page
- start_page
) == 1) {
3698 if (!(val
& INT_REG_CED
)) {
3699 ms_set_err_code(chip
, MS_FLASH_WRITE_ERROR
);
3704 if (page_addr
== (end_page
- 1)) {
3705 if (!(val
& INT_REG_CED
)) {
3706 retval
= ms_send_cmd(chip
, BLOCK_END
,
3708 if (retval
!= STATUS_SUCCESS
) {
3714 retval
= ms_read_bytes(chip
, GET_INT
, 1,
3715 NO_WAIT_INT
, &val
, 1);
3716 if (retval
!= STATUS_SUCCESS
) {
3722 if ((page_addr
== (end_page
- 1)) ||
3723 (page_addr
== ms_card
->page_off
)) {
3724 if (!(val
& INT_REG_CED
)) {
3725 ms_set_err_code(chip
,
3726 MS_FLASH_WRITE_ERROR
);
3733 if (scsi_sg_count(chip
->srb
) == 0)
3737 return STATUS_SUCCESS
;
3740 static int ms_finish_write(struct rtsx_chip
*chip
, u16 old_blk
, u16 new_blk
,
3741 u16 log_blk
, u8 page_off
)
3743 struct ms_info
*ms_card
= &chip
->ms_card
;
3746 retval
= ms_copy_page(chip
, old_blk
, new_blk
, log_blk
,
3747 page_off
, ms_card
->page_off
+ 1);
3748 if (retval
!= STATUS_SUCCESS
) {
3753 seg_no
= old_blk
>> 9;
3755 if (MS_TST_BAD_BLOCK_FLG(ms_card
)) {
3756 MS_CLR_BAD_BLOCK_FLG(ms_card
);
3757 ms_set_bad_block(chip
, old_blk
);
3759 retval
= ms_erase_block(chip
, old_blk
);
3760 if (retval
== STATUS_SUCCESS
)
3761 ms_set_unused_block(chip
, old_blk
);
3764 ms_set_l2p_tbl(chip
, seg_no
, log_blk
- ms_start_idx
[seg_no
], new_blk
);
3766 return STATUS_SUCCESS
;
3769 static int ms_prepare_write(struct rtsx_chip
*chip
, u16 old_blk
, u16 new_blk
,
3770 u16 log_blk
, u8 start_page
)
3775 retval
= ms_copy_page(chip
, old_blk
, new_blk
, log_blk
,
3777 if (retval
!= STATUS_SUCCESS
) {
3783 return STATUS_SUCCESS
;
3786 #ifdef MS_DELAY_WRITE
3787 int ms_delay_write(struct rtsx_chip
*chip
)
3789 struct ms_info
*ms_card
= &chip
->ms_card
;
3790 struct ms_delay_write_tag
*delay_write
= &ms_card
->delay_write
;
3793 if (delay_write
->delay_write_flag
) {
3794 retval
= ms_set_init_para(chip
);
3795 if (retval
!= STATUS_SUCCESS
) {
3800 delay_write
->delay_write_flag
= 0;
3801 retval
= ms_finish_write(chip
,
3802 delay_write
->old_phyblock
,
3803 delay_write
->new_phyblock
,
3804 delay_write
->logblock
,
3805 delay_write
->pageoff
);
3806 if (retval
!= STATUS_SUCCESS
) {
3812 return STATUS_SUCCESS
;
3816 static inline void ms_rw_fail(struct scsi_cmnd
*srb
, struct rtsx_chip
*chip
)
3818 if (srb
->sc_data_direction
== DMA_FROM_DEVICE
)
3819 set_sense_type(chip
, SCSI_LUN(srb
),
3820 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR
);
3822 set_sense_type(chip
, SCSI_LUN(srb
), SENSE_TYPE_MEDIA_WRITE_ERR
);
3825 static int ms_rw_multi_sector(struct scsi_cmnd
*srb
, struct rtsx_chip
*chip
,
3826 u32 start_sector
, u16 sector_cnt
)
3828 struct ms_info
*ms_card
= &chip
->ms_card
;
3829 unsigned int lun
= SCSI_LUN(srb
);
3831 unsigned int index
= 0, offset
= 0;
3832 u16 old_blk
= 0, new_blk
= 0, log_blk
, total_sec_cnt
= sector_cnt
;
3833 u8 start_page
, end_page
= 0, page_cnt
;
3835 #ifdef MS_DELAY_WRITE
3836 struct ms_delay_write_tag
*delay_write
= &ms_card
->delay_write
;
3839 ms_set_err_code(chip
, MS_NO_ERROR
);
3841 ms_card
->cleanup_counter
= 0;
3843 ptr
= (u8
*)scsi_sglist(srb
);
3845 retval
= ms_switch_clock(chip
);
3846 if (retval
!= STATUS_SUCCESS
) {
3847 ms_rw_fail(srb
, chip
);
3852 log_blk
= (u16
)(start_sector
>> ms_card
->block_shift
);
3853 start_page
= (u8
)(start_sector
& ms_card
->page_off
);
3855 for (seg_no
= 0; seg_no
< ARRAY_SIZE(ms_start_idx
) - 1; seg_no
++) {
3856 if (log_blk
< ms_start_idx
[seg_no
+ 1])
3860 if (ms_card
->segment
[seg_no
].build_flag
== 0) {
3861 retval
= ms_build_l2p_tbl(chip
, seg_no
);
3862 if (retval
!= STATUS_SUCCESS
) {
3863 chip
->card_fail
|= MS_CARD
;
3864 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_NOT_PRESENT
);
3870 if (srb
->sc_data_direction
== DMA_TO_DEVICE
) {
3871 #ifdef MS_DELAY_WRITE
3872 if (delay_write
->delay_write_flag
&&
3873 (delay_write
->logblock
== log_blk
) &&
3874 (start_page
> delay_write
->pageoff
)) {
3875 delay_write
->delay_write_flag
= 0;
3876 retval
= ms_copy_page(chip
,
3877 delay_write
->old_phyblock
,
3878 delay_write
->new_phyblock
,
3880 delay_write
->pageoff
, start_page
);
3881 if (retval
!= STATUS_SUCCESS
) {
3882 set_sense_type(chip
, lun
,
3883 SENSE_TYPE_MEDIA_WRITE_ERR
);
3887 old_blk
= delay_write
->old_phyblock
;
3888 new_blk
= delay_write
->new_phyblock
;
3889 } else if (delay_write
->delay_write_flag
&&
3890 (delay_write
->logblock
== log_blk
) &&
3891 (start_page
== delay_write
->pageoff
)) {
3892 delay_write
->delay_write_flag
= 0;
3893 old_blk
= delay_write
->old_phyblock
;
3894 new_blk
= delay_write
->new_phyblock
;
3896 retval
= ms_delay_write(chip
);
3897 if (retval
!= STATUS_SUCCESS
) {
3898 set_sense_type(chip
, lun
,
3899 SENSE_TYPE_MEDIA_WRITE_ERR
);
3904 old_blk
= ms_get_l2p_tbl
3906 log_blk
- ms_start_idx
[seg_no
]);
3907 new_blk
= ms_get_unused_block(chip
, seg_no
);
3908 if ((old_blk
== 0xFFFF) || (new_blk
== 0xFFFF)) {
3909 set_sense_type(chip
, lun
,
3910 SENSE_TYPE_MEDIA_WRITE_ERR
);
3915 retval
= ms_prepare_write(chip
, old_blk
, new_blk
,
3916 log_blk
, start_page
);
3917 if (retval
!= STATUS_SUCCESS
) {
3918 if (detect_card_cd(chip
, MS_CARD
) !=
3922 SENSE_TYPE_MEDIA_NOT_PRESENT
);
3926 set_sense_type(chip
, lun
,
3927 SENSE_TYPE_MEDIA_WRITE_ERR
);
3931 #ifdef MS_DELAY_WRITE
3935 #ifdef MS_DELAY_WRITE
3936 retval
= ms_delay_write(chip
);
3937 if (retval
!= STATUS_SUCCESS
) {
3938 if (detect_card_cd(chip
, MS_CARD
) != STATUS_SUCCESS
) {
3939 set_sense_type(chip
, lun
,
3940 SENSE_TYPE_MEDIA_NOT_PRESENT
);
3944 set_sense_type(chip
, lun
,
3945 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR
);
3950 old_blk
= ms_get_l2p_tbl(chip
, seg_no
,
3951 log_blk
- ms_start_idx
[seg_no
]);
3952 if (old_blk
== 0xFFFF) {
3953 set_sense_type(chip
, lun
,
3954 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR
);
3960 dev_dbg(rtsx_dev(chip
), "seg_no = %d, old_blk = 0x%x, new_blk = 0x%x\n",
3961 seg_no
, old_blk
, new_blk
);
3963 while (total_sec_cnt
) {
3964 if ((start_page
+ total_sec_cnt
) > (ms_card
->page_off
+ 1))
3965 end_page
= ms_card
->page_off
+ 1;
3967 end_page
= start_page
+ (u8
)total_sec_cnt
;
3969 page_cnt
= end_page
- start_page
;
3971 dev_dbg(rtsx_dev(chip
), "start_page = %d, end_page = %d, page_cnt = %d\n",
3972 start_page
, end_page
, page_cnt
);
3974 if (srb
->sc_data_direction
== DMA_FROM_DEVICE
) {
3975 retval
= ms_read_multiple_pages(chip
,
3977 start_page
, end_page
,
3978 ptr
, &index
, &offset
);
3980 retval
= ms_write_multiple_pages(chip
, old_blk
, new_blk
,
3981 log_blk
, start_page
,
3982 end_page
, ptr
, &index
,
3986 if (retval
!= STATUS_SUCCESS
) {
3987 toggle_gpio(chip
, 1);
3988 if (detect_card_cd(chip
, MS_CARD
) != STATUS_SUCCESS
) {
3989 set_sense_type(chip
, lun
,
3990 SENSE_TYPE_MEDIA_NOT_PRESENT
);
3994 ms_rw_fail(srb
, chip
);
3999 if (srb
->sc_data_direction
== DMA_TO_DEVICE
) {
4000 if (end_page
== (ms_card
->page_off
+ 1)) {
4001 retval
= ms_erase_block(chip
, old_blk
);
4002 if (retval
== STATUS_SUCCESS
)
4003 ms_set_unused_block(chip
, old_blk
);
4005 ms_set_l2p_tbl(chip
, seg_no
,
4006 log_blk
- ms_start_idx
[seg_no
],
4011 total_sec_cnt
-= page_cnt
;
4012 if (scsi_sg_count(srb
) == 0)
4013 ptr
+= page_cnt
* 512;
4015 if (total_sec_cnt
== 0)
4020 for (seg_no
= 0; seg_no
< ARRAY_SIZE(ms_start_idx
) - 1;
4022 if (log_blk
< ms_start_idx
[seg_no
+ 1])
4026 if (ms_card
->segment
[seg_no
].build_flag
== 0) {
4027 retval
= ms_build_l2p_tbl(chip
, seg_no
);
4028 if (retval
!= STATUS_SUCCESS
) {
4029 chip
->card_fail
|= MS_CARD
;
4030 set_sense_type(chip
, lun
,
4031 SENSE_TYPE_MEDIA_NOT_PRESENT
);
4037 old_blk
= ms_get_l2p_tbl(chip
, seg_no
,
4038 log_blk
- ms_start_idx
[seg_no
]);
4039 if (old_blk
== 0xFFFF) {
4040 ms_rw_fail(srb
, chip
);
4045 if (srb
->sc_data_direction
== DMA_TO_DEVICE
) {
4046 new_blk
= ms_get_unused_block(chip
, seg_no
);
4047 if (new_blk
== 0xFFFF) {
4048 ms_rw_fail(srb
, chip
);
4054 dev_dbg(rtsx_dev(chip
), "seg_no = %d, old_blk = 0x%x, new_blk = 0x%x\n",
4055 seg_no
, old_blk
, new_blk
);
4060 if (srb
->sc_data_direction
== DMA_TO_DEVICE
) {
4061 if (end_page
< (ms_card
->page_off
+ 1)) {
4062 #ifdef MS_DELAY_WRITE
4063 delay_write
->delay_write_flag
= 1;
4064 delay_write
->old_phyblock
= old_blk
;
4065 delay_write
->new_phyblock
= new_blk
;
4066 delay_write
->logblock
= log_blk
;
4067 delay_write
->pageoff
= end_page
;
4069 retval
= ms_finish_write(chip
, old_blk
, new_blk
,
4071 if (retval
!= STATUS_SUCCESS
) {
4072 if (detect_card_cd(chip
, MS_CARD
) !=
4076 SENSE_TYPE_MEDIA_NOT_PRESENT
);
4081 ms_rw_fail(srb
, chip
);
4089 scsi_set_resid(srb
, 0);
4091 return STATUS_SUCCESS
;
4094 int ms_rw(struct scsi_cmnd
*srb
, struct rtsx_chip
*chip
,
4095 u32 start_sector
, u16 sector_cnt
)
4097 struct ms_info
*ms_card
= &chip
->ms_card
;
4100 if (CHK_MSPRO(ms_card
))
4101 retval
= mspro_rw_multi_sector(srb
, chip
, start_sector
,
4104 retval
= ms_rw_multi_sector(srb
, chip
, start_sector
,
4110 void ms_free_l2p_tbl(struct rtsx_chip
*chip
)
4112 struct ms_info
*ms_card
= &chip
->ms_card
;
4115 if (ms_card
->segment
) {
4116 for (i
= 0; i
< ms_card
->segment_cnt
; i
++) {
4117 vfree(ms_card
->segment
[i
].l2p_table
);
4118 ms_card
->segment
[i
].l2p_table
= NULL
;
4119 vfree(ms_card
->segment
[i
].free_table
);
4120 ms_card
->segment
[i
].free_table
= NULL
;
4122 vfree(ms_card
->segment
);
4123 ms_card
->segment
= NULL
;
4127 #ifdef SUPPORT_MAGIC_GATE
4129 #ifdef READ_BYTES_WAIT_INT
4130 static int ms_poll_int(struct rtsx_chip
*chip
)
4135 rtsx_init_cmd(chip
);
4137 rtsx_add_cmd(chip
, CHECK_REG_CMD
, MS_TRANS_CFG
, MS_INT_CED
, MS_INT_CED
);
4139 retval
= rtsx_send_cmd(chip
, MS_CARD
, 5000);
4140 if (retval
!= STATUS_SUCCESS
) {
4145 val
= *rtsx_get_cmd_data(chip
);
4146 if (val
& MS_INT_ERR
) {
4151 return STATUS_SUCCESS
;
4155 #ifdef MS_SAMPLE_INT_ERR
4156 static int check_ms_err(struct rtsx_chip
*chip
)
4161 retval
= rtsx_read_register(chip
, MS_TRANSFER
, &val
);
4162 if (retval
!= STATUS_SUCCESS
)
4164 if (val
& MS_TRANSFER_ERR
)
4167 retval
= rtsx_read_register(chip
, MS_TRANS_CFG
, &val
);
4168 if (retval
!= STATUS_SUCCESS
)
4171 if (val
& (MS_INT_ERR
| MS_INT_CMDNK
))
4177 static int check_ms_err(struct rtsx_chip
*chip
)
4182 retval
= rtsx_read_register(chip
, MS_TRANSFER
, &val
);
4183 if (retval
!= STATUS_SUCCESS
)
4185 if (val
& MS_TRANSFER_ERR
)
4192 static int mg_send_ex_cmd(struct rtsx_chip
*chip
, u8 cmd
, u8 entry_num
)
4203 data
[6] = entry_num
;
4206 for (i
= 0; i
< MS_MAX_RETRY_COUNT
; i
++) {
4207 retval
= ms_write_bytes(chip
, PRO_EX_SET_CMD
, 7, WAIT_INT
,
4209 if (retval
== STATUS_SUCCESS
)
4212 if (i
== MS_MAX_RETRY_COUNT
) {
4217 if (check_ms_err(chip
)) {
4218 rtsx_clear_ms_error(chip
);
4223 return STATUS_SUCCESS
;
4226 static int mg_set_tpc_para_sub(struct rtsx_chip
*chip
, int type
,
4233 retval
= ms_set_rw_reg_addr(chip
, 0, 0, Pro_TPCParm
, 1);
4235 retval
= ms_set_rw_reg_addr(chip
, 0, 0, Pro_DataCount1
, 6);
4237 if (retval
!= STATUS_SUCCESS
) {
4248 buf
[5] = mg_entry_num
;
4250 retval
= ms_write_bytes(chip
, PRO_WRITE_REG
, (type
== 0) ? 1 : 6,
4251 NO_WAIT_INT
, buf
, 6);
4252 if (retval
!= STATUS_SUCCESS
) {
4257 return STATUS_SUCCESS
;
4260 int mg_set_leaf_id(struct scsi_cmnd
*srb
, struct rtsx_chip
*chip
)
4264 unsigned int lun
= SCSI_LUN(srb
);
4265 u8 buf1
[32], buf2
[12];
4267 if (scsi_bufflen(srb
) < 12) {
4268 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
4273 ms_cleanup_work(chip
);
4275 retval
= ms_switch_clock(chip
);
4276 if (retval
!= STATUS_SUCCESS
) {
4281 retval
= mg_send_ex_cmd(chip
, MG_SET_LID
, 0);
4282 if (retval
!= STATUS_SUCCESS
) {
4283 set_sense_type(chip
, lun
, SENSE_TYPE_MG_KEY_FAIL_NOT_ESTAB
);
4288 memset(buf1
, 0, 32);
4289 rtsx_stor_get_xfer_buf(buf2
, min_t(int, 12, scsi_bufflen(srb
)), srb
);
4290 for (i
= 0; i
< 8; i
++)
4291 buf1
[8 + i
] = buf2
[4 + i
];
4293 retval
= ms_write_bytes(chip
, PRO_WRITE_SHORT_DATA
, 32, WAIT_INT
,
4295 if (retval
!= STATUS_SUCCESS
) {
4296 set_sense_type(chip
, lun
, SENSE_TYPE_MG_KEY_FAIL_NOT_ESTAB
);
4300 if (check_ms_err(chip
)) {
4301 set_sense_type(chip
, lun
, SENSE_TYPE_MG_KEY_FAIL_NOT_ESTAB
);
4302 rtsx_clear_ms_error(chip
);
4307 return STATUS_SUCCESS
;
4310 int mg_get_local_EKB(struct scsi_cmnd
*srb
, struct rtsx_chip
*chip
)
4312 int retval
= STATUS_FAIL
;
4314 unsigned int lun
= SCSI_LUN(srb
);
4317 ms_cleanup_work(chip
);
4319 retval
= ms_switch_clock(chip
);
4320 if (retval
!= STATUS_SUCCESS
) {
4325 buf
= kmalloc(1540, GFP_KERNEL
);
4328 return STATUS_ERROR
;
4336 retval
= mg_send_ex_cmd(chip
, MG_GET_LEKB
, 0);
4337 if (retval
!= STATUS_SUCCESS
) {
4338 set_sense_type(chip
, lun
, SENSE_TYPE_MG_KEY_FAIL_NOT_AUTHEN
);
4343 retval
= ms_transfer_data(chip
, MS_TM_AUTO_READ
, PRO_READ_LONG_DATA
,
4344 3, WAIT_INT
, 0, 0, buf
+ 4, 1536);
4345 if (retval
!= STATUS_SUCCESS
) {
4346 set_sense_type(chip
, lun
, SENSE_TYPE_MG_KEY_FAIL_NOT_AUTHEN
);
4347 rtsx_clear_ms_error(chip
);
4351 if (check_ms_err(chip
)) {
4352 set_sense_type(chip
, lun
, SENSE_TYPE_MG_KEY_FAIL_NOT_AUTHEN
);
4353 rtsx_clear_ms_error(chip
);
4355 retval
= STATUS_FAIL
;
4359 bufflen
= min_t(int, 1052, scsi_bufflen(srb
));
4360 rtsx_stor_set_xfer_buf(buf
, bufflen
, srb
);
4367 int mg_chg(struct scsi_cmnd
*srb
, struct rtsx_chip
*chip
)
4369 struct ms_info
*ms_card
= &chip
->ms_card
;
4373 unsigned int lun
= SCSI_LUN(srb
);
4376 ms_cleanup_work(chip
);
4378 retval
= ms_switch_clock(chip
);
4379 if (retval
!= STATUS_SUCCESS
) {
4384 retval
= mg_send_ex_cmd(chip
, MG_GET_ID
, 0);
4385 if (retval
!= STATUS_SUCCESS
) {
4386 set_sense_type(chip
, lun
, SENSE_TYPE_MG_INCOMPATIBLE_MEDIUM
);
4391 retval
= ms_read_bytes(chip
, PRO_READ_SHORT_DATA
, 32, WAIT_INT
,
4393 if (retval
!= STATUS_SUCCESS
) {
4394 set_sense_type(chip
, lun
, SENSE_TYPE_MG_INCOMPATIBLE_MEDIUM
);
4398 if (check_ms_err(chip
)) {
4399 set_sense_type(chip
, lun
, SENSE_TYPE_MG_INCOMPATIBLE_MEDIUM
);
4400 rtsx_clear_ms_error(chip
);
4405 memcpy(ms_card
->magic_gate_id
, buf
, 16);
4407 #ifdef READ_BYTES_WAIT_INT
4408 retval
= ms_poll_int(chip
);
4409 if (retval
!= STATUS_SUCCESS
) {
4410 set_sense_type(chip
, lun
, SENSE_TYPE_MG_INCOMPATIBLE_MEDIUM
);
4416 retval
= mg_send_ex_cmd(chip
, MG_SET_RD
, 0);
4417 if (retval
!= STATUS_SUCCESS
) {
4418 set_sense_type(chip
, lun
, SENSE_TYPE_MG_INCOMPATIBLE_MEDIUM
);
4423 bufflen
= min_t(int, 12, scsi_bufflen(srb
));
4424 rtsx_stor_get_xfer_buf(buf
, bufflen
, srb
);
4426 for (i
= 0; i
< 8; i
++)
4427 buf
[i
] = buf
[4 + i
];
4429 for (i
= 0; i
< 24; i
++)
4432 retval
= ms_write_bytes(chip
, PRO_WRITE_SHORT_DATA
,
4433 32, WAIT_INT
, buf
, 32);
4434 if (retval
!= STATUS_SUCCESS
) {
4435 set_sense_type(chip
, lun
, SENSE_TYPE_MG_INCOMPATIBLE_MEDIUM
);
4439 if (check_ms_err(chip
)) {
4440 set_sense_type(chip
, lun
, SENSE_TYPE_MG_INCOMPATIBLE_MEDIUM
);
4441 rtsx_clear_ms_error(chip
);
4446 ms_card
->mg_auth
= 0;
4448 return STATUS_SUCCESS
;
4451 int mg_get_rsp_chg(struct scsi_cmnd
*srb
, struct rtsx_chip
*chip
)
4453 struct ms_info
*ms_card
= &chip
->ms_card
;
4456 unsigned int lun
= SCSI_LUN(srb
);
4457 u8 buf1
[32], buf2
[36];
4459 ms_cleanup_work(chip
);
4461 retval
= ms_switch_clock(chip
);
4462 if (retval
!= STATUS_SUCCESS
) {
4467 retval
= mg_send_ex_cmd(chip
, MG_MAKE_RMS
, 0);
4468 if (retval
!= STATUS_SUCCESS
) {
4469 set_sense_type(chip
, lun
, SENSE_TYPE_MG_KEY_FAIL_NOT_AUTHEN
);
4474 retval
= ms_read_bytes(chip
, PRO_READ_SHORT_DATA
, 32, WAIT_INT
,
4476 if (retval
!= STATUS_SUCCESS
) {
4477 set_sense_type(chip
, lun
, SENSE_TYPE_MG_KEY_FAIL_NOT_AUTHEN
);
4481 if (check_ms_err(chip
)) {
4482 set_sense_type(chip
, lun
, SENSE_TYPE_MG_KEY_FAIL_NOT_AUTHEN
);
4483 rtsx_clear_ms_error(chip
);
4493 memcpy(buf2
+ 4, ms_card
->magic_gate_id
, 16);
4494 memcpy(buf2
+ 20, buf1
, 16);
4496 bufflen
= min_t(int, 36, scsi_bufflen(srb
));
4497 rtsx_stor_set_xfer_buf(buf2
, bufflen
, srb
);
4499 #ifdef READ_BYTES_WAIT_INT
4500 retval
= ms_poll_int(chip
);
4501 if (retval
!= STATUS_SUCCESS
) {
4502 set_sense_type(chip
, lun
, SENSE_TYPE_MG_KEY_FAIL_NOT_AUTHEN
);
4508 return STATUS_SUCCESS
;
4511 int mg_rsp(struct scsi_cmnd
*srb
, struct rtsx_chip
*chip
)
4513 struct ms_info
*ms_card
= &chip
->ms_card
;
4517 unsigned int lun
= SCSI_LUN(srb
);
4520 ms_cleanup_work(chip
);
4522 retval
= ms_switch_clock(chip
);
4523 if (retval
!= STATUS_SUCCESS
) {
4528 retval
= mg_send_ex_cmd(chip
, MG_MAKE_KSE
, 0);
4529 if (retval
!= STATUS_SUCCESS
) {
4530 set_sense_type(chip
, lun
, SENSE_TYPE_MG_KEY_FAIL_NOT_AUTHEN
);
4535 bufflen
= min_t(int, 12, scsi_bufflen(srb
));
4536 rtsx_stor_get_xfer_buf(buf
, bufflen
, srb
);
4538 for (i
= 0; i
< 8; i
++)
4539 buf
[i
] = buf
[4 + i
];
4541 for (i
= 0; i
< 24; i
++)
4544 retval
= ms_write_bytes(chip
, PRO_WRITE_SHORT_DATA
, 32, WAIT_INT
,
4546 if (retval
!= STATUS_SUCCESS
) {
4547 set_sense_type(chip
, lun
, SENSE_TYPE_MG_KEY_FAIL_NOT_AUTHEN
);
4551 if (check_ms_err(chip
)) {
4552 set_sense_type(chip
, lun
, SENSE_TYPE_MG_KEY_FAIL_NOT_AUTHEN
);
4553 rtsx_clear_ms_error(chip
);
4558 ms_card
->mg_auth
= 1;
4560 return STATUS_SUCCESS
;
4563 int mg_get_ICV(struct scsi_cmnd
*srb
, struct rtsx_chip
*chip
)
4565 struct ms_info
*ms_card
= &chip
->ms_card
;
4568 unsigned int lun
= SCSI_LUN(srb
);
4571 ms_cleanup_work(chip
);
4573 retval
= ms_switch_clock(chip
);
4574 if (retval
!= STATUS_SUCCESS
) {
4579 buf
= kmalloc(1028, GFP_KERNEL
);
4582 return STATUS_ERROR
;
4590 retval
= mg_send_ex_cmd(chip
, MG_GET_IBD
, ms_card
->mg_entry_num
);
4591 if (retval
!= STATUS_SUCCESS
) {
4592 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR
);
4597 retval
= ms_transfer_data(chip
, MS_TM_AUTO_READ
, PRO_READ_LONG_DATA
,
4598 2, WAIT_INT
, 0, 0, buf
+ 4, 1024);
4599 if (retval
!= STATUS_SUCCESS
) {
4600 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR
);
4601 rtsx_clear_ms_error(chip
);
4605 if (check_ms_err(chip
)) {
4606 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR
);
4607 rtsx_clear_ms_error(chip
);
4609 retval
= STATUS_FAIL
;
4613 bufflen
= min_t(int, 1028, scsi_bufflen(srb
));
4614 rtsx_stor_set_xfer_buf(buf
, bufflen
, srb
);
4621 int mg_set_ICV(struct scsi_cmnd
*srb
, struct rtsx_chip
*chip
)
4623 struct ms_info
*ms_card
= &chip
->ms_card
;
4626 #ifdef MG_SET_ICV_SLOW
4629 unsigned int lun
= SCSI_LUN(srb
);
4632 ms_cleanup_work(chip
);
4634 retval
= ms_switch_clock(chip
);
4635 if (retval
!= STATUS_SUCCESS
) {
4640 buf
= kmalloc(1028, GFP_KERNEL
);
4643 return STATUS_ERROR
;
4646 bufflen
= min_t(int, 1028, scsi_bufflen(srb
));
4647 rtsx_stor_get_xfer_buf(buf
, bufflen
, srb
);
4649 retval
= mg_send_ex_cmd(chip
, MG_SET_IBD
, ms_card
->mg_entry_num
);
4650 if (retval
!= STATUS_SUCCESS
) {
4651 if (ms_card
->mg_auth
== 0) {
4652 if ((buf
[5] & 0xC0) != 0)
4655 SENSE_TYPE_MG_KEY_FAIL_NOT_ESTAB
);
4657 set_sense_type(chip
, lun
,
4658 SENSE_TYPE_MG_WRITE_ERR
);
4660 set_sense_type(chip
, lun
, SENSE_TYPE_MG_WRITE_ERR
);
4666 #ifdef MG_SET_ICV_SLOW
4667 for (i
= 0; i
< 2; i
++) {
4670 rtsx_init_cmd(chip
);
4672 rtsx_add_cmd(chip
, WRITE_REG_CMD
, MS_TPC
,
4673 0xFF, PRO_WRITE_LONG_DATA
);
4674 rtsx_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANS_CFG
, 0xFF, WAIT_INT
);
4675 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
,
4678 trans_dma_enable(DMA_TO_DEVICE
, chip
, 512, DMA_512
);
4680 rtsx_add_cmd(chip
, WRITE_REG_CMD
, MS_TRANSFER
, 0xFF,
4681 MS_TRANSFER_START
| MS_TM_NORMAL_WRITE
);
4682 rtsx_add_cmd(chip
, CHECK_REG_CMD
, MS_TRANSFER
,
4683 MS_TRANSFER_END
, MS_TRANSFER_END
);
4685 rtsx_send_cmd_no_wait(chip
);
4687 retval
= rtsx_transfer_data(chip
, MS_CARD
, buf
+ 4 + i
* 512,
4688 512, 0, DMA_TO_DEVICE
, 3000);
4689 if ((retval
< 0) || check_ms_err(chip
)) {
4690 rtsx_clear_ms_error(chip
);
4691 if (ms_card
->mg_auth
== 0) {
4692 if ((buf
[5] & 0xC0) != 0)
4695 SENSE_TYPE_MG_KEY_FAIL_NOT_ESTAB
);
4697 set_sense_type(chip
, lun
,
4698 SENSE_TYPE_MG_WRITE_ERR
);
4700 set_sense_type(chip
, lun
,
4701 SENSE_TYPE_MG_WRITE_ERR
);
4703 retval
= STATUS_FAIL
;
4709 retval
= ms_transfer_data(chip
, MS_TM_AUTO_WRITE
, PRO_WRITE_LONG_DATA
,
4710 2, WAIT_INT
, 0, 0, buf
+ 4, 1024);
4711 if ((retval
!= STATUS_SUCCESS
) || check_ms_err(chip
)) {
4712 rtsx_clear_ms_error(chip
);
4713 if (ms_card
->mg_auth
== 0) {
4714 if ((buf
[5] & 0xC0) != 0)
4717 SENSE_TYPE_MG_KEY_FAIL_NOT_ESTAB
);
4719 set_sense_type(chip
, lun
,
4720 SENSE_TYPE_MG_WRITE_ERR
);
4722 set_sense_type(chip
, lun
, SENSE_TYPE_MG_WRITE_ERR
);
4734 #endif /* SUPPORT_MAGIC_GATE */
4736 void ms_cleanup_work(struct rtsx_chip
*chip
)
4738 struct ms_info
*ms_card
= &chip
->ms_card
;
4740 if (CHK_MSPRO(ms_card
)) {
4741 if (ms_card
->seq_mode
) {
4742 dev_dbg(rtsx_dev(chip
), "MS Pro: stop transmission\n");
4743 mspro_stop_seq_mode(chip
);
4744 ms_card
->cleanup_counter
= 0;
4746 if (CHK_MSHG(ms_card
)) {
4747 rtsx_write_register(chip
, MS_CFG
,
4748 MS_2K_SECTOR_MODE
, 0x00);
4751 #ifdef MS_DELAY_WRITE
4752 else if ((!CHK_MSPRO(ms_card
)) &&
4753 ms_card
->delay_write
.delay_write_flag
) {
4754 dev_dbg(rtsx_dev(chip
), "MS: delay write\n");
4755 ms_delay_write(chip
);
4756 ms_card
->cleanup_counter
= 0;
4761 int ms_power_off_card3v3(struct rtsx_chip
*chip
)
4765 retval
= disable_card_clock(chip
, MS_CARD
);
4766 if (retval
!= STATUS_SUCCESS
) {
4771 if (chip
->asic_code
) {
4772 retval
= ms_pull_ctl_disable(chip
);
4773 if (retval
!= STATUS_SUCCESS
) {
4778 retval
= rtsx_write_register(chip
, FPGA_PULL_CTL
,
4779 FPGA_MS_PULL_CTL_BIT
| 0x20,
4780 FPGA_MS_PULL_CTL_BIT
);
4786 retval
= rtsx_write_register(chip
, CARD_OE
, MS_OUTPUT_EN
, 0);
4791 if (!chip
->ft2_fast_mode
) {
4792 retval
= card_power_off(chip
, MS_CARD
);
4793 if (retval
!= STATUS_SUCCESS
) {
4799 return STATUS_SUCCESS
;
4802 int release_ms_card(struct rtsx_chip
*chip
)
4804 struct ms_info
*ms_card
= &chip
->ms_card
;
4807 #ifdef MS_DELAY_WRITE
4808 ms_card
->delay_write
.delay_write_flag
= 0;
4810 ms_card
->pro_under_formatting
= 0;
4812 chip
->card_ready
&= ~MS_CARD
;
4813 chip
->card_fail
&= ~MS_CARD
;
4814 chip
->card_wp
&= ~MS_CARD
;
4816 ms_free_l2p_tbl(chip
);
4818 memset(ms_card
->raw_sys_info
, 0, 96);
4819 #ifdef SUPPORT_PCGL_1P18
4820 memset(ms_card
->raw_model_name
, 0, 48);
4823 retval
= ms_power_off_card3v3(chip
);
4824 if (retval
!= STATUS_SUCCESS
) {
4829 return STATUS_SUCCESS
;