drv_sdio.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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_TAG "drv.sdio"
  71. #define DBG_LVL DBG_INFO
  72. #include "rtdbg.h"
  73. struct sdhci_pl180_pdata_t
  74. {
  75. rt_uint32_t virt;
  76. };
  77. static inline rt_uint32_t read32(uint32_t addr)
  78. {
  79. return( *((volatile rt_uint32_t *)(addr)) );
  80. }
  81. static inline void write32(uint32_t addr, rt_uint32_t value)
  82. {
  83. *((volatile rt_uint32_t *)(addr)) = value;
  84. }
  85. static rt_err_t pl180_transfer_command(struct sdhci_pl180_pdata_t * pdat, struct sdhci_cmd_t * cmd)
  86. {
  87. rt_uint32_t cmdidx;
  88. rt_uint32_t status;
  89. rt_err_t ret = RT_EOK;
  90. if(read32(pdat->virt + PL180_COMMAND) & PL180_CMD_ENABLE)
  91. write32(pdat->virt + PL180_COMMAND, 0x0);
  92. cmdidx = (cmd->cmdidx & 0xff) | PL180_CMD_ENABLE;
  93. if(cmd->resptype)
  94. {
  95. cmdidx |= PL180_CMD_WAITRESP;
  96. if(cmd->resptype & PL180_RSP_136BIT)
  97. cmdidx |= PL180_CMD_LONGRSP;
  98. }
  99. write32(pdat->virt + PL180_ARGUMENT, cmd->cmdarg);
  100. write32(pdat->virt + PL180_COMMAND, cmdidx);
  101. do {
  102. status = read32(pdat->virt + PL180_STATUS);
  103. } while(!(status & (PL180_STAT_CMD_SENT | PL180_STAT_CMD_RESP_END | PL180_STAT_CMD_TIME_OUT | PL180_STAT_CMD_CRC_FAIL)));
  104. LOG_D("mmc status done!");
  105. if(cmd->resptype & PL180_RSP_PRESENT)
  106. {
  107. cmd->response[0] = read32(pdat->virt + PL180_RESP0);
  108. if(cmd->resptype & PL180_RSP_136BIT)
  109. {
  110. LOG_D("136bit response");
  111. cmd->response[1] = read32(pdat->virt + PL180_RESP1);
  112. cmd->response[2] = read32(pdat->virt + PL180_RESP2);
  113. cmd->response[3] = read32(pdat->virt + PL180_RESP3);
  114. }
  115. }
  116. if(status & PL180_STAT_CMD_TIME_OUT)
  117. {
  118. ret = -RT_ETIMEOUT;
  119. }
  120. else if ((status & PL180_STAT_CMD_CRC_FAIL) && (cmd->resptype & PL180_RSP_CRC))
  121. {
  122. ret = -RT_ERROR;
  123. }
  124. write32(pdat->virt + PL180_CLEAR, (PL180_CLR_CMD_SENT | PL180_CLR_CMD_RESP_END | PL180_CLR_CMD_TIMEOUT | PL180_CLR_CMD_CRC_FAIL));
  125. return ret;
  126. }
  127. static rt_err_t read_bytes(struct sdhci_pl180_pdata_t * pdat, rt_uint32_t * buf, rt_uint32_t blkcount, rt_uint32_t blksize)
  128. {
  129. rt_uint32_t * tmp = buf;
  130. rt_uint32_t count = blkcount * blksize;
  131. rt_uint32_t status, err;
  132. status = read32(pdat->virt + PL180_STATUS);
  133. err = status & (PL180_STAT_DAT_CRC_FAIL | PL180_STAT_DAT_TIME_OUT | PL180_STAT_RX_OVERRUN);
  134. while((!err) && (count >= sizeof(rt_uint32_t)))
  135. {
  136. if(status & PL180_STAT_RX_FIFO_AVL)
  137. {
  138. *(tmp) = read32(pdat->virt + PL180_FIFO);
  139. tmp++;
  140. count -= sizeof(rt_uint32_t);
  141. }
  142. status = read32(pdat->virt + PL180_STATUS);
  143. err = status & (PL180_STAT_DAT_CRC_FAIL | PL180_STAT_DAT_TIME_OUT | PL180_STAT_RX_OVERRUN);
  144. }
  145. err = status & (PL180_STAT_DAT_CRC_FAIL | PL180_STAT_DAT_TIME_OUT | PL180_STAT_DAT_BLK_END | PL180_STAT_RX_OVERRUN);
  146. while(!err)
  147. {
  148. status = read32(pdat->virt + PL180_STATUS);
  149. err = status & (PL180_STAT_DAT_CRC_FAIL | PL180_STAT_DAT_TIME_OUT | PL180_STAT_DAT_BLK_END | PL180_STAT_RX_OVERRUN);
  150. }
  151. if(status & PL180_STAT_DAT_TIME_OUT)
  152. return -RT_ERROR;
  153. else if (status & PL180_STAT_DAT_CRC_FAIL)
  154. return -RT_ERROR;
  155. else if (status & PL180_STAT_RX_OVERRUN)
  156. return -RT_ERROR;
  157. write32(pdat->virt + PL180_CLEAR, 0x1DC007FF);
  158. if(count)
  159. return -RT_ERROR;
  160. return RT_EOK;
  161. }
  162. static rt_err_t write_bytes(struct sdhci_pl180_pdata_t * pdat, rt_uint32_t * buf, rt_uint32_t blkcount, rt_uint32_t blksize)
  163. {
  164. rt_uint32_t * tmp = buf;
  165. rt_uint32_t count = blkcount * blksize;
  166. rt_uint32_t status, err;
  167. int i;
  168. status = read32(pdat->virt + PL180_STATUS);
  169. err = status & (PL180_STAT_DAT_CRC_FAIL | PL180_STAT_DAT_TIME_OUT);
  170. while(!err && count)
  171. {
  172. if(status & PL180_STAT_TX_FIFO_HALF)
  173. {
  174. if(count >= 8 * sizeof(rt_uint32_t))
  175. {
  176. for(i = 0; i < 8; i++)
  177. write32(pdat->virt + PL180_FIFO, *(tmp + i));
  178. tmp += 8;
  179. count -= 8 * sizeof(rt_uint32_t);
  180. }
  181. else
  182. {
  183. while(count >= sizeof(rt_uint32_t))
  184. {
  185. write32(pdat->virt + PL180_FIFO, *tmp);
  186. tmp++;
  187. count -= sizeof(rt_uint32_t);
  188. }
  189. }
  190. }
  191. status = read32(pdat->virt + PL180_STATUS);
  192. err = status & (PL180_STAT_DAT_CRC_FAIL | PL180_STAT_DAT_TIME_OUT);
  193. }
  194. err = status & (PL180_STAT_DAT_CRC_FAIL | PL180_STAT_DAT_TIME_OUT | PL180_STAT_DAT_BLK_END);
  195. while(!err)
  196. {
  197. status = read32(pdat->virt + PL180_STATUS);
  198. err = status & (PL180_STAT_DAT_CRC_FAIL | PL180_STAT_DAT_TIME_OUT | PL180_STAT_DAT_BLK_END);
  199. }
  200. if(status & PL180_STAT_DAT_TIME_OUT)
  201. return -RT_ERROR;
  202. else if (status & PL180_STAT_DAT_CRC_FAIL)
  203. return -RT_ERROR;
  204. write32(pdat->virt + PL180_CLEAR, 0x1DC007FF);
  205. if(count)
  206. return -RT_ERROR;
  207. return RT_EOK;
  208. }
  209. static rt_err_t pl180_transfer_data(struct sdhci_pl180_pdata_t * pdat, struct sdhci_cmd_t * cmd, struct sdhci_data_t * dat)
  210. {
  211. rt_uint32_t dlen = (rt_uint32_t)(dat->blkcnt * dat->blksz);
  212. rt_uint32_t blksz_bits = dat->blksz - 1;
  213. rt_uint32_t dctrl = (blksz_bits << 4) | (0x1 << 0) | (0x1 << 14);
  214. rt_err_t ret = -RT_ERROR;
  215. write32(pdat->virt + PL180_DATA_TIMER, 0xffff);
  216. write32(pdat->virt + PL180_DATA_LENGTH, dlen);
  217. if(dat->flag & DATA_DIR_READ)
  218. {
  219. dctrl |= (0x1 << 1);
  220. write32(pdat->virt + PL180_DATA_CTRL, dctrl);
  221. ret = pl180_transfer_command(pdat, cmd);
  222. if (ret < 0) return ret;
  223. ret = read_bytes(pdat, (rt_uint32_t *)dat->buf, dat->blkcnt, dat->blksz);
  224. }
  225. else if(dat->flag & DATA_DIR_WRITE)
  226. {
  227. ret = pl180_transfer_command(pdat, cmd);
  228. if (ret < 0) return ret;
  229. write32(pdat->virt + PL180_DATA_CTRL, dctrl);
  230. ret = write_bytes(pdat, (rt_uint32_t *)dat->buf, dat->blkcnt, dat->blksz);
  231. }
  232. return ret;
  233. }
  234. static rt_err_t sdhci_pl180_detect(struct sdhci_t * sdhci)
  235. {
  236. return RT_EOK;
  237. }
  238. static rt_err_t sdhci_pl180_setwidth(struct sdhci_t * sdhci, rt_uint32_t width)
  239. {
  240. return RT_EOK;
  241. }
  242. static rt_err_t sdhci_pl180_setclock(struct sdhci_t * sdhci, rt_uint32_t clock)
  243. {
  244. rt_uint32_t temp = 0;
  245. struct sdhci_pl180_pdata_t * pdat = (struct sdhci_pl180_pdata_t *)sdhci->priv;
  246. if(clock)
  247. {
  248. temp = read32(pdat->virt + PL180_CLOCK) | (0x1<<8);
  249. temp = temp; // skip warning
  250. write32(pdat->virt + PL180_CLOCK, 0x100);
  251. }
  252. else
  253. {
  254. //write32(pdat->virt + PL180_CLOCK, read32(pdat->virt + PL180_CLOCK) & (~(0x1<<8)));
  255. }
  256. return RT_EOK;
  257. }
  258. static rt_err_t sdhci_pl180_transfer(struct sdhci_t * sdhci, struct sdhci_cmd_t * cmd, struct sdhci_data_t * dat)
  259. {
  260. struct sdhci_pl180_pdata_t * pdat = (struct sdhci_pl180_pdata_t *)sdhci->priv;
  261. if(!dat)
  262. return pl180_transfer_command(pdat, cmd);
  263. return pl180_transfer_data(pdat, cmd, dat);
  264. }
  265. static void mmc_request_send(struct rt_mmcsd_host *host, struct rt_mmcsd_req *req)
  266. {
  267. struct sdhci_t *sdhci = (struct sdhci_t *)host->private_data;
  268. struct sdhci_cmd_t cmd;
  269. struct sdhci_cmd_t stop;
  270. struct sdhci_data_t dat;
  271. rt_memset(&cmd, 0, sizeof(struct sdhci_cmd_t));
  272. rt_memset(&stop, 0, sizeof(struct sdhci_cmd_t));
  273. rt_memset(&dat, 0, sizeof(struct sdhci_data_t));
  274. cmd.cmdidx = req->cmd->cmd_code;
  275. cmd.cmdarg = req->cmd->arg;
  276. if (req->cmd->flags & RESP_MASK)
  277. {
  278. cmd.resptype = PL180_RSP_PRESENT;
  279. if (resp_type(req->cmd) == RESP_R2)
  280. cmd.resptype |= PL180_RSP_136BIT;
  281. }
  282. else
  283. cmd.resptype = 0;
  284. if(req->data)
  285. {
  286. dat.buf = (rt_uint8_t *)req->data->buf;
  287. dat.flag = req->data->flags;
  288. dat.blksz = req->data->blksize;
  289. dat.blkcnt = req->data->blks;
  290. req->cmd->err = sdhci_pl180_transfer(sdhci, &cmd, &dat);
  291. }
  292. else
  293. {
  294. req->cmd->err = sdhci_pl180_transfer(sdhci, &cmd, RT_NULL);
  295. }
  296. LOG_D("cmdarg:%d", cmd.cmdarg);
  297. LOG_D("cmdidx:%d", cmd.cmdidx);
  298. 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]);
  299. req->cmd->resp[3] = cmd.response[3];
  300. req->cmd->resp[2] = cmd.response[2];
  301. req->cmd->resp[1] = cmd.response[1];
  302. req->cmd->resp[0] = cmd.response[0];
  303. if (req->stop)
  304. {
  305. stop.cmdidx = req->stop->cmd_code;
  306. stop.cmdarg = req->stop->arg;
  307. if (req->stop->flags & RESP_MASK)
  308. {
  309. stop.resptype = PL180_RSP_PRESENT;
  310. if (resp_type(req->stop) == RESP_R2)
  311. stop.resptype |= PL180_RSP_136BIT;
  312. }
  313. else
  314. stop.resptype = 0;
  315. req->stop->err = sdhci_pl180_transfer(sdhci, &stop, RT_NULL);
  316. }
  317. mmcsd_req_complete(host);
  318. }
  319. static void mmc_set_iocfg(struct rt_mmcsd_host *host, struct rt_mmcsd_io_cfg *io_cfg)
  320. {
  321. struct sdhci_t * sdhci = (struct sdhci_t *)host->private_data;
  322. sdhci_pl180_setclock(sdhci, io_cfg->clock);
  323. sdhci_pl180_setwidth(sdhci, io_cfg->bus_width);
  324. LOG_D("clock:%d bus_width:%d", io_cfg->clock, io_cfg->bus_width);
  325. }
  326. static const struct rt_mmcsd_host_ops ops =
  327. {
  328. mmc_request_send,
  329. mmc_set_iocfg,
  330. RT_NULL,
  331. RT_NULL,
  332. };
  333. int pl180_init(void)
  334. {
  335. rt_uint32_t virt;
  336. rt_uint32_t id;
  337. struct rt_mmcsd_host * host = RT_NULL;
  338. struct sdhci_pl180_pdata_t * pdat = RT_NULL;
  339. struct sdhci_t * sdhci = RT_NULL;
  340. host = mmcsd_alloc_host();
  341. if (!host)
  342. {
  343. LOG_E("alloc host failed");
  344. goto err;
  345. }
  346. sdhci = rt_malloc(sizeof(struct sdhci_t));
  347. if (!sdhci)
  348. {
  349. LOG_E("alloc sdhci failed");
  350. goto err;
  351. }
  352. rt_memset(sdhci, 0, sizeof(struct sdhci_t));
  353. virt = MMC_BASE_ADDR;
  354. id = (((read32((virt + 0xfec)) & 0xff) << 24) |
  355. ((read32((virt + 0xfe8)) & 0xff) << 16) |
  356. ((read32((virt + 0xfe4)) & 0xff) << 8) |
  357. ((read32((virt + 0xfe0)) & 0xff) << 0));
  358. LOG_D("id=0x%08x", id);
  359. if(((id >> 12) & 0xff) != 0x41 || (id & 0xfff) != 0x181)
  360. {
  361. LOG_E("check id failed");
  362. goto err;
  363. }
  364. pdat = (struct sdhci_pl180_pdata_t *)rt_malloc(sizeof(struct sdhci_pl180_pdata_t));
  365. RT_ASSERT(pdat != RT_NULL);
  366. pdat->virt = (uint32_t)virt;
  367. sdhci->name = "sd0";
  368. sdhci->voltages = VDD_33_34;
  369. sdhci->width = MMCSD_BUSWIDTH_4;
  370. sdhci->clock = 26 * 1000 * 1000;
  371. sdhci->removeable = RT_TRUE;
  372. sdhci->detect = sdhci_pl180_detect;
  373. sdhci->setwidth = sdhci_pl180_setwidth;
  374. sdhci->setclock = sdhci_pl180_setclock;
  375. sdhci->transfer = sdhci_pl180_transfer;
  376. sdhci->priv = pdat;
  377. write32(pdat->virt + PL180_POWER, 0xbf);
  378. host->ops = &ops;
  379. host->freq_min = 400000;
  380. host->freq_max = 50000000;
  381. host->valid_ocr = VDD_32_33 | VDD_33_34;
  382. host->flags = MMCSD_MUTBLKWRITE | MMCSD_SUP_HIGHSPEED | MMCSD_SUP_SDIO_IRQ | MMCSD_BUSWIDTH_4;
  383. host->max_seg_size = 2048;
  384. host->max_dma_segs = 10;
  385. host->max_blk_size = 512;
  386. host->max_blk_count = 4096;
  387. host->private_data = sdhci;
  388. mmcsd_change(host);
  389. return RT_EOK;
  390. err:
  391. if(host) rt_free(host);
  392. if(sdhci) rt_free(sdhci);
  393. return -RT_EIO;
  394. }
  395. INIT_DEVICE_EXPORT(pl180_init);
  396. #endif