sdmmc.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322
  1. /*
  2. **********************************************************************
  3. Copyright (c)2007 Arkmicro Technologies Inc. All Rights Reserved
  4. Filename: sdmmc.c
  5. Version : 1.0
  6. Date : 2015.12.01
  7. Abstract: ark1680 soc sd driver
  8. History :
  9. ***********************************************************************
  10. */
  11. #include <string.h>
  12. #include "typedef.h"
  13. #include "amt630h.h"
  14. #include "sdmmc.h"
  15. #include "uart.h"
  16. #include "timer.h"
  17. #include "fs/ff.h"
  18. #include "fs/diskio.h"
  19. #include "os_adapt.h"
  20. #define SDMMC_BUS_CLK CLK_24MHZ
  21. #define INIT_CLOCK 400000
  22. #define SD_TRUE 1
  23. #define SD_FAULSE 0
  24. #define TIMEOUT_SD 10000
  25. #if SD_DEBUG
  26. #define DEBUG_MSG(fmt, ...) printf(fmt, __VA_ARGS__)
  27. #else
  28. #define DEBUG_MSG(fmt, ...)
  29. #endif
  30. static UINT32 lg_ulChip;
  31. static UINT32 SDHC_Controls[1] = {SDHC0_BASE};
  32. static UINT32 mmc_voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
  33. #define READ_INT32(r) *((volatile UINT32 *)(r))
  34. #define WRITE_INT32(r,d) (*((volatile UINT32 *)(r)) = d)
  35. #define SDHC_REG_READ32(c,r)\
  36. (READ_INT32(SDHC_Controls[(c)] + (r)))
  37. #define SDHC_REG_WRITE32(c,r,v)\
  38. (WRITE_INT32(SDHC_Controls[(c)] + (r),(v)))
  39. #define SDMMC_CTRL 0x00
  40. #define SDMMC_PWREN 0x04
  41. #define SDMMC_CLKDIV 0x08
  42. #define SDMMC_CLKSRC 0x0C
  43. #define SDMMC_CLKENA 0x10
  44. #define SDMMC_TMOUT 0x14
  45. #define SDMMC_CTYPE 0x18
  46. #define SDMMC_BLKSIZ 0x1C
  47. #define SDMMC_BYTCNT 0x20
  48. #define SDMMC_INTMASK 0x24
  49. #define SDMMC_CMDARG 0x28
  50. #define SDMMC_CMD 0x2C
  51. #define SDMMC_RESP0 0x30
  52. #define SDMMC_RESP1 0x34
  53. #define SDMMC_RESP2 0x38
  54. #define SDMMC_RESP3 0x3C
  55. #define SDMMC_MINTSTS 0x40
  56. #define SDMMC_RINTSTS 0x44
  57. #define SDMMC_STATUS 0x48
  58. #define SDMMC_FIFOTH 0x4C
  59. #define SDMMC_CDETECT 0x50
  60. #define SDMMC_WRTPRT 0x54
  61. #define SDMMC_GPIO 0x58
  62. #define SDMMC_TCBCNT 0x5C
  63. #define SDMMC_TBBCNT 0x60
  64. #define SDMMC_DEBNCE 0x64
  65. #define SDMMC_USRID 0x68
  66. #define SDMMC_VERID 0x6C
  67. #define SDMMC_HCON 0x70
  68. #define SDMMC_UHS_REG 0x74
  69. #define SDMMC_BMOD 0x80
  70. #define SDMMC_PLDMND 0x84
  71. #define SDMMC_DBADDR 0x88
  72. #define SDMMC_IDSTS 0x8C
  73. #define SDMMC_IDINTEN 0x90
  74. #define SDMMC_DATA 0x200
  75. #define SDMMC_FIFO (SDHC_Controls[lg_ulChip] + 0x200)
  76. /* Interrupt Mask register */
  77. #define SDMMC_INTMSK_ALL 0xffffffff
  78. #define SDMMC_INTMSK_RE (1 << 1)
  79. #define SDMMC_INTMSK_CDONE (1 << 2)
  80. #define SDMMC_INTMSK_DTO (1 << 3)
  81. #define SDMMC_INTMSK_TXDR (1 << 4)
  82. #define SDMMC_INTMSK_RXDR (1 << 5)
  83. #define SDMMC_INTMSK_DCRC (1 << 7)
  84. #define SDMMC_INTMSK_RTO (1 << 8)
  85. #define SDMMC_INTMSK_DRTO (1 << 9)
  86. #define SDMMC_INTMSK_HTO (1 << 10)
  87. #define SDMMC_INTMSK_FRUN (1 << 11)
  88. #define SDMMC_INTMSK_HLE (1 << 12)
  89. #define SDMMC_INTMSK_SBE (1 << 13)
  90. #define SDMMC_INTMSK_ACD (1 << 14)
  91. #define SDMMC_INTMSK_EBE (1 << 15)
  92. /* Raw interrupt Regsiter */
  93. #define SDMMC_DATA_ERR (SDMMC_INTMSK_EBE | SDMMC_INTMSK_SBE | SDMMC_INTMSK_HLE |\
  94. SDMMC_INTMSK_FRUN | SDMMC_INTMSK_EBE | SDMMC_INTMSK_DCRC)
  95. #define SDMMC_DATA_TOUT (SDMMC_INTMSK_HTO | SDMMC_INTMSK_DRTO)
  96. /* CTRL register */
  97. #define SDMMC_CTRL_RESET (1 << 0)
  98. #define SDMMC_CTRL_FIFO_RESET (1 << 1)
  99. #define SDMMC_CTRL_DMA_RESET (1 << 2)
  100. #define SDMMC_DMA_EN (1 << 5)
  101. #define SDMMC_CTRL_SEND_AS_CCSD (1 << 10)
  102. #define SDMMC_IDMAC_EN (1 << 25)
  103. #define SDMMC_RESET_ALL (SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET |\
  104. SDMMC_CTRL_DMA_RESET)
  105. /* CMD register */
  106. #define SDMMC_CMD_RESP_EXP (1 << 6)
  107. #define SDMMC_CMD_RESP_LENGTH (1 << 7)
  108. #define SDMMC_CMD_CHECK_CRC (1 << 8)
  109. #define SDMMC_CMD_DATA_EXP (1 << 9)
  110. #define SDMMC_CMD_RW (1 << 10)
  111. #define SDMMC_CMD_SEND_STOP (1 << 12)
  112. #define SDMMC_CMD_ABORT_STOP (1 << 14)
  113. #define SDMMC_CMD_PRV_DAT_WAIT (1 << 13)
  114. #define SDMMC_CMD_UPD_CLK (1 << 21)
  115. #define SDMMC_CMD_USE_HOLD_REG (1 << 29)
  116. #define SDMMC_CMD_START (1UL << 31)
  117. /* CLKENA register */
  118. #define SDMMC_CLKEN_ENABLE (1 << 0)
  119. #define SDMMC_CLKEN_LOW_PWR (1 << 16)
  120. /* Card-type registe */
  121. #define SDMMC_CTYPE_1BIT 0
  122. #define SDMMC_CTYPE_4BIT (1 << 0)
  123. #define SDMMC_CTYPE_8BIT (1 << 16)
  124. /* Status Register */
  125. #define SDMMC_BUSY (1 << 9)
  126. #define SDMMC_FIFO_MASK 0x1fff
  127. #define SDMMC_FIFO_SHIFT 17
  128. /* FIFOTH Register */
  129. #define MSIZE(x) ((x) << 28)
  130. #define RX_WMARK(x) ((x) << 16)
  131. #define TX_WMARK(x) (x)
  132. #define RX_WMARK_SHIFT 16
  133. #define RX_WMARK_MASK (0xfff << RX_WMARK_SHIFT)
  134. #define fifo_tran_over() do{}while (!(SDHC_REG_READ32(lg_ulChip,SDMMC_STATUS) & 0x00000004))
  135. #define confi_fifo_full() do{}while (!(SDHC_REG_READ32(lg_ulChip,SDMMC_STATUS) & 0x00000008))
  136. //#define confi_fifo_unempty() do{}while ((rSTATUS & 0x00000004))
  137. /* frequency bases */
  138. /* divided by 10 to be nice to platforms without floating point */
  139. static const int fbase[] = {
  140. 10000,
  141. 100000,
  142. 1000000,
  143. 10000000,
  144. };
  145. /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
  146. * to platforms without floating point.
  147. */
  148. static const UINT8 multipliers[] = {
  149. 0, /* reserved */
  150. 10,
  151. 12,
  152. 13,
  153. 15,
  154. 20,
  155. 25,
  156. 30,
  157. 35,
  158. 40,
  159. 45,
  160. 50,
  161. 55,
  162. 60,
  163. 70,
  164. 80,
  165. };
  166. //extern volatile FS_CARD SDCard;
  167. static UINT32 fifoth_val;
  168. static UINT32 sd_ocr;
  169. static UINT32 sd_cid[4];
  170. static UINT32 sd_csd[4];
  171. static UINT32 sd_rca;
  172. static UINT16 read_bl_len;
  173. static UINT64 capacity_user;
  174. typedef enum {MMC, SD}CARDTYPE;
  175. static CARDTYPE CardType = SD; // 1 sd card, 0 means MMC
  176. static enum {Ver0, Ver1, Ver2} Spec = Ver1;
  177. static enum {Standard, High} Capacity = Standard;
  178. //static INT8 *sd_spec[3]={"1.0 & 1.01", "1.1", "2.0"};
  179. //static INT8 *mmc_spec[5]={"1.0-1.2", "1.4", "2.0-2.2","3.1-3.2-3.31","4.x"};
  180. //static UINT8 SD_SPEC;
  181. //static UINT8 MMC_SPEC;
  182. static UINT32 g_MatchSdVcc = 1;
  183. #define SDMMC_SECTOR_SIZE 512
  184. static void select_sd_pad(UINT32 ulChip)
  185. {
  186. UINT32 val;
  187. UINT32 regval;
  188. val = rSYS_BOOT_SAMPLE;
  189. val = (val>>2)&0x1;
  190. if(val == 1)
  191. { /*sd0 128pin*/
  192. regval = rSYS_PAD_CTRL01;
  193. regval &= ~((0x3<<12)|(0x3<<10)|(0x3<<8)|(0x3<<6)|(0x3<<4)|(0x3<<2)|(0x3<<0));
  194. regval |= ((0x1<<12)|(0x1<<10)|(0x1<<8)|(0x1<<6)|(0x1<<4)|(0x1<<2)|(0x1<<0));
  195. rSYS_PAD_CTRL01 = regval;
  196. regval = rSYS_PAD_CTRL07;
  197. regval &= ~((0x1<<26)|(0x1<<25)|(0x1<<24)|(0x1<<2)|(0x1<<1)|(0x1<<0));
  198. rSYS_PAD_CTRL07 = regval;
  199. }
  200. else
  201. {
  202. /*sd0 96pin*/
  203. regval = rSYS_PAD_CTRL01;
  204. regval &= ~((0x3<<20)|(0x3<<18)|(0x3<<16)|(0x3<<14));
  205. regval |= ((0x3<<20)|(0x3<<18)|(0x3<<16)|(0x3<<14));
  206. rSYS_PAD_CTRL01 = regval;
  207. regval = rSYS_PAD_CTRL02;
  208. regval &= ~((0x3<<28)|(0x3<<26)|(0x3<<24));
  209. regval |= ((0x2<<28)|(0x2<<26)|(0x2<<24));
  210. rSYS_PAD_CTRL02 = regval;
  211. regval = rSYS_PAD_CTRL07;
  212. regval &= ~((0x1<<31)|(0x1<<26)|(0x1<<25)|(0x1<<24));
  213. regval |= (0x1<<31)|(0x1<<26)|(0x1<<25)|(0x1<<24);
  214. rSYS_PAD_CTRL07 = regval;
  215. }
  216. // rSYS_SDMMC_CLK_CFG |= (1<<6);
  217. }
  218. static int sdmmc_wait_reset(UINT32 value)
  219. {
  220. unsigned long timeout = 1000;
  221. UINT32 ctrl;
  222. SDHC_REG_WRITE32(lg_ulChip, SDMMC_CTRL, value);
  223. while (timeout--) {
  224. ctrl = SDHC_REG_READ32(lg_ulChip, SDMMC_CTRL);
  225. if (!(ctrl & SDMMC_RESET_ALL))
  226. return -1;
  227. }
  228. return 0;
  229. }
  230. static int sdmmc_data_transfer(struct mmc_data *data)
  231. {
  232. int ret = 0;
  233. UINT32 timeout = 240000;
  234. UINT32 mask, size, i, len = 0;
  235. UINT32 *buf = NULL;
  236. ULONG start = get_timer(0);
  237. UINT32 fifo_depth = (((fifoth_val & RX_WMARK_MASK) >>
  238. RX_WMARK_SHIFT) + 1) * 2;
  239. size = data->blocksize * data->blocks / 4;
  240. if (data->flags == MMC_DATA_READ)
  241. buf = (unsigned int *)data->dest;
  242. else
  243. buf = (unsigned int *)data->src;
  244. for (;;) {
  245. mask = SDHC_REG_READ32(lg_ulChip, SDMMC_RINTSTS);
  246. /* Error during data transfer. */
  247. if (mask & (SDMMC_DATA_ERR | SDMMC_DATA_TOUT)) {
  248. DEBUG_MSG("%s: DATA ERROR!\n", __func__);
  249. ret = -1;
  250. break;
  251. }
  252. if (size) {
  253. len = 0;
  254. if (data->flags == MMC_DATA_READ &&
  255. (mask & SDMMC_INTMSK_RXDR)) {
  256. while (size) {
  257. len = SDHC_REG_READ32(lg_ulChip, SDMMC_STATUS);
  258. len = (len >> SDMMC_FIFO_SHIFT) &
  259. SDMMC_FIFO_MASK;
  260. len = min(size, len);
  261. for (i = 0; i < len; i++)
  262. *buf++ = SDHC_REG_READ32(lg_ulChip, SDMMC_DATA);
  263. size = size > len ? (size - len) : 0;
  264. }
  265. SDHC_REG_WRITE32(lg_ulChip, SDMMC_RINTSTS,
  266. SDMMC_INTMSK_RXDR);
  267. } else if (data->flags == MMC_DATA_WRITE &&
  268. (mask & SDMMC_INTMSK_TXDR)) {
  269. while (size) {
  270. len = SDHC_REG_READ32(lg_ulChip, SDMMC_STATUS);
  271. len = fifo_depth - ((len >>
  272. SDMMC_FIFO_SHIFT) &
  273. SDMMC_FIFO_MASK);
  274. len = min(size, len);
  275. for (i = 0; i < len; i++)
  276. SDHC_REG_WRITE32(lg_ulChip, SDMMC_DATA,
  277. *buf++);
  278. size = size > len ? (size - len) : 0;
  279. }
  280. SDHC_REG_WRITE32(lg_ulChip, SDMMC_RINTSTS,
  281. SDMMC_INTMSK_TXDR);
  282. }
  283. }
  284. /* Data arrived correctly. */
  285. if (mask & SDMMC_INTMSK_DTO) {
  286. ret = 0;
  287. break;
  288. }
  289. /* Check for timeout. */
  290. if (get_timer(start) > timeout) {
  291. DEBUG_MSG("%s: Timeout waiting for data!\n",
  292. __func__);
  293. ret = -1;
  294. break;
  295. }
  296. }
  297. SDHC_REG_WRITE32(lg_ulChip, SDMMC_RINTSTS, mask);
  298. return ret;
  299. }
  300. static int sdmmc_set_transfer_mode(struct mmc_data *data)
  301. {
  302. unsigned long mode;
  303. mode = SDMMC_CMD_DATA_EXP;
  304. if (data->flags & MMC_DATA_WRITE)
  305. mode |= SDMMC_CMD_RW;
  306. return mode;
  307. }
  308. static int mmc_send_cmd(struct mmc_cmd *cmd, struct mmc_data *data)
  309. {
  310. int ret = 0, flags = 0, i;
  311. unsigned int timeout = 500;
  312. UINT32 retry = 100000;
  313. UINT32 mask;
  314. ULONG start = get_timer(0);
  315. while (SDHC_REG_READ32(lg_ulChip, SDMMC_STATUS) & SDMMC_BUSY) {
  316. if (get_timer(start) > timeout) {
  317. DEBUG_MSG("%s: Timeout on data busy\n", __func__);
  318. return SDMMC_RW_CMDTIMEROUT;
  319. }
  320. }
  321. SDHC_REG_WRITE32(lg_ulChip, SDMMC_RINTSTS, SDMMC_INTMSK_ALL);
  322. if (data) {
  323. SDHC_REG_WRITE32(lg_ulChip, SDMMC_BLKSIZ, data->blocksize);
  324. SDHC_REG_WRITE32(lg_ulChip, SDMMC_BYTCNT,
  325. data->blocksize * data->blocks);
  326. sdmmc_wait_reset(SDMMC_CTRL_FIFO_RESET);
  327. }
  328. SDHC_REG_WRITE32(lg_ulChip, SDMMC_CMDARG, cmd->cmdarg);
  329. if (data)
  330. flags = sdmmc_set_transfer_mode(data);
  331. if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
  332. return -1;
  333. if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
  334. flags |= SDMMC_CMD_ABORT_STOP;
  335. else
  336. flags |= SDMMC_CMD_PRV_DAT_WAIT;
  337. if (cmd->resp_type & MMC_RSP_PRESENT) {
  338. flags |= SDMMC_CMD_RESP_EXP;
  339. if (cmd->resp_type & MMC_RSP_136)
  340. flags |= SDMMC_CMD_RESP_LENGTH;
  341. }
  342. if (cmd->resp_type & MMC_RSP_CRC)
  343. flags |= SDMMC_CMD_CHECK_CRC;
  344. flags |= (cmd->cmdidx | SDMMC_CMD_START | SDMMC_CMD_USE_HOLD_REG);
  345. DEBUG_MSG("Sending CMD%d\n",cmd->cmdidx);
  346. SDHC_REG_WRITE32(lg_ulChip, SDMMC_CMD, flags);
  347. for (i = 0; i < retry; i++) {
  348. mask = SDHC_REG_READ32(lg_ulChip, SDMMC_RINTSTS);
  349. if (mask & SDMMC_INTMSK_CDONE) {
  350. if (!data)
  351. SDHC_REG_WRITE32(lg_ulChip, SDMMC_RINTSTS, mask);
  352. break;
  353. }
  354. }
  355. if (i == retry) {
  356. DEBUG_MSG("%s: Timeout.\n", __func__);
  357. return SDMMC_RW_CMDTIMEROUT;
  358. }
  359. if (mask & SDMMC_INTMSK_RTO) {
  360. /*
  361. * Timeout here is not necessarily fatal. (e)MMC cards
  362. * will splat here when they receive CMD55 as they do
  363. * not support this command and that is exactly the way
  364. * to tell them apart from SD cards. Thus, this output
  365. * below shall be debug(). eMMC cards also do not favor
  366. * CMD8, please keep that in mind.
  367. */
  368. DEBUG_MSG("%s: Response Timeout.\n", __func__);
  369. return SDMMC_RW_CMDTIMEROUT;
  370. } else if (mask & SDMMC_INTMSK_RE) {
  371. DEBUG_MSG("%s: Response Error.\n", __func__);
  372. return -1;
  373. }
  374. if (cmd->resp_type & MMC_RSP_PRESENT) {
  375. if (cmd->resp_type & MMC_RSP_136) {
  376. cmd->response[0] = SDHC_REG_READ32(lg_ulChip, SDMMC_RESP3);
  377. cmd->response[1] = SDHC_REG_READ32(lg_ulChip, SDMMC_RESP2);
  378. cmd->response[2] = SDHC_REG_READ32(lg_ulChip, SDMMC_RESP1);
  379. cmd->response[3] = SDHC_REG_READ32(lg_ulChip, SDMMC_RESP0);
  380. } else {
  381. cmd->response[0] = SDHC_REG_READ32(lg_ulChip, SDMMC_RESP0);
  382. }
  383. }
  384. if (data) {
  385. ret = sdmmc_data_transfer(data);
  386. }
  387. udelay(100);
  388. return ret;
  389. }
  390. /* static void GetCardInfo(INT32 *CID, CARDTYPE ct)
  391. {
  392. UINT16 Year;
  393. UINT8 Month;
  394. UINT8 CardName[7];
  395. UINT8 RevisionN;
  396. UINT8 RevisionM;
  397. if(ct== MMC)
  398. {
  399. Month = (UINT8)((CID[0]&0x0000f000)>>12);
  400. Year = (unsigned long)((CID[0]&0x00000f00)>>8)+1997;
  401. RevisionN = (UINT8)((CID[1]&0x00f00000)>>20);
  402. RevisionM = (UINT8)((CID[1]&0x000f0000)>>16);
  403. CardName[0] = (UINT8)(CID[3]&0x000000ff);
  404. CardName[1] = (UINT8)((CID[2]&0xff000000)>>24);
  405. CardName[2] = (UINT8)((CID[2]&0x00ff0000)>>16);
  406. CardName[3] = (UINT8)((CID[2]&0x0000ff00)>>8);
  407. CardName[4] = (UINT8)(CID[2]&0x000000ff);
  408. CardName[5] = (UINT8)((CID[1]&0xff000000)>>24);
  409. CardName[6] = '\0';
  410. DEBUG_MSG("Product Name : %s\n", CardName);
  411. DEBUG_MSG("Product revision : %d.%d\n", RevisionN, RevisionM);
  412. DEBUG_MSG("Manufacturing date %d-%d\n", Month, Year);
  413. }
  414. else
  415. {
  416. Month = (UINT8)((CID[0]&0x00000f00)>>8);
  417. Year = (unsigned long)((CID[0]&0x000ff000)>>12)+2000;
  418. RevisionN = (UINT8)((CID[1]&0xf0000000)>>28);
  419. RevisionM = (UINT8)((CID[1]&0x0f00000)>>24);
  420. CardName[0] = (UINT8)(CID[3]&0x000000ff);
  421. CardName[1] = (UINT8)((CID[2]&0xff000000)>>24);
  422. CardName[2] = (UINT8)((CID[2]&0x00ff0000)>>16);
  423. CardName[3] = (UINT8)((CID[2]&0x0000ff00)>>8);
  424. CardName[4] = (UINT8)(CID[2]&0x000000ff);
  425. CardName[5] = '\0';
  426. DEBUG_MSG("Product Name : %s\n", CardName);
  427. DEBUG_MSG("Product revision : %d.%d\n", RevisionN, RevisionM);
  428. DEBUG_MSG("Manufacturing date %d-%d\n", Month, Year);
  429. }
  430. } */
  431. /* static UINT8 GetSD_SPEC(UINT32 *scr)
  432. {
  433. UINT8 ss;
  434. ss=(UINT8)(scr[0]&0x0000000f);
  435. return ss;
  436. }
  437. static UINT8 GetMMC_SPEC(void)
  438. {
  439. UINT8 ss;
  440. ss = (sd_csd[3]>>26)&0x0000000f;
  441. return ss;
  442. } */
  443. INT32 data_tran_over(void)
  444. {
  445. INT32 count_sd = 0;
  446. INT32 tt;
  447. do
  448. {
  449. count_sd++;
  450. if(count_sd > TIMEOUT_SD)
  451. return -1;
  452. //tt = rSDMMC_RINTSTS; //Command done
  453. tt = SDHC_REG_READ32(lg_ulChip, SDMMC_RINTSTS);
  454. }while (!(tt & 0x00000008));
  455. if((tt&0xBFC2) != 0)
  456. {
  457. DEBUG_MSG("This rSDMMC_RINTSTS = 0x%x\n", tt);
  458. //rSDMMC_RINTSTS = 0x0000ffff;
  459. SDHC_REG_WRITE32(lg_ulChip, SDMMC_RINTSTS, 0x0000ffff);
  460. return -1;
  461. }
  462. //rSDMMC_RINTSTS = 0x0000ffff;
  463. SDHC_REG_WRITE32(lg_ulChip, SDMMC_RINTSTS, 0x0000ffff);
  464. return 0; // 0 means successful
  465. }
  466. static INT32 mmc_go_idle(void)
  467. {
  468. struct mmc_cmd cmd;
  469. int err;
  470. udelay(1000);
  471. cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
  472. cmd.cmdarg = 0;
  473. cmd.resp_type = MMC_RSP_NONE;
  474. err = mmc_send_cmd(&cmd, NULL);
  475. if (err)
  476. return err;
  477. udelay(2000);
  478. return 0;
  479. }
  480. static int mmc_set_blocklen(int len)
  481. {
  482. struct mmc_cmd cmd;
  483. int err;
  484. cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
  485. cmd.resp_type = MMC_RSP_R1;
  486. cmd.cmdarg = len;
  487. err = mmc_send_cmd(&cmd, NULL);
  488. return err;
  489. }
  490. static int sd_select_bus_width(int w)
  491. {
  492. int err;
  493. struct mmc_cmd cmd;
  494. if ((w != 4) && (w != 1))
  495. return -1;
  496. cmd.cmdidx = MMC_CMD_APP_CMD;
  497. cmd.resp_type = MMC_RSP_R1;
  498. cmd.cmdarg = sd_rca << 16;
  499. err = mmc_send_cmd(&cmd, NULL);
  500. if (err)
  501. return err;
  502. cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
  503. cmd.resp_type = MMC_RSP_R1;
  504. if (w == 4)
  505. cmd.cmdarg = 2;
  506. else if (w == 1)
  507. cmd.cmdarg = 0;
  508. err = mmc_send_cmd(&cmd, NULL);
  509. if (err)
  510. return err;
  511. return 0;
  512. }
  513. /* static UINT8 get_width(UINT32 *scr)
  514. {
  515. if((scr[0]&0x00000f00) == 0x00000500)
  516. return 4;
  517. else
  518. return 1;
  519. } */
  520. static int sdmmc_setup_bus(UINT32 freq)
  521. {
  522. UINT32 div, status;
  523. int timeout = 10000;
  524. unsigned long sclk = SDMMC_BUS_CLK;
  525. div = sclk / freq;
  526. if (sclk % freq && sclk > freq)
  527. div += 1;
  528. div = (sclk != freq) ? DIV_ROUND_UP(div, 2) : 0;
  529. #if 0
  530. if (sclk == freq)
  531. div = 0; /* bypass mode */
  532. else
  533. div = DIV_ROUND_UP(sclk, 2 * freq);
  534. #endif
  535. SDHC_REG_WRITE32(lg_ulChip, SDMMC_CLKENA, 0);
  536. SDHC_REG_WRITE32(lg_ulChip, SDMMC_CLKSRC, 0);
  537. SDHC_REG_WRITE32(lg_ulChip, SDMMC_CLKDIV, div);
  538. SDHC_REG_WRITE32(lg_ulChip, SDMMC_CMD, SDMMC_CMD_PRV_DAT_WAIT |
  539. SDMMC_CMD_UPD_CLK | SDMMC_CMD_START);
  540. do {
  541. status = SDHC_REG_READ32(lg_ulChip, SDMMC_CMD);
  542. if (timeout-- < 0) {
  543. DEBUG_MSG("%s: Timeout!\n", __func__);
  544. return -1;
  545. }
  546. } while (status & SDMMC_CMD_START);
  547. SDHC_REG_WRITE32(lg_ulChip, SDMMC_CLKENA, SDMMC_CLKEN_ENABLE |
  548. SDMMC_CLKEN_LOW_PWR);
  549. SDHC_REG_WRITE32(lg_ulChip, SDMMC_CMD, SDMMC_CMD_PRV_DAT_WAIT |
  550. SDMMC_CMD_UPD_CLK | SDMMC_CMD_START);
  551. timeout = 10000;
  552. do {
  553. status = SDHC_REG_READ32(lg_ulChip, SDMMC_CMD);
  554. if (timeout-- < 0) {
  555. DEBUG_MSG("%s: Timeout!\n", __func__);
  556. return -1;
  557. }
  558. } while (status & SDMMC_CMD_START);
  559. return 0;
  560. }
  561. static int sdmmc_hw_init()
  562. {
  563. UINT32 fifo_size;
  564. SDHC_REG_WRITE32(lg_ulChip, SDMMC_PWREN, 1);
  565. if (!sdmmc_wait_reset(SDMMC_RESET_ALL)) {
  566. DEBUG_MSG("%s[%d] Fail-reset!!\n", __func__, __LINE__);
  567. return -1;
  568. }
  569. /* Enumerate at 400KHz */
  570. sdmmc_setup_bus(INIT_CLOCK);
  571. SDHC_REG_WRITE32(lg_ulChip, SDMMC_RINTSTS, 0xFFFFFFFF);
  572. SDHC_REG_WRITE32(lg_ulChip, SDMMC_INTMASK, 0);
  573. SDHC_REG_WRITE32(lg_ulChip, SDMMC_TMOUT, 0xFFFFFFFF);
  574. SDHC_REG_WRITE32(lg_ulChip, SDMMC_IDINTEN, 0);
  575. SDHC_REG_WRITE32(lg_ulChip, SDMMC_BMOD, 1);
  576. fifo_size = SDHC_REG_READ32(lg_ulChip, SDMMC_FIFOTH);
  577. fifo_size = ((fifo_size & RX_WMARK_MASK) >> RX_WMARK_SHIFT) + 1;
  578. fifoth_val = MSIZE(0x2) | RX_WMARK(fifo_size / 2 - 1) |
  579. TX_WMARK(fifo_size / 2);
  580. SDHC_REG_WRITE32(lg_ulChip, SDMMC_FIFOTH, fifoth_val);
  581. return 0;
  582. }
  583. static int mmc_send_if_cond()
  584. {
  585. struct mmc_cmd cmd;
  586. int err;
  587. cmd.cmdidx = SD_CMD_SEND_IF_COND;
  588. /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
  589. cmd.cmdarg = (1 << 8) | 0xaa;
  590. cmd.resp_type = MMC_RSP_R7;
  591. err = mmc_send_cmd(&cmd, NULL);
  592. if (err)
  593. return err;
  594. if ((cmd.response[0] & 0xff) != 0xaa)
  595. return -1;
  596. else
  597. Spec = Ver2;
  598. return 0;
  599. }
  600. static int sd_send_op_cond()
  601. {
  602. int timeout = 1000;
  603. int err;
  604. struct mmc_cmd cmd;
  605. while (1) {
  606. cmd.cmdidx = MMC_CMD_APP_CMD;
  607. cmd.resp_type = MMC_RSP_R1;
  608. cmd.cmdarg = 0;
  609. err = mmc_send_cmd(&cmd, NULL);
  610. if (err)
  611. return err;
  612. cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
  613. cmd.resp_type = MMC_RSP_R3;
  614. /*
  615. * Most cards do not answer if some reserved bits
  616. * in the ocr are set. However, Some controller
  617. * can set bit 7 (reserved for low voltages), but
  618. * how to manage low voltages SD card is not yet
  619. * specified.
  620. */
  621. cmd.cmdarg = mmc_voltages & 0xff8000;
  622. if (Spec == Ver2)
  623. cmd.cmdarg |= OCR_HCS;
  624. err = mmc_send_cmd(&cmd, NULL);
  625. if (err)
  626. return err;
  627. if (cmd.response[0] & OCR_BUSY)
  628. break;
  629. if (timeout-- <= 0)
  630. return -1;
  631. udelay(1000);
  632. }
  633. sd_ocr = cmd.response[0];
  634. if ((sd_ocr & OCR_HCS) == OCR_HCS)
  635. Capacity = High;
  636. sd_rca = 0;
  637. return 0;
  638. }
  639. static int mmc_send_op_cond_iter(int use_arg)
  640. {
  641. struct mmc_cmd cmd;
  642. int err;
  643. cmd.cmdidx = MMC_CMD_SEND_OP_COND;
  644. cmd.resp_type = MMC_RSP_R3;
  645. cmd.cmdarg = 0;
  646. if (use_arg)
  647. cmd.cmdarg = OCR_HCS |
  648. (mmc_voltages &
  649. (sd_ocr & OCR_VOLTAGE_MASK)) |
  650. (sd_ocr & OCR_ACCESS_MODE);
  651. err = mmc_send_cmd(&cmd, NULL);
  652. if (err)
  653. return err;
  654. sd_ocr = cmd.response[0];
  655. return 0;
  656. }
  657. static int mmc_send_op_cond()
  658. {
  659. int err, i;
  660. /* Some cards seem to need this */
  661. mmc_go_idle();
  662. /* Asking to the card its capabilities */
  663. for (i = 0; i < 2; i++) {
  664. err = mmc_send_op_cond_iter(i != 0);
  665. if (err)
  666. return err;
  667. /* exit if not busy (flag seems to be inverted) */
  668. if (sd_ocr & OCR_BUSY)
  669. break;
  670. }
  671. return 0;
  672. }
  673. static int mmc_complete_op_cond()
  674. {
  675. int timeout = 1000;
  676. ULONG start;
  677. int err;
  678. if (!(sd_ocr & OCR_BUSY)) {
  679. /* Some cards seem to need this */
  680. mmc_go_idle();
  681. start = get_timer(0);
  682. while (1) {
  683. err = mmc_send_op_cond_iter(1);
  684. if (err)
  685. return err;
  686. if (sd_ocr & OCR_BUSY)
  687. break;
  688. if (get_timer(start) > timeout)
  689. return -1;
  690. udelay(100);
  691. }
  692. }
  693. if ((sd_ocr & OCR_HCS) == OCR_HCS);
  694. Capacity = High;
  695. sd_rca = 1;
  696. return 0;
  697. }
  698. int mmc_send_status(int timeout)
  699. {
  700. struct mmc_cmd cmd;
  701. int err, retries = 5;
  702. cmd.cmdidx = MMC_CMD_SEND_STATUS;
  703. cmd.resp_type = MMC_RSP_R1;
  704. cmd.cmdarg = sd_rca << 16;
  705. while (1) {
  706. err = mmc_send_cmd(&cmd, NULL);
  707. if (!err) {
  708. if ((cmd.response[0] & MMC_STATUS_RDY_FOR_DATA) &&
  709. (cmd.response[0] & MMC_STATUS_CURR_STATE) !=
  710. MMC_STATE_PRG)
  711. break;
  712. if (cmd.response[0] & MMC_STATUS_MASK) {
  713. return -1;
  714. }
  715. } else if (--retries < 0)
  716. return err;
  717. if (timeout-- <= 0)
  718. break;
  719. udelay(1000);
  720. }
  721. if (timeout <= 0) {
  722. return -1;
  723. }
  724. return 0;
  725. }
  726. int mmc_switch(UINT8 set, UINT8 index, UINT8 value)
  727. {
  728. struct mmc_cmd cmd;
  729. int timeout = 1000;
  730. int retries = 3;
  731. int ret;
  732. cmd.cmdidx = MMC_CMD_SWITCH;
  733. cmd.resp_type = MMC_RSP_R1b;
  734. cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
  735. (index << 16) |
  736. (value << 8);
  737. while (retries > 0) {
  738. ret = mmc_send_cmd(&cmd, NULL);
  739. /* Waiting for the ready status */
  740. if (!ret) {
  741. ret = mmc_send_status(timeout);
  742. return ret;
  743. }
  744. retries--;
  745. }
  746. return ret;
  747. }
  748. static int mmc_startup()
  749. {
  750. int err;
  751. UINT mult, freq;
  752. UINT64 cmult, csize;
  753. struct mmc_cmd cmd;
  754. /* Put the Card in Identify Mode */
  755. cmd.cmdidx = MMC_CMD_ALL_SEND_CID;
  756. cmd.resp_type = MMC_RSP_R2;
  757. cmd.cmdarg = 0;
  758. err = mmc_send_cmd(&cmd, NULL);
  759. if (err)
  760. return err;
  761. memcpy(sd_cid, cmd.response, 16);
  762. /*
  763. * For MMC cards, set the Relative Address.
  764. * For SD cards, get the Relatvie Address.
  765. * This also puts the cards into Standby State
  766. */
  767. cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
  768. cmd.cmdarg = sd_rca << 16;
  769. cmd.resp_type = MMC_RSP_R6;
  770. err = mmc_send_cmd(&cmd, NULL);
  771. if (err)
  772. return err;
  773. if (CardType == SD)
  774. sd_rca = (cmd.response[0] >> 16) & 0xffff;
  775. /* Get the Card-Specific Data */
  776. cmd.cmdidx = MMC_CMD_SEND_CSD;
  777. cmd.resp_type = MMC_RSP_R2;
  778. cmd.cmdarg = sd_rca << 16;
  779. err = mmc_send_cmd(&cmd, NULL);
  780. if (err)
  781. return err;
  782. sd_csd[0] = cmd.response[0];
  783. sd_csd[1] = cmd.response[1];
  784. sd_csd[2] = cmd.response[2];
  785. sd_csd[3] = cmd.response[3];
  786. /* divide frequency by 10, since the mults are 10x bigger */
  787. freq = fbase[(cmd.response[0] & 0x7)];
  788. mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
  789. read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
  790. if (Capacity == High) {
  791. csize = (sd_csd[1] & 0x3f) << 16
  792. | (sd_csd[2] & 0xffff0000) >> 16;
  793. cmult = 8;
  794. } else {
  795. csize = (sd_csd[1] & 0x3ff) << 2
  796. | (sd_csd[2] & 0xc0000000) >> 30;
  797. cmult = (sd_csd[2] & 0x00038000) >> 15;
  798. }
  799. capacity_user = (csize + 1) << (cmult + 2);
  800. capacity_user *= read_bl_len;
  801. if (read_bl_len > MMC_MAX_BLOCK_LEN)
  802. read_bl_len = MMC_MAX_BLOCK_LEN;
  803. /* Select the card, and put it into Transfer Mode */
  804. cmd.cmdidx = MMC_CMD_SELECT_CARD;
  805. cmd.resp_type = MMC_RSP_R1;
  806. cmd.cmdarg = sd_rca << 16;
  807. err = mmc_send_cmd(&cmd, NULL);
  808. if (err)
  809. return err;
  810. mmc_set_blocklen(read_bl_len);
  811. sdmmc_setup_bus(freq * mult);
  812. if (CardType == SD)
  813. {
  814. err = sd_select_bus_width(4);
  815. SDHC_REG_WRITE32(0, SDMMC_CTYPE, 0x00000001);
  816. }
  817. else
  818. err = mmc_switch(EXT_CSD_CMD_SET_NORMAL,
  819. EXT_CSD_BUS_WIDTH,
  820. EXT_CSD_BUS_WIDTH_1);
  821. if (err)
  822. return err;
  823. return 0;
  824. }
  825. INT32 sd_mmc_card_identify(void)
  826. {
  827. INT32 ret;
  828. INT32 op_cond_pending = 0;
  829. CardType = SD;
  830. Capacity = Standard; //need to re_initialize in case of MMC card
  831. sdmmc_hw_init();
  832. // set the controller 1bit mode
  833. SDHC_REG_WRITE32(lg_ulChip, SDMMC_CTYPE, 0x00000000);
  834. if (mmc_go_idle())
  835. return -1;
  836. /* Test for SD version 2 */
  837. mmc_send_if_cond();
  838. if (g_MatchSdVcc)
  839. ret = sd_send_op_cond();
  840. else
  841. ret = SDMMC_RW_CMDTIMEROUT;
  842. if (ret == SDMMC_RW_CMDTIMEROUT)
  843. {
  844. /* If the command timed out, we check for an MMC card */
  845. ret = mmc_send_op_cond();
  846. if (ret) {
  847. DEBUG_MSG("Card did not respond to voltage select!\n");
  848. return -1;
  849. }
  850. CardType = MMC;
  851. op_cond_pending = 1;
  852. }
  853. if (op_cond_pending)
  854. mmc_complete_op_cond();
  855. return 0;
  856. }
  857. static int mmc_read_blocks(void *dst, ULONG start, ULONG blkcnt)
  858. {
  859. struct mmc_cmd cmd;
  860. struct mmc_data data;
  861. if (blkcnt > 1)
  862. cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
  863. else
  864. cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
  865. if (Capacity == High)
  866. cmd.cmdarg = start;
  867. else
  868. cmd.cmdarg = start * read_bl_len;
  869. cmd.resp_type = MMC_RSP_R1;
  870. data.dest = dst;
  871. data.blocks = blkcnt;
  872. data.blocksize = read_bl_len;
  873. data.flags = MMC_DATA_READ;
  874. if (mmc_send_cmd(&cmd, &data))
  875. return 0;
  876. if (blkcnt > 1) {
  877. cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
  878. cmd.cmdarg = 0;
  879. cmd.resp_type = MMC_RSP_R1b;
  880. if (mmc_send_cmd(&cmd, NULL)) {
  881. return 0;
  882. }
  883. }
  884. return blkcnt;
  885. }
  886. static INT32 SD_Identify(UINT32 Unit)
  887. {
  888. INT32 ret;
  889. ret = sd_mmc_card_identify();
  890. // if(ret)
  891. // DEBUG_MSG("SD_Identify fails\n");
  892. return ret;
  893. }
  894. INT32 SD_Init(UINT32 Unit)
  895. {
  896. INT32 ret;
  897. ret = mmc_startup();
  898. return ret;
  899. }
  900. INT32 SD_ReadSector(UINT32 Unit,ULONG Sector,UINT8 *pBuffer, ULONG Count)
  901. {
  902. INT32 ret;
  903. ret = mmc_read_blocks((UINT32 *)pBuffer, Sector, Count);
  904. return ret;
  905. }
  906. INT32 InitSDMMCCard()
  907. {
  908. INT32 result = -1;
  909. if (!SD_Identify(lg_ulChip))
  910. {
  911. SendUartString("SDMMC card successfully\n");
  912. if (!SD_Init(lg_ulChip))
  913. {
  914. result = 0;
  915. }
  916. }
  917. else
  918. {
  919. SendUartString("SDMMC card identify failed\n");
  920. }
  921. return result;
  922. }
  923. int IsCardExist(void)
  924. {
  925. return !(SDHC_REG_READ32(lg_ulChip, SDMMC_CDETECT) & 0x1);
  926. }
  927. #ifdef CHECK_FISRT_FILE
  928. static BYTE *g_rBuffer = g_fs.win;
  929. static INT32 check_fatfs(UINT8 *buf)
  930. {
  931. if (!memcmp(&buf[BS_FilSysType], "FAT", 3)) /* Check FAT signature */
  932. return 0;
  933. if (!memcmp(&buf[BS_FilSysType32], "FAT32", 5) && !(buf[BPB_ExtFlags] & 0x80))
  934. return 0;
  935. return 1;
  936. }
  937. static INT32 check_loaderfile(void)
  938. {
  939. BYTE fmt, *tbl;
  940. DWORD bootsect = 0, fatsize, fatbase, totalsect, csize, n_rootdir, maxclust, dirbase;
  941. INT i;
  942. if (mmc_read_blocks((UINT32 *)g_rBuffer, 0, 1) != 1)
  943. return 1;
  944. fmt = check_fatfs(g_rBuffer);
  945. #if 0
  946. if (fmt == 1) { /* Not an FAT boot record, it may be patitioned */
  947. /* Check a partition listed in top of the partition table */
  948. tbl = &g_rBuffer[MBR_Table]; /* Partition table */
  949. if (tbl[4]) { /* Is the partition existing? */
  950. bootsect = LD_DWORD(&tbl[8]); /* Partition offset in LBA */
  951. if (mmc_read_blocks((UINT32 *)g_rBuffer, bootsect, 1) != 1)
  952. return 1;
  953. fmt = check_fatfs(g_rBuffer); /* Check the partition */
  954. }
  955. }
  956. #else
  957. /* Check a first fat partition listed in partition table */
  958. for(i = 0; i < 4; i++) {
  959. tbl = &g_rBuffer[MBR_Table + i * 16]; /* Partition table */
  960. if (tbl[4] == 0x01 || tbl[4] == 0x04 || tbl[4] == 0x06 || tbl[4] == 0x0B ||
  961. tbl[4] == 0x0C || tbl[4] == 0x0E) { /* Is the FAT partition? */
  962. bootsect = LD_DWORD(&tbl[8]); /* Partition offset in LBA */
  963. if (mmc_read_blocks((UINT32 *)g_rBuffer, bootsect, 1) != 1)
  964. return 1;
  965. fmt = check_fatfs(g_rBuffer); /* Check the partition */
  966. break;
  967. }
  968. }
  969. #endif
  970. if (fmt || LD_WORD(&g_rBuffer[BPB_BytsPerSec]) != 512) /* No valid FAT patition is found */
  971. {
  972. SendUartString("Not found fatfs.\r\n");
  973. return 2;
  974. }
  975. /* Initialize the file system object */
  976. fatsize = LD_WORD(&g_rBuffer[BPB_FATSz16]); /* Number of sectors per FAT */
  977. if (!fatsize) fatsize = LD_DWORD(&g_rBuffer[BPB_FATSz32]);
  978. fatsize *= g_rBuffer[BPB_NumFATs]; /* (Number of sectors in FAT area) */
  979. fatbase = bootsect + LD_WORD(&g_rBuffer[BPB_RsvdSecCnt]); /* FAT start sector (lba) */
  980. csize = g_rBuffer[BPB_SecPerClus]; /* Number of sectors per cluster */
  981. n_rootdir = LD_WORD(&g_rBuffer[BPB_RootEntCnt]); /* Nmuber of root directory entries */
  982. totalsect = LD_WORD(&g_rBuffer[BPB_TotSec16]); /* Number of sectors on the file system */
  983. if (!totalsect) totalsect = LD_DWORD(&g_rBuffer[BPB_TotSec32]);
  984. maxclust = (totalsect /* max_clust = Last cluster# + 1 */
  985. - LD_WORD(&g_rBuffer[BPB_RsvdSecCnt]) - fatsize - n_rootdir / (512/32)
  986. ) / csize + 2;
  987. fmt = FS_FAT12; /* Determine the FAT sub type */
  988. if (maxclust >= 0xFF7) fmt = FS_FAT16;
  989. if (maxclust >= 0xFFF7) fmt = FS_FAT32;
  990. dirbase = fatbase + fatsize; /* Root directory start sector (lba) */
  991. if (fmt == FS_FAT32)
  992. {
  993. DWORD clust = LD_DWORD(&g_rBuffer[BPB_RootClus]);
  994. if(clust <= maxclust)
  995. dirbase += csize * (clust - 2); /* Root directory start cluster */
  996. }
  997. if (mmc_read_blocks((UINT32 *)g_rBuffer, dirbase, 1) != 1)
  998. return 1;
  999. //有卷标时第一个拷贝的文件位于第二个目录项
  1000. if (!memcmp(&g_rBuffer[0], "ARKSDLDRBIN", 11) ||
  1001. !memcmp(&g_rBuffer[32], "ARKSDLDRBIN", 11)
  1002. )
  1003. {
  1004. return 0;
  1005. }
  1006. //Win10下格式化会自动生成一个System Volume Information目录
  1007. else if (!memcmp(&g_rBuffer[64], "SYSTEM~1 ", 11))
  1008. {
  1009. if (!memcmp(&g_rBuffer[96], "ARKSDLDRBIN", 11) ||
  1010. !memcmp(&g_rBuffer[128], "ARKSDLDRBIN", 11))
  1011. return 0;
  1012. }
  1013. //有卷标
  1014. else if (!memcmp(&g_rBuffer[96], "SYSTEM~1 ", 11))
  1015. {
  1016. if (!memcmp(&g_rBuffer[128], "ARKSDLDRBIN", 11) ||
  1017. !memcmp(&g_rBuffer[160], "ARKSDLDRBIN", 11))
  1018. return 0;
  1019. }
  1020. else
  1021. {
  1022. SendUartString("Not found ARKSDLDR.bin.\r\n");
  1023. return 3;
  1024. }
  1025. return -1;
  1026. }
  1027. #endif
  1028. extern void UpdateFromMedia(int drv);
  1029. void updateFromSD(int chipid)
  1030. {
  1031. lg_ulChip = chipid;
  1032. g_MatchSdVcc = 1;
  1033. select_sd_pad(lg_ulChip);
  1034. if(!IsCardExist())
  1035. {
  1036. SendUartString("Card is not in the slot.\r\n");
  1037. return;
  1038. }
  1039. else
  1040. {
  1041. if(InitSDMMCCard())
  1042. {
  1043. SendUartString("InitSDMMCCard fail.\r\n");
  1044. return;
  1045. }
  1046. }
  1047. UpdateFromMedia(SDMMC);
  1048. }
  1049. // for fatfs's sd interface
  1050. static unsigned long sdmmc_disk_size;
  1051. #define SECOTR_SIZE 512
  1052. int MMC_disk_initialize()
  1053. {
  1054. UINT64 secotrs;
  1055. /* if(InitSDMMCCard() < 0)
  1056. {
  1057. SendUartString("disk mount fail!\r\n");
  1058. return -1;
  1059. }
  1060. else
  1061. SendUartString("disk mount success!\r\n"); */
  1062. secotrs = capacity_user / SECOTR_SIZE;
  1063. if(secotrs == -1) return -1;
  1064. //最大支持4G卡。
  1065. if(secotrs * SECOTR_SIZE > (unsigned int)-1 )
  1066. secotrs = ((unsigned int)-1)/SECOTR_SIZE;
  1067. sdmmc_disk_size = secotrs * SECOTR_SIZE;
  1068. return 0;
  1069. }
  1070. int MMC_disk_read(void *buff, DWORD sector, BYTE count)
  1071. {
  1072. return SD_ReadSector(0, sector, buff, count);
  1073. }
  1074. int MMC_disk_ioctl(BYTE ctrl, void *buff)
  1075. {
  1076. switch(ctrl)
  1077. {
  1078. case CTRL_SYNC:
  1079. break;
  1080. case GET_SECTOR_COUNT:
  1081. *(DWORD*)buff = sdmmc_disk_size / SECOTR_SIZE;
  1082. break;
  1083. case GET_BLOCK_SIZE:
  1084. *(DWORD*)buff = 1;
  1085. break;
  1086. default:
  1087. return -1;
  1088. }
  1089. return 0;
  1090. }