drv_sdio.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. #include <rthw.h>
  2. #include <rtthread.h>
  3. #include <rtdevice.h>
  4. #include <drivers/mmcsd_core.h>
  5. #include <stdint.h>
  6. #include <stdio.h>
  7. #include "drv_sdio.h"
  8. #ifdef RT_USING_SDIO
  9. #define MMC_BASE_ADDR (0x10005000)
  10. #define PL180_POWER (0x00)
  11. #define PL180_CLOCK (0x04)
  12. #define PL180_ARGUMENT (0x08)
  13. #define PL180_COMMAND (0x0c)
  14. #define PL180_RESPCMD (0x10)
  15. #define PL180_RESP0 (0x14)
  16. #define PL180_RESP1 (0x18)
  17. #define PL180_RESP2 (0x1c)
  18. #define PL180_RESP3 (0x20)
  19. #define PL180_DATA_TIMER (0x24)
  20. #define PL180_DATA_LENGTH (0x28)
  21. #define PL180_DATA_CTRL (0x2c)
  22. #define PL180_DATA_CNT (0x30)
  23. #define PL180_STATUS (0x34)
  24. #define PL180_CLEAR (0x38)
  25. #define PL180_MASK0 (0x3c)
  26. #define PL180_MASK1 (0x40)
  27. #define PL180_SELECT (0x44)
  28. #define PL180_FIFO_CNT (0x48)
  29. #define PL180_FIFO (0x80)
  30. #define PL180_RSP_NONE (0 << 0)
  31. #define PL180_RSP_PRESENT (1 << 0)
  32. #define PL180_RSP_136BIT (1 << 1)
  33. #define PL180_RSP_CRC (1 << 2)
  34. #define PL180_CMD_WAITRESP (1 << 6)
  35. #define PL180_CMD_LONGRSP (1 << 7)
  36. #define PL180_CMD_WAITINT (1 << 8)
  37. #define PL180_CMD_WAITPEND (1 << 9)
  38. #define PL180_CMD_ENABLE (1 << 10)
  39. #define PL180_STAT_CMD_CRC_FAIL (1 << 0)
  40. #define PL180_STAT_DAT_CRC_FAIL (1 << 1)
  41. #define PL180_STAT_CMD_TIME_OUT (1 << 2)
  42. #define PL180_STAT_DAT_TIME_OUT (1 << 3)
  43. #define PL180_STAT_TX_UNDERRUN (1 << 4)
  44. #define PL180_STAT_RX_OVERRUN (1 << 5)
  45. #define PL180_STAT_CMD_RESP_END (1 << 6)
  46. #define PL180_STAT_CMD_SENT (1 << 7)
  47. #define PL180_STAT_DAT_END (1 << 8)
  48. #define PL180_STAT_DAT_BLK_END (1 << 10)
  49. #define PL180_STAT_CMD_ACT (1 << 11)
  50. #define PL180_STAT_TX_ACT (1 << 12)
  51. #define PL180_STAT_RX_ACT (1 << 13)
  52. #define PL180_STAT_TX_FIFO_HALF (1 << 14)
  53. #define PL180_STAT_RX_FIFO_HALF (1 << 15)
  54. #define PL180_STAT_TX_FIFO_FULL (1 << 16)
  55. #define PL180_STAT_RX_FIFO_FULL (1 << 17)
  56. #define PL180_STAT_TX_FIFO_ZERO (1 << 18)
  57. #define PL180_STAT_RX_DAT_ZERO (1 << 19)
  58. #define PL180_STAT_TX_DAT_AVL (1 << 20)
  59. #define PL180_STAT_RX_FIFO_AVL (1 << 21)
  60. #define PL180_CLR_CMD_CRC_FAIL (1 << 0)
  61. #define PL180_CLR_DAT_CRC_FAIL (1 << 1)
  62. #define PL180_CLR_CMD_TIMEOUT (1 << 2)
  63. #define PL180_CLR_DAT_TIMEOUT (1 << 3)
  64. #define PL180_CLR_TX_UNDERRUN (1 << 4)
  65. #define PL180_CLR_RX_OVERRUN (1 << 5)
  66. #define PL180_CLR_CMD_RESP_END (1 << 6)
  67. #define PL180_CLR_CMD_SENT (1 << 7)
  68. #define PL180_CLR_DAT_END (1 << 8)
  69. #define PL180_CLR_DAT_BLK_END (1 << 10)
  70. #define DBG_LEVEL DBG_LOG
  71. // #define DBG_ENABLE
  72. #define DBG_COLOR
  73. #include "rtdbg.h"
  74. struct sdhci_pl180_pdata_t
  75. {
  76. rt_uint32_t virt;
  77. };
  78. static inline rt_uint32_t read32(uint32_t addr)
  79. {
  80. return( *((volatile rt_uint32_t *)(addr)) );
  81. }
  82. static inline void write32(uint32_t addr, rt_uint32_t value)
  83. {
  84. *((volatile rt_uint32_t *)(addr)) = value;
  85. }
  86. static rt_err_t pl180_transfer_command(struct sdhci_pl180_pdata_t * pdat, struct sdhci_cmd_t * cmd)
  87. {
  88. rt_uint32_t cmdidx;
  89. rt_uint32_t status;
  90. rt_err_t ret = RT_EOK;
  91. if(read32(pdat->virt + PL180_COMMAND) & PL180_CMD_ENABLE)
  92. write32(pdat->virt + PL180_COMMAND, 0x0);
  93. cmdidx = (cmd->cmdidx & 0xff) | PL180_CMD_ENABLE;
  94. if(cmd->resptype)
  95. {
  96. cmdidx |= PL180_CMD_WAITRESP;
  97. if(cmd->resptype & PL180_RSP_136BIT)
  98. cmdidx |= PL180_CMD_LONGRSP;
  99. }
  100. write32(pdat->virt + PL180_ARGUMENT, cmd->cmdarg);
  101. write32(pdat->virt + PL180_COMMAND, cmdidx);
  102. do {
  103. status = read32(pdat->virt + PL180_STATUS);
  104. } while(!(status & (PL180_STAT_CMD_SENT | PL180_STAT_CMD_RESP_END | PL180_STAT_CMD_TIME_OUT | PL180_STAT_CMD_CRC_FAIL)));
  105. dbg_log(DBG_LOG, "mmc status done!\n");
  106. if(cmd->resptype & PL180_RSP_PRESENT)
  107. {
  108. cmd->response[0] = read32(pdat->virt + PL180_RESP0);
  109. if(cmd->resptype & PL180_RSP_136BIT)
  110. {
  111. dbg_log(DBG_LOG, "136bit response\n");
  112. cmd->response[1] = read32(pdat->virt + PL180_RESP1);
  113. cmd->response[2] = read32(pdat->virt + PL180_RESP2);
  114. cmd->response[3] = read32(pdat->virt + PL180_RESP3);
  115. }
  116. }
  117. if(status & PL180_STAT_CMD_TIME_OUT)
  118. {
  119. ret = -RT_ETIMEOUT;
  120. }
  121. else if ((status & PL180_STAT_CMD_CRC_FAIL) && (cmd->resptype & PL180_RSP_CRC))
  122. {
  123. ret = -RT_ERROR;
  124. }
  125. write32(pdat->virt + PL180_CLEAR, (PL180_CLR_CMD_SENT | PL180_CLR_CMD_RESP_END | PL180_CLR_CMD_TIMEOUT | PL180_CLR_CMD_CRC_FAIL));
  126. return ret;
  127. }
  128. static rt_err_t read_bytes(struct sdhci_pl180_pdata_t * pdat, rt_uint32_t * buf, rt_uint32_t blkcount, rt_uint32_t blksize)
  129. {
  130. rt_uint32_t * tmp = buf;
  131. rt_uint32_t count = blkcount * blksize;
  132. rt_uint32_t status, err;
  133. status = read32(pdat->virt + PL180_STATUS);
  134. err = status & (PL180_STAT_DAT_CRC_FAIL | PL180_STAT_DAT_TIME_OUT | PL180_STAT_RX_OVERRUN);
  135. while((!err) && (count >= sizeof(rt_uint32_t)))
  136. {
  137. if(status & PL180_STAT_RX_FIFO_AVL)
  138. {
  139. *(tmp) = read32(pdat->virt + PL180_FIFO);
  140. tmp++;
  141. count -= sizeof(rt_uint32_t);
  142. }
  143. status = read32(pdat->virt + PL180_STATUS);
  144. err = status & (PL180_STAT_DAT_CRC_FAIL | PL180_STAT_DAT_TIME_OUT | PL180_STAT_RX_OVERRUN);
  145. }
  146. err = status & (PL180_STAT_DAT_CRC_FAIL | PL180_STAT_DAT_TIME_OUT | PL180_STAT_DAT_BLK_END | PL180_STAT_RX_OVERRUN);
  147. while(!err)
  148. {
  149. status = read32(pdat->virt + PL180_STATUS);
  150. err = status & (PL180_STAT_DAT_CRC_FAIL | PL180_STAT_DAT_TIME_OUT | PL180_STAT_DAT_BLK_END | PL180_STAT_RX_OVERRUN);
  151. }
  152. if(status & PL180_STAT_DAT_TIME_OUT)
  153. return -RT_ERROR;
  154. else if (status & PL180_STAT_DAT_CRC_FAIL)
  155. return -RT_ERROR;
  156. else if (status & PL180_STAT_RX_OVERRUN)
  157. return -RT_ERROR;
  158. write32(pdat->virt + PL180_CLEAR, 0x1DC007FF);
  159. if(count)
  160. return -RT_ERROR;
  161. return RT_EOK;
  162. }
  163. static rt_err_t write_bytes(struct sdhci_pl180_pdata_t * pdat, rt_uint32_t * buf, rt_uint32_t blkcount, rt_uint32_t blksize)
  164. {
  165. rt_uint32_t * tmp = buf;
  166. rt_uint32_t count = blkcount * blksize;
  167. rt_uint32_t status, err;
  168. int i;
  169. status = read32(pdat->virt + PL180_STATUS);
  170. err = status & (PL180_STAT_DAT_CRC_FAIL | PL180_STAT_DAT_TIME_OUT);
  171. while(!err && count)
  172. {
  173. if(status & PL180_STAT_TX_FIFO_HALF)
  174. {
  175. if(count >= 8 * sizeof(rt_uint32_t))
  176. {
  177. for(i = 0; i < 8; i++)
  178. write32(pdat->virt + PL180_FIFO, *(tmp + i));
  179. tmp += 8;
  180. count -= 8 * sizeof(rt_uint32_t);
  181. }
  182. else
  183. {
  184. while(count >= sizeof(rt_uint32_t))
  185. {
  186. write32(pdat->virt + PL180_FIFO, *tmp);
  187. tmp++;
  188. count -= sizeof(rt_uint32_t);
  189. }
  190. }
  191. }
  192. status = read32(pdat->virt + PL180_STATUS);
  193. err = status & (PL180_STAT_DAT_CRC_FAIL | PL180_STAT_DAT_TIME_OUT);
  194. }
  195. err = status & (PL180_STAT_DAT_CRC_FAIL | PL180_STAT_DAT_TIME_OUT | PL180_STAT_DAT_BLK_END);
  196. while(!err)
  197. {
  198. status = read32(pdat->virt + PL180_STATUS);
  199. err = status & (PL180_STAT_DAT_CRC_FAIL | PL180_STAT_DAT_TIME_OUT | PL180_STAT_DAT_BLK_END);
  200. }
  201. if(status & PL180_STAT_DAT_TIME_OUT)
  202. return -RT_ERROR;
  203. else if (status & PL180_STAT_DAT_CRC_FAIL)
  204. return -RT_ERROR;
  205. write32(pdat->virt + PL180_CLEAR, 0x1DC007FF);
  206. if(count)
  207. return -RT_ERROR;
  208. return RT_EOK;
  209. }
  210. static rt_err_t pl180_transfer_data(struct sdhci_pl180_pdata_t * pdat, struct sdhci_cmd_t * cmd, struct sdhci_data_t * dat)
  211. {
  212. rt_uint32_t dlen = (rt_uint32_t)(dat->blkcnt * dat->blksz);
  213. rt_uint32_t blksz_bits = dat->blksz - 1;
  214. rt_uint32_t dctrl = (blksz_bits << 4) | (0x1 << 0) | (0x1 << 14);
  215. rt_err_t ret = -RT_ERROR;
  216. write32(pdat->virt + PL180_DATA_TIMER, 0xffff);
  217. write32(pdat->virt + PL180_DATA_LENGTH, dlen);
  218. if(dat->flag & DATA_DIR_READ)
  219. {
  220. dctrl |= (0x1 << 1);
  221. write32(pdat->virt + PL180_DATA_CTRL, dctrl);
  222. ret = pl180_transfer_command(pdat, cmd);
  223. if (ret < 0) return ret;
  224. ret = read_bytes(pdat, (rt_uint32_t *)dat->buf, dat->blkcnt, dat->blksz);
  225. }
  226. else if(dat->flag & DATA_DIR_WRITE)
  227. {
  228. ret = pl180_transfer_command(pdat, cmd);
  229. if (ret < 0) return ret;
  230. write32(pdat->virt + PL180_DATA_CTRL, dctrl);
  231. ret = write_bytes(pdat, (rt_uint32_t *)dat->buf, dat->blkcnt, dat->blksz);
  232. }
  233. return ret;
  234. }
  235. static rt_err_t sdhci_pl180_detect(struct sdhci_t * sdhci)
  236. {
  237. return RT_EOK;
  238. }
  239. static rt_err_t sdhci_pl180_setwidth(struct sdhci_t * sdhci, rt_uint32_t width)
  240. {
  241. return RT_EOK;
  242. }
  243. static rt_err_t sdhci_pl180_setclock(struct sdhci_t * sdhci, rt_uint32_t clock)
  244. {
  245. rt_uint32_t temp = 0;
  246. struct sdhci_pl180_pdata_t * pdat = (struct sdhci_pl180_pdata_t *)sdhci->priv;
  247. if(clock)
  248. {
  249. temp = read32(pdat->virt + PL180_CLOCK) | (0x1<<8);
  250. temp = temp; // skip warning
  251. write32(pdat->virt + PL180_CLOCK, 0x100);
  252. }
  253. else
  254. {
  255. //write32(pdat->virt + PL180_CLOCK, read32(pdat->virt + PL180_CLOCK) & (~(0x1<<8)));
  256. }
  257. return RT_EOK;
  258. }
  259. static rt_err_t sdhci_pl180_transfer(struct sdhci_t * sdhci, struct sdhci_cmd_t * cmd, struct sdhci_data_t * dat)
  260. {
  261. struct sdhci_pl180_pdata_t * pdat = (struct sdhci_pl180_pdata_t *)sdhci->priv;
  262. if(!dat)
  263. return pl180_transfer_command(pdat, cmd);
  264. return pl180_transfer_data(pdat, cmd, dat);
  265. }
  266. static void mmc_request_send(struct rt_mmcsd_host *host, struct rt_mmcsd_req *req)
  267. {
  268. struct sdhci_t *sdhci = (struct sdhci_t *)host->private_data;
  269. struct sdhci_cmd_t cmd;
  270. struct sdhci_cmd_t stop;
  271. struct sdhci_data_t dat;
  272. rt_memset(&cmd, 0, sizeof(struct sdhci_cmd_t));
  273. rt_memset(&stop, 0, sizeof(struct sdhci_cmd_t));
  274. rt_memset(&dat, 0, sizeof(struct sdhci_data_t));
  275. cmd.cmdidx = req->cmd->cmd_code;
  276. cmd.cmdarg = req->cmd->arg;
  277. if (req->cmd->flags & RESP_MASK)
  278. {
  279. cmd.resptype = PL180_RSP_PRESENT;
  280. if (resp_type(req->cmd) == RESP_R2)
  281. cmd.resptype |= PL180_RSP_136BIT;
  282. }
  283. else
  284. cmd.resptype = 0;
  285. if(req->data)
  286. {
  287. dat.buf = (rt_uint8_t *)req->data->buf;
  288. dat.flag = req->data->flags;
  289. dat.blksz = req->data->blksize;
  290. dat.blkcnt = req->data->blks;
  291. req->cmd->err = sdhci_pl180_transfer(sdhci, &cmd, &dat);
  292. }
  293. else
  294. {
  295. req->cmd->err = sdhci_pl180_transfer(sdhci, &cmd, RT_NULL);
  296. }
  297. dbg_log(DBG_INFO, "cmdarg:%d\n", cmd.cmdarg);
  298. dbg_log(DBG_INFO, "cmdidx:%d\n", cmd.cmdidx);
  299. dbg_log(DBG_INFO, "[0]:0x%08x [1]:0x%08x [2]:0x%08x [3]:0x%08x\n", cmd.response[0], cmd.response[1], cmd.response[2], cmd.response[3]);
  300. req->cmd->resp[3] = cmd.response[3];
  301. req->cmd->resp[2] = cmd.response[2];
  302. req->cmd->resp[1] = cmd.response[1];
  303. req->cmd->resp[0] = cmd.response[0];
  304. if(req->cmd->err)
  305. dbg_log(DBG_ERROR, "transfer cmd err \n");
  306. if (req->stop)
  307. {
  308. stop.cmdidx = req->stop->cmd_code;
  309. stop.cmdarg = req->stop->arg;
  310. if (req->stop->flags & RESP_MASK)
  311. {
  312. stop.resptype = PL180_RSP_PRESENT;
  313. if (resp_type(req->stop) == RESP_R2)
  314. stop.resptype |= PL180_RSP_136BIT;
  315. }
  316. else
  317. stop.resptype = 0;
  318. req->stop->err = sdhci_pl180_transfer(sdhci, &stop, RT_NULL);
  319. }
  320. mmcsd_req_complete(host);
  321. }
  322. static void mmc_set_iocfg(struct rt_mmcsd_host *host, struct rt_mmcsd_io_cfg *io_cfg)
  323. {
  324. struct sdhci_t * sdhci = (struct sdhci_t *)host->private_data;
  325. sdhci_pl180_setclock(sdhci, io_cfg->clock);
  326. sdhci_pl180_setwidth(sdhci, io_cfg->bus_width);
  327. dbg_log(DBG_INFO, "clock:%d bus_width:%d\n", io_cfg->clock, io_cfg->bus_width);
  328. }
  329. static const struct rt_mmcsd_host_ops ops =
  330. {
  331. mmc_request_send,
  332. mmc_set_iocfg,
  333. RT_NULL,
  334. RT_NULL,
  335. };
  336. int pl180_init(void)
  337. {
  338. rt_uint32_t virt;
  339. rt_uint32_t id;
  340. struct rt_mmcsd_host * host = RT_NULL;
  341. struct sdhci_pl180_pdata_t * pdat = RT_NULL;
  342. struct sdhci_t * sdhci = RT_NULL;
  343. host = mmcsd_alloc_host();
  344. if (!host)
  345. {
  346. dbg_log(DBG_ERROR, "alloc host failed\n");
  347. goto err;
  348. }
  349. sdhci = rt_malloc(sizeof(struct sdhci_t));
  350. if (!sdhci)
  351. {
  352. dbg_log(DBG_ERROR, "alloc sdhci failed\n");
  353. goto err;
  354. }
  355. rt_memset(sdhci, 0, sizeof(struct sdhci_t));
  356. virt = MMC_BASE_ADDR;
  357. id = (((read32((virt + 0xfec)) & 0xff) << 24) |
  358. ((read32((virt + 0xfe8)) & 0xff) << 16) |
  359. ((read32((virt + 0xfe4)) & 0xff) << 8) |
  360. ((read32((virt + 0xfe0)) & 0xff) << 0));
  361. dbg_log(DBG_LOG, "id=0x%08x\n", id);
  362. if(((id >> 12) & 0xff) != 0x41 || (id & 0xfff) != 0x181)
  363. {
  364. dbg_log(DBG_ERROR, "check id failed\n");
  365. goto err;
  366. }
  367. pdat = (struct sdhci_pl180_pdata_t *)rt_malloc(sizeof(struct sdhci_pl180_pdata_t));
  368. RT_ASSERT(pdat != RT_NULL);
  369. pdat->virt = (uint32_t)virt;
  370. sdhci->name = "sd0";
  371. sdhci->voltages = VDD_33_34;
  372. sdhci->width = MMCSD_BUSWIDTH_4;
  373. sdhci->clock = 26 * 1000 * 1000;
  374. sdhci->removeable = RT_TRUE;
  375. sdhci->detect = sdhci_pl180_detect;
  376. sdhci->setwidth = sdhci_pl180_setwidth;
  377. sdhci->setclock = sdhci_pl180_setclock;
  378. sdhci->transfer = sdhci_pl180_transfer;
  379. sdhci->priv = pdat;
  380. write32(pdat->virt + PL180_POWER, 0xbf);
  381. // rt_kprintf("power:0x%08x\n", read32(pdat->virt + PL180_POWER));
  382. host->ops = &ops;
  383. host->freq_min = 400000;
  384. host->freq_max = 50000000;
  385. host->valid_ocr = VDD_32_33 | VDD_33_34;
  386. // host->flags = MMCSD_MUTBLKWRITE | MMCSD_SUP_HIGHSPEED | MMCSD_SUP_SDIO_IRQ | MMCSD_BUSWIDTH_4;
  387. host->flags = MMCSD_MUTBLKWRITE | MMCSD_SUP_HIGHSPEED | MMCSD_SUP_SDIO_IRQ;
  388. host->max_seg_size = 2048;
  389. host->max_dma_segs = 10;
  390. host->max_blk_size = 512;
  391. host->max_blk_count = 4096;
  392. host->private_data = sdhci;
  393. mmcsd_change(host);
  394. return RT_EOK;
  395. err:
  396. if(host) rt_free(host);
  397. if(sdhci) rt_free(sdhci);
  398. return -RT_EIO;
  399. }
  400. INIT_DEVICE_EXPORT(pl180_init);
  401. #endif