sd2.c 50.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236
/*****************************************************************************
*  Copyright Statement:
*  --------------------
*  This software is protected by Copyright and the information contained
*  herein is confidential. The software may not be copied and the information
*  contained herein may not be used or disclosed except with the written
*  permission of MediaTek Inc. (C) 2005
*
*  BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
*  THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
*  RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
*  AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
*  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
*  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
*  NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
*  SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
*  SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
*  THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
*  NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
*  SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
*
*  BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
*  LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
*  AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
*  OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
*  MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE. 
*
*  THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
*  WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
*  LAWS PRINCIPLES.  ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
*  RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
*  THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
*
*****************************************************************************/

/*****************************************************************************
 *
 * Filename:
 * ---------
 *   sd.c
 *
 * Project:
 * --------
 *   Maui_Software
 *
 * Description:
 * ------------
 *   driver functons for SD/MMC 
 *   
 *
 * Author:
 * -------
 * -------
 *
 *============================================================================
 *             HISTORY
 * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *------------------------------------------------------------------------------
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 *------------------------------------------------------------------------------
 * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *============================================================================ 
 ****************************************************************************/
#ifndef DRV_MSDC_OFF
#include "kal_release.h"
//RHR ADD
#include "kal_general_types.h"
//#include "btif_sw.h"
#include "kal_public_api.h"
#include "kal_internal_api.h"
#include "kal_public_defs.h"
//RHR REMOVE
/*
#include "app_buff_alloc.h"
#include "intrCtrl.h"
#include "gpio_sw.h"
#include "init.h"
*/
//RHR
#include "drv_comm.h"
#include "msdc_reg_adap.h"
#include "msdc_def.h"
#include "sd_def.h"
#include "reg_base.h"
#include "upll_ctrl.h"

#if defined(__MSDC2_SD_MMC__) || defined(__MSDC2_SD_SDIO__)
T_SDC_HANDLE	*gSD2 = &gSD_blk[SD_MSDC2];
extern MSDC_HANDLE *msdc2_handle;
extern kal_uint32 MSDC_Sector2[128];

extern kal_uint8 MSDC_GetIOCtrlParam(void);
extern kal_bool INT_USBBoot(void);
extern int MSDC_GetCustom(void);

/*************************************************************************
* FUNCTION
*  SD_Acmd42
*
* DESCRIPTION
*	connect/disconnect the 50K Ohm pull-up resistor on CD/DAT3
*
* PARAMETERS
*
* RETURNS
*	SDC_CMD_STATUS
*
* GLOBALS AFFECTED
*	gSD
*
* NOTE
*	
*
*************************************************************************/
SDC_CMD_STATUS SD_Acmd42_2(kal_bool connect)
{
	SDC_CMD_STATUS status;

	// send APP_CMD
	if((status = SD_Cmd55_2(gSD2->mRCA))!=NO_ERROR)
		return status;
	// send cmd6
	if((status = SD_Send_Cmd_2(SDC_CMD_ACMD42,connect))!=NO_ERROR)
		return status;
	//read R1
	if((status = SD_CheckStatus_2())!=NO_ERROR)
		return status;	

	gSD2->mCD_DAT3 = KAL_FALSE;	// pull-up resistor is disconnected for data trnasfer
	return NO_ERROR;
}

/*************************************************************************
* FUNCTION
*  SD_SetMMC40_4bit_high_speed
*
* DESCRIPTION
*	Check inserted card is SD or MMC
*
* PARAMETERS
*	
*
* RETURNS
*  SD_CARD or MMC_CARD
*
* GLOBALS AFFECTED
*	gMSDC_Handle
*
*************************************************************************/
SDC_CMD_STATUS SD_SetMMC40_bus_high_speed_2(void)
{
		kal_uint32 clock, hs;
	
		MSDC_LSD_WriteReg32(MSDC_IOCON2,0x1B);


		if(SD_SetBlength_2(512)!=NO_ERROR)			
			goto err;
		
		// read the EXT_CSD
		if(SD_SendEXTCSD_MMC40_2(MSDC_Sector2) != NO_ERROR)
			goto err;;

		// set high speed
		if(gSD2->mCSD.ext_csd->card_type & HS_52M)
		{
			// should be 52000
			//clock = 52000;
                        clock = msdc2_handle->msdc_clock / 2;
			hs = 1;
			//MSDC_LSD_WriteReg32(MSDC_IOCON,0x010002FF);
			MSDC_LSD_ClearBits32(MSDC_CFG2,MSDC_CFG_CRED);;
			
		}
		else if(gSD2->mCSD.ext_csd->card_type & HS_26M)
		{
			// should be 26000
			clock = 26000;
			hs = 1;
		}
		else
		{
			clock = 13000;
			hs = 0;
		}
		if(hs)
		{
			// select proper power class
			if(SD_Switch_MMC40_2(SET_BYTE,EXT_CSD_POW_CLASS_INDEX,
				(gSD2->mCSD.ext_csd->pwr_52_360&0xf) ,0) != NO_ERROR)
				goto err;
			
			// enable high speed (26M or 52M)
			if(SD_Switch_MMC40_2(SET_BYTE,EXT_CSD_HIGH_SPPED_INDEX,
				EXT_CSD_ENABLE_HIGH_SPEED,0) != NO_ERROR)
				goto err;

			// latch data at falling edge to cover the card driving capability
			// MSDC_LSD_SetBits32(MSDC_CFG2,MSDC_CFG_RED);			
		}
		
		msdc2_handle->msdc_clock = MSDC_CLOCK;
		MSDC_SetClock2(clock);
	

		
		if(SD_SendEXTCSD_MMC40_2(MSDC_Sector2) != NO_ERROR)
			goto err;;
				
		return NO_ERROR;
err:
		return ERR_MMC_BUS_HS_ERROR;
}
/*************************************************************************
* FUNCTION
*  SD_CheckSDorMMC
*
* DESCRIPTION
*	Check inserted card is SD or MMC
*
* PARAMETERS
*	
*
* RETURNS
*  SD_CARD or MMC_CARD
*
* GLOBALS AFFECTED
*	gMSDC_Handle
*
*************************************************************************/
T_MSDC_CARD SD_CheckSDorMMC_2()
{
	SDC_CMD_STATUS status; 

	SD_Cmd8_2();

	if((status = SD_Acmd41_SD_2())==NO_ERROR)
		return msdc2_handle->mMSDC_type;	// SD_CARD
	else if((status = SD_Cmd1_MMC_2())==NO_ERROR)
		return msdc2_handle->mMSDC_type;	// MMC_CARD

	return UNKNOWN_CARD;
}
/*************************************************************************
* FUNCTION
*  SD_SetDefault
*
* DESCRIPTION
*	set default values to gSD
*
* PARAMETERS
*	
*
* RETURNS
*
* GLOBALS AFFECTED
*	gSD
*
*************************************************************************/
void SD_SetDefault_2(void)
{
	kal_mem_set(gSD2,0,sizeof(T_SDC_HANDLE));
	gSD2->mBKLength = 512;
	gSD2->mRCA = 0;
	gSD2->mInactive = KAL_FALSE;
	gSD2->mState = IDLE_STA;
	gSD2->bus_width = 1;
	gSD2->mCD_DAT3 = KAL_TRUE;
}

void SD_Use24M_Clock_2(void)
{
	if(msdc2_handle->mMSDC_type == SD_CARD || msdc2_handle->mMSDC_type == SD20_LCS_CARD
		|| msdc2_handle->mMSDC_type == SD20_HCS_CARD)
	{
		UPLL_Enable(UPLL_OWNER_MSDC2);
		MSDC_LSD_SetBits32(MSDC_CFG2, MSDC_CFG_CLKSRC);
		msdc2_handle->msdc_clock = MSDC_CLOCK_USB;
		MSDC_SetClock2(MSDC_SD_OP_CLOCK);
		gSD2->flags |= SD_FLAG_USE_USB_CLK;	
	}
}

void SD_Use13M_Clock_2(void)
{
	MSDC_LSD_ClearBits32(MSDC_CFG2, MSDC_CFG_CLKSRC);
	msdc2_handle->msdc_clock = MSDC_CLOCK;
	MSDC_SetClock2(MSDC_OP_CLOCK);
	gSD2->flags &= ~SD_FLAG_USE_USB_CLK;
}


