]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
mmc: cv1800b: Add sdhci driver support for cv1800b SoC
authorKongyang Liu <seashell11234455@gmail.com>
Sat, 9 Mar 2024 17:51:55 +0000 (01:51 +0800)
committerLeo Yu-Chi Liang <ycliang@andestech.com>
Tue, 9 Apr 2024 03:30:10 +0000 (11:30 +0800)
Add sdhci driver for cv1800b SoC.

Signed-off-by: Kongyang Liu <seashell11234455@gmail.com>
Reviewed-by: Leo Yu-Chi Liang <ycliang@andestech.com>
drivers/mmc/Kconfig
drivers/mmc/Makefile
drivers/mmc/cv1800b_sdhci.c [new file with mode: 0644]

index cef05790dd9df619ea8f1deabfbeb5f4da54fcf9..f7fe6d1042e325524c0dabfa710c4eb4763c2e5d 100644 (file)
@@ -568,6 +568,19 @@ config MMC_SDHCI_CADENCE
 
          If unsure, say N.
 
+config MMC_SDHCI_CV1800B
+       bool "SDHCI support for the CV1800B SD/SDIO/eMMC controller"
+       depends on BLK && DM_MMC
+       depends on MMC_SDHCI
+       depends on OF_CONTROL
+       help
+         This selects the CV1800B SD/SDIO/eMMC driver.
+
+         If you have a controller with this interface,
+         say Y here.
+
+         If unsure, say N.
+
 config MMC_SDHCI_AM654
        bool "SDHCI Controller on TI's Am654 devices"
        depends on ARCH_K3
index e9cf1fcc6400889eb241fc7e0187c6b2b8cc7a6d..3374321e290c37f4f179fffe78ea28a9e384f665 100644 (file)
@@ -60,6 +60,7 @@ obj-$(CONFIG_MMC_SDHCI_ATMEL)         += atmel_sdhci.o
 obj-$(CONFIG_MMC_SDHCI_BCM2835)                += bcm2835_sdhci.o
 obj-$(CONFIG_MMC_SDHCI_BCMSTB)         += bcmstb_sdhci.o
 obj-$(CONFIG_MMC_SDHCI_CADENCE)                += sdhci-cadence.o
+obj-$(CONFIG_MMC_SDHCI_CV1800B)                += cv1800b_sdhci.o
 obj-$(CONFIG_MMC_SDHCI_AM654)          += am654_sdhci.o
 obj-$(CONFIG_MMC_SDHCI_IPROC)          += iproc_sdhci.o
 obj-$(CONFIG_MMC_SDHCI_KONA)           += kona_sdhci.o
diff --git a/drivers/mmc/cv1800b_sdhci.c b/drivers/mmc/cv1800b_sdhci.c
new file mode 100644 (file)
index 0000000..2275c53
--- /dev/null
@@ -0,0 +1,116 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2024, Kongyang Liu <seashell11234455@gmail.com>
+ */
+
+#include <dm.h>
+#include <mmc.h>
+#include <sdhci.h>
+#include <linux/delay.h>
+
+#define SDHCI_PHY_TX_RX_DLY  0x240
+#define MMC_MAX_CLOCK        375000000
+#define TUNE_MAX_PHCODE      128
+
+struct cv1800b_sdhci_plat {
+       struct mmc_config cfg;
+       struct mmc mmc;
+};
+
+static void cv1800b_set_tap_delay(struct sdhci_host *host, u16 tap)
+{
+       sdhci_writel(host, tap << 16, SDHCI_PHY_TX_RX_DLY);
+}
+
+static void cv1800b_sdhci_reset(struct sdhci_host *host, u8 mask)
+{
+       sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
+       while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask)
+               udelay(10);
+}
+
+static int cv1800b_execute_tuning(struct mmc *mmc, u8 opcode)
+{
+       struct sdhci_host *host = dev_get_priv(mmc->dev);
+
+       u16 tap;
+
+       int current_size = 0;
+       int max_size = 0;
+       int max_window = 0;
+
+       for (tap = 0; tap < TUNE_MAX_PHCODE; tap++) {
+               cv1800b_set_tap_delay(host, tap);
+
+               if (mmc_send_tuning(host->mmc, opcode, NULL)) {
+                       current_size = 0;
+               } else {
+                       current_size++;
+                       if (current_size > max_size) {
+                               max_size = current_size;
+                               max_window = tap;
+                       }
+               }
+       }
+
+       cv1800b_sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
+
+       cv1800b_set_tap_delay(host, max_window - max_size / 2);
+
+       return 0;
+}
+
+const struct sdhci_ops cv1800b_sdhci_sd_ops = {
+       .platform_execute_tuning = cv1800b_execute_tuning,
+};
+
+static int cv1800b_sdhci_bind(struct udevice *dev)
+{
+       struct cv1800b_sdhci_plat *plat = dev_get_plat(dev);
+
+       return sdhci_bind(dev, &plat->mmc, &plat->cfg);
+}
+
+static int cv1800b_sdhci_probe(struct udevice *dev)
+{
+       struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
+       struct cv1800b_sdhci_plat *plat = dev_get_plat(dev);
+       struct sdhci_host *host = dev_get_priv(dev);
+       int ret;
+
+       host->name = dev->name;
+       host->ioaddr = devfdt_get_addr_ptr(dev);
+
+       upriv->mmc = &plat->mmc;
+       host->mmc = &plat->mmc;
+       host->mmc->priv = host;
+       host->mmc->dev = dev;
+       host->ops = &cv1800b_sdhci_sd_ops;
+       host->max_clk = MMC_MAX_CLOCK;
+
+       ret = mmc_of_parse(dev, &plat->cfg);
+       if (ret)
+               return ret;
+
+       ret = sdhci_setup_cfg(&plat->cfg, host, 0, 200000);
+       if (ret)
+               return ret;
+
+       return sdhci_probe(dev);
+}
+
+static const struct udevice_id cv1800b_sdhci_match[] = {
+       { .compatible = "sophgo,cv1800b-dwcmshc" },
+       { }
+};
+
+U_BOOT_DRIVER(cv1800b_sdhci) = {
+       .name = "sdhci-cv1800b",
+       .id = UCLASS_MMC,
+       .of_match = cv1800b_sdhci_match,
+       .bind = cv1800b_sdhci_bind,
+       .probe = cv1800b_sdhci_probe,
+       .priv_auto = sizeof(struct sdhci_host),
+       .plat_auto = sizeof(struct cv1800b_sdhci_plat),
+       .ops = &sdhci_ops,
+};