2 * (C) Copyright 2012 SAMSUNG Electronics
3 * Jaehoon Chung <jh80.chung@samsung.com>
5 * SPDX-License-Identifier: GPL-2.0+
14 #include <asm/arch/mmc.h>
15 #include <asm/arch/clk.h>
17 #include <asm/arch/pinmux.h>
19 static char *S5P_NAME = "SAMSUNG SDHCI";
20 static void s5p_sdhci_set_control_reg(struct sdhci_host *host)
22 unsigned long val, ctrl;
30 sdhci_writel(host, SDHCI_CTRL4_DRIVE_MASK(0x3), SDHCI_CONTROL4);
32 val = sdhci_readl(host, SDHCI_CONTROL2);
33 val &= SDHCI_CTRL2_SELBASECLK_MASK(3);
35 val |= SDHCI_CTRL2_ENSTAASYNCCLR |
36 SDHCI_CTRL2_ENCMDCNFMSK |
37 SDHCI_CTRL2_ENFBCLKRX |
38 SDHCI_CTRL2_ENCLKOUTHOLD;
40 sdhci_writel(host, val, SDHCI_CONTROL2);
43 * FCSEL3[31] FCSEL2[23] FCSEL1[15] FCSEL0[7]
44 * FCSel[1:0] : Rx Feedback Clock Delay Control
45 * Inverter delay means10ns delay if SDCLK 50MHz setting
46 * 01 = Delay1 (basic delay)
47 * 11 = Delay2 (basic delay + 2ns)
48 * 00 = Delay3 (inverter delay)
49 * 10 = Delay4 (inverter delay + 2ns)
51 val = SDHCI_CTRL3_FCSEL0 | SDHCI_CTRL3_FCSEL1;
52 sdhci_writel(host, val, SDHCI_CONTROL3);
60 ctrl = sdhci_readl(host, SDHCI_CONTROL2);
61 ctrl &= ~SDHCI_CTRL2_SELBASECLK_MASK(0x3);
62 ctrl |= SDHCI_CTRL2_SELBASECLK_MASK(0x2);
63 sdhci_writel(host, ctrl, SDHCI_CONTROL2);
66 static int s5p_sdhci_core_init(struct sdhci_host *host)
68 host->name = S5P_NAME;
70 host->quirks = SDHCI_QUIRK_NO_HISPD_BIT | SDHCI_QUIRK_BROKEN_VOLTAGE |
71 SDHCI_QUIRK_BROKEN_R1B | SDHCI_QUIRK_32BIT_DMA_ADDR |
72 SDHCI_QUIRK_WAIT_SEND_CMD | SDHCI_QUIRK_USE_WIDE8;
73 host->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
74 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
76 host->set_control_reg = &s5p_sdhci_set_control_reg;
77 host->set_clock = set_mmc_clk;
79 if (host->bus_width == 8)
80 host->host_caps |= MMC_MODE_8BIT;
82 return add_sdhci(host, 52000000, 400000);
85 int s5p_sdhci_init(u32 regbase, int index, int bus_width)
87 struct sdhci_host *host = calloc(1, sizeof(struct sdhci_host));
89 printf("sdhci__host allocation fail!\n");
92 host->ioaddr = (void *)regbase;
94 host->bus_width = bus_width;
96 return s5p_sdhci_core_init(host);
99 #if CONFIG_IS_ENABLED(OF_CONTROL)
100 struct sdhci_host sdhci_host[SDHCI_MAX_HOSTS];
102 static int do_sdhci_init(struct sdhci_host *host)
104 int dev_id, flag, ret;
106 flag = host->bus_width == 8 ? PINMUX_FLAG_8BIT_MODE : PINMUX_FLAG_NONE;
107 dev_id = host->index + PERIPH_ID_SDMMC0;
109 if (dm_gpio_is_valid(&host->pwr_gpio)) {
110 dm_gpio_set_value(&host->pwr_gpio, 1);
111 ret = exynos_pinmux_config(dev_id, flag);
113 debug("MMC not configured\n");
118 if (dm_gpio_is_valid(&host->cd_gpio)) {
119 ret = dm_gpio_get_value(&host->cd_gpio);
121 debug("no SD card detected (%d)\n", ret);
125 ret = exynos_pinmux_config(dev_id, flag);
127 printf("external SD not configured\n");
132 return s5p_sdhci_core_init(host);
135 static int sdhci_get_config(const void *blob, int node, struct sdhci_host *host)
137 int bus_width, dev_id;
141 dev_id = pinmux_decode_periph_id(blob, node);
142 if (dev_id < PERIPH_ID_SDMMC0 && dev_id > PERIPH_ID_SDMMC3) {
143 debug("MMC: Can't get device id\n");
146 host->index = dev_id - PERIPH_ID_SDMMC0;
149 bus_width = fdtdec_get_int(blob, node, "samsung,bus-width", 0);
150 if (bus_width <= 0) {
151 debug("MMC: Can't get bus-width\n");
154 host->bus_width = bus_width;
156 /* Get the base address from the device node */
157 base = fdtdec_get_addr(blob, node, "reg");
159 debug("MMC: Can't get base address\n");
162 host->ioaddr = (void *)base;
164 gpio_request_by_name_nodev(blob, node, "pwr-gpios", 0, &host->pwr_gpio,
166 gpio_request_by_name_nodev(blob, node, "cd-gpios", 0, &host->cd_gpio,
172 static int process_nodes(const void *blob, int node_list[], int count)
174 struct sdhci_host *host;
178 debug("%s: count = %d\n", __func__, count);
180 /* build sdhci_host[] for each controller */
181 for (i = 0; i < count; i++) {
186 host = &sdhci_host[i];
188 ret = sdhci_get_config(blob, node, host);
190 printf("%s: failed to decode dev %d (%d)\n", __func__, i, ret);
195 ret = do_sdhci_init(host);
197 printf("%s: failed to initialize dev %d (%d)\n", __func__, i, ret);
202 /* we only consider it an error when all nodes fail */
203 return (failed == count ? -1 : 0);
206 int exynos_mmc_init(const void *blob)
209 int node_list[SDHCI_MAX_HOSTS];
211 count = fdtdec_find_aliases_for_id(blob, "mmc",
212 COMPAT_SAMSUNG_EXYNOS_MMC, node_list,
215 return process_nodes(blob, node_list, count);