/*************************************************************************
* FUNCTION
*  SD_Initialize
*
* DESCRIPTION
*	Initial SD controller and card
*
* PARAMETERS
*	
*
* RETURNS
*  SDC_CMD_STATUS
*
* GLOBALS AFFECTED
*	gSD
*
*************************************************************************/
SDC_CMD_STATUS SD_Initialize_2(void)
{
	kal_uint32 cid[4],csd[4],scr[4];
	kal_uint16 rca, iocon;
	SDC_CMD_STATUS status;

	if(msdc2_handle->mIsInitialized == KAL_TRUE)
	{
		return NO_ERROR;
	}
	// reset the events
	kal_set_eg_events(msdc2_handle->MSDC_Events, 0, KAL_AND);
	// reset msdc
	if(MSDC_Reg32(MSDC_CFG2) & MSDC_CFG_RST)
	{
		MSDC_LSD_ClearBits32(MSDC_CFG2, MSDC_CFG_RST);
	}
	else
	{
		Reset_MSDC2();
	}
	
	
	// set the output driving capability from customization interface
	iocon = MSDC_SetData(MSDC_IOCON2, 0xff, MSDC_GetIOCtrlParam2());		
	// set pull up the data and cmd 
	BitFieldWrite32((kal_uint32*)MSDC_CFG2,(kal_uint32)2,MSDC_CFG_PRCFG0);
	BitFieldWrite32((kal_uint32*)MSDC_CFG2,(kal_uint32)2,MSDC_CFG_PRCFG1);
	BitFieldWrite32((kal_uint32*)MSDC_CFG2,(kal_uint32)2,MSDC_CFG_PRCFG2);	
	// set read timeout x5ms
	BitFieldWrite32((kal_uint32*)SDC_CFG2,(kal_uint32)40,SDC_CFG_DTOC);
	//set clock of serial clcok for initialization
	MSDC_LSD_ClearBits32(MSDC_CFG2, MSDC_CFG_CLKSRC);
	msdc2_handle->msdc_clock = MSDC_CLOCK;
	MSDC_SetClock2(MSDC_INI_CLOCK);	
	MSDC_LSD_ClearBits32(SDC_CFG2,SDC_CFG_MDLEN);
	// initial global sturctures
	SD_SetDefault_2();
		
	// send the card to IDLE state
	if((status = SD_Reset_2())!=NO_ERROR)
	{
		goto err;
	}

	// and validate the OCR  (CMD0,CMD1 or ADMD41)
	if(SD_CheckSDorMMC_2() == UNKNOWN_CARD)
	{
		status = ERR_STATUS;
		goto err;
	}

	// get CID(CMD2)
	if((status = SD_GetCID_2(cid))!=NO_ERROR)
	{
		goto err;
	}
	// get or set RCA(CMD3)
	if((status = SD_ValidateRCA_2(&rca))!=NO_ERROR)
	{
		goto err;
	}
	// get CSD and analysis the CSD(CMD9)
	if((status = SD_GetCSD_2(gSD2->mRCA,csd))!=NO_ERROR)
	{
		goto err;
	}		
	// Set driver stage register DSR to default value (0x0404)(CMD4)
	if(gSD2->mCSD.dsr_imp)
		if((status = SD_SetDSR_2())!=NO_ERROR)
		{
			//dbg_print("6\r\n");
			goto err;
		}
	
	if(MSDC_GetCustom() & MSDC2_WP)
	{
		if((MSDC_Reg32(SDC_STA2) & SDC_STA_WP))
			gSD2->mWPEnabled = KAL_TRUE;
	}
	status = SD_SelectCard_2(gSD2->mRCA);
	if(status == CARD_IS_LOCKED)
		gSD2->mIsLocked = KAL_TRUE;

	if(gSD2->flags & SD_FLAG_SD_TYPE_CARD)
	{		
		SD_Use24M_Clock_2();
		MSDC_SetIOCONRegDLT2();
		if((status = SD_ReadSCR_2(scr))!=NO_ERROR)
		{
			goto err;
		}	
		#if 0 // MSDC2 only support 1-bit data
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
		#endif
		
		
		if((status = SD_Acmd42_2(KAL_FALSE))!=NO_ERROR)
		{
			goto err;
		}

		
		if(gSD2->flags & SD_FLAG_CMD6_SUPPORT)
		{
			status = SD_SelectHighSpeed_SD11_2();
			if(status == NO_ERROR)
			{
				gSD2->flags |= SD_FLAG_HS_ENABLED;
				MSDC_LSD_ClearBits32(MSDC_CFG2, MSDC_CFG_CLKSRC);
				msdc2_handle->msdc_clock = MSDC_CLOCK;
				MSDC_SetClock(26000);			
			}
		}
	}
	else
	{
		{
			SD_Use13M_Clock_2();
		}
	}
	status = SD_SetBlength_2(512);
err:	
	if(status != NO_ERROR)
	{
		kal_print("SD2 mount fail!");
		SD_SetDefault();
		msdc2_handle->mIsInitialized = KAL_FALSE;
	}
	else
	{
		kal_print("SD2 mount ok!");
		msdc2_handle->mIsInitialized = KAL_TRUE;
	}
	kal_set_eg_events(msdc2_handle->MSDC_Events, 0, KAL_AND);

	return status	;
}

static kal_uint32 power2(kal_uint32 num)
{
   return 1 << num;
}

/*************************************************************************
* FUNCTION
*  SD_AnalysisCSD
*
* DESCRIPTION
*	Analysis Card Specific Data and store in the member of gSD
*
* PARAMETERS
*	csd: input csd for analysis
* RETURNS
*
* GLOBALS AFFECTED
*	gSD
*
*************************************************************************/
void SD_AnalysisCSD_2(kal_uint32* csd)
{	
	kal_uint8 *ptr;
	kal_uint32 c_mult,c_size;
	
	ptr = (kal_uint8*)csd;
	c_mult = c_size = 0;
	// these offsets refer to the spec. of SD and MMC 
	GetBitFieldN((kal_uint8*)&gSD2->mCSD.csd_ver, ptr, 126,2);	
	GetBitFieldN((kal_uint8*)&gSD2->mCSD.tacc,ptr,112,8);
	GetBitFieldN((kal_uint8*)&gSD2->mCSD.nsac,ptr,104,8);
	GetBitFieldN((kal_uint8*)&gSD2->mCSD.tran_speed,ptr,96,8);
	GetBitFieldN((kal_uint8*)&gSD2->mCSD.ccc,ptr,84,12);
	GetBitFieldN((kal_uint8*)&gSD2->mCSD.r_blk_len,ptr,80,4);
	gSD2->mCSD.r_blk_len = power2(gSD2->mCSD.r_blk_len);
	GetBitFieldN((kal_uint8*)&gSD2->mCSD.r_blk_part,ptr,79,1);
	GetBitFieldN((kal_uint8*)&gSD2->mCSD.w_blk_misali,ptr,78,1);
	GetBitFieldN((kal_uint8*)&gSD2->mCSD.r_blk_misali,ptr,77,1);
	GetBitFieldN((kal_uint8*)&gSD2->mCSD.dsr_imp,ptr,76,1);
	GetBitFieldN((kal_uint8*)&gSD2->mCSD.w_blk_part,ptr,21,1);
	GetBitFieldN((kal_uint8*)&gSD2->mCSD.w_blk_len,ptr,22,4);
	gSD2->mCSD.w_blk_len = power2(gSD2->mCSD.w_blk_len);
	GetBitFieldN((kal_uint8*)&gSD2->mCSD.wp_grp_enable,ptr,31,1);
	// there are some difference of CSD between SD and MMC
	if(msdc2_handle->mMSDC_type == MMC_CARD)
	{
		GetBitFieldN((kal_uint8*)&gSD2->mCSD.spec_ver, ptr, 122,4);		
		GetBitFieldN((kal_uint8*)&gSD2->mCSD.erase_sec_size_mmc,ptr,42,5);
		gSD2->mCSD.erase_sec_size_mmc = (gSD2->mCSD.erase_sec_size_mmc+1)*gSD2->mCSD.w_blk_len; 
		GetBitFieldN((kal_uint8*)&gSD2->mCSD.erase_grp_size_mmc,ptr,37,5);
		gSD2->mCSD.erase_grp_size_mmc = (gSD2->mCSD.erase_grp_size_mmc+1)*gSD2->mCSD.erase_sec_size_mmc;
		GetBitFieldN((kal_uint8*)&gSD2->mCSD.wp_grp_size_mmc,ptr,32,5);		
		gSD2->mCSD.wp_grp_size_mmc = (gSD2->mCSD.wp_grp_size_mmc + 1)*gSD2->mCSD.erase_grp_size_mmc;	
	}
	else // SD_CARD
	{
		GetBitFieldN((kal_uint8*)&gSD2->mCSD.erase_sec_size_sd,ptr,39,7);
		gSD2->mCSD.erase_sec_size_sd += 1;
		GetBitFieldN((kal_uint8*)&gSD2->mCSD.wp_prg_size_sd,ptr,32,7);
		gSD2->mCSD.wp_prg_size_sd = (gSD2->mCSD.wp_prg_size_sd+1) * gSD2->mCSD.erase_sec_size_sd;
		GetBitFieldN((kal_uint8*)&gSD2->mCSD.erase_blk_en_sd,ptr,46,1);
	}
	

	if(msdc2_handle->mMSDC_type == SD20_HCS_CARD && gSD2->mCSD.csd_ver >= SD_CSD_VER_20)
	{
		GetBitFieldN((kal_uint8*)&c_size,ptr,48,22);
		gSD2->mBKNum = (c_size+1);
		gSD2->mCSD.capacity = (kal_uint64)gSD2->mBKNum*512*1024;
	}
	else
	{
		GetBitFieldN((kal_uint8*)&c_mult,ptr,47,3);
		c_mult = power2(c_mult+2);
		GetBitFieldN((kal_uint8*)&c_size,ptr,62,12);
		gSD2->mBKNum = (c_size+1)*c_mult;
		gSD2->mCSD.capacity = (c_size+1)*c_mult*gSD2->mCSD.r_blk_len;
	}
	
}

