1 // SPDX-License-Identifier: GPL-2.0+
3 * Qualcomm SDHCI driver - SD/eMMC controller
7 * Based on Linux driver
16 #include <asm/global_data.h>
18 #include <linux/bitops.h>
20 /* Non-standard registers needed for SDHCI startup */
21 #define SDCC_MCI_POWER 0x0
22 #define SDCC_MCI_POWER_SW_RST BIT(7)
24 /* This is undocumented register */
25 #define SDCC_MCI_VERSION 0x50
26 #define SDCC_V5_VERSION 0x318
28 #define SDCC_VERSION_MAJOR_SHIFT 28
29 #define SDCC_VERSION_MAJOR_MASK (0xf << SDCC_VERSION_MAJOR_SHIFT)
30 #define SDCC_VERSION_MINOR_MASK 0xff
32 #define SDCC_MCI_STATUS2 0x6C
33 #define SDCC_MCI_STATUS2_MCI_ACT 0x1
34 #define SDCC_MCI_HC_MODE 0x78
36 struct msm_sdhc_plat {
37 struct mmc_config cfg;
42 struct sdhci_host host;
47 struct msm_sdhc_variant_info {
50 u32 core_vendor_spec_capabilities0;
53 DECLARE_GLOBAL_DATA_PTR;
55 static int msm_sdc_clk_init(struct udevice *dev)
57 struct msm_sdhc *prv = dev_get_priv(dev);
58 ofnode node = dev_ofnode(dev);
60 int ret, i = 0, n_clks;
63 ret = ofnode_read_u32(node, "clock-frequency", (uint *)(&clk_rate));
67 ret = clk_get_bulk(dev, &prv->clks);
69 log_warning("Couldn't get mmc clocks: %d\n", ret);
73 ret = clk_enable_bulk(&prv->clks);
75 log_warning("Couldn't enable mmc clocks: %d\n", ret);
79 /* If clock-names is unspecified, then the first clock is the core clock */
80 if (!ofnode_get_property(node, "clock-names", &n_clks)) {
81 if (!clk_set_rate(&prv->clks.clks[0], clk_rate)) {
82 log_warning("Couldn't set core clock rate: %d\n", ret);
87 /* Find the index of the "core" clock */
89 ofnode_read_string_index(node, "clock-names", i, &clk_name);
90 if (!strcmp(clk_name, "core"))
95 if (i >= prv->clks.count) {
96 log_warning("Couldn't find core clock (index %d but only have %d clocks)\n", i,
101 /* The clock is already enabled by the clk_bulk above */
102 clk_rate = clk_set_rate(&prv->clks.clks[i], clk_rate);
103 /* If we get a rate of 0 then something has probably gone wrong. */
104 if (clk_rate == 0 || IS_ERR((void *)clk_rate)) {
105 log_warning("Couldn't set MMC core clock rate: %dE\n", clk_rate ? (int)PTR_ERR((void *)clk_rate) : 0);
112 static int msm_sdc_mci_init(struct msm_sdhc *prv)
114 /* Reset the core and Enable SDHC mode */
115 writel(readl(prv->base + SDCC_MCI_POWER) | SDCC_MCI_POWER_SW_RST,
116 prv->base + SDCC_MCI_POWER);
119 /* Wait for reset to be written to register */
120 if (wait_for_bit_le32(prv->base + SDCC_MCI_STATUS2,
121 SDCC_MCI_STATUS2_MCI_ACT, false, 10, false)) {
122 printf("msm_sdhci: reset request failed\n");
126 /* SW reset can take upto 10HCLK + 15MCLK cycles. (min 40us) */
127 if (wait_for_bit_le32(prv->base + SDCC_MCI_POWER,
128 SDCC_MCI_POWER_SW_RST, false, 2, false)) {
129 printf("msm_sdhci: stuck in reset\n");
133 /* Enable host-controller mode */
134 writel(1, prv->base + SDCC_MCI_HC_MODE);
139 static int msm_sdc_probe(struct udevice *dev)
141 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
142 struct msm_sdhc_plat *plat = dev_get_plat(dev);
143 struct msm_sdhc *prv = dev_get_priv(dev);
144 const struct msm_sdhc_variant_info *var_info;
145 struct sdhci_host *host = &prv->host;
146 u32 core_version, core_minor, core_major;
150 host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD | SDHCI_QUIRK_BROKEN_R1B;
155 ret = msm_sdc_clk_init(dev);
159 var_info = (void *)dev_get_driver_data(dev);
160 if (!var_info->mci_removed) {
161 ret = msm_sdc_mci_init(prv);
166 if (!var_info->mci_removed)
167 core_version = readl(prv->base + SDCC_MCI_VERSION);
169 core_version = readl(host->ioaddr + SDCC_V5_VERSION);
171 core_major = (core_version & SDCC_VERSION_MAJOR_MASK);
172 core_major >>= SDCC_VERSION_MAJOR_SHIFT;
174 core_minor = core_version & SDCC_VERSION_MINOR_MASK;
176 log_debug("SDCC version %d.%d\n", core_major, core_minor);
179 * Support for some capabilities is not advertised by newer
180 * controller versions and must be explicitly enabled.
182 if (core_major >= 1 && core_minor != 0x11 && core_minor != 0x12) {
183 caps = readl(host->ioaddr + SDHCI_CAPABILITIES);
184 caps |= SDHCI_CAN_VDD_300 | SDHCI_CAN_DO_8BIT;
185 writel(caps, host->ioaddr + var_info->core_vendor_spec_capabilities0);
188 ret = mmc_of_parse(dev, &plat->cfg);
192 host->mmc = &plat->mmc;
193 host->mmc->dev = dev;
194 ret = sdhci_setup_cfg(&plat->cfg, host, 0, 0);
197 host->mmc->priv = &prv->host;
198 upriv->mmc = host->mmc;
200 return sdhci_probe(dev);
203 static int msm_sdc_remove(struct udevice *dev)
205 struct msm_sdhc *priv = dev_get_priv(dev);
206 const struct msm_sdhc_variant_info *var_info;
208 var_info = (void *)dev_get_driver_data(dev);
210 /* Disable host-controller mode */
211 if (!var_info->mci_removed && priv->base)
212 writel(0, priv->base + SDCC_MCI_HC_MODE);
214 clk_release_bulk(&priv->clks);
219 static int msm_of_to_plat(struct udevice *dev)
221 struct msm_sdhc *priv = dev_get_priv(dev);
222 const struct msm_sdhc_variant_info *var_info;
223 struct sdhci_host *host = &priv->host;
226 var_info = (void*)dev_get_driver_data(dev);
228 host->name = strdup(dev->name);
229 host->ioaddr = dev_read_addr_ptr(dev);
230 ret = dev_read_u32(dev, "bus-width", &host->bus_width);
233 ret = dev_read_u32(dev, "index", &host->index);
236 priv->base = dev_read_addr_index_ptr(dev, 1);
241 if (!var_info->mci_removed && !priv->base) {
242 printf("msm_sdhci: MCI base address not found\n");
249 static int msm_sdc_bind(struct udevice *dev)
251 struct msm_sdhc_plat *plat = dev_get_plat(dev);
253 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
256 static const struct msm_sdhc_variant_info msm_sdhc_mci_var = {
257 .mci_removed = false,
259 .core_vendor_spec_capabilities0 = 0x11c,
262 static const struct msm_sdhc_variant_info msm_sdhc_v5_var = {
265 .core_vendor_spec_capabilities0 = 0x21c,
268 static const struct udevice_id msm_mmc_ids[] = {
269 { .compatible = "qcom,sdhci-msm-v4", .data = (ulong)&msm_sdhc_mci_var },
270 { .compatible = "qcom,sdhci-msm-v5", .data = (ulong)&msm_sdhc_v5_var },
274 U_BOOT_DRIVER(msm_sdc_drv) = {
277 .of_match = msm_mmc_ids,
278 .of_to_plat = msm_of_to_plat,
280 .bind = msm_sdc_bind,
281 .probe = msm_sdc_probe,
282 .remove = msm_sdc_remove,
283 .priv_auto = sizeof(struct msm_sdhc),
284 .plat_auto = sizeof(struct msm_sdhc_plat),