tegra_mmc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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-2012 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 <common.h>
  22. #include <mmc.h>
  23. #include <asm/gpio.h>
  24. #include <asm/io.h>
  25. #include <asm/arch/clk_rst.h>
  26. #include <asm/arch/clock.h>
  27. #include "tegra_mmc.h"
  28. /* support 4 mmc hosts */
  29. struct mmc mmc_dev[4];
  30. struct mmc_host mmc_host[4];
  31. /**
  32. * Get the host address and peripheral ID for a device. Devices are numbered
  33. * from 0 to 3.
  34. *
  35. * @param host Structure to fill in (base, reg, mmc_id)
  36. * @param dev_index Device index (0-3)
  37. */
  38. static void tegra2_get_setup(struct mmc_host *host, int dev_index)
  39. {
  40. debug("tegra2_get_base_mmc: dev_index = %d\n", dev_index);
  41. switch (dev_index) {
  42. case 1:
  43. host->base = TEGRA2_SDMMC3_BASE;
  44. host->mmc_id = PERIPH_ID_SDMMC3;
  45. break;
  46. case 2:
  47. host->base = TEGRA2_SDMMC2_BASE;
  48. host->mmc_id = PERIPH_ID_SDMMC2;
  49. break;
  50. case 3:
  51. host->base = TEGRA2_SDMMC1_BASE;
  52. host->mmc_id = PERIPH_ID_SDMMC1;
  53. break;
  54. case 0:
  55. default:
  56. host->base = TEGRA2_SDMMC4_BASE;
  57. host->mmc_id = PERIPH_ID_SDMMC4;
  58. break;
  59. }
  60. host->reg = (struct tegra2_mmc *)host->base;
  61. }
  62. static void mmc_prepare_data(struct mmc_host *host, struct mmc_data *data)
  63. {
  64. unsigned char ctrl;
  65. debug("data->dest: %08X, data->blocks: %u, data->blocksize: %u\n",
  66. (u32)data->dest, data->blocks, data->blocksize);
  67. writel((u32)data->dest, &host->reg->sysad);
  68. /*
  69. * DMASEL[4:3]
  70. * 00 = Selects SDMA
  71. * 01 = Reserved
  72. * 10 = Selects 32-bit Address ADMA2
  73. * 11 = Selects 64-bit Address ADMA2
  74. */
  75. ctrl = readb(&host->reg->hostctl);
  76. ctrl &= ~TEGRA_MMC_HOSTCTL_DMASEL_MASK;
  77. ctrl |= TEGRA_MMC_HOSTCTL_DMASEL_SDMA;
  78. writeb(ctrl, &host->reg->hostctl);
  79. /* We do not handle DMA boundaries, so set it to max (512 KiB) */
  80. writew((7 << 12) | (data->blocksize & 0xFFF), &host->reg->blksize);
  81. writew(data->blocks, &host->reg->blkcnt);
  82. }
  83. static void mmc_set_transfer_mode(struct mmc_host *host, struct mmc_data *data)
  84. {
  85. unsigned short mode;
  86. debug(" mmc_set_transfer_mode called\n");
  87. /*
  88. * TRNMOD
  89. * MUL1SIN0[5] : Multi/Single Block Select
  90. * RD1WT0[4] : Data Transfer Direction Select
  91. * 1 = read
  92. * 0 = write
  93. * ENACMD12[2] : Auto CMD12 Enable
  94. * ENBLKCNT[1] : Block Count Enable
  95. * ENDMA[0] : DMA Enable
  96. */
  97. mode = (TEGRA_MMC_TRNMOD_DMA_ENABLE |
  98. TEGRA_MMC_TRNMOD_BLOCK_COUNT_ENABLE);
  99. if (data->blocks > 1)
  100. mode |= TEGRA_MMC_TRNMOD_MULTI_BLOCK_SELECT;
  101. if (data->flags & MMC_DATA_READ)
  102. mode |= TEGRA_MMC_TRNMOD_DATA_XFER_DIR_SEL_READ;
  103. if (data->flags & MMC_DATA_WRITE) {
  104. if ((uintptr_t)data->src & (ARCH_DMA_MINALIGN - 1))
  105. printf("Warning: unaligned write to %p may fail\n",
  106. data->src);
  107. flush_dcache_range((ulong)data->src, (ulong)data->src +
  108. data->blocks * data->blocksize);
  109. }
  110. writew(mode, &host->reg->trnmod);
  111. }
  112. static int mmc_wait_inhibit(struct mmc_host *host,
  113. struct mmc_cmd *cmd,
  114. struct mmc_data *data,
  115. unsigned int timeout)
  116. {
  117. /*
  118. * PRNSTS
  119. * CMDINHDAT[1] : Command Inhibit (DAT)
  120. * CMDINHCMD[0] : Command Inhibit (CMD)
  121. */
  122. unsigned int mask = TEGRA_MMC_PRNSTS_CMD_INHIBIT_CMD;
  123. /*
  124. * We shouldn't wait for data inhibit for stop commands, even
  125. * though they might use busy signaling
  126. */
  127. if ((data == NULL) && (cmd->resp_type & MMC_RSP_BUSY))
  128. mask |= TEGRA_MMC_PRNSTS_CMD_INHIBIT_DAT;
  129. while (readl(&host->reg->prnsts) & mask) {
  130. if (timeout == 0) {
  131. printf("%s: timeout error\n", __func__);
  132. return -1;
  133. }
  134. timeout--;
  135. udelay(1000);
  136. }
  137. return 0;
  138. }
  139. static int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
  140. struct mmc_data *data)
  141. {
  142. struct mmc_host *host = (struct mmc_host *)mmc->priv;
  143. int flags, i;
  144. int result;
  145. unsigned int mask = 0;
  146. unsigned int retry = 0x100000;
  147. debug(" mmc_send_cmd called\n");
  148. result = mmc_wait_inhibit(host, cmd, data, 10 /* ms */);
  149. if (result < 0)
  150. return result;
  151. if (data)
  152. mmc_prepare_data(host, data);
  153. debug("cmd->arg: %08x\n", cmd->cmdarg);
  154. writel(cmd->cmdarg, &host->reg->argument);
  155. if (data)
  156. mmc_set_transfer_mode(host, data);
  157. if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
  158. return -1;
  159. /*
  160. * CMDREG
  161. * CMDIDX[13:8] : Command index
  162. * DATAPRNT[5] : Data Present Select
  163. * ENCMDIDX[4] : Command Index Check Enable
  164. * ENCMDCRC[3] : Command CRC Check Enable
  165. * RSPTYP[1:0]
  166. * 00 = No Response
  167. * 01 = Length 136
  168. * 10 = Length 48
  169. * 11 = Length 48 Check busy after response
  170. */
  171. if (!(cmd->resp_type & MMC_RSP_PRESENT))
  172. flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_NO_RESPONSE;
  173. else if (cmd->resp_type & MMC_RSP_136)
  174. flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_136;
  175. else if (cmd->resp_type & MMC_RSP_BUSY)
  176. flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_48_BUSY;
  177. else
  178. flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_48;
  179. if (cmd->resp_type & MMC_RSP_CRC)
  180. flags |= TEGRA_MMC_TRNMOD_CMD_CRC_CHECK;
  181. if (cmd->resp_type & MMC_RSP_OPCODE)
  182. flags |= TEGRA_MMC_TRNMOD_CMD_INDEX_CHECK;
  183. if (data)
  184. flags |= TEGRA_MMC_TRNMOD_DATA_PRESENT_SELECT_DATA_TRANSFER;
  185. debug("cmd: %d\n", cmd->cmdidx);
  186. writew((cmd->cmdidx << 8) | flags, &host->reg->cmdreg);
  187. for (i = 0; i < retry; i++) {
  188. mask = readl(&host->reg->norintsts);
  189. /* Command Complete */
  190. if (mask & TEGRA_MMC_NORINTSTS_CMD_COMPLETE) {
  191. if (!data)
  192. writel(mask, &host->reg->norintsts);
  193. break;
  194. }
  195. }
  196. if (i == retry) {
  197. printf("%s: waiting for status update\n", __func__);
  198. writel(mask, &host->reg->norintsts);
  199. return TIMEOUT;
  200. }
  201. if (mask & TEGRA_MMC_NORINTSTS_CMD_TIMEOUT) {
  202. /* Timeout Error */
  203. debug("timeout: %08x cmd %d\n", mask, cmd->cmdidx);
  204. writel(mask, &host->reg->norintsts);
  205. return TIMEOUT;
  206. } else if (mask & TEGRA_MMC_NORINTSTS_ERR_INTERRUPT) {
  207. /* Error Interrupt */
  208. debug("error: %08x cmd %d\n", mask, cmd->cmdidx);
  209. writel(mask, &host->reg->norintsts);
  210. return -1;
  211. }
  212. if (cmd->resp_type & MMC_RSP_PRESENT) {
  213. if (cmd->resp_type & MMC_RSP_136) {
  214. /* CRC is stripped so we need to do some shifting. */
  215. for (i = 0; i < 4; i++) {
  216. unsigned int offset =
  217. (unsigned int)(&host->reg->rspreg3 - i);
  218. cmd->response[i] = readl(offset) << 8;
  219. if (i != 3) {
  220. cmd->response[i] |=
  221. readb(offset - 1);
  222. }
  223. debug("cmd->resp[%d]: %08x\n",
  224. i, cmd->response[i]);
  225. }
  226. } else if (cmd->resp_type & MMC_RSP_BUSY) {
  227. for (i = 0; i < retry; i++) {
  228. /* PRNTDATA[23:20] : DAT[3:0] Line Signal */
  229. if (readl(&host->reg->prnsts)
  230. & (1 << 20)) /* DAT[0] */
  231. break;
  232. }
  233. if (i == retry) {
  234. printf("%s: card is still busy\n", __func__);
  235. writel(mask, &host->reg->norintsts);
  236. return TIMEOUT;
  237. }
  238. cmd->response[0] = readl(&host->reg->rspreg0);
  239. debug("cmd->resp[0]: %08x\n", cmd->response[0]);
  240. } else {
  241. cmd->response[0] = readl(&host->reg->rspreg0);
  242. debug("cmd->resp[0]: %08x\n", cmd->response[0]);
  243. }
  244. }
  245. if (data) {
  246. unsigned long start = get_timer(0);
  247. while (1) {
  248. mask = readl(&host->reg->norintsts);
  249. if (mask & TEGRA_MMC_NORINTSTS_ERR_INTERRUPT) {
  250. /* Error Interrupt */
  251. writel(mask, &host->reg->norintsts);
  252. printf("%s: error during transfer: 0x%08x\n",
  253. __func__, mask);
  254. return -1;
  255. } else if (mask & TEGRA_MMC_NORINTSTS_DMA_INTERRUPT) {
  256. /*
  257. * DMA Interrupt, restart the transfer where
  258. * it was interrupted.
  259. */
  260. unsigned int address = readl(&host->reg->sysad);
  261. debug("DMA end\n");
  262. writel(TEGRA_MMC_NORINTSTS_DMA_INTERRUPT,
  263. &host->reg->norintsts);
  264. writel(address, &host->reg->sysad);
  265. } else if (mask & TEGRA_MMC_NORINTSTS_XFER_COMPLETE) {
  266. /* Transfer Complete */
  267. debug("r/w is done\n");
  268. break;
  269. } else if (get_timer(start) > 2000UL) {
  270. writel(mask, &host->reg->norintsts);
  271. printf("%s: MMC Timeout\n"
  272. " Interrupt status 0x%08x\n"
  273. " Interrupt status enable 0x%08x\n"
  274. " Interrupt signal enable 0x%08x\n"
  275. " Present status 0x%08x\n",
  276. __func__, mask,
  277. readl(&host->reg->norintstsen),
  278. readl(&host->reg->norintsigen),
  279. readl(&host->reg->prnsts));
  280. return -1;
  281. }
  282. }
  283. writel(mask, &host->reg->norintsts);
  284. if (data->flags & MMC_DATA_READ) {
  285. if ((uintptr_t)data->dest & (ARCH_DMA_MINALIGN - 1))
  286. printf("Warning: unaligned read from %p "
  287. "may fail\n", data->dest);
  288. invalidate_dcache_range((ulong)data->dest,
  289. (ulong)data->dest +
  290. data->blocks * data->blocksize);
  291. }
  292. }
  293. udelay(1000);
  294. return 0;
  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 Tegra2 SDMMCx clock divisor here. Source is 216MHz,
  304. * PLLP_OUT0
  305. */
  306. if (clock == 0)
  307. goto out;
  308. clock_adjust_periph_pll_div(host->mmc_id, CLOCK_ID_PERIPH, clock,
  309. &div);
  310. debug("div = %d\n", div);
  311. writew(0, &host->reg->clkcon);
  312. /*
  313. * CLKCON
  314. * SELFREQ[15:8] : base clock divided by value
  315. * ENSDCLK[2] : SD Clock Enable
  316. * STBLINTCLK[1] : Internal Clock Stable
  317. * ENINTCLK[0] : Internal Clock Enable
  318. */
  319. div >>= 1;
  320. clk = ((div << TEGRA_MMC_CLKCON_SDCLK_FREQ_SEL_SHIFT) |
  321. TEGRA_MMC_CLKCON_INTERNAL_CLOCK_ENABLE);
  322. writew(clk, &host->reg->clkcon);
  323. /* Wait max 10 ms */
  324. timeout = 10;
  325. while (!(readw(&host->reg->clkcon) &
  326. TEGRA_MMC_CLKCON_INTERNAL_CLOCK_STABLE)) {
  327. if (timeout == 0) {
  328. printf("%s: timeout error\n", __func__);
  329. return;
  330. }
  331. timeout--;
  332. udelay(1000);
  333. }
  334. clk |= TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE;
  335. writew(clk, &host->reg->clkcon);
  336. debug("mmc_change_clock: clkcon = %08X\n", clk);
  337. out:
  338. host->clock = clock;
  339. }
  340. static void mmc_set_ios(struct mmc *mmc)
  341. {
  342. struct mmc_host *host = mmc->priv;
  343. unsigned char ctrl;
  344. debug(" mmc_set_ios called\n");
  345. debug("bus_width: %x, clock: %d\n", mmc->bus_width, mmc->clock);
  346. /* Change clock first */
  347. mmc_change_clock(host, mmc->clock);
  348. ctrl = readb(&host->reg->hostctl);
  349. /*
  350. * WIDE8[5]
  351. * 0 = Depend on WIDE4
  352. * 1 = 8-bit mode
  353. * WIDE4[1]
  354. * 1 = 4-bit mode
  355. * 0 = 1-bit mode
  356. */
  357. if (mmc->bus_width == 8)
  358. ctrl |= (1 << 5);
  359. else if (mmc->bus_width == 4)
  360. ctrl |= (1 << 1);
  361. else
  362. ctrl &= ~(1 << 1);
  363. writeb(ctrl, &host->reg->hostctl);
  364. debug("mmc_set_ios: hostctl = %08X\n", ctrl);
  365. }
  366. static void mmc_reset(struct mmc_host *host)
  367. {
  368. unsigned int timeout;
  369. debug(" mmc_reset called\n");
  370. /*
  371. * RSTALL[0] : Software reset for all
  372. * 1 = reset
  373. * 0 = work
  374. */
  375. writeb(TEGRA_MMC_SWRST_SW_RESET_FOR_ALL, &host->reg->swrst);
  376. host->clock = 0;
  377. /* Wait max 100 ms */
  378. timeout = 100;
  379. /* hw clears the bit when it's done */
  380. while (readb(&host->reg->swrst) & TEGRA_MMC_SWRST_SW_RESET_FOR_ALL) {
  381. if (timeout == 0) {
  382. printf("%s: timeout error\n", __func__);
  383. return;
  384. }
  385. timeout--;
  386. udelay(1000);
  387. }
  388. }
  389. static int mmc_core_init(struct mmc *mmc)
  390. {
  391. struct mmc_host *host = (struct mmc_host *)mmc->priv;
  392. unsigned int mask;
  393. debug(" mmc_core_init called\n");
  394. mmc_reset(host);
  395. host->version = readw(&host->reg->hcver);
  396. debug("host version = %x\n", host->version);
  397. /* mask all */
  398. writel(0xffffffff, &host->reg->norintstsen);
  399. writel(0xffffffff, &host->reg->norintsigen);
  400. writeb(0xe, &host->reg->timeoutcon); /* TMCLK * 2^27 */
  401. /*
  402. * NORMAL Interrupt Status Enable Register init
  403. * [5] ENSTABUFRDRDY : Buffer Read Ready Status Enable
  404. * [4] ENSTABUFWTRDY : Buffer write Ready Status Enable
  405. * [3] ENSTADMAINT : DMA boundary interrupt
  406. * [1] ENSTASTANSCMPLT : Transfre Complete Status Enable
  407. * [0] ENSTACMDCMPLT : Command Complete Status Enable
  408. */
  409. mask = readl(&host->reg->norintstsen);
  410. mask &= ~(0xffff);
  411. mask |= (TEGRA_MMC_NORINTSTSEN_CMD_COMPLETE |
  412. TEGRA_MMC_NORINTSTSEN_XFER_COMPLETE |
  413. TEGRA_MMC_NORINTSTSEN_DMA_INTERRUPT |
  414. TEGRA_MMC_NORINTSTSEN_BUFFER_WRITE_READY |
  415. TEGRA_MMC_NORINTSTSEN_BUFFER_READ_READY);
  416. writel(mask, &host->reg->norintstsen);
  417. /*
  418. * NORMAL Interrupt Signal Enable Register init
  419. * [1] ENSTACMDCMPLT : Transfer Complete Signal Enable
  420. */
  421. mask = readl(&host->reg->norintsigen);
  422. mask &= ~(0xffff);
  423. mask |= TEGRA_MMC_NORINTSIGEN_XFER_COMPLETE;
  424. writel(mask, &host->reg->norintsigen);
  425. return 0;
  426. }
  427. int tegra2_mmc_getcd(struct mmc *mmc)
  428. {
  429. struct mmc_host *host = (struct mmc_host *)mmc->priv;
  430. debug("tegra2_mmc_getcd called\n");
  431. if (host->cd_gpio >= 0)
  432. return !gpio_get_value(host->cd_gpio);
  433. return 1;
  434. }
  435. int tegra2_mmc_init(int dev_index, int bus_width, int pwr_gpio, int cd_gpio)
  436. {
  437. struct mmc_host *host;
  438. char gpusage[12]; /* "SD/MMCn PWR" or "SD/MMCn CD" */
  439. struct mmc *mmc;
  440. debug(" tegra2_mmc_init: index %d, bus width %d "
  441. "pwr_gpio %d cd_gpio %d\n",
  442. dev_index, bus_width, pwr_gpio, cd_gpio);
  443. host = &mmc_host[dev_index];
  444. host->clock = 0;
  445. host->pwr_gpio = pwr_gpio;
  446. host->cd_gpio = cd_gpio;
  447. tegra2_get_setup(host, dev_index);
  448. clock_start_periph_pll(host->mmc_id, CLOCK_ID_PERIPH, 20000000);
  449. if (host->pwr_gpio >= 0) {
  450. sprintf(gpusage, "SD/MMC%d PWR", dev_index);
  451. gpio_request(host->pwr_gpio, gpusage);
  452. gpio_direction_output(host->pwr_gpio, 1);
  453. }
  454. if (host->cd_gpio >= 0) {
  455. sprintf(gpusage, "SD/MMC%d CD", dev_index);
  456. gpio_request(host->cd_gpio, gpusage);
  457. gpio_direction_input(host->cd_gpio);
  458. }
  459. mmc = &mmc_dev[dev_index];
  460. sprintf(mmc->name, "Tegra2 SD/MMC");
  461. mmc->priv = host;
  462. mmc->send_cmd = mmc_send_cmd;
  463. mmc->set_ios = mmc_set_ios;
  464. mmc->init = mmc_core_init;
  465. mmc->getcd = tegra2_mmc_getcd;
  466. mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
  467. if (bus_width == 8)
  468. mmc->host_caps = MMC_MODE_8BIT;
  469. else
  470. mmc->host_caps = MMC_MODE_4BIT;
  471. mmc->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS | MMC_MODE_HC;
  472. /*
  473. * min freq is for card identification, and is the highest
  474. * low-speed SDIO card frequency (actually 400KHz)
  475. * max freq is highest HS eMMC clock as per the SD/MMC spec
  476. * (actually 52MHz)
  477. * Both of these are the closest equivalents w/216MHz source
  478. * clock and Tegra2 SDMMC divisors.
  479. */
  480. mmc->f_min = 375000;
  481. mmc->f_max = 48000000;
  482. mmc_register(mmc);
  483. return 0;
  484. }