/*************************************************************************
* FUNCTION
*  SD_AnalysisCID
*
* DESCRIPTION
*	Analysis Card Identificaton and store in the member of gSD
*
* PARAMETERS
*	cid: input of card ID for analysis
* RETURNS
*
* GLOBALS AFFECTED
*	gSD
*
*************************************************************************/
void SD_AnalysisCID_2(kal_uint32* cid)
{
	kal_uint8	i;
	kal_uint8* pcid;
	pcid = (kal_uint8*)cid;

	if(msdc2_handle->mMSDC_type == MMC_CARD)
	{
		GetBitFieldN((kal_uint8*)&gSD2->mCID.year,pcid,8,4);
		gSD2->mCID.year += 1997;
		GetBitFieldN((kal_uint8*)&gSD2->mCID.month,pcid,12,4);
		GetBitFieldN((kal_uint8*)&gSD2->mCID.psn,pcid,16,32);
		GetBitFieldN((kal_uint8*)&gSD2->mCID.prv,pcid,48,8);
		for(i=0;i<6;i++)
			gSD2->mCID.pnm[i] = *(pcid+7+i);
		GetBitFieldN((kal_uint8*)&gSD2->mCID.oid,pcid,104,16);
		GetBitFieldN((kal_uint8*)&gSD2->mCID.mid,pcid,120,8);
		
		// special case handling
		{
			kal_uint8 pnm[] = {0xFF,0xFF,0xFF,0xFF,0x36,0x31};
			if(gSD2->mCID.mid == 6 && gSD2->mCID.oid == 0 &&
				!kal_mem_cmp(gSD2->mCID.pnm,pnm,6))
			{
				gSD2->flags |= SD_FLAG_MMC_MRSW_FAIL;
			}
		}
	}
	else // SD_CARD
	{
		gSD2->mCID.mid = *(pcid+15);
		gSD2->mCID.oid = *(pcid+13) + 256*(*(pcid+14));
		for(i=0;i<5;i++)
			gSD2->mCID.pnm[i] = *(pcid+8+i);
		gSD2->mCID.prv = *(pcid+7);
		//gSD2->mCID.psn = *(kal_uint32*)(pcid+3);
		gSD->mCID.psn = (*(kal_uint32*)(pcid+4)<<8)|*(pcid+3);
		gSD2->mCID.month = (kal_uint8)GET_BIT(*(pcid+1),0,BIT_MASK_4);
		gSD2->mCID.year = GET_BIT(*(pcid+1),4,BIT_MASK_4)+16*GET_BIT(*(pcid+2),0,BIT_MASK_4) + 2000;
	}
}

/*************************************************************************
* FUNCTION
*  SD_AnalysisSCR
*
* DESCRIPTION
*	Analysis SD Card Configuration Register and store in the member of gSD
*
* PARAMETERS
*	scr: input of scr for analysis
* RETURNS
*
* GLOBALS AFFECTED
*	gSD
*
* NOTE
*	Only for SD card.
*
*************************************************************************/
void SD_AnalysisSCR_2(kal_uint32* scr)
{
	kal_uint8 *pscr;

	pscr = (kal_uint8*)scr;
	gSD2->mSCR.spec_ver = (kal_uint8)GET_BIT(*(pscr),0,BIT_MASK_4);
	if(gSD2->mSCR.spec_ver > SD_SPEC_101)
		gSD2->flags |= SD_FLAG_CMD6_SUPPORT;
	gSD2->mSCR.dat_after_erase = (kal_uint8)GET_BIT(*(pscr+1),7,BIT_MASK_1);
	gSD2->mSCR.security = (kal_uint8)GET_BIT(*(pscr+1),4,BIT_MASK_3);
	gSD2->mSCR.bus_width = (kal_uint8)GET_BIT(*(pscr+1),0,BIT_MASK_4);
}

/*************************************************************************
* FUNCTION
*  SD_WaitCmdRdyOrTo
*
* DESCRIPTION
*	Wait until command ready or timeout
*
* PARAMETERS
*	
* RETURNS
*	SDC_CMD_STATUS
*
* GLOBALS AFFECTED
*	
* NOTE
*	Interrupt driven and polling are both implemented
*
*************************************************************************/
SDC_CMD_STATUS SD_WaitCmdRdyOrTo_2(void)
{
	MSDC_START_TIMER2(MSDC_TIMEOUT_PERIOD_CMD);

	{
	volatile kal_uint16 sdc_cmdsta;
	kal_uint32 t1;

	t1 = drv_get_current_time();
	while(!(sdc_cmdsta = MSDC_Reg32(SDC_CMDSTA2))
		&& MSDC_Check_Card_Present2() && !msdc2_handle->is_timeout)
	{
		if(drv_get_duration_ms(t1) > MSDC_TIMEOUT_PERIOD_CMD*11)
			msdc2_handle->is_timeout = KAL_TRUE;
	}
	MSDC_STOP_TIMER2();
	MSDC_CLR_INT2();
	msdc2_handle->cmd_sta = sdc_cmdsta;
	if(sdc_cmdsta & SDC_CMDSTA_CMDTO)            
	{
		kal_prompt_trace(MOD_MSDC_HISR,"[MSDC]:cmd timeout");
		return ERR_CMD_TIMEOUT;
	}
	else if(sdc_cmdsta & SDC_CMDSTA_RSPCRCERR)   
	{
		kal_prompt_trace(MOD_MSDC_HISR,"[MSDC]:cmd crc");
		return ERR_CMD_RSPCRCERR;
	}
	else if(sdc_cmdsta & SDC_CMDSTA_CMDRDY)      
		return NO_ERROR;                                     	
	}
	if(msdc2_handle->is_timeout)
		return MSDC_GPT_TIMEOUT_ERR;
	
	return NO_ERROR;
}

/*************************************************************************
* FUNCTION
*  SD_WaitDatRdyOrTo_2
*
* DESCRIPTION
*	Wait until data ready or timeout
*
* PARAMETERS
*	
* RETURNS
*	SDC_CMD_STATUS
*
* GLOBALS AFFECTED
*	
* NOTE
*	Interrupt driven and polling are both implemented
*
*************************************************************************/
SDC_CMD_STATUS SD_WaitDatRdyOrTo_2(void)
{
	MSDC_START_TIMER2(MSDC_TIMEOUT_PERIOD_DAT);
	

	{
	volatile kal_uint16 sdc_datsta;	
	kal_uint32 t1;

	t1 = drv_get_current_time();
	while(!(sdc_datsta = MSDC_Reg32(SDC_DATSTA2))
		&& MSDC_Check_Card_Present2() && !msdc2_handle->is_timeout)
	{
		if(drv_get_duration_ms(t1) > MSDC_TIMEOUT_PERIOD_DAT*11)
			msdc2_handle->is_timeout = KAL_TRUE;		
	};
	MSDC_STOP_TIMER2();
	MSDC_CLR_INT2();
	msdc2_handle->dat_sta = sdc_datsta;
	if(sdc_datsta & SDC_DATSTA_DATTO)              
	{
		kal_prompt_trace(MOD_MSDC_HISR,"[MSDC]:dat timeout");
		return ERR_DAT_TIMEOUT;                    
	}
	else if(sdc_datsta & SDC_DATSTA_DATCRCERR)     
	{
		kal_prompt_trace(MOD_MSDC_HISR,"[MSDC]:dat crc");
		return ERR_DAT_CRCERR;                     
	}
	else if(sdc_datsta & SDC_DATSTA_BLKDONE)       
		return NO_ERROR;                                     
	}
	if(msdc2_handle->is_timeout)
		return MSDC_GPT_TIMEOUT_ERR;
	
	return NO_ERROR;
}
/*************************************************************************
* FUNCTION
*  SD_WaitCardNotBusy_2
*
* DESCRIPTION
*	Wait until card is not busy (R1b)
*
* PARAMETERS
*	
* RETURNS
*	void
*
* GLOBALS AFFECTED
*	
* NOTE
*	Interrupt driven and polling are both implemented
*
*************************************************************************/

