ca_dw_mmc.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2019 Cortina Access
  4. * Arthur Li <arthur.li@cortina-access.com>
  5. */
  6. #include <common.h>
  7. #include <dwmmc.h>
  8. #include <fdtdec.h>
  9. #include <linux/libfdt.h>
  10. #include <malloc.h>
  11. #include <errno.h>
  12. #include <dm.h>
  13. #include <mapmem.h>
  14. #define SD_CLK_SEL_MASK (0x3)
  15. #define SD_DLL_DEFAULT (0x143000)
  16. #define SD_SCLK_MAX (200000000)
  17. #define SD_CLK_SEL_200MHZ (0x2)
  18. #define SD_CLK_SEL_100MHZ (0x1)
  19. #define SD_CLK_SEL_50MHZ (0x0)
  20. #define IO_DRV_SD_DS_OFFSET (16)
  21. #define IO_DRV_SD_DS_MASK (0xff << IO_DRV_SD_DS_OFFSET)
  22. #define MIN_FREQ (400000)
  23. DECLARE_GLOBAL_DATA_PTR;
  24. struct ca_mmc_plat {
  25. struct mmc_config cfg;
  26. struct mmc mmc;
  27. };
  28. struct ca_dwmmc_priv_data {
  29. struct dwmci_host host;
  30. void __iomem *sd_dll_reg;
  31. void __iomem *io_drv_reg;
  32. u8 ds;
  33. };
  34. static void ca_dwmci_clksel(struct dwmci_host *host)
  35. {
  36. struct ca_dwmmc_priv_data *priv = host->priv;
  37. u32 val = readl(priv->sd_dll_reg);
  38. val &= ~SD_CLK_SEL_MASK;
  39. if (host->bus_hz >= 200000000)
  40. val |= SD_CLK_SEL_200MHZ;
  41. else if (host->bus_hz >= 100000000)
  42. val |= SD_CLK_SEL_100MHZ;
  43. writel(val, priv->sd_dll_reg);
  44. }
  45. static void ca_dwmci_board_init(struct dwmci_host *host)
  46. {
  47. struct ca_dwmmc_priv_data *priv = host->priv;
  48. u32 val = readl(priv->io_drv_reg);
  49. writel(SD_DLL_DEFAULT, priv->sd_dll_reg);
  50. val &= ~IO_DRV_SD_DS_MASK;
  51. if (priv && priv->ds)
  52. val |= priv->ds << IO_DRV_SD_DS_OFFSET;
  53. writel(val, priv->io_drv_reg);
  54. }
  55. unsigned int ca_dwmci_get_mmc_clock(struct dwmci_host *host, uint freq)
  56. {
  57. struct ca_dwmmc_priv_data *priv = host->priv;
  58. u8 sd_clk_sel = readl(priv->sd_dll_reg) & SD_CLK_SEL_MASK;
  59. u8 clk_div;
  60. switch (sd_clk_sel) {
  61. case SD_CLK_SEL_50MHZ:
  62. clk_div = 4;
  63. break;
  64. case SD_CLK_SEL_100MHZ:
  65. clk_div = 2;
  66. break;
  67. default:
  68. clk_div = 1;
  69. }
  70. return SD_SCLK_MAX / clk_div / (host->div + 1);
  71. }
  72. static int ca_dwmmc_ofdata_to_platdata(struct udevice *dev)
  73. {
  74. struct ca_dwmmc_priv_data *priv = dev_get_priv(dev);
  75. struct dwmci_host *host = &priv->host;
  76. u32 tmp;
  77. host->name = dev->name;
  78. host->dev_index = 0;
  79. host->buswidth = dev_read_u32_default(dev, "bus-width", 1);
  80. host->bus_hz = dev_read_u32_default(dev, "max-frequency", 50000000);
  81. priv->ds = dev_read_u32_default(dev, "io_ds", 0x33);
  82. host->fifo_mode = dev_read_bool(dev, "fifo-mode");
  83. dev_read_u32(dev, "sd_dll_ctrl", &tmp);
  84. priv->sd_dll_reg = map_sysmem((uintptr_t)tmp, sizeof(uintptr_t));
  85. if (!priv->sd_dll_reg)
  86. return -EINVAL;
  87. dev_read_u32(dev, "io_drv_ctrl", &tmp);
  88. priv->io_drv_reg = map_sysmem((uintptr_t)tmp, sizeof(uintptr_t));
  89. if (!priv->io_drv_reg)
  90. return -EINVAL;
  91. host->ioaddr = dev_read_addr_ptr(dev);
  92. if (!host->ioaddr)
  93. return -EINVAL;
  94. host->priv = priv;
  95. return 0;
  96. }
  97. struct dm_mmc_ops ca_dwmci_dm_ops;
  98. static int ca_dwmmc_probe(struct udevice *dev)
  99. {
  100. struct ca_mmc_plat *plat = dev_get_plat(dev);
  101. struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
  102. struct ca_dwmmc_priv_data *priv = dev_get_priv(dev);
  103. struct dwmci_host *host = &priv->host;
  104. memcpy(&ca_dwmci_dm_ops, &dm_dwmci_ops, sizeof(struct dm_mmc_ops));
  105. dwmci_setup_cfg(&plat->cfg, host, host->bus_hz, MIN_FREQ);
  106. if (host->buswidth == 1)
  107. (&plat->cfg)->host_caps &= ~(MMC_MODE_8BIT | MMC_MODE_4BIT);
  108. host->mmc = &plat->mmc;
  109. host->mmc->priv = &priv->host;
  110. upriv->mmc = host->mmc;
  111. host->mmc->dev = dev;
  112. host->clksel = ca_dwmci_clksel;
  113. host->board_init = ca_dwmci_board_init;
  114. host->get_mmc_clk = ca_dwmci_get_mmc_clock;
  115. return dwmci_probe(dev);
  116. }
  117. static int ca_dwmmc_bind(struct udevice *dev)
  118. {
  119. struct ca_mmc_plat *plat = dev_get_plat(dev);
  120. return dwmci_bind(dev, &plat->mmc, &plat->cfg);
  121. }
  122. static const struct udevice_id ca_dwmmc_ids[] = {
  123. { .compatible = "cortina,ca-mmc" },
  124. { }
  125. };
  126. U_BOOT_DRIVER(ca_dwmmc_drv) = {
  127. .name = "cortina_dwmmc",
  128. .id = UCLASS_MMC,
  129. .of_match = ca_dwmmc_ids,
  130. .ofdata_to_platdata = ca_dwmmc_ofdata_to_platdata,
  131. .bind = ca_dwmmc_bind,
  132. .ops = &ca_dwmci_dm_ops,
  133. .probe = ca_dwmmc_probe,
  134. .priv_auto = sizeof(struct ca_dwmmc_priv_data),
  135. .plat_auto = sizeof(struct ca_mmc_plat),
  136. };