tegra2_mmc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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 &= ~(3 << 3); /* SDMA */
  76. writeb(ctrl, &host->reg->hostctl);
  77. /* We do not handle DMA boundaries, so set it to max (512 KiB) */
  78. writew((7 << 12) | (data->blocksize & 0xFFF), &host->reg->blksize);
  79. writew(data->blocks, &host->reg->blkcnt);
  80. }
  81. static void mmc_set_transfer_mode(struct mmc_host *host, struct mmc_data *data)
  82. {
  83. unsigned short mode;
  84. debug(" mmc_set_transfer_mode called\n");
  85. /*
  86. * TRNMOD
  87. * MUL1SIN0[5] : Multi/Single Block Select
  88. * RD1WT0[4] : Data Transfer Direction Select
  89. * 1 = read
  90. * 0 = write
  91. * ENACMD12[2] : Auto CMD12 Enable
  92. * ENBLKCNT[1] : Block Count Enable
  93. * ENDMA[0] : DMA Enable
  94. */
  95. mode = (1 << 1) | (1 << 0);
  96. if (data->blocks > 1)
  97. mode |= (1 << 5);
  98. if (data->flags & MMC_DATA_READ)
  99. mode |= (1 << 4);
  100. writew(mode, &host->reg->trnmod);
  101. }
  102. static int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
  103. struct mmc_data *data)
  104. {
  105. struct mmc_host *host = (struct mmc_host *)mmc->priv;
  106. int flags, i;
  107. unsigned int timeout;
  108. unsigned int mask;
  109. unsigned int retry = 0x100000;
  110. debug(" mmc_send_cmd called\n");
  111. /* Wait max 10 ms */
  112. timeout = 10;
  113. /*
  114. * PRNSTS
  115. * CMDINHDAT[1] : Command Inhibit (DAT)
  116. * CMDINHCMD[0] : Command Inhibit (CMD)
  117. */
  118. mask = (1 << 0);
  119. if ((data != NULL) || (cmd->resp_type & MMC_RSP_BUSY))
  120. mask |= (1 << 1);
  121. /*
  122. * We shouldn't wait for data inhibit for stop commands, even
  123. * though they might use busy signaling
  124. */
  125. if (data)
  126. mask &= ~(1 << 1);
  127. while (readl(&host->reg->prnsts) & mask) {
  128. if (timeout == 0) {
  129. printf("%s: timeout error\n", __func__);
  130. return -1;
  131. }
  132. timeout--;
  133. udelay(1000);
  134. }
  135. if (data)
  136. mmc_prepare_data(host, data);
  137. debug("cmd->arg: %08x\n", cmd->cmdarg);
  138. writel(cmd->cmdarg, &host->reg->argument);
  139. if (data)
  140. mmc_set_transfer_mode(host, data);
  141. if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
  142. return -1;
  143. /*
  144. * CMDREG
  145. * CMDIDX[13:8] : Command index
  146. * DATAPRNT[5] : Data Present Select
  147. * ENCMDIDX[4] : Command Index Check Enable
  148. * ENCMDCRC[3] : Command CRC Check Enable
  149. * RSPTYP[1:0]
  150. * 00 = No Response
  151. * 01 = Length 136
  152. * 10 = Length 48
  153. * 11 = Length 48 Check busy after response
  154. */
  155. if (!(cmd->resp_type & MMC_RSP_PRESENT))
  156. flags = 0;
  157. else if (cmd->resp_type & MMC_RSP_136)
  158. flags = (1 << 0);
  159. else if (cmd->resp_type & MMC_RSP_BUSY)
  160. flags = (3 << 0);
  161. else
  162. flags = (2 << 0);
  163. if (cmd->resp_type & MMC_RSP_CRC)
  164. flags |= (1 << 3);
  165. if (cmd->resp_type & MMC_RSP_OPCODE)
  166. flags |= (1 << 4);
  167. if (data)
  168. flags |= (1 << 5);
  169. debug("cmd: %d\n", cmd->cmdidx);
  170. writew((cmd->cmdidx << 8) | flags, &host->reg->cmdreg);
  171. for (i = 0; i < retry; i++) {
  172. mask = readl(&host->reg->norintsts);
  173. /* Command Complete */
  174. if (mask & (1 << 0)) {
  175. if (!data)
  176. writel(mask, &host->reg->norintsts);
  177. break;
  178. }
  179. }
  180. if (i == retry) {
  181. printf("%s: waiting for status update\n", __func__);
  182. return TIMEOUT;
  183. }
  184. if (mask & (1 << 16)) {
  185. /* Timeout Error */
  186. debug("timeout: %08x cmd %d\n", mask, cmd->cmdidx);
  187. return TIMEOUT;
  188. } else if (mask & (1 << 15)) {
  189. /* Error Interrupt */
  190. debug("error: %08x cmd %d\n", mask, cmd->cmdidx);
  191. return -1;
  192. }
  193. if (cmd->resp_type & MMC_RSP_PRESENT) {
  194. if (cmd->resp_type & MMC_RSP_136) {
  195. /* CRC is stripped so we need to do some shifting. */
  196. for (i = 0; i < 4; i++) {
  197. unsigned int offset =
  198. (unsigned int)(&host->reg->rspreg3 - i);
  199. cmd->response[i] = readl(offset) << 8;
  200. if (i != 3) {
  201. cmd->response[i] |=
  202. readb(offset - 1);
  203. }
  204. debug("cmd->resp[%d]: %08x\n",
  205. i, cmd->response[i]);
  206. }
  207. } else if (cmd->resp_type & MMC_RSP_BUSY) {
  208. for (i = 0; i < retry; i++) {
  209. /* PRNTDATA[23:20] : DAT[3:0] Line Signal */
  210. if (readl(&host->reg->prnsts)
  211. & (1 << 20)) /* DAT[0] */
  212. break;
  213. }
  214. if (i == retry) {
  215. printf("%s: card is still busy\n", __func__);
  216. return TIMEOUT;
  217. }
  218. cmd->response[0] = readl(&host->reg->rspreg0);
  219. debug("cmd->resp[0]: %08x\n", cmd->response[0]);
  220. } else {
  221. cmd->response[0] = readl(&host->reg->rspreg0);
  222. debug("cmd->resp[0]: %08x\n", cmd->response[0]);
  223. }
  224. }
  225. if (data) {
  226. while (1) {
  227. mask = readl(&host->reg->norintsts);
  228. if (mask & (1 << 15)) {
  229. /* Error Interrupt */
  230. writel(mask, &host->reg->norintsts);
  231. printf("%s: error during transfer: 0x%08x\n",
  232. __func__, mask);
  233. return -1;
  234. } else if (mask & (1 << 3)) {
  235. /* DMA Interrupt */
  236. debug("DMA end\n");
  237. break;
  238. } else if (mask & (1 << 1)) {
  239. /* Transfer Complete */
  240. debug("r/w is done\n");
  241. break;
  242. }
  243. }
  244. writel(mask, &host->reg->norintsts);
  245. }
  246. udelay(1000);
  247. return 0;
  248. }
  249. static void mmc_change_clock(struct mmc_host *host, uint clock)
  250. {
  251. int div;
  252. unsigned short clk;
  253. unsigned long timeout;
  254. debug(" mmc_change_clock called\n");
  255. /*
  256. * Change Tegra2 SDMMCx clock divisor here. Source is 216MHz,
  257. * PLLP_OUT0
  258. */
  259. if (clock == 0)
  260. goto out;
  261. clock_adjust_periph_pll_div(host->mmc_id, CLOCK_ID_PERIPH, clock,
  262. &div);
  263. debug("div = %d\n", div);
  264. writew(0, &host->reg->clkcon);
  265. /*
  266. * CLKCON
  267. * SELFREQ[15:8] : base clock divided by value
  268. * ENSDCLK[2] : SD Clock Enable
  269. * STBLINTCLK[1] : Internal Clock Stable
  270. * ENINTCLK[0] : Internal Clock Enable
  271. */
  272. div >>= 1;
  273. clk = (div << 8) | (1 << 0);
  274. writew(clk, &host->reg->clkcon);
  275. /* Wait max 10 ms */
  276. timeout = 10;
  277. while (!(readw(&host->reg->clkcon) & (1 << 1))) {
  278. if (timeout == 0) {
  279. printf("%s: timeout error\n", __func__);
  280. return;
  281. }
  282. timeout--;
  283. udelay(1000);
  284. }
  285. clk |= (1 << 2);
  286. writew(clk, &host->reg->clkcon);
  287. debug("mmc_change_clock: clkcon = %08X\n", clk);
  288. out:
  289. host->clock = clock;
  290. }
  291. static void mmc_set_ios(struct mmc *mmc)
  292. {
  293. struct mmc_host *host = mmc->priv;
  294. unsigned char ctrl;
  295. debug(" mmc_set_ios called\n");
  296. debug("bus_width: %x, clock: %d\n", mmc->bus_width, mmc->clock);
  297. /* Change clock first */
  298. mmc_change_clock(host, mmc->clock);
  299. ctrl = readb(&host->reg->hostctl);
  300. /*
  301. * WIDE8[5]
  302. * 0 = Depend on WIDE4
  303. * 1 = 8-bit mode
  304. * WIDE4[1]
  305. * 1 = 4-bit mode
  306. * 0 = 1-bit mode
  307. */
  308. if (mmc->bus_width == 8)
  309. ctrl |= (1 << 5);
  310. else if (mmc->bus_width == 4)
  311. ctrl |= (1 << 1);
  312. else
  313. ctrl &= ~(1 << 1);
  314. writeb(ctrl, &host->reg->hostctl);
  315. debug("mmc_set_ios: hostctl = %08X\n", ctrl);
  316. }
  317. static void mmc_reset(struct mmc_host *host)
  318. {
  319. unsigned int timeout;
  320. debug(" mmc_reset called\n");
  321. /*
  322. * RSTALL[0] : Software reset for all
  323. * 1 = reset
  324. * 0 = work
  325. */
  326. writeb((1 << 0), &host->reg->swrst);
  327. host->clock = 0;
  328. /* Wait max 100 ms */
  329. timeout = 100;
  330. /* hw clears the bit when it's done */
  331. while (readb(&host->reg->swrst) & (1 << 0)) {
  332. if (timeout == 0) {
  333. printf("%s: timeout error\n", __func__);
  334. return;
  335. }
  336. timeout--;
  337. udelay(1000);
  338. }
  339. }
  340. static int mmc_core_init(struct mmc *mmc)
  341. {
  342. struct mmc_host *host = (struct mmc_host *)mmc->priv;
  343. unsigned int mask;
  344. debug(" mmc_core_init called\n");
  345. mmc_reset(host);
  346. host->version = readw(&host->reg->hcver);
  347. debug("host version = %x\n", host->version);
  348. /* mask all */
  349. writel(0xffffffff, &host->reg->norintstsen);
  350. writel(0xffffffff, &host->reg->norintsigen);
  351. writeb(0xe, &host->reg->timeoutcon); /* TMCLK * 2^27 */
  352. /*
  353. * NORMAL Interrupt Status Enable Register init
  354. * [5] ENSTABUFRDRDY : Buffer Read Ready Status Enable
  355. * [4] ENSTABUFWTRDY : Buffer write Ready Status Enable
  356. * [1] ENSTASTANSCMPLT : Transfre Complete Status Enable
  357. * [0] ENSTACMDCMPLT : Command Complete Status Enable
  358. */
  359. mask = readl(&host->reg->norintstsen);
  360. mask &= ~(0xffff);
  361. mask |= (1 << 5) | (1 << 4) | (1 << 1) | (1 << 0);
  362. writel(mask, &host->reg->norintstsen);
  363. /*
  364. * NORMAL Interrupt Signal Enable Register init
  365. * [1] ENSTACMDCMPLT : Transfer Complete Signal Enable
  366. */
  367. mask = readl(&host->reg->norintsigen);
  368. mask &= ~(0xffff);
  369. mask |= (1 << 1);
  370. writel(mask, &host->reg->norintsigen);
  371. return 0;
  372. }
  373. static int tegra2_mmc_initialize(int dev_index, int bus_width)
  374. {
  375. struct mmc *mmc;
  376. debug(" mmc_initialize called\n");
  377. mmc = &mmc_dev[dev_index];
  378. sprintf(mmc->name, "Tegra2 SD/MMC");
  379. mmc->priv = &mmc_host[dev_index];
  380. mmc->send_cmd = mmc_send_cmd;
  381. mmc->set_ios = mmc_set_ios;
  382. mmc->init = mmc_core_init;
  383. mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
  384. if (bus_width == 8)
  385. mmc->host_caps = MMC_MODE_8BIT;
  386. else
  387. mmc->host_caps = MMC_MODE_4BIT;
  388. mmc->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS | MMC_MODE_HC;
  389. /*
  390. * min freq is for card identification, and is the highest
  391. * low-speed SDIO card frequency (actually 400KHz)
  392. * max freq is highest HS eMMC clock as per the SD/MMC spec
  393. * (actually 52MHz)
  394. * Both of these are the closest equivalents w/216MHz source
  395. * clock and Tegra2 SDMMC divisors.
  396. */
  397. mmc->f_min = 375000;
  398. mmc->f_max = 48000000;
  399. mmc_host[dev_index].clock = 0;
  400. tegra2_get_setup(&mmc_host[dev_index], dev_index);
  401. mmc_register(mmc);
  402. return 0;
  403. }
  404. int tegra2_mmc_init(int dev_index, int bus_width)
  405. {
  406. debug(" tegra2_mmc_init: index %d, bus width %d\n",
  407. dev_index, bus_width);
  408. return tegra2_mmc_initialize(dev_index, bus_width);
  409. }