1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2008 by Maurus Cuelenaere
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
25 #include "ata-sd-target.h"
36 static struct wakeup sd_wakeup
;
37 static long last_disk_activity
= -1;
38 static tCardInfo card
;
40 //#define SD_DMA_ENABLE
41 #define SD_DMA_INTERRUPT 0
43 #define DEBUG(x...) logf(x)
45 #define SD_INSERT_STATUS() __gpio_get_pin(MMC_CD_PIN)
46 #define SD_RESET() __msc_reset()
48 #define SD_IRQ_MASK() \
50 REG_MSC_IMASK = 0xffff; \
51 REG_MSC_IREG = 0xffff; \
59 SD_ERROR_OUT_OF_RANGE
,
64 SD_ERROR_WP_VIOLATION
,
65 SD_ERROR_CARD_IS_LOCKED
,
66 SD_ERROR_LOCK_UNLOCK_FAILED
,
68 SD_ERROR_ILLEGAL_COMMAND
,
69 SD_ERROR_CARD_ECC_FAILED
,
74 SD_ERROR_CID_CSD_OVERWRITE
,
75 SD_ERROR_STATE_MISMATCH
,
76 SD_ERROR_HEADER_MISMATCH
,
79 SD_ERROR_DRIVER_FAILURE
,
82 /* Standard MMC/SD clock speeds */
83 #define MMC_CLOCK_SLOW 400000 /* 400 kHz for initial setup */
84 #define SD_CLOCK_FAST 24000000 /* 24 MHz for SD Cards */
85 #define SD_CLOCK_HIGH 48000000 /* 48 MHz for SD Cards */
87 /* Extra commands for state control */
88 /* Use negative numbers to disambiguate */
89 #define SD_CIM_RESET -1
91 /* Proprietary commands, illegal/reserved according to SD Specification 2.00 */
93 #define SD_READ_DAT_UNTIL_STOP 11 /* adtc [31:0] dadr R1 */
96 #define SD_WRITE_DAT_UNTIL_STOP 20 /* adtc [31:0] data addr R1 */
99 #define SD_PROGRAM_CID 26 /* adtc R1 */
100 #define SD_PROGRAM_CSD 27 /* adtc R1 */
103 #define SD_GO_IRQ_STATE 40 /* bcr R5 */
105 /* Don't change the order of these; they are used in dispatch tables */
126 r : detected and set for the actual command response
127 x : detected and set during command execution. the host must poll
128 the card by sending status command in order to read these bits.
130 a : according to the card state
131 b : always related to the previous command. Reception of
132 a valid command will clear it (with a delay of one command)
136 #define R1_OUT_OF_RANGE (1 << 31) /* er, c */
137 #define R1_ADDRESS_ERROR (1 << 30) /* erx, c */
138 #define R1_BLOCK_LEN_ERROR (1 << 29) /* er, c */
139 #define R1_ERASE_SEQ_ERROR (1 << 28) /* er, c */
140 #define R1_ERASE_PARAM (1 << 27) /* ex, c */
141 #define R1_WP_VIOLATION (1 << 26) /* erx, c */
142 #define R1_CARD_IS_LOCKED (1 << 25) /* sx, a */
143 #define R1_LOCK_UNLOCK_FAILED (1 << 24) /* erx, c */
144 #define R1_COM_CRC_ERROR (1 << 23) /* er, b */
145 #define R1_ILLEGAL_COMMAND (1 << 22) /* er, b */
146 #define R1_CARD_ECC_FAILED (1 << 21) /* ex, c */
147 #define R1_CC_ERROR (1 << 20) /* erx, c */
148 #define R1_ERROR (1 << 19) /* erx, c */
149 #define R1_UNDERRUN (1 << 18) /* ex, c */
150 #define R1_OVERRUN (1 << 17) /* ex, c */
151 #define R1_CID_CSD_OVERWRITE (1 << 16) /* erx, c, CID/CSD overwrite */
152 #define R1_WP_ERASE_SKIP (1 << 15) /* sx, c */
153 #define R1_CARD_ECC_DISABLED (1 << 14) /* sx, a */
154 #define R1_ERASE_RESET (1 << 13) /* sr, c */
155 #define R1_STATUS(x) (x & 0xFFFFE000)
156 #define R1_CURRENT_STATE(x) ((x & 0x00001E00) >> 9) /* sx, b (4 bits) */
157 #define R1_READY_FOR_DATA (1 << 8) /* sx, a */
158 #define R1_APP_CMD (1 << 7) /* sr, c */
160 /* These are unpacked versions of the actual responses */
161 struct sd_response_r1
167 struct sd_response_r3
172 #define SD_CARD_BUSY 0x80000000 /* Card Power up status bit */
176 int index
; /* Slot index - used for CS lines */
177 int cmd
; /* Command to send */
178 unsigned int arg
; /* Argument to send */
179 enum sd_rsp_t rtype
; /* Response type expected */
181 /* Data transfer (these may be modified at the low level) */
182 unsigned short nob
; /* Number of blocks to transfer*/
183 unsigned short block_len
; /* Block length */
184 unsigned char *buffer
; /* Data buffer */
185 unsigned int cnt
; /* Data length, for PIO */
188 unsigned char response
[18]; /* Buffer to store response - CRC is optional */
189 enum sd_result_t result
;
192 #define SD_OCR_ARG 0x00ff8000 /* Argument of OCR */
194 /***********************************************************************
197 #define SD_EVENT_NONE 0x00 /* No events */
198 #define SD_EVENT_RX_DATA_DONE 0x01 /* Rx data done */
199 #define SD_EVENT_TX_DATA_DONE 0x02 /* Tx data done */
200 #define SD_EVENT_PROG_DONE 0x04 /* Programming is done */
202 static int use_4bit
= 1; /* Use 4-bit data bus */
203 static int num_6
= 0;
204 static int sd2_0
= 0;
206 /**************************************************************************
208 **************************************************************************/
210 #define PARSE_U32(_buf,_index) \
211 (((unsigned int)_buf[_index]) << 24) | (((unsigned int)_buf[_index+1]) << 16) | \
212 (((unsigned int)_buf[_index+2]) << 8) | ((unsigned int)_buf[_index+3]);
214 #define PARSE_U16(_buf,_index) \
215 (((unsigned short)_buf[_index]) << 8) | ((unsigned short)_buf[_index+1]);
217 int sd_unpack_r1(struct sd_request
*request
, struct sd_response_r1
*r1
)
219 unsigned char *buf
= request
->response
;
222 return request
->result
;
225 r1
->status
= PARSE_U32(buf
,1);
227 DEBUG("sd_unpack_r1: cmd=%d status=%08x", r1
->cmd
, r1
->status
);
229 if (R1_STATUS(r1
->status
)) {
230 if (r1
->status
& R1_OUT_OF_RANGE
) return SD_ERROR_OUT_OF_RANGE
;
231 if (r1
->status
& R1_ADDRESS_ERROR
) return SD_ERROR_ADDRESS
;
232 if (r1
->status
& R1_BLOCK_LEN_ERROR
) return SD_ERROR_BLOCK_LEN
;
233 if (r1
->status
& R1_ERASE_SEQ_ERROR
) return SD_ERROR_ERASE_SEQ
;
234 if (r1
->status
& R1_ERASE_PARAM
) return SD_ERROR_ERASE_PARAM
;
235 if (r1
->status
& R1_WP_VIOLATION
) return SD_ERROR_WP_VIOLATION
;
236 //if (r1->status & R1_CARD_IS_LOCKED) return SD_ERROR_CARD_IS_LOCKED;
237 if (r1
->status
& R1_LOCK_UNLOCK_FAILED
) return SD_ERROR_LOCK_UNLOCK_FAILED
;
238 if (r1
->status
& R1_COM_CRC_ERROR
) return SD_ERROR_COM_CRC
;
239 if (r1
->status
& R1_ILLEGAL_COMMAND
) return SD_ERROR_ILLEGAL_COMMAND
;
240 if (r1
->status
& R1_CARD_ECC_FAILED
) return SD_ERROR_CARD_ECC_FAILED
;
241 if (r1
->status
& R1_CC_ERROR
) return SD_ERROR_CC
;
242 if (r1
->status
& R1_ERROR
) return SD_ERROR_GENERAL
;
243 if (r1
->status
& R1_UNDERRUN
) return SD_ERROR_UNDERRUN
;
244 if (r1
->status
& R1_OVERRUN
) return SD_ERROR_OVERRUN
;
245 if (r1
->status
& R1_CID_CSD_OVERWRITE
) return SD_ERROR_CID_CSD_OVERWRITE
;
248 if (buf
[0] != request
->cmd
)
249 return SD_ERROR_HEADER_MISMATCH
;
251 /* This should be last - it's the least dangerous error */
256 int sd_unpack_scr(struct sd_request
*request
, struct sd_response_r1
*r1
, unsigned int *scr
)
258 unsigned char *buf
= request
->response
;
260 return request
->result
;
262 *scr
= PARSE_U32(buf
, 5); /* Save SCR returned by the SD Card */
263 return sd_unpack_r1(request
, r1
);
266 static inline int sd_unpack_r6(struct sd_request
*request
, struct sd_response_r1
*r1
, unsigned long *rca
)
268 unsigned char *buf
= request
->response
;
271 return request
->result
;
273 *rca
= PARSE_U16(buf
,1); /* Save RCA returned by the SD Card */
278 return sd_unpack_r1(request
, r1
);
281 int sd_unpack_r3(struct sd_request
*request
, struct sd_response_r3
*r3
)
283 unsigned char *buf
= request
->response
;
285 if (request
->result
) return request
->result
;
287 r3
->ocr
= PARSE_U32(buf
,1);
288 DEBUG("sd_unpack_r3: ocr=%08x", r3
->ocr
);
290 if (buf
[0] != 0x3f) return SD_ERROR_HEADER_MISMATCH
;
294 /* Stop the MMC clock and wait while it happens */
295 static inline int jz_sd_stop_clock(void)
297 register int timeout
= 1000;
299 //DEBUG("stop MMC clock");
300 REG_MSC_STRPCL
= MSC_STRPCL_CLOCK_CONTROL_STOP
;
302 while (timeout
&& (REG_MSC_STAT
& MSC_STAT_CLK_EN
))
307 DEBUG("Timeout on stop clock waiting");
308 return SD_ERROR_TIMEOUT
;
312 //DEBUG("clock off time is %d microsec", timeout);
316 /* Start the MMC clock and operation */
317 static inline int jz_sd_start_clock(void)
319 REG_MSC_STRPCL
= MSC_STRPCL_CLOCK_CONTROL_START
| MSC_STRPCL_START_OP
;
323 static int jz_sd_check_status(struct sd_request
*request
)
326 unsigned int status
= REG_MSC_STAT
;
328 /* Checking for response or data timeout */
329 if (status
& (MSC_STAT_TIME_OUT_RES
| MSC_STAT_TIME_OUT_READ
))
331 DEBUG("SD timeout, MSC_STAT 0x%x CMD %d", status
,
333 return SD_ERROR_TIMEOUT
;
336 /* Checking for CRC error */
338 (MSC_STAT_CRC_READ_ERROR
| MSC_STAT_CRC_WRITE_ERROR
|
339 MSC_STAT_CRC_RES_ERR
))
341 DEBUG("SD CRC error, MSC_STAT 0x%x", status
);
347 /* Checking for FIFO empty */
348 /*if(status & MSC_STAT_DATA_FIFO_EMPTY && request->rtype != RESPONSE_NONE)
350 DEBUG("SD FIFO empty, MSC_STAT 0x%x", status);
351 return SD_ERROR_UNDERRUN;
357 /* Obtain response to the command and store it to response buffer */
358 static void jz_sd_get_response(struct sd_request
*request
)
364 DEBUG("fetch response for request %d, cmd %d", request
->rtype
,
366 buf
= request
->response
;
367 request
->result
= SD_NO_ERROR
;
369 switch (request
->rtype
)
380 buf
[0] = (data
>> 8) & 0xff;
381 buf
[1] = data
& 0xff;
383 buf
[2] = (data
>> 8) & 0xff;
384 buf
[3] = data
& 0xff;
386 buf
[4] = data
& 0xff;
388 DEBUG("request %d, response [%02x %02x %02x %02x %02x]",
389 request
->rtype
, buf
[0], buf
[1], buf
[2],
393 case RESPONSE_R2_CID
:
394 case RESPONSE_R2_CSD
:
396 for (i
= 0; i
< 16; i
+= 2)
399 buf
[i
] = (data
>> 8) & 0xff;
400 buf
[i
+ 1] = data
& 0xff;
402 DEBUG("request %d, response []", request
->rtype
);
406 DEBUG("No response");
410 DEBUG("unhandled response type for request %d",
417 static void jz_sd_receive_data_dma(struct sd_request
*req
)
419 unsigned int size
= req
->block_len
* req
->nob
;
420 #if MMC_DMA_INTERRUPT
421 unsigned char err
= 0;
425 //dma_cache_wback_inv((unsigned long) req->buffer, size);
426 /* setup dma channel */
427 REG_DMAC_DSAR(DMA_SD_RX_CHANNEL
) = PHYSADDR(MSC_RXFIFO
); /* DMA source addr */
428 REG_DMAC_DTAR(DMA_SD_RX_CHANNEL
) = PHYSADDR((unsigned long) req
->buffer
); /* DMA dest addr */
429 REG_DMAC_DTCR(DMA_SD_RX_CHANNEL
) = (size
+ 3) / 4; /* DMA transfer count */
430 REG_DMAC_DRSR(DMA_SD_RX_CHANNEL
) = DMAC_DRSR_RS_MSCIN
; /* DMA request type */
433 REG_DMAC_DCMD(DMA_SD_RX_CHANNEL
) =
434 DMAC_DCMD_DAI
| DMAC_DCMD_SWDH_32
| DMAC_DCMD_DWDH_32
|
435 DMAC_DCMD_DS_32BIT
| DMAC_DCMD_TIE
;
436 REG_DMAC_DCCSR(DMA_SD_RX_CHANNEL
) = DMAC_DCCSR_EN
| DMAC_DCCSR_NDES
;
437 OSSemPend(sd_dma_rx_sem
, 100, &err
);
439 REG_DMAC_DCMD(DMA_SD_RX_CHANNEL
) =
440 DMAC_DCMD_DAI
| DMAC_DCMD_SWDH_32
| DMAC_DCMD_DWDH_32
|
442 REG_DMAC_DCCSR(DMA_SD_RX_CHANNEL
) = DMAC_DCCSR_EN
| DMAC_DCCSR_NDES
;
444 //while (REG_DMAC_DTCR(DMA_SD_RX_CHANNEL));
445 while( !(REG_DMAC_DCCSR(DMA_SD_RX_CHANNEL
) & DMAC_DCCSR_TT
) );
448 /* clear status and disable channel */
449 REG_DMAC_DCCSR(DMA_SD_RX_CHANNEL
) = 0;
452 static void jz_mmc_transmit_data_dma(struct mmc_request
*req
)
454 unsigned int size
= req
->block_len
* req
->nob
;
456 unsigned char err
= 0;
460 //dma_cache_wback_inv((unsigned long) req->buffer, size);
461 /* setup dma channel */
462 REG_DMAC_DSAR(DMA_SD_TX_CHANNEL
) = PHYSADDR((unsigned long) req
->buffer
); /* DMA source addr */
463 REG_DMAC_DTAR(DMA_SD_TX_CHANNEL
) = PHYSADDR(MSC_TXFIFO
); /* DMA dest addr */
464 REG_DMAC_DTCR(DMA_SD_TX_CHANNEL
) = (size
+ 3) / 4; /* DMA transfer count */
465 REG_DMAC_DRSR(DMA_SD_TX_CHANNEL
) = DMAC_DRSR_RS_MSCOUT
; /* DMA request type */
468 REG_DMAC_DCMD(DMA_SD_TX_CHANNEL
) =
469 DMAC_DCMD_SAI
| DMAC_DCMD_SWDH_32
| DMAC_DCMD_DWDH_32
|
470 DMAC_DCMD_DS_32BIT
| DMAC_DCMD_TIE
;
471 REG_DMAC_DCCSR(DMA_SD_TX_CHANNEL
) = DMAC_DCCSR_EN
| DMAC_DCCSR_NDES
;
472 OSSemPend(sd_dma_tx_sem
, 100, &err
);
474 REG_DMAC_DCMD(DMA_SD_TX_CHANNEL
) =
475 DMAC_DCMD_SAI
| DMAC_DCMD_SWDH_32
| DMAC_DCMD_DWDH_32
|
477 REG_DMAC_DCCSR(DMA_SD_TX_CHANNEL
) = DMAC_DCCSR_EN
| DMAC_DCCSR_NDES
;
478 /* wait for dma completion */
479 while( !(REG_DMAC_DCCSR(DMA_SD_TX_CHANNEL
) & DMAC_DCCSR_TT
) );
481 /* clear status and disable channel */
483 REG_DMAC_DCCSR(DMA_SD_TX_CHANNEL
) = 0;
486 #else /* SD_DMA_ENABLE */
488 static int jz_sd_receive_data(struct sd_request
*req
)
490 unsigned int nob
= req
->nob
;
491 unsigned int wblocklen
= (unsigned int) (req
->block_len
+ 3) >> 2; /* length in word */
492 unsigned char *buf
= req
->buffer
;
493 unsigned int *wbuf
= (unsigned int *) buf
;
494 unsigned int waligned
= (((unsigned int) buf
& 0x3) == 0); /* word aligned ? */
495 unsigned int stat
, timeout
, data
, cnt
;
497 for (; nob
>= 1; nob
--)
506 if (stat
& MSC_STAT_TIME_OUT_READ
)
507 return SD_ERROR_TIMEOUT
;
508 else if (stat
& MSC_STAT_CRC_READ_ERROR
)
510 else if (!(stat
& MSC_STAT_DATA_FIFO_EMPTY
)
511 || (stat
& MSC_STAT_DATA_FIFO_AFULL
))
512 /* Ready to read data */
519 return SD_ERROR_TIMEOUT
;
521 /* Read data from RXFIFO. It could be FULL or PARTIAL FULL */
522 DEBUG("Receive Data = %d", wblocklen
);
526 data
= REG_MSC_RXFIFO
;
531 *buf
++ = (unsigned char) (data
>> 0);
532 *buf
++ = (unsigned char) (data
>> 8);
533 *buf
++ = (unsigned char) (data
>> 16);
534 *buf
++ = (unsigned char) (data
>> 24);
539 MSC_STAT_DATA_FIFO_EMPTY
));
546 static int jz_sd_transmit_data(struct sd_request
*req
)
548 unsigned int nob
= req
->nob
;
549 unsigned int wblocklen
= (unsigned int) (req
->block_len
+ 3) >> 2; /* length in word */
550 unsigned char *buf
= req
->buffer
;
551 unsigned int *wbuf
= (unsigned int *) buf
;
552 unsigned int waligned
= (((unsigned int) buf
& 0x3) == 0); /* word aligned ? */
553 unsigned int stat
, timeout
, data
, cnt
;
555 for (; nob
>= 1; nob
--)
565 (MSC_STAT_CRC_WRITE_ERROR
|
566 MSC_STAT_CRC_WRITE_ERROR_NOSTS
))
568 else if (!(stat
& MSC_STAT_DATA_FIFO_FULL
))
569 /* Ready to write data */
576 return SD_ERROR_TIMEOUT
;
578 /* Write data to TXFIFO */
582 while (REG_MSC_STAT
& MSC_STAT_DATA_FIFO_FULL
);
585 REG_MSC_TXFIFO
= *wbuf
++;
590 data
|= *buf
++ << 16;
591 data
|= *buf
++ << 24;
592 REG_MSC_TXFIFO
= data
;
603 static inline unsigned int jz_sd_calc_clkrt(unsigned int rate
)
606 unsigned int clk_src
= sd2_0
? SD_CLOCK_HIGH
: SD_CLOCK_FAST
;
609 while (rate
< clk_src
)
617 static inline void cpm_select_msc_clk(unsigned int rate
)
619 unsigned int div
= __cpm_get_pllout2() / rate
;
621 REG_CPM_MSCCDR
= div
- 1;
624 /* Set the MMC clock frequency */
625 static void jz_sd_set_clock(unsigned int rate
)
631 /* select clock source from CPM */
632 cpm_select_msc_clk(rate
);
634 REG_CPM_CPCCR
|= CPM_CPCCR_CE
;
635 clkrt
= jz_sd_calc_clkrt(rate
);
636 REG_MSC_CLKRT
= clkrt
;
638 DEBUG("set clock to %u Hz clkrt=%d", rate
, clkrt
);
641 /********************************************************************************************************************
642 ** Name: int jz_sd_exec_cmd()
643 ** Function: send command to the card, and get a response
644 ** Input: struct sd_request *req: SD request
645 ** Output: 0: right >0: error code
646 ********************************************************************************************************************/
647 static int jz_sd_exec_cmd(struct sd_request
*request
)
649 unsigned int cmdat
= 0, events
= 0;
650 int retval
, timeout
= 0x3fffff;
652 /* Indicate we have no result yet */
653 request
->result
= SD_NO_RESPONSE
;
655 if (request
->cmd
== SD_CIM_RESET
) {
656 /* On reset, 1-bit bus width */
659 /* Reset MMC/SD controller */
662 /* On reset, drop SD clock down */
663 jz_sd_set_clock(MMC_CLOCK_SLOW
);
665 /* On reset, stop SD clock */
668 if (request
->cmd
== SD_SET_BUS_WIDTH
)
670 if (request
->arg
== 0x2)
672 DEBUG("Use 4-bit bus width");
677 DEBUG("Use 1-bit bus width");
685 /* mask all interrupts */
686 //REG_MSC_IMASK = 0xffff;
688 REG_MSC_IREG
= 0xffff;
690 REG_MSC_IMASK
= (~7);
691 /* use 4-bit bus width when possible */
693 cmdat
|= MSC_CMDAT_BUS_WIDTH_4BIT
;
695 /* Set command type and events */
696 switch (request
->cmd
)
698 /* SD core extra command */
700 cmdat
|= MSC_CMDAT_INIT
; /* Initialization sequence sent prior to command */
702 /* bc - broadcast - no response */
703 case SD_GO_IDLE_STATE
:
707 /* bcr - broadcast with response */
709 case SD_ALL_SEND_CID
:
710 case SD_GO_IRQ_STATE
:
713 /* adtc - addressed with data transfer */
714 case SD_READ_DAT_UNTIL_STOP
:
715 case SD_READ_SINGLE_BLOCK
:
716 case SD_READ_MULTIPLE_BLOCK
:
718 #if defined(SD_DMA_ENABLE)
720 MSC_CMDAT_DATA_EN
| MSC_CMDAT_READ
| MSC_CMDAT_DMA_EN
;
722 cmdat
|= MSC_CMDAT_DATA_EN
| MSC_CMDAT_READ
;
724 events
= SD_EVENT_RX_DATA_DONE
;
730 #if defined(SD_DMA_ENABLE)
732 MSC_CMDAT_DATA_EN
| MSC_CMDAT_READ
|
735 cmdat
|= MSC_CMDAT_DATA_EN
| MSC_CMDAT_READ
;
737 events
= SD_EVENT_RX_DATA_DONE
;
741 case SD_WRITE_DAT_UNTIL_STOP
:
743 case SD_WRITE_MULTIPLE_BLOCK
:
747 #if defined(SD_DMA_ENABLE)
749 MSC_CMDAT_DATA_EN
| MSC_CMDAT_WRITE
| MSC_CMDAT_DMA_EN
;
751 cmdat
|= MSC_CMDAT_DATA_EN
| MSC_CMDAT_WRITE
;
753 events
= SD_EVENT_TX_DATA_DONE
| SD_EVENT_PROG_DONE
;
756 case SD_STOP_TRANSMISSION
:
757 events
= SD_EVENT_PROG_DONE
;
760 /* ac - no data transfer */
765 /* Set response type */
766 switch (request
->rtype
)
771 cmdat
|= MSC_CMDAT_BUSY
;
775 cmdat
|= MSC_CMDAT_RESPONSE_R1
;
777 case RESPONSE_R2_CID
:
778 case RESPONSE_R2_CSD
:
779 cmdat
|= MSC_CMDAT_RESPONSE_R2
;
782 cmdat
|= MSC_CMDAT_RESPONSE_R3
;
785 cmdat
|= MSC_CMDAT_RESPONSE_R4
;
788 cmdat
|= MSC_CMDAT_RESPONSE_R5
;
791 cmdat
|= MSC_CMDAT_RESPONSE_R6
;
797 /* Set command index */
798 if (request
->cmd
== SD_CIM_RESET
)
799 REG_MSC_CMD
= SD_GO_IDLE_STATE
;
801 REG_MSC_CMD
= request
->cmd
;
804 REG_MSC_ARG
= request
->arg
;
806 /* Set block length and nob */
807 if (request
->cmd
== SD_SEND_SCR
)
808 { /* get SCR from DataFIFO */
814 REG_MSC_BLKLEN
= request
->block_len
;
815 REG_MSC_NOB
= request
->nob
;
819 REG_MSC_CMDAT
= cmdat
;
821 DEBUG("Send cmd %d cmdat: %x arg: %x resp %d", request
->cmd
,
822 cmdat
, request
->arg
, request
->rtype
);
824 /* Start SD clock and send command to card */
827 /* Wait for command completion */
828 //__intc_unmask_irq(IRQ_MSC);
829 //wakeup_wait(&sd_wakeup, 100);
830 while (timeout
-- && !(REG_MSC_STAT
& MSC_STAT_END_CMD_RES
));
834 return SD_ERROR_TIMEOUT
;
836 REG_MSC_IREG
= MSC_IREG_END_CMD_RES
; /* clear flag */
838 /* Check for status */
839 retval
= jz_sd_check_status(request
);
843 /* Complete command with no response */
844 if (request
->rtype
== RESPONSE_NONE
)
848 jz_sd_get_response(request
);
850 /* Start data operation */
851 if (events
& (SD_EVENT_RX_DATA_DONE
| SD_EVENT_TX_DATA_DONE
))
853 if (events
& SD_EVENT_RX_DATA_DONE
)
855 if (request
->cmd
== SD_SEND_SCR
)
857 /* SD card returns SCR register as data.
858 SD core expect it in the response buffer,
859 after normal response. */
861 (unsigned char *) ((unsigned int) request
->response
+ 5);
864 jz_sd_receive_data_dma(request
);
866 jz_sd_receive_data(request
);
870 if (events
& SD_EVENT_TX_DATA_DONE
)
873 jz_sd_transmit_data_dma(request
);
875 jz_sd_transmit_data(request
);
878 //__intc_unmask_irq(IRQ_MSC);
879 //wakeup_wait(&sd_wakeup, 100);
880 /* Wait for Data Done */
881 while (!(REG_MSC_IREG
& MSC_IREG_DATA_TRAN_DONE
));
882 REG_MSC_IREG
= MSC_IREG_DATA_TRAN_DONE
; /* clear status */
885 /* Wait for Prog Done event */
886 if (events
& SD_EVENT_PROG_DONE
)
888 //__intc_unmask_irq(IRQ_MSC);
889 //wakeup_wait(&sd_wakeup, 100);
890 while (!(REG_MSC_IREG
& MSC_IREG_PRG_DONE
));
891 REG_MSC_IREG
= MSC_IREG_PRG_DONE
; /* clear status */
894 /* Command completed */
896 return SD_NO_ERROR
; /* return successfully */
899 /*******************************************************************************************************************
900 ** Name: int sd_chkcard()
901 ** Function: check whether card is insert entirely
903 ** Output: 1: insert entirely 0: not insert entirely
904 ********************************************************************************************************************/
905 static int jz_sd_chkcard(void)
907 return (SD_INSERT_STATUS() == 0 ? 1 : 0);
911 static void jz_sd_tx_handler(unsigned int arg
)
913 if (__dmac_channel_address_error_detected(arg
))
915 DEBUG("%s: DMAC address error.", __FUNCTION__
);
916 __dmac_channel_clear_address_error(arg
);
918 if (__dmac_channel_transmit_end_detected(arg
))
921 __dmac_channel_clear_transmit_end(arg
);
922 OSSemPost(sd_dma_tx_sem
);
926 static void jz_sd_rx_handler(unsigned int arg
)
928 if (__dmac_channel_address_error_detected(arg
))
930 DEBUG("%s: DMAC address error.", __FUNCTION__
);
931 __dmac_channel_clear_address_error(arg
);
933 if (__dmac_channel_transmit_end_detected(arg
))
935 __dmac_channel_clear_transmit_end(arg
);
936 OSSemPost(sd_dma_rx_sem
);
941 /* MSC interrupt handler */
944 //wakeup_signal(&sd_wakeup);
945 logf("MSC interrupt");
948 /*******************************************************************************************************************
949 ** Name: void sd_hardware_init()
950 ** Function: initialize the hardware condiction that access sd card
953 ********************************************************************************************************************/
954 static void jz_sd_hardware_init(void)
956 __cpm_start_msc(); /* enable mmc clock */
957 mmc_init_gpio(); /* init GPIO */
959 SD_POWER_ON(); /* turn on power of card */
961 SD_RESET(); /* reset mmc/sd controller */
962 SD_IRQ_MASK(); /* mask all IRQs */
963 jz_sd_stop_clock(); /* stop SD clock */
965 // __cpm_start_dmac();
966 // __dmac_enable_module();
967 // REG_DMAC_DMACR = DMAC_DMACR_DME;
969 sd_dma_rx_sem
= OSSemCreate(0);
970 sd_dma_tx_sem
= OSSemCreate(0);
971 request_irq(IRQ_DMA_0
+ RX_DMA_CHANNEL
, jz_sd_rx_handler
,
973 request_irq(IRQ_DMA_0
+ TX_DMA_CHANNEL
, jz_sd_tx_handler
,
979 static int sd_send_cmd(struct sd_request
*request
, int cmd
, unsigned int arg
,
980 unsigned short nob
, unsigned short block_len
,
981 enum sd_rsp_t rtype
, unsigned char* buffer
)
985 request
->rtype
= rtype
;
987 request
->block_len
= block_len
;
988 request
->buffer
= buffer
;
989 request
->cnt
= nob
* block_len
;
991 return jz_sd_exec_cmd(request
);
994 static void sd_simple_cmd(struct sd_request
*request
, int cmd
, unsigned int arg
,
997 sd_send_cmd(request
, cmd
, arg
, 0, 0, rtype
, NULL
);
1000 #define SD_INIT_DOING 0
1001 #define SD_INIT_PASSED 1
1002 #define SD_INIT_FAILED 2
1004 static int sd_init_card_state(struct sd_request
*request
)
1006 struct sd_response_r1 r1
;
1007 struct sd_response_r3 r3
;
1008 int retval
, i
, ocr
= 0x40300000, limit_41
= 0;
1010 switch (request
->cmd
)
1012 case SD_GO_IDLE_STATE
: /* No response to parse */
1013 sd_simple_cmd(request
, SD_SEND_IF_COND
, 0x1AA, RESPONSE_R1
);
1016 case SD_SEND_IF_COND
:
1017 retval
= sd_unpack_r1(request
, &r1
);
1018 sd_simple_cmd(request
, SD_APP_CMD
, 0, RESPONSE_R1
);
1022 retval
= sd_unpack_r1(request
, &r1
);
1023 if (retval
& (limit_41
< 100))
1025 DEBUG("sd_init_card_state: unable to SD_APP_CMD error=%d",
1028 sd_simple_cmd(request
, SD_APP_OP_COND
, ocr
, RESPONSE_R3
);
1029 } else if (limit_41
< 100) {
1031 sd_simple_cmd(request
, SD_APP_OP_COND
, ocr
, RESPONSE_R3
);
1033 /* reset the card to idle*/
1034 sd_simple_cmd(request
, SD_GO_IDLE_STATE
, 0, RESPONSE_NONE
);
1038 case SD_APP_OP_COND
:
1039 retval
= sd_unpack_r3(request
, &r3
);
1045 DEBUG("sd_init_card_state: read ocr value = 0x%08x", r3
.ocr
);
1048 if(!(r3
.ocr
& SD_CARD_BUSY
|| ocr
== 0)){
1050 sd_simple_cmd(request
, SD_APP_CMD
, 0, RESPONSE_R1
);
1054 /* Set the data bus width to 4 bits */
1056 sd_simple_cmd(request
, SD_ALL_SEND_CID
, 0, RESPONSE_R2_CID
);
1060 case SD_ALL_SEND_CID
:
1062 card
.cid
[i
] = ((request
->response
[1+i
*4]<<24) | (request
->response
[2+i
*4]<<16) |
1063 (request
->response
[3+i
*4]<< 8) | request
->response
[4+i
*4]);
1065 logf("CID: %08lx%08lx%08lx%08lx", card
.cid
[0], card
.cid
[1], card
.cid
[2], card
.cid
[3]);
1066 sd_simple_cmd(request
, SD_SEND_RELATIVE_ADDR
, 0, RESPONSE_R6
);
1068 case SD_SEND_RELATIVE_ADDR
:
1069 retval
= sd_unpack_r6(request
, &r1
, &card
.rca
);
1070 card
.rca
= card
.rca
<< 16;
1071 DEBUG("sd_init_card_state: Get RCA from SD: 0x%04x Status: %x", card
.rca
, r1
.status
);
1074 DEBUG("sd_init_card_state: unable to SET_RELATIVE_ADDR error=%d",
1076 return SD_INIT_FAILED
;
1079 sd_simple_cmd(request
, SD_SEND_CSD
, card
.rca
, RESPONSE_R2_CSD
);
1084 card
.csd
[i
] = ((request
->response
[1+i
*4]<<24) | (request
->response
[2+i
*4]<<16) |
1085 (request
->response
[3+i
*4]<< 8) | request
->response
[4+i
*4]);
1087 sd_parse_csd(&card
);
1088 sd2_0
= (card_extract_bits(card
.csd
, 127, 2) == 1);
1090 logf("CSD: %08lx%08lx%08lx%08lx", card
.csd
[0], card
.csd
[1], card
.csd
[2], card
.csd
[3]);
1091 DEBUG("SD card is ready");
1092 jz_sd_set_clock(SD_CLOCK_FAST
);
1093 return SD_INIT_PASSED
;
1095 DEBUG("sd_init_card_state: error! Illegal last cmd %d", request
->cmd
);
1096 return SD_INIT_FAILED
;
1099 return SD_INIT_DOING
;
1102 static int sd_switch(struct sd_request
*request
, int mode
, int group
,
1103 unsigned char value
, unsigned char * resp
)
1109 arg
= (mode
<< 31 | 0x00FFFFFF);
1110 arg
&= ~(0xF << (group
* 4));
1111 arg
|= value
<< (group
* 4);
1112 sd_send_cmd(request
, 6, arg
, 1, 64, RESPONSE_R1
, resp
);
1118 * Fetches and decodes switch information
1120 static int sd_read_switch(struct sd_request
*request
)
1122 unsigned int status
[64 / 4];
1124 memset((unsigned char *)status
, 0, 64);
1125 sd_switch(request
, 0, 0, 1, (unsigned char*) status
);
1127 if (((unsigned char *)status
)[13] & 0x02)
1134 * Test if the card supports high-speed mode and, if so, switch to it.
1136 static int sd_switch_hs(struct sd_request
*request
)
1138 unsigned int status
[64 / 4];
1140 sd_switch(request
, 1, 0, 1, (unsigned char*) status
);
1144 int sd_select_card(void)
1146 struct sd_request request
;
1147 struct sd_response_r1 r1
;
1150 sd_simple_cmd(&request
, SD_SELECT_CARD
, card
.rca
,
1152 retval
= sd_unpack_r1(&request
, &r1
);
1158 retval
= sd_read_switch(&request
);
1161 sd_switch_hs(&request
);
1162 jz_sd_set_clock(SD_CLOCK_HIGH
);
1166 sd_simple_cmd(&request
, SD_APP_CMD
, card
.rca
,
1168 retval
= sd_unpack_r1(&request
, &r1
);
1171 sd_simple_cmd(&request
, SD_SET_BUS_WIDTH
, 2, RESPONSE_R1
);
1172 retval
= sd_unpack_r1(&request
, &r1
);
1182 static bool inited
= false;
1183 struct sd_request init_req
;
1186 jz_sd_hardware_init();
1187 wakeup_init(&sd_wakeup
);
1192 sd_simple_cmd(&init_req
, SD_CIM_RESET
, 0, RESPONSE_NONE
);
1193 sd_simple_cmd(&init_req
, SD_GO_IDLE_STATE
, 0, RESPONSE_NONE
);
1195 while ((retval
= sd_init_card_state(&init_req
)) == SD_INIT_DOING
);
1197 if (retval
== SD_INIT_PASSED
)
1198 return sd_select_card();
1203 static inline bool card_detect_target(void)
1205 return (jz_sd_chkcard() == 1);
1209 void card_enable_monitoring_target(bool on
)
1222 tCardInfo
* card_get_info_target(int card_no
)
1228 int sd_read_sectors(IF_MV2(int drive
,) unsigned long start
, int count
, void* buf
)
1230 #ifdef HAVE_MULTIVOLUME
1235 struct sd_request request
;
1236 struct sd_response_r1 r1
;
1239 if (!card_detect_target() || count
== 0 || start
> card
.numblocks
)
1242 sd_simple_cmd(&request
, SD_SEND_STATUS
, card
.rca
, RESPONSE_R1
);
1243 retval
= sd_unpack_r1(&request
, &r1
);
1244 if (retval
&& (retval
!= SD_ERROR_STATE_MISMATCH
))
1247 sd_simple_cmd(&request
, SD_SET_BLOCKLEN
, SD_BLOCK_SIZE
, RESPONSE_R1
);
1248 if ((retval
= sd_unpack_r1(&request
, &r1
)))
1253 sd_send_cmd(&request
, SD_READ_MULTIPLE_BLOCK
, start
,
1254 count
, SD_BLOCK_SIZE
, RESPONSE_R1
, buf
);
1255 if ((retval
= sd_unpack_r1(&request
, &r1
)))
1260 sd_send_cmd(&request
, SD_READ_MULTIPLE_BLOCK
,
1261 start
* SD_BLOCK_SIZE
, count
,
1262 SD_BLOCK_SIZE
, RESPONSE_R1
, buf
);
1263 if ((retval
= sd_unpack_r1(&request
, &r1
)))
1267 last_disk_activity
= current_tick
;
1269 sd_simple_cmd(&request
, SD_STOP_TRANSMISSION
, 0, RESPONSE_R1B
);
1270 if ((retval
= sd_unpack_r1(&request
, &r1
)))
1278 int sd_write_sectors(IF_MV2(int drive
,) unsigned long start
, int count
, const void* buf
)
1280 #ifdef HAVE_MULTIVOLUME
1285 struct sd_request request
;
1286 struct sd_response_r1 r1
;
1289 if (!card_detect_target() || count
== 0 || start
> card
.numblocks
)
1292 sd_simple_cmd(&request
, SD_SEND_STATUS
, card
.rca
, RESPONSE_R1
);
1293 retval
= sd_unpack_r1(&request
, &r1
);
1294 if (retval
&& (retval
!= SD_ERROR_STATE_MISMATCH
))
1297 sd_simple_cmd(&request
, SD_SET_BLOCKLEN
, SD_BLOCK_SIZE
, RESPONSE_R1
);
1298 if ((retval
= sd_unpack_r1(&request
, &r1
)))
1303 sd_send_cmd(&request
, SD_WRITE_MULTIPLE_BLOCK
, start
,
1304 count
, SD_BLOCK_SIZE
, RESPONSE_R1
,
1306 if ((retval
= sd_unpack_r1(&request
, &r1
)))
1311 sd_send_cmd(&request
, SD_WRITE_MULTIPLE_BLOCK
,
1312 start
* SD_BLOCK_SIZE
, count
,
1313 SD_BLOCK_SIZE
, RESPONSE_R1
, (void*)buf
);
1314 if ((retval
= sd_unpack_r1(&request
, &r1
)))
1317 sd_simple_cmd(&request
, SD_STOP_TRANSMISSION
, 0, RESPONSE_R1B
);
1318 if ((retval
= sd_unpack_r1(&request
, &r1
)))
1326 long sd_last_disk_activity(void)
1328 return last_disk_activity
;
1331 int sd_spinup_time(void)
1336 void sd_enable(bool on
)
1341 void sd_sleepnow(void)
1346 bool sd_disk_is_active(void)
1351 int sd_soft_reset(void)
1357 bool sd_removable(IF_MV_NONVOID(int drive
))
1359 #ifdef HAVE_MULTIVOLUME
1367 bool sd_present(IF_MV_NONVOID(int drive
))
1369 #ifdef HAVE_MULTIDRIVE
1372 return card_detect_target();
1375 #ifdef CONFIG_STORAGE_MULTI
1376 int sd_num_drives(int first_drive
)
1378 /* We don't care which logical drive number(s) we have been assigned */