1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2012 SAMSUNG Electronics
4 * Jaehoon Chung <jh80.chung@samsung.com>
12 #include <linux/libfdt.h>
14 #include <asm/arch/mmc.h>
15 #include <asm/arch/clk.h>
17 #include <asm/arch/pinmux.h>
20 struct s5p_sdhci_plat {
21 struct mmc_config cfg;
25 DECLARE_GLOBAL_DATA_PTR;
28 static char *S5P_NAME = "SAMSUNG SDHCI";
29 static void s5p_sdhci_set_control_reg(struct sdhci_host *host)
31 unsigned long val, ctrl;
39 sdhci_writel(host, SDHCI_CTRL4_DRIVE_MASK(0x3), SDHCI_CONTROL4);
41 val = sdhci_readl(host, SDHCI_CONTROL2);
42 val &= SDHCI_CTRL2_SELBASECLK_MASK(3);
44 val |= SDHCI_CTRL2_ENSTAASYNCCLR |
45 SDHCI_CTRL2_ENCMDCNFMSK |
46 SDHCI_CTRL2_ENFBCLKRX |
47 SDHCI_CTRL2_ENCLKOUTHOLD;
49 sdhci_writel(host, val, SDHCI_CONTROL2);
52 * FCSEL3[31] FCSEL2[23] FCSEL1[15] FCSEL0[7]
53 * FCSel[1:0] : Rx Feedback Clock Delay Control
54 * Inverter delay means10ns delay if SDCLK 50MHz setting
55 * 01 = Delay1 (basic delay)
56 * 11 = Delay2 (basic delay + 2ns)
57 * 00 = Delay3 (inverter delay)
58 * 10 = Delay4 (inverter delay + 2ns)
60 val = SDHCI_CTRL3_FCSEL0 | SDHCI_CTRL3_FCSEL1;
61 sdhci_writel(host, val, SDHCI_CONTROL3);
69 ctrl = sdhci_readl(host, SDHCI_CONTROL2);
70 ctrl &= ~SDHCI_CTRL2_SELBASECLK_MASK(0x3);
71 ctrl |= SDHCI_CTRL2_SELBASECLK_MASK(0x2);
72 sdhci_writel(host, ctrl, SDHCI_CONTROL2);
75 static void s5p_set_clock(struct sdhci_host *host, u32 div)
77 /* ToDo : Use the Clock Framework */
78 set_mmc_clk(host->index, div);
81 static const struct sdhci_ops s5p_sdhci_ops = {
82 .set_clock = &s5p_set_clock,
83 .set_control_reg = &s5p_sdhci_set_control_reg,
86 static int s5p_sdhci_core_init(struct sdhci_host *host)
88 host->name = S5P_NAME;
90 host->quirks = SDHCI_QUIRK_NO_HISPD_BIT | SDHCI_QUIRK_BROKEN_VOLTAGE |
91 SDHCI_QUIRK_32BIT_DMA_ADDR |
92 SDHCI_QUIRK_WAIT_SEND_CMD | SDHCI_QUIRK_USE_WIDE8;
93 host->max_clk = 52000000;
94 host->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
95 host->ops = &s5p_sdhci_ops;
97 if (host->bus_width == 8)
98 host->host_caps |= MMC_MODE_8BIT;
101 return add_sdhci(host, 0, 400000);
107 int s5p_sdhci_init(u32 regbase, int index, int bus_width)
109 struct sdhci_host *host = calloc(1, sizeof(struct sdhci_host));
111 printf("sdhci__host allocation fail!\n");
114 host->ioaddr = (void *)regbase;
116 host->bus_width = bus_width;
118 return s5p_sdhci_core_init(host);
121 static int do_sdhci_init(struct sdhci_host *host)
123 int dev_id, flag, ret;
125 flag = host->bus_width == 8 ? PINMUX_FLAG_8BIT_MODE : PINMUX_FLAG_NONE;
126 dev_id = host->index + PERIPH_ID_SDMMC0;
128 ret = exynos_pinmux_config(dev_id, flag);
130 printf("external SD not configured\n");
134 if (dm_gpio_is_valid(&host->pwr_gpio)) {
135 dm_gpio_set_value(&host->pwr_gpio, 1);
136 ret = exynos_pinmux_config(dev_id, flag);
138 debug("MMC not configured\n");
143 if (dm_gpio_is_valid(&host->cd_gpio)) {
144 ret = dm_gpio_get_value(&host->cd_gpio);
146 debug("no SD card detected (%d)\n", ret);
151 return s5p_sdhci_core_init(host);
154 static int sdhci_get_config(const void *blob, int node, struct sdhci_host *host)
156 int bus_width, dev_id;
160 dev_id = pinmux_decode_periph_id(blob, node);
161 if (dev_id < PERIPH_ID_SDMMC0 || dev_id > PERIPH_ID_SDMMC3) {
162 debug("MMC: Can't get device id\n");
165 host->index = dev_id - PERIPH_ID_SDMMC0;
168 bus_width = fdtdec_get_int(blob, node, "samsung,bus-width", 0);
169 if (bus_width <= 0) {
170 debug("MMC: Can't get bus-width\n");
173 host->bus_width = bus_width;
175 /* Get the base address from the device node */
176 base = fdtdec_get_addr(blob, node, "reg");
178 debug("MMC: Can't get base address\n");
181 host->ioaddr = (void *)base;
183 gpio_request_by_name_nodev(offset_to_ofnode(node), "pwr-gpios", 0,
184 &host->pwr_gpio, GPIOD_IS_OUT);
185 gpio_request_by_name_nodev(offset_to_ofnode(node), "cd-gpios", 0,
186 &host->cd_gpio, GPIOD_IS_IN);
192 static int s5p_sdhci_probe(struct udevice *dev)
194 struct s5p_sdhci_plat *plat = dev_get_platdata(dev);
195 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
196 struct sdhci_host *host = dev_get_priv(dev);
199 ret = sdhci_get_config(gd->fdt_blob, dev_of_offset(dev), host);
203 ret = do_sdhci_init(host);
207 host->mmc = &plat->mmc;
208 host->mmc->dev = dev;
209 ret = sdhci_setup_cfg(&plat->cfg, host, 0, 400000);
213 host->mmc->priv = host;
214 upriv->mmc = host->mmc;
216 return sdhci_probe(dev);
219 static int s5p_sdhci_bind(struct udevice *dev)
221 struct s5p_sdhci_plat *plat = dev_get_platdata(dev);
224 ret = sdhci_bind(dev, &plat->mmc, &plat->cfg);
231 static const struct udevice_id s5p_sdhci_ids[] = {
232 { .compatible = "samsung,exynos4412-sdhci"},
236 U_BOOT_DRIVER(s5p_sdhci_drv) = {
239 .of_match = s5p_sdhci_ids,
240 .bind = s5p_sdhci_bind,
242 .probe = s5p_sdhci_probe,
243 .priv_auto_alloc_size = sizeof(struct sdhci_host),
244 .platdata_auto_alloc_size = sizeof(struct s5p_sdhci_plat),
246 #endif /* CONFIG_DM_MMC */