]> git.sur5r.net Git - u-boot/blobdiff - drivers/mmc/s5p_sdhci.c
Merge branch 'master' of git://git.denx.de/u-boot-spi
[u-boot] / drivers / mmc / s5p_sdhci.c
index 0dea45d079f33e9c4f8b3b55288d7f508e4abc2d..591a3bce0849f1b9302d4c7c8bd1ad69dedfc466 100644 (file)
@@ -1,21 +1,30 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * (C) Copyright 2012 SAMSUNG Electronics
  * Jaehoon Chung <jh80.chung@samsung.com>
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #include <common.h>
+#include <dm.h>
 #include <malloc.h>
 #include <sdhci.h>
 #include <fdtdec.h>
-#include <libfdt.h>
+#include <linux/libfdt.h>
 #include <asm/gpio.h>
 #include <asm/arch/mmc.h>
 #include <asm/arch/clk.h>
 #include <errno.h>
 #include <asm/arch/pinmux.h>
 
+#ifdef CONFIG_DM_MMC
+struct s5p_sdhci_plat {
+       struct mmc_config cfg;
+       struct mmc mmc;
+};
+
+DECLARE_GLOBAL_DATA_PTR;
+#endif
+
 static char *S5P_NAME = "SAMSUNG SDHCI";
 static void s5p_sdhci_set_control_reg(struct sdhci_host *host)
 {
@@ -30,7 +39,7 @@ static void s5p_sdhci_set_control_reg(struct sdhci_host *host)
        sdhci_writel(host, SDHCI_CTRL4_DRIVE_MASK(0x3), SDHCI_CONTROL4);
 
        val = sdhci_readl(host, SDHCI_CONTROL2);
-       val &= SDHCI_CTRL2_SELBASECLK_SHIFT;
+       val &= SDHCI_CTRL2_SELBASECLK_MASK(3);
 
        val |=  SDHCI_CTRL2_ENSTAASYNCCLR |
                SDHCI_CTRL2_ENCMDCNFMSK |
@@ -63,32 +72,44 @@ static void s5p_sdhci_set_control_reg(struct sdhci_host *host)
        sdhci_writel(host, ctrl, SDHCI_CONTROL2);
 }
 
+static void s5p_set_clock(struct sdhci_host *host, u32 div)
+{
+       /* ToDo : Use the Clock Framework */
+       set_mmc_clk(host->index, div);
+}
+
+static const struct sdhci_ops s5p_sdhci_ops = {
+       .set_clock      = &s5p_set_clock,
+       .set_control_reg = &s5p_sdhci_set_control_reg,
+};
+
 static int s5p_sdhci_core_init(struct sdhci_host *host)
 {
        host->name = S5P_NAME;
 
        host->quirks = SDHCI_QUIRK_NO_HISPD_BIT | SDHCI_QUIRK_BROKEN_VOLTAGE |
-               SDHCI_QUIRK_BROKEN_R1B | SDHCI_QUIRK_32BIT_DMA_ADDR |
+               SDHCI_QUIRK_32BIT_DMA_ADDR |
                SDHCI_QUIRK_WAIT_SEND_CMD | SDHCI_QUIRK_USE_WIDE8;
+       host->max_clk = 52000000;
        host->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
-       host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
+       host->ops = &s5p_sdhci_ops;
 
-       host->set_control_reg = &s5p_sdhci_set_control_reg;
-       host->set_clock = set_mmc_clk;
-
-       host->host_caps = MMC_MODE_HC;
        if (host->bus_width == 8)
                host->host_caps |= MMC_MODE_8BIT;
 
-       return add_sdhci(host, 52000000, 400000);
+#ifndef CONFIG_BLK
+       return add_sdhci(host, 0, 400000);
+#else
+       return 0;
+#endif
 }
 
 int s5p_sdhci_init(u32 regbase, int index, int bus_width)
 {
-       struct sdhci_host *host = malloc(sizeof(struct sdhci_host));
+       struct sdhci_host *host = calloc(1, sizeof(struct sdhci_host));
        if (!host) {
-               printf("sdhci__host malloc fail!\n");
-               return 1;
+               printf("sdhci__host allocation fail!\n");
+               return -ENOMEM;
        }
        host->ioaddr = (void *)regbase;
        host->index = index;
@@ -97,40 +118,36 @@ int s5p_sdhci_init(u32 regbase, int index, int bus_width)
        return s5p_sdhci_core_init(host);
 }
 
