Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * sdhci-pltfm.c Support for SDHCI platform devices
0004  * Copyright (c) 2009 Intel Corporation
0005  *
0006  * Copyright (c) 2007, 2011 Freescale Semiconductor, Inc.
0007  * Copyright (c) 2009 MontaVista Software, Inc.
0008  *
0009  * Authors: Xiaobo Xie <X.Xie@freescale.com>
0010  *      Anton Vorontsov <avorontsov@ru.mvista.com>
0011  */
0012 
0013 /* Supports:
0014  * SDHCI platform devices
0015  *
0016  * Inspired by sdhci-pci.c, by Pierre Ossman
0017  */
0018 
0019 #include <linux/err.h>
0020 #include <linux/module.h>
0021 #include <linux/property.h>
0022 #include <linux/of.h>
0023 #ifdef CONFIG_PPC
0024 #include <asm/machdep.h>
0025 #endif
0026 #include "sdhci-pltfm.h"
0027 
0028 unsigned int sdhci_pltfm_clk_get_max_clock(struct sdhci_host *host)
0029 {
0030     struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
0031 
0032     return clk_get_rate(pltfm_host->clk);
0033 }
0034 EXPORT_SYMBOL_GPL(sdhci_pltfm_clk_get_max_clock);
0035 
0036 static const struct sdhci_ops sdhci_pltfm_ops = {
0037     .set_clock = sdhci_set_clock,
0038     .set_bus_width = sdhci_set_bus_width,
0039     .reset = sdhci_reset,
0040     .set_uhs_signaling = sdhci_set_uhs_signaling,
0041 };
0042 
0043 static bool sdhci_wp_inverted(struct device *dev)
0044 {
0045     if (device_property_present(dev, "sdhci,wp-inverted") ||
0046         device_property_present(dev, "wp-inverted"))
0047         return true;
0048 
0049     /* Old device trees don't have the wp-inverted property. */
0050 #ifdef CONFIG_PPC
0051     return machine_is(mpc837x_rdb) || machine_is(mpc837x_mds);
0052 #else
0053     return false;
0054 #endif /* CONFIG_PPC */
0055 }
0056 
0057 #ifdef CONFIG_OF
0058 static void sdhci_get_compatibility(struct platform_device *pdev)
0059 {
0060     struct sdhci_host *host = platform_get_drvdata(pdev);
0061     struct device_node *np = pdev->dev.of_node;
0062 
0063     if (!np)
0064         return;
0065 
0066     if (of_device_is_compatible(np, "fsl,p2020-rev1-esdhc"))
0067         host->quirks |= SDHCI_QUIRK_BROKEN_DMA;
0068 
0069     if (of_device_is_compatible(np, "fsl,p2020-esdhc") ||
0070         of_device_is_compatible(np, "fsl,p1010-esdhc") ||
0071         of_device_is_compatible(np, "fsl,t4240-esdhc") ||
0072         of_device_is_compatible(np, "fsl,mpc8536-esdhc"))
0073         host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
0074 }
0075 #else
0076 void sdhci_get_compatibility(struct platform_device *pdev) {}
0077 #endif /* CONFIG_OF */
0078 
0079 void sdhci_get_property(struct platform_device *pdev)
0080 {
0081     struct device *dev = &pdev->dev;
0082     struct sdhci_host *host = platform_get_drvdata(pdev);
0083     struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
0084     u32 bus_width;
0085 
0086     if (device_property_present(dev, "sdhci,auto-cmd12"))
0087         host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
0088 
0089     if (device_property_present(dev, "sdhci,1-bit-only") ||
0090         (device_property_read_u32(dev, "bus-width", &bus_width) == 0 &&
0091         bus_width == 1))
0092         host->quirks |= SDHCI_QUIRK_FORCE_1_BIT_DATA;
0093 
0094     if (sdhci_wp_inverted(dev))
0095         host->quirks |= SDHCI_QUIRK_INVERTED_WRITE_PROTECT;
0096 
0097     if (device_property_present(dev, "broken-cd"))
0098         host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
0099 
0100     if (device_property_present(dev, "no-1-8-v"))
0101         host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
0102 
0103     sdhci_get_compatibility(pdev);
0104 
0105     device_property_read_u32(dev, "clock-frequency", &pltfm_host->clock);
0106 
0107     if (device_property_present(dev, "keep-power-in-suspend"))
0108         host->mmc->pm_caps |= MMC_PM_KEEP_POWER;
0109 
0110     if (device_property_read_bool(dev, "wakeup-source") ||
0111         device_property_read_bool(dev, "enable-sdio-wakeup")) /* legacy */
0112         host->mmc->pm_caps |= MMC_PM_WAKE_SDIO_IRQ;
0113 }
0114 EXPORT_SYMBOL_GPL(sdhci_get_property);
0115 
0116 struct sdhci_host *sdhci_pltfm_init(struct platform_device *pdev,
0117                     const struct sdhci_pltfm_data *pdata,
0118                     size_t priv_size)
0119 {
0120     struct sdhci_host *host;
0121     void __iomem *ioaddr;
0122     int irq, ret;
0123 
0124     ioaddr = devm_platform_ioremap_resource(pdev, 0);
0125     if (IS_ERR(ioaddr)) {
0126         ret = PTR_ERR(ioaddr);
0127         goto err;
0128     }
0129 
0130     irq = platform_get_irq(pdev, 0);
0131     if (irq < 0) {
0132         ret = irq;
0133         goto err;
0134     }
0135 
0136     host = sdhci_alloc_host(&pdev->dev,
0137         sizeof(struct sdhci_pltfm_host) + priv_size);
0138 
0139     if (IS_ERR(host)) {
0140         ret = PTR_ERR(host);
0141         goto err;
0142     }
0143 
0144     host->ioaddr = ioaddr;
0145     host->irq = irq;
0146     host->hw_name = dev_name(&pdev->dev);
0147     if (pdata && pdata->ops)
0148         host->ops = pdata->ops;
0149     else
0150         host->ops = &sdhci_pltfm_ops;
0151     if (pdata) {
0152         host->quirks = pdata->quirks;
0153         host->quirks2 = pdata->quirks2;
0154     }
0155 
0156     platform_set_drvdata(pdev, host);
0157 
0158     return host;
0159 err:
0160     dev_err(&pdev->dev, "%s failed %d\n", __func__, ret);
0161     return ERR_PTR(ret);
0162 }
0163 EXPORT_SYMBOL_GPL(sdhci_pltfm_init);
0164 
0165 void sdhci_pltfm_free(struct platform_device *pdev)
0166 {
0167     struct sdhci_host *host = platform_get_drvdata(pdev);
0168 
0169     sdhci_free_host(host);
0170 }
0171 EXPORT_SYMBOL_GPL(sdhci_pltfm_free);
0172 
0173 int sdhci_pltfm_register(struct platform_device *pdev,
0174             const struct sdhci_pltfm_data *pdata,
0175             size_t priv_size)
0176 {
0177     struct sdhci_host *host;
0178     int ret = 0;
0179 
0180     host = sdhci_pltfm_init(pdev, pdata, priv_size);
0181     if (IS_ERR(host))
0182         return PTR_ERR(host);
0183 
0184     sdhci_get_property(pdev);
0185 
0186     ret = sdhci_add_host(host);
0187     if (ret)
0188         sdhci_pltfm_free(pdev);
0189 
0190     return ret;
0191 }
0192 EXPORT_SYMBOL_GPL(sdhci_pltfm_register);
0193 
0194 int sdhci_pltfm_unregister(struct platform_device *pdev)
0195 {
0196     struct sdhci_host *host = platform_get_drvdata(pdev);
0197     struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
0198     int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
0199 
0200     sdhci_remove_host(host, dead);
0201     clk_disable_unprepare(pltfm_host->clk);
0202     sdhci_pltfm_free(pdev);
0203 
0204     return 0;
0205 }
0206 EXPORT_SYMBOL_GPL(sdhci_pltfm_unregister);
0207 
0208 #ifdef CONFIG_PM_SLEEP
0209 int sdhci_pltfm_suspend(struct device *dev)
0210 {
0211     struct sdhci_host *host = dev_get_drvdata(dev);
0212     struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
0213     int ret;
0214 
0215     if (host->tuning_mode != SDHCI_TUNING_MODE_3)
0216         mmc_retune_needed(host->mmc);
0217 
0218     ret = sdhci_suspend_host(host);
0219     if (ret)
0220         return ret;
0221 
0222     clk_disable_unprepare(pltfm_host->clk);
0223 
0224     return 0;
0225 }
0226 EXPORT_SYMBOL_GPL(sdhci_pltfm_suspend);
0227 
0228 int sdhci_pltfm_resume(struct device *dev)
0229 {
0230     struct sdhci_host *host = dev_get_drvdata(dev);
0231     struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
0232     int ret;
0233 
0234     ret = clk_prepare_enable(pltfm_host->clk);
0235     if (ret)
0236         return ret;
0237 
0238     ret = sdhci_resume_host(host);
0239     if (ret)
0240         clk_disable_unprepare(pltfm_host->clk);
0241 
0242     return ret;
0243 }
0244 EXPORT_SYMBOL_GPL(sdhci_pltfm_resume);
0245 #endif
0246 
0247 const struct dev_pm_ops sdhci_pltfm_pmops = {
0248     SET_SYSTEM_SLEEP_PM_OPS(sdhci_pltfm_suspend, sdhci_pltfm_resume)
0249 };
0250 EXPORT_SYMBOL_GPL(sdhci_pltfm_pmops);
0251 
0252 static int __init sdhci_pltfm_drv_init(void)
0253 {
0254     pr_info("sdhci-pltfm: SDHCI platform and OF driver helper\n");
0255 
0256     return 0;
0257 }
0258 module_init(sdhci_pltfm_drv_init);
0259 
0260 static void __exit sdhci_pltfm_drv_exit(void)
0261 {
0262 }
0263 module_exit(sdhci_pltfm_drv_exit);
0264 
0265 MODULE_DESCRIPTION("SDHCI platform and OF driver helper");
0266 MODULE_AUTHOR("Intel Corporation");
0267 MODULE_LICENSE("GPL v2");