s5p_sdhci.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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_MASK(3);
  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. if (host->bus_width == 8)
  69. host->host_caps |= MMC_MODE_8BIT;
  70. return add_sdhci(host, 52000000, 400000);
  71. }
  72. int s5p_sdhci_init(u32 regbase, int index, int bus_width)
  73. {
  74. struct sdhci_host *host = malloc(sizeof(struct sdhci_host));
  75. if (!host) {
  76. printf("sdhci__host malloc fail!\n");
  77. return 1;
  78. }
  79. host->ioaddr = (void *)regbase;
  80. host->index = index;
  81. host->bus_width = bus_width;
  82. return s5p_sdhci_core_init(host);
  83. }
  84. #ifdef CONFIG_OF_CONTROL
  85. struct sdhci_host sdhci_host[SDHCI_MAX_HOSTS];
  86. static int do_sdhci_init(struct sdhci_host *host)
  87. {
  88. int dev_id, flag;
  89. int err = 0;
  90. flag = host->bus_width == 8 ? PINMUX_FLAG_8BIT_MODE : PINMUX_FLAG_NONE;
  91. dev_id = host->index + PERIPH_ID_SDMMC0;
  92. if (dm_gpio_is_valid(&host->pwr_gpio)) {
  93. dm_gpio_set_value(&host->pwr_gpio, 1);
  94. err = exynos_pinmux_config(dev_id, flag);
  95. if (err) {
  96. debug("MMC not configured\n");
  97. return err;
  98. }
  99. }
  100. if (dm_gpio_is_valid(&host->cd_gpio)) {
  101. if (dm_gpio_get_value(&host->cd_gpio))
  102. return -ENODEV;
  103. err = exynos_pinmux_config(dev_id, flag);
  104. if (err) {
  105. printf("external SD not configured\n");
  106. return err;
  107. }
  108. }
  109. return s5p_sdhci_core_init(host);
  110. }
  111. static int sdhci_get_config(const void *blob, int node, struct sdhci_host *host)
  112. {
  113. int bus_width, dev_id;
  114. unsigned int base;
  115. /* Get device id */
  116. dev_id = pinmux_decode_periph_id(blob, node);
  117. if (dev_id < PERIPH_ID_SDMMC0 && dev_id > PERIPH_ID_SDMMC3) {
  118. debug("MMC: Can't get device id\n");
  119. return -1;
  120. }
  121. host->index = dev_id - PERIPH_ID_SDMMC0;
  122. /* Get bus width */
  123. bus_width = fdtdec_get_int(blob, node, "samsung,bus-width", 0);
  124. if (bus_width <= 0) {
  125. debug("MMC: Can't get bus-width\n");
  126. return -1;
  127. }
  128. host->bus_width = bus_width;
  129. /* Get the base address from the device node */
  130. base = fdtdec_get_addr(blob, node, "reg");
  131. if (!base) {
  132. debug("MMC: Can't get base address\n");
  133. return -1;
  134. }
  135. host->ioaddr = (void *)base;
  136. gpio_request_by_name_nodev(blob, node, "pwr-gpios", 0, &host->pwr_gpio,
  137. GPIOD_IS_OUT);
  138. gpio_request_by_name_nodev(blob, node, "cd-gpios", 0, &host->cd_gpio,
  139. GPIOD_IS_IN);
  140. return 0;
  141. }
  142. static int process_nodes(const void *blob, int node_list[], int count)
  143. {
  144. struct sdhci_host *host;
  145. int i, node;
  146. debug("%s: count = %d\n", __func__, count);
  147. /* build sdhci_host[] for each controller */
  148. for (i = 0; i < count; i++) {
  149. node = node_list[i];
  150. if (node <= 0)
  151. continue;
  152. host = &sdhci_host[i];
  153. if (sdhci_get_config(blob, node, host)) {
  154. printf("%s: failed to decode dev %d\n", __func__, i);
  155. return -1;
  156. }
  157. do_sdhci_init(host);
  158. }
  159. return 0;
  160. }
  161. int exynos_mmc_init(const void *blob)
  162. {
  163. int count;
  164. int node_list[SDHCI_MAX_HOSTS];
  165. count = fdtdec_find_aliases_for_id(blob, "mmc",
  166. COMPAT_SAMSUNG_EXYNOS_MMC, node_list,
  167. SDHCI_MAX_HOSTS);
  168. process_nodes(blob, node_list, count);
  169. return 1;
  170. }
  171. #endif