xref: /openbmc/linux/drivers/mmc/host/dw_mmc-pltfm.c (revision 0529b810)
1 /*
2  * Synopsys DesignWare Multimedia Card Interface driver
3  *
4  * Copyright (C) 2009 NXP Semiconductors
5  * Copyright (C) 2009, 2010 Imagination Technologies Ltd.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  */
12 
13 #include <linux/err.h>
14 #include <linux/interrupt.h>
15 #include <linux/module.h>
16 #include <linux/io.h>
17 #include <linux/irq.h>
18 #include <linux/platform_device.h>
19 #include <linux/slab.h>
20 #include <linux/mmc/host.h>
21 #include <linux/mmc/mmc.h>
22 #include <linux/mmc/dw_mmc.h>
23 #include <linux/of.h>
24 #include <linux/clk.h>
25 
26 #include "dw_mmc.h"
27 #include "dw_mmc-pltfm.h"
28 
29 #define RK3288_CLKGEN_DIV	2
30 
31 static void dw_mci_pltfm_prepare_command(struct dw_mci *host, u32 *cmdr)
32 {
33 	*cmdr |= SDMMC_CMD_USE_HOLD_REG;
34 }
35 
36 static int dw_mci_rk3288_setup_clock(struct dw_mci *host)
37 {
38 	host->bus_hz /= RK3288_CLKGEN_DIV;
39 
40 	return 0;
41 }
42 
43 static void dw_mci_rk3288_set_ios(struct dw_mci *host, struct mmc_ios *ios)
44 {
45 	int ret;
46 	unsigned int cclkin;
47 	u32 bus_hz;
48 
49 	/*
50 	 * cclkin: source clock of mmc controller.
51 	 * bus_hz: card interface clock generated by CLKGEN.
52 	 * bus_hz = cclkin / RK3288_CLKGEN_DIV;
53 	 * ios->clock = (div == 0) ? bus_hz : (bus_hz / (2 * div))
54 	 *
55 	 * Note: div can only be 0 or 1
56 	 *       if DDR50 8bit mode(only emmc work in 8bit mode),
57 	 *       div must be set 1
58 	 */
59 	if ((ios->bus_width == MMC_BUS_WIDTH_8) &&
60 	    (ios->timing == MMC_TIMING_MMC_DDR52))
61 		cclkin = 2 * ios->clock * RK3288_CLKGEN_DIV;
62 	else
63 		cclkin = ios->clock * RK3288_CLKGEN_DIV;
64 
65 	ret = clk_set_rate(host->ciu_clk, cclkin);
66 	if (ret)
67 		dev_warn(host->dev, "failed to set rate %uHz\n", ios->clock);
68 
69 	bus_hz = clk_get_rate(host->ciu_clk) / RK3288_CLKGEN_DIV;
70 	if (bus_hz != host->bus_hz) {
71 		host->bus_hz = bus_hz;
72 		/* force dw_mci_setup_bus() */
73 		host->current_speed = 0;
74 	}
75 }
76 
77 static const struct dw_mci_drv_data rk2928_drv_data = {
78 	.prepare_command	= dw_mci_pltfm_prepare_command,
79 };
80 
81 static const struct dw_mci_drv_data rk3288_drv_data = {
82 	.prepare_command	= dw_mci_pltfm_prepare_command,
83 	.set_ios	= dw_mci_rk3288_set_ios,
84 	.setup_clock	= dw_mci_rk3288_setup_clock,
85 };
86 
87 static const struct dw_mci_drv_data socfpga_drv_data = {
88 	.prepare_command	= dw_mci_pltfm_prepare_command,
89 };
90 
91 int dw_mci_pltfm_register(struct platform_device *pdev,
92 			  const struct dw_mci_drv_data *drv_data)
93 {
94 	struct dw_mci *host;
95 	struct resource	*regs;
96 
97 	host = devm_kzalloc(&pdev->dev, sizeof(struct dw_mci), GFP_KERNEL);
98 	if (!host)
99 		return -ENOMEM;
100 
101 	host->irq = platform_get_irq(pdev, 0);
102 	if (host->irq < 0)
103 		return host->irq;
104 
105 	host->drv_data = drv_data;
106 	host->dev = &pdev->dev;
107 	host->irq_flags = 0;
108 	host->pdata = pdev->dev.platform_data;
109 
110 	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
111 	host->regs = devm_ioremap_resource(&pdev->dev, regs);
112 	if (IS_ERR(host->regs))
113 		return PTR_ERR(host->regs);
114 
115 	platform_set_drvdata(pdev, host);
116 	return dw_mci_probe(host);
117 }
118 EXPORT_SYMBOL_GPL(dw_mci_pltfm_register);
119 
120 #ifdef CONFIG_PM_SLEEP
121 /*
122  * TODO: we should probably disable the clock to the card in the suspend path.
123  */
124 static int dw_mci_pltfm_suspend(struct device *dev)
125 {
126 	struct dw_mci *host = dev_get_drvdata(dev);
127 
128 	return dw_mci_suspend(host);
129 }
130 
131 static int dw_mci_pltfm_resume(struct device *dev)
132 {
133 	struct dw_mci *host = dev_get_drvdata(dev);
134 
135 	return dw_mci_resume(host);
136 }
137 #endif /* CONFIG_PM_SLEEP */
138 
139 SIMPLE_DEV_PM_OPS(dw_mci_pltfm_pmops, dw_mci_pltfm_suspend, dw_mci_pltfm_resume);
140 EXPORT_SYMBOL_GPL(dw_mci_pltfm_pmops);
141 
142 static const struct of_device_id dw_mci_pltfm_match[] = {
143 	{ .compatible = "snps,dw-mshc", },
144 	{ .compatible = "rockchip,rk2928-dw-mshc",
145 		.data = &rk2928_drv_data },
146 	{ .compatible = "rockchip,rk3288-dw-mshc",
147 		.data = &rk3288_drv_data },
148 	{ .compatible = "altr,socfpga-dw-mshc",
149 		.data = &socfpga_drv_data },
150 	{},
151 };
152 MODULE_DEVICE_TABLE(of, dw_mci_pltfm_match);
153 
154 static int dw_mci_pltfm_probe(struct platform_device *pdev)
155 {
156 	const struct dw_mci_drv_data *drv_data = NULL;
157 	const struct of_device_id *match;
158 
159 	if (pdev->dev.of_node) {
160 		match = of_match_node(dw_mci_pltfm_match, pdev->dev.of_node);
161 		drv_data = match->data;
162 	}
163 
164 	return dw_mci_pltfm_register(pdev, drv_data);
165 }
166 
167 int dw_mci_pltfm_remove(struct platform_device *pdev)
168 {
169 	struct dw_mci *host = platform_get_drvdata(pdev);
170 
171 	dw_mci_remove(host);
172 	return 0;
173 }
174 EXPORT_SYMBOL_GPL(dw_mci_pltfm_remove);
175 
176 static struct platform_driver dw_mci_pltfm_driver = {
177 	.probe		= dw_mci_pltfm_probe,
178 	.remove		= dw_mci_pltfm_remove,
179 	.driver		= {
180 		.name		= "dw_mmc",
181 		.of_match_table	= dw_mci_pltfm_match,
182 		.pm		= &dw_mci_pltfm_pmops,
183 	},
184 };
185 
186 module_platform_driver(dw_mci_pltfm_driver);
187 
188 MODULE_DESCRIPTION("DW Multimedia Card Interface driver");
189 MODULE_AUTHOR("NXP Semiconductor VietNam");
190 MODULE_AUTHOR("Imagination Technologies Ltd");
191 MODULE_LICENSE("GPL v2");
192