exynos_dw_mmc.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * (C) Copyright 2012 SAMSUNG Electronics
  3. * Jaehoon Chung <jh80.chung@samsung.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. #include <common.h>
  21. #include <dwmmc.h>
  22. #include <fdtdec.h>
  23. #include <libfdt.h>
  24. #include <malloc.h>
  25. #include <asm/arch/dwmmc.h>
  26. #include <asm/arch/clk.h>
  27. #include <asm/arch/pinmux.h>
  28. #define DWMMC_MAX_CH_NUM 4
  29. #define DWMMC_MAX_FREQ 52000000
  30. #define DWMMC_MIN_FREQ 400000
  31. #define DWMMC_MMC0_CLKSEL_VAL 0x03030001
  32. #define DWMMC_MMC2_CLKSEL_VAL 0x03020001
  33. /*
  34. * Function used as callback function to initialise the
  35. * CLKSEL register for every mmc channel.
  36. */
  37. static void exynos_dwmci_clksel(struct dwmci_host *host)
  38. {
  39. dwmci_writel(host, DWMCI_CLKSEL, host->clksel_val);
  40. }
  41. unsigned int exynos_dwmci_get_clk(int dev_index)
  42. {
  43. return get_mmc_clk(dev_index);
  44. }
  45. /*
  46. * This function adds the mmc channel to be registered with mmc core.
  47. * index - mmc channel number.
  48. * regbase - register base address of mmc channel specified in 'index'.
  49. * bus_width - operating bus width of mmc channel specified in 'index'.
  50. * clksel - value to be written into CLKSEL register in case of FDT.
  51. * NULL in case od non-FDT.
  52. */
  53. int exynos_dwmci_add_port(int index, u32 regbase, int bus_width, u32 clksel)
  54. {
  55. struct dwmci_host *host = NULL;
  56. unsigned int div;
  57. unsigned long freq, sclk;
  58. host = malloc(sizeof(struct dwmci_host));
  59. if (!host) {
  60. printf("dwmci_host malloc fail!\n");
  61. return 1;
  62. }
  63. /* request mmc clock vlaue of 52MHz. */
  64. freq = 52000000;
  65. sclk = get_mmc_clk(index);
  66. div = DIV_ROUND_UP(sclk, freq);
  67. /* set the clock divisor for mmc */
  68. set_mmc_clk(index, div);
  69. host->name = "EXYNOS DWMMC";
  70. host->ioaddr = (void *)regbase;
  71. host->buswidth = bus_width;
  72. if (clksel) {
  73. host->clksel_val = clksel;
  74. } else {
  75. if (0 == index)
  76. host->clksel_val = DWMMC_MMC0_CLKSEL_VAL;
  77. if (2 == index)
  78. host->clksel_val = DWMMC_MMC2_CLKSEL_VAL;
  79. }
  80. host->clksel = exynos_dwmci_clksel;
  81. host->dev_index = index;
  82. host->mmc_clk = exynos_dwmci_get_clk;
  83. /* Add the mmc channel to be registered with mmc core */
  84. if (add_dwmci(host, DWMMC_MAX_FREQ, DWMMC_MIN_FREQ)) {
  85. debug("dwmmc%d registration failed\n", index);
  86. return -1;
  87. }
  88. return 0;
  89. }
  90. #ifdef CONFIG_OF_CONTROL
  91. int exynos_dwmmc_init(const void *blob)
  92. {
  93. int index, bus_width;
  94. int node_list[DWMMC_MAX_CH_NUM];
  95. int err = 0, dev_id, flag, count, i;
  96. u32 clksel_val, base, timing[3];
  97. count = fdtdec_find_aliases_for_id(blob, "mmc",
  98. COMPAT_SAMSUNG_EXYNOS5_DWMMC, node_list,
  99. DWMMC_MAX_CH_NUM);
  100. for (i = 0; i < count; i++) {
  101. int node = node_list[i];
  102. if (node <= 0)
  103. continue;
  104. /* Extract device id for each mmc channel */
  105. dev_id = pinmux_decode_periph_id(blob, node);
  106. /* Get the bus width from the device node */
  107. bus_width = fdtdec_get_int(blob, node, "samsung,bus-width", 0);
  108. if (bus_width <= 0) {
  109. debug("DWMMC: Can't get bus-width\n");
  110. return -1;
  111. }
  112. if (8 == bus_width)
  113. flag = PINMUX_FLAG_8BIT_MODE;
  114. else
  115. flag = PINMUX_FLAG_NONE;
  116. /* config pinmux for each mmc channel */
  117. err = exynos_pinmux_config(dev_id, flag);
  118. if (err) {
  119. debug("DWMMC not configured\n");
  120. return err;
  121. }
  122. index = dev_id - PERIPH_ID_SDMMC0;
  123. /* Get the base address from the device node */
  124. base = fdtdec_get_addr(blob, node, "reg");
  125. if (!base) {
  126. debug("DWMMC: Can't get base address\n");
  127. return -1;
  128. }
  129. /* Extract the timing info from the node */
  130. err = fdtdec_get_int_array(blob, node, "samsung,timing",
  131. timing, 3);
  132. if (err) {
  133. debug("Can't get sdr-timings for divider\n");
  134. return -1;
  135. }
  136. clksel_val = (DWMCI_SET_SAMPLE_CLK(timing[0]) |
  137. DWMCI_SET_DRV_CLK(timing[1]) |
  138. DWMCI_SET_DIV_RATIO(timing[2]));
  139. /* Initialise each mmc channel */
  140. err = exynos_dwmci_add_port(index, base, bus_width, clksel_val);
  141. if (err)
  142. debug("dwmmc Channel-%d init failed\n", index);
  143. }
  144. return 0;
  145. }
  146. #endif