sunxi_mmc.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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_host {
  18. unsigned mmc_no;
  19. uint32_t *mclkreg;
  20. unsigned database;
  21. unsigned fatal_err;
  22. unsigned mod_clk;
  23. struct sunxi_mmc *reg;
  24. struct mmc_config cfg;
  25. };
  26. /* support 4 mmc hosts */
  27. struct sunxi_mmc_host mmc_host[4];
  28. static int mmc_resource_init(int sdc_no)
  29. {
  30. struct sunxi_mmc_host *mmchost = &mmc_host[sdc_no];
  31. struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  32. debug("init mmc %d resource\n", sdc_no);
  33. switch (sdc_no) {
  34. case 0:
  35. mmchost->reg = (struct sunxi_mmc *)SUNXI_MMC0_BASE;
  36. mmchost->mclkreg = &ccm->sd0_clk_cfg;
  37. break;
  38. case 1:
  39. mmchost->reg = (struct sunxi_mmc *)SUNXI_MMC1_BASE;
  40. mmchost->mclkreg = &ccm->sd1_clk_cfg;
  41. break;
  42. case 2:
  43. mmchost->reg = (struct sunxi_mmc *)SUNXI_MMC2_BASE;
  44. mmchost->mclkreg = &ccm->sd2_clk_cfg;
  45. break;
  46. case 3:
  47. mmchost->reg = (struct sunxi_mmc *)SUNXI_MMC3_BASE;
  48. mmchost->mclkreg = &ccm->sd3_clk_cfg;
  49. break;
  50. default:
  51. printf("Wrong mmc number %d\n", sdc_no);
  52. return -1;
  53. }
  54. mmchost->database = (unsigned int)mmchost->reg + 0x100;
  55. mmchost->mmc_no = sdc_no;
  56. return 0;
  57. }
  58. static int mmc_clk_io_on(int sdc_no)
  59. {
  60. unsigned int pll_clk;
  61. unsigned int divider;
  62. struct sunxi_mmc_host *mmchost = &mmc_host[sdc_no];
  63. struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  64. debug("init mmc %d clock and io\n", sdc_no);
  65. /* config ahb clock */
  66. setbits_le32(&ccm->ahb_gate0, 1 << AHB_GATE_OFFSET_MMC(sdc_no));
  67. /* config mod clock */
  68. pll_clk = clock_get_pll6();
  69. /* should be close to 100 MHz but no more, so round up */
  70. divider = ((pll_clk + 99999999) / 100000000) - 1;
  71. writel(CCM_MMC_CTRL_ENABLE | CCM_MMC_CTRL_PLL6 | divider,
  72. mmchost->mclkreg);
  73. mmchost->mod_clk = pll_clk / (divider + 1);
  74. return 0;
  75. }
  76. static int mmc_update_clk(struct mmc *mmc)
  77. {
  78. struct sunxi_mmc_host *mmchost = mmc->priv;
  79. unsigned int cmd;
  80. unsigned timeout_msecs = 2000;
  81. cmd = SUNXI_MMC_CMD_START |
  82. SUNXI_MMC_CMD_UPCLK_ONLY |
  83. SUNXI_MMC_CMD_WAIT_PRE_OVER;
  84. writel(cmd, &mmchost->reg->cmd);
  85. while (readl(&mmchost->reg->cmd) & SUNXI_MMC_CMD_START) {
  86. if (!timeout_msecs--)
  87. return -1;
  88. udelay(1000);
  89. }
  90. /* clock update sets various irq status bits, clear these */
  91. writel(readl(&mmchost->reg->rint), &mmchost->reg->rint);
  92. return 0;
  93. }
  94. static int mmc_config_clock(struct mmc *mmc, unsigned div)
  95. {
  96. struct sunxi_mmc_host *mmchost = mmc->priv;
  97. unsigned rval = readl(&mmchost->reg->clkcr);
  98. /* Disable Clock */
  99. rval &= ~SUNXI_MMC_CLK_ENABLE;
  100. writel(rval, &mmchost->reg->clkcr);
  101. if (mmc_update_clk(mmc))
  102. return -1;
  103. /* Change Divider Factor */
  104. rval &= ~SUNXI_MMC_CLK_DIVIDER_MASK;
  105. rval |= div;
  106. writel(rval, &mmchost->reg->clkcr);
  107. if (mmc_update_clk(mmc))
  108. return -1;
  109. /* Re-enable Clock */
  110. rval |= SUNXI_MMC_CLK_ENABLE;
  111. writel(rval, &mmchost->reg->clkcr);
  112. if (mmc_update_clk(mmc))
  113. return -1;
  114. return 0;
  115. }
  116. static void mmc_set_ios(struct mmc *mmc)
  117. {
  118. struct sunxi_mmc_host *mmchost = mmc->priv;
  119. unsigned int clkdiv = 0;
  120. debug("set ios: bus_width: %x, clock: %d, mod_clk: %d\n",
  121. mmc->bus_width, mmc->clock, mmchost->mod_clk);
  122. /* Change clock first */
  123. clkdiv = (mmchost->mod_clk + (mmc->clock >> 1)) / mmc->clock / 2;
  124. if (mmc->clock) {
  125. if (mmc_config_clock(mmc, clkdiv)) {
  126. mmchost->fatal_err = 1;
  127. return;
  128. }
  129. }
  130. /* Change bus width */
  131. if (mmc->bus_width == 8)
  132. writel(0x2, &mmchost->reg->width);
  133. else if (mmc->bus_width == 4)
  134. writel(0x1, &mmchost->reg->width);
  135. else
  136. writel(0x0, &mmchost->reg->width);
  137. }
  138. static int mmc_core_init(struct mmc *mmc)
  139. {
  140. struct sunxi_mmc_host *mmchost = mmc->priv;
  141. /* Reset controller */
  142. writel(SUNXI_MMC_GCTRL_RESET, &mmchost->reg->gctrl);
  143. udelay(1000);
  144. return 0;
  145. }
  146. static int mmc_trans_data_by_cpu(struct mmc *mmc, struct mmc_data *data)
  147. {
  148. struct sunxi_mmc_host *mmchost = mmc->priv;
  149. const int reading = !!(data->flags & MMC_DATA_READ);
  150. const uint32_t status_bit = reading ? SUNXI_MMC_STATUS_FIFO_EMPTY :
  151. SUNXI_MMC_STATUS_FIFO_FULL;
  152. unsigned i;
  153. unsigned byte_cnt = data->blocksize * data->blocks;
  154. unsigned timeout_msecs = 2000;
  155. unsigned *buff = (unsigned int *)(reading ? data->dest : data->src);
  156. /* Always read / write data through the CPU */
  157. setbits_le32(&mmchost->reg->gctrl, SUNXI_MMC_GCTRL_ACCESS_BY_AHB);
  158. for (i = 0; i < (byte_cnt >> 2); i++) {
  159. while (readl(&mmchost->reg->status) & status_bit) {
  160. if (!timeout_msecs--)
  161. return -1;
  162. udelay(1000);
  163. }
  164. if (reading)
  165. buff[i] = readl(mmchost->database);
  166. else
  167. writel(buff[i], mmchost->database);
  168. }
  169. return 0;
  170. }
  171. static int mmc_rint_wait(struct mmc *mmc, unsigned int timeout_msecs,
  172. unsigned int done_bit, const char *what)
  173. {
  174. struct sunxi_mmc_host *mmchost = mmc->priv;
  175. unsigned int status;
  176. do {
  177. status = readl(&mmchost->reg->rint);
  178. if (!timeout_msecs-- ||
  179. (status & SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT)) {
  180. debug("%s timeout %x\n", what,
  181. status & SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT);
  182. return TIMEOUT;
  183. }
  184. udelay(1000);
  185. } while (!(status & done_bit));
  186. return 0;
  187. }
  188. static int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
  189. struct mmc_data *data)
  190. {
  191. struct sunxi_mmc_host *mmchost = mmc->priv;
  192. unsigned int cmdval = SUNXI_MMC_CMD_START;
  193. unsigned int timeout_msecs;
  194. int error = 0;
  195. unsigned int status = 0;
  196. unsigned int bytecnt = 0;
  197. if (mmchost->fatal_err)
  198. return -1;
  199. if (cmd->resp_type & MMC_RSP_BUSY)
  200. debug("mmc cmd %d check rsp busy\n", cmd->cmdidx);
  201. if (cmd->cmdidx == 12)
  202. return 0;
  203. if (!cmd->cmdidx)
  204. cmdval |= SUNXI_MMC_CMD_SEND_INIT_SEQ;
  205. if (cmd->resp_type & MMC_RSP_PRESENT)
  206. cmdval |= SUNXI_MMC_CMD_RESP_EXPIRE;
  207. if (cmd->resp_type & MMC_RSP_136)
  208. cmdval |= SUNXI_MMC_CMD_LONG_RESPONSE;
  209. if (cmd->resp_type & MMC_RSP_CRC)
  210. cmdval |= SUNXI_MMC_CMD_CHK_RESPONSE_CRC;
  211. if (data) {
  212. if ((u32) data->dest & 0x3) {
  213. error = -1;
  214. goto out;
  215. }
  216. cmdval |= SUNXI_MMC_CMD_DATA_EXPIRE|SUNXI_MMC_CMD_WAIT_PRE_OVER;
  217. if (data->flags & MMC_DATA_WRITE)
  218. cmdval |= SUNXI_MMC_CMD_WRITE;
  219. if (data->blocks > 1)
  220. cmdval |= SUNXI_MMC_CMD_AUTO_STOP;
  221. writel(data->blocksize, &mmchost->reg->blksz);
  222. writel(data->blocks * data->blocksize, &mmchost->reg->bytecnt);
  223. }
  224. debug("mmc %d, cmd %d(0x%08x), arg 0x%08x\n", mmchost->mmc_no,
  225. cmd->cmdidx, cmdval | cmd->cmdidx, cmd->cmdarg);
  226. writel(cmd->cmdarg, &mmchost->reg->arg);
  227. if (!data)
  228. writel(cmdval | cmd->cmdidx, &mmchost->reg->cmd);
  229. /*
  230. * transfer data and check status
  231. * STATREG[2] : FIFO empty
  232. * STATREG[3] : FIFO full
  233. */
  234. if (data) {
  235. int ret = 0;
  236. bytecnt = data->blocksize * data->blocks;
  237. debug("trans data %d bytes\n", bytecnt);
  238. writel(cmdval | cmd->cmdidx, &mmchost->reg->cmd);
  239. ret = mmc_trans_data_by_cpu(mmc, data);
  240. if (ret) {
  241. error = readl(&mmchost->reg->rint) & \
  242. SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT;
  243. error = TIMEOUT;
  244. goto out;
  245. }
  246. }
  247. error = mmc_rint_wait(mmc, 0xfffff, SUNXI_MMC_RINT_COMMAND_DONE, "cmd");
  248. if (error)
  249. goto out;
  250. if (data) {
  251. timeout_msecs = 120;
  252. debug("cacl timeout %x msec\n", timeout_msecs);
  253. error = mmc_rint_wait(mmc, timeout_msecs,
  254. data->blocks > 1 ?
  255. SUNXI_MMC_RINT_AUTO_COMMAND_DONE :
  256. SUNXI_MMC_RINT_DATA_OVER,
  257. "data");
  258. if (error)
  259. goto out;
  260. }
  261. if (cmd->resp_type & MMC_RSP_BUSY) {
  262. timeout_msecs = 2000;
  263. do {
  264. status = readl(&mmchost->reg->status);
  265. if (!timeout_msecs--) {
  266. debug("busy timeout\n");
  267. error = TIMEOUT;
  268. goto out;
  269. }
  270. udelay(1000);
  271. } while (status & SUNXI_MMC_STATUS_CARD_DATA_BUSY);
  272. }
  273. if (cmd->resp_type & MMC_RSP_136) {
  274. cmd->response[0] = readl(&mmchost->reg->resp3);
  275. cmd->response[1] = readl(&mmchost->reg->resp2);
  276. cmd->response[2] = readl(&mmchost->reg->resp1);
  277. cmd->response[3] = readl(&mmchost->reg->resp0);
  278. debug("mmc resp 0x%08x 0x%08x 0x%08x 0x%08x\n",
  279. cmd->response[3], cmd->response[2],
  280. cmd->response[1], cmd->response[0]);
  281. } else {
  282. cmd->response[0] = readl(&mmchost->reg->resp0);
  283. debug("mmc resp 0x%08x\n", cmd->response[0]);
  284. }
  285. out:
  286. if (error < 0) {
  287. writel(SUNXI_MMC_GCTRL_RESET, &mmchost->reg->gctrl);
  288. mmc_update_clk(mmc);
  289. }
  290. writel(0xffffffff, &mmchost->reg->rint);
  291. writel(readl(&mmchost->reg->gctrl) | SUNXI_MMC_GCTRL_FIFO_RESET,
  292. &mmchost->reg->gctrl);
  293. return error;
  294. }
  295. static const struct mmc_ops sunxi_mmc_ops = {
  296. .send_cmd = mmc_send_cmd,
  297. .set_ios = mmc_set_ios,
  298. .init = mmc_core_init,
  299. };
  300. int sunxi_mmc_init(int sdc_no)
  301. {
  302. struct mmc_config *cfg = &mmc_host[sdc_no].cfg;
  303. memset(&mmc_host[sdc_no], 0, sizeof(struct sunxi_mmc_host));
  304. cfg->name = "SUNXI SD/MMC";
  305. cfg->ops = &sunxi_mmc_ops;
  306. cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
  307. cfg->host_caps = MMC_MODE_4BIT;
  308. cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
  309. cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
  310. cfg->f_min = 400000;
  311. cfg->f_max = 52000000;
  312. mmc_resource_init(sdc_no);
  313. mmc_clk_io_on(sdc_no);
  314. if (mmc_create(cfg, &mmc_host[sdc_no]) == NULL)
  315. return -1;
  316. return 0;
  317. }