blob: 884294576356d4c87e3869162186aadbc24b4f6a [file] [log] [blame]
Barry Songb3b665b2013-03-21 16:27:19 +08001/*
2 * SDHCI support for SiRF primaII and marco SoCs
3 *
4 * Copyright (c) 2011 Cambridge Silicon Radio Limited, a CSR plc group company.
5 *
6 * Licensed under GPLv2 or later.
7 */
8
9#include <linux/delay.h>
10#include <linux/device.h>
11#include <linux/mmc/host.h>
12#include <linux/module.h>
13#include <linux/of.h>
14#include <linux/of_gpio.h>
15#include <linux/mmc/slot-gpio.h>
Barry Songb3b665b2013-03-21 16:27:19 +080016#include "sdhci-pltfm.h"
17
Minda Chenfc0b6382014-12-04 20:09:20 +080018#define SDHCI_CLK_DELAY_SETTING 0x4C
Minda Chen1ba4c322014-08-26 10:50:42 +080019#define SDHCI_SIRF_8BITBUS BIT(3)
Weijun Yangd1ba44a2015-04-27 08:15:13 +000020#define SIRF_TUNING_COUNT 16384
Minda Chen1ba4c322014-08-26 10:50:42 +080021
Barry Songb3b665b2013-03-21 16:27:19 +080022struct sdhci_sirf_priv {
Barry Songb3b665b2013-03-21 16:27:19 +080023 int gpio_cd;
24};
25
Minda Chen1ba4c322014-08-26 10:50:42 +080026static void sdhci_sirf_set_bus_width(struct sdhci_host *host, int width)
27{
28 u8 ctrl;
29
30 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
31 ctrl &= ~(SDHCI_CTRL_4BITBUS | SDHCI_SIRF_8BITBUS);
32
33 /*
34 * CSR atlas7 and prima2 SD host version is not 3.0
35 * 8bit-width enable bit of CSR SD hosts is 3,
36 * while stardard hosts use bit 5
37 */
38 if (width == MMC_BUS_WIDTH_8)
39 ctrl |= SDHCI_SIRF_8BITBUS;
40 else if (width == MMC_BUS_WIDTH_4)
41 ctrl |= SDHCI_CTRL_4BITBUS;
42
43 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
44}
45
Weijun Yanga1b0b972015-04-27 08:15:14 +000046static u32 sdhci_sirf_readl_le(struct sdhci_host *host, int reg)
47{
48 u32 val = readl(host->ioaddr + reg);
49
50 if (unlikely((reg == SDHCI_CAPABILITIES_1) &&
51 (host->mmc->caps & MMC_CAP_UHS_SDR50))) {
52 /* fake CAP_1 register */
53 val = SDHCI_SUPPORT_SDR50 | SDHCI_USE_SDR50_TUNING;
54 }
55
56 if (unlikely(reg == SDHCI_SLOT_INT_STATUS)) {
57 u32 prss = val;
58 /* fake chips as V3.0 host conreoller */
59 prss &= ~(0xFF << 16);
60 val = prss | (SDHCI_SPEC_300 << 16);
61 }
62 return val;
63}
64
65static u16 sdhci_sirf_readw_le(struct sdhci_host *host, int reg)
66{
67 u16 ret = 0;
68
69 ret = readw(host->ioaddr + reg);
70
71 if (unlikely(reg == SDHCI_HOST_VERSION)) {
72 ret = readw(host->ioaddr + SDHCI_HOST_VERSION);
73 ret |= SDHCI_SPEC_300;
74 }
75
76 return ret;
77}
78
Minda Chenfc0b6382014-12-04 20:09:20 +080079static int sdhci_sirf_execute_tuning(struct sdhci_host *host, u32 opcode)
80{
81 int tuning_seq_cnt = 3;
Weijun Yangd1ba44a2015-04-27 08:15:13 +000082 int phase;
Minda Chenfc0b6382014-12-04 20:09:20 +080083 u8 tuned_phase_cnt = 0;
weijun yangb36ac1b2015-02-15 23:43:51 +080084 int rc = 0, longest_range = 0;
Minda Chenfc0b6382014-12-04 20:09:20 +080085 int start = -1, end = 0, tuning_value = -1, range = 0;
86 u16 clock_setting;
87 struct mmc_host *mmc = host->mmc;
88
89 clock_setting = sdhci_readw(host, SDHCI_CLK_DELAY_SETTING);
90 clock_setting &= ~0x3fff;
91
92retry:
93 phase = 0;
Weijun Yangd1ba44a2015-04-27 08:15:13 +000094 tuned_phase_cnt = 0;
Minda Chenfc0b6382014-12-04 20:09:20 +080095 do {
96 sdhci_writel(host,
weijun yangb36ac1b2015-02-15 23:43:51 +080097 clock_setting | phase,
Minda Chenfc0b6382014-12-04 20:09:20 +080098 SDHCI_CLK_DELAY_SETTING);
99
100 if (!mmc_send_tuning(mmc)) {
101 /* Tuning is successful at this tuning point */
Weijun Yangd1ba44a2015-04-27 08:15:13 +0000102 tuned_phase_cnt++;
Minda Chenfc0b6382014-12-04 20:09:20 +0800103 dev_dbg(mmc_dev(mmc), "%s: Found good phase = %d\n",
104 mmc_hostname(mmc), phase);
105 if (start == -1)
106 start = phase;
107 end = phase;
108 range++;
109 if (phase == (SIRF_TUNING_COUNT - 1)
110 && range > longest_range)
111 tuning_value = (start + end) / 2;
112 } else {
113 dev_dbg(mmc_dev(mmc), "%s: Found bad phase = %d\n",
114 mmc_hostname(mmc), phase);
115 if (range > longest_range) {
116 tuning_value = (start + end) / 2;
117 longest_range = range;
118 }
119 start = -1;
120 end = range = 0;
121 }
Weijun Yangd1ba44a2015-04-27 08:15:13 +0000122 } while (++phase < SIRF_TUNING_COUNT);
Minda Chenfc0b6382014-12-04 20:09:20 +0800123
124 if (tuned_phase_cnt && tuning_value > 0) {
125 /*
126 * Finally set the selected phase in delay
127 * line hw block.
128 */
129 phase = tuning_value;
130 sdhci_writel(host,
weijun yangb36ac1b2015-02-15 23:43:51 +0800131 clock_setting | phase,
Minda Chenfc0b6382014-12-04 20:09:20 +0800132 SDHCI_CLK_DELAY_SETTING);
133
134 dev_dbg(mmc_dev(mmc), "%s: Setting the tuning phase to %d\n",
135 mmc_hostname(mmc), phase);
136 } else {
137 if (--tuning_seq_cnt)
138 goto retry;
139 /* Tuning failed */
140 dev_dbg(mmc_dev(mmc), "%s: No tuning point found\n",
141 mmc_hostname(mmc));
142 rc = -EIO;
143 }
144
145 return rc;
146}
147
Barry Songb3b665b2013-03-21 16:27:19 +0800148static struct sdhci_ops sdhci_sirf_ops = {
Weijun Yanga1b0b972015-04-27 08:15:14 +0000149 .read_l = sdhci_sirf_readl_le,
150 .read_w = sdhci_sirf_readw_le,
Minda Chenfc0b6382014-12-04 20:09:20 +0800151 .platform_execute_tuning = sdhci_sirf_execute_tuning,
Russell King17710592014-04-25 12:58:55 +0100152 .set_clock = sdhci_set_clock,
Kevin Haoe46af292015-02-27 15:47:28 +0800153 .get_max_clock = sdhci_pltfm_clk_get_max_clock,
Minda Chen1ba4c322014-08-26 10:50:42 +0800154 .set_bus_width = sdhci_sirf_set_bus_width,
Russell King03231f92014-04-25 12:57:12 +0100155 .reset = sdhci_reset,
Russell King96d7b782014-04-25 12:59:26 +0100156 .set_uhs_signaling = sdhci_set_uhs_signaling,
Barry Songb3b665b2013-03-21 16:27:19 +0800157};
158
159static struct sdhci_pltfm_data sdhci_sirf_pdata = {
160 .ops = &sdhci_sirf_ops,
161 .quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
162 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
163 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
Barry Song1880d8f2015-08-12 06:59:33 +0000164 SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS,
165 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
Barry Songb3b665b2013-03-21 16:27:19 +0800166};
167
168static int sdhci_sirf_probe(struct platform_device *pdev)
169{
170 struct sdhci_host *host;
171 struct sdhci_pltfm_host *pltfm_host;
172 struct sdhci_sirf_priv *priv;
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -0400173 struct clk *clk;
174 int gpio_cd;
Barry Songb3b665b2013-03-21 16:27:19 +0800175 int ret;
176
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -0400177 clk = devm_clk_get(&pdev->dev, NULL);
178 if (IS_ERR(clk)) {
Barry Songb3b665b2013-03-21 16:27:19 +0800179 dev_err(&pdev->dev, "unable to get clock");
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -0400180 return PTR_ERR(clk);
Barry Songb3b665b2013-03-21 16:27:19 +0800181 }
182
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -0400183 if (pdev->dev.of_node)
184 gpio_cd = of_get_named_gpio(pdev->dev.of_node, "cd-gpios", 0);
185 else
186 gpio_cd = -EINVAL;
Barry Songb3b665b2013-03-21 16:27:19 +0800187
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -0400188 host = sdhci_pltfm_init(pdev, &sdhci_sirf_pdata, sizeof(struct sdhci_sirf_priv));
189 if (IS_ERR(host))
190 return PTR_ERR(host);
Barry Songb3b665b2013-03-21 16:27:19 +0800191
192 pltfm_host = sdhci_priv(host);
Kevin Haoe46af292015-02-27 15:47:28 +0800193 pltfm_host->clk = clk;
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -0400194 priv = sdhci_pltfm_priv(pltfm_host);
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -0400195 priv->gpio_cd = gpio_cd;
Barry Songb3b665b2013-03-21 16:27:19 +0800196
197 sdhci_get_of_property(pdev);
198
Kevin Haoe46af292015-02-27 15:47:28 +0800199 ret = clk_prepare_enable(pltfm_host->clk);
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -0400200 if (ret)
201 goto err_clk_prepare;
Barry Songb3b665b2013-03-21 16:27:19 +0800202
203 ret = sdhci_add_host(host);
204 if (ret)
205 goto err_sdhci_add;
206
207 /*
208 * We must request the IRQ after sdhci_add_host(), as the tasklet only
209 * gets setup in sdhci_add_host() and we oops.
210 */
211 if (gpio_is_valid(priv->gpio_cd)) {
Laurent Pinchart214fc302013-08-08 12:38:31 +0200212 ret = mmc_gpio_request_cd(host->mmc, priv->gpio_cd, 0);
Barry Songb3b665b2013-03-21 16:27:19 +0800213 if (ret) {
214 dev_err(&pdev->dev, "card detect irq request failed: %d\n",
215 ret);
216 goto err_request_cd;
217 }
Stephen Warrend4d11442014-09-22 09:57:42 -0600218 mmc_gpiod_request_cd_irq(host->mmc);
Barry Songb3b665b2013-03-21 16:27:19 +0800219 }
220
221 return 0;
222
223err_request_cd:
224 sdhci_remove_host(host, 0);
225err_sdhci_add:
Kevin Haoe46af292015-02-27 15:47:28 +0800226 clk_disable_unprepare(pltfm_host->clk);
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -0400227err_clk_prepare:
Barry Songb3b665b2013-03-21 16:27:19 +0800228 sdhci_pltfm_free(pdev);
Barry Songb3b665b2013-03-21 16:27:19 +0800229 return ret;
230}
231
Barry Songb3b665b2013-03-21 16:27:19 +0800232#ifdef CONFIG_PM_SLEEP
233static int sdhci_sirf_suspend(struct device *dev)
234{
235 struct sdhci_host *host = dev_get_drvdata(dev);
236 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Barry Songb3b665b2013-03-21 16:27:19 +0800237 int ret;
238
239 ret = sdhci_suspend_host(host);
240 if (ret)
241 return ret;
242
Kevin Haoe46af292015-02-27 15:47:28 +0800243 clk_disable(pltfm_host->clk);
Barry Songb3b665b2013-03-21 16:27:19 +0800244
245 return 0;
246}
247
248static int sdhci_sirf_resume(struct device *dev)
249{
250 struct sdhci_host *host = dev_get_drvdata(dev);
251 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Barry Songb3b665b2013-03-21 16:27:19 +0800252 int ret;
253
Kevin Haoe46af292015-02-27 15:47:28 +0800254 ret = clk_enable(pltfm_host->clk);
Barry Songb3b665b2013-03-21 16:27:19 +0800255 if (ret) {
256 dev_dbg(dev, "Resume: Error enabling clock\n");
257 return ret;
258 }
259
260 return sdhci_resume_host(host);
261}
262
263static SIMPLE_DEV_PM_OPS(sdhci_sirf_pm_ops, sdhci_sirf_suspend, sdhci_sirf_resume);
264#endif
265
266static const struct of_device_id sdhci_sirf_of_match[] = {
267 { .compatible = "sirf,prima2-sdhc" },
268 { }
269};
270MODULE_DEVICE_TABLE(of, sdhci_sirf_of_match);
271
272static struct platform_driver sdhci_sirf_driver = {
273 .driver = {
274 .name = "sdhci-sirf",
Barry Songb3b665b2013-03-21 16:27:19 +0800275 .of_match_table = sdhci_sirf_of_match,
276#ifdef CONFIG_PM_SLEEP
277 .pm = &sdhci_sirf_pm_ops,
278#endif
279 },
280 .probe = sdhci_sirf_probe,
Kevin Haocaebcae2015-02-27 15:47:31 +0800281 .remove = sdhci_pltfm_unregister,
Barry Songb3b665b2013-03-21 16:27:19 +0800282};
283
284module_platform_driver(sdhci_sirf_driver);
285
286MODULE_DESCRIPTION("SDHCI driver for SiRFprimaII/SiRFmarco");
287MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
288MODULE_LICENSE("GPL v2");