xref: /openbmc/linux/drivers/mmc/host/dw_mmc-pltfm.c (revision d47a97bd)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Synopsys DesignWare Multimedia Card Interface driver
4  *
5  * Copyright (C) 2009 NXP Semiconductors
6  * Copyright (C) 2009, 2010 Imagination Technologies Ltd.
7  */
8 
9 #include <linux/err.h>
10 #include <linux/interrupt.h>
11 #include <linux/module.h>
12 #include <linux/io.h>
13 #include <linux/irq.h>
14 #include <linux/platform_device.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/slab.h>
17 #include <linux/mmc/host.h>
18 #include <linux/mmc/mmc.h>
19 #include <linux/of.h>
20 #include <linux/mfd/altera-sysmgr.h>
21 #include <linux/regmap.h>
22 
23 #include "dw_mmc.h"
24 #include "dw_mmc-pltfm.h"
25 
26 #define SOCFPGA_DW_MMC_CLK_PHASE_STEP	45
27 #define SYSMGR_SDMMC_CTRL_SET(smplsel, drvsel, reg_shift) \
28 	((((smplsel) & 0x7) << reg_shift) | (((drvsel) & 0x7) << 0))
29 
30 int dw_mci_pltfm_register(struct platform_device *pdev,
31 			  const struct dw_mci_drv_data *drv_data)
32 {
33 	struct dw_mci *host;
34 	struct resource	*regs;
35 
36 	host = devm_kzalloc(&pdev->dev, sizeof(struct dw_mci), GFP_KERNEL);
37 	if (!host)
38 		return -ENOMEM;
39 
40 	host->irq = platform_get_irq(pdev, 0);
41 	if (host->irq < 0)
42 		return host->irq;
43 
44 	host->drv_data = drv_data;
45 	host->dev = &pdev->dev;
46 	host->irq_flags = 0;
47 	host->pdata = pdev->dev.platform_data;
48 
49 	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
50 	host->regs = devm_ioremap_resource(&pdev->dev, regs);
51 	if (IS_ERR(host->regs))
52 		return PTR_ERR(host->regs);
53 
54 	/* Get registers' physical base address */
55 	host->phy_regs = regs->start;
56 
57 	platform_set_drvdata(pdev, host);
58 	return dw_mci_probe(host);
59 }
60 EXPORT_SYMBOL_GPL(dw_mci_pltfm_register);
61 
62 const struct dev_pm_ops dw_mci_pltfm_pmops = {
63 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
64 				pm_runtime_force_resume)
65 	SET_RUNTIME_PM_OPS(dw_mci_runtime_suspend,
66 			   dw_mci_runtime_resume,
67 			   NULL)
68 };
69 EXPORT_SYMBOL_GPL(dw_mci_pltfm_pmops);
70 
71 static int dw_mci_socfpga_priv_init(struct dw_mci *host)
72 {
73 	struct device_node *np = host->dev->of_node;
74 	struct regmap *sys_mgr_base_addr;
75 	u32 clk_phase[2] = {0}, reg_offset, reg_shift;
76 	int i, rc, hs_timing;
77 
78 	rc = of_property_read_variable_u32_array(np, "clk-phase-sd-hs", &clk_phase[0], 2, 0);
79 	if (rc < 0)
80 		return 0;
81 
82 	sys_mgr_base_addr = altr_sysmgr_regmap_lookup_by_phandle(np, "altr,sysmgr-syscon");
83 	if (IS_ERR(sys_mgr_base_addr)) {
84 		dev_warn(host->dev, "clk-phase-sd-hs was specified, but failed to find altr,sys-mgr regmap!\n");
85 		return 0;
86 	}
87 
88 	of_property_read_u32_index(np, "altr,sysmgr-syscon", 1, &reg_offset);
89 	of_property_read_u32_index(np, "altr,sysmgr-syscon", 2, &reg_shift);
90 
91 	for (i = 0; i < ARRAY_SIZE(clk_phase); i++)
92 		clk_phase[i] /= SOCFPGA_DW_MMC_CLK_PHASE_STEP;
93 
94 	hs_timing = SYSMGR_SDMMC_CTRL_SET(clk_phase[0], clk_phase[1], reg_shift);
95 	regmap_write(sys_mgr_base_addr, reg_offset, hs_timing);
96 
97 	return 0;
98 }
99 
100 static const struct dw_mci_drv_data socfpga_drv_data = {
101 	.init		= dw_mci_socfpga_priv_init,
102 };
103 
104 static const struct of_device_id dw_mci_pltfm_match[] = {
105 	{ .compatible = "snps,dw-mshc", },
106 	{ .compatible = "altr,socfpga-dw-mshc", .data = &socfpga_drv_data, },
107 	{ .compatible = "img,pistachio-dw-mshc", },
108 	{},
109 };
110 MODULE_DEVICE_TABLE(of, dw_mci_pltfm_match);
111 
112 static int dw_mci_pltfm_probe(struct platform_device *pdev)
113 {
114 	const struct dw_mci_drv_data *drv_data = NULL;
115 	const struct of_device_id *match;
116 
117 	if (pdev->dev.of_node) {
118 		match = of_match_node(dw_mci_pltfm_match, pdev->dev.of_node);
119 		drv_data = match->data;
120 	}
121 
122 	return dw_mci_pltfm_register(pdev, drv_data);
123 }
124 
125 int dw_mci_pltfm_remove(struct platform_device *pdev)
126 {
127 	struct dw_mci *host = platform_get_drvdata(pdev);
128 
129 	dw_mci_remove(host);
130 	return 0;
131 }
132 EXPORT_SYMBOL_GPL(dw_mci_pltfm_remove);
133 
134 static struct platform_driver dw_mci_pltfm_driver = {
135 	.probe		= dw_mci_pltfm_probe,
136 	.remove		= dw_mci_pltfm_remove,
137 	.driver		= {
138 		.name		= "dw_mmc",
139 		.probe_type	= PROBE_PREFER_ASYNCHRONOUS,
140 		.of_match_table	= dw_mci_pltfm_match,
141 		.pm		= &dw_mci_pltfm_pmops,
142 	},
143 };
144 
145 module_platform_driver(dw_mci_pltfm_driver);
146 
147 MODULE_DESCRIPTION("DW Multimedia Card Interface driver");
148 MODULE_AUTHOR("NXP Semiconductor VietNam");
149 MODULE_AUTHOR("Imagination Technologies Ltd");
150 MODULE_LICENSE("GPL v2");
151