xref: /openbmc/u-boot/drivers/mmc/s5p_sdhci.c (revision 151b8339ccd33428d249e75bd036bd7eb33c1ef6)
1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2442d5568SJaehoon Chung /*
3442d5568SJaehoon Chung  * (C) Copyright 2012 SAMSUNG Electronics
4442d5568SJaehoon Chung  * Jaehoon Chung <jh80.chung@samsung.com>
5442d5568SJaehoon Chung  */
6442d5568SJaehoon Chung 
7442d5568SJaehoon Chung #include <common.h>
87aedafd6SJaehoon Chung #include <dm.h>
9442d5568SJaehoon Chung #include <malloc.h>
10442d5568SJaehoon Chung #include <sdhci.h>
113577fe8bSPiotr Wilczek #include <fdtdec.h>
12b08c8c48SMasahiro Yamada #include <linux/libfdt.h>
133577fe8bSPiotr Wilczek #include <asm/gpio.h>
14442d5568SJaehoon Chung #include <asm/arch/mmc.h>
15b09ed6e4SJaehoon Chung #include <asm/arch/clk.h>
163577fe8bSPiotr Wilczek #include <errno.h>
173577fe8bSPiotr Wilczek #include <asm/arch/pinmux.h>
18442d5568SJaehoon Chung 
197aedafd6SJaehoon Chung #ifdef CONFIG_DM_MMC
207aedafd6SJaehoon Chung struct s5p_sdhci_plat {
217aedafd6SJaehoon Chung 	struct mmc_config cfg;
227aedafd6SJaehoon Chung 	struct mmc mmc;
237aedafd6SJaehoon Chung };
247aedafd6SJaehoon Chung 
257aedafd6SJaehoon Chung DECLARE_GLOBAL_DATA_PTR;
267aedafd6SJaehoon Chung #endif
277aedafd6SJaehoon Chung 
28442d5568SJaehoon Chung static char *S5P_NAME = "SAMSUNG SDHCI";
s5p_sdhci_set_control_reg(struct sdhci_host * host)29442d5568SJaehoon Chung static void s5p_sdhci_set_control_reg(struct sdhci_host *host)
30442d5568SJaehoon Chung {
31442d5568SJaehoon Chung 	unsigned long val, ctrl;
32442d5568SJaehoon Chung 	/*
33442d5568SJaehoon Chung 	 * SELCLKPADDS[17:16]
34442d5568SJaehoon Chung 	 * 00 = 2mA
35442d5568SJaehoon Chung 	 * 01 = 4mA
36442d5568SJaehoon Chung 	 * 10 = 7mA
37442d5568SJaehoon Chung 	 * 11 = 9mA
38442d5568SJaehoon Chung 	 */
39442d5568SJaehoon Chung 	sdhci_writel(host, SDHCI_CTRL4_DRIVE_MASK(0x3), SDHCI_CONTROL4);
40442d5568SJaehoon Chung 
41442d5568SJaehoon Chung 	val = sdhci_readl(host, SDHCI_CONTROL2);
428ebde4f0SMatt Reimer 	val &= SDHCI_CTRL2_SELBASECLK_MASK(3);
43442d5568SJaehoon Chung 
44442d5568SJaehoon Chung 	val |=	SDHCI_CTRL2_ENSTAASYNCCLR |
45442d5568SJaehoon Chung 		SDHCI_CTRL2_ENCMDCNFMSK |
46442d5568SJaehoon Chung 		SDHCI_CTRL2_ENFBCLKRX |
47442d5568SJaehoon Chung 		SDHCI_CTRL2_ENCLKOUTHOLD;
48442d5568SJaehoon Chung 
49442d5568SJaehoon Chung 	sdhci_writel(host, val, SDHCI_CONTROL2);
50442d5568SJaehoon Chung 
51442d5568SJaehoon Chung 	/*
52442d5568SJaehoon Chung 	 * FCSEL3[31] FCSEL2[23] FCSEL1[15] FCSEL0[7]
53442d5568SJaehoon Chung 	 * FCSel[1:0] : Rx Feedback Clock Delay Control
54442d5568SJaehoon Chung 	 *	Inverter delay means10ns delay if SDCLK 50MHz setting
55442d5568SJaehoon Chung 	 *	01 = Delay1 (basic delay)
56442d5568SJaehoon Chung 	 *	11 = Delay2 (basic delay + 2ns)
57442d5568SJaehoon Chung 	 *	00 = Delay3 (inverter delay)
58442d5568SJaehoon Chung 	 *	10 = Delay4 (inverter delay + 2ns)
59442d5568SJaehoon Chung 	 */
60b268660cSJaehoon Chung 	val = SDHCI_CTRL3_FCSEL0 | SDHCI_CTRL3_FCSEL1;
61442d5568SJaehoon Chung 	sdhci_writel(host, val, SDHCI_CONTROL3);
62442d5568SJaehoon Chung 
63442d5568SJaehoon Chung 	/*
64442d5568SJaehoon Chung 	 * SELBASECLK[5:4]
65442d5568SJaehoon Chung 	 * 00/01 = HCLK
66442d5568SJaehoon Chung 	 * 10 = EPLL
67442d5568SJaehoon Chung 	 * 11 = XTI or XEXTCLK
68442d5568SJaehoon Chung 	 */
69442d5568SJaehoon Chung 	ctrl = sdhci_readl(host, SDHCI_CONTROL2);
70442d5568SJaehoon Chung 	ctrl &= ~SDHCI_CTRL2_SELBASECLK_MASK(0x3);
71442d5568SJaehoon Chung 	ctrl |= SDHCI_CTRL2_SELBASECLK_MASK(0x2);
72442d5568SJaehoon Chung 	sdhci_writel(host, ctrl, SDHCI_CONTROL2);
73442d5568SJaehoon Chung }
74442d5568SJaehoon Chung 
s5p_set_clock(struct sdhci_host * host,u32 div)75f73b33ffSJaehoon Chung static void s5p_set_clock(struct sdhci_host *host, u32 div)
76f73b33ffSJaehoon Chung {
77f73b33ffSJaehoon Chung 	/* ToDo : Use the Clock Framework */
78f73b33ffSJaehoon Chung 	set_mmc_clk(host->index, div);
79f73b33ffSJaehoon Chung }
80f73b33ffSJaehoon Chung 
8162226b68SJaehoon Chung static const struct sdhci_ops s5p_sdhci_ops = {
8262226b68SJaehoon Chung 	.set_clock	= &s5p_set_clock,
8362226b68SJaehoon Chung 	.set_control_reg = &s5p_sdhci_set_control_reg,
8462226b68SJaehoon Chung };
8562226b68SJaehoon Chung 
s5p_sdhci_core_init(struct sdhci_host * host)869b8c9a3cSJaehoon Chung static int s5p_sdhci_core_init(struct sdhci_host *host)
87442d5568SJaehoon Chung {
88442d5568SJaehoon Chung 	host->name = S5P_NAME;
89442d5568SJaehoon Chung 
90b268660cSJaehoon Chung 	host->quirks = SDHCI_QUIRK_NO_HISPD_BIT | SDHCI_QUIRK_BROKEN_VOLTAGE |
91a034ec06SJaehoon Chung 		SDHCI_QUIRK_32BIT_DMA_ADDR |
92113e5dfcSJaehoon Chung 		SDHCI_QUIRK_WAIT_SEND_CMD | SDHCI_QUIRK_USE_WIDE8;
936d0e34bfSStefan Herbrechtsmeier 	host->max_clk = 52000000;
94442d5568SJaehoon Chung 	host->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
9562226b68SJaehoon Chung 	host->ops = &s5p_sdhci_ops;
96442d5568SJaehoon Chung 
979b8c9a3cSJaehoon Chung 	if (host->bus_width == 8)
98113e5dfcSJaehoon Chung 		host->host_caps |= MMC_MODE_8BIT;
99442d5568SJaehoon Chung 
1007aedafd6SJaehoon Chung #ifndef CONFIG_BLK
1016d0e34bfSStefan Herbrechtsmeier 	return add_sdhci(host, 0, 400000);
1027aedafd6SJaehoon Chung #else
1037aedafd6SJaehoon Chung 	return 0;
1047aedafd6SJaehoon Chung #endif
105442d5568SJaehoon Chung }
1063577fe8bSPiotr Wilczek 
s5p_sdhci_init(u32 regbase,int index,int bus_width)1079b8c9a3cSJaehoon Chung int s5p_sdhci_init(u32 regbase, int index, int bus_width)
1089b8c9a3cSJaehoon Chung {
1091a9d1731STobias Jakobi 	struct sdhci_host *host = calloc(1, sizeof(struct sdhci_host));
1109b8c9a3cSJaehoon Chung 	if (!host) {
1111a9d1731STobias Jakobi 		printf("sdhci__host allocation fail!\n");
1122cb5d67cSJaehoon Chung 		return -ENOMEM;
1139b8c9a3cSJaehoon Chung 	}
1149b8c9a3cSJaehoon Chung 	host->ioaddr = (void *)regbase;
1159b8c9a3cSJaehoon Chung 	host->index = index;
1169b8c9a3cSJaehoon Chung 	host->bus_width = bus_width;
1179b8c9a3cSJaehoon Chung 
1189b8c9a3cSJaehoon Chung 	return s5p_sdhci_core_init(host);
1199b8c9a3cSJaehoon Chung }
1209b8c9a3cSJaehoon Chung 
do_sdhci_init(struct sdhci_host * host)1213577fe8bSPiotr Wilczek static int do_sdhci_init(struct sdhci_host *host)
1223577fe8bSPiotr Wilczek {
1232308ea7cSTobias Jakobi 	int dev_id, flag, ret;
1243577fe8bSPiotr Wilczek 
1253577fe8bSPiotr Wilczek 	flag = host->bus_width == 8 ? PINMUX_FLAG_8BIT_MODE : PINMUX_FLAG_NONE;
1263577fe8bSPiotr Wilczek 	dev_id = host->index + PERIPH_ID_SDMMC0;
1273577fe8bSPiotr Wilczek 
12896094d4cSPrzemyslaw Marczak 	ret = exynos_pinmux_config(dev_id, flag);
12996094d4cSPrzemyslaw Marczak 	if (ret) {
13096094d4cSPrzemyslaw Marczak 		printf("external SD not configured\n");
13196094d4cSPrzemyslaw Marczak 		return ret;
13296094d4cSPrzemyslaw Marczak 	}
13396094d4cSPrzemyslaw Marczak 
1340347960bSSimon Glass 	if (dm_gpio_is_valid(&host->pwr_gpio)) {
1350347960bSSimon Glass 		dm_gpio_set_value(&host->pwr_gpio, 1);
1362308ea7cSTobias Jakobi 		ret = exynos_pinmux_config(dev_id, flag);
1372308ea7cSTobias Jakobi 		if (ret) {
1383577fe8bSPiotr Wilczek 			debug("MMC not configured\n");
1392308ea7cSTobias Jakobi 			return ret;
1403577fe8bSPiotr Wilczek 		}
1413577fe8bSPiotr Wilczek 	}
1423577fe8bSPiotr Wilczek 
1430347960bSSimon Glass 	if (dm_gpio_is_valid(&host->cd_gpio)) {
1442308ea7cSTobias Jakobi 		ret = dm_gpio_get_value(&host->cd_gpio);
1452308ea7cSTobias Jakobi 		if (ret) {
1462308ea7cSTobias Jakobi 			debug("no SD card detected (%d)\n", ret);
1473577fe8bSPiotr Wilczek 			return -ENODEV;
1482308ea7cSTobias Jakobi 		}
1493577fe8bSPiotr Wilczek 	}
1503577fe8bSPiotr Wilczek 
1519b8c9a3cSJaehoon Chung 	return s5p_sdhci_core_init(host);
1523577fe8bSPiotr Wilczek }
1533577fe8bSPiotr Wilczek 
sdhci_get_config(const void * blob,int node,struct sdhci_host * host)1543577fe8bSPiotr Wilczek static int sdhci_get_config(const void *blob, int node, struct sdhci_host *host)
1553577fe8bSPiotr Wilczek {
1563577fe8bSPiotr Wilczek 	int bus_width, dev_id;
1573577fe8bSPiotr Wilczek 	unsigned int base;
1583577fe8bSPiotr Wilczek 
1593577fe8bSPiotr Wilczek 	/* Get device id */
1603577fe8bSPiotr Wilczek 	dev_id = pinmux_decode_periph_id(blob, node);
161f0ecfc5eSSeung-Woo Kim 	if (dev_id < PERIPH_ID_SDMMC0 || dev_id > PERIPH_ID_SDMMC3) {
1623577fe8bSPiotr Wilczek 		debug("MMC: Can't get device id\n");
1632cb5d67cSJaehoon Chung 		return -EINVAL;
1643577fe8bSPiotr Wilczek 	}
1653577fe8bSPiotr Wilczek 	host->index = dev_id - PERIPH_ID_SDMMC0;
1663577fe8bSPiotr Wilczek 
1673577fe8bSPiotr Wilczek 	/* Get bus width */
1683577fe8bSPiotr Wilczek 	bus_width = fdtdec_get_int(blob, node, "samsung,bus-width", 0);
1693577fe8bSPiotr Wilczek 	if (bus_width <= 0) {
1703577fe8bSPiotr Wilczek 		debug("MMC: Can't get bus-width\n");
1712cb5d67cSJaehoon Chung 		return -EINVAL;
1723577fe8bSPiotr Wilczek 	}
1733577fe8bSPiotr Wilczek 	host->bus_width = bus_width;
1743577fe8bSPiotr Wilczek 
1753577fe8bSPiotr Wilczek 	/* Get the base address from the device node */
1763577fe8bSPiotr Wilczek 	base = fdtdec_get_addr(blob, node, "reg");
1773577fe8bSPiotr Wilczek 	if (!base) {
1783577fe8bSPiotr Wilczek 		debug("MMC: Can't get base address\n");
1792cb5d67cSJaehoon Chung 		return -EINVAL;
1803577fe8bSPiotr Wilczek 	}
1813577fe8bSPiotr Wilczek 	host->ioaddr = (void *)base;
1823577fe8bSPiotr Wilczek 
183150c5afeSSimon Glass 	gpio_request_by_name_nodev(offset_to_ofnode(node), "pwr-gpios", 0,
184150c5afeSSimon Glass 				   &host->pwr_gpio, GPIOD_IS_OUT);
185150c5afeSSimon Glass 	gpio_request_by_name_nodev(offset_to_ofnode(node), "cd-gpios", 0,
186150c5afeSSimon Glass 				   &host->cd_gpio, GPIOD_IS_IN);
1873577fe8bSPiotr Wilczek 
1883577fe8bSPiotr Wilczek 	return 0;
1893577fe8bSPiotr Wilczek }
1903577fe8bSPiotr Wilczek 
1917aedafd6SJaehoon Chung #ifdef CONFIG_DM_MMC
s5p_sdhci_probe(struct udevice * dev)1927aedafd6SJaehoon Chung static int s5p_sdhci_probe(struct udevice *dev)
1937aedafd6SJaehoon Chung {
1947aedafd6SJaehoon Chung 	struct s5p_sdhci_plat *plat = dev_get_platdata(dev);
1957aedafd6SJaehoon Chung 	struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
1967aedafd6SJaehoon Chung 	struct sdhci_host *host = dev_get_priv(dev);
1977aedafd6SJaehoon Chung 	int ret;
1987aedafd6SJaehoon Chung 
199e160f7d4SSimon Glass 	ret = sdhci_get_config(gd->fdt_blob, dev_of_offset(dev), host);
2007aedafd6SJaehoon Chung 	if (ret)
2017aedafd6SJaehoon Chung 		return ret;
2027aedafd6SJaehoon Chung 
2037aedafd6SJaehoon Chung 	ret = do_sdhci_init(host);
2047aedafd6SJaehoon Chung 	if (ret)
2057aedafd6SJaehoon Chung 		return ret;
2067aedafd6SJaehoon Chung 
2076d0e34bfSStefan Herbrechtsmeier 	ret = sdhci_setup_cfg(&plat->cfg, host, 0, 400000);
2087aedafd6SJaehoon Chung 	if (ret)
2097aedafd6SJaehoon Chung 		return ret;
2107aedafd6SJaehoon Chung 
2117aedafd6SJaehoon Chung 	host->mmc = &plat->mmc;
2127aedafd6SJaehoon Chung 	host->mmc->priv = host;
2137aedafd6SJaehoon Chung 	host->mmc->dev = dev;
2147aedafd6SJaehoon Chung 	upriv->mmc = host->mmc;
2157aedafd6SJaehoon Chung 
2167aedafd6SJaehoon Chung 	return sdhci_probe(dev);
2177aedafd6SJaehoon Chung }
2187aedafd6SJaehoon Chung 
s5p_sdhci_bind(struct udevice * dev)2197aedafd6SJaehoon Chung static int s5p_sdhci_bind(struct udevice *dev)
2207aedafd6SJaehoon Chung {
2217aedafd6SJaehoon Chung 	struct s5p_sdhci_plat *plat = dev_get_platdata(dev);
2227aedafd6SJaehoon Chung 	int ret;
2237aedafd6SJaehoon Chung 
2247aedafd6SJaehoon Chung 	ret = sdhci_bind(dev, &plat->mmc, &plat->cfg);
2257aedafd6SJaehoon Chung 	if (ret)
2267aedafd6SJaehoon Chung 		return ret;
2277aedafd6SJaehoon Chung 
2287aedafd6SJaehoon Chung 	return 0;
2297aedafd6SJaehoon Chung }
2307aedafd6SJaehoon Chung 
2317aedafd6SJaehoon Chung static const struct udevice_id s5p_sdhci_ids[] = {
2327aedafd6SJaehoon Chung 	{ .compatible = "samsung,exynos4412-sdhci"},
2337aedafd6SJaehoon Chung 	{ }
2347aedafd6SJaehoon Chung };
2357aedafd6SJaehoon Chung 
2367aedafd6SJaehoon Chung U_BOOT_DRIVER(s5p_sdhci_drv) = {
2377aedafd6SJaehoon Chung 	.name		= "s5p_sdhci",
2387aedafd6SJaehoon Chung 	.id		= UCLASS_MMC,
2397aedafd6SJaehoon Chung 	.of_match	= s5p_sdhci_ids,
2407aedafd6SJaehoon Chung 	.bind		= s5p_sdhci_bind,
2417aedafd6SJaehoon Chung 	.ops		= &sdhci_ops,
2427aedafd6SJaehoon Chung 	.probe		= s5p_sdhci_probe,
2437aedafd6SJaehoon Chung 	.priv_auto_alloc_size = sizeof(struct sdhci_host),
2447aedafd6SJaehoon Chung 	.platdata_auto_alloc_size = sizeof(struct s5p_sdhci_plat),
2457aedafd6SJaehoon Chung };
2467aedafd6SJaehoon Chung #endif /* CONFIG_DM_MMC */
247