1 /* Driver for Realtek RTS51xx USB card reader 2 * Header file 3 * 4 * Copyright(c) 2009 Realtek Semiconductor Corp. All rights reserved. 5 * 6 * This program is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License as published by the 8 * Free Software Foundation; either version 2, or (at your option) any 9 * later version. 10 * 11 * This program is distributed in the hope that it will be useful, but 12 * WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License along 17 * with this program; if not, see <http://www.gnu.org/licenses/>. 18 * 19 * Author: 20 * wwang (wei_wang@realsil.com.cn) 21 * No. 450, Shenhu Road, Suzhou Industry Park, Suzhou, China 22 * Maintainer: 23 * Edwin Rong (edwin_rong@realsil.com.cn) 24 * No. 450, Shenhu Road, Suzhou Industry Park, Suzhou, China 25 */ 26 27 #ifndef __RTS51X_SD_H 28 #define __RTS51X_SD_H 29 30 #include "rts51x_chip.h" 31 32 #define SD_MAX_RETRY_COUNT 3 33 34 #define SUPPORT_VOLTAGE 0x003C0000 35 36 #define SD_RESET_FAIL 0x01 37 #define MMC_RESET_FAIL 0x02 38 39 /* Error Code */ 40 #define SD_NO_ERROR 0x0 41 #define SD_CRC_ERR 0x80 42 #define SD_TO_ERR 0x40 43 #define SD_NO_CARD 0x20 44 #define SD_BUSY 0x10 45 #define SD_STS_ERR 0x08 46 #define SD_RSP_TIMEOUT 0x04 47 48 /* MMC/SD Command Index */ 49 /* Basic command (class 0) */ 50 #define GO_IDLE_STATE 0 51 #define SEND_OP_COND 1 /* reserved for SD */ 52 #define ALL_SEND_CID 2 53 #define SET_RELATIVE_ADDR 3 54 #define SEND_RELATIVE_ADDR 3 55 #define SET_DSR 4 56 #define IO_SEND_OP_COND 5 57 #define SWITCH 6 58 #define SELECT_CARD 7 59 #define DESELECT_CARD 7 60 /* CMD8 is "SEND_EXT_CSD" for MMC4.x Spec 61 * while is "SEND_IF_COND" for SD 2.0 */ 62 #define SEND_EXT_CSD 8 63 #define SEND_IF_COND 8 64 /* end */ 65 #define SEND_CSD 9 66 #define SEND_CID 10 67 #define VOLTAGE_SWITCH 11 68 #define READ_DAT_UTIL_STOP 11 /* reserved for SD */ 69 #define STOP_TRANSMISSION 12 70 #define SEND_STATUS 13 71 #define GO_INACTIVE_STATE 15 72 73 /* Block oriented read commands (class 2) */ 74 #define SET_BLOCKLEN 16 75 #define READ_SINGLE_BLOCK 17 76 #define READ_MULTIPLE_BLOCK 18 77 #define SEND_TUNING_PATTERN 19 78 79 /* Bus Width Test */ 80 #define BUSTEST_R 14 81 #define BUSTEST_W 19 82 /* end */ 83 84 /* Block oriented write commands (class 4) */ 85 #define WRITE_BLOCK 24 86 #define WRITE_MULTIPLE_BLOCK 25 87 #define PROGRAM_CSD 27 88 89 /* Erase commands */ 90 #define ERASE_WR_BLK_START 32 91 #define ERASE_WR_BLK_END 33 92 #define ERASE_CMD 38 93 94 /* Block Oriented Write Protection Commands */ 95 #define LOCK_UNLOCK 42 96 97 #define IO_RW_DIRECT 52 98 99 /* Application specific commands (class 8) */ 100 #define APP_CMD 55 101 #define GEN_CMD 56 102 103 /* SD Application command Index */ 104 #define SET_BUS_WIDTH 6 105 #define SD_STATUS 13 106 #define SEND_NUM_WR_BLOCKS 22 107 #define SET_WR_BLK_ERASE_COUNT 23 108 #define SD_APP_OP_COND 41 109 #define SET_CLR_CARD_DETECT 42 110 #define SEND_SCR 51 111 112 /* SD TIMEOUT function return error */ 113 #define SD_READ_COMPLETE 0x00 114 #define SD_READ_TO 0x01 115 #define SD_READ_ADVENCE 0x02 116 117 /* SD v1.1 CMD6 SWITCH function */ 118 #define SD_CHECK_MODE 0x00 119 #define SD_SWITCH_MODE 0x80 120 #define SD_FUNC_GROUP_1 0x01 121 #define SD_FUNC_GROUP_2 0x02 122 #define SD_FUNC_GROUP_3 0x03 123 #define SD_FUNC_GROUP_4 0x04 124 #define SD_CHECK_SPEC_V1_1 0xFF 125 126 /* SD Command Argument */ 127 #define NO_ARGUMENT 0x00 128 #define CHECK_PATTERN 0x000000AA 129 #define VOLTAGE_SUPPLY_RANGE 0x00000100 /* 2.7~3.6V */ 130 #define SUPPORT_HIGH_AND_EXTENDED_CAPACITY 0x40000000 131 #define SUPPORT_MAX_POWER_PERMANCE 0x10000000 132 #define SUPPORT_1V8 0x01000000 133 134 /* Switch Command Error Code */ 135 #define SWTICH_NO_ERR 0x00 136 #define CARD_NOT_EXIST 0x01 137 #define SPEC_NOT_SUPPORT 0x02 138 #define CHECK_MODE_ERR 0x03 139 #define CHECK_NOT_READY 0x04 140 #define SWITCH_CRC_ERR 0x05 141 #define SWITCH_MODE_ERR 0x06 142 #define SWITCH_PASS 0x07 143 144 #ifdef SUPPORT_SD_LOCK 145 /* CMD42 Parameter */ 146 #define SD_ERASE 0x08 147 #define SD_LOCK 0x04 148 #define SD_UNLOCK 0x00 149 #define SD_CLR_PWD 0x02 150 #define SD_SET_PWD 0x01 151 152 #define SD_PWD_LEN 0x10 153 154 /* SD lock unlock Status */ 155 #define SD_LOCKED 0x80 /* Global lock status */ 156 #define SD_LOCK_1BIT_MODE 0x40 /**/ 157 #define SD_PWD_EXIST 0x20 158 #define SD_UNLOCK_POW_ON 0x01 /**/ 159 #define SD_SDR_RST 0x02 /* Reset SD30 card with current DDR mode to SDR mode. */ 160 /* g_bySDEraseStatus */ 161 #define SD_NOT_ERASE 0x00 162 #define SD_UNDER_ERASING 0x01 163 #define SD_COMPLETE_ERASE 0x02 164 /* SD_RW FAIL status */ 165 #define SD_RW_FORBIDDEN 0x0F /* read/write is forbidden (SD card) */ 166 #endif 167 /* Function Group Definition */ 168 /* Function Group 1 */ 169 #define HS_SUPPORT 0x01 170 #define SDR50_SUPPORT 0x02 171 #define SDR104_SUPPORT 0x03 172 #define DDR50_SUPPORT 0x04 173 #define HS_SUPPORT_MASK 0x02 174 #define SDR50_SUPPORT_MASK 0x04 175 #define SDR104_SUPPORT_MASK 0x08 176 #define DDR50_SUPPORT_MASK 0x10 177 #define HS_QUERY_SWITCH_OK 0x01 178 #define SDR50_QUERY_SWITCH_OK 0x02 179 #define SDR104_QUERY_SWITCH_OK 0x03 180 #define DDR50_QUERY_SWITCH_OK 0x04 181 #define HS_SWITCH_BUSY 0x02 182 #define SDR50_SWITCH_BUSY 0x04 183 #define SDR104_SWITCH_BUSY 0x08 184 #define DDR50_SWITCH_BUSY 0x10 185 #define FUNCTION_GROUP1_SUPPORT_OFFSET 0x0D 186 #define FUNCTION_GROUP1_QUERY_SWITCH_OFFSET 0x10 187 #define FUNCTION_GROUP1_CHECK_BUSY_OFFSET 0x1D 188 /* Function Group 3 */ 189 #define DRIVING_TYPE_A 0x01 190 #define DRIVING_TYPE_B 0x00 191 #define DRIVING_TYPE_C 0x02 192 #define DRIVING_TYPE_D 0x03 193 #define DRIVING_TYPE_A_MASK 0x02 194 #define DRIVING_TYPE_B_MASK 0x01 195 #define DRIVING_TYPE_C_MASK 0x04 196 #define DRIVING_TYPE_D_MASK 0x08 197 #define TYPE_A_QUERY_SWITCH_OK 0x01 198 #define TYPE_B_QUERY_SWITCH_OK 0x00 199 #define TYPE_C_QUERY_SWITCH_OK 0x02 200 #define TYPE_D_QUERY_SWITCH_OK 0x03 201 #define TYPE_A_SWITCH_BUSY 0x02 202 #define TYPE_B_SWITCH_BUSY 0x01 203 #define TYPE_C_SWITCH_BUSY 0x04 204 #define TYPE_D_SWITCH_BUSY 0x08 205 #define FUNCTION_GROUP3_SUPPORT_OFFSET 0x09 206 #define FUNCTION_GROUP3_QUERY_SWITCH_OFFSET 0x0F 207 #define FUNCTION_GROUP3_CHECK_BUSY_OFFSET 0x19 208 /* Function Group 4 */ 209 #define CURRENT_LIMIT_200 0x00 210 #define CURRENT_LIMIT_400 0x01 211 #define CURRENT_LIMIT_600 0x02 212 #define CURRENT_LIMIT_800 0x03 213 #define CURRENT_LIMIT_200_MASK 0x01 214 #define CURRENT_LIMIT_400_MASK 0x02 215 #define CURRENT_LIMIT_600_MASK 0x04 216 #define CURRENT_LIMIT_800_MASK 0x08 217 #define CURRENT_LIMIT_200_QUERY_SWITCH_OK 0x00 218 #define CURRENT_LIMIT_400_QUERY_SWITCH_OK 0x01 219 #define CURRENT_LIMIT_600_QUERY_SWITCH_OK 0x02 220 #define CURRENT_LIMIT_800_QUERY_SWITCH_OK 0x03 221 #define CURRENT_LIMIT_200_SWITCH_BUSY 0x01 222 #define CURRENT_LIMIT_400_SWITCH_BUSY 0x02 223 #define CURRENT_LIMIT_600_SWITCH_BUSY 0x04 224 #define CURRENT_LIMIT_800_SWITCH_BUSY 0x08 225 #define FUNCTION_GROUP4_SUPPORT_OFFSET 0x07 226 #define FUNCTION_GROUP4_QUERY_SWITCH_OFFSET 0x0F 227 #define FUNCTION_GROUP4_CHECK_BUSY_OFFSET 0x17 228 /* Switch Function Status Offset */ 229 #define DATA_STRUCTURE_VER_OFFSET 0x11 /* The high offset */ 230 #define MAX_PHASE 15 231 /* #define TOTAL_READ_PHASE 0x20 */ 232 /* #define TOTAL_WRITE_PHASE 0x20 */ 233 /* MMC v4.0 */ 234 /* #define MMC_52MHZ_SPEED 0x0001 */ 235 /* #define MMC_26MHZ_SPEED 0x0002 */ 236 #define MMC_8BIT_BUS 0x0010 237 #define MMC_4BIT_BUS 0x0020 238 /* #define MMC_SECTOR_MODE 0x0100 */ 239 #define MMC_SWITCH_ERR 0x80 240 /* Tuning direction RX or TX */ 241 #define TUNE_TX 0x00 242 #define TUNE_RX 0x01 243 /* For Change_DCM_FreqMode Function */ 244 #define CHANGE_TX 0x00 245 #define CHANGE_RX 0x01 246 #define DCM_HIGH_FREQUENCY_MODE 0x00 247 #define DCM_LOW_FREQUENCY_MODE 0x01 248 #define DCM_HIGH_FREQUENCY_MODE_SET 0x0C 249 #define DCM_Low_FREQUENCY_MODE_SET 0x00 250 /* For Change_FPGA_SSCClock Function */ 251 #define MULTIPLY_BY_1 0x00 252 #define MULTIPLY_BY_2 0x01 253 #define MULTIPLY_BY_3 0x02 254 #define MULTIPLY_BY_4 0x03 255 #define MULTIPLY_BY_5 0x04 256 #define MULTIPLY_BY_6 0x05 257 #define MULTIPLY_BY_7 0x06 258 #define MULTIPLY_BY_8 0x07 259 #define MULTIPLY_BY_9 0x08 260 #define MULTIPLY_BY_10 0x09 261 #define DIVIDE_BY_2 0x01 262 #define DIVIDE_BY_3 0x02 263 #define DIVIDE_BY_4 0x03 264 #define DIVIDE_BY_5 0x04 265 #define DIVIDE_BY_6 0x05 266 #define DIVIDE_BY_7 0x06 267 #define DIVIDE_BY_8 0x07 268 #define DIVIDE_BY_9 0x08 269 #define DIVIDE_BY_10 0x09 270 #define CHECK_SD_TRANS_FAIL(chip, retval) \ 271 (((retval) != STATUS_SUCCESS) || \ 272 (chip->rsp_buf[0] & SD_TRANSFER_ERR)) 273 /* SD Tuning Data Structure */ 274 /* Record continuous timing phase path */ 275 struct timing_phase_path { 276 int start; 277 int end; 278 int mid; 279 int len; 280 }; 281 282 int sd_select_card(struct rts51x_chip *chip, int select); 283 int reset_sd_card(struct rts51x_chip *chip); 284 int sd_switch_clock(struct rts51x_chip *chip); 285 void sd_stop_seq_mode(struct rts51x_chip *chip); 286 int sd_rw(struct scsi_cmnd *srb, struct rts51x_chip *chip, u32 start_sector, 287 u16 sector_cnt); 288 void sd_cleanup_work(struct rts51x_chip *chip); 289 int sd_power_off_card3v3(struct rts51x_chip *chip); 290 int release_sd_card(struct rts51x_chip *chip); 291 292 #ifdef SUPPORT_SD_LOCK 293 int sd_update_lock_status(struct rts51x_chip *chip); 294 #endif 295 296 #ifdef SUPPORT_CPRM 297 extern int reset_sd(struct rts51x_chip *chip); 298 extern int sd_check_data0_status(struct rts51x_chip *chip); 299 extern int sd_read_data(struct rts51x_chip *chip, u8 trans_mode, u8 *cmd, 300 int cmd_len, u16 byte_cnt, u16 blk_cnt, u8 bus_width, 301 u8 *buf, int buf_len, int timeout); 302 #endif 303 304 #endif /* __RTS51X_SD_H */ 305