exynos_dw_mmc.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 int exynos_dwmci_clksel(struct dwmci_host *host)
  42. {
  43. #ifdef CONFIG_DM_MMC
  44. struct dwmci_exynos_priv_data *priv =
  45. container_of(host, struct dwmci_exynos_priv_data, host);
  46. #else
  47. struct dwmci_exynos_priv_data *priv = host->priv;
  48. #endif
  49. dwmci_writel(host, DWMCI_CLKSEL, priv->sdr_timing);
  50. return 0;
  51. }
  52. unsigned int exynos_dwmci_get_clk(struct dwmci_host *host, uint freq)
  53. {
  54. unsigned long sclk;
  55. int8_t clk_div;
  56. /*
  57. * Since SDCLKIN is divided inside controller by the DIVRATIO
  58. * value set in the CLKSEL register, we need to use the same output
  59. * clock value to calculate the CLKDIV value.
  60. * as per user manual:cclk_in = SDCLKIN / (DIVRATIO + 1)
  61. */
  62. clk_div = ((dwmci_readl(host, DWMCI_CLKSEL) >> DWMCI_DIVRATIO_BIT)
  63. & DWMCI_DIVRATIO_MASK) + 1;
  64. sclk = get_mmc_clk(host->dev_index);
  65. /*
  66. * Assume to know divider value.
  67. * When clock unit is broken, need to set "host->div"
  68. */
  69. return sclk / clk_div / (host->div + 1);
  70. }
  71. static void exynos_dwmci_board_init(struct dwmci_host *host)
  72. {
  73. struct dwmci_exynos_priv_data *priv = host->priv;
  74. if (host->quirks & DWMCI_QUIRK_DISABLE_SMU) {
  75. dwmci_writel(host, EMMCP_MPSBEGIN0, 0);
  76. dwmci_writel(host, EMMCP_SEND0, 0);
  77. dwmci_writel(host, EMMCP_CTRL0,
  78. MPSCTRL_SECURE_READ_BIT |
  79. MPSCTRL_SECURE_WRITE_BIT |
  80. MPSCTRL_NON_SECURE_READ_BIT |
  81. MPSCTRL_NON_SECURE_WRITE_BIT | MPSCTRL_VALID);
  82. }
  83. /* Set to timing value at initial time */
  84. if (priv->sdr_timing)
  85. exynos_dwmci_clksel(host);
  86. }
  87. static int exynos_dwmci_core_init(struct dwmci_host *host)
  88. {
  89. unsigned int div;
  90. unsigned long freq, sclk;
  91. if (host->bus_hz)
  92. freq = host->bus_hz;
  93. else
  94. freq = DWMMC_MAX_FREQ;
  95. /* request mmc clock vlaue of 52MHz. */
  96. sclk = get_mmc_clk(host->dev_index);
  97. div = DIV_ROUND_UP(sclk, freq);
  98. /* set the clock divisor for mmc */
  99. set_mmc_clk(host->dev_index, div);
  100. host->name = "EXYNOS DWMMC";
  101. #ifdef CONFIG_EXYNOS5420
  102. host->quirks = DWMCI_QUIRK_DISABLE_SMU;
  103. #endif
  104. host->board_init = exynos_dwmci_board_init;
  105. host->caps = MMC_MODE_DDR_52MHz;
  106. host->clksel = exynos_dwmci_clksel;
  107. host->get_mmc_clk = exynos_dwmci_get_clk;
  108. #ifndef CONFIG_DM_MMC
  109. /* Add the mmc channel to be registered with mmc core */
  110. if (add_dwmci(host, DWMMC_MAX_FREQ, DWMMC_MIN_FREQ)) {
  111. printf("DWMMC%d registration failed\n", host->dev_index);
  112. return -1;
  113. }
  114. #endif
  115. return 0;
  116. }
  117. static struct dwmci_host dwmci_host[DWMMC_MAX_CH_NUM];
  118. static int do_dwmci_init(struct dwmci_host *host)
  119. {
  120. int flag, err;
  121. flag = host->buswidth == 8 ? PINMUX_FLAG_8BIT_MODE : PINMUX_FLAG_NONE;
  122. err = exynos_pinmux_config(host->dev_id, flag);
  123. if (err) {
  124. printf("DWMMC%d not configure\n", host->dev_index);
  125. return err;
  126. }
  127. return exynos_dwmci_core_init(host);
  128. }
  129. static int exynos_dwmci_get_config(const void *blob, int node,
  130. struct dwmci_host *host,
  131. struct dwmci_exynos_priv_data *priv)
  132. {
  133. int err = 0;
  134. u32 base, timing[3];
  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. return 0;
  174. }
  175. static int exynos_dwmci_process_node(const void *blob,
  176. int node_list[], int count)
  177. {
  178. struct dwmci_exynos_priv_data *priv;
  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. priv = malloc(sizeof(struct dwmci_exynos_priv_data));
  187. if (!priv) {
  188. pr_err("dwmci_exynos_priv_data malloc fail!\n");
  189. return -ENOMEM;
  190. }
  191. err = exynos_dwmci_get_config(blob, node, host, priv);
  192. if (err) {
  193. printf("%s: failed to decode dev %d\n", __func__, i);
  194. free(priv);
  195. return err;
  196. }
  197. host->priv = priv;
  198. do_dwmci_init(host);
  199. }
  200. return 0;
  201. }
  202. int exynos_dwmmc_init(const void *blob)
  203. {
  204. int node_list[DWMMC_MAX_CH_NUM];
  205. int boot_dev_node;
  206. int err = 0, count;
  207. count = fdtdec_find_aliases_for_id(blob, "mmc",
  208. COMPAT_SAMSUNG_EXYNOS_DWMMC, node_list,
  209. DWMMC_MAX_CH_NUM);
  210. /* For DWMMC always set boot device as mmc 0 */
  211. if (count >= 3 && get_boot_mode() == BOOT_MODE_SD) {
  212. boot_dev_node = node_list[2];
  213. node_list[2] = node_list[0];
  214. node_list[0] = boot_dev_node;
  215. }
  216. err = exynos_dwmci_process_node(blob, node_list, count);
  217. return err;
  218. }
  219. #ifdef CONFIG_DM_MMC
  220. static int exynos_dwmmc_probe(struct udevice *dev)
  221. {
  222. struct exynos_mmc_plat *plat = dev_get_plat(dev);
  223. struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
  224. struct dwmci_exynos_priv_data *priv = dev_get_priv(dev);
  225. struct dwmci_host *host = &priv->host;
  226. int err;
  227. err = exynos_dwmci_get_config(gd->fdt_blob, dev_of_offset(dev), host,
  228. priv);
  229. if (err)
  230. return err;
  231. err = do_dwmci_init(host);
  232. if (err)
  233. return err;
  234. dwmci_setup_cfg(&plat->cfg, host, DWMMC_MAX_FREQ, DWMMC_MIN_FREQ);
  235. host->mmc = &plat->mmc;
  236. host->mmc->priv = &priv->host;
  237. host->priv = dev;
  238. upriv->mmc = host->mmc;
  239. return dwmci_probe(dev);
  240. }
  241. static int exynos_dwmmc_bind(struct udevice *dev)
  242. {
  243. struct exynos_mmc_plat *plat = dev_get_plat(dev);
  244. return dwmci_bind(dev, &plat->mmc, &plat->cfg);
  245. }
  246. static const struct udevice_id exynos_dwmmc_ids[] = {
  247. { .compatible = "samsung,exynos4412-dw-mshc" },
  248. { .compatible = "samsung,exynos-dwmmc" },
  249. { }
  250. };
  251. U_BOOT_DRIVER(exynos_dwmmc_drv) = {
  252. .name = "exynos_dwmmc",
  253. .id = UCLASS_MMC,
  254. .of_match = exynos_dwmmc_ids,
  255. .bind = exynos_dwmmc_bind,
  256. .ops = &dm_dwmci_ops,
  257. .probe = exynos_dwmmc_probe,
  258. .priv_auto = sizeof(struct dwmci_exynos_priv_data),
  259. .plat_auto = sizeof(struct exynos_mmc_plat),
  260. };
  261. #endif