s5p_sdhci.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * (C) Copyright 2012 SAMSUNG Electronics
  3. * Jaehoon Chung <jh80.chung@samsung.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <dm.h>
  9. #include <malloc.h>
  10. #include <sdhci.h>
  11. #include <fdtdec.h>
  12. #include <libfdt.h>
  13. #include <asm/gpio.h>
  14. #include <asm/arch/mmc.h>
  15. #include <asm/arch/clk.h>
  16. #include <errno.h>
  17. #include <asm/arch/pinmux.h>
  18. #ifdef CONFIG_DM_MMC
  19. struct s5p_sdhci_plat {
  20. struct mmc_config cfg;
  21. struct mmc mmc;
  22. };
  23. DECLARE_GLOBAL_DATA_PTR;
  24. #endif
  25. static char *S5P_NAME = "SAMSUNG SDHCI";
  26. static void s5p_sdhci_set_control_reg(struct sdhci_host *host)
  27. {
  28. unsigned long val, ctrl;
  29. /*
  30. * SELCLKPADDS[17:16]
  31. * 00 = 2mA
  32. * 01 = 4mA
  33. * 10 = 7mA
  34. * 11 = 9mA
  35. */
  36. sdhci_writel(host, SDHCI_CTRL4_DRIVE_MASK(0x3), SDHCI_CONTROL4);
  37. val = sdhci_readl(host, SDHCI_CONTROL2);
  38. val &= SDHCI_CTRL2_SELBASECLK_MASK(3);
  39. val |= SDHCI_CTRL2_ENSTAASYNCCLR |
  40. SDHCI_CTRL2_ENCMDCNFMSK |
  41. SDHCI_CTRL2_ENFBCLKRX |
  42. SDHCI_CTRL2_ENCLKOUTHOLD;
  43. sdhci_writel(host, val, SDHCI_CONTROL2);
  44. /*
  45. * FCSEL3[31] FCSEL2[23] FCSEL1[15] FCSEL0[7]
  46. * FCSel[1:0] : Rx Feedback Clock Delay Control
  47. * Inverter delay means10ns delay if SDCLK 50MHz setting
  48. * 01 = Delay1 (basic delay)
  49. * 11 = Delay2 (basic delay + 2ns)
  50. * 00 = Delay3 (inverter delay)
  51. * 10 = Delay4 (inverter delay + 2ns)
  52. */
  53. val = SDHCI_CTRL3_FCSEL0 | SDHCI_CTRL3_FCSEL1;
  54. sdhci_writel(host, val, SDHCI_CONTROL3);
  55. /*
  56. * SELBASECLK[5:4]
  57. * 00/01 = HCLK
  58. * 10 = EPLL
  59. * 11 = XTI or XEXTCLK
  60. */
  61. ctrl = sdhci_readl(host, SDHCI_CONTROL2);
  62. ctrl &= ~SDHCI_CTRL2_SELBASECLK_MASK(0x3);
  63. ctrl |= SDHCI_CTRL2_SELBASECLK_MASK(0x2);
  64. sdhci_writel(host, ctrl, SDHCI_CONTROL2);
  65. }
  66. static int s5p_sdhci_core_init(struct sdhci_host *host)
  67. {
  68. host->name = S5P_NAME;
  69. host->quirks = SDHCI_QUIRK_NO_HISPD_BIT | SDHCI_QUIRK_BROKEN_VOLTAGE |
  70. SDHCI_QUIRK_32BIT_DMA_ADDR |
  71. SDHCI_QUIRK_WAIT_SEND_CMD | SDHCI_QUIRK_USE_WIDE8;
  72. host->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
  73. host->set_control_reg = &s5p_sdhci_set_control_reg;
  74. host->set_clock = set_mmc_clk;
  75. if (host->bus_width == 8)
  76. host->host_caps |= MMC_MODE_8BIT;
  77. #ifndef CONFIG_BLK
  78. return add_sdhci(host, 52000000, 400000);
  79. #else
  80. return 0;
  81. #endif
  82. }
  83. int s5p_sdhci_init(u32 regbase, int index, int bus_width)
  84. {
  85. struct sdhci_host *host = calloc(1, sizeof(struct sdhci_host));
  86. if (!host) {
  87. printf("sdhci__host allocation fail!\n");
  88. return -ENOMEM;
  89. }
  90. host->ioaddr = (void *)regbase;
  91. host->index = index;
  92. host->bus_width = bus_width;
  93. return s5p_sdhci_core_init(host);
  94. }
  95. #if CONFIG_IS_ENABLED(OF_CONTROL)
  96. struct sdhci_host sdhci_host[SDHCI_MAX_HOSTS];
  97. static int do_sdhci_init(struct sdhci_host *host)
  98. {
  99. int dev_id, flag, ret;
  100. flag = host->bus_width == 8 ? PINMUX_FLAG_8BIT_MODE : PINMUX_FLAG_NONE;
  101. dev_id = host->index + PERIPH_ID_SDMMC0;
  102. ret = exynos_pinmux_config(dev_id, flag);
  103. if (ret) {
  104. printf("external SD not configured\n");
  105. return ret;
  106. }
  107. if (dm_gpio_is_valid(&host->pwr_gpio)) {
  108. dm_gpio_set_value(&host->pwr_gpio, 1);
  109. ret = exynos_pinmux_config(dev_id, flag);
  110. if (ret) {
  111. debug("MMC not configured\n");
  112. return ret;
  113. }
  114. }
  115. if (dm_gpio_is_valid(&host->cd_gpio)) {
  116. ret = dm_gpio_get_value(&host->cd_gpio);
  117. if (ret) {
  118. debug("no SD card detected (%d)\n", ret);
  119. return -ENODEV;
  120. }
  121. }
  122. return s5p_sdhci_core_init(host);
  123. }
  124. static int sdhci_get_config(const void *blob, int node, struct sdhci_host *host)
  125. {
  126. int bus_width, dev_id;
  127. unsigned int base;
  128. /* Get device id */
  129. dev_id = pinmux_decode_periph_id(blob, node);
  130. if (dev_id < PERIPH_ID_SDMMC0 || dev_id > PERIPH_ID_SDMMC3) {
  131. debug("MMC: Can't get device id\n");
  132. return -EINVAL;
  133. }
  134. host->index = dev_id - PERIPH_ID_SDMMC0;
  135. /* Get bus width */
  136. bus_width = fdtdec_get_int(blob, node, "samsung,bus-width", 0);
  137. if (bus_width <= 0) {
  138. debug("MMC: Can't get bus-width\n");
  139. return -EINVAL;
  140. }
  141. host->bus_width = bus_width;
  142. /* Get the base address from the device node */
  143. base = fdtdec_get_addr(blob, node, "reg");
  144. if (!base) {
  145. debug("MMC: Can't get base address\n");
  146. return -EINVAL;
  147. }
  148. host->ioaddr = (void *)base;
  149. gpio_request_by_name_nodev(blob, node, "pwr-gpios", 0, &host->pwr_gpio,
  150. GPIOD_IS_OUT);
  151. gpio_request_by_name_nodev(blob, node, "cd-gpios", 0, &host->cd_gpio,
  152. GPIOD_IS_IN);
  153. return 0;
  154. }
  155. static int process_nodes(const void *blob, int node_list[], int count)
  156. {
  157. struct sdhci_host *host;
  158. int i, node, ret;
  159. int failed = 0;
  160. debug("%s: count = %d\n", __func__, count);
  161. /* build sdhci_host[] for each controller */
  162. for (i = 0; i < count; i++) {
  163. node = node_list[i];
  164. if (node <= 0)
  165. continue;
  166. host = &sdhci_host[i];
  167. ret = sdhci_get_config(blob, node, host);
  168. if (ret) {
  169. printf("%s: failed to decode dev %d (%d)\n", __func__, i, ret);
  170. failed++;
  171. continue;
  172. }
  173. ret = do_sdhci_init(host);
  174. if (ret && ret != -ENODEV) {
  175. printf("%s: failed to initialize dev %d (%d)\n", __func__, i, ret);
  176. failed++;
  177. }
  178. }
  179. /* we only consider it an error when all nodes fail */
  180. return (failed == count ? -1 : 0);
  181. }
  182. int exynos_mmc_init(const void *blob)
  183. {
  184. int count;
  185. int node_list[SDHCI_MAX_HOSTS];
  186. count = fdtdec_find_aliases_for_id(blob, "mmc",
  187. COMPAT_SAMSUNG_EXYNOS_MMC, node_list,
  188. SDHCI_MAX_HOSTS);
  189. return process_nodes(blob, node_list, count);
  190. }
  191. #endif
  192. #ifdef CONFIG_DM_MMC
  193. static int s5p_sdhci_probe(struct udevice *dev)
  194. {
  195. struct s5p_sdhci_plat *plat = dev_get_platdata(dev);
  196. struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
  197. struct sdhci_host *host = dev_get_priv(dev);
  198. int ret;
  199. ret = sdhci_get_config(gd->fdt_blob, dev->of_offset, host);
  200. if (ret)
  201. return ret;
  202. ret = do_sdhci_init(host);
  203. if (ret)
  204. return ret;
  205. ret = sdhci_setup_cfg(&plat->cfg, host, 52000000, 400000);
  206. if (ret)
  207. return ret;
  208. host->mmc = &plat->mmc;
  209. host->mmc->priv = host;
  210. host->mmc->dev = dev;
  211. upriv->mmc = host->mmc;
  212. return sdhci_probe(dev);
  213. }
  214. static int s5p_sdhci_bind(struct udevice *dev)
  215. {
  216. struct s5p_sdhci_plat *plat = dev_get_platdata(dev);
  217. int ret;
  218. ret = sdhci_bind(dev, &plat->mmc, &plat->cfg);
  219. if (ret)
  220. return ret;
  221. return 0;
  222. }
  223. static const struct udevice_id s5p_sdhci_ids[] = {
  224. { .compatible = "samsung,exynos4412-sdhci"},
  225. { }
  226. };
  227. U_BOOT_DRIVER(s5p_sdhci_drv) = {
  228. .name = "s5p_sdhci",
  229. .id = UCLASS_MMC,
  230. .of_match = s5p_sdhci_ids,
  231. .bind = s5p_sdhci_bind,
  232. .ops = &sdhci_ops,
  233. .probe = s5p_sdhci_probe,
  234. .priv_auto_alloc_size = sizeof(struct sdhci_host),
  235. .platdata_auto_alloc_size = sizeof(struct s5p_sdhci_plat),
  236. };
  237. #endif /* CONFIG_DM_MMC */