SDC_CMD_STATUS SD_WaitCardNotBusy_2(void)
{
	kal_uint32 t1;

	t1 = drv_get_current_time();
	MSDC_START_TIMER2(MSDC_TIMEOUT_PERIOD_DAT);


	while(SD_IS_R1B_BUSY_2 && MSDC_Check_Card_Present2() && !msdc2_handle->is_timeout)
	{
		if(drv_get_duration_ms(t1) > MSDC_TIMEOUT_PERIOD_DAT*11)
			msdc2_handle->is_timeout = KAL_TRUE;
	};
	MSDC_CLR_INT2();
	MSDC_STOP_TIMER2();


	return NO_ERROR;
}

/*************************************************************************
* FUNCTION
*  SD_CheckStatus_2
*
* DESCRIPTION
*	Check command status
*
* PARAMETERS
*	
* RETURNS
*	SDC_CMD_STATUS
*
* GLOBALS AFFECTED
*	
* NOTE
*
*************************************************************************/
SDC_CMD_STATUS SD_CheckStatus_2()
{
	kal_uint32 status;

	MSDC_LSD_ReadReg32(SDC_RESP02,&status);
	if(status & SDC_CSTA_MASK)
		ASSERT(0);
	if((status & SDC_CSTA_MASK)==0 )
		return NO_ERROR;
	if(status &SDC_CARD_IS_LOCKED)
		return CARD_IS_LOCKED;

	return ERR_STATUS;
}

/*************************************************************************
* FUNCTION
*  SD_Send_Cmd_2
*
* DESCRIPTION
*	to launch the command packet to the card
*
* PARAMETERS
*	1. cmd: the content of SDC_CMD register
*	2. arg: the argument(if the command need no argument, fill it with 0)
*
* RETURNS
*	SDC_CMD_STATUS
*
* GLOBALS AFFECTED
*	
* NOTE
*	1. Check if controller is available  before launch any commands 
*	2. Maybe add check if card is busy (R1b)
*************************************************************************/
SDC_CMD_STATUS SD_Send_Cmd_2(kal_uint32 cmd, kal_uint32 arg)
{
	SDC_CMD_STATUS status;
	kal_uint32 t1;

	t1 = drv_get_current_time();
	MSDC_START_TIMER2(MSDC_TIMEOUT_PERIOD_CMD);
	// check the controller is ready (stop transaction will fail)
	if(cmd != SDC_CMD_CMD12)		
	{
		while(SD_IS_SDC_BUSY_2 && MSDC_Check_Card_Present2() && !msdc2_handle->is_timeout)
		{
			if(drv_get_duration_ms(t1) > MSDC_TIMEOUT_PERIOD_DAT*11)
				msdc2_handle->is_timeout = KAL_TRUE;
		}
	}
	else
	{
		while(SD_IS_CMD_BUSY_2 && MSDC_Check_Card_Present2() && !msdc2_handle->is_timeout)
		{
			if(drv_get_duration_ms(t1) > MSDC_TIMEOUT_PERIOD_DAT*11)
				msdc2_handle->is_timeout = KAL_TRUE;
		}
	}
	MSDC_STOP_TIMER2();
	if(msdc2_handle->is_timeout)
		return MSDC_GPT_TIMEOUT_ERR;
	
	MSDC_CLR_INT2();
	// fill out the argument
	MSDC_LSD_WriteReg32(SDC_ARG2,arg);
	// launch the command
	MSDC_LSD_WriteReg32(SDC_CMD2,cmd);
	if((status = SD_WaitCmdRdyOrTo_2())!=NO_ERROR)
		return status; 

	return NO_ERROR;
}

/*************************************************************************
* FUNCTION
*  SD_Reset
*
* DESCRIPTION
*	reset all cards to idle state
*
* PARAMETERS
*	1. cmd: the content of SDC_CMD register
*	2. arg: the argument(if the command need no argument, fill it with 0)
*
* RETURNS
*	SDC_CMD_STATUS
*
* GLOBALS AFFECTED
*	
* NOTE
*
*************************************************************************/
SDC_CMD_STATUS SD_Reset_2(void)
{
	SDC_CMD_STATUS status;

	status = SD_Send_Cmd_2(SDC_CMD_CMD0,SDC_NO_ARG);
	gSD2->mState = IDLE_STA;

	return status;
}

/*************************************************************************
* FUNCTION
*  SD_Cmd55_2
*
* DESCRIPTION
*	APP_CMD: inidicate to the card that the next command is an application specified command
*			rather than a standard command
*
* PARAMETERS
*	rca: relative card address
*
* RETURNS
*	SDC_CMD_STATUS
*
* GLOBALS AFFECTED
*	
* NOTE
*
*************************************************************************/
SDC_CMD_STATUS SD_Cmd55_2(kal_uint16 rca)
{
	SDC_CMD_STATUS status;

	if((status = SD_Send_Cmd_2(SDC_CMD_CMD55,(kal_uint32)rca<<16))!=NO_ERROR)
		return status;
	//read R1
	if((status = SD_CheckStatus_2())!=NO_ERROR)
		return status;
	//check APP_CMD bit in status register
	MSDC_LSD_ReadReg32(SDC_RESP02,&status);
	if(status & SDC_CSTA_MASK)
		ASSERT(0);
	if(!(status & R1_APP_CMD_5))
		return ERR_APPCMD_FAILED;

	return NO_ERROR;	
}
/*************************************************************************
* FUNCTION
*  SD_Cmd8
*
* DESCRIPTION
*	1. Sends SD Memory Card interface conditions for support larger than 2G cards
*	2. check if the card is compliant to SD2.0 or higher
*	3. only performed while at IDLE state.
*
* PARAMETERS
*
* RETURNS
*
* GLOBALS AFFECTED
*	gSD2->mIsCMD8
*
*************************************************************************/
void SD_Cmd8_2(void)
{
	kal_uint32 resp;

	if(SD_Send_Cmd_2(SDC_CMD_CMD8,SDC_CMD8_ARG)!=NO_ERROR)
	{
		SD_Reset_2();
		gSD2->mCMD8Resp = SD_CMD8_RESP_NORESP;
		return;
	}
	MSDC_LSD_ReadReg32(SDC_RESP02,&resp);
	if(resp == SDC_CMD8_ARG)
		gSD2->mCMD8Resp = SD_CMD8_RESP_VALID;
	else
		gSD2->mCMD8Resp = SD_CMD8_RESP_INVALID;
}

/*************************************************************************
* FUNCTION
*  SD_Cmd1_MMC
*
* DESCRIPTION
*	 asks all cards in idle state to send their OCR in the response on the CMD line
*
* PARAMETERS
*
* RETURNS
*	SDC_CMD_STATUS
*
* GLOBALS AFFECTED
*	gSD
*
* NOTE
*	only works for MMC
*
*************************************************************************/
SDC_CMD_STATUS SD_Cmd1_MMC_2(void)
{
	SDC_CMD_STATUS status;
	kal_uint32 _ocr, ocr_i, t1, t2;

	if(gSD2->mCMD8Resp == SD_CMD8_RESP_INVALID)
		return ERR_CMD8_INVALID;
	ocr_i = (SDC_OCR_DEFAULT|MMC_HIGH_DESITY_CHECK_BIT);


	if(msdc2_handle->is_init_timeout == KAL_TRUE)
		return ERR_R3_OCR_BUSY;
	t2 = drv_get_current_time();
	do{
		t1 = drv_get_current_time();
		MSDC_START_TIMER2(MSDC_TIMEOUT_PERIOD_DAT);
		while((MSDC_IS_BUSY2)
			&& MSDC_Check_Card_Present2() && !msdc2_handle->is_timeout)
		{
			if(drv_get_duration_ms(t1) > MSDC_TIMEOUT_PERIOD_DAT*11)
				msdc2_handle->is_timeout = KAL_TRUE;
		};
		MSDC_STOP_TIMER2();		
		if(msdc2_handle->is_timeout)
			return MSDC_GPT_TIMEOUT_ERR;		
		MSDC_LSD_WriteReg32(SDC_ARG2,ocr_i);
		MSDC_LSD_WriteReg32(SDC_CMD2,SDC_CMD_CMD1);
		if((status = SD_WaitCmdRdyOrTo_2())  != NO_ERROR)
		{
			return status;
		}		
		MSDC_LSD_ReadReg32(SDC_RESP02, &_ocr);
		if((_ocr & SDC_OCR_DEFAULT) == 0)
			return ERR_OCR_NOT_SUPPORT;
		if(!msdc2_handle->mIsPresent)
			return MSDC_CARD_NOT_PRESENT;
		
		if(!(_ocr&SDC_OCR_BUSY))		
		{
			if(drv_get_duration_ms(t2) > MSDC_TIMEOUT_PERIOD_INI)
			{
				msdc2_handle->is_init_timeout = KAL_TRUE;			
				break;
			}					
			if((kal_query_systemInit() == KAL_TRUE) 
#ifdef  __TST_WRITE_TO_FILE__ 			/*error recording: considering error recording additionally*/
				|| (KAL_TRUE == INT_QueryExceptionStatus())
#endif
			)
				MSDC_BusyWait_ms(30);
			else
				kal_sleep_task(7);
				
		}
		else
			break;		
	}while(1);

	if(msdc2_handle->is_init_timeout)
		return ERR_CMD_TIMEOUT;

	if((_ocr & MMC_HIGH_DESITY_CHECK_MSK) == MMC_HIGH_DESITY_CHECK_BIT)
	{
		gSD2->flags |= SD_FLAG_HCS_SUPPORT;
		msdc2_handle->mMSDC_type = MMC42_CARD;
		kal_print("MMC4.2 or higher");
	}
	else
	msdc2_handle->mMSDC_type = MMC_CARD;
	gSD2->mInactive = KAL_FALSE;
	gSD2->mSDC_ocr = _ocr;
	gSD2->mState = READY_STA;
	
	return NO_ERROR;
}

