exynos_dw_mmc.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 <errno.h>
  13. #include <asm/arch/dwmmc.h>
  14. #include <asm/arch/clk.h>
  15. #include <asm/arch/pinmux.h>
  16. #include <asm/arch/power.h>
  17. #include <asm/gpio.h>
  18. #define DWMMC_MAX_CH_NUM 4
  19. #define DWMMC_MAX_FREQ 52000000
  20. #define DWMMC_MIN_FREQ 400000
  21. #define DWMMC_MMC0_SDR_TIMING_VAL 0x03030001
  22. #define DWMMC_MMC2_SDR_TIMING_VAL 0x03020001
  23. #ifdef CONFIG_DM_MMC
  24. #include <dm.h>
  25. DECLARE_GLOBAL_DATA_PTR;
  26. struct exynos_mmc_plat {
  27. struct mmc_config cfg;
  28. struct mmc mmc;
  29. };
  30. #endif
  31. /* Exynos implmentation specific drver private data */
  32. struct dwmci_exynos_priv_data {
  33. #ifdef CONFIG_DM_MMC
  34. struct dwmci_host host;
  35. #endif
  36. u32 sdr_timing;
  37. };
  38. /*
  39. * Function used as callback function to initialise the
  40. * CLKSEL register for every mmc channel.
  41. */
  42. static void exynos_dwmci_clksel(struct dwmci_host *host)
  43. {
  44. struct dwmci_exynos_priv_data *priv = host->priv;
  45. dwmci_writel(host, DWMCI_CLKSEL, priv->sdr_timing);
  46. }
  47. unsigned int exynos_dwmci_get_clk(struct dwmci_host *host, uint freq)
  48. {
  49. unsigned long sclk;
  50. int8_t clk_div;
  51. /*
  52. * Since SDCLKIN is divided inside controller by the DIVRATIO
  53. * value set in the CLKSEL register, we need to use the same output
  54. * clock value to calculate the CLKDIV value.
  55. * as per user manual:cclk_in = SDCLKIN / (DIVRATIO + 1)
  56. */
  57. clk_div = ((dwmci_readl(host, DWMCI_CLKSEL) >> DWMCI_DIVRATIO_BIT)
  58. & DWMCI_DIVRATIO_MASK) + 1;
  59. sclk = get_mmc_clk(host->dev_index);
  60. /*
  61. * Assume to know divider value.
  62. * When clock unit is broken, need to set "host->div"
  63. */
  64. return sclk / clk_div / (host->div + 1);
  65. }
  66. static void exynos_dwmci_board_init(struct dwmci_host *host)
  67. {
  68. struct dwmci_exynos_priv_data *priv = host->priv;
  69. if (host->quirks & DWMCI_QUIRK_DISABLE_SMU) {
  70. dwmci_writel(host, EMMCP_MPSBEGIN0, 0);
  71. dwmci_writel(host, EMMCP_SEND0, 0);
  72. dwmci_writel(host, EMMCP_CTRL0,
  73. MPSCTRL_SECURE_READ_BIT |
  74. MPSCTRL_SECURE_WRITE_BIT |
  75. MPSCTRL_NON_SECURE_READ_BIT |
  76. MPSCTRL_NON_SECURE_WRITE_BIT | MPSCTRL_VALID);
  77. }
  78. /* Set to timing value at initial time */
  79. if (priv->sdr_timing)
  80. exynos_dwmci_clksel(host);
  81. }
  82. static int exynos_dwmci_core_init(struct dwmci_host *host)
  83. {
  84. unsigned int div;
  85. unsigned long freq, sclk;
  86. if (host->bus_hz)
  87. freq = host->bus_hz;
  88. else
  89. freq = DWMMC_MAX_FREQ;
  90. /* request mmc clock vlaue of 52MHz. */
  91. sclk = get_mmc_clk(host->dev_index);
  92. div = DIV_ROUND_UP(sclk, freq);
  93. /* set the clock divisor for mmc */
  94. set_mmc_clk(host->dev_index, div);
  95. host->name = "EXYNOS DWMMC";
  96. #ifdef CONFIG_EXYNOS5420
  97. host->quirks = DWMCI_QUIRK_DISABLE_SMU;
  98. #endif
  99. host->board_init = exynos_dwmci_board_init;
  100. host->caps = MMC_MODE_DDR_52MHz;
  101. host->clksel = exynos_dwmci_clksel;
  102. host->get_mmc_clk = exynos_dwmci_get_clk;
  103. #ifndef CONFIG_DM_MMC
  104. /* Add the mmc channel to be registered with mmc core */
  105. if (add_dwmci(host, DWMMC_MAX_FREQ, DWMMC_MIN_FREQ)) {
  106. printf("DWMMC%d registration failed\n", host->dev_index);
  107. return -1;
  108. }
  109. #endif
  110. return 0;
  111. }
  112. static struct dwmci_host dwmci_host[DWMMC_MAX_CH_NUM];
  113. static int do_dwmci_init(struct dwmci_host *host)
  114. {
  115. int flag, err;
  116. flag = host->buswidth == 8 ? PINMUX_FLAG_8BIT_MODE : PINMUX_FLAG_NONE;
  117. err = exynos_pinmux_config(host->dev_id, flag);
  118. if (err) {
  119. printf("DWMMC%d not configure\n", host->dev_index);
  120. return err;
  121. }
  122. return exynos_dwmci_core_init(host);
  123. }
  124. static int exynos_dwmci_get_config(const void *blob, int node,
  125. struct dwmci_host *host)
  126. {
  127. int err = 0;
  128. u32 base, timing[3];
  129. struct dwmci_exynos_priv_data *priv;
  130. priv = malloc(sizeof(struct dwmci_exynos_priv_data));
  131. if (!priv) {
  132. error("dwmci_exynos_priv_data malloc fail!\n");
  133. return -ENOMEM;
  134. }
  135. /* Extract device id for each mmc channel */
  136. host->dev_id = pinmux_decode_periph_id(blob, node);
  137. host->dev_index = fdtdec_get_int(blob, node, "index", host->dev_id);
  138. if (host->dev_index == host->dev_id)
  139. host->dev_index = host->dev_id - PERIPH_ID_SDMMC0;
  140. if (host->dev_index > 4) {
  141. printf("DWMMC%d: Can't get the dev index\n", host->dev_index);
  142. return -EINVAL;
  143. }
  144. /* Get the bus width from the device node (Default is 4bit buswidth) */
  145. host->buswidth = fdtdec_get_int(blob, node, "samsung,bus-width", 4);
  146. /* Set the base address from the device node */
  147. base = fdtdec_get_addr(blob, node, "reg");
  148. if (!base) {
  149. printf("DWMMC%d: Can't get base address\n", host->dev_index);
  150. return -EINVAL;
  151. }
  152. host->ioaddr = (void *)base;
  153. /* Extract the timing info from the node */
  154. err = fdtdec_get_int_array(blob, node, "samsung,timing", timing, 3);
  155. if (err) {
  156. printf("DWMMC%d: Can't get sdr-timings for devider\n",
  157. host->dev_index);
  158. return -EINVAL;
  159. }
  160. priv->sdr_timing = (DWMCI_SET_SAMPLE_CLK(timing[0]) |
  161. DWMCI_SET_DRV_CLK(timing[1]) |
  162. DWMCI_SET_DIV_RATIO(timing[2]));
  163. /* sdr_timing didn't assigned anything, use the default value */
  164. if (!priv->sdr_timing) {
  165. if (host->dev_index == 0)
  166. priv->sdr_timing = DWMMC_MMC0_SDR_TIMING_VAL;
  167. else if (host->dev_index == 2)
  168. priv->sdr_timing = DWMMC_MMC2_SDR_TIMING_VAL;
  169. }
  170. host->fifoth_val = fdtdec_get_int(blob, node, "fifoth_val", 0);
  171. host->bus_hz = fdtdec_get_int(blob, node, "bus_hz", 0);
  172. host->div = fdtdec_get_int(blob, node, "div", 0);
  173. host->priv = priv;
  174. return 0;
  175. }
  176. static int exynos_dwmci_process_node(const void *blob,
  177. int node_list[], int count)
  178. {
  179. struct dwmci_host *host;
  180. int i, node, err;
  181. for (i = 0; i < count; i++) {
  182. node = node_list[i];
  183. if (node <= 0)
  184. continue;
  185. host = &dwmci_host[i];
  186. err = exynos_dwmci_get_config(blob, node, host);
  187. if (err) {
  188. printf("%s: failed to decode dev %d\n", __func__, i);
  189. return err;
  190. }
  191. do_dwmci_init(host);
  192. }
  193. return 0;
  194. }
  195. int exynos_dwmmc_init(const void *blob)
  196. {
  197. int node_list[DWMMC_MAX_CH_NUM];
  198. int boot_dev_node;
  199. int err = 0, count;
  200. count = fdtdec_find_aliases_for_id(blob, "mmc",
  201. COMPAT_SAMSUNG_EXYNOS_DWMMC, node_list,
  202. DWMMC_MAX_CH_NUM);
  203. /* For DWMMC always set boot device as mmc 0 */
  204. if (count >= 3 && get_boot_mode() == BOOT_MODE_SD) {
  205. boot_dev_node = node_list[2];
  206. node_list[2] = node_list[0];
  207. node_list[0] = boot_dev_node;
  208. }
  209. err = exynos_dwmci_process_node(blob, node_list, count);
  210. return err;
  211. }
  212. #ifdef CONFIG_DM_MMC
  213. static int exynos_dwmmc_probe(struct udevice *dev)
  214. {
  215. struct exynos_mmc_plat *plat = dev_get_platdata(dev);
  216. struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
  217. struct dwmci_exynos_priv_data *priv = dev_get_priv(dev);
  218. struct dwmci_host *host = &priv->host;
  219. int err;
  220. err = exynos_dwmci_get_config(gd->fdt_blob, dev->of_offset, host);
  221. if (err)
  222. return err;
  223. err = do_dwmci_init(host);
  224. if (err)
  225. return err;
  226. dwmci_setup_cfg(&plat->cfg, host, DWMMC_MAX_FREQ, DWMMC_MIN_FREQ);
  227. host->mmc = &plat->mmc;
  228. host->mmc->priv = &priv->host;
  229. host->priv = dev;
  230. upriv->mmc = host->mmc;
  231. return dwmci_probe(dev);
  232. }
  233. static int exynos_dwmmc_bind(struct udevice *dev)
  234. {
  235. struct exynos_mmc_plat *plat = dev_get_platdata(dev);
  236. return dwmci_bind(dev, &plat->mmc, &plat->cfg);
  237. }
  238. static const struct udevice_id exynos_dwmmc_ids[] = {
  239. { .compatible = "samsung,exynos4412-dw-mshc" },
  240. { }
  241. };
  242. U_BOOT_DRIVER(exynos_dwmmc_drv) = {
  243. .name = "exynos_dwmmc",
  244. .id = UCLASS_MMC,
  245. .of_match = exynos_dwmmc_ids,
  246. .bind = exynos_dwmmc_bind,
  247. .ops = &dm_dwmci_ops,
  248. .probe = exynos_dwmmc_probe,
  249. .priv_auto_alloc_size = sizeof(struct dwmci_exynos_priv_data),
  250. .platdata_auto_alloc_size = sizeof(struct exynos_mmc_plat),
  251. };
  252. #endif