1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
29d11d12aSMateusz Kulikowski /*
39d11d12aSMateusz Kulikowski * Qualcomm SDHCI driver - SD/eMMC controller
49d11d12aSMateusz Kulikowski *
59d11d12aSMateusz Kulikowski * (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com>
69d11d12aSMateusz Kulikowski *
79d11d12aSMateusz Kulikowski * Based on Linux driver
89d11d12aSMateusz Kulikowski */
99d11d12aSMateusz Kulikowski
109d11d12aSMateusz Kulikowski #include <common.h>
119d11d12aSMateusz Kulikowski #include <clk.h>
129d11d12aSMateusz Kulikowski #include <dm.h>
139d11d12aSMateusz Kulikowski #include <sdhci.h>
149d11d12aSMateusz Kulikowski #include <wait_bit.h>
159d11d12aSMateusz Kulikowski #include <asm/io.h>
169d11d12aSMateusz Kulikowski #include <linux/bitops.h>
179d11d12aSMateusz Kulikowski
189d11d12aSMateusz Kulikowski /* Non-standard registers needed for SDHCI startup */
199d11d12aSMateusz Kulikowski #define SDCC_MCI_POWER 0x0
209d11d12aSMateusz Kulikowski #define SDCC_MCI_POWER_SW_RST BIT(7)
219d11d12aSMateusz Kulikowski
229d11d12aSMateusz Kulikowski /* This is undocumented register */
239d11d12aSMateusz Kulikowski #define SDCC_MCI_VERSION 0x50
249d11d12aSMateusz Kulikowski #define SDCC_MCI_VERSION_MAJOR_SHIFT 28
259d11d12aSMateusz Kulikowski #define SDCC_MCI_VERSION_MAJOR_MASK (0xf << SDCC_MCI_VERSION_MAJOR_SHIFT)
269d11d12aSMateusz Kulikowski #define SDCC_MCI_VERSION_MINOR_MASK 0xff
279d11d12aSMateusz Kulikowski
289d11d12aSMateusz Kulikowski #define SDCC_MCI_STATUS2 0x6C
299d11d12aSMateusz Kulikowski #define SDCC_MCI_STATUS2_MCI_ACT 0x1
309d11d12aSMateusz Kulikowski #define SDCC_MCI_HC_MODE 0x78
319d11d12aSMateusz Kulikowski
329d11d12aSMateusz Kulikowski /* Offset to SDHCI registers */
339d11d12aSMateusz Kulikowski #define SDCC_SDHCI_OFFSET 0x900
349d11d12aSMateusz Kulikowski
359d11d12aSMateusz Kulikowski /* Non standard (?) SDHCI register */
369d11d12aSMateusz Kulikowski #define SDHCI_VENDOR_SPEC_CAPABILITIES0 0x11c
379d11d12aSMateusz Kulikowski
3812293f6dSSimon Glass struct msm_sdhc_plat {
3912293f6dSSimon Glass struct mmc_config cfg;
4012293f6dSSimon Glass struct mmc mmc;
4112293f6dSSimon Glass };
4212293f6dSSimon Glass
439d11d12aSMateusz Kulikowski struct msm_sdhc {
449d11d12aSMateusz Kulikowski struct sdhci_host host;
459d11d12aSMateusz Kulikowski void *base;
469d11d12aSMateusz Kulikowski };
479d11d12aSMateusz Kulikowski
489d11d12aSMateusz Kulikowski DECLARE_GLOBAL_DATA_PTR;
499d11d12aSMateusz Kulikowski
msm_sdc_clk_init(struct udevice * dev)509d11d12aSMateusz Kulikowski static int msm_sdc_clk_init(struct udevice *dev)
519d11d12aSMateusz Kulikowski {
52e160f7d4SSimon Glass int node = dev_of_offset(dev);
53e160f7d4SSimon Glass uint clk_rate = fdtdec_get_uint(gd->fdt_blob, node, "clock-frequency",
54e160f7d4SSimon Glass 400000);
559d11d12aSMateusz Kulikowski uint clkd[2]; /* clk_id and clk_no */
569d11d12aSMateusz Kulikowski int clk_offset;
57135aa950SStephen Warren struct udevice *clk_dev;
58135aa950SStephen Warren struct clk clk;
599d11d12aSMateusz Kulikowski int ret;
609d11d12aSMateusz Kulikowski
61e160f7d4SSimon Glass ret = fdtdec_get_int_array(gd->fdt_blob, node, "clock", clkd, 2);
629d11d12aSMateusz Kulikowski if (ret)
639d11d12aSMateusz Kulikowski return ret;
649d11d12aSMateusz Kulikowski
659d11d12aSMateusz Kulikowski clk_offset = fdt_node_offset_by_phandle(gd->fdt_blob, clkd[0]);
669d11d12aSMateusz Kulikowski if (clk_offset < 0)
679d11d12aSMateusz Kulikowski return clk_offset;
689d11d12aSMateusz Kulikowski
69135aa950SStephen Warren ret = uclass_get_device_by_of_offset(UCLASS_CLK, clk_offset, &clk_dev);
709d11d12aSMateusz Kulikowski if (ret)
719d11d12aSMateusz Kulikowski return ret;
729d11d12aSMateusz Kulikowski
73135aa950SStephen Warren clk.id = clkd[1];
74135aa950SStephen Warren ret = clk_request(clk_dev, &clk);
75135aa950SStephen Warren if (ret < 0)
76135aa950SStephen Warren return ret;
77135aa950SStephen Warren
78135aa950SStephen Warren ret = clk_set_rate(&clk, clk_rate);
79135aa950SStephen Warren clk_free(&clk);
809d11d12aSMateusz Kulikowski if (ret < 0)
819d11d12aSMateusz Kulikowski return ret;
829d11d12aSMateusz Kulikowski
839d11d12aSMateusz Kulikowski return 0;
849d11d12aSMateusz Kulikowski }
859d11d12aSMateusz Kulikowski
msm_sdc_probe(struct udevice * dev)869d11d12aSMateusz Kulikowski static int msm_sdc_probe(struct udevice *dev)
879d11d12aSMateusz Kulikowski {
8812293f6dSSimon Glass struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
8912293f6dSSimon Glass struct msm_sdhc_plat *plat = dev_get_platdata(dev);
909d11d12aSMateusz Kulikowski struct msm_sdhc *prv = dev_get_priv(dev);
919d11d12aSMateusz Kulikowski struct sdhci_host *host = &prv->host;
929d11d12aSMateusz Kulikowski u32 core_version, core_minor, core_major;
9312293f6dSSimon Glass u32 caps;
949d11d12aSMateusz Kulikowski int ret;
959d11d12aSMateusz Kulikowski
969d11d12aSMateusz Kulikowski host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD | SDHCI_QUIRK_BROKEN_R1B;
979d11d12aSMateusz Kulikowski
986d0e34bfSStefan Herbrechtsmeier host->max_clk = 0;
996d0e34bfSStefan Herbrechtsmeier
1009d11d12aSMateusz Kulikowski /* Init clocks */
1019d11d12aSMateusz Kulikowski ret = msm_sdc_clk_init(dev);
1029d11d12aSMateusz Kulikowski if (ret)
1039d11d12aSMateusz Kulikowski return ret;
1049d11d12aSMateusz Kulikowski
1059d11d12aSMateusz Kulikowski /* Reset the core and Enable SDHC mode */
1069d11d12aSMateusz Kulikowski writel(readl(prv->base + SDCC_MCI_POWER) | SDCC_MCI_POWER_SW_RST,
1079d11d12aSMateusz Kulikowski prv->base + SDCC_MCI_POWER);
1089d11d12aSMateusz Kulikowski
1099d11d12aSMateusz Kulikowski
1109d11d12aSMateusz Kulikowski /* Wait for reset to be written to register */
11148263504SÁlvaro Fernández Rojas if (wait_for_bit_le32(prv->base + SDCC_MCI_STATUS2,
1129d11d12aSMateusz Kulikowski SDCC_MCI_STATUS2_MCI_ACT, false, 10, false)) {
1139d11d12aSMateusz Kulikowski printf("msm_sdhci: reset request failed\n");
1149d11d12aSMateusz Kulikowski return -EIO;
1159d11d12aSMateusz Kulikowski }
1169d11d12aSMateusz Kulikowski
1179d11d12aSMateusz Kulikowski /* SW reset can take upto 10HCLK + 15MCLK cycles. (min 40us) */
11848263504SÁlvaro Fernández Rojas if (wait_for_bit_le32(prv->base + SDCC_MCI_POWER,
1199d11d12aSMateusz Kulikowski SDCC_MCI_POWER_SW_RST, false, 2, false)) {
1209d11d12aSMateusz Kulikowski printf("msm_sdhci: stuck in reset\n");
1219d11d12aSMateusz Kulikowski return -ETIMEDOUT;
1229d11d12aSMateusz Kulikowski }
1239d11d12aSMateusz Kulikowski
1249d11d12aSMateusz Kulikowski /* Enable host-controller mode */
1259d11d12aSMateusz Kulikowski writel(1, prv->base + SDCC_MCI_HC_MODE);
1269d11d12aSMateusz Kulikowski
1279d11d12aSMateusz Kulikowski core_version = readl(prv->base + SDCC_MCI_VERSION);
1289d11d12aSMateusz Kulikowski
1299d11d12aSMateusz Kulikowski core_major = (core_version & SDCC_MCI_VERSION_MAJOR_MASK);
1309d11d12aSMateusz Kulikowski core_major >>= SDCC_MCI_VERSION_MAJOR_SHIFT;
1319d11d12aSMateusz Kulikowski
1329d11d12aSMateusz Kulikowski core_minor = core_version & SDCC_MCI_VERSION_MINOR_MASK;
1339d11d12aSMateusz Kulikowski
1349d11d12aSMateusz Kulikowski /*
1359d11d12aSMateusz Kulikowski * Support for some capabilities is not advertised by newer
1369d11d12aSMateusz Kulikowski * controller versions and must be explicitly enabled.
1379d11d12aSMateusz Kulikowski */
1389d11d12aSMateusz Kulikowski if (core_major >= 1 && core_minor != 0x11 && core_minor != 0x12) {
13912293f6dSSimon Glass caps = readl(host->ioaddr + SDHCI_CAPABILITIES);
1409d11d12aSMateusz Kulikowski caps |= SDHCI_CAN_VDD_300 | SDHCI_CAN_DO_8BIT;
1419d11d12aSMateusz Kulikowski writel(caps, host->ioaddr + SDHCI_VENDOR_SPEC_CAPABILITIES0);
1429d11d12aSMateusz Kulikowski }
1439d11d12aSMateusz Kulikowski
14414bed52dSJaehoon Chung ret = sdhci_setup_cfg(&plat->cfg, host, 0, 0);
14512293f6dSSimon Glass host->mmc = &plat->mmc;
146eb9d3ca3SMateusz Kulikowski if (ret)
147eb9d3ca3SMateusz Kulikowski return ret;
14812293f6dSSimon Glass host->mmc->priv = &prv->host;
149eb9d3ca3SMateusz Kulikowski host->mmc->dev = dev;
15012293f6dSSimon Glass upriv->mmc = host->mmc;
151eb9d3ca3SMateusz Kulikowski
15212293f6dSSimon Glass return sdhci_probe(dev);
1539d11d12aSMateusz Kulikowski }
1549d11d12aSMateusz Kulikowski
msm_sdc_remove(struct udevice * dev)1559d11d12aSMateusz Kulikowski static int msm_sdc_remove(struct udevice *dev)
1569d11d12aSMateusz Kulikowski {
1579d11d12aSMateusz Kulikowski struct msm_sdhc *priv = dev_get_priv(dev);
1589d11d12aSMateusz Kulikowski
1599d11d12aSMateusz Kulikowski /* Disable host-controller mode */
1609d11d12aSMateusz Kulikowski writel(0, priv->base + SDCC_MCI_HC_MODE);
1619d11d12aSMateusz Kulikowski
1629d11d12aSMateusz Kulikowski return 0;
1639d11d12aSMateusz Kulikowski }
1649d11d12aSMateusz Kulikowski
msm_ofdata_to_platdata(struct udevice * dev)1659d11d12aSMateusz Kulikowski static int msm_ofdata_to_platdata(struct udevice *dev)
1669d11d12aSMateusz Kulikowski {
1679d11d12aSMateusz Kulikowski struct udevice *parent = dev->parent;
1689d11d12aSMateusz Kulikowski struct msm_sdhc *priv = dev_get_priv(dev);
1699d11d12aSMateusz Kulikowski struct sdhci_host *host = &priv->host;
170e160f7d4SSimon Glass int node = dev_of_offset(dev);
1719d11d12aSMateusz Kulikowski
1729d11d12aSMateusz Kulikowski host->name = strdup(dev->name);
173a821c4afSSimon Glass host->ioaddr = (void *)devfdt_get_addr(dev);
174e160f7d4SSimon Glass host->bus_width = fdtdec_get_int(gd->fdt_blob, node, "bus-width", 4);
175e160f7d4SSimon Glass host->index = fdtdec_get_uint(gd->fdt_blob, node, "index", 0);
1769d11d12aSMateusz Kulikowski priv->base = (void *)fdtdec_get_addr_size_auto_parent(gd->fdt_blob,
177e160f7d4SSimon Glass dev_of_offset(parent), node, "reg", 1, NULL, false);
1789d11d12aSMateusz Kulikowski if (priv->base == (void *)FDT_ADDR_T_NONE ||
1799d11d12aSMateusz Kulikowski host->ioaddr == (void *)FDT_ADDR_T_NONE)
1809d11d12aSMateusz Kulikowski return -EINVAL;
1819d11d12aSMateusz Kulikowski
1829d11d12aSMateusz Kulikowski return 0;
1839d11d12aSMateusz Kulikowski }
1849d11d12aSMateusz Kulikowski
msm_sdc_bind(struct udevice * dev)18512293f6dSSimon Glass static int msm_sdc_bind(struct udevice *dev)
18612293f6dSSimon Glass {
18712293f6dSSimon Glass struct msm_sdhc_plat *plat = dev_get_platdata(dev);
18812293f6dSSimon Glass
18924f5aec3SMasahiro Yamada return sdhci_bind(dev, &plat->mmc, &plat->cfg);
19012293f6dSSimon Glass }
19112293f6dSSimon Glass
1929d11d12aSMateusz Kulikowski static const struct udevice_id msm_mmc_ids[] = {
1939d11d12aSMateusz Kulikowski { .compatible = "qcom,sdhci-msm-v4" },
1949d11d12aSMateusz Kulikowski { }
1959d11d12aSMateusz Kulikowski };
1969d11d12aSMateusz Kulikowski
1979d11d12aSMateusz Kulikowski U_BOOT_DRIVER(msm_sdc_drv) = {
1989d11d12aSMateusz Kulikowski .name = "msm_sdc",
1999d11d12aSMateusz Kulikowski .id = UCLASS_MMC,
2009d11d12aSMateusz Kulikowski .of_match = msm_mmc_ids,
2019d11d12aSMateusz Kulikowski .ofdata_to_platdata = msm_ofdata_to_platdata,
20212293f6dSSimon Glass .ops = &sdhci_ops,
20312293f6dSSimon Glass .bind = msm_sdc_bind,
2049d11d12aSMateusz Kulikowski .probe = msm_sdc_probe,
2059d11d12aSMateusz Kulikowski .remove = msm_sdc_remove,
2069d11d12aSMateusz Kulikowski .priv_auto_alloc_size = sizeof(struct msm_sdhc),
20712293f6dSSimon Glass .platdata_auto_alloc_size = sizeof(struct msm_sdhc_plat),
2089d11d12aSMateusz Kulikowski };
209