sdhci.c 12 KB

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