mmcsd_core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. /*
  2. * File : mmcsd_core.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006, RT-Thread Development 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. * 2011-07-25 weety first version
  13. */
  14. #include <rtthread.h>
  15. #include <drivers/mmcsd_core.h>
  16. #include <drivers/sd.h>
  17. #ifndef RT_MMCSD_STACK_SIZE
  18. #define RT_MMCSD_STACK_SIZE 1024
  19. #endif
  20. #ifndef RT_MMCSD_THREAD_PREORITY
  21. #if (RT_THREAD_PRIORITY_MAX == 32)
  22. #define RT_MMCSD_THREAD_PREORITY 0x16
  23. #else
  24. #define RT_MMCSD_THREAD_PREORITY 0x40
  25. #endif
  26. #endif
  27. //static struct rt_semaphore mmcsd_sem;
  28. static struct rt_thread mmcsd_detect_thread;
  29. static rt_uint8_t mmcsd_stack[RT_MMCSD_STACK_SIZE];
  30. static struct rt_mailbox mmcsd_detect_mb;
  31. static rt_uint32_t mmcsd_detect_mb_pool[4];
  32. void mmcsd_host_lock(struct rt_mmcsd_host *host)
  33. {
  34. rt_sem_take(&host->bus_lock, RT_WAITING_FOREVER);
  35. }
  36. void mmcsd_host_unlock(struct rt_mmcsd_host *host)
  37. {
  38. rt_sem_release(&host->bus_lock);
  39. }
  40. void mmcsd_req_complete(struct rt_mmcsd_host *host)
  41. {
  42. rt_sem_release(&host->sem_ack);
  43. }
  44. void mmcsd_send_request(struct rt_mmcsd_host *host, struct rt_mmcsd_req *req)
  45. {
  46. req->cmd->err = 0;
  47. req->cmd->mrq = req;
  48. if (req->data)
  49. {
  50. req->cmd->data = req->data;
  51. req->data->err = 0;
  52. req->data->mrq = req;
  53. if (req->stop)
  54. {
  55. req->data->stop = req->stop;
  56. req->stop->err = 0;
  57. req->stop->mrq = req;
  58. }
  59. }
  60. host->ops->request(host, req);
  61. rt_sem_take(&host->sem_ack, RT_WAITING_FOREVER);
  62. }
  63. rt_int32_t mmcsd_send_cmd(struct rt_mmcsd_host *host, struct rt_mmcsd_cmd *cmd, int retries)
  64. {
  65. struct rt_mmcsd_req req;
  66. rt_memset(&req, 0, sizeof(struct rt_mmcsd_req));
  67. rt_memset(cmd->resp, 0, sizeof(cmd->resp));
  68. req.cmd = cmd;
  69. cmd->data = RT_NULL;
  70. mmcsd_send_request(host, &req);
  71. return cmd->err;
  72. }
  73. rt_int32_t mmcsd_go_idle(struct rt_mmcsd_host *host)
  74. {
  75. rt_int32_t err;
  76. struct rt_mmcsd_cmd cmd;
  77. if (!controller_is_spi(host)) {
  78. mmcsd_set_chip_select(host, MMCSD_CS_HIGH);
  79. mmcsd_delay_ms(1);
  80. }
  81. rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
  82. cmd.cmd_code = GO_IDLE_STATE;
  83. cmd.arg = 0;
  84. cmd.flags = RESP_SPI_R1 | RESP_NONE | CMD_BC;
  85. err = mmcsd_send_cmd(host, &cmd, 0);
  86. mmcsd_delay_ms(1);
  87. if (!controller_is_spi(host))
  88. {
  89. mmcsd_set_chip_select(host, MMCSD_CS_IGNORE);
  90. mmcsd_delay_ms(1);
  91. }
  92. return err;
  93. }
  94. rt_int32_t mmcsd_spi_read_ocr(struct rt_mmcsd_host *host, rt_int32_t high_capacity, rt_uint32_t *ocr)
  95. {
  96. struct rt_mmcsd_cmd cmd;
  97. rt_int32_t err;
  98. rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
  99. cmd.cmd_code = SPI_READ_OCR;
  100. cmd.arg = high_capacity ? (1 << 30) : 0;
  101. cmd.flags = RESP_SPI_R3;
  102. err = mmcsd_send_cmd(host, &cmd, 0);
  103. *ocr = cmd.resp[1];
  104. return err;
  105. }
  106. rt_int32_t mmcsd_all_get_cid(struct rt_mmcsd_host *host, rt_uint32_t *cid)
  107. {
  108. rt_int32_t err;
  109. struct rt_mmcsd_cmd cmd;
  110. rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
  111. cmd.cmd_code = ALL_SEND_CID;
  112. cmd.arg = 0;
  113. cmd.flags = RESP_R2 | CMD_BCR;
  114. err = mmcsd_send_cmd(host, &cmd, 3);
  115. if (err)
  116. return err;
  117. rt_memcpy(cid, cmd.resp, sizeof(rt_uint32_t) * 4);
  118. return 0;
  119. }
  120. rt_int32_t mmcsd_get_cid(struct rt_mmcsd_host *host, rt_uint32_t *cid)
  121. {
  122. rt_int32_t err, i;
  123. struct rt_mmcsd_req req;
  124. struct rt_mmcsd_cmd cmd;
  125. struct rt_mmcsd_data data;
  126. rt_uint32_t *buf = RT_NULL;
  127. if (!controller_is_spi(host))
  128. {
  129. if (!host->card)
  130. return -RT_ERROR;
  131. rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
  132. cmd.cmd_code = SEND_CID;
  133. cmd.arg = host->card->rca << 16;
  134. cmd.flags = RESP_R2 | CMD_AC;
  135. err = mmcsd_send_cmd(host, &cmd, 3);
  136. if (err)
  137. return err;
  138. rt_memcpy(cid, cmd.resp, sizeof(rt_uint32_t) * 4);
  139. return 0;
  140. }
  141. buf = (rt_uint32_t *)rt_malloc(16);
  142. if (!buf)
  143. {
  144. rt_kprintf("allocate memory failed\n");
  145. return -RT_ENOMEM;
  146. }
  147. rt_memset(&req, 0, sizeof(struct rt_mmcsd_req));
  148. rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
  149. rt_memset(&data, 0, sizeof(struct rt_mmcsd_data));
  150. req.cmd = &cmd;
  151. req.data = &data;
  152. cmd.cmd_code = SEND_CID;
  153. cmd.arg = 0;
  154. /* NOTE HACK: the RESP_SPI_R1 is always correct here, but we
  155. * rely on callers to never use this with "native" calls for reading
  156. * CSD or CID. Native versions of those commands use the R2 type,
  157. * not R1 plus a data block.
  158. */
  159. cmd.flags = RESP_SPI_R1 | RESP_R1 | CMD_ADTC;
  160. data.blksize = 16;
  161. data.blks = 1;
  162. data.flags = DATA_DIR_READ;
  163. data.buf = buf;
  164. /*
  165. * The spec states that CSR and CID accesses have a timeout
  166. * of 64 clock cycles.
  167. */
  168. data.timeout_ns = 0;
  169. data.timeout_clks = 64;
  170. mmcsd_send_request(host, &req);
  171. if (cmd.err || data.err)
  172. {
  173. rt_free(buf);
  174. return -RT_ERROR;
  175. }
  176. for (i = 0;i < 4;i++)
  177. cid[i] = buf[i];
  178. rt_free(buf);
  179. return 0;
  180. }
  181. rt_int32_t mmcsd_get_csd(struct rt_mmcsd_card *card, rt_uint32_t *csd)
  182. {
  183. rt_int32_t err, i;
  184. struct rt_mmcsd_req req;
  185. struct rt_mmcsd_cmd cmd;
  186. struct rt_mmcsd_data data;
  187. rt_uint32_t *buf = RT_NULL;
  188. if (!controller_is_spi(card->host))
  189. {
  190. rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
  191. cmd.cmd_code = SEND_CSD;
  192. cmd.arg = card->rca << 16;
  193. cmd.flags = RESP_R2 | CMD_AC;
  194. err = mmcsd_send_cmd(card->host, &cmd, 3);
  195. if (err)
  196. return err;
  197. rt_memcpy(csd, cmd.resp, sizeof(rt_uint32_t) * 4);
  198. return 0;
  199. }
  200. buf = (rt_uint32_t*)rt_malloc(16);
  201. if (!buf)
  202. {
  203. rt_kprintf("allocate memory failed\n");
  204. return -RT_ENOMEM;
  205. }
  206. rt_memset(&req, 0, sizeof(struct rt_mmcsd_req));
  207. rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
  208. rt_memset(&data, 0, sizeof(struct rt_mmcsd_data));
  209. req.cmd = &cmd;
  210. req.data = &data;
  211. cmd.cmd_code = SEND_CSD;
  212. cmd.arg = 0;
  213. /* NOTE HACK: the RESP_SPI_R1 is always correct here, but we
  214. * rely on callers to never use this with "native" calls for reading
  215. * CSD or CID. Native versions of those commands use the R2 type,
  216. * not R1 plus a data block.
  217. */
  218. cmd.flags = RESP_SPI_R1 | RESP_R1 | CMD_ADTC;
  219. data.blksize = 16;
  220. data.blks = 1;
  221. data.flags = DATA_DIR_READ;
  222. data.buf = buf;
  223. /*
  224. * The spec states that CSR and CID accesses have a timeout
  225. * of 64 clock cycles.
  226. */
  227. data.timeout_ns = 0;
  228. data.timeout_clks = 64;
  229. mmcsd_send_request(card->host, &req);
  230. if (cmd.err || data.err)
  231. {
  232. rt_free(buf);
  233. return -RT_ERROR;
  234. }
  235. for (i = 0;i < 4;i++)
  236. csd[i] = buf[i];
  237. rt_free(buf);
  238. return 0;
  239. }
  240. static rt_int32_t _mmcsd_select_card(struct rt_mmcsd_host *host, struct rt_mmcsd_card *card)
  241. {
  242. rt_int32_t err;
  243. struct rt_mmcsd_cmd cmd;
  244. rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
  245. cmd.cmd_code = SELECT_CARD;
  246. if (card)
  247. {
  248. cmd.arg = card->rca << 16;
  249. cmd.flags = RESP_R1 | CMD_AC;
  250. }
  251. else
  252. {
  253. cmd.arg = 0;
  254. cmd.flags = RESP_NONE | CMD_AC;
  255. }
  256. err = mmcsd_send_cmd(host, &cmd, 3);
  257. if (err)
  258. return err;
  259. return 0;
  260. }
  261. rt_int32_t mmcsd_select_card(struct rt_mmcsd_card *card)
  262. {
  263. return _mmcsd_select_card(card->host, card);
  264. }
  265. rt_int32_t mmcsd_deselect_cards(struct rt_mmcsd_card *card)
  266. {
  267. return _mmcsd_select_card(card->host, RT_NULL);
  268. }
  269. rt_int32_t mmcsd_spi_use_crc(struct rt_mmcsd_host *host, rt_int32_t use_crc)
  270. {
  271. struct rt_mmcsd_cmd cmd;
  272. rt_int32_t err;
  273. rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
  274. cmd.cmd_code = SPI_CRC_ON_OFF;
  275. cmd.flags = RESP_SPI_R1;
  276. cmd.arg = use_crc;
  277. err = mmcsd_send_cmd(host, &cmd, 0);
  278. if (!err)
  279. host->spi_use_crc = use_crc;
  280. return err;
  281. }
  282. rt_inline void mmcsd_set_iocfg(struct rt_mmcsd_host *host)
  283. {
  284. struct rt_mmcsd_io_cfg *io_cfg = &host->io_cfg;
  285. mmcsd_dbg("clock %uHz busmode %u powermode %u cs %u Vdd %u "
  286. "width %u \n",
  287. io_cfg->clock, io_cfg->bus_mode,
  288. io_cfg->power_mode, io_cfg->chip_select, io_cfg->vdd,
  289. io_cfg->bus_width);
  290. host->ops->set_iocfg(host, io_cfg);
  291. }
  292. /*
  293. * Control chip select pin on a host.
  294. */
  295. void mmcsd_set_chip_select(struct rt_mmcsd_host *host, rt_int32_t mode)
  296. {
  297. host->io_cfg.chip_select = mode;
  298. mmcsd_set_iocfg(host);
  299. }
  300. /*
  301. * Sets the host clock to the highest possible frequency that
  302. * is below "hz".
  303. */
  304. void mmcsd_set_clock(struct rt_mmcsd_host *host, rt_uint32_t clk)
  305. {
  306. if (clk < host->freq_min)
  307. {
  308. rt_kprintf("clock too low\n");
  309. }
  310. host->io_cfg.clock = clk;
  311. mmcsd_set_iocfg(host);
  312. }
  313. /*
  314. * Change the bus mode (open drain/push-pull) of a host.
  315. */
  316. void mmcsd_set_bus_mode(struct rt_mmcsd_host *host, rt_uint32_t mode)
  317. {
  318. host->io_cfg.bus_mode = mode;
  319. mmcsd_set_iocfg(host);
  320. }
  321. /*
  322. * Change data bus width of a host.
  323. */
  324. void mmcsd_set_bus_width(struct rt_mmcsd_host *host, rt_uint32_t width)
  325. {
  326. host->io_cfg.bus_width = width;
  327. mmcsd_set_iocfg(host);
  328. }
  329. void mmcsd_set_data_timeout(struct rt_mmcsd_data *data, const struct rt_mmcsd_card *card)
  330. {
  331. rt_uint32_t mult;
  332. if (card->card_type == CARD_TYPE_SDIO)
  333. {
  334. data->timeout_ns = 1000000000; /* SDIO card 1s */
  335. data->timeout_clks = 0;
  336. return;
  337. }
  338. /*
  339. * SD cards use a 100 multiplier rather than 10
  340. */
  341. mult = (card->card_type == CARD_TYPE_SD) ? 100 : 10;
  342. /*
  343. * Scale up the multiplier (and therefore the timeout) by
  344. * the r2w factor for writes.
  345. */
  346. if (data->flags & DATA_DIR_WRITE)
  347. mult <<= card->csd.r2w_factor;
  348. data->timeout_ns = card->tacc_ns * mult;
  349. data->timeout_clks = card->tacc_clks * mult;
  350. /*
  351. * SD cards also have an upper limit on the timeout.
  352. */
  353. if (card->card_type == CARD_TYPE_SD)
  354. {
  355. rt_uint32_t timeout_us, limit_us;
  356. timeout_us = data->timeout_ns / 1000;
  357. timeout_us += data->timeout_clks * 1000 /
  358. (card->host->io_cfg.clock / 1000);
  359. if (data->flags & DATA_DIR_WRITE)
  360. /*
  361. * The limit is really 250 ms, but that is
  362. * insufficient for some crappy cards.
  363. */
  364. limit_us = 300000;
  365. else
  366. limit_us = 100000;
  367. /*
  368. * SDHC cards always use these fixed values.
  369. */
  370. if (timeout_us > limit_us || card->flags & CARD_FLAG_SDHC)
  371. {
  372. data->timeout_ns = limit_us * 1000; /* SDHC card fixed 250ms */
  373. data->timeout_clks = 0;
  374. }
  375. }
  376. if (controller_is_spi(card->host))
  377. {
  378. if (data->flags & DATA_DIR_WRITE)
  379. {
  380. if (data->timeout_ns < 1000000000)
  381. data->timeout_ns = 1000000000; /* 1s */
  382. }
  383. else
  384. {
  385. if (data->timeout_ns < 100000000)
  386. data->timeout_ns = 100000000; /* 100ms */
  387. }
  388. }
  389. }
  390. /*
  391. * Mask off any voltages we don't support and select
  392. * the lowest voltage
  393. */
  394. rt_uint32_t mmcsd_select_voltage(struct rt_mmcsd_host *host, rt_uint32_t ocr)
  395. {
  396. int bit;
  397. ocr &= host->valid_ocr;
  398. bit = __rt_ffs(ocr);
  399. if (bit)
  400. {
  401. bit -= 1;
  402. ocr &= 3 << bit;
  403. host->io_cfg.vdd = bit;
  404. mmcsd_set_iocfg(host);
  405. }
  406. else
  407. {
  408. rt_kprintf("host doesn't support card's voltages\n");
  409. ocr = 0;
  410. }
  411. return ocr;
  412. }
  413. static void mmcsd_power_up(struct rt_mmcsd_host *host)
  414. {
  415. int bit = fls(host->valid_ocr) - 1;
  416. host->io_cfg.vdd = bit;
  417. if (controller_is_spi(host))
  418. {
  419. host->io_cfg.chip_select = MMCSD_CS_HIGH;
  420. host->io_cfg.bus_mode = MMCSD_BUSMODE_PUSHPULL;
  421. }
  422. else
  423. {
  424. host->io_cfg.chip_select = MMCSD_CS_IGNORE;
  425. host->io_cfg.bus_mode = MMCSD_BUSMODE_OPENDRAIN;
  426. }
  427. host->io_cfg.power_mode = MMCSD_POWER_UP;
  428. host->io_cfg.bus_width = MMCSD_BUS_WIDTH_1;
  429. mmcsd_set_iocfg(host);
  430. /*
  431. * This delay should be sufficient to allow the power supply
  432. * to reach the minimum voltage.
  433. */
  434. mmcsd_delay_ms(10);
  435. host->io_cfg.clock = host->freq_min;
  436. host->io_cfg.power_mode = MMCSD_POWER_ON;
  437. mmcsd_set_iocfg(host);
  438. /*
  439. * This delay must be at least 74 clock sizes, or 1 ms, or the
  440. * time required to reach a stable voltage.
  441. */
  442. mmcsd_delay_ms(10);
  443. }
  444. static void mmcsd_power_off(struct rt_mmcsd_host *host)
  445. {
  446. host->io_cfg.clock = 0;
  447. host->io_cfg.vdd = 0;
  448. if (!controller_is_spi(host))
  449. {
  450. host->io_cfg.bus_mode = MMCSD_BUSMODE_OPENDRAIN;
  451. host->io_cfg.chip_select = MMCSD_CS_IGNORE;
  452. }
  453. host->io_cfg.power_mode = MMCSD_POWER_OFF;
  454. host->io_cfg.bus_width = MMCSD_BUS_WIDTH_1;
  455. mmcsd_set_iocfg(host);
  456. }
  457. void mmcsd_change(struct rt_mmcsd_host *host)
  458. {
  459. rt_mb_send(&mmcsd_detect_mb, (rt_uint32_t)host);
  460. }
  461. void mmcsd_detect(void *param)
  462. {
  463. struct rt_mmcsd_host *host;
  464. rt_uint32_t ocr;
  465. rt_int32_t err;
  466. while (1)
  467. {
  468. if (rt_mb_recv(&mmcsd_detect_mb, (rt_uint32_t*)&host, RT_WAITING_FOREVER) == RT_EOK)
  469. {
  470. if (host->card == RT_NULL)
  471. {
  472. mmcsd_host_lock(host);
  473. mmcsd_power_up(host);
  474. mmcsd_go_idle(host);
  475. mmcsd_send_if_cond(host, host->valid_ocr);
  476. err = sdio_io_send_op_cond(host, 0, &ocr);
  477. if (!err) {
  478. if (init_sdio(host, ocr))
  479. mmcsd_power_off(host);
  480. mmcsd_host_unlock(host);
  481. continue;
  482. }
  483. /*
  484. * detect SD card
  485. */
  486. err = mmcsd_send_app_op_cond(host, 0, &ocr);
  487. if (!err)
  488. {
  489. if (init_sd(host, ocr))
  490. mmcsd_power_off(host);
  491. mmcsd_host_unlock(host);
  492. continue;
  493. }
  494. mmcsd_host_unlock(host);
  495. }
  496. }
  497. }
  498. }
  499. struct rt_mmcsd_host *mmcsd_alloc_host(void)
  500. {
  501. struct rt_mmcsd_host *host;
  502. host = rt_malloc(sizeof(struct rt_mmcsd_host));
  503. if (!host)
  504. {
  505. rt_kprintf("alloc host failed\n");
  506. return RT_NULL;
  507. }
  508. rt_memset(host, 0, sizeof(struct rt_mmcsd_host));
  509. host->max_seg_size = 65535;
  510. host->max_dma_segs = 1;
  511. host->max_blk_size = 512;
  512. host->max_blk_count = 4096;
  513. rt_sem_init(&host->bus_lock, "sd_bus_lock", 1, RT_IPC_FLAG_FIFO);
  514. rt_sem_init(&host->sem_ack, "sd_ack", 0, RT_IPC_FLAG_FIFO);
  515. return host;
  516. }
  517. void mmcsd_free_host(struct rt_mmcsd_host *host)
  518. {
  519. rt_sem_detach(&host->bus_lock);
  520. rt_sem_detach(&host->sem_ack);
  521. rt_free(host);
  522. }
  523. void rt_mmcsd_core_init(void)
  524. {
  525. rt_err_t ret;
  526. /* init detect sd cart thread */
  527. /* init mailbox and create detect sd card thread */
  528. ret = rt_mb_init(&mmcsd_detect_mb, "mmcsdmb",
  529. &mmcsd_detect_mb_pool[0], sizeof(mmcsd_detect_mb_pool),
  530. RT_IPC_FLAG_FIFO);
  531. RT_ASSERT(ret == RT_EOK);
  532. ret = rt_thread_init(&mmcsd_detect_thread, "mmcsd_detect", mmcsd_detect, RT_NULL,
  533. &mmcsd_stack[0], RT_MMCSD_STACK_SIZE, RT_MMCSD_THREAD_PREORITY, 20);
  534. if (ret == RT_EOK)
  535. {
  536. rt_thread_startup(&mmcsd_detect_thread);
  537. }
  538. rt_sdio_init();
  539. }