tegra2_mmc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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 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 "tegra2_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;
  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. return TIMEOUT;
  199. }
  200. if (mask & TEGRA_MMC_NORINTSTS_CMD_TIMEOUT) {
  201. /* Timeout Error */
  202. debug("timeout: %08x cmd %d\n", mask, cmd->cmdidx);
  203. return TIMEOUT;
  204. } else if (mask & TEGRA_MMC_NORINTSTS_ERR_INTERRUPT) {
  205. /* Error Interrupt */
  206. debug("error: %08x cmd %d\n", mask, cmd->cmdidx);
  207. return -1;
  208. }
  209. if (cmd->resp_type & MMC_RSP_PRESENT) {
  210. if (cmd->resp_type & MMC_RSP_136) {
  211. /* CRC is stripped so we need to do some shifting. */
  212. for (i = 0; i < 4; i++) {
  213. unsigned int offset =
  214. (unsigned int)(&host->reg->rspreg3 - i);
  215. cmd->response[i] = readl(offset) << 8;
  216. if (i != 3) {
  217. cmd->response[i] |=
  218. readb(offset - 1);
  219. }
  220. debug("cmd->resp[%d]: %08x\n",
  221. i, cmd->response[i]);
  222. }
  223. } else if (cmd->resp_type & MMC_RSP_BUSY) {
  224. for (i = 0; i < retry; i++) {
  225. /* PRNTDATA[23:20] : DAT[3:0] Line Signal */
  226. if (readl(&host->reg->prnsts)
  227. & (1 << 20)) /* DAT[0] */
  228. break;
  229. }
  230. if (i == retry) {
  231. printf("%s: card is still busy\n", __func__);
  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. if (data->flags & MMC_DATA_READ) {
  281. if ((uintptr_t)data->dest & (ARCH_DMA_MINALIGN - 1))
  282. printf("Warning: unaligned read from %p "
  283. "may fail\n", data->dest);
  284. invalidate_dcache_range((ulong)data->dest,
  285. (ulong)data->dest +
  286. data->blocks * data->blocksize);
  287. }
  288. }
  289. udelay(1000);
  290. return 0;
  291. }
  292. static void mmc_change_clock(struct mmc_host *host, uint clock)
  293. {
  294. int div;
  295. unsigned short clk;
  296. unsigned long timeout;
  297. debug(" mmc_change_clock called\n");
  298. /*
  299. * Change Tegra2 SDMMCx clock divisor here. Source is 216MHz,
  300. * PLLP_OUT0
  301. */
  302. if (clock == 0)
  303. goto out;
  304. clock_adjust_periph_pll_div(host->mmc_id, CLOCK_ID_PERIPH, clock,
  305. &div);
  306. debug("div = %d\n", div);
  307. writew(0, &host->reg->clkcon);
  308. /*
  309. * CLKCON
  310. * SELFREQ[15:8] : base clock divided by value
  311. * ENSDCLK[2] : SD Clock Enable
  312. * STBLINTCLK[1] : Internal Clock Stable
  313. * ENINTCLK[0] : Internal Clock Enable
  314. */
  315. div >>= 1;
  316. clk = ((div << TEGRA_MMC_CLKCON_SDCLK_FREQ_SEL_SHIFT) |
  317. TEGRA_MMC_CLKCON_INTERNAL_CLOCK_ENABLE);
  318. writew(clk, &host->reg->clkcon);
  319. /* Wait max 10 ms */
  320. timeout = 10;
  321. while (!(readw(&host->reg->clkcon) &
  322. TEGRA_MMC_CLKCON_INTERNAL_CLOCK_STABLE)) {
  323. if (timeout == 0) {
  324. printf("%s: timeout error\n", __func__);
  325. return;
  326. }
  327. timeout--;
  328. udelay(1000);
  329. }
  330. clk |= TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE;
  331. writew(clk, &host->reg->clkcon);
  332. debug("mmc_change_clock: clkcon = %08X\n", clk);
  333. out:
  334. host->clock = clock;
  335. }
  336. static void mmc_set_ios(struct mmc *mmc)
  337. {
  338. struct mmc_host *host = mmc->priv;
  339. unsigned char ctrl;
  340. debug(" mmc_set_ios called\n");
  341. debug("bus_width: %x, clock: %d\n", mmc->bus_width, mmc->clock);
  342. /* Change clock first */
  343. mmc_change_clock(host, mmc->clock);
  344. ctrl = readb(&host->reg->hostctl);
  345. /*
  346. * WIDE8[5]
  347. * 0 = Depend on WIDE4
  348. * 1 = 8-bit mode
  349. * WIDE4[1]
  350. * 1 = 4-bit mode
  351. * 0 = 1-bit mode
  352. */
  353. if (mmc->bus_width == 8)
  354. ctrl |= (1 << 5);
  355. else if (mmc->bus_width == 4)
  356. ctrl |= (1 << 1);
  357. else
  358. ctrl &= ~(1 << 1);
  359. writeb(ctrl, &host->reg->hostctl);
  360. debug("mmc_set_ios: hostctl = %08X\n", ctrl);
  361. }
  362. static void mmc_reset(struct mmc_host *host)
  363. {
  364. unsigned int timeout;
  365. debug(" mmc_reset called\n");
  366. /*
  367. * RSTALL[0] : Software reset for all
  368. * 1 = reset
  369. * 0 = work
  370. */
  371. writeb(TEGRA_MMC_SWRST_SW_RESET_FOR_ALL, &host->reg->swrst);
  372. host->clock = 0;
  373. /* Wait max 100 ms */
  374. timeout = 100;
  375. /* hw clears the bit when it's done */
  376. while (readb(&host->reg->swrst) & TEGRA_MMC_SWRST_SW_RESET_FOR_ALL) {
  377. if (timeout == 0) {
  378. printf("%s: timeout error\n", __func__);
  379. return;
  380. }
  381. timeout--;
  382. udelay(1000);
  383. }
  384. }
  385. static int mmc_core_init(struct mmc *mmc)
  386. {
  387. struct mmc_host *host = (struct mmc_host *)mmc->priv;
  388. unsigned int mask;
  389. debug(" mmc_core_init called\n");
  390. mmc_reset(host);
  391. host->version = readw(&host->reg->hcver);
  392. debug("host version = %x\n", host->version);
  393. /* mask all */
  394. writel(0xffffffff, &host->reg->norintstsen);
  395. writel(0xffffffff, &host->reg->norintsigen);
  396. writeb(0xe, &host->reg->timeoutcon); /* TMCLK * 2^27 */
  397. /*
  398. * NORMAL Interrupt Status Enable Register init
  399. * [5] ENSTABUFRDRDY : Buffer Read Ready Status Enable
  400. * [4] ENSTABUFWTRDY : Buffer write Ready Status Enable
  401. * [3] ENSTADMAINT : DMA boundary interrupt
  402. * [1] ENSTASTANSCMPLT : Transfre Complete Status Enable
  403. * [0] ENSTACMDCMPLT : Command Complete Status Enable
  404. */
  405. mask = readl(&host->reg->norintstsen);
  406. mask &= ~(0xffff);
  407. mask |= (TEGRA_MMC_NORINTSTSEN_CMD_COMPLETE |
  408. TEGRA_MMC_NORINTSTSEN_XFER_COMPLETE |
  409. TEGRA_MMC_NORINTSTSEN_DMA_INTERRUPT |
  410. TEGRA_MMC_NORINTSTSEN_BUFFER_WRITE_READY |
  411. TEGRA_MMC_NORINTSTSEN_BUFFER_READ_READY);
  412. writel(mask, &host->reg->norintstsen);
  413. /*
  414. * NORMAL Interrupt Signal Enable Register init
  415. * [1] ENSTACMDCMPLT : Transfer Complete Signal Enable
  416. */
  417. mask = readl(&host->reg->norintsigen);
  418. mask &= ~(0xffff);
  419. mask |= TEGRA_MMC_NORINTSIGEN_XFER_COMPLETE;
  420. writel(mask, &host->reg->norintsigen);
  421. return 0;
  422. }
  423. int tegra2_mmc_getcd(struct mmc *mmc)
  424. {
  425. struct mmc_host *host = (struct mmc_host *)mmc->priv;
  426. debug("tegra2_mmc_getcd called\n");
  427. if (host->cd_gpio >= 0)
  428. return !gpio_get_value(host->cd_gpio);
  429. return 1;
  430. }
  431. int tegra2_mmc_init(int dev_index, int bus_width, int pwr_gpio, int cd_gpio)
  432. {
  433. struct mmc_host *host;
  434. char gpusage[12]; /* "SD/MMCn PWR" or "SD/MMCn CD" */
  435. struct mmc *mmc;
  436. debug(" tegra2_mmc_init: index %d, bus width %d "
  437. "pwr_gpio %d cd_gpio %d\n",
  438. dev_index, bus_width, pwr_gpio, cd_gpio);
  439. host = &mmc_host[dev_index];
  440. host->clock = 0;
  441. host->pwr_gpio = pwr_gpio;
  442. host->cd_gpio = cd_gpio;
  443. tegra2_get_setup(host, dev_index);
  444. clock_start_periph_pll(host->mmc_id, CLOCK_ID_PERIPH, 20000000);
  445. if (host->pwr_gpio >= 0) {
  446. sprintf(gpusage, "SD/MMC%d PWR", dev_index);
  447. gpio_request(host->pwr_gpio, gpusage);
  448. gpio_direction_output(host->pwr_gpio, 1);
  449. }
  450. if (host->cd_gpio >= 0) {
  451. sprintf(gpusage, "SD/MMC%d CD", dev_index);
  452. gpio_request(host->cd_gpio, gpusage);
  453. gpio_direction_input(host->cd_gpio);
  454. }
  455. mmc = &mmc_dev[dev_index];
  456. sprintf(mmc->name, "Tegra2 SD/MMC");
  457. mmc->priv = host;
  458. mmc->send_cmd = mmc_send_cmd;
  459. mmc->set_ios = mmc_set_ios;
  460. mmc->init = mmc_core_init;
  461. mmc->getcd = tegra2_mmc_getcd;
  462. mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
  463. if (bus_width == 8)
  464. mmc->host_caps = MMC_MODE_8BIT;
  465. else
  466. mmc->host_caps = MMC_MODE_4BIT;
  467. mmc->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS | MMC_MODE_HC;
  468. /*
  469. * min freq is for card identification, and is the highest
  470. * low-speed SDIO card frequency (actually 400KHz)
  471. * max freq is highest HS eMMC clock as per the SD/MMC spec
  472. * (actually 52MHz)
  473. * Both of these are the closest equivalents w/216MHz source
  474. * clock and Tegra2 SDMMC divisors.
  475. */
  476. mmc->f_min = 375000;
  477. mmc->f_max = 48000000;
  478. mmc_register(mmc);
  479. return 0;
  480. }