atmel_sdhci.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #define ATMEL_SDHC_GCK_RATE 240000000
  15. #ifndef CONFIG_DM_MMC
  16. int atmel_sdhci_init(void *regbase, u32 id)
  17. {
  18. struct sdhci_host *host;
  19. u32 max_clk, min_clk = ATMEL_SDHC_MIN_FREQ;
  20. host = (struct sdhci_host *)calloc(1, sizeof(struct sdhci_host));
  21. if (!host) {
  22. printf("%s: sdhci_host calloc failed\n", __func__);
  23. return -ENOMEM;
  24. }
  25. host->name = "atmel_sdhci";
  26. host->ioaddr = regbase;
  27. host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD;
  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. host->max_clk = max_clk;
  35. add_sdhci(host, 0, min_clk);
  36. return 0;
  37. }
  38. #else
  39. DECLARE_GLOBAL_DATA_PTR;
  40. struct atmel_sdhci_plat {
  41. struct mmc_config cfg;
  42. struct mmc mmc;
  43. };
  44. static int atmel_sdhci_probe(struct udevice *dev)
  45. {
  46. struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
  47. struct atmel_sdhci_plat *plat = dev_get_platdata(dev);
  48. struct sdhci_host *host = dev_get_priv(dev);
  49. u32 max_clk;
  50. struct clk clk;
  51. int ret;
  52. ret = clk_get_by_index(dev, 0, &clk);
  53. if (ret)
  54. return ret;
  55. ret = clk_enable(&clk);
  56. if (ret)
  57. return ret;
  58. host->name = dev->name;
  59. host->ioaddr = (void *)devfdt_get_addr(dev);
  60. host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD;
  61. host->bus_width = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
  62. "bus-width", 4);
  63. ret = clk_get_by_index(dev, 1, &clk);
  64. if (ret)
  65. return ret;
  66. ret = clk_set_rate(&clk, ATMEL_SDHC_GCK_RATE);
  67. if (ret)
  68. return ret;
  69. max_clk = clk_get_rate(&clk);
  70. if (!max_clk)
  71. return -EINVAL;
  72. host->max_clk = max_clk;
  73. ret = sdhci_setup_cfg(&plat->cfg, host, 0, ATMEL_SDHC_MIN_FREQ);
  74. if (ret)
  75. return ret;
  76. host->mmc = &plat->mmc;
  77. host->mmc->dev = dev;
  78. host->mmc->priv = host;
  79. upriv->mmc = host->mmc;
  80. clk_free(&clk);
  81. return sdhci_probe(dev);
  82. }
  83. static int atmel_sdhci_bind(struct udevice *dev)
  84. {
  85. struct atmel_sdhci_plat *plat = dev_get_platdata(dev);
  86. return sdhci_bind(dev, &plat->mmc, &plat->cfg);
  87. }
  88. static const struct udevice_id atmel_sdhci_ids[] = {
  89. { .compatible = "atmel,sama5d2-sdhci" },
  90. { }
  91. };
  92. U_BOOT_DRIVER(atmel_sdhci_drv) = {
  93. .name = "atmel_sdhci",
  94. .id = UCLASS_MMC,
  95. .of_match = atmel_sdhci_ids,
  96. .ops = &sdhci_ops,
  97. .bind = atmel_sdhci_bind,
  98. .probe = atmel_sdhci_probe,
  99. .priv_auto_alloc_size = sizeof(struct sdhci_host),
  100. .platdata_auto_alloc_size = sizeof(struct atmel_sdhci_plat),
  101. };
  102. #endif