dev_mmcsd_core.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2011-07-25 weety first version
  9. */
  10. #include <rtthread.h>
  11. #include <drivers/dev_mmcsd_core.h>
  12. #include <drivers/dev_sd.h>
  13. #include <drivers/dev_mmc.h>
  14. #include <drivers/dev_sdio.h>
  15. #include <string.h>
  16. #define DBG_TAG "SDIO"
  17. #ifdef RT_SDIO_DEBUG
  18. #define DBG_LVL DBG_LOG
  19. #else
  20. #define DBG_LVL DBG_INFO
  21. #endif /* RT_SDIO_DEBUG */
  22. #include <rtdbg.h>
  23. #ifndef RT_MMCSD_STACK_SIZE
  24. #define RT_MMCSD_STACK_SIZE 1024
  25. #endif
  26. #ifndef RT_MMCSD_THREAD_PRIORITY
  27. #if (RT_THREAD_PRIORITY_MAX == 32)
  28. #define RT_MMCSD_THREAD_PRIORITY 0x16
  29. #else
  30. #define RT_MMCSD_THREAD_PRIORITY 0x40
  31. #endif
  32. #endif
  33. //static struct rt_semaphore mmcsd_sem;
  34. static struct rt_thread mmcsd_detect_thread;
  35. static rt_uint8_t mmcsd_stack[RT_MMCSD_STACK_SIZE];
  36. static struct rt_mailbox mmcsd_detect_mb;
  37. static rt_uint32_t mmcsd_detect_mb_pool[4];
  38. static struct rt_mailbox mmcsd_hotpluge_mb;
  39. static rt_uint32_t mmcsd_hotpluge_mb_pool[4];
  40. void mmcsd_host_lock(struct rt_mmcsd_host *host)
  41. {
  42. rt_mutex_take(&host->bus_lock, RT_WAITING_FOREVER);
  43. }
  44. void mmcsd_host_unlock(struct rt_mmcsd_host *host)
  45. {
  46. rt_mutex_release(&host->bus_lock);
  47. }
  48. void mmcsd_req_complete(struct rt_mmcsd_host *host)
  49. {
  50. rt_sem_release(&host->sem_ack);
  51. }
  52. void mmcsd_send_request(struct rt_mmcsd_host *host, struct rt_mmcsd_req *req)
  53. {
  54. do
  55. {
  56. req->cmd->retries--;
  57. req->cmd->err = 0;
  58. req->cmd->mrq = req;
  59. if (req->data)
  60. {
  61. req->cmd->data = req->data;
  62. req->data->err = 0;
  63. req->data->mrq = req;
  64. if (req->stop)
  65. {
  66. req->data->stop = req->stop;
  67. req->stop->err = 0;
  68. req->stop->mrq = req;
  69. }
  70. }
  71. host->ops->request(host, req);
  72. rt_sem_take(&host->sem_ack, RT_WAITING_FOREVER);
  73. }
  74. while (req->cmd->err && (req->cmd->retries > 0));
  75. }
  76. rt_int32_t mmcsd_send_cmd(struct rt_mmcsd_host *host,
  77. struct rt_mmcsd_cmd *cmd,
  78. int retries)
  79. {
  80. struct rt_mmcsd_req req;
  81. rt_memset(&req, 0, sizeof(struct rt_mmcsd_req));
  82. rt_memset(cmd->resp, 0, sizeof(cmd->resp));
  83. cmd->retries = retries;
  84. req.cmd = cmd;
  85. cmd->data = RT_NULL;
  86. mmcsd_send_request(host, &req);
  87. return cmd->err;
  88. }
  89. rt_int32_t mmcsd_go_idle(struct rt_mmcsd_host *host)
  90. {
  91. rt_int32_t err;
  92. struct rt_mmcsd_cmd cmd;
  93. if (!controller_is_spi(host))
  94. {
  95. mmcsd_set_chip_select(host, MMCSD_CS_HIGH);
  96. rt_thread_mdelay(1);
  97. }
  98. rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
  99. cmd.cmd_code = GO_IDLE_STATE;
  100. cmd.arg = 0;
  101. cmd.flags = RESP_SPI_R1 | RESP_NONE | CMD_BC;
  102. err = mmcsd_send_cmd(host, &cmd, 0);
  103. rt_thread_mdelay(1);
  104. if (!controller_is_spi(host))
  105. {
  106. mmcsd_set_chip_select(host, MMCSD_CS_IGNORE);
  107. rt_thread_mdelay(1);
  108. }
  109. return err;
  110. }
  111. rt_int32_t mmcsd_spi_read_ocr(struct rt_mmcsd_host *host,
  112. rt_int32_t high_capacity,
  113. rt_uint32_t *ocr)
  114. {
  115. struct rt_mmcsd_cmd cmd;
  116. rt_int32_t err;
  117. rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
  118. cmd.cmd_code = SPI_READ_OCR;
  119. cmd.arg = high_capacity ? (1 << 30) : 0;
  120. cmd.flags = RESP_SPI_R3;
  121. err = mmcsd_send_cmd(host, &cmd, 0);
  122. *ocr = cmd.resp[1];
  123. return err;
  124. }
  125. rt_int32_t mmcsd_all_get_cid(struct rt_mmcsd_host *host, rt_uint32_t *cid)
  126. {
  127. rt_int32_t err;
  128. struct rt_mmcsd_cmd cmd;
  129. rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
  130. cmd.cmd_code = ALL_SEND_CID;
  131. cmd.arg = 0;
  132. cmd.flags = RESP_R2 | CMD_BCR;
  133. err = mmcsd_send_cmd(host, &cmd, 3);
  134. if (err)
  135. return err;
  136. rt_memcpy(cid, cmd.resp, sizeof(rt_uint32_t) * 4);
  137. return 0;
  138. }
  139. rt_int32_t mmcsd_get_cid(struct rt_mmcsd_host *host, rt_uint32_t *cid)
  140. {
  141. rt_int32_t err, i;
  142. struct rt_mmcsd_req req;
  143. struct rt_mmcsd_cmd cmd;
  144. struct rt_mmcsd_data data;
  145. rt_uint32_t *buf = RT_NULL;
  146. if (!controller_is_spi(host))
  147. {
  148. if (!host->card)
  149. return -RT_ERROR;
  150. rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
  151. cmd.cmd_code = SEND_CID;
  152. cmd.arg = host->card->rca << 16;
  153. cmd.flags = RESP_R2 | CMD_AC;
  154. err = mmcsd_send_cmd(host, &cmd, 3);
  155. if (err)
  156. return err;
  157. rt_memcpy(cid, cmd.resp, sizeof(rt_uint32_t) * 4);
  158. return 0;
  159. }
  160. buf = (rt_uint32_t *)rt_malloc(16);
  161. if (!buf)
  162. {
  163. LOG_E("allocate memory failed!");
  164. return -RT_ENOMEM;
  165. }
  166. rt_memset(&req, 0, sizeof(struct rt_mmcsd_req));
  167. rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
  168. rt_memset(&data, 0, sizeof(struct rt_mmcsd_data));
  169. req.cmd = &cmd;
  170. req.data = &data;
  171. cmd.cmd_code = SEND_CID;
  172. cmd.arg = 0;
  173. /* NOTE HACK: the RESP_SPI_R1 is always correct here, but we
  174. * rely on callers to never use this with "native" calls for reading
  175. * CSD or CID. Native versions of those commands use the R2 type,
  176. * not R1 plus a data block.
  177. */
  178. cmd.flags = RESP_SPI_R1 | RESP_R1 | CMD_ADTC;
  179. data.blksize = 16;
  180. data.blks = 1;
  181. data.flags = DATA_DIR_READ;
  182. data.buf = buf;
  183. /*
  184. * The spec states that CSR and CID accesses have a timeout
  185. * of 64 clock cycles.
  186. */
  187. data.timeout_ns = 0;
  188. data.timeout_clks = 64;
  189. mmcsd_send_request(host, &req);
  190. if (cmd.err || data.err)
  191. {
  192. rt_free(buf);
  193. return -RT_ERROR;
  194. }
  195. for (i = 0; i < 4; i++)
  196. cid[i] = buf[i];
  197. rt_free(buf);
  198. return 0;
  199. }
  200. rt_int32_t mmcsd_get_csd(struct rt_mmcsd_card *card, rt_uint32_t *csd)
  201. {
  202. rt_int32_t err, i;
  203. struct rt_mmcsd_req req;
  204. struct rt_mmcsd_cmd cmd;
  205. struct rt_mmcsd_data data;
  206. rt_uint32_t *buf = RT_NULL;
  207. if (!controller_is_spi(card->host))
  208. {
  209. rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
  210. cmd.cmd_code = SEND_CSD;
  211. cmd.arg = card->rca << 16;
  212. cmd.flags = RESP_R2 | CMD_AC;
  213. err = mmcsd_send_cmd(card->host, &cmd, 3);
  214. if (err)
  215. return err;
  216. rt_memcpy(csd, cmd.resp, sizeof(rt_uint32_t) * 4);
  217. return 0;
  218. }
  219. buf = (rt_uint32_t *)rt_malloc(16);
  220. if (!buf)
  221. {
  222. LOG_E("allocate memory failed!");
  223. return -RT_ENOMEM;
  224. }
  225. rt_memset(&req, 0, sizeof(struct rt_mmcsd_req));
  226. rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
  227. rt_memset(&data, 0, sizeof(struct rt_mmcsd_data));
  228. req.cmd = &cmd;
  229. req.data = &data;
  230. cmd.cmd_code = SEND_CSD;
  231. cmd.arg = 0;
  232. /* NOTE HACK: the RESP_SPI_R1 is always correct here, but we
  233. * rely on callers to never use this with "native" calls for reading
  234. * CSD or CID. Native versions of those commands use the R2 type,
  235. * not R1 plus a data block.
  236. */
  237. cmd.flags = RESP_SPI_R1 | RESP_R1 | CMD_ADTC;
  238. data.blksize = 16;
  239. data.blks = 1;
  240. data.flags = DATA_DIR_READ;
  241. data.buf = buf;
  242. /*
  243. * The spec states that CSR and CID accesses have a timeout
  244. * of 64 clock cycles.
  245. */
  246. data.timeout_ns = 0;
  247. data.timeout_clks = 64;
  248. mmcsd_send_request(card->host, &req);
  249. if (cmd.err || data.err)
  250. {
  251. rt_free(buf);
  252. return -RT_ERROR;
  253. }
  254. for (i = 0; i < 4; i++)
  255. csd[i] = buf[i];
  256. rt_free(buf);
  257. return 0;
  258. }
  259. static rt_int32_t _mmcsd_select_card(struct rt_mmcsd_host *host,
  260. struct rt_mmcsd_card *card)
  261. {
  262. rt_int32_t err;
  263. struct rt_mmcsd_cmd cmd;
  264. rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
  265. cmd.cmd_code = SELECT_CARD;
  266. if (card)
  267. {
  268. cmd.arg = card->rca << 16;
  269. cmd.flags = RESP_R1 | CMD_AC;
  270. }
  271. else
  272. {
  273. cmd.arg = 0;
  274. cmd.flags = RESP_NONE | CMD_AC;
  275. }
  276. err = mmcsd_send_cmd(host, &cmd, 3);
  277. if (err)
  278. return err;
  279. return 0;
  280. }
  281. rt_int32_t mmcsd_select_card(struct rt_mmcsd_card *card)
  282. {
  283. return _mmcsd_select_card(card->host, card);
  284. }
  285. rt_int32_t mmcsd_deselect_cards(struct rt_mmcsd_card *card)
  286. {
  287. return _mmcsd_select_card(card->host, RT_NULL);
  288. }
  289. rt_int32_t mmcsd_spi_use_crc(struct rt_mmcsd_host *host, rt_int32_t use_crc)
  290. {
  291. struct rt_mmcsd_cmd cmd;
  292. rt_int32_t err;
  293. rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
  294. cmd.cmd_code = SPI_CRC_ON_OFF;
  295. cmd.flags = RESP_SPI_R1;
  296. cmd.arg = use_crc;
  297. err = mmcsd_send_cmd(host, &cmd, 0);
  298. if (!err)
  299. host->spi_use_crc = use_crc;
  300. return err;
  301. }
  302. rt_inline void mmcsd_set_iocfg(struct rt_mmcsd_host *host)
  303. {
  304. struct rt_mmcsd_io_cfg *io_cfg = &host->io_cfg;
  305. mmcsd_dbg("clock %uHz busmode %u powermode %u cs %u Vdd %u "
  306. "width %u \n",
  307. io_cfg->clock, io_cfg->bus_mode,
  308. io_cfg->power_mode, io_cfg->chip_select, io_cfg->vdd,
  309. io_cfg->bus_width);
  310. host->ops->set_iocfg(host, io_cfg);
  311. }
  312. /*
  313. * Control chip select pin on a host.
  314. */
  315. void mmcsd_set_chip_select(struct rt_mmcsd_host *host, rt_int32_t mode)
  316. {
  317. host->io_cfg.chip_select = mode;
  318. mmcsd_set_iocfg(host);
  319. }
  320. /*
  321. * Sets the host clock to the highest possible frequency that
  322. * is below "hz".
  323. */
  324. void mmcsd_set_clock(struct rt_mmcsd_host *host, rt_uint32_t clk)
  325. {
  326. if (clk < host->freq_min)
  327. {
  328. LOG_W("clock too low!");
  329. }
  330. host->io_cfg.clock = clk;
  331. mmcsd_set_iocfg(host);
  332. }
  333. /*
  334. * Change the bus mode (open drain/push-pull) of a host.
  335. */
  336. void mmcsd_set_bus_mode(struct rt_mmcsd_host *host, rt_uint32_t mode)
  337. {
  338. host->io_cfg.bus_mode = mode;
  339. mmcsd_set_iocfg(host);
  340. }
  341. /*
  342. * Change data bus width of a host.
  343. */
  344. void mmcsd_set_bus_width(struct rt_mmcsd_host *host, rt_uint32_t width)
  345. {
  346. host->io_cfg.bus_width = width;
  347. mmcsd_set_iocfg(host);
  348. }
  349. void mmcsd_set_timing(struct rt_mmcsd_host *host, rt_uint32_t timing)
  350. {
  351. host->io_cfg.timing = timing;
  352. mmcsd_set_iocfg(host);
  353. }
  354. void mmcsd_set_data_timeout(struct rt_mmcsd_data *data,
  355. const struct rt_mmcsd_card *card)
  356. {
  357. rt_uint32_t mult;
  358. if (card->card_type == CARD_TYPE_SDIO)
  359. {
  360. data->timeout_ns = 1000000000; /* SDIO card 1s */
  361. data->timeout_clks = 0;
  362. return;
  363. }
  364. /*
  365. * SD cards use a 100 multiplier rather than 10
  366. */
  367. mult = (card->card_type == CARD_TYPE_SD) ? 100 : 10;
  368. /*
  369. * Scale up the multiplier (and therefore the timeout) by
  370. * the r2w factor for writes.
  371. */
  372. if (data->flags & DATA_DIR_WRITE)
  373. mult <<= card->csd.r2w_factor;
  374. data->timeout_ns = card->tacc_ns * mult;
  375. data->timeout_clks = card->tacc_clks * mult;
  376. /*
  377. * SD cards also have an upper limit on the timeout.
  378. */
  379. if (card->card_type == CARD_TYPE_SD)
  380. {
  381. rt_uint32_t timeout_us, limit_us;
  382. timeout_us = data->timeout_ns / 1000;
  383. timeout_us += data->timeout_clks * 1000 /
  384. (card->host->io_cfg.clock / 1000);
  385. if (data->flags & DATA_DIR_WRITE)
  386. /*
  387. * The limit is really 250 ms, but that is
  388. * insufficient for some crappy cards.
  389. */
  390. limit_us = 300000;
  391. else
  392. limit_us = 100000;
  393. /*
  394. * SDHC cards always use these fixed values.
  395. */
  396. if (timeout_us > limit_us || card->flags & CARD_FLAG_SDHC)
  397. {
  398. data->timeout_ns = limit_us * 1000; /* SDHC card fixed 250ms */
  399. data->timeout_clks = 0;
  400. }
  401. }
  402. if (controller_is_spi(card->host))
  403. {
  404. if (data->flags & DATA_DIR_WRITE)
  405. {
  406. if (data->timeout_ns < 1000000000)
  407. data->timeout_ns = 1000000000; /* 1s */
  408. }
  409. else
  410. {
  411. if (data->timeout_ns < 100000000)
  412. data->timeout_ns = 100000000; /* 100ms */
  413. }
  414. }
  415. }
  416. /*
  417. * Mask off any voltages we don't support and select
  418. * the lowest voltage
  419. */
  420. rt_uint32_t mmcsd_select_voltage(struct rt_mmcsd_host *host, rt_uint32_t ocr)
  421. {
  422. int bit;
  423. extern int __rt_ffs(int value);
  424. ocr &= host->valid_ocr;
  425. bit = __rt_ffs(ocr);
  426. if (bit)
  427. {
  428. bit -= 1;
  429. ocr &= 3 << bit;
  430. host->io_cfg.vdd = bit;
  431. mmcsd_set_iocfg(host);
  432. }
  433. else
  434. {
  435. LOG_W("host doesn't support card's voltages!");
  436. ocr = 0;
  437. }
  438. return ocr;
  439. }
  440. rt_err_t mmcsd_set_signal_voltage(struct rt_mmcsd_host *host, unsigned char signal_voltage)
  441. {
  442. rt_err_t err = RT_EOK;
  443. unsigned char old_signal_voltage = host->io_cfg.signal_voltage;
  444. host->io_cfg.signal_voltage = signal_voltage;
  445. if (host->ops->signal_voltage_switch)
  446. {
  447. err = host->ops->signal_voltage_switch(host, &host->io_cfg);
  448. }
  449. if (err)
  450. {
  451. host->io_cfg.signal_voltage = old_signal_voltage;
  452. }
  453. return err;
  454. }
  455. void mmcsd_set_initial_signal_voltage(struct rt_mmcsd_host *host)
  456. {
  457. /* 3.3V -> 1.8v -> 1.2v */
  458. if (!mmcsd_set_signal_voltage(host, MMCSD_SIGNAL_VOLTAGE_330))
  459. {
  460. LOG_D("Initial signal voltage of %sv", "3.3");
  461. }
  462. else if (!mmcsd_set_signal_voltage(host, MMCSD_SIGNAL_VOLTAGE_180))
  463. {
  464. LOG_D("Initial signal voltage of %sv", "1.8");
  465. }
  466. else if (!mmcsd_set_signal_voltage(host, MMCSD_SIGNAL_VOLTAGE_120))
  467. {
  468. LOG_D("Initial signal voltage of %sv", "1.2");
  469. }
  470. }
  471. rt_err_t mmcsd_host_set_uhs_voltage(struct rt_mmcsd_host *host)
  472. {
  473. rt_uint32_t old_clock = host->io_cfg.clock;
  474. host->io_cfg.clock = 0;
  475. mmcsd_set_iocfg(host);
  476. if (mmcsd_set_signal_voltage(host, MMCSD_SIGNAL_VOLTAGE_180))
  477. {
  478. return -RT_ERROR;
  479. }
  480. /* Keep clock gated for at least 10 ms, though spec only says 5 ms */
  481. rt_thread_mdelay(10);
  482. host->io_cfg.clock = old_clock;
  483. mmcsd_set_iocfg(host);
  484. return RT_EOK;
  485. }
  486. static void mmcsd_power_cycle(struct rt_mmcsd_host *host, rt_uint32_t ocr);
  487. rt_err_t mmcsd_set_uhs_voltage(struct rt_mmcsd_host *host, rt_uint32_t ocr)
  488. {
  489. rt_err_t err = RT_EOK;
  490. struct rt_mmcsd_cmd cmd;
  491. if (!host->ops->signal_voltage_switch)
  492. {
  493. return -RT_EINVAL;
  494. }
  495. if (!host->ops->card_busy)
  496. {
  497. LOG_W("%s: Cannot verify signal voltage switch", host->name);
  498. }
  499. rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
  500. cmd.cmd_code = VOLTAGE_SWITCH;
  501. cmd.arg = 0;
  502. cmd.flags = RESP_R1 | CMD_AC;
  503. err = mmcsd_send_cmd(host, &cmd, 0);
  504. if (err)
  505. {
  506. goto power_cycle;
  507. }
  508. if (!controller_is_spi(host) && (cmd.resp[0] & R1_ERROR))
  509. {
  510. return -RT_EIO;
  511. }
  512. /*
  513. * The card should drive cmd and dat[0:3] low immediately
  514. * after the response of cmd11, but wait 1 ms to be sure
  515. */
  516. rt_thread_mdelay(1);
  517. if (host->ops->card_busy && !host->ops->card_busy(host))
  518. {
  519. err = -RT_ERROR;
  520. goto power_cycle;
  521. }
  522. if (mmcsd_host_set_uhs_voltage(host))
  523. {
  524. /*
  525. * Voltages may not have been switched, but we've already
  526. * sent CMD11, so a power cycle is required anyway
  527. */
  528. err = -RT_ERROR;
  529. goto power_cycle;
  530. }
  531. /* Wait for at least 1 ms according to spec */
  532. rt_thread_mdelay(1);
  533. /*
  534. * Failure to switch is indicated by the card holding
  535. * dat[0:3] low
  536. */
  537. if (host->ops->card_busy && host->ops->card_busy(host))
  538. {
  539. err = -RT_ERROR;
  540. }
  541. power_cycle:
  542. if (err)
  543. {
  544. LOG_D("%s: Signal voltage switch failed, power cycling card", host->name);
  545. mmcsd_power_cycle(host, ocr);
  546. }
  547. return err;
  548. }
  549. static const rt_uint8_t tuning_blk_pattern_4bit[] =
  550. {
  551. 0xff, 0x0f, 0xff, 0x00, 0xff, 0xcc, 0xc3, 0xcc,
  552. 0xc3, 0x3c, 0xcc, 0xff, 0xfe, 0xff, 0xfe, 0xef,
  553. 0xff, 0xdf, 0xff, 0xdd, 0xff, 0xfb, 0xff, 0xfb,
  554. 0xbf, 0xff, 0x7f, 0xff, 0x77, 0xf7, 0xbd, 0xef,
  555. 0xff, 0xf0, 0xff, 0xf0, 0x0f, 0xfc, 0xcc, 0x3c,
  556. 0xcc, 0x33, 0xcc, 0xcf, 0xff, 0xef, 0xff, 0xee,
  557. 0xff, 0xfd, 0xff, 0xfd, 0xdf, 0xff, 0xbf, 0xff,
  558. 0xbb, 0xff, 0xf7, 0xff, 0xf7, 0x7f, 0x7b, 0xde,
  559. };
  560. static const rt_uint8_t tuning_blk_pattern_8bit[] =
  561. {
  562. 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00,
  563. 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc, 0xcc,
  564. 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff, 0xff,
  565. 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee, 0xff,
  566. 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd, 0xdd,
  567. 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb,
  568. 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff, 0xff,
  569. 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee, 0xff,
  570. 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00,
  571. 0x00, 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc,
  572. 0xcc, 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff,
  573. 0xff, 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee,
  574. 0xff, 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd,
  575. 0xdd, 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff,
  576. 0xbb, 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff,
  577. 0xff, 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee,
  578. };
  579. rt_err_t mmcsd_send_tuning(struct rt_mmcsd_host *host, rt_uint32_t opcode, rt_err_t *cmd_error)
  580. {
  581. rt_err_t err = RT_EOK;
  582. int size;
  583. rt_uint8_t *data_buf;
  584. const rt_uint8_t *tuning_block_pattern;
  585. struct rt_mmcsd_req req = {};
  586. struct rt_mmcsd_cmd cmd = {};
  587. struct rt_mmcsd_data data = {};
  588. struct rt_mmcsd_io_cfg *io_cfg = &host->io_cfg;
  589. if (io_cfg->bus_width == MMCSD_BUS_WIDTH_8)
  590. {
  591. tuning_block_pattern = tuning_blk_pattern_8bit;
  592. size = sizeof(tuning_blk_pattern_8bit);
  593. }
  594. else if (io_cfg->bus_width == MMCSD_BUS_WIDTH_4)
  595. {
  596. tuning_block_pattern = tuning_blk_pattern_4bit;
  597. size = sizeof(tuning_blk_pattern_4bit);
  598. }
  599. else
  600. {
  601. return -RT_EINVAL;
  602. }
  603. data_buf = rt_malloc(size);
  604. if (!data_buf)
  605. {
  606. return -RT_ENOMEM;
  607. }
  608. rt_memset(data_buf, 0, size);
  609. rt_memset(&req, 0, sizeof(struct rt_mmcsd_req));
  610. rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
  611. rt_memset(&data, 0, sizeof(struct rt_mmcsd_data));
  612. req.cmd = &cmd;
  613. req.data = &data;
  614. cmd.cmd_code = opcode;
  615. cmd.flags = RESP_R1 | CMD_ADTC;
  616. data.blksize = size;
  617. data.blks = 1;
  618. data.flags = DATA_DIR_READ;
  619. /*
  620. * According to the tuning specs, Tuning process
  621. * is normally shorter 40 executions of CMD19,
  622. * and timeout value should be shorter than 150 ms
  623. */
  624. data.timeout_ns = 150 * 1000000;
  625. mmcsd_send_request(host, &req);
  626. if (cmd_error)
  627. {
  628. *cmd_error = cmd.err;
  629. }
  630. if (cmd.err)
  631. {
  632. err = cmd.err;
  633. goto out_free;
  634. }
  635. if (data.err)
  636. {
  637. err = data.err;
  638. goto out_free;
  639. }
  640. if (rt_memcmp(data_buf, tuning_block_pattern, size))
  641. {
  642. err = -RT_EIO;
  643. }
  644. out_free:
  645. rt_free(data_buf);
  646. return err;
  647. }
  648. rt_err_t mmcsd_send_abort_tuning(struct rt_mmcsd_host *host, rt_uint32_t opcode)
  649. {
  650. struct rt_mmcsd_cmd cmd = {};
  651. /*
  652. * eMMC specification specifies that CMD12 can be used to stop a tuning
  653. * command, but SD specification does not, so do nothing unless it is eMMC.
  654. */
  655. if (opcode != SEND_TUNING_BLOCK_HS200)
  656. {
  657. return 0;
  658. }
  659. cmd.cmd_code = STOP_TRANSMISSION;
  660. cmd.flags = RESP_SPI_R1 | RESP_R1 | CMD_AC;
  661. /*
  662. * For drivers that override R1 to R1b, set an arbitrary timeout based
  663. * on the tuning timeout i.e. 150ms.
  664. */
  665. cmd.busy_timeout = 150;
  666. return mmcsd_send_cmd(host, &cmd, 0);
  667. }
  668. static void mmcsd_power_up(struct rt_mmcsd_host *host)
  669. {
  670. int bit = __rt_fls(host->valid_ocr) - 1;
  671. host->io_cfg.vdd = bit;
  672. if (controller_is_spi(host))
  673. {
  674. host->io_cfg.chip_select = MMCSD_CS_HIGH;
  675. host->io_cfg.bus_mode = MMCSD_BUSMODE_PUSHPULL;
  676. }
  677. else
  678. {
  679. host->io_cfg.chip_select = MMCSD_CS_IGNORE;
  680. host->io_cfg.bus_mode = MMCSD_BUSMODE_OPENDRAIN;
  681. }
  682. host->io_cfg.power_mode = MMCSD_POWER_UP;
  683. host->io_cfg.bus_width = MMCSD_BUS_WIDTH_1;
  684. mmcsd_set_iocfg(host);
  685. mmcsd_set_initial_signal_voltage(host);
  686. /*
  687. * This delay should be sufficient to allow the power supply
  688. * to reach the minimum voltage.
  689. */
  690. rt_thread_mdelay(10);
  691. host->io_cfg.clock = host->freq_min;
  692. host->io_cfg.power_mode = MMCSD_POWER_ON;
  693. mmcsd_set_iocfg(host);
  694. /*
  695. * This delay must be at least 74 clock sizes, or 1 ms, or the
  696. * time required to reach a stable voltage.
  697. */
  698. rt_thread_mdelay(10);
  699. }
  700. static void mmcsd_power_off(struct rt_mmcsd_host *host)
  701. {
  702. host->io_cfg.clock = 0;
  703. host->io_cfg.vdd = 0;
  704. if (!controller_is_spi(host))
  705. {
  706. host->io_cfg.bus_mode = MMCSD_BUSMODE_OPENDRAIN;
  707. host->io_cfg.chip_select = MMCSD_CS_IGNORE;
  708. }
  709. host->io_cfg.power_mode = MMCSD_POWER_OFF;
  710. host->io_cfg.bus_width = MMCSD_BUS_WIDTH_1;
  711. mmcsd_set_iocfg(host);
  712. }
  713. static void mmcsd_power_cycle(struct rt_mmcsd_host *host, rt_uint32_t ocr)
  714. {
  715. mmcsd_power_off(host);
  716. /* Wait at least 1 ms according to SD spec */
  717. rt_thread_mdelay(1);
  718. mmcsd_power_up(host);
  719. mmcsd_select_voltage(host, ocr);
  720. }
  721. int mmcsd_wait_cd_changed(rt_int32_t timeout)
  722. {
  723. struct rt_mmcsd_host *host;
  724. if (rt_mb_recv(&mmcsd_hotpluge_mb, (rt_ubase_t *)&host, timeout) == RT_EOK)
  725. {
  726. if (host->card == RT_NULL)
  727. {
  728. return MMCSD_HOST_UNPLUGED;
  729. }
  730. else
  731. {
  732. return MMCSD_HOST_PLUGED;
  733. }
  734. }
  735. return -RT_ETIMEOUT;
  736. }
  737. RTM_EXPORT(mmcsd_wait_cd_changed);
  738. void mmcsd_change(struct rt_mmcsd_host *host)
  739. {
  740. rt_mb_send(&mmcsd_detect_mb, (rt_ubase_t)host);
  741. }
  742. void mmcsd_detect(void *param)
  743. {
  744. struct rt_mmcsd_host *host;
  745. rt_uint32_t ocr;
  746. rt_int32_t err;
  747. while (1)
  748. {
  749. if (rt_mb_recv(&mmcsd_detect_mb, (rt_ubase_t *)&host, RT_WAITING_FOREVER) == RT_EOK)
  750. {
  751. if (host->card == RT_NULL)
  752. {
  753. mmcsd_host_lock(host);
  754. mmcsd_power_up(host);
  755. mmcsd_go_idle(host);
  756. mmcsd_send_if_cond(host, host->valid_ocr);
  757. err = sdio_io_send_op_cond(host, 0, &ocr);
  758. if (!err)
  759. {
  760. if (init_sdio(host, ocr))
  761. mmcsd_power_off(host);
  762. mmcsd_host_unlock(host);
  763. continue;
  764. }
  765. /*
  766. * detect SD card
  767. */
  768. err = mmcsd_send_app_op_cond(host, 0, &ocr);
  769. if (!err)
  770. {
  771. if (init_sd(host, ocr))
  772. mmcsd_power_off(host);
  773. mmcsd_host_unlock(host);
  774. rt_mb_send(&mmcsd_hotpluge_mb, (rt_ubase_t)host);
  775. continue;
  776. }
  777. /*
  778. * detect mmc card
  779. */
  780. err = mmc_send_op_cond(host, 0, &ocr);
  781. if (!err)
  782. {
  783. if (init_mmc(host, ocr))
  784. mmcsd_power_off(host);
  785. mmcsd_host_unlock(host);
  786. rt_mb_send(&mmcsd_hotpluge_mb, (rt_ubase_t)host);
  787. continue;
  788. }
  789. mmcsd_host_unlock(host);
  790. }
  791. else
  792. {
  793. /* card removed */
  794. mmcsd_host_lock(host);
  795. if (host->card->sdio_function_num != 0)
  796. {
  797. LOG_W("unsupport sdio card plug out!");
  798. }
  799. else
  800. {
  801. rt_mmcsd_blk_remove(host->card);
  802. rt_free(host->card);
  803. host->card = RT_NULL;
  804. }
  805. mmcsd_host_unlock(host);
  806. rt_mb_send(&mmcsd_hotpluge_mb, (rt_ubase_t)host);
  807. }
  808. }
  809. }
  810. }
  811. void mmcsd_host_init(struct rt_mmcsd_host *host)
  812. {
  813. rt_memset(host, 0, sizeof(struct rt_mmcsd_host));
  814. strncpy(host->name, "sd", sizeof(host->name) - 1);
  815. host->max_seg_size = 65535;
  816. host->max_dma_segs = 1;
  817. host->max_blk_size = 512;
  818. host->max_blk_count = 4096;
  819. rt_mutex_init(&host->bus_lock, "sd_bus_lock", RT_IPC_FLAG_FIFO);
  820. rt_sem_init(&host->sem_ack, "sd_ack", 0, RT_IPC_FLAG_FIFO);
  821. }
  822. struct rt_mmcsd_host *mmcsd_alloc_host(void)
  823. {
  824. struct rt_mmcsd_host *host;
  825. host = rt_malloc(sizeof(struct rt_mmcsd_host));
  826. if (!host)
  827. {
  828. LOG_E("alloc host failed");
  829. return RT_NULL;
  830. }
  831. mmcsd_host_init(host);
  832. return host;
  833. }
  834. void mmcsd_free_host(struct rt_mmcsd_host *host)
  835. {
  836. rt_mutex_detach(&host->bus_lock);
  837. rt_sem_detach(&host->sem_ack);
  838. rt_free(host);
  839. }
  840. rt_int32_t mmcsd_excute_tuning(struct rt_mmcsd_card *card)
  841. {
  842. struct rt_mmcsd_host *host = card->host;
  843. rt_int32_t opcode;
  844. if (!host->ops->execute_tuning)
  845. return RT_EOK;
  846. if (card->card_type == CARD_TYPE_MMC)
  847. opcode = SEND_TUNING_BLOCK_HS200;
  848. else
  849. opcode = SEND_TUNING_BLOCK;
  850. return host->ops->execute_tuning(host, opcode);;
  851. }
  852. int rt_mmcsd_core_init(void)
  853. {
  854. rt_err_t ret;
  855. /* initialize detect SD cart thread */
  856. /* initialize mailbox and create detect SD card thread */
  857. ret = rt_mb_init(&mmcsd_detect_mb, "mmcsdmb",
  858. &mmcsd_detect_mb_pool[0], sizeof(mmcsd_detect_mb_pool) / sizeof(mmcsd_detect_mb_pool[0]),
  859. RT_IPC_FLAG_FIFO);
  860. RT_ASSERT(ret == RT_EOK);
  861. ret = rt_mb_init(&mmcsd_hotpluge_mb, "mmcsdhotplugmb",
  862. &mmcsd_hotpluge_mb_pool[0], sizeof(mmcsd_hotpluge_mb_pool) / sizeof(mmcsd_hotpluge_mb_pool[0]),
  863. RT_IPC_FLAG_FIFO);
  864. RT_ASSERT(ret == RT_EOK);
  865. ret = rt_thread_init(&mmcsd_detect_thread, "mmcsd_detect", mmcsd_detect, RT_NULL,
  866. &mmcsd_stack[0], RT_MMCSD_STACK_SIZE, RT_MMCSD_THREAD_PRIORITY, 20);
  867. if (ret == RT_EOK)
  868. {
  869. rt_thread_startup(&mmcsd_detect_thread);
  870. }
  871. rt_sdio_init();
  872. return 0;
  873. }
  874. INIT_PREV_EXPORT(rt_mmcsd_core_init);