tegra2_mmc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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. writew(mode, &host->reg->trnmod);
  104. }
  105. static int mmc_wait_inhibit(struct mmc_host *host,
  106. struct mmc_cmd *cmd,
  107. struct mmc_data *data,
  108. unsigned int timeout)
  109. {
  110. /*
  111. * PRNSTS
  112. * CMDINHDAT[1] : Command Inhibit (DAT)
  113. * CMDINHCMD[0] : Command Inhibit (CMD)
  114. */
  115. unsigned int mask = TEGRA_MMC_PRNSTS_CMD_INHIBIT_CMD;
  116. /*
  117. * We shouldn't wait for data inhibit for stop commands, even
  118. * though they might use busy signaling
  119. */
  120. if ((data == NULL) && (cmd->resp_type & MMC_RSP_BUSY))
  121. mask |= TEGRA_MMC_PRNSTS_CMD_INHIBIT_DAT;
  122. while (readl(&host->reg->prnsts) & mask) {
  123. if (timeout == 0) {
  124. printf("%s: timeout error\n", __func__);
  125. return -1;
  126. }
  127. timeout--;
  128. udelay(1000);
  129. }
  130. return 0;
  131. }
  132. static int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
  133. struct mmc_data *data)
  134. {
  135. struct mmc_host *host = (struct mmc_host *)mmc->priv;
  136. int flags, i;
  137. int result;
  138. unsigned int mask;
  139. unsigned int retry = 0x100000;
  140. debug(" mmc_send_cmd called\n");
  141. result = mmc_wait_inhibit(host, cmd, data, 10 /* ms */);
  142. if (result < 0)
  143. return result;
  144. if (data)
  145. mmc_prepare_data(host, data);
  146. debug("cmd->arg: %08x\n", cmd->cmdarg);
  147. writel(cmd->cmdarg, &host->reg->argument);
  148. if (data)
  149. mmc_set_transfer_mode(host, data);
  150. if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
  151. return -1;
  152. /*
  153. * CMDREG
  154. * CMDIDX[13:8] : Command index
  155. * DATAPRNT[5] : Data Present Select
  156. * ENCMDIDX[4] : Command Index Check Enable
  157. * ENCMDCRC[3] : Command CRC Check Enable
  158. * RSPTYP[1:0]
  159. * 00 = No Response
  160. * 01 = Length 136
  161. * 10 = Length 48
  162. * 11 = Length 48 Check busy after response
  163. */
  164. if (!(cmd->resp_type & MMC_RSP_PRESENT))
  165. flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_NO_RESPONSE;
  166. else if (cmd->resp_type & MMC_RSP_136)
  167. flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_136;
  168. else if (cmd->resp_type & MMC_RSP_BUSY)
  169. flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_48_BUSY;
  170. else
  171. flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_48;
  172. if (cmd->resp_type & MMC_RSP_CRC)
  173. flags |= TEGRA_MMC_TRNMOD_CMD_CRC_CHECK;
  174. if (cmd->resp_type & MMC_RSP_OPCODE)
  175. flags |= TEGRA_MMC_TRNMOD_CMD_INDEX_CHECK;
  176. if (data)
  177. flags |= TEGRA_MMC_TRNMOD_DATA_PRESENT_SELECT_DATA_TRANSFER;
  178. debug("cmd: %d\n", cmd->cmdidx);
  179. writew((cmd->cmdidx << 8) | flags, &host->reg->cmdreg);
  180. for (i = 0; i < retry; i++) {
  181. mask = readl(&host->reg->norintsts);
  182. /* Command Complete */
  183. if (mask & TEGRA_MMC_NORINTSTS_CMD_COMPLETE) {
  184. if (!data)
  185. writel(mask, &host->reg->norintsts);
  186. break;
  187. }
  188. }
  189. if (i == retry) {
  190. printf("%s: waiting for status update\n", __func__);
  191. return TIMEOUT;
  192. }
  193. if (mask & TEGRA_MMC_NORINTSTS_CMD_TIMEOUT) {
  194. /* Timeout Error */
  195. debug("timeout: %08x cmd %d\n", mask, cmd->cmdidx);
  196. return TIMEOUT;
  197. } else if (mask & TEGRA_MMC_NORINTSTS_ERR_INTERRUPT) {
  198. /* Error Interrupt */
  199. debug("error: %08x cmd %d\n", mask, cmd->cmdidx);
  200. return -1;
  201. }
  202. if (cmd->resp_type & MMC_RSP_PRESENT) {
  203. if (cmd->resp_type & MMC_RSP_136) {
  204. /* CRC is stripped so we need to do some shifting. */
  205. for (i = 0; i < 4; i++) {
  206. unsigned int offset =
  207. (unsigned int)(&host->reg->rspreg3 - i);
  208. cmd->response[i] = readl(offset) << 8;
  209. if (i != 3) {
  210. cmd->response[i] |=
  211. readb(offset - 1);
  212. }
  213. debug("cmd->resp[%d]: %08x\n",
  214. i, cmd->response[i]);
  215. }
  216. } else if (cmd->resp_type & MMC_RSP_BUSY) {
  217. for (i = 0; i < retry; i++) {
  218. /* PRNTDATA[23:20] : DAT[3:0] Line Signal */
  219. if (readl(&host->reg->prnsts)
  220. & (1 << 20)) /* DAT[0] */
  221. break;
  222. }
  223. if (i == retry) {
  224. printf("%s: card is still busy\n", __func__);
  225. return TIMEOUT;
  226. }
  227. cmd->response[0] = readl(&host->reg->rspreg0);
  228. debug("cmd->resp[0]: %08x\n", cmd->response[0]);
  229. } else {
  230. cmd->response[0] = readl(&host->reg->rspreg0);
  231. debug("cmd->resp[0]: %08x\n", cmd->response[0]);
  232. }
  233. }
  234. if (data) {
  235. unsigned long start = get_timer(0);
  236. while (1) {
  237. mask = readl(&host->reg->norintsts);
  238. if (mask & TEGRA_MMC_NORINTSTS_ERR_INTERRUPT) {
  239. /* Error Interrupt */
  240. writel(mask, &host->reg->norintsts);
  241. printf("%s: error during transfer: 0x%08x\n",
  242. __func__, mask);
  243. return -1;
  244. } else if (mask & TEGRA_MMC_NORINTSTS_DMA_INTERRUPT) {
  245. /*
  246. * DMA Interrupt, restart the transfer where
  247. * it was interrupted.
  248. */
  249. unsigned int address = readl(&host->reg->sysad);
  250. debug("DMA end\n");
  251. writel(TEGRA_MMC_NORINTSTS_DMA_INTERRUPT,
  252. &host->reg->norintsts);
  253. writel(address, &host->reg->sysad);
  254. } else if (mask & TEGRA_MMC_NORINTSTS_XFER_COMPLETE) {
  255. /* Transfer Complete */
  256. debug("r/w is done\n");
  257. break;
  258. } else if (get_timer(start) > 2000UL) {
  259. writel(mask, &host->reg->norintsts);
  260. printf("%s: MMC Timeout\n"
  261. " Interrupt status 0x%08x\n"
  262. " Interrupt status enable 0x%08x\n"
  263. " Interrupt signal enable 0x%08x\n"
  264. " Present status 0x%08x\n",
  265. __func__, mask,
  266. readl(&host->reg->norintstsen),
  267. readl(&host->reg->norintsigen),
  268. readl(&host->reg->prnsts));
  269. return -1;
  270. }
  271. }
  272. writel(mask, &host->reg->norintsts);
  273. }
  274. udelay(1000);
  275. return 0;
  276. }
  277. static void mmc_change_clock(struct mmc_host *host, uint clock)
  278. {
  279. int div;
  280. unsigned short clk;
  281. unsigned long timeout;
  282. debug(" mmc_change_clock called\n");
  283. /*
  284. * Change Tegra2 SDMMCx clock divisor here. Source is 216MHz,
  285. * PLLP_OUT0
  286. */
  287. if (clock == 0)
  288. goto out;
  289. clock_adjust_periph_pll_div(host->mmc_id, CLOCK_ID_PERIPH, clock,
  290. &div);
  291. debug("div = %d\n", div);
  292. writew(0, &host->reg->clkcon);
  293. /*
  294. * CLKCON
  295. * SELFREQ[15:8] : base clock divided by value
  296. * ENSDCLK[2] : SD Clock Enable
  297. * STBLINTCLK[1] : Internal Clock Stable
  298. * ENINTCLK[0] : Internal Clock Enable
  299. */
  300. div >>= 1;
  301. clk = ((div << TEGRA_MMC_CLKCON_SDCLK_FREQ_SEL_SHIFT) |
  302. TEGRA_MMC_CLKCON_INTERNAL_CLOCK_ENABLE);
  303. writew(clk, &host->reg->clkcon);
  304. /* Wait max 10 ms */
  305. timeout = 10;
  306. while (!(readw(&host->reg->clkcon) &
  307. TEGRA_MMC_CLKCON_INTERNAL_CLOCK_STABLE)) {
  308. if (timeout == 0) {
  309. printf("%s: timeout error\n", __func__);
  310. return;
  311. }
  312. timeout--;
  313. udelay(1000);
  314. }
  315. clk |= TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE;
  316. writew(clk, &host->reg->clkcon);
  317. debug("mmc_change_clock: clkcon = %08X\n", clk);
  318. out:
  319. host->clock = clock;
  320. }
  321. static void mmc_set_ios(struct mmc *mmc)
  322. {
  323. struct mmc_host *host = mmc->priv;
  324. unsigned char ctrl;
  325. debug(" mmc_set_ios called\n");
  326. debug("bus_width: %x, clock: %d\n", mmc->bus_width, mmc->clock);
  327. /* Change clock first */
  328. mmc_change_clock(host, mmc->clock);
  329. ctrl = readb(&host->reg->hostctl);
  330. /*
  331. * WIDE8[5]
  332. * 0 = Depend on WIDE4
  333. * 1 = 8-bit mode
  334. * WIDE4[1]
  335. * 1 = 4-bit mode
  336. * 0 = 1-bit mode
  337. */
  338. if (mmc->bus_width == 8)
  339. ctrl |= (1 << 5);
  340. else if (mmc->bus_width == 4)
  341. ctrl |= (1 << 1);
  342. else
  343. ctrl &= ~(1 << 1);
  344. writeb(ctrl, &host->reg->hostctl);
  345. debug("mmc_set_ios: hostctl = %08X\n", ctrl);
  346. }
  347. static void mmc_reset(struct mmc_host *host)
  348. {
  349. unsigned int timeout;
  350. debug(" mmc_reset called\n");
  351. /*
  352. * RSTALL[0] : Software reset for all
  353. * 1 = reset
  354. * 0 = work
  355. */
  356. writeb(TEGRA_MMC_SWRST_SW_RESET_FOR_ALL, &host->reg->swrst);
  357. host->clock = 0;
  358. /* Wait max 100 ms */
  359. timeout = 100;
  360. /* hw clears the bit when it's done */
  361. while (readb(&host->reg->swrst) & TEGRA_MMC_SWRST_SW_RESET_FOR_ALL) {
  362. if (timeout == 0) {
  363. printf("%s: timeout error\n", __func__);
  364. return;
  365. }
  366. timeout--;
  367. udelay(1000);
  368. }
  369. }
  370. static int mmc_core_init(struct mmc *mmc)
  371. {
  372. struct mmc_host *host = (struct mmc_host *)mmc->priv;
  373. unsigned int mask;
  374. debug(" mmc_core_init called\n");
  375. mmc_reset(host);
  376. host->version = readw(&host->reg->hcver);
  377. debug("host version = %x\n", host->version);
  378. /* mask all */
  379. writel(0xffffffff, &host->reg->norintstsen);
  380. writel(0xffffffff, &host->reg->norintsigen);
  381. writeb(0xe, &host->reg->timeoutcon); /* TMCLK * 2^27 */
  382. /*
  383. * NORMAL Interrupt Status Enable Register init
  384. * [5] ENSTABUFRDRDY : Buffer Read Ready Status Enable
  385. * [4] ENSTABUFWTRDY : Buffer write Ready Status Enable
  386. * [3] ENSTADMAINT : DMA boundary interrupt
  387. * [1] ENSTASTANSCMPLT : Transfre Complete Status Enable
  388. * [0] ENSTACMDCMPLT : Command Complete Status Enable
  389. */
  390. mask = readl(&host->reg->norintstsen);
  391. mask &= ~(0xffff);
  392. mask |= (TEGRA_MMC_NORINTSTSEN_CMD_COMPLETE |
  393. TEGRA_MMC_NORINTSTSEN_XFER_COMPLETE |
  394. TEGRA_MMC_NORINTSTSEN_DMA_INTERRUPT |
  395. TEGRA_MMC_NORINTSTSEN_BUFFER_WRITE_READY |
  396. TEGRA_MMC_NORINTSTSEN_BUFFER_READ_READY);
  397. writel(mask, &host->reg->norintstsen);
  398. /*
  399. * NORMAL Interrupt Signal Enable Register init
  400. * [1] ENSTACMDCMPLT : Transfer Complete Signal Enable
  401. */
  402. mask = readl(&host->reg->norintsigen);
  403. mask &= ~(0xffff);
  404. mask |= TEGRA_MMC_NORINTSIGEN_XFER_COMPLETE;
  405. writel(mask, &host->reg->norintsigen);
  406. return 0;
  407. }
  408. int tegra2_mmc_getcd(struct mmc *mmc)
  409. {
  410. struct mmc_host *host = (struct mmc_host *)mmc->priv;
  411. debug("tegra2_mmc_getcd called\n");
  412. if (host->cd_gpio >= 0)
  413. return !gpio_get_value(host->cd_gpio);
  414. return 1;
  415. }
  416. int tegra2_mmc_init(int dev_index, int bus_width, int pwr_gpio, int cd_gpio)
  417. {
  418. struct mmc_host *host;
  419. char gpusage[12]; /* "SD/MMCn PWR" or "SD/MMCn CD" */
  420. struct mmc *mmc;
  421. debug(" tegra2_mmc_init: index %d, bus width %d "
  422. "pwr_gpio %d cd_gpio %d\n",
  423. dev_index, bus_width, pwr_gpio, cd_gpio);
  424. host = &mmc_host[dev_index];
  425. host->clock = 0;
  426. host->pwr_gpio = pwr_gpio;
  427. host->cd_gpio = cd_gpio;
  428. tegra2_get_setup(host, dev_index);
  429. clock_start_periph_pll(host->mmc_id, CLOCK_ID_PERIPH, 20000000);
  430. if (host->pwr_gpio >= 0) {
  431. sprintf(gpusage, "SD/MMC%d PWR", dev_index);
  432. gpio_request(host->pwr_gpio, gpusage);
  433. gpio_direction_output(host->pwr_gpio, 1);
  434. }
  435. if (host->cd_gpio >= 0) {
  436. sprintf(gpusage, "SD/MMC%d CD", dev_index);
  437. gpio_request(host->cd_gpio, gpusage);
  438. gpio_direction_input(host->cd_gpio);
  439. }
  440. mmc = &mmc_dev[dev_index];
  441. sprintf(mmc->name, "Tegra2 SD/MMC");
  442. mmc->priv = host;
  443. mmc->send_cmd = mmc_send_cmd;
  444. mmc->set_ios = mmc_set_ios;
  445. mmc->init = mmc_core_init;
  446. mmc->getcd = tegra2_mmc_getcd;
  447. mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
  448. if (bus_width == 8)
  449. mmc->host_caps = MMC_MODE_8BIT;
  450. else
  451. mmc->host_caps = MMC_MODE_4BIT;
  452. mmc->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS | MMC_MODE_HC;
  453. /*
  454. * min freq is for card identification, and is the highest
  455. * low-speed SDIO card frequency (actually 400KHz)
  456. * max freq is highest HS eMMC clock as per the SD/MMC spec
  457. * (actually 52MHz)
  458. * Both of these are the closest equivalents w/216MHz source
  459. * clock and Tegra2 SDMMC divisors.
  460. */
  461. mmc->f_min = 375000;
  462. mmc->f_max = 48000000;
  463. mmc_register(mmc);
  464. return 0;
  465. }