xref: /openbmc/u-boot/drivers/mmc/s5p_sdhci.c (revision 2cb5d67c1aceb758033954cc06382367ac89e6ac)
1442d5568SJaehoon Chung /*
2442d5568SJaehoon Chung  * (C) Copyright 2012 SAMSUNG Electronics
3442d5568SJaehoon Chung  * Jaehoon Chung <jh80.chung@samsung.com>
4442d5568SJaehoon Chung  *
51a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
6442d5568SJaehoon Chung  */
7442d5568SJaehoon Chung 
8442d5568SJaehoon Chung #include <common.h>
97aedafd6SJaehoon Chung #include <dm.h>
10442d5568SJaehoon Chung #include <malloc.h>
11442d5568SJaehoon Chung #include <sdhci.h>
123577fe8bSPiotr Wilczek #include <fdtdec.h>
133577fe8bSPiotr Wilczek #include <libfdt.h>
143577fe8bSPiotr Wilczek #include <asm/gpio.h>
15442d5568SJaehoon Chung #include <asm/arch/mmc.h>
16b09ed6e4SJaehoon Chung #include <asm/arch/clk.h>
173577fe8bSPiotr Wilczek #include <errno.h>
183577fe8bSPiotr Wilczek #include <asm/arch/pinmux.h>
19442d5568SJaehoon Chung 
207aedafd6SJaehoon Chung #ifdef CONFIG_DM_MMC
217aedafd6SJaehoon Chung struct s5p_sdhci_plat {
227aedafd6SJaehoon Chung 	struct mmc_config cfg;
237aedafd6SJaehoon Chung 	struct mmc mmc;
247aedafd6SJaehoon Chung };
257aedafd6SJaehoon Chung 
267aedafd6SJaehoon Chung DECLARE_GLOBAL_DATA_PTR;
277aedafd6SJaehoon Chung #endif
287aedafd6SJaehoon Chung 
29442d5568SJaehoon Chung static char *S5P_NAME = "SAMSUNG SDHCI";
30442d5568SJaehoon Chung static void s5p_sdhci_set_control_reg(struct sdhci_host *host)
31442d5568SJaehoon Chung {
32442d5568SJaehoon Chung 	unsigned long val, ctrl;
33442d5568SJaehoon Chung 	/*
34442d5568SJaehoon Chung 	 * SELCLKPADDS[17:16]
35442d5568SJaehoon Chung 	 * 00 = 2mA
36442d5568SJaehoon Chung 	 * 01 = 4mA
37442d5568SJaehoon Chung 	 * 10 = 7mA
38442d5568SJaehoon Chung 	 * 11 = 9mA
39442d5568SJaehoon Chung 	 */
40442d5568SJaehoon Chung 	sdhci_writel(host, SDHCI_CTRL4_DRIVE_MASK(0x3), SDHCI_CONTROL4);
41442d5568SJaehoon Chung 
42442d5568SJaehoon Chung 	val = sdhci_readl(host, SDHCI_CONTROL2);
438ebde4f0SMatt Reimer 	val &= SDHCI_CTRL2_SELBASECLK_MASK(3);
44442d5568SJaehoon Chung 
45442d5568SJaehoon Chung 	val |=	SDHCI_CTRL2_ENSTAASYNCCLR |
46442d5568SJaehoon Chung 		SDHCI_CTRL2_ENCMDCNFMSK |
47442d5568SJaehoon Chung 		SDHCI_CTRL2_ENFBCLKRX |
48442d5568SJaehoon Chung 		SDHCI_CTRL2_ENCLKOUTHOLD;
49442d5568SJaehoon Chung 
50442d5568SJaehoon Chung 	sdhci_writel(host, val, SDHCI_CONTROL2);
51442d5568SJaehoon Chung 
52442d5568SJaehoon Chung 	/*
53442d5568SJaehoon Chung 	 * FCSEL3[31] FCSEL2[23] FCSEL1[15] FCSEL0[7]
54442d5568SJaehoon Chung 	 * FCSel[1:0] : Rx Feedback Clock Delay Control
55442d5568SJaehoon Chung 	 *	Inverter delay means10ns delay if SDCLK 50MHz setting
56442d5568SJaehoon Chung 	 *	01 = Delay1 (basic delay)
57442d5568SJaehoon Chung 	 *	11 = Delay2 (basic delay + 2ns)
58442d5568SJaehoon Chung 	 *	00 = Delay3 (inverter delay)
59442d5568SJaehoon Chung 	 *	10 = Delay4 (inverter delay + 2ns)
60442d5568SJaehoon Chung 	 */
61b268660cSJaehoon Chung 	val = SDHCI_CTRL3_FCSEL0 | SDHCI_CTRL3_FCSEL1;
62442d5568SJaehoon Chung 	sdhci_writel(host, val, SDHCI_CONTROL3);
63442d5568SJaehoon Chung 
64442d5568SJaehoon Chung 	/*
65442d5568SJaehoon Chung 	 * SELBASECLK[5:4]
66442d5568SJaehoon Chung 	 * 00/01 = HCLK
67442d5568SJaehoon Chung 	 * 10 = EPLL
68442d5568SJaehoon Chung 	 * 11 = XTI or XEXTCLK
69442d5568SJaehoon Chung 	 */
70442d5568SJaehoon Chung 	ctrl = sdhci_readl(host, SDHCI_CONTROL2);
71442d5568SJaehoon Chung 	ctrl &= ~SDHCI_CTRL2_SELBASECLK_MASK(0x3);
72442d5568SJaehoon Chung 	ctrl |= SDHCI_CTRL2_SELBASECLK_MASK(0x2);
73442d5568SJaehoon Chung 	sdhci_writel(host, ctrl, SDHCI_CONTROL2);
74442d5568SJaehoon Chung }
75442d5568SJaehoon Chung 
769b8c9a3cSJaehoon Chung static int s5p_sdhci_core_init(struct sdhci_host *host)
77442d5568SJaehoon Chung {
78442d5568SJaehoon Chung 	host->name = S5P_NAME;
79442d5568SJaehoon Chung 
80b268660cSJaehoon Chung 	host->quirks = SDHCI_QUIRK_NO_HISPD_BIT | SDHCI_QUIRK_BROKEN_VOLTAGE |
81a034ec06SJaehoon Chung 		SDHCI_QUIRK_32BIT_DMA_ADDR |
82113e5dfcSJaehoon Chung 		SDHCI_QUIRK_WAIT_SEND_CMD | SDHCI_QUIRK_USE_WIDE8;
83442d5568SJaehoon Chung 	host->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
84442d5568SJaehoon Chung 
85442d5568SJaehoon Chung 	host->set_control_reg = &s5p_sdhci_set_control_reg;
86b09ed6e4SJaehoon Chung 	host->set_clock = set_mmc_clk;
87442d5568SJaehoon Chung 
889b8c9a3cSJaehoon Chung 	if (host->bus_width == 8)
89113e5dfcSJaehoon Chung 		host->host_caps |= MMC_MODE_8BIT;
90442d5568SJaehoon Chung 
917aedafd6SJaehoon Chung #ifndef CONFIG_BLK
92a68aac49SJaehoon Chung 	return add_sdhci(host, 52000000, 400000);
937aedafd6SJaehoon Chung #else
947aedafd6SJaehoon Chung 	return 0;
957aedafd6SJaehoon Chung #endif
96442d5568SJaehoon Chung }
973577fe8bSPiotr Wilczek 
989b8c9a3cSJaehoon Chung int s5p_sdhci_init(u32 regbase, int index, int bus_width)
999b8c9a3cSJaehoon Chung {
1001a9d1731STobias Jakobi 	struct sdhci_host *host = calloc(1, sizeof(struct sdhci_host));
1019b8c9a3cSJaehoon Chung 	if (!host) {
1021a9d1731STobias Jakobi 		printf("sdhci__host allocation fail!\n");
103*2cb5d67cSJaehoon Chung 		return -ENOMEM;
1049b8c9a3cSJaehoon Chung 	}
1059b8c9a3cSJaehoon Chung 	host->ioaddr = (void *)regbase;
1069b8c9a3cSJaehoon Chung 	host->index = index;
1079b8c9a3cSJaehoon Chung 	host->bus_width = bus_width;
1089b8c9a3cSJaehoon Chung 
1099b8c9a3cSJaehoon Chung 	return s5p_sdhci_core_init(host);
1109b8c9a3cSJaehoon Chung }
1119b8c9a3cSJaehoon Chung 
1120f925822SMasahiro Yamada #if CONFIG_IS_ENABLED(OF_CONTROL)
1133577fe8bSPiotr Wilczek struct sdhci_host sdhci_host[SDHCI_MAX_HOSTS];
1143577fe8bSPiotr Wilczek 
1153577fe8bSPiotr Wilczek static int do_sdhci_init(struct sdhci_host *host)
1163577fe8bSPiotr Wilczek {
1172308ea7cSTobias Jakobi 	int dev_id, flag, ret;
1183577fe8bSPiotr Wilczek 
1193577fe8bSPiotr Wilczek 	flag = host->bus_width == 8 ? PINMUX_FLAG_8BIT_MODE : PINMUX_FLAG_NONE;
1203577fe8bSPiotr Wilczek 	dev_id = host->index + PERIPH_ID_SDMMC0;
1213577fe8bSPiotr Wilczek 
12296094d4cSPrzemyslaw Marczak 	ret = exynos_pinmux_config(dev_id, flag);
12396094d4cSPrzemyslaw Marczak 	if (ret) {
12496094d4cSPrzemyslaw Marczak 		printf("external SD not configured\n");
12596094d4cSPrzemyslaw Marczak 		return ret;
12696094d4cSPrzemyslaw Marczak 	}
12796094d4cSPrzemyslaw Marczak 
1280347960bSSimon Glass 	if (dm_gpio_is_valid(&host->pwr_gpio)) {
1290347960bSSimon Glass 		dm_gpio_set_value(&host->pwr_gpio, 1);
1302308ea7cSTobias Jakobi 		ret = exynos_pinmux_config(dev_id, flag);
1312308ea7cSTobias Jakobi 		if (ret) {
1323577fe8bSPiotr Wilczek 			debug("MMC not configured\n");
1332308ea7cSTobias Jakobi 			return ret;
1343577fe8bSPiotr Wilczek 		}
1353577fe8bSPiotr Wilczek 	}
1363577fe8bSPiotr Wilczek 
1370347960bSSimon Glass 	if (dm_gpio_is_valid(&host->cd_gpio)) {
1382308ea7cSTobias Jakobi 		ret = dm_gpio_get_value(&host->cd_gpio);
1392308ea7cSTobias Jakobi 		if (ret) {
1402308ea7cSTobias Jakobi 			debug("no SD card detected (%d)\n", ret);
1413577fe8bSPiotr Wilczek 			return -ENODEV;
1422308ea7cSTobias Jakobi 		}
1433577fe8bSPiotr Wilczek 	}
1443577fe8bSPiotr Wilczek 
1459b8c9a3cSJaehoon Chung 	return s5p_sdhci_core_init(host);
1463577fe8bSPiotr Wilczek }
1473577fe8bSPiotr Wilczek 
1483577fe8bSPiotr Wilczek static int sdhci_get_config(const void *blob, int node, struct sdhci_host *host)
1493577fe8bSPiotr Wilczek {
1503577fe8bSPiotr Wilczek 	int bus_width, dev_id;
1513577fe8bSPiotr Wilczek 	unsigned int base;
1523577fe8bSPiotr Wilczek 
1533577fe8bSPiotr Wilczek 	/* Get device id */
1543577fe8bSPiotr Wilczek 	dev_id = pinmux_decode_periph_id(blob, node);
1553577fe8bSPiotr Wilczek 	if (dev_id < PERIPH_ID_SDMMC0 && dev_id > PERIPH_ID_SDMMC3) {
1563577fe8bSPiotr Wilczek 		debug("MMC: Can't get device id\n");
157*2cb5d67cSJaehoon Chung 		return -EINVAL;
1583577fe8bSPiotr Wilczek 	}
1593577fe8bSPiotr Wilczek 	host->index = dev_id - PERIPH_ID_SDMMC0;
1603577fe8bSPiotr Wilczek 
1613577fe8bSPiotr Wilczek 	/* Get bus width */
1623577fe8bSPiotr Wilczek 	bus_width = fdtdec_get_int(blob, node, "samsung,bus-width", 0);
1633577fe8bSPiotr Wilczek 	if (bus_width <= 0) {
1643577fe8bSPiotr Wilczek 		debug("MMC: Can't get bus-width\n");
165*2cb5d67cSJaehoon Chung 		return -EINVAL;
1663577fe8bSPiotr Wilczek 	}
1673577fe8bSPiotr Wilczek 	host->bus_width = bus_width;
1683577fe8bSPiotr Wilczek 
1693577fe8bSPiotr Wilczek 	/* Get the base address from the device node */
1703577fe8bSPiotr Wilczek 	base = fdtdec_get_addr(blob, node, "reg");
1713577fe8bSPiotr Wilczek 	if (!base) {
1723577fe8bSPiotr Wilczek 		debug("MMC: Can't get base address\n");
173*2cb5d67cSJaehoon Chung 		return -EINVAL;
1743577fe8bSPiotr Wilczek 	}
1753577fe8bSPiotr Wilczek 	host->ioaddr = (void *)base;
1763577fe8bSPiotr Wilczek 
1770347960bSSimon Glass 	gpio_request_by_name_nodev(blob, node, "pwr-gpios", 0, &host->pwr_gpio,
1780347960bSSimon Glass 				   GPIOD_IS_OUT);
1790347960bSSimon Glass 	gpio_request_by_name_nodev(blob, node, "cd-gpios", 0, &host->cd_gpio,
1800347960bSSimon Glass 				   GPIOD_IS_IN);
1813577fe8bSPiotr Wilczek 
1823577fe8bSPiotr Wilczek 	return 0;
1833577fe8bSPiotr Wilczek }
1843577fe8bSPiotr Wilczek 
1853577fe8bSPiotr Wilczek static int process_nodes(const void *blob, int node_list[], int count)
1863577fe8bSPiotr Wilczek {
1873577fe8bSPiotr Wilczek 	struct sdhci_host *host;
188995a54ccSTobias Jakobi 	int i, node, ret;
1896a9fbb6eSTobias Jakobi 	int failed = 0;
1903577fe8bSPiotr Wilczek 
1913577fe8bSPiotr Wilczek 	debug("%s: count = %d\n", __func__, count);
1923577fe8bSPiotr Wilczek 
1933577fe8bSPiotr Wilczek 	/* build sdhci_host[] for each controller */
1943577fe8bSPiotr Wilczek 	for (i = 0; i < count; i++) {
1953577fe8bSPiotr Wilczek 		node = node_list[i];
1963577fe8bSPiotr Wilczek 		if (node <= 0)
1973577fe8bSPiotr Wilczek 			continue;
1983577fe8bSPiotr Wilczek 
1993577fe8bSPiotr Wilczek 		host = &sdhci_host[i];
2003577fe8bSPiotr Wilczek 
201995a54ccSTobias Jakobi 		ret = sdhci_get_config(blob, node, host);
202995a54ccSTobias Jakobi 		if (ret) {
203995a54ccSTobias Jakobi 			printf("%s: failed to decode dev %d (%d)\n",	__func__, i, ret);
2046a9fbb6eSTobias Jakobi 			failed++;
2056a9fbb6eSTobias Jakobi 			continue;
2063577fe8bSPiotr Wilczek 		}
2076a9fbb6eSTobias Jakobi 
208995a54ccSTobias Jakobi 		ret = do_sdhci_init(host);
20996094d4cSPrzemyslaw Marczak 		if (ret && ret != -ENODEV) {
210995a54ccSTobias Jakobi 			printf("%s: failed to initialize dev %d (%d)\n", __func__, i, ret);
2116a9fbb6eSTobias Jakobi 			failed++;
2123577fe8bSPiotr Wilczek 		}
2136a9fbb6eSTobias Jakobi 	}
2146a9fbb6eSTobias Jakobi 
2156a9fbb6eSTobias Jakobi 	/* we only consider it an error when all nodes fail */
2166a9fbb6eSTobias Jakobi 	return (failed == count ? -1 : 0);
2173577fe8bSPiotr Wilczek }
2183577fe8bSPiotr Wilczek 
2193577fe8bSPiotr Wilczek int exynos_mmc_init(const void *blob)
2203577fe8bSPiotr Wilczek {
2213577fe8bSPiotr Wilczek 	int count;
2223577fe8bSPiotr Wilczek 	int node_list[SDHCI_MAX_HOSTS];
2233577fe8bSPiotr Wilczek 
2243577fe8bSPiotr Wilczek 	count = fdtdec_find_aliases_for_id(blob, "mmc",
2253577fe8bSPiotr Wilczek 			COMPAT_SAMSUNG_EXYNOS_MMC, node_list,
2263577fe8bSPiotr Wilczek 			SDHCI_MAX_HOSTS);
2273577fe8bSPiotr Wilczek 
2286a9fbb6eSTobias Jakobi 	return process_nodes(blob, node_list, count);
2293577fe8bSPiotr Wilczek }
2303577fe8bSPiotr Wilczek #endif
2317aedafd6SJaehoon Chung 
2327aedafd6SJaehoon Chung #ifdef CONFIG_DM_MMC
2337aedafd6SJaehoon Chung static int s5p_sdhci_probe(struct udevice *dev)
2347aedafd6SJaehoon Chung {
2357aedafd6SJaehoon Chung 	struct s5p_sdhci_plat *plat = dev_get_platdata(dev);
2367aedafd6SJaehoon Chung 	struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
2377aedafd6SJaehoon Chung 	struct sdhci_host *host = dev_get_priv(dev);
2387aedafd6SJaehoon Chung 	int ret;
2397aedafd6SJaehoon Chung 
2407aedafd6SJaehoon Chung 	ret = sdhci_get_config(gd->fdt_blob, dev->of_offset, host);
2417aedafd6SJaehoon Chung 	if (ret)
2427aedafd6SJaehoon Chung 		return ret;
2437aedafd6SJaehoon Chung 
2447aedafd6SJaehoon Chung 	ret = do_sdhci_init(host);
2457aedafd6SJaehoon Chung 	if (ret)
2467aedafd6SJaehoon Chung 		return ret;
2477aedafd6SJaehoon Chung 
2487aedafd6SJaehoon Chung 	ret = sdhci_setup_cfg(&plat->cfg, host, 52000000, 400000);
2497aedafd6SJaehoon Chung 	if (ret)
2507aedafd6SJaehoon Chung 		return ret;
2517aedafd6SJaehoon Chung 
2527aedafd6SJaehoon Chung 	host->mmc = &plat->mmc;
2537aedafd6SJaehoon Chung 	host->mmc->priv = host;
2547aedafd6SJaehoon Chung 	host->mmc->dev = dev;
2557aedafd6SJaehoon Chung 	upriv->mmc = host->mmc;
2567aedafd6SJaehoon Chung 
2577aedafd6SJaehoon Chung 	return sdhci_probe(dev);
2587aedafd6SJaehoon Chung }
2597aedafd6SJaehoon Chung 
2607aedafd6SJaehoon Chung static int s5p_sdhci_bind(struct udevice *dev)
2617aedafd6SJaehoon Chung {
2627aedafd6SJaehoon Chung 	struct s5p_sdhci_plat *plat = dev_get_platdata(dev);
2637aedafd6SJaehoon Chung 	int ret;
2647aedafd6SJaehoon Chung 
2657aedafd6SJaehoon Chung 	ret = sdhci_bind(dev, &plat->mmc, &plat->cfg);
2667aedafd6SJaehoon Chung 	if (ret)
2677aedafd6SJaehoon Chung 		return ret;
2687aedafd6SJaehoon Chung 
2697aedafd6SJaehoon Chung 	return 0;
2707aedafd6SJaehoon Chung }
2717aedafd6SJaehoon Chung 
2727aedafd6SJaehoon Chung static const struct udevice_id s5p_sdhci_ids[] = {
2737aedafd6SJaehoon Chung 	{ .compatible = "samsung,exynos4412-sdhci"},
2747aedafd6SJaehoon Chung 	{ }
2757aedafd6SJaehoon Chung };
2767aedafd6SJaehoon Chung 
2777aedafd6SJaehoon Chung U_BOOT_DRIVER(s5p_sdhci_drv) = {
2787aedafd6SJaehoon Chung 	.name		= "s5p_sdhci",
2797aedafd6SJaehoon Chung 	.id		= UCLASS_MMC,
2807aedafd6SJaehoon Chung 	.of_match	= s5p_sdhci_ids,
2817aedafd6SJaehoon Chung 	.bind		= s5p_sdhci_bind,
2827aedafd6SJaehoon Chung 	.ops		= &sdhci_ops,
2837aedafd6SJaehoon Chung 	.probe		= s5p_sdhci_probe,
2847aedafd6SJaehoon Chung 	.priv_auto_alloc_size = sizeof(struct sdhci_host),
2857aedafd6SJaehoon Chung 	.platdata_auto_alloc_size = sizeof(struct s5p_sdhci_plat),
2867aedafd6SJaehoon Chung };
2877aedafd6SJaehoon Chung #endif /* CONFIG_DM_MMC */
288