tegra2_mmc.c 13 KB

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