nds32_mmc.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Andestech ATFSDC010 SD/MMC driver
  3. *
  4. * (C) Copyright 2017
  5. * Rick Chen, NDS32 Software Engineering, rick@andestech.com
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <clk.h>
  10. #include <dm.h>
  11. #include <dt-structs.h>
  12. #include <errno.h>
  13. #include <mapmem.h>
  14. #include <mmc.h>
  15. #include <pwrseq.h>
  16. #include <syscon.h>
  17. #include <linux/err.h>
  18. #include <faraday/ftsdc010.h>
  19. #include "ftsdc010_mci.h"
  20. DECLARE_GLOBAL_DATA_PTR;
  21. #if CONFIG_IS_ENABLED(OF_PLATDATA)
  22. struct nds_mmc {
  23. fdt32_t bus_width;
  24. bool cap_mmc_highspeed;
  25. bool cap_sd_highspeed;
  26. fdt32_t clock_freq_min_max[2];
  27. struct phandle_2_cell clocks[4];
  28. fdt32_t fifo_depth;
  29. fdt32_t reg[2];
  30. };
  31. #endif
  32. struct nds_mmc_plat {
  33. #if CONFIG_IS_ENABLED(OF_PLATDATA)
  34. struct nds_mmc dtplat;
  35. #endif
  36. struct mmc_config cfg;
  37. struct mmc mmc;
  38. };
  39. struct ftsdc_priv {
  40. struct clk clk;
  41. struct ftsdc010_chip chip;
  42. int fifo_depth;
  43. bool fifo_mode;
  44. u32 minmax[2];
  45. };
  46. static int nds32_mmc_ofdata_to_platdata(struct udevice *dev)
  47. {
  48. #if !CONFIG_IS_ENABLED(OF_PLATDATA)
  49. struct ftsdc_priv *priv = dev_get_priv(dev);
  50. struct ftsdc010_chip *chip = &priv->chip;
  51. chip->name = dev->name;
  52. chip->ioaddr = (void *)devfdt_get_addr(dev);
  53. chip->buswidth = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
  54. "bus-width", 4);
  55. chip->priv = dev;
  56. priv->fifo_depth = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
  57. "fifo-depth", 0);
  58. priv->fifo_mode = fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
  59. "fifo-mode");
  60. if (fdtdec_get_int_array(gd->fdt_blob, dev_of_offset(dev),
  61. "clock-freq-min-max", priv->minmax, 2)) {
  62. int val = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
  63. "max-frequency", -EINVAL);
  64. if (val < 0)
  65. return val;
  66. priv->minmax[0] = 400000; /* 400 kHz */
  67. priv->minmax[1] = val;
  68. } else {
  69. debug("%s: 'clock-freq-min-max' property was deprecated.\n",
  70. __func__);
  71. }
  72. #endif
  73. chip->sclk = priv->minmax[1];
  74. chip->regs = chip->ioaddr;
  75. return 0;
  76. }
  77. static int nds32_mmc_probe(struct udevice *dev)
  78. {
  79. struct nds_mmc_plat *plat = dev_get_platdata(dev);
  80. struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
  81. struct ftsdc_priv *priv = dev_get_priv(dev);
  82. struct ftsdc010_chip *chip = &priv->chip;
  83. struct udevice *pwr_dev __maybe_unused;
  84. #if CONFIG_IS_ENABLED(OF_PLATDATA)
  85. int ret;
  86. struct nds_mmc *dtplat = &plat->dtplat;
  87. chip->name = dev->name;
  88. chip->ioaddr = map_sysmem(dtplat->reg[0], dtplat->reg[1]);
  89. chip->buswidth = dtplat->bus_width;
  90. chip->priv = dev;
  91. chip->dev_index = 1;
  92. memcpy(priv->minmax, dtplat->clock_freq_min_max, sizeof(priv->minmax));
  93. ret = clk_get_by_index_platdata(dev, 0, dtplat->clocks, &priv->clk);
  94. if (ret < 0)
  95. return ret;
  96. #endif
  97. ftsdc_setup_cfg(&plat->cfg, dev->name, chip->buswidth, chip->caps,
  98. priv->minmax[1] , priv->minmax[0]);
  99. chip->mmc = &plat->mmc;
  100. chip->mmc->priv = &priv->chip;
  101. chip->mmc->dev = dev;
  102. upriv->mmc = chip->mmc;
  103. return ftsdc010_probe(dev);
  104. }
  105. static int nds32_mmc_bind(struct udevice *dev)
  106. {
  107. struct nds_mmc_plat *plat = dev_get_platdata(dev);
  108. return ftsdc010_bind(dev, &plat->mmc, &plat->cfg);
  109. }
  110. static const struct udevice_id nds32_mmc_ids[] = {
  111. { .compatible = "andestech,atsdc010" },
  112. { }
  113. };
  114. U_BOOT_DRIVER(nds32_mmc_drv) = {
  115. .name = "nds32_mmc",
  116. .id = UCLASS_MMC,
  117. .of_match = nds32_mmc_ids,
  118. .ofdata_to_platdata = nds32_mmc_ofdata_to_platdata,
  119. .ops = &dm_ftsdc010_ops,
  120. .bind = nds32_mmc_bind,
  121. .probe = nds32_mmc_probe,
  122. .priv_auto_alloc_size = sizeof(struct ftsdc_priv),
  123. .platdata_auto_alloc_size = sizeof(struct nds_mmc_plat),
  124. };