tegra_mmc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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-2015 NVIDIA Corporation
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <bouncebuf.h>
  10. #include <common.h>
  11. #include <dm/device.h>
  12. #include <errno.h>
  13. #include <asm/gpio.h>
  14. #include <asm/io.h>
  15. #ifndef CONFIG_TEGRA186
  16. #include <asm/arch/clock.h>
  17. #include <asm/arch-tegra/clk_rst.h>
  18. #endif
  19. #include <asm/arch-tegra/mmc.h>
  20. #include <asm/arch-tegra/tegra_mmc.h>
  21. #include <mmc.h>
  22. /*
  23. * FIXME: TODO: This driver contains a number of ifdef CONFIG_TEGRA186 that
  24. * should not be present. These are needed because newer Tegra SoCs support
  25. * only the standard clock/reset APIs, whereas older Tegra SoCs support only
  26. * a custom Tegra-specific API. ASAP the older Tegra SoCs' code should be
  27. * fixed to implement the standard APIs, and all drivers converted to solely
  28. * use the new standard APIs, with no ifdefs.
  29. */
  30. DECLARE_GLOBAL_DATA_PTR;
  31. struct mmc_host mmc_host[CONFIG_SYS_MMC_MAX_DEVICE];
  32. #if !CONFIG_IS_ENABLED(OF_CONTROL)
  33. #error "Please enable device tree support to use this driver"
  34. #endif
  35. static void mmc_set_power(struct mmc_host *host, unsigned short power)
  36. {
  37. u8 pwr = 0;
  38. debug("%s: power = %x\n", __func__, power);
  39. if (power != (unsigned short)-1) {
  40. switch (1 << power) {
  41. case MMC_VDD_165_195:
  42. pwr = TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V1_8;
  43. break;
  44. case MMC_VDD_29_30:
  45. case MMC_VDD_30_31:
  46. pwr = TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V3_0;
  47. break;
  48. case MMC_VDD_32_33:
  49. case MMC_VDD_33_34:
  50. pwr = TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V3_3;
  51. break;
  52. }
  53. }
  54. debug("%s: pwr = %X\n", __func__, pwr);
  55. /* Set the bus voltage first (if any) */
  56. writeb(pwr, &host->reg->pwrcon);
  57. if (pwr == 0)
  58. return;
  59. /* Now enable bus power */
  60. pwr |= TEGRA_MMC_PWRCTL_SD_BUS_POWER;
  61. writeb(pwr, &host->reg->pwrcon);
  62. }
  63. static void mmc_prepare_data(struct mmc_host *host, struct mmc_data *data,
  64. struct bounce_buffer *bbstate)
  65. {
  66. unsigned char ctrl;
  67. debug("buf: %p (%p), data->blocks: %u, data->blocksize: %u\n",
  68. bbstate->bounce_buffer, bbstate->user_buffer, data->blocks,
  69. data->blocksize);
  70. writel((u32)(unsigned long)bbstate->bounce_buffer, &host->reg->sysad);
  71. /*
  72. * DMASEL[4:3]
  73. * 00 = Selects SDMA
  74. * 01 = Reserved
  75. * 10 = Selects 32-bit Address ADMA2
  76. * 11 = Selects 64-bit Address ADMA2
  77. */
  78. ctrl = readb(&host->reg->hostctl);
  79. ctrl &= ~TEGRA_MMC_HOSTCTL_DMASEL_MASK;
  80. ctrl |= TEGRA_MMC_HOSTCTL_DMASEL_SDMA;
  81. writeb(ctrl, &host->reg->hostctl);
  82. /* We do not handle DMA boundaries, so set it to max (512 KiB) */
  83. writew((7 << 12) | (data->blocksize & 0xFFF), &host->reg->blksize);
  84. writew(data->blocks, &host->reg->blkcnt);
  85. }
  86. static void mmc_set_transfer_mode(struct mmc_host *host, 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, &host->reg->trnmod);
  107. }
  108. static int mmc_wait_inhibit(struct mmc_host *host,
  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(&host->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 mmc_send_cmd_bounced(struct mmc *mmc, struct mmc_cmd *cmd,
  136. struct mmc_data *data, struct bounce_buffer *bbstate)
  137. {
  138. struct mmc_host *host = mmc->priv;
  139. int flags, i;
  140. int result;
  141. unsigned int mask = 0;
  142. unsigned int retry = 0x100000;
  143. debug(" mmc_send_cmd called\n");
  144. result = mmc_wait_inhibit(host, cmd, data, 10 /* ms */);
  145. if (result < 0)
  146. return result;
  147. if (data)
  148. mmc_prepare_data(host, data, bbstate);
  149. debug("cmd->arg: %08x\n", cmd->cmdarg);
  150. writel(cmd->cmdarg, &host->reg->argument);
  151. if (data)
  152. mmc_set_transfer_mode(host, data);
  153. if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
  154. return -1;
  155. /*
  156. * CMDREG
  157. * CMDIDX[13:8] : Command index
  158. * DATAPRNT[5] : Data Present Select
  159. * ENCMDIDX[4] : Command Index Check Enable
  160. * ENCMDCRC[3] : Command CRC Check Enable
  161. * RSPTYP[1:0]
  162. * 00 = No Response
  163. * 01 = Length 136
  164. * 10 = Length 48
  165. * 11 = Length 48 Check busy after response
  166. */
  167. if (!(cmd->resp_type & MMC_RSP_PRESENT))
  168. flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_NO_RESPONSE;
  169. else if (cmd->resp_type & MMC_RSP_136)
  170. flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_136;
  171. else if (cmd->resp_type & MMC_RSP_BUSY)
  172. flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_48_BUSY;
  173. else
  174. flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_48;
  175. if (cmd->resp_type & MMC_RSP_CRC)
  176. flags |= TEGRA_MMC_TRNMOD_CMD_CRC_CHECK;
  177. if (cmd->resp_type & MMC_RSP_OPCODE)
  178. flags |= TEGRA_MMC_TRNMOD_CMD_INDEX_CHECK;
  179. if (data)
  180. flags |= TEGRA_MMC_TRNMOD_DATA_PRESENT_SELECT_DATA_TRANSFER;
  181. debug("cmd: %d\n", cmd->cmdidx);
  182. writew((cmd->cmdidx << 8) | flags, &host->reg->cmdreg);
  183. for (i = 0; i < retry; i++) {
  184. mask = readl(&host->reg->norintsts);
  185. /* Command Complete */
  186. if (mask & TEGRA_MMC_NORINTSTS_CMD_COMPLETE) {
  187. if (!data)
  188. writel(mask, &host->reg->norintsts);
  189. break;
  190. }
  191. }
  192. if (i == retry) {
  193. printf("%s: waiting for status update\n", __func__);
  194. writel(mask, &host->reg->norintsts);
  195. return -ETIMEDOUT;
  196. }
  197. if (mask & TEGRA_MMC_NORINTSTS_CMD_TIMEOUT) {
  198. /* Timeout Error */
  199. debug("timeout: %08x cmd %d\n", mask, cmd->cmdidx);
  200. writel(mask, &host->reg->norintsts);
  201. return -ETIMEDOUT;
  202. } else if (mask & TEGRA_MMC_NORINTSTS_ERR_INTERRUPT) {
  203. /* Error Interrupt */
  204. debug("error: %08x cmd %d\n", mask, cmd->cmdidx);
  205. writel(mask, &host->reg->norintsts);
  206. return -1;
  207. }
  208. if (cmd->resp_type & MMC_RSP_PRESENT) {
  209. if (cmd->resp_type & MMC_RSP_136) {
  210. /* CRC is stripped so we need to do some shifting. */
  211. for (i = 0; i < 4; i++) {
  212. unsigned long offset =
  213. (unsigned long)(&host->reg->rspreg3 - i);
  214. cmd->response[i] = readl(offset) << 8;
  215. if (i != 3) {
  216. cmd->response[i] |=
  217. readb(offset - 1);
  218. }
  219. debug("cmd->resp[%d]: %08x\n",
  220. i, cmd->response[i]);
  221. }
  222. } else if (cmd->resp_type & MMC_RSP_BUSY) {
  223. for (i = 0; i < retry; i++) {
  224. /* PRNTDATA[23:20] : DAT[3:0] Line Signal */
  225. if (readl(&host->reg->prnsts)
  226. & (1 << 20)) /* DAT[0] */
  227. break;
  228. }
  229. if (i == retry) {
  230. printf("%s: card is still busy\n", __func__);
  231. writel(mask, &host->reg->norintsts);
  232. return -ETIMEDOUT;
  233. }
  234. cmd->response[0] = readl(&host->reg->rspreg0);
  235. debug("cmd->resp[0]: %08x\n", cmd->response[0]);
  236. } else {
  237. cmd->response[0] = readl(&host->reg->rspreg0);
  238. debug("cmd->resp[0]: %08x\n", cmd->response[0]);
  239. }
  240. }
  241. if (data) {
  242. unsigned long start = get_timer(0);
  243. while (1) {
  244. mask = readl(&host->reg->norintsts);
  245. if (mask & TEGRA_MMC_NORINTSTS_ERR_INTERRUPT) {
  246. /* Error Interrupt */
  247. writel(mask, &host->reg->norintsts);
  248. printf("%s: error during transfer: 0x%08x\n",
  249. __func__, mask);
  250. return -1;
  251. } else if (mask & TEGRA_MMC_NORINTSTS_DMA_INTERRUPT) {
  252. /*
  253. * DMA Interrupt, restart the transfer where
  254. * it was interrupted.
  255. */
  256. unsigned int address = readl(&host->reg->sysad);
  257. debug("DMA end\n");
  258. writel(TEGRA_MMC_NORINTSTS_DMA_INTERRUPT,
  259. &host->reg->norintsts);
  260. writel(address, &host->reg->sysad);
  261. } else if (mask & TEGRA_MMC_NORINTSTS_XFER_COMPLETE) {
  262. /* Transfer Complete */
  263. debug("r/w is done\n");
  264. break;
  265. } else if (get_timer(start) > 8000UL) {
  266. writel(mask, &host->reg->norintsts);
  267. printf("%s: MMC Timeout\n"
  268. " Interrupt status 0x%08x\n"
  269. " Interrupt status enable 0x%08x\n"
  270. " Interrupt signal enable 0x%08x\n"
  271. " Present status 0x%08x\n",
  272. __func__, mask,
  273. readl(&host->reg->norintstsen),
  274. readl(&host->reg->norintsigen),
  275. readl(&host->reg->prnsts));
  276. return -1;
  277. }
  278. }
  279. writel(mask, &host->reg->norintsts);
  280. }
  281. udelay(1000);
  282. return 0;
  283. }
  284. static int tegra_mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
  285. struct mmc_data *data)
  286. {
  287. void *buf;
  288. unsigned int bbflags;
  289. size_t len;
  290. struct bounce_buffer bbstate;
  291. int ret;
  292. if (data) {
  293. if (data->flags & MMC_DATA_READ) {
  294. buf = data->dest;
  295. bbflags = GEN_BB_WRITE;
  296. } else {
  297. buf = (void *)data->src;
  298. bbflags = GEN_BB_READ;
  299. }
  300. len = data->blocks * data->blocksize;
  301. bounce_buffer_start(&bbstate, buf, len, bbflags);
  302. }
  303. ret = mmc_send_cmd_bounced(mmc, cmd, data, &bbstate);
  304. if (data)
  305. bounce_buffer_stop(&bbstate);
  306. return ret;
  307. }
  308. static void mmc_change_clock(struct mmc_host *host, uint clock)
  309. {
  310. int div;
  311. unsigned short clk;
  312. unsigned long timeout;
  313. debug(" mmc_change_clock called\n");
  314. /*
  315. * Change Tegra SDMMCx clock divisor here. Source is PLLP_OUT0
  316. */
  317. if (clock == 0)
  318. goto out;
  319. #ifdef CONFIG_TEGRA186
  320. {
  321. ulong rate = clk_set_rate(&host->clk, clock);
  322. div = (rate + clock - 1) / clock;
  323. }
  324. #else
  325. clock_adjust_periph_pll_div(host->mmc_id, CLOCK_ID_PERIPH, clock,
  326. &div);
  327. #endif
  328. debug("div = %d\n", div);
  329. writew(0, &host->reg->clkcon);
  330. /*
  331. * CLKCON
  332. * SELFREQ[15:8] : base clock divided by value
  333. * ENSDCLK[2] : SD Clock Enable
  334. * STBLINTCLK[1] : Internal Clock Stable
  335. * ENINTCLK[0] : Internal Clock Enable
  336. */
  337. div >>= 1;
  338. clk = ((div << TEGRA_MMC_CLKCON_SDCLK_FREQ_SEL_SHIFT) |
  339. TEGRA_MMC_CLKCON_INTERNAL_CLOCK_ENABLE);
  340. writew(clk, &host->reg->clkcon);
  341. /* Wait max 10 ms */
  342. timeout = 10;
  343. while (!(readw(&host->reg->clkcon) &
  344. TEGRA_MMC_CLKCON_INTERNAL_CLOCK_STABLE)) {
  345. if (timeout == 0) {
  346. printf("%s: timeout error\n", __func__);
  347. return;
  348. }
  349. timeout--;
  350. udelay(1000);
  351. }
  352. clk |= TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE;
  353. writew(clk, &host->reg->clkcon);
  354. debug("mmc_change_clock: clkcon = %08X\n", clk);
  355. out:
  356. host->clock = clock;
  357. }
  358. static void tegra_mmc_set_ios(struct mmc *mmc)
  359. {
  360. struct mmc_host *host = mmc->priv;
  361. unsigned char ctrl;
  362. debug(" mmc_set_ios called\n");
  363. debug("bus_width: %x, clock: %d\n", mmc->bus_width, mmc->clock);
  364. /* Change clock first */
  365. mmc_change_clock(host, mmc->clock);
  366. ctrl = readb(&host->reg->hostctl);
  367. /*
  368. * WIDE8[5]
  369. * 0 = Depend on WIDE4
  370. * 1 = 8-bit mode
  371. * WIDE4[1]
  372. * 1 = 4-bit mode
  373. * 0 = 1-bit mode
  374. */
  375. if (mmc->bus_width == 8)
  376. ctrl |= (1 << 5);
  377. else if (mmc->bus_width == 4)
  378. ctrl |= (1 << 1);
  379. else
  380. ctrl &= ~(1 << 1);
  381. writeb(ctrl, &host->reg->hostctl);
  382. debug("mmc_set_ios: hostctl = %08X\n", ctrl);
  383. }
  384. static void mmc_reset(struct mmc_host *host, struct mmc *mmc)
  385. {
  386. unsigned int timeout;
  387. debug(" mmc_reset called\n");
  388. /*
  389. * RSTALL[0] : Software reset for all
  390. * 1 = reset
  391. * 0 = work
  392. */
  393. writeb(TEGRA_MMC_SWRST_SW_RESET_FOR_ALL, &host->reg->swrst);
  394. host->clock = 0;
  395. /* Wait max 100 ms */
  396. timeout = 100;
  397. /* hw clears the bit when it's done */
  398. while (readb(&host->reg->swrst) & TEGRA_MMC_SWRST_SW_RESET_FOR_ALL) {
  399. if (timeout == 0) {
  400. printf("%s: timeout error\n", __func__);
  401. return;
  402. }
  403. timeout--;
  404. udelay(1000);
  405. }
  406. /* Set SD bus voltage & enable bus power */
  407. mmc_set_power(host, fls(mmc->cfg->voltages) - 1);
  408. debug("%s: power control = %02X, host control = %02X\n", __func__,
  409. readb(&host->reg->pwrcon), readb(&host->reg->hostctl));
  410. /* Make sure SDIO pads are set up */
  411. pad_init_mmc(host);
  412. }
  413. static int tegra_mmc_core_init(struct mmc *mmc)
  414. {
  415. struct mmc_host *host = mmc->priv;
  416. unsigned int mask;
  417. debug(" mmc_core_init called\n");
  418. mmc_reset(host, mmc);
  419. host->version = readw(&host->reg->hcver);
  420. debug("host version = %x\n", host->version);
  421. /* mask all */
  422. writel(0xffffffff, &host->reg->norintstsen);
  423. writel(0xffffffff, &host->reg->norintsigen);
  424. writeb(0xe, &host->reg->timeoutcon); /* TMCLK * 2^27 */
  425. /*
  426. * NORMAL Interrupt Status Enable Register init
  427. * [5] ENSTABUFRDRDY : Buffer Read Ready Status Enable
  428. * [4] ENSTABUFWTRDY : Buffer write Ready Status Enable
  429. * [3] ENSTADMAINT : DMA boundary interrupt
  430. * [1] ENSTASTANSCMPLT : Transfre Complete Status Enable
  431. * [0] ENSTACMDCMPLT : Command Complete Status Enable
  432. */
  433. mask = readl(&host->reg->norintstsen);
  434. mask &= ~(0xffff);
  435. mask |= (TEGRA_MMC_NORINTSTSEN_CMD_COMPLETE |
  436. TEGRA_MMC_NORINTSTSEN_XFER_COMPLETE |
  437. TEGRA_MMC_NORINTSTSEN_DMA_INTERRUPT |
  438. TEGRA_MMC_NORINTSTSEN_BUFFER_WRITE_READY |
  439. TEGRA_MMC_NORINTSTSEN_BUFFER_READ_READY);
  440. writel(mask, &host->reg->norintstsen);
  441. /*
  442. * NORMAL Interrupt Signal Enable Register init
  443. * [1] ENSTACMDCMPLT : Transfer Complete Signal Enable
  444. */
  445. mask = readl(&host->reg->norintsigen);
  446. mask &= ~(0xffff);
  447. mask |= TEGRA_MMC_NORINTSIGEN_XFER_COMPLETE;
  448. writel(mask, &host->reg->norintsigen);
  449. return 0;
  450. }
  451. static int tegra_mmc_getcd(struct mmc *mmc)
  452. {
  453. struct mmc_host *host = mmc->priv;
  454. debug("tegra_mmc_getcd called\n");
  455. if (dm_gpio_is_valid(&host->cd_gpio))
  456. return dm_gpio_get_value(&host->cd_gpio);
  457. return 1;
  458. }
  459. static const struct mmc_ops tegra_mmc_ops = {
  460. .send_cmd = tegra_mmc_send_cmd,
  461. .set_ios = tegra_mmc_set_ios,
  462. .init = tegra_mmc_core_init,
  463. .getcd = tegra_mmc_getcd,
  464. };
  465. static int do_mmc_init(int dev_index, bool removable)
  466. {
  467. struct mmc_host *host;
  468. struct mmc *mmc;
  469. #ifdef CONFIG_TEGRA186
  470. int ret;
  471. #endif
  472. /* DT should have been read & host config filled in */
  473. host = &mmc_host[dev_index];
  474. if (!host->enabled)
  475. return -1;
  476. debug(" do_mmc_init: index %d, bus width %d pwr_gpio %d cd_gpio %d\n",
  477. dev_index, host->width, gpio_get_number(&host->pwr_gpio),
  478. gpio_get_number(&host->cd_gpio));
  479. host->clock = 0;
  480. #ifdef CONFIG_TEGRA186
  481. ret = reset_assert(&host->reset_ctl);
  482. if (ret)
  483. return ret;
  484. ret = clk_enable(&host->clk);
  485. if (ret)
  486. return ret;
  487. ret = clk_set_rate(&host->clk, 20000000);
  488. if (IS_ERR_VALUE(ret))
  489. return ret;
  490. ret = reset_deassert(&host->reset_ctl);
  491. if (ret)
  492. return ret;
  493. #else
  494. clock_start_periph_pll(host->mmc_id, CLOCK_ID_PERIPH, 20000000);
  495. #endif
  496. if (dm_gpio_is_valid(&host->pwr_gpio))
  497. dm_gpio_set_value(&host->pwr_gpio, 1);
  498. memset(&host->cfg, 0, sizeof(host->cfg));
  499. host->cfg.name = "Tegra SD/MMC";
  500. host->cfg.ops = &tegra_mmc_ops;
  501. host->cfg.voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
  502. host->cfg.host_caps = 0;
  503. if (host->width == 8)
  504. host->cfg.host_caps |= MMC_MODE_8BIT;
  505. if (host->width >= 4)
  506. host->cfg.host_caps |= MMC_MODE_4BIT;
  507. host->cfg.host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
  508. /*
  509. * min freq is for card identification, and is the highest
  510. * low-speed SDIO card frequency (actually 400KHz)
  511. * max freq is highest HS eMMC clock as per the SD/MMC spec
  512. * (actually 52MHz)
  513. */
  514. host->cfg.f_min = 375000;
  515. host->cfg.f_max = 48000000;
  516. host->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
  517. mmc = mmc_create(&host->cfg, host);
  518. mmc->block_dev.removable = removable;
  519. if (mmc == NULL)
  520. return -1;
  521. return 0;
  522. }
  523. /**
  524. * Get the host address and peripheral ID for a node.
  525. *
  526. * @param blob fdt blob
  527. * @param node Device index (0-3)
  528. * @param host Structure to fill in (reg, width, mmc_id)
  529. */
  530. static int mmc_get_config(const void *blob, int node, struct mmc_host *host,
  531. bool *removablep)
  532. {
  533. debug("%s: node = %d\n", __func__, node);
  534. host->enabled = fdtdec_get_is_enabled(blob, node);
  535. host->reg = (struct tegra_mmc *)fdtdec_get_addr(blob, node, "reg");
  536. if ((fdt_addr_t)host->reg == FDT_ADDR_T_NONE) {
  537. debug("%s: no sdmmc base reg info found\n", __func__);
  538. return -FDT_ERR_NOTFOUND;
  539. }
  540. #ifdef CONFIG_TEGRA186
  541. {
  542. /*
  543. * FIXME: This variable should go away when the MMC device
  544. * actually is a udevice.
  545. */
  546. struct udevice dev;
  547. int ret;
  548. dev.of_offset = node;
  549. ret = reset_get_by_name(&dev, "sdhci", &host->reset_ctl);
  550. if (ret) {
  551. debug("reset_get_by_name() failed: %d\n", ret);
  552. return ret;
  553. }
  554. ret = clk_get_by_index(&dev, 0, &host->clk);
  555. if (ret) {
  556. debug("clk_get_by_index() failed: %d\n", ret);
  557. return ret;
  558. }
  559. }
  560. #else
  561. host->mmc_id = clock_decode_periph_id(blob, node);
  562. if (host->mmc_id == PERIPH_ID_NONE) {
  563. debug("%s: could not decode periph id\n", __func__);
  564. return -FDT_ERR_NOTFOUND;
  565. }
  566. #endif
  567. /*
  568. * NOTE: mmc->bus_width is determined by mmc.c dynamically.
  569. * TBD: Override it with this value?
  570. */
  571. host->width = fdtdec_get_int(blob, node, "bus-width", 0);
  572. if (!host->width)
  573. debug("%s: no sdmmc width found\n", __func__);
  574. /* These GPIOs are optional */
  575. gpio_request_by_name_nodev(blob, node, "cd-gpios", 0, &host->cd_gpio,
  576. GPIOD_IS_IN);
  577. gpio_request_by_name_nodev(blob, node, "wp-gpios", 0, &host->wp_gpio,
  578. GPIOD_IS_IN);
  579. gpio_request_by_name_nodev(blob, node, "power-gpios", 0,
  580. &host->pwr_gpio, GPIOD_IS_OUT);
  581. *removablep = !fdtdec_get_bool(blob, node, "non-removable");
  582. debug("%s: found controller at %p, width = %d, periph_id = %d\n",
  583. __func__, host->reg, host->width,
  584. #ifndef CONFIG_TEGRA186
  585. host->mmc_id
  586. #else
  587. -1
  588. #endif
  589. );
  590. return 0;
  591. }
  592. /*
  593. * Process a list of nodes, adding them to our list of SDMMC ports.
  594. *
  595. * @param blob fdt blob
  596. * @param node_list list of nodes to process (any <=0 are ignored)
  597. * @param count number of nodes to process
  598. * @return 0 if ok, -1 on error
  599. */
  600. static int process_nodes(const void *blob, int node_list[], int count)
  601. {
  602. struct mmc_host *host;
  603. bool removable;
  604. int i, node;
  605. debug("%s: count = %d\n", __func__, count);
  606. /* build mmc_host[] for each controller */
  607. for (i = 0; i < count; i++) {
  608. node = node_list[i];
  609. if (node <= 0)
  610. continue;
  611. host = &mmc_host[i];
  612. host->id = i;
  613. if (mmc_get_config(blob, node, host, &removable)) {
  614. printf("%s: failed to decode dev %d\n", __func__, i);
  615. return -1;
  616. }
  617. do_mmc_init(i, removable);
  618. }
  619. return 0;
  620. }
  621. void tegra_mmc_init(void)
  622. {
  623. int node_list[CONFIG_SYS_MMC_MAX_DEVICE], count;
  624. const void *blob = gd->fdt_blob;
  625. debug("%s entry\n", __func__);
  626. /* See if any Tegra186 MMC controllers are present */
  627. count = fdtdec_find_aliases_for_id(blob, "sdhci",
  628. COMPAT_NVIDIA_TEGRA186_SDMMC, node_list,
  629. CONFIG_SYS_MMC_MAX_DEVICE);
  630. debug("%s: count of Tegra186 sdhci nodes is %d\n", __func__, count);
  631. if (process_nodes(blob, node_list, count)) {
  632. printf("%s: Error processing T186 mmc node(s)!\n", __func__);
  633. return;
  634. }
  635. /* See if any Tegra210 MMC controllers are present */
  636. count = fdtdec_find_aliases_for_id(blob, "sdhci",
  637. COMPAT_NVIDIA_TEGRA210_SDMMC, node_list,
  638. CONFIG_SYS_MMC_MAX_DEVICE);
  639. debug("%s: count of Tegra210 sdhci nodes is %d\n", __func__, count);
  640. if (process_nodes(blob, node_list, count)) {
  641. printf("%s: Error processing T210 mmc node(s)!\n", __func__);
  642. return;
  643. }
  644. /* See if any Tegra124 MMC controllers are present */
  645. count = fdtdec_find_aliases_for_id(blob, "sdhci",
  646. COMPAT_NVIDIA_TEGRA124_SDMMC, node_list,
  647. CONFIG_SYS_MMC_MAX_DEVICE);
  648. debug("%s: count of Tegra124 sdhci nodes is %d\n", __func__, count);
  649. if (process_nodes(blob, node_list, count)) {
  650. printf("%s: Error processing T124 mmc node(s)!\n", __func__);
  651. return;
  652. }
  653. /* See if any Tegra30 MMC controllers are present */
  654. count = fdtdec_find_aliases_for_id(blob, "sdhci",
  655. COMPAT_NVIDIA_TEGRA30_SDMMC, node_list,
  656. CONFIG_SYS_MMC_MAX_DEVICE);
  657. debug("%s: count of T30 sdhci nodes is %d\n", __func__, count);
  658. if (process_nodes(blob, node_list, count)) {
  659. printf("%s: Error processing T30 mmc node(s)!\n", __func__);
  660. return;
  661. }
  662. /* Now look for any Tegra20 MMC controllers */
  663. count = fdtdec_find_aliases_for_id(blob, "sdhci",
  664. COMPAT_NVIDIA_TEGRA20_SDMMC, node_list,
  665. CONFIG_SYS_MMC_MAX_DEVICE);
  666. debug("%s: count of T20 sdhci nodes is %d\n", __func__, count);
  667. if (process_nodes(blob, node_list, count)) {
  668. printf("%s: Error processing T20 mmc node(s)!\n", __func__);
  669. return;
  670. }
  671. }