sdhci.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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 <errno.h>
  12. #include <malloc.h>
  13. #include <mmc.h>
  14. #include <sdhci.h>
  15. #if defined(CONFIG_FIXED_SDHCI_ALIGNED_BUFFER)
  16. void *aligned_buffer = (void *)CONFIG_FIXED_SDHCI_ALIGNED_BUFFER;
  17. #else
  18. void *aligned_buffer;
  19. #endif
  20. static void sdhci_reset(struct sdhci_host *host, u8 mask)
  21. {
  22. unsigned long timeout;
  23. /* Wait max 100 ms */
  24. timeout = 100;
  25. sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
  26. while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
  27. if (timeout == 0) {
  28. printf("%s: Reset 0x%x never completed.\n",
  29. __func__, (int)mask);
  30. return;
  31. }
  32. timeout--;
  33. udelay(1000);
  34. }
  35. }
  36. static void sdhci_cmd_done(struct sdhci_host *host, struct mmc_cmd *cmd)
  37. {
  38. int i;
  39. if (cmd->resp_type & MMC_RSP_136) {
  40. /* CRC is stripped so we need to do some shifting. */
  41. for (i = 0; i < 4; i++) {
  42. cmd->response[i] = sdhci_readl(host,
  43. SDHCI_RESPONSE + (3-i)*4) << 8;
  44. if (i != 3)
  45. cmd->response[i] |= sdhci_readb(host,
  46. SDHCI_RESPONSE + (3-i)*4-1);
  47. }
  48. } else {
  49. cmd->response[0] = sdhci_readl(host, SDHCI_RESPONSE);
  50. }
  51. }
  52. static void sdhci_transfer_pio(struct sdhci_host *host, struct mmc_data *data)
  53. {
  54. int i;
  55. char *offs;
  56. for (i = 0; i < data->blocksize; i += 4) {
  57. offs = data->dest + i;
  58. if (data->flags == MMC_DATA_READ)
  59. *(u32 *)offs = sdhci_readl(host, SDHCI_BUFFER);
  60. else
  61. sdhci_writel(host, *(u32 *)offs, SDHCI_BUFFER);
  62. }
  63. }
  64. static int sdhci_transfer_data(struct sdhci_host *host, struct mmc_data *data,
  65. unsigned int start_addr)
  66. {
  67. unsigned int stat, rdy, mask, timeout, block = 0;
  68. bool transfer_done = false;
  69. #ifdef CONFIG_MMC_SDHCI_SDMA
  70. unsigned char ctrl;
  71. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  72. ctrl &= ~SDHCI_CTRL_DMA_MASK;
  73. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  74. #endif
  75. timeout = 1000000;
  76. rdy = SDHCI_INT_SPACE_AVAIL | SDHCI_INT_DATA_AVAIL;
  77. mask = SDHCI_DATA_AVAILABLE | SDHCI_SPACE_AVAILABLE;
  78. do {
  79. stat = sdhci_readl(host, SDHCI_INT_STATUS);
  80. if (stat & SDHCI_INT_ERROR) {
  81. pr_debug("%s: Error detected in status(0x%X)!\n",
  82. __func__, stat);
  83. return -EIO;
  84. }
  85. if (!transfer_done && (stat & rdy)) {
  86. if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) & mask))
  87. continue;
  88. sdhci_writel(host, rdy, SDHCI_INT_STATUS);
  89. sdhci_transfer_pio(host, data);
  90. data->dest += data->blocksize;
  91. if (++block >= data->blocks) {
  92. /* Keep looping until the SDHCI_INT_DATA_END is
  93. * cleared, even if we finished sending all the
  94. * blocks.
  95. */
  96. transfer_done = true;
  97. continue;
  98. }
  99. }
  100. #ifdef CONFIG_MMC_SDHCI_SDMA
  101. if (!transfer_done && (stat & SDHCI_INT_DMA_END)) {
  102. sdhci_writel(host, SDHCI_INT_DMA_END, SDHCI_INT_STATUS);
  103. start_addr &= ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1);
  104. start_addr += SDHCI_DEFAULT_BOUNDARY_SIZE;
  105. sdhci_writel(host, start_addr, SDHCI_DMA_ADDRESS);
  106. }
  107. #endif
  108. if (timeout-- > 0)
  109. udelay(10);
  110. else {
  111. printf("%s: Transfer data timeout\n", __func__);
  112. return -ETIMEDOUT;
  113. }
  114. } while (!(stat & SDHCI_INT_DATA_END));
  115. return 0;
  116. }
  117. /*
  118. * No command will be sent by driver if card is busy, so driver must wait
  119. * for card ready state.
  120. * Every time when card is busy after timeout then (last) timeout value will be
  121. * increased twice but only if it doesn't exceed global defined maximum.
  122. * Each function call will use last timeout value.
  123. */
  124. #define SDHCI_CMD_MAX_TIMEOUT 3200
  125. #define SDHCI_CMD_DEFAULT_TIMEOUT 100
  126. #define SDHCI_READ_STATUS_TIMEOUT 1000
  127. #ifdef CONFIG_DM_MMC
  128. static int sdhci_send_command(struct udevice *dev, struct mmc_cmd *cmd,
  129. struct mmc_data *data)
  130. {
  131. struct mmc *mmc = mmc_get_mmc_dev(dev);
  132. #else
  133. static int sdhci_send_command(struct mmc *mmc, struct mmc_cmd *cmd,
  134. struct mmc_data *data)
  135. {
  136. #endif
  137. struct sdhci_host *host = mmc->priv;
  138. unsigned int stat = 0;
  139. int ret = 0;
  140. int trans_bytes = 0, is_aligned = 1;
  141. u32 mask, flags, mode;
  142. unsigned int time = 0, start_addr = 0;
  143. int mmc_dev = mmc_get_blk_desc(mmc)->devnum;
  144. unsigned start = get_timer(0);
  145. /* Timeout unit - ms */
  146. static unsigned int cmd_timeout = SDHCI_CMD_DEFAULT_TIMEOUT;
  147. mask = SDHCI_CMD_INHIBIT | SDHCI_DATA_INHIBIT;
  148. /* We shouldn't wait for data inihibit for stop commands, even
  149. though they might use busy signaling */
  150. if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
  151. mask &= ~SDHCI_DATA_INHIBIT;
  152. while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
  153. if (time >= cmd_timeout) {
  154. printf("%s: MMC: %d busy ", __func__, mmc_dev);
  155. if (2 * cmd_timeout <= SDHCI_CMD_MAX_TIMEOUT) {
  156. cmd_timeout += cmd_timeout;
  157. printf("timeout increasing to: %u ms.\n",
  158. cmd_timeout);
  159. } else {
  160. puts("timeout.\n");
  161. return -ECOMM;
  162. }
  163. }
  164. time++;
  165. udelay(1000);
  166. }
  167. sdhci_writel(host, SDHCI_INT_ALL_MASK, SDHCI_INT_STATUS);
  168. mask = SDHCI_INT_RESPONSE;
  169. if (!(cmd->resp_type & MMC_RSP_PRESENT))
  170. flags = SDHCI_CMD_RESP_NONE;
  171. else if (cmd->resp_type & MMC_RSP_136)
  172. flags = SDHCI_CMD_RESP_LONG;
  173. else if (cmd->resp_type & MMC_RSP_BUSY) {
  174. flags = SDHCI_CMD_RESP_SHORT_BUSY;
  175. if (data)
  176. mask |= SDHCI_INT_DATA_END;
  177. } else
  178. flags = SDHCI_CMD_RESP_SHORT;
  179. if (cmd->resp_type & MMC_RSP_CRC)
  180. flags |= SDHCI_CMD_CRC;
  181. if (cmd->resp_type & MMC_RSP_OPCODE)
  182. flags |= SDHCI_CMD_INDEX;
  183. if (data)
  184. flags |= SDHCI_CMD_DATA;
  185. /* Set Transfer mode regarding to data flag */
  186. if (data) {
  187. sdhci_writeb(host, 0xe, SDHCI_TIMEOUT_CONTROL);
  188. mode = SDHCI_TRNS_BLK_CNT_EN;
  189. trans_bytes = data->blocks * data->blocksize;
  190. if (data->blocks > 1)
  191. mode |= SDHCI_TRNS_MULTI;
  192. if (data->flags == MMC_DATA_READ)
  193. mode |= SDHCI_TRNS_READ;
  194. #ifdef CONFIG_MMC_SDHCI_SDMA
  195. if (data->flags == MMC_DATA_READ)
  196. start_addr = (unsigned long)data->dest;
  197. else
  198. start_addr = (unsigned long)data->src;
  199. if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) &&
  200. (start_addr & 0x7) != 0x0) {
  201. is_aligned = 0;
  202. start_addr = (unsigned long)aligned_buffer;
  203. if (data->flags != MMC_DATA_READ)
  204. memcpy(aligned_buffer, data->src, trans_bytes);
  205. }
  206. #if defined(CONFIG_FIXED_SDHCI_ALIGNED_BUFFER)
  207. /*
  208. * Always use this bounce-buffer when
  209. * CONFIG_FIXED_SDHCI_ALIGNED_BUFFER is defined
  210. */
  211. is_aligned = 0;
  212. start_addr = (unsigned long)aligned_buffer;
  213. if (data->flags != MMC_DATA_READ)
  214. memcpy(aligned_buffer, data->src, trans_bytes);
  215. #endif
  216. sdhci_writel(host, start_addr, SDHCI_DMA_ADDRESS);
  217. mode |= SDHCI_TRNS_DMA;
  218. #endif
  219. sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
  220. data->blocksize),
  221. SDHCI_BLOCK_SIZE);
  222. sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
  223. sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
  224. } else if (cmd->resp_type & MMC_RSP_BUSY) {
  225. sdhci_writeb(host, 0xe, SDHCI_TIMEOUT_CONTROL);
  226. }
  227. sdhci_writel(host, cmd->cmdarg, SDHCI_ARGUMENT);
  228. #ifdef CONFIG_MMC_SDHCI_SDMA
  229. if (data) {
  230. trans_bytes = ALIGN(trans_bytes, CONFIG_SYS_CACHELINE_SIZE);
  231. flush_cache(start_addr, trans_bytes);
  232. }
  233. #endif
  234. sdhci_writew(host, SDHCI_MAKE_CMD(cmd->cmdidx, flags), SDHCI_COMMAND);
  235. start = get_timer(0);
  236. do {
  237. stat = sdhci_readl(host, SDHCI_INT_STATUS);
  238. if (stat & SDHCI_INT_ERROR)
  239. break;
  240. if (get_timer(start) >= SDHCI_READ_STATUS_TIMEOUT) {
  241. if (host->quirks & SDHCI_QUIRK_BROKEN_R1B) {
  242. return 0;
  243. } else {
  244. printf("%s: Timeout for status update!\n",
  245. __func__);
  246. return -ETIMEDOUT;
  247. }
  248. }
  249. } while ((stat & mask) != mask);
  250. if ((stat & (SDHCI_INT_ERROR | mask)) == mask) {
  251. sdhci_cmd_done(host, cmd);
  252. sdhci_writel(host, mask, SDHCI_INT_STATUS);
  253. } else
  254. ret = -1;
  255. if (!ret && data)
  256. ret = sdhci_transfer_data(host, data, start_addr);
  257. if (host->quirks & SDHCI_QUIRK_WAIT_SEND_CMD)
  258. udelay(1000);
  259. stat = sdhci_readl(host, SDHCI_INT_STATUS);
  260. sdhci_writel(host, SDHCI_INT_ALL_MASK, SDHCI_INT_STATUS);
  261. if (!ret) {
  262. if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) &&
  263. !is_aligned && (data->flags == MMC_DATA_READ))
  264. memcpy(data->dest, aligned_buffer, trans_bytes);
  265. return 0;
  266. }
  267. sdhci_reset(host, SDHCI_RESET_CMD);
  268. sdhci_reset(host, SDHCI_RESET_DATA);
  269. if (stat & SDHCI_INT_TIMEOUT)
  270. return -ETIMEDOUT;
  271. else
  272. return -ECOMM;
  273. }
  274. static int sdhci_set_clock(struct mmc *mmc, unsigned int clock)
  275. {
  276. struct sdhci_host *host = mmc->priv;
  277. unsigned int div, clk = 0, timeout;
  278. /* Wait max 20 ms */
  279. timeout = 200;
  280. while (sdhci_readl(host, SDHCI_PRESENT_STATE) &
  281. (SDHCI_CMD_INHIBIT | SDHCI_DATA_INHIBIT)) {
  282. if (timeout == 0) {
  283. printf("%s: Timeout to wait cmd & data inhibit\n",
  284. __func__);
  285. return -EBUSY;
  286. }
  287. timeout--;
  288. udelay(100);
  289. }
  290. sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
  291. if (clock == 0)
  292. return 0;
  293. if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) {
  294. /*
  295. * Check if the Host Controller supports Programmable Clock
  296. * Mode.
  297. */
  298. if (host->clk_mul) {
  299. for (div = 1; div <= 1024; div++) {
  300. if ((host->max_clk / div) <= clock)
  301. break;
  302. }
  303. /*
  304. * Set Programmable Clock Mode in the Clock
  305. * Control register.
  306. */
  307. clk = SDHCI_PROG_CLOCK_MODE;
  308. div--;
  309. } else {
  310. /* Version 3.00 divisors must be a multiple of 2. */
  311. if (host->max_clk <= clock) {
  312. div = 1;
  313. } else {
  314. for (div = 2;
  315. div < SDHCI_MAX_DIV_SPEC_300;
  316. div += 2) {
  317. if ((host->max_clk / div) <= clock)
  318. break;
  319. }
  320. }
  321. div >>= 1;
  322. }
  323. } else {
  324. /* Version 2.00 divisors must be a power of 2. */
  325. for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
  326. if ((host->max_clk / div) <= clock)
  327. break;
  328. }
  329. div >>= 1;
  330. }
  331. if (host->ops && host->ops->set_clock)
  332. host->ops->set_clock(host, div);
  333. clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
  334. clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
  335. << SDHCI_DIVIDER_HI_SHIFT;
  336. clk |= SDHCI_CLOCK_INT_EN;
  337. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  338. /* Wait max 20 ms */
  339. timeout = 20;
  340. while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
  341. & SDHCI_CLOCK_INT_STABLE)) {
  342. if (timeout == 0) {
  343. printf("%s: Internal clock never stabilised.\n",
  344. __func__);
  345. return -EBUSY;
  346. }
  347. timeout--;
  348. udelay(1000);
  349. }
  350. clk |= SDHCI_CLOCK_CARD_EN;
  351. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  352. return 0;
  353. }
  354. static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
  355. {
  356. u8 pwr = 0;
  357. if (power != (unsigned short)-1) {
  358. switch (1 << power) {
  359. case MMC_VDD_165_195:
  360. pwr = SDHCI_POWER_180;
  361. break;
  362. case MMC_VDD_29_30:
  363. case MMC_VDD_30_31:
  364. pwr = SDHCI_POWER_300;
  365. break;
  366. case MMC_VDD_32_33:
  367. case MMC_VDD_33_34:
  368. pwr = SDHCI_POWER_330;
  369. break;
  370. }
  371. }
  372. if (pwr == 0) {
  373. sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
  374. return;
  375. }
  376. pwr |= SDHCI_POWER_ON;
  377. sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
  378. }
  379. #ifdef CONFIG_DM_MMC
  380. static int sdhci_set_ios(struct udevice *dev)
  381. {
  382. struct mmc *mmc = mmc_get_mmc_dev(dev);
  383. #else
  384. static int sdhci_set_ios(struct mmc *mmc)
  385. {
  386. #endif
  387. u32 ctrl;
  388. struct sdhci_host *host = mmc->priv;
  389. if (host->ops && host->ops->set_control_reg)
  390. host->ops->set_control_reg(host);
  391. if (mmc->clock != host->clock)
  392. sdhci_set_clock(mmc, mmc->clock);
  393. /* Set bus width */
  394. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  395. if (mmc->bus_width == 8) {
  396. ctrl &= ~SDHCI_CTRL_4BITBUS;
  397. if ((SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) ||
  398. (host->quirks & SDHCI_QUIRK_USE_WIDE8))
  399. ctrl |= SDHCI_CTRL_8BITBUS;
  400. } else {
  401. if ((SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) ||
  402. (host->quirks & SDHCI_QUIRK_USE_WIDE8))
  403. ctrl &= ~SDHCI_CTRL_8BITBUS;
  404. if (mmc->bus_width == 4)
  405. ctrl |= SDHCI_CTRL_4BITBUS;
  406. else
  407. ctrl &= ~SDHCI_CTRL_4BITBUS;
  408. }
  409. if (mmc->clock > 26000000)
  410. ctrl |= SDHCI_CTRL_HISPD;
  411. else
  412. ctrl &= ~SDHCI_CTRL_HISPD;
  413. if (host->quirks & SDHCI_QUIRK_NO_HISPD_BIT)
  414. ctrl &= ~SDHCI_CTRL_HISPD;
  415. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  416. /* If available, call the driver specific "post" set_ios() function */
  417. if (host->ops && host->ops->set_ios_post)
  418. host->ops->set_ios_post(host);
  419. return 0;
  420. }
  421. static int sdhci_init(struct mmc *mmc)
  422. {
  423. struct sdhci_host *host = mmc->priv;
  424. sdhci_reset(host, SDHCI_RESET_ALL);
  425. if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) && !aligned_buffer) {
  426. aligned_buffer = memalign(8, 512*1024);
  427. if (!aligned_buffer) {
  428. printf("%s: Aligned buffer alloc failed!!!\n",
  429. __func__);
  430. return -ENOMEM;
  431. }
  432. }
  433. sdhci_set_power(host, fls(mmc->cfg->voltages) - 1);
  434. if (host->ops && host->ops->get_cd)
  435. host->ops->get_cd(host);
  436. /* Enable only interrupts served by the SD controller */
  437. sdhci_writel(host, SDHCI_INT_DATA_MASK | SDHCI_INT_CMD_MASK,
  438. SDHCI_INT_ENABLE);
  439. /* Mask all sdhci interrupt sources */
  440. sdhci_writel(host, 0x0, SDHCI_SIGNAL_ENABLE);
  441. return 0;
  442. }
  443. #ifdef CONFIG_DM_MMC
  444. int sdhci_probe(struct udevice *dev)
  445. {
  446. struct mmc *mmc = mmc_get_mmc_dev(dev);
  447. return sdhci_init(mmc);
  448. }
  449. const struct dm_mmc_ops sdhci_ops = {
  450. .send_cmd = sdhci_send_command,
  451. .set_ios = sdhci_set_ios,
  452. };
  453. #else
  454. static const struct mmc_ops sdhci_ops = {
  455. .send_cmd = sdhci_send_command,
  456. .set_ios = sdhci_set_ios,
  457. .init = sdhci_init,
  458. };
  459. #endif
  460. int sdhci_setup_cfg(struct mmc_config *cfg, struct sdhci_host *host,
  461. u32 f_max, u32 f_min)
  462. {
  463. u32 caps, caps_1;
  464. caps = sdhci_readl(host, SDHCI_CAPABILITIES);
  465. #ifdef CONFIG_MMC_SDHCI_SDMA
  466. if (!(caps & SDHCI_CAN_DO_SDMA)) {
  467. printf("%s: Your controller doesn't support SDMA!!\n",
  468. __func__);
  469. return -EINVAL;
  470. }
  471. #endif
  472. if (host->quirks & SDHCI_QUIRK_REG32_RW)
  473. host->version =
  474. sdhci_readl(host, SDHCI_HOST_VERSION - 2) >> 16;
  475. else
  476. host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
  477. cfg->name = host->name;
  478. #ifndef CONFIG_DM_MMC
  479. cfg->ops = &sdhci_ops;
  480. #endif
  481. /* Check whether the clock multiplier is supported or not */
  482. if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) {
  483. caps_1 = sdhci_readl(host, SDHCI_CAPABILITIES_1);
  484. host->clk_mul = (caps_1 & SDHCI_CLOCK_MUL_MASK) >>
  485. SDHCI_CLOCK_MUL_SHIFT;
  486. }
  487. if (host->max_clk == 0) {
  488. if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300)
  489. host->max_clk = (caps & SDHCI_CLOCK_V3_BASE_MASK) >>
  490. SDHCI_CLOCK_BASE_SHIFT;
  491. else
  492. host->max_clk = (caps & SDHCI_CLOCK_BASE_MASK) >>
  493. SDHCI_CLOCK_BASE_SHIFT;
  494. host->max_clk *= 1000000;
  495. if (host->clk_mul)
  496. host->max_clk *= host->clk_mul;
  497. }
  498. if (host->max_clk == 0) {
  499. printf("%s: Hardware doesn't specify base clock frequency\n",
  500. __func__);
  501. return -EINVAL;
  502. }
  503. if (f_max && (f_max < host->max_clk))
  504. cfg->f_max = f_max;
  505. else
  506. cfg->f_max = host->max_clk;
  507. if (f_min)
  508. cfg->f_min = f_min;
  509. else {
  510. if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300)
  511. cfg->f_min = cfg->f_max / SDHCI_MAX_DIV_SPEC_300;
  512. else
  513. cfg->f_min = cfg->f_max / SDHCI_MAX_DIV_SPEC_200;
  514. }
  515. cfg->voltages = 0;
  516. if (caps & SDHCI_CAN_VDD_330)
  517. cfg->voltages |= MMC_VDD_32_33 | MMC_VDD_33_34;
  518. if (caps & SDHCI_CAN_VDD_300)
  519. cfg->voltages |= MMC_VDD_29_30 | MMC_VDD_30_31;
  520. if (caps & SDHCI_CAN_VDD_180)
  521. cfg->voltages |= MMC_VDD_165_195;
  522. if (host->quirks & SDHCI_QUIRK_BROKEN_VOLTAGE)
  523. cfg->voltages |= host->voltages;
  524. cfg->host_caps |= MMC_MODE_HS | MMC_MODE_HS_52MHz | MMC_MODE_4BIT;
  525. /* Since Host Controller Version3.0 */
  526. if (SDHCI_GET_VERSION(host) >= SDHCI_SPEC_300) {
  527. if (!(caps & SDHCI_CAN_DO_8BIT))
  528. cfg->host_caps &= ~MMC_MODE_8BIT;
  529. }
  530. if (host->host_caps)
  531. cfg->host_caps |= host->host_caps;
  532. cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
  533. return 0;
  534. }
  535. #ifdef CONFIG_BLK
  536. int sdhci_bind(struct udevice *dev, struct mmc *mmc, struct mmc_config *cfg)
  537. {
  538. return mmc_bind(dev, mmc, cfg);
  539. }
  540. #else
  541. int add_sdhci(struct sdhci_host *host, u32 f_max, u32 f_min)
  542. {
  543. int ret;
  544. ret = sdhci_setup_cfg(&host->cfg, host, f_max, f_min);
  545. if (ret)
  546. return ret;
  547. host->mmc = mmc_create(&host->cfg, host);
  548. if (host->mmc == NULL) {
  549. printf("%s: mmc create fail!\n", __func__);
  550. return -ENOMEM;
  551. }
  552. return 0;
  553. }
  554. #endif