socfpga_dw_mmc.c 4.0 KB

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