rockchip_dw_mmc.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2013 Google, Inc
  4. */
  5. #include <common.h>
  6. #include <clk.h>
  7. #include <dm.h>
  8. #include <dt-structs.h>
  9. #include <dwmmc.h>
  10. #include <errno.h>
  11. #include <log.h>
  12. #include <mapmem.h>
  13. #include <pwrseq.h>
  14. #include <syscon.h>
  15. #include <asm/gpio.h>
  16. #include <asm/arch-rockchip/clock.h>
  17. #include <asm/arch-rockchip/periph.h>
  18. #include <linux/delay.h>
  19. #include <linux/err.h>
  20. struct rockchip_mmc_plat {
  21. #if CONFIG_IS_ENABLED(OF_PLATDATA)
  22. struct dtd_rockchip_rk3288_dw_mshc dtplat;
  23. #endif
  24. struct mmc_config cfg;
  25. struct mmc mmc;
  26. };
  27. struct rockchip_dwmmc_priv {
  28. struct clk clk;
  29. struct dwmci_host host;
  30. int fifo_depth;
  31. bool fifo_mode;
  32. u32 minmax[2];
  33. };
  34. static uint rockchip_dwmmc_get_mmc_clk(struct dwmci_host *host, uint freq)
  35. {
  36. struct udevice *dev = host->priv;
  37. struct rockchip_dwmmc_priv *priv = dev_get_priv(dev);
  38. int ret;
  39. /*
  40. * The clock frequency chosen here affects CLKDIV in the dw_mmc core.
  41. * That can be either 0 or 1, but it must be set to 1 for eMMC DDR52
  42. * 8-bit mode. It will be set to 0 for all other modes.
  43. */
  44. if (host->mmc->selected_mode == MMC_DDR_52 && host->mmc->bus_width == 8)
  45. freq *= 2;
  46. ret = clk_set_rate(&priv->clk, freq);
  47. if (ret < 0) {
  48. debug("%s: err=%d\n", __func__, ret);
  49. return 0;
  50. }
  51. return freq;
  52. }
  53. static int rockchip_dwmmc_of_to_plat(struct udevice *dev)
  54. {
  55. struct rockchip_dwmmc_priv *priv = dev_get_priv(dev);
  56. struct dwmci_host *host = &priv->host;
  57. if (!CONFIG_IS_ENABLED(OF_REAL))
  58. return 0;
  59. host->name = dev->name;
  60. host->ioaddr = dev_read_addr_ptr(dev);
  61. host->buswidth = dev_read_u32_default(dev, "bus-width", 4);
  62. host->get_mmc_clk = rockchip_dwmmc_get_mmc_clk;
  63. host->priv = dev;
  64. /* use non-removeable as sdcard and emmc as judgement */
  65. if (dev_read_bool(dev, "non-removable"))
  66. host->dev_index = 0;
  67. else
  68. host->dev_index = 1;
  69. priv->fifo_depth = dev_read_u32_default(dev, "fifo-depth", 0);
  70. if (priv->fifo_depth < 0)
  71. return -EINVAL;
  72. priv->fifo_mode = dev_read_bool(dev, "fifo-mode");
  73. #ifdef CONFIG_SPL_BUILD
  74. if (!priv->fifo_mode)
  75. priv->fifo_mode = dev_read_bool(dev, "u-boot,spl-fifo-mode");
  76. #endif
  77. /*
  78. * 'clock-freq-min-max' is deprecated
  79. * (see https://github.com/torvalds/linux/commit/b023030f10573de738bbe8df63d43acab64c9f7b)
  80. */
  81. if (dev_read_u32_array(dev, "clock-freq-min-max", priv->minmax, 2)) {
  82. int val = dev_read_u32_default(dev, "max-frequency", -EINVAL);
  83. if (val < 0)
  84. return val;
  85. priv->minmax[0] = 400000; /* 400 kHz */
  86. priv->minmax[1] = val;
  87. } else {
  88. debug("%s: 'clock-freq-min-max' property was deprecated.\n",
  89. __func__);
  90. }
  91. return 0;
  92. }
  93. static int rockchip_dwmmc_probe(struct udevice *dev)
  94. {
  95. struct rockchip_mmc_plat *plat = dev_get_plat(dev);
  96. struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
  97. struct rockchip_dwmmc_priv *priv = dev_get_priv(dev);
  98. struct dwmci_host *host = &priv->host;
  99. int ret;
  100. #if CONFIG_IS_ENABLED(OF_PLATDATA)
  101. struct dtd_rockchip_rk3288_dw_mshc *dtplat = &plat->dtplat;
  102. host->name = dev->name;
  103. host->ioaddr = map_sysmem(dtplat->reg[0], dtplat->reg[1]);
  104. host->buswidth = dtplat->bus_width;
  105. host->get_mmc_clk = rockchip_dwmmc_get_mmc_clk;
  106. host->priv = dev;
  107. host->dev_index = 0;
  108. priv->fifo_depth = dtplat->fifo_depth;
  109. priv->fifo_mode = dtplat->u_boot_spl_fifo_mode;
  110. priv->minmax[0] = 400000; /* 400 kHz */
  111. priv->minmax[1] = dtplat->max_frequency;
  112. ret = clk_get_by_phandle(dev, &dtplat->clocks[1], &priv->clk);
  113. if (ret < 0)
  114. return ret;
  115. #else
  116. ret = clk_get_by_index(dev, 1, &priv->clk);
  117. if (ret < 0)
  118. return ret;
  119. #endif
  120. host->fifoth_val = MSIZE(0x2) |
  121. RX_WMARK(priv->fifo_depth / 2 - 1) |
  122. TX_WMARK(priv->fifo_depth / 2);
  123. host->fifo_mode = priv->fifo_mode;
  124. #ifdef CONFIG_MMC_PWRSEQ
  125. /* Enable power if needed */
  126. ret = mmc_pwrseq_get_power(dev, &plat->cfg);
  127. if (!ret) {
  128. ret = pwrseq_set_power(plat->cfg.pwr_dev, true);
  129. if (ret)
  130. return ret;
  131. }
  132. #endif
  133. dwmci_setup_cfg(&plat->cfg, host, priv->minmax[1], priv->minmax[0]);
  134. host->mmc = &plat->mmc;
  135. host->mmc->priv = &priv->host;
  136. host->mmc->dev = dev;
  137. upriv->mmc = host->mmc;
  138. return dwmci_probe(dev);
  139. }
  140. static int rockchip_dwmmc_bind(struct udevice *dev)
  141. {
  142. struct rockchip_mmc_plat *plat = dev_get_plat(dev);
  143. return dwmci_bind(dev, &plat->mmc, &plat->cfg);
  144. }
  145. static const struct udevice_id rockchip_dwmmc_ids[] = {
  146. { .compatible = "rockchip,rk2928-dw-mshc" },
  147. { .compatible = "rockchip,rk3288-dw-mshc" },
  148. { }
  149. };
  150. U_BOOT_DRIVER(rockchip_rk3288_dw_mshc) = {
  151. .name = "rockchip_rk3288_dw_mshc",
  152. .id = UCLASS_MMC,
  153. .of_match = rockchip_dwmmc_ids,
  154. .of_to_plat = rockchip_dwmmc_of_to_plat,
  155. .ops = &dm_dwmci_ops,
  156. .bind = rockchip_dwmmc_bind,
  157. .probe = rockchip_dwmmc_probe,
  158. .priv_auto = sizeof(struct rockchip_dwmmc_priv),
  159. .plat_auto = sizeof(struct rockchip_mmc_plat),
  160. };
  161. DM_DRIVER_ALIAS(rockchip_rk3288_dw_mshc, rockchip_rk2928_dw_mshc)
  162. DM_DRIVER_ALIAS(rockchip_rk3288_dw_mshc, rockchip_rk3328_dw_mshc)
  163. DM_DRIVER_ALIAS(rockchip_rk3288_dw_mshc, rockchip_rk3368_dw_mshc)