3 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
28 #if defined CONFIG_JZ4740
29 #include <asm-mips/jz4740.h>
33 #define CFG_MMC_BASE 0x80600000
39 #define __msc_init_io() \
41 __gpio_as_output(GPIO_SD_VCC_EN_N); \
42 __gpio_as_input(GPIO_SD_CD_N); \
45 #define __msc_enable_power() \
47 __gpio_clear_pin(GPIO_SD_VCC_EN_N); \
50 #define __msc_disable_power() \
52 __gpio_set_pin(GPIO_SD_VCC_EN_N); \
55 #define __msc_card_detected() \
58 __gpio_as_input(GPIO_SD_CD_N); \
59 if (!__gpio_get_pin(GPIO_SD_CD_N)) \
70 fat_register_device(block_dev_desc_t *dev_desc, int part_no);
72 static block_dev_desc_t mmc_dev;
74 block_dev_desc_t * mmc_get_dev(int dev)
76 return ((block_dev_desc_t *)&mmc_dev);
80 * FIXME needs to read cid and csd info to determine block size
81 * and other parameters
83 static uchar mmc_buf[MMC_BLOCK_SIZE];
84 static int mmc_ready = 0;
85 static mmc_csd_t mmc_csd;
86 static int use_4bit; /* Use 4-bit data bus */
90 #define MMC_EVENT_NONE 0x00 /* No events */
91 #define MMC_EVENT_RX_DATA_DONE 0x01 /* Rx data done */
92 #define MMC_EVENT_TX_DATA_DONE 0x02 /* Tx data done */
93 #define MMC_EVENT_PROG_DONE 0x04 /* Programming is done */
96 #define MMC_IRQ_MASK() \
98 REG_MSC_IMASK = 0xffff; \
99 REG_MSC_IREG = 0xffff; \
102 /* Stop the MMC clock and wait while it happens */
103 static inline int jz_mmc_stop_clock(void)
107 REG_MSC_STRPCL = MSC_STRPCL_CLOCK_CONTROL_STOP;
109 while (timeout && (REG_MSC_STAT & MSC_STAT_CLK_EN)) {
112 return MMC_ERROR_TIMEOUT;
119 /* Start the MMC clock and operation */
120 static inline int jz_mmc_start_clock(void)
122 REG_MSC_STRPCL = MSC_STRPCL_CLOCK_CONTROL_START | MSC_STRPCL_START_OP;
126 static inline u32 jz_mmc_calc_clkrt(int is_sd, u32 rate)
129 u32 clk_src = is_sd ? 24000000: 16000000;
132 while (rate < clk_src)
140 /* Set the MMC clock frequency */
141 void jz_mmc_set_clock(int sd, u32 rate)
145 /* Select clock source of MSC */
146 __cpm_select_msc_clk(sd);
148 /* Set clock dividor of MSC */
149 REG_MSC_CLKRT = jz_mmc_calc_clkrt(sd, rate);
152 static int jz_mmc_check_status(struct mmc_request *request)
154 u32 status = REG_MSC_STAT;
156 /* Checking for response or data timeout */
157 if (status & (MSC_STAT_TIME_OUT_RES | MSC_STAT_TIME_OUT_READ)) {
158 printf("MMC/SD timeout, MMC_STAT 0x%x CMD %d\n", status, request->cmd);
159 return MMC_ERROR_TIMEOUT;
162 /* Checking for CRC error */
163 if (status & (MSC_STAT_CRC_READ_ERROR | MSC_STAT_CRC_WRITE_ERROR | MSC_STAT_CRC_RES_ERR)) {
164 printf("MMC/CD CRC error, MMC_STAT 0x%x\n", status);
165 return MMC_ERROR_CRC;
171 /* Obtain response to the command and store it to response buffer */
172 static void jz_mmc_get_response(struct mmc_request *request)
178 DEBUG(3, "fetch response for request %d, cmd %d\n", request->rtype, request->cmd);
180 buf = request->response;
181 request->result = MMC_NO_ERROR;
183 switch (request->rtype) {
184 case RESPONSE_R1: case RESPONSE_R1B: case RESPONSE_R6:
185 case RESPONSE_R3: case RESPONSE_R4: case RESPONSE_R5:
188 buf[0] = (data >> 8) & 0xff;
189 buf[1] = data & 0xff;
191 buf[2] = (data >> 8) & 0xff;
192 buf[3] = data & 0xff;
194 buf[4] = data & 0xff;
196 DEBUG(3, "request %d, response [%02x %02x %02x %02x %02x]\n",
197 request->rtype, buf[0], buf[1], buf[2], buf[3], buf[4]);
200 case RESPONSE_R2_CID: case RESPONSE_R2_CSD:
202 for (i = 0; i < 16; i += 2) {
204 buf[i] = (data >> 8) & 0xff;
205 buf[i+1] = data & 0xff;
207 DEBUG(3, "request %d, response [", request->rtype);
208 #if CONFIG_MMC_DEBUG_VERBOSE > 2
209 if (g_mmc_debug >= 3) {
211 for (n = 0; n < 17; n++)
212 printk("%02x ", buf[n]);
219 DEBUG(3, "No response\n");
223 DEBUG(3, "unhandled response type for request %d\n", request->rtype);
228 static int jz_mmc_receive_data(struct mmc_request *req)
230 u32 stat, timeout, data, cnt;
231 u8 *buf = req->buffer;
232 u32 wblocklen = (u32)(req->block_len + 3) >> 2; /* length in word */
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 */
252 return MMC_ERROR_TIMEOUT;
254 /* Read data from RXFIFO. It could be FULL or PARTIAL FULL */
257 data = REG_MSC_RXFIFO;
259 *buf++ = (u8)(data >> 0);
260 *buf++ = (u8)(data >> 8);
261 *buf++ = (u8)(data >> 16);
262 *buf++ = (u8)(data >> 24);
265 while (cnt && (REG_MSC_STAT & MSC_STAT_DATA_FIFO_EMPTY))
271 static int jz_mmc_transmit_data(struct mmc_request *req)
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;
281 for (nob; nob >= 1; nob--) {
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 */
299 return MMC_ERROR_TIMEOUT;
301 /* Write data to TXFIFO */
304 while (REG_MSC_STAT & MSC_STAT_DATA_FIFO_FULL)
308 REG_MSC_TXFIFO = *wbuf++;
311 data = *buf++ | (*buf++ << 8) | (*buf++ << 16) | (*buf++ << 24);
312 REG_MSC_TXFIFO = data;
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
329 int jz_mmc_exec_cmd(struct mmc_request *request)
331 u32 cmdat = 0, events = 0;
332 int retval, timeout = 0x3fffff;
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 */
340 /* Reset MMC/SD controller */
343 /* On reset, drop MMC clock down */
344 jz_mmc_set_clock(0, MMC_CLOCK_SLOW);
346 /* On reset, stop MMC clock */
349 if (request->cmd == MMC_SEND_OP_COND) {
350 DEBUG(3, "Have an MMC card\n");
351 /* always use 1bit for MMC */
354 if (request->cmd == SET_BUS_WIDTH) {
355 if (request->arg == 0x2) {
356 DEBUG(2, "Use 4-bit bus width\n");
360 DEBUG(2, "Use 1-bit bus width\n");
368 /* mask all interrupts */
369 REG_MSC_IMASK = 0xffff;
372 REG_MSC_IREG = 0xffff;
374 /* use 4-bit bus width when possible */
376 cmdat |= MSC_CMDAT_BUS_WIDTH_4BIT;
378 /* Set command type and events */
379 switch (request->cmd) {
380 /* MMC core extra command */
382 cmdat |= MSC_CMDAT_INIT; /* Initialization sequence sent prior to command */
385 /* bc - broadcast - no response */
386 case MMC_GO_IDLE_STATE:
390 /* bcr - broadcast with response */
391 case MMC_SEND_OP_COND:
392 case MMC_ALL_SEND_CID:
393 case MMC_GO_IRQ_STATE:
396 /* adtc - addressed with data transfer */
397 case MMC_READ_DAT_UNTIL_STOP:
398 case MMC_READ_SINGLE_BLOCK:
399 case MMC_READ_MULTIPLE_BLOCK:
401 cmdat |= MSC_CMDAT_DATA_EN | MSC_CMDAT_READ;
402 events = MMC_EVENT_RX_DATA_DONE;
405 case MMC_WRITE_DAT_UNTIL_STOP:
406 case MMC_WRITE_BLOCK:
407 case MMC_WRITE_MULTIPLE_BLOCK:
408 case MMC_PROGRAM_CID:
409 case MMC_PROGRAM_CSD:
410 case MMC_SEND_WRITE_PROT:
412 case MMC_LOCK_UNLOCK:
413 cmdat |= MSC_CMDAT_DATA_EN | MSC_CMDAT_WRITE;
414 events = MMC_EVENT_TX_DATA_DONE | MMC_EVENT_PROG_DONE;
418 case MMC_STOP_TRANSMISSION:
419 events = MMC_EVENT_PROG_DONE;
422 /* ac - no data transfer */
427 /* Set response type */
428 switch (request->rtype) {
433 cmdat |= MSC_CMDAT_BUSY;
436 cmdat |= MSC_CMDAT_RESPONSE_R1;
438 case RESPONSE_R2_CID:
439 case RESPONSE_R2_CSD:
440 cmdat |= MSC_CMDAT_RESPONSE_R2;
443 cmdat |= MSC_CMDAT_RESPONSE_R3;
446 cmdat |= MSC_CMDAT_RESPONSE_R4;
449 cmdat |= MSC_CMDAT_RESPONSE_R5;
452 cmdat |= MSC_CMDAT_RESPONSE_R6;
458 /* Set command index */
459 if (request->cmd == MMC_CIM_RESET) {
460 REG_MSC_CMD = MMC_GO_IDLE_STATE;
462 REG_MSC_CMD = request->cmd;
466 REG_MSC_ARG = request->arg;
468 /* Set block length and nob */
469 if (request->cmd == SEND_SCR) { /* get SCR from DataFIFO */
473 REG_MSC_BLKLEN = request->block_len;
474 REG_MSC_NOB = request->nob;
478 REG_MSC_CMDAT = cmdat;
480 DEBUG(1, "Send cmd %d cmdat: %x arg: %x resp %d\n", request->cmd,
481 cmdat, request->arg, request->rtype);
483 /* Start MMC/SD clock and send command to card */
484 jz_mmc_start_clock();
486 /* Wait for command completion */
487 while (timeout-- && !(REG_MSC_STAT & MSC_STAT_END_CMD_RES))
491 return MMC_ERROR_TIMEOUT;
493 REG_MSC_IREG = MSC_IREG_END_CMD_RES; /* clear flag */
495 /* Check for status */
496 retval = jz_mmc_check_status(request);
501 /* Complete command with no response */
502 if (request->rtype == RESPONSE_NONE) {
507 jz_mmc_get_response(request);
509 /* Start data operation */
510 if (events & (MMC_EVENT_RX_DATA_DONE | MMC_EVENT_TX_DATA_DONE)) {
511 if (events & MMC_EVENT_RX_DATA_DONE) {
512 if (request->cmd == SEND_SCR) {
513 /* SD card returns SCR register as data.
514 MMC core expect it in the response buffer,
515 after normal response. */
516 request->buffer = (u8 *)((u32)request->response + 5);
518 jz_mmc_receive_data(request);
521 if (events & MMC_EVENT_TX_DATA_DONE) {
522 jz_mmc_transmit_data(request);
525 /* Wait for Data Done */
526 while (!(REG_MSC_IREG & MSC_IREG_DATA_TRAN_DONE))
528 REG_MSC_IREG = MSC_IREG_DATA_TRAN_DONE; /* clear status */
531 /* Wait for Prog Done event */
532 if (events & MMC_EVENT_PROG_DONE) {
533 while (!(REG_MSC_IREG & MSC_IREG_PRG_DONE))
535 REG_MSC_IREG = MSC_IREG_PRG_DONE; /* clear status */
538 /* Command completed */
540 return MMC_NO_ERROR; /* return successfully */
543 int mmc_block_read(u8 *dst, ulong src, ulong len)
546 struct mmc_request request;
547 struct mmc_response_r1 r1;
553 mmc_simple_cmd(&request, MMC_SEND_STATUS, mmcinfo.rca, RESPONSE_R1);
554 retval = mmc_unpack_r1(&request, &r1, 0);
555 if (retval && (retval != MMC_ERROR_STATE_MISMATCH)) {
559 mmc_simple_cmd(&request, MMC_SET_BLOCKLEN, len, RESPONSE_R1);
560 if ((retval = mmc_unpack_r1(&request, &r1, 0))) {
567 mmc_send_cmd(&request, MMC_READ_SINGLE_BLOCK, src, 1,len, RESPONSE_R1, dst);
568 if ((retval = mmc_unpack_r1(&request, &r1, 0))) {
574 int mmc_block_write(ulong dst, uchar *src, int len)
579 int mmc_read(ulong src, uchar *dst, int size)
581 ulong end, part_start, part_end, part_len, aligned_start, aligned_end;
582 ulong mmc_block_size, mmc_block_address;
589 printf("MMC card is not ready\n");
593 mmc_block_size = MMC_BLOCK_SIZE;
594 mmc_block_address = ~(mmc_block_size - 1);
598 part_start = ~mmc_block_address & src;
599 part_end = ~mmc_block_address & end;
600 aligned_start = mmc_block_address & src;
601 aligned_end = mmc_block_address & end;
602 /* all block aligned accesses */
603 debug("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
604 src, (ulong)dst, end, part_start, part_end, aligned_start, aligned_end);
606 part_len = mmc_block_size - part_start;
607 debug("ps src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
608 src, (ulong)dst, end, part_start, part_end, aligned_start, aligned_end);
609 if ((mmc_block_read(mmc_buf, aligned_start, mmc_block_size)) < 0) {
612 memcpy(dst, mmc_buf+part_start, part_len);
616 debug("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
617 src, (ulong)dst, end, part_start, part_end, aligned_start, aligned_end);
618 for (; src < aligned_end; src += mmc_block_size, dst += mmc_block_size) {
619 debug("al src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
620 src, (ulong)dst, end, part_start, part_end, aligned_start, aligned_end);
622 if ((mmc_block_read((uchar *)(dst), src, mmc_block_size)) < 0) {
626 debug("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
627 src, (ulong)dst, end, part_start, part_end, aligned_start, aligned_end);
629 if (part_end && src < end) {
630 if ((mmc_block_read(mmc_buf, aligned_end, mmc_block_size)) < 0) {
633 memcpy(dst, mmc_buf, part_end);
639 int mmc_write(uchar *src, ulong dst, int size)
641 ulong end, part_start, part_end, part_len, aligned_start, aligned_end;
642 ulong mmc_block_size, mmc_block_address;
649 printf("MMC card is not ready\n");
653 mmc_block_size = MMC_BLOCK_SIZE;
654 mmc_block_address = ~(mmc_block_size - 1);
658 part_start = ~mmc_block_address & dst;
659 part_end = ~mmc_block_address & end;
660 aligned_start = mmc_block_address & dst;
661 aligned_end = mmc_block_address & end;
663 /* all block aligned accesses */
664 debug("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
665 src, (ulong)dst, end, part_start, part_end, aligned_start, aligned_end);
667 part_len = mmc_block_size - part_start;
668 debug("ps src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
669 (ulong)src, dst, end, part_start, part_end, aligned_start, aligned_end);
670 if ((mmc_block_read(mmc_buf, aligned_start, mmc_block_size)) < 0) {
673 memcpy(mmc_buf+part_start, src, part_len);
674 if ((mmc_block_write(aligned_start, mmc_buf, mmc_block_size)) < 0) {
680 debug("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
681 src, (ulong)dst, end, part_start, part_end, aligned_start, aligned_end);
682 for (; dst < aligned_end; src += mmc_block_size, dst += mmc_block_size) {
683 debug("al src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
684 src, (ulong)dst, end, part_start, part_end, aligned_start, aligned_end);
685 if ((mmc_block_write(dst, (uchar *)src, mmc_block_size)) < 0) {
689 debug("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
690 src, (ulong)dst, end, part_start, part_end, aligned_start, aligned_end);
691 if (part_end && dst < end) {
692 debug("pe src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
693 src, (ulong)dst, end, part_start, part_end, aligned_start, aligned_end);
694 if ((mmc_block_read(mmc_buf, aligned_end, mmc_block_size)) < 0) {
697 memcpy(mmc_buf, src, part_end);
698 if ((mmc_block_write(aligned_end, mmc_buf, mmc_block_size)) < 0) {
705 ulong mmc_bread(int dev_num, ulong blknr, ulong blkcnt, ulong *dst)
708 int mmc_block_size = MMC_BLOCK_SIZE;
710 src = blknr * mmc_block_size + CFG_MMC_BASE;
711 mmc_read(src, (uchar *)dst, blkcnt*mmc_block_size);
715 int mmc_select_card(void)
717 struct mmc_request request;
718 struct mmc_response_r1 r1;
721 mmc_simple_cmd(&request, MMC_SELECT_CARD, mmcinfo.rca, RESPONSE_R1B);
722 retval = mmc_unpack_r1(&request, &r1, 0);
728 mmc_simple_cmd(&request, MMC_APP_CMD, mmcinfo.rca, RESPONSE_R1);
729 retval = mmc_unpack_r1(&request,&r1,0);
733 mmc_simple_cmd(&request, SET_BUS_WIDTH, 2, RESPONSE_R1);
734 retval = mmc_unpack_r1(&request,&r1,0);
745 static void mmc_configure_card(void)
751 mmcinfo.block_num = (mmcinfo.csd.c_size + 1) << 10;
753 mmcinfo.block_num = (mmcinfo.csd.c_size + 1) * (1 << (mmcinfo.csd.c_size_mult + 2));
755 mmcinfo.block_len = 1 << mmcinfo.csd.read_bl_len;
757 /* Fix the clock rate */
758 rate = mmc_tran_speed(mmcinfo.csd.tran_speed);
759 if (rate < MMC_CLOCK_SLOW)
760 rate = MMC_CLOCK_SLOW;
761 if ((mmcinfo.sd == 0) && (rate > MMC_CLOCK_FAST))
762 rate = MMC_CLOCK_FAST;
763 if ((mmcinfo.sd) && (rate > SD_CLOCK_FAST))
764 rate = SD_CLOCK_FAST;
766 DEBUG(2,"mmc_configure_card: block_len=%d block_num=%d rate=%d\n", mmcinfo.block_len, mmcinfo.block_num, rate);
768 jz_mmc_set_clock(mmcinfo.sd, rate);
772 * State machine routines to initialize card(s)
776 CIM_SINGLE_CARD_ACQ (frequency at 400 kHz)
777 --- Must enter from GO_IDLE_STATE ---
778 1. SD_SEND_OP_COND (SD Card) [CMD55] + [CMD41]
779 2. SEND_OP_COND (Full Range) [CMD1] {optional}
780 3. SEND_OP_COND (Set Range ) [CMD1]
781 If busy, delay and repeat step 2
782 4. ALL_SEND_CID [CMD2]
783 If timeout, set an error (no cards found)
784 5. SET_RELATIVE_ADDR [CMD3]
786 7. SET_DSR [CMD4] Only call this if (csd.dsr_imp).
787 8. Set clock frequency (check available in csd.tran_speed)
790 #define MMC_INIT_DOING 0
791 #define MMC_INIT_PASSED 1
792 #define MMC_INIT_FAILED 2
794 static int mmc_init_card_state(struct mmc_request *request)
796 struct mmc_response_r1 r1;
797 struct mmc_response_r3 r3;
799 int ocr = 0x40300000;
802 DEBUG(2,"mmc_init_card_state\n");
804 switch (request->cmd) {
805 case MMC_GO_IDLE_STATE: /* No response to parse */
807 mmc_simple_cmd(request, 8, 0x1aa, RESPONSE_R1);
809 mmc_simple_cmd(request, MMC_SEND_OP_COND, MMC_OCR_ARG, RESPONSE_R3);
813 retval = mmc_unpack_r1(request,&r1,mmcinfo.state);
814 mmc_simple_cmd(request, MMC_APP_CMD, 0, RESPONSE_R1);
818 retval = mmc_unpack_r1(request,&r1,mmcinfo.state);
819 if (retval & (limit_41 < 100)) {
820 DEBUG(0, "mmc_init_card_state: unable to MMC_APP_CMD error=%d (%s)\n",
821 retval, mmc_result_to_string(retval));
823 mmc_simple_cmd(request, SD_SEND_OP_COND, ocr, RESPONSE_R3);
824 } else if (limit_41 < 100) {
826 mmc_simple_cmd(request, SD_SEND_OP_COND, ocr, RESPONSE_R3);
828 /* reset the card to idle*/
829 mmc_simple_cmd(request, MMC_GO_IDLE_STATE, 0, RESPONSE_NONE);
834 case SD_SEND_OP_COND:
835 retval = mmc_unpack_r3(request, &r3);
838 mmc_simple_cmd(request, MMC_SEND_OP_COND, MMC_OCR_ARG, RESPONSE_R3);
842 DEBUG(2,"mmc_init_card_state: read ocr value = 0x%08x\n", r3.ocr);
844 if(!(r3.ocr & MMC_CARD_BUSY || ocr == 0)){
846 mmc_simple_cmd(request, MMC_APP_CMD, 0, RESPONSE_R1);
849 /* Set the data bus width to 4 bits */
850 mmcinfo.sd = 1; /* SD Card ready */
851 mmcinfo.state = CARD_STATE_READY;
852 mmc_simple_cmd(request, MMC_ALL_SEND_CID, 0, RESPONSE_R2_CID);
856 case MMC_SEND_OP_COND:
857 retval = mmc_unpack_r3(request, &r3);
859 DEBUG(0,"mmc_init_card_state: failed SEND_OP_COND error=%d (%s)\n",
860 retval, mmc_result_to_string(retval));
861 return MMC_INIT_FAILED;
864 DEBUG(2,"mmc_init_card_state: read ocr value = 0x%08x\n", r3.ocr);
865 if (!(r3.ocr & MMC_CARD_BUSY)) {
866 mmc_simple_cmd(request, MMC_SEND_OP_COND, MMC_OCR_ARG, RESPONSE_R3);
869 mmcinfo.sd = 0; /* MMC Card ready */
870 mmcinfo.state = CARD_STATE_READY;
871 mmc_simple_cmd(request, MMC_ALL_SEND_CID, 0, RESPONSE_R2_CID);
875 case MMC_ALL_SEND_CID:
876 retval = mmc_unpack_cid( request, &mmcinfo.cid );
877 mmc_dev.if_type = IF_TYPE_MMC;
878 mmc_dev.part_type = PART_TYPE_DOS;
882 /* FIXME fill in the correct size (is set to 32MByte) */
884 mmc_dev.lba = 0x10000;
885 mmc_dev.removable = 0;
887 /*FIXME:ignore CRC error for CMD2/CMD9/CMD10 */
888 if ( retval && (retval != MMC_ERROR_CRC)) {
889 DEBUG(0,"mmc_init_card_state: unable to ALL_SEND_CID error=%d (%s)\n",
890 retval, mmc_result_to_string(retval));
891 return MMC_INIT_FAILED;
893 mmcinfo.state = CARD_STATE_IDENT;
895 mmc_simple_cmd(request, MMC_SET_RELATIVE_ADDR, 0, RESPONSE_R6);
897 mmc_simple_cmd(request, MMC_SET_RELATIVE_ADDR, ID_TO_RCA(mmcinfo.id) << 16, RESPONSE_R1);
900 case MMC_SET_RELATIVE_ADDR:
902 retval = mmc_unpack_r6(request, &r1, mmcinfo.state, &mmcinfo.rca);
903 mmcinfo.rca = mmcinfo.rca << 16;
904 DEBUG(2, "mmc_init_card_state: Get RCA from SD: 0x%04x Status: %x\n", mmcinfo.rca, r1.status);
906 retval = mmc_unpack_r1(request,&r1,mmcinfo.state);
907 mmcinfo.rca = ID_TO_RCA(mmcinfo.id) << 16;
910 DEBUG(0, "mmc_init_card_state: unable to SET_RELATIVE_ADDR error=%d (%s)\n",
911 retval, mmc_result_to_string(retval));
912 return MMC_INIT_FAILED;
915 mmcinfo.state = CARD_STATE_STBY;
916 mmc_simple_cmd(request, MMC_SEND_CSD, mmcinfo.rca, RESPONSE_R2_CSD);
921 retval = mmc_unpack_csd(request, &mmcinfo.csd);
922 mmc_csd_t *csd = (mmc_csd_t *)retval;
923 memcpy(&mmc_csd, csd, sizeof(csd));
926 printf("MMC card is ready\n");
927 /* FIXME add verbose printout for csd */
929 /*FIXME:ignore CRC error for CMD2/CMD9/CMD10 */
930 if (retval && (retval != MMC_ERROR_CRC)) {
931 DEBUG(0, "mmc_init_card_state: unable to SEND_CSD error=%d (%s)\n",
932 retval, mmc_result_to_string(retval));
933 return MMC_INIT_FAILED;
935 if (mmcinfo.csd.dsr_imp) {
936 DEBUG(0, "mmc_init_card_state: driver doesn't support setting DSR\n");
938 mmc_configure_card();
939 return MMC_INIT_PASSED;
942 DEBUG(0, "mmc_init_card_state: error! Illegal last cmd %d\n", request->cmd);
943 return MMC_INIT_FAILED;
946 return MMC_INIT_DOING;
949 int mmc_init_card(void)
951 struct mmc_request request;
954 mmc_simple_cmd(&request, MMC_CIM_RESET, 0, RESPONSE_NONE); /* reset card */
955 mmc_simple_cmd(&request, MMC_GO_IDLE_STATE, 0, RESPONSE_NONE);
956 mmcinfo.sd = 1; /* assuming a SD card */
958 while ((retval = mmc_init_card_state(&request)) == MMC_INIT_DOING)
961 if (retval == MMC_INIT_PASSED)
964 return MMC_NO_RESPONSE;
967 int mmc_legacy_init(int verbose)
969 if (!__msc_card_detected())
972 printf("MMC card found\n");
974 /* Step-1: init GPIO */
979 /* Step-2: turn on power of card */
980 __msc_enable_power();
982 /* Step-3: Reset MSC Controller. */
985 /* Step-3: mask all IRQs. */
988 /* Step-4: stop MMC/SD clock */
993 mmc_dev.block_read = mmc_bread;
994 fat_register_device(&mmc_dev,1); /* partitions start counting with 1 */
999 int mmc_ident(block_dev_desc_t *dev)
1004 int mmc2info(ulong addr)
1006 /* FIXME hard codes to 32 MB device */
1007 if (addr >= CFG_MMC_BASE && addr < CFG_MMC_BASE + 0x02000000) {
1013 * Debugging functions
1016 static char * mmc_result_strings[] = {
1019 "ERROR_OUT_OF_RANGE",
1023 "ERROR_ERASE_PARAM",
1024 "ERROR_WP_VIOLATION",
1025 "ERROR_CARD_IS_LOCKED",
1026 "ERROR_LOCK_UNLOCK_FAILED",
1028 "ERROR_ILLEGAL_COMMAND",
1029 "ERROR_CARD_ECC_FAILED",
1034 "ERROR_CID_CSD_OVERWRITE",
1035 "ERROR_STATE_MISMATCH",
1036 "ERROR_HEADER_MISMATCH",
1039 "ERROR_DRIVER_FAILURE",
1042 char * mmc_result_to_string(int i)
1044 return mmc_result_strings[i+1];
1047 static char * card_state_strings[] = {
1060 static inline char * card_state_to_string(int i)
1062 return card_state_strings[i+1];
1069 #define PARSE_U32(_buf,_index) \
1070 (((u32)_buf[_index]) << 24) | (((u32)_buf[_index+1]) << 16) | \
1071 (((u32)_buf[_index+2]) << 8) | ((u32)_buf[_index+3]);
1073 #define PARSE_U16(_buf,_index) \
1074 (((u16)_buf[_index]) << 8) | ((u16)_buf[_index+1]);
1076 int mmc_unpack_csd(struct mmc_request *request, struct mmc_csd *csd)
1078 u8 *buf = request->response;
1081 if (request->result)
1082 return request->result;
1084 csd->csd_structure = (buf[1] & 0xc0) >> 6;
1085 if (csd->csd_structure)
1090 switch (csd->csd_structure) {
1094 csd->tran_speed = buf[4];
1095 csd->ccc = (((u16)buf[5]) << 4) | ((buf[6] & 0xf0) >> 4);
1096 csd->read_bl_len = buf[6] & 0x0f;
1097 /* for support 2GB card*/
1098 if (csd->read_bl_len >= 10)
1100 num = csd->read_bl_len - 9;
1101 csd->read_bl_len = 9;
1104 csd->read_bl_partial = (buf[7] & 0x80) ? 1 : 0;
1105 csd->write_blk_misalign = (buf[7] & 0x40) ? 1 : 0;
1106 csd->read_blk_misalign = (buf[7] & 0x20) ? 1 : 0;
1107 csd->dsr_imp = (buf[7] & 0x10) ? 1 : 0;
1108 csd->c_size = ((((u16)buf[7]) & 0x03) << 10) | (((u16)buf[8]) << 2) | (((u16)buf[9]) & 0xc0) >> 6;
1111 csd->c_size = csd->c_size << num;
1114 csd->vdd_r_curr_min = (buf[9] & 0x38) >> 3;
1115 csd->vdd_r_curr_max = buf[9] & 0x07;
1116 csd->vdd_w_curr_min = (buf[10] & 0xe0) >> 5;
1117 csd->vdd_w_curr_max = (buf[10] & 0x1c) >> 2;
1118 csd->c_size_mult = ((buf[10] & 0x03) << 1) | ((buf[11] & 0x80) >> 7);
1119 switch (csd->csd_structure) {
1120 case CSD_STRUCT_VER_1_0:
1121 case CSD_STRUCT_VER_1_1:
1122 csd->erase.v22.sector_size = (buf[11] & 0x7c) >> 2;
1123 csd->erase.v22.erase_grp_size = ((buf[11] & 0x03) << 3) | ((buf[12] & 0xe0) >> 5);
1126 case CSD_STRUCT_VER_1_2:
1128 csd->erase.v31.erase_grp_size = (buf[11] & 0x7c) >> 2;
1129 csd->erase.v31.erase_grp_mult = ((buf[11] & 0x03) << 3) | ((buf[12] & 0xe0) >> 5);
1132 csd->wp_grp_size = buf[12] & 0x1f;
1133 csd->wp_grp_enable = (buf[13] & 0x80) ? 1 : 0;
1134 csd->default_ecc = (buf[13] & 0x60) >> 5;
1135 csd->r2w_factor = (buf[13] & 0x1c) >> 2;
1136 csd->write_bl_len = ((buf[13] & 0x03) << 2) | ((buf[14] & 0xc0) >> 6);
1137 if (csd->write_bl_len >= 10)
1138 csd->write_bl_len = 9;
1140 csd->write_bl_partial = (buf[14] & 0x20) ? 1 : 0;
1141 csd->file_format_grp = (buf[15] & 0x80) ? 1 : 0;
1142 csd->copy = (buf[15] & 0x40) ? 1 : 0;
1143 csd->perm_write_protect = (buf[15] & 0x20) ? 1 : 0;
1144 csd->tmp_write_protect = (buf[15] & 0x10) ? 1 : 0;
1145 csd->file_format = (buf[15] & 0x0c) >> 2;
1146 csd->ecc = buf[15] & 0x03;
1148 DEBUG(2," csd_structure=%d spec_vers=%d taac=%02x nsac=%02x tran_speed=%02x\n"
1149 " ccc=%04x read_bl_len=%d read_bl_partial=%d write_blk_misalign=%d\n"
1150 " read_blk_misalign=%d dsr_imp=%d c_size=%d vdd_r_curr_min=%d\n"
1151 " vdd_r_curr_max=%d vdd_w_curr_min=%d vdd_w_curr_max=%d c_size_mult=%d\n"
1152 " wp_grp_size=%d wp_grp_enable=%d default_ecc=%d r2w_factor=%d\n"
1153 " write_bl_len=%d write_bl_partial=%d file_format_grp=%d copy=%d\n"
1154 " perm_write_protect=%d tmp_write_protect=%d file_format=%d ecc=%d\n",
1155 csd->csd_structure, csd->spec_vers,
1156 csd->taac, csd->nsac, csd->tran_speed,
1157 csd->ccc, csd->read_bl_len,
1158 csd->read_bl_partial, csd->write_blk_misalign,
1159 csd->read_blk_misalign, csd->dsr_imp,
1160 csd->c_size, csd->vdd_r_curr_min,
1161 csd->vdd_r_curr_max, csd->vdd_w_curr_min,
1162 csd->vdd_w_curr_max, csd->c_size_mult,
1163 csd->wp_grp_size, csd->wp_grp_enable,
1164 csd->default_ecc, csd->r2w_factor,
1165 csd->write_bl_len, csd->write_bl_partial,
1166 csd->file_format_grp, csd->copy,
1167 csd->perm_write_protect, csd->tmp_write_protect,
1168 csd->file_format, csd->ecc);
1169 switch (csd->csd_structure) {
1170 case CSD_STRUCT_VER_1_0:
1171 case CSD_STRUCT_VER_1_1:
1172 DEBUG(2," V22 sector_size=%d erase_grp_size=%d\n",
1173 csd->erase.v22.sector_size,
1174 csd->erase.v22.erase_grp_size);
1176 case CSD_STRUCT_VER_1_2:
1178 DEBUG(2," V31 erase_grp_size=%d erase_grp_mult=%d\n",
1179 csd->erase.v31.erase_grp_size,
1180 csd->erase.v31.erase_grp_mult);
1189 csd->tran_speed = buf[4];
1190 csd->ccc = (((u16)buf[5]) << 4) | ((buf[6] & 0xf0) >> 4);
1192 csd->read_bl_len = 9;
1193 csd->read_bl_partial = 0;
1194 csd->write_blk_misalign = 0;
1195 csd->read_blk_misalign = 0;
1196 csd->dsr_imp = (buf[7] & 0x10) ? 1 : 0;
1197 csd->c_size = ((((u16)buf[8]) & 0x3f) << 16) | (((u16)buf[9]) << 8) | ((u16)buf[10]) ;
1198 switch (csd->csd_structure) {
1199 case CSD_STRUCT_VER_1_0:
1200 case CSD_STRUCT_VER_1_1:
1201 csd->erase.v22.sector_size = 0x7f;
1202 csd->erase.v22.erase_grp_size = 0;
1204 case CSD_STRUCT_VER_1_2:
1206 csd->erase.v31.erase_grp_size = 0x7f;
1207 csd->erase.v31.erase_grp_mult = 0;
1210 csd->wp_grp_size = 0;
1211 csd->wp_grp_enable = 0;
1212 csd->default_ecc = (buf[13] & 0x60) >> 5;
1213 csd->r2w_factor = 4;/* Unused */
1214 csd->write_bl_len = 9;
1216 csd->write_bl_partial = 0;
1217 csd->file_format_grp = 0;
1218 csd->copy = (buf[15] & 0x40) ? 1 : 0;
1219 csd->perm_write_protect = (buf[15] & 0x20) ? 1 : 0;
1220 csd->tmp_write_protect = (buf[15] & 0x10) ? 1 : 0;
1221 csd->file_format = 0;
1222 csd->ecc = buf[15] & 0x03;
1224 DEBUG(2," csd_structure=%d spec_vers=%d taac=%02x nsac=%02x tran_speed=%02x\n"
1225 " ccc=%04x read_bl_len=%d read_bl_partial=%d write_blk_misalign=%d\n"
1226 " read_blk_misalign=%d dsr_imp=%d c_size=%d vdd_r_curr_min=%d\n"
1227 " vdd_r_curr_max=%d vdd_w_curr_min=%d vdd_w_curr_max=%d c_size_mult=%d\n"
1228 " wp_grp_size=%d wp_grp_enable=%d default_ecc=%d r2w_factor=%d\n"
1229 " write_bl_len=%d write_bl_partial=%d file_format_grp=%d copy=%d\n"
1230 " perm_write_protect=%d tmp_write_protect=%d file_format=%d ecc=%d\n",
1231 csd->csd_structure, csd->spec_vers,
1232 csd->taac, csd->nsac, csd->tran_speed,
1233 csd->ccc, csd->read_bl_len,
1234 csd->read_bl_partial, csd->write_blk_misalign,
1235 csd->read_blk_misalign, csd->dsr_imp,
1236 csd->c_size, csd->vdd_r_curr_min,
1237 csd->vdd_r_curr_max, csd->vdd_w_curr_min,
1238 csd->vdd_w_curr_max, csd->c_size_mult,
1239 csd->wp_grp_size, csd->wp_grp_enable,
1240 csd->default_ecc, csd->r2w_factor,
1241 csd->write_bl_len, csd->write_bl_partial,
1242 csd->file_format_grp, csd->copy,
1243 csd->perm_write_protect, csd->tmp_write_protect,
1244 csd->file_format, csd->ecc);
1245 switch (csd->csd_structure) {
1246 case CSD_STRUCT_VER_1_0:
1247 case CSD_STRUCT_VER_1_1:
1248 DEBUG(2," V22 sector_size=%d erase_grp_size=%d\n",
1249 csd->erase.v22.sector_size,
1250 csd->erase.v22.erase_grp_size);
1252 case CSD_STRUCT_VER_1_2:
1254 DEBUG(2," V31 erase_grp_size=%d erase_grp_mult=%d\n",
1255 csd->erase.v31.erase_grp_size,
1256 csd->erase.v31.erase_grp_mult);
1261 if (buf[0] != 0x3f) return MMC_ERROR_HEADER_MISMATCH;
1266 int mmc_unpack_r1(struct mmc_request *request, struct mmc_response_r1 *r1, enum card_state state)
1268 u8 *buf = request->response;
1270 if (request->result) return request->result;
1273 r1->status = PARSE_U32(buf,1);
1275 DEBUG(2, "mmc_unpack_r1: cmd=%d status=%08x\n", r1->cmd, r1->status);
1277 if (R1_STATUS(r1->status)) {
1278 if (r1->status & R1_OUT_OF_RANGE) return MMC_ERROR_OUT_OF_RANGE;
1279 if (r1->status & R1_ADDRESS_ERROR) return MMC_ERROR_ADDRESS;
1280 if (r1->status & R1_BLOCK_LEN_ERROR) return MMC_ERROR_BLOCK_LEN;
1281 if (r1->status & R1_ERASE_SEQ_ERROR) return MMC_ERROR_ERASE_SEQ;
1282 if (r1->status & R1_ERASE_PARAM) return MMC_ERROR_ERASE_PARAM;
1283 if (r1->status & R1_WP_VIOLATION) return MMC_ERROR_WP_VIOLATION;
1284 /*if (r1->status & R1_CARD_IS_LOCKED) return MMC_ERROR_CARD_IS_LOCKED; */
1285 if (r1->status & R1_LOCK_UNLOCK_FAILED) return MMC_ERROR_LOCK_UNLOCK_FAILED;
1286 if (r1->status & R1_COM_CRC_ERROR) return MMC_ERROR_COM_CRC;
1287 if (r1->status & R1_ILLEGAL_COMMAND) return MMC_ERROR_ILLEGAL_COMMAND;
1288 if (r1->status & R1_CARD_ECC_FAILED) return MMC_ERROR_CARD_ECC_FAILED;
1289 if (r1->status & R1_CC_ERROR) return MMC_ERROR_CC;
1290 if (r1->status & R1_ERROR) return MMC_ERROR_GENERAL;
1291 if (r1->status & R1_UNDERRUN) return MMC_ERROR_UNDERRUN;
1292 if (r1->status & R1_OVERRUN) return MMC_ERROR_OVERRUN;
1293 if (r1->status & R1_CID_CSD_OVERWRITE) return MMC_ERROR_CID_CSD_OVERWRITE;
1296 if (buf[0] != request->cmd) return MMC_ERROR_HEADER_MISMATCH;
1298 /* This should be last - it's the least dangerous error */
1303 int mmc_unpack_scr(struct mmc_request *request, struct mmc_response_r1 *r1, enum card_state state, u32 *scr)
1305 u8 *buf = request->response;
1306 if (request->result) return request->result;
1308 *scr = PARSE_U32(buf, 5); /* Save SCR returned by the SD Card */
1309 return mmc_unpack_r1(request, r1, state);
1313 int mmc_unpack_r6(struct mmc_request *request, struct mmc_response_r1 *r1, enum card_state state, int *rca)
1315 u8 *buf = request->response;
1317 if (request->result) return request->result;
1319 *rca = PARSE_U16(buf,1); /* Save RCA returned by the SD Card */
1324 return mmc_unpack_r1(request, r1, state);
1327 int mmc_unpack_cid(struct mmc_request *request, struct mmc_cid *cid)
1329 u8 *buf = request->response;
1332 if (request->result) return request->result;
1335 cid->oid = PARSE_U16(buf,2);
1336 for (i = 0 ; i < 6 ; i++)
1337 cid->pnm[i] = buf[4+i];
1340 cid->psn = PARSE_U32(buf,11);
1343 DEBUG(2,"mmc_unpack_cid: mid=%d oid=%d pnm=%s prv=%d.%d psn=%08x mdt=%d/%d\n",
1344 cid->mid, cid->oid, cid->pnm,
1345 (cid->prv>>4), (cid->prv&0xf),
1346 cid->psn, (cid->mdt>>4), (cid->mdt&0xf)+1997);
1348 if (buf[0] != 0x3f) return MMC_ERROR_HEADER_MISMATCH;
1352 int mmc_unpack_r3(struct mmc_request *request, struct mmc_response_r3 *r3)
1354 u8 *buf = request->response;
1356 if (request->result) return request->result;
1358 r3->ocr = PARSE_U32(buf,1);
1359 DEBUG(2,"mmc_unpack_r3: ocr=%08x\n", r3->ocr);
1361 if (buf[0] != 0x3f) return MMC_ERROR_HEADER_MISMATCH;
1368 static u32 ts_exp[] = { 100*KBPS, 1*MBPS, 10*MBPS, 100*MBPS, 0, 0, 0, 0 };
1369 static u32 ts_mul[] = { 0, 1000, 1200, 1300, 1500, 2000, 2500, 3000,
1370 3500, 4000, 4500, 5000, 5500, 6000, 7000, 8000 };
1372 u32 mmc_tran_speed(u8 ts)
1374 u32 rate = ts_exp[(ts & 0x7)] * ts_mul[(ts & 0x78) >> 3];
1377 DEBUG(0, "mmc_tran_speed: error - unrecognized speed 0x%02x\n", ts);
1384 void mmc_send_cmd(struct mmc_request *request, int cmd, u32 arg,
1385 u16 nob, u16 block_len, enum mmc_rsp_t rtype, u8 *buffer)
1389 request->rtype = rtype;
1391 request->block_len = block_len;
1392 request->buffer = buffer;
1393 request->cnt = nob * block_len;
1395 jz_mmc_exec_cmd(request);
1398 #endif /* CONFIG_MMC */
1399 #endif /* CONFIG_JZ4740 */