sunxi_mmc.c 17 KB

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