sunxi_mmc.c 10 KB

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