| 1 | /* |
| 2 | * (C) Copyright 2003 |
| 3 | * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | #include <config.h> |
| 25 | #include <common.h> |
| 26 | #include <part.h> |
| 27 | #include <mmc.h> |
| 28 | |
| 29 | #include <asm/jz4740.h> |
| 30 | #include "jz_mmc.h" |
| 31 | |
| 32 | static int sd2_0 = 0; |
| 33 | static int mmc_ready = 0; |
| 34 | static int use_4bit; /* Use 4-bit data bus */ |
| 35 | /* |
| 36 | * MMC Events |
| 37 | */ |
| 38 | #define MMC_EVENT_NONE 0x00 /* No events */ |
| 39 | #define MMC_EVENT_RX_DATA_DONE 0x01 /* Rx data done */ |
| 40 | #define MMC_EVENT_TX_DATA_DONE 0x02 /* Tx data done */ |
| 41 | #define MMC_EVENT_PROG_DONE 0x04 /* Programming is done */ |
| 42 | |
| 43 | |
| 44 | #define MMC_IRQ_MASK() \ |
| 45 | do { \ |
| 46 | REG_MSC_IMASK = 0xffff; \ |
| 47 | REG_MSC_IREG = 0xffff; \ |
| 48 | } while (0) |
| 49 | |
| 50 | /* |
| 51 | * GPIO definition |
| 52 | */ |
| 53 | #if defined(CONFIG_SAKC) |
| 54 | |
| 55 | #define __msc_init_io() \ |
| 56 | do { \ |
| 57 | __gpio_as_input(GPIO_SD_CD_N); \ |
| 58 | } while (0) |
| 59 | |
| 60 | #else |
| 61 | #define __msc_init_io() \ |
| 62 | do { \ |
| 63 | __gpio_as_output(GPIO_SD_VCC_EN_N); \ |
| 64 | __gpio_as_input(GPIO_SD_CD_N); \ |
| 65 | } while (0) |
| 66 | |
| 67 | #define __msc_enable_power() \ |
| 68 | do { \ |
| 69 | __gpio_clear_pin(GPIO_SD_VCC_EN_N); \ |
| 70 | } while (0) |
| 71 | |
| 72 | #define __msc_disable_power() \ |
| 73 | do { \ |
| 74 | __gpio_set_pin(GPIO_SD_VCC_EN_N); \ |
| 75 | } while (0) |
| 76 | |
| 77 | #endif /* CONFIG_SAKE */ |
| 78 | |
| 79 | #define __msc_card_detected() \ |
| 80 | ({ \ |
| 81 | int detected = 1; \ |
| 82 | __gpio_as_input(GPIO_SD_CD_N); \ |
| 83 | __gpio_disable_pull(GPIO_SD_CD_N); \ |
| 84 | if (!__gpio_get_pin(GPIO_SD_CD_N)) \ |
| 85 | detected = 0; \ |
| 86 | detected; \ |
| 87 | }) |
| 88 | |
| 89 | /* |
| 90 | * Local functions |
| 91 | */ |
| 92 | |
| 93 | extern int |
| 94 | fat_register_device(block_dev_desc_t *dev_desc, int part_no); |
| 95 | |
| 96 | static block_dev_desc_t mmc_dev; |
| 97 | |
| 98 | block_dev_desc_t * mmc_get_dev(int dev) |
| 99 | { |
| 100 | return ((block_dev_desc_t *)&mmc_dev); |
| 101 | } |
| 102 | |
| 103 | /* Stop the MMC clock and wait while it happens */ |
| 104 | static inline int jz_mmc_stop_clock(void) |
| 105 | { |
| 106 | int timeout = 1000; |
| 107 | |
| 108 | REG_MSC_STRPCL = MSC_STRPCL_CLOCK_CONTROL_STOP; |
| 109 | |
| 110 | while (timeout && (REG_MSC_STAT & MSC_STAT_CLK_EN)) { |
| 111 | timeout--; |
| 112 | if (timeout == 0) |
| 113 | return MMC_ERROR_TIMEOUT; |
| 114 | udelay(1); |
| 115 | } |
| 116 | return MMC_NO_ERROR; |
| 117 | } |
| 118 | |
| 119 | /* Start the MMC clock and operation */ |
| 120 | static inline int jz_mmc_start_clock(void) |
| 121 | { |
| 122 | REG_MSC_STRPCL = MSC_STRPCL_CLOCK_CONTROL_START | MSC_STRPCL_START_OP; |
| 123 | return MMC_NO_ERROR; |
| 124 | } |
| 125 | |
| 126 | static inline u32 jz_mmc_calc_clkrt(int is_sd, u32 rate) |
| 127 | { |
| 128 | u32 clkrt = 0; |
| 129 | u32 clk_src = is_sd ? 24000000 : 16000000; |
| 130 | |
| 131 | while (rate < clk_src) { |
| 132 | clkrt ++; |
| 133 | clk_src >>= 1; |
| 134 | } |
| 135 | |
| 136 | return clkrt; |
| 137 | } |
| 138 | |
| 139 | /* Set the MMC clock frequency */ |
| 140 | void jz_mmc_set_clock(int sd, u32 rate) |
| 141 | { |
| 142 | jz_mmc_stop_clock(); |
| 143 | |
| 144 | /* Select clock source of MSC */ |
| 145 | __cpm_select_msc_clk(sd); |
| 146 | |
| 147 | /* Set clock dividor of MSC */ |
| 148 | REG_MSC_CLKRT = jz_mmc_calc_clkrt(sd, rate); |
| 149 | } |
| 150 | |
| 151 | static int jz_mmc_check_status(struct mmc_request *request) |
| 152 | { |
| 153 | u32 status = REG_MSC_STAT; |
| 154 | |
| 155 | /* Checking for response or data timeout */ |
| 156 | if (status & (MSC_STAT_TIME_OUT_RES | MSC_STAT_TIME_OUT_READ)) { |
| 157 | printf("MMC/SD timeout, MMC_STAT 0x%x CMD %d\n", status, request->cmd); |
| 158 | return MMC_ERROR_TIMEOUT; |
| 159 | } |
| 160 | |
| 161 | /* Checking for CRC error */ |
| 162 | if (status & (MSC_STAT_CRC_READ_ERROR | MSC_STAT_CRC_WRITE_ERROR | MSC_STAT_CRC_RES_ERR)) { |
| 163 | printf("MMC/CD CRC error, MMC_STAT 0x%x\n", status); |
| 164 | return MMC_ERROR_CRC; |
| 165 | } |
| 166 | |
| 167 | return MMC_NO_ERROR; |
| 168 | } |
| 169 | |
| 170 | /* Obtain response to the command and store it to response buffer */ |
| 171 | static void jz_mmc_get_response(struct mmc_request *request) |
| 172 | { |
| 173 | int i; |
| 174 | u8 *buf; |
| 175 | u32 data; |
| 176 | |
| 177 | debug("fetch response for request %d, cmd %d\n", |
| 178 | request->rtype, request->cmd); |
| 179 | |
| 180 | buf = request->response; |
| 181 | request->result = MMC_NO_ERROR; |
| 182 | |
| 183 | switch (request->rtype) { |
| 184 | case RESPONSE_R1: case RESPONSE_R1B: case RESPONSE_R6: |
| 185 | case RESPONSE_R3: case RESPONSE_R4: case RESPONSE_R5: |
| 186 | { |
| 187 | data = REG_MSC_RES; |
| 188 | buf[0] = (data >> 8) & 0xff; |
| 189 | buf[1] = data & 0xff; |
| 190 | data = REG_MSC_RES; |
| 191 | buf[2] = (data >> 8) & 0xff; |
| 192 | buf[3] = data & 0xff; |
| 193 | data = REG_MSC_RES; |
| 194 | buf[4] = data & 0xff; |
| 195 | |
| 196 | debug("request %d, response [%02x %02x %02x %02x %02x]\n", |
| 197 | request->rtype, buf[0], buf[1], buf[2], buf[3], buf[4]); |
| 198 | break; |
| 199 | } |
| 200 | case RESPONSE_R2_CID: case RESPONSE_R2_CSD: |
| 201 | { |
| 202 | for (i = 0; i < 16; i += 2) { |
| 203 | data = REG_MSC_RES; |
| 204 | buf[i] = (data >> 8) & 0xff; |
| 205 | buf[i+1] = data & 0xff; |
| 206 | } |
| 207 | debug("request %d, response [", request->rtype); |
| 208 | #if CONFIG_MMC_DEBUG_VERBOSE > 2 |
| 209 | if (g_mmc_debug >= 3) { |
| 210 | int n; |
| 211 | for (n = 0; n < 17; n++) |
| 212 | printk("%02x ", buf[n]); |
| 213 | printk("]\n"); |
| 214 | } |
| 215 | #endif |
| 216 | break; |
| 217 | } |
| 218 | case RESPONSE_NONE: |
| 219 | debug("No response\n"); |
| 220 | break; |
| 221 | |
| 222 | default: |
| 223 | debug("unhandled response type for request %d\n", request->rtype); |
| 224 | break; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | static int jz_mmc_receive_data(struct mmc_request *req) |
| 229 | { |
| 230 | u32 stat, timeout, data, cnt; |
| 231 | u8 *buf = req->buffer; |
| 232 | u32 wblocklen = (u32)(req->block_len + 3) >> 2; /* length in word */ |
| 233 | |
| 234 | timeout = 0x3ffffff; |
| 235 | |
| 236 | while (timeout) { |
| 237 | timeout--; |
| 238 | stat = REG_MSC_STAT; |
| 239 | |
| 240 | if (stat & MSC_STAT_TIME_OUT_READ) |
| 241 | return MMC_ERROR_TIMEOUT; |
| 242 | else if (stat & MSC_STAT_CRC_READ_ERROR) |
| 243 | return MMC_ERROR_CRC; |
| 244 | else if (!(stat & MSC_STAT_DATA_FIFO_EMPTY) |
| 245 | || (stat & MSC_STAT_DATA_FIFO_AFULL)) { |
| 246 | /* Ready to read data */ |
| 247 | break; |
| 248 | } |
| 249 | udelay(1); |
| 250 | } |
| 251 | if (!timeout) |
| 252 | return MMC_ERROR_TIMEOUT; |
| 253 | |
| 254 | /* Read data from RXFIFO. It could be FULL or PARTIAL FULL */ |
| 255 | cnt = wblocklen; |
| 256 | while (cnt) { |
| 257 | data = REG_MSC_RXFIFO; |
| 258 | { |
| 259 | *buf++ = (u8)(data >> 0); |
| 260 | *buf++ = (u8)(data >> 8); |
| 261 | *buf++ = (u8)(data >> 16); |
| 262 | *buf++ = (u8)(data >> 24); |
| 263 | } |
| 264 | cnt --; |
| 265 | while (cnt && (REG_MSC_STAT & MSC_STAT_DATA_FIFO_EMPTY)) |
| 266 | ; |
| 267 | } |
| 268 | return MMC_NO_ERROR; |
| 269 | } |
| 270 | |
| 271 | static int jz_mmc_transmit_data(struct mmc_request *req) |
| 272 | { |
| 273 | #if 0 |
| 274 | u32 nob = req->nob; |
| 275 | u32 wblocklen = (u32)(req->block_len + 3) >> 2; /* length in word */ |
| 276 | u8 *buf = req->buffer; |
| 277 | u32 *wbuf = (u32 *)buf; |
| 278 | u32 waligned = (((u32)buf & 0x3) == 0); /* word aligned ? */ |
| 279 | u32 stat, timeout, data, cnt; |
| 280 | |
| 281 | for (nob; nob >= 1; nob--) { |
| 282 | timeout = 0x3FFFFFF; |
| 283 | |
| 284 | while (timeout) { |
| 285 | timeout--; |
| 286 | stat = REG_MSC_STAT; |
| 287 | |
| 288 | if (stat & (MSC_STAT_CRC_WRITE_ERROR | MSC_STAT_CRC_WRITE_ERROR_NOSTS)) |
| 289 | return MMC_ERROR_CRC; |
| 290 | else if (!(stat & MSC_STAT_DATA_FIFO_FULL)) { |
| 291 | /* Ready to write data */ |
| 292 | break; |
| 293 | } |
| 294 | |
| 295 | udelay(1); |
| 296 | } |
| 297 | |
| 298 | if (!timeout) |
| 299 | return MMC_ERROR_TIMEOUT; |
| 300 | |
| 301 | /* Write data to TXFIFO */ |
| 302 | cnt = wblocklen; |
| 303 | while (cnt) { |
| 304 | while (REG_MSC_STAT & MSC_STAT_DATA_FIFO_FULL) |
| 305 | ; |
| 306 | |
| 307 | if (waligned) { |
| 308 | REG_MSC_TXFIFO = *wbuf++; |
| 309 | } |
| 310 | else { |
| 311 | data = *buf++ | (*buf++ << 8) | (*buf++ << 16) | (*buf++ << 24); |
| 312 | REG_MSC_TXFIFO = data; |
| 313 | } |
| 314 | |
| 315 | cnt--; |
| 316 | } |
| 317 | } |
| 318 | #endif |
| 319 | return MMC_NO_ERROR; |
| 320 | } |
| 321 | |
| 322 | |
| 323 | /* |
| 324 | * Name: int jz_mmc_exec_cmd() |
| 325 | * Function: send command to the card, and get a response |
| 326 | * Input: struct mmc_request *req : MMC/SD request |
| 327 | * Output: 0: right >0: error code |
| 328 | */ |
| 329 | int jz_mmc_exec_cmd(struct mmc_request *request) |
| 330 | { |
| 331 | u32 cmdat = 0, events = 0; |
| 332 | int retval, timeout = 0x3fffff; |
| 333 | |
| 334 | /* Indicate we have no result yet */ |
| 335 | request->result = MMC_NO_RESPONSE; |
| 336 | if (request->cmd == MMC_CIM_RESET) { |
| 337 | /* On reset, 1-bit bus width */ |
| 338 | use_4bit = 0; |
| 339 | |
| 340 | /* Reset MMC/SD controller */ |
| 341 | __msc_reset(); |
| 342 | |
| 343 | /* On reset, drop MMC clock down */ |
| 344 | jz_mmc_set_clock(0, MMC_CLOCK_SLOW); |
| 345 | |
| 346 | /* On reset, stop MMC clock */ |
| 347 | jz_mmc_stop_clock(); |
| 348 | } |
| 349 | if (request->cmd == MMC_CMD_SEND_OP_COND) { |
| 350 | debug("Have an MMC card\n"); |
| 351 | /* always use 1bit for MMC */ |
| 352 | use_4bit = 0; |
| 353 | } |
| 354 | if (request->cmd == SET_BUS_WIDTH) { |
| 355 | if (request->arg == 0x2) { |
| 356 | printf("Use 4-bit bus width\n"); |
| 357 | use_4bit = 1; |
| 358 | } else { |
| 359 | printf("Use 1-bit bus width\n"); |
| 360 | use_4bit = 0; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | /* stop clock */ |
| 365 | jz_mmc_stop_clock(); |
| 366 | |
| 367 | /* mask all interrupts */ |
| 368 | REG_MSC_IMASK = 0xffff; |
| 369 | |
| 370 | /* clear status */ |
| 371 | REG_MSC_IREG = 0xffff; |
| 372 | |
| 373 | /* use 4-bit bus width when possible */ |
| 374 | if (use_4bit) |
| 375 | cmdat |= MSC_CMDAT_BUS_WIDTH_4BIT; |
| 376 | |
| 377 | /* Set command type and events */ |
| 378 | switch (request->cmd) { |
| 379 | /* MMC core extra command */ |
| 380 | case MMC_CIM_RESET: |
| 381 | cmdat |= MSC_CMDAT_INIT; /* Initialization sequence sent prior to command */ |
| 382 | break; |
| 383 | |
| 384 | /* bc - broadcast - no response */ |
| 385 | case MMC_CMD_GO_IDLE_STATE: |
| 386 | case MMC_CMD_SET_DSR: |
| 387 | break; |
| 388 | |
| 389 | /* bcr - broadcast with response */ |
| 390 | case MMC_CMD_SEND_OP_COND: |
| 391 | case MMC_CMD_ALL_SEND_CID: |
| 392 | case MMC_GO_IRQ_STATE: |
| 393 | break; |
| 394 | |
| 395 | /* adtc - addressed with data transfer */ |
| 396 | case MMC_READ_DAT_UNTIL_STOP: |
| 397 | case MMC_CMD_READ_SINGLE_BLOCK: |
| 398 | case MMC_CMD_READ_MULTIPLE_BLOCK: |
| 399 | case SD_CMD_APP_SEND_SCR: |
| 400 | cmdat |= MSC_CMDAT_DATA_EN | MSC_CMDAT_READ; |
| 401 | events = MMC_EVENT_RX_DATA_DONE; |
| 402 | break; |
| 403 | |
| 404 | case MMC_WRITE_DAT_UNTIL_STOP: |
| 405 | case MMC_CMD_WRITE_SINGLE_BLOCK: |
| 406 | case MMC_CMD_WRITE_MULTIPLE_BLOCK: |
| 407 | case MMC_PROGRAM_CID: |
| 408 | case MMC_PROGRAM_CSD: |
| 409 | case MMC_SEND_WRITE_PROT: |
| 410 | case MMC_GEN_CMD: |
| 411 | case MMC_LOCK_UNLOCK: |
| 412 | cmdat |= MSC_CMDAT_DATA_EN | MSC_CMDAT_WRITE; |
| 413 | events = MMC_EVENT_TX_DATA_DONE | MMC_EVENT_PROG_DONE; |
| 414 | |
| 415 | break; |
| 416 | |
| 417 | case MMC_CMD_STOP_TRANSMISSION: |
| 418 | events = MMC_EVENT_PROG_DONE; |
| 419 | break; |
| 420 | |
| 421 | /* ac - no data transfer */ |
| 422 | default: |
| 423 | break; |
| 424 | } |
| 425 | |
| 426 | /* Set response type */ |
| 427 | switch (request->rtype) { |
| 428 | case RESPONSE_NONE: |
| 429 | break; |
| 430 | |
| 431 | case RESPONSE_R1B: |
| 432 | cmdat |= MSC_CMDAT_BUSY; |
| 433 | /*FALLTHRU*/ |
| 434 | case RESPONSE_R1: |
| 435 | cmdat |= MSC_CMDAT_RESPONSE_R1; |
| 436 | break; |
| 437 | case RESPONSE_R2_CID: |
| 438 | case RESPONSE_R2_CSD: |
| 439 | cmdat |= MSC_CMDAT_RESPONSE_R2; |
| 440 | break; |
| 441 | case RESPONSE_R3: |
| 442 | cmdat |= MSC_CMDAT_RESPONSE_R3; |
| 443 | break; |
| 444 | case RESPONSE_R4: |
| 445 | cmdat |= MSC_CMDAT_RESPONSE_R4; |
| 446 | break; |
| 447 | case RESPONSE_R5: |
| 448 | cmdat |= MSC_CMDAT_RESPONSE_R5; |
| 449 | break; |
| 450 | case RESPONSE_R6: |
| 451 | cmdat |= MSC_CMDAT_RESPONSE_R6; |
| 452 | break; |
| 453 | default: |
| 454 | break; |
| 455 | } |
| 456 | |
| 457 | /* Set command index */ |
| 458 | if (request->cmd == MMC_CIM_RESET) { |
| 459 | REG_MSC_CMD = MMC_CMD_GO_IDLE_STATE; |
| 460 | } else { |
| 461 | REG_MSC_CMD = request->cmd; |
| 462 | } |
| 463 | |
| 464 | /* Set argument */ |
| 465 | REG_MSC_ARG = request->arg; |
| 466 | |
| 467 | /* Set block length and nob */ |
| 468 | if (request->cmd == SD_CMD_APP_SEND_SCR) { /* get SCR from DataFIFO */ |
| 469 | REG_MSC_BLKLEN = 8; |
| 470 | REG_MSC_NOB = 1; |
| 471 | } else { |
| 472 | REG_MSC_BLKLEN = request->block_len; |
| 473 | REG_MSC_NOB = request->nob; |
| 474 | } |
| 475 | |
| 476 | /* Set command */ |
| 477 | REG_MSC_CMDAT = cmdat; |
| 478 | |
| 479 | debug("Send cmd %d cmdat: %x arg: %x resp %d\n", request->cmd, |
| 480 | cmdat, request->arg, request->rtype); |
| 481 | |
| 482 | /* Start MMC/SD clock and send command to card */ |
| 483 | jz_mmc_start_clock(); |
| 484 | |
| 485 | /* Wait for command completion */ |
| 486 | while (timeout-- && !(REG_MSC_STAT & MSC_STAT_END_CMD_RES)) |
| 487 | ; |
| 488 | |
| 489 | if (timeout == 0) |
| 490 | return MMC_ERROR_TIMEOUT; |
| 491 | |
| 492 | REG_MSC_IREG = MSC_IREG_END_CMD_RES; /* clear flag */ |
| 493 | |
| 494 | /* Check for status */ |
| 495 | retval = jz_mmc_check_status(request); |
| 496 | if (retval) { |
| 497 | return retval; |
| 498 | } |
| 499 | |
| 500 | /* Complete command with no response */ |
| 501 | if (request->rtype == RESPONSE_NONE) { |
| 502 | return MMC_NO_ERROR; |
| 503 | } |
| 504 | |
| 505 | /* Get response */ |
| 506 | jz_mmc_get_response(request); |
| 507 | |
| 508 | /* Start data operation */ |
| 509 | if (events & (MMC_EVENT_RX_DATA_DONE | MMC_EVENT_TX_DATA_DONE)) { |
| 510 | if (events & MMC_EVENT_RX_DATA_DONE) { |
| 511 | if (request->cmd == SD_CMD_APP_SEND_SCR) { |
| 512 | /* SD card returns SCR register as data. |
| 513 | MMC core expect it in the response buffer, |
| 514 | after normal response. */ |
| 515 | request->buffer = (u8 *)((u32)request->response + 5); |
| 516 | } |
| 517 | jz_mmc_receive_data(request); |
| 518 | } |
| 519 | |
| 520 | if (events & MMC_EVENT_TX_DATA_DONE) { |
| 521 | jz_mmc_transmit_data(request); |
| 522 | } |
| 523 | |
| 524 | /* Wait for Data Done */ |
| 525 | while (!(REG_MSC_IREG & MSC_IREG_DATA_TRAN_DONE)) |
| 526 | ; |
| 527 | REG_MSC_IREG = MSC_IREG_DATA_TRAN_DONE; /* clear status */ |
| 528 | } |
| 529 | |
| 530 | /* Wait for Prog Done event */ |
| 531 | if (events & MMC_EVENT_PROG_DONE) { |
| 532 | while (!(REG_MSC_IREG & MSC_IREG_PRG_DONE)) |
| 533 | ; |
| 534 | REG_MSC_IREG = MSC_IREG_PRG_DONE; /* clear status */ |
| 535 | } |
| 536 | |
| 537 | /* Command completed */ |
| 538 | |
| 539 | return MMC_NO_ERROR; /* return successfully */ |
| 540 | } |
| 541 | |
| 542 | int mmc_block_read(u8 *dst, ulong src, ulong len) |
| 543 | { |
| 544 | |
| 545 | struct mmc_request request; |
| 546 | struct mmc_response_r1 r1; |
| 547 | int retval = 0; |
| 548 | |
| 549 | if (len == 0) |
| 550 | goto exit; |
| 551 | |
| 552 | mmc_simple_cmd(&request, MMC_CMD_SEND_STATUS, mmcinfo.rca, RESPONSE_R1); |
| 553 | retval = mmc_unpack_r1(&request, &r1, 0); |
| 554 | if (retval && (retval != MMC_ERROR_STATE_MISMATCH)) |
| 555 | goto exit; |
| 556 | |
| 557 | mmc_simple_cmd(&request, MMC_CMD_SET_BLOCKLEN, len, RESPONSE_R1); |
| 558 | if (retval = mmc_unpack_r1(&request, &r1, 0)) |
| 559 | goto exit; |
| 560 | |
| 561 | if (!sd2_0) |
| 562 | src *= mmcinfo.block_len; |
| 563 | |
| 564 | mmc_send_cmd(&request, MMC_CMD_READ_SINGLE_BLOCK, src, 1, len, RESPONSE_R1, dst); |
| 565 | if (retval = mmc_unpack_r1(&request, &r1, 0)) |
| 566 | goto exit; |
| 567 | |
| 568 | exit: |
| 569 | return retval; |
| 570 | } |
| 571 | |
| 572 | ulong mmc_bread(int dev_num, ulong blkstart, ulong blkcnt, ulong *dst) |
| 573 | { |
| 574 | if (!mmc_ready) { |
| 575 | printf("Please initial the MMC first\n"); |
| 576 | return -1; |
| 577 | } |
| 578 | |
| 579 | int i = 0; |
| 580 | ulong dst_tmp = dst; |
| 581 | |
| 582 | for (i = 0; i < blkcnt; i++) { |
| 583 | if ((mmc_block_read((uchar *)(dst_tmp), blkstart, mmcinfo.block_len)) < 0) |
| 584 | return -1; |
| 585 | |
| 586 | dst_tmp += mmcinfo.block_len; |
| 587 | blkstart++; |
| 588 | } |
| 589 | |
| 590 | return i; |
| 591 | } |
| 592 | |
| 593 | int mmc_select_card(void) |
| 594 | { |
| 595 | struct mmc_request request; |
| 596 | struct mmc_response_r1 r1; |
| 597 | int retval; |
| 598 | |
| 599 | mmc_simple_cmd(&request, MMC_CMD_SELECT_CARD, mmcinfo.rca, RESPONSE_R1B); |
| 600 | retval = mmc_unpack_r1(&request, &r1, 0); |
| 601 | if (retval) { |
| 602 | return retval; |
| 603 | } |
| 604 | |
| 605 | if (mmcinfo.sd) { |
| 606 | mmc_simple_cmd(&request, MMC_CMD_APP_CMD, mmcinfo.rca, RESPONSE_R1); |
| 607 | retval = mmc_unpack_r1(&request,&r1,0); |
| 608 | if (retval) { |
| 609 | return retval; |
| 610 | } |
| 611 | #if defined(MMC_BUS_WIDTH_1BIT) |
| 612 | mmc_simple_cmd(&request, SET_BUS_WIDTH, 1, RESPONSE_R1); |
| 613 | #else |
| 614 | mmc_simple_cmd(&request, SET_BUS_WIDTH, 2, RESPONSE_R1); |
| 615 | #endif |
| 616 | retval = mmc_unpack_r1(&request,&r1,0); |
| 617 | if (retval) { |
| 618 | return retval; |
| 619 | } |
| 620 | } |
| 621 | return 0; |
| 622 | } |
| 623 | |
| 624 | /* |
| 625 | * Configure card |
| 626 | */ |
| 627 | static void mmc_configure_card(void) |
| 628 | { |
| 629 | u32 rate; |
| 630 | |
| 631 | /* Get card info */ |
| 632 | if (sd2_0) |
| 633 | mmcinfo.block_num = (mmcinfo.csd.c_size + 1) << 10; |
| 634 | else |
| 635 | mmcinfo.block_num = (mmcinfo.csd.c_size + 1) * (1 << (mmcinfo.csd.c_size_mult + 2)); |
| 636 | |
| 637 | mmcinfo.block_len = 1 << mmcinfo.csd.read_bl_len; |
| 638 | |
| 639 | mmc_dev.if_type = IF_TYPE_SD; |
| 640 | mmc_dev.part_type = PART_TYPE_DOS; |
| 641 | mmc_dev.dev = 0; |
| 642 | mmc_dev.lun = 0; |
| 643 | mmc_dev.type = 0; |
| 644 | mmc_dev.blksz = mmcinfo.block_len; |
| 645 | mmc_dev.lba = mmcinfo.block_num; |
| 646 | mmc_dev.removable = 0; |
| 647 | |
| 648 | printf("%s Detected: %lu blocks of %lu bytes\n", |
| 649 | sd2_0 == 1 ? "SDHC" : "SD", |
| 650 | mmc_dev.lba, |
| 651 | mmc_dev.blksz); |
| 652 | |
| 653 | /* Fix the clock rate */ |
| 654 | rate = mmc_tran_speed(mmcinfo.csd.tran_speed); |
| 655 | if (rate < MMC_CLOCK_SLOW) |
| 656 | rate = MMC_CLOCK_SLOW; |
| 657 | if ((mmcinfo.sd == 0) && (rate > MMC_CLOCK_FAST)) |
| 658 | rate = MMC_CLOCK_FAST; |
| 659 | if ((mmcinfo.sd) && (rate > SD_CLOCK_FAST)) |
| 660 | rate = SD_CLOCK_FAST; |
| 661 | |
| 662 | debug("%s: block_len=%d block_num=%d rate=%d\n", |
| 663 | __func__, mmcinfo.block_len, mmcinfo.block_num, rate); |
| 664 | |
| 665 | jz_mmc_set_clock(mmcinfo.sd, rate); |
| 666 | } |
| 667 | |
| 668 | /* |
| 669 | * State machine routines to initialize card(s) |
| 670 | */ |
| 671 | |
| 672 | /* |
| 673 | CIM_SINGLE_CARD_ACQ (frequency at 400 kHz) |
| 674 | --- Must enter from GO_IDLE_STATE --- |
| 675 | 1. SD_SEND_OP_COND (SD Card) [CMD55] + [CMD41] |
| 676 | 2. SEND_OP_COND (Full Range) [CMD1] {optional} |
| 677 | 3. SEND_OP_COND (Set Range ) [CMD1] |
| 678 | If busy, delay and repeat step 2 |
| 679 | 4. ALL_SEND_CID [CMD2] |
| 680 | If timeout, set an error (no cards found) |
| 681 | 5. SET_RELATIVE_ADDR [CMD3] |
| 682 | 6. SEND_CSD [CMD9] |
| 683 | 7. SET_DSR [CMD4] Only call this if (csd.dsr_imp). |
| 684 | 8. Set clock frequency (check available in csd.tran_speed) |
| 685 | */ |
| 686 | |
| 687 | #define MMC_INIT_DOING 0 |
| 688 | #define MMC_INIT_PASSED 1 |
| 689 | #define MMC_INIT_FAILED 2 |
| 690 | |
| 691 | static int mmc_init_card_state(struct mmc_request *request) |
| 692 | { |
| 693 | struct mmc_response_r1 r1; |
| 694 | struct mmc_response_r3 r3; |
| 695 | int retval; |
| 696 | int ocr = 0x40300000; |
| 697 | int limit_41 = 0; |
| 698 | |
| 699 | switch (request->cmd) { |
| 700 | case MMC_CMD_GO_IDLE_STATE: /* No response to parse */ |
| 701 | if (mmcinfo.sd) |
| 702 | mmc_simple_cmd(request, 8, 0x1aa, RESPONSE_R1); |
| 703 | else |
| 704 | mmc_simple_cmd(request, MMC_CMD_SEND_OP_COND, MMC_OCR_ARG, RESPONSE_R3); |
| 705 | break; |
| 706 | |
| 707 | case 8: |
| 708 | retval = mmc_unpack_r1(request,&r1,mmcinfo.state); |
| 709 | mmc_simple_cmd(request, MMC_CMD_APP_CMD, 0, RESPONSE_R1); |
| 710 | break; |
| 711 | |
| 712 | case MMC_CMD_APP_CMD: |
| 713 | retval = mmc_unpack_r1(request,&r1,mmcinfo.state); |
| 714 | if (retval & (limit_41 < 100)) { |
| 715 | debug("%s: unable to MMC_APP_CMD error=%d (%s)\n", |
| 716 | __func__, retval, mmc_result_to_string(retval)); |
| 717 | limit_41++; |
| 718 | mmc_simple_cmd(request, SD_CMD_APP_SEND_OP_COND, ocr, RESPONSE_R3); |
| 719 | } else if (limit_41 < 100) { |
| 720 | limit_41++; |
| 721 | mmc_simple_cmd(request, SD_CMD_APP_SEND_OP_COND, ocr, RESPONSE_R3); |
| 722 | } else{ |
| 723 | /* reset the card to idle*/ |
| 724 | mmc_simple_cmd(request, MMC_CMD_GO_IDLE_STATE, 0, RESPONSE_NONE); |
| 725 | mmcinfo.sd = 0; |
| 726 | } |
| 727 | break; |
| 728 | |
| 729 | case SD_CMD_APP_SEND_OP_COND: |
| 730 | retval = mmc_unpack_r3(request, &r3); |
| 731 | if (retval) { |
| 732 | debug("%s: try MMC card\n", __func__); |
| 733 | mmc_simple_cmd(request, SD_CMD_APP_SEND_OP_COND, MMC_OCR_ARG, RESPONSE_R3); |
| 734 | break; |
| 735 | } |
| 736 | |
| 737 | debug("%s: read ocr value = 0x%08x\n", __func__, r3.ocr); |
| 738 | |
| 739 | if(!(r3.ocr & MMC_CARD_BUSY || ocr == 0)){ |
| 740 | udelay(50000); |
| 741 | mmc_simple_cmd(request, MMC_CMD_APP_CMD, 0, RESPONSE_R1); |
| 742 | } else { |
| 743 | mmcinfo.sd = 1; /* SD Card ready */ |
| 744 | mmcinfo.state = CARD_STATE_READY; |
| 745 | mmc_simple_cmd(request, MMC_CMD_ALL_SEND_CID, 0, RESPONSE_R2_CID); |
| 746 | } |
| 747 | break; |
| 748 | |
| 749 | case MMC_CMD_SEND_OP_COND: |
| 750 | retval = mmc_unpack_r3(request, &r3); |
| 751 | if (retval) { |
| 752 | debug("%s: failed SEND_OP_COND error=%d (%s)\n", |
| 753 | __func__, retval, mmc_result_to_string(retval)); |
| 754 | return MMC_INIT_FAILED; |
| 755 | } |
| 756 | |
| 757 | debug("%s: read ocr value = 0x%08x\n", __func__, r3.ocr); |
| 758 | if (!(r3.ocr & MMC_CARD_BUSY)) { |
| 759 | mmc_simple_cmd(request, MMC_CMD_SEND_OP_COND, MMC_OCR_ARG, RESPONSE_R3); |
| 760 | } else { |
| 761 | mmcinfo.sd = 0; /* MMC Card ready */ |
| 762 | mmcinfo.state = CARD_STATE_READY; |
| 763 | mmc_simple_cmd(request, MMC_CMD_ALL_SEND_CID, 0, RESPONSE_R2_CID); |
| 764 | } |
| 765 | break; |
| 766 | |
| 767 | case MMC_CMD_ALL_SEND_CID: |
| 768 | retval = mmc_unpack_cid( request, &mmcinfo.cid ); |
| 769 | /*FIXME:ignore CRC error for CMD2/CMD9/CMD10 */ |
| 770 | if ( retval && (retval != MMC_ERROR_CRC)) { |
| 771 | debug("mmc_init_card_state: unable to ALL_SEND_CID error=%d (%s)\n", |
| 772 | retval, mmc_result_to_string(retval)); |
| 773 | return MMC_INIT_FAILED; |
| 774 | } |
| 775 | mmcinfo.state = CARD_STATE_IDENT; |
| 776 | if(mmcinfo.sd) |
| 777 | mmc_simple_cmd(request, MMC_CMD_SET_RELATIVE_ADDR, 0, RESPONSE_R6); |
| 778 | else |
| 779 | mmc_simple_cmd(request, MMC_CMD_SET_RELATIVE_ADDR, ID_TO_RCA(mmcinfo.id) << 16, RESPONSE_R1); |
| 780 | break; |
| 781 | |
| 782 | case MMC_CMD_SET_RELATIVE_ADDR: |
| 783 | if (mmcinfo.sd) { |
| 784 | retval = mmc_unpack_r6(request, &r1, mmcinfo.state, &mmcinfo.rca); |
| 785 | mmcinfo.rca = mmcinfo.rca << 16; |
| 786 | debug("%s: Get RCA from SD: 0x%04x Status: %x\n", |
| 787 | __func__, mmcinfo.rca, r1.status); |
| 788 | } else { |
| 789 | retval = mmc_unpack_r1(request,&r1,mmcinfo.state); |
| 790 | mmcinfo.rca = ID_TO_RCA(mmcinfo.id) << 16; |
| 791 | } |
| 792 | if (retval) { |
| 793 | debug("%s: unable to SET_RELATIVE_ADDR error=%d (%s)\n", |
| 794 | __func__, retval, mmc_result_to_string(retval)); |
| 795 | return MMC_INIT_FAILED; |
| 796 | } |
| 797 | |
| 798 | mmcinfo.state = CARD_STATE_STBY; |
| 799 | mmc_simple_cmd(request, MMC_CMD_SEND_CSD, mmcinfo.rca, RESPONSE_R2_CSD); |
| 800 | |
| 801 | break; |
| 802 | |
| 803 | case MMC_CMD_SEND_CSD: |
| 804 | retval = mmc_unpack_csd(request, &mmcinfo.csd); |
| 805 | mmc_ready = 1; |
| 806 | /*FIXME:ignore CRC error for CMD2/CMD9/CMD10 */ |
| 807 | if (retval && (retval != MMC_ERROR_CRC)) { |
| 808 | debug("%s: unable to SEND_CSD error=%d (%s)\n", |
| 809 | __func__, retval, mmc_result_to_string(retval)); |
| 810 | return MMC_INIT_FAILED; |
| 811 | } |
| 812 | if (mmcinfo.csd.dsr_imp) { |
| 813 | debug("%s: driver doesn't support setting DSR\n", __func__); |
| 814 | } |
| 815 | mmc_configure_card(); |
| 816 | return MMC_INIT_PASSED; |
| 817 | |
| 818 | default: |
| 819 | debug("%s: error! Illegal last cmd %d\n", __func__, request->cmd); |
| 820 | return MMC_INIT_FAILED; |
| 821 | } |
| 822 | |
| 823 | return MMC_INIT_DOING; |
| 824 | } |
| 825 | |
| 826 | int mmc_init_card(void) |
| 827 | { |
| 828 | struct mmc_request request; |
| 829 | int retval; |
| 830 | |
| 831 | mmc_simple_cmd(&request, MMC_CIM_RESET, 0, RESPONSE_NONE); /* reset card */ |
| 832 | mmc_simple_cmd(&request, MMC_CMD_GO_IDLE_STATE, 0, RESPONSE_NONE); |
| 833 | mmcinfo.sd = 1; /* assuming a SD card */ |
| 834 | |
| 835 | while ((retval = mmc_init_card_state(&request)) == MMC_INIT_DOING) |
| 836 | ; |
| 837 | |
| 838 | if (retval == MMC_INIT_PASSED) |
| 839 | return MMC_NO_ERROR; |
| 840 | else |
| 841 | return MMC_NO_RESPONSE; |
| 842 | } |
| 843 | |
| 844 | int mmc_legacy_init(int verbose) |
| 845 | { |
| 846 | if (!__msc_card_detected()) |
| 847 | return 1; |
| 848 | |
| 849 | /* Step-1: init GPIO */ |
| 850 | __gpio_as_msc(); |
| 851 | __msc_init_io(); |
| 852 | |
| 853 | /* Step-2: turn on power of card */ |
| 854 | #if !defined(CONFIG_SAKC) |
| 855 | __msc_enable_power(); |
| 856 | #endif |
| 857 | |
| 858 | /* Step-3: Reset MSC Controller. */ |
| 859 | __msc_reset(); |
| 860 | |
| 861 | /* Step-3: mask all IRQs. */ |
| 862 | MMC_IRQ_MASK(); |
| 863 | |
| 864 | /* Step-4: stop MMC/SD clock */ |
| 865 | jz_mmc_stop_clock(); |
| 866 | mmc_init_card(); |
| 867 | mmc_select_card(); |
| 868 | |
| 869 | mmc_dev.block_read = mmc_bread; |
| 870 | fat_register_device(&mmc_dev,1); /* partitions start counting with 1 */ |
| 871 | |
| 872 | return 0; |
| 873 | } |
| 874 | |
| 875 | /* |
| 876 | * Debugging functions |
| 877 | */ |
| 878 | static char * mmc_result_strings[] = { |
| 879 | "NO_RESPONSE", |
| 880 | "NO_ERROR", |
| 881 | "ERROR_OUT_OF_RANGE", |
| 882 | "ERROR_ADDRESS", |
| 883 | "ERROR_BLOCK_LEN", |
| 884 | "ERROR_ERASE_SEQ", |
| 885 | "ERROR_ERASE_PARAM", |
| 886 | "ERROR_WP_VIOLATION", |
| 887 | "ERROR_CARD_IS_LOCKED", |
| 888 | "ERROR_LOCK_UNLOCK_FAILED", |
| 889 | "ERROR_COM_CRC", |
| 890 | "ERROR_ILLEGAL_COMMAND", |
| 891 | "ERROR_CARD_ECC_FAILED", |
| 892 | "ERROR_CC", |
| 893 | "ERROR_GENERAL", |
| 894 | "ERROR_UNDERRUN", |
| 895 | "ERROR_OVERRUN", |
| 896 | "ERROR_CID_CSD_OVERWRITE", |
| 897 | "ERROR_STATE_MISMATCH", |
| 898 | "ERROR_HEADER_MISMATCH", |
| 899 | "ERROR_TIMEOUT", |
| 900 | "ERROR_CRC", |
| 901 | "ERROR_DRIVER_FAILURE", |
| 902 | }; |
| 903 | |
| 904 | char * mmc_result_to_string(int i) |
| 905 | { |
| 906 | return mmc_result_strings[i+1]; |
| 907 | } |
| 908 | |
| 909 | static char * card_state_strings[] = { |
| 910 | "empty", |
| 911 | "idle", |
| 912 | "ready", |
| 913 | "ident", |
| 914 | "stby", |
| 915 | "tran", |
| 916 | "data", |
| 917 | "rcv", |
| 918 | "prg", |
| 919 | "dis", |
| 920 | }; |
| 921 | |
| 922 | static inline char * card_state_to_string(int i) |
| 923 | { |
| 924 | return card_state_strings[i+1]; |
| 925 | } |
| 926 | |
| 927 | /* |
| 928 | * Utility functions |
| 929 | */ |
| 930 | |
| 931 | #define PARSE_U32(_buf,_index) \ |
| 932 | (((u32)_buf[_index]) << 24) | (((u32)_buf[_index+1]) << 16) | \ |
| 933 | (((u32)_buf[_index+2]) << 8) | ((u32)_buf[_index+3]); |
| 934 | |
| 935 | #define PARSE_U16(_buf,_index) \ |
| 936 | (((u16)_buf[_index]) << 8) | ((u16)_buf[_index+1]); |
| 937 | |
| 938 | int mmc_unpack_csd(struct mmc_request *request, struct mmc_csd *csd) |
| 939 | { |
| 940 | u8 *buf = request->response; |
| 941 | int num = 0; |
| 942 | |
| 943 | if (request->result) |
| 944 | return request->result; |
| 945 | |
| 946 | if (buf[0] != 0x3f) |
| 947 | return MMC_ERROR_HEADER_MISMATCH; |
| 948 | |
| 949 | csd->csd_structure = (buf[1] & 0xc0) >> 6; |
| 950 | if (csd->csd_structure) |
| 951 | sd2_0 = 1; |
| 952 | else |
| 953 | sd2_0 = 0; |
| 954 | |
| 955 | switch (csd->csd_structure) { |
| 956 | case 0 :/* Version 1.01-1.10 |
| 957 | * Version 2.00/Standard Capacity */ |
| 958 | csd->taac = buf[2]; |
| 959 | csd->nsac = buf[3]; |
| 960 | csd->tran_speed = buf[4]; |
| 961 | csd->ccc = (((u16)buf[5]) << 4) | ((buf[6] & 0xf0) >> 4); |
| 962 | csd->read_bl_len = buf[6] & 0x0f; |
| 963 | /* for support 2GB card*/ |
| 964 | if (csd->read_bl_len >= 10) |
| 965 | { |
| 966 | num = csd->read_bl_len - 9; |
| 967 | csd->read_bl_len = 9; |
| 968 | } |
| 969 | |
| 970 | csd->read_bl_partial = (buf[7] & 0x80) ? 1 : 0; |
| 971 | csd->write_blk_misalign = (buf[7] & 0x40) ? 1 : 0; |
| 972 | csd->read_blk_misalign = (buf[7] & 0x20) ? 1 : 0; |
| 973 | csd->dsr_imp = (buf[7] & 0x10) ? 1 : 0; |
| 974 | csd->c_size = ((((u16)buf[7]) & 0x03) << 10) | (((u16)buf[8]) << 2) | (((u16)buf[9]) & 0xc0) >> 6; |
| 975 | |
| 976 | if (num) |
| 977 | csd->c_size = csd->c_size << num; |
| 978 | |
| 979 | |
| 980 | csd->vdd_r_curr_min = (buf[9] & 0x38) >> 3; |
| 981 | csd->vdd_r_curr_max = buf[9] & 0x07; |
| 982 | csd->vdd_w_curr_min = (buf[10] & 0xe0) >> 5; |
| 983 | csd->vdd_w_curr_max = (buf[10] & 0x1c) >> 2; |
| 984 | csd->c_size_mult = ((buf[10] & 0x03) << 1) | ((buf[11] & 0x80) >> 7); |
| 985 | csd->sector_size = (buf[11] & 0x7c) >> 2; |
| 986 | csd->erase_grp_size = ((buf[11] & 0x03) << 3) | ((buf[12] & 0xe0) >> 5); |
| 987 | csd->wp_grp_size = buf[12] & 0x1f; |
| 988 | csd->wp_grp_enable = (buf[13] & 0x80) ? 1 : 0; |
| 989 | csd->default_ecc = (buf[13] & 0x60) >> 5; |
| 990 | csd->r2w_factor = (buf[13] & 0x1c) >> 2; |
| 991 | csd->write_bl_len = ((buf[13] & 0x03) << 2) | ((buf[14] & 0xc0) >> 6); |
| 992 | if (csd->write_bl_len >= 10) |
| 993 | csd->write_bl_len = 9; |
| 994 | |
| 995 | csd->write_bl_partial = (buf[14] & 0x20) ? 1 : 0; |
| 996 | csd->file_format_grp = (buf[15] & 0x80) ? 1 : 0; |
| 997 | csd->copy = (buf[15] & 0x40) ? 1 : 0; |
| 998 | csd->perm_write_protect = (buf[15] & 0x20) ? 1 : 0; |
| 999 | csd->tmp_write_protect = (buf[15] & 0x10) ? 1 : 0; |
| 1000 | csd->file_format = (buf[15] & 0x0c) >> 2; |
| 1001 | csd->ecc = buf[15] & 0x03; |
| 1002 | break; |
| 1003 | case 1 : /* Version 2.00/High Capacity */ |
| 1004 | csd->taac = 0; |
| 1005 | csd->nsac = 0; |
| 1006 | csd->tran_speed = buf[4]; |
| 1007 | csd->ccc = (((u16)buf[5]) << 4) | ((buf[6] & 0xf0) >> 4); |
| 1008 | |
| 1009 | csd->read_bl_len = 9; |
| 1010 | csd->read_bl_partial = 0; |
| 1011 | csd->write_blk_misalign = 0; |
| 1012 | csd->read_blk_misalign = 0; |
| 1013 | csd->dsr_imp = (buf[7] & 0x10) ? 1 : 0; |
| 1014 | csd->c_size = ((((u16)buf[8]) & 0x3f) << 16) | (((u16)buf[9]) << 8) | ((u16)buf[10]) ; |
| 1015 | csd->sector_size = 0x7f; |
| 1016 | csd->erase_grp_size = 0; |
| 1017 | csd->wp_grp_size = 0; |
| 1018 | csd->wp_grp_enable = 0; |
| 1019 | csd->default_ecc = (buf[13] & 0x60) >> 5; |
| 1020 | csd->r2w_factor = 4;/* Unused */ |
| 1021 | csd->write_bl_len = 9; |
| 1022 | |
| 1023 | csd->write_bl_partial = 0; |
| 1024 | csd->file_format_grp = 0; |
| 1025 | csd->copy = (buf[15] & 0x40) ? 1 : 0; |
| 1026 | csd->perm_write_protect = (buf[15] & 0x20) ? 1 : 0; |
| 1027 | csd->tmp_write_protect = (buf[15] & 0x10) ? 1 : 0; |
| 1028 | csd->file_format = 0; |
| 1029 | csd->ecc = buf[15] & 0x03; |
| 1030 | } |
| 1031 | |
| 1032 | return 0; |
| 1033 | } |
| 1034 | |
| 1035 | int mmc_unpack_r1(struct mmc_request *request, struct mmc_response_r1 *r1, enum card_state state) |
| 1036 | { |
| 1037 | u8 *buf = request->response; |
| 1038 | |
| 1039 | if (request->result) |
| 1040 | return request->result; |
| 1041 | |
| 1042 | r1->cmd = buf[0]; |
| 1043 | r1->status = PARSE_U32(buf,1); |
| 1044 | |
| 1045 | debug("mmc_unpack_r1: cmd=%d status=%08x\n", r1->cmd, r1->status); |
| 1046 | |
| 1047 | if (R1_STATUS(r1->status)) { |
| 1048 | if (r1->status & R1_OUT_OF_RANGE) return MMC_ERROR_OUT_OF_RANGE; |
| 1049 | if (r1->status & R1_ADDRESS_ERROR) return MMC_ERROR_ADDRESS; |
| 1050 | if (r1->status & R1_BLOCK_LEN_ERROR) return MMC_ERROR_BLOCK_LEN; |
| 1051 | if (r1->status & R1_ERASE_SEQ_ERROR) return MMC_ERROR_ERASE_SEQ; |
| 1052 | if (r1->status & R1_ERASE_PARAM) return MMC_ERROR_ERASE_PARAM; |
| 1053 | if (r1->status & R1_WP_VIOLATION) return MMC_ERROR_WP_VIOLATION; |
| 1054 | /*if (r1->status & R1_CARD_IS_LOCKED) return MMC_ERROR_CARD_IS_LOCKED; */ |
| 1055 | if (r1->status & R1_LOCK_UNLOCK_FAILED) return MMC_ERROR_LOCK_UNLOCK_FAILED; |
| 1056 | if (r1->status & R1_COM_CRC_ERROR) return MMC_ERROR_COM_CRC; |
| 1057 | if (r1->status & R1_ILLEGAL_COMMAND) return MMC_ERROR_ILLEGAL_COMMAND; |
| 1058 | if (r1->status & R1_CARD_ECC_FAILED) return MMC_ERROR_CARD_ECC_FAILED; |
| 1059 | if (r1->status & R1_CC_ERROR) return MMC_ERROR_CC; |
| 1060 | if (r1->status & R1_ERROR) return MMC_ERROR_GENERAL; |
| 1061 | if (r1->status & R1_UNDERRUN) return MMC_ERROR_UNDERRUN; |
| 1062 | if (r1->status & R1_OVERRUN) return MMC_ERROR_OVERRUN; |
| 1063 | if (r1->status & R1_CID_CSD_OVERWRITE) return MMC_ERROR_CID_CSD_OVERWRITE; |
| 1064 | } |
| 1065 | |
| 1066 | if (buf[0] != request->cmd) |
| 1067 | return MMC_ERROR_HEADER_MISMATCH; |
| 1068 | |
| 1069 | /* This should be last - it's the least dangerous error */ |
| 1070 | |
| 1071 | return 0; |
| 1072 | } |
| 1073 | |
| 1074 | int mmc_unpack_scr(struct mmc_request *request, struct mmc_response_r1 *r1, enum card_state state, u32 *scr) |
| 1075 | { |
| 1076 | u8 *buf = request->response; |
| 1077 | if (request->result) |
| 1078 | return request->result; |
| 1079 | |
| 1080 | *scr = PARSE_U32(buf, 5); /* Save SCR returned by the SD Card */ |
| 1081 | return mmc_unpack_r1(request, r1, state); |
| 1082 | |
| 1083 | } |
| 1084 | |
| 1085 | int mmc_unpack_r6(struct mmc_request *request, struct mmc_response_r1 *r1, enum card_state state, int *rca) |
| 1086 | { |
| 1087 | u8 *buf = request->response; |
| 1088 | |
| 1089 | if (request->result) |
| 1090 | return request->result; |
| 1091 | |
| 1092 | *rca = PARSE_U16(buf,1); /* Save RCA returned by the SD Card */ |
| 1093 | |
| 1094 | *(buf+1) = 0; |
| 1095 | *(buf+2) = 0; |
| 1096 | |
| 1097 | return mmc_unpack_r1(request, r1, state); |
| 1098 | } |
| 1099 | |
| 1100 | int mmc_unpack_cid(struct mmc_request *request, struct mmc_cid *cid) |
| 1101 | { |
| 1102 | int i; |
| 1103 | u8 *buf = request->response; |
| 1104 | |
| 1105 | if (request->result) |
| 1106 | return request->result; |
| 1107 | |
| 1108 | cid->mid = buf[1]; |
| 1109 | cid->oid = PARSE_U16(buf,2); |
| 1110 | for (i = 0 ; i < 5 ; i++) |
| 1111 | cid->pnm[i] = buf[4+i]; |
| 1112 | cid->pnm[6] = 0; |
| 1113 | cid->prv = buf[10]; |
| 1114 | cid->psn = PARSE_U32(buf,10); |
| 1115 | cid->mdt = buf[15]; |
| 1116 | |
| 1117 | printf("Man %02x OEM 0x%04x \"%s\" %d.%d 0x%08x " |
| 1118 | "Date %02u/%04u\n", |
| 1119 | cid->mid, |
| 1120 | cid->oid, |
| 1121 | cid->pnm, |
| 1122 | cid->prv >> 4, |
| 1123 | cid->prv & 0xf, |
| 1124 | cid->psn, |
| 1125 | cid->mdt & 0xf, |
| 1126 | (cid->mdt >> 4) + 2000); |
| 1127 | |
| 1128 | if (buf[0] != 0x3f) |
| 1129 | return MMC_ERROR_HEADER_MISMATCH; |
| 1130 | return 0; |
| 1131 | } |
| 1132 | |
| 1133 | int mmc_unpack_r3(struct mmc_request *request, struct mmc_response_r3 *r3) |
| 1134 | { |
| 1135 | u8 *buf = request->response; |
| 1136 | |
| 1137 | if (request->result) |
| 1138 | return request->result; |
| 1139 | |
| 1140 | r3->ocr = PARSE_U32(buf,1); |
| 1141 | debug("mmc_unpack_r3: ocr=%08x\n", r3->ocr); |
| 1142 | |
| 1143 | if (buf[0] != 0x3f) return MMC_ERROR_HEADER_MISMATCH; |
| 1144 | return 0; |
| 1145 | } |
| 1146 | |
| 1147 | #define KBPS 1 |
| 1148 | #define MBPS 1000 |
| 1149 | |
| 1150 | static u32 ts_exp[] = { 100*KBPS, 1*MBPS, 10*MBPS, 100*MBPS, 0, 0, 0, 0 }; |
| 1151 | static u32 ts_mul[] = { 0, 1000, 1200, 1300, 1500, 2000, 2500, 3000, |
| 1152 | 3500, 4000, 4500, 5000, 5500, 6000, 7000, 8000 }; |
| 1153 | |
| 1154 | u32 mmc_tran_speed(u8 ts) |
| 1155 | { |
| 1156 | u32 rate = ts_exp[(ts & 0x7)] * ts_mul[(ts & 0x78) >> 3]; |
| 1157 | |
| 1158 | if (rate <= 0) { |
| 1159 | debug("%s: error - unrecognized speed 0x%02x\n", __func__, ts); |
| 1160 | return 1; |
| 1161 | } |
| 1162 | |
| 1163 | return rate; |
| 1164 | } |
| 1165 | |
| 1166 | void mmc_send_cmd(struct mmc_request *request, int cmd, u32 arg, |
| 1167 | u16 nob, u16 block_len, enum mmc_rsp_t rtype, u8 *buffer) |
| 1168 | { |
| 1169 | request->cmd = cmd; |
| 1170 | request->arg = arg; |
| 1171 | request->rtype = rtype; |
| 1172 | request->nob = nob; |
| 1173 | request->block_len = block_len; |
| 1174 | request->buffer = buffer; |
| 1175 | request->cnt = nob * block_len; |
| 1176 | |
| 1177 | jz_mmc_exec_cmd(request); |
| 1178 | } |
| 1179 | |