drv_sdio.c 14 KB

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