msm_sdhci.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Qualcomm SDHCI driver - SD/eMMC controller
  3. *
  4. * (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com>
  5. *
  6. * Based on Linux driver
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <common.h>
  11. #include <clk.h>
  12. #include <dm.h>
  13. #include <sdhci.h>
  14. #include <wait_bit.h>
  15. #include <asm/io.h>
  16. #include <linux/bitops.h>
  17. /* Non-standard registers needed for SDHCI startup */
  18. #define SDCC_MCI_POWER 0x0
  19. #define SDCC_MCI_POWER_SW_RST BIT(7)
  20. /* This is undocumented register */
  21. #define SDCC_MCI_VERSION 0x50
  22. #define SDCC_MCI_VERSION_MAJOR_SHIFT 28
  23. #define SDCC_MCI_VERSION_MAJOR_MASK (0xf << SDCC_MCI_VERSION_MAJOR_SHIFT)
  24. #define SDCC_MCI_VERSION_MINOR_MASK 0xff
  25. #define SDCC_MCI_STATUS2 0x6C
  26. #define SDCC_MCI_STATUS2_MCI_ACT 0x1
  27. #define SDCC_MCI_HC_MODE 0x78
  28. /* Offset to SDHCI registers */
  29. #define SDCC_SDHCI_OFFSET 0x900
  30. /* Non standard (?) SDHCI register */
  31. #define SDHCI_VENDOR_SPEC_CAPABILITIES0 0x11c
  32. struct msm_sdhc {
  33. struct sdhci_host host;
  34. void *base;
  35. };
  36. DECLARE_GLOBAL_DATA_PTR;
  37. static int msm_sdc_clk_init(struct udevice *dev)
  38. {
  39. uint clk_rate = fdtdec_get_uint(gd->fdt_blob, dev->of_offset,
  40. "clock-frequency", 400000);
  41. uint clkd[2]; /* clk_id and clk_no */
  42. int clk_offset;
  43. struct udevice *clk_dev;
  44. struct clk clk;
  45. int ret;
  46. ret = fdtdec_get_int_array(gd->fdt_blob, dev->of_offset, "clock", clkd,
  47. 2);
  48. if (ret)
  49. return ret;
  50. clk_offset = fdt_node_offset_by_phandle(gd->fdt_blob, clkd[0]);
  51. if (clk_offset < 0)
  52. return clk_offset;
  53. ret = uclass_get_device_by_of_offset(UCLASS_CLK, clk_offset, &clk_dev);
  54. if (ret)
  55. return ret;
  56. clk.id = clkd[1];
  57. ret = clk_request(clk_dev, &clk);
  58. if (ret < 0)
  59. return ret;
  60. ret = clk_set_rate(&clk, clk_rate);
  61. clk_free(&clk);
  62. if (ret < 0)
  63. return ret;
  64. return 0;
  65. }
  66. static int msm_sdc_probe(struct udevice *dev)
  67. {
  68. struct msm_sdhc *prv = dev_get_priv(dev);
  69. struct sdhci_host *host = &prv->host;
  70. u32 core_version, core_minor, core_major;
  71. int ret;
  72. host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD | SDHCI_QUIRK_BROKEN_R1B;
  73. /* Init clocks */
  74. ret = msm_sdc_clk_init(dev);
  75. if (ret)
  76. return ret;
  77. /* Reset the core and Enable SDHC mode */
  78. writel(readl(prv->base + SDCC_MCI_POWER) | SDCC_MCI_POWER_SW_RST,
  79. prv->base + SDCC_MCI_POWER);
  80. /* Wait for reset to be written to register */
  81. if (wait_for_bit(__func__, prv->base + SDCC_MCI_STATUS2,
  82. SDCC_MCI_STATUS2_MCI_ACT, false, 10, false)) {
  83. printf("msm_sdhci: reset request failed\n");
  84. return -EIO;
  85. }
  86. /* SW reset can take upto 10HCLK + 15MCLK cycles. (min 40us) */
  87. if (wait_for_bit(__func__, prv->base + SDCC_MCI_POWER,
  88. SDCC_MCI_POWER_SW_RST, false, 2, false)) {
  89. printf("msm_sdhci: stuck in reset\n");
  90. return -ETIMEDOUT;
  91. }
  92. /* Enable host-controller mode */
  93. writel(1, prv->base + SDCC_MCI_HC_MODE);
  94. core_version = readl(prv->base + SDCC_MCI_VERSION);
  95. core_major = (core_version & SDCC_MCI_VERSION_MAJOR_MASK);
  96. core_major >>= SDCC_MCI_VERSION_MAJOR_SHIFT;
  97. core_minor = core_version & SDCC_MCI_VERSION_MINOR_MASK;
  98. /*
  99. * Support for some capabilities is not advertised by newer
  100. * controller versions and must be explicitly enabled.
  101. */
  102. if (core_major >= 1 && core_minor != 0x11 && core_minor != 0x12) {
  103. u32 caps = readl(host->ioaddr + SDHCI_CAPABILITIES);
  104. caps |= SDHCI_CAN_VDD_300 | SDHCI_CAN_DO_8BIT;
  105. writel(caps, host->ioaddr + SDHCI_VENDOR_SPEC_CAPABILITIES0);
  106. }
  107. /* Set host controller version */
  108. host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
  109. /* automatically detect max and min speed */
  110. ret = add_sdhci(host, 0, 0);
  111. if (ret)
  112. return ret;
  113. host->mmc->dev = dev;
  114. return 0;
  115. }
  116. static int msm_sdc_remove(struct udevice *dev)
  117. {
  118. struct msm_sdhc *priv = dev_get_priv(dev);
  119. /* Disable host-controller mode */
  120. writel(0, priv->base + SDCC_MCI_HC_MODE);
  121. return 0;
  122. }
  123. static int msm_ofdata_to_platdata(struct udevice *dev)
  124. {
  125. struct udevice *parent = dev->parent;
  126. struct msm_sdhc *priv = dev_get_priv(dev);
  127. struct sdhci_host *host = &priv->host;
  128. host->name = strdup(dev->name);
  129. host->ioaddr = (void *)dev_get_addr(dev);
  130. host->bus_width = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
  131. "bus-width", 4);
  132. host->index = fdtdec_get_uint(gd->fdt_blob, dev->of_offset, "index", 0);
  133. priv->base = (void *)fdtdec_get_addr_size_auto_parent(gd->fdt_blob,
  134. parent->of_offset,
  135. dev->of_offset,
  136. "reg", 1, NULL);
  137. if (priv->base == (void *)FDT_ADDR_T_NONE ||
  138. host->ioaddr == (void *)FDT_ADDR_T_NONE)
  139. return -EINVAL;
  140. return 0;
  141. }
  142. static const struct udevice_id msm_mmc_ids[] = {
  143. { .compatible = "qcom,sdhci-msm-v4" },
  144. { }
  145. };
  146. U_BOOT_DRIVER(msm_sdc_drv) = {
  147. .name = "msm_sdc",
  148. .id = UCLASS_MMC,
  149. .of_match = msm_mmc_ids,
  150. .ofdata_to_platdata = msm_ofdata_to_platdata,
  151. .probe = msm_sdc_probe,
  152. .remove = msm_sdc_remove,
  153. .priv_auto_alloc_size = sizeof(struct msm_sdhc),
  154. };