atmel_sdhci.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (C) 2015 Atmel Corporation
  3. * Wenyou.Yang <wenyou.yang@atmel.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <clk.h>
  9. #include <dm.h>
  10. #include <malloc.h>
  11. #include <sdhci.h>
  12. #include <asm/arch/clk.h>
  13. #define ATMEL_SDHC_MIN_FREQ 400000
  14. #ifndef CONFIG_DM_MMC
  15. int atmel_sdhci_init(void *regbase, u32 id)
  16. {
  17. struct sdhci_host *host;
  18. u32 max_clk, min_clk = ATMEL_SDHC_MIN_FREQ;
  19. host = (struct sdhci_host *)calloc(1, sizeof(struct sdhci_host));
  20. if (!host) {
  21. printf("%s: sdhci_host calloc failed\n", __func__);
  22. return -ENOMEM;
  23. }
  24. host->name = "atmel_sdhci";
  25. host->ioaddr = regbase;
  26. host->quirks = 0;
  27. host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
  28. max_clk = at91_get_periph_generated_clk(id);
  29. if (!max_clk) {
  30. printf("%s: Failed to get the proper clock\n", __func__);
  31. free(host);
  32. return -ENODEV;
  33. }
  34. add_sdhci(host, max_clk, min_clk);
  35. return 0;
  36. }
  37. #else
  38. DECLARE_GLOBAL_DATA_PTR;
  39. struct atmel_sdhci_plat {
  40. struct mmc_config cfg;
  41. struct mmc mmc;
  42. };
  43. static int atmel_sdhci_get_clk(struct udevice *dev, int index, struct clk *clk)
  44. {
  45. struct udevice *dev_clk;
  46. int periph, ret;
  47. ret = clk_get_by_index(dev, index, clk);
  48. if (ret)
  49. return ret;
  50. periph = fdtdec_get_uint(gd->fdt_blob, clk->dev->of_offset, "reg", -1);
  51. if (periph < 0)
  52. return -EINVAL;
  53. dev_clk = dev_get_parent(clk->dev);
  54. ret = clk_request(dev_clk, clk);
  55. if (ret)
  56. return ret;
  57. clk->id = periph;
  58. return 0;
  59. }
  60. static int atmel_sdhci_probe(struct udevice *dev)
  61. {
  62. struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
  63. struct atmel_sdhci_plat *plat = dev_get_platdata(dev);
  64. struct sdhci_host *host = dev_get_priv(dev);
  65. u32 max_clk;
  66. u32 caps, caps_1;
  67. u32 clk_base, clk_mul;
  68. ulong gck_rate;
  69. struct clk clk;
  70. int ret;
  71. ret = atmel_sdhci_get_clk(dev, 0, &clk);
  72. if (ret)
  73. return ret;
  74. ret = clk_enable(&clk);
  75. if (ret)
  76. return ret;
  77. host->name = dev->name;
  78. host->ioaddr = (void *)dev_get_addr(dev);
  79. host->quirks = 0;
  80. host->bus_width = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
  81. "bus-width", 4);
  82. caps = sdhci_readl(host, SDHCI_CAPABILITIES);
  83. clk_base = (caps & SDHCI_CLOCK_V3_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT;
  84. caps_1 = sdhci_readl(host, SDHCI_CAPABILITIES_1);
  85. clk_mul = (caps_1 & SDHCI_CLOCK_MUL_MASK) >> SDHCI_CLOCK_MUL_SHIFT;
  86. gck_rate = clk_base * 1000000 * (clk_mul + 1);
  87. ret = atmel_sdhci_get_clk(dev, 1, &clk);
  88. if (ret)
  89. return ret;
  90. ret = clk_set_rate(&clk, gck_rate);
  91. if (ret)
  92. return ret;
  93. max_clk = clk_get_rate(&clk);
  94. if (!max_clk)
  95. return -EINVAL;
  96. ret = sdhci_setup_cfg(&plat->cfg, host, max_clk, ATMEL_SDHC_MIN_FREQ);
  97. if (ret)
  98. return ret;
  99. host->mmc = &plat->mmc;
  100. host->mmc->dev = dev;
  101. host->mmc->priv = host;
  102. upriv->mmc = host->mmc;
  103. clk_free(&clk);
  104. return sdhci_probe(dev);
  105. }
  106. static int atmel_sdhci_bind(struct udevice *dev)
  107. {
  108. struct atmel_sdhci_plat *plat = dev_get_platdata(dev);
  109. return sdhci_bind(dev, &plat->mmc, &plat->cfg);
  110. }
  111. static const struct udevice_id atmel_sdhci_ids[] = {
  112. { .compatible = "atmel,sama5d2-sdhci" },
  113. { }
  114. };
  115. U_BOOT_DRIVER(atmel_sdhci_drv) = {
  116. .name = "atmel_sdhci",
  117. .id = UCLASS_MMC,
  118. .of_match = atmel_sdhci_ids,
  119. .ops = &sdhci_ops,
  120. .bind = atmel_sdhci_bind,
  121. .probe = atmel_sdhci_probe,
  122. .priv_auto_alloc_size = sizeof(struct sdhci_host),
  123. .platdata_auto_alloc_size = sizeof(struct atmel_sdhci_plat),
  124. };
  125. #endif