s5p_mmc.c 11 KB

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