tegra2_mmc.c 12 KB

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