atmel_sdhci.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 = (void *)devfdt_get_addr(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. ret = clk_set_rate(&clk, ATMEL_SDHC_GCK_RATE);
  66. if (ret)
  67. return ret;
  68. max_clk = clk_get_rate(&clk);
  69. if (!max_clk)
  70. return -EINVAL;
  71. host->max_clk = max_clk;
  72. host->mmc = &plat->mmc;
  73. host->mmc->dev = dev;
  74. ret = sdhci_setup_cfg(&plat->cfg, host, 0, ATMEL_SDHC_MIN_FREQ);
  75. if (ret)
  76. return ret;
  77. host->mmc->priv = host;
  78. upriv->mmc = host->mmc;
  79. clk_free(&clk);
  80. return sdhci_probe(dev);
  81. }
  82. static int atmel_sdhci_bind(struct udevice *dev)
  83. {
  84. struct atmel_sdhci_plat *plat = dev_get_platdata(dev);
  85. return sdhci_bind(dev, &plat->mmc, &plat->cfg);
  86. }
  87. static const struct udevice_id atmel_sdhci_ids[] = {
  88. { .compatible = "atmel,sama5d2-sdhci" },
  89. { .compatible = "microchip,sam9x60-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