exynos_dw_mmc.c 7.5 KB

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