atmel_sdhci.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. max_clk = at91_get_periph_generated_clk(id);
  28. if (!max_clk) {
  29. printf("%s: Failed to get the proper clock\n", __func__);
  30. free(host);
  31. return -ENODEV;
  32. }
  33. host->max_clk = max_clk;
  34. add_sdhci(host, 0, 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_probe(struct udevice *dev)
  44. {
  45. struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
  46. struct atmel_sdhci_plat *plat = dev_get_platdata(dev);
  47. struct sdhci_host *host = dev_get_priv(dev);
  48. u32 max_clk;
  49. u32 caps, caps_1;
  50. u32 clk_base, clk_mul;
  51. ulong gck_rate;
  52. struct clk clk;
  53. int ret;
  54. ret = clk_get_by_index(dev, 0, &clk);
  55. if (ret)
  56. return ret;
  57. ret = clk_enable(&clk);
  58. if (ret)
  59. return ret;
  60. host->name = dev->name;
  61. host->ioaddr = (void *)dev_get_addr(dev);
  62. host->quirks = 0;
  63. host->bus_width = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
  64. "bus-width", 4);
  65. caps = sdhci_readl(host, SDHCI_CAPABILITIES);
  66. clk_base = (caps & SDHCI_CLOCK_V3_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT;
  67. caps_1 = sdhci_readl(host, SDHCI_CAPABILITIES_1);
  68. clk_mul = (caps_1 & SDHCI_CLOCK_MUL_MASK) >> SDHCI_CLOCK_MUL_SHIFT;
  69. gck_rate = clk_base * 1000000 * (clk_mul + 1);
  70. ret = clk_get_by_index(dev, 1, &clk);
  71. if (ret)
  72. return ret;
  73. ret = clk_set_rate(&clk, gck_rate);
  74. if (ret)
  75. return ret;
  76. max_clk = clk_get_rate(&clk);
  77. if (!max_clk)
  78. return -EINVAL;
  79. host->max_clk = max_clk;
  80. ret = sdhci_setup_cfg(&plat->cfg, host, 0, ATMEL_SDHC_MIN_FREQ);
  81. if (ret)
  82. return ret;
  83. host->mmc = &plat->mmc;
  84. host->mmc->dev = dev;
  85. host->mmc->priv = host;
  86. upriv->mmc = host->mmc;
  87. clk_free(&clk);
  88. return sdhci_probe(dev);
  89. }
  90. static int atmel_sdhci_bind(struct udevice *dev)
  91. {
  92. struct atmel_sdhci_plat *plat = dev_get_platdata(dev);
  93. return sdhci_bind(dev, &plat->mmc, &plat->cfg);
  94. }
  95. static const struct udevice_id atmel_sdhci_ids[] = {
  96. { .compatible = "atmel,sama5d2-sdhci" },
  97. { }
  98. };
  99. U_BOOT_DRIVER(atmel_sdhci_drv) = {
  100. .name = "atmel_sdhci",
  101. .id = UCLASS_MMC,
  102. .of_match = atmel_sdhci_ids,
  103. .ops = &sdhci_ops,
  104. .bind = atmel_sdhci_bind,
  105. .probe = atmel_sdhci_probe,
  106. .priv_auto_alloc_size = sizeof(struct sdhci_host),
  107. .platdata_auto_alloc_size = sizeof(struct atmel_sdhci_plat),
  108. };
  109. #endif