s5p_sdhci.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 <malloc.h>
  9. #include <sdhci.h>
  10. #include <fdtdec.h>
  11. #include <libfdt.h>
  12. #include <asm/gpio.h>
  13. #include <asm/arch/mmc.h>
  14. #include <asm/arch/clk.h>
  15. #include <errno.h>
  16. #include <asm/arch/pinmux.h>
  17. static char *S5P_NAME = "SAMSUNG SDHCI";
  18. static void s5p_sdhci_set_control_reg(struct sdhci_host *host)
  19. {
  20. unsigned long val, ctrl;
  21. /*
  22. * SELCLKPADDS[17:16]
  23. * 00 = 2mA
  24. * 01 = 4mA
  25. * 10 = 7mA
  26. * 11 = 9mA
  27. */
  28. sdhci_writel(host, SDHCI_CTRL4_DRIVE_MASK(0x3), SDHCI_CONTROL4);
  29. val = sdhci_readl(host, SDHCI_CONTROL2);
  30. val &= SDHCI_CTRL2_SELBASECLK_SHIFT;
  31. val |= SDHCI_CTRL2_ENSTAASYNCCLR |
  32. SDHCI_CTRL2_ENCMDCNFMSK |
  33. SDHCI_CTRL2_ENFBCLKRX |
  34. SDHCI_CTRL2_ENCLKOUTHOLD;
  35. sdhci_writel(host, val, SDHCI_CONTROL2);
  36. /*
  37. * FCSEL3[31] FCSEL2[23] FCSEL1[15] FCSEL0[7]
  38. * FCSel[1:0] : Rx Feedback Clock Delay Control
  39. * Inverter delay means10ns delay if SDCLK 50MHz setting
  40. * 01 = Delay1 (basic delay)
  41. * 11 = Delay2 (basic delay + 2ns)
  42. * 00 = Delay3 (inverter delay)
  43. * 10 = Delay4 (inverter delay + 2ns)
  44. */
  45. val = SDHCI_CTRL3_FCSEL0 | SDHCI_CTRL3_FCSEL1;
  46. sdhci_writel(host, val, SDHCI_CONTROL3);
  47. /*
  48. * SELBASECLK[5:4]
  49. * 00/01 = HCLK
  50. * 10 = EPLL
  51. * 11 = XTI or XEXTCLK
  52. */
  53. ctrl = sdhci_readl(host, SDHCI_CONTROL2);
  54. ctrl &= ~SDHCI_CTRL2_SELBASECLK_MASK(0x3);
  55. ctrl |= SDHCI_CTRL2_SELBASECLK_MASK(0x2);
  56. sdhci_writel(host, ctrl, SDHCI_CONTROL2);
  57. }
  58. static int s5p_sdhci_core_init(struct sdhci_host *host)
  59. {
  60. host->name = S5P_NAME;
  61. host->quirks = SDHCI_QUIRK_NO_HISPD_BIT | SDHCI_QUIRK_BROKEN_VOLTAGE |
  62. SDHCI_QUIRK_BROKEN_R1B | SDHCI_QUIRK_32BIT_DMA_ADDR |
  63. SDHCI_QUIRK_WAIT_SEND_CMD | SDHCI_QUIRK_USE_WIDE8;
  64. host->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
  65. host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
  66. host->set_control_reg = &s5p_sdhci_set_control_reg;
  67. host->set_clock = set_mmc_clk;
  68. host->host_caps = MMC_MODE_HC;
  69. if (host->bus_width == 8)
  70. host->host_caps |= MMC_MODE_8BIT;
  71. return add_sdhci(host, 52000000, 400000);
  72. }
  73. int s5p_sdhci_init(u32 regbase, int index, int bus_width)
  74. {
  75. struct sdhci_host *host = malloc(sizeof(struct sdhci_host));
  76. if (!host) {
  77. printf("sdhci__host malloc fail!\n");
  78. return 1;
  79. }
  80. host->ioaddr = (void *)regbase;
  81. host->index = index;
  82. host->bus_width = bus_width;
  83. return s5p_sdhci_core_init(host);
  84. }
  85. #ifdef CONFIG_OF_CONTROL
  86. struct sdhci_host sdhci_host[SDHCI_MAX_HOSTS];
  87. static int do_sdhci_init(struct sdhci_host *host)
  88. {
  89. char str[20];
  90. int dev_id, flag;
  91. int err = 0;
  92. flag = host->bus_width == 8 ? PINMUX_FLAG_8BIT_MODE : PINMUX_FLAG_NONE;
  93. dev_id = host->index + PERIPH_ID_SDMMC0;
  94. if (fdt_gpio_isvalid(&host->pwr_gpio)) {
  95. sprintf(str, "sdhci%d_power", host->index & 0xf);
  96. gpio_request(host->pwr_gpio.gpio, str);
  97. gpio_direction_output(host->pwr_gpio.gpio, 1);
  98. err = exynos_pinmux_config(dev_id, flag);
  99. if (err) {
  100. debug("MMC not configured\n");
  101. return err;
  102. }
  103. }
  104. if (fdt_gpio_isvalid(&host->cd_gpio)) {
  105. sprintf(str, "sdhci%d_cd", host->index & 0xf);
  106. gpio_request(host->cd_gpio.gpio, str);
  107. gpio_direction_input(host->cd_gpio.gpio);
  108. if (gpio_get_value(host->cd_gpio.gpio))
  109. return -ENODEV;
  110. err = exynos_pinmux_config(dev_id, flag);
  111. if (err) {
  112. printf("external SD not configured\n");
  113. return err;
  114. }
  115. }
  116. return s5p_sdhci_core_init(host);
  117. }
  118. static int sdhci_get_config(const void *blob, int node, struct sdhci_host *host)
  119. {
  120. int bus_width, dev_id;
  121. unsigned int base;
  122. /* Get device id */
  123. dev_id = pinmux_decode_periph_id(blob, node);
  124. if (dev_id < PERIPH_ID_SDMMC0 && dev_id > PERIPH_ID_SDMMC3) {
  125. debug("MMC: Can't get device id\n");
  126. return -1;
  127. }
  128. host->index = dev_id - PERIPH_ID_SDMMC0;
  129. /* Get bus width */
  130. bus_width = fdtdec_get_int(blob, node, "samsung,bus-width", 0);
  131. if (bus_width <= 0) {
  132. debug("MMC: Can't get bus-width\n");
  133. return -1;
  134. }
  135. host->bus_width = bus_width;
  136. /* Get the base address from the device node */
  137. base = fdtdec_get_addr(blob, node, "reg");
  138. if (!base) {
  139. debug("MMC: Can't get base address\n");
  140. return -1;
  141. }
  142. host->ioaddr = (void *)base;
  143. fdtdec_decode_gpio(blob, node, "pwr-gpios", &host->pwr_gpio);
  144. fdtdec_decode_gpio(blob, node, "cd-gpios", &host->cd_gpio);
  145. return 0;
  146. }
  147. static int process_nodes(const void *blob, int node_list[], int count)
  148. {
  149. struct sdhci_host *host;
  150. int i, node;
  151. debug("%s: count = %d\n", __func__, count);
  152. /* build sdhci_host[] for each controller */
  153. for (i = 0; i < count; i++) {
  154. node = node_list[i];
  155. if (node <= 0)
  156. continue;
  157. host = &sdhci_host[i];
  158. if (sdhci_get_config(blob, node, host)) {
  159. printf("%s: failed to decode dev %d\n", __func__, i);
  160. return -1;
  161. }
  162. do_sdhci_init(host);
  163. }
  164. return 0;
  165. }
  166. int exynos_mmc_init(const void *blob)
  167. {
  168. int count;
  169. int node_list[SDHCI_MAX_HOSTS];
  170. count = fdtdec_find_aliases_for_id(blob, "mmc",
  171. COMPAT_SAMSUNG_EXYNOS_MMC, node_list,
  172. SDHCI_MAX_HOSTS);
  173. process_nodes(blob, node_list, count);
  174. return 1;
  175. }
  176. #endif