wm_sdio_host.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. #include "wm_sdio_host.h"
  2. #include "wm_debug.h"
  3. #include "wm_mem.h"
  4. #include "wm_dma.h"
  5. #include "wm_cpu.h"
  6. #define TEST_DEBUG_EN 0
  7. #if TEST_DEBUG_EN
  8. #define TEST_DEBUG(fmt, ...) printf("%s: "fmt, __func__, ##__VA_ARGS__)
  9. #else
  10. #define TEST_DEBUG(fmt, ...)
  11. #endif
  12. SD_CardInfo_t SDCardInfo;
  13. extern void delay_cnt(int count);
  14. static void sh_dumpBuffer(char *name, char* buffer, int len)
  15. {
  16. #if TEST_DEBUG_EN
  17. int i = 0;
  18. printf("%s:\n", name);
  19. for(; i < len; i++)
  20. {
  21. printf("%02X, ", buffer[i]);
  22. if((i + 1) % 16 == 0)
  23. {
  24. printf("\n");
  25. }
  26. }
  27. printf("\n");
  28. #endif
  29. }
  30. void wm_sdh_send_cmd(uint8_t cmdnum, uint32_t cmdarg, uint8_t mmcio)
  31. {
  32. SDIO_HOST->CMD_BUF[4] = cmdnum | 0x40;
  33. SDIO_HOST->CMD_BUF[3] = (cmdarg >> 24) & 0xFF;
  34. SDIO_HOST->CMD_BUF[2] = (cmdarg >> 16) & 0xFF;
  35. SDIO_HOST->CMD_BUF[1] = (cmdarg >> 8) & 0xFF;
  36. SDIO_HOST->CMD_BUF[0] = (cmdarg & 0xFF);
  37. SDIO_HOST->MMC_INT_SRC |= 0x7ff; // clear all firstly
  38. SDIO_HOST->MMC_IO = mmcio;
  39. }
  40. void wm_sdh_get_response(uint32_t * respbuf, uint32_t buflen)
  41. {
  42. int i = 0;
  43. for(i = 0; i < buflen; i++)
  44. {
  45. respbuf[i] = (SDIO_HOST->CMD_BUF[i*4 + 3] << 24) | (SDIO_HOST->CMD_BUF[i*4 + 2] << 16) | (SDIO_HOST->CMD_BUF[i*4 + 1] << 8) | (SDIO_HOST->CMD_BUF[i*4]);
  46. }
  47. }
  48. static int sm_sdh_wait_interrupt(uint8_t srcbit, int timeout)
  49. {
  50. int ret = 0;
  51. unsigned int tmp = (1 << srcbit);
  52. volatile int vtimeout= timeout;
  53. if(vtimeout == -1) {
  54. vtimeout = 0x7FFFF;
  55. }
  56. while(1)
  57. {
  58. if(SDIO_HOST->MMC_INT_SRC & tmp)
  59. {
  60. SDIO_HOST->MMC_INT_SRC |= tmp;
  61. if(SDIO_HOST->MMC_INT_SRC)
  62. {
  63. TEST_DEBUG("Err Int 0x%x\n", SDIO_HOST->MMC_INT_SRC);
  64. }
  65. break;
  66. }
  67. vtimeout--;
  68. if((vtimeout == 0) || (vtimeout < 0))
  69. {
  70. ret = 1; //timeout, err
  71. break;
  72. }
  73. delay_cnt(1);
  74. }
  75. return ret;
  76. }
  77. int wm_sdh_config(void)
  78. {
  79. tls_sys_clk sysclk;
  80. tls_sys_clk_get(&sysclk);
  81. SDIO_HOST->MMC_CARDSEL = 0xC0 | (sysclk.cpuclk / 2 - 1);//0xd3; //enable module, enable mmcclk
  82. SDIO_HOST->MMC_CTL = 0xD3;//0xC3; //4bits, low speed, 1/4 divider, auto transfer, mmc mode.
  83. SDIO_HOST->MMC_INT_MASK = 0x100; //unmask sdio data interrupt.
  84. SDIO_HOST->MMC_CRCCTL = 0xC0; //
  85. SDIO_HOST->MMC_TIMEOUTCNT = 0xff;
  86. return 0;
  87. }
  88. int wm_sd_card_initialize(uint32_t *rca)
  89. {
  90. int ret = -1;
  91. uint32_t respCmd[4];
  92. int recnt = 5;
  93. wm_sdh_config();
  94. //======================================================
  95. // set up
  96. // Test: Init sequence, With response check
  97. // CMD 0 Reset Card
  98. // CMD 8 Get voltage (Only 2.0 Card response to this)
  99. // CMD55 Indicate Next Command are Application specific
  100. // ACMD41 Get Voltage windows
  101. // CMD 2 CID reg
  102. // CMD 3 Get RCA.
  103. //======================================================
  104. begin:
  105. wm_sdh_send_cmd(0, 0, 0x04); //Send CMD0
  106. sm_sdh_wait_interrupt(0, -1);
  107. delay_cnt(1000);
  108. wm_sdh_send_cmd(8, 0x1AA, 0x44); //Send CMD8
  109. sm_sdh_wait_interrupt(0, -1);
  110. wm_sdh_get_response(respCmd, 2);
  111. sh_dumpBuffer("CMD8 respCmd", (char *)respCmd, 5);
  112. if(respCmd[0] != 0x1AA || (respCmd[1] & 0xFF) != 8)
  113. {
  114. TEST_DEBUG("CMD8 Error\n");
  115. if(recnt--)
  116. goto begin;
  117. goto end;
  118. }
  119. while(1)
  120. {
  121. wm_sdh_send_cmd(55, 0, 0x44); //Send CMD55
  122. sm_sdh_wait_interrupt(0, -1);
  123. wm_sdh_get_response(respCmd, 2);
  124. sh_dumpBuffer("CMD55 respCmd", (char *)respCmd, 5);
  125. if((respCmd[1] & 0xFF) != 55)
  126. goto end;
  127. wm_sdh_send_cmd(41, 0xC0100000, 0x44); //Send ACMD41
  128. sm_sdh_wait_interrupt(0, -1);
  129. sm_sdh_wait_interrupt(3, 1000); //由于sd规范中,Acmd41返回的crc永远是11111,也就是应该忽略crc;这里的crc错误应该忽略。
  130. wm_sdh_get_response(respCmd, 2);
  131. sh_dumpBuffer("ACMD41 respCmd", (char *)respCmd, 5);
  132. if((respCmd[1] & 0xFF) != 0x3F) //sd规范定义固定为0x3F,所以导致crc错误
  133. goto end;
  134. if(respCmd[0] >> 31 & 0x1)
  135. {
  136. TEST_DEBUG("card is ready\n");
  137. break;
  138. }
  139. }
  140. wm_sdh_send_cmd(2, 0, 0x54); //Send CMD2
  141. sm_sdh_wait_interrupt(0, -1);
  142. sm_sdh_wait_interrupt(3, 1000);
  143. wm_sdh_get_response(respCmd, 4);
  144. sh_dumpBuffer("CMD2 respCmd", (char *)respCmd, 16);
  145. if((respCmd[3] >> 24 & 0xFF) != 0x3F) //sd规范定义固定为0x3F,所以导致crc错误
  146. goto end;
  147. wm_sdh_send_cmd(3, 0, 0x44); //Send CMD3
  148. sm_sdh_wait_interrupt(0, -1);
  149. wm_sdh_get_response(respCmd, 2);
  150. sh_dumpBuffer("CMD3 respCmd", (char *)respCmd, 5);
  151. if((respCmd[1] & 0xFF) != 3)
  152. goto end;
  153. *rca = respCmd[0] >> 16;
  154. TEST_DEBUG("RCA = %x\n", *rca);
  155. ret = 0;
  156. end:
  157. return ret;
  158. }
  159. static uint32_t SD_GetCapacity(uint8_t *csd, SD_CardInfo_t *SDCardInfo)
  160. {
  161. uint32_t Capacity;
  162. uint16_t n;
  163. uint32_t csize;
  164. if((csd[0]&0xC0)==0x40)//判断bit126是否为1
  165. {
  166. csize = csd[9] + ((uint32_t)csd[8] << 8) + ((uint32_t)(csd[7] & 63) << 16) + 1;
  167. Capacity = csize << 9;
  168. SDCardInfo->CardCapacity = (long long)Capacity*1024;
  169. SDCardInfo->CardBlockSize = 512;
  170. }
  171. else
  172. {
  173. n = (csd[5] & 0x0F) + ((csd[10] & 0x80) >> 7) + ((csd[9] & 0x03) << 1) + 2;
  174. csize = (csd[8] >> 6) + ((uint16_t)csd[7] << 2) + ((uint16_t)(csd[6] & 0x03) << 10) + 1;
  175. Capacity = (uint32_t)csize << (n - 10);
  176. SDCardInfo->CardCapacity = (long long)Capacity*1024;
  177. SDCardInfo->CardBlockSize = 1<<(csd[5] & 0x0F);
  178. }
  179. return Capacity;
  180. }
  181. int wm_sd_card_query_csd(uint32_t rca)
  182. {
  183. int ret = -1, i;
  184. uint32_t respCmd[4];
  185. char adjustResp[16];
  186. wm_sdh_send_cmd(9, rca<<16, 0x54); //Send CMD9
  187. sm_sdh_wait_interrupt(0, -1);
  188. sm_sdh_wait_interrupt(3, 1000);
  189. wm_sdh_get_response(respCmd, 4);
  190. for(i=0; i<16; i++) adjustResp[15-i] = SDIO_HOST->CMD_BUF[i];
  191. SD_GetCapacity((uint8_t*)&adjustResp[1], &SDCardInfo);
  192. sh_dumpBuffer("CMD9 respCmd", adjustResp, 16);
  193. if((respCmd[3] >> 24 & 0xFF) != 0x3F) //sd规范定义固定为0x3F,所以导致crc错误
  194. goto end;
  195. ret = 0;
  196. end:
  197. return ret;
  198. }
  199. int wm_sd_card_set_blocklen(uint32_t blocklen)
  200. {
  201. int ret = -1;
  202. uint32_t respCmd[2];
  203. wm_sdh_send_cmd(16, blocklen, 0x44); //Send CMD16
  204. sm_sdh_wait_interrupt(0, -1);
  205. wm_sdh_get_response(respCmd, 2);
  206. sh_dumpBuffer("CMD16 respCmd", (char *)respCmd, 5);
  207. if((respCmd[1] & 0xFF) != 16)
  208. goto end;
  209. ret = 0;
  210. end:
  211. return ret;
  212. }
  213. int wm_sd_card_select(uint32_t rca)
  214. {
  215. int ret = -1;
  216. uint32_t respCmd[2];
  217. wm_sdh_send_cmd(7, rca<<16, 0x44); //Send CMD7
  218. sm_sdh_wait_interrupt(0, -1);
  219. wm_sdh_get_response(respCmd, 2);
  220. sh_dumpBuffer("CMD7 respCmd", (char *)respCmd, 5);
  221. if((respCmd[1] & 0xFF) != 7)
  222. goto end;
  223. ret = 0;
  224. end:
  225. return ret;
  226. }
  227. void wm_sd_card_deselect()
  228. {
  229. wm_sdh_send_cmd(7, 0, 0x04); //Send CMD7
  230. sm_sdh_wait_interrupt(0, -1);
  231. }
  232. int wm_sd_card_query_status(uint32_t rca, uint32_t *respCmd0)
  233. {
  234. int ret = -1;
  235. #if TEST_DEBUG_EN
  236. uint8_t current_state = 0;
  237. uint8_t error_state = 0;
  238. #endif
  239. uint32_t respCmd[2];
  240. wm_sdh_send_cmd(13, rca<<16, 0x44); //Send CMD13
  241. sm_sdh_wait_interrupt(0, -1);
  242. wm_sdh_get_response(respCmd, 2);
  243. sh_dumpBuffer("CMD13 respCmd", (char *)respCmd, 5);
  244. if((respCmd[1] & 0xFF) != 13)
  245. goto end;
  246. if(respCmd0)
  247. {
  248. *respCmd0 = respCmd[0];
  249. }
  250. #if TEST_DEBUG_EN
  251. current_state = respCmd[0] >> 9 & 0xF;
  252. error_state = respCmd[0] >> 19 & 0x1;
  253. TEST_DEBUG("current_state %d, error_state %d\n",current_state,error_state);
  254. #endif
  255. ret = 0;
  256. end:
  257. return ret;
  258. }
  259. /*
  260. * speed_mode: 0: low speed; 1: high speed;
  261. * */
  262. int wm_sd_card_switch_func(uint8_t speed_mode)
  263. {
  264. int ret = -1;
  265. int i;
  266. uint32_t respCmd[2];
  267. wm_sdh_send_cmd(6, 0x00fffff1, 0x44); //Send CMD6
  268. sm_sdh_wait_interrupt(0, -1);
  269. wm_sdh_get_response(respCmd, 2);
  270. sh_dumpBuffer("CMD6 respCmd", (char *)respCmd, 5);
  271. if((respCmd[1] & 0xFF) != 6)
  272. goto end;
  273. SDIO_HOST->BUF_CTL = 0x4020; //disable dma, read sd card
  274. SDIO_HOST->MMC_INT_SRC |= 0x7ff; // clear all firstly
  275. SDIO_HOST->MMC_IO = 0x3; //!!!read data, auto transfer
  276. sm_sdh_wait_interrupt(1, -1);
  277. TEST_DEBUG("read complete\n");
  278. for(i = 0; i < 128; i++)
  279. {
  280. respCmd[0] = SDIO_HOST->DATA_BUF[0];
  281. if(i == 4)
  282. {
  283. respCmd[1] = respCmd[0];
  284. }
  285. printf("0x%x, ", respCmd[0]);
  286. if(i % 4 == 3)
  287. {
  288. printf("\n");
  289. }
  290. }
  291. TEST_DEBUG("the value of byte 17~20 is 0x%x\n", respCmd[1]);
  292. if(respCmd[1] & 0xF) //support high speed
  293. {
  294. wm_sdh_send_cmd(6, 0x80fffff0 | speed_mode, 0x44); //Send CMD6
  295. sm_sdh_wait_interrupt(0, -1);
  296. wm_sdh_get_response(respCmd, 2);
  297. sh_dumpBuffer("CMD6 respCmd", (char *)respCmd, 5);
  298. if((respCmd[1] & 0xFF) != 6)
  299. goto end;
  300. SDIO_HOST->BUF_CTL = 0x4020; //disable dma, read sd card
  301. SDIO_HOST->MMC_INT_SRC |= 0x7ff; // clear all firstly
  302. SDIO_HOST->MMC_IO = 0x3; //!!!read data, auto transfer
  303. sm_sdh_wait_interrupt(1, -1);
  304. TEST_DEBUG("read complete\n");
  305. for(i = 0; i < 128; i++)
  306. {
  307. respCmd[0] = SDIO_HOST->DATA_BUF[0];
  308. if(i == 4)
  309. {
  310. respCmd[1] = respCmd[0];
  311. }
  312. printf("0x%x, ", respCmd[0]);
  313. if(i % 4 == 3)
  314. {
  315. printf("\n");
  316. }
  317. }
  318. TEST_DEBUG("the value of byte 17~20 is 0x%x\n", respCmd[1]);
  319. if((respCmd[1] & 0xF) == speed_mode)
  320. {
  321. if(speed_mode == 1)
  322. {
  323. SDIO_HOST->MMC_CTL |= (1 << 6);
  324. }
  325. else
  326. {
  327. SDIO_HOST->MMC_CTL &= ~(1 << 6);
  328. }
  329. TEST_DEBUG("switch speed_mode %d success\n", speed_mode);
  330. }
  331. }
  332. ret = 0;
  333. end:
  334. return ret;
  335. }
  336. /*
  337. * bus_width: 0:1bit; 2:4bits
  338. * */
  339. int wm_sd_card_set_bus_width(uint32_t rca, uint8_t bus_width)
  340. {
  341. int ret = -1;
  342. uint32_t respCmd[2];
  343. if(bus_width != 0 && bus_width != 2)
  344. {
  345. TEST_DEBUG("bus width parameter error\n");
  346. goto end;
  347. }
  348. if(bus_width == 2)
  349. {
  350. SDIO_HOST->MMC_CTL |= (1 << 7);
  351. }
  352. else
  353. {
  354. SDIO_HOST->MMC_CTL &= ~(1 << 7);
  355. }
  356. wm_sdh_send_cmd(55, rca<<16, 0x44); //Send CMD55
  357. sm_sdh_wait_interrupt(0, -1);
  358. wm_sdh_get_response(respCmd, 2);
  359. sh_dumpBuffer("CMD55 respCmd", (char *)respCmd, 5);
  360. if((respCmd[1] & 0xFF) != 55)
  361. goto end;
  362. wm_sdh_send_cmd(6, bus_width, 0x44); //Send ACMD6
  363. sm_sdh_wait_interrupt(0, -1);
  364. wm_sdh_get_response(respCmd, 2);
  365. sh_dumpBuffer("ACMD6 respCmd", (char *)respCmd, 5);
  366. if((respCmd[1] & 0xFF) != 6)
  367. goto end;
  368. ret = 0;
  369. end:
  370. return ret;
  371. }
  372. int wm_sd_card_stop_trans(void)
  373. {
  374. int ret = -1;
  375. uint32_t respCmd[2];
  376. wm_sdh_send_cmd(12, 0, 0x44); //Send CMD12
  377. ret = sm_sdh_wait_interrupt(0, -1);
  378. if(ret)
  379. goto end;
  380. wm_sdh_get_response(respCmd, 2);
  381. sh_dumpBuffer("CMD12 respCmd", (char *)respCmd, 5);
  382. if((respCmd[1] & 0xFF) != 12)
  383. goto end;
  384. ret = 0;
  385. end:
  386. return ret;
  387. }
  388. static void sdio_host_reset(void)
  389. {
  390. tls_bitband_write(HR_CLK_RST_CTL, 27, 0);
  391. tls_bitband_write(HR_CLK_RST_CTL, 27, 1);
  392. while(tls_bitband_read(HR_CLK_RST_CTL, 27) == 0);
  393. }
  394. int sdh_card_init(uint32_t *rca_ref)
  395. {
  396. int ret = -1;
  397. uint32_t rca = 0;
  398. sdio_host_reset();
  399. ret = wm_sd_card_initialize(&rca);
  400. if(ret)
  401. goto end;
  402. ret = wm_sd_card_query_csd(rca);
  403. if(ret)
  404. goto end;
  405. ret = wm_sd_card_query_status(rca, NULL);
  406. if(ret)
  407. goto end;
  408. ret = wm_sd_card_select(rca);
  409. if(ret)
  410. goto end;
  411. ret = wm_sd_card_query_status(rca, NULL);
  412. if(ret)
  413. goto end;
  414. *rca_ref = rca;
  415. ret = 0;
  416. end:
  417. TEST_DEBUG("ret %d\n", ret);
  418. return ret;
  419. }
  420. /*buf's len must >= 512*/
  421. int wm_sd_card_block_read(uint32_t rca, uint32_t sd_addr, char *buf)
  422. {
  423. int ret = -1;
  424. int i;
  425. uint32_t respCmd[2];
  426. wm_sdh_send_cmd(17, sd_addr, 0x44); //Send CMD17
  427. sm_sdh_wait_interrupt(0, -1);
  428. wm_sdh_get_response(respCmd, 2);
  429. sh_dumpBuffer("CMD17 respCmd", (char *)respCmd, 5);
  430. if((respCmd[1] & 0xFF) != 17)
  431. goto end;
  432. SDIO_HOST->BUF_CTL = 0x4020; //disable dma, read sd card
  433. SDIO_HOST->MMC_INT_SRC |= 0x7ff; // clear all firstly
  434. SDIO_HOST->MMC_IO = 0x3; //!!!read data, auto transfer
  435. sm_sdh_wait_interrupt(1, -1);
  436. TEST_DEBUG("read complete\n");
  437. for(i = 0; i < 128; i++)
  438. {
  439. *((uint32_t*)(buf + 4*i)) = SDIO_HOST->DATA_BUF[0];
  440. }
  441. ret = 0;
  442. end:
  443. return ret;
  444. }
  445. /*buf's len must be 512*/
  446. int wm_sd_card_block_write(uint32_t rca, uint32_t sd_addr, char *buf)
  447. {
  448. int i;
  449. int ret = -1;
  450. uint32_t respCmd[2];
  451. uint8_t current_state = 0;
  452. uint8_t error_state = 0;
  453. wm_sdh_send_cmd(24, sd_addr, 0x44); //Send CMD24
  454. sm_sdh_wait_interrupt(0, -1);
  455. wm_sdh_get_response(respCmd, 2);
  456. sh_dumpBuffer("CMD24 respCmd", (char *)respCmd, 5);
  457. if((respCmd[1] & 0xFF) != 24)
  458. goto end;
  459. SDIO_HOST->BUF_CTL = 0x4820; //disable dma, write sd card
  460. for(i = 0; i < 128; i++)
  461. {
  462. SDIO_HOST->DATA_BUF[i] = *((uint32*)(buf + 4*i));
  463. }
  464. SDIO_HOST->MMC_BYTECNTL = 512;
  465. SDIO_HOST->MMC_INT_SRC |= 0x7ff; // clear all firstly
  466. SDIO_HOST->MMC_IO = 0x1; //!!!write data, auto transfer
  467. sm_sdh_wait_interrupt(1, -1);
  468. while(true)
  469. {
  470. ret = wm_sd_card_query_status(rca, &respCmd[0]);
  471. if(ret)
  472. goto end;
  473. current_state = respCmd[0] >> 9 & 0xF;
  474. error_state = respCmd[0] >> 19 & 0x1;
  475. if(current_state == 4) //tran
  476. {
  477. break;
  478. }
  479. if(error_state)
  480. {
  481. ret = -1;
  482. goto end;
  483. }
  484. }
  485. ret = 0;
  486. end:
  487. TEST_DEBUG("write complete err %d\n", ret);
  488. return ret;
  489. }
  490. /* dir: 1, write; 0, read*/
  491. int wm_sd_card_dma_config(u32*mbuf,u32 bufsize,u8 dir)
  492. {
  493. int ch;
  494. u32 addr_inc = 0;
  495. ch = tls_dma_request(0, NULL);
  496. DMA_CHNLCTRL_REG(ch) = DMA_CHNL_CTRL_CHNL_OFF;
  497. if(dir)
  498. {
  499. DMA_SRCADDR_REG(ch) = (unsigned int)mbuf;
  500. DMA_DESTADDR_REG(ch) = (unsigned int)SDIO_HOST->DATA_BUF;
  501. addr_inc = (1 << 1);
  502. }
  503. else
  504. {
  505. DMA_SRCADDR_REG(ch) = (unsigned int)SDIO_HOST->DATA_BUF;
  506. DMA_DESTADDR_REG(ch) = (unsigned int)mbuf;
  507. addr_inc = (1 << 3);
  508. }
  509. DMA_CTRL_REG(ch) = addr_inc | (2 << 5) | (bufsize << 8);
  510. DMA_MODE_REG(ch) = DMA_MODE_SEL_SDIOHOST | DMA_MODE_HARD_MODE;
  511. DMA_CHNLCTRL_REG(ch) = DMA_CHNL_CTRL_CHNL_ON;
  512. return ch;
  513. }
  514. static int wm_sdh_wait_blocks_done(void)
  515. {
  516. int ret = 0;
  517. uint32_t timeout = 0x7FFFF * 8;
  518. while(1)
  519. {
  520. if((SDIO_HOST->MMC_IO_MBCTL & 0x01) == 0x00)
  521. {
  522. break;
  523. }
  524. if(timeout == 0)
  525. {
  526. ret = -1;
  527. tls_os_time_delay(HZ);
  528. }
  529. else
  530. {
  531. delay_cnt(1);
  532. timeout--;
  533. }
  534. }
  535. return ret;
  536. }
  537. /*read blocks by dma
  538. * buflen must be integer multiple of 512
  539. * */
  540. int wm_sd_card_blocks_read(uint32_t rca, uint32_t sd_addr, char *buf, uint32_t buflen)
  541. {
  542. int ret = -1, dma_channel = 0xFF, retresp = -100;
  543. uint32_t respCmd[2];
  544. int block_cnt = buflen/512;
  545. uint8_t current_state = 0;
  546. uint8_t error_state = 0;
  547. wm_sdh_send_cmd(18, sd_addr, 0x44); //Send CMD18
  548. sm_sdh_wait_interrupt(0, -1);
  549. wm_sdh_get_response(respCmd, 2);
  550. sh_dumpBuffer("CMD18 respCmd", (char *)respCmd, 5);
  551. if((respCmd[1] & 0xFF) != 18)
  552. goto end;
  553. SDIO_HOST->BUF_CTL = 0x4000; //disable dma,
  554. dma_channel = wm_sd_card_dma_config((u32*)buf, 512*block_cnt, 0);
  555. SDIO_HOST->BUF_CTL = 0x404; //enable dma, read sd card
  556. SDIO_HOST->MMC_BLOCKCNT = block_cnt;
  557. SDIO_HOST->MMC_INT_SRC |= 0x7ff; // clear all firstly
  558. SDIO_HOST->MMC_IO_MBCTL = 0xa3; //read data, enable multi blocks data transfer
  559. // sm_sdh_wait_interrupt(1, -1);
  560. ret = wm_sdh_wait_blocks_done();
  561. if(ret)
  562. {
  563. TEST_DEBUG("wm_sd_card_blocks_read: timeout error\n");
  564. goto end;
  565. }
  566. TEST_DEBUG("read complete\n");
  567. ret = wm_sd_card_stop_trans();
  568. if(ret)
  569. goto end;
  570. /*waiting for card to trans state*/
  571. do
  572. {
  573. ret = wm_sd_card_query_status(rca, &respCmd[0]);
  574. if(ret)
  575. break;
  576. current_state = respCmd[0] >> 9 & 0xF;
  577. error_state = respCmd[0] >> 19 & 0x1;
  578. if(error_state)
  579. {
  580. ret = -1;
  581. break;
  582. }
  583. }while(current_state != 4);
  584. if (ret)
  585. {
  586. TEST_DEBUG("mr blocks:%x\r\n", error_state);
  587. goto end;
  588. }
  589. ret = 0;
  590. end:
  591. if (ret)
  592. {
  593. wm_sd_card_stop_trans();
  594. do
  595. {
  596. retresp = wm_sd_card_query_status(rca, &respCmd[0]);
  597. if(retresp)
  598. break;
  599. current_state = respCmd[0] >> 9 & 0xF;
  600. error_state = respCmd[0] >> 19 & 0x1;
  601. if(error_state)
  602. {
  603. ret = -1;
  604. break;
  605. }
  606. }while(current_state != 4);
  607. }
  608. tls_dma_free(dma_channel);
  609. return ret;
  610. }
  611. /*write blocks by dma
  612. * buflen must be integer multiple of 512
  613. * */
  614. int wm_sd_card_blocks_write(uint32_t rca, uint32_t sd_addr, char *buf, uint32_t buflen)
  615. {
  616. int dma_channel = 0xFF;
  617. int ret = -1, retresp = -100;
  618. uint32_t respCmd[2];
  619. int block_cnt = buflen/512;
  620. uint8_t current_state = 0;
  621. uint8_t error_state = 0;
  622. wm_sdh_send_cmd(25, sd_addr, 0x44); //Send CMD25
  623. ret = sm_sdh_wait_interrupt(0, -1);
  624. if(ret)
  625. goto end;
  626. wm_sdh_get_response(respCmd, 2);
  627. sh_dumpBuffer("CMD25 respCmd", (char *)respCmd, 5);
  628. if((respCmd[1] & 0xFF) != 25) {
  629. ret = -1;
  630. goto end;
  631. }
  632. SDIO_HOST->BUF_CTL = 0x4000; //disable dma,
  633. dma_channel = wm_sd_card_dma_config((u32*)buf, 512*block_cnt, 1);
  634. SDIO_HOST->BUF_CTL = 0xC20; //enable dma, write sd card
  635. SDIO_HOST->MMC_BLOCKCNT = block_cnt;
  636. SDIO_HOST->MMC_INT_SRC |= 0x7ff; // clear all firstly
  637. SDIO_HOST->MMC_IO_MBCTL = 0xa1;////write data, enable multi blocks data transfer
  638. #if 0
  639. ret = sm_sdh_wait_interrupt(1, -1);
  640. if(ret)
  641. goto end;
  642. #endif
  643. ret = wm_sdh_wait_blocks_done();
  644. if(ret)
  645. goto end;
  646. ret = wm_sd_card_stop_trans();
  647. if(ret)
  648. goto end;
  649. /*waiting for card to trans state*/
  650. do
  651. {
  652. ret = wm_sd_card_query_status(rca, &respCmd[0]);
  653. if(ret)
  654. break;
  655. current_state = respCmd[0] >> 9 & 0xF;
  656. error_state = respCmd[0] >> 19 & 0x1;
  657. if(error_state)
  658. {
  659. ret = -1;
  660. break;
  661. }
  662. }while(current_state != 4);
  663. if (ret)
  664. {
  665. TEST_DEBUG("mw blocks:%x\r\n", error_state);
  666. goto end;
  667. }
  668. TEST_DEBUG("write complete\n");
  669. ret = 0;
  670. end:
  671. if (ret)
  672. {
  673. wm_sd_card_stop_trans();
  674. do
  675. {
  676. retresp = wm_sd_card_query_status(rca, &respCmd[0]);
  677. if(retresp)
  678. break;
  679. current_state = respCmd[0] >> 9 & 0xF;
  680. error_state = respCmd[0] >> 19 & 0x1;
  681. if(error_state)
  682. {
  683. ret = -1;
  684. break;
  685. }
  686. }while(current_state != 4);
  687. }
  688. tls_dma_free(dma_channel);
  689. return ret;
  690. }