mmcsd_core.c 13 KB

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