/*************************************************************************
* FUNCTION
*  SD_Acmd41_SD
*
* DESCRIPTION
*	asks all cards in idle state to send their OCR in the response on the CMD line
*	OCR: Operation Condition Register
*
* PARAMETERS
*
* RETURNS
*	SDC_CMD_STATUS
*
* GLOBALS AFFECTED
*	gSD
*
* NOTE
*	only works for SD
*
*************************************************************************/
SDC_CMD_STATUS SD_Acmd41_SD_2(void)
{

	SDC_CMD_STATUS		status;
	kal_uint32			_ocr = 0, ocr_i, t1, t2;
	
	if(gSD2->mCMD8Resp == SD_CMD8_RESP_NORESP)
		ocr_i = SDC_OCR_DEFAULT;
	else if(gSD2->mCMD8Resp == SD_CMD8_RESP_VALID)
		ocr_i = (SDC_OCR_DEFAULT|SD_ACMD41_HCS);
	else if(gSD2->mCMD8Resp == SD_CMD8_RESP_INVALID)
		return ERR_CMD8_INVALID;	


	msdc2_handle->is_init_timeout = KAL_FALSE;
	t2 = drv_get_current_time();
	do{
		t1 = drv_get_current_time();		
		MSDC_START_TIMER2(MSDC_TIMEOUT_PERIOD_CMD);
		while((MSDC_IS_BUSY2)
			&& MSDC_Check_Card_Present2() && !msdc2_handle->is_timeout)
		{
			if(drv_get_duration_ms(t1) > MSDC_TIMEOUT_PERIOD_CMD*11)
				msdc2_handle->is_timeout = KAL_TRUE;			
		};
		MSDC_STOP_TIMER2();
		if(msdc2_handle->is_timeout)
			return MSDC_GPT_TIMEOUT_ERR;		
		status=SD_Cmd55_2(SDC_RCA_DEFAULT);
		if(status != NO_ERROR)
		{
			return status;
		}
		MSDC_START_TIMER2(MSDC_TIMEOUT_PERIOD_CMD);
		t1 = drv_get_current_time();
		while((MSDC_IS_BUSY2)
			&& MSDC_Check_Card_Present2() && !msdc2_handle->is_timeout)
		{
			if(drv_get_duration_ms(t1) > MSDC_TIMEOUT_PERIOD_CMD*11)
				msdc2_handle->is_timeout = KAL_TRUE;			
		};
		MSDC_STOP_TIMER2();
		if(msdc2_handle->is_timeout)
			return MSDC_GPT_TIMEOUT_ERR;		
		MSDC_LSD_WriteReg32(SDC_ARG2,ocr_i);
		MSDC_LSD_WriteReg32(SDC_CMD2,SDC_CMD_CMD41_SD);
		if((status = SD_WaitCmdRdyOrTo_2())  != NO_ERROR)
		{
			return status;
		}		
		MSDC_LSD_ReadReg32(SDC_RESP02, &_ocr);
		if((_ocr & SDC_OCR_DEFAULT) == 0)
			return ERR_OCR_NOT_SUPPORT;
		if(!msdc2_handle->mIsPresent)
			return ERR_CARD_NOT_PRESENT;	
		if(!(_ocr&SDC_OCR_BUSY))		
		{
			if(drv_get_duration_ms(t2) > MSDC_TIMEOUT_PERIOD_INI)
			{
				msdc2_handle->is_init_timeout = KAL_TRUE;			
				break;
			}					
			if((kal_query_systemInit() == KAL_TRUE) 
#ifdef  __TST_WRITE_TO_FILE__ 			/*error recording: considering error recording additionally*/
				|| (KAL_TRUE == INT_QueryExceptionStatus())
#endif
			)
				MSDC_BusyWait_ms(30);
			else
				kal_sleep_task(7);
		}
		else
			break;		
	}
	while(1);

	if(msdc2_handle->is_init_timeout)
		return ERR_R3_OCR_BUSY;

	gSD2->mInactive = KAL_FALSE;
	gSD2->mSDC_ocr = _ocr;
	gSD2->flags |= SD_FLAG_SD_TYPE_CARD;	
	if(_ocr & SD_ACMD41_HCS)
	{
		gSD2->flags |= SD_FLAG_HCS_SUPPORT;
		msdc2_handle->mMSDC_type = SD20_HCS_CARD;
		kal_print("SD2.0 or higher");
	}
	else if(gSD2->mCMD8Resp == SD_CMD8_RESP_VALID)
		msdc2_handle->mMSDC_type = SD20_LCS_CARD;
	else
		msdc2_handle->mMSDC_type = SD_CARD;
	gSD2->mState = READY_STA;

	return NO_ERROR;
}

/*************************************************************************
* FUNCTION
*  SD_GetCID
*
* DESCRIPTION
*	Read Card Identification.
*
* PARAMETERS
*
* RETURNS
*	SDC_CMD_STATUS
*
* GLOBALS AFFECTED
*	gSD
*
* NOTE
*	
*
*************************************************************************/

// Get CID(CMD2)
SDC_CMD_STATUS SD_GetCID_2(kal_uint32 Cid[4])
{
	int i;
	SDC_CMD_STATUS status;

	if((status = SD_Send_Cmd_2(SDC_CMD_CMD2,SDC_NO_ARG))!=NO_ERROR)
		return status;
	//read R2
	for(i=0;i<4;i++)
		MSDC_LSD_ReadReg32((SDC_RESP02+i*sizeof(kal_uint32)), &Cid[i]);
	SD_AnalysisCID_2(Cid);
	gSD2->mState = IDENT_STA;
	
	return NO_ERROR;
}

/*************************************************************************
* FUNCTION
*  SD_ValidateRCA
*
* DESCRIPTION
*	assing or read RCA
*
* PARAMETERS
*	pRca: used for input or output RCA
*
* RETURNS
*	SDC_CMD_STATUS
*
* GLOBALS AFFECTED
*	gSD
*
* NOTE
*	RCA is assinged to MMC card fixed to SDC_RCA_MMC(1)
*
*************************************************************************/

// assign or read RCA
SDC_CMD_STATUS SD_ValidateRCA_2(kal_uint16* pRca)
{
	SDC_CMD_STATUS status;
	kal_uint32 resp;
	kal_uint8  state;

	if(gSD2->flags & SD_FLAG_SD_TYPE_CARD )
	{
		//read RCA form card
		if((status = SD_Send_Cmd_2(SDC_CMD_CMD3_SD,SDC_NO_ARG))!=NO_ERROR)
			return status;
		//read R6
		MSDC_LSD_ReadReg32(SDC_RESP02, &resp);
		*pRca = resp >>  16;
		gSD2->mRCA =*pRca;
		
	}
	else
	{
		//assign RCA to card
		if((status = SD_Send_Cmd_2(SDC_CMD_CMD3_MMC,(kal_uint32)SDC_RCA_MMC<<16))!=NO_ERROR)
			return status;

		//read R1
		MSDC_LSD_ReadReg32(SDC_RESP02, &resp);
		SD_GetStatus_2(SDC_RCA_MMC,&resp);
		state = 0;
		GetBitFieldN((kal_uint8*)&state,(kal_uint8*)&resp,9,4);
		if(STBY_STA != state)
			return ERR_RCA_FAIL;		
		*pRca = gSD2->mRCA = SDC_RCA_MMC;			
	}

	gSD2->mState = STBY_STA;
	return NO_ERROR;
}

/*************************************************************************
* FUNCTION
*  SD_SetDSR
*
* DESCRIPTION
*	set default value to the DSR
*
* PARAMETERS
*
* RETURNS
*	SDC_CMD_STATUS
*
* GLOBALS AFFECTED
*
* NOTE
*	SDC_DSR_DEFAULT(0x404)
*
*************************************************************************/
SDC_CMD_STATUS SD_SetDSR_2(void)
{
	return SD_Send_Cmd_2(SDC_CMD_CMD4,(kal_uint32)SDC_DSR_DEFAULT<<16);
}

