blob: c1b4dcaecad497e71cab400be1b4bd9b675a0420 [file] [log] [blame]
rjw6c1fd8f2022-11-30 14:33:01 +08001/*****************************************************************************
2* Copyright Statement:
3* --------------------
4* This software is protected by Copyright and the information contained
5* herein is confidential. The software may not be copied and the information
6* contained herein may not be used or disclosed except with the written
7* permission of MediaTek Inc. (C) 2005
8*
9* BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
10* THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
11* RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
12* AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
13* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
14* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
15* NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
16* SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
17* SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
18* THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
19* NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
20* SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
21*
22* BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
23* LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
24* AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
25* OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
26* MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
27*
28* THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
29* WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
30* LAWS PRINCIPLES. ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
31* RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
32* THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
33*
34*****************************************************************************/
35
36/*****************************************************************************
37 *
38 * Filename:
39 * ---------
40 * sd.c
41 *
42 * Project:
43 * --------
44 * Maui_Software
45 *
46 * Description:
47 * ------------
48 * driver functons for SD/MMC
49 *
50 *
51 * Author:
52 * -------
53 * -------
54 *
55 *============================================================================
56 * HISTORY
57 * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
58 *------------------------------------------------------------------------------
59 * removed!
60 * removed!
61 * removed!
62 *
63 * removed!
64 * removed!
65 * removed!
66 *
67 * removed!
68 * removed!
69 * removed!
70 *
71 * removed!
72 * removed!
73 * removed!
74 *
75 * removed!
76 * removed!
77 * removed!
78 *
79 * removed!
80 * removed!
81 * removed!
82 *
83 * removed!
84 * removed!
85 * removed!
86 *
87 * removed!
88 * removed!
89 * removed!
90 *
91 * removed!
92 * removed!
93 * removed!
94 *
95 * removed!
96 * removed!
97 * removed!
98 *
99 * removed!
100 * removed!
101 * removed!
102 *
103 * removed!
104 * removed!
105 * removed!
106 *
107 * removed!
108 * removed!
109 * removed!
110 *
111 *------------------------------------------------------------------------------
112 * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
113 *============================================================================
114 ****************************************************************************/
115#ifndef DRV_MSDC_OFF
116#include "kal_public_api.h" //MSBB change #include "kal_release.h"
117//RHR ADD
118#include "kal_general_types.h"
119//#include "btif_sw.h"
120#include "kal_public_api.h"
121//#include "kal_internal_api.h"
122#include "kal_debug.h"
123#include "kal_public_defs.h"
124//RHR REMOVE
125/*
126//MSBB remove #include "app_buff_alloc.h"
127#include "intrCtrl.h"
128#include "gpio_sw.h"
129#include "init.h"
130*/
131//RHR
132#include "drv_comm.h"
133#include "msdc_reg_adap.h"
134#include "msdc_def.h"
135#include "sd_def.h"
136#include "reg_base.h"
137#include "upll_ctrl.h"
138
139#if defined(__MSDC2_SD_MMC__) || defined(__MSDC2_SD_SDIO__)
140T_SDC_HANDLE *gSD2 = &gSD_blk[SD_MSDC2];
141extern MSDC_HANDLE *msdc2_handle;
142extern kal_uint32 MSDC_Sector2[128];
143
144extern kal_uint8 MSDC_GetIOCtrlParam(void);
145extern kal_bool INT_USBBoot(void);
146extern int MSDC_GetCustom(void);
147
148/*************************************************************************
149* FUNCTION
150* SD_Acmd42
151*
152* DESCRIPTION
153* connect/disconnect the 50K Ohm pull-up resistor on CD/DAT3
154*
155* PARAMETERS
156*
157* RETURNS
158* SDC_CMD_STATUS
159*
160* GLOBALS AFFECTED
161* gSD
162*
163* NOTE
164*
165*
166*************************************************************************/
167SDC_CMD_STATUS SD_Acmd42_2(kal_bool connect)
168{
169 SDC_CMD_STATUS status;
170
171 // send APP_CMD
172 if((status = SD_Cmd55_2(gSD2->mRCA))!=NO_ERROR)
173 return status;
174 // send cmd6
175 if((status = SD_Send_Cmd_2(SDC_CMD_ACMD42,connect))!=NO_ERROR)
176 return status;
177 //read R1
178 if((status = SD_CheckStatus_2())!=NO_ERROR)
179 return status;
180
181 gSD2->mCD_DAT3 = KAL_FALSE; // pull-up resistor is disconnected for data trnasfer
182 return NO_ERROR;
183}
184
185/*************************************************************************
186* FUNCTION
187* SD_SetMMC40_4bit_high_speed
188*
189* DESCRIPTION
190* Check inserted card is SD or MMC
191*
192* PARAMETERS
193*
194*
195* RETURNS
196* SD_CARD or MMC_CARD
197*
198* GLOBALS AFFECTED
199* gMSDC_Handle
200*
201*************************************************************************/
202SDC_CMD_STATUS SD_SetMMC40_bus_high_speed_2(void)
203{
204 kal_uint32 clock, hs;
205 #ifdef DRV_MSDC_MT6225_SERIES
206 /*use larger driving current. In mt6225 and mt6227D, IOCON is 32 bit register and DLT cannot be 0*/
207 MSDC_LSD_WriteReg(MSDC_IOCON2,0x36);
208 MSDC_SetIOCONRegDLT2();
209 #else
210 MSDC_LSD_WriteReg(MSDC_IOCON2,0x1B);
211 #endif
212
213 if(SD_SetBlength_2(512)!=NO_ERROR)
214 goto err;
215
216 // read the EXT_CSD
217 if(SD_SendEXTCSD_MMC40_2(MSDC_Sector2) != NO_ERROR)
218 goto err;;
219
220 // set high speed
221 if(gSD2->mCSD.ext_csd->card_type & HS_52M)
222 {
223 // should be 52000
224 //clock = 52000;
225 clock = msdc2_handle->msdc_clock / 2;
226 hs = 1;
227 //MSDC_LSD_WriteReg32(MSDC_IOCON,0x010002FF);
228 MSDC_LSD_ClearBits32(MSDC_CFG2,MSDC_CFG_CRED);;
229
230 }
231 else if(gSD2->mCSD.ext_csd->card_type & HS_26M)
232 {
233 // should be 26000
234 clock = 26000;
235 hs = 1;
236 }
237 else
238 {
239 clock = 13000;
240 hs = 0;
241 }
242 if(hs)
243 {
244 // select proper power class
245 if(SD_Switch_MMC40_2(SET_BYTE,EXT_CSD_POW_CLASS_INDEX,
246 (gSD2->mCSD.ext_csd->pwr_52_360&0xf) ,0) != NO_ERROR)
247 goto err;
248
249 // enable high speed (26M or 52M)
250 if(SD_Switch_MMC40_2(SET_BYTE,EXT_CSD_HIGH_SPPED_INDEX,
251 EXT_CSD_ENABLE_HIGH_SPEED,0) != NO_ERROR)
252 goto err;
253
254 // latch data at falling edge to cover the card driving capability
255 // MSDC_LSD_SetBits32(MSDC_CFG2,MSDC_CFG_RED);
256 }
257
258 msdc2_handle->msdc_clock = MSDC_CLOCK;
259 MSDC_SetClock2(clock);
260
261 #ifdef DRV_MSDC_MT6225_SERIES
262 /*change DLT and set cmd latch at rising edge*/
263 MSDC_LSD_WriteReg(MSDC_IOCON2,0x8036);
264 MSDC_SetIOCONRegDLT2();
265 #endif
266
267 #if 0 // MSDC2 only support 1-bit data
268/* under construction !*/
269 #if defined(MMC40_USE_4BIT_BUS)
270/* under construction !*/
271/* under construction !*/
272/* under construction !*/
273/* under construction !*/
274/* under construction !*/
275 #elif defined(MMC40_USE_8BIT_BUS)
276/* under construction !*/
277/* under construction !*/
278/* under construction !*/
279/* under construction !*/
280/* under construction !*/
281 #endif
282 #endif
283
284 if(SD_SendEXTCSD_MMC40_2(MSDC_Sector2) != NO_ERROR)
285 goto err;;
286
287 return NO_ERROR;
288err:
289 return ERR_MMC_BUS_HS_ERROR;
290}
291/*************************************************************************
292* FUNCTION
293* SD_CheckSDorMMC
294*
295* DESCRIPTION
296* Check inserted card is SD or MMC
297*
298* PARAMETERS
299*
300*
301* RETURNS
302* SD_CARD or MMC_CARD
303*
304* GLOBALS AFFECTED
305* gMSDC_Handle
306*
307*************************************************************************/
308T_MSDC_CARD SD_CheckSDorMMC_2()
309{
310 SDC_CMD_STATUS status;
311
312#if defined(SD_MMC_HIGH_DENSITY_SUPPORT)
313 SD_Cmd8_2();
314#endif
315 if((status = SD_Acmd41_SD_2())==NO_ERROR)
316 return msdc2_handle->mMSDC_type; // SD_CARD
317 else if((status = SD_Cmd1_MMC_2())==NO_ERROR)
318 return msdc2_handle->mMSDC_type; // MMC_CARD
319
320 return UNKNOWN_CARD;
321}
322/*************************************************************************
323* FUNCTION
324* SD_SetDefault
325*
326* DESCRIPTION
327* set default values to gSD
328*
329* PARAMETERS
330*
331*
332* RETURNS
333*
334* GLOBALS AFFECTED
335* gSD
336*
337*************************************************************************/
338void SD_SetDefault_2(void)
339{
340 kal_mem_set(gSD2,0,sizeof(T_SDC_HANDLE));
341 gSD2->mBKLength = 512;
342 gSD2->mRCA = 0;
343 gSD2->mInactive = KAL_FALSE;
344 gSD2->mState = IDLE_STA;
345 gSD2->bus_width = 1;
346 gSD2->mCD_DAT3 = KAL_TRUE;
347}
348
349void SD_Use24M_Clock_2(void)
350{
351 if(msdc2_handle->mMSDC_type == SD_CARD || msdc2_handle->mMSDC_type == SD20_LCS_CARD
352 || msdc2_handle->mMSDC_type == SD20_HCS_CARD)
353 {
354 UPLL_Enable(UPLL_OWNER_MSDC2);
355 MSDC_LSD_SetBits32(MSDC_CFG2, MSDC_CFG_CLKSRC);
356 msdc2_handle->msdc_clock = MSDC_CLOCK_USB;
357 MSDC_SetClock2(MSDC_SD_OP_CLOCK);
358 gSD2->flags |= SD_FLAG_USE_USB_CLK;
359 }
360}
361
362void SD_Use13M_Clock_2(void)
363{
364 MSDC_LSD_ClearBits32(MSDC_CFG2, MSDC_CFG_CLKSRC);
365 msdc2_handle->msdc_clock = MSDC_CLOCK;
366 MSDC_SetClock2(MSDC_OP_CLOCK);
367 gSD2->flags &= ~SD_FLAG_USE_USB_CLK;
368}
369
370
371/*************************************************************************
372* FUNCTION
373* SD_Initialize
374*
375* DESCRIPTION
376* Initial SD controller and card
377*
378* PARAMETERS
379*
380*
381* RETURNS
382* SDC_CMD_STATUS
383*
384* GLOBALS AFFECTED
385* gSD
386*
387*************************************************************************/
388SDC_CMD_STATUS SD_Initialize_2(void)
389{
390 kal_uint32 cid[4],csd[4],scr[4];
391 kal_uint16 rca, iocon;
392 SDC_CMD_STATUS status;
393
394 if(msdc2_handle->mIsInitialized == KAL_TRUE)
395 {
396 return NO_ERROR;
397 }
398 // reset the events
399 kal_set_eg_events(msdc2_handle->MSDC_Events, 0, KAL_AND);
400 // reset msdc
401 if(MSDC_Reg32(MSDC_CFG2) & MSDC_CFG_RST)
402 {
403 MSDC_LSD_ClearBits32(MSDC_CFG2, MSDC_CFG_RST);
404 }
405 else
406 {
407 RESET_MSDC2();
408 }
409
410 #if defined(DRV_MSDC_MT6225_SERIES)
411 MSDC_LSD_SetBits32(MSDC_CFG2,MSDC_CFG_CRED);
412 #endif
413
414 // set the output driving capability from customization interface
415 iocon = MSDC_SetData(MSDC_IOCON2, 0xff, MSDC_GetIOCtrlParam2());
416 // set pull up the data and cmd
417 BitFieldWrite32((kal_uint32*)MSDC_CFG2,(kal_uint32)2,MSDC_CFG_PRCFG0);
418 BitFieldWrite32((kal_uint32*)MSDC_CFG2,(kal_uint32)2,MSDC_CFG_PRCFG1);
419 BitFieldWrite32((kal_uint32*)MSDC_CFG2,(kal_uint32)2,MSDC_CFG_PRCFG2);
420 // set read timeout x5ms
421 BitFieldWrite32((kal_uint32*)SDC_CFG2,(kal_uint32)40,SDC_CFG_DTOC);
422 //set clock of serial clcok for initialization
423 MSDC_LSD_ClearBits32(MSDC_CFG2, MSDC_CFG_CLKSRC);
424 msdc2_handle->msdc_clock = MSDC_CLOCK;
425 MSDC_SetClock2(MSDC_INI_CLOCK);
426 MSDC_LSD_ClearBits32(SDC_CFG2,SDC_CFG_MDLEN);
427 // initial global sturctures
428 SD_SetDefault_2();
429
430 // send the card to IDLE state
431 if((status = SD_Reset_2())!=NO_ERROR)
432 {
433 goto err;
434 }
435
436 // and validate the OCR (CMD0,CMD1 or ADMD41)
437 if(SD_CheckSDorMMC_2() == UNKNOWN_CARD)
438 {
439 status = ERR_STATUS;
440 goto err;
441 }
442
443 // get CID(CMD2)
444 if((status = SD_GetCID_2(cid))!=NO_ERROR)
445 {
446 goto err;
447 }
448 // get or set RCA(CMD3)
449 if((status = SD_ValidateRCA_2(&rca))!=NO_ERROR)
450 {
451 goto err;
452 }
453 // get CSD and analysis the CSD(CMD9)
454 if((status = SD_GetCSD_2(gSD2->mRCA,csd))!=NO_ERROR)
455 {
456 goto err;
457 }
458 // Set driver stage register DSR to default value (0x0404)(CMD4)
459 if(gSD2->mCSD.dsr_imp)
460 if((status = SD_SetDSR_2())!=NO_ERROR)
461 {
462 //dbg_print("6\r\n");
463 goto err;
464 }
465
466 if(MSDC_GetCustom() & MSDC2_WP)
467 {
468 if((MSDC_Reg(SDC_STA2) & SDC_STA_WP))
469 gSD2->mWPEnabled = KAL_TRUE;
470 }
471 status = SD_SelectCard_2(gSD2->mRCA);
472 if(status == CARD_IS_LOCKED)
473 gSD2->mIsLocked = KAL_TRUE;
474
475 #if defined(SD_MMC_HIGH_DENSITY_SUPPORT)
476 if(gSD2->flags & SD_FLAG_SD_TYPE_CARD)
477 #else
478 if(msdc2_handle->mMSDC_type == SD_CARD)
479 #endif
480 {
481 SD_Use24M_Clock_2();
482 MSDC_SetIOCONRegDLT2();
483 if((status = SD_ReadSCR_2(scr))!=NO_ERROR)
484 {
485 goto err;
486 }
487 #if 0 // MSDC2 only support 1-bit data
488/* under construction !*/
489/* under construction !*/
490/* under construction !*/
491/* under construction !*/
492 #endif
493
494 #if !defined(__MSDC_TFLASH_DAT3_1BIT_HOT_PLUG__)
495 if((status = SD_Acmd42_2(KAL_FALSE))!=NO_ERROR)
496 {
497 goto err;
498 }
499 #endif
500
501 if(gSD2->flags & SD_FLAG_CMD6_SUPPORT)
502 {
503 status = SD_SelectHighSpeed_SD11_2();
504 if(status == NO_ERROR)
505 {
506 gSD2->flags |= SD_FLAG_HS_ENABLED;
507 MSDC_LSD_ClearBits32(MSDC_CFG2, MSDC_CFG_CLKSRC);
508 msdc2_handle->msdc_clock = MSDC_CLOCK;
509 MSDC_SetClock(26000);
510 }
511 }
512 }
513 else
514 {
515 if(msdc2_handle->mMSDC_type == MMC_CARD && gSD2->mCSD.spec_ver >= 4)
516 {
517 msdc2_handle->mMSDC_type = MMC40_CARD;
518 if((status = SD_SetMMC40_bus_high_speed_2())!= NO_ERROR)
519 goto err;
520 }
521 else
522 {
523 SD_Use13M_Clock_2();
524 }
525 }
526 status = SD_SetBlength_2(512);
527err:
528 if(status != NO_ERROR)
529 {
530 tst_sys_trace("SD2 mount fail!");
531 SD_SetDefault();
532 msdc2_handle->mIsInitialized = KAL_FALSE;
533 }
534 else
535 {
536 tst_sys_trace("SD2 mount ok!");
537 msdc2_handle->mIsInitialized = KAL_TRUE;
538 }
539 kal_set_eg_events(msdc2_handle->MSDC_Events, 0, KAL_AND);
540
541 return status ;
542}
543
544static kal_uint32 power2(kal_uint32 num)
545{
546 return 1 << num;
547}
548
549/*************************************************************************
550* FUNCTION
551* SD_AnalysisCSD
552*
553* DESCRIPTION
554* Analysis Card Specific Data and store in the member of gSD
555*
556* PARAMETERS
557* csd: input csd for analysis
558* RETURNS
559*
560* GLOBALS AFFECTED
561* gSD
562*
563*************************************************************************/
564void SD_AnalysisCSD_2(kal_uint32* csd)
565{
566 kal_uint8 *ptr;
567 kal_uint32 c_mult,c_size;
568
569 ptr = (kal_uint8*)csd;
570 c_mult = c_size = 0;
571 // these offsets refer to the spec. of SD and MMC
572 GetBitFieldN((kal_uint8*)&gSD2->mCSD.csd_ver, ptr, 126,2);
573 GetBitFieldN((kal_uint8*)&gSD2->mCSD.tacc,ptr,112,8);
574 GetBitFieldN((kal_uint8*)&gSD2->mCSD.nsac,ptr,104,8);
575 GetBitFieldN((kal_uint8*)&gSD2->mCSD.tran_speed,ptr,96,8);
576 GetBitFieldN((kal_uint8*)&gSD2->mCSD.ccc,ptr,84,12);
577 GetBitFieldN((kal_uint8*)&gSD2->mCSD.r_blk_len,ptr,80,4);
578 gSD2->mCSD.r_blk_len = power2(gSD2->mCSD.r_blk_len);
579 GetBitFieldN((kal_uint8*)&gSD2->mCSD.r_blk_part,ptr,79,1);
580 GetBitFieldN((kal_uint8*)&gSD2->mCSD.w_blk_misali,ptr,78,1);
581 GetBitFieldN((kal_uint8*)&gSD2->mCSD.r_blk_misali,ptr,77,1);
582 GetBitFieldN((kal_uint8*)&gSD2->mCSD.dsr_imp,ptr,76,1);
583 GetBitFieldN((kal_uint8*)&gSD2->mCSD.w_blk_part,ptr,21,1);
584 GetBitFieldN((kal_uint8*)&gSD2->mCSD.w_blk_len,ptr,22,4);
585 gSD2->mCSD.w_blk_len = power2(gSD2->mCSD.w_blk_len);
586 GetBitFieldN((kal_uint8*)&gSD2->mCSD.wp_grp_enable,ptr,31,1);
587 // there are some difference of CSD between SD and MMC
588 if(msdc2_handle->mMSDC_type == MMC_CARD)
589 {
590 GetBitFieldN((kal_uint8*)&gSD2->mCSD.spec_ver, ptr, 122,4);
591 GetBitFieldN((kal_uint8*)&gSD2->mCSD.erase_sec_size_mmc,ptr,42,5);
592 gSD2->mCSD.erase_sec_size_mmc = (gSD2->mCSD.erase_sec_size_mmc+1)*gSD2->mCSD.w_blk_len;
593 GetBitFieldN((kal_uint8*)&gSD2->mCSD.erase_grp_size_mmc,ptr,37,5);
594 gSD2->mCSD.erase_grp_size_mmc = (gSD2->mCSD.erase_grp_size_mmc+1)*gSD2->mCSD.erase_sec_size_mmc;
595 GetBitFieldN((kal_uint8*)&gSD2->mCSD.wp_grp_size_mmc,ptr,32,5);
596 gSD2->mCSD.wp_grp_size_mmc = (gSD2->mCSD.wp_grp_size_mmc + 1)*gSD2->mCSD.erase_grp_size_mmc;
597 }
598 else // SD_CARD
599 {
600 GetBitFieldN((kal_uint8*)&gSD2->mCSD.erase_sec_size_sd,ptr,39,7);
601 gSD2->mCSD.erase_sec_size_sd += 1;
602 GetBitFieldN((kal_uint8*)&gSD2->mCSD.wp_prg_size_sd,ptr,32,7);
603 gSD2->mCSD.wp_prg_size_sd = (gSD2->mCSD.wp_prg_size_sd+1) * gSD2->mCSD.erase_sec_size_sd;
604 GetBitFieldN((kal_uint8*)&gSD2->mCSD.erase_blk_en_sd,ptr,46,1);
605 }
606
607 #if defined(SD_MMC_HIGH_DENSITY_SUPPORT)
608 if(msdc2_handle->mMSDC_type == SD20_HCS_CARD && gSD2->mCSD.csd_ver >= SD_CSD_VER_20)
609 {
610 GetBitFieldN((kal_uint8*)&c_size,ptr,48,22);
611 gSD2->mBKNum = (c_size+1);
612 gSD2->mCSD.capacity = (kal_uint64)gSD2->mBKNum*512*1024;
613 }
614 else
615 #endif
616 {
617 GetBitFieldN((kal_uint8*)&c_mult,ptr,47,3);
618 c_mult = power2(c_mult+2);
619 GetBitFieldN((kal_uint8*)&c_size,ptr,62,12);
620 gSD2->mBKNum = (c_size+1)*c_mult;
621 gSD2->mCSD.capacity = (c_size+1)*c_mult*gSD2->mCSD.r_blk_len;
622 }
623
624}
625
626/*************************************************************************
627* FUNCTION
628* SD_AnalysisCID
629*
630* DESCRIPTION
631* Analysis Card Identificaton and store in the member of gSD
632*
633* PARAMETERS
634* cid: input of card ID for analysis
635* RETURNS
636*
637* GLOBALS AFFECTED
638* gSD
639*
640*************************************************************************/
641void SD_AnalysisCID_2(kal_uint32* cid)
642{
643 kal_uint8 i;
644 kal_uint8* pcid;
645 pcid = (kal_uint8*)cid;
646
647 if(msdc2_handle->mMSDC_type == MMC_CARD)
648 {
649 GetBitFieldN((kal_uint8*)&gSD2->mCID.year,pcid,8,4);
650 gSD2->mCID.year += 1997;
651 GetBitFieldN((kal_uint8*)&gSD2->mCID.month,pcid,12,4);
652 GetBitFieldN((kal_uint8*)&gSD2->mCID.psn,pcid,16,32);
653 GetBitFieldN((kal_uint8*)&gSD2->mCID.prv,pcid,48,8);
654 for(i=0;i<6;i++)
655 gSD2->mCID.pnm[i] = *(pcid+7+i);
656 GetBitFieldN((kal_uint8*)&gSD2->mCID.oid,pcid,104,16);
657 GetBitFieldN((kal_uint8*)&gSD2->mCID.mid,pcid,120,8);
658
659 // special case handling
660 {
661 kal_uint8 pnm[] = {0xFF,0xFF,0xFF,0xFF,0x36,0x31};
662 if(gSD2->mCID.mid == 6 && gSD2->mCID.oid == 0 &&
663 !kal_mem_cmp(gSD2->mCID.pnm,pnm,6))
664 {
665 gSD2->flags |= SD_FLAG_MMC_MRSW_FAIL;
666 }
667 }
668 }
669 else // SD_CARD
670 {
671 gSD2->mCID.mid = *(pcid+15);
672 gSD2->mCID.oid = *(pcid+13) + 256*(*(pcid+14));
673 for(i=0;i<5;i++)
674 gSD2->mCID.pnm[i] = *(pcid+8+i);
675 gSD2->mCID.prv = *(pcid+7);
676 //gSD2->mCID.psn = *(kal_uint32*)(pcid+3);
677 gSD->mCID.psn = (*(kal_uint32*)(pcid+4)<<8)|*(pcid+3);
678 gSD2->mCID.month = (kal_uint8)GET_BIT(*(pcid+1),0,BIT_MASK_4);
679 gSD2->mCID.year = GET_BIT(*(pcid+1),4,BIT_MASK_4)+16*GET_BIT(*(pcid+2),0,BIT_MASK_4) + 2000;
680 }
681}
682
683/*************************************************************************
684* FUNCTION
685* SD_AnalysisSCR
686*
687* DESCRIPTION
688* Analysis SD Card Configuration Register and store in the member of gSD
689*
690* PARAMETERS
691* scr: input of scr for analysis
692* RETURNS
693*
694* GLOBALS AFFECTED
695* gSD
696*
697* NOTE
698* Only for SD card.
699*
700*************************************************************************/
701void SD_AnalysisSCR_2(kal_uint32* scr)
702{
703 kal_uint8 *pscr;
704
705 pscr = (kal_uint8*)scr;
706 gSD2->mSCR.spec_ver = (kal_uint8)GET_BIT(*(pscr),0,BIT_MASK_4);
707 if(gSD2->mSCR.spec_ver > SD_SPEC_101)
708 gSD2->flags |= SD_FLAG_CMD6_SUPPORT;
709 gSD2->mSCR.dat_after_erase = (kal_uint8)GET_BIT(*(pscr+1),7,BIT_MASK_1);
710 gSD2->mSCR.security = (kal_uint8)GET_BIT(*(pscr+1),4,BIT_MASK_3);
711 gSD2->mSCR.bus_width = (kal_uint8)GET_BIT(*(pscr+1),0,BIT_MASK_4);
712}
713
714/*************************************************************************
715* FUNCTION
716* SD_WaitCmdRdyOrTo
717*
718* DESCRIPTION
719* Wait until command ready or timeout
720*
721* PARAMETERS
722*
723* RETURNS
724* SDC_CMD_STATUS
725*
726* GLOBALS AFFECTED
727*
728* NOTE
729* Interrupt driven and polling are both implemented
730*
731*************************************************************************/
732SDC_CMD_STATUS SD_WaitCmdRdyOrTo_2(void)
733{
734 MSDC_START_TIMER2(MSDC_TIMEOUT_PERIOD_CMD);
735
736 {
737 volatile kal_uint16 sdc_cmdsta;
738 kal_uint32 t1;
739
740 t1 = drv_get_current_time();
741 while(!(sdc_cmdsta = MSDC_Reg(SDC_CMDSTA2))
742 && MSDC_Check_Card_Present2() && !msdc2_handle->is_timeout)
743 {
744 if(drv_get_duration_ms(t1) > MSDC_TIMEOUT_PERIOD_CMD*11)
745 msdc2_handle->is_timeout = KAL_TRUE;
746 }
747 MSDC_STOP_TIMER2();
748 MSDC_CLR_INT2();
749 msdc2_handle->cmd_sta = sdc_cmdsta;
750 if(sdc_cmdsta & SDC_CMDSTA_CMDTO)
751 {
752 kal_prompt_trace(MOD_MSDC_HISR,"[MSDC]:cmd timeout");
753 return ERR_CMD_TIMEOUT;
754 }
755 else if(sdc_cmdsta & SDC_CMDSTA_RSPCRCERR)
756 {
757 kal_prompt_trace(MOD_MSDC_HISR,"[MSDC]:cmd crc");
758 return ERR_CMD_RSPCRCERR;
759 }
760 else if(sdc_cmdsta & SDC_CMDSTA_CMDRDY)
761 return NO_ERROR;
762 }
763 if(msdc2_handle->is_timeout)
764 return MSDC_GPT_TIMEOUT_ERR;
765
766 return NO_ERROR;
767}
768
769/*************************************************************************
770* FUNCTION
771* SD_WaitDatRdyOrTo_2
772*
773* DESCRIPTION
774* Wait until data ready or timeout
775*
776* PARAMETERS
777*
778* RETURNS
779* SDC_CMD_STATUS
780*
781* GLOBALS AFFECTED
782*
783* NOTE
784* Interrupt driven and polling are both implemented
785*
786*************************************************************************/
787SDC_CMD_STATUS SD_WaitDatRdyOrTo_2(void)
788{
789 MSDC_START_TIMER2(MSDC_TIMEOUT_PERIOD_DAT);
790
791
792 {
793 volatile kal_uint16 sdc_datsta;
794 kal_uint32 t1;
795
796 t1 = drv_get_current_time();
797 while(!(sdc_datsta = MSDC_Reg(SDC_DATSTA2))
798 && MSDC_Check_Card_Present2() && !msdc2_handle->is_timeout)
799 {
800 if(drv_get_duration_ms(t1) > MSDC_TIMEOUT_PERIOD_DAT*11)
801 msdc2_handle->is_timeout = KAL_TRUE;
802 };
803 MSDC_STOP_TIMER2();
804 MSDC_CLR_INT2();
805 msdc2_handle->dat_sta = sdc_datsta;
806 if(sdc_datsta & SDC_DATSTA_DATTO)
807 {
808 kal_prompt_trace(MOD_MSDC_HISR,"[MSDC]:dat timeout");
809 return ERR_DAT_TIMEOUT;
810 }
811 else if(sdc_datsta & SDC_DATSTA_DATCRCERR)
812 {
813 kal_prompt_trace(MOD_MSDC_HISR,"[MSDC]:dat crc");
814 return ERR_DAT_CRCERR;
815 }
816 else if(sdc_datsta & SDC_DATSTA_BLKDONE)
817 return NO_ERROR;
818 }
819 if(msdc2_handle->is_timeout)
820 return MSDC_GPT_TIMEOUT_ERR;
821
822 return NO_ERROR;
823}
824/*************************************************************************
825* FUNCTION
826* SD_WaitCardNotBusy_2
827*
828* DESCRIPTION
829* Wait until card is not busy (R1b)
830*
831* PARAMETERS
832*
833* RETURNS
834* void
835*
836* GLOBALS AFFECTED
837*
838* NOTE
839* Interrupt driven and polling are both implemented
840*
841*************************************************************************/
842
843SDC_CMD_STATUS SD_WaitCardNotBusy_2(void)
844{
845 kal_uint32 t1;
846
847 t1 = drv_get_current_time();
848 MSDC_START_TIMER2(MSDC_TIMEOUT_PERIOD_DAT);
849#ifdef MSDC_USE_INT
850 {
851 kal_uint32 flags = 0;
852 if(!msdc2_handle->mIsPresent)
853 return;
854 kal_retrieve_eg_events(msdc2_handle->MSDC_Events,EVENT_SDR1BIRQ,KAL_AND_CONSUME,&flags,KAL_SUSPEND);
855 }
856#else
857 {
858 while(SD_IS_R1B_BUSY_2 && MSDC_Check_Card_Present2() && !msdc2_handle->is_timeout)
859 {
860 if(drv_get_duration_ms(t1) > MSDC_TIMEOUT_PERIOD_DAT*11)
861 msdc2_handle->is_timeout = KAL_TRUE;
862 };
863 MSDC_CLR_INT2();
864 MSDC_STOP_TIMER2();
865 }
866#endif
867
868 return NO_ERROR;
869}
870
871/*************************************************************************
872* FUNCTION
873* SD_CheckStatus_2
874*
875* DESCRIPTION
876* Check command status
877*
878* PARAMETERS
879*
880* RETURNS
881* SDC_CMD_STATUS
882*
883* GLOBALS AFFECTED
884*
885* NOTE
886*
887*************************************************************************/
888SDC_CMD_STATUS SD_CheckStatus_2()
889{
890 kal_uint32 status;
891
892 MSDC_LSD_ReadReg32(SDC_RESP02,&status);
893 if(status & SDC_CSTA_MASK)
894 ASSERT(0);
895 if((status & SDC_CSTA_MASK)==0 )
896 return NO_ERROR;
897 if(status &SDC_CARD_IS_LOCKED)
898 return CARD_IS_LOCKED;
899
900 return ERR_STATUS;
901}
902
903/*************************************************************************
904* FUNCTION
905* SD_Send_Cmd_2
906*
907* DESCRIPTION
908* to launch the command packet to the card
909*
910* PARAMETERS
911* 1. cmd: the content of SDC_CMD register
912* 2. arg: the argument(if the command need no argument, fill it with 0)
913*
914* RETURNS
915* SDC_CMD_STATUS
916*
917* GLOBALS AFFECTED
918*
919* NOTE
920* 1. Check if controller is available before launch any commands
921* 2. Maybe add check if card is busy (R1b)
922*************************************************************************/
923SDC_CMD_STATUS SD_Send_Cmd_2(kal_uint32 cmd, kal_uint32 arg)
924{
925 SDC_CMD_STATUS status;
926 kal_uint32 t1;
927
928 t1 = drv_get_current_time();
929 MSDC_START_TIMER2(MSDC_TIMEOUT_PERIOD_CMD);
930 // check the controller is ready (stop transaction will fail)
931 if(cmd != SDC_CMD_CMD12)
932 {
933 while(SD_IS_SDC_BUSY_2 && MSDC_Check_Card_Present2() && !msdc2_handle->is_timeout)
934 {
935 if(drv_get_duration_ms(t1) > MSDC_TIMEOUT_PERIOD_DAT*11)
936 msdc2_handle->is_timeout = KAL_TRUE;
937 }
938 }
939 else
940 {
941 while(SD_IS_CMD_BUSY_2 && MSDC_Check_Card_Present2() && !msdc2_handle->is_timeout)
942 {
943 if(drv_get_duration_ms(t1) > MSDC_TIMEOUT_PERIOD_DAT*11)
944 msdc2_handle->is_timeout = KAL_TRUE;
945 }
946 }
947 MSDC_STOP_TIMER2();
948 if(msdc2_handle->is_timeout)
949 return MSDC_GPT_TIMEOUT_ERR;
950
951 MSDC_CLR_INT2();
952 // fill out the argument
953 MSDC_LSD_WriteReg32(SDC_ARG2,arg);
954 // launch the command
955 MSDC_LSD_WriteReg32(SDC_CMD2,cmd);
956 if((status = SD_WaitCmdRdyOrTo_2())!=NO_ERROR)
957 return status;
958
959 return NO_ERROR;
960}
961
962/*************************************************************************
963* FUNCTION
964* SD_Reset
965*
966* DESCRIPTION
967* reset all cards to idle state
968*
969* PARAMETERS
970* 1. cmd: the content of SDC_CMD register
971* 2. arg: the argument(if the command need no argument, fill it with 0)
972*
973* RETURNS
974* SDC_CMD_STATUS
975*
976* GLOBALS AFFECTED
977*
978* NOTE
979*
980*************************************************************************/
981SDC_CMD_STATUS SD_Reset_2(void)
982{
983 SDC_CMD_STATUS status;
984
985 status = SD_Send_Cmd_2(SDC_CMD_CMD0,SDC_NO_ARG);
986 gSD2->mState = IDLE_STA;
987
988 return status;
989}
990
991/*************************************************************************
992* FUNCTION
993* SD_Cmd55_2
994*
995* DESCRIPTION
996* APP_CMD: inidicate to the card that the next command is an application specified command
997* rather than a standard command
998*
999* PARAMETERS
1000* rca: relative card address
1001*
1002* RETURNS
1003* SDC_CMD_STATUS
1004*
1005* GLOBALS AFFECTED
1006*
1007* NOTE
1008*
1009*************************************************************************/
1010SDC_CMD_STATUS SD_Cmd55_2(kal_uint16 rca)
1011{
1012 SDC_CMD_STATUS status;
1013
1014 if((status = SD_Send_Cmd_2(SDC_CMD_CMD55,(kal_uint32)rca<<16))!=NO_ERROR)
1015 return status;
1016 //read R1
1017 if((status = SD_CheckStatus_2())!=NO_ERROR)
1018 return status;
1019 //check APP_CMD bit in status register
1020 MSDC_LSD_ReadReg32(SDC_RESP02,&status);
1021 if(status & SDC_CSTA_MASK)
1022 ASSERT(0);
1023 if(!(status & R1_APP_CMD_5))
1024 return ERR_APPCMD_FAILED;
1025
1026 return NO_ERROR;
1027}
1028/*************************************************************************
1029* FUNCTION
1030* SD_Cmd8
1031*
1032* DESCRIPTION
1033* 1. Sends SD Memory Card interface conditions for support larger than 2G cards
1034* 2. check if the card is compliant to SD2.0 or higher
1035* 3. only performed while at IDLE state.
1036*
1037* PARAMETERS
1038*
1039* RETURNS
1040*
1041* GLOBALS AFFECTED
1042* gSD2->mIsCMD8
1043*
1044*************************************************************************/
1045void SD_Cmd8_2(void)
1046{
1047 kal_uint32 resp;
1048
1049 if(SD_Send_Cmd_2(SDC_CMD_CMD8,SDC_CMD8_ARG)!=NO_ERROR)
1050 {
1051 SD_Reset_2();
1052 gSD2->mCMD8Resp = SD_CMD8_RESP_NORESP;
1053 return;
1054 }
1055 MSDC_LSD_ReadReg32(SDC_RESP02,&resp);
1056 if(resp == SDC_CMD8_ARG)
1057 gSD2->mCMD8Resp = SD_CMD8_RESP_VALID;
1058 else
1059 gSD2->mCMD8Resp = SD_CMD8_RESP_INVALID;
1060}
1061
1062/*************************************************************************
1063* FUNCTION
1064* SD_Cmd1_MMC
1065*
1066* DESCRIPTION
1067* asks all cards in idle state to send their OCR in the response on the CMD line
1068*
1069* PARAMETERS
1070*
1071* RETURNS
1072* SDC_CMD_STATUS
1073*
1074* GLOBALS AFFECTED
1075* gSD
1076*
1077* NOTE
1078* only works for MMC
1079*
1080*************************************************************************/
1081SDC_CMD_STATUS SD_Cmd1_MMC_2(void)
1082{
1083 SDC_CMD_STATUS status;
1084 kal_uint32 _ocr, ocr_i, t1, t2;
1085
1086 #if defined(SD_MMC_HIGH_DENSITY_SUPPORT)
1087 if(gSD2->mCMD8Resp == SD_CMD8_RESP_INVALID)
1088 return ERR_CMD8_INVALID;
1089 ocr_i = (SDC_OCR_DEFAULT|MMC_HIGH_DESITY_CHECK_BIT);
1090 #else
1091 ocr_i = SDC_OCR_DEFAULT;
1092 #endif
1093
1094 if(msdc2_handle->is_init_timeout == KAL_TRUE)
1095 return ERR_R3_OCR_BUSY;
1096 t2 = drv_get_current_time();
1097 do{
1098 t1 = drv_get_current_time();
1099 MSDC_START_TIMER2(MSDC_TIMEOUT_PERIOD_DAT);
1100 while((MSDC_IS_BUSY2)
1101 && MSDC_Check_Card_Present2() && !msdc2_handle->is_timeout)
1102 {
1103 if(drv_get_duration_ms(t1) > MSDC_TIMEOUT_PERIOD_DAT*11)
1104 msdc2_handle->is_timeout = KAL_TRUE;
1105 };
1106 MSDC_STOP_TIMER2();
1107 if(msdc2_handle->is_timeout)
1108 return MSDC_GPT_TIMEOUT_ERR;
1109 MSDC_LSD_WriteReg32(SDC_ARG2,ocr_i);
1110 MSDC_LSD_WriteReg32(SDC_CMD2,SDC_CMD_CMD1);
1111 if((status = SD_WaitCmdRdyOrTo_2()) != NO_ERROR)
1112 {
1113 return status;
1114 }
1115 MSDC_LSD_ReadReg32(SDC_RESP02, &_ocr);
1116 if((_ocr & SDC_OCR_DEFAULT) == 0)
1117 return ERR_OCR_NOT_SUPPORT;
1118 if(!msdc2_handle->mIsPresent)
1119 return MSDC_CARD_NOT_PRESENT;
1120
1121 if(!(_ocr&SDC_OCR_BUSY))
1122 {
1123 if(drv_get_duration_ms(t2) > MSDC_TIMEOUT_PERIOD_INI)
1124 {
1125 msdc2_handle->is_init_timeout = KAL_TRUE;
1126 break;
1127 }
1128 if((kal_query_systemInit() == KAL_TRUE)
1129#ifdef __TST_WRITE_TO_FILE_ONLY__ /*error recording: considering error recording additionally*/
1130 || (KAL_TRUE == INT_QueryExceptionStatus())
1131#endif
1132 )
1133 MSDC_GPTI_BusyWait(30);
1134 else
1135 kal_sleep_task(7);
1136
1137 }
1138 else
1139 break;
1140 }while(1);
1141
1142 if(msdc2_handle->is_init_timeout)
1143 return ERR_CMD_TIMEOUT;
1144
1145 #if defined(SD_MMC_HIGH_DENSITY_SUPPORT)
1146 if((_ocr & MMC_HIGH_DESITY_CHECK_MSK) == MMC_HIGH_DESITY_CHECK_BIT)
1147 {
1148 gSD2->flags |= SD_FLAG_HCS_SUPPORT;
1149 msdc2_handle->mMSDC_type = MMC42_CARD;
1150 tst_sys_trace("MMC4.2 or higher");
1151 }
1152 else
1153 #endif
1154 msdc2_handle->mMSDC_type = MMC_CARD;
1155 gSD2->mInactive = KAL_FALSE;
1156 gSD2->mSDC_ocr = _ocr;
1157 gSD2->mState = READY_STA;
1158
1159 return NO_ERROR;
1160}
1161
1162/*************************************************************************
1163* FUNCTION
1164* SD_Acmd41_SD
1165*
1166* DESCRIPTION
1167* asks all cards in idle state to send their OCR in the response on the CMD line
1168* OCR: Operation Condition Register
1169*
1170* PARAMETERS
1171*
1172* RETURNS
1173* SDC_CMD_STATUS
1174*
1175* GLOBALS AFFECTED
1176* gSD
1177*
1178* NOTE
1179* only works for SD
1180*
1181*************************************************************************/
1182SDC_CMD_STATUS SD_Acmd41_SD_2(void)
1183{
1184
1185 SDC_CMD_STATUS status;
1186 kal_uint32 _ocr = 0, ocr_i, t1, t2;
1187
1188#if defined(SD_MMC_HIGH_DENSITY_SUPPORT)
1189 if(gSD2->mCMD8Resp == SD_CMD8_RESP_NORESP)
1190 ocr_i = SDC_OCR_DEFAULT;
1191 else if(gSD2->mCMD8Resp == SD_CMD8_RESP_VALID)
1192 ocr_i = (SDC_OCR_DEFAULT|SD_ACMD41_HCS);
1193 else if(gSD2->mCMD8Resp == SD_CMD8_RESP_INVALID)
1194 return ERR_CMD8_INVALID;
1195#else
1196 ocr_i = SDC_OCR_DEFAULT;
1197#endif
1198
1199 msdc2_handle->is_init_timeout = KAL_FALSE;
1200 t2 = drv_get_current_time();
1201 do{
1202 t1 = drv_get_current_time();
1203 MSDC_START_TIMER2(MSDC_TIMEOUT_PERIOD_CMD);
1204 while((MSDC_IS_BUSY2)
1205 && MSDC_Check_Card_Present2() && !msdc2_handle->is_timeout)
1206 {
1207 if(drv_get_duration_ms(t1) > MSDC_TIMEOUT_PERIOD_CMD*11)
1208 msdc2_handle->is_timeout = KAL_TRUE;
1209 };
1210 MSDC_STOP_TIMER2();
1211 if(msdc2_handle->is_timeout)
1212 return MSDC_GPT_TIMEOUT_ERR;
1213 status=SD_Cmd55_2(SDC_RCA_DEFAULT);
1214 if(status != NO_ERROR)
1215 {
1216 return status;
1217 }
1218 MSDC_START_TIMER2(MSDC_TIMEOUT_PERIOD_CMD);
1219 t1 = drv_get_current_time();
1220 while((MSDC_IS_BUSY2)
1221 && MSDC_Check_Card_Present2() && !msdc2_handle->is_timeout)
1222 {
1223 if(drv_get_duration_ms(t1) > MSDC_TIMEOUT_PERIOD_CMD*11)
1224 msdc2_handle->is_timeout = KAL_TRUE;
1225 };
1226 MSDC_STOP_TIMER2();
1227 if(msdc2_handle->is_timeout)
1228 return MSDC_GPT_TIMEOUT_ERR;
1229 MSDC_LSD_WriteReg32(SDC_ARG2,ocr_i);
1230 MSDC_LSD_WriteReg32(SDC_CMD2,SDC_CMD_CMD41_SD);
1231 if((status = SD_WaitCmdRdyOrTo_2()) != NO_ERROR)
1232 {
1233 return status;
1234 }
1235 MSDC_LSD_ReadReg32(SDC_RESP02, &_ocr);
1236 if((_ocr & SDC_OCR_DEFAULT) == 0)
1237 return ERR_OCR_NOT_SUPPORT;
1238 if(!msdc2_handle->mIsPresent)
1239 return ERR_CARD_NOT_PRESENT;
1240 if(!(_ocr&SDC_OCR_BUSY))
1241 {
1242 if(drv_get_duration_ms(t2) > MSDC_TIMEOUT_PERIOD_INI)
1243 {
1244 #ifdef DRV_LSD
1245 LSD_ASSERT(0, __LINE__);
1246 #endif
1247 msdc2_handle->is_init_timeout = KAL_TRUE;
1248 break;
1249 }
1250 if((kal_query_systemInit() == KAL_TRUE)
1251#ifdef __TST_WRITE_TO_FILE_ONLY__ /*error recording: considering error recording additionally*/
1252 || (KAL_TRUE == INT_QueryExceptionStatus())
1253#endif
1254 )
1255 MSDC_GPTI_BusyWait(30);
1256 else
1257 kal_sleep_task(7);
1258 }
1259 else
1260 break;
1261 }
1262 while(1);
1263
1264 if(msdc2_handle->is_init_timeout)
1265 return ERR_R3_OCR_BUSY;
1266
1267 gSD2->mInactive = KAL_FALSE;
1268 gSD2->mSDC_ocr = _ocr;
1269 #if defined(SD_MMC_HIGH_DENSITY_SUPPORT)
1270 gSD2->flags |= SD_FLAG_SD_TYPE_CARD;
1271 if(_ocr & SD_ACMD41_HCS)
1272 {
1273 gSD2->flags |= SD_FLAG_HCS_SUPPORT;
1274 msdc2_handle->mMSDC_type = SD20_HCS_CARD;
1275 tst_sys_trace("SD2.0 or higher");
1276 }
1277 else if(gSD2->mCMD8Resp == SD_CMD8_RESP_VALID)
1278 msdc2_handle->mMSDC_type = SD20_LCS_CARD;
1279 else
1280 #endif
1281 msdc2_handle->mMSDC_type = SD_CARD;
1282 gSD2->mState = READY_STA;
1283
1284 return NO_ERROR;
1285}
1286
1287/*************************************************************************
1288* FUNCTION
1289* SD_GetCID
1290*
1291* DESCRIPTION
1292* Read Card Identification.
1293*
1294* PARAMETERS
1295*
1296* RETURNS
1297* SDC_CMD_STATUS
1298*
1299* GLOBALS AFFECTED
1300* gSD
1301*
1302* NOTE
1303*
1304*
1305*************************************************************************/
1306
1307// Get CID(CMD2)
1308SDC_CMD_STATUS SD_GetCID_2(kal_uint32 Cid[4])
1309{
1310 int i;
1311 SDC_CMD_STATUS status;
1312
1313 if((status = SD_Send_Cmd_2(SDC_CMD_CMD2,SDC_NO_ARG))!=NO_ERROR)
1314 return status;
1315 //read R2
1316 for(i=0;i<4;i++)
1317 MSDC_LSD_ReadReg32((SDC_RESP02+i*sizeof(kal_uint32)), &Cid[i]);
1318 SD_AnalysisCID_2(Cid);
1319 gSD2->mState = IDENT_STA;
1320
1321 return NO_ERROR;
1322}
1323
1324/*************************************************************************
1325* FUNCTION
1326* SD_ValidateRCA
1327*
1328* DESCRIPTION
1329* assing or read RCA
1330*
1331* PARAMETERS
1332* pRca: used for input or output RCA
1333*
1334* RETURNS
1335* SDC_CMD_STATUS
1336*
1337* GLOBALS AFFECTED
1338* gSD
1339*
1340* NOTE
1341* RCA is assinged to MMC card fixed to SDC_RCA_MMC(1)
1342*
1343*************************************************************************/
1344
1345// assign or read RCA
1346SDC_CMD_STATUS SD_ValidateRCA_2(kal_uint16* pRca)
1347{
1348 SDC_CMD_STATUS status;
1349 kal_uint32 resp;
1350 kal_uint8 state;
1351
1352 #if defined(SD_MMC_HIGH_DENSITY_SUPPORT)
1353 if(gSD2->flags & SD_FLAG_SD_TYPE_CARD )
1354 #else
1355 if(msdc2_handle->mMSDC_type == SD_CARD)
1356 #endif
1357 {
1358 //read RCA form card
1359 if((status = SD_Send_Cmd_2(SDC_CMD_CMD3_SD,SDC_NO_ARG))!=NO_ERROR)
1360 return status;
1361 //read R6
1362 MSDC_LSD_ReadReg32(SDC_RESP02, &resp);
1363 *pRca = resp >> 16;
1364 gSD2->mRCA =*pRca;
1365
1366 }
1367 else
1368 {
1369 //assign RCA to card
1370 if((status = SD_Send_Cmd_2(SDC_CMD_CMD3_MMC,(kal_uint32)SDC_RCA_MMC<<16))!=NO_ERROR)
1371 return status;
1372
1373 //read R1
1374 MSDC_LSD_ReadReg32(SDC_RESP02, &resp);
1375 SD_GetStatus_2(SDC_RCA_MMC,&resp);
1376 state = 0;
1377 GetBitFieldN((kal_uint8*)&state,(kal_uint8*)&resp,9,4);
1378 if(STBY_STA != state)
1379 return ERR_RCA_FAIL;
1380 *pRca = gSD2->mRCA = SDC_RCA_MMC;
1381 }
1382
1383 gSD2->mState = STBY_STA;
1384 return NO_ERROR;
1385}
1386
1387/*************************************************************************
1388* FUNCTION
1389* SD_SetDSR
1390*
1391* DESCRIPTION
1392* set default value to the DSR
1393*
1394* PARAMETERS
1395*
1396* RETURNS
1397* SDC_CMD_STATUS
1398*
1399* GLOBALS AFFECTED
1400*
1401* NOTE
1402* SDC_DSR_DEFAULT(0x404)
1403*
1404*************************************************************************/
1405SDC_CMD_STATUS SD_SetDSR_2(void)
1406{
1407 return SD_Send_Cmd_2(SDC_CMD_CMD4,(kal_uint32)SDC_DSR_DEFAULT<<16);
1408}
1409
1410/*************************************************************************
1411* FUNCTION
1412* SD_SelectCard
1413*
1414* DESCRIPTION
1415* select/deselect card
1416*
1417* PARAMETERS
1418* rca: relative card address
1419*
1420* RETURNS
1421* SDC_CMD_STATUS
1422*
1423* GLOBALS AFFECTED
1424*
1425* NOTE
1426*
1427*************************************************************************/
1428SDC_CMD_STATUS SD_SelectCard_2(kal_uint16 rca)
1429{
1430 SDC_CMD_STATUS status;
1431
1432 if((status = SD_Send_Cmd_2(SDC_CMD_CMD7,(kal_uint32)rca<<16))!=NO_ERROR)
1433 return status;
1434
1435 //read R1b
1436 if((status = SD_WaitCardNotBusy_2())!=NO_ERROR)
1437 return status;
1438 if((status = SD_CheckStatus_2())!=NO_ERROR)
1439 return status;
1440
1441 return NO_ERROR;
1442}
1443
1444/*************************************************************************
1445* FUNCTION
1446* SD_GetCSD
1447*
1448* DESCRIPTION
1449* Get CSD from addressed card
1450*
1451* PARAMETERS
1452* rca: relative card address
1453* Csd: used for containing read CSD
1454*
1455* RETURNS
1456* SDC_CMD_STATUS
1457*
1458* GLOBALS AFFECTED
1459*
1460* NOTE
1461*
1462*************************************************************************/
1463SDC_CMD_STATUS SD_GetCSD_2(kal_uint16 rca, kal_uint32 Csd[4])
1464{
1465 SDC_CMD_STATUS status;
1466 kal_uint32 i;
1467
1468 if((status = SD_Send_Cmd_2(SDC_CMD_CMD9,(kal_uint32)rca<<16))!=NO_ERROR)
1469 return status;
1470 for(i=0;i<4;i++)
1471 {
1472 MSDC_LSD_ReadReg32((volatile kal_uint32 *)(SDC_RESP02+i*4), &Csd[i]);
1473 }
1474 SD_AnalysisCSD_2(Csd);
1475
1476 return NO_ERROR;
1477}
1478
1479// addressed send CID
1480SDC_CMD_STATUS SD_GetAddressedCID_2(kal_uint16 rca, kal_uint32 Cid[4])
1481{
1482 SDC_CMD_STATUS status;
1483 kal_uint32 i;
1484
1485 if((status = SD_Send_Cmd_2(SDC_CMD_CMD10,(kal_uint32)rca<<16))!=NO_ERROR)
1486 return status;
1487 for(i=0;i<4;i++)
1488 {
1489 MSDC_LSD_ReadReg32((volatile kal_uint32 *)(SDC_RESP02+i*4), &Cid[i]);
1490 }
1491 return NO_ERROR;
1492}
1493
1494
1495/*************************************************************************
1496* FUNCTION
1497* SD_StopTrans_2
1498*
1499* DESCRIPTION
1500* Stop Muli-Block operation
1501*
1502* PARAMETERS
1503*
1504* RETURNS
1505* SDC_CMD_STATUS
1506*
1507* GLOBALS AFFECTED
1508*
1509* NOTE
1510* definition of SD_STOP_SLOW is used for some erroneous card
1511*************************************************************************/
1512SDC_CMD_STATUS SD_StopTrans_2(kal_bool isTx)
1513{
1514 SDC_CMD_STATUS status;
1515 kal_uint32 retry = 0;
1516
1517 while(retry < 30)
1518 {
1519 if((status = SD_Send_Cmd_2(SDC_CMD_CMD12,SDC_NO_ARG))!=NO_ERROR)
1520 {
1521 retry ++;
1522 }
1523 else
1524 {
1525 break;
1526 }
1527 }
1528 if(retry >= 30)
1529 {
1530 return status;
1531 }
1532 if(isTx)
1533 SD_WaitCardNotBusy_2();
1534
1535
1536#ifdef SD_STOP_SLOW
1537 while(MSDC_Reg(SDC_STA2) & SDC_STA_R1BSY);
1538 do{
1539 SD_GetStatus_2(gSD2->mRCA,(kal_uint32*)&status);
1540 }while((status & R1_CUR_STATE) >> 9 != TRAN_STA);
1541#endif
1542
1543 return NO_ERROR;
1544}
1545
1546/*************************************************************************
1547* FUNCTION
1548* SD_GetStatus_2
1549*
1550* DESCRIPTION
1551* addressed send status
1552*
1553* PARAMETERS
1554*
1555* RETURNS
1556* SDC_CMD_STATUS
1557*
1558* GLOBALS AFFECTED
1559*
1560* NOTE
1561*
1562*************************************************************************/
1563SDC_CMD_STATUS SD_GetStatus_2(kal_uint16 rca, kal_uint32* resp)
1564{
1565 SDC_CMD_STATUS status;
1566
1567 if((status = SD_Send_Cmd_2(SDC_CMD_CMD13,(kal_uint32)rca <<16))!=NO_ERROR)
1568 return status;
1569
1570 MSDC_LSD_ReadReg32(SDC_RESP02,resp);
1571 if((*resp) & SDC_CSTA_MASK)
1572 ASSERT(0);
1573
1574 return NO_ERROR;
1575}
1576
1577/*************************************************************************
1578* FUNCTION
1579* SD_SetBlength_2
1580*
1581* DESCRIPTION
1582* set block length
1583*
1584* PARAMETERS
1585* BKLength: block length u want to set
1586*
1587* RETURNS
1588* SDC_CMD_STATUS
1589*
1590* GLOBALS AFFECTED
1591* gSD2->mBKLength
1592*
1593* NOTE
1594*
1595*************************************************************************/
1596SDC_CMD_STATUS SD_SetBlength_2(kal_uint32 BKLength)
1597{
1598 SDC_CMD_STATUS status;
1599
1600 // maximal value of block length is 2048
1601 if(BKLength > SDC_MAX_BKLENGTH)
1602 return ERR_INVALID_BKLENGTH;
1603 if(!gSD2->mCSD.r_blk_part && BKLength < gSD2->mCSD.max_r_blk_len )
1604 return ERR_INVALID_BKLENGTH;
1605 if((status = SD_Send_Cmd_2(SDC_CMD_CMD16,BKLength))!=NO_ERROR)
1606 return status;
1607 //read R1
1608 status = SD_CheckStatus_2();
1609 // 2. configure the controller
1610 gSD2->mBKLength = BKLength;
1611 BitFieldWrite32((kal_uint32*)SDC_CFG2,BKLength,SDC_CFG_BLKLEN);
1612
1613 return NO_ERROR;
1614}
1615
1616
1617/*************************************************************************
1618* FUNCTION
1619* SD_ReadSingleBlock
1620*
1621* DESCRIPTION
1622* 1. read a single block form data_adrs of card to the rxbuffer
1623* 2. the block length is set by set block length
1624*
1625* PARAMETERS
1626* data_adrs: starting address to read
1627* rxbuffer: as name
1628*
1629* RETURNS
1630* SDC_CMD_STATUS
1631*
1632* GLOBALS AFFECTED
1633*
1634* NOTE
1635* the size of rxbuffer should be 4*n (n : integer)
1636*
1637*************************************************************************/
1638SDC_CMD_STATUS SD_ReadSingleBlock_2(kal_uint32 data_adrs, kal_uint32* rxbuffer)
1639{
1640 kal_uint32 count;
1641 SDC_CMD_STATUS status;
1642
1643
1644 EnableMSDC_DMA2();
1645 count = MSDC_SD_BLOCK_SIZE;
1646 MSDC_DMATransferFirst2((kal_uint32)rxbuffer,count,KAL_FALSE);
1647 if((status = SD_Send_Cmd_2(SDC_CMD_CMD17,data_adrs))!=NO_ERROR)
1648 goto ERR_Exit;
1649 if((status = SD_CheckStatus_2())!=NO_ERROR)
1650 goto ERR_Exit;
1651 status = MSDC_DMATransferFinal2();
1652 if(status != NO_ERROR)
1653 {
1654 goto ERR_Exit;
1655 }
1656 if((status = SD_WaitDatRdyOrTo_2())!=NO_ERROR)
1657 goto ERR_Exit;
1658
1659 DisableMSDC_DMA2();
1660 MSDC_CLR_FIFO2();
1661
1662 return NO_ERROR;
1663ERR_Exit:
1664 {
1665 kal_uint32 tmp;
1666
1667 #ifdef MSDC_USE_INT
1668 kal_set_eg_events(msdc2_handle->MSDC_Events, 0, KAL_AND);
1669 #endif
1670 DisableMSDC_DMA2();
1671 RESET_MSDC2();
1672
1673 // SD_StopTrans_2(KAL_FALSE);
1674 SD_GetStatus_2(gSD2->mRCA,(kal_uint32*)&tmp);
1675 MSDC_LSD_ReadReg32(SDC_DATSTA2,&tmp);
1676 MSDC_CLR_FIFO2();
1677 return status;
1678 }
1679
1680}
1681
1682/*************************************************************************
1683* FUNCTION
1684* SD_ReadMultiBlock
1685*
1686* DESCRIPTION
1687* read num of blocks into rxbuffer
1688*
1689* PARAMETERS
1690* data_adrs: starting address to read
1691* rxbuffer: as name
1692* num: number of blocks to read
1693*
1694* RETURNS
1695* SDC_CMD_STATUS
1696*
1697* GLOBALS AFFECTED
1698*
1699* NOTE
1700*
1701*************************************************************************/
1702SDC_CMD_STATUS SD_ReadMultiBlock_2(kal_uint32 data_adrs, kal_uint32* rxbuffer, kal_uint32 num)
1703{
1704 SDC_CMD_STATUS status;
1705 kal_uint32 j, count;
1706 #ifndef MSDC_DMA
1707 kal_uint32 i;
1708 #endif
1709
1710 EnableMSDC_DMA2();
1711 count = MSDC_SD_BLOCK_SIZE;
1712 MSDC_DMATransferFirst2((kal_uint32)rxbuffer,count*num,KAL_FALSE);
1713 #ifndef DRV_LSD
1714 if((status = SD_Send_Cmd_2(SDC_CMD_CMD18,data_adrs))!=NO_ERROR)
1715 #else
1716 LSD_HostSetBuffer((kal_uint8 *)rxbuffer);
1717 if((status = SD_Send_Cmd_2(SDC_CMD_CMD18|(num<<20) ,data_adrs))!=NO_ERROR)
1718 #endif
1719 goto ERR_Exit;
1720 if((status = SD_CheckStatus_2())!=NO_ERROR)
1721 goto ERR_Exit;
1722 count = MSDC_SD_BLOCK_SIZE;
1723 status = MSDC_DMATransferFinal2();
1724 if(status != NO_ERROR)
1725 goto ERR_Exit;
1726
1727 if((status = SD_WaitDatRdyOrTo_2())!=NO_ERROR)
1728 goto ERR_Exit;
1729
1730
1731 MSDC_CLR_INT2();
1732 DisableMSDC_DMA2();
1733
1734 if(gSD2->flags & SD_FLAG_MMC_MRSW_FAIL)
1735 {
1736 kal_uint32 delay = 200;
1737 while(delay--);
1738 }
1739 if((status = SD_StopTrans_2(KAL_FALSE))!=NO_ERROR)
1740 {
1741 //if((data_adrs/gSD2->mBKLength + j) < gSD2->mBKNum)
1742 goto ERR_Exit;
1743 }
1744 MSDC_CLR_FIFO2();
1745 return NO_ERROR;
1746
1747ERR_Exit:
1748
1749 #ifdef MSDC_USE_INT
1750 kal_set_eg_events(msdc2_handle->MSDC_Events, 0, KAL_AND);
1751 #endif
1752 DisableMSDC_DMA2();
1753 RESET_MSDC2();
1754 SD_StopTrans_2(KAL_FALSE);
1755 SD_GetStatus_2(gSD2->mRCA,(kal_uint32*)&j);
1756 MSDC_LSD_ReadReg32(SDC_DATSTA2,&j);
1757 MSDC_CLR_FIFO2();
1758 return status;
1759
1760}
1761
1762/*************************************************************************
1763* FUNCTION
1764* SD_WriteSingleBlock
1765*
1766* DESCRIPTION
1767* write a single block
1768*
1769* PARAMETERS
1770* address: starting address to write
1771* txbuffer: as name
1772*
1773* RETURNS
1774* SDC_CMD_STATUS
1775*
1776* GLOBALS AFFECTED
1777*
1778* NOTE
1779* block length is set by Set_Block_Length
1780*
1781*************************************************************************/
1782SDC_CMD_STATUS SD_WriteSingleBlock_2(kal_uint32 address, kal_uint32* txbuffer)
1783{
1784 SDC_CMD_STATUS status;
1785 kal_uint32 count;
1786 kal_uint32 *ptr;
1787#if defined(MT6225)
1788 kal_bool is_aligned;
1789 ECO_VERSION eco;
1790 eco = INT_ecoVersion();
1791#endif
1792
1793 if(gSD2->mWPEnabled)
1794 return ERR_WRITE_PROTECT;
1795 EnableMSDC_DMA2();
1796 count = MSDC_SD_BLOCK_SIZE;
1797
1798 #if defined(MT6225)
1799 if(eco <= ECO_E3)
1800 {
1801 is_aligned = ((kal_uint32)txbuffer%4 == 0);
1802 if(is_aligned)
1803 {
1804 ptr = txbuffer;
1805 }
1806 else
1807 {
1808 kal_mem_cpy(MSDC_Sector2, txbuffer, 512);
1809 ptr = MSDC_Sector2;
1810 }
1811 }
1812 else
1813 {
1814 ptr = txbuffer;
1815 }
1816 #else
1817 ptr = txbuffer;
1818 #endif
1819
1820 EnableMSDC_DMA2();
1821 count = MSDC_SD_BLOCK_SIZE;
1822 MSDC_DMATransferFirst2((kal_uint32)ptr,count,KAL_TRUE);
1823 if((status = SD_Send_Cmd_2(SDC_CMD_CMD24,address))!=NO_ERROR)
1824 goto ERR_Exit;
1825 if((status = SD_CheckStatus_2())!=NO_ERROR)
1826 goto ERR_Exit;
1827 status = MSDC_DMATransferFinal2();
1828 if(status != NO_ERROR)
1829 goto ERR_Exit;
1830 if((status = SD_WaitCardNotBusy_2())!=NO_ERROR)
1831 goto ERR_Exit;
1832 DisableMSDC_DMA2();
1833 if((status = SD_WaitDatRdyOrTo_2())!=NO_ERROR)
1834 goto ERR_Exit;
1835
1836
1837 return NO_ERROR;
1838ERR_Exit:
1839 {
1840 kal_uint32 tmp;
1841
1842 #ifdef MSDC_USE_INT
1843 kal_set_eg_events(msdc2_handle->MSDC_Events, 0, KAL_AND);
1844 #endif
1845 DisableMSDC_DMA2();
1846 RESET_MSDC2();
1847 SD_GetStatus_2(gSD2->mRCA,(kal_uint32*)&tmp);
1848 MSDC_LSD_ReadReg32(SDC_DATSTA2,&tmp);
1849 return status;
1850 }
1851
1852}
1853
1854/*************************************************************************
1855* FUNCTION
1856* SD_WriteMultiBlock
1857*
1858* DESCRIPTION
1859* write num blocks starting at address
1860*
1861* PARAMETERS
1862* address: starting address to write
1863* txbuffer: as name
1864* num: number of blocks to write
1865*
1866* RETURNS
1867* SDC_CMD_STATUS
1868*
1869* GLOBALS AFFECTED
1870*
1871* NOTE
1872* block length is set by Set_Block_Length
1873*
1874*************************************************************************/
1875SDC_CMD_STATUS SD_WriteMultiBlock_2(kal_uint32 address, kal_uint32* txbuffer, kal_uint32 num)
1876{
1877 SDC_CMD_STATUS status;
1878 kal_uint32 count;
1879 kal_uint32 *ptr;
1880 #if defined(MT6225)
1881 kal_bool is_aligned, dma_issue;
1882 ECO_VERSION eco;
1883 eco = INT_ecoVersion();
1884 if(eco <= ECO_E3)
1885 dma_issue = KAL_TRUE;
1886 #endif
1887
1888 if(gSD2->mWPEnabled)
1889 return ERR_WRITE_PROTECT;
1890 EnableMSDC_DMA2();
1891 count = MSDC_SD_BLOCK_SIZE;
1892 #if defined(MT6225)
1893 if(dma_issue)
1894 {
1895 is_aligned = ((kal_uint32)txbuffer%4 == 0);
1896 if(is_aligned)
1897 {
1898 MSDC_DMATransferFirst2((kal_uint32)txbuffer,count*num,KAL_TRUE);
1899 }
1900 else
1901 {
1902 kal_mem_cpy(MSDC_Sector2, txbuffer, 512);
1903 ptr = txbuffer;
1904 MSDC_DMATransferFirst2((kal_uint32)MSDC_Sector2,count,KAL_TRUE);
1905 }
1906 }
1907 else
1908 {
1909 MSDC_DMATransferFirst2((kal_uint32)txbuffer,count*num,KAL_TRUE);
1910 }
1911 #elif defined(MSDC_MULTI_BLOCK_WRITE)
1912 MSDC_DMATransferFirst2((kal_uint32)txbuffer,count*num,KAL_TRUE);
1913 #else
1914 ptr = txbuffer;
1915 MSDC_DMATransferFirst2((kal_uint32)ptr,count,KAL_TRUE);
1916 #endif
1917 if((status = SD_Send_Cmd_2(SDC_CMD_CMD25,address))!=NO_ERROR)
1918 goto ERR_Exit;
1919 if((status = SD_CheckStatus_2())!=NO_ERROR)
1920 goto ERR_Exit;
1921
1922 #if defined(MT6225)
1923 if(dma_issue)
1924 {
1925 if(is_aligned)
1926 {
1927 status = MSDC_DMATransferFinal2();
1928 if(status != NO_ERROR)
1929 goto ERR_Exit;
1930 if((status = SD_WaitDatRdyOrTo_2())!=NO_ERROR)
1931 goto ERR_Exit;
1932 }
1933 else
1934 {
1935 kal_uint32 j;
1936
1937 for(j=0;j<num;j++)
1938 {
1939 if(msdc2_handle->mIsPresent == KAL_FALSE)
1940 {
1941 status = ERR_CARD_NOT_PRESENT;
1942 goto ERR_Exit;
1943 }
1944 if(j!=0)
1945 {
1946 kal_mem_cpy(MSDC_Sector2, ptr, 512);
1947 MSDC_DMATransferFirst2((kal_uint32)MSDC_Sector2,count,KAL_TRUE);
1948 }
1949 status = MSDC_DMATransferFinal2();
1950 ptr += MSDC_SD_BLOCK_SIZE;
1951 if(status != NO_ERROR)
1952 goto ERR_Exit;
1953 if((status = SD_WaitDatRdyOrTo_2())!=NO_ERROR)
1954 goto ERR_Exit;
1955 }
1956 }
1957 }
1958 else
1959 {
1960 status = MSDC_DMATransferFinal2();
1961 if(status != NO_ERROR)
1962 goto ERR_Exit;
1963 if((status = SD_WaitDatRdyOrTo_2())!=NO_ERROR)
1964 goto ERR_Exit;
1965 }
1966 #elif defined(MSDC_MULTI_BLOCK_WRITE)
1967 status = MSDC_DMATransferFinal2();
1968 if(status != NO_ERROR)
1969 goto ERR_Exit;
1970 if((status = SD_WaitDatRdyOrTo_2())!=NO_ERROR)
1971 goto ERR_Exit;
1972 #else // not MT6225
1973 {
1974 kal_uint32 j;
1975
1976 for(j=0;j<num;j++)
1977 {
1978 if(msdc2_handle->mIsPresent == KAL_FALSE)
1979 {
1980 status = ERR_CARD_NOT_PRESENT;
1981 goto ERR_Exit;
1982 }
1983 if(j!=0)
1984 {
1985 MSDC_DMATransferFirst2((kal_uint32)ptr,count,KAL_TRUE);
1986 }
1987 status = MSDC_DMATransferFinal2();
1988 ptr += MSDC_SD_BLOCK_SIZE;
1989 if(status != NO_ERROR)
1990 goto ERR_Exit;
1991 if((status = SD_WaitDatRdyOrTo_2())!=NO_ERROR)
1992 goto ERR_Exit;
1993 }
1994
1995 }
1996 #endif // MT6225
1997
1998 DisableMSDC_DMA2();
1999 if((status = SD_StopTrans_2(KAL_TRUE))!=NO_ERROR)
2000 goto ERR_Exit;
2001
2002 MSDC_CLR_INT2();
2003
2004 return NO_ERROR;
2005ERR_Exit:
2006 {
2007 kal_uint32 tmp;
2008
2009 DisableMSDC_DMA2();
2010 RESET_MSDC2();
2011 SD_StopTrans_2(KAL_TRUE);
2012 SD_GetStatus_2(gSD2->mRCA,(kal_uint32*)&tmp);
2013 MSDC_LSD_ReadReg32(SDC_DATSTA2,&tmp);
2014 return status;
2015 }
2016
2017}
2018/*************************************************************************
2019* FUNCTION
2020* SD_SetBusWidth
2021*
2022* DESCRIPTION
2023* ACMD6: set the data width 00 for 1 bit, 10 for 4 bits
2024*
2025* PARAMETERS
2026* width: indicate the bus width
2027*
2028* RETURNS
2029* SDC_CMD_STATUS
2030*
2031* GLOBALS AFFECTED
2032*
2033* NOTE
2034* Not every card support 4-bits bus
2035* only for SD
2036*
2037*************************************************************************/
2038SDC_CMD_STATUS SD_SetBusWidth_2(SD_BITWIDTH width)
2039{
2040 SDC_CMD_STATUS status;
2041
2042 // check if card support 4 bits bus
2043 if((width == BIT_4W) && !(gSD2->mSCR.bus_width&0x04))
2044 return ERR_NOT_SUPPORT_4BITS;
2045 // send APP_CMD
2046 if((status = SD_Cmd55_2(gSD2->mRCA))!=NO_ERROR)
2047 return status;
2048 // send cmd6
2049 if((status = SD_Send_Cmd_2(SDC_CMD_ACMD6,width))!=NO_ERROR)
2050 return status;
2051 //read R1
2052 if((status = SD_CheckStatus_2())!=NO_ERROR)
2053 return status;
2054 // set the controler MDLEN to enalbe 4bits bus width
2055 MSDC_LSD_SetBits32(SDC_CFG2,SDC_CFG_MDLEN);
2056 gSD2->bus_width = 4;
2057
2058 return NO_ERROR;
2059}
2060
2061/*************************************************************************
2062* FUNCTION
2063* SD_ReadSCR
2064*
2065* DESCRIPTION
2066* ACMD51: read the SD Configuration Register(8bytes block read)
2067*
2068* PARAMETERS
2069* scr: used for store SCR
2070*
2071* RETURNS
2072* SDC_CMD_STATUS
2073*
2074* GLOBALS AFFECTED
2075*
2076* NOTE
2077* Make sure the size of SCR is 8 bytes
2078*
2079*************************************************************************/
2080SDC_CMD_STATUS SD_ReadSCR_2(kal_uint32* scr)
2081{
2082 SDC_CMD_STATUS status;
2083 kal_uint32 blklen,i, t1;
2084
2085 ASSERT((kal_uint32)scr % 4 == 0);
2086 // save the original block length
2087 blklen = gSD2->mBKLength;
2088 // set block length(MSDC_CFG2)
2089 if((status = SD_SetBlength_2(8))!=NO_ERROR)
2090 return status;
2091 // send APP_CMD
2092 if((status = SD_Cmd55_2(gSD2->mRCA))!=NO_ERROR)
2093 return status;
2094 // send command
2095 if((status = SD_Send_Cmd_2(SDC_CMD_ACMD51,SDC_NO_ARG))!=NO_ERROR)
2096 return status;
2097 //read R1
2098 if((status = SD_CheckStatus_2())!=NO_ERROR)
2099 return status;
2100 // read data(8bytes)
2101#ifndef DRV_LSD
2102 // failed to use DMA with burst mode
2103 t1 = drv_get_current_time();
2104 MSDC_START_TIMER2(MSDC_TIMEOUT_PERIOD_DAT);
2105 for(i=0;i<2;)
2106 {
2107 if(drv_get_duration_ms(t1) > MSDC_TIMEOUT_PERIOD_DAT*11)
2108 msdc2_handle->is_timeout = KAL_TRUE;
2109 if(!msdc2_handle->mIsPresent)
2110 return ERR_CARD_NOT_PRESENT;
2111 if(msdc2_handle->is_timeout)
2112 return MSDC_GPT_TIMEOUT_ERR;
2113 if(!MSDC_IS_FIFO_EMPTY2)
2114 {
2115 *(kal_uint32*)(scr+i) = MSDC_Reg32(MSDC_DAT2);
2116 i++;
2117 }
2118 }
2119#else
2120 LSD_readFIFO(scr ,2);
2121
2122#endif
2123 MSDC_STOP_TIMER2();
2124 // analysis scr
2125 SD_AnalysisSCR(scr);
2126 // clean EVENT_SDDATIRQ
2127 #ifdef MSDC_USE_INT
2128 kal_set_eg_events(msdc2_handle->MSDC_Events, 0, KAL_AND);
2129 #endif
2130 MSDC_CLR_FIFO2();
2131 return NO_ERROR;
2132
2133}
2134
2135/*************************************************************************
2136* FUNCTION
2137* SD_SetPreEraseBlk
2138*
2139* DESCRIPTION
2140* ACMD23: set the number of write blocksto be pre-erased before writing
2141* used for faster multiple Block Write
2142*
2143* PARAMETERS
2144* num: used for storing number of blocks during multi-block operation
2145*
2146* RETURNS
2147* SDC_CMD_STATUS
2148*
2149* GLOBALS AFFECTED
2150*
2151* NOTE
2152*
2153*************************************************************************/
2154SDC_CMD_STATUS SD_SetPreEraseBlk_2(kal_uint32 num)
2155{
2156 SDC_CMD_STATUS status;
2157
2158 //[22:0] number of blocks
2159 num &= 0x003FFF;
2160 // send APP_CMD
2161 if((status = SD_Cmd55_2(gSD2->mRCA))!=NO_ERROR)
2162 return status;
2163 // send CMD23
2164 if((status = SD_Send_Cmd_2(SDC_CMD_ACMD23,num))!=NO_ERROR)
2165 return status;
2166 //read R1
2167 if((status = SD_CheckStatus_2())!=NO_ERROR)
2168 return status;
2169
2170 return NO_ERROR;
2171}
2172
2173/*************************************************************************
2174* FUNCTION
2175* SD_EraseCmdClass
2176*
2177* DESCRIPTION
2178* groups of erase commands including CMD32 ~CMD38
2179*
2180* PARAMETERS
2181* cmd: indicate which command to execute
2182* address: starting address wiht write protection
2183*
2184* RETURNS
2185* SDC_CMD_STATUS
2186*
2187* GLOBALS AFFECTED
2188*
2189* NOTE
2190* CMD34~CMD37 are only for MMC
2191*
2192*************************************************************************/
2193SDC_CMD_STATUS SD_EraseCmdClass_2(kal_uint32 cmd ,kal_uint32 address)
2194{
2195 SDC_CMD_STATUS status;
2196
2197 if(cmd != SDC_CMD_CMD38)
2198 {
2199 if((status = SD_Send_Cmd_2(cmd,address))!=NO_ERROR)
2200 return status;
2201 }
2202 else if((status = SD_Send_Cmd_2(cmd,SDC_NO_ARG))!=NO_ERROR)
2203 return status;
2204
2205 //read R1
2206 if((status = SD_CheckStatus_2())!=NO_ERROR)
2207 return status;
2208
2209 if(cmd == SDC_CMD_CMD38)
2210 {
2211 SD_WaitCardNotBusy_2();
2212 do{
2213 SD_GetStatus_2(gSD2->mRCA,(kal_uint32*)&status);
2214 if(msdc2_handle->mIsPresent == KAL_FALSE)
2215 break;
2216 }while(CurState(status)!= TRAN_STA);
2217 }
2218
2219 return NO_ERROR;
2220}
2221/*************************************************************************
2222* FUNCTION
2223* SD_Switch_MMC40
2224*
2225* DESCRIPTION
2226* CMD6: set the command set or write to the EXT_CSD (for MMC4.0)
2227*
2228* PARAMETERS
2229* access: access mode
2230* index: index to EXT_CSD
2231* value: value to write to EXT_CSD
2232* set: selected command set
2233*
2234* RETURNS
2235* SDC_CMD_STATUS
2236*
2237* GLOBALS AFFECTED
2238*
2239* NOTE
2240*
2241*************************************************************************/
2242SDC_CMD_STATUS SD_Switch_MMC40_2(kal_uint8 access, kal_uint8 index, kal_uint8 value, kal_uint8 set)
2243{
2244 SDC_CMD_STATUS status;
2245 kal_uint32 arg = 0;
2246
2247 arg = (access<<24)|(index<<16)|(value<<8)|set;
2248 // send command
2249 if((status = SD_Send_Cmd_2(SDC_CMD_CMD6_MMC40,arg))!=NO_ERROR)
2250 return status;
2251 //read R1
2252 if((status = SD_CheckStatus_2())!=NO_ERROR)
2253 return status;
2254
2255 return NO_ERROR;
2256}
2257
2258/*************************************************************************
2259* FUNCTION
2260* SD_SendEXTCSD_MMC40
2261*
2262* DESCRIPTION
2263* CMD8: read the content of EXT_CSD register
2264*
2265* PARAMETERS
2266* kal: access mode
2267* index: index to EXT_CSD
2268* value: value to write to EXT_CSD
2269* set: selected command set
2270*
2271* RETURNS
2272* SDC_CMD_STATUS
2273*
2274* GLOBALS AFFECTED
2275*
2276* NOTE
2277*
2278*************************************************************************/
2279SDC_CMD_STATUS SD_SendEXTCSD_MMC40_2(kal_uint32* rxbuffer)
2280{
2281 SDC_CMD_STATUS status;
2282 kal_bool retry_4bit = KAL_FALSE;
2283
2284start:
2285 // read the block of 512 bytes (make sure the rxbuffer is 4 byte aligned)
2286 EnableMSDC_DMA2();
2287 MSDC_DMATransferFirst2((kal_uint32)rxbuffer,128,KAL_FALSE);
2288#ifdef DRV_LSD
2289 LSD_HostSetBuffer((kal_uint8 *)rxbuffer);
2290#endif
2291 if((status = SD_Send_Cmd_2(SDC_CMD_CMD8_MMC40,SDC_NO_ARG))!=NO_ERROR)
2292 goto ERR_Exit;
2293 //read R1
2294 if((status = SD_CheckStatus_2())!=NO_ERROR)
2295 goto ERR_Exit;
2296 // read the block of 512 bytes (make sure the rxbuffer is 4 byte aligned)
2297 status = MSDC_DMATransferFinal2();
2298 if(status != NO_ERROR)
2299 goto ERR_Exit;
2300 if((status = SD_WaitDatRdyOrTo_2())!=NO_ERROR)
2301 goto ERR_Exit;
2302
2303 DisableMSDC_DMA2();
2304 MSDC_CLR_FIFO2();
2305 gSD2->mCSD.ext_csd = (T_EXT_CSD_MMC40 *)rxbuffer;
2306 return NO_ERROR;
2307
2308ERR_Exit:
2309
2310 if(retry_4bit == KAL_FALSE)
2311 {
2312 retry_4bit = KAL_TRUE;
2313 MSDC_LSD_SetBits32(SDC_CFG2,SDC_CFG_MDLEN);
2314 gSD2->bus_width = 4;
2315 goto start;
2316 }
2317 MSDC_LSD_ClearBits32(SDC_CFG2,SDC_CFG_MDLEN);
2318 gSD2->bus_width = 1;
2319 DisableMSDC_DMA2();
2320 MSDC_CLR_FIFO2();
2321 RESET_MSDC2();
2322
2323 return status;
2324}
2325
2326/*************************************************************************
2327* FUNCTION
2328* SD_Switch_SD11
2329*
2330* DESCRIPTION
2331* CMD6: switch command to query and select the specific functions. (SD1.1 or later)
2332* PARAMETERS
2333* arg: argument
2334* resp: buffer to contain the ther 64 bytes status information
2335*
2336* RETURNS
2337* SDC_CMD_STATUS
2338*
2339* GLOBALS AFFECTED
2340*
2341* NOTE
2342*
2343*************************************************************************/
2344SDC_CMD_STATUS SD_Switch_SD11_2(kal_uint32 arg, T_SWITCH_STATUS* info)
2345{
2346 SDC_CMD_STATUS status = NO_ERROR;
2347
2348 BitFieldWrite32((kal_uint32*)SDC_CFG2,SD_CMD6_RESP_LEN,SDC_CFG_BLKLEN);
2349 EnableMSDC_DMA2();
2350 MSDC_DMATransferFirst2((kal_uint32)info,(SD_CMD6_RESP_LEN>>2),KAL_FALSE);
2351 if((status = SD_Send_Cmd_2(SDC_CMD_CMD6_SD11,arg))!=NO_ERROR)
2352 goto exit;
2353 if((status = SD_CheckStatus_2())!=NO_ERROR)
2354 goto exit;
2355 status = MSDC_DMATransferFinal2();
2356
2357exit:
2358 DisableMSDC_DMA2();
2359 return status;
2360}
2361
2362/*************************************************************************
2363* FUNCTION
2364* SD_Switch_SD11
2365*
2366* DESCRIPTION
2367* Enable the high speed interface to support up to 50M Hz clock
2368*
2369* PARAMETERS
2370* arg: argument
2371* resp: buffer to contain the ther 64 bytes status information
2372*
2373* RETURNS
2374* SDC_CMD_STATUS
2375*
2376* GLOBALS AFFECTED
2377*
2378* NOTE
2379*
2380*************************************************************************/
2381SDC_CMD_STATUS SD_SelectHighSpeed_SD11_2(void)
2382{
2383 SDC_CMD_STATUS status;
2384 T_SWITCH_STATUS *p = (T_SWITCH_STATUS*)MSDC_Sector2;
2385
2386 if((status = SD_Switch_SD11(SD_CMD6_QUERY_HIGH_SPEED, p))!=NO_ERROR)
2387 return status;
2388 if(p->max_current == 0)
2389 return ERR_SD_HS_FAIL;
2390 if((p->group1_info & (1 << SD_FUNC_HIGH_SPEED)) &&
2391 (p->group1_result == SD_FUNC_HIGH_SPEED))
2392 {
2393 if((status = SD_Switch_SD11(SD_CMD6_SELECT_HIGH_SPEED, p))!=NO_ERROR)
2394 return status;
2395 if(p->max_current == 0)
2396 return ERR_SD_HS_FAIL;
2397 if(p->group1_result == SD_FUNC_HIGH_SPEED)
2398 gSD2->flags |= SD_FLAG_HS_SUPPORT;
2399 }
2400 else
2401 return ERR_SD_HS_FAIL;
2402
2403
2404 return NO_ERROR;
2405}
2406
2407kal_bool MSDC_ModuleTest_Report_2(void)
2408{
2409 return msdc2_handle->mIsInitialized;
2410}
2411
2412#endif // defined(__MSDC2_SD_MMC__) || defined(__MSDC2_SD_SDIO__)
2413
2414#endif //DRV_MSDC_OFF