tegra_mmc.c 17 KB

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