sunxi_mmc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /*
  2. * (C) Copyright 2007-2011
  3. * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
  4. * Aaron <leafy.myeh@allwinnertech.com>
  5. *
  6. * MMC driver for allwinner sunxi platform.
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <common.h>
  11. #include <malloc.h>
  12. #include <mmc.h>
  13. #include <asm/io.h>
  14. #include <asm/arch/clock.h>
  15. #include <asm/arch/cpu.h>
  16. #include <asm/arch/mmc.h>
  17. struct sunxi_mmc_des {
  18. u32 reserved1_1:1;
  19. u32 dic:1; /* disable interrupt on completion */
  20. u32 last_des:1; /* 1-this data buffer is the last buffer */
  21. u32 first_des:1; /* 1-data buffer is the first buffer,
  22. 0-data buffer contained in the next
  23. descriptor is 1st buffer */
  24. u32 des_chain:1; /* 1-the 2nd address in the descriptor is the
  25. next descriptor address */
  26. u32 end_of_ring:1; /* 1-last descriptor flag when using dual
  27. data buffer in descriptor */
  28. u32 reserved1_2:24;
  29. u32 card_err_sum:1; /* transfer error flag */
  30. u32 own:1; /* des owner:1-idma owns it, 0-host owns it */
  31. #define SDXC_DES_NUM_SHIFT 16
  32. #define SDXC_DES_BUFFER_MAX_LEN (1 << SDXC_DES_NUM_SHIFT)
  33. u32 data_buf1_sz:16;
  34. u32 data_buf2_sz:16;
  35. u32 buf_addr_ptr1;
  36. u32 buf_addr_ptr2;
  37. };
  38. struct sunxi_mmc_host {
  39. unsigned mmc_no;
  40. uint32_t *mclkreg;
  41. unsigned database;
  42. unsigned fatal_err;
  43. unsigned mod_clk;
  44. struct sunxi_mmc *reg;
  45. struct mmc_config cfg;
  46. };
  47. /* support 4 mmc hosts */
  48. struct sunxi_mmc_host mmc_host[4];
  49. static int mmc_resource_init(int sdc_no)
  50. {
  51. struct sunxi_mmc_host *mmchost = &mmc_host[sdc_no];
  52. struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  53. debug("init mmc %d resource\n", sdc_no);
  54. switch (sdc_no) {
  55. case 0:
  56. mmchost->reg = (struct sunxi_mmc *)SUNXI_MMC0_BASE;
  57. mmchost->mclkreg = &ccm->sd0_clk_cfg;
  58. break;
  59. case 1:
  60. mmchost->reg = (struct sunxi_mmc *)SUNXI_MMC1_BASE;
  61. mmchost->mclkreg = &ccm->sd1_clk_cfg;
  62. break;
  63. case 2:
  64. mmchost->reg = (struct sunxi_mmc *)SUNXI_MMC2_BASE;
  65. mmchost->mclkreg = &ccm->sd2_clk_cfg;
  66. break;
  67. case 3:
  68. mmchost->reg = (struct sunxi_mmc *)SUNXI_MMC3_BASE;
  69. mmchost->mclkreg = &ccm->sd3_clk_cfg;
  70. break;
  71. default:
  72. printf("Wrong mmc number %d\n", sdc_no);
  73. return -1;
  74. }
  75. mmchost->database = (unsigned int)mmchost->reg + 0x100;
  76. mmchost->mmc_no = sdc_no;
  77. return 0;
  78. }
  79. static int mmc_clk_io_on(int sdc_no)
  80. {
  81. unsigned int pll_clk;
  82. unsigned int divider;
  83. struct sunxi_mmc_host *mmchost = &mmc_host[sdc_no];
  84. struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  85. debug("init mmc %d clock and io\n", sdc_no);
  86. /* config ahb clock */
  87. setbits_le32(&ccm->ahb_gate0, 1 << AHB_GATE_OFFSET_MMC(sdc_no));
  88. /* config mod clock */
  89. pll_clk = clock_get_pll6();
  90. /* should be close to 100 MHz but no more, so round up */
  91. divider = ((pll_clk + 99999999) / 100000000) - 1;
  92. writel(CCM_MMC_CTRL_ENABLE | CCM_MMC_CTRL_PLL6 | divider,
  93. mmchost->mclkreg);
  94. mmchost->mod_clk = pll_clk / (divider + 1);
  95. return 0;
  96. }
  97. static int mmc_update_clk(struct mmc *mmc)
  98. {
  99. struct sunxi_mmc_host *mmchost = mmc->priv;
  100. unsigned int cmd;
  101. unsigned timeout_msecs = 2000;
  102. cmd = SUNXI_MMC_CMD_START |
  103. SUNXI_MMC_CMD_UPCLK_ONLY |
  104. SUNXI_MMC_CMD_WAIT_PRE_OVER;
  105. writel(cmd, &mmchost->reg->cmd);
  106. while (readl(&mmchost->reg->cmd) & SUNXI_MMC_CMD_START) {
  107. if (!timeout_msecs--)
  108. return -1;
  109. udelay(1000);
  110. }
  111. /* clock update sets various irq status bits, clear these */
  112. writel(readl(&mmchost->reg->rint), &mmchost->reg->rint);
  113. return 0;
  114. }
  115. static int mmc_config_clock(struct mmc *mmc, unsigned div)
  116. {
  117. struct sunxi_mmc_host *mmchost = mmc->priv;
  118. unsigned rval = readl(&mmchost->reg->clkcr);
  119. /* Disable Clock */
  120. rval &= ~SUNXI_MMC_CLK_ENABLE;
  121. writel(rval, &mmchost->reg->clkcr);
  122. if (mmc_update_clk(mmc))
  123. return -1;
  124. /* Change Divider Factor */
  125. rval &= ~SUNXI_MMC_CLK_DIVIDER_MASK;
  126. rval |= div;
  127. writel(rval, &mmchost->reg->clkcr);
  128. if (mmc_update_clk(mmc))
  129. return -1;
  130. /* Re-enable Clock */
  131. rval |= SUNXI_MMC_CLK_ENABLE;
  132. writel(rval, &mmchost->reg->clkcr);
  133. if (mmc_update_clk(mmc))
  134. return -1;
  135. return 0;
  136. }
  137. static void mmc_set_ios(struct mmc *mmc)
  138. {
  139. struct sunxi_mmc_host *mmchost = mmc->priv;
  140. unsigned int clkdiv = 0;
  141. debug("set ios: bus_width: %x, clock: %d, mod_clk: %d\n",
  142. mmc->bus_width, mmc->clock, mmchost->mod_clk);
  143. /* Change clock first */
  144. clkdiv = (mmchost->mod_clk + (mmc->clock >> 1)) / mmc->clock / 2;
  145. if (mmc->clock) {
  146. if (mmc_config_clock(mmc, clkdiv)) {
  147. mmchost->fatal_err = 1;
  148. return;
  149. }
  150. }
  151. /* Change bus width */
  152. if (mmc->bus_width == 8)
  153. writel(0x2, &mmchost->reg->width);
  154. else if (mmc->bus_width == 4)
  155. writel(0x1, &mmchost->reg->width);
  156. else
  157. writel(0x0, &mmchost->reg->width);
  158. }
  159. static int mmc_core_init(struct mmc *mmc)
  160. {
  161. struct sunxi_mmc_host *mmchost = mmc->priv;
  162. /* Reset controller */
  163. writel(SUNXI_MMC_GCTRL_RESET, &mmchost->reg->gctrl);
  164. return 0;
  165. }
  166. static int mmc_trans_data_by_cpu(struct mmc *mmc, struct mmc_data *data)
  167. {
  168. struct sunxi_mmc_host *mmchost = mmc->priv;
  169. const int reading = !!(data->flags & MMC_DATA_READ);
  170. const uint32_t status_bit = reading ? SUNXI_MMC_STATUS_FIFO_EMPTY :
  171. SUNXI_MMC_STATUS_FIFO_FULL;
  172. unsigned i;
  173. unsigned byte_cnt = data->blocksize * data->blocks;
  174. unsigned timeout_msecs = 2000;
  175. unsigned *buff = (unsigned int *)(reading ? data->dest : data->src);
  176. for (i = 0; i < (byte_cnt >> 2); i++) {
  177. while (readl(&mmchost->reg->status) & status_bit) {
  178. if (!timeout_msecs--)
  179. return -1;
  180. udelay(1000);
  181. }
  182. if (reading)
  183. buff[i] = readl(mmchost->database);
  184. else
  185. writel(buff[i], mmchost->database);
  186. }
  187. return 0;
  188. }
  189. static int mmc_trans_data_by_dma(struct mmc *mmc, struct mmc_data *data)
  190. {
  191. struct sunxi_mmc_host *mmchost = mmc->priv;
  192. unsigned byte_cnt = data->blocksize * data->blocks;
  193. unsigned char *buff;
  194. unsigned des_idx = 0;
  195. unsigned buff_frag_num =
  196. (byte_cnt + SDXC_DES_BUFFER_MAX_LEN - 1) >> SDXC_DES_NUM_SHIFT;
  197. unsigned remain;
  198. unsigned i, rval;
  199. ALLOC_CACHE_ALIGN_BUFFER(struct sunxi_mmc_des, pdes, buff_frag_num);
  200. buff = data->flags & MMC_DATA_READ ?
  201. (unsigned char *)data->dest : (unsigned char *)data->src;
  202. remain = byte_cnt & (SDXC_DES_BUFFER_MAX_LEN - 1);
  203. flush_cache((unsigned long)buff, (unsigned long)byte_cnt);
  204. for (i = 0; i < buff_frag_num; i++, des_idx++) {
  205. memset((void *)&pdes[des_idx], 0, sizeof(struct sunxi_mmc_des));
  206. pdes[des_idx].des_chain = 1;
  207. pdes[des_idx].own = 1;
  208. pdes[des_idx].dic = 1;
  209. if (buff_frag_num > 1 && i != buff_frag_num - 1)
  210. pdes[des_idx].data_buf1_sz = 0; /* 0 == max_len */
  211. else
  212. pdes[des_idx].data_buf1_sz = remain;
  213. pdes[des_idx].buf_addr_ptr1 =
  214. (u32) buff + i * SDXC_DES_BUFFER_MAX_LEN;
  215. if (i == 0)
  216. pdes[des_idx].first_des = 1;
  217. if (i == buff_frag_num - 1) {
  218. pdes[des_idx].dic = 0;
  219. pdes[des_idx].last_des = 1;
  220. pdes[des_idx].end_of_ring = 1;
  221. pdes[des_idx].buf_addr_ptr2 = 0;
  222. } else {
  223. pdes[des_idx].buf_addr_ptr2 = (u32)&pdes[des_idx + 1];
  224. }
  225. }
  226. flush_cache((unsigned long)pdes,
  227. sizeof(struct sunxi_mmc_des) * (des_idx + 1));
  228. rval = readl(&mmchost->reg->gctrl);
  229. /* Enable DMA */
  230. writel(rval | SUNXI_MMC_GCTRL_DMA_RESET | SUNXI_MMC_GCTRL_DMA_ENABLE,
  231. &mmchost->reg->gctrl);
  232. /* Reset iDMA */
  233. writel(SUNXI_MMC_IDMAC_RESET, &mmchost->reg->dmac);
  234. /* Enable iDMA */
  235. writel(SUNXI_MMC_IDMAC_FIXBURST | SUNXI_MMC_IDMAC_ENABLE,
  236. &mmchost->reg->dmac);
  237. rval = readl(&mmchost->reg->idie) &
  238. ~(SUNXI_MMC_IDIE_TXIRQ|SUNXI_MMC_IDIE_RXIRQ);
  239. if (data->flags & MMC_DATA_WRITE)
  240. rval |= SUNXI_MMC_IDIE_TXIRQ;
  241. else
  242. rval |= SUNXI_MMC_IDIE_RXIRQ;
  243. writel(rval, &mmchost->reg->idie);
  244. writel((u32) pdes, &mmchost->reg->dlba);
  245. writel((0x2 << 28) | (0x7 << 16) | (0x01 << 3),
  246. &mmchost->reg->ftrglevel);
  247. return 0;
  248. }
  249. static void mmc_enable_dma_accesses(struct mmc *mmc, int dma)
  250. {
  251. struct sunxi_mmc_host *mmchost = mmc->priv;
  252. unsigned int gctrl = readl(&mmchost->reg->gctrl);
  253. if (dma)
  254. gctrl &= ~SUNXI_MMC_GCTRL_ACCESS_BY_AHB;
  255. else
  256. gctrl |= SUNXI_MMC_GCTRL_ACCESS_BY_AHB;
  257. writel(gctrl, &mmchost->reg->gctrl);
  258. }
  259. static int mmc_rint_wait(struct mmc *mmc, unsigned int timeout_msecs,
  260. unsigned int done_bit, const char *what)
  261. {
  262. struct sunxi_mmc_host *mmchost = mmc->priv;
  263. unsigned int status;
  264. do {
  265. status = readl(&mmchost->reg->rint);
  266. if (!timeout_msecs-- ||
  267. (status & SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT)) {
  268. debug("%s timeout %x\n", what,
  269. status & SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT);
  270. return TIMEOUT;
  271. }
  272. udelay(1000);
  273. } while (!(status & done_bit));
  274. return 0;
  275. }
  276. static int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
  277. struct mmc_data *data)
  278. {
  279. struct sunxi_mmc_host *mmchost = mmc->priv;
  280. unsigned int cmdval = SUNXI_MMC_CMD_START;
  281. unsigned int timeout_msecs;
  282. int error = 0;
  283. unsigned int status = 0;
  284. unsigned int usedma = 0;
  285. unsigned int bytecnt = 0;
  286. if (mmchost->fatal_err)
  287. return -1;
  288. if (cmd->resp_type & MMC_RSP_BUSY)
  289. debug("mmc cmd %d check rsp busy\n", cmd->cmdidx);
  290. if (cmd->cmdidx == 12)
  291. return 0;
  292. if (!cmd->cmdidx)
  293. cmdval |= SUNXI_MMC_CMD_SEND_INIT_SEQ;
  294. if (cmd->resp_type & MMC_RSP_PRESENT)
  295. cmdval |= SUNXI_MMC_CMD_RESP_EXPIRE;
  296. if (cmd->resp_type & MMC_RSP_136)
  297. cmdval |= SUNXI_MMC_CMD_LONG_RESPONSE;
  298. if (cmd->resp_type & MMC_RSP_CRC)
  299. cmdval |= SUNXI_MMC_CMD_CHK_RESPONSE_CRC;
  300. if (data) {
  301. if ((u32) data->dest & 0x3) {
  302. error = -1;
  303. goto out;
  304. }
  305. cmdval |= SUNXI_MMC_CMD_DATA_EXPIRE|SUNXI_MMC_CMD_WAIT_PRE_OVER;
  306. if (data->flags & MMC_DATA_WRITE)
  307. cmdval |= SUNXI_MMC_CMD_WRITE;
  308. if (data->blocks > 1)
  309. cmdval |= SUNXI_MMC_CMD_AUTO_STOP;
  310. writel(data->blocksize, &mmchost->reg->blksz);
  311. writel(data->blocks * data->blocksize, &mmchost->reg->bytecnt);
  312. }
  313. debug("mmc %d, cmd %d(0x%08x), arg 0x%08x\n", mmchost->mmc_no,
  314. cmd->cmdidx, cmdval | cmd->cmdidx, cmd->cmdarg);
  315. writel(cmd->cmdarg, &mmchost->reg->arg);
  316. if (!data)
  317. writel(cmdval | cmd->cmdidx, &mmchost->reg->cmd);
  318. /*
  319. * transfer data and check status
  320. * STATREG[2] : FIFO empty
  321. * STATREG[3] : FIFO full
  322. */
  323. if (data) {
  324. int ret = 0;
  325. bytecnt = data->blocksize * data->blocks;
  326. debug("trans data %d bytes\n", bytecnt);
  327. #if defined(CONFIG_MMC_SUNXI_USE_DMA) && !defined(CONFIG_SPL_BUILD)
  328. if (bytecnt > 64) {
  329. #else
  330. if (0) {
  331. #endif
  332. usedma = 1;
  333. mmc_enable_dma_accesses(mmc, 1);
  334. ret = mmc_trans_data_by_dma(mmc, data);
  335. writel(cmdval | cmd->cmdidx, &mmchost->reg->cmd);
  336. } else {
  337. mmc_enable_dma_accesses(mmc, 0);
  338. writel(cmdval | cmd->cmdidx, &mmchost->reg->cmd);
  339. ret = mmc_trans_data_by_cpu(mmc, data);
  340. }
  341. if (ret) {
  342. error = readl(&mmchost->reg->rint) & \
  343. SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT;
  344. error = TIMEOUT;
  345. goto out;
  346. }
  347. }
  348. error = mmc_rint_wait(mmc, 0xfffff, SUNXI_MMC_RINT_COMMAND_DONE, "cmd");
  349. if (error)
  350. goto out;
  351. if (data) {
  352. timeout_msecs = usedma ? 120 * bytecnt : 120;
  353. debug("cacl timeout %x msec\n", timeout_msecs);
  354. error = mmc_rint_wait(mmc, timeout_msecs,
  355. data->blocks > 1 ?
  356. SUNXI_MMC_RINT_AUTO_COMMAND_DONE :
  357. SUNXI_MMC_RINT_DATA_OVER,
  358. "data");
  359. if (error)
  360. goto out;
  361. }
  362. if (cmd->resp_type & MMC_RSP_BUSY) {
  363. timeout_msecs = 2000;
  364. do {
  365. status = readl(&mmchost->reg->status);
  366. if (!timeout_msecs--) {
  367. debug("busy timeout\n");
  368. error = TIMEOUT;
  369. goto out;
  370. }
  371. udelay(1000);
  372. } while (status & SUNXI_MMC_STATUS_CARD_DATA_BUSY);
  373. }
  374. if (cmd->resp_type & MMC_RSP_136) {
  375. cmd->response[0] = readl(&mmchost->reg->resp3);
  376. cmd->response[1] = readl(&mmchost->reg->resp2);
  377. cmd->response[2] = readl(&mmchost->reg->resp1);
  378. cmd->response[3] = readl(&mmchost->reg->resp0);
  379. debug("mmc resp 0x%08x 0x%08x 0x%08x 0x%08x\n",
  380. cmd->response[3], cmd->response[2],
  381. cmd->response[1], cmd->response[0]);
  382. } else {
  383. cmd->response[0] = readl(&mmchost->reg->resp0);
  384. debug("mmc resp 0x%08x\n", cmd->response[0]);
  385. }
  386. out:
  387. if (data && usedma) {
  388. /* IDMASTAREG
  389. * IDST[0] : idma tx int
  390. * IDST[1] : idma rx int
  391. * IDST[2] : idma fatal bus error
  392. * IDST[4] : idma descriptor invalid
  393. * IDST[5] : idma error summary
  394. * IDST[8] : idma normal interrupt sumary
  395. * IDST[9] : idma abnormal interrupt sumary
  396. */
  397. status = readl(&mmchost->reg->idst);
  398. writel(status, &mmchost->reg->idst);
  399. writel(0, &mmchost->reg->idie);
  400. writel(0, &mmchost->reg->dmac);
  401. writel(readl(&mmchost->reg->gctrl) & ~SUNXI_MMC_GCTRL_DMA_ENABLE,
  402. &mmchost->reg->gctrl);
  403. }
  404. if (error < 0) {
  405. writel(SUNXI_MMC_GCTRL_RESET, &mmchost->reg->gctrl);
  406. mmc_update_clk(mmc);
  407. }
  408. writel(0xffffffff, &mmchost->reg->rint);
  409. writel(readl(&mmchost->reg->gctrl) | SUNXI_MMC_GCTRL_FIFO_RESET,
  410. &mmchost->reg->gctrl);
  411. return error;
  412. }
  413. static const struct mmc_ops sunxi_mmc_ops = {
  414. .send_cmd = mmc_send_cmd,
  415. .set_ios = mmc_set_ios,
  416. .init = mmc_core_init,
  417. };
  418. int sunxi_mmc_init(int sdc_no)
  419. {
  420. struct mmc_config *cfg = &mmc_host[sdc_no].cfg;
  421. memset(&mmc_host[sdc_no], 0, sizeof(struct sunxi_mmc_host));
  422. cfg->name = "SUNXI SD/MMC";
  423. cfg->ops = &sunxi_mmc_ops;
  424. cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
  425. cfg->host_caps = MMC_MODE_4BIT;
  426. cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
  427. cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
  428. cfg->f_min = 400000;
  429. cfg->f_max = 52000000;
  430. mmc_resource_init(sdc_no);
  431. mmc_clk_io_on(sdc_no);
  432. if (mmc_create(cfg, &mmc_host[sdc_no]) == NULL)
  433. return -1;
  434. return 0;
  435. }