sunxi_mmc.c 11 KB

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