dw_mmc.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*
  2. * (C) Copyright 2012 SAMSUNG Electronics
  3. * Jaehoon Chung <jh80.chung@samsung.com>
  4. * Rajeshawari Shinde <rajeshwari.s@samsung.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or (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. */
  21. #include <common.h>
  22. #include <malloc.h>
  23. #include <mmc.h>
  24. #include <dwmmc.h>
  25. #include <asm-generic/errno.h>
  26. #define PAGE_SIZE 4096
  27. static int dwmci_wait_reset(struct dwmci_host *host, u32 value)
  28. {
  29. unsigned long timeout = 1000;
  30. u32 ctrl;
  31. dwmci_writel(host, DWMCI_CTRL, value);
  32. while (timeout--) {
  33. ctrl = dwmci_readl(host, DWMCI_CTRL);
  34. if (!(ctrl & DWMCI_RESET_ALL))
  35. return 1;
  36. }
  37. return 0;
  38. }
  39. static void dwmci_set_idma_desc(struct dwmci_idmac *idmac,
  40. u32 desc0, u32 desc1, u32 desc2)
  41. {
  42. struct dwmci_idmac *desc = idmac;
  43. desc->flags = desc0;
  44. desc->cnt = desc1;
  45. desc->addr = desc2;
  46. desc->next_addr = (unsigned int)desc + sizeof(struct dwmci_idmac);
  47. }
  48. static void dwmci_prepare_data(struct dwmci_host *host,
  49. struct mmc_data *data)
  50. {
  51. unsigned long ctrl;
  52. unsigned int i = 0, flags, cnt, blk_cnt;
  53. ulong data_start, data_end, start_addr;
  54. ALLOC_CACHE_ALIGN_BUFFER(struct dwmci_idmac, cur_idmac, data->blocks);
  55. blk_cnt = data->blocks;
  56. dwmci_wait_reset(host, DWMCI_CTRL_FIFO_RESET);
  57. data_start = (ulong)cur_idmac;
  58. dwmci_writel(host, DWMCI_DBADDR, (unsigned int)cur_idmac);
  59. if (data->flags == MMC_DATA_READ)
  60. start_addr = (unsigned int)data->dest;
  61. else
  62. start_addr = (unsigned int)data->src;
  63. do {
  64. flags = DWMCI_IDMAC_OWN | DWMCI_IDMAC_CH ;
  65. flags |= (i == 0) ? DWMCI_IDMAC_FS : 0;
  66. if (blk_cnt <= 8) {
  67. flags |= DWMCI_IDMAC_LD;
  68. cnt = data->blocksize * blk_cnt;
  69. } else
  70. cnt = data->blocksize * 8;
  71. dwmci_set_idma_desc(cur_idmac, flags, cnt,
  72. start_addr + (i * PAGE_SIZE));
  73. if(blk_cnt < 8)
  74. break;
  75. blk_cnt -= 8;
  76. cur_idmac++;
  77. i++;
  78. } while(1);
  79. data_end = (ulong)cur_idmac;
  80. flush_dcache_range(data_start, data_end + ARCH_DMA_MINALIGN);
  81. ctrl = dwmci_readl(host, DWMCI_CTRL);
  82. ctrl |= DWMCI_IDMAC_EN | DWMCI_DMA_EN;
  83. dwmci_writel(host, DWMCI_CTRL, ctrl);
  84. ctrl = dwmci_readl(host, DWMCI_BMOD);
  85. ctrl |= DWMCI_BMOD_IDMAC_FB | DWMCI_BMOD_IDMAC_EN;
  86. dwmci_writel(host, DWMCI_BMOD, ctrl);
  87. dwmci_writel(host, DWMCI_BLKSIZ, data->blocksize);
  88. dwmci_writel(host, DWMCI_BYTCNT, data->blocksize * data->blocks);
  89. }
  90. static int dwmci_set_transfer_mode(struct dwmci_host *host,
  91. struct mmc_data *data)
  92. {
  93. unsigned long mode;
  94. mode = DWMCI_CMD_DATA_EXP;
  95. if (data->flags & MMC_DATA_WRITE)
  96. mode |= DWMCI_CMD_RW;
  97. return mode;
  98. }
  99. static int dwmci_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
  100. struct mmc_data *data)
  101. {
  102. struct dwmci_host *host = (struct dwmci_host *)mmc->priv;
  103. int flags = 0, i;
  104. unsigned int timeout = 100000;
  105. u32 retry = 10000;
  106. u32 mask, ctrl;
  107. ulong start = get_timer(0);
  108. while (dwmci_readl(host, DWMCI_STATUS) & DWMCI_BUSY) {
  109. if (get_timer(start) > timeout) {
  110. printf("Timeout on data busy\n");
  111. return TIMEOUT;
  112. }
  113. }
  114. dwmci_writel(host, DWMCI_RINTSTS, DWMCI_INTMSK_ALL);
  115. if (data)
  116. dwmci_prepare_data(host, data);
  117. dwmci_writel(host, DWMCI_CMDARG, cmd->cmdarg);
  118. if (data)
  119. flags = dwmci_set_transfer_mode(host, data);
  120. if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
  121. return -1;
  122. if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
  123. flags |= DWMCI_CMD_ABORT_STOP;
  124. else
  125. flags |= DWMCI_CMD_PRV_DAT_WAIT;
  126. if (cmd->resp_type & MMC_RSP_PRESENT) {
  127. flags |= DWMCI_CMD_RESP_EXP;
  128. if (cmd->resp_type & MMC_RSP_136)
  129. flags |= DWMCI_CMD_RESP_LENGTH;
  130. }
  131. if (cmd->resp_type & MMC_RSP_CRC)
  132. flags |= DWMCI_CMD_CHECK_CRC;
  133. flags |= (cmd->cmdidx | DWMCI_CMD_START | DWMCI_CMD_USE_HOLD_REG);
  134. debug("Sending CMD%d\n",cmd->cmdidx);
  135. dwmci_writel(host, DWMCI_CMD, flags);
  136. for (i = 0; i < retry; i++) {
  137. mask = dwmci_readl(host, DWMCI_RINTSTS);
  138. if (mask & DWMCI_INTMSK_CDONE) {
  139. if (!data)
  140. dwmci_writel(host, DWMCI_RINTSTS, mask);
  141. break;
  142. }
  143. }
  144. if (i == retry)
  145. return TIMEOUT;
  146. if (mask & DWMCI_INTMSK_RTO) {
  147. debug("Response Timeout..\n");
  148. return TIMEOUT;
  149. } else if (mask & DWMCI_INTMSK_RE) {
  150. debug("Response Error..\n");
  151. return -1;
  152. }
  153. if (cmd->resp_type & MMC_RSP_PRESENT) {
  154. if (cmd->resp_type & MMC_RSP_136) {
  155. cmd->response[0] = dwmci_readl(host, DWMCI_RESP3);
  156. cmd->response[1] = dwmci_readl(host, DWMCI_RESP2);
  157. cmd->response[2] = dwmci_readl(host, DWMCI_RESP1);
  158. cmd->response[3] = dwmci_readl(host, DWMCI_RESP0);
  159. } else {
  160. cmd->response[0] = dwmci_readl(host, DWMCI_RESP0);
  161. }
  162. }
  163. if (data) {
  164. do {
  165. mask = dwmci_readl(host, DWMCI_RINTSTS);
  166. if (mask & (DWMCI_DATA_ERR | DWMCI_DATA_TOUT)) {
  167. debug("DATA ERROR!\n");
  168. return -1;
  169. }
  170. } while (!(mask & DWMCI_INTMSK_DTO));
  171. dwmci_writel(host, DWMCI_RINTSTS, mask);
  172. ctrl = dwmci_readl(host, DWMCI_CTRL);
  173. ctrl &= ~(DWMCI_DMA_EN);
  174. dwmci_writel(host, DWMCI_CTRL, ctrl);
  175. }
  176. udelay(100);
  177. return 0;
  178. }
  179. static int dwmci_setup_bus(struct dwmci_host *host, u32 freq)
  180. {
  181. u32 div, status;
  182. int timeout = 10000;
  183. unsigned long sclk;
  184. if ((freq == host->clock) || (freq == 0))
  185. return 0;
  186. /*
  187. * If host->mmc_clk didn't define,
  188. * then assume that host->bus_hz is source clock value.
  189. * host->bus_hz should be set from user.
  190. */
  191. if (host->mmc_clk)
  192. sclk = host->mmc_clk(host->dev_index);
  193. else if (host->bus_hz)
  194. sclk = host->bus_hz;
  195. else {
  196. printf("Didn't get source clock value..\n");
  197. return -EINVAL;
  198. }
  199. div = DIV_ROUND_UP(sclk, 2 * freq);
  200. dwmci_writel(host, DWMCI_CLKENA, 0);
  201. dwmci_writel(host, DWMCI_CLKSRC, 0);
  202. dwmci_writel(host, DWMCI_CLKDIV, div);
  203. dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_PRV_DAT_WAIT |
  204. DWMCI_CMD_UPD_CLK | DWMCI_CMD_START);
  205. do {
  206. status = dwmci_readl(host, DWMCI_CMD);
  207. if (timeout-- < 0) {
  208. printf("TIMEOUT error!!\n");
  209. return -ETIMEDOUT;
  210. }
  211. } while (status & DWMCI_CMD_START);
  212. dwmci_writel(host, DWMCI_CLKENA, DWMCI_CLKEN_ENABLE |
  213. DWMCI_CLKEN_LOW_PWR);
  214. dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_PRV_DAT_WAIT |
  215. DWMCI_CMD_UPD_CLK | DWMCI_CMD_START);
  216. timeout = 10000;
  217. do {
  218. status = dwmci_readl(host, DWMCI_CMD);
  219. if (timeout-- < 0) {
  220. printf("TIMEOUT error!!\n");
  221. return -ETIMEDOUT;
  222. }
  223. } while (status & DWMCI_CMD_START);
  224. host->clock = freq;
  225. return 0;
  226. }
  227. static void dwmci_set_ios(struct mmc *mmc)
  228. {
  229. struct dwmci_host *host = (struct dwmci_host *)mmc->priv;
  230. u32 ctype;
  231. debug("Buswidth = %d, clock: %d\n",mmc->bus_width, mmc->clock);
  232. dwmci_setup_bus(host, mmc->clock);
  233. switch (mmc->bus_width) {
  234. case 8:
  235. ctype = DWMCI_CTYPE_8BIT;
  236. break;
  237. case 4:
  238. ctype = DWMCI_CTYPE_4BIT;
  239. break;
  240. default:
  241. ctype = DWMCI_CTYPE_1BIT;
  242. break;
  243. }
  244. dwmci_writel(host, DWMCI_CTYPE, ctype);
  245. if (host->clksel)
  246. host->clksel(host);
  247. }
  248. static int dwmci_init(struct mmc *mmc)
  249. {
  250. struct dwmci_host *host = (struct dwmci_host *)mmc->priv;
  251. u32 fifo_size;
  252. dwmci_writel(host, DWMCI_PWREN, 1);
  253. if (!dwmci_wait_reset(host, DWMCI_RESET_ALL)) {
  254. debug("%s[%d] Fail-reset!!\n",__func__,__LINE__);
  255. return -1;
  256. }
  257. /* Enumerate at 400KHz */
  258. dwmci_setup_bus(host, mmc->f_min);
  259. dwmci_writel(host, DWMCI_RINTSTS, 0xFFFFFFFF);
  260. dwmci_writel(host, DWMCI_INTMASK, 0);
  261. dwmci_writel(host, DWMCI_TMOUT, 0xFFFFFFFF);
  262. dwmci_writel(host, DWMCI_IDINTEN, 0);
  263. dwmci_writel(host, DWMCI_BMOD, 1);
  264. if (!host->fifoth_val) {
  265. fifo_size = dwmci_readl(host, DWMCI_FIFOTH);
  266. fifo_size = ((fifo_size & RX_WMARK_MASK) >> RX_WMARK_SHIFT) + 1;
  267. host->fifoth_val = MSIZE(0x2) | RX_WMARK(fifo_size / 2 - 1) |
  268. TX_WMARK(fifo_size / 2);
  269. }
  270. dwmci_writel(host, DWMCI_FIFOTH, host->fifoth_val);
  271. dwmci_writel(host, DWMCI_CLKENA, 0);
  272. dwmci_writel(host, DWMCI_CLKSRC, 0);
  273. return 0;
  274. }
  275. int add_dwmci(struct dwmci_host *host, u32 max_clk, u32 min_clk)
  276. {
  277. struct mmc *mmc;
  278. int err = 0;
  279. mmc = malloc(sizeof(struct mmc));
  280. if (!mmc) {
  281. printf("mmc malloc fail!\n");
  282. return -1;
  283. }
  284. mmc->priv = host;
  285. host->mmc = mmc;
  286. sprintf(mmc->name, "%s", host->name);
  287. mmc->send_cmd = dwmci_send_cmd;
  288. mmc->set_ios = dwmci_set_ios;
  289. mmc->init = dwmci_init;
  290. mmc->f_min = min_clk;
  291. mmc->f_max = max_clk;
  292. mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
  293. mmc->host_caps = host->caps;
  294. if (host->buswidth == 8) {
  295. mmc->host_caps |= MMC_MODE_8BIT;
  296. mmc->host_caps &= ~MMC_MODE_4BIT;
  297. } else {
  298. mmc->host_caps |= MMC_MODE_4BIT;
  299. mmc->host_caps &= ~MMC_MODE_8BIT;
  300. }
  301. mmc->host_caps |= MMC_MODE_HS | MMC_MODE_HS_52MHz | MMC_MODE_HC;
  302. err = mmc_register(mmc);
  303. return err;
  304. }