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 #ifdef CONFIG_OF_CONTROL
18 #include <asm/arch/pinmux.h>
21 static char *S5P_NAME = "SAMSUNG SDHCI";
22 static void s5p_sdhci_set_control_reg(struct sdhci_host *host)
24 unsigned long val, ctrl;
32 sdhci_writel(host, SDHCI_CTRL4_DRIVE_MASK(0x3), SDHCI_CONTROL4);
34 val = sdhci_readl(host, SDHCI_CONTROL2);
35 val &= SDHCI_CTRL2_SELBASECLK_SHIFT;
37 val |= SDHCI_CTRL2_ENSTAASYNCCLR |
38 SDHCI_CTRL2_ENCMDCNFMSK |
39 SDHCI_CTRL2_ENFBCLKRX |
40 SDHCI_CTRL2_ENCLKOUTHOLD;
42 sdhci_writel(host, val, SDHCI_CONTROL2);
45 * FCSEL3[31] FCSEL2[23] FCSEL1[15] FCSEL0[7]
46 * FCSel[1:0] : Rx Feedback Clock Delay Control
47 * Inverter delay means10ns delay if SDCLK 50MHz setting
48 * 01 = Delay1 (basic delay)
49 * 11 = Delay2 (basic delay + 2ns)
50 * 00 = Delay3 (inverter delay)
51 * 10 = Delay4 (inverter delay + 2ns)
53 val = SDHCI_CTRL3_FCSEL0 | SDHCI_CTRL3_FCSEL1;
54 sdhci_writel(host, val, SDHCI_CONTROL3);
62 ctrl = sdhci_readl(host, SDHCI_CONTROL2);
63 ctrl &= ~SDHCI_CTRL2_SELBASECLK_MASK(0x3);
64 ctrl |= SDHCI_CTRL2_SELBASECLK_MASK(0x2);
65 sdhci_writel(host, ctrl, SDHCI_CONTROL2);
68 static int s5p_sdhci_core_init(struct sdhci_host *host)
70 host->name = S5P_NAME;
72 host->quirks = SDHCI_QUIRK_NO_HISPD_BIT | SDHCI_QUIRK_BROKEN_VOLTAGE |
73 SDHCI_QUIRK_BROKEN_R1B | SDHCI_QUIRK_32BIT_DMA_ADDR |
74 SDHCI_QUIRK_WAIT_SEND_CMD | SDHCI_QUIRK_USE_WIDE8;
75 host->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
76 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
78 host->set_control_reg = &s5p_sdhci_set_control_reg;
79 host->set_clock = set_mmc_clk;
81 host->host_caps = MMC_MODE_HC;
82 if (host->bus_width == 8)
83 host->host_caps |= MMC_MODE_8BIT;
85 return add_sdhci(host, 52000000, 400000);
88 int s5p_sdhci_init(u32 regbase, int index, int bus_width)
90 struct sdhci_host *host = malloc(sizeof(struct sdhci_host));
92 printf("sdhci__host malloc fail!\n");
95 host->ioaddr = (void *)regbase;
97 host->bus_width = bus_width;
99 return s5p_sdhci_core_init(host);
102 #ifdef CONFIG_OF_CONTROL
103 struct sdhci_host sdhci_host[SDHCI_MAX_HOSTS];
105 static int do_sdhci_init(struct sdhci_host *host)
110 flag = host->bus_width == 8 ? PINMUX_FLAG_8BIT_MODE : PINMUX_FLAG_NONE;
111 dev_id = host->index + PERIPH_ID_SDMMC0;
113 if (fdt_gpio_isvalid(&host->pwr_gpio)) {
114 gpio_direction_output(host->pwr_gpio.gpio, 1);
115 err = exynos_pinmux_config(dev_id, flag);
117 debug("MMC not configured\n");
122 if (fdt_gpio_isvalid(&host->cd_gpio)) {
123 gpio_direction_output(host->cd_gpio.gpio, 0xf);
124 if (gpio_get_value(host->cd_gpio.gpio))
127 err = exynos_pinmux_config(dev_id, flag);
129 printf("external SD not configured\n");
134 return s5p_sdhci_core_init(host);
137 static int sdhci_get_config(const void *blob, int node, struct sdhci_host *host)
139 int bus_width, dev_id;
143 dev_id = pinmux_decode_periph_id(blob, node);
144 if (dev_id < PERIPH_ID_SDMMC0 && dev_id > PERIPH_ID_SDMMC3) {
145 debug("MMC: Can't get device id\n");
148 host->index = dev_id - PERIPH_ID_SDMMC0;
151 bus_width = fdtdec_get_int(blob, node, "samsung,bus-width", 0);
152 if (bus_width <= 0) {
153 debug("MMC: Can't get bus-width\n");
156 host->bus_width = bus_width;
158 /* Get the base address from the device node */
159 base = fdtdec_get_addr(blob, node, "reg");
161 debug("MMC: Can't get base address\n");
164 host->ioaddr = (void *)base;
166 fdtdec_decode_gpio(blob, node, "pwr-gpios", &host->pwr_gpio);
167 fdtdec_decode_gpio(blob, node, "cd-gpios", &host->cd_gpio);
172 static int process_nodes(const void *blob, int node_list[], int count)
174 struct sdhci_host *host;
177 debug("%s: count = %d\n", __func__, count);
179 /* build sdhci_host[] for each controller */
180 for (i = 0; i < count; i++) {
185 host = &sdhci_host[i];
187 if (sdhci_get_config(blob, node, host)) {
188 printf("%s: failed to decode dev %d\n", __func__, i);
196 int exynos_mmc_init(const void *blob)
199 int node_list[SDHCI_MAX_HOSTS];
201 count = fdtdec_find_aliases_for_id(blob, "mmc",
202 COMPAT_SAMSUNG_EXYNOS_MMC, node_list,
205 process_nodes(blob, node_list, count);