sunxi_mmc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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. #include <common.h>
  10. #include <dm.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_plat {
  21. struct mmc_config cfg;
  22. struct mmc mmc;
  23. };
  24. struct sunxi_mmc_priv {
  25. unsigned mmc_no;
  26. uint32_t *mclkreg;
  27. unsigned fatal_err;
  28. struct gpio_desc cd_gpio; /* Change Detect GPIO */
  29. int cd_inverted; /* Inverted Card Detect */
  30. struct sunxi_mmc *reg;
  31. struct mmc_config cfg;
  32. };
  33. #if !CONFIG_IS_ENABLED(DM_MMC)
  34. /* support 4 mmc hosts */
  35. struct sunxi_mmc_priv mmc_host[4];
  36. static int sunxi_mmc_getcd_gpio(int sdc_no)
  37. {
  38. switch (sdc_no) {
  39. case 0: return sunxi_name_to_gpio(CONFIG_MMC0_CD_PIN);
  40. case 1: return sunxi_name_to_gpio(CONFIG_MMC1_CD_PIN);
  41. case 2: return sunxi_name_to_gpio(CONFIG_MMC2_CD_PIN);
  42. case 3: return sunxi_name_to_gpio(CONFIG_MMC3_CD_PIN);
  43. }
  44. return -EINVAL;
  45. }
  46. static int mmc_resource_init(int sdc_no)
  47. {
  48. struct sunxi_mmc_priv *priv = &mmc_host[sdc_no];
  49. struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  50. int cd_pin, ret = 0;
  51. debug("init mmc %d resource\n", sdc_no);
  52. switch (sdc_no) {
  53. case 0:
  54. priv->reg = (struct sunxi_mmc *)SUNXI_MMC0_BASE;
  55. priv->mclkreg = &ccm->sd0_clk_cfg;
  56. break;
  57. case 1:
  58. priv->reg = (struct sunxi_mmc *)SUNXI_MMC1_BASE;
  59. priv->mclkreg = &ccm->sd1_clk_cfg;
  60. break;
  61. case 2:
  62. priv->reg = (struct sunxi_mmc *)SUNXI_MMC2_BASE;
  63. priv->mclkreg = &ccm->sd2_clk_cfg;
  64. break;
  65. #ifdef SUNXI_MMC3_BASE
  66. case 3:
  67. priv->reg = (struct sunxi_mmc *)SUNXI_MMC3_BASE;
  68. priv->mclkreg = &ccm->sd3_clk_cfg;
  69. break;
  70. #endif
  71. default:
  72. printf("Wrong mmc number %d\n", sdc_no);
  73. return -1;
  74. }
  75. priv->mmc_no = sdc_no;
  76. cd_pin = sunxi_mmc_getcd_gpio(sdc_no);
  77. if (cd_pin >= 0) {
  78. ret = gpio_request(cd_pin, "mmc_cd");
  79. if (!ret) {
  80. sunxi_gpio_set_pull(cd_pin, SUNXI_GPIO_PULL_UP);
  81. ret = gpio_direction_input(cd_pin);
  82. }
  83. }
  84. return ret;
  85. }
  86. #endif
  87. static int mmc_set_mod_clk(struct sunxi_mmc_priv *priv, unsigned int hz)
  88. {
  89. unsigned int pll, pll_hz, div, n, oclk_dly, sclk_dly;
  90. bool new_mode = false;
  91. u32 val = 0;
  92. if (IS_ENABLED(CONFIG_MMC_SUNXI_HAS_NEW_MODE) && (priv->mmc_no == 2))
  93. new_mode = true;
  94. /*
  95. * The MMC clock has an extra /2 post-divider when operating in the new
  96. * mode.
  97. */
  98. if (new_mode)
  99. hz = hz * 2;
  100. if (hz <= 24000000) {
  101. pll = CCM_MMC_CTRL_OSCM24;
  102. pll_hz = 24000000;
  103. } else {
  104. #ifdef CONFIG_MACH_SUN9I
  105. pll = CCM_MMC_CTRL_PLL_PERIPH0;
  106. pll_hz = clock_get_pll4_periph0();
  107. #elif defined(CONFIG_MACH_SUN50I_H6)
  108. pll = CCM_MMC_CTRL_PLL6X2;
  109. pll_hz = clock_get_pll6() * 2;
  110. #else
  111. pll = CCM_MMC_CTRL_PLL6;
  112. pll_hz = clock_get_pll6();
  113. #endif
  114. }
  115. div = pll_hz / hz;
  116. if (pll_hz % hz)
  117. div++;
  118. n = 0;
  119. while (div > 16) {
  120. n++;
  121. div = (div + 1) / 2;
  122. }
  123. if (n > 3) {
  124. printf("mmc %u error cannot set clock to %u\n", priv->mmc_no,
  125. hz);
  126. return -1;
  127. }
  128. /* determine delays */
  129. if (hz <= 400000) {
  130. oclk_dly = 0;
  131. sclk_dly = 0;
  132. } else if (hz <= 25000000) {
  133. oclk_dly = 0;
  134. sclk_dly = 5;
  135. #ifdef CONFIG_MACH_SUN9I
  136. } else if (hz <= 52000000) {
  137. oclk_dly = 5;
  138. sclk_dly = 4;
  139. } else {
  140. /* hz > 52000000 */
  141. oclk_dly = 2;
  142. sclk_dly = 4;
  143. #else
  144. } else if (hz <= 52000000) {
  145. oclk_dly = 3;
  146. sclk_dly = 4;
  147. } else {
  148. /* hz > 52000000 */
  149. oclk_dly = 1;
  150. sclk_dly = 4;
  151. #endif
  152. }
  153. if (new_mode) {
  154. #ifdef CONFIG_MMC_SUNXI_HAS_NEW_MODE
  155. val = CCM_MMC_CTRL_MODE_SEL_NEW;
  156. setbits_le32(&priv->reg->ntsr, SUNXI_MMC_NTSR_MODE_SEL_NEW);
  157. #endif
  158. } else {
  159. val = CCM_MMC_CTRL_OCLK_DLY(oclk_dly) |
  160. CCM_MMC_CTRL_SCLK_DLY(sclk_dly);
  161. }
  162. writel(CCM_MMC_CTRL_ENABLE| pll | CCM_MMC_CTRL_N(n) |
  163. CCM_MMC_CTRL_M(div) | val, priv->mclkreg);
  164. debug("mmc %u set mod-clk req %u parent %u n %u m %u rate %u\n",
  165. priv->mmc_no, hz, pll_hz, 1u << n, div, pll_hz / (1u << n) / div);
  166. return 0;
  167. }
  168. static int mmc_update_clk(struct sunxi_mmc_priv *priv)
  169. {
  170. unsigned int cmd;
  171. unsigned timeout_msecs = 2000;
  172. unsigned long start = get_timer(0);
  173. cmd = SUNXI_MMC_CMD_START |
  174. SUNXI_MMC_CMD_UPCLK_ONLY |
  175. SUNXI_MMC_CMD_WAIT_PRE_OVER;
  176. writel(cmd, &priv->reg->cmd);
  177. while (readl(&priv->reg->cmd) & SUNXI_MMC_CMD_START) {
  178. if (get_timer(start) > timeout_msecs)
  179. return -1;
  180. }
  181. /* clock update sets various irq status bits, clear these */
  182. writel(readl(&priv->reg->rint), &priv->reg->rint);
  183. return 0;
  184. }
  185. static int mmc_config_clock(struct sunxi_mmc_priv *priv, struct mmc *mmc)
  186. {
  187. unsigned rval = readl(&priv->reg->clkcr);
  188. /* Disable Clock */
  189. rval &= ~SUNXI_MMC_CLK_ENABLE;
  190. writel(rval, &priv->reg->clkcr);
  191. if (mmc_update_clk(priv))
  192. return -1;
  193. /* Set mod_clk to new rate */
  194. if (mmc_set_mod_clk(priv, mmc->clock))
  195. return -1;
  196. /* Clear internal divider */
  197. rval &= ~SUNXI_MMC_CLK_DIVIDER_MASK;
  198. writel(rval, &priv->reg->clkcr);
  199. /* Re-enable Clock */
  200. rval |= SUNXI_MMC_CLK_ENABLE;
  201. writel(rval, &priv->reg->clkcr);
  202. if (mmc_update_clk(priv))
  203. return -1;
  204. return 0;
  205. }
  206. static int sunxi_mmc_set_ios_common(struct sunxi_mmc_priv *priv,
  207. struct mmc *mmc)
  208. {
  209. debug("set ios: bus_width: %x, clock: %d\n",
  210. mmc->bus_width, mmc->clock);
  211. /* Change clock first */
  212. if (mmc->clock && mmc_config_clock(priv, mmc) != 0) {
  213. priv->fatal_err = 1;
  214. return -EINVAL;
  215. }
  216. /* Change bus width */
  217. if (mmc->bus_width == 8)
  218. writel(0x2, &priv->reg->width);
  219. else if (mmc->bus_width == 4)
  220. writel(0x1, &priv->reg->width);
  221. else
  222. writel(0x0, &priv->reg->width);
  223. return 0;
  224. }
  225. #if !CONFIG_IS_ENABLED(DM_MMC)
  226. static int sunxi_mmc_core_init(struct mmc *mmc)
  227. {
  228. struct sunxi_mmc_priv *priv = mmc->priv;
  229. /* Reset controller */
  230. writel(SUNXI_MMC_GCTRL_RESET, &priv->reg->gctrl);
  231. udelay(1000);
  232. return 0;
  233. }
  234. #endif
  235. static int mmc_trans_data_by_cpu(struct sunxi_mmc_priv *priv, struct mmc *mmc,
  236. struct mmc_data *data)
  237. {
  238. const int reading = !!(data->flags & MMC_DATA_READ);
  239. const uint32_t status_bit = reading ? SUNXI_MMC_STATUS_FIFO_EMPTY :
  240. SUNXI_MMC_STATUS_FIFO_FULL;
  241. unsigned i;
  242. unsigned *buff = (unsigned int *)(reading ? data->dest : data->src);
  243. unsigned byte_cnt = data->blocksize * data->blocks;
  244. unsigned timeout_msecs = byte_cnt >> 8;
  245. unsigned long start;
  246. if (timeout_msecs < 2000)
  247. timeout_msecs = 2000;
  248. /* Always read / write data through the CPU */
  249. setbits_le32(&priv->reg->gctrl, SUNXI_MMC_GCTRL_ACCESS_BY_AHB);
  250. start = get_timer(0);
  251. for (i = 0; i < (byte_cnt >> 2); i++) {
  252. while (readl(&priv->reg->status) & status_bit) {
  253. if (get_timer(start) > timeout_msecs)
  254. return -1;
  255. }
  256. if (reading)
  257. buff[i] = readl(&priv->reg->fifo);
  258. else
  259. writel(buff[i], &priv->reg->fifo);
  260. }
  261. return 0;
  262. }
  263. static int mmc_rint_wait(struct sunxi_mmc_priv *priv, struct mmc *mmc,
  264. uint timeout_msecs, uint done_bit, const char *what)
  265. {
  266. unsigned int status;
  267. unsigned long start = get_timer(0);
  268. do {
  269. status = readl(&priv->reg->rint);
  270. if ((get_timer(start) > timeout_msecs) ||
  271. (status & SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT)) {
  272. debug("%s timeout %x\n", what,
  273. status & SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT);
  274. return -ETIMEDOUT;
  275. }
  276. } while (!(status & done_bit));
  277. return 0;
  278. }
  279. static int sunxi_mmc_send_cmd_common(struct sunxi_mmc_priv *priv,
  280. struct mmc *mmc, struct mmc_cmd *cmd,
  281. struct mmc_data *data)
  282. {
  283. unsigned int cmdval = SUNXI_MMC_CMD_START;
  284. unsigned int timeout_msecs;
  285. int error = 0;
  286. unsigned int status = 0;
  287. unsigned int bytecnt = 0;
  288. if (priv->fatal_err)
  289. return -1;
  290. if (cmd->resp_type & MMC_RSP_BUSY)
  291. debug("mmc cmd %d check rsp busy\n", cmd->cmdidx);
  292. if (cmd->cmdidx == 12)
  293. return 0;
  294. if (!cmd->cmdidx)
  295. cmdval |= SUNXI_MMC_CMD_SEND_INIT_SEQ;
  296. if (cmd->resp_type & MMC_RSP_PRESENT)
  297. cmdval |= SUNXI_MMC_CMD_RESP_EXPIRE;
  298. if (cmd->resp_type & MMC_RSP_136)
  299. cmdval |= SUNXI_MMC_CMD_LONG_RESPONSE;
  300. if (cmd->resp_type & MMC_RSP_CRC)
  301. cmdval |= SUNXI_MMC_CMD_CHK_RESPONSE_CRC;
  302. if (data) {
  303. if ((u32)(long)data->dest & 0x3) {
  304. error = -1;
  305. goto out;
  306. }
  307. cmdval |= SUNXI_MMC_CMD_DATA_EXPIRE|SUNXI_MMC_CMD_WAIT_PRE_OVER;
  308. if (data->flags & MMC_DATA_WRITE)
  309. cmdval |= SUNXI_MMC_CMD_WRITE;
  310. if (data->blocks > 1)
  311. cmdval |= SUNXI_MMC_CMD_AUTO_STOP;
  312. writel(data->blocksize, &priv->reg->blksz);
  313. writel(data->blocks * data->blocksize, &priv->reg->bytecnt);
  314. }
  315. debug("mmc %d, cmd %d(0x%08x), arg 0x%08x\n", priv->mmc_no,
  316. cmd->cmdidx, cmdval | cmd->cmdidx, cmd->cmdarg);
  317. writel(cmd->cmdarg, &priv->reg->arg);
  318. if (!data)
  319. writel(cmdval | cmd->cmdidx, &priv->reg->cmd);
  320. /*
  321. * transfer data and check status
  322. * STATREG[2] : FIFO empty
  323. * STATREG[3] : FIFO full
  324. */
  325. if (data) {
  326. int ret = 0;
  327. bytecnt = data->blocksize * data->blocks;
  328. debug("trans data %d bytes\n", bytecnt);
  329. writel(cmdval | cmd->cmdidx, &priv->reg->cmd);
  330. ret = mmc_trans_data_by_cpu(priv, mmc, data);
  331. if (ret) {
  332. error = readl(&priv->reg->rint) &
  333. SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT;
  334. error = -ETIMEDOUT;
  335. goto out;
  336. }
  337. }
  338. error = mmc_rint_wait(priv, mmc, 1000, SUNXI_MMC_RINT_COMMAND_DONE,
  339. "cmd");
  340. if (error)
  341. goto out;
  342. if (data) {
  343. timeout_msecs = 120;
  344. debug("cacl timeout %x msec\n", timeout_msecs);
  345. error = mmc_rint_wait(priv, mmc, timeout_msecs,
  346. data->blocks > 1 ?
  347. SUNXI_MMC_RINT_AUTO_COMMAND_DONE :
  348. SUNXI_MMC_RINT_DATA_OVER,
  349. "data");
  350. if (error)
  351. goto out;
  352. }
  353. if (cmd->resp_type & MMC_RSP_BUSY) {
  354. unsigned long start = get_timer(0);
  355. timeout_msecs = 2000;
  356. do {
  357. status = readl(&priv->reg->status);
  358. if (get_timer(start) > timeout_msecs) {
  359. debug("busy timeout\n");
  360. error = -ETIMEDOUT;
  361. goto out;
  362. }
  363. } while (status & SUNXI_MMC_STATUS_CARD_DATA_BUSY);
  364. }
  365. if (cmd->resp_type & MMC_RSP_136) {
  366. cmd->response[0] = readl(&priv->reg->resp3);
  367. cmd->response[1] = readl(&priv->reg->resp2);
  368. cmd->response[2] = readl(&priv->reg->resp1);
  369. cmd->response[3] = readl(&priv->reg->resp0);
  370. debug("mmc resp 0x%08x 0x%08x 0x%08x 0x%08x\n",
  371. cmd->response[3], cmd->response[2],
  372. cmd->response[1], cmd->response[0]);
  373. } else {
  374. cmd->response[0] = readl(&priv->reg->resp0);
  375. debug("mmc resp 0x%08x\n", cmd->response[0]);
  376. }
  377. out:
  378. if (error < 0) {
  379. writel(SUNXI_MMC_GCTRL_RESET, &priv->reg->gctrl);
  380. mmc_update_clk(priv);
  381. }
  382. writel(0xffffffff, &priv->reg->rint);
  383. writel(readl(&priv->reg->gctrl) | SUNXI_MMC_GCTRL_FIFO_RESET,
  384. &priv->reg->gctrl);
  385. return error;
  386. }
  387. #if !CONFIG_IS_ENABLED(DM_MMC)
  388. static int sunxi_mmc_set_ios_legacy(struct mmc *mmc)
  389. {
  390. struct sunxi_mmc_priv *priv = mmc->priv;
  391. return sunxi_mmc_set_ios_common(priv, mmc);
  392. }
  393. static int sunxi_mmc_send_cmd_legacy(struct mmc *mmc, struct mmc_cmd *cmd,
  394. struct mmc_data *data)
  395. {
  396. struct sunxi_mmc_priv *priv = mmc->priv;
  397. return sunxi_mmc_send_cmd_common(priv, mmc, cmd, data);
  398. }
  399. static int sunxi_mmc_getcd_legacy(struct mmc *mmc)
  400. {
  401. struct sunxi_mmc_priv *priv = mmc->priv;
  402. int cd_pin;
  403. cd_pin = sunxi_mmc_getcd_gpio(priv->mmc_no);
  404. if (cd_pin < 0)
  405. return 1;
  406. return !gpio_get_value(cd_pin);
  407. }
  408. static const struct mmc_ops sunxi_mmc_ops = {
  409. .send_cmd = sunxi_mmc_send_cmd_legacy,
  410. .set_ios = sunxi_mmc_set_ios_legacy,
  411. .init = sunxi_mmc_core_init,
  412. .getcd = sunxi_mmc_getcd_legacy,
  413. };
  414. struct mmc *sunxi_mmc_init(int sdc_no)
  415. {
  416. struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  417. struct sunxi_mmc_priv *priv = &mmc_host[sdc_no];
  418. struct mmc_config *cfg = &priv->cfg;
  419. int ret;
  420. memset(priv, '\0', sizeof(struct sunxi_mmc_priv));
  421. cfg->name = "SUNXI SD/MMC";
  422. cfg->ops = &sunxi_mmc_ops;
  423. cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
  424. cfg->host_caps = MMC_MODE_4BIT;
  425. #if defined(CONFIG_MACH_SUN50I) || defined(CONFIG_MACH_SUN8I) || defined(CONFIG_MACH_SUN50I_H6)
  426. if (sdc_no == 2)
  427. cfg->host_caps = MMC_MODE_8BIT;
  428. #endif
  429. cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
  430. cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
  431. cfg->f_min = 400000;
  432. cfg->f_max = 52000000;
  433. if (mmc_resource_init(sdc_no) != 0)
  434. return NULL;
  435. /* config ahb clock */
  436. debug("init mmc %d clock and io\n", sdc_no);
  437. #if !defined(CONFIG_MACH_SUN50I_H6)
  438. setbits_le32(&ccm->ahb_gate0, 1 << AHB_GATE_OFFSET_MMC(sdc_no));
  439. #ifdef CONFIG_SUNXI_GEN_SUN6I
  440. /* unassert reset */
  441. setbits_le32(&ccm->ahb_reset0_cfg, 1 << AHB_RESET_OFFSET_MMC(sdc_no));
  442. #endif
  443. #if defined(CONFIG_MACH_SUN9I)
  444. /* sun9i has a mmc-common module, also set the gate and reset there */
  445. writel(SUNXI_MMC_COMMON_CLK_GATE | SUNXI_MMC_COMMON_RESET,
  446. SUNXI_MMC_COMMON_BASE + 4 * sdc_no);
  447. #endif
  448. #else /* CONFIG_MACH_SUN50I_H6 */
  449. setbits_le32(&ccm->sd_gate_reset, 1 << sdc_no);
  450. /* unassert reset */
  451. setbits_le32(&ccm->sd_gate_reset, 1 << (RESET_SHIFT + sdc_no));
  452. #endif
  453. ret = mmc_set_mod_clk(priv, 24000000);
  454. if (ret)
  455. return NULL;
  456. return mmc_create(cfg, priv);
  457. }
  458. #else
  459. static int sunxi_mmc_set_ios(struct udevice *dev)
  460. {
  461. struct sunxi_mmc_plat *plat = dev_get_platdata(dev);
  462. struct sunxi_mmc_priv *priv = dev_get_priv(dev);
  463. return sunxi_mmc_set_ios_common(priv, &plat->mmc);
  464. }
  465. static int sunxi_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
  466. struct mmc_data *data)
  467. {
  468. struct sunxi_mmc_plat *plat = dev_get_platdata(dev);
  469. struct sunxi_mmc_priv *priv = dev_get_priv(dev);
  470. return sunxi_mmc_send_cmd_common(priv, &plat->mmc, cmd, data);
  471. }
  472. static int sunxi_mmc_getcd(struct udevice *dev)
  473. {
  474. struct sunxi_mmc_priv *priv = dev_get_priv(dev);
  475. if (dm_gpio_is_valid(&priv->cd_gpio)) {
  476. int cd_state = dm_gpio_get_value(&priv->cd_gpio);
  477. return cd_state ^ priv->cd_inverted;
  478. }
  479. return 1;
  480. }
  481. static const struct dm_mmc_ops sunxi_mmc_ops = {
  482. .send_cmd = sunxi_mmc_send_cmd,
  483. .set_ios = sunxi_mmc_set_ios,
  484. .get_cd = sunxi_mmc_getcd,
  485. };
  486. static int sunxi_mmc_probe(struct udevice *dev)
  487. {
  488. struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
  489. struct sunxi_mmc_plat *plat = dev_get_platdata(dev);
  490. struct sunxi_mmc_priv *priv = dev_get_priv(dev);
  491. struct mmc_config *cfg = &plat->cfg;
  492. struct ofnode_phandle_args args;
  493. u32 *gate_reg;
  494. int bus_width, ret;
  495. cfg->name = dev->name;
  496. bus_width = dev_read_u32_default(dev, "bus-width", 1);
  497. cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
  498. cfg->host_caps = 0;
  499. if (bus_width == 8)
  500. cfg->host_caps |= MMC_MODE_8BIT;
  501. if (bus_width >= 4)
  502. cfg->host_caps |= MMC_MODE_4BIT;
  503. cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
  504. cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
  505. cfg->f_min = 400000;
  506. cfg->f_max = 52000000;
  507. priv->reg = (void *)dev_read_addr(dev);
  508. /* We don't have a sunxi clock driver so find the clock address here */
  509. ret = dev_read_phandle_with_args(dev, "clocks", "#clock-cells", 0,
  510. 1, &args);
  511. if (ret)
  512. return ret;
  513. priv->mclkreg = (u32 *)ofnode_get_addr(args.node);
  514. ret = dev_read_phandle_with_args(dev, "clocks", "#clock-cells", 0,
  515. 0, &args);
  516. if (ret)
  517. return ret;
  518. gate_reg = (u32 *)ofnode_get_addr(args.node);
  519. setbits_le32(gate_reg, 1 << args.args[0]);
  520. priv->mmc_no = args.args[0] - 8;
  521. ret = mmc_set_mod_clk(priv, 24000000);
  522. if (ret)
  523. return ret;
  524. /* This GPIO is optional */
  525. if (!gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio,
  526. GPIOD_IS_IN)) {
  527. int cd_pin = gpio_get_number(&priv->cd_gpio);
  528. sunxi_gpio_set_pull(cd_pin, SUNXI_GPIO_PULL_UP);
  529. }
  530. /* Check if card detect is inverted */
  531. priv->cd_inverted = dev_read_bool(dev, "cd-inverted");
  532. upriv->mmc = &plat->mmc;
  533. /* Reset controller */
  534. writel(SUNXI_MMC_GCTRL_RESET, &priv->reg->gctrl);
  535. udelay(1000);
  536. return 0;
  537. }
  538. static int sunxi_mmc_bind(struct udevice *dev)
  539. {
  540. struct sunxi_mmc_plat *plat = dev_get_platdata(dev);
  541. return mmc_bind(dev, &plat->mmc, &plat->cfg);
  542. }
  543. static const struct udevice_id sunxi_mmc_ids[] = {
  544. { .compatible = "allwinner,sun4i-a10-mmc" },
  545. { .compatible = "allwinner,sun5i-a13-mmc" },
  546. { .compatible = "allwinner,sun7i-a20-mmc" },
  547. { }
  548. };
  549. U_BOOT_DRIVER(sunxi_mmc_drv) = {
  550. .name = "sunxi_mmc",
  551. .id = UCLASS_MMC,
  552. .of_match = sunxi_mmc_ids,
  553. .bind = sunxi_mmc_bind,
  554. .probe = sunxi_mmc_probe,
  555. .ops = &sunxi_mmc_ops,
  556. .platdata_auto_alloc_size = sizeof(struct sunxi_mmc_plat),
  557. .priv_auto_alloc_size = sizeof(struct sunxi_mmc_priv),
  558. };
  559. #endif