/*************************************************************************
* FUNCTION
*  SD_SelectCard
*
* DESCRIPTION
*	select/deselect card
*
* PARAMETERS
*	rca: relative card address
*
* RETURNS
*	SDC_CMD_STATUS
*
* GLOBALS AFFECTED
*
* NOTE
*
*************************************************************************/
SDC_CMD_STATUS SD_SelectCard_2(kal_uint16 rca)
{
	SDC_CMD_STATUS status;

	if((status = SD_Send_Cmd_2(SDC_CMD_CMD7,(kal_uint32)rca<<16))!=NO_ERROR)
		return status;
		
	//read R1b
	if((status = SD_WaitCardNotBusy_2())!=NO_ERROR)	
		return status;
	if((status = SD_CheckStatus_2())!=NO_ERROR)
		return status;

	return NO_ERROR;
}

/*************************************************************************
* FUNCTION
*  SD_GetCSD
*
* DESCRIPTION
*	Get CSD from addressed card
*
* PARAMETERS
*	rca: relative card address
*	Csd: used for containing read CSD
*
* RETURNS
*	SDC_CMD_STATUS
*
* GLOBALS AFFECTED
*	
* NOTE
*
*************************************************************************/
SDC_CMD_STATUS SD_GetCSD_2(kal_uint16 rca, kal_uint32 Csd[4])
{
	SDC_CMD_STATUS status;
	kal_uint32 i;

	if((status = SD_Send_Cmd_2(SDC_CMD_CMD9,(kal_uint32)rca<<16))!=NO_ERROR)
		return status;
	for(i=0;i<4;i++)
	{
		MSDC_LSD_ReadReg32((volatile kal_uint32 *)(SDC_RESP02+i*4), &Csd[i]);
	}
	SD_AnalysisCSD_2(Csd);

	return NO_ERROR;
}

// addressed send CID
SDC_CMD_STATUS SD_GetAddressedCID_2(kal_uint16 rca, kal_uint32 Cid[4])
{
	SDC_CMD_STATUS status;
	kal_uint32 i;

	if((status = SD_Send_Cmd_2(SDC_CMD_CMD10,(kal_uint32)rca<<16))!=NO_ERROR)
		return status;
	for(i=0;i<4;i++)
	{
		MSDC_LSD_ReadReg32((volatile kal_uint32 *)(SDC_RESP02+i*4), &Cid[i]);
	}
	return NO_ERROR;
}


/*************************************************************************
* FUNCTION
*  SD_StopTrans_2
*
* DESCRIPTION
*	Stop Muli-Block operation
*
* PARAMETERS
*
* RETURNS
*	SDC_CMD_STATUS
*
* GLOBALS AFFECTED
*	
* NOTE
*	definition of SD_STOP_SLOW is used for some erroneous card
*************************************************************************/
SDC_CMD_STATUS SD_StopTrans_2(kal_bool isTx)
{
	SDC_CMD_STATUS status;
	kal_uint32 retry = 0;

	while(retry < 30)
	{
		if((status = SD_Send_Cmd_2(SDC_CMD_CMD12,SDC_NO_ARG))!=NO_ERROR)
		{
			retry ++;
		}
		else
		{
			break;
		}
	}
	if(retry >= 30)
	{
		return status;
	}
	if(isTx)
		SD_WaitCardNotBusy_2();
	

#ifdef SD_STOP_SLOW 	
		while(MSDC_Reg32(SDC_STA2) & SDC_STA_R1BSY);
		do{	
		SD_GetStatus_2(gSD2->mRCA,(kal_uint32*)&status);
			}while((status & R1_CUR_STATE) >> 9 !=  TRAN_STA);
#endif

	return NO_ERROR;
}

/*************************************************************************
* FUNCTION
*  SD_GetStatus_2
*
* DESCRIPTION
*	addressed send status
*
* PARAMETERS
*
* RETURNS
*	SDC_CMD_STATUS
*
* GLOBALS AFFECTED
*	
* NOTE
*
*************************************************************************/
SDC_CMD_STATUS SD_GetStatus_2(kal_uint16 rca, kal_uint32* resp)
{
	SDC_CMD_STATUS status;

	if((status = SD_Send_Cmd_2(SDC_CMD_CMD13,(kal_uint32)rca <<16))!=NO_ERROR)
		return status;

	MSDC_LSD_ReadReg32(SDC_RESP02,resp);
	if((*resp) & SDC_CSTA_MASK)
		ASSERT(0);

	return NO_ERROR;
}

/*************************************************************************
* FUNCTION
*  SD_SetBlength_2
*
* DESCRIPTION
*	set block length
*
* PARAMETERS
*	BKLength: block length u want to set
*
* RETURNS
*	SDC_CMD_STATUS
*
* GLOBALS AFFECTED
*	gSD2->mBKLength
*
* NOTE
*
*************************************************************************/
SDC_CMD_STATUS SD_SetBlength_2(kal_uint32 BKLength)
{
	SDC_CMD_STATUS status;

	// maximal value of block length is 2048
	if(BKLength > SDC_MAX_BKLENGTH)
		return ERR_INVALID_BKLENGTH;
	if(!gSD2->mCSD.r_blk_part && BKLength < gSD2->mCSD.max_r_blk_len )
		return ERR_INVALID_BKLENGTH;		
	if((status = SD_Send_Cmd_2(SDC_CMD_CMD16,BKLength))!=NO_ERROR)
		return status;
	//read R1
	status = SD_CheckStatus_2();		
	// 2. configure the controller
	gSD2->mBKLength = BKLength;
	BitFieldWrite32((kal_uint32*)SDC_CFG2,BKLength,SDC_CFG_BLKLEN);

	return NO_ERROR;
}


/*************************************************************************
* FUNCTION
*  SD_ReadSingleBlock
*
* DESCRIPTION
*	1. read a single block form data_adrs of card to the rxbuffer
*	2. the block length is set by set block length
*
* PARAMETERS
*	data_adrs: starting address to read
*	rxbuffer: as name
*
* RETURNS
*	SDC_CMD_STATUS
*
* GLOBALS AFFECTED
*
* NOTE
*	the size of rxbuffer should be 4*n (n : integer)
*
*************************************************************************/
SDC_CMD_STATUS SD_ReadSingleBlock_2(kal_uint32 data_adrs, kal_uint32* rxbuffer)
{
	kal_uint32 count;
	SDC_CMD_STATUS status;

	
	EnableMSDC_DMA2();
	count = MSDC_SD_BLOCK_SIZE;
	MSDC_DMATransferFirst2((kal_uint32)rxbuffer,count,KAL_FALSE);
	if((status = SD_Send_Cmd_2(SDC_CMD_CMD17,data_adrs))!=NO_ERROR)	
		goto ERR_Exit;
	if((status = SD_CheckStatus_2())!=NO_ERROR)
		goto ERR_Exit;
	status = MSDC_DMATransferFinal2();
	if(status != NO_ERROR)
	{
		goto ERR_Exit;			
	}		
	if((status = SD_WaitDatRdyOrTo_2())!=NO_ERROR)
		goto ERR_Exit;
	
	DisableMSDC_DMA2();
	MSDC_CLR_FIFO2();
	
	return NO_ERROR;
ERR_Exit:
	{
		kal_uint32 tmp;
		
		DisableMSDC_DMA2();
		Reset_MSDC2();

		// SD_StopTrans_2(KAL_FALSE);		
		SD_GetStatus_2(gSD2->mRCA,(kal_uint32*)&tmp);
		MSDC_LSD_ReadReg32(SDC_DATSTA2,&tmp);
		MSDC_CLR_FIFO2();		
		return status;
	}
	
}

/*************************************************************************
* FUNCTION
*  SD_ReadMultiBlock
*
* DESCRIPTION
*	read num of blocks into rxbuffer
*
* PARAMETERS
*	data_adrs: starting address to read
*	rxbuffer: as name
*	num: number of blocks to read
*
* RETURNS
*	SDC_CMD_STATUS
*
* GLOBALS AFFECTED
*
* NOTE
*
*************************************************************************/
SDC_CMD_STATUS SD_ReadMultiBlock_2(kal_uint32 data_adrs, kal_uint32* rxbuffer, kal_uint32 num)
{
	SDC_CMD_STATUS status;
	kal_uint32 j, count;
	#ifndef MSDC_DMA
	kal_uint32 i;	
	#endif

	EnableMSDC_DMA2();
	count = MSDC_SD_BLOCK_SIZE;	
	MSDC_DMATransferFirst2((kal_uint32)rxbuffer,count*num,KAL_FALSE);	
	if((status = SD_Send_Cmd_2(SDC_CMD_CMD18,data_adrs))!=NO_ERROR)
		goto ERR_Exit;
	if((status = SD_CheckStatus_2())!=NO_ERROR)
		goto ERR_Exit;	
	count = MSDC_SD_BLOCK_SIZE;	
	status = MSDC_DMATransferFinal2();
	if(status != NO_ERROR)
			goto ERR_Exit;	

	if((status = SD_WaitDatRdyOrTo_2())!=NO_ERROR)
		goto ERR_Exit;
	
	
	MSDC_CLR_INT2();
	DisableMSDC_DMA2();

	if(gSD2->flags & SD_FLAG_MMC_MRSW_FAIL)
	{
		kal_uint32 delay = 200;
		while(delay--);		
	}
	if((status = SD_StopTrans_2(KAL_FALSE))!=NO_ERROR)
	{
		//if((data_adrs/gSD2->mBKLength + j) < gSD2->mBKNum)			
			goto ERR_Exit;		
	}
	MSDC_CLR_FIFO2();			
	return NO_ERROR;
	
ERR_Exit:


	DisableMSDC_DMA2();
	Reset_MSDC2();		
	SD_StopTrans_2(KAL_FALSE);		
	SD_GetStatus_2(gSD2->mRCA,(kal_uint32*)&j);
	MSDC_LSD_ReadReg32(SDC_DATSTA2,&j);
	MSDC_CLR_FIFO2();
	return status;
	
}

