xref: /linux/drivers/mmc/host/dw_mmc-pltfm.c (revision 45bd2d77fbedec862204bb5c0fcaba2b7fa5fb56)
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 = dw_mci_alloc_host(&pdev->dev);
37 	if (IS_ERR(host))
38 		return PTR_ERR(host);
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->irq_flags = 0;
46 
47 	host->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &regs);
48 	if (IS_ERR(host->regs))
49 		return PTR_ERR(host->regs);
50 
51 	/* Get registers' physical base address */
52 	host->phy_regs = regs->start;
53 
54 	platform_set_drvdata(pdev, host);
55 	return dw_mci_probe(host);
56 }
57 EXPORT_SYMBOL_GPL(dw_mci_pltfm_register);
58 
59 static int dw_mci_socfpga_priv_init(struct dw_mci *host)
60 {
61 	struct device_node *np = host->dev->of_node;
62 	struct mmc_clk_phase phase;
63 	struct regmap *sys_mgr_base_addr;
64 	u32 reg_offset, reg_shift;
65 	int hs_timing;
66 
67 	phase = host->phase_map.phase[MMC_TIMING_SD_HS];
68 	if (!phase.valid)
69 		return 0;
70 
71 	sys_mgr_base_addr = altr_sysmgr_regmap_lookup_by_phandle(np, "altr,sysmgr-syscon");
72 	if (IS_ERR(sys_mgr_base_addr)) {
73 		dev_warn(host->dev, "clk-phase-sd-hs was specified, but failed to find altr,sys-mgr regmap!\n");
74 		return 0;
75 	}
76 
77 	of_property_read_u32_index(np, "altr,sysmgr-syscon", 1, &reg_offset);
78 	of_property_read_u32_index(np, "altr,sysmgr-syscon", 2, &reg_shift);
79 
80 	phase.in_deg /= SOCFPGA_DW_MMC_CLK_PHASE_STEP;
81 	phase.out_deg /= SOCFPGA_DW_MMC_CLK_PHASE_STEP;
82 
83 	hs_timing = SYSMGR_SDMMC_CTRL_SET(phase.in_deg, phase.out_deg, reg_shift);
84 	regmap_write(sys_mgr_base_addr, reg_offset, hs_timing);
85 
86 	return 0;
87 }
88 
89 static const struct dw_mci_drv_data socfpga_drv_data = {
90 	.init		= dw_mci_socfpga_priv_init,
91 };
92 
93 static const struct of_device_id dw_mci_pltfm_match[] = {
94 	{ .compatible = "snps,dw-mshc", },
95 	{ .compatible = "altr,socfpga-dw-mshc", .data = &socfpga_drv_data, },
96 	{ .compatible = "img,pistachio-dw-mshc", },
97 	{},
98 };
99 MODULE_DEVICE_TABLE(of, dw_mci_pltfm_match);
100 
101 static int dw_mci_pltfm_probe(struct platform_device *pdev)
102 {
103 	const struct dw_mci_drv_data *drv_data = NULL;
104 	const struct of_device_id *match;
105 
106 	if (pdev->dev.of_node) {
107 		match = of_match_node(dw_mci_pltfm_match, pdev->dev.of_node);
108 		drv_data = match->data;
109 	}
110 
111 	return dw_mci_pltfm_register(pdev, drv_data);
112 }
113 
114 void dw_mci_pltfm_remove(struct platform_device *pdev)
115 {
116 	struct dw_mci *host = platform_get_drvdata(pdev);
117 
118 	dw_mci_remove(host);
119 }
120 EXPORT_SYMBOL_GPL(dw_mci_pltfm_remove);
121 
122 static struct platform_driver dw_mci_pltfm_driver = {
123 	.probe		= dw_mci_pltfm_probe,
124 	.remove		= dw_mci_pltfm_remove,
125 	.driver		= {
126 		.name		= "dw_mmc",
127 		.probe_type	= PROBE_PREFER_ASYNCHRONOUS,
128 		.of_match_table	= dw_mci_pltfm_match,
129 		.pm		= pm_ptr(&dw_mci_pmops),
130 	},
131 };
132 
133 module_platform_driver(dw_mci_pltfm_driver);
134 
135 MODULE_DESCRIPTION("DW Multimedia Card Interface driver");
136 MODULE_AUTHOR("NXP Semiconductor VietNam");
137 MODULE_AUTHOR("Imagination Technologies Ltd");
138 MODULE_LICENSE("GPL v2");
139