socfpga_dw_mmc.c 3.3 KB

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