Blame view

drivers/mmc/host/sdhci-spear.c 4.91 KB
c63b3cba4   Viresh KUMAR   sdhci-spear: ST S...
1
2
3
4
5
6
  /*
   * drivers/mmc/host/sdhci-spear.c
   *
   * Support of SDHCI platform devices for spear soc family
   *
   * Copyright (C) 2010 ST Microelectronics
da89947b4   Viresh Kumar   Update Viresh Kum...
7
   * Viresh Kumar <vireshk@kernel.org>
c63b3cba4   Viresh KUMAR   sdhci-spear: ST S...
8
9
10
11
12
13
14
15
16
17
18
19
   *
   * Inspired by sdhci-pltfm.c
   *
   * This file is licensed under the terms of the GNU General Public
   * License version 2. This program is licensed "as is" without any
   * warranty of any kind, whether express or implied.
   */
  
  #include <linux/clk.h>
  #include <linux/delay.h>
  #include <linux/gpio.h>
  #include <linux/highmem.h>
88b476797   Paul Gortmaker   mmc: Add module.h...
20
  #include <linux/module.h>
c63b3cba4   Viresh KUMAR   sdhci-spear: ST S...
21
22
  #include <linux/interrupt.h>
  #include <linux/irq.h>
067bf748b   Viresh Kumar   mmc: sdhci-spear:...
23
24
  #include <linux/of.h>
  #include <linux/of_gpio.h>
c63b3cba4   Viresh KUMAR   sdhci-spear: ST S...
25
  #include <linux/platform_device.h>
b70a7fab2   Viresh Kumar   mmc: sdhci-spear:...
26
  #include <linux/pm.h>
c63b3cba4   Viresh KUMAR   sdhci-spear: ST S...
27
28
  #include <linux/slab.h>
  #include <linux/mmc/host.h>
b42b9b12e   Russell King   mmc: sdhci-spear:...
29
  #include <linux/mmc/slot-gpio.h>
c63b3cba4   Viresh KUMAR   sdhci-spear: ST S...
30
31
32
33
34
  #include <linux/io.h>
  #include "sdhci.h"
  
  struct spear_sdhci {
  	struct clk *clk;
03a6d2910   Ulf Hansson   mmc: sdhci-spear:...
35
  	int card_int_gpio;
c63b3cba4   Viresh KUMAR   sdhci-spear: ST S...
36
37
38
  };
  
  /* sdhci ops */
c915568d9   Lars-Peter Clausen   mmc: sdhci: Const...
39
  static const struct sdhci_ops sdhci_pltfm_ops = {
1771059cf   Russell King   mmc: sdhci: conve...
40
  	.set_clock = sdhci_set_clock,
2317f56c0   Russell King   mmc: sdhci: conve...
41
  	.set_bus_width = sdhci_set_bus_width,
03231f9b7   Russell King   mmc: sdhci: conve...
42
  	.reset = sdhci_reset,
96d7b78cf   Russell King   mmc: sdhci: conve...
43
  	.set_uhs_signaling = sdhci_set_uhs_signaling,
c63b3cba4   Viresh KUMAR   sdhci-spear: ST S...
44
  };
03a6d2910   Ulf Hansson   mmc: sdhci-spear:...
45
46
  static void sdhci_probe_config_dt(struct device_node *np,
  				struct spear_sdhci *host)
067bf748b   Viresh Kumar   mmc: sdhci-spear:...
47
  {
067bf748b   Viresh Kumar   mmc: sdhci-spear:...
48
49
50
51
52
  	int cd_gpio;
  
  	cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
  	if (!gpio_is_valid(cd_gpio))
  		cd_gpio = -1;
03a6d2910   Ulf Hansson   mmc: sdhci-spear:...
53
  	host->card_int_gpio = cd_gpio;
067bf748b   Viresh Kumar   mmc: sdhci-spear:...
54
  }
