socfpga_dw_mmc.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2013 Altera Corporation <www.altera.com>
  4. */
  5. #include <common.h>
  6. #include <asm/arch/clock_manager.h>
  7. #include <asm/arch/system_manager.h>
  8. #include <clk.h>
  9. #include <dm.h>
  10. #include <dwmmc.h>
  11. #include <errno.h>
  12. #include <fdtdec.h>
  13. #include <linux/libfdt.h>
  14. #include <linux/err.h>
  15. #include <malloc.h>
  16. #include <reset.h>
  17. DECLARE_GLOBAL_DATA_PTR;
  18. static const struct socfpga_clock_manager *clock_manager_base =
  19. (void *)SOCFPGA_CLKMGR_ADDRESS;
  20. static const struct socfpga_system_manager *system_manager_base =
  21. (void *)SOCFPGA_SYSMGR_ADDRESS;
  22. struct socfpga_dwmci_plat {
  23. struct mmc_config cfg;
  24. struct mmc mmc;
  25. };
  26. /* socfpga implmentation specific driver private data */
  27. struct dwmci_socfpga_priv_data {
  28. struct dwmci_host host;
  29. unsigned int drvsel;
  30. unsigned int smplsel;
  31. };
  32. static void socfpga_dwmci_reset(struct udevice *dev)
  33. {
  34. struct reset_ctl_bulk reset_bulk;
  35. int ret;
  36. ret = reset_get_bulk(dev, &reset_bulk);
  37. if (ret) {
  38. dev_warn(dev, "Can't get reset: %d\n", ret);
  39. return;
  40. }
  41. reset_deassert_bulk(&reset_bulk);
  42. }
  43. static void socfpga_dwmci_clksel(struct dwmci_host *host)
  44. {
  45. struct dwmci_socfpga_priv_data *priv = host->priv;
  46. u32 sdmmc_mask = ((priv->smplsel & 0x7) << SYSMGR_SDMMC_SMPLSEL_SHIFT) |
  47. ((priv->drvsel & 0x7) << SYSMGR_SDMMC_DRVSEL_SHIFT);
  48. /* Disable SDMMC clock. */
  49. clrbits_le32(&clock_manager_base->per_pll.en,
  50. CLKMGR_PERPLLGRP_EN_SDMMCCLK_MASK);
  51. debug("%s: drvsel %d smplsel %d\n", __func__,
  52. priv->drvsel, priv->smplsel);
  53. writel(sdmmc_mask, &system_manager_base->sdmmcgrp_ctrl);
  54. debug("%s: SYSMGR_SDMMCGRP_CTRL_REG = 0x%x\n", __func__,
  55. readl(&system_manager_base->sdmmcgrp_ctrl));
  56. /* Enable SDMMC clock */
  57. setbits_le32(&clock_manager_base->per_pll.en,
  58. CLKMGR_PERPLLGRP_EN_SDMMCCLK_MASK);
  59. }
  60. static int socfpga_dwmmc_get_clk_rate(struct udevice *dev)
  61. {
  62. struct dwmci_socfpga_priv_data *priv = dev_get_priv(dev);
  63. struct dwmci_host *host = &priv->host;
  64. #if CONFIG_IS_ENABLED(CLK)
  65. struct clk clk;
  66. int ret;
  67. ret = clk_get_by_index(dev, 1, &clk);
  68. if (ret)
  69. return ret;
  70. host->bus_hz = clk_get_rate(&clk);
  71. clk_free(&clk);
  72. #else
  73. /* Fixed clock divide by 4 which due to the SDMMC wrapper */
  74. host->bus_hz = cm_get_mmc_controller_clk_hz();
  75. #endif
  76. if (host->bus_hz == 0) {
  77. printf("DWMMC: MMC clock is zero!");
  78. return -EINVAL;
  79. }
  80. return 0;
  81. }
  82. static int socfpga_dwmmc_ofdata_to_platdata(struct udevice *dev)
  83. {
  84. struct dwmci_socfpga_priv_data *priv = dev_get_priv(dev);
  85. struct dwmci_host *host = &priv->host;
  86. int fifo_depth;
  87. fifo_depth = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
  88. "fifo-depth", 0);
  89. if (fifo_depth < 0) {
  90. printf("DWMMC: Can't get FIFO depth\n");
  91. return -EINVAL;
  92. }
  93. host->name = dev->name;
  94. host->ioaddr = (void *)devfdt_get_addr(dev);
  95. host->buswidth = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
  96. "bus-width", 4);
  97. host->clksel = socfpga_dwmci_clksel;
  98. /*
  99. * TODO(sjg@chromium.org): Remove the need for this hack.
  100. * We only have one dwmmc block on gen5 SoCFPGA.
  101. */
  102. host->dev_index = 0;
  103. host->fifoth_val = MSIZE(0x2) |
  104. RX_WMARK(fifo_depth / 2 - 1) | TX_WMARK(fifo_depth / 2);
  105. priv->drvsel = fdtdec_get_uint(gd->fdt_blob, dev_of_offset(dev),
  106. "drvsel", 3);
  107. priv->smplsel = fdtdec_get_uint(gd->fdt_blob, dev_of_offset(dev),
  108. "smplsel", 0);
  109. host->priv = priv;
  110. return 0;
  111. }
  112. static int socfpga_dwmmc_probe(struct udevice *dev)
  113. {
  114. #ifdef CONFIG_BLK
  115. struct socfpga_dwmci_plat *plat = dev_get_platdata(dev);
  116. #endif
  117. struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
  118. struct dwmci_socfpga_priv_data *priv = dev_get_priv(dev);
  119. struct dwmci_host *host = &priv->host;
  120. int ret;
  121. ret = socfpga_dwmmc_get_clk_rate(dev);
  122. if (ret)
  123. return ret;
  124. socfpga_dwmci_reset(dev);
  125. #ifdef CONFIG_BLK
  126. dwmci_setup_cfg(&plat->cfg, host, host->bus_hz, 400000);
  127. host->mmc = &plat->mmc;
  128. #else
  129. ret = add_dwmci(host, host->bus_hz, 400000);
  130. if (ret)
  131. return ret;
  132. #endif
  133. host->mmc->priv = &priv->host;
  134. upriv->mmc = host->mmc;
  135. host->mmc->dev = dev;
  136. return dwmci_probe(dev);
  137. }
  138. static int socfpga_dwmmc_bind(struct udevice *dev)
  139. {
  140. #ifdef CONFIG_BLK
  141. struct socfpga_dwmci_plat *plat = dev_get_platdata(dev);
  142. int ret;
  143. ret = dwmci_bind(dev, &plat->mmc, &plat->cfg);
  144. if (ret)
  145. return ret;
  146. #endif
  147. return 0;
  148. }
  149. static const struct udevice_id socfpga_dwmmc_ids[] = {
  150. { .compatible = "altr,socfpga-dw-mshc" },
  151. { }
  152. };
  153. U_BOOT_DRIVER(socfpga_dwmmc_drv) = {
  154. .name = "socfpga_dwmmc",
  155. .id = UCLASS_MMC,
  156. .of_match = socfpga_dwmmc_ids,
  157. .ofdata_to_platdata = socfpga_dwmmc_ofdata_to_platdata,
  158. .ops = &dm_dwmci_ops,
  159. .bind = socfpga_dwmmc_bind,
  160. .probe = socfpga_dwmmc_probe,
  161. .priv_auto_alloc_size = sizeof(struct dwmci_socfpga_priv_data),
  162. .platdata_auto_alloc_size = sizeof(struct socfpga_dwmci_plat),
  163. };