sdhci.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*
  2. * Copyright 2011, Marvell Semiconductor Inc.
  3. * Lei Wen <leiwen@marvell.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. *
  7. * Back ported to the 8xx platform (from the 8260 platform) by
  8. * Murray.Jensen@cmst.csiro.au, 27-Jan-01.
  9. */
  10. #include <common.h>
  11. #include <malloc.h>
  12. #include <mmc.h>
  13. #include <sdhci.h>
  14. void *aligned_buffer;
  15. static void sdhci_reset(struct sdhci_host *host, u8 mask)
  16. {
  17. unsigned long timeout;
  18. /* Wait max 100 ms */
  19. timeout = 100;
  20. sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
  21. while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
  22. if (timeout == 0) {
  23. printf("%s: Reset 0x%x never completed.\n",
  24. __func__, (int)mask);
  25. return;
  26. }
  27. timeout--;
  28. udelay(1000);
  29. }
  30. }
  31. static void sdhci_cmd_done(struct sdhci_host *host, struct mmc_cmd *cmd)
  32. {
  33. int i;
  34. if (cmd->resp_type & MMC_RSP_136) {
  35. /* CRC is stripped so we need to do some shifting. */
  36. for (i = 0; i < 4; i++) {
  37. cmd->response[i] = sdhci_readl(host,
  38. SDHCI_RESPONSE + (3-i)*4) << 8;
  39. if (i != 3)
  40. cmd->response[i] |= sdhci_readb(host,
  41. SDHCI_RESPONSE + (3-i)*4-1);
  42. }
  43. } else {
  44. cmd->response[0] = sdhci_readl(host, SDHCI_RESPONSE);
  45. }
  46. }
  47. static void sdhci_transfer_pio(struct sdhci_host *host, struct mmc_data *data)
  48. {
  49. int i;
  50. char *offs;
  51. for (i = 0; i < data->blocksize; i += 4) {
  52. offs = data->dest + i;
  53. if (data->flags == MMC_DATA_READ)
  54. *(u32 *)offs = sdhci_readl(host, SDHCI_BUFFER);
  55. else
  56. sdhci_writel(host, *(u32 *)offs, SDHCI_BUFFER);
  57. }
  58. }
  59. static int sdhci_transfer_data(struct sdhci_host *host, struct mmc_data *data,
  60. unsigned int start_addr)
  61. {
  62. unsigned int stat, rdy, mask, timeout, block = 0;
  63. #ifdef CONFIG_MMC_SDMA
  64. unsigned char ctrl;
  65. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  66. ctrl &= ~SDHCI_CTRL_DMA_MASK;
  67. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  68. #endif
  69. timeout = 1000000;
  70. rdy = SDHCI_INT_SPACE_AVAIL | SDHCI_INT_DATA_AVAIL;
  71. mask = SDHCI_DATA_AVAILABLE | SDHCI_SPACE_AVAILABLE;
  72. do {
  73. stat = sdhci_readl(host, SDHCI_INT_STATUS);
  74. if (stat & SDHCI_INT_ERROR) {
  75. printf("%s: Error detected in status(0x%X)!\n",
  76. __func__, stat);
  77. return -1;
  78. }
  79. if (stat & rdy) {
  80. if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) & mask))
  81. continue;
  82. sdhci_writel(host, rdy, SDHCI_INT_STATUS);
  83. sdhci_transfer_pio(host, data);
  84. data->dest += data->blocksize;
  85. if (++block >= data->blocks)
  86. break;
  87. }
  88. #ifdef CONFIG_MMC_SDMA
  89. if (stat & SDHCI_INT_DMA_END) {
  90. sdhci_writel(host, SDHCI_INT_DMA_END, SDHCI_INT_STATUS);
  91. start_addr &= ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1);
  92. start_addr += SDHCI_DEFAULT_BOUNDARY_SIZE;
  93. sdhci_writel(host, start_addr, SDHCI_DMA_ADDRESS);
  94. }
  95. #endif
  96. if (timeout-- > 0)
  97. udelay(10);
  98. else {
  99. printf("%s: Transfer data timeout\n", __func__);
  100. return -1;
  101. }
  102. } while (!(stat & SDHCI_INT_DATA_END));
  103. return 0;
  104. }
  105. /*
  106. * No command will be sent by driver if card is busy, so driver must wait
  107. * for card ready state.
  108. * Every time when card is busy after timeout then (last) timeout value will be
  109. * increased twice but only if it doesn't exceed global defined maximum.
  110. * Each function call will use last timeout value. Max timeout can be redefined
  111. * in board config file.
  112. */
  113. #ifndef CONFIG_SDHCI_CMD_MAX_TIMEOUT
  114. #define CONFIG_SDHCI_CMD_MAX_TIMEOUT 3200
  115. #endif
  116. #define CONFIG_SDHCI_CMD_DEFAULT_TIMEOUT 100
  117. static int sdhci_send_command(struct mmc *mmc, struct mmc_cmd *cmd,
  118. struct mmc_data *data)
  119. {
  120. struct sdhci_host *host = mmc->priv;
  121. unsigned int stat = 0;
  122. int ret = 0;
  123. int trans_bytes = 0, is_aligned = 1;
  124. u32 mask, flags, mode;
  125. unsigned int time = 0, start_addr = 0;
  126. unsigned int retry = 10000;
  127. int mmc_dev = mmc->block_dev.dev;
  128. /* Timeout unit - ms */
  129. static unsigned int cmd_timeout = CONFIG_SDHCI_CMD_DEFAULT_TIMEOUT;
  130. sdhci_writel(host, SDHCI_INT_ALL_MASK, SDHCI_INT_STATUS);
  131. mask = SDHCI_CMD_INHIBIT | SDHCI_DATA_INHIBIT;
  132. /* We shouldn't wait for data inihibit for stop commands, even
  133. though they might use busy signaling */
  134. if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
  135. mask &= ~SDHCI_DATA_INHIBIT;
  136. while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
  137. if (time >= cmd_timeout) {
  138. printf("%s: MMC: %d busy ", __func__, mmc_dev);
  139. if (2 * cmd_timeout <= CONFIG_SDHCI_CMD_MAX_TIMEOUT) {
  140. cmd_timeout += cmd_timeout;
  141. printf("timeout increasing to: %u ms.\n",
  142. cmd_timeout);
  143. } else {
  144. puts("timeout.\n");
  145. return COMM_ERR;
  146. }
  147. }
  148. time++;
  149. udelay(1000);
  150. }
  151. mask = SDHCI_INT_RESPONSE;
  152. if (!(cmd->resp_type & MMC_RSP_PRESENT))
  153. flags = SDHCI_CMD_RESP_NONE;
  154. else if (cmd->resp_type & MMC_RSP_136)
  155. flags = SDHCI_CMD_RESP_LONG;
  156. else if (cmd->resp_type & MMC_RSP_BUSY) {
  157. flags = SDHCI_CMD_RESP_SHORT_BUSY;
  158. mask |= SDHCI_INT_DATA_END;
  159. } else
  160. flags = SDHCI_CMD_RESP_SHORT;
  161. if (cmd->resp_type & MMC_RSP_CRC)
  162. flags |= SDHCI_CMD_CRC;
  163. if (cmd->resp_type & MMC_RSP_OPCODE)
  164. flags |= SDHCI_CMD_INDEX;
  165. if (data)
  166. flags |= SDHCI_CMD_DATA;
  167. /* Set Transfer mode regarding to data flag */
  168. if (data != 0) {
  169. sdhci_writeb(host, 0xe, SDHCI_TIMEOUT_CONTROL);
  170. mode = SDHCI_TRNS_BLK_CNT_EN;
  171. trans_bytes = data->blocks * data->blocksize;
  172. if (data->blocks > 1)
  173. mode |= SDHCI_TRNS_MULTI;
  174. if (data->flags == MMC_DATA_READ)
  175. mode |= SDHCI_TRNS_READ;
  176. #ifdef CONFIG_MMC_SDMA
  177. if (data->flags == MMC_DATA_READ)
  178. start_addr = (unsigned long)data->dest;
  179. else
  180. start_addr = (unsigned long)data->src;
  181. if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) &&
  182. (start_addr & 0x7) != 0x0) {
  183. is_aligned = 0;
  184. start_addr = (unsigned long)aligned_buffer;
  185. if (data->flags != MMC_DATA_READ)
  186. memcpy(aligned_buffer, data->src, trans_bytes);
  187. }
  188. sdhci_writel(host, start_addr, SDHCI_DMA_ADDRESS);
  189. mode |= SDHCI_TRNS_DMA;
  190. #endif
  191. sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
  192. data->blocksize),
  193. SDHCI_BLOCK_SIZE);
  194. sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
  195. sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
  196. }
  197. sdhci_writel(host, cmd->cmdarg, SDHCI_ARGUMENT);
  198. #ifdef CONFIG_MMC_SDMA
  199. flush_cache(start_addr, trans_bytes);
  200. #endif
  201. sdhci_writew(host, SDHCI_MAKE_CMD(cmd->cmdidx, flags), SDHCI_COMMAND);
  202. do {
  203. stat = sdhci_readl(host, SDHCI_INT_STATUS);
  204. if (stat & SDHCI_INT_ERROR)
  205. break;
  206. if (--retry == 0)
  207. break;
  208. } while ((stat & mask) != mask);
  209. if (retry == 0) {
  210. if (host->quirks & SDHCI_QUIRK_BROKEN_R1B)
  211. return 0;
  212. else {
  213. printf("%s: Timeout for status update!\n", __func__);
  214. return TIMEOUT;
  215. }
  216. }
  217. if ((stat & (SDHCI_INT_ERROR | mask)) == mask) {
  218. sdhci_cmd_done(host, cmd);
  219. sdhci_writel(host, mask, SDHCI_INT_STATUS);
  220. } else
  221. ret = -1;
  222. if (!ret && data)
  223. ret = sdhci_transfer_data(host, data, start_addr);
  224. if (host->quirks & SDHCI_QUIRK_WAIT_SEND_CMD)
  225. udelay(1000);
  226. stat = sdhci_readl(host, SDHCI_INT_STATUS);
  227. sdhci_writel(host, SDHCI_INT_ALL_MASK, SDHCI_INT_STATUS);
  228. if (!ret) {
  229. if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) &&
  230. !is_aligned && (data->flags == MMC_DATA_READ))
  231. memcpy(data->dest, aligned_buffer, trans_bytes);
  232. return 0;
  233. }
  234. sdhci_reset(host, SDHCI_RESET_CMD);
  235. sdhci_reset(host, SDHCI_RESET_DATA);
  236. if (stat & SDHCI_INT_TIMEOUT)
  237. return TIMEOUT;
  238. else
  239. return COMM_ERR;
  240. }
  241. static int sdhci_set_clock(struct mmc *mmc, unsigned int clock)
  242. {
  243. struct sdhci_host *host = mmc->priv;
  244. unsigned int div, clk, timeout;
  245. sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
  246. if (clock == 0)
  247. return 0;
  248. if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) {
  249. /* Version 3.00 divisors must be a multiple of 2. */
  250. if (mmc->cfg->f_max <= clock)
  251. div = 1;
  252. else {
  253. for (div = 2; div < SDHCI_MAX_DIV_SPEC_300; div += 2) {
  254. if ((mmc->cfg->f_max / div) <= clock)
  255. break;
  256. }
  257. }
  258. } else {
  259. /* Version 2.00 divisors must be a power of 2. */
  260. for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
  261. if ((mmc->cfg->f_max / div) <= clock)
  262. break;
  263. }
  264. }
  265. div >>= 1;
  266. if (host->set_clock)
  267. host->set_clock(host->index, div);
  268. clk = (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
  269. clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
  270. << SDHCI_DIVIDER_HI_SHIFT;
  271. clk |= SDHCI_CLOCK_INT_EN;
  272. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  273. /* Wait max 20 ms */
  274. timeout = 20;
  275. while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
  276. & SDHCI_CLOCK_INT_STABLE)) {
  277. if (timeout == 0) {
  278. printf("%s: Internal clock never stabilised.\n",
  279. __func__);
  280. return -1;
  281. }
  282. timeout--;
  283. udelay(1000);
  284. }
  285. clk |= SDHCI_CLOCK_CARD_EN;
  286. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  287. return 0;
  288. }
  289. static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
  290. {
  291. u8 pwr = 0;
  292. if (power != (unsigned short)-1) {
  293. switch (1 << power) {
  294. case MMC_VDD_165_195:
  295. pwr = SDHCI_POWER_180;
  296. break;
  297. case MMC_VDD_29_30:
  298. case MMC_VDD_30_31:
  299. pwr = SDHCI_POWER_300;
  300. break;
  301. case MMC_VDD_32_33:
  302. case MMC_VDD_33_34:
  303. pwr = SDHCI_POWER_330;
  304. break;
  305. }
  306. }
  307. if (pwr == 0) {
  308. sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
  309. return;
  310. }
  311. if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
  312. sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
  313. pwr |= SDHCI_POWER_ON;
  314. sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
  315. }
  316. static void sdhci_set_ios(struct mmc *mmc)
  317. {
  318. u32 ctrl;
  319. struct sdhci_host *host = mmc->priv;
  320. if (host->set_control_reg)
  321. host->set_control_reg(host);
  322. if (mmc->clock != host->clock)
  323. sdhci_set_clock(mmc, mmc->clock);
  324. /* Set bus width */
  325. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  326. if (mmc->bus_width == 8) {
  327. ctrl &= ~SDHCI_CTRL_4BITBUS;
  328. if ((SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) ||
  329. (host->quirks & SDHCI_QUIRK_USE_WIDE8))
  330. ctrl |= SDHCI_CTRL_8BITBUS;
  331. } else {
  332. if ((SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) ||
  333. (host->quirks & SDHCI_QUIRK_USE_WIDE8))
  334. ctrl &= ~SDHCI_CTRL_8BITBUS;
  335. if (mmc->bus_width == 4)
  336. ctrl |= SDHCI_CTRL_4BITBUS;
  337. else
  338. ctrl &= ~SDHCI_CTRL_4BITBUS;
  339. }
  340. if (mmc->clock > 26000000)
  341. ctrl |= SDHCI_CTRL_HISPD;
  342. else
  343. ctrl &= ~SDHCI_CTRL_HISPD;
  344. if (host->quirks & SDHCI_QUIRK_NO_HISPD_BIT)
  345. ctrl &= ~SDHCI_CTRL_HISPD;
  346. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  347. }
  348. static int sdhci_init(struct mmc *mmc)
  349. {
  350. struct sdhci_host *host = mmc->priv;
  351. if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) && !aligned_buffer) {
  352. aligned_buffer = memalign(8, 512*1024);
  353. if (!aligned_buffer) {
  354. printf("%s: Aligned buffer alloc failed!!!\n",
  355. __func__);
  356. return -1;
  357. }
  358. }
  359. sdhci_set_power(host, fls(mmc->cfg->voltages) - 1);
  360. if (host->quirks & SDHCI_QUIRK_NO_CD) {
  361. unsigned int status;
  362. sdhci_writeb(host, SDHCI_CTRL_CD_TEST_INS | SDHCI_CTRL_CD_TEST,
  363. SDHCI_HOST_CONTROL);
  364. status = sdhci_readl(host, SDHCI_PRESENT_STATE);
  365. while ((!(status & SDHCI_CARD_PRESENT)) ||
  366. (!(status & SDHCI_CARD_STATE_STABLE)) ||
  367. (!(status & SDHCI_CARD_DETECT_PIN_LEVEL)))
  368. status = sdhci_readl(host, SDHCI_PRESENT_STATE);
  369. }
  370. /* Enable only interrupts served by the SD controller */
  371. sdhci_writel(host, SDHCI_INT_DATA_MASK | SDHCI_INT_CMD_MASK,
  372. SDHCI_INT_ENABLE);
  373. /* Mask all sdhci interrupt sources */
  374. sdhci_writel(host, 0x0, SDHCI_SIGNAL_ENABLE);
  375. return 0;
  376. }
  377. static const struct mmc_ops sdhci_ops = {
  378. .send_cmd = sdhci_send_command,
  379. .set_ios = sdhci_set_ios,
  380. .init = sdhci_init,
  381. };
  382. int add_sdhci(struct sdhci_host *host, u32 max_clk, u32 min_clk)
  383. {
  384. unsigned int caps;
  385. host->cfg.name = host->name;
  386. host->cfg.ops = &sdhci_ops;
  387. caps = sdhci_readl(host, SDHCI_CAPABILITIES);
  388. #ifdef CONFIG_MMC_SDMA
  389. if (!(caps & SDHCI_CAN_DO_SDMA)) {
  390. printf("%s: Your controller doesn't support SDMA!!\n",
  391. __func__);
  392. return -1;
  393. }
  394. #endif
  395. if (max_clk)
  396. host->cfg.f_max = max_clk;
  397. else {
  398. if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300)
  399. host->cfg.f_max = (caps & SDHCI_CLOCK_V3_BASE_MASK)
  400. >> SDHCI_CLOCK_BASE_SHIFT;
  401. else
  402. host->cfg.f_max = (caps & SDHCI_CLOCK_BASE_MASK)
  403. >> SDHCI_CLOCK_BASE_SHIFT;
  404. host->cfg.f_max *= 1000000;
  405. }
  406. if (host->cfg.f_max == 0) {
  407. printf("%s: Hardware doesn't specify base clock frequency\n",
  408. __func__);
  409. return -1;
  410. }
  411. if (min_clk)
  412. host->cfg.f_min = min_clk;
  413. else {
  414. if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300)
  415. host->cfg.f_min = host->cfg.f_max /
  416. SDHCI_MAX_DIV_SPEC_300;
  417. else
  418. host->cfg.f_min = host->cfg.f_max /
  419. SDHCI_MAX_DIV_SPEC_200;
  420. }
  421. host->cfg.voltages = 0;
  422. if (caps & SDHCI_CAN_VDD_330)
  423. host->cfg.voltages |= MMC_VDD_32_33 | MMC_VDD_33_34;
  424. if (caps & SDHCI_CAN_VDD_300)
  425. host->cfg.voltages |= MMC_VDD_29_30 | MMC_VDD_30_31;
  426. if (caps & SDHCI_CAN_VDD_180)
  427. host->cfg.voltages |= MMC_VDD_165_195;
  428. if (host->quirks & SDHCI_QUIRK_BROKEN_VOLTAGE)
  429. host->cfg.voltages |= host->voltages;
  430. host->cfg.host_caps = MMC_MODE_HS | MMC_MODE_HS_52MHz | MMC_MODE_4BIT;
  431. if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) {
  432. if (caps & SDHCI_CAN_DO_8BIT)
  433. host->cfg.host_caps |= MMC_MODE_8BIT;
  434. }
  435. if (host->host_caps)
  436. host->cfg.host_caps |= host->host_caps;
  437. host->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
  438. sdhci_reset(host, SDHCI_RESET_ALL);
  439. host->mmc = mmc_create(&host->cfg, host);
  440. if (host->mmc == NULL) {
  441. printf("%s: mmc create fail!\n", __func__);
  442. return -1;
  443. }
  444. return 0;
  445. }