sdcard.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. /*
  2. * File : sd.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006, 2007, RT-Thread Develop Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://openlab.rt-thread.com/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2007-12-02 Yi.Qiu the first version
  13. */
  14. #include "sdcard.h"
  15. extern rt_uint32_t PCLK;
  16. volatile rt_uint32_t rd_cnt;
  17. volatile rt_uint32_t wt_cnt;
  18. volatile rt_int32_t RCA;
  19. /**
  20. * This function will set a hook function, which will be invoked when a memory
  21. * block is allocated from heap memory.
  22. *
  23. * @param hook the hook function
  24. */
  25. static void sd_delay(rt_uint32_t ms)
  26. {
  27. ms *= 7326;
  28. while(--ms);
  29. }
  30. /**
  31. * This function will set a hook function, which will be invoked when a memory
  32. * block is allocated from heap memory.
  33. *
  34. * @param hook the hook function
  35. */
  36. static int sd_cmd_end(int cmd, int be_resp)
  37. {
  38. int finish0;
  39. if(!be_resp)
  40. {
  41. finish0=SDICSTA;
  42. while((finish0&0x800)!=0x800)
  43. finish0=SDICSTA;
  44. SDICSTA=finish0;
  45. return RT_EOK;
  46. }
  47. else
  48. {
  49. finish0=SDICSTA;
  50. while( !( ((finish0&0x200)==0x200) | ((finish0&0x400)==0x400) ))
  51. finish0=SDICSTA;
  52. if(cmd==1 || cmd==41)
  53. {
  54. if( (finish0&0xf00) != 0xa00 )
  55. {
  56. SDICSTA=finish0;
  57. if(((finish0&0x400)==0x400))
  58. return RT_ERROR;
  59. }
  60. SDICSTA=finish0;
  61. }
  62. else
  63. {
  64. if( (finish0&0x1f00) != 0xa00 )
  65. {
  66. rt_kprintf("CMD%d:SDICSTA=0x%x, SDIRSP0=0x%x\n",
  67. cmd, SDICSTA, SDIRSP0);
  68. SDICSTA=finish0;
  69. if(((finish0&0x400)==0x400))
  70. return RT_ERROR;
  71. }
  72. SDICSTA=finish0;
  73. }
  74. return RT_EOK;
  75. }
  76. }
  77. /**
  78. * This function will set a hook function, which will be invoked when a memory
  79. * block is allocated from heap memory.
  80. *
  81. * @param hook the hook function
  82. */
  83. static int sd_data_end(void)
  84. {
  85. int finish;
  86. finish=SDIDSTA;
  87. while( !( ((finish&0x10)==0x10) | ((finish&0x20)==0x20) ))
  88. finish=SDIDSTA;
  89. if( (finish&0xfc) != 0x10 )
  90. {
  91. SDIDSTA=0xec;
  92. return RT_ERROR;
  93. }
  94. return RT_EOK;
  95. }
  96. /**
  97. * This function will set a hook function, which will be invoked when a memory
  98. * block is allocated from heap memory.
  99. *
  100. * @param hook the hook function
  101. */
  102. static void sd_cmd0(void)
  103. {
  104. SDICARG=0x0;
  105. SDICCON=(1<<8)|0x40;
  106. sd_cmd_end(0, 0);
  107. }
  108. /**
  109. * This function will set a hook function, which will be invoked when a memory
  110. * block is allocated from heap memory.
  111. *
  112. * @param hook the hook function
  113. */
  114. static int sd_cmd55(void)
  115. {
  116. SDICARG = RCA << 16;
  117. SDICCON = (0x1 << 9) | (0x1 << 8) | 0x77;
  118. if(sd_cmd_end(55, 1) == RT_ERROR)
  119. return RT_ERROR;
  120. return RT_EOK;
  121. }
  122. /**
  123. * This function will set a hook function, which will be invoked when a memory
  124. * block is allocated from heap memory.
  125. *
  126. * @param hook the hook function
  127. */
  128. static void sd_sel_desel(char sel_desel)
  129. {
  130. if(sel_desel)
  131. {
  132. RECMDS7:
  133. SDICARG =RCA << 16;
  134. SDICCON = (0x1 << 9) | (0x1 << 8) | 0x47;
  135. if(sd_cmd_end(7, 1) == RT_ERROR)
  136. goto RECMDS7;
  137. if( SDIRSP0 & (0x1e00 != 0x800))
  138. goto RECMDS7;
  139. }
  140. else
  141. {
  142. RECMDD7:
  143. SDICARG=0<<16;
  144. SDICCON=(0x1<<8)|0x47;
  145. if(sd_cmd_end(7, 0) == RT_ERROR)
  146. goto RECMDD7;
  147. }
  148. }
  149. /**
  150. * This function will set a hook function, which will be invoked when a memory
  151. * block is allocated from heap memory.
  152. *
  153. * @param hook the hook function
  154. */
  155. static void sd_setbus(void)
  156. {
  157. SET_BUS:
  158. sd_cmd55();
  159. SDICARG=1<<1;
  160. SDICCON=(0x1<<9)|(0x1<<8)|0x46;
  161. if(sd_cmd_end(6, 1) == RT_ERROR)
  162. goto SET_BUS;
  163. }
  164. /**
  165. * This function will set a hook function, which will be invoked when a memory
  166. * block is allocated from heap memory.
  167. *
  168. * @param hook the hook function
  169. */
  170. int sd_mmc_ocr(void)
  171. {
  172. int i;
  173. /* Negotiate operating condition for MMC, it makes card ready state */
  174. for(i=0; i<100; i++)
  175. {
  176. SDICARG = 0xff8000;
  177. SDICCON = (0x1<<9)|(0x1<<8)|0x41;
  178. /* Check end of CMD1 */
  179. if((sd_cmd_end(1, 1) == RT_EOK) && (SDIRSP0>>16)==0x80ff)
  180. {
  181. SDICSTA=0xa00;
  182. return RT_EOK;
  183. }
  184. }
  185. SDICSTA=0xa00;
  186. return RT_ERROR;
  187. }
  188. /**
  189. * This function will set a hook function, which will be invoked when a memory
  190. * block is allocated from heap memory.
  191. *
  192. * @param hook the hook function
  193. */
  194. int sd_ocr(void)
  195. {
  196. int i;
  197. /* Negotiate operating condition for SD, it makes card ready state */
  198. for(i=0;i<50;i++)
  199. {
  200. sd_cmd55();
  201. SDICARG=0xff8000;
  202. SDICCON=(0x1<<9)|(0x1<<8)|0x69;
  203. /* if using real board, should replace code here. need to modify qemu in near future*/
  204. /* Check end of ACMD41 */
  205. //if( rt_hw_sd_cmd_end(41, 1) && SDIRSP0==0x80ff8000 )
  206. if( sd_cmd_end(41, 1) == RT_EOK)
  207. {
  208. SDICSTA=0xa00;
  209. return RT_EOK;
  210. }
  211. sd_delay(200);
  212. }
  213. SDICSTA=0xa00;
  214. return RT_ERROR;
  215. }
  216. /**
  217. * This function will set a hook function, which will be invoked when a memory
  218. * block is allocated from heap memory.
  219. *
  220. * @param hook the hook function
  221. */
  222. rt_uint8_t sd_init(void)
  223. {
  224. //-- SD controller & card initialize
  225. int i;
  226. /* Important notice for MMC test condition */
  227. /* Cmd & Data lines must be enabled by pull up resister */
  228. SDIPRE=PCLK/(INICLK)-1;
  229. SDICON=1;
  230. SDIFSTA=SDIFSTA|(1<<16);
  231. SDIBSIZE = 0x200;
  232. SDIDTIMER=0x7fffff;
  233. /* Wait 74SDCLK for MMC card */
  234. for(i=0; i<0x1000; i++);
  235. sd_cmd0();
  236. /* Check MMC card OCR */
  237. if(sd_mmc_ocr() == RT_EOK)
  238. {
  239. rt_kprintf("In MMC ready\n");
  240. goto RECMD2;
  241. }
  242. rt_kprintf("MMC check end!!\n");
  243. /* Check SD card OCR */
  244. if(sd_ocr() == RT_EOK)
  245. {
  246. rt_kprintf("In SD ready\n");
  247. }
  248. else
  249. {
  250. rt_kprintf("Initialize fail\nNo Card assertion\n");
  251. return RT_ERROR;
  252. }
  253. RECMD2:
  254. SDICARG=0x0;
  255. SDICCON=(0x1<<10)|(0x1<<9)|(0x1<<8)|0x42;
  256. if(sd_cmd_end(2, 1) == RT_ERROR)
  257. goto RECMD2;
  258. RECMD3:
  259. SDICARG=0<<16;
  260. SDICCON=(0x1<<9)|(0x1<<8)|0x43;
  261. if(sd_cmd_end(3, 1) == RT_ERROR)
  262. goto RECMD3;
  263. RCA=(SDIRSP0 & 0xffff0000 )>>16;
  264. SDIPRE=(PCLK/(SDCLK*2))-1;
  265. if( SDIRSP0 & (0x1e00!=0x600) )
  266. goto RECMD3;
  267. sd_sel_desel(1);
  268. sd_delay(200);
  269. sd_setbus();
  270. return RT_EOK;
  271. }
  272. /**
  273. * This function will set a hook function, which will be invoked when a memory
  274. * block is allocated from heap memory.
  275. *
  276. * @param hook the hook function
  277. */
  278. rt_uint8_t sd_readblock(rt_uint32_t address, rt_uint8_t* buf)
  279. {
  280. int status;
  281. rd_cnt=0;
  282. // SDICON |= (1<<1);
  283. SDIDCON = (2 << 22) | (1 << 19) | (1 << 17) | (1 << 16) | (1 << 14) | (2 << 12) | (1 << 0);
  284. SDICARG = address;
  285. RERDCMD:
  286. SDICCON = (0x1 << 9 ) | (0x1 << 8) | 0x51;
  287. if(sd_cmd_end(17, 1) == RT_ERROR)
  288. goto RERDCMD;
  289. SDICSTA = 0xa00;
  290. while(rd_cnt < 128)
  291. {
  292. if((SDIDSTA & 0x20) == 0x20)
  293. {
  294. SDIDSTA = 0x1 << 0x5;
  295. break;
  296. }
  297. status = SDIFSTA;
  298. if((status & 0x1000) == 0x1000)
  299. {
  300. #if 0
  301. register rt_uint32_t value;
  302. value = SDIDAT;
  303. /* swap 4 bytes */
  304. buf[0] = (value >> 24) & 0xff;
  305. buf[1] = (value >> 16) & 0xff;
  306. buf[2] = (value >> 8) & 0xff;
  307. buf[3] = value & 0xff;
  308. #endif
  309. *(rt_uint32_t *)buf = SDIDAT;
  310. rd_cnt++;
  311. buf += 4;
  312. }
  313. }
  314. if(sd_data_end() == RT_ERROR)
  315. {
  316. rt_kprintf("Dat error\n");
  317. return RT_ERROR;
  318. }
  319. SDIDSTA = 0x10;
  320. SDIDCON = SDIDCON &~ (7<<12);
  321. SDIFSTA = SDIFSTA & 0x200;
  322. SDIDSTA = 0x10;
  323. return RT_EOK;
  324. }
  325. /**
  326. * This function will set a hook function, which will be invoked when a memory
  327. * block is allocated from heap memory.
  328. *
  329. * @param hook the hook function
  330. */
  331. rt_uint8_t sd_writeblock(rt_uint32_t address, rt_uint8_t* buf)
  332. {
  333. int status;
  334. wt_cnt=0;
  335. SDIFSTA = SDIFSTA | (1 << 16);
  336. SDIDCON = (2 << 22) | (1 << 20) | (1 << 17) | (1 << 16) | (1 << 14) | (3 << 12) | (1 << 0);
  337. SDICARG = address;
  338. REWTCMD:
  339. SDICCON = (0x1 << 9) | (0x1 << 8) |0x58;
  340. if(sd_cmd_end(24, 1) == RT_ERROR)
  341. goto REWTCMD;
  342. SDICSTA=0xa00;
  343. while(wt_cnt < 128*1)
  344. {
  345. status = SDIFSTA;
  346. if((status & 0x2000) == 0x2000)
  347. {
  348. SDIDAT=*(rt_uint32_t*)buf;
  349. wt_cnt++;
  350. buf += 4;
  351. }
  352. }
  353. if(sd_data_end() == RT_ERROR)
  354. {
  355. rt_kprintf("Data Error\n");
  356. return RT_ERROR;
  357. }
  358. SDIDCON = SDIDCON &~ (7<<12);
  359. SDIDSTA = 0x10;
  360. return RT_EOK;
  361. }
  362. #ifdef RT_USING_DFS
  363. /* RT-Thread Device Driver Interface */
  364. #include <rtthread.h>
  365. #include <dfs_fs.h>
  366. struct rt_device sdcard_device[4];
  367. struct dfs_partition part[4];
  368. /**
  369. * This function will set a hook function, which will be invoked when a memory
  370. * block is allocated from heap memory.
  371. *
  372. * @param hook the hook function
  373. */
  374. static rt_err_t rt_sdcard_init(rt_device_t dev)
  375. {
  376. return 0;
  377. }
  378. /**
  379. * This function will set a hook function, which will be invoked when a memory
  380. * block is allocated from heap memory.
  381. *
  382. * @param hook the hook function
  383. */
  384. static rt_err_t rt_sdcard_open(rt_device_t dev, rt_uint16_t oflag)
  385. {
  386. return 0;
  387. }
  388. /**
  389. * This function will set a hook function, which will be invoked when a memory
  390. * block is allocated from heap memory.
  391. *
  392. * @param hook the hook function
  393. */
  394. static rt_err_t rt_sdcard_close(rt_device_t dev)
  395. {
  396. return 0;
  397. }
  398. /**
  399. * This function will set a hook function, which will be invoked when a memory
  400. * block is allocated from heap memory.
  401. *
  402. * @param hook the hook function
  403. */
  404. static rt_err_t rt_sdcard_control(rt_device_t dev, rt_uint8_t cmd, void *args)
  405. {
  406. return 0;
  407. }
  408. /**
  409. * This function will set a hook function, which will be invoked when a memory
  410. * block is allocated from heap memory.
  411. *
  412. * @param hook the hook function
  413. */
  414. static rt_size_t rt_sdcard_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  415. {
  416. int i;
  417. struct dfs_partition *part = (struct dfs_partition *)dev->private;
  418. if ( dev == RT_NULL )
  419. {
  420. rt_set_errno(-DFS_STATUS_EINVAL);
  421. return 0;
  422. }
  423. /* read all sectors */
  424. for (i = 0; i < size / SECTOR_SIZE; i ++)
  425. {
  426. rt_sem_take(part->lock, RT_WAITING_FOREVER);
  427. sd_readblock((part->offset + i)*SECTOR_SIZE + pos,
  428. (rt_uint8_t*)((rt_uint8_t*)buffer + i * SECTOR_SIZE));
  429. rt_sem_release(part->lock);
  430. }
  431. /* the length of reading must align to SECTOR SIZE */
  432. return size;
  433. }
  434. /**
  435. * This function will set a hook function, which will be invoked when a memory
  436. * block is allocated from heap memory.
  437. *
  438. * @param hook the hook function
  439. */
  440. static rt_size_t rt_sdcard_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  441. {
  442. int i;
  443. struct dfs_partition *part = (struct dfs_partition *)dev->private;
  444. if ( dev == RT_NULL )
  445. {
  446. rt_set_errno(-DFS_STATUS_EINVAL);
  447. return 0;
  448. }
  449. /* read all sectors */
  450. for (i = 0; i < size / SECTOR_SIZE; i++)
  451. {
  452. rt_sem_take(part->lock, RT_WAITING_FOREVER);
  453. sd_writeblock((part->offset + i)*SECTOR_SIZE + pos,
  454. (rt_uint8_t*)((rt_uint8_t*)buffer + i * SECTOR_SIZE));
  455. rt_sem_release(part->lock);
  456. }
  457. /* the length of reading must align to SECTOR SIZE */
  458. return size;
  459. }
  460. /**
  461. * This function will register sd card to device system
  462. *
  463. * @param hook the hook function
  464. */
  465. void rt_hw_sdcard_init()
  466. {
  467. rt_uint8_t i, status;
  468. rt_uint8_t *sector;
  469. char dname[4];
  470. char sname[8];
  471. if (sd_init() == RT_EOK)
  472. {
  473. /* get the first sector to read partition table */
  474. sector = (rt_uint8_t*) rt_malloc (512);
  475. if (sector == RT_NULL)
  476. {
  477. rt_kprintf("allocate partition sector buffer failed\n");
  478. return;
  479. }
  480. status = sd_readblock(0, sector);
  481. if (status == RT_EOK)
  482. {
  483. for(i=0; i<4; i++)
  484. {
  485. /* get the first partition */
  486. status = dfs_filesystem_get_partition(&part[i], sector, i);
  487. if (status == RT_EOK)
  488. {
  489. rt_snprintf(dname, 4, "sd%d", i);
  490. rt_snprintf(sname, 8, "sem_sd%d", i);
  491. part[i].lock = rt_sem_create(sname, 1, RT_IPC_FLAG_FIFO);
  492. /* register sdcard device */
  493. sdcard_device[i].init = rt_sdcard_init;
  494. sdcard_device[i].open = rt_sdcard_open;
  495. sdcard_device[i].close = rt_sdcard_close;
  496. sdcard_device[i].read = rt_sdcard_read;
  497. sdcard_device[i].write = rt_sdcard_write;
  498. sdcard_device[i].control = rt_sdcard_control;
  499. sdcard_device[i].private= &part[i];
  500. rt_device_register(&sdcard_device[i], dname,
  501. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE);
  502. }
  503. else
  504. {
  505. if(i == 0)
  506. {
  507. /* there is no partition table */
  508. part[0].offset = 0;
  509. part[0].size = 0;
  510. part[0].lock = rt_sem_create("sem_sd0", 1, RT_IPC_FLAG_FIFO);
  511. /* register sdcard device */
  512. sdcard_device[0].init = rt_sdcard_init;
  513. sdcard_device[0].open = rt_sdcard_open;
  514. sdcard_device[0].close = rt_sdcard_close;
  515. sdcard_device[0].read = rt_sdcard_read;
  516. sdcard_device[0].write = rt_sdcard_write;
  517. sdcard_device[0].control = rt_sdcard_control;
  518. sdcard_device[0].private= &part[0];
  519. rt_device_register(&sdcard_device[0], "sd0",
  520. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE);
  521. break;
  522. }
  523. }
  524. }
  525. }
  526. else
  527. {
  528. rt_kprintf("read sdcard first sector failed\n");
  529. }
  530. /* release sector buffer */
  531. rt_free(sector);
  532. return;
  533. }
  534. else
  535. {
  536. rt_kprintf("sdcard init failed\n");
  537. }
  538. }
  539. #endif