sunxi_mmc.c 15 KB

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