atmel_sdhci.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2015 Atmel Corporation
  4. * Wenyou.Yang <wenyou.yang@atmel.com>
  5. */
  6. #include <common.h>
  7. #include <clk.h>
  8. #include <dm.h>
  9. #include <malloc.h>
  10. #include <sdhci.h>
  11. #include <asm/arch/clk.h>
  12. #define ATMEL_SDHC_MIN_FREQ 400000
  13. #define ATMEL_SDHC_GCK_RATE 240000000
  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 = SDHCI_QUIRK_WAIT_SEND_CMD;
  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. struct clk clk;
  50. int ret;
  51. ret = clk_get_by_index(dev, 0, &clk);
  52. if (ret)
  53. return ret;
  54. ret = clk_enable(&clk);
  55. if (ret)
  56. return ret;
  57. host->name = dev->name;
  58. host->ioaddr = dev_read_addr_ptr(dev);
  59. host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD;
  60. host->bus_width = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
  61. "bus-width", 4);
  62. ret = clk_get_by_index(dev, 1, &clk);
  63. if (ret)
  64. return ret;
  65. clk_set_rate(&clk, ATMEL_SDHC_GCK_RATE);
  66. max_clk = clk_get_rate(&clk);
  67. if (!max_clk)
  68. return -EINVAL;
  69. ret = clk_enable(&clk);
  70. if (ret)
  71. return ret;
  72. ret = mmc_of_parse(dev, &plat->cfg);
  73. if (ret)
  74. return ret;
  75. host->max_clk = max_clk;
  76. host->mmc = &plat->mmc;
  77. host->mmc->dev = dev;
  78. ret = sdhci_setup_cfg(&plat->cfg, host, 0, ATMEL_SDHC_MIN_FREQ);
  79. if (ret)
  80. return ret;
  81. host->mmc->priv = host;
  82. upriv->mmc = host->mmc;
  83. clk_free(&clk);
  84. return sdhci_probe(dev);
  85. }
  86. static int atmel_sdhci_bind(struct udevice *dev)
  87. {
  88. struct atmel_sdhci_plat *plat = dev_get_platdata(dev);
  89. return sdhci_bind(dev, &plat->mmc, &plat->cfg);
  90. }
  91. static const struct udevice_id atmel_sdhci_ids[] = {
  92. { .compatible = "atmel,sama5d2-sdhci" },
  93. { .compatible = "microchip,sam9x60-sdhci" },
  94. { .compatible = "microchip,sama7g5-sdhci" },
  95. { }
  96. };
  97. U_BOOT_DRIVER(atmel_sdhci_drv) = {
  98. .name = "atmel_sdhci",
  99. .id = UCLASS_MMC,
  100. .of_match = atmel_sdhci_ids,
  101. .ops = &sdhci_ops,
  102. .bind = atmel_sdhci_bind,
  103. .probe = atmel_sdhci_probe,
  104. .priv_auto_alloc_size = sizeof(struct sdhci_host),
  105. .platdata_auto_alloc_size = sizeof(struct atmel_sdhci_plat),
  106. };
  107. #endif