xref: /openbmc/linux/drivers/mmc/host/dw_mmc-exynos.c (revision 8e2b36ea6e3abc613cbbdb41692fbd2f9ee18996)
1c3665006SThomas Abraham /*
2c3665006SThomas Abraham  * Exynos Specific Extensions for Synopsys DW Multimedia Card Interface driver
3c3665006SThomas Abraham  *
4c3665006SThomas Abraham  * Copyright (C) 2012, Samsung Electronics Co., Ltd.
5c3665006SThomas Abraham  *
6c3665006SThomas Abraham  * This program is free software; you can redistribute it and/or modify
7c3665006SThomas Abraham  * it under the terms of the GNU General Public License as published by
8c3665006SThomas Abraham  * the Free Software Foundation; either version 2 of the License, or
9c3665006SThomas Abraham  * (at your option) any later version.
10c3665006SThomas Abraham  */
11c3665006SThomas Abraham 
12c3665006SThomas Abraham #include <linux/module.h>
13c3665006SThomas Abraham #include <linux/platform_device.h>
14c3665006SThomas Abraham #include <linux/clk.h>
15c3665006SThomas Abraham #include <linux/mmc/host.h>
16c3665006SThomas Abraham #include <linux/mmc/dw_mmc.h>
17c3665006SThomas Abraham #include <linux/of.h>
18c3665006SThomas Abraham #include <linux/of_gpio.h>
19c3665006SThomas Abraham 
20c3665006SThomas Abraham #include "dw_mmc.h"
21c3665006SThomas Abraham #include "dw_mmc-pltfm.h"
22c3665006SThomas Abraham 
23c3665006SThomas Abraham #define NUM_PINS(x)			(x + 2)
24c3665006SThomas Abraham 
25c3665006SThomas Abraham #define SDMMC_CLKSEL			0x09C
26c3665006SThomas Abraham #define SDMMC_CLKSEL_CCLK_SAMPLE(x)	(((x) & 7) << 0)
27c3665006SThomas Abraham #define SDMMC_CLKSEL_CCLK_DRIVE(x)	(((x) & 7) << 16)
28c3665006SThomas Abraham #define SDMMC_CLKSEL_CCLK_DIVIDER(x)	(((x) & 7) << 24)
29c3665006SThomas Abraham #define SDMMC_CLKSEL_GET_DRV_WD3(x)	(((x) >> 16) & 0x7)
30c3665006SThomas Abraham #define SDMMC_CLKSEL_TIMING(x, y, z)	(SDMMC_CLKSEL_CCLK_SAMPLE(x) |	\
31c3665006SThomas Abraham 					SDMMC_CLKSEL_CCLK_DRIVE(y) |	\
32c3665006SThomas Abraham 					SDMMC_CLKSEL_CCLK_DIVIDER(z))
33c3665006SThomas Abraham 
34c3665006SThomas Abraham #define SDMMC_CMD_USE_HOLD_REG		BIT(29)
35c3665006SThomas Abraham 
36c3665006SThomas Abraham #define EXYNOS4210_FIXED_CIU_CLK_DIV	2
37c3665006SThomas Abraham #define EXYNOS4412_FIXED_CIU_CLK_DIV	4
38c3665006SThomas Abraham 
39c3665006SThomas Abraham /* Variations in Exynos specific dw-mshc controller */
40c3665006SThomas Abraham enum dw_mci_exynos_type {
41c3665006SThomas Abraham 	DW_MCI_TYPE_EXYNOS4210,
42c3665006SThomas Abraham 	DW_MCI_TYPE_EXYNOS4412,
43c3665006SThomas Abraham 	DW_MCI_TYPE_EXYNOS5250,
44c3665006SThomas Abraham };
45c3665006SThomas Abraham 
46c3665006SThomas Abraham /* Exynos implementation specific driver private data */
47c3665006SThomas Abraham struct dw_mci_exynos_priv_data {
48c3665006SThomas Abraham 	enum dw_mci_exynos_type		ctrl_type;
49c3665006SThomas Abraham 	u8				ciu_div;
50c3665006SThomas Abraham 	u32				sdr_timing;
51c3665006SThomas Abraham 	u32				ddr_timing;
52c3665006SThomas Abraham };
53c3665006SThomas Abraham 
54c3665006SThomas Abraham static struct dw_mci_exynos_compatible {
55c3665006SThomas Abraham 	char				*compatible;
56c3665006SThomas Abraham 	enum dw_mci_exynos_type		ctrl_type;
57c3665006SThomas Abraham } exynos_compat[] = {
58c3665006SThomas Abraham 	{
59c3665006SThomas Abraham 		.compatible	= "samsung,exynos4210-dw-mshc",
60c3665006SThomas Abraham 		.ctrl_type	= DW_MCI_TYPE_EXYNOS4210,
61c3665006SThomas Abraham 	}, {
62c3665006SThomas Abraham 		.compatible	= "samsung,exynos4412-dw-mshc",
63c3665006SThomas Abraham 		.ctrl_type	= DW_MCI_TYPE_EXYNOS4412,
64c3665006SThomas Abraham 	}, {
65c3665006SThomas Abraham 		.compatible	= "samsung,exynos5250-dw-mshc",
66c3665006SThomas Abraham 		.ctrl_type	= DW_MCI_TYPE_EXYNOS5250,
67c3665006SThomas Abraham 	},
68c3665006SThomas Abraham };
69c3665006SThomas Abraham 
70c3665006SThomas Abraham static int dw_mci_exynos_priv_init(struct dw_mci *host)
71c3665006SThomas Abraham {
72c3665006SThomas Abraham 	struct dw_mci_exynos_priv_data *priv;
73c3665006SThomas Abraham 	int idx;
74c3665006SThomas Abraham 
75c3665006SThomas Abraham 	priv = devm_kzalloc(host->dev, sizeof(*priv), GFP_KERNEL);
76c3665006SThomas Abraham 	if (!priv) {
77c3665006SThomas Abraham 		dev_err(host->dev, "mem alloc failed for private data\n");
78c3665006SThomas Abraham 		return -ENOMEM;
79c3665006SThomas Abraham 	}
80c3665006SThomas Abraham 
81c3665006SThomas Abraham 	for (idx = 0; idx < ARRAY_SIZE(exynos_compat); idx++) {
82c3665006SThomas Abraham 		if (of_device_is_compatible(host->dev->of_node,
83c3665006SThomas Abraham 					exynos_compat[idx].compatible))
84c3665006SThomas Abraham 			priv->ctrl_type = exynos_compat[idx].ctrl_type;
85c3665006SThomas Abraham 	}
86c3665006SThomas Abraham 
87c3665006SThomas Abraham 	host->priv = priv;
88c3665006SThomas Abraham 	return 0;
89c3665006SThomas Abraham }
90c3665006SThomas Abraham 
91c3665006SThomas Abraham static int dw_mci_exynos_setup_clock(struct dw_mci *host)
92c3665006SThomas Abraham {
93c3665006SThomas Abraham 	struct dw_mci_exynos_priv_data *priv = host->priv;
94c3665006SThomas Abraham 
95c3665006SThomas Abraham 	if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS5250)
96c3665006SThomas Abraham 		host->bus_hz /= (priv->ciu_div + 1);
97c3665006SThomas Abraham 	else if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS4412)
98c3665006SThomas Abraham 		host->bus_hz /= EXYNOS4412_FIXED_CIU_CLK_DIV;
99c3665006SThomas Abraham 	else if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS4210)
100c3665006SThomas Abraham 		host->bus_hz /= EXYNOS4210_FIXED_CIU_CLK_DIV;
101c3665006SThomas Abraham 
102c3665006SThomas Abraham 	return 0;
103c3665006SThomas Abraham }
104c3665006SThomas Abraham 
105c3665006SThomas Abraham static void dw_mci_exynos_prepare_command(struct dw_mci *host, u32 *cmdr)
106c3665006SThomas Abraham {
107c3665006SThomas Abraham 	/*
108c3665006SThomas Abraham 	 * Exynos4412 and Exynos5250 extends the use of CMD register with the
109c3665006SThomas Abraham 	 * use of bit 29 (which is reserved on standard MSHC controllers) for
110c3665006SThomas Abraham 	 * optionally bypassing the HOLD register for command and data. The
111c3665006SThomas Abraham 	 * HOLD register should be bypassed in case there is no phase shift
112c3665006SThomas Abraham 	 * applied on CMD/DATA that is sent to the card.
113c3665006SThomas Abraham 	 */
114c3665006SThomas Abraham 	if (SDMMC_CLKSEL_GET_DRV_WD3(mci_readl(host, CLKSEL)))
115c3665006SThomas Abraham 		*cmdr |= SDMMC_CMD_USE_HOLD_REG;
116c3665006SThomas Abraham }
117c3665006SThomas Abraham 
118c3665006SThomas Abraham static void dw_mci_exynos_set_ios(struct dw_mci *host, struct mmc_ios *ios)
119c3665006SThomas Abraham {
120c3665006SThomas Abraham 	struct dw_mci_exynos_priv_data *priv = host->priv;
121c3665006SThomas Abraham 
122c3665006SThomas Abraham 	if (ios->timing == MMC_TIMING_UHS_DDR50)
123c3665006SThomas Abraham 		mci_writel(host, CLKSEL, priv->ddr_timing);
124c3665006SThomas Abraham 	else
125c3665006SThomas Abraham 		mci_writel(host, CLKSEL, priv->sdr_timing);
126c3665006SThomas Abraham }
127c3665006SThomas Abraham 
128c3665006SThomas Abraham static int dw_mci_exynos_parse_dt(struct dw_mci *host)
129c3665006SThomas Abraham {
130c3665006SThomas Abraham 	struct dw_mci_exynos_priv_data *priv = host->priv;
131c3665006SThomas Abraham 	struct device_node *np = host->dev->of_node;
132c3665006SThomas Abraham 	u32 timing[2];
133c3665006SThomas Abraham 	u32 div = 0;
134c3665006SThomas Abraham 	int ret;
135c3665006SThomas Abraham 
136c3665006SThomas Abraham 	of_property_read_u32(np, "samsung,dw-mshc-ciu-div", &div);
137c3665006SThomas Abraham 	priv->ciu_div = div;
138c3665006SThomas Abraham 
139c3665006SThomas Abraham 	ret = of_property_read_u32_array(np,
140c3665006SThomas Abraham 			"samsung,dw-mshc-sdr-timing", timing, 2);
141c3665006SThomas Abraham 	if (ret)
142c3665006SThomas Abraham 		return ret;
143c3665006SThomas Abraham 
144c3665006SThomas Abraham 	priv->sdr_timing = SDMMC_CLKSEL_TIMING(timing[0], timing[1], div);
145c3665006SThomas Abraham 
146c3665006SThomas Abraham 	ret = of_property_read_u32_array(np,
147c3665006SThomas Abraham 			"samsung,dw-mshc-ddr-timing", timing, 2);
148c3665006SThomas Abraham 	if (ret)
149c3665006SThomas Abraham 		return ret;
150c3665006SThomas Abraham 
151c3665006SThomas Abraham 	priv->ddr_timing = SDMMC_CLKSEL_TIMING(timing[0], timing[1], div);
152c3665006SThomas Abraham 	return 0;
153c3665006SThomas Abraham }
154c3665006SThomas Abraham 
155c3665006SThomas Abraham static int dw_mci_exynos_setup_bus(struct dw_mci *host,
156c3665006SThomas Abraham 				struct device_node *slot_np, u8 bus_width)
157c3665006SThomas Abraham {
158c3665006SThomas Abraham 	int idx, gpio, ret;
159c3665006SThomas Abraham 
160c3665006SThomas Abraham 	if (!slot_np)
161c3665006SThomas Abraham 		return -EINVAL;
162c3665006SThomas Abraham 
163c3665006SThomas Abraham 	/* cmd + clock + bus-width pins */
164c3665006SThomas Abraham 	for (idx = 0; idx < NUM_PINS(bus_width); idx++) {
165c3665006SThomas Abraham 		gpio = of_get_gpio(slot_np, idx);
166c3665006SThomas Abraham 		if (!gpio_is_valid(gpio)) {
167c3665006SThomas Abraham 			dev_err(host->dev, "invalid gpio: %d\n", gpio);
168c3665006SThomas Abraham 			return -EINVAL;
169c3665006SThomas Abraham 		}
170c3665006SThomas Abraham 
171c3665006SThomas Abraham 		ret = devm_gpio_request(host->dev, gpio, "dw-mci-bus");
172c3665006SThomas Abraham 		if (ret) {
173c3665006SThomas Abraham 			dev_err(host->dev, "gpio [%d] request failed\n", gpio);
174c3665006SThomas Abraham 			return -EBUSY;
175c3665006SThomas Abraham 		}
176c3665006SThomas Abraham 	}
177c3665006SThomas Abraham 
178c3665006SThomas Abraham 	gpio = of_get_named_gpio(slot_np, "wp-gpios", 0);
179c3665006SThomas Abraham 	if (gpio_is_valid(gpio)) {
180c3665006SThomas Abraham 		if (devm_gpio_request(host->dev, gpio, "dw-mci-wp"))
181c3665006SThomas Abraham 			dev_info(host->dev, "gpio [%d] request failed\n",
182c3665006SThomas Abraham 						gpio);
183c3665006SThomas Abraham 	} else {
184c3665006SThomas Abraham 		dev_info(host->dev, "wp gpio not available");
185c3665006SThomas Abraham 		host->pdata->quirks |= DW_MCI_QUIRK_NO_WRITE_PROTECT;
186c3665006SThomas Abraham 	}
187c3665006SThomas Abraham 
188c3665006SThomas Abraham 	if (host->pdata->quirks & DW_MCI_QUIRK_BROKEN_CARD_DETECTION)
189c3665006SThomas Abraham 		return 0;
190c3665006SThomas Abraham 
191c3665006SThomas Abraham 	gpio = of_get_named_gpio(slot_np, "samsung,cd-pinmux-gpio", 0);
192c3665006SThomas Abraham 	if (gpio_is_valid(gpio)) {
193c3665006SThomas Abraham 		if (devm_gpio_request(host->dev, gpio, "dw-mci-cd"))
194c3665006SThomas Abraham 			dev_err(host->dev, "gpio [%d] request failed\n", gpio);
195c3665006SThomas Abraham 	} else {
196c3665006SThomas Abraham 		dev_info(host->dev, "cd gpio not available");
197c3665006SThomas Abraham 	}
198c3665006SThomas Abraham 
199c3665006SThomas Abraham 	return 0;
200c3665006SThomas Abraham }
201c3665006SThomas Abraham 
202c3665006SThomas Abraham /* Exynos5250 controller specific capabilities */
203c3665006SThomas Abraham static unsigned long exynos5250_dwmmc_caps[4] = {
204c3665006SThomas Abraham 	MMC_CAP_UHS_DDR50 | MMC_CAP_1_8V_DDR |
205c3665006SThomas Abraham 		MMC_CAP_8_BIT_DATA | MMC_CAP_CMD23,
206c3665006SThomas Abraham 	MMC_CAP_CMD23,
207c3665006SThomas Abraham 	MMC_CAP_CMD23,
208c3665006SThomas Abraham 	MMC_CAP_CMD23,
209c3665006SThomas Abraham };
210c3665006SThomas Abraham 
211*8e2b36eaSArnd Bergmann static const struct dw_mci_drv_data exynos5250_drv_data = {
212c3665006SThomas Abraham 	.caps			= exynos5250_dwmmc_caps,
213c3665006SThomas Abraham 	.init			= dw_mci_exynos_priv_init,
214c3665006SThomas Abraham 	.setup_clock		= dw_mci_exynos_setup_clock,
215c3665006SThomas Abraham 	.prepare_command	= dw_mci_exynos_prepare_command,
216c3665006SThomas Abraham 	.set_ios		= dw_mci_exynos_set_ios,
217c3665006SThomas Abraham 	.parse_dt		= dw_mci_exynos_parse_dt,
218c3665006SThomas Abraham 	.setup_bus		= dw_mci_exynos_setup_bus,
219c3665006SThomas Abraham };
220c3665006SThomas Abraham 
221c3665006SThomas Abraham static const struct of_device_id dw_mci_exynos_match[] = {
222c3665006SThomas Abraham 	{ .compatible = "samsung,exynos5250-dw-mshc",
223*8e2b36eaSArnd Bergmann 			.data = &exynos5250_drv_data, },
224c3665006SThomas Abraham 	{},
225c3665006SThomas Abraham };
226517cb9f1SArnd Bergmann MODULE_DEVICE_TABLE(of, dw_mci_exynos_match);
227c3665006SThomas Abraham 
228c3665006SThomas Abraham int dw_mci_exynos_probe(struct platform_device *pdev)
229c3665006SThomas Abraham {
230*8e2b36eaSArnd Bergmann 	const struct dw_mci_drv_data *drv_data;
231c3665006SThomas Abraham 	const struct of_device_id *match;
232c3665006SThomas Abraham 
233c3665006SThomas Abraham 	match = of_match_node(dw_mci_exynos_match, pdev->dev.of_node);
234c3665006SThomas Abraham 	drv_data = match->data;
235c3665006SThomas Abraham 	return dw_mci_pltfm_register(pdev, drv_data);
236c3665006SThomas Abraham }
237c3665006SThomas Abraham 
238c3665006SThomas Abraham static struct platform_driver dw_mci_exynos_pltfm_driver = {
239c3665006SThomas Abraham 	.probe		= dw_mci_exynos_probe,
240c3665006SThomas Abraham 	.remove		= __exit_p(dw_mci_pltfm_remove),
241c3665006SThomas Abraham 	.driver		= {
242c3665006SThomas Abraham 		.name		= "dwmmc_exynos",
243c3665006SThomas Abraham 		.of_match_table	= of_match_ptr(dw_mci_exynos_match),
244c3665006SThomas Abraham 		.pm		= &dw_mci_pltfm_pmops,
245c3665006SThomas Abraham 	},
246c3665006SThomas Abraham };
247c3665006SThomas Abraham 
248c3665006SThomas Abraham module_platform_driver(dw_mci_exynos_pltfm_driver);
249c3665006SThomas Abraham 
250c3665006SThomas Abraham MODULE_DESCRIPTION("Samsung Specific DW-MSHC Driver Extension");
251c3665006SThomas Abraham MODULE_AUTHOR("Thomas Abraham <thomas.ab@samsung.com");
252c3665006SThomas Abraham MODULE_LICENSE("GPL v2");
253c3665006SThomas Abraham MODULE_ALIAS("platform:dwmmc-exynos");
254