/*************************************************************************
* FUNCTION
*  SD_WriteSingleBlock
*
* DESCRIPTION
*	write a single block
*
* PARAMETERS
*	address: starting address to write
*	txbuffer: as name
*
* RETURNS
*	SDC_CMD_STATUS
*
* GLOBALS AFFECTED
*
* NOTE
*	block length is set by Set_Block_Length
*
*************************************************************************/
SDC_CMD_STATUS SD_WriteSingleBlock_2(kal_uint32 address, kal_uint32* txbuffer)
{
	SDC_CMD_STATUS status;
	kal_uint32 count;
	kal_uint32 *ptr;	

	if(gSD2->mWPEnabled)
		return ERR_WRITE_PROTECT;
	EnableMSDC_DMA2();
	count = MSDC_SD_BLOCK_SIZE;
	
			ptr = txbuffer;

	
	EnableMSDC_DMA2();
	count = MSDC_SD_BLOCK_SIZE;	
	MSDC_DMATransferFirst2((kal_uint32)ptr,count,KAL_TRUE);	
	if((status = SD_Send_Cmd_2(SDC_CMD_CMD24,address))!=NO_ERROR)
		goto ERR_Exit;
	if((status = SD_CheckStatus_2())!=NO_ERROR)
		goto ERR_Exit;
	status = MSDC_DMATransferFinal2();
	if(status != NO_ERROR)
		goto ERR_Exit;	
	if((status = SD_WaitCardNotBusy_2())!=NO_ERROR)	
		 goto ERR_Exit;	
	DisableMSDC_DMA2();
	if((status = SD_WaitDatRdyOrTo_2())!=NO_ERROR)
		goto ERR_Exit;
	

	return NO_ERROR;
ERR_Exit:
	{
		kal_uint32 tmp;
		
		DisableMSDC_DMA2();
		Reset_MSDC2();
		SD_GetStatus_2(gSD2->mRCA,(kal_uint32*)&tmp);
		MSDC_LSD_ReadReg32(SDC_DATSTA2,&tmp);
		return status;
	}
	
}

/*************************************************************************
* FUNCTION
*	SD_WriteMultiBlock
*
* DESCRIPTION
*	write num blocks starting at address
*
* PARAMETERS
*	address: starting address to write
*	txbuffer: as name
*	num: number of blocks to write
*
* RETURNS
*	SDC_CMD_STATUS
*
* GLOBALS AFFECTED
*
* NOTE
*	block length is set by Set_Block_Length
*
*************************************************************************/
SDC_CMD_STATUS SD_WriteMultiBlock_2(kal_uint32 address, kal_uint32* txbuffer, kal_uint32 num)
{
	SDC_CMD_STATUS status;
	kal_uint32 count;
	kal_uint32 *ptr;
		
	if(gSD2->mWPEnabled)
		return ERR_WRITE_PROTECT;
	EnableMSDC_DMA2();	
	count = MSDC_SD_BLOCK_SIZE;	


	MSDC_DMATransferFirst2((kal_uint32)txbuffer,count*num,KAL_TRUE);

	if((status = SD_Send_Cmd_2(SDC_CMD_CMD25,address))!=NO_ERROR)
		goto ERR_Exit;
	if((status = SD_CheckStatus_2())!=NO_ERROR)
		goto ERR_Exit;
	   
	

			status = MSDC_DMATransferFinal2();
			if(status != NO_ERROR)
				goto ERR_Exit;		
			if((status = SD_WaitDatRdyOrTo_2())!=NO_ERROR)	
				goto ERR_Exit;
	

	DisableMSDC_DMA2();
	if((status = SD_StopTrans_2(KAL_TRUE))!=NO_ERROR)
		goto ERR_Exit;
	
	MSDC_CLR_INT2();
	
	return NO_ERROR;
ERR_Exit:
	{
		kal_uint32 tmp;
		
		DisableMSDC_DMA2();
		Reset_MSDC2();
		SD_StopTrans_2(KAL_TRUE);		
		SD_GetStatus_2(gSD2->mRCA,(kal_uint32*)&tmp);
		MSDC_LSD_ReadReg32(SDC_DATSTA2,&tmp);
		return status;
	}
	
}
/*************************************************************************
* FUNCTION
*	SD_SetBusWidth
*
* DESCRIPTION
*	ACMD6: set the data width 00 for 1 bit, 10 for 4 bits
*
* PARAMETERS
*	width: indicate the bus width
*
* RETURNS
*	SDC_CMD_STATUS
*
* GLOBALS AFFECTED
*
* NOTE
*	Not every card support 4-bits bus
*	only for SD
*
*************************************************************************/
SDC_CMD_STATUS SD_SetBusWidth_2(SD_BITWIDTH width)
{
	SDC_CMD_STATUS status;

	// check if card support 4 bits bus
	if((width == BIT_4W) && !(gSD2->mSCR.bus_width&0x04))
		return ERR_NOT_SUPPORT_4BITS;
	// send APP_CMD
	if((status = SD_Cmd55_2(gSD2->mRCA))!=NO_ERROR)
		return status;
	// send cmd6
	if((status = SD_Send_Cmd_2(SDC_CMD_ACMD6,width))!=NO_ERROR)
		return status;
	//read R1
	if((status = SD_CheckStatus_2())!=NO_ERROR)
		return status;	
	// set the controler MDLEN to enalbe 4bits bus width
	MSDC_LSD_SetBits32(SDC_CFG2,SDC_CFG_MDLEN);
	gSD2->bus_width = 4;
	
	return NO_ERROR;
}

/*************************************************************************
* FUNCTION
*	SD_ReadSCR
*
* DESCRIPTION
*	ACMD51: read the SD Configuration Register(8bytes block read)
*
* PARAMETERS
*	scr: used for store SCR
*
* RETURNS
*	SDC_CMD_STATUS
*
* GLOBALS AFFECTED
*
* NOTE
*	Make sure the size of SCR is 8 bytes 
*
*************************************************************************/
SDC_CMD_STATUS SD_ReadSCR_2(kal_uint32* scr)
{
	SDC_CMD_STATUS status;
	kal_uint32 blklen,i, t1;

	ASSERT((kal_uint32)scr % 4  == 0);
	// save the original block length 
	blklen = gSD2->mBKLength;
	// set block length(MSDC_CFG2)
	if((status = SD_SetBlength_2(8))!=NO_ERROR)
		return status;
	// send APP_CMD
	if((status = SD_Cmd55_2(gSD2->mRCA))!=NO_ERROR)
		return status;
	// send command
	if((status = SD_Send_Cmd_2(SDC_CMD_ACMD51,SDC_NO_ARG))!=NO_ERROR)
		return status;
	//read R1
	if((status = SD_CheckStatus_2())!=NO_ERROR)
		return status;	
	// read data(8bytes)	
	// failed to use DMA with burst mode
	t1 = drv_get_current_time();
	MSDC_START_TIMER2(MSDC_TIMEOUT_PERIOD_DAT);
	for(i=0;i<2;)
	{	
		if(drv_get_duration_ms(t1) > MSDC_TIMEOUT_PERIOD_DAT*11)
			msdc2_handle->is_timeout = KAL_TRUE;		
		if(!msdc2_handle->mIsPresent)
			return ERR_CARD_NOT_PRESENT;
		if(msdc2_handle->is_timeout)
			return MSDC_GPT_TIMEOUT_ERR;
		if(!MSDC_IS_FIFO_EMPTY2)
		{
			*(kal_uint32*)(scr+i) = MSDC_Reg32(MSDC_DAT2);
			i++;
		}
	}

	MSDC_STOP_TIMER2();
	// analysis scr
	SD_AnalysisSCR(scr);

	MSDC_CLR_FIFO2();
	return NO_ERROR;
	
}

