s5p_mmc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /*
  2. * (C) Copyright 2009 SAMSUNG Electronics
  3. * Minkyu Kang <mk7.kang@samsung.com>
  4. * Jaehoon Chung <jh80.chung@samsung.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <common.h>
  21. #include <mmc.h>
  22. #include <asm/io.h>
  23. #include <asm/arch/mmc.h>
  24. /* support 4 mmc hosts */
  25. struct mmc mmc_dev[4];
  26. struct mmc_host mmc_host[4];
  27. static inline struct s5p_mmc *s5p_get_base_mmc(int dev_index)
  28. {
  29. unsigned long offset = dev_index * sizeof(struct s5p_mmc);
  30. if (cpu_is_s5pc100())
  31. return (struct s5p_mmc *)(S5PC100_MMC_BASE + offset);
  32. else
  33. return (struct s5p_mmc *)(S5PC110_MMC_BASE + offset);
  34. }
  35. static void mmc_prepare_data(struct mmc_host *host, struct mmc_data *data)
  36. {
  37. unsigned char ctrl;
  38. debug("data->dest: %08x\n", (u32)data->dest);
  39. writel((u32)data->dest, &host->reg->sysad);
  40. /*
  41. * DMASEL[4:3]
  42. * 00 = Selects SDMA
  43. * 01 = Reserved
  44. * 10 = Selects 32-bit Address ADMA2
  45. * 11 = Selects 64-bit Address ADMA2
  46. */
  47. ctrl = readb(&host->reg->hostctl);
  48. ctrl &= ~(3 << 3);
  49. writeb(ctrl, &host->reg->hostctl);
  50. /* We do not handle DMA boundaries, so set it to max (512 KiB) */
  51. writew((7 << 12) | (512 << 0), &host->reg->blksize);
  52. writew(data->blocks, &host->reg->blkcnt);
  53. }
  54. static void mmc_set_transfer_mode(struct mmc_host *host, struct mmc_data *data)
  55. {
  56. unsigned short mode;
  57. /*
  58. * TRNMOD
  59. * MUL1SIN0[5] : Multi/Single Block Select
  60. * RD1WT0[4] : Data Transfer Direction Select
  61. * 1 = read
  62. * 0 = write
  63. * ENACMD12[2] : Auto CMD12 Enable
  64. * ENBLKCNT[1] : Block Count Enable
  65. * ENDMA[0] : DMA Enable
  66. */
  67. mode = (1 << 1) | (1 << 0);
  68. if (data->blocks > 1)
  69. mode |= (1 << 5);
  70. if (data->flags & MMC_DATA_READ)
  71. mode |= (1 << 4);
  72. writew(mode, &host->reg->trnmod);
  73. }
  74. static int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
  75. struct mmc_data *data)
  76. {
  77. struct mmc_host *host = (struct mmc_host *)mmc->priv;
  78. int flags, i;
  79. unsigned int timeout;
  80. unsigned int mask;
  81. unsigned int retry = 0x100000;
  82. /* Wait max 10 ms */
  83. timeout = 10;
  84. /*
  85. * PRNSTS
  86. * CMDINHDAT[1] : Command Inhibit (DAT)
  87. * CMDINHCMD[0] : Command Inhibit (CMD)
  88. */
  89. mask = (1 << 0);
  90. if ((data != NULL) || (cmd->resp_type & MMC_RSP_BUSY))
  91. mask |= (1 << 1);
  92. /*
  93. * We shouldn't wait for data inihibit for stop commands, even
  94. * though they might use busy signaling
  95. */
  96. if (data)
  97. mask &= ~(1 << 1);
  98. while (readl(&host->reg->prnsts) & mask) {
  99. if (timeout == 0) {
  100. printf("%s: timeout error\n", __func__);
  101. return -1;
  102. }
  103. timeout--;
  104. udelay(1000);
  105. }
  106. if (data)
  107. mmc_prepare_data(host, data);
  108. debug("cmd->arg: %08x\n", cmd->cmdarg);
  109. writel(cmd->cmdarg, &host->reg->argument);
  110. if (data)
  111. mmc_set_transfer_mode(host, data);
  112. if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
  113. return -1;
  114. /*
  115. * CMDREG
  116. * CMDIDX[13:8] : Command index
  117. * DATAPRNT[5] : Data Present Select
  118. * ENCMDIDX[4] : Command Index Check Enable
  119. * ENCMDCRC[3] : Command CRC Check Enable
  120. * RSPTYP[1:0]
  121. * 00 = No Response
  122. * 01 = Length 136
  123. * 10 = Length 48
  124. * 11 = Length 48 Check busy after response
  125. */
  126. if (!(cmd->resp_type & MMC_RSP_PRESENT))
  127. flags = 0;
  128. else if (cmd->resp_type & MMC_RSP_136)
  129. flags = (1 << 0);
  130. else if (cmd->resp_type & MMC_RSP_BUSY)
  131. flags = (3 << 0);
  132. else
  133. flags = (2 << 0);
  134. if (cmd->resp_type & MMC_RSP_CRC)
  135. flags |= (1 << 3);
  136. if (cmd->resp_type & MMC_RSP_OPCODE)
  137. flags |= (1 << 4);
  138. if (data)
  139. flags |= (1 << 5);
  140. debug("cmd: %d\n", cmd->cmdidx);
  141. writew((cmd->cmdidx << 8) | flags, &host->reg->cmdreg);
  142. for (i = 0; i < retry; i++) {
  143. mask = readl(&host->reg->norintsts);
  144. /* Command Complete */
  145. if (mask & (1 << 0)) {
  146. if (!data)
  147. writel(mask, &host->reg->norintsts);
  148. break;
  149. }
  150. }
  151. if (i == retry) {
  152. printf("%s: waiting for status update\n", __func__);
  153. return TIMEOUT;
  154. }
  155. if (mask & (1 << 16)) {
  156. /* Timeout Error */
  157. debug("timeout: %08x cmd %d\n", mask, cmd->cmdidx);
  158. return TIMEOUT;
  159. } else if (mask & (1 << 15)) {
  160. /* Error Interrupt */
  161. debug("error: %08x cmd %d\n", mask, cmd->cmdidx);
  162. return -1;
  163. }
  164. if (cmd->resp_type & MMC_RSP_PRESENT) {
  165. if (cmd->resp_type & MMC_RSP_136) {
  166. /* CRC is stripped so we need to do some shifting. */
  167. for (i = 0; i < 4; i++) {
  168. unsigned int offset =
  169. (unsigned int)(&host->reg->rspreg3 - i);
  170. cmd->response[i] = readl(offset) << 8;
  171. if (i != 3) {
  172. cmd->response[i] |=
  173. readb(offset - 1);
  174. }
  175. debug("cmd->resp[%d]: %08x\n",
  176. i, cmd->response[i]);
  177. }
  178. } else if (cmd->resp_type & MMC_RSP_BUSY) {
  179. for (i = 0; i < retry; i++) {
  180. /* PRNTDATA[23:20] : DAT[3:0] Line Signal */
  181. if (readl(&host->reg->prnsts)
  182. & (1 << 20)) /* DAT[0] */
  183. break;
  184. }
  185. if (i == retry) {
  186. printf("%s: card is still busy\n", __func__);
  187. return TIMEOUT;
  188. }
  189. cmd->response[0] = readl(&host->reg->rspreg0);
  190. debug("cmd->resp[0]: %08x\n", cmd->response[0]);
  191. } else {
  192. cmd->response[0] = readl(&host->reg->rspreg0);
  193. debug("cmd->resp[0]: %08x\n", cmd->response[0]);
  194. }
  195. }
  196. if (data) {
  197. while (1) {
  198. mask = readl(&host->reg->norintsts);
  199. if (mask & (1 << 15)) {
  200. /* Error Interrupt */
  201. writel(mask, &host->reg->norintsts);
  202. printf("%s: error during transfer: 0x%08x\n",
  203. __func__, mask);
  204. return -1;
  205. } else if (mask & (1 << 3)) {
  206. /* DMA Interrupt */
  207. debug("DMA end\n");
  208. break;
  209. } else if (mask & (1 << 1)) {
  210. /* Transfer Complete */
  211. debug("r/w is done\n");
  212. break;
  213. }
  214. }
  215. writel(mask, &host->reg->norintsts);
  216. }
  217. udelay(1000);
  218. return 0;
  219. }
  220. static void mmc_change_clock(struct mmc_host *host, uint clock)
  221. {
  222. int div;
  223. unsigned short clk;
  224. unsigned long timeout;
  225. unsigned long ctrl2;
  226. /*
  227. * SELBASECLK[5:4]
  228. * 00/01 = HCLK
  229. * 10 = EPLL
  230. * 11 = XTI or XEXTCLK
  231. */
  232. ctrl2 = readl(&host->reg->control2);
  233. ctrl2 &= ~(3 << 4);
  234. ctrl2 |= (2 << 4);
  235. writel(ctrl2, &host->reg->control2);
  236. writew(0, &host->reg->clkcon);
  237. /* XXX: we assume that clock is between 40MHz and 50MHz */
  238. if (clock == 0)
  239. goto out;
  240. else if (clock <= 400000)
  241. div = 0x100;
  242. else if (clock <= 20000000)
  243. div = 4;
  244. else if (clock <= 26000000)
  245. div = 2;
  246. else
  247. div = 1;
  248. debug("div: %d\n", div);
  249. div >>= 1;
  250. /*
  251. * CLKCON
  252. * SELFREQ[15:8] : base clock divied by value
  253. * ENSDCLK[2] : SD Clock Enable
  254. * STBLINTCLK[1] : Internal Clock Stable
  255. * ENINTCLK[0] : Internal Clock Enable
  256. */
  257. clk = (div << 8) | (1 << 0);
  258. writew(clk, &host->reg->clkcon);
  259. /* Wait max 10 ms */
  260. timeout = 10;
  261. while (!(readw(&host->reg->clkcon) & (1 << 1))) {
  262. if (timeout == 0) {
  263. printf("%s: timeout error\n", __func__);
  264. return;
  265. }
  266. timeout--;
  267. udelay(1000);
  268. }
  269. clk |= (1 << 2);
  270. writew(clk, &host->reg->clkcon);
  271. out:
  272. host->clock = clock;
  273. }
  274. static void mmc_set_ios(struct mmc *mmc)
  275. {
  276. struct mmc_host *host = mmc->priv;
  277. unsigned char ctrl;
  278. unsigned long val;
  279. debug("bus_width: %x, clock: %d\n", mmc->bus_width, mmc->clock);
  280. /*
  281. * SELCLKPADDS[17:16]
  282. * 00 = 2mA
  283. * 01 = 4mA
  284. * 10 = 7mA
  285. * 11 = 9mA
  286. */
  287. writel(0x3 << 16, &host->reg->control4);
  288. val = readl(&host->reg->control2);
  289. val &= (0x3 << 4);
  290. val |= (1 << 31) | /* write status clear async mode enable */
  291. (1 << 30) | /* command conflict mask enable */
  292. (1 << 14) | /* Feedback Clock Enable for Rx Clock */
  293. (1 << 8); /* SDCLK hold enable */
  294. writel(val, &host->reg->control2);
  295. /*
  296. * FCSEL1[15] FCSEL0[7]
  297. * FCSel[1:0] : Rx Feedback Clock Delay Control
  298. * Inverter delay means10ns delay if SDCLK 50MHz setting
  299. * 01 = Delay1 (basic delay)
  300. * 11 = Delay2 (basic delay + 2ns)
  301. * 00 = Delay3 (inverter delay)
  302. * 10 = Delay4 (inverter delay + 2ns)
  303. */
  304. writel(0x8080, &host->reg->control3);
  305. mmc_change_clock(host, mmc->clock);
  306. ctrl = readb(&host->reg->hostctl);
  307. /*
  308. * WIDE4[1]
  309. * 1 = 4-bit mode
  310. * 0 = 1-bit mode
  311. */
  312. if (mmc->bus_width == 4)
  313. ctrl |= (1 << 1);
  314. else
  315. ctrl &= ~(1 << 1);
  316. /*
  317. * OUTEDGEINV[2]
  318. * 1 = Riging edge output
  319. * 0 = Falling edge output
  320. */
  321. ctrl &= ~(1 << 2);
  322. writeb(ctrl, &host->reg->hostctl);
  323. }
  324. static void mmc_reset(struct mmc_host *host)
  325. {
  326. unsigned int timeout;
  327. /*
  328. * RSTALL[0] : Software reset for all
  329. * 1 = reset
  330. * 0 = work
  331. */
  332. writeb((1 << 0), &host->reg->swrst);
  333. host->clock = 0;
  334. /* Wait max 100 ms */
  335. timeout = 100;
  336. /* hw clears the bit when it's done */
  337. while (readb(&host->reg->swrst) & (1 << 0)) {
  338. if (timeout == 0) {
  339. printf("%s: timeout error\n", __func__);
  340. return;
  341. }
  342. timeout--;
  343. udelay(1000);
  344. }
  345. }
  346. static int mmc_core_init(struct mmc *mmc)
  347. {
  348. struct mmc_host *host = (struct mmc_host *)mmc->priv;
  349. unsigned int mask;
  350. mmc_reset(host);
  351. host->version = readw(&host->reg->hcver);
  352. /* mask all */
  353. writel(0xffffffff, &host->reg->norintstsen);
  354. writel(0xffffffff, &host->reg->norintsigen);
  355. writeb(0xe, &host->reg->timeoutcon); /* TMCLK * 2^27 */
  356. /*
  357. * NORMAL Interrupt Status Enable Register init
  358. * [5] ENSTABUFRDRDY : Buffer Read Ready Status Enable
  359. * [4] ENSTABUFWTRDY : Buffer write Ready Status Enable
  360. * [1] ENSTASTANSCMPLT : Transfre Complete Status Enable
  361. * [0] ENSTACMDCMPLT : Command Complete Status Enable
  362. */
  363. mask = readl(&host->reg->norintstsen);
  364. mask &= ~(0xffff);
  365. mask |= (1 << 5) | (1 << 4) | (1 << 1) | (1 << 0);
  366. writel(mask, &host->reg->norintstsen);
  367. /*
  368. * NORMAL Interrupt Signal Enable Register init
  369. * [1] ENSTACMDCMPLT : Transfer Complete Signal Enable
  370. */
  371. mask = readl(&host->reg->norintsigen);
  372. mask &= ~(0xffff);
  373. mask |= (1 << 1);
  374. writel(mask, &host->reg->norintsigen);
  375. return 0;
  376. }
  377. static int s5p_mmc_initialize(int dev_index)
  378. {
  379. struct mmc *mmc;
  380. mmc = &mmc_dev[dev_index];
  381. sprintf(mmc->name, "SAMSUNG SD/MMC");
  382. mmc->priv = &mmc_host[dev_index];
  383. mmc->send_cmd = mmc_send_cmd;
  384. mmc->set_ios = mmc_set_ios;
  385. mmc->init = mmc_core_init;
  386. mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
  387. mmc->host_caps = MMC_MODE_4BIT | MMC_MODE_HS_52MHz | MMC_MODE_HS;
  388. mmc->f_min = 400000;
  389. mmc->f_max = 52000000;
  390. mmc_host[dev_index].clock = 0;
  391. mmc_host[dev_index].reg = s5p_get_base_mmc(dev_index);
  392. mmc_register(mmc);
  393. return 0;
  394. }
  395. int s5p_mmc_init(int dev_index)
  396. {
  397. return s5p_mmc_initialize(dev_index);
  398. }