blob: 2201f76b539dd6194a558798e4bb1f09a3a9a2fd [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
Minda Chenfc0b6382014-12-04 20:09:20 +080046static int sdhci_sirf_execute_tuning(struct sdhci_host *host, u32 opcode)
47{
48 int tuning_seq_cnt = 3;
Weijun Yangd1ba44a2015-04-27 08:15:13 +000049 int phase;
Minda Chenfc0b6382014-12-04 20:09:20 +080050 u8 tuned_phase_cnt = 0;
weijun yangb36ac1b2015-02-15 23:43:51 +080051 int rc = 0, longest_range = 0;
Minda Chenfc0b6382014-12-04 20:09:20 +080052 int start = -1, end = 0, tuning_value = -1, range = 0;
53 u16 clock_setting;
54 struct mmc_host *mmc = host->mmc;
55
56 clock_setting = sdhci_readw(host, SDHCI_CLK_DELAY_SETTING);
57 clock_setting &= ~0x3fff;
58
59retry:
60 phase = 0;
Weijun Yangd1ba44a2015-04-27 08:15:13 +000061 tuned_phase_cnt = 0;
Minda Chenfc0b6382014-12-04 20:09:20 +080062 do {
63 sdhci_writel(host,
weijun yangb36ac1b2015-02-15 23:43:51 +080064 clock_setting | phase,
Minda Chenfc0b6382014-12-04 20:09:20 +080065 SDHCI_CLK_DELAY_SETTING);
66
67 if (!mmc_send_tuning(mmc)) {
68 /* Tuning is successful at this tuning point */
Weijun Yangd1ba44a2015-04-27 08:15:13 +000069 tuned_phase_cnt++;
Minda Chenfc0b6382014-12-04 20:09:20 +080070 dev_dbg(mmc_dev(mmc), "%s: Found good phase = %d\n",
71 mmc_hostname(mmc), phase);
72 if (start == -1)
73 start = phase;
74 end = phase;
75 range++;
76 if (phase == (SIRF_TUNING_COUNT - 1)
77 && range > longest_range)
78 tuning_value = (start + end) / 2;
79 } else {
80 dev_dbg(mmc_dev(mmc), "%s: Found bad phase = %d\n",
81 mmc_hostname(mmc), phase);
82 if (range > longest_range) {
83 tuning_value = (start + end) / 2;
84 longest_range = range;
85 }
86 start = -1;
87 end = range = 0;
88 }
Weijun Yangd1ba44a2015-04-27 08:15:13 +000089 } while (++phase < SIRF_TUNING_COUNT);
Minda Chenfc0b6382014-12-04 20:09:20 +080090
91 if (tuned_phase_cnt && tuning_value > 0) {
92 /*
93 * Finally set the selected phase in delay
94 * line hw block.
95 */
96 phase = tuning_value;
97 sdhci_writel(host,
weijun yangb36ac1b2015-02-15 23:43:51 +080098 clock_setting | phase,
Minda Chenfc0b6382014-12-04 20:09:20 +080099 SDHCI_CLK_DELAY_SETTING);
100
101 dev_dbg(mmc_dev(mmc), "%s: Setting the tuning phase to %d\n",
102 mmc_hostname(mmc), phase);
103 } else {
104 if (--tuning_seq_cnt)
105 goto retry;
106 /* Tuning failed */
107 dev_dbg(mmc_dev(mmc), "%s: No tuning point found\n",
108 mmc_hostname(mmc));
109 rc = -EIO;
110 }
111
112 return rc;
113}
114
Barry Songb3b665b2013-03-21 16:27:19 +0800115static struct sdhci_ops sdhci_sirf_ops = {
Minda Chenfc0b6382014-12-04 20:09:20 +0800116 .platform_execute_tuning = sdhci_sirf_execute_tuning,
Russell King17710592014-04-25 12:58:55 +0100117 .set_clock = sdhci_set_clock,
Kevin Haoe46af292015-02-27 15:47:28 +0800118 .get_max_clock = sdhci_pltfm_clk_get_max_clock,
Minda Chen1ba4c322014-08-26 10:50:42 +0800119 .set_bus_width = sdhci_sirf_set_bus_width,
Russell King03231f92014-04-25 12:57:12 +0100120 .reset = sdhci_reset,
Russell King96d7b782014-04-25 12:59:26 +0100121 .set_uhs_signaling = sdhci_set_uhs_signaling,
Barry Songb3b665b2013-03-21 16:27:19 +0800122};
123
124static struct sdhci_pltfm_data sdhci_sirf_pdata = {
125 .ops = &sdhci_sirf_ops,
126 .quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
127 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
128 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
129 SDHCI_QUIRK_INVERTED_WRITE_PROTECT |
130 SDHCI_QUIRK_DELAY_AFTER_POWER,
131};
132
133static int sdhci_sirf_probe(struct platform_device *pdev)
134{
135 struct sdhci_host *host;
136 struct sdhci_pltfm_host *pltfm_host;
137 struct sdhci_sirf_priv *priv;
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -0400138 struct clk *clk;
139 int gpio_cd;
Barry Songb3b665b2013-03-21 16:27:19 +0800140 int ret;
141
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -0400142 clk = devm_clk_get(&pdev->dev, NULL);
143 if (IS_ERR(clk)) {
Barry Songb3b665b2013-03-21 16:27:19 +0800144 dev_err(&pdev->dev, "unable to get clock");
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -0400145 return PTR_ERR(clk);
Barry Songb3b665b2013-03-21 16:27:19 +0800146 }
147
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -0400148 if (pdev->dev.of_node)
149 gpio_cd = of_get_named_gpio(pdev->dev.of_node, "cd-gpios", 0);
150 else
151 gpio_cd = -EINVAL;
Barry Songb3b665b2013-03-21 16:27:19 +0800152
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -0400153 host = sdhci_pltfm_init(pdev, &sdhci_sirf_pdata, sizeof(struct sdhci_sirf_priv));
154 if (IS_ERR(host))
155 return PTR_ERR(host);
Barry Songb3b665b2013-03-21 16:27:19 +0800156
157 pltfm_host = sdhci_priv(host);
Kevin Haoe46af292015-02-27 15:47:28 +0800158 pltfm_host->clk = clk;
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -0400159 priv = sdhci_pltfm_priv(pltfm_host);
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -0400160 priv->gpio_cd = gpio_cd;
Barry Songb3b665b2013-03-21 16:27:19 +0800161
162 sdhci_get_of_property(pdev);
163
Kevin Haoe46af292015-02-27 15:47:28 +0800164 ret = clk_prepare_enable(pltfm_host->clk);
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -0400165 if (ret)
166 goto err_clk_prepare;
Barry Songb3b665b2013-03-21 16:27:19 +0800167
168 ret = sdhci_add_host(host);
169 if (ret)
170 goto err_sdhci_add;
171
172 /*
173 * We must request the IRQ after sdhci_add_host(), as the tasklet only
174 * gets setup in sdhci_add_host() and we oops.
175 */
176 if (gpio_is_valid(priv->gpio_cd)) {
Laurent Pinchart214fc302013-08-08 12:38:31 +0200177 ret = mmc_gpio_request_cd(host->mmc, priv->gpio_cd, 0);
Barry Songb3b665b2013-03-21 16:27:19 +0800178 if (ret) {
179 dev_err(&pdev->dev, "card detect irq request failed: %d\n",
180 ret);
181 goto err_request_cd;
182 }
Stephen Warrend4d11442014-09-22 09:57:42 -0600183 mmc_gpiod_request_cd_irq(host->mmc);
Barry Songb3b665b2013-03-21 16:27:19 +0800184 }
185
186 return 0;
187
188err_request_cd:
189 sdhci_remove_host(host, 0);
190err_sdhci_add:
Kevin Haoe46af292015-02-27 15:47:28 +0800191 clk_disable_unprepare(pltfm_host->clk);
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -0400192err_clk_prepare:
Barry Songb3b665b2013-03-21 16:27:19 +0800193 sdhci_pltfm_free(pdev);
Barry Songb3b665b2013-03-21 16:27:19 +0800194 return ret;
195}
196
Barry Songb3b665b2013-03-21 16:27:19 +0800197#ifdef CONFIG_PM_SLEEP
198static int sdhci_sirf_suspend(struct device *dev)
199{
200 struct sdhci_host *host = dev_get_drvdata(dev);
201 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Barry Songb3b665b2013-03-21 16:27:19 +0800202 int ret;
203
204 ret = sdhci_suspend_host(host);
205 if (ret)
206 return ret;
207
Kevin Haoe46af292015-02-27 15:47:28 +0800208 clk_disable(pltfm_host->clk);
Barry Songb3b665b2013-03-21 16:27:19 +0800209
210 return 0;
211}
212
213static int sdhci_sirf_resume(struct device *dev)
214{
215 struct sdhci_host *host = dev_get_drvdata(dev);
216 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Barry Songb3b665b2013-03-21 16:27:19 +0800217 int ret;
218
Kevin Haoe46af292015-02-27 15:47:28 +0800219 ret = clk_enable(pltfm_host->clk);
Barry Songb3b665b2013-03-21 16:27:19 +0800220 if (ret) {
221 dev_dbg(dev, "Resume: Error enabling clock\n");
222 return ret;
223 }
224
225 return sdhci_resume_host(host);
226}
227
228static SIMPLE_DEV_PM_OPS(sdhci_sirf_pm_ops, sdhci_sirf_suspend, sdhci_sirf_resume);
229#endif
230
231static const struct of_device_id sdhci_sirf_of_match[] = {
232 { .compatible = "sirf,prima2-sdhc" },
233 { }
234};
235MODULE_DEVICE_TABLE(of, sdhci_sirf_of_match);
236
237static struct platform_driver sdhci_sirf_driver = {
238 .driver = {
239 .name = "sdhci-sirf",
Barry Songb3b665b2013-03-21 16:27:19 +0800240 .of_match_table = sdhci_sirf_of_match,
241#ifdef CONFIG_PM_SLEEP
242 .pm = &sdhci_sirf_pm_ops,
243#endif
244 },
245 .probe = sdhci_sirf_probe,
Kevin Haocaebcae2015-02-27 15:47:31 +0800246 .remove = sdhci_pltfm_unregister,
Barry Songb3b665b2013-03-21 16:27:19 +0800247};
248
249module_platform_driver(sdhci_sirf_driver);
250
251MODULE_DESCRIPTION("SDHCI driver for SiRFprimaII/SiRFmarco");
252MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
253MODULE_LICENSE("GPL v2");