s5p_mmc.c 11 KB

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