sdcard.c 13 KB

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