tegra_mmc.c 19 KB

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