2 * Qualcomm SDHCI driver - SD/eMMC controller
4 * (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com>
6 * Based on Linux driver
8 * SPDX-License-Identifier: GPL-2.0+
17 #include <linux/bitops.h>
19 /* Non-standard registers needed for SDHCI startup */
20 #define SDCC_MCI_POWER 0x0
21 #define SDCC_MCI_POWER_SW_RST BIT(7)
23 /* This is undocumented register */
24 #define SDCC_MCI_VERSION 0x50
25 #define SDCC_MCI_VERSION_MAJOR_SHIFT 28
26 #define SDCC_MCI_VERSION_MAJOR_MASK (0xf << SDCC_MCI_VERSION_MAJOR_SHIFT)
27 #define SDCC_MCI_VERSION_MINOR_MASK 0xff
29 #define SDCC_MCI_STATUS2 0x6C
30 #define SDCC_MCI_STATUS2_MCI_ACT 0x1
31 #define SDCC_MCI_HC_MODE 0x78
33 /* Offset to SDHCI registers */
34 #define SDCC_SDHCI_OFFSET 0x900
36 /* Non standard (?) SDHCI register */
37 #define SDHCI_VENDOR_SPEC_CAPABILITIES0 0x11c
39 struct msm_sdhc_plat {
40 struct mmc_config cfg;
45 struct sdhci_host host;
49 DECLARE_GLOBAL_DATA_PTR;
51 static int msm_sdc_clk_init(struct udevice *dev)
53 uint clk_rate = fdtdec_get_uint(gd->fdt_blob, dev->of_offset,
54 "clock-frequency", 400000);
55 uint clkd[2]; /* clk_id and clk_no */
57 struct udevice *clk_dev;
61 ret = fdtdec_get_int_array(gd->fdt_blob, dev->of_offset, "clock", clkd,
66 clk_offset = fdt_node_offset_by_phandle(gd->fdt_blob, clkd[0]);
70 ret = uclass_get_device_by_of_offset(UCLASS_CLK, clk_offset, &clk_dev);
75 ret = clk_request(clk_dev, &clk);
79 ret = clk_set_rate(&clk, clk_rate);
87 static int msm_sdc_probe(struct udevice *dev)
89 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
90 struct msm_sdhc_plat *plat = dev_get_platdata(dev);
91 struct msm_sdhc *prv = dev_get_priv(dev);
92 struct sdhci_host *host = &prv->host;
93 u32 core_version, core_minor, core_major;
97 host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD | SDHCI_QUIRK_BROKEN_R1B;
100 ret = msm_sdc_clk_init(dev);
104 /* Reset the core and Enable SDHC mode */
105 writel(readl(prv->base + SDCC_MCI_POWER) | SDCC_MCI_POWER_SW_RST,
106 prv->base + SDCC_MCI_POWER);
109 /* Wait for reset to be written to register */
110 if (wait_for_bit(__func__, prv->base + SDCC_MCI_STATUS2,
111 SDCC_MCI_STATUS2_MCI_ACT, false, 10, false)) {
112 printf("msm_sdhci: reset request failed\n");
116 /* SW reset can take upto 10HCLK + 15MCLK cycles. (min 40us) */
117 if (wait_for_bit(__func__, prv->base + SDCC_MCI_POWER,
118 SDCC_MCI_POWER_SW_RST, false, 2, false)) {
119 printf("msm_sdhci: stuck in reset\n");
123 /* Enable host-controller mode */
124 writel(1, prv->base + SDCC_MCI_HC_MODE);
126 core_version = readl(prv->base + SDCC_MCI_VERSION);
128 core_major = (core_version & SDCC_MCI_VERSION_MAJOR_MASK);
129 core_major >>= SDCC_MCI_VERSION_MAJOR_SHIFT;
131 core_minor = core_version & SDCC_MCI_VERSION_MINOR_MASK;
134 * Support for some capabilities is not advertised by newer
135 * controller versions and must be explicitly enabled.
137 if (core_major >= 1 && core_minor != 0x11 && core_minor != 0x12) {
138 caps = readl(host->ioaddr + SDHCI_CAPABILITIES);
139 caps |= SDHCI_CAN_VDD_300 | SDHCI_CAN_DO_8BIT;
140 writel(caps, host->ioaddr + SDHCI_VENDOR_SPEC_CAPABILITIES0);
143 /* Set host controller version */
144 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
146 caps = sdhci_readl(host, SDHCI_CAPABILITIES);
147 ret = sdhci_setup_cfg(&plat->cfg, dev->name, host->bus_width,
148 caps, 0, 0, host->version, host->quirks, 0);
149 host->mmc = &plat->mmc;
152 host->mmc->priv = &prv->host;
153 host->mmc->dev = dev;
154 upriv->mmc = host->mmc;
156 return sdhci_probe(dev);
159 static int msm_sdc_remove(struct udevice *dev)
161 struct msm_sdhc *priv = dev_get_priv(dev);
163 /* Disable host-controller mode */
164 writel(0, priv->base + SDCC_MCI_HC_MODE);
169 static int msm_ofdata_to_platdata(struct udevice *dev)
171 struct udevice *parent = dev->parent;
172 struct msm_sdhc *priv = dev_get_priv(dev);
173 struct sdhci_host *host = &priv->host;
175 host->name = strdup(dev->name);
176 host->ioaddr = (void *)dev_get_addr(dev);
177 host->bus_width = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
179 host->index = fdtdec_get_uint(gd->fdt_blob, dev->of_offset, "index", 0);
180 priv->base = (void *)fdtdec_get_addr_size_auto_parent(gd->fdt_blob,
184 if (priv->base == (void *)FDT_ADDR_T_NONE ||
185 host->ioaddr == (void *)FDT_ADDR_T_NONE)
191 static int msm_sdc_bind(struct udevice *dev)
193 struct msm_sdhc_plat *plat = dev_get_platdata(dev);
196 ret = sdhci_bind(dev, &plat->mmc, &plat->cfg);
203 static const struct udevice_id msm_mmc_ids[] = {
204 { .compatible = "qcom,sdhci-msm-v4" },
208 U_BOOT_DRIVER(msm_sdc_drv) = {
211 .of_match = msm_mmc_ids,
212 .ofdata_to_platdata = msm_ofdata_to_platdata,
214 .bind = msm_sdc_bind,
215 .probe = msm_sdc_probe,
216 .remove = msm_sdc_remove,
217 .priv_auto_alloc_size = sizeof(struct msm_sdhc),
218 .platdata_auto_alloc_size = sizeof(struct msm_sdhc_plat),