067bf748b   Viresh Kumar   mmc: sdhci-spear:...
55

c3be1efd4   Bill Pemberton   mmc: remove use o...
56
  static int sdhci_probe(struct platform_device *pdev)
c63b3cba4   Viresh KUMAR   sdhci-spear: ST S...
57
58
59
60
  {
  	struct sdhci_host *host;
  	struct resource *iomem;
  	struct spear_sdhci *sdhci;
fcdb7c8f5   Russell King   mmc: sdhci-spear:...
61
  	struct device *dev;
c63b3cba4   Viresh KUMAR   sdhci-spear: ST S...
62
  	int ret;
fcdb7c8f5   Russell King   mmc: sdhci-spear:...
63
64
65
66
  	dev = pdev->dev.parent ? pdev->dev.parent : &pdev->dev;
  	host = sdhci_alloc_host(dev, sizeof(*sdhci));
  	if (IS_ERR(host)) {
  		ret = PTR_ERR(host);
c63b3cba4   Viresh KUMAR   sdhci-spear: ST S...
67
68
  		dev_dbg(&pdev->dev, "cannot allocate memory for sdhci
  ");
6ebaf8f2b   Viresh Kumar   mmc: sdhci-spear:...
69
  		goto err;
c63b3cba4   Viresh KUMAR   sdhci-spear: ST S...
70
  	}
475d9e3eb   Russell King   mmc: sdhci-spear:...
71
72
73
74
75
76
77
78
79
80
81
82
83
  	iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  	host->ioaddr = devm_ioremap_resource(&pdev->dev, iomem);
  	if (IS_ERR(host->ioaddr)) {
  		ret = PTR_ERR(host->ioaddr);
  		dev_dbg(&pdev->dev, "unable to map iomem: %d
  ", ret);
  		goto err_host;
  	}
  
  	host->hw_name = "sdhci";
  	host->ops = &sdhci_pltfm_ops;
  	host->irq = platform_get_irq(pdev, 0);
  	host->quirks = SDHCI_QUIRK_BROKEN_ADMA;
fcdb7c8f5   Russell King   mmc: sdhci-spear:...
84
  	sdhci = sdhci_priv(host);
c63b3cba4   Viresh KUMAR   sdhci-spear: ST S...
85
  	/* clk enable */
142dbab95   Russell King   mmc: sdhci-spear:...
86
  	sdhci->clk = devm_clk_get(&pdev->dev, NULL);
c63b3cba4   Viresh KUMAR   sdhci-spear: ST S...
87
88
89
90
  	if (IS_ERR(sdhci->clk)) {
  		ret = PTR_ERR(sdhci->clk);
  		dev_dbg(&pdev->dev, "Error getting clock
  ");
fcdb7c8f5   Russell King   mmc: sdhci-spear:...
91
  		goto err_host;
c63b3cba4   Viresh KUMAR   sdhci-spear: ST S...
92
  	}
da764f97d   Viresh Kumar   mmc: sdhci-spear:...
93
  	ret = clk_prepare_enable(sdhci->clk);
c63b3cba4   Viresh KUMAR   sdhci-spear: ST S...
94
95
96
  	if (ret) {
  		dev_dbg(&pdev->dev, "Error enabling clock
  ");
fcdb7c8f5   Russell King   mmc: sdhci-spear:...
97
  		goto err_host;
c63b3cba4   Viresh KUMAR   sdhci-spear: ST S...
98
  	}
257f9df12   Vipul Kumar Samar   mmc: sdhci-spear:...
99
100
101
102
103
  	ret = clk_set_rate(sdhci->clk, 50000000);
  	if (ret)
  		dev_dbg(&pdev->dev, "Error setting desired clk, clk=%lu
  ",
  				clk_get_rate(sdhci->clk));
03a6d2910   Ulf Hansson   mmc: sdhci-spear:...
104
  	sdhci_probe_config_dt(pdev->dev.of_node, sdhci);
b42b9b12e   Russell King   mmc: sdhci-spear:...
105
106
  	/*
  	 * It is optional to use GPIOs for sdhci card detection. If
03a6d2910   Ulf Hansson   mmc: sdhci-spear:...
107
  	 * sdhci->card_int_gpio < 0, then use original sdhci lines otherwise
b42b9b12e   Russell King   mmc: sdhci-spear:...
108
109
  	 * GPIO lines. We use the built-in GPIO support for this.
  	 */
03a6d2910   Ulf Hansson   mmc: sdhci-spear:...
110
111
  	if (sdhci->card_int_gpio >= 0) {
  		ret = mmc_gpio_request_cd(host->mmc, sdhci->card_int_gpio, 0);
b42b9b12e   Russell King   mmc: sdhci-spear:...
112
113
114
115
  		if (ret < 0) {
  			dev_dbg(&pdev->dev,
  				"failed to request card-detect gpio%d
  ",
03a6d2910   Ulf Hansson   mmc: sdhci-spear:...
116
  				sdhci->card_int_gpio);
b42b9b12e   Russell King   mmc: sdhci-spear:...
117
118
119
  			goto disable_clk;
  		}
  	}
c63b3cba4   Viresh KUMAR   sdhci-spear: ST S...
120
121
122
123
  	ret = sdhci_add_host(host);
  	if (ret) {
  		dev_dbg(&pdev->dev, "error adding host
  ");
fcdb7c8f5   Russell King   mmc: sdhci-spear:...
124
  		goto disable_clk;
c63b3cba4   Viresh KUMAR   sdhci-spear: ST S...
125
126
127
  	}
  
  	platform_set_drvdata(pdev, host);
c63b3cba4   Viresh KUMAR   sdhci-spear: ST S...
128
  	return 0;
6ebaf8f2b   Viresh Kumar   mmc: sdhci-spear:...
129
  disable_clk:
da764f97d   Viresh Kumar   mmc: sdhci-spear:...
130
  	clk_disable_unprepare(sdhci->clk);
fcdb7c8f5   Russell King   mmc: sdhci-spear:...
131
132
  err_host:
  	sdhci_free_host(host);
c63b3cba4   Viresh KUMAR   sdhci-spear: ST S...
133
134
135
136
137
  err:
  	dev_err(&pdev->dev, "spear-sdhci probe failed: %d
  ", ret);
  	return ret;
  }
6e0ee714f   Bill Pemberton   mmc: remove use o...
138
  static int sdhci_remove(struct platform_device *pdev)
c63b3cba4   Viresh KUMAR   sdhci-spear: ST S...
139
140
  {
  	struct sdhci_host *host = platform_get_drvdata(pdev);
fcdb7c8f5   Russell King   mmc: sdhci-spear:...
141
  	struct spear_sdhci *sdhci = sdhci_priv(host);
6ebaf8f2b   Viresh Kumar   mmc: sdhci-spear:...
142
  	int dead = 0;
c63b3cba4   Viresh KUMAR   sdhci-spear: ST S...
143
  	u32 scratch;
c63b3cba4   Viresh KUMAR   sdhci-spear: ST S...
144
145
146
147
148
  	scratch = readl(host->ioaddr + SDHCI_INT_STATUS);
  	if (scratch == (u32)-1)
  		dead = 1;
  
  	sdhci_remove_host(host, dead);
da764f97d   Viresh Kumar   mmc: sdhci-spear:...
149
  	clk_disable_unprepare(sdhci->clk);
fcdb7c8f5   Russell King   mmc: sdhci-spear:...
150
  	sdhci_free_host(host);
c63b3cba4   Viresh KUMAR   sdhci-spear: ST S...
151
152
153
  
  	return 0;
  }
b0dd099ce   Jingoo Han   mmc: sdhci-spear:...
154
  #ifdef CONFIG_PM_SLEEP
b70a7fab2   Viresh Kumar   mmc: sdhci-spear:...
155
156
157
  static int sdhci_suspend(struct device *dev)
  {
  	struct sdhci_host *host = dev_get_drvdata(dev);
fcdb7c8f5   Russell King   mmc: sdhci-spear:...
158
  	struct spear_sdhci *sdhci = sdhci_priv(host);
b70a7fab2   Viresh Kumar   mmc: sdhci-spear:...
159
  	int ret;
984589e59   Viresh Kumar   mmc: sdhci-spear:...
160
  	ret = sdhci_suspend_host(host);
b70a7fab2   Viresh Kumar   mmc: sdhci-spear:...
161
  	if (!ret)
06960a1b5   Viresh Kumar   mmc: sdhci-spear:...
162
  		clk_disable(sdhci->clk);
b70a7fab2   Viresh Kumar   mmc: sdhci-spear:...
163
164
165
166
167
168
169
  
  	return ret;
  }
  
  static int sdhci_resume(struct device *dev)
  {
  	struct sdhci_host *host = dev_get_drvdata(dev);
fcdb7c8f5   Russell King   mmc: sdhci-spear:...
170
  	struct spear_sdhci *sdhci = sdhci_priv(host);
b70a7fab2   Viresh Kumar   mmc: sdhci-spear:...
171
  	int ret;
06960a1b5   Viresh Kumar   mmc: sdhci-spear:...
172
  	ret = clk_enable(sdhci->clk);
b70a7fab2   Viresh Kumar   mmc: sdhci-spear:...
173
174
175
176
177
178
179
180
  	if (ret) {
  		dev_dbg(dev, "Resume: Error enabling clock
  ");
  		return ret;
  	}
  
  	return sdhci_resume_host(host);
  }
b70a7fab2   Viresh Kumar   mmc: sdhci-spear:...
181
  #endif
4b1a61705   Shiraz Hashim   mmc: sdhci-spear:...
182
  static SIMPLE_DEV_PM_OPS(sdhci_pm_ops, sdhci_suspend, sdhci_resume);
067bf748b   Viresh Kumar   mmc: sdhci-spear:...
183
184
185
186
187
188
189
  #ifdef CONFIG_OF
  static const struct of_device_id sdhci_spear_id_table[] = {
  	{ .compatible = "st,spear300-sdhci" },
  	{}
  };
  MODULE_DEVICE_TABLE(of, sdhci_spear_id_table);
  #endif
c63b3cba4   Viresh KUMAR   sdhci-spear: ST S...
190
191
192
  static struct platform_driver sdhci_driver = {
  	.driver = {
  		.name	= "sdhci",
b70a7fab2   Viresh Kumar   mmc: sdhci-spear:...
193
  		.pm	= &sdhci_pm_ops,
067bf748b   Viresh Kumar   mmc: sdhci-spear:...
194
  		.of_match_table = of_match_ptr(sdhci_spear_id_table),
c63b3cba4   Viresh KUMAR   sdhci-spear: ST S...
195
196
  	},
  	.probe		= sdhci_probe,
0433c1435   Bill Pemberton   mmc: remove use o...
197
  	.remove		= sdhci_remove,
c63b3cba4   Viresh KUMAR   sdhci-spear: ST S...
198
  };
d1f81a64a   Axel Lin   mmc: convert driv...
199
  module_platform_driver(sdhci_driver);
c63b3cba4   Viresh KUMAR   sdhci-spear: ST S...
200
201
  
  MODULE_DESCRIPTION("SPEAr Secure Digital Host Controller Interface driver");
da89947b4   Viresh Kumar   Update Viresh Kum...
202
  MODULE_AUTHOR("Viresh Kumar <vireshk@kernel.org>");
c63b3cba4   Viresh KUMAR   sdhci-spear: ST S...
203
  MODULE_LICENSE("GPL v2");