socfpga_dw_mmc.c 4.0 KB

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