12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
262ca8034SShashidhar Hiremath /*
362ca8034SShashidhar Hiremath * Synopsys DesignWare Multimedia Card Interface driver
462ca8034SShashidhar Hiremath *
562ca8034SShashidhar Hiremath * Copyright (C) 2009 NXP Semiconductors
662ca8034SShashidhar Hiremath * Copyright (C) 2009, 2010 Imagination Technologies Ltd.
762ca8034SShashidhar Hiremath */
862ca8034SShashidhar Hiremath
9a3e2cd7fSThierry Reding #include <linux/err.h>
1062ca8034SShashidhar Hiremath #include <linux/interrupt.h>
1162ca8034SShashidhar Hiremath #include <linux/module.h>
1262ca8034SShashidhar Hiremath #include <linux/io.h>
1362ca8034SShashidhar Hiremath #include <linux/irq.h>
1462ca8034SShashidhar Hiremath #include <linux/platform_device.h>
15ee2112beSShawn Lin #include <linux/pm_runtime.h>
1662ca8034SShashidhar Hiremath #include <linux/slab.h>
1762ca8034SShashidhar Hiremath #include <linux/mmc/host.h>
1862ca8034SShashidhar Hiremath #include <linux/mmc/mmc.h>
19c91eab4bSThomas Abraham #include <linux/of.h>
20ef87bd81SDinh Nguyen #include <linux/mfd/altera-sysmgr.h>
21ef87bd81SDinh Nguyen #include <linux/regmap.h>
22c91eab4bSThomas Abraham
2362ca8034SShashidhar Hiremath #include "dw_mmc.h"
247725a52cSJingoo Han #include "dw_mmc-pltfm.h"
2562ca8034SShashidhar Hiremath
26ef87bd81SDinh Nguyen #define SOCFPGA_DW_MMC_CLK_PHASE_STEP 45
27ef87bd81SDinh Nguyen #define SYSMGR_SDMMC_CTRL_SET(smplsel, drvsel, reg_shift) \
28ef87bd81SDinh Nguyen ((((smplsel) & 0x7) << reg_shift) | (((drvsel) & 0x7) << 0))
29ef87bd81SDinh Nguyen
dw_mci_pltfm_register(struct platform_device * pdev,const struct dw_mci_drv_data * drv_data)30800d78bfSThomas Abraham int dw_mci_pltfm_register(struct platform_device *pdev,
318e2b36eaSArnd Bergmann const struct dw_mci_drv_data *drv_data)
3262ca8034SShashidhar Hiremath {
3362ca8034SShashidhar Hiremath struct dw_mci *host;
3462ca8034SShashidhar Hiremath struct resource *regs;
3562ca8034SShashidhar Hiremath
36bb8bdc77SThomas Abraham host = devm_kzalloc(&pdev->dev, sizeof(struct dw_mci), GFP_KERNEL);
3762ca8034SShashidhar Hiremath if (!host)
3862ca8034SShashidhar Hiremath return -ENOMEM;
3962ca8034SShashidhar Hiremath
4062ca8034SShashidhar Hiremath host->irq = platform_get_irq(pdev, 0);
41bb8bdc77SThomas Abraham if (host->irq < 0)
42bb8bdc77SThomas Abraham return host->irq;
4362ca8034SShashidhar Hiremath
44800d78bfSThomas Abraham host->drv_data = drv_data;
454a90920cSThomas Abraham host->dev = &pdev->dev;
4662ca8034SShashidhar Hiremath host->irq_flags = 0;
4762ca8034SShashidhar Hiremath host->pdata = pdev->dev.platform_data;
48bcc87666SAndy Shevchenko
49*220b484fSYang Li host->regs = devm_platform_get_and_ioremap_resource(pdev, 0, ®s);
50a3e2cd7fSThierry Reding if (IS_ERR(host->regs))
51a3e2cd7fSThierry Reding return PTR_ERR(host->regs);
52bb8bdc77SThomas Abraham
5345c7a490SJaehoon Chung /* Get registers' physical base address */
5445c7a490SJaehoon Chung host->phy_regs = regs->start;
5545c7a490SJaehoon Chung
5662ca8034SShashidhar Hiremath platform_set_drvdata(pdev, host);
57dd369800SAndy Shevchenko return dw_mci_probe(host);
5862ca8034SShashidhar Hiremath }
5917403f23SThomas Abraham EXPORT_SYMBOL_GPL(dw_mci_pltfm_register);
6017403f23SThomas Abraham
61ee2112beSShawn Lin const struct dev_pm_ops dw_mci_pltfm_pmops = {
62ee2112beSShawn Lin SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
63ee2112beSShawn Lin pm_runtime_force_resume)
64ee2112beSShawn Lin SET_RUNTIME_PM_OPS(dw_mci_runtime_suspend,
65ee2112beSShawn Lin dw_mci_runtime_resume,
66ee2112beSShawn Lin NULL)
67ee2112beSShawn Lin };
6817403f23SThomas Abraham EXPORT_SYMBOL_GPL(dw_mci_pltfm_pmops);
6962ca8034SShashidhar Hiremath
dw_mci_socfpga_priv_init(struct dw_mci * host)70ef87bd81SDinh Nguyen static int dw_mci_socfpga_priv_init(struct dw_mci *host)
71ef87bd81SDinh Nguyen {
72ef87bd81SDinh Nguyen struct device_node *np = host->dev->of_node;
73ef87bd81SDinh Nguyen struct regmap *sys_mgr_base_addr;
74ef87bd81SDinh Nguyen u32 clk_phase[2] = {0}, reg_offset, reg_shift;
75ef87bd81SDinh Nguyen int i, rc, hs_timing;
76ef87bd81SDinh Nguyen
77ef87bd81SDinh Nguyen rc = of_property_read_variable_u32_array(np, "clk-phase-sd-hs", &clk_phase[0], 2, 0);
78ef87bd81SDinh Nguyen if (rc < 0)
79ef87bd81SDinh Nguyen return 0;
80ef87bd81SDinh Nguyen
81ef87bd81SDinh Nguyen sys_mgr_base_addr = altr_sysmgr_regmap_lookup_by_phandle(np, "altr,sysmgr-syscon");
82ef87bd81SDinh Nguyen if (IS_ERR(sys_mgr_base_addr)) {
83ef87bd81SDinh Nguyen dev_warn(host->dev, "clk-phase-sd-hs was specified, but failed to find altr,sys-mgr regmap!\n");
84ef87bd81SDinh Nguyen return 0;
85ef87bd81SDinh Nguyen }
86ef87bd81SDinh Nguyen
87ef87bd81SDinh Nguyen of_property_read_u32_index(np, "altr,sysmgr-syscon", 1, ®_offset);
88ef87bd81SDinh Nguyen of_property_read_u32_index(np, "altr,sysmgr-syscon", 2, ®_shift);
89ef87bd81SDinh Nguyen
90ef87bd81SDinh Nguyen for (i = 0; i < ARRAY_SIZE(clk_phase); i++)
91ef87bd81SDinh Nguyen clk_phase[i] /= SOCFPGA_DW_MMC_CLK_PHASE_STEP;
92ef87bd81SDinh Nguyen
93ef87bd81SDinh Nguyen hs_timing = SYSMGR_SDMMC_CTRL_SET(clk_phase[0], clk_phase[1], reg_shift);
94ef87bd81SDinh Nguyen regmap_write(sys_mgr_base_addr, reg_offset, hs_timing);
95ef87bd81SDinh Nguyen
96ef87bd81SDinh Nguyen return 0;
97ef87bd81SDinh Nguyen }
98ef87bd81SDinh Nguyen
99ef87bd81SDinh Nguyen static const struct dw_mci_drv_data socfpga_drv_data = {
100ef87bd81SDinh Nguyen .init = dw_mci_socfpga_priv_init,
101ef87bd81SDinh Nguyen };
102ef87bd81SDinh Nguyen
103c91eab4bSThomas Abraham static const struct of_device_id dw_mci_pltfm_match[] = {
104c91eab4bSThomas Abraham { .compatible = "snps,dw-mshc", },
105ef87bd81SDinh Nguyen { .compatible = "altr,socfpga-dw-mshc", .data = &socfpga_drv_data, },
106aaaaeb7aSJaehoon Chung { .compatible = "img,pistachio-dw-mshc", },
107c91eab4bSThomas Abraham {},
108c91eab4bSThomas Abraham };
109c91eab4bSThomas Abraham MODULE_DEVICE_TABLE(of, dw_mci_pltfm_match);
110c91eab4bSThomas Abraham
dw_mci_pltfm_probe(struct platform_device * pdev)111b177a530SHeiko Stübner static int dw_mci_pltfm_probe(struct platform_device *pdev)
112b177a530SHeiko Stübner {
113c73e41c8SHeiko Stübner const struct dw_mci_drv_data *drv_data = NULL;
114c73e41c8SHeiko Stübner const struct of_device_id *match;
115c73e41c8SHeiko Stübner
116c73e41c8SHeiko Stübner if (pdev->dev.of_node) {
117c73e41c8SHeiko Stübner match = of_match_node(dw_mci_pltfm_match, pdev->dev.of_node);
118c73e41c8SHeiko Stübner drv_data = match->data;
119c73e41c8SHeiko Stübner }
120c73e41c8SHeiko Stübner
121c73e41c8SHeiko Stübner return dw_mci_pltfm_register(pdev, drv_data);
122b177a530SHeiko Stübner }
123b177a530SHeiko Stübner
dw_mci_pltfm_remove(struct platform_device * pdev)124b177a530SHeiko Stübner void dw_mci_pltfm_remove(struct platform_device *pdev)
125b177a530SHeiko Stübner {
126b177a530SHeiko Stübner struct dw_mci *host = platform_get_drvdata(pdev);
127b177a530SHeiko Stübner
128b177a530SHeiko Stübner dw_mci_remove(host);
129b177a530SHeiko Stübner }
130b177a530SHeiko Stübner EXPORT_SYMBOL_GPL(dw_mci_pltfm_remove);
131b177a530SHeiko Stübner
132b177a530SHeiko Stübner static struct platform_driver dw_mci_pltfm_driver = {
13362ca8034SShashidhar Hiremath .probe = dw_mci_pltfm_probe,
13449480cf2SAndy Shevchenko .remove_new = dw_mci_pltfm_remove,
1354e608e4eSGreg Kroah-Hartman .driver = {
13662ca8034SShashidhar Hiremath .name = "dw_mmc",
13762ca8034SShashidhar Hiremath .probe_type = PROBE_PREFER_ASYNCHRONOUS,
13821b2cec6SDouglas Anderson .of_match_table = dw_mci_pltfm_match,
139cf109bc0SSachin Kamat .pm = &dw_mci_pltfm_pmops,
14062ca8034SShashidhar Hiremath },
14162ca8034SShashidhar Hiremath };
14262ca8034SShashidhar Hiremath
14362ca8034SShashidhar Hiremath module_platform_driver(dw_mci_pltfm_driver);
14449480cf2SAndy Shevchenko
14562ca8034SShashidhar Hiremath MODULE_DESCRIPTION("DW Multimedia Card Interface driver");
14662ca8034SShashidhar Hiremath MODULE_AUTHOR("NXP Semiconductor VietNam");
14762ca8034SShashidhar Hiremath MODULE_AUTHOR("Imagination Technologies Ltd");
14862ca8034SShashidhar Hiremath MODULE_LICENSE("GPL v2");
14962ca8034SShashidhar Hiremath