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 325def5c1cSKunihiko Hayashi static void *sdhci_f_sdhost_priv(struct sdhci_host *host) 335def5c1cSKunihiko Hayashi { 345def5c1cSKunihiko Hayashi struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 355def5c1cSKunihiko Hayashi 365def5c1cSKunihiko Hayashi return sdhci_pltfm_priv(pltfm_host); 375def5c1cSKunihiko Hayashi } 385def5c1cSKunihiko Hayashi 39cee4e7a5SAxel Lin static void sdhci_f_sdh30_soft_voltage_switch(struct sdhci_host *host) 4087a50745SVincent Yang { 415def5c1cSKunihiko Hayashi struct f_sdhost_priv *priv = sdhci_f_sdhost_priv(host); 4287a50745SVincent Yang u32 ctrl = 0; 4387a50745SVincent Yang 4487a50745SVincent Yang usleep_range(2500, 3000); 4587a50745SVincent Yang ctrl = sdhci_readl(host, F_SDH30_IO_CONTROL2); 4687a50745SVincent Yang ctrl |= F_SDH30_CRES_O_DN; 4787a50745SVincent Yang sdhci_writel(host, ctrl, F_SDH30_IO_CONTROL2); 4887a50745SVincent Yang ctrl |= F_SDH30_MSEL_O_1_8; 4987a50745SVincent Yang sdhci_writel(host, ctrl, F_SDH30_IO_CONTROL2); 5087a50745SVincent Yang 5187a50745SVincent Yang ctrl &= ~F_SDH30_CRES_O_DN; 5287a50745SVincent Yang sdhci_writel(host, ctrl, F_SDH30_IO_CONTROL2); 5387a50745SVincent Yang usleep_range(2500, 3000); 5487a50745SVincent Yang 5587a50745SVincent Yang if (priv->vendor_hs200) { 5687a50745SVincent Yang dev_info(priv->dev, "%s: setting hs200\n", __func__); 5787a50745SVincent Yang ctrl = sdhci_readl(host, F_SDH30_ESD_CONTROL); 5887a50745SVincent Yang ctrl |= priv->vendor_hs200; 5987a50745SVincent Yang sdhci_writel(host, ctrl, F_SDH30_ESD_CONTROL); 6087a50745SVincent Yang } 6187a50745SVincent Yang 6287a50745SVincent Yang ctrl = sdhci_readl(host, F_SDH30_TUNING_SETTING); 6387a50745SVincent Yang ctrl |= F_SDH30_CMD_CHK_DIS; 6487a50745SVincent Yang sdhci_writel(host, ctrl, F_SDH30_TUNING_SETTING); 6587a50745SVincent Yang } 6687a50745SVincent Yang 67cee4e7a5SAxel Lin static unsigned int sdhci_f_sdh30_get_min_clock(struct sdhci_host *host) 6887a50745SVincent Yang { 6987a50745SVincent Yang return F_SDH30_MIN_CLOCK; 7087a50745SVincent Yang } 7187a50745SVincent Yang 72cee4e7a5SAxel Lin static void sdhci_f_sdh30_reset(struct sdhci_host *host, u8 mask) 7387a50745SVincent Yang { 745def5c1cSKunihiko Hayashi struct f_sdhost_priv *priv = sdhci_f_sdhost_priv(host); 7506641e8dSArd Biesheuvel u32 ctl; 7606641e8dSArd Biesheuvel 7787a50745SVincent Yang if (sdhci_readw(host, SDHCI_CLOCK_CONTROL) == 0) 7887a50745SVincent Yang sdhci_writew(host, 0xBC01, SDHCI_CLOCK_CONTROL); 7987a50745SVincent Yang 8087a50745SVincent Yang sdhci_reset(host, mask); 8106641e8dSArd Biesheuvel 8206641e8dSArd Biesheuvel if (priv->enable_cmd_dat_delay) { 8306641e8dSArd Biesheuvel ctl = sdhci_readl(host, F_SDH30_ESD_CONTROL); 8406641e8dSArd Biesheuvel ctl |= F_SDH30_CMD_DAT_DELAY; 8506641e8dSArd Biesheuvel sdhci_writel(host, ctl, F_SDH30_ESD_CONTROL); 8606641e8dSArd Biesheuvel } 87e2d2dcc8SKunihiko Hayashi 88e2d2dcc8SKunihiko Hayashi if ((host->mmc->caps & MMC_CAP_NONREMOVABLE) && 89e2d2dcc8SKunihiko Hayashi !(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) { 90e2d2dcc8SKunihiko Hayashi ctl = sdhci_readl(host, F_SDH30_TEST); 91e2d2dcc8SKunihiko Hayashi ctl |= F_SDH30_FORCE_CARD_INSERT; 92e2d2dcc8SKunihiko Hayashi sdhci_writel(host, ctl, F_SDH30_TEST); 93e2d2dcc8SKunihiko Hayashi } 9487a50745SVincent Yang } 9587a50745SVincent Yang 9687a50745SVincent Yang static const struct sdhci_ops sdhci_f_sdh30_ops = { 9787a50745SVincent Yang .voltage_switch = sdhci_f_sdh30_soft_voltage_switch, 9887a50745SVincent Yang .get_min_clock = sdhci_f_sdh30_get_min_clock, 9987a50745SVincent Yang .reset = sdhci_f_sdh30_reset, 10087a50745SVincent Yang .set_clock = sdhci_set_clock, 10187a50745SVincent Yang .set_bus_width = sdhci_set_bus_width, 10287a50745SVincent Yang .set_uhs_signaling = sdhci_set_uhs_signaling, 10387a50745SVincent Yang }; 10487a50745SVincent Yang 1055def5c1cSKunihiko Hayashi static const struct sdhci_pltfm_data sdhci_f_sdh30_pltfm_data = { 1065def5c1cSKunihiko Hayashi .ops = &sdhci_f_sdh30_ops, 1075def5c1cSKunihiko Hayashi .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC 1085def5c1cSKunihiko Hayashi | SDHCI_QUIRK_INVERTED_WRITE_PROTECT, 1095def5c1cSKunihiko Hayashi .quirks2 = SDHCI_QUIRK2_SUPPORT_SINGLE 1105def5c1cSKunihiko Hayashi | SDHCI_QUIRK2_TUNING_WORK_AROUND, 1115def5c1cSKunihiko Hayashi }; 1125def5c1cSKunihiko Hayashi 11387a50745SVincent Yang static int sdhci_f_sdh30_probe(struct platform_device *pdev) 11487a50745SVincent Yang { 11587a50745SVincent Yang struct sdhci_host *host; 11687a50745SVincent Yang struct device *dev = &pdev->dev; 1175def5c1cSKunihiko Hayashi int ctrl = 0, ret = 0; 11887a50745SVincent Yang struct f_sdhost_priv *priv; 1195def5c1cSKunihiko Hayashi struct sdhci_pltfm_host *pltfm_host; 12087a50745SVincent Yang u32 reg = 0; 12187a50745SVincent Yang 1225def5c1cSKunihiko Hayashi host = sdhci_pltfm_init(pdev, &sdhci_f_sdh30_pltfm_data, 1235def5c1cSKunihiko Hayashi sizeof(struct f_sdhost_priv)); 12487a50745SVincent Yang if (IS_ERR(host)) 12587a50745SVincent Yang return PTR_ERR(host); 12687a50745SVincent Yang 1275def5c1cSKunihiko Hayashi pltfm_host = sdhci_priv(host); 1285def5c1cSKunihiko Hayashi priv = sdhci_pltfm_priv(pltfm_host); 12987a50745SVincent Yang priv->dev = dev; 13087a50745SVincent Yang 13106641e8dSArd Biesheuvel priv->enable_cmd_dat_delay = device_property_read_bool(dev, 13206641e8dSArd Biesheuvel "fujitsu,cmd-dat-delay-select"); 13306641e8dSArd Biesheuvel 13487a50745SVincent Yang ret = mmc_of_parse(host->mmc); 13587a50745SVincent Yang if (ret) 13687a50745SVincent Yang goto err; 13787a50745SVincent Yang 13890e1d8ccSArd Biesheuvel if (dev_of_node(dev)) { 13990e1d8ccSArd Biesheuvel sdhci_get_of_property(pdev); 14090e1d8ccSArd Biesheuvel 14187a50745SVincent Yang priv->clk_iface = devm_clk_get(&pdev->dev, "iface"); 14287a50745SVincent Yang if (IS_ERR(priv->clk_iface)) { 14387a50745SVincent Yang ret = PTR_ERR(priv->clk_iface); 14487a50745SVincent Yang goto err; 14587a50745SVincent Yang } 14687a50745SVincent Yang 14787a50745SVincent Yang ret = clk_prepare_enable(priv->clk_iface); 14887a50745SVincent Yang if (ret) 14987a50745SVincent Yang goto err; 15087a50745SVincent Yang 15187a50745SVincent Yang priv->clk = devm_clk_get(&pdev->dev, "core"); 15287a50745SVincent Yang if (IS_ERR(priv->clk)) { 15387a50745SVincent Yang ret = PTR_ERR(priv->clk); 15487a50745SVincent Yang goto err_clk; 15587a50745SVincent Yang } 15687a50745SVincent Yang 15787a50745SVincent Yang ret = clk_prepare_enable(priv->clk); 15887a50745SVincent Yang if (ret) 15987a50745SVincent Yang goto err_clk; 160bd724b27SKunihiko Hayashi 161bd724b27SKunihiko Hayashi priv->rst = devm_reset_control_get_optional_shared(dev, NULL); 162bd724b27SKunihiko Hayashi if (IS_ERR(priv->rst)) { 163bd724b27SKunihiko Hayashi ret = PTR_ERR(priv->rst); 164bd724b27SKunihiko Hayashi goto err_rst; 165bd724b27SKunihiko Hayashi } 166bd724b27SKunihiko Hayashi 167bd724b27SKunihiko Hayashi ret = reset_control_deassert(priv->rst); 168bd724b27SKunihiko Hayashi if (ret) 169bd724b27SKunihiko Hayashi goto err_rst; 17090e1d8ccSArd Biesheuvel } 17187a50745SVincent Yang 17287a50745SVincent Yang /* init vendor specific regs */ 17387a50745SVincent Yang ctrl = sdhci_readw(host, F_SDH30_AHB_CONFIG); 17487a50745SVincent Yang ctrl |= F_SDH30_SIN | F_SDH30_AHB_INCR_16 | F_SDH30_AHB_INCR_8 | 17587a50745SVincent Yang F_SDH30_AHB_INCR_4; 17687a50745SVincent Yang ctrl &= ~(F_SDH30_AHB_BIGED | F_SDH30_BUSLOCK_EN); 17787a50745SVincent Yang sdhci_writew(host, ctrl, F_SDH30_AHB_CONFIG); 17887a50745SVincent Yang 17987a50745SVincent Yang reg = sdhci_readl(host, F_SDH30_ESD_CONTROL); 18087a50745SVincent Yang sdhci_writel(host, reg & ~F_SDH30_EMMC_RST, F_SDH30_ESD_CONTROL); 18187a50745SVincent Yang msleep(20); 18287a50745SVincent Yang sdhci_writel(host, reg | F_SDH30_EMMC_RST, F_SDH30_ESD_CONTROL); 18387a50745SVincent Yang 18487a50745SVincent Yang reg = sdhci_readl(host, SDHCI_CAPABILITIES); 18587a50745SVincent Yang if (reg & SDHCI_CAN_DO_8BIT) 18687a50745SVincent Yang priv->vendor_hs200 = F_SDH30_EMMC_HS200; 18787a50745SVincent Yang 188aae9d3a4SKunihiko Hayashi if (!(reg & SDHCI_TIMEOUT_CLK_MASK)) 189aae9d3a4SKunihiko Hayashi host->quirks |= SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK; 190aae9d3a4SKunihiko Hayashi 19187a50745SVincent Yang ret = sdhci_add_host(host); 19287a50745SVincent Yang if (ret) 19387a50745SVincent Yang goto err_add_host; 19487a50745SVincent Yang 19587a50745SVincent Yang return 0; 19687a50745SVincent Yang 19787a50745SVincent Yang err_add_host: 198bd724b27SKunihiko Hayashi reset_control_assert(priv->rst); 199bd724b27SKunihiko Hayashi err_rst: 20087a50745SVincent Yang clk_disable_unprepare(priv->clk); 20187a50745SVincent Yang err_clk: 20287a50745SVincent Yang clk_disable_unprepare(priv->clk_iface); 20387a50745SVincent Yang err: 2045def5c1cSKunihiko Hayashi sdhci_pltfm_free(pdev); 2055def5c1cSKunihiko Hayashi 20687a50745SVincent Yang return ret; 20787a50745SVincent Yang } 20887a50745SVincent Yang 20987a50745SVincent Yang static int sdhci_f_sdh30_remove(struct platform_device *pdev) 21087a50745SVincent Yang { 21187a50745SVincent Yang struct sdhci_host *host = platform_get_drvdata(pdev); 2125def5c1cSKunihiko Hayashi struct f_sdhost_priv *priv = sdhci_f_sdhost_priv(host); 213*58abdd80SYangtao Li struct clk *clk_iface = priv->clk_iface; 214*58abdd80SYangtao Li struct reset_control *rst = priv->rst; 215*58abdd80SYangtao Li struct clk *clk = priv->clk; 21687a50745SVincent Yang 2175def5c1cSKunihiko Hayashi sdhci_pltfm_unregister(pdev); 21887a50745SVincent Yang 219*58abdd80SYangtao Li reset_control_assert(rst); 220*58abdd80SYangtao Li clk_disable_unprepare(clk); 221*58abdd80SYangtao Li clk_disable_unprepare(clk_iface); 222*58abdd80SYangtao Li 22387a50745SVincent Yang return 0; 22487a50745SVincent Yang } 22587a50745SVincent Yang 22690e1d8ccSArd Biesheuvel #ifdef CONFIG_OF 22787a50745SVincent Yang static const struct of_device_id f_sdh30_dt_ids[] = { 22887a50745SVincent Yang { .compatible = "fujitsu,mb86s70-sdhci-3.0" }, 2295914a9b1SKunihiko Hayashi { .compatible = "socionext,f-sdh30-e51-mmc" }, 23087a50745SVincent Yang { /* sentinel */ } 23187a50745SVincent Yang }; 23287a50745SVincent Yang MODULE_DEVICE_TABLE(of, f_sdh30_dt_ids); 23390e1d8ccSArd Biesheuvel #endif 23490e1d8ccSArd Biesheuvel 23590e1d8ccSArd Biesheuvel #ifdef CONFIG_ACPI 23690e1d8ccSArd Biesheuvel static const struct acpi_device_id f_sdh30_acpi_ids[] = { 23790e1d8ccSArd Biesheuvel { "SCX0002" }, 23890e1d8ccSArd Biesheuvel { /* sentinel */ } 23990e1d8ccSArd Biesheuvel }; 24090e1d8ccSArd Biesheuvel MODULE_DEVICE_TABLE(acpi, f_sdh30_acpi_ids); 24190e1d8ccSArd Biesheuvel #endif 24287a50745SVincent Yang 24387a50745SVincent Yang static struct platform_driver sdhci_f_sdh30_driver = { 24487a50745SVincent Yang .driver = { 24587a50745SVincent Yang .name = "f_sdh30", 24621b2cec6SDouglas Anderson .probe_type = PROBE_PREFER_ASYNCHRONOUS, 24790e1d8ccSArd Biesheuvel .of_match_table = of_match_ptr(f_sdh30_dt_ids), 24890e1d8ccSArd Biesheuvel .acpi_match_table = ACPI_PTR(f_sdh30_acpi_ids), 249fa243f64SUlf Hansson .pm = &sdhci_pltfm_pmops, 25087a50745SVincent Yang }, 25187a50745SVincent Yang .probe = sdhci_f_sdh30_probe, 25287a50745SVincent Yang .remove = sdhci_f_sdh30_remove, 25387a50745SVincent Yang }; 25487a50745SVincent Yang 25587a50745SVincent Yang module_platform_driver(sdhci_f_sdh30_driver); 25687a50745SVincent Yang 25787a50745SVincent Yang MODULE_DESCRIPTION("F_SDH30 SD Card Controller driver"); 25887a50745SVincent Yang MODULE_LICENSE("GPL v2"); 2595914a9b1SKunihiko Hayashi MODULE_AUTHOR("FUJITSU SEMICONDUCTOR LTD., Socionext Inc."); 26087a50745SVincent Yang MODULE_ALIAS("platform:f_sdh30"); 261