-#ifdef CONFIG_OF_CONTROL
+#if CONFIG_IS_ENABLED(OF_CONTROL)
 struct sdhci_host sdhci_host[SDHCI_MAX_HOSTS];
 
 static int do_sdhci_init(struct sdhci_host *host)
 {
-       char str[20];
-       int dev_id, flag;
-       int err = 0;
+       int dev_id, flag, ret;
 
        flag = host->bus_width == 8 ? PINMUX_FLAG_8BIT_MODE : PINMUX_FLAG_NONE;
        dev_id = host->index + PERIPH_ID_SDMMC0;
 
-       if (fdt_gpio_isvalid(&host->pwr_gpio)) {
-               sprintf(str, "sdhci%d_power", host->index & 0xf);
-               gpio_request(host->pwr_gpio.gpio, str);
-               gpio_direction_output(host->pwr_gpio.gpio, 1);
-               err = exynos_pinmux_config(dev_id, flag);
-               if (err) {
+       ret = exynos_pinmux_config(dev_id, flag);
+       if (ret) {
+               printf("external SD not configured\n");
+               return ret;
+       }
+
+       if (dm_gpio_is_valid(&host->pwr_gpio)) {
+               dm_gpio_set_value(&host->pwr_gpio, 1);
+               ret = exynos_pinmux_config(dev_id, flag);
+               if (ret) {
                        debug("MMC not configured\n");
-                       return err;
+                       return ret;
                }
        }
 
-       if (fdt_gpio_isvalid(&host->cd_gpio)) {
-               sprintf(str, "sdhci%d_cd", host->index & 0xf);
-               gpio_request(host->cd_gpio.gpio, str);
-               gpio_direction_output(host->cd_gpio.gpio, 1);
-               if (gpio_get_value(host->cd_gpio.gpio))
+       if (dm_gpio_is_valid(&host->cd_gpio)) {
+               ret = dm_gpio_get_value(&host->cd_gpio);
+               if (ret) {
+                       debug("no SD card detected (%d)\n", ret);
                        return -ENODEV;
-
-               err = exynos_pinmux_config(dev_id, flag);
-               if (err) {
-                       printf("external SD not configured\n");
-                       return err;
                }
        }
 
@@ -144,9 +161,9 @@ static int sdhci_get_config(const void *blob, int node, struct sdhci_host *host)
 
        /* Get device id */
        dev_id = pinmux_decode_periph_id(blob, node);
-       if (dev_id < PERIPH_ID_SDMMC0 && dev_id > PERIPH_ID_SDMMC3) {
+       if (dev_id < PERIPH_ID_SDMMC0 || dev_id > PERIPH_ID_SDMMC3) {
                debug("MMC: Can't get device id\n");
-               return -1;
+               return -EINVAL;
        }
        host->index = dev_id - PERIPH_ID_SDMMC0;
 
@@ -154,7 +171,7 @@ static int sdhci_get_config(const void *blob, int node, struct sdhci_host *host)
        bus_width = fdtdec_get_int(blob, node, "samsung,bus-width", 0);
        if (bus_width <= 0) {
                debug("MMC: Can't get bus-width\n");
-               return -1;
+               return -EINVAL;
        }
        host->bus_width = bus_width;
 
@@ -162,12 +179,14 @@ static int sdhci_get_config(const void *blob, int node, struct sdhci_host *host)
        base = fdtdec_get_addr(blob, node, "reg");
        if (!base) {
                debug("MMC: Can't get base address\n");
-               return -1;
+               return -EINVAL;
        }
        host->ioaddr = (void *)base;
 
-       fdtdec_decode_gpio(blob, node, "pwr-gpios", &host->pwr_gpio);
-       fdtdec_decode_gpio(blob, node, "cd-gpios", &host->cd_gpio);
+       gpio_request_by_name_nodev(offset_to_ofnode(node), "pwr-gpios", 0,
+                                  &host->pwr_gpio, GPIOD_IS_OUT);
+       gpio_request_by_name_nodev(offset_to_ofnode(node), "cd-gpios", 0,
+                                  &host->cd_gpio, GPIOD_IS_IN);
 
        return 0;
 }
@@ -175,7 +194,8 @@ static int sdhci_get_config(const void *blob, int node, struct sdhci_host *host)
 static int process_nodes(const void *blob, int node_list[], int count)
 {
        struct sdhci_host *host;
-       int i, node;
+       int i, node, ret;
+       int failed = 0;
 
        debug("%s: count = %d\n", __func__, count);
 
@@ -187,13 +207,22 @@ static int process_nodes(const void *blob, int node_list[], int count)
 
                host = &sdhci_host[i];
 
-               if (sdhci_get_config(blob, node, host)) {
-                       printf("%s: failed to decode dev %d\n", __func__, i);
-                       return -1;
+               ret = sdhci_get_config(blob, node, host);
+               if (ret) {
+                       printf("%s: failed to decode dev %d (%d)\n",    __func__, i, ret);
+                       failed++;
+                       continue;
+               }
+
+               ret = do_sdhci_init(host);
+               if (ret && ret != -ENODEV) {
+                       printf("%s: failed to initialize dev %d (%d)\n", __func__, i, ret);
+                       failed++;
                }
-               do_sdhci_init(host);
        }
-       return 0;
+
+       /* we only consider it an error when all nodes fail */
+       return (failed == count ? -1 : 0);
 }
 
 int exynos_mmc_init(const void *blob)
@@ -205,8 +234,63 @@ int exynos_mmc_init(const void *blob)
                        COMPAT_SAMSUNG_EXYNOS_MMC, node_list,
                        SDHCI_MAX_HOSTS);
 
-       process_nodes(blob, node_list, count);
-
-       return 1;
+       return process_nodes(blob, node_list, count);
 }
 #endif
+
+#ifdef CONFIG_DM_MMC
+static int s5p_sdhci_probe(struct udevice *dev)
+{
+       struct s5p_sdhci_plat *plat = dev_get_platdata(dev);
+       struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
+       struct sdhci_host *host = dev_get_priv(dev);
+       int ret;
+
+       ret = sdhci_get_config(gd->fdt_blob, dev_of_offset(dev), host);
+       if (ret)
+               return ret;
+
+       ret = do_sdhci_init(host);
+       if (ret)
+               return ret;
+
+       ret = sdhci_setup_cfg(&plat->cfg, host, 0, 400000);
+       if (ret)
+               return ret;
+
+       host->mmc = &plat->mmc;
+       host->mmc->priv = host;
+       host->mmc->dev = dev;
+       upriv->mmc = host->mmc;
+
+       return sdhci_probe(dev);
+}
+
+static int s5p_sdhci_bind(struct udevice *dev)
+{
+       struct s5p_sdhci_plat *plat = dev_get_platdata(dev);
+       int ret;
+
+       ret = sdhci_bind(dev, &plat->mmc, &plat->cfg);
+       if (ret)
+               return ret;
+
+       return 0;
+}
+
+static const struct udevice_id s5p_sdhci_ids[] = {
+       { .compatible = "samsung,exynos4412-sdhci"},
+       { }
+};
+
+U_BOOT_DRIVER(s5p_sdhci_drv) = {
+       .name           = "s5p_sdhci",
+       .id             = UCLASS_MMC,
+       .of_match       = s5p_sdhci_ids,
+       .bind           = s5p_sdhci_bind,
+       .ops            = &sdhci_ops,
+       .probe          = s5p_sdhci_probe,
+       .priv_auto_alloc_size = sizeof(struct sdhci_host),
+       .platdata_auto_alloc_size = sizeof(struct s5p_sdhci_plat),
+};
+#endif /* CONFIG_DM_MMC */