sdhci.c 12 KB

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