/*************************************************************************
* FUNCTION
*	SD_SetPreEraseBlk
*
* DESCRIPTION
*	ACMD23: set the number of write blocksto be pre-erased before writing
*	used for faster multiple Block Write
*
* PARAMETERS
*	num: used for storing number of blocks during multi-block operation
*
* RETURNS
*	SDC_CMD_STATUS
*
* GLOBALS AFFECTED
*
* NOTE
*
*************************************************************************/
SDC_CMD_STATUS SD_SetPreEraseBlk_2(kal_uint32 num)
{
	SDC_CMD_STATUS status;

	//[22:0] number of blocks 
	num &= 0x003FFF;
	// send APP_CMD
	if((status = SD_Cmd55_2(gSD2->mRCA))!=NO_ERROR)
		return status;
	// send CMD23
	if((status = SD_Send_Cmd_2(SDC_CMD_ACMD23,num))!=NO_ERROR)
		return status;
	//read R1
	if((status = SD_CheckStatus_2())!=NO_ERROR)
		return status;	

	return NO_ERROR;
}

/*************************************************************************
* FUNCTION
*	SD_EraseCmdClass
*
* DESCRIPTION
*	groups of erase commands including CMD32 ~CMD38
*
* PARAMETERS
*	cmd: indicate which command to execute
*	address: starting address wiht write protection
*
* RETURNS
*	SDC_CMD_STATUS
*
* GLOBALS AFFECTED
*
* NOTE
*	CMD34~CMD37 are only for MMC
*
*************************************************************************/
SDC_CMD_STATUS SD_EraseCmdClass_2(kal_uint32 cmd ,kal_uint32 address)
{
	SDC_CMD_STATUS status;

	if(cmd != SDC_CMD_CMD38)
	{
		if((status = SD_Send_Cmd_2(cmd,address))!=NO_ERROR)
			return status;
	}
	else if((status = SD_Send_Cmd_2(cmd,SDC_NO_ARG))!=NO_ERROR)
			return status;

	//read R1
	if((status = SD_CheckStatus_2())!=NO_ERROR)
		return status;

	if(cmd == SDC_CMD_CMD38)
	{
		SD_WaitCardNotBusy_2();
		do{	
		SD_GetStatus_2(gSD2->mRCA,(kal_uint32*)&status);
		if(msdc2_handle->mIsPresent == KAL_FALSE)
			break;
			}while(CurState(status)!=  TRAN_STA);
	}
	
	return NO_ERROR;
}
/*************************************************************************
* FUNCTION
*	SD_Switch_MMC40
*
* DESCRIPTION
*	CMD6: set the command set or write to the EXT_CSD (for MMC4.0)
*
* PARAMETERS
*	access: access mode
*	index: index to EXT_CSD
*  value: value to write to EXT_CSD
*	set:	selected command set 
*
* RETURNS
*	SDC_CMD_STATUS
*
* GLOBALS AFFECTED
*
* NOTE
*
*************************************************************************/
SDC_CMD_STATUS SD_Switch_MMC40_2(kal_uint8 access, kal_uint8 index, kal_uint8 value, kal_uint8 set)
{
	SDC_CMD_STATUS status;
	kal_uint32 arg = 0;
	
	arg = (access<<24)|(index<<16)|(value<<8)|set;
	// send command
	if((status = SD_Send_Cmd_2(SDC_CMD_CMD6_MMC40,arg))!=NO_ERROR)
		return status;
	//read R1
	if((status = SD_CheckStatus_2())!=NO_ERROR)
		return status;

	return NO_ERROR;
}

/*************************************************************************
* FUNCTION
*	SD_SendEXTCSD_MMC40
*
* DESCRIPTION
*	CMD8: read the content of EXT_CSD register
*
* PARAMETERS
*	kal: access mode
*	index: index to EXT_CSD
*  value: value to write to EXT_CSD
*	set:	selected command set 
*
* RETURNS
*	SDC_CMD_STATUS
*
* GLOBALS AFFECTED
*
* NOTE
*
*************************************************************************/
SDC_CMD_STATUS SD_SendEXTCSD_MMC40_2(kal_uint32* rxbuffer)
{
	SDC_CMD_STATUS status;
	kal_bool retry_4bit = KAL_FALSE;

start:
	// read the block	of 512 bytes (make sure the rxbuffer is 4 byte aligned)
	EnableMSDC_DMA2();
	MSDC_DMATransferFirst2((kal_uint32)rxbuffer,128,KAL_FALSE);		
	if((status = SD_Send_Cmd_2(SDC_CMD_CMD8_MMC40,SDC_NO_ARG))!=NO_ERROR)
		goto ERR_Exit;
	//read R1
	if((status = SD_CheckStatus_2())!=NO_ERROR)
		goto ERR_Exit;		
	// read the block	of 512 bytes (make sure the rxbuffer is 4 byte aligned)
	status = MSDC_DMATransferFinal2();	
	if(status != NO_ERROR)
		goto ERR_Exit;			
	if((status = SD_WaitDatRdyOrTo_2())!=NO_ERROR)
		goto ERR_Exit;
	
	DisableMSDC_DMA2();
	MSDC_CLR_FIFO2();	
	gSD2->mCSD.ext_csd = (T_EXT_CSD_MMC40 *)rxbuffer;
	return NO_ERROR;	
	
ERR_Exit:

	if(retry_4bit == KAL_FALSE)
	{
		retry_4bit = KAL_TRUE;
		MSDC_LSD_SetBits32(SDC_CFG2,SDC_CFG_MDLEN);
		gSD2->bus_width = 4;
		goto start;
	}
	MSDC_LSD_ClearBits32(SDC_CFG2,SDC_CFG_MDLEN);
	gSD2->bus_width = 1;
	DisableMSDC_DMA2();
	MSDC_CLR_FIFO2();	
	Reset_MSDC2();

	return status;
}

/*************************************************************************
* FUNCTION
*	SD_Switch_SD11
*
* DESCRIPTION
*	CMD6: switch command to query and select the specific functions. (SD1.1 or later)
* PARAMETERS
*	arg: argument
*	resp: buffer to contain the ther 64 bytes status information
*
* RETURNS
*	SDC_CMD_STATUS
*
* GLOBALS AFFECTED
*
* NOTE
*
*************************************************************************/
SDC_CMD_STATUS SD_Switch_SD11_2(kal_uint32 arg, T_SWITCH_STATUS* info)
{
	SDC_CMD_STATUS status = NO_ERROR;
	
	BitFieldWrite32((kal_uint32*)SDC_CFG2,SD_CMD6_RESP_LEN,SDC_CFG_BLKLEN);	
	EnableMSDC_DMA2();
	MSDC_DMATransferFirst2((kal_uint32)info,(SD_CMD6_RESP_LEN>>2),KAL_FALSE);	
	if((status = SD_Send_Cmd_2(SDC_CMD_CMD6_SD11,arg))!=NO_ERROR)
		goto exit;
	if((status = SD_CheckStatus_2())!=NO_ERROR)
		goto exit;
	status = MSDC_DMATransferFinal2();	
	
exit:	
	DisableMSDC_DMA2();	
	return status;
}

/*************************************************************************
* FUNCTION
*	SD_Switch_SD11
*
* DESCRIPTION
*	Enable the high speed interface to support up to 50M Hz clock 
*
* PARAMETERS
*	arg: argument
*	resp: buffer to contain the ther 64 bytes status information
*
* RETURNS
*	SDC_CMD_STATUS
*
* GLOBALS AFFECTED
*
* NOTE
*
*************************************************************************/
SDC_CMD_STATUS SD_SelectHighSpeed_SD11_2(void)
{
	SDC_CMD_STATUS status;
	T_SWITCH_STATUS *p = (T_SWITCH_STATUS*)MSDC_Sector2;
	
	if((status = SD_Switch_SD11(SD_CMD6_QUERY_HIGH_SPEED, p))!=NO_ERROR)
		return status;
	if(p->max_current == 0)
		return ERR_SD_HS_FAIL;
	if((p->group1_info & (1 << SD_FUNC_HIGH_SPEED)) && 
		(p->group1_result == SD_FUNC_HIGH_SPEED))
	{
		if((status = SD_Switch_SD11(SD_CMD6_SELECT_HIGH_SPEED, p))!=NO_ERROR)
				return status;
		if(p->max_current == 0)
			return ERR_SD_HS_FAIL;
		if(p->group1_result == SD_FUNC_HIGH_SPEED)
			gSD2->flags |= SD_FLAG_HS_SUPPORT;
	}	
	else
		return ERR_SD_HS_FAIL;


	return NO_ERROR;
}

kal_bool MSDC_ModuleTest_Report_2(void)
{
    return msdc2_handle->mIsInitialized;
}

#endif // defined(__MSDC2_SD_MMC__) || defined(__MSDC2_SD_SDIO__)

#endif //DRV_MSDC_OFF