tegra_mmc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. /*
  2. * (C) Copyright 2009 SAMSUNG Electronics
  3. * Minkyu Kang <mk7.kang@samsung.com>
  4. * Jaehoon Chung <jh80.chung@samsung.com>
  5. * Portions Copyright 2011-2016 NVIDIA Corporation
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <bouncebuf.h>
  10. #include <common.h>
  11. #include <dm.h>
  12. #include <errno.h>
  13. #include <mmc.h>
  14. #include <asm/gpio.h>
  15. #include <asm/io.h>
  16. #include <asm/arch-tegra/tegra_mmc.h>
  17. DECLARE_GLOBAL_DATA_PTR;
  18. struct tegra_mmc_plat {
  19. struct mmc_config cfg;
  20. struct mmc mmc;
  21. };
  22. struct tegra_mmc_priv {
  23. struct tegra_mmc *reg;
  24. struct reset_ctl reset_ctl;
  25. struct clk clk;
  26. struct gpio_desc cd_gpio; /* Change Detect GPIO */
  27. struct gpio_desc pwr_gpio; /* Power GPIO */
  28. struct gpio_desc wp_gpio; /* Write Protect GPIO */
  29. unsigned int version; /* SDHCI spec. version */
  30. unsigned int clock; /* Current clock (MHz) */
  31. };
  32. static void tegra_mmc_set_power(struct tegra_mmc_priv *priv,
  33. unsigned short power)
  34. {
  35. u8 pwr = 0;
  36. debug("%s: power = %x\n", __func__, power);
  37. if (power != (unsigned short)-1) {
  38. switch (1 << power) {
  39. case MMC_VDD_165_195:
  40. pwr = TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V1_8;
  41. break;
  42. case MMC_VDD_29_30:
  43. case MMC_VDD_30_31:
  44. pwr = TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V3_0;
  45. break;
  46. case MMC_VDD_32_33:
  47. case MMC_VDD_33_34:
  48. pwr = TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V3_3;
  49. break;
  50. }
  51. }
  52. debug("%s: pwr = %X\n", __func__, pwr);
  53. /* Set the bus voltage first (if any) */
  54. writeb(pwr, &priv->reg->pwrcon);
  55. if (pwr == 0)
  56. return;
  57. /* Now enable bus power */
  58. pwr |= TEGRA_MMC_PWRCTL_SD_BUS_POWER;
  59. writeb(pwr, &priv->reg->pwrcon);
  60. }
  61. static void tegra_mmc_prepare_data(struct tegra_mmc_priv *priv,
  62. struct mmc_data *data,
  63. struct bounce_buffer *bbstate)
  64. {
  65. unsigned char ctrl;
  66. debug("buf: %p (%p), data->blocks: %u, data->blocksize: %u\n",
  67. bbstate->bounce_buffer, bbstate->user_buffer, data->blocks,
  68. data->blocksize);
  69. writel((u32)(unsigned long)bbstate->bounce_buffer, &priv->reg->sysad);
  70. /*
  71. * DMASEL[4:3]
  72. * 00 = Selects SDMA
  73. * 01 = Reserved
  74. * 10 = Selects 32-bit Address ADMA2
  75. * 11 = Selects 64-bit Address ADMA2
  76. */
  77. ctrl = readb(&priv->reg->hostctl);
  78. ctrl &= ~TEGRA_MMC_HOSTCTL_DMASEL_MASK;
  79. ctrl |= TEGRA_MMC_HOSTCTL_DMASEL_SDMA;
  80. writeb(ctrl, &priv->reg->hostctl);
  81. /* We do not handle DMA boundaries, so set it to max (512 KiB) */
  82. writew((7 << 12) | (data->blocksize & 0xFFF), &priv->reg->blksize);
  83. writew(data->blocks, &priv->reg->blkcnt);
  84. }
  85. static void tegra_mmc_set_transfer_mode(struct tegra_mmc_priv *priv,
  86. struct mmc_data *data)
  87. {
  88. unsigned short mode;
  89. debug(" mmc_set_transfer_mode called\n");
  90. /*
  91. * TRNMOD
  92. * MUL1SIN0[5] : Multi/Single Block Select
  93. * RD1WT0[4] : Data Transfer Direction Select
  94. * 1 = read
  95. * 0 = write
  96. * ENACMD12[2] : Auto CMD12 Enable
  97. * ENBLKCNT[1] : Block Count Enable
  98. * ENDMA[0] : DMA Enable
  99. */
  100. mode = (TEGRA_MMC_TRNMOD_DMA_ENABLE |
  101. TEGRA_MMC_TRNMOD_BLOCK_COUNT_ENABLE);
  102. if (data->blocks > 1)
  103. mode |= TEGRA_MMC_TRNMOD_MULTI_BLOCK_SELECT;
  104. if (data->flags & MMC_DATA_READ)
  105. mode |= TEGRA_MMC_TRNMOD_DATA_XFER_DIR_SEL_READ;
  106. writew(mode, &priv->reg->trnmod);
  107. }
  108. static int tegra_mmc_wait_inhibit(struct tegra_mmc_priv *priv,
  109. struct mmc_cmd *cmd,
  110. struct mmc_data *data,
  111. unsigned int timeout)
  112. {
  113. /*
  114. * PRNSTS
  115. * CMDINHDAT[1] : Command Inhibit (DAT)
  116. * CMDINHCMD[0] : Command Inhibit (CMD)
  117. */
  118. unsigned int mask = TEGRA_MMC_PRNSTS_CMD_INHIBIT_CMD;
  119. /*
  120. * We shouldn't wait for data inhibit for stop commands, even
  121. * though they might use busy signaling
  122. */
  123. if ((data == NULL) && (cmd->resp_type & MMC_RSP_BUSY))
  124. mask |= TEGRA_MMC_PRNSTS_CMD_INHIBIT_DAT;
  125. while (readl(&priv->reg->prnsts) & mask) {
  126. if (timeout == 0) {
  127. printf("%s: timeout error\n", __func__);
  128. return -1;
  129. }
  130. timeout--;
  131. udelay(1000);
  132. }
  133. return 0;
  134. }
  135. static int tegra_mmc_send_cmd_bounced(struct udevice *dev, struct mmc_cmd *cmd,
  136. struct mmc_data *data,
  137. struct bounce_buffer *bbstate)
  138. {
  139. struct tegra_mmc_priv *priv = dev_get_priv(dev);
  140. int flags, i;
  141. int result;
  142. unsigned int mask = 0;
  143. unsigned int retry = 0x100000;
  144. debug(" mmc_send_cmd called\n");
  145. result = tegra_mmc_wait_inhibit(priv, cmd, data, 10 /* ms */);
  146. if (result < 0)
  147. return result;
  148. if (data)
  149. tegra_mmc_prepare_data(priv, data, bbstate);
  150. debug("cmd->arg: %08x\n", cmd->cmdarg);
  151. writel(cmd->cmdarg, &priv->reg->argument);
  152. if (data)
  153. tegra_mmc_set_transfer_mode(priv, data);
  154. if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
  155. return -1;
  156. /*
  157. * CMDREG
  158. * CMDIDX[13:8] : Command index
  159. * DATAPRNT[5] : Data Present Select
  160. * ENCMDIDX[4] : Command Index Check Enable
  161. * ENCMDCRC[3] : Command CRC Check Enable
  162. * RSPTYP[1:0]
  163. * 00 = No Response
  164. * 01 = Length 136
  165. * 10 = Length 48
  166. * 11 = Length 48 Check busy after response
  167. */
  168. if (!(cmd->resp_type & MMC_RSP_PRESENT))
  169. flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_NO_RESPONSE;
  170. else if (cmd->resp_type & MMC_RSP_136)
  171. flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_136;
  172. else if (cmd->resp_type & MMC_RSP_BUSY)
  173. flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_48_BUSY;
  174. else
  175. flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_48;
  176. if (cmd->resp_type & MMC_RSP_CRC)
  177. flags |= TEGRA_MMC_TRNMOD_CMD_CRC_CHECK;
  178. if (cmd->resp_type & MMC_RSP_OPCODE)
  179. flags |= TEGRA_MMC_TRNMOD_CMD_INDEX_CHECK;
  180. if (data)
  181. flags |= TEGRA_MMC_TRNMOD_DATA_PRESENT_SELECT_DATA_TRANSFER;
  182. debug("cmd: %d\n", cmd->cmdidx);
  183. writew((cmd->cmdidx << 8) | flags, &priv->reg->cmdreg);
  184. for (i = 0; i < retry; i++) {
  185. mask = readl(&priv->reg->norintsts);
  186. /* Command Complete */
  187. if (mask & TEGRA_MMC_NORINTSTS_CMD_COMPLETE) {
  188. if (!data)
  189. writel(mask, &priv->reg->norintsts);
  190. break;
  191. }
  192. }
  193. if (i == retry) {
  194. printf("%s: waiting for status update\n", __func__);
  195. writel(mask, &priv->reg->norintsts);
  196. return -ETIMEDOUT;
  197. }
  198. if (mask & TEGRA_MMC_NORINTSTS_CMD_TIMEOUT) {
  199. /* Timeout Error */
  200. debug("timeout: %08x cmd %d\n", mask, cmd->cmdidx);
  201. writel(mask, &priv->reg->norintsts);
  202. return -ETIMEDOUT;
  203. } else if (mask & TEGRA_MMC_NORINTSTS_ERR_INTERRUPT) {
  204. /* Error Interrupt */
  205. debug("error: %08x cmd %d\n", mask, cmd->cmdidx);
  206. writel(mask, &priv->reg->norintsts);
  207. return -1;
  208. }
  209. if (cmd->resp_type & MMC_RSP_PRESENT) {
  210. if (cmd->resp_type & MMC_RSP_136) {
  211. /* CRC is stripped so we need to do some shifting. */
  212. for (i = 0; i < 4; i++) {
  213. unsigned long offset = (unsigned long)
  214. (&priv->reg->rspreg3 - i);
  215. cmd->response[i] = readl(offset) << 8;
  216. if (i != 3) {
  217. cmd->response[i] |=
  218. readb(offset - 1);
  219. }
  220. debug("cmd->resp[%d]: %08x\n",
  221. i, cmd->response[i]);
  222. }
  223. } else if (cmd->resp_type & MMC_RSP_BUSY) {
  224. for (i = 0; i < retry; i++) {
  225. /* PRNTDATA[23:20] : DAT[3:0] Line Signal */
  226. if (readl(&priv->reg->prnsts)
  227. & (1 << 20)) /* DAT[0] */
  228. break;
  229. }
  230. if (i == retry) {
  231. printf("%s: card is still busy\n", __func__);
  232. writel(mask, &priv->reg->norintsts);
  233. return -ETIMEDOUT;
  234. }
  235. cmd->response[0] = readl(&priv->reg->rspreg0);
  236. debug("cmd->resp[0]: %08x\n", cmd->response[0]);
  237. } else {
  238. cmd->response[0] = readl(&priv->reg->rspreg0);
  239. debug("cmd->resp[0]: %08x\n", cmd->response[0]);
  240. }
  241. }
  242. if (data) {
  243. unsigned long start = get_timer(0);
  244. while (1) {
  245. mask = readl(&priv->reg->norintsts);
  246. if (mask & TEGRA_MMC_NORINTSTS_ERR_INTERRUPT) {
  247. /* Error Interrupt */
  248. writel(mask, &priv->reg->norintsts);
  249. printf("%s: error during transfer: 0x%08x\n",
  250. __func__, mask);
  251. return -1;
  252. } else if (mask & TEGRA_MMC_NORINTSTS_DMA_INTERRUPT) {
  253. /*
  254. * DMA Interrupt, restart the transfer where
  255. * it was interrupted.
  256. */
  257. unsigned int address = readl(&priv->reg->sysad);
  258. debug("DMA end\n");
  259. writel(TEGRA_MMC_NORINTSTS_DMA_INTERRUPT,
  260. &priv->reg->norintsts);
  261. writel(address, &priv->reg->sysad);
  262. } else if (mask & TEGRA_MMC_NORINTSTS_XFER_COMPLETE) {
  263. /* Transfer Complete */
  264. debug("r/w is done\n");
  265. break;
  266. } else if (get_timer(start) > 8000UL) {
  267. writel(mask, &priv->reg->norintsts);
  268. printf("%s: MMC Timeout\n"
  269. " Interrupt status 0x%08x\n"
  270. " Interrupt status enable 0x%08x\n"
  271. " Interrupt signal enable 0x%08x\n"
  272. " Present status 0x%08x\n",
  273. __func__, mask,
  274. readl(&priv->reg->norintstsen),
  275. readl(&priv->reg->norintsigen),
  276. readl(&priv->reg->prnsts));
  277. return -1;
  278. }
  279. }
  280. writel(mask, &priv->reg->norintsts);
  281. }
  282. udelay(1000);
  283. return 0;
  284. }
  285. static int tegra_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
  286. struct mmc_data *data)
  287. {
  288. void *buf;
  289. unsigned int bbflags;
  290. size_t len;
  291. struct bounce_buffer bbstate;
  292. int ret;
  293. if (data) {
  294. if (data->flags & MMC_DATA_READ) {
  295. buf = data->dest;
  296. bbflags = GEN_BB_WRITE;
  297. } else {
  298. buf = (void *)data->src;
  299. bbflags = GEN_BB_READ;
  300. }
  301. len = data->blocks * data->blocksize;
  302. bounce_buffer_start(&bbstate, buf, len, bbflags);
  303. }
  304. ret = tegra_mmc_send_cmd_bounced(dev, cmd, data, &bbstate);
  305. if (data)
  306. bounce_buffer_stop(&bbstate);
  307. return ret;
  308. }
  309. static void tegra_mmc_change_clock(struct tegra_mmc_priv *priv, uint clock)
  310. {
  311. ulong rate;
  312. int div;
  313. unsigned short clk;
  314. unsigned long timeout;
  315. debug(" mmc_change_clock called\n");
  316. /*
  317. * Change Tegra SDMMCx clock divisor here. Source is PLLP_OUT0
  318. */
  319. if (clock == 0)
  320. goto out;
  321. rate = clk_set_rate(&priv->clk, clock);
  322. div = (rate + clock - 1) / clock;
  323. debug("div = %d\n", div);
  324. writew(0, &priv->reg->clkcon);
  325. /*
  326. * CLKCON
  327. * SELFREQ[15:8] : base clock divided by value
  328. * ENSDCLK[2] : SD Clock Enable
  329. * STBLINTCLK[1] : Internal Clock Stable
  330. * ENINTCLK[0] : Internal Clock Enable
  331. */
  332. div >>= 1;
  333. clk = ((div << TEGRA_MMC_CLKCON_SDCLK_FREQ_SEL_SHIFT) |
  334. TEGRA_MMC_CLKCON_INTERNAL_CLOCK_ENABLE);
  335. writew(clk, &priv->reg->clkcon);
  336. /* Wait max 10 ms */
  337. timeout = 10;
  338. while (!(readw(&priv->reg->clkcon) &
  339. TEGRA_MMC_CLKCON_INTERNAL_CLOCK_STABLE)) {
  340. if (timeout == 0) {
  341. printf("%s: timeout error\n", __func__);
  342. return;
  343. }
  344. timeout--;
  345. udelay(1000);
  346. }
  347. clk |= TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE;
  348. writew(clk, &priv->reg->clkcon);
  349. debug("mmc_change_clock: clkcon = %08X\n", clk);
  350. out:
  351. priv->clock = clock;
  352. }
  353. static int tegra_mmc_set_ios(struct udevice *dev)
  354. {
  355. struct tegra_mmc_priv *priv = dev_get_priv(dev);
  356. struct mmc *mmc = mmc_get_mmc_dev(dev);
  357. unsigned char ctrl;
  358. debug(" mmc_set_ios called\n");
  359. debug("bus_width: %x, clock: %d\n", mmc->bus_width, mmc->clock);
  360. /* Change clock first */
  361. tegra_mmc_change_clock(priv, mmc->clock);
  362. ctrl = readb(&priv->reg->hostctl);
  363. /*
  364. * WIDE8[5]
  365. * 0 = Depend on WIDE4
  366. * 1 = 8-bit mode
  367. * WIDE4[1]
  368. * 1 = 4-bit mode
  369. * 0 = 1-bit mode
  370. */
  371. if (mmc->bus_width == 8)
  372. ctrl |= (1 << 5);
  373. else if (mmc->bus_width == 4)
  374. ctrl |= (1 << 1);
  375. else
  376. ctrl &= ~(1 << 1 | 1 << 5);
  377. writeb(ctrl, &priv->reg->hostctl);
  378. debug("mmc_set_ios: hostctl = %08X\n", ctrl);
  379. return 0;
  380. }
  381. static void tegra_mmc_pad_init(struct tegra_mmc_priv *priv)
  382. {
  383. #if defined(CONFIG_TEGRA30)
  384. u32 val;
  385. debug("%s: sdmmc address = %08x\n", __func__, (unsigned int)priv->reg);
  386. /* Set the pad drive strength for SDMMC1 or 3 only */
  387. if (priv->reg != (void *)0x78000000 &&
  388. priv->reg != (void *)0x78000400) {
  389. debug("%s: settings are only valid for SDMMC1/SDMMC3!\n",
  390. __func__);
  391. return;
  392. }
  393. val = readl(&priv->reg->sdmemcmppadctl);
  394. val &= 0xFFFFFFF0;
  395. val |= MEMCOMP_PADCTRL_VREF;
  396. writel(val, &priv->reg->sdmemcmppadctl);
  397. val = readl(&priv->reg->autocalcfg);
  398. val &= 0xFFFF0000;
  399. val |= AUTO_CAL_PU_OFFSET | AUTO_CAL_PD_OFFSET | AUTO_CAL_ENABLED;
  400. writel(val, &priv->reg->autocalcfg);
  401. #endif
  402. }
  403. static void tegra_mmc_reset(struct tegra_mmc_priv *priv, struct mmc *mmc)
  404. {
  405. unsigned int timeout;
  406. debug(" mmc_reset called\n");
  407. /*
  408. * RSTALL[0] : Software reset for all
  409. * 1 = reset
  410. * 0 = work
  411. */
  412. writeb(TEGRA_MMC_SWRST_SW_RESET_FOR_ALL, &priv->reg->swrst);
  413. priv->clock = 0;
  414. /* Wait max 100 ms */
  415. timeout = 100;
  416. /* hw clears the bit when it's done */
  417. while (readb(&priv->reg->swrst) & TEGRA_MMC_SWRST_SW_RESET_FOR_ALL) {
  418. if (timeout == 0) {
  419. printf("%s: timeout error\n", __func__);
  420. return;
  421. }
  422. timeout--;
  423. udelay(1000);
  424. }
  425. /* Set SD bus voltage & enable bus power */
  426. tegra_mmc_set_power(priv, fls(mmc->cfg->voltages) - 1);
  427. debug("%s: power control = %02X, host control = %02X\n", __func__,
  428. readb(&priv->reg->pwrcon), readb(&priv->reg->hostctl));
  429. /* Make sure SDIO pads are set up */
  430. tegra_mmc_pad_init(priv);
  431. }
  432. static int tegra_mmc_init(struct udevice *dev)
  433. {
  434. struct tegra_mmc_priv *priv = dev_get_priv(dev);
  435. struct mmc *mmc = mmc_get_mmc_dev(dev);
  436. unsigned int mask;
  437. debug(" tegra_mmc_init called\n");
  438. tegra_mmc_reset(priv, mmc);
  439. #if defined(CONFIG_TEGRA124_MMC_DISABLE_EXT_LOOPBACK)
  440. /*
  441. * Disable the external clock loopback and use the internal one on
  442. * SDMMC3 as per the SDMMC_VENDOR_MISC_CNTRL_0 register's SDMMC_SPARE1
  443. * bits being set to 0xfffd according to the TRM.
  444. *
  445. * TODO(marcel.ziswiler@toradex.com): Move to device tree controlled
  446. * approach once proper kernel integration made it mainline.
  447. */
  448. if (priv->reg == (void *)0x700b0400) {
  449. mask = readl(&priv->reg->venmiscctl);
  450. mask &= ~TEGRA_MMC_MISCON_ENABLE_EXT_LOOPBACK;
  451. writel(mask, &priv->reg->venmiscctl);
  452. }
  453. #endif
  454. priv->version = readw(&priv->reg->hcver);
  455. debug("host version = %x\n", priv->version);
  456. /* mask all */
  457. writel(0xffffffff, &priv->reg->norintstsen);
  458. writel(0xffffffff, &priv->reg->norintsigen);
  459. writeb(0xe, &priv->reg->timeoutcon); /* TMCLK * 2^27 */
  460. /*
  461. * NORMAL Interrupt Status Enable Register init
  462. * [5] ENSTABUFRDRDY : Buffer Read Ready Status Enable
  463. * [4] ENSTABUFWTRDY : Buffer write Ready Status Enable
  464. * [3] ENSTADMAINT : DMA boundary interrupt
  465. * [1] ENSTASTANSCMPLT : Transfre Complete Status Enable
  466. * [0] ENSTACMDCMPLT : Command Complete Status Enable
  467. */
  468. mask = readl(&priv->reg->norintstsen);
  469. mask &= ~(0xffff);
  470. mask |= (TEGRA_MMC_NORINTSTSEN_CMD_COMPLETE |
  471. TEGRA_MMC_NORINTSTSEN_XFER_COMPLETE |
  472. TEGRA_MMC_NORINTSTSEN_DMA_INTERRUPT |
  473. TEGRA_MMC_NORINTSTSEN_BUFFER_WRITE_READY |
  474. TEGRA_MMC_NORINTSTSEN_BUFFER_READ_READY);
  475. writel(mask, &priv->reg->norintstsen);
  476. /*
  477. * NORMAL Interrupt Signal Enable Register init
  478. * [1] ENSTACMDCMPLT : Transfer Complete Signal Enable
  479. */
  480. mask = readl(&priv->reg->norintsigen);
  481. mask &= ~(0xffff);
  482. mask |= TEGRA_MMC_NORINTSIGEN_XFER_COMPLETE;
  483. writel(mask, &priv->reg->norintsigen);
  484. return 0;
  485. }
  486. static int tegra_mmc_getcd(struct udevice *dev)
  487. {
  488. struct tegra_mmc_priv *priv = dev_get_priv(dev);
  489. debug("tegra_mmc_getcd called\n");
  490. if (dm_gpio_is_valid(&priv->cd_gpio))
  491. return dm_gpio_get_value(&priv->cd_gpio);
  492. return 1;
  493. }
  494. static const struct dm_mmc_ops tegra_mmc_ops = {
  495. .send_cmd = tegra_mmc_send_cmd,
  496. .set_ios = tegra_mmc_set_ios,
  497. .get_cd = tegra_mmc_getcd,
  498. };
  499. static int tegra_mmc_probe(struct udevice *dev)
  500. {
  501. struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
  502. struct tegra_mmc_plat *plat = dev_get_platdata(dev);
  503. struct tegra_mmc_priv *priv = dev_get_priv(dev);
  504. struct mmc_config *cfg = &plat->cfg;
  505. int bus_width, ret;
  506. cfg->name = dev->name;
  507. bus_width = dev_read_u32_default(dev, "bus-width", 1);
  508. cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
  509. cfg->host_caps = 0;
  510. if (bus_width == 8)
  511. cfg->host_caps |= MMC_MODE_8BIT;
  512. if (bus_width >= 4)
  513. cfg->host_caps |= MMC_MODE_4BIT;
  514. cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
  515. /*
  516. * min freq is for card identification, and is the highest
  517. * low-speed SDIO card frequency (actually 400KHz)
  518. * max freq is highest HS eMMC clock as per the SD/MMC spec
  519. * (actually 52MHz)
  520. */
  521. cfg->f_min = 375000;
  522. cfg->f_max = 48000000;
  523. cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
  524. priv->reg = (void *)dev_read_addr(dev);
  525. ret = reset_get_by_name(dev, "sdhci", &priv->reset_ctl);
  526. if (ret) {
  527. debug("reset_get_by_name() failed: %d\n", ret);
  528. return ret;
  529. }
  530. ret = clk_get_by_index(dev, 0, &priv->clk);
  531. if (ret) {
  532. debug("clk_get_by_index() failed: %d\n", ret);
  533. return ret;
  534. }
  535. ret = reset_assert(&priv->reset_ctl);
  536. if (ret)
  537. return ret;
  538. ret = clk_enable(&priv->clk);
  539. if (ret)
  540. return ret;
  541. ret = clk_set_rate(&priv->clk, 20000000);
  542. if (IS_ERR_VALUE(ret))
  543. return ret;
  544. ret = reset_deassert(&priv->reset_ctl);
  545. if (ret)
  546. return ret;
  547. /* These GPIOs are optional */
  548. gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio, GPIOD_IS_IN);
  549. gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio, GPIOD_IS_IN);
  550. gpio_request_by_name(dev, "power-gpios", 0, &priv->pwr_gpio,
  551. GPIOD_IS_OUT);
  552. if (dm_gpio_is_valid(&priv->pwr_gpio))
  553. dm_gpio_set_value(&priv->pwr_gpio, 1);
  554. upriv->mmc = &plat->mmc;
  555. return tegra_mmc_init(dev);
  556. }
  557. static int tegra_mmc_bind(struct udevice *dev)
  558. {
  559. struct tegra_mmc_plat *plat = dev_get_platdata(dev);
  560. return mmc_bind(dev, &plat->mmc, &plat->cfg);
  561. }
  562. static const struct udevice_id tegra_mmc_ids[] = {
  563. { .compatible = "nvidia,tegra20-sdhci" },
  564. { .compatible = "nvidia,tegra30-sdhci" },
  565. { .compatible = "nvidia,tegra114-sdhci" },
  566. { .compatible = "nvidia,tegra124-sdhci" },
  567. { .compatible = "nvidia,tegra210-sdhci" },
  568. { .compatible = "nvidia,tegra186-sdhci" },
  569. { }
  570. };
  571. U_BOOT_DRIVER(tegra_mmc_drv) = {
  572. .name = "tegra_mmc",
  573. .id = UCLASS_MMC,
  574. .of_match = tegra_mmc_ids,
  575. .bind = tegra_mmc_bind,
  576. .probe = tegra_mmc_probe,
  577. .ops = &tegra_mmc_ops,
  578. .platdata_auto_alloc_size = sizeof(struct tegra_mmc_plat),
  579. .priv_auto_alloc_size = sizeof(struct tegra_mmc_priv),
  580. };