tegra_mmc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  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. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <bouncebuf.h>
  22. #include <common.h>
  23. #include <asm/gpio.h>
  24. #include <asm/io.h>
  25. #include <asm/arch/clock.h>
  26. #include <asm/arch-tegra/clk_rst.h>
  27. #include <asm/arch-tegra/tegra_mmc.h>
  28. #include <mmc.h>
  29. DECLARE_GLOBAL_DATA_PTR;
  30. struct mmc mmc_dev[MAX_HOSTS];
  31. struct mmc_host mmc_host[MAX_HOSTS];
  32. #ifndef CONFIG_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)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 = (struct mmc_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 TIMEOUT;
  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 TIMEOUT;
  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 int offset =
  213. (unsigned int)(&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 TIMEOUT;
  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) > 2000UL) {
  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 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. clock_adjust_periph_pll_div(host->mmc_id, CLOCK_ID_PERIPH, clock,
  320. &div);
  321. debug("div = %d\n", div);
  322. writew(0, &host->reg->clkcon);
  323. /*
  324. * CLKCON
  325. * SELFREQ[15:8] : base clock divided by value
  326. * ENSDCLK[2] : SD Clock Enable
  327. * STBLINTCLK[1] : Internal Clock Stable
  328. * ENINTCLK[0] : Internal Clock Enable
  329. */
  330. div >>= 1;
  331. clk = ((div << TEGRA_MMC_CLKCON_SDCLK_FREQ_SEL_SHIFT) |
  332. TEGRA_MMC_CLKCON_INTERNAL_CLOCK_ENABLE);
  333. writew(clk, &host->reg->clkcon);
  334. /* Wait max 10 ms */
  335. timeout = 10;
  336. while (!(readw(&host->reg->clkcon) &
  337. TEGRA_MMC_CLKCON_INTERNAL_CLOCK_STABLE)) {
  338. if (timeout == 0) {
  339. printf("%s: timeout error\n", __func__);
  340. return;
  341. }
  342. timeout--;
  343. udelay(1000);
  344. }
  345. clk |= TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE;
  346. writew(clk, &host->reg->clkcon);
  347. debug("mmc_change_clock: clkcon = %08X\n", clk);
  348. out:
  349. host->clock = clock;
  350. }
  351. static void mmc_set_ios(struct mmc *mmc)
  352. {
  353. struct mmc_host *host = mmc->priv;
  354. unsigned char ctrl;
  355. debug(" mmc_set_ios called\n");
  356. debug("bus_width: %x, clock: %d\n", mmc->bus_width, mmc->clock);
  357. /* Change clock first */
  358. mmc_change_clock(host, mmc->clock);
  359. ctrl = readb(&host->reg->hostctl);
  360. /*
  361. * WIDE8[5]
  362. * 0 = Depend on WIDE4
  363. * 1 = 8-bit mode
  364. * WIDE4[1]
  365. * 1 = 4-bit mode
  366. * 0 = 1-bit mode
  367. */
  368. if (mmc->bus_width == 8)
  369. ctrl |= (1 << 5);
  370. else if (mmc->bus_width == 4)
  371. ctrl |= (1 << 1);
  372. else
  373. ctrl &= ~(1 << 1);
  374. writeb(ctrl, &host->reg->hostctl);
  375. debug("mmc_set_ios: hostctl = %08X\n", ctrl);
  376. }
  377. static void mmc_reset(struct mmc_host *host, struct mmc *mmc)
  378. {
  379. unsigned int timeout;
  380. debug(" mmc_reset called\n");
  381. /*
  382. * RSTALL[0] : Software reset for all
  383. * 1 = reset
  384. * 0 = work
  385. */
  386. writeb(TEGRA_MMC_SWRST_SW_RESET_FOR_ALL, &host->reg->swrst);
  387. host->clock = 0;
  388. /* Wait max 100 ms */
  389. timeout = 100;
  390. /* hw clears the bit when it's done */
  391. while (readb(&host->reg->swrst) & TEGRA_MMC_SWRST_SW_RESET_FOR_ALL) {
  392. if (timeout == 0) {
  393. printf("%s: timeout error\n", __func__);
  394. return;
  395. }
  396. timeout--;
  397. udelay(1000);
  398. }
  399. /* Set SD bus voltage & enable bus power */
  400. mmc_set_power(host, fls(mmc->voltages) - 1);
  401. debug("%s: power control = %02X, host control = %02X\n", __func__,
  402. readb(&host->reg->pwrcon), readb(&host->reg->hostctl));
  403. /* Make sure SDIO pads are set up */
  404. pad_init_mmc(host);
  405. }
  406. static int mmc_core_init(struct mmc *mmc)
  407. {
  408. struct mmc_host *host = (struct mmc_host *)mmc->priv;
  409. unsigned int mask;
  410. debug(" mmc_core_init called\n");
  411. mmc_reset(host, mmc);
  412. host->version = readw(&host->reg->hcver);
  413. debug("host version = %x\n", host->version);
  414. /* mask all */
  415. writel(0xffffffff, &host->reg->norintstsen);
  416. writel(0xffffffff, &host->reg->norintsigen);
  417. writeb(0xe, &host->reg->timeoutcon); /* TMCLK * 2^27 */
  418. /*
  419. * NORMAL Interrupt Status Enable Register init
  420. * [5] ENSTABUFRDRDY : Buffer Read Ready Status Enable
  421. * [4] ENSTABUFWTRDY : Buffer write Ready Status Enable
  422. * [3] ENSTADMAINT : DMA boundary interrupt
  423. * [1] ENSTASTANSCMPLT : Transfre Complete Status Enable
  424. * [0] ENSTACMDCMPLT : Command Complete Status Enable
  425. */
  426. mask = readl(&host->reg->norintstsen);
  427. mask &= ~(0xffff);
  428. mask |= (TEGRA_MMC_NORINTSTSEN_CMD_COMPLETE |
  429. TEGRA_MMC_NORINTSTSEN_XFER_COMPLETE |
  430. TEGRA_MMC_NORINTSTSEN_DMA_INTERRUPT |
  431. TEGRA_MMC_NORINTSTSEN_BUFFER_WRITE_READY |
  432. TEGRA_MMC_NORINTSTSEN_BUFFER_READ_READY);
  433. writel(mask, &host->reg->norintstsen);
  434. /*
  435. * NORMAL Interrupt Signal Enable Register init
  436. * [1] ENSTACMDCMPLT : Transfer Complete Signal Enable
  437. */
  438. mask = readl(&host->reg->norintsigen);
  439. mask &= ~(0xffff);
  440. mask |= TEGRA_MMC_NORINTSIGEN_XFER_COMPLETE;
  441. writel(mask, &host->reg->norintsigen);
  442. return 0;
  443. }
  444. int tegra_mmc_getcd(struct mmc *mmc)
  445. {
  446. struct mmc_host *host = (struct mmc_host *)mmc->priv;
  447. debug("tegra_mmc_getcd called\n");
  448. if (fdt_gpio_isvalid(&host->cd_gpio))
  449. return fdtdec_get_gpio(&host->cd_gpio);
  450. return 1;
  451. }
  452. static int do_mmc_init(int dev_index)
  453. {
  454. struct mmc_host *host;
  455. char gpusage[12]; /* "SD/MMCn PWR" or "SD/MMCn CD" */
  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 "
  462. "pwr_gpio %d cd_gpio %d\n",
  463. dev_index, host->width,
  464. host->pwr_gpio.gpio, host->cd_gpio.gpio);
  465. host->clock = 0;
  466. clock_start_periph_pll(host->mmc_id, CLOCK_ID_PERIPH, 20000000);
  467. if (fdt_gpio_isvalid(&host->pwr_gpio)) {
  468. sprintf(gpusage, "SD/MMC%d PWR", dev_index);
  469. gpio_request(host->pwr_gpio.gpio, gpusage);
  470. gpio_direction_output(host->pwr_gpio.gpio, 1);
  471. debug(" Power GPIO name = %s\n", host->pwr_gpio.name);
  472. }
  473. if (fdt_gpio_isvalid(&host->cd_gpio)) {
  474. sprintf(gpusage, "SD/MMC%d CD", dev_index);
  475. gpio_request(host->cd_gpio.gpio, gpusage);
  476. gpio_direction_input(host->cd_gpio.gpio);
  477. debug(" CD GPIO name = %s\n", host->cd_gpio.name);
  478. }
  479. mmc = &mmc_dev[dev_index];
  480. sprintf(mmc->name, "Tegra SD/MMC");
  481. mmc->priv = host;
  482. mmc->send_cmd = mmc_send_cmd;
  483. mmc->set_ios = mmc_set_ios;
  484. mmc->init = mmc_core_init;
  485. mmc->getcd = tegra_mmc_getcd;
  486. mmc->getwp = NULL;
  487. mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
  488. mmc->host_caps = 0;
  489. if (host->width == 8)
  490. mmc->host_caps |= MMC_MODE_8BIT;
  491. if (host->width >= 4)
  492. mmc->host_caps |= MMC_MODE_4BIT;
  493. mmc->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS | MMC_MODE_HC;
  494. /*
  495. * min freq is for card identification, and is the highest
  496. * low-speed SDIO card frequency (actually 400KHz)
  497. * max freq is highest HS eMMC clock as per the SD/MMC spec
  498. * (actually 52MHz)
  499. */
  500. mmc->f_min = 375000;
  501. mmc->f_max = 48000000;
  502. mmc_register(mmc);
  503. return 0;
  504. }
  505. /**
  506. * Get the host address and peripheral ID for a node.
  507. *
  508. * @param blob fdt blob
  509. * @param node Device index (0-3)
  510. * @param host Structure to fill in (reg, width, mmc_id)
  511. */
  512. static int mmc_get_config(const void *blob, int node, struct mmc_host *host)
  513. {
  514. debug("%s: node = %d\n", __func__, node);
  515. host->enabled = fdtdec_get_is_enabled(blob, node);
  516. host->reg = (struct tegra_mmc *)fdtdec_get_addr(blob, node, "reg");
  517. if ((fdt_addr_t)host->reg == FDT_ADDR_T_NONE) {
  518. debug("%s: no sdmmc base reg info found\n", __func__);
  519. return -FDT_ERR_NOTFOUND;
  520. }
  521. host->mmc_id = clock_decode_periph_id(blob, node);
  522. if (host->mmc_id == PERIPH_ID_NONE) {
  523. debug("%s: could not decode periph id\n", __func__);
  524. return -FDT_ERR_NOTFOUND;
  525. }
  526. /*
  527. * NOTE: mmc->bus_width is determined by mmc.c dynamically.
  528. * TBD: Override it with this value?
  529. */
  530. host->width = fdtdec_get_int(blob, node, "bus-width", 0);
  531. if (!host->width)
  532. debug("%s: no sdmmc width found\n", __func__);
  533. /* These GPIOs are optional */
  534. fdtdec_decode_gpio(blob, node, "cd-gpios", &host->cd_gpio);
  535. fdtdec_decode_gpio(blob, node, "wp-gpios", &host->wp_gpio);
  536. fdtdec_decode_gpio(blob, node, "power-gpios", &host->pwr_gpio);
  537. debug("%s: found controller at %p, width = %d, periph_id = %d\n",
  538. __func__, host->reg, host->width, host->mmc_id);
  539. return 0;
  540. }
  541. /*
  542. * Process a list of nodes, adding them to our list of SDMMC ports.
  543. *
  544. * @param blob fdt blob
  545. * @param node_list list of nodes to process (any <=0 are ignored)
  546. * @param count number of nodes to process
  547. * @return 0 if ok, -1 on error
  548. */
  549. static int process_nodes(const void *blob, int node_list[], int count)
  550. {
  551. struct mmc_host *host;
  552. int i, node;
  553. debug("%s: count = %d\n", __func__, count);
  554. /* build mmc_host[] for each controller */
  555. for (i = 0; i < count; i++) {
  556. node = node_list[i];
  557. if (node <= 0)
  558. continue;
  559. host = &mmc_host[i];
  560. host->id = i;
  561. if (mmc_get_config(blob, node, host)) {
  562. printf("%s: failed to decode dev %d\n", __func__, i);
  563. return -1;
  564. }
  565. do_mmc_init(i);
  566. }
  567. return 0;
  568. }
  569. void tegra_mmc_init(void)
  570. {
  571. int node_list[MAX_HOSTS], count;
  572. const void *blob = gd->fdt_blob;
  573. debug("%s entry\n", __func__);
  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, MAX_HOSTS);
  577. debug("%s: count of T30 sdhci nodes is %d\n", __func__, count);
  578. if (process_nodes(blob, node_list, count)) {
  579. printf("%s: Error processing T30 mmc node(s)!\n", __func__);
  580. return;
  581. }
  582. /* Now look for any Tegra20 MMC controllers */
  583. count = fdtdec_find_aliases_for_id(blob, "sdhci",
  584. COMPAT_NVIDIA_TEGRA20_SDMMC, node_list, MAX_HOSTS);
  585. debug("%s: count of T20 sdhci nodes is %d\n", __func__, count);
  586. if (process_nodes(blob, node_list, count)) {
  587. printf("%s: Error processing T20 mmc node(s)!\n", __func__);
  588. return;
  589. }
  590. }