s5p_sdhci.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 void s5p_set_clock(struct sdhci_host *host, u32 div)
  67. {
  68. /* ToDo : Use the Clock Framework */
  69. set_mmc_clk(host->index, div);
  70. }
  71. static const struct sdhci_ops s5p_sdhci_ops = {
  72. .set_clock = &s5p_set_clock,
  73. .set_control_reg = &s5p_sdhci_set_control_reg,
  74. };
  75. static int s5p_sdhci_core_init(struct sdhci_host *host)
  76. {
  77. host->name = S5P_NAME;
  78. host->quirks = SDHCI_QUIRK_NO_HISPD_BIT | SDHCI_QUIRK_BROKEN_VOLTAGE |
  79. SDHCI_QUIRK_32BIT_DMA_ADDR |
  80. SDHCI_QUIRK_WAIT_SEND_CMD | SDHCI_QUIRK_USE_WIDE8;
  81. host->max_clk = 52000000;
  82. host->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
  83. host->ops = &s5p_sdhci_ops;
  84. if (host->bus_width == 8)
  85. host->host_caps |= MMC_MODE_8BIT;
  86. #ifndef CONFIG_BLK
  87. return add_sdhci(host, 0, 400000);
  88. #else
  89. return 0;
  90. #endif
  91. }
  92. int s5p_sdhci_init(u32 regbase, int index, int bus_width)
  93. {
  94. struct sdhci_host *host = calloc(1, sizeof(struct sdhci_host));
  95. if (!host) {
  96. printf("sdhci__host allocation fail!\n");
  97. return -ENOMEM;
  98. }
  99. host->ioaddr = (void *)regbase;
  100. host->index = index;
  101. host->bus_width = bus_width;
  102. return s5p_sdhci_core_init(host);
  103. }
  104. #if CONFIG_IS_ENABLED(OF_CONTROL)
  105. struct sdhci_host sdhci_host[SDHCI_MAX_HOSTS];
  106. static int do_sdhci_init(struct sdhci_host *host)
  107. {
  108. int dev_id, flag, ret;
  109. flag = host->bus_width == 8 ? PINMUX_FLAG_8BIT_MODE : PINMUX_FLAG_NONE;
  110. dev_id = host->index + PERIPH_ID_SDMMC0;
  111. ret = exynos_pinmux_config(dev_id, flag);
  112. if (ret) {
  113. printf("external SD not configured\n");
  114. return ret;
  115. }
  116. if (dm_gpio_is_valid(&host->pwr_gpio)) {
  117. dm_gpio_set_value(&host->pwr_gpio, 1);
  118. ret = exynos_pinmux_config(dev_id, flag);
  119. if (ret) {
  120. debug("MMC not configured\n");
  121. return ret;
  122. }
  123. }
  124. if (dm_gpio_is_valid(&host->cd_gpio)) {
  125. ret = dm_gpio_get_value(&host->cd_gpio);
  126. if (ret) {
  127. debug("no SD card detected (%d)\n", ret);
  128. return -ENODEV;
  129. }
  130. }
  131. return s5p_sdhci_core_init(host);
  132. }
  133. static int sdhci_get_config(const void *blob, int node, struct sdhci_host *host)
  134. {
  135. int bus_width, dev_id;
  136. unsigned int base;
  137. /* Get device id */
  138. dev_id = pinmux_decode_periph_id(blob, node);
  139. if (dev_id < PERIPH_ID_SDMMC0 || dev_id > PERIPH_ID_SDMMC3) {
  140. debug("MMC: Can't get device id\n");
  141. return -EINVAL;
  142. }
  143. host->index = dev_id - PERIPH_ID_SDMMC0;
  144. /* Get bus width */
  145. bus_width = fdtdec_get_int(blob, node, "samsung,bus-width", 0);
  146. if (bus_width <= 0) {
  147. debug("MMC: Can't get bus-width\n");
  148. return -EINVAL;
  149. }
  150. host->bus_width = bus_width;
  151. /* Get the base address from the device node */
  152. base = fdtdec_get_addr(blob, node, "reg");
  153. if (!base) {
  154. debug("MMC: Can't get base address\n");
  155. return -EINVAL;
  156. }
  157. host->ioaddr = (void *)base;
  158. gpio_request_by_name_nodev(blob, node, "pwr-gpios", 0, &host->pwr_gpio,
  159. GPIOD_IS_OUT);
  160. gpio_request_by_name_nodev(blob, node, "cd-gpios", 0, &host->cd_gpio,
  161. GPIOD_IS_IN);
  162. return 0;
  163. }
  164. static int process_nodes(const void *blob, int node_list[], int count)
  165. {
  166. struct sdhci_host *host;
  167. int i, node, ret;
  168. int failed = 0;
  169. debug("%s: count = %d\n", __func__, count);
  170. /* build sdhci_host[] for each controller */
  171. for (i = 0; i < count; i++) {
  172. node = node_list[i];
  173. if (node <= 0)
  174. continue;
  175. host = &sdhci_host[i];
  176. ret = sdhci_get_config(blob, node, host);
  177. if (ret) {
  178. printf("%s: failed to decode dev %d (%d)\n", __func__, i, ret);
  179. failed++;
  180. continue;
  181. }
  182. ret = do_sdhci_init(host);
  183. if (ret && ret != -ENODEV) {
  184. printf("%s: failed to initialize dev %d (%d)\n", __func__, i, ret);
  185. failed++;
  186. }
  187. }
  188. /* we only consider it an error when all nodes fail */
  189. return (failed == count ? -1 : 0);
  190. }
  191. int exynos_mmc_init(const void *blob)
  192. {
  193. int count;
  194. int node_list[SDHCI_MAX_HOSTS];
  195. count = fdtdec_find_aliases_for_id(blob, "mmc",
  196. COMPAT_SAMSUNG_EXYNOS_MMC, node_list,
  197. SDHCI_MAX_HOSTS);
  198. return process_nodes(blob, node_list, count);
  199. }
  200. #endif
  201. #ifdef CONFIG_DM_MMC
  202. static int s5p_sdhci_probe(struct udevice *dev)
  203. {
  204. struct s5p_sdhci_plat *plat = dev_get_platdata(dev);
  205. struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
  206. struct sdhci_host *host = dev_get_priv(dev);
  207. int ret;
  208. ret = sdhci_get_config(gd->fdt_blob, dev_of_offset(dev), host);
  209. if (ret)
  210. return ret;
  211. ret = do_sdhci_init(host);
  212. if (ret)
  213. return ret;
  214. ret = sdhci_setup_cfg(&plat->cfg, host, 0, 400000);
  215. if (ret)
  216. return ret;
  217. host->mmc = &plat->mmc;
  218. host->mmc->priv = host;
  219. host->mmc->dev = dev;
  220. upriv->mmc = host->mmc;
  221. return sdhci_probe(dev);
  222. }
  223. static int s5p_sdhci_bind(struct udevice *dev)
  224. {
  225. struct s5p_sdhci_plat *plat = dev_get_platdata(dev);
  226. int ret;
  227. ret = sdhci_bind(dev, &plat->mmc, &plat->cfg);
  228. if (ret)
  229. return ret;
  230. return 0;
  231. }
  232. static const struct udevice_id s5p_sdhci_ids[] = {
  233. { .compatible = "samsung,exynos4412-sdhci"},
  234. { }
  235. };
  236. U_BOOT_DRIVER(s5p_sdhci_drv) = {
  237. .name = "s5p_sdhci",
  238. .id = UCLASS_MMC,
  239. .of_match = s5p_sdhci_ids,
  240. .bind = s5p_sdhci_bind,
  241. .ops = &sdhci_ops,
  242. .probe = s5p_sdhci_probe,
  243. .priv_auto_alloc_size = sizeof(struct sdhci_host),
  244. .platdata_auto_alloc_size = sizeof(struct s5p_sdhci_plat),
  245. };
  246. #endif /* CONFIG_DM_MMC */