sunxi_mmc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2007-2011
  4. * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
  5. * Aaron <leafy.myeh@allwinnertech.com>
  6. *
  7. * MMC driver for allwinner sunxi platform.
  8. *
  9. * This driver is used by the (ARM) SPL with the legacy MMC interface, and
  10. * by U-Boot proper using the full DM interface. The actual hardware access
  11. * code is common, and comes first in this file.
  12. * The legacy MMC interface implementation comes next, followed by the
  13. * proper DM_MMC implementation at the end.
  14. */
  15. #include <common.h>
  16. #include <dm.h>
  17. #include <errno.h>
  18. #include <log.h>
  19. #include <malloc.h>
  20. #include <mmc.h>
  21. #include <clk.h>
  22. #include <reset.h>
  23. #include <asm/gpio.h>
  24. #include <asm/io.h>
  25. #include <asm/arch/clock.h>
  26. #include <asm/arch/cpu.h>
  27. #include <asm/arch/mmc.h>
  28. #include <linux/delay.h>
  29. #ifndef CCM_MMC_CTRL_MODE_SEL_NEW
  30. #define CCM_MMC_CTRL_MODE_SEL_NEW 0
  31. #endif
  32. struct sunxi_mmc_plat {
  33. struct mmc_config cfg;
  34. struct mmc mmc;
  35. };
  36. struct sunxi_mmc_priv {
  37. unsigned mmc_no;
  38. uint32_t *mclkreg;
  39. unsigned fatal_err;
  40. struct gpio_desc cd_gpio; /* Change Detect GPIO */
  41. struct sunxi_mmc *reg;
  42. struct mmc_config cfg;
  43. };
  44. /*
  45. * All A64 and later MMC controllers feature auto-calibration. This would
  46. * normally be detected via the compatible string, but we need something
  47. * which works in the SPL as well.
  48. */
  49. static bool sunxi_mmc_can_calibrate(void)
  50. {
  51. return IS_ENABLED(CONFIG_MACH_SUN50I) ||
  52. IS_ENABLED(CONFIG_MACH_SUN50I_H5) ||
  53. IS_ENABLED(CONFIG_SUN50I_GEN_H6) ||
  54. IS_ENABLED(CONFIG_MACH_SUN8I_R40);
  55. }
  56. static int mmc_set_mod_clk(struct sunxi_mmc_priv *priv, unsigned int hz)
  57. {
  58. unsigned int pll, pll_hz, div, n, oclk_dly, sclk_dly;
  59. bool new_mode = IS_ENABLED(CONFIG_MMC_SUNXI_HAS_NEW_MODE);
  60. u32 val = 0;
  61. /* A83T support new mode only on eMMC */
  62. if (IS_ENABLED(CONFIG_MACH_SUN8I_A83T) && priv->mmc_no != 2)
  63. new_mode = false;
  64. if (hz <= 24000000) {
  65. pll = CCM_MMC_CTRL_OSCM24;
  66. pll_hz = 24000000;
  67. } else {
  68. #ifdef CONFIG_MACH_SUN9I
  69. pll = CCM_MMC_CTRL_PLL_PERIPH0;
  70. pll_hz = clock_get_pll4_periph0();
  71. #else
  72. /*
  73. * SoCs since the A64 (H5, H6, H616) actually use the doubled
  74. * rate of PLL6/PERIPH0 as an input clock, but compensate for
  75. * that with a fixed post-divider of 2 in the mod clock.
  76. * This cancels each other out, so for simplicity we just
  77. * pretend it's always PLL6 without a post divider here.
  78. */
  79. pll = CCM_MMC_CTRL_PLL6;
  80. pll_hz = clock_get_pll6();
  81. #endif
  82. }
  83. div = pll_hz / hz;
  84. if (pll_hz % hz)
  85. div++;
  86. n = 0;
  87. while (div > 16) {
  88. n++;
  89. div = (div + 1) / 2;
  90. }
  91. if (n > 3) {
  92. printf("mmc %u error cannot set clock to %u\n", priv->mmc_no,
  93. hz);
  94. return -1;
  95. }
  96. /* determine delays */
  97. if (hz <= 400000) {
  98. oclk_dly = 0;
  99. sclk_dly = 0;
  100. } else if (hz <= 25000000) {
  101. oclk_dly = 0;
  102. sclk_dly = 5;
  103. } else {
  104. if (IS_ENABLED(CONFIG_MACH_SUN9I)) {
  105. if (hz <= 52000000)
  106. oclk_dly = 5;
  107. else
  108. oclk_dly = 2;
  109. } else {
  110. if (hz <= 52000000)
  111. oclk_dly = 3;
  112. else
  113. oclk_dly = 1;
  114. }
  115. sclk_dly = 4;
  116. }
  117. if (new_mode) {
  118. val |= CCM_MMC_CTRL_MODE_SEL_NEW;
  119. setbits_le32(&priv->reg->ntsr, SUNXI_MMC_NTSR_MODE_SEL_NEW);
  120. }
  121. if (!sunxi_mmc_can_calibrate()) {
  122. /*
  123. * Use hardcoded delay values if controller doesn't support
  124. * calibration
  125. */
  126. val = CCM_MMC_CTRL_OCLK_DLY(oclk_dly) |
  127. CCM_MMC_CTRL_SCLK_DLY(sclk_dly);
  128. }
  129. writel(CCM_MMC_CTRL_ENABLE| pll | CCM_MMC_CTRL_N(n) |
  130. CCM_MMC_CTRL_M(div) | val, priv->mclkreg);
  131. debug("mmc %u set mod-clk req %u parent %u n %u m %u rate %u\n",
  132. priv->mmc_no, hz, pll_hz, 1u << n, div, pll_hz / (1u << n) / div);
  133. return 0;
  134. }
  135. static int mmc_update_clk(struct sunxi_mmc_priv *priv)
  136. {
  137. unsigned int cmd;
  138. unsigned timeout_msecs = 2000;
  139. unsigned long start = get_timer(0);
  140. cmd = SUNXI_MMC_CMD_START |
  141. SUNXI_MMC_CMD_UPCLK_ONLY |
  142. SUNXI_MMC_CMD_WAIT_PRE_OVER;
  143. writel(cmd, &priv->reg->cmd);
  144. while (readl(&priv->reg->cmd) & SUNXI_MMC_CMD_START) {
  145. if (get_timer(start) > timeout_msecs)
  146. return -1;
  147. }
  148. /* clock update sets various irq status bits, clear these */
  149. writel(readl(&priv->reg->rint), &priv->reg->rint);
  150. return 0;
  151. }
  152. static int mmc_config_clock(struct sunxi_mmc_priv *priv, struct mmc *mmc)
  153. {
  154. unsigned rval = readl(&priv->reg->clkcr);
  155. /* Disable Clock */
  156. rval &= ~SUNXI_MMC_CLK_ENABLE;
  157. writel(rval, &priv->reg->clkcr);
  158. if (mmc_update_clk(priv))
  159. return -1;
  160. /* Set mod_clk to new rate */
  161. if (mmc_set_mod_clk(priv, mmc->clock))
  162. return -1;
  163. /* Clear internal divider */
  164. rval &= ~SUNXI_MMC_CLK_DIVIDER_MASK;
  165. writel(rval, &priv->reg->clkcr);
  166. #if defined(CONFIG_SUNXI_GEN_SUN6I) || defined(CONFIG_SUN50I_GEN_H6)
  167. /* A64 supports calibration of delays on MMC controller and we
  168. * have to set delay of zero before starting calibration.
  169. * Allwinner BSP driver sets a delay only in the case of
  170. * using HS400 which is not supported by mainline U-Boot or
  171. * Linux at the moment
  172. */
  173. if (sunxi_mmc_can_calibrate())
  174. writel(SUNXI_MMC_CAL_DL_SW_EN, &priv->reg->samp_dl);
  175. #endif
  176. /* Re-enable Clock */
  177. rval |= SUNXI_MMC_CLK_ENABLE;
  178. writel(rval, &priv->reg->clkcr);
  179. if (mmc_update_clk(priv))
  180. return -1;
  181. return 0;
  182. }
  183. static int sunxi_mmc_set_ios_common(struct sunxi_mmc_priv *priv,
  184. struct mmc *mmc)
  185. {
  186. debug("set ios: bus_width: %x, clock: %d\n",
  187. mmc->bus_width, mmc->clock);
  188. /* Change clock first */
  189. if (mmc->clock && mmc_config_clock(priv, mmc) != 0) {
  190. priv->fatal_err = 1;
  191. return -EINVAL;
  192. }
  193. /* Change bus width */
  194. if (mmc->bus_width == 8)
  195. writel(0x2, &priv->reg->width);
  196. else if (mmc->bus_width == 4)
  197. writel(0x1, &priv->reg->width);
  198. else
  199. writel(0x0, &priv->reg->width);
  200. return 0;
  201. }
  202. static int mmc_trans_data_by_cpu(struct sunxi_mmc_priv *priv, struct mmc *mmc,
  203. struct mmc_data *data)
  204. {
  205. const int reading = !!(data->flags & MMC_DATA_READ);
  206. const uint32_t status_bit = reading ? SUNXI_MMC_STATUS_FIFO_EMPTY :
  207. SUNXI_MMC_STATUS_FIFO_FULL;
  208. unsigned i;
  209. unsigned *buff = (unsigned int *)(reading ? data->dest : data->src);
  210. unsigned word_cnt = (data->blocksize * data->blocks) >> 2;
  211. unsigned timeout_msecs = word_cnt >> 6;
  212. uint32_t status;
  213. unsigned long start;
  214. if (timeout_msecs < 2000)
  215. timeout_msecs = 2000;
  216. /* Always read / write data through the CPU */
  217. setbits_le32(&priv->reg->gctrl, SUNXI_MMC_GCTRL_ACCESS_BY_AHB);
  218. start = get_timer(0);
  219. for (i = 0; i < word_cnt;) {
  220. unsigned int in_fifo;
  221. while ((status = readl(&priv->reg->status)) & status_bit) {
  222. if (get_timer(start) > timeout_msecs)
  223. return -1;
  224. }
  225. /*
  226. * For writing we do not easily know the FIFO size, so have
  227. * to check the FIFO status after every word written.
  228. * TODO: For optimisation we could work out a minimum FIFO
  229. * size across all SoCs, and use that together with the current
  230. * fill level to write chunks of words.
  231. */
  232. if (!reading) {
  233. writel(buff[i++], &priv->reg->fifo);
  234. continue;
  235. }
  236. /*
  237. * The status register holds the current FIFO level, so we
  238. * can be sure to collect as many words from the FIFO
  239. * register without checking the status register after every
  240. * read. That saves half of the costly MMIO reads, effectively
  241. * doubling the read performance.
  242. * Some SoCs (A20) report a level of 0 if the FIFO is
  243. * completely full (value masked out?). Use a safe minimal
  244. * FIFO size in this case.
  245. */
  246. in_fifo = SUNXI_MMC_STATUS_FIFO_LEVEL(status);
  247. if (in_fifo == 0 && (status & SUNXI_MMC_STATUS_FIFO_FULL))
  248. in_fifo = 32;
  249. for (; in_fifo > 0; in_fifo--)
  250. buff[i++] = readl_relaxed(&priv->reg->fifo);
  251. dmb();
  252. }
  253. return 0;
  254. }
  255. static int mmc_rint_wait(struct sunxi_mmc_priv *priv, struct mmc *mmc,
  256. uint timeout_msecs, uint done_bit, const char *what)
  257. {
  258. unsigned int status;
  259. unsigned long start = get_timer(0);
  260. do {
  261. status = readl(&priv->reg->rint);
  262. if ((get_timer(start) > timeout_msecs) ||
  263. (status & SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT)) {
  264. debug("%s timeout %x\n", what,
  265. status & SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT);
  266. return -ETIMEDOUT;
  267. }
  268. } while (!(status & done_bit));
  269. return 0;
  270. }
  271. static int sunxi_mmc_send_cmd_common(struct sunxi_mmc_priv *priv,
  272. struct mmc *mmc, struct mmc_cmd *cmd,
  273. struct mmc_data *data)
  274. {
  275. unsigned int cmdval = SUNXI_MMC_CMD_START;
  276. unsigned int timeout_msecs;
  277. int error = 0;
  278. unsigned int status = 0;
  279. unsigned int bytecnt = 0;
  280. if (priv->fatal_err)
  281. return -1;
  282. if (cmd->resp_type & MMC_RSP_BUSY)
  283. debug("mmc cmd %d check rsp busy\n", cmd->cmdidx);
  284. if (cmd->cmdidx == 12)
  285. return 0;
  286. if (!cmd->cmdidx)
  287. cmdval |= SUNXI_MMC_CMD_SEND_INIT_SEQ;
  288. if (cmd->resp_type & MMC_RSP_PRESENT)
  289. cmdval |= SUNXI_MMC_CMD_RESP_EXPIRE;
  290. if (cmd->resp_type & MMC_RSP_136)
  291. cmdval |= SUNXI_MMC_CMD_LONG_RESPONSE;
  292. if (cmd->resp_type & MMC_RSP_CRC)
  293. cmdval |= SUNXI_MMC_CMD_CHK_RESPONSE_CRC;
  294. if (data) {
  295. if ((u32)(long)data->dest & 0x3) {
  296. error = -1;
  297. goto out;
  298. }
  299. cmdval |= SUNXI_MMC_CMD_DATA_EXPIRE|SUNXI_MMC_CMD_WAIT_PRE_OVER;
  300. if (data->flags & MMC_DATA_WRITE)
  301. cmdval |= SUNXI_MMC_CMD_WRITE;
  302. if (data->blocks > 1)
  303. cmdval |= SUNXI_MMC_CMD_AUTO_STOP;
  304. writel(data->blocksize, &priv->reg->blksz);
  305. writel(data->blocks * data->blocksize, &priv->reg->bytecnt);
  306. }
  307. debug("mmc %d, cmd %d(0x%08x), arg 0x%08x\n", priv->mmc_no,
  308. cmd->cmdidx, cmdval | cmd->cmdidx, cmd->cmdarg);
  309. writel(cmd->cmdarg, &priv->reg->arg);
  310. if (!data)
  311. writel(cmdval | cmd->cmdidx, &priv->reg->cmd);
  312. /*
  313. * transfer data and check status
  314. * STATREG[2] : FIFO empty
  315. * STATREG[3] : FIFO full
  316. */
  317. if (data) {
  318. int ret = 0;
  319. bytecnt = data->blocksize * data->blocks;
  320. debug("trans data %d bytes\n", bytecnt);
  321. writel(cmdval | cmd->cmdidx, &priv->reg->cmd);
  322. ret = mmc_trans_data_by_cpu(priv, mmc, data);
  323. if (ret) {
  324. error = readl(&priv->reg->rint) &
  325. SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT;
  326. error = -ETIMEDOUT;
  327. goto out;
  328. }
  329. }
  330. error = mmc_rint_wait(priv, mmc, 1000, SUNXI_MMC_RINT_COMMAND_DONE,
  331. "cmd");
  332. if (error)
  333. goto out;
  334. if (data) {
  335. timeout_msecs = 120;
  336. debug("cacl timeout %x msec\n", timeout_msecs);
  337. error = mmc_rint_wait(priv, mmc, timeout_msecs,
  338. data->blocks > 1 ?
  339. SUNXI_MMC_RINT_AUTO_COMMAND_DONE :
  340. SUNXI_MMC_RINT_DATA_OVER,
  341. "data");
  342. if (error)
  343. goto out;
  344. }
  345. if (cmd->resp_type & MMC_RSP_BUSY) {
  346. unsigned long start = get_timer(0);
  347. timeout_msecs = 2000;
  348. do {
  349. status = readl(&priv->reg->status);
  350. if (get_timer(start) > timeout_msecs) {
  351. debug("busy timeout\n");
  352. error = -ETIMEDOUT;
  353. goto out;
  354. }
  355. } while (status & SUNXI_MMC_STATUS_CARD_DATA_BUSY);
  356. }
  357. if (cmd->resp_type & MMC_RSP_136) {
  358. cmd->response[0] = readl(&priv->reg->resp3);
  359. cmd->response[1] = readl(&priv->reg->resp2);
  360. cmd->response[2] = readl(&priv->reg->resp1);
  361. cmd->response[3] = readl(&priv->reg->resp0);
  362. debug("mmc resp 0x%08x 0x%08x 0x%08x 0x%08x\n",
  363. cmd->response[3], cmd->response[2],
  364. cmd->response[1], cmd->response[0]);
  365. } else {
  366. cmd->response[0] = readl(&priv->reg->resp0);
  367. debug("mmc resp 0x%08x\n", cmd->response[0]);
  368. }
  369. out:
  370. if (error < 0) {
  371. writel(SUNXI_MMC_GCTRL_RESET, &priv->reg->gctrl);
  372. mmc_update_clk(priv);
  373. }
  374. writel(0xffffffff, &priv->reg->rint);
  375. writel(readl(&priv->reg->gctrl) | SUNXI_MMC_GCTRL_FIFO_RESET,
  376. &priv->reg->gctrl);
  377. return error;
  378. }
  379. /* non-DM code here is used by the (ARM) SPL only */
  380. #if !CONFIG_IS_ENABLED(DM_MMC)
  381. /* support 4 mmc hosts */
  382. struct sunxi_mmc_priv mmc_host[4];
  383. static int mmc_resource_init(int sdc_no)
  384. {
  385. struct sunxi_mmc_priv *priv = &mmc_host[sdc_no];
  386. struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  387. debug("init mmc %d resource\n", sdc_no);
  388. switch (sdc_no) {
  389. case 0:
  390. priv->reg = (struct sunxi_mmc *)SUNXI_MMC0_BASE;
  391. priv->mclkreg = &ccm->sd0_clk_cfg;
  392. break;
  393. case 1:
  394. priv->reg = (struct sunxi_mmc *)SUNXI_MMC1_BASE;
  395. priv->mclkreg = &ccm->sd1_clk_cfg;
  396. break;
  397. #ifdef SUNXI_MMC2_BASE
  398. case 2:
  399. priv->reg = (struct sunxi_mmc *)SUNXI_MMC2_BASE;
  400. priv->mclkreg = &ccm->sd2_clk_cfg;
  401. break;
  402. #endif
  403. #ifdef SUNXI_MMC3_BASE
  404. case 3:
  405. priv->reg = (struct sunxi_mmc *)SUNXI_MMC3_BASE;
  406. priv->mclkreg = &ccm->sd3_clk_cfg;
  407. break;
  408. #endif
  409. default:
  410. printf("Wrong mmc number %d\n", sdc_no);
  411. return -1;
  412. }
  413. priv->mmc_no = sdc_no;
  414. return 0;
  415. }
  416. static int sunxi_mmc_core_init(struct mmc *mmc)
  417. {
  418. struct sunxi_mmc_priv *priv = mmc->priv;
  419. /* Reset controller */
  420. writel(SUNXI_MMC_GCTRL_RESET, &priv->reg->gctrl);
  421. udelay(1000);
  422. return 0;
  423. }
  424. static int sunxi_mmc_set_ios_legacy(struct mmc *mmc)
  425. {
  426. struct sunxi_mmc_priv *priv = mmc->priv;
  427. return sunxi_mmc_set_ios_common(priv, mmc);
  428. }
  429. static int sunxi_mmc_send_cmd_legacy(struct mmc *mmc, struct mmc_cmd *cmd,
  430. struct mmc_data *data)
  431. {
  432. struct sunxi_mmc_priv *priv = mmc->priv;
  433. return sunxi_mmc_send_cmd_common(priv, mmc, cmd, data);
  434. }
  435. /* .getcd is not needed by the SPL */
  436. static const struct mmc_ops sunxi_mmc_ops = {
  437. .send_cmd = sunxi_mmc_send_cmd_legacy,
  438. .set_ios = sunxi_mmc_set_ios_legacy,
  439. .init = sunxi_mmc_core_init,
  440. };
  441. struct mmc *sunxi_mmc_init(int sdc_no)
  442. {
  443. struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  444. struct sunxi_mmc_priv *priv = &mmc_host[sdc_no];
  445. struct mmc_config *cfg = &priv->cfg;
  446. int ret;
  447. memset(priv, '\0', sizeof(struct sunxi_mmc_priv));
  448. cfg->name = "SUNXI SD/MMC";
  449. cfg->ops = &sunxi_mmc_ops;
  450. cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
  451. cfg->host_caps = MMC_MODE_4BIT;
  452. if ((IS_ENABLED(CONFIG_MACH_SUN50I) || IS_ENABLED(CONFIG_MACH_SUN8I) ||
  453. IS_ENABLED(CONFIG_SUN50I_GEN_H6)) && (sdc_no == 2))
  454. cfg->host_caps = MMC_MODE_8BIT;
  455. cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
  456. cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
  457. cfg->f_min = 400000;
  458. cfg->f_max = 52000000;
  459. if (mmc_resource_init(sdc_no) != 0)
  460. return NULL;
  461. /* config ahb clock */
  462. debug("init mmc %d clock and io\n", sdc_no);
  463. #if !defined(CONFIG_SUN50I_GEN_H6)
  464. setbits_le32(&ccm->ahb_gate0, 1 << AHB_GATE_OFFSET_MMC(sdc_no));
  465. #ifdef CONFIG_SUNXI_GEN_SUN6I
  466. /* unassert reset */
  467. setbits_le32(&ccm->ahb_reset0_cfg, 1 << AHB_RESET_OFFSET_MMC(sdc_no));
  468. #endif
  469. #if defined(CONFIG_MACH_SUN9I)
  470. /* sun9i has a mmc-common module, also set the gate and reset there */
  471. writel(SUNXI_MMC_COMMON_CLK_GATE | SUNXI_MMC_COMMON_RESET,
  472. SUNXI_MMC_COMMON_BASE + 4 * sdc_no);
  473. #endif
  474. #else /* CONFIG_SUN50I_GEN_H6 */
  475. setbits_le32(&ccm->sd_gate_reset, 1 << sdc_no);
  476. /* unassert reset */
  477. setbits_le32(&ccm->sd_gate_reset, 1 << (RESET_SHIFT + sdc_no));
  478. #endif
  479. ret = mmc_set_mod_clk(priv, 24000000);
  480. if (ret)
  481. return NULL;
  482. return mmc_create(cfg, priv);
  483. }
  484. #else /* CONFIG_DM_MMC code below, as used by U-Boot proper */
  485. static int sunxi_mmc_set_ios(struct udevice *dev)
  486. {
  487. struct sunxi_mmc_plat *plat = dev_get_plat(dev);
  488. struct sunxi_mmc_priv *priv = dev_get_priv(dev);
  489. return sunxi_mmc_set_ios_common(priv, &plat->mmc);
  490. }
  491. static int sunxi_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
  492. struct mmc_data *data)
  493. {
  494. struct sunxi_mmc_plat *plat = dev_get_plat(dev);
  495. struct sunxi_mmc_priv *priv = dev_get_priv(dev);
  496. return sunxi_mmc_send_cmd_common(priv, &plat->mmc, cmd, data);
  497. }
  498. static int sunxi_mmc_getcd(struct udevice *dev)
  499. {
  500. struct mmc *mmc = mmc_get_mmc_dev(dev);
  501. struct sunxi_mmc_priv *priv = dev_get_priv(dev);
  502. /* If polling, assume that the card is always present. */
  503. if ((mmc->cfg->host_caps & MMC_CAP_NONREMOVABLE) ||
  504. (mmc->cfg->host_caps & MMC_CAP_NEEDS_POLL))
  505. return 1;
  506. if (dm_gpio_is_valid(&priv->cd_gpio)) {
  507. int cd_state = dm_gpio_get_value(&priv->cd_gpio);
  508. if (mmc->cfg->host_caps & MMC_CAP_CD_ACTIVE_HIGH)
  509. return !cd_state;
  510. else
  511. return cd_state;
  512. }
  513. return 1;
  514. }
  515. static const struct dm_mmc_ops sunxi_mmc_ops = {
  516. .send_cmd = sunxi_mmc_send_cmd,
  517. .set_ios = sunxi_mmc_set_ios,
  518. .get_cd = sunxi_mmc_getcd,
  519. };
  520. static unsigned get_mclk_offset(void)
  521. {
  522. if (IS_ENABLED(CONFIG_MACH_SUN9I_A80))
  523. return 0x410;
  524. if (IS_ENABLED(CONFIG_SUN50I_GEN_H6))
  525. return 0x830;
  526. return 0x88;
  527. };
  528. static int sunxi_mmc_probe(struct udevice *dev)
  529. {
  530. struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
  531. struct sunxi_mmc_plat *plat = dev_get_plat(dev);
  532. struct sunxi_mmc_priv *priv = dev_get_priv(dev);
  533. struct reset_ctl_bulk reset_bulk;
  534. struct clk gate_clk;
  535. struct mmc_config *cfg = &plat->cfg;
  536. struct ofnode_phandle_args args;
  537. u32 *ccu_reg;
  538. int ret;
  539. cfg->name = dev->name;
  540. cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
  541. cfg->host_caps = MMC_MODE_HS_52MHz | MMC_MODE_HS;
  542. cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
  543. cfg->f_min = 400000;
  544. cfg->f_max = 52000000;
  545. ret = mmc_of_parse(dev, cfg);
  546. if (ret)
  547. return ret;
  548. priv->reg = dev_read_addr_ptr(dev);
  549. /* We don't have a sunxi clock driver so find the clock address here */
  550. ret = dev_read_phandle_with_args(dev, "clocks", "#clock-cells", 0,
  551. 1, &args);
  552. if (ret)
  553. return ret;
  554. ccu_reg = (u32 *)(uintptr_t)ofnode_get_addr(args.node);
  555. priv->mmc_no = ((uintptr_t)priv->reg - SUNXI_MMC0_BASE) / 0x1000;
  556. priv->mclkreg = (void *)ccu_reg + get_mclk_offset() + priv->mmc_no * 4;
  557. ret = clk_get_by_name(dev, "ahb", &gate_clk);
  558. if (!ret)
  559. clk_enable(&gate_clk);
  560. ret = reset_get_bulk(dev, &reset_bulk);
  561. if (!ret)
  562. reset_deassert_bulk(&reset_bulk);
  563. ret = mmc_set_mod_clk(priv, 24000000);
  564. if (ret)
  565. return ret;
  566. /* This GPIO is optional */
  567. gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio,
  568. GPIOD_IS_IN | GPIOD_PULL_UP);
  569. upriv->mmc = &plat->mmc;
  570. /* Reset controller */
  571. writel(SUNXI_MMC_GCTRL_RESET, &priv->reg->gctrl);
  572. udelay(1000);
  573. return 0;
  574. }
  575. static int sunxi_mmc_bind(struct udevice *dev)
  576. {
  577. struct sunxi_mmc_plat *plat = dev_get_plat(dev);
  578. return mmc_bind(dev, &plat->mmc, &plat->cfg);
  579. }
  580. static const struct udevice_id sunxi_mmc_ids[] = {
  581. { .compatible = "allwinner,sun4i-a10-mmc" },
  582. { .compatible = "allwinner,sun5i-a13-mmc" },
  583. { .compatible = "allwinner,sun7i-a20-mmc" },
  584. { .compatible = "allwinner,sun8i-a83t-emmc" },
  585. { .compatible = "allwinner,sun9i-a80-mmc" },
  586. { .compatible = "allwinner,sun50i-a64-mmc" },
  587. { .compatible = "allwinner,sun50i-a64-emmc" },
  588. { .compatible = "allwinner,sun50i-h6-mmc" },
  589. { .compatible = "allwinner,sun50i-h6-emmc" },
  590. { .compatible = "allwinner,sun50i-a100-mmc" },
  591. { .compatible = "allwinner,sun50i-a100-emmc" },
  592. { /* sentinel */ }
  593. };
  594. U_BOOT_DRIVER(sunxi_mmc_drv) = {
  595. .name = "sunxi_mmc",
  596. .id = UCLASS_MMC,
  597. .of_match = sunxi_mmc_ids,
  598. .bind = sunxi_mmc_bind,
  599. .probe = sunxi_mmc_probe,
  600. .ops = &sunxi_mmc_ops,
  601. .plat_auto = sizeof(struct sunxi_mmc_plat),
  602. .priv_auto = sizeof(struct sunxi_mmc_priv),
  603. };
  604. #endif