tegra_mmc.c 18 KB

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