rockchip_dw_mmc.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright (c) 2013 Google, Inc
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <clk.h>
  8. #include <dm.h>
  9. #include <dwmmc.h>
  10. #include <errno.h>
  11. #include <pwrseq.h>
  12. #include <syscon.h>
  13. #include <asm/gpio.h>
  14. #include <asm/arch/clock.h>
  15. #include <asm/arch/periph.h>
  16. #include <linux/err.h>
  17. DECLARE_GLOBAL_DATA_PTR;
  18. struct rockchip_mmc_plat {
  19. struct mmc_config cfg;
  20. struct mmc mmc;
  21. };
  22. struct rockchip_dwmmc_priv {
  23. struct clk clk;
  24. struct dwmci_host host;
  25. };
  26. static uint rockchip_dwmmc_get_mmc_clk(struct dwmci_host *host, uint freq)
  27. {
  28. struct udevice *dev = host->priv;
  29. struct rockchip_dwmmc_priv *priv = dev_get_priv(dev);
  30. int ret;
  31. ret = clk_set_rate(&priv->clk, freq);
  32. if (ret < 0) {
  33. debug("%s: err=%d\n", __func__, ret);
  34. return ret;
  35. }
  36. return freq;
  37. }
  38. static int rockchip_dwmmc_ofdata_to_platdata(struct udevice *dev)
  39. {
  40. struct rockchip_dwmmc_priv *priv = dev_get_priv(dev);
  41. struct dwmci_host *host = &priv->host;
  42. host->name = dev->name;
  43. host->ioaddr = (void *)dev_get_addr(dev);
  44. host->buswidth = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
  45. "bus-width", 4);
  46. host->get_mmc_clk = rockchip_dwmmc_get_mmc_clk;
  47. host->priv = dev;
  48. /* use non-removeable as sdcard and emmc as judgement */
  49. if (fdtdec_get_bool(gd->fdt_blob, dev->of_offset, "non-removable"))
  50. host->dev_index = 0;
  51. else
  52. host->dev_index = 1;
  53. return 0;
  54. }
  55. static int rockchip_dwmmc_probe(struct udevice *dev)
  56. {
  57. #ifdef CONFIG_BLK
  58. struct rockchip_mmc_plat *plat = dev_get_platdata(dev);
  59. #endif
  60. struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
  61. struct rockchip_dwmmc_priv *priv = dev_get_priv(dev);
  62. struct dwmci_host *host = &priv->host;
  63. struct udevice *pwr_dev __maybe_unused;
  64. u32 minmax[2];
  65. int ret;
  66. int fifo_depth;
  67. ret = clk_get_by_index(dev, 0, &priv->clk);
  68. if (ret < 0)
  69. return ret;
  70. if (fdtdec_get_int_array(gd->fdt_blob, dev->of_offset,
  71. "clock-freq-min-max", minmax, 2))
  72. return -EINVAL;
  73. fifo_depth = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
  74. "fifo-depth", 0);
  75. if (fifo_depth < 0)
  76. return -EINVAL;
  77. host->fifoth_val = MSIZE(0x2) |
  78. RX_WMARK(fifo_depth / 2 - 1) | TX_WMARK(fifo_depth / 2);
  79. if (fdtdec_get_bool(gd->fdt_blob, dev->of_offset, "fifo-mode"))
  80. host->fifo_mode = true;
  81. #ifdef CONFIG_PWRSEQ
  82. /* Enable power if needed */
  83. ret = uclass_get_device_by_phandle(UCLASS_PWRSEQ, dev, "mmc-pwrseq",
  84. &pwr_dev);
  85. if (!ret) {
  86. ret = pwrseq_set_power(pwr_dev, true);
  87. if (ret)
  88. return ret;
  89. }
  90. #endif
  91. #ifdef CONFIG_BLK
  92. dwmci_setup_cfg(&plat->cfg, dev->name, host->buswidth, host->caps,
  93. minmax[1], minmax[0]);
  94. host->mmc = &plat->mmc;
  95. #else
  96. ret = add_dwmci(host, minmax[1], minmax[0]);
  97. if (ret)
  98. return ret;
  99. #endif
  100. host->mmc->priv = &priv->host;
  101. host->mmc->dev = dev;
  102. upriv->mmc = host->mmc;
  103. return 0;
  104. }
  105. static int rockchip_dwmmc_bind(struct udevice *dev)
  106. {
  107. #ifdef CONFIG_BLK
  108. struct rockchip_mmc_plat *plat = dev_get_platdata(dev);
  109. int ret;
  110. ret = dwmci_bind(dev, &plat->mmc, &plat->cfg);
  111. if (ret)
  112. return ret;
  113. #endif
  114. return 0;
  115. }
  116. static const struct udevice_id rockchip_dwmmc_ids[] = {
  117. { .compatible = "rockchip,rk3288-dw-mshc" },
  118. { }
  119. };
  120. U_BOOT_DRIVER(rockchip_dwmmc_drv) = {
  121. .name = "rockchip_dwmmc",
  122. .id = UCLASS_MMC,
  123. .of_match = rockchip_dwmmc_ids,
  124. .ofdata_to_platdata = rockchip_dwmmc_ofdata_to_platdata,
  125. .bind = rockchip_dwmmc_bind,
  126. .probe = rockchip_dwmmc_probe,
  127. .priv_auto_alloc_size = sizeof(struct rockchip_dwmmc_priv),
  128. .platdata_auto_alloc_size = sizeof(struct rockchip_mmc_plat),
  129. };
  130. #ifdef CONFIG_PWRSEQ
  131. static int rockchip_dwmmc_pwrseq_set_power(struct udevice *dev, bool enable)
  132. {
  133. struct gpio_desc reset;
  134. int ret;
  135. ret = gpio_request_by_name(dev, "reset-gpios", 0, &reset, GPIOD_IS_OUT);
  136. if (ret)
  137. return ret;
  138. dm_gpio_set_value(&reset, 1);
  139. udelay(1);
  140. dm_gpio_set_value(&reset, 0);
  141. udelay(200);
  142. return 0;
  143. }
  144. static const struct pwrseq_ops rockchip_dwmmc_pwrseq_ops = {
  145. .set_power = rockchip_dwmmc_pwrseq_set_power,
  146. };
  147. static const struct udevice_id rockchip_dwmmc_pwrseq_ids[] = {
  148. { .compatible = "mmc-pwrseq-emmc" },
  149. { }
  150. };
  151. U_BOOT_DRIVER(rockchip_dwmmc_pwrseq_drv) = {
  152. .name = "mmc_pwrseq_emmc",
  153. .id = UCLASS_PWRSEQ,
  154. .of_match = rockchip_dwmmc_pwrseq_ids,
  155. .ops = &rockchip_dwmmc_pwrseq_ops,
  156. };
  157. #endif