exynos_dw_mmc.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. #define DWMMC_MAX_CH_NUM 4
  16. #define DWMMC_MAX_FREQ 52000000
  17. #define DWMMC_MIN_FREQ 400000
  18. #define DWMMC_MMC0_CLKSEL_VAL 0x03030001
  19. #define DWMMC_MMC2_CLKSEL_VAL 0x03020001
  20. /*
  21. * Function used as callback function to initialise the
  22. * CLKSEL register for every mmc channel.
  23. */
  24. static void exynos_dwmci_clksel(struct dwmci_host *host)
  25. {
  26. dwmci_writel(host, DWMCI_CLKSEL, host->clksel_val);
  27. }
  28. unsigned int exynos_dwmci_get_clk(int dev_index)
  29. {
  30. return get_mmc_clk(dev_index);
  31. }
  32. /*
  33. * This function adds the mmc channel to be registered with mmc core.
  34. * index - mmc channel number.
  35. * regbase - register base address of mmc channel specified in 'index'.
  36. * bus_width - operating bus width of mmc channel specified in 'index'.
  37. * clksel - value to be written into CLKSEL register in case of FDT.
  38. * NULL in case od non-FDT.
  39. */
  40. int exynos_dwmci_add_port(int index, u32 regbase, int bus_width, u32 clksel)
  41. {
  42. struct dwmci_host *host = NULL;
  43. unsigned int div;
  44. unsigned long freq, sclk;
  45. host = malloc(sizeof(struct dwmci_host));
  46. if (!host) {
  47. printf("dwmci_host malloc fail!\n");
  48. return 1;
  49. }
  50. /* request mmc clock vlaue of 52MHz. */
  51. freq = 52000000;
  52. sclk = get_mmc_clk(index);
  53. div = DIV_ROUND_UP(sclk, freq);
  54. /* set the clock divisor for mmc */
  55. set_mmc_clk(index, div);
  56. host->name = "EXYNOS DWMMC";
  57. host->ioaddr = (void *)regbase;
  58. host->buswidth = bus_width;
  59. #ifdef CONFIG_EXYNOS5420
  60. host->quirks = DWMCI_QUIRK_DISABLE_SMU;
  61. #endif
  62. if (clksel) {
  63. host->clksel_val = clksel;
  64. } else {
  65. if (0 == index)
  66. host->clksel_val = DWMMC_MMC0_CLKSEL_VAL;
  67. if (2 == index)
  68. host->clksel_val = DWMMC_MMC2_CLKSEL_VAL;
  69. }
  70. host->clksel = exynos_dwmci_clksel;
  71. host->dev_index = index;
  72. host->get_mmc_clk = exynos_dwmci_get_clk;
  73. /* Add the mmc channel to be registered with mmc core */
  74. if (add_dwmci(host, DWMMC_MAX_FREQ, DWMMC_MIN_FREQ)) {
  75. debug("dwmmc%d registration failed\n", index);
  76. return -1;
  77. }
  78. return 0;
  79. }
  80. #ifdef CONFIG_OF_CONTROL
  81. int exynos_dwmmc_init(const void *blob)
  82. {
  83. int index, bus_width;
  84. int node_list[DWMMC_MAX_CH_NUM];
  85. int err = 0, dev_id, flag, count, i;
  86. u32 clksel_val, base, timing[3];
  87. count = fdtdec_find_aliases_for_id(blob, "mmc",
  88. COMPAT_SAMSUNG_EXYNOS5_DWMMC, node_list,
  89. DWMMC_MAX_CH_NUM);
  90. for (i = 0; i < count; i++) {
  91. int node = node_list[i];
  92. if (node <= 0)
  93. continue;
  94. /* Extract device id for each mmc channel */
  95. dev_id = pinmux_decode_periph_id(blob, node);
  96. /* Get the bus width from the device node */
  97. bus_width = fdtdec_get_int(blob, node, "samsung,bus-width", 0);
  98. if (bus_width <= 0) {
  99. debug("DWMMC: Can't get bus-width\n");
  100. return -1;
  101. }
  102. if (8 == bus_width)
  103. flag = PINMUX_FLAG_8BIT_MODE;
  104. else
  105. flag = PINMUX_FLAG_NONE;
  106. /* config pinmux for each mmc channel */
  107. err = exynos_pinmux_config(dev_id, flag);
  108. if (err) {
  109. debug("DWMMC not configured\n");
  110. return err;
  111. }
  112. index = dev_id - PERIPH_ID_SDMMC0;
  113. /* Get the base address from the device node */
  114. base = fdtdec_get_addr(blob, node, "reg");
  115. if (!base) {
  116. debug("DWMMC: Can't get base address\n");
  117. return -1;
  118. }
  119. /* Extract the timing info from the node */
  120. err = fdtdec_get_int_array(blob, node, "samsung,timing",
  121. timing, 3);
  122. if (err) {
  123. debug("Can't get sdr-timings for divider\n");
  124. return -1;
  125. }
  126. clksel_val = (DWMCI_SET_SAMPLE_CLK(timing[0]) |
  127. DWMCI_SET_DRV_CLK(timing[1]) |
  128. DWMCI_SET_DIV_RATIO(timing[2]));
  129. /* Initialise each mmc channel */
  130. err = exynos_dwmci_add_port(index, base, bus_width, clksel_val);
  131. if (err)
  132. debug("dwmmc Channel-%d init failed\n", index);
  133. }
  134. return 0;
  135. }
  136. #endif