sdhci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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, timeout, block = 0;
  78. timeout = 10000;
  79. rdy = SDHCI_INT_SPACE_AVAIL | SDHCI_INT_DATA_AVAIL;
  80. mask = SDHCI_DATA_AVAILABLE | SDHCI_SPACE_AVAILABLE;
  81. do {
  82. stat = sdhci_readl(host, SDHCI_INT_STATUS);
  83. if (stat & SDHCI_INT_ERROR) {
  84. printf("Error detected in status(0x%X)!\n", stat);
  85. return -1;
  86. }
  87. if (stat & rdy) {
  88. if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) & mask))
  89. continue;
  90. sdhci_writel(host, rdy, SDHCI_INT_STATUS);
  91. sdhci_transfer_pio(host, data);
  92. data->dest += data->blocksize;
  93. if (++block >= data->blocks)
  94. break;
  95. }
  96. #ifdef CONFIG_MMC_SDMA
  97. if (stat & SDHCI_INT_DMA_END) {
  98. sdhci_writel(host, SDHCI_INT_DMA_END, SDHCI_INT_STATUS);
  99. start_addr &= ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1);
  100. start_addr += SDHCI_DEFAULT_BOUNDARY_SIZE;
  101. sdhci_writel(host, start_addr, SDHCI_DMA_ADDRESS);
  102. }
  103. #endif
  104. if (timeout-- > 0)
  105. udelay(10);
  106. else {
  107. printf("Transfer data timeout\n");
  108. return -1;
  109. }
  110. } while (!(stat & SDHCI_INT_DATA_END));
  111. return 0;
  112. }
  113. int sdhci_send_command(struct mmc *mmc, struct mmc_cmd *cmd,
  114. struct mmc_data *data)
  115. {
  116. struct sdhci_host *host = (struct sdhci_host *)mmc->priv;
  117. unsigned int stat = 0;
  118. int ret = 0;
  119. int trans_bytes = 0, is_aligned = 1;
  120. u32 mask, flags, mode;
  121. unsigned int timeout, start_addr = 0;
  122. /* Wait max 10 ms */
  123. timeout = 10;
  124. sdhci_writel(host, SDHCI_INT_ALL_MASK, SDHCI_INT_STATUS);
  125. mask = SDHCI_CMD_INHIBIT | SDHCI_DATA_INHIBIT;
  126. /* We shouldn't wait for data inihibit for stop commands, even
  127. though they might use busy signaling */
  128. if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
  129. mask &= ~SDHCI_DATA_INHIBIT;
  130. while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
  131. if (timeout == 0) {
  132. printf("Controller never released inhibit bit(s).\n");
  133. return COMM_ERR;
  134. }
  135. timeout--;
  136. udelay(1000);
  137. }
  138. mask = SDHCI_INT_RESPONSE;
  139. if (!(cmd->resp_type & MMC_RSP_PRESENT))
  140. flags = SDHCI_CMD_RESP_NONE;
  141. else if (cmd->resp_type & MMC_RSP_136)
  142. flags = SDHCI_CMD_RESP_LONG;
  143. else if (cmd->resp_type & MMC_RSP_BUSY) {
  144. flags = SDHCI_CMD_RESP_SHORT_BUSY;
  145. mask |= SDHCI_INT_DATA_END;
  146. } else
  147. flags = SDHCI_CMD_RESP_SHORT;
  148. if (cmd->resp_type & MMC_RSP_CRC)
  149. flags |= SDHCI_CMD_CRC;
  150. if (cmd->resp_type & MMC_RSP_OPCODE)
  151. flags |= SDHCI_CMD_INDEX;
  152. if (data)
  153. flags |= SDHCI_CMD_DATA;
  154. /*Set Transfer mode regarding to data flag*/
  155. if (data != 0) {
  156. sdhci_writeb(host, 0xe, SDHCI_TIMEOUT_CONTROL);
  157. mode = SDHCI_TRNS_BLK_CNT_EN;
  158. trans_bytes = data->blocks * data->blocksize;
  159. if (data->blocks > 1)
  160. mode |= SDHCI_TRNS_MULTI;
  161. if (data->flags == MMC_DATA_READ)
  162. mode |= SDHCI_TRNS_READ;
  163. #ifdef CONFIG_MMC_SDMA
  164. if (data->flags == MMC_DATA_READ)
  165. start_addr = (unsigned int)data->dest;
  166. else
  167. start_addr = (unsigned int)data->src;
  168. if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) &&
  169. (start_addr & 0x7) != 0x0) {
  170. is_aligned = 0;
  171. start_addr = (unsigned int)aligned_buffer;
  172. if (data->flags != MMC_DATA_READ)
  173. memcpy(aligned_buffer, data->src, trans_bytes);
  174. }
  175. sdhci_writel(host, start_addr, SDHCI_DMA_ADDRESS);
  176. mode |= SDHCI_TRNS_DMA;
  177. #endif
  178. sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
  179. data->blocksize),
  180. SDHCI_BLOCK_SIZE);
  181. sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
  182. sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
  183. }
  184. sdhci_writel(host, cmd->cmdarg, SDHCI_ARGUMENT);
  185. #ifdef CONFIG_MMC_SDMA
  186. flush_cache(start_addr, trans_bytes);
  187. #endif
  188. sdhci_writew(host, SDHCI_MAKE_CMD(cmd->cmdidx, flags), SDHCI_COMMAND);
  189. do {
  190. stat = sdhci_readl(host, SDHCI_INT_STATUS);
  191. if (stat & SDHCI_INT_ERROR)
  192. break;
  193. } while ((stat & mask) != mask);
  194. if ((stat & (SDHCI_INT_ERROR | mask)) == mask) {
  195. sdhci_cmd_done(host, cmd);
  196. sdhci_writel(host, mask, SDHCI_INT_STATUS);
  197. } else
  198. ret = -1;
  199. if (!ret && data)
  200. ret = sdhci_transfer_data(host, data, start_addr);
  201. stat = sdhci_readl(host, SDHCI_INT_STATUS);
  202. sdhci_writel(host, SDHCI_INT_ALL_MASK, SDHCI_INT_STATUS);
  203. if (!ret) {
  204. if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) &&
  205. !is_aligned && (data->flags == MMC_DATA_READ))
  206. memcpy(data->dest, aligned_buffer, trans_bytes);
  207. return 0;
  208. }
  209. sdhci_reset(host, SDHCI_RESET_CMD);
  210. sdhci_reset(host, SDHCI_RESET_DATA);
  211. if (stat & SDHCI_INT_TIMEOUT)
  212. return TIMEOUT;
  213. else
  214. return COMM_ERR;
  215. }
  216. static int sdhci_set_clock(struct mmc *mmc, unsigned int clock)
  217. {
  218. struct sdhci_host *host = (struct sdhci_host *)mmc->priv;
  219. unsigned int div, clk, timeout;
  220. sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
  221. if (clock == 0)
  222. return 0;
  223. if (host->version >= SDHCI_SPEC_300) {
  224. /* Version 3.00 divisors must be a multiple of 2. */
  225. if (mmc->f_max <= clock)
  226. div = 1;
  227. else {
  228. for (div = 2; div < SDHCI_MAX_DIV_SPEC_300; div += 2) {
  229. if ((mmc->f_max / div) <= clock)
  230. break;
  231. }
  232. }
  233. } else {
  234. /* Version 2.00 divisors must be a power of 2. */
  235. for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
  236. if ((mmc->f_max / div) <= clock)
  237. break;
  238. }
  239. }
  240. div >>= 1;
  241. clk = (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
  242. clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
  243. << SDHCI_DIVIDER_HI_SHIFT;
  244. clk |= SDHCI_CLOCK_INT_EN;
  245. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  246. /* Wait max 20 ms */
  247. timeout = 20;
  248. while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
  249. & SDHCI_CLOCK_INT_STABLE)) {
  250. if (timeout == 0) {
  251. printf("Internal clock never stabilised.\n");
  252. return -1;
  253. }
  254. timeout--;
  255. udelay(1000);
  256. }
  257. clk |= SDHCI_CLOCK_CARD_EN;
  258. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  259. return 0;
  260. }
  261. static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
  262. {
  263. u8 pwr = 0;
  264. if (power != (unsigned short)-1) {
  265. switch (1 << power) {
  266. case MMC_VDD_165_195:
  267. pwr = SDHCI_POWER_180;
  268. break;
  269. case MMC_VDD_29_30:
  270. case MMC_VDD_30_31:
  271. pwr = SDHCI_POWER_300;
  272. break;
  273. case MMC_VDD_32_33:
  274. case MMC_VDD_33_34:
  275. pwr = SDHCI_POWER_330;
  276. break;
  277. }
  278. }
  279. if (pwr == 0) {
  280. sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
  281. return;
  282. }
  283. pwr |= SDHCI_POWER_ON;
  284. sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
  285. }
  286. void sdhci_set_ios(struct mmc *mmc)
  287. {
  288. u32 ctrl;
  289. struct sdhci_host *host = (struct sdhci_host *)mmc->priv;
  290. if (mmc->clock != host->clock)
  291. sdhci_set_clock(mmc, mmc->clock);
  292. /* Set bus width */
  293. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  294. if (mmc->bus_width == 8) {
  295. ctrl &= ~SDHCI_CTRL_4BITBUS;
  296. if (host->version >= SDHCI_SPEC_300)
  297. ctrl |= SDHCI_CTRL_8BITBUS;
  298. } else {
  299. if (host->version >= SDHCI_SPEC_300)
  300. ctrl &= ~SDHCI_CTRL_8BITBUS;
  301. if (mmc->bus_width == 4)
  302. ctrl |= SDHCI_CTRL_4BITBUS;
  303. else
  304. ctrl &= ~SDHCI_CTRL_4BITBUS;
  305. }
  306. if (mmc->clock > 26000000)
  307. ctrl |= SDHCI_CTRL_HISPD;
  308. else
  309. ctrl &= ~SDHCI_CTRL_HISPD;
  310. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  311. }
  312. int sdhci_init(struct mmc *mmc)
  313. {
  314. struct sdhci_host *host = (struct sdhci_host *)mmc->priv;
  315. if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) && !aligned_buffer) {
  316. aligned_buffer = memalign(8, 512*1024);
  317. if (!aligned_buffer) {
  318. printf("Aligned buffer alloc failed!!!");
  319. return -1;
  320. }
  321. }
  322. /* Eable all state */
  323. sdhci_writel(host, SDHCI_INT_ALL_MASK, SDHCI_INT_ENABLE);
  324. sdhci_writel(host, SDHCI_INT_ALL_MASK, SDHCI_SIGNAL_ENABLE);
  325. sdhci_set_power(host, fls(mmc->voltages) - 1);
  326. return 0;
  327. }
  328. int add_sdhci(struct sdhci_host *host, u32 max_clk, u32 min_clk)
  329. {
  330. struct mmc *mmc;
  331. unsigned int caps;
  332. mmc = malloc(sizeof(struct mmc));
  333. if (!mmc) {
  334. printf("mmc malloc fail!\n");
  335. return -1;
  336. }
  337. mmc->priv = host;
  338. host->mmc = mmc;
  339. sprintf(mmc->name, "%s", host->name);
  340. mmc->send_cmd = sdhci_send_command;
  341. mmc->set_ios = sdhci_set_ios;
  342. mmc->init = sdhci_init;
  343. caps = sdhci_readl(host, SDHCI_CAPABILITIES);
  344. #ifdef CONFIG_MMC_SDMA
  345. if (!(caps & SDHCI_CAN_DO_SDMA)) {
  346. printf("Your controller don't support sdma!!\n");
  347. return -1;
  348. }
  349. #endif
  350. if (max_clk)
  351. mmc->f_max = max_clk;
  352. else {
  353. if (host->version >= SDHCI_SPEC_300)
  354. mmc->f_max = (caps & SDHCI_CLOCK_V3_BASE_MASK)
  355. >> SDHCI_CLOCK_BASE_SHIFT;
  356. else
  357. mmc->f_max = (caps & SDHCI_CLOCK_BASE_MASK)
  358. >> SDHCI_CLOCK_BASE_SHIFT;
  359. mmc->f_max *= 1000000;
  360. }
  361. if (mmc->f_max == 0) {
  362. printf("Hardware doesn't specify base clock frequency\n");
  363. return -1;
  364. }
  365. if (min_clk)
  366. mmc->f_min = min_clk;
  367. else {
  368. if (host->version >= SDHCI_SPEC_300)
  369. mmc->f_min = mmc->f_max / SDHCI_MAX_DIV_SPEC_300;
  370. else
  371. mmc->f_min = mmc->f_max / SDHCI_MAX_DIV_SPEC_200;
  372. }
  373. mmc->voltages = 0;
  374. if (caps & SDHCI_CAN_VDD_330)
  375. mmc->voltages |= MMC_VDD_32_33 | MMC_VDD_33_34;
  376. if (caps & SDHCI_CAN_VDD_300)
  377. mmc->voltages |= MMC_VDD_29_30 | MMC_VDD_30_31;
  378. if (caps & SDHCI_CAN_VDD_180)
  379. mmc->voltages |= MMC_VDD_165_195;
  380. mmc->host_caps = MMC_MODE_HS | MMC_MODE_HS_52MHz | MMC_MODE_4BIT;
  381. if (caps & SDHCI_CAN_DO_8BIT)
  382. mmc->host_caps |= MMC_MODE_8BIT;
  383. sdhci_reset(host, SDHCI_RESET_ALL);
  384. mmc_register(mmc);
  385. return 0;
  386. }