socfpga_dw_mmc.c 3.4 KB

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