xref: /openbmc/linux/drivers/mmc/host/sdhci_f_sdh30.c (revision aae9d3a4)
1b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
287a50745SVincent Yang /*
387a50745SVincent Yang  * linux/drivers/mmc/host/sdhci_f_sdh30.c
487a50745SVincent Yang  *
587a50745SVincent Yang  * Copyright (C) 2013 - 2015 Fujitsu Semiconductor, Ltd
687a50745SVincent Yang  *              Vincent Yang <vincent.yang@tw.fujitsu.com>
787a50745SVincent Yang  * Copyright (C) 2015 Linaro Ltd  Andy Green <andy.green@linaro.org>
85914a9b1SKunihiko Hayashi  * Copyright (C) 2019 Socionext Inc.
987a50745SVincent Yang  */
1087a50745SVincent Yang 
1190e1d8ccSArd Biesheuvel #include <linux/acpi.h>
1287a50745SVincent Yang #include <linux/err.h>
1387a50745SVincent Yang #include <linux/delay.h>
1487a50745SVincent Yang #include <linux/module.h>
1590e1d8ccSArd Biesheuvel #include <linux/of.h>
1606641e8dSArd Biesheuvel #include <linux/property.h>
1787a50745SVincent Yang #include <linux/clk.h>
18bd724b27SKunihiko Hayashi #include <linux/reset.h>
1987a50745SVincent Yang 
2087a50745SVincent Yang #include "sdhci-pltfm.h"
21dd79b7e3STakao Orito #include "sdhci_f_sdh30.h"
2287a50745SVincent Yang 
2387a50745SVincent Yang struct f_sdhost_priv {
2487a50745SVincent Yang 	struct clk *clk_iface;
2587a50745SVincent Yang 	struct clk *clk;
26bd724b27SKunihiko Hayashi 	struct reset_control *rst;
2787a50745SVincent Yang 	u32 vendor_hs200;
2887a50745SVincent Yang 	struct device *dev;
2906641e8dSArd Biesheuvel 	bool enable_cmd_dat_delay;
3087a50745SVincent Yang };
3187a50745SVincent Yang 
32cee4e7a5SAxel Lin static void sdhci_f_sdh30_soft_voltage_switch(struct sdhci_host *host)
3387a50745SVincent Yang {
3487a50745SVincent Yang 	struct f_sdhost_priv *priv = sdhci_priv(host);
3587a50745SVincent Yang 	u32 ctrl = 0;
3687a50745SVincent Yang 
3787a50745SVincent Yang 	usleep_range(2500, 3000);
3887a50745SVincent Yang 	ctrl = sdhci_readl(host, F_SDH30_IO_CONTROL2);
3987a50745SVincent Yang 	ctrl |= F_SDH30_CRES_O_DN;
4087a50745SVincent Yang 	sdhci_writel(host, ctrl, F_SDH30_IO_CONTROL2);
4187a50745SVincent Yang 	ctrl |= F_SDH30_MSEL_O_1_8;
4287a50745SVincent Yang 	sdhci_writel(host, ctrl, F_SDH30_IO_CONTROL2);
4387a50745SVincent Yang 
4487a50745SVincent Yang 	ctrl &= ~F_SDH30_CRES_O_DN;
4587a50745SVincent Yang 	sdhci_writel(host, ctrl, F_SDH30_IO_CONTROL2);
4687a50745SVincent Yang 	usleep_range(2500, 3000);
4787a50745SVincent Yang 
4887a50745SVincent Yang 	if (priv->vendor_hs200) {
4987a50745SVincent Yang 		dev_info(priv->dev, "%s: setting hs200\n", __func__);
5087a50745SVincent Yang 		ctrl = sdhci_readl(host, F_SDH30_ESD_CONTROL);
5187a50745SVincent Yang 		ctrl |= priv->vendor_hs200;
5287a50745SVincent Yang 		sdhci_writel(host, ctrl, F_SDH30_ESD_CONTROL);
5387a50745SVincent Yang 	}
5487a50745SVincent Yang 
5587a50745SVincent Yang 	ctrl = sdhci_readl(host, F_SDH30_TUNING_SETTING);
5687a50745SVincent Yang 	ctrl |= F_SDH30_CMD_CHK_DIS;
5787a50745SVincent Yang 	sdhci_writel(host, ctrl, F_SDH30_TUNING_SETTING);
5887a50745SVincent Yang }
5987a50745SVincent Yang 
60cee4e7a5SAxel Lin static unsigned int sdhci_f_sdh30_get_min_clock(struct sdhci_host *host)
6187a50745SVincent Yang {
6287a50745SVincent Yang 	return F_SDH30_MIN_CLOCK;
6387a50745SVincent Yang }
6487a50745SVincent Yang 
65cee4e7a5SAxel Lin static void sdhci_f_sdh30_reset(struct sdhci_host *host, u8 mask)
6687a50745SVincent Yang {
6706641e8dSArd Biesheuvel 	struct f_sdhost_priv *priv = sdhci_priv(host);
6806641e8dSArd Biesheuvel 	u32 ctl;
6906641e8dSArd Biesheuvel 
7087a50745SVincent Yang 	if (sdhci_readw(host, SDHCI_CLOCK_CONTROL) == 0)
7187a50745SVincent Yang 		sdhci_writew(host, 0xBC01, SDHCI_CLOCK_CONTROL);
7287a50745SVincent Yang 
7387a50745SVincent Yang 	sdhci_reset(host, mask);
7406641e8dSArd Biesheuvel 
7506641e8dSArd Biesheuvel 	if (priv->enable_cmd_dat_delay) {
7606641e8dSArd Biesheuvel 		ctl = sdhci_readl(host, F_SDH30_ESD_CONTROL);
7706641e8dSArd Biesheuvel 		ctl |= F_SDH30_CMD_DAT_DELAY;
7806641e8dSArd Biesheuvel 		sdhci_writel(host, ctl, F_SDH30_ESD_CONTROL);
7906641e8dSArd Biesheuvel 	}
80e2d2dcc8SKunihiko Hayashi 
81e2d2dcc8SKunihiko Hayashi 	if ((host->mmc->caps & MMC_CAP_NONREMOVABLE) &&
82e2d2dcc8SKunihiko Hayashi 	    !(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
83e2d2dcc8SKunihiko Hayashi 		ctl = sdhci_readl(host, F_SDH30_TEST);
84e2d2dcc8SKunihiko Hayashi 		ctl |= F_SDH30_FORCE_CARD_INSERT;
85e2d2dcc8SKunihiko Hayashi 		sdhci_writel(host, ctl, F_SDH30_TEST);
86e2d2dcc8SKunihiko Hayashi 	}
8787a50745SVincent Yang }
8887a50745SVincent Yang 
8987a50745SVincent Yang static const struct sdhci_ops sdhci_f_sdh30_ops = {
9087a50745SVincent Yang 	.voltage_switch = sdhci_f_sdh30_soft_voltage_switch,
9187a50745SVincent Yang 	.get_min_clock = sdhci_f_sdh30_get_min_clock,
9287a50745SVincent Yang 	.reset = sdhci_f_sdh30_reset,
9387a50745SVincent Yang 	.set_clock = sdhci_set_clock,
9487a50745SVincent Yang 	.set_bus_width = sdhci_set_bus_width,
9587a50745SVincent Yang 	.set_uhs_signaling = sdhci_set_uhs_signaling,
9687a50745SVincent Yang };
9787a50745SVincent Yang 
9887a50745SVincent Yang static int sdhci_f_sdh30_probe(struct platform_device *pdev)
9987a50745SVincent Yang {
10087a50745SVincent Yang 	struct sdhci_host *host;
10187a50745SVincent Yang 	struct device *dev = &pdev->dev;
10287a50745SVincent Yang 	int irq, ctrl = 0, ret = 0;
10387a50745SVincent Yang 	struct f_sdhost_priv *priv;
10487a50745SVincent Yang 	u32 reg = 0;
10587a50745SVincent Yang 
10687a50745SVincent Yang 	irq = platform_get_irq(pdev, 0);
1079a7957d0SStephen Boyd 	if (irq < 0)
10887a50745SVincent Yang 		return irq;
10987a50745SVincent Yang 
1102dbf1dc3SAxel Lin 	host = sdhci_alloc_host(dev, sizeof(struct f_sdhost_priv));
11187a50745SVincent Yang 	if (IS_ERR(host))
11287a50745SVincent Yang 		return PTR_ERR(host);
11387a50745SVincent Yang 
11487a50745SVincent Yang 	priv = sdhci_priv(host);
11587a50745SVincent Yang 	priv->dev = dev;
11687a50745SVincent Yang 
11787a50745SVincent Yang 	host->quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
11887a50745SVincent Yang 		       SDHCI_QUIRK_INVERTED_WRITE_PROTECT;
11987a50745SVincent Yang 	host->quirks2 = SDHCI_QUIRK2_SUPPORT_SINGLE |
12087a50745SVincent Yang 			SDHCI_QUIRK2_TUNING_WORK_AROUND;
12187a50745SVincent Yang 
12206641e8dSArd Biesheuvel 	priv->enable_cmd_dat_delay = device_property_read_bool(dev,
12306641e8dSArd Biesheuvel 						"fujitsu,cmd-dat-delay-select");
12406641e8dSArd Biesheuvel 
12587a50745SVincent Yang 	ret = mmc_of_parse(host->mmc);
12687a50745SVincent Yang 	if (ret)
12787a50745SVincent Yang 		goto err;
12887a50745SVincent Yang 
12987a50745SVincent Yang 	platform_set_drvdata(pdev, host);
13087a50745SVincent Yang 
13187a50745SVincent Yang 	host->hw_name = "f_sdh30";
13287a50745SVincent Yang 	host->ops = &sdhci_f_sdh30_ops;
13387a50745SVincent Yang 	host->irq = irq;
13487a50745SVincent Yang 
135dbf90a17SYangtao Li 	host->ioaddr = devm_platform_ioremap_resource(pdev, 0);
13687a50745SVincent Yang 	if (IS_ERR(host->ioaddr)) {
1373e7cf902SWu Fengguang 		ret = PTR_ERR(host->ioaddr);
13887a50745SVincent Yang 		goto err;
13987a50745SVincent Yang 	}
14087a50745SVincent Yang 
14190e1d8ccSArd Biesheuvel 	if (dev_of_node(dev)) {
14290e1d8ccSArd Biesheuvel 		sdhci_get_of_property(pdev);
14390e1d8ccSArd Biesheuvel 
14487a50745SVincent Yang 		priv->clk_iface = devm_clk_get(&pdev->dev, "iface");
14587a50745SVincent Yang 		if (IS_ERR(priv->clk_iface)) {
14687a50745SVincent Yang 			ret = PTR_ERR(priv->clk_iface);
14787a50745SVincent Yang 			goto err;
14887a50745SVincent Yang 		}
14987a50745SVincent Yang 
15087a50745SVincent Yang 		ret = clk_prepare_enable(priv->clk_iface);
15187a50745SVincent Yang 		if (ret)
15287a50745SVincent Yang 			goto err;
15387a50745SVincent Yang 
15487a50745SVincent Yang 		priv->clk = devm_clk_get(&pdev->dev, "core");
15587a50745SVincent Yang 		if (IS_ERR(priv->clk)) {
15687a50745SVincent Yang 			ret = PTR_ERR(priv->clk);
15787a50745SVincent Yang 			goto err_clk;
15887a50745SVincent Yang 		}
15987a50745SVincent Yang 
16087a50745SVincent Yang 		ret = clk_prepare_enable(priv->clk);
16187a50745SVincent Yang 		if (ret)
16287a50745SVincent Yang 			goto err_clk;
163bd724b27SKunihiko Hayashi 
164bd724b27SKunihiko Hayashi 		priv->rst = devm_reset_control_get_optional_shared(dev, NULL);
165bd724b27SKunihiko Hayashi 		if (IS_ERR(priv->rst)) {
166bd724b27SKunihiko Hayashi 			ret = PTR_ERR(priv->rst);
167bd724b27SKunihiko Hayashi 			goto err_rst;
168bd724b27SKunihiko Hayashi 		}
169bd724b27SKunihiko Hayashi 
170bd724b27SKunihiko Hayashi 		ret = reset_control_deassert(priv->rst);
171bd724b27SKunihiko Hayashi 		if (ret)
172bd724b27SKunihiko Hayashi 			goto err_rst;
17390e1d8ccSArd Biesheuvel 	}
17487a50745SVincent Yang 
17587a50745SVincent Yang 	/* init vendor specific regs */
17687a50745SVincent Yang 	ctrl = sdhci_readw(host, F_SDH30_AHB_CONFIG);
17787a50745SVincent Yang 	ctrl |= F_SDH30_SIN | F_SDH30_AHB_INCR_16 | F_SDH30_AHB_INCR_8 |
17887a50745SVincent Yang 		F_SDH30_AHB_INCR_4;
17987a50745SVincent Yang 	ctrl &= ~(F_SDH30_AHB_BIGED | F_SDH30_BUSLOCK_EN);
18087a50745SVincent Yang 	sdhci_writew(host, ctrl, F_SDH30_AHB_CONFIG);
18187a50745SVincent Yang 
18287a50745SVincent Yang 	reg = sdhci_readl(host, F_SDH30_ESD_CONTROL);
18387a50745SVincent Yang 	sdhci_writel(host, reg & ~F_SDH30_EMMC_RST, F_SDH30_ESD_CONTROL);
18487a50745SVincent Yang 	msleep(20);
18587a50745SVincent Yang 	sdhci_writel(host, reg | F_SDH30_EMMC_RST, F_SDH30_ESD_CONTROL);
18687a50745SVincent Yang 
18787a50745SVincent Yang 	reg = sdhci_readl(host, SDHCI_CAPABILITIES);
18887a50745SVincent Yang 	if (reg & SDHCI_CAN_DO_8BIT)
18987a50745SVincent Yang 		priv->vendor_hs200 = F_SDH30_EMMC_HS200;
19087a50745SVincent Yang 
191*aae9d3a4SKunihiko Hayashi 	if (!(reg & SDHCI_TIMEOUT_CLK_MASK))
192*aae9d3a4SKunihiko Hayashi 		host->quirks |= SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK;
193*aae9d3a4SKunihiko Hayashi 
19487a50745SVincent Yang 	ret = sdhci_add_host(host);
19587a50745SVincent Yang 	if (ret)
19687a50745SVincent Yang 		goto err_add_host;
19787a50745SVincent Yang 
19887a50745SVincent Yang 	return 0;
19987a50745SVincent Yang 
20087a50745SVincent Yang err_add_host:
201bd724b27SKunihiko Hayashi 	reset_control_assert(priv->rst);
202bd724b27SKunihiko Hayashi err_rst:
20387a50745SVincent Yang 	clk_disable_unprepare(priv->clk);
20487a50745SVincent Yang err_clk:
20587a50745SVincent Yang 	clk_disable_unprepare(priv->clk_iface);
20687a50745SVincent Yang err:
20787a50745SVincent Yang 	sdhci_free_host(host);
20887a50745SVincent Yang 	return ret;
20987a50745SVincent Yang }
21087a50745SVincent Yang 
21187a50745SVincent Yang static int sdhci_f_sdh30_remove(struct platform_device *pdev)
21287a50745SVincent Yang {
21387a50745SVincent Yang 	struct sdhci_host *host = platform_get_drvdata(pdev);
21487a50745SVincent Yang 	struct f_sdhost_priv *priv = sdhci_priv(host);
21587a50745SVincent Yang 
21687a50745SVincent Yang 	sdhci_remove_host(host, readl(host->ioaddr + SDHCI_INT_STATUS) ==
21787a50745SVincent Yang 			  0xffffffff);
21887a50745SVincent Yang 
219bd724b27SKunihiko Hayashi 	reset_control_assert(priv->rst);
22087a50745SVincent Yang 	clk_disable_unprepare(priv->clk);
221bd724b27SKunihiko Hayashi 	clk_disable_unprepare(priv->clk_iface);
22287a50745SVincent Yang 
22387a50745SVincent Yang 	sdhci_free_host(host);
22487a50745SVincent Yang 	platform_set_drvdata(pdev, NULL);
22587a50745SVincent Yang 
22687a50745SVincent Yang 	return 0;
22787a50745SVincent Yang }
22887a50745SVincent Yang 
22990e1d8ccSArd Biesheuvel #ifdef CONFIG_OF
23087a50745SVincent Yang static const struct of_device_id f_sdh30_dt_ids[] = {
23187a50745SVincent Yang 	{ .compatible = "fujitsu,mb86s70-sdhci-3.0" },
2325914a9b1SKunihiko Hayashi 	{ .compatible = "socionext,f-sdh30-e51-mmc" },
23387a50745SVincent Yang 	{ /* sentinel */ }
23487a50745SVincent Yang };
23587a50745SVincent Yang MODULE_DEVICE_TABLE(of, f_sdh30_dt_ids);
23690e1d8ccSArd Biesheuvel #endif
23790e1d8ccSArd Biesheuvel 
23890e1d8ccSArd Biesheuvel #ifdef CONFIG_ACPI
23990e1d8ccSArd Biesheuvel static const struct acpi_device_id f_sdh30_acpi_ids[] = {
24090e1d8ccSArd Biesheuvel 	{ "SCX0002" },
24190e1d8ccSArd Biesheuvel 	{ /* sentinel */ }
24290e1d8ccSArd Biesheuvel };
24390e1d8ccSArd Biesheuvel MODULE_DEVICE_TABLE(acpi, f_sdh30_acpi_ids);
24490e1d8ccSArd Biesheuvel #endif
24587a50745SVincent Yang 
24687a50745SVincent Yang static struct platform_driver sdhci_f_sdh30_driver = {
24787a50745SVincent Yang 	.driver = {
24887a50745SVincent Yang 		.name = "f_sdh30",
24921b2cec6SDouglas Anderson 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
25090e1d8ccSArd Biesheuvel 		.of_match_table = of_match_ptr(f_sdh30_dt_ids),
25190e1d8ccSArd Biesheuvel 		.acpi_match_table = ACPI_PTR(f_sdh30_acpi_ids),
252fa243f64SUlf Hansson 		.pm	= &sdhci_pltfm_pmops,
25387a50745SVincent Yang 	},
25487a50745SVincent Yang 	.probe	= sdhci_f_sdh30_probe,
25587a50745SVincent Yang 	.remove	= sdhci_f_sdh30_remove,
25687a50745SVincent Yang };
25787a50745SVincent Yang 
25887a50745SVincent Yang module_platform_driver(sdhci_f_sdh30_driver);
25987a50745SVincent Yang 
26087a50745SVincent Yang MODULE_DESCRIPTION("F_SDH30 SD Card Controller driver");
26187a50745SVincent Yang MODULE_LICENSE("GPL v2");
2625914a9b1SKunihiko Hayashi MODULE_AUTHOR("FUJITSU SEMICONDUCTOR LTD., Socionext Inc.");
26387a50745SVincent Yang MODULE_ALIAS("platform:f_sdh30");
264