1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2012 SAMSUNG Electronics
4 * Jaehoon Chung <jh80.chung@samsung.com>
13 #include <asm/global_data.h>
14 #include <linux/libfdt.h>
16 #include <asm/arch/mmc.h>
17 #include <asm/arch/clk.h>
19 #include <asm/arch/pinmux.h>
22 struct s5p_sdhci_plat
{
23 struct mmc_config cfg
;
27 DECLARE_GLOBAL_DATA_PTR
;
30 static char *S5P_NAME
= "SAMSUNG SDHCI";
31 static void s5p_sdhci_set_control_reg(struct sdhci_host
*host
)
33 unsigned long val
, ctrl
;
41 sdhci_writel(host
, SDHCI_CTRL4_DRIVE_MASK(0x3), SDHCI_CONTROL4
);
43 val
= sdhci_readl(host
, SDHCI_CONTROL2
);
44 val
&= SDHCI_CTRL2_SELBASECLK_MASK(3);
46 val
|= SDHCI_CTRL2_ENSTAASYNCCLR
|
47 SDHCI_CTRL2_ENCMDCNFMSK
|
48 SDHCI_CTRL2_ENFBCLKRX
|
49 SDHCI_CTRL2_ENCLKOUTHOLD
;
51 sdhci_writel(host
, val
, SDHCI_CONTROL2
);
54 * FCSEL3[31] FCSEL2[23] FCSEL1[15] FCSEL0[7]
55 * FCSel[1:0] : Rx Feedback Clock Delay Control
56 * Inverter delay means10ns delay if SDCLK 50MHz setting
57 * 01 = Delay1 (basic delay)
58 * 11 = Delay2 (basic delay + 2ns)
59 * 00 = Delay3 (inverter delay)
60 * 10 = Delay4 (inverter delay + 2ns)
62 val
= SDHCI_CTRL3_FCSEL0
| SDHCI_CTRL3_FCSEL1
;
63 sdhci_writel(host
, val
, SDHCI_CONTROL3
);
71 ctrl
= sdhci_readl(host
, SDHCI_CONTROL2
);
72 ctrl
&= ~SDHCI_CTRL2_SELBASECLK_MASK(0x3);
73 ctrl
|= SDHCI_CTRL2_SELBASECLK_MASK(0x2);
74 sdhci_writel(host
, ctrl
, SDHCI_CONTROL2
);
77 static void s5p_set_clock(struct sdhci_host
*host
, u32 div
)
79 /* ToDo : Use the Clock Framework */
80 set_mmc_clk(host
->index
, div
);
83 static const struct sdhci_ops s5p_sdhci_ops
= {
84 .set_clock
= &s5p_set_clock
,
85 .set_control_reg
= &s5p_sdhci_set_control_reg
,
88 static int s5p_sdhci_core_init(struct sdhci_host
*host
)
90 host
->name
= S5P_NAME
;
92 host
->quirks
= SDHCI_QUIRK_NO_HISPD_BIT
| SDHCI_QUIRK_BROKEN_VOLTAGE
|
93 SDHCI_QUIRK_BROKEN_R1B
| SDHCI_QUIRK_32BIT_DMA_ADDR
|
94 SDHCI_QUIRK_WAIT_SEND_CMD
| SDHCI_QUIRK_USE_WIDE8
;
95 host
->max_clk
= 52000000;
96 host
->voltages
= MMC_VDD_32_33
| MMC_VDD_33_34
| MMC_VDD_165_195
;
97 host
->ops
= &s5p_sdhci_ops
;
99 if (host
->bus_width
== 8)
100 host
->host_caps
|= MMC_MODE_8BIT
;
103 return add_sdhci(host
, 0, 400000);
109 int s5p_sdhci_init(u32 regbase
, int index
, int bus_width
)
111 struct sdhci_host
*host
= calloc(1, sizeof(struct sdhci_host
));
113 printf("sdhci__host allocation fail!\n");
116 host
->ioaddr
= (void *)regbase
;
118 host
->bus_width
= bus_width
;
120 return s5p_sdhci_core_init(host
);
123 static int do_sdhci_init(struct sdhci_host
*host
)
125 int dev_id
, flag
, ret
;
127 flag
= host
->bus_width
== 8 ? PINMUX_FLAG_8BIT_MODE
: PINMUX_FLAG_NONE
;
128 dev_id
= host
->index
+ PERIPH_ID_SDMMC0
;
130 ret
= exynos_pinmux_config(dev_id
, flag
);
132 printf("external SD not configured\n");
136 if (dm_gpio_is_valid(&host
->pwr_gpio
)) {
137 dm_gpio_set_value(&host
->pwr_gpio
, 1);
138 ret
= exynos_pinmux_config(dev_id
, flag
);
140 debug("MMC not configured\n");
145 if (dm_gpio_is_valid(&host
->cd_gpio
)) {
146 ret
= dm_gpio_get_value(&host
->cd_gpio
);
148 debug("no SD card detected (%d)\n", ret
);
153 return s5p_sdhci_core_init(host
);
156 static int sdhci_get_config(const void *blob
, int node
, struct sdhci_host
*host
)
158 int bus_width
, dev_id
;
162 dev_id
= pinmux_decode_periph_id(blob
, node
);
163 if (dev_id
< PERIPH_ID_SDMMC0
|| dev_id
> PERIPH_ID_SDMMC3
) {
164 debug("MMC: Can't get device id\n");
167 host
->index
= dev_id
- PERIPH_ID_SDMMC0
;
170 bus_width
= fdtdec_get_int(blob
, node
, "samsung,bus-width", 0);
171 if (bus_width
<= 0) {
172 debug("MMC: Can't get bus-width\n");
175 host
->bus_width
= bus_width
;
177 /* Get the base address from the device node */
178 base
= fdtdec_get_addr(blob
, node
, "reg");
180 debug("MMC: Can't get base address\n");
183 host
->ioaddr
= (void *)base
;
185 gpio_request_by_name_nodev(offset_to_ofnode(node
), "pwr-gpios", 0,
186 &host
->pwr_gpio
, GPIOD_IS_OUT
);
187 gpio_request_by_name_nodev(offset_to_ofnode(node
), "cd-gpios", 0,
188 &host
->cd_gpio
, GPIOD_IS_IN
);
194 static int s5p_sdhci_probe(struct udevice
*dev
)
196 struct s5p_sdhci_plat
*plat
= dev_get_plat(dev
);
197 struct mmc_uclass_priv
*upriv
= dev_get_uclass_priv(dev
);
198 struct sdhci_host
*host
= dev_get_priv(dev
);
201 ret
= sdhci_get_config(gd
->fdt_blob
, dev_of_offset(dev
), host
);
205 ret
= do_sdhci_init(host
);
209 ret
= mmc_of_parse(dev
, &plat
->cfg
);
213 host
->mmc
= &plat
->mmc
;
214 host
->mmc
->dev
= dev
;
216 ret
= sdhci_setup_cfg(&plat
->cfg
, host
, 0, 400000);
220 host
->mmc
->priv
= host
;
221 upriv
->mmc
= host
->mmc
;
223 return sdhci_probe(dev
);
226 static int s5p_sdhci_bind(struct udevice
*dev
)
228 struct s5p_sdhci_plat
*plat
= dev_get_plat(dev
);
231 ret
= sdhci_bind(dev
, &plat
->mmc
, &plat
->cfg
);
238 static const struct udevice_id s5p_sdhci_ids
[] = {
239 { .compatible
= "samsung,exynos4412-sdhci"},
243 U_BOOT_DRIVER(s5p_sdhci_drv
) = {
246 .of_match
= s5p_sdhci_ids
,
247 .bind
= s5p_sdhci_bind
,
249 .probe
= s5p_sdhci_probe
,
250 .priv_auto
= sizeof(struct sdhci_host
),
251 .plat_auto
= sizeof(struct s5p_sdhci_plat
),
253 #endif /* CONFIG_DM_MMC */