sunxi_mmc.c 11 KB

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