sdhci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /*
  2. * Copyright 2011, Marvell Semiconductor Inc.
  3. * Lei Wen <leiwen@marvell.com>
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. *
  23. * Back ported to the 8xx platform (from the 8260 platform) by
  24. * Murray.Jensen@cmst.csiro.au, 27-Jan-01.
  25. */
  26. #include <common.h>
  27. #include <malloc.h>
  28. #include <mmc.h>
  29. #include <sdhci.h>
  30. void *aligned_buffer;
  31. static void sdhci_reset(struct sdhci_host *host, u8 mask)
  32. {
  33. unsigned long timeout;
  34. /* Wait max 100 ms */
  35. timeout = 100;
  36. sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
  37. while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
  38. if (timeout == 0) {
  39. printf("Reset 0x%x never completed.\n", (int)mask);
  40. return;
  41. }
  42. timeout--;
  43. udelay(1000);
  44. }
  45. }
  46. static void sdhci_cmd_done(struct sdhci_host *host, struct mmc_cmd *cmd)
  47. {
  48. int i;
  49. if (cmd->resp_type & MMC_RSP_136) {
  50. /* CRC is stripped so we need to do some shifting. */
  51. for (i = 0; i < 4; i++) {
  52. cmd->response[i] = sdhci_readl(host,
  53. SDHCI_RESPONSE + (3-i)*4) << 8;
  54. if (i != 3)
  55. cmd->response[i] |= sdhci_readb(host,
  56. SDHCI_RESPONSE + (3-i)*4-1);
  57. }
  58. } else {
  59. cmd->response[0] = sdhci_readl(host, SDHCI_RESPONSE);
  60. }
  61. }
  62. static void sdhci_transfer_pio(struct sdhci_host *host, struct mmc_data *data)
  63. {
  64. int i;
  65. char *offs;
  66. for (i = 0; i < data->blocksize; i += 4) {
  67. offs = data->dest + i;
  68. if (data->flags == MMC_DATA_READ)
  69. *(u32 *)offs = sdhci_readl(host, SDHCI_BUFFER);
  70. else
  71. sdhci_writel(host, *(u32 *)offs, SDHCI_BUFFER);
  72. }
  73. }
  74. static int sdhci_transfer_data(struct sdhci_host *host, struct mmc_data *data,
  75. unsigned int start_addr)
  76. {
  77. unsigned int stat, rdy, mask, block = 0;
  78. rdy = SDHCI_INT_SPACE_AVAIL | SDHCI_INT_DATA_AVAIL;
  79. mask = SDHCI_DATA_AVAILABLE | SDHCI_SPACE_AVAILABLE;
  80. do {
  81. stat = sdhci_readl(host, SDHCI_INT_STATUS);
  82. if (stat & SDHCI_INT_ERROR) {
  83. printf("Error detected in status(0x%X)!\n", stat);
  84. return -1;
  85. }
  86. if (stat & rdy) {
  87. if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) & mask))
  88. continue;
  89. sdhci_writel(host, rdy, SDHCI_INT_STATUS);
  90. sdhci_transfer_pio(host, data);
  91. data->dest += data->blocksize;
  92. if (++block >= data->blocks)
  93. break;
  94. }
  95. #ifdef CONFIG_MMC_SDMA
  96. if (stat & SDHCI_INT_DMA_END) {
  97. sdhci_writel(host, SDHCI_INT_DMA_END, SDHCI_INT_STATUS);
  98. start_addr &= SDHCI_DEFAULT_BOUNDARY_SIZE - 1;
  99. start_addr += SDHCI_DEFAULT_BOUNDARY_SIZE;
  100. sdhci_writel(host, start_addr, SDHCI_DMA_ADDRESS);
  101. }
  102. #endif
  103. } while (!(stat & SDHCI_INT_DATA_END));
  104. return 0;
  105. }
  106. int sdhci_send_command(struct mmc *mmc, struct mmc_cmd *cmd,
  107. struct mmc_data *data)
  108. {
  109. struct sdhci_host *host = (struct sdhci_host *)mmc->priv;
  110. unsigned int stat = 0;
  111. int ret = 0;
  112. int trans_bytes = 0, is_aligned = 1;
  113. u32 mask, flags, mode;
  114. unsigned int timeout, start_addr = 0;
  115. /* Wait max 10 ms */
  116. timeout = 10;
  117. sdhci_writel(host, SDHCI_INT_ALL_MASK, SDHCI_INT_STATUS);
  118. mask = SDHCI_CMD_INHIBIT | SDHCI_DATA_INHIBIT;
  119. /* We shouldn't wait for data inihibit for stop commands, even
  120. though they might use busy signaling */
  121. if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
  122. mask &= ~SDHCI_DATA_INHIBIT;
  123. while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
  124. if (timeout == 0) {
  125. printf("Controller never released inhibit bit(s).\n");
  126. return COMM_ERR;
  127. }
  128. timeout--;
  129. udelay(1000);
  130. }
  131. mask = SDHCI_INT_RESPONSE;
  132. if (!(cmd->resp_type & MMC_RSP_PRESENT))
  133. flags = SDHCI_CMD_RESP_NONE;
  134. else if (cmd->resp_type & MMC_RSP_136)
  135. flags = SDHCI_CMD_RESP_LONG;
  136. else if (cmd->resp_type & MMC_RSP_BUSY) {
  137. flags = SDHCI_CMD_RESP_SHORT_BUSY;
  138. mask |= SDHCI_INT_DATA_END;
  139. } else
  140. flags = SDHCI_CMD_RESP_SHORT;
  141. if (cmd->resp_type & MMC_RSP_CRC)
  142. flags |= SDHCI_CMD_CRC;
  143. if (cmd->resp_type & MMC_RSP_OPCODE)
  144. flags |= SDHCI_CMD_INDEX;
  145. if (data)
  146. flags |= SDHCI_CMD_DATA;
  147. /*Set Transfer mode regarding to data flag*/
  148. if (data != 0) {
  149. sdhci_writeb(host, 0xe, SDHCI_TIMEOUT_CONTROL);
  150. mode = SDHCI_TRNS_BLK_CNT_EN;
  151. trans_bytes = data->blocks * data->blocksize;
  152. if (data->blocks > 1)
  153. mode |= SDHCI_TRNS_MULTI;
  154. if (data->flags == MMC_DATA_READ)
  155. mode |= SDHCI_TRNS_READ;
  156. #ifdef CONFIG_MMC_SDMA
  157. if (data->flags == MMC_DATA_READ)
  158. start_addr = (unsigned int)data->dest;
  159. else
  160. start_addr = (unsigned int)data->src;
  161. if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) &&
  162. (start_addr & 0x7) != 0x0) {
  163. is_aligned = 0;
  164. start_addr = (unsigned int)aligned_buffer;
  165. if (data->flags != MMC_DATA_READ)
  166. memcpy(aligned_buffer, data->src, trans_bytes);
  167. }
  168. sdhci_writel(host, start_addr, SDHCI_DMA_ADDRESS);
  169. mode |= SDHCI_TRNS_DMA;
  170. #endif
  171. sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
  172. data->blocksize),
  173. SDHCI_BLOCK_SIZE);
  174. sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
  175. sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
  176. }
  177. sdhci_writel(host, cmd->cmdarg, SDHCI_ARGUMENT);
  178. #ifdef CONFIG_MMC_SDMA
  179. flush_cache(0, ~0);
  180. #endif
  181. sdhci_writew(host, SDHCI_MAKE_CMD(cmd->cmdidx, flags), SDHCI_COMMAND);
  182. do {
  183. stat = sdhci_readl(host, SDHCI_INT_STATUS);
  184. if (stat & SDHCI_INT_ERROR)
  185. break;
  186. } while ((stat & mask) != mask);
  187. if ((stat & (SDHCI_INT_ERROR | mask)) == mask) {
  188. sdhci_cmd_done(host, cmd);
  189. sdhci_writel(host, mask, SDHCI_INT_STATUS);
  190. } else
  191. ret = -1;
  192. if (!ret && data)
  193. ret = sdhci_transfer_data(host, data, start_addr);
  194. stat = sdhci_readl(host, SDHCI_INT_STATUS);
  195. sdhci_writel(host, SDHCI_INT_ALL_MASK, SDHCI_INT_STATUS);
  196. if (!ret) {
  197. if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) &&
  198. !is_aligned && (data->flags == MMC_DATA_READ))
  199. memcpy(data->dest, aligned_buffer, trans_bytes);
  200. return 0;
  201. }
  202. sdhci_reset(host, SDHCI_RESET_CMD);
  203. sdhci_reset(host, SDHCI_RESET_DATA);
  204. if (stat & SDHCI_INT_TIMEOUT)
  205. return TIMEOUT;
  206. else
  207. return COMM_ERR;
  208. }
  209. static int sdhci_set_clock(struct mmc *mmc, unsigned int clock)
  210. {
  211. struct sdhci_host *host = (struct sdhci_host *)mmc->priv;
  212. unsigned int div, clk, timeout;
  213. sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
  214. if (clock == 0)
  215. return 0;
  216. if (host->version >= SDHCI_SPEC_300) {
  217. /* Version 3.00 divisors must be a multiple of 2. */
  218. if (mmc->f_max <= clock)
  219. div = 1;
  220. else {
  221. for (div = 2; div < SDHCI_MAX_DIV_SPEC_300; div += 2) {
  222. if ((mmc->f_max / div) <= clock)
  223. break;
  224. }
  225. }
  226. } else {
  227. /* Version 2.00 divisors must be a power of 2. */
  228. for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
  229. if ((mmc->f_max / div) <= clock)
  230. break;
  231. }
  232. }
  233. div >>= 1;
  234. clk = (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
  235. clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
  236. << SDHCI_DIVIDER_HI_SHIFT;
  237. clk |= SDHCI_CLOCK_INT_EN;
  238. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  239. /* Wait max 20 ms */
  240. timeout = 20;
  241. while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
  242. & SDHCI_CLOCK_INT_STABLE)) {
  243. if (timeout == 0) {
  244. printf("Internal clock never stabilised.\n");
  245. return -1;
  246. }
  247. timeout--;
  248. udelay(1000);
  249. }
  250. clk |= SDHCI_CLOCK_CARD_EN;
  251. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  252. return 0;
  253. }
  254. static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
  255. {
  256. u8 pwr = 0;
  257. if (power != (unsigned short)-1) {
  258. switch (1 << power) {
  259. case MMC_VDD_165_195:
  260. pwr = SDHCI_POWER_180;
  261. break;
  262. case MMC_VDD_29_30:
  263. case MMC_VDD_30_31:
  264. pwr = SDHCI_POWER_300;
  265. break;
  266. case MMC_VDD_32_33:
  267. case MMC_VDD_33_34:
  268. pwr = SDHCI_POWER_330;
  269. break;
  270. }
  271. }
  272. if (pwr == 0) {
  273. sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
  274. return;
  275. }
  276. pwr |= SDHCI_POWER_ON;
  277. sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
  278. }
  279. void sdhci_set_ios(struct mmc *mmc)
  280. {
  281. u32 ctrl;
  282. struct sdhci_host *host = (struct sdhci_host *)mmc->priv;
  283. if (mmc->clock != host->clock)
  284. sdhci_set_clock(mmc, mmc->clock);
  285. /* Set bus width */
  286. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  287. if (mmc->bus_width == 8) {
  288. ctrl &= ~SDHCI_CTRL_4BITBUS;
  289. if (host->version >= SDHCI_SPEC_300)
  290. ctrl |= SDHCI_CTRL_8BITBUS;
  291. } else {
  292. if (host->version >= SDHCI_SPEC_300)
  293. ctrl &= ~SDHCI_CTRL_8BITBUS;
  294. if (mmc->bus_width == 4)
  295. ctrl |= SDHCI_CTRL_4BITBUS;
  296. else
  297. ctrl &= ~SDHCI_CTRL_4BITBUS;
  298. }
  299. if (mmc->clock > 26000000)
  300. ctrl |= SDHCI_CTRL_HISPD;
  301. else
  302. ctrl &= ~SDHCI_CTRL_HISPD;
  303. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  304. }
  305. int sdhci_init(struct mmc *mmc)
  306. {
  307. struct sdhci_host *host = (struct sdhci_host *)mmc->priv;
  308. if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) && !aligned_buffer) {
  309. aligned_buffer = memalign(8, 512*1024);
  310. if (!aligned_buffer) {
  311. printf("Aligned buffer alloc failed!!!");
  312. return -1;
  313. }
  314. }
  315. /* Eable all state */
  316. sdhci_writel(host, SDHCI_INT_ALL_MASK, SDHCI_INT_ENABLE);
  317. sdhci_writel(host, SDHCI_INT_ALL_MASK, SDHCI_SIGNAL_ENABLE);
  318. sdhci_set_power(host, fls(mmc->voltages) - 1);
  319. return 0;
  320. }
  321. int add_sdhci(struct sdhci_host *host, u32 max_clk, u32 min_clk)
  322. {
  323. struct mmc *mmc;
  324. unsigned int caps;
  325. mmc = malloc(sizeof(struct mmc));
  326. if (!mmc) {
  327. printf("mmc malloc fail!\n");
  328. return -1;
  329. }
  330. mmc->priv = host;
  331. sprintf(mmc->name, "%s", host->name);
  332. mmc->send_cmd = sdhci_send_command;
  333. mmc->set_ios = sdhci_set_ios;
  334. mmc->init = sdhci_init;
  335. caps = sdhci_readl(host, SDHCI_CAPABILITIES);
  336. #ifdef CONFIG_MMC_SDMA
  337. if (!(caps & SDHCI_CAN_DO_SDMA)) {
  338. printf("Your controller don't support sdma!!\n");
  339. return -1;
  340. }
  341. #endif
  342. if (max_clk)
  343. mmc->f_max = max_clk;
  344. else {
  345. if (host->version >= SDHCI_SPEC_300)
  346. mmc->f_max = (caps & SDHCI_CLOCK_V3_BASE_MASK)
  347. >> SDHCI_CLOCK_BASE_SHIFT;
  348. else
  349. mmc->f_max = (caps & SDHCI_CLOCK_BASE_MASK)
  350. >> SDHCI_CLOCK_BASE_SHIFT;
  351. mmc->f_max *= 1000000;
  352. }
  353. if (mmc->f_max == 0) {
  354. printf("Hardware doesn't specify base clock frequency\n");
  355. return -1;
  356. }
  357. if (min_clk)
  358. mmc->f_min = min_clk;
  359. else {
  360. if (host->version >= SDHCI_SPEC_300)
  361. mmc->f_min = mmc->f_max / SDHCI_MAX_DIV_SPEC_300;
  362. else
  363. mmc->f_min = mmc->f_max / SDHCI_MAX_DIV_SPEC_200;
  364. }
  365. mmc->voltages = 0;
  366. if (caps & SDHCI_CAN_VDD_330)
  367. mmc->voltages |= MMC_VDD_32_33 | MMC_VDD_33_34;
  368. if (caps & SDHCI_CAN_VDD_300)
  369. mmc->voltages |= MMC_VDD_29_30 | MMC_VDD_30_31;
  370. if (caps & SDHCI_CAN_VDD_180)
  371. mmc->voltages |= MMC_VDD_165_195;
  372. mmc->host_caps = MMC_MODE_HS | MMC_MODE_HS_52MHz | MMC_MODE_4BIT;
  373. if (caps & SDHCI_CAN_DO_8BIT)
  374. mmc->host_caps |= MMC_MODE_8BIT;
  375. sdhci_reset(host, SDHCI_RESET_ALL);
  376. mmc_register(mmc);
  377. return 0;
  378. }