drv_sdio.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. /*
  2. * File : drv_sdio.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2017, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2018-02-08 RT-Thread the first version
  23. */
  24. #include <rtthread.h>
  25. #include <rthw.h>
  26. #include <rtdevice.h>
  27. #include <string.h>
  28. #include "drv_sdio.h"
  29. #include "interrupt.h"
  30. #include "mmu.h"
  31. #include "drv_gpio.h"
  32. #include "drv_clock.h"
  33. #define DBG_ENABLE
  34. #define DBG_SECTION_NAME "MMC"
  35. // #define DBG_LEVEL DBG_LOG
  36. // #define DBG_LEVEL DBG_INFO
  37. #define DBG_LEVEL DBG_WARNING
  38. // #define DBG_LEVEL DBG_ERROR
  39. #define DBG_COLOR
  40. #include <rtdbg.h>
  41. #ifdef RT_USING_SDIO
  42. #define CONFIG_MMC_USE_DMA
  43. #define DMA_ALIGN (32U)
  44. struct mmc_xfe_des
  45. {
  46. rt_uint32_t size; /* block size */
  47. rt_uint32_t num; /* block num */
  48. rt_uint8_t *buff; /* buff addr */
  49. rt_uint32_t flag; /* write or read or stream */
  50. #define MMC_DATA_WRITE (1 << 0)
  51. #define MMC_DATA_READ (1 << 1)
  52. #define MMC_DATA_STREAM (1 << 2)
  53. };
  54. struct mmc_flag
  55. {
  56. volatile rt_uint32_t risr;
  57. volatile rt_uint32_t idst;
  58. };
  59. struct sdio_drv
  60. {
  61. struct rt_mmcsd_host *host;
  62. struct rt_mmcsd_req *req;
  63. struct rt_semaphore rt_sem;
  64. struct mmc_xfe_des xfe;
  65. struct mmc_flag flag;
  66. tina_mmc_t mmc_des;
  67. rt_uint8_t *mmc_buf;
  68. rt_uint8_t usedma;
  69. };
  70. #ifdef CONFIG_MMC_USE_DMA
  71. #ifdef TINA_USING_SDIO0
  72. ALIGN(32) static rt_uint8_t dma_buffer[64 * 1024];
  73. #endif
  74. #endif
  75. static void mmc_request_end(struct rt_mmcsd_host *host, struct rt_mmcsd_req *req);
  76. static void mmc_delay_us(int us)
  77. {
  78. volatile unsigned int temp;
  79. while (us--)
  80. {
  81. temp = 0x2f;
  82. while (temp--)
  83. {
  84. temp = temp;
  85. };
  86. }
  87. }
  88. static void mmc_dump_errinfo(unsigned int err)
  89. {
  90. dbg_log(DBG_ERROR, "[err]:0x%08x, %s%s%s%s%s%s%s%s%s%s%s\n",
  91. err,
  92. err & SDXC_RespErr ? " RE" : "",
  93. err & SDXC_RespCRCErr ? " RCE" : "",
  94. err & SDXC_DataCRCErr ? " DCE" : "",
  95. err & SDXC_RespTimeout ? " RTO" : "",
  96. err & SDXC_DataTimeout ? " DTO" : "",
  97. err & SDXC_DataStarve ? " DS" : "",
  98. err & SDXC_FIFORunErr ? " FE" : "",
  99. err & SDXC_HardWLocked ? " HL" : "",
  100. err & SDXC_StartBitErr ? " SBE" : "",
  101. err & SDXC_EndBitErr ? " EBE" : "",
  102. err == 0 ? " STO" : ""
  103. );
  104. }
  105. static int mmc_update_clk(tina_mmc_t mmc)
  106. {
  107. rt_uint32_t cmd;
  108. rt_uint32_t timeout = 2000000;
  109. /* cmd load */
  110. cmd = SDXC_LOAD_CMD | SDXC_UPDATE_CLOCK_CMD | SDXC_WAIT_OVER_CMD;
  111. mmc->cmdr_reg = cmd;
  112. /* while load success */
  113. while ((mmc->cmdr_reg & SDXC_LOAD_CMD) && (--timeout))
  114. {
  115. mmc_delay_us(1);
  116. }
  117. if (!timeout)
  118. {
  119. dbg_log(DBG_ERROR, "mmc update clk failed\n");
  120. return -RT_ERROR;
  121. }
  122. /* clean interrupt */
  123. mmc->risr_reg = mmc->risr_reg;
  124. return RT_EOK;
  125. }
  126. static rt_err_t mmc_trans_data_by_dma(tina_mmc_t mmc, struct mmc_xfe_des *xfe)
  127. {
  128. ALIGN(32) static struct mmc_des_v4p1 pdes[128]; // mast ALIGN(32)
  129. unsigned i, rval;
  130. unsigned des_idx;
  131. unsigned length = xfe->size * xfe->num;
  132. unsigned buff_frag_num = length >> SDXC_DES_NUM_SHIFT;
  133. unsigned remain = length & (SDXC_DES_BUFFER_MAX_LEN - 1);
  134. if (remain)
  135. {
  136. buff_frag_num ++;
  137. }
  138. else
  139. {
  140. remain = SDXC_DES_BUFFER_MAX_LEN;
  141. }
  142. memset(pdes, 0, sizeof(pdes));
  143. mmu_clean_dcache((rt_uint32_t)(xfe->buff), length);
  144. for (i = 0, des_idx = 0; i < buff_frag_num; i++, des_idx++)
  145. {
  146. // memset((void*)&pdes[des_idx], 0, sizeof(struct mmc_v4p1));
  147. pdes[des_idx].des_chain = 1;
  148. pdes[des_idx].own = 1;
  149. pdes[des_idx].dic = 1;
  150. if ((buff_frag_num > 1) && (i != buff_frag_num - 1))
  151. {
  152. pdes[des_idx].data_buf1_sz = SDXC_DES_BUFFER_MAX_LEN;
  153. }
  154. else
  155. {
  156. pdes[des_idx].data_buf1_sz = remain;
  157. }
  158. pdes[des_idx].buf_addr_ptr1 = (unsigned long)(xfe->buff) + i * SDXC_DES_BUFFER_MAX_LEN;
  159. if (i == 0)
  160. {
  161. pdes[des_idx].first_des = 1;
  162. }
  163. if (i == (buff_frag_num - 1))
  164. {
  165. pdes[des_idx].dic = 0;
  166. pdes[des_idx].last_des = 1;
  167. pdes[des_idx].end_of_ring = 1;
  168. pdes[des_idx].buf_addr_ptr2 = 0;
  169. }
  170. else
  171. {
  172. pdes[des_idx].buf_addr_ptr2 = (unsigned long)&pdes[des_idx+1];
  173. }
  174. dbg_log(DBG_LOG, "frag %d, remain %d, des[%d](%08x): " \
  175. "[0] = %08x, [1] = %08x, [2] = %08x, [3] = %08x\n", \
  176. i, remain, des_idx, (unsigned int)&pdes[des_idx],
  177. (unsigned int)((unsigned int*)&pdes[des_idx])[0], (unsigned int)((unsigned int*)&pdes[des_idx])[1],
  178. (unsigned int)((unsigned int*)&pdes[des_idx])[2], (unsigned int)((unsigned int*)&pdes[des_idx])[3]);
  179. }
  180. mmu_clean_dcache((rt_uint32_t)pdes, sizeof(struct mmc_des_v4p1) * (des_idx + 1));
  181. /*
  182. * GCTRLREG
  183. * GCTRL[2] : DMA reset
  184. * GCTRL[5] : DMA enable
  185. *
  186. * IDMACREG
  187. * IDMAC[0] : IDMA soft reset
  188. * IDMAC[1] : IDMA fix burst flag
  189. * IDMAC[7] : IDMA on
  190. *
  191. * IDIECREG
  192. * IDIE[0] : IDMA transmit interrupt flag
  193. * IDIE[1] : IDMA receive interrupt flag
  194. */
  195. rval = mmc->gctl_reg;
  196. mmc->gctl_reg = rval | (1 << 5) | (1 << 2); /* dma enable */
  197. mmc->dmac_reg = (1 << 0); /* idma reset */
  198. while(mmc->dmac_reg & 0x1) {}; /* wait idma reset done */
  199. mmc->dmac_reg = (1 << 1) | (1 << 7); /* idma on */
  200. rval = mmc->idie_reg & (~3);
  201. if (xfe->flag == MMC_DATA_WRITE)
  202. rval |= (1 << 0);
  203. else
  204. rval |= (1 << 1);
  205. mmc->idie_reg = rval;
  206. mmc->dlba_reg = (unsigned long)pdes;
  207. mmc->fwlr_reg = (2U << 28) | (7U << 16) | 8;
  208. return 0;
  209. }
  210. static rt_err_t mmc_trans_data_by_cpu(tina_mmc_t mmc, struct mmc_xfe_des *xfe)
  211. {
  212. unsigned i;
  213. unsigned byte_cnt = xfe->size * xfe->num;
  214. unsigned *buff = (unsigned *)(xfe->buff);
  215. volatile unsigned timeout = 2000000;
  216. if (xfe->flag == MMC_DATA_WRITE)
  217. {
  218. for (i = 0; i < (byte_cnt >> 2); i++)
  219. {
  220. while(--timeout && (mmc->star_reg & (1 << 3)));
  221. if (timeout <= 0)
  222. {
  223. dbg_log(DBG_ERROR, "write data by cpu failed status:0x%08x\n", mmc->star_reg);
  224. return -RT_ERROR;
  225. }
  226. mmc->fifo_reg = buff[i];
  227. timeout = 2000000;
  228. }
  229. }
  230. else
  231. {
  232. for (i = 0; i < (byte_cnt >> 2); i++)
  233. {
  234. while(--timeout && (mmc->star_reg & (1 << 2)));
  235. if (timeout <= 0)
  236. {
  237. dbg_log(DBG_ERROR, "read data by cpu failed status:0x%08x\n", mmc->star_reg);
  238. return -RT_ERROR;
  239. }
  240. buff[i] = mmc->fifo_reg;
  241. timeout = 2000000;
  242. }
  243. }
  244. return RT_EOK;
  245. }
  246. static rt_err_t mmc_config_clock(tina_mmc_t mmc, int clk)
  247. {
  248. rt_uint32_t rval = 0;
  249. /* disable card clock */
  250. rval = mmc->ckcr_reg;
  251. rval &= ~(1 << 16);
  252. mmc->ckcr_reg = rval;
  253. if (mmc_update_clk(mmc) != RT_EOK)
  254. {
  255. dbg_log(DBG_ERROR, "clk update fail line:%d\n", __LINE__);
  256. return -RT_ERROR;
  257. }
  258. if (mmc == MMC0)
  259. {
  260. mmc_set_clk(SDMMC0, clk);
  261. }
  262. else
  263. {
  264. mmc_set_clk(SDMMC1, clk);
  265. }
  266. /* Re-enable card clock */
  267. rval = mmc->ckcr_reg;
  268. rval |= (0x1 << 16); //(3 << 16);
  269. mmc->ckcr_reg = rval;
  270. if(mmc_update_clk(mmc) != RT_EOK)
  271. {
  272. dbg_log(DBG_ERROR, "clk update fail line:%d\n", __LINE__);
  273. return -RT_ERROR;
  274. }
  275. return RT_EOK;
  276. }
  277. static rt_err_t mmc_set_ios(tina_mmc_t mmc, int clk, int bus_width)
  278. {
  279. dbg_log(DBG_LOG, "mmc set io bus width:%d clock:%d\n", \
  280. (bus_width == MMCSD_BUS_WIDTH_8 ? 8 : (bus_width == MMCSD_BUS_WIDTH_4 ? 4 : 1)), clk);
  281. /* change clock */
  282. if (clk && (mmc_config_clock(mmc, clk) != RT_EOK))
  283. {
  284. dbg_log(DBG_ERROR, "update clock failed\n");
  285. return -RT_ERROR;
  286. }
  287. /* Change bus width */
  288. if (bus_width == MMCSD_BUS_WIDTH_8)
  289. {
  290. mmc->bwdr_reg = 2;
  291. }
  292. else if (bus_width == MMCSD_BUS_WIDTH_4)
  293. {
  294. mmc->bwdr_reg = 1;
  295. }
  296. else
  297. {
  298. mmc->bwdr_reg = 0;
  299. }
  300. return RT_EOK;
  301. }
  302. static int mmc_send_cmd(struct rt_mmcsd_host *host, struct rt_mmcsd_cmd *cmd)
  303. {
  304. unsigned int cmdval = 0x80000000;
  305. signed int timeout = 0;
  306. int err = 0;
  307. unsigned int status = 0;
  308. struct rt_mmcsd_data *data = cmd->data;
  309. unsigned int bytecnt = 0;
  310. struct sdio_drv *sdio_des = (struct sdio_drv *)host->private_data;
  311. tina_mmc_t mmc = sdio_des->mmc_des;
  312. timeout = 5000 * 1000;
  313. status = mmc->star_reg;
  314. while (status & (1 << 9))
  315. {
  316. dbg_log(DBG_LOG, "note: check card busy\n");
  317. status = mmc->star_reg;
  318. if (!timeout--)
  319. {
  320. err = -1;
  321. dbg_log(DBG_ERROR, "mmc cmd12 busy timeout data:0x%08x\n", status);
  322. return err;
  323. }
  324. mmc_delay_us(1);
  325. }
  326. /*
  327. * CMDREG
  328. * CMD[5:0] : Command index
  329. * CMD[6] : Has response
  330. * CMD[7] : Long response
  331. * CMD[8] : Check response CRC
  332. * CMD[9] : Has data
  333. * CMD[10] : Write
  334. * CMD[11] : Steam mode
  335. * CMD[12] : Auto stop
  336. * CMD[13] : Wait previous over
  337. * CMD[14] : About cmd
  338. * CMD[15] : Send initialization
  339. * CMD[21] : Update clock
  340. * CMD[31] : Load cmd
  341. */
  342. if (!cmd->cmd_code)
  343. cmdval |= (1 << 15);
  344. if (resp_type(cmd) != RESP_NONE)
  345. cmdval |= (1 << 6);
  346. if (resp_type(cmd) == RESP_R2)
  347. cmdval |= (1 << 7);
  348. if ((resp_type(cmd) != RESP_R3) && (resp_type(cmd) != RESP_R4))
  349. cmdval |= (1 << 8);
  350. if (data)
  351. {
  352. cmdval |= (1 << 9) | (1 << 13);
  353. if (data->flags & DATA_DIR_WRITE)
  354. cmdval |= (1 << 10);
  355. if (data->blks > 1)
  356. cmdval |= (1 << 12);
  357. mmc->bksr_reg = data->blksize;
  358. bytecnt = data->blksize * data->blks;
  359. mmc->bycr_reg = bytecnt;
  360. }
  361. dbg_log(DBG_LOG, "cmd %d(0x%08x), arg 0x%08x\n", cmd->cmd_code, cmdval | cmd->cmd_code, cmd->arg);
  362. mmc->cagr_reg = cmd->arg;
  363. if (!data)
  364. {
  365. mmc->cmdr_reg = cmdval | cmd->cmd_code;
  366. mmc->imkr_reg |= 0x1 << 2;
  367. }
  368. /*
  369. * transfer data and check status
  370. * STATREG[2] : FIFO empty
  371. * STATREG[3] : FIFO full
  372. */
  373. if (data)
  374. {
  375. dbg_log(DBG_LOG, "mmc trans data %d bytes addr:0x%08x\n", bytecnt, data);
  376. #ifdef CONFIG_MMC_USE_DMA
  377. if (bytecnt > 64)
  378. {
  379. #else
  380. if (0)
  381. {
  382. #endif
  383. sdio_des->usedma = 1;
  384. mmc->gctl_reg = mmc->gctl_reg & (~0x80000000);
  385. mmc_trans_data_by_dma(mmc, &sdio_des->xfe);
  386. mmc->cmdr_reg = cmdval | cmd->cmd_code;
  387. }
  388. else
  389. {
  390. sdio_des->usedma = 0;
  391. mmc->gctl_reg = mmc->gctl_reg | 0x80000000;
  392. mmc->cmdr_reg = cmdval | cmd->cmd_code;
  393. mmc_trans_data_by_cpu(mmc, &sdio_des->xfe);
  394. }
  395. if (data->blks > 1)
  396. {
  397. mmc->imkr_reg |= (0x1 << 14);
  398. }
  399. else
  400. {
  401. mmc->imkr_reg |= (0x1 << 3);
  402. }
  403. }
  404. mmc->imkr_reg |= 0xbfc2;
  405. if (data)
  406. {
  407. //TODO:2 * bytecnt * 4?
  408. timeout = sdio_des->usedma ? (2 * bytecnt * 4) : 100; //0.04us(25M)*2(4bit width)*25()
  409. if (timeout < 10)
  410. {
  411. timeout = 10;
  412. }
  413. }
  414. else
  415. {
  416. timeout = 200;
  417. }
  418. if (rt_sem_take(&sdio_des->rt_sem, timeout) != RT_EOK)
  419. {
  420. err = (mmc->risr_reg | sdio_des->flag.risr) & 0xbfc2;
  421. goto out;
  422. }
  423. err = (mmc->risr_reg | sdio_des->flag.risr) & 0xbfc2;
  424. if (err)
  425. {
  426. cmd->err = -RT_ETIMEOUT;
  427. goto out;
  428. }
  429. if (resp_type(cmd) == RESP_R2)
  430. {
  431. cmd->resp[3] = mmc->resp0_reg;
  432. cmd->resp[2] = mmc->resp1_reg;
  433. cmd->resp[1] = mmc->resp2_reg;
  434. cmd->resp[0] = mmc->resp3_reg;
  435. dbg_log(DBG_LOG, "mmc resp 0x%08x 0x%08x 0x%08x 0x%08x\n",
  436. cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);
  437. }
  438. else
  439. {
  440. cmd->resp[0] = mmc->resp0_reg;
  441. dbg_log(DBG_LOG, "mmc resp 0x%08x\n", cmd->resp[0]);
  442. }
  443. out:
  444. if (err)
  445. {
  446. mmc_dump_errinfo(err & 0xbfc2);
  447. }
  448. if (data && sdio_des->usedma)
  449. {
  450. /* IDMASTAREG
  451. * IDST[0] : idma tx int
  452. * IDST[1] : idma rx int
  453. * IDST[2] : idma fatal bus error
  454. * IDST[4] : idma descriptor invalid
  455. * IDST[5] : idma error summary
  456. * IDST[8] : idma normal interrupt sumary
  457. * IDST[9] : idma abnormal interrupt sumary
  458. */
  459. status = mmc->idst_reg;
  460. mmc->idst_reg = status;
  461. mmc->idie_reg = 0;
  462. mmc->dmac_reg = 0;
  463. mmc->gctl_reg = mmc->gctl_reg & (~(1 << 5));
  464. }
  465. if (err)
  466. {
  467. if (data && (data->flags & DATA_DIR_READ) && (bytecnt == 512))
  468. {
  469. mmc->gctl_reg = mmc->gctl_reg | 0x80000000;
  470. mmc->dbgc_reg = 0xdeb;
  471. timeout = 1000;
  472. dbg_log(DBG_LOG, "Read remain data\n");
  473. while (mmc->bbcr_reg < 512)
  474. {
  475. unsigned int tmp = mmc->fifo_reg;
  476. tmp = tmp;
  477. dbg_log(DBG_LOG, "Read data 0x%08x, bbcr 0x%04x\n", tmp, mmc->bbcr_reg);
  478. mmc_delay_us(1);
  479. if (!(timeout--))
  480. {
  481. dbg_log(DBG_ERROR, "Read remain data timeout\n");
  482. break;
  483. }
  484. }
  485. }
  486. mmc->gctl_reg = 0x7;
  487. while (mmc->gctl_reg & 0x7) { };
  488. mmc_update_clk(mmc);
  489. cmd->err = -RT_ETIMEOUT;
  490. dbg_log(DBG_ERROR, "mmc cmd %d err\n", cmd->cmd_code);
  491. }
  492. mmc->gctl_reg &= ~(0x1 << 4);
  493. mmc->imkr_reg &= ~0xffff;
  494. mmc->risr_reg = 0xffffffff;
  495. mmc->gctl_reg |= 0x1 << 4;
  496. while (!rt_sem_take(&sdio_des->rt_sem, 0)) {}
  497. mmc_request_end(sdio_des->host, sdio_des->req);
  498. return err;
  499. }
  500. static void mmc_request_end(struct rt_mmcsd_host *host, struct rt_mmcsd_req *req)
  501. {
  502. struct rt_mmcsd_data *data;
  503. unsigned byte_cnt;
  504. struct sdio_drv *sdio = (struct sdio_drv *)host->private_data;
  505. #ifdef CONFIG_MMC_USE_DMA
  506. data = req->cmd->data;
  507. if (data)
  508. {
  509. byte_cnt = data->blksize * data->blks;
  510. if ((byte_cnt > 64) && (data->flags & DATA_DIR_READ))
  511. {
  512. mmu_invalidate_dcache((rt_uint32_t)sdio->xfe.buff, (rt_uint32_t)byte_cnt);
  513. if (((rt_uint32_t)data->buf) & (DMA_ALIGN - 1))
  514. {
  515. memcpy(data->buf, sdio->xfe.buff, byte_cnt);
  516. }
  517. }
  518. }
  519. #endif
  520. mmcsd_req_complete(host);
  521. }
  522. static void sdio_request_send(struct rt_mmcsd_host *host, struct rt_mmcsd_req *req)
  523. {
  524. struct rt_mmcsd_data *data;
  525. int byte_cnt;
  526. struct sdio_drv *sdio;
  527. sdio = (struct sdio_drv *)host->private_data;
  528. sdio->req = req;
  529. data = req->cmd->data;
  530. if (data)
  531. {
  532. sdio->xfe.size = data->blksize;
  533. sdio->xfe.num = data->blks;
  534. sdio->xfe.buff = (rt_uint8_t *)data->buf;
  535. sdio->xfe.flag = (data->flags & DATA_DIR_WRITE) ? \
  536. MMC_DATA_WRITE : MMC_DATA_READ;
  537. #ifdef CONFIG_MMC_USE_DMA
  538. byte_cnt = data->blksize * data->blks;
  539. if ((byte_cnt > 64) && (((rt_uint32_t)data->buf) & (DMA_ALIGN - 1)))
  540. {
  541. sdio->xfe.buff = (rt_uint8_t *)sdio->mmc_buf;
  542. if (data->flags & DATA_DIR_WRITE)
  543. {
  544. memcpy(sdio->mmc_buf, data->buf, byte_cnt);
  545. mmu_clean_dcache((rt_uint32_t)sdio->mmc_buf, (rt_uint32_t)byte_cnt);
  546. }
  547. }
  548. #endif
  549. }
  550. memset(&sdio->flag, 0, sizeof(struct mmc_flag));
  551. mmc_send_cmd(host, req->cmd);
  552. return;
  553. }
  554. static void sdio_set_iocfg(struct rt_mmcsd_host *host, struct rt_mmcsd_io_cfg *io_cfg)
  555. {
  556. int clk = io_cfg->clock;
  557. int width = io_cfg->bus_width;
  558. struct sdio_drv *sdio_des = (struct sdio_drv *)host->private_data;
  559. tina_mmc_t mmc = sdio_des->mmc_des;
  560. mmc_set_ios(mmc, clk, width);
  561. }
  562. static const struct rt_mmcsd_host_ops ops =
  563. {
  564. sdio_request_send,
  565. sdio_set_iocfg,
  566. RT_NULL,
  567. RT_NULL,
  568. };
  569. static void sdio_interrupt_handle(int irqno, void *param)
  570. {
  571. rt_uint32_t risr, idst;
  572. rt_uint32_t status;
  573. struct sdio_drv *sdio_des = (struct sdio_drv *)param;
  574. struct rt_mmcsd_data *data = sdio_des->req->cmd->data;
  575. tina_mmc_t mmc = sdio_des->mmc_des;
  576. risr = mmc->risr_reg;
  577. idst = mmc->idst_reg;
  578. mmc->risr_reg = risr & mmc->imkr_reg;
  579. mmc->idst_reg = idst & mmc->idie_reg;
  580. sdio_des->flag.risr |= risr;
  581. sdio_des->flag.idst |= idst;
  582. if (data)
  583. {
  584. int done = 0;
  585. status = sdio_des->flag.risr | mmc->risr_reg;
  586. if (data->blks > 1)//not wait auto stop when MMC_CMD_MANUAL is set
  587. {
  588. if (sdio_des->usedma)
  589. done = ((status & (1 << 14)) && (sdio_des->flag.idst & 0x3)) ? 1 : 0;
  590. else
  591. done = status & (1 << 14);
  592. }
  593. else
  594. {
  595. if (sdio_des->usedma)
  596. done = ((status & (1 << 3)) && (sdio_des->flag.idst & 0x3)) ? 1 : 0;
  597. else
  598. done = status & (1 << 3);
  599. }
  600. if (done)
  601. {
  602. rt_sem_release(&sdio_des->rt_sem);
  603. }
  604. }
  605. else
  606. {
  607. rt_sem_release(&sdio_des->rt_sem);
  608. }
  609. }
  610. static void sdio_gpio_init(struct sdio_drv *sdio_des)
  611. {
  612. int pin;
  613. if ((rt_uint32_t)sdio_des->mmc_des == MMC0_BASE_ADDR)
  614. {
  615. /* SDC0: PF0-PF5 */
  616. for (pin = GPIO_PIN_0; pin <= GPIO_PIN_5; pin++)
  617. {
  618. gpio_set_func(GPIO_PORT_F, pin, IO_FUN_1);
  619. gpio_set_pull_mode(GPIO_PORT_F, pin, PULL_UP);
  620. gpio_set_drive_level(GPIO_PORT_F, pin, DRV_LEVEL_2);
  621. }
  622. }
  623. else if ((rt_uint32_t)sdio_des->mmc_des == MMC1_BASE_ADDR)
  624. {
  625. //todo: config gpio port
  626. RT_ASSERT(0);
  627. }
  628. }
  629. static void sdio_clk_io_on(struct sdio_drv *sdio_des)
  630. {
  631. if ((rt_uint32_t)sdio_des->mmc_des == MMC0_BASE_ADDR)
  632. {
  633. CCU->bus_clk_gating0 |= 0x1 << 8;
  634. CCU->bus_soft_rst0 |= 0x1 << 8;
  635. }
  636. else if ((rt_uint32_t)sdio_des->mmc_des == MMC1_BASE_ADDR)
  637. {
  638. CCU->bus_clk_gating0 |= 0x1 << 9;
  639. CCU->bus_soft_rst0 |= 0x1 << 9;
  640. }
  641. mmc_set_clk(SDMMC0, 24000000);
  642. }
  643. static void sdio_irq_init(void *param)
  644. {
  645. struct sdio_drv *sdio_des = (struct sdio_drv *)param;
  646. if ((rt_uint32_t)sdio_des->mmc_des == MMC0_BASE_ADDR)
  647. {
  648. rt_hw_interrupt_install(SDC0_INTERRUPT, sdio_interrupt_handle, param, "mmc0_irq");
  649. rt_hw_interrupt_umask(SDC0_INTERRUPT);
  650. }
  651. else if ((rt_uint32_t)sdio_des->mmc_des == MMC1_BASE_ADDR)
  652. {
  653. rt_hw_interrupt_install(SDC1_INTERRUPT, sdio_interrupt_handle, param, "mmc1_irq");
  654. rt_hw_interrupt_umask(SDC1_INTERRUPT);
  655. }
  656. sdio_des->mmc_des->gctl_reg |= (0x1 << 4);
  657. }
  658. int tina_sdio_init(void)
  659. {
  660. struct rt_mmcsd_host *host;
  661. #ifdef TINA_USING_SDIO0
  662. {
  663. static struct sdio_drv _sdio_drv;
  664. host = mmcsd_alloc_host();
  665. if (!host)
  666. {
  667. dbg_log(DBG_ERROR, "alloc host failed\n");
  668. goto err;
  669. }
  670. if (rt_sem_init(&_sdio_drv.rt_sem, "sdio_sem", RT_NULL, RT_IPC_FLAG_FIFO))
  671. {
  672. dbg_log(DBG_ERROR, "sem init failed\n");
  673. goto err;
  674. }
  675. _sdio_drv.mmc_des = (tina_mmc_t)MMC0_BASE_ADDR;
  676. _sdio_drv.mmc_buf = dma_buffer;
  677. //init gpio pin
  678. sdio_gpio_init(&_sdio_drv);
  679. //clk is on
  680. sdio_clk_io_on(&_sdio_drv);
  681. //irq init
  682. sdio_irq_init(&_sdio_drv);
  683. host->ops = &ops;
  684. host->freq_min = 400 * 1000;
  685. host->freq_max = 50 * 1000 * 1000;
  686. host->valid_ocr = VDD_26_27 | VDD_27_28 | VDD_28_29 | VDD_29_30 | VDD_30_31 | VDD_31_32 |
  687. VDD_32_33 | VDD_33_34 | VDD_34_35 | VDD_35_36;
  688. host->flags = MMCSD_BUSWIDTH_4 | MMCSD_MUTBLKWRITE | MMCSD_SUP_SDIO_IRQ | MMCSD_SUP_HIGHSPEED;
  689. host->max_seg_size = 2048;
  690. host->max_dma_segs = 10;
  691. host->max_blk_size = 512;
  692. host->max_blk_count = 4096;
  693. host->private_data = &_sdio_drv;
  694. _sdio_drv.host = host;
  695. mmcsd_change(host);
  696. }
  697. #endif
  698. return RT_EOK;
  699. err:
  700. if (host)
  701. {
  702. rt_free(host);
  703. }
  704. return RT_ERROR;
  705. }
  706. INIT_APP_EXPORT(tina_sdio_init);
  707. #endif