exynos_dw_mmc.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 <dwmmc.h>
  9. #include <fdtdec.h>
  10. #include <libfdt.h>
  11. #include <malloc.h>
  12. #include <asm/arch/dwmmc.h>
  13. #include <asm/arch/clk.h>
  14. #include <asm/arch/pinmux.h>
  15. #include <asm/gpio.h>
  16. #include <asm-generic/errno.h>
  17. #define DWMMC_MAX_CH_NUM 4
  18. #define DWMMC_MAX_FREQ 52000000
  19. #define DWMMC_MIN_FREQ 400000
  20. #define DWMMC_MMC0_CLKSEL_VAL 0x03030001
  21. #define DWMMC_MMC2_CLKSEL_VAL 0x03020001
  22. /*
  23. * Function used as callback function to initialise the
  24. * CLKSEL register for every mmc channel.
  25. */
  26. static void exynos_dwmci_clksel(struct dwmci_host *host)
  27. {
  28. dwmci_writel(host, DWMCI_CLKSEL, host->clksel_val);
  29. }
  30. unsigned int exynos_dwmci_get_clk(struct dwmci_host *host)
  31. {
  32. unsigned long sclk;
  33. int8_t clk_div;
  34. /*
  35. * Since SDCLKIN is divided inside controller by the DIVRATIO
  36. * value set in the CLKSEL register, we need to use the same output
  37. * clock value to calculate the CLKDIV value.
  38. * as per user manual:cclk_in = SDCLKIN / (DIVRATIO + 1)
  39. */
  40. clk_div = ((dwmci_readl(host, DWMCI_CLKSEL) >> DWMCI_DIVRATIO_BIT)
  41. & DWMCI_DIVRATIO_MASK) + 1;
  42. sclk = get_mmc_clk(host->dev_index);
  43. /*
  44. * Assume to know divider value.
  45. * When clock unit is broken, need to set "host->div"
  46. */
  47. return sclk / clk_div / (host->div + 1);
  48. }
  49. static void exynos_dwmci_board_init(struct dwmci_host *host)
  50. {
  51. if (host->quirks & DWMCI_QUIRK_DISABLE_SMU) {
  52. dwmci_writel(host, EMMCP_MPSBEGIN0, 0);
  53. dwmci_writel(host, EMMCP_SEND0, 0);
  54. dwmci_writel(host, EMMCP_CTRL0,
  55. MPSCTRL_SECURE_READ_BIT |
  56. MPSCTRL_SECURE_WRITE_BIT |
  57. MPSCTRL_NON_SECURE_READ_BIT |
  58. MPSCTRL_NON_SECURE_WRITE_BIT | MPSCTRL_VALID);
  59. }
  60. }
  61. static int exynos_dwmci_core_init(struct dwmci_host *host, int index)
  62. {
  63. unsigned int div;
  64. unsigned long freq, sclk;
  65. if (host->bus_hz)
  66. freq = host->bus_hz;
  67. else
  68. freq = DWMMC_MAX_FREQ;
  69. /* request mmc clock vlaue of 52MHz. */
  70. sclk = get_mmc_clk(index);
  71. div = DIV_ROUND_UP(sclk, freq);
  72. /* set the clock divisor for mmc */
  73. set_mmc_clk(index, div);
  74. host->name = "EXYNOS DWMMC";
  75. #ifdef CONFIG_EXYNOS5420
  76. host->quirks = DWMCI_QUIRK_DISABLE_SMU;
  77. #endif
  78. host->board_init = exynos_dwmci_board_init;
  79. if (!host->clksel_val) {
  80. if (index == 0)
  81. host->clksel_val = DWMMC_MMC0_CLKSEL_VAL;
  82. else if (index == 2)
  83. host->clksel_val = DWMMC_MMC2_CLKSEL_VAL;
  84. }
  85. host->caps = MMC_MODE_DDR_52MHz;
  86. host->clksel = exynos_dwmci_clksel;
  87. host->dev_index = index;
  88. host->get_mmc_clk = exynos_dwmci_get_clk;
  89. /* Add the mmc channel to be registered with mmc core */
  90. if (add_dwmci(host, DWMMC_MAX_FREQ, DWMMC_MIN_FREQ)) {
  91. debug("dwmmc%d registration failed\n", index);
  92. return -1;
  93. }
  94. return 0;
  95. }
  96. /*
  97. * This function adds the mmc channel to be registered with mmc core.
  98. * index - mmc channel number.
  99. * regbase - register base address of mmc channel specified in 'index'.
  100. * bus_width - operating bus width of mmc channel specified in 'index'.
  101. * clksel - value to be written into CLKSEL register in case of FDT.
  102. * NULL in case od non-FDT.
  103. */
  104. int exynos_dwmci_add_port(int index, u32 regbase, int bus_width, u32 clksel)
  105. {
  106. struct dwmci_host *host = NULL;
  107. host = malloc(sizeof(struct dwmci_host));
  108. if (!host) {
  109. error("dwmci_host malloc fail!\n");
  110. return -ENOMEM;
  111. }
  112. host->ioaddr = (void *)regbase;
  113. host->buswidth = bus_width;
  114. if (clksel)
  115. host->clksel_val = clksel;
  116. return exynos_dwmci_core_init(host, index);
  117. }
  118. #ifdef CONFIG_OF_CONTROL
  119. static struct dwmci_host dwmci_host[DWMMC_MAX_CH_NUM];
  120. static int do_dwmci_init(struct dwmci_host *host)
  121. {
  122. int index, flag, err;
  123. index = host->dev_index;
  124. flag = host->buswidth == 8 ? PINMUX_FLAG_8BIT_MODE : PINMUX_FLAG_NONE;
  125. err = exynos_pinmux_config(host->dev_id, flag);
  126. if (err) {
  127. debug("DWMMC not configure\n");
  128. return err;
  129. }
  130. return exynos_dwmci_core_init(host, index);
  131. }
  132. static int exynos_dwmci_get_config(const void *blob, int node,
  133. struct dwmci_host *host)
  134. {
  135. int err = 0;
  136. u32 base, clksel_val, timing[3];
  137. /* Extract device id for each mmc channel */
  138. host->dev_id = pinmux_decode_periph_id(blob, node);
  139. /* Get the bus width from the device node */
  140. host->buswidth = fdtdec_get_int(blob, node, "samsung,bus-width", 0);
  141. if (host->buswidth <= 0) {
  142. debug("DWMMC: Can't get bus-width\n");
  143. return -EINVAL;
  144. }
  145. host->dev_index = fdtdec_get_int(blob, node, "index", host->dev_id);
  146. if (host->dev_index == host->dev_id)
  147. host->dev_index = host->dev_id - PERIPH_ID_SDMMC0;
  148. /* Set the base address from the device node */
  149. base = fdtdec_get_addr(blob, node, "reg");
  150. if (!base) {
  151. debug("DWMMC: Can't get base address\n");
  152. return -EINVAL;
  153. }
  154. host->ioaddr = (void *)base;
  155. /* Extract the timing info from the node */
  156. err = fdtdec_get_int_array(blob, node, "samsung,timing", timing, 3);
  157. if (err) {
  158. debug("Can't get sdr-timings for devider\n");
  159. return -EINVAL;
  160. }
  161. clksel_val = (DWMCI_SET_SAMPLE_CLK(timing[0]) |
  162. DWMCI_SET_DRV_CLK(timing[1]) |
  163. DWMCI_SET_DIV_RATIO(timing[2]));
  164. if (clksel_val)
  165. host->clksel_val = clksel_val;
  166. host->fifoth_val = fdtdec_get_int(blob, node, "fifoth_val", 0);
  167. host->bus_hz = fdtdec_get_int(blob, node, "bus_hz", 0);
  168. host->div = fdtdec_get_int(blob, node, "div", 0);
  169. return 0;
  170. }
  171. static int exynos_dwmci_process_node(const void *blob,
  172. int node_list[], int count)
  173. {
  174. struct dwmci_host *host;
  175. int i, node, err;
  176. for (i = 0; i < count; i++) {
  177. node = node_list[i];
  178. if (node <= 0)
  179. continue;
  180. host = &dwmci_host[i];
  181. err = exynos_dwmci_get_config(blob, node, host);
  182. if (err) {
  183. debug("%s: failed to decode dev %d\n", __func__, i);
  184. return err;
  185. }
  186. do_dwmci_init(host);
  187. }
  188. return 0;
  189. }
  190. int exynos_dwmmc_init(const void *blob)
  191. {
  192. int compat_id;
  193. int node_list[DWMMC_MAX_CH_NUM];
  194. int err = 0, count;
  195. compat_id = COMPAT_SAMSUNG_EXYNOS_DWMMC;
  196. count = fdtdec_find_aliases_for_id(blob, "mmc",
  197. compat_id, node_list, DWMMC_MAX_CH_NUM);
  198. err = exynos_dwmci_process_node(blob, node_list, count);
  199. return err;
  200. }
  201. #endif