blob: 78a36eba4df0b77d5fd433a3a805373484c50c22 [file] [log] [blame]
Olof Johansson03d2bfc2011-01-01 23:52:56 -05001/*
2 * Copyright (C) 2010 Google, Inc.
3 *
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 */
14
15#include <linux/err.h>
Paul Gortmaker96547f52011-07-03 15:15:51 -040016#include <linux/module.h>
Olof Johansson03d2bfc2011-01-01 23:52:56 -050017#include <linux/init.h>
18#include <linux/platform_device.h>
19#include <linux/clk.h>
20#include <linux/io.h>
Stephen Warren55cd65e2011-08-30 13:17:16 -060021#include <linux/of.h>
Grant Likely275173b2011-08-23 12:15:33 -060022#include <linux/of_gpio.h>
Olof Johansson03d2bfc2011-01-01 23:52:56 -050023#include <linux/gpio.h>
24#include <linux/mmc/card.h>
25#include <linux/mmc/host.h>
Paul Gortmaker88b47672011-07-03 15:15:51 -040026#include <linux/module.h>
Olof Johansson03d2bfc2011-01-01 23:52:56 -050027
Russell Kinge6b750d2011-07-26 10:59:32 +010028#include <asm/gpio.h>
Stephen Warrenea5abbd2011-09-26 19:00:02 +010029
30#include <mach/gpio-tegra.h>
Olof Johansson03d2bfc2011-01-01 23:52:56 -050031#include <mach/sdhci.h>
32
Olof Johansson03d2bfc2011-01-01 23:52:56 -050033#include "sdhci-pltfm.h"
34
35static u32 tegra_sdhci_readl(struct sdhci_host *host, int reg)
36{
37 u32 val;
38
39 if (unlikely(reg == SDHCI_PRESENT_STATE)) {
40 /* Use wp_gpio here instead? */
41 val = readl(host->ioaddr + reg);
42 return val | SDHCI_WRITE_PROTECT;
43 }
44
45 return readl(host->ioaddr + reg);
46}
47
48static u16 tegra_sdhci_readw(struct sdhci_host *host, int reg)
49{
50 if (unlikely(reg == SDHCI_HOST_VERSION)) {
51 /* Erratum: Version register is invalid in HW. */
52 return SDHCI_SPEC_200;
53 }
54
55 return readw(host->ioaddr + reg);
56}
57
58static void tegra_sdhci_writel(struct sdhci_host *host, u32 val, int reg)
59{
60 /* Seems like we're getting spurious timeout and crc errors, so
61 * disable signalling of them. In case of real errors software
62 * timers should take care of eventually detecting them.
63 */
64 if (unlikely(reg == SDHCI_SIGNAL_ENABLE))
65 val &= ~(SDHCI_INT_TIMEOUT|SDHCI_INT_CRC);
66
67 writel(val, host->ioaddr + reg);
68
69 if (unlikely(reg == SDHCI_INT_ENABLE)) {
70 /* Erratum: Must enable block gap interrupt detection */
71 u8 gap_ctrl = readb(host->ioaddr + SDHCI_BLOCK_GAP_CONTROL);
72 if (val & SDHCI_INT_CARD_INT)
73 gap_ctrl |= 0x8;
74 else
75 gap_ctrl &= ~0x8;
76 writeb(gap_ctrl, host->ioaddr + SDHCI_BLOCK_GAP_CONTROL);
77 }
78}
79
80static unsigned int tegra_sdhci_get_ro(struct sdhci_host *sdhci)
81{
Grant Likely275173b2011-08-23 12:15:33 -060082 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(sdhci);
83 struct tegra_sdhci_platform_data *plat = pltfm_host->priv;
Olof Johansson03d2bfc2011-01-01 23:52:56 -050084
85 if (!gpio_is_valid(plat->wp_gpio))
86 return -1;
87
88 return gpio_get_value(plat->wp_gpio);
89}
90
91static irqreturn_t carddetect_irq(int irq, void *data)
92{
93 struct sdhci_host *sdhost = (struct sdhci_host *)data;
94
95 tasklet_schedule(&sdhost->card_tasklet);
96 return IRQ_HANDLED;
97};
98
99static int tegra_sdhci_8bit(struct sdhci_host *host, int bus_width)
100{
Grant Likely275173b2011-08-23 12:15:33 -0600101 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
102 struct tegra_sdhci_platform_data *plat = pltfm_host->priv;
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500103 u32 ctrl;
104
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500105 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
106 if (plat->is_8bit && bus_width == MMC_BUS_WIDTH_8) {
107 ctrl &= ~SDHCI_CTRL_4BITBUS;
108 ctrl |= SDHCI_CTRL_8BITBUS;
109 } else {
110 ctrl &= ~SDHCI_CTRL_8BITBUS;
111 if (bus_width == MMC_BUS_WIDTH_4)
112 ctrl |= SDHCI_CTRL_4BITBUS;
113 else
114 ctrl &= ~SDHCI_CTRL_4BITBUS;
115 }
116 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
117 return 0;
118}
119
Shawn Guo85d65092011-05-27 23:48:12 +0800120static struct sdhci_ops tegra_sdhci_ops = {
121 .get_ro = tegra_sdhci_get_ro,
122 .read_l = tegra_sdhci_readl,
123 .read_w = tegra_sdhci_readw,
124 .write_l = tegra_sdhci_writel,
125 .platform_8bit_width = tegra_sdhci_8bit,
126};
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500127
Shawn Guo85d65092011-05-27 23:48:12 +0800128static struct sdhci_pltfm_data sdhci_tegra_pdata = {
129 .quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
130 SDHCI_QUIRK_SINGLE_POWER_WRITE |
131 SDHCI_QUIRK_NO_HISPD_BIT |
132 SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC,
133 .ops = &tegra_sdhci_ops,
134};
135
Grant Likely275173b2011-08-23 12:15:33 -0600136static const struct of_device_id sdhci_tegra_dt_match[] __devinitdata = {
137 { .compatible = "nvidia,tegra20-sdhci", },
138 {}
139};
140MODULE_DEVICE_TABLE(of, sdhci_dt_ids);
141
142static struct tegra_sdhci_platform_data * __devinit sdhci_tegra_dt_parse_pdata(
143 struct platform_device *pdev)
144{
145 struct tegra_sdhci_platform_data *plat;
146 struct device_node *np = pdev->dev.of_node;
147
148 if (!np)
149 return NULL;
150
151 plat = devm_kzalloc(&pdev->dev, sizeof(*plat), GFP_KERNEL);
152 if (!plat) {
153 dev_err(&pdev->dev, "Can't allocate platform data\n");
154 return NULL;
155 }
156
157 plat->cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
158 plat->wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
159 plat->power_gpio = of_get_named_gpio(np, "power-gpios", 0);
Stephen Warren55cd65e2011-08-30 13:17:16 -0600160 if (of_find_property(np, "support-8bit", NULL))
161 plat->is_8bit = 1;
Grant Likely275173b2011-08-23 12:15:33 -0600162
163 return plat;
164}
165
Shawn Guo85d65092011-05-27 23:48:12 +0800166static int __devinit sdhci_tegra_probe(struct platform_device *pdev)
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500167{
Shawn Guo85d65092011-05-27 23:48:12 +0800168 struct sdhci_pltfm_host *pltfm_host;
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500169 struct tegra_sdhci_platform_data *plat;
Shawn Guo85d65092011-05-27 23:48:12 +0800170 struct sdhci_host *host;
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500171 struct clk *clk;
172 int rc;
173
Shawn Guo85d65092011-05-27 23:48:12 +0800174 host = sdhci_pltfm_init(pdev, &sdhci_tegra_pdata);
175 if (IS_ERR(host))
176 return PTR_ERR(host);
177
178 pltfm_host = sdhci_priv(host);
179
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500180 plat = pdev->dev.platform_data;
Shawn Guo85d65092011-05-27 23:48:12 +0800181
Grant Likely275173b2011-08-23 12:15:33 -0600182 if (plat == NULL)
183 plat = sdhci_tegra_dt_parse_pdata(pdev);
184
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500185 if (plat == NULL) {
186 dev_err(mmc_dev(host->mmc), "missing platform data\n");
Shawn Guo85d65092011-05-27 23:48:12 +0800187 rc = -ENXIO;
188 goto err_no_plat;
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500189 }
190
Grant Likely275173b2011-08-23 12:15:33 -0600191 pltfm_host->priv = plat;
192
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500193 if (gpio_is_valid(plat->power_gpio)) {
194 rc = gpio_request(plat->power_gpio, "sdhci_power");
195 if (rc) {
196 dev_err(mmc_dev(host->mmc),
197 "failed to allocate power gpio\n");
Shawn Guo85d65092011-05-27 23:48:12 +0800198 goto err_power_req;
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500199 }
200 tegra_gpio_enable(plat->power_gpio);
201 gpio_direction_output(plat->power_gpio, 1);
202 }
203
204 if (gpio_is_valid(plat->cd_gpio)) {
205 rc = gpio_request(plat->cd_gpio, "sdhci_cd");
206 if (rc) {
207 dev_err(mmc_dev(host->mmc),
208 "failed to allocate cd gpio\n");
Shawn Guo85d65092011-05-27 23:48:12 +0800209 goto err_cd_req;
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500210 }
211 tegra_gpio_enable(plat->cd_gpio);
212 gpio_direction_input(plat->cd_gpio);
213
214 rc = request_irq(gpio_to_irq(plat->cd_gpio), carddetect_irq,
215 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
216 mmc_hostname(host->mmc), host);
217
218 if (rc) {
219 dev_err(mmc_dev(host->mmc), "request irq error\n");
Shawn Guo85d65092011-05-27 23:48:12 +0800220 goto err_cd_irq_req;
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500221 }
222
223 }
224
225 if (gpio_is_valid(plat->wp_gpio)) {
226 rc = gpio_request(plat->wp_gpio, "sdhci_wp");
227 if (rc) {
228 dev_err(mmc_dev(host->mmc),
229 "failed to allocate wp gpio\n");
Shawn Guo85d65092011-05-27 23:48:12 +0800230 goto err_wp_req;
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500231 }
232 tegra_gpio_enable(plat->wp_gpio);
233 gpio_direction_input(plat->wp_gpio);
234 }
235
236 clk = clk_get(mmc_dev(host->mmc), NULL);
237 if (IS_ERR(clk)) {
238 dev_err(mmc_dev(host->mmc), "clk err\n");
239 rc = PTR_ERR(clk);
Shawn Guo85d65092011-05-27 23:48:12 +0800240 goto err_clk_get;
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500241 }
242 clk_enable(clk);
243 pltfm_host->clk = clk;
244
Venkat Raoc7f409e2011-03-25 20:37:47 -0400245 host->mmc->pm_caps = plat->pm_flags;
246
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500247 if (plat->is_8bit)
248 host->mmc->caps |= MMC_CAP_8_BIT_DATA;
249
Shawn Guo85d65092011-05-27 23:48:12 +0800250 rc = sdhci_add_host(host);
251 if (rc)
252 goto err_add_host;
253
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500254 return 0;
255
Shawn Guo85d65092011-05-27 23:48:12 +0800256err_add_host:
257 clk_disable(pltfm_host->clk);
258 clk_put(pltfm_host->clk);
259err_clk_get:
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500260 if (gpio_is_valid(plat->wp_gpio)) {
261 tegra_gpio_disable(plat->wp_gpio);
262 gpio_free(plat->wp_gpio);
263 }
Shawn Guo85d65092011-05-27 23:48:12 +0800264err_wp_req:
Wolfram Sang8154b572011-02-10 18:07:30 +0100265 if (gpio_is_valid(plat->cd_gpio))
266 free_irq(gpio_to_irq(plat->cd_gpio), host);
Shawn Guo85d65092011-05-27 23:48:12 +0800267err_cd_irq_req:
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500268 if (gpio_is_valid(plat->cd_gpio)) {
269 tegra_gpio_disable(plat->cd_gpio);
270 gpio_free(plat->cd_gpio);
271 }
Shawn Guo85d65092011-05-27 23:48:12 +0800272err_cd_req:
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500273 if (gpio_is_valid(plat->power_gpio)) {
274 tegra_gpio_disable(plat->power_gpio);
275 gpio_free(plat->power_gpio);
276 }
Shawn Guo85d65092011-05-27 23:48:12 +0800277err_power_req:
278err_no_plat:
279 sdhci_pltfm_free(pdev);
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500280 return rc;
281}
282
Shawn Guo85d65092011-05-27 23:48:12 +0800283static int __devexit sdhci_tegra_remove(struct platform_device *pdev)
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500284{
Shawn Guo85d65092011-05-27 23:48:12 +0800285 struct sdhci_host *host = platform_get_drvdata(pdev);
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500286 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Grant Likely275173b2011-08-23 12:15:33 -0600287 struct tegra_sdhci_platform_data *plat = pltfm_host->priv;
Shawn Guo85d65092011-05-27 23:48:12 +0800288 int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
289
290 sdhci_remove_host(host, dead);
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500291
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500292 if (gpio_is_valid(plat->wp_gpio)) {
293 tegra_gpio_disable(plat->wp_gpio);
294 gpio_free(plat->wp_gpio);
295 }
296
297 if (gpio_is_valid(plat->cd_gpio)) {
Wolfram Sang8154b572011-02-10 18:07:30 +0100298 free_irq(gpio_to_irq(plat->cd_gpio), host);
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500299 tegra_gpio_disable(plat->cd_gpio);
300 gpio_free(plat->cd_gpio);
301 }
302
303 if (gpio_is_valid(plat->power_gpio)) {
304 tegra_gpio_disable(plat->power_gpio);
305 gpio_free(plat->power_gpio);
306 }
307
308 clk_disable(pltfm_host->clk);
309 clk_put(pltfm_host->clk);
Shawn Guo85d65092011-05-27 23:48:12 +0800310
311 sdhci_pltfm_free(pdev);
312
313 return 0;
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500314}
315
Shawn Guo85d65092011-05-27 23:48:12 +0800316static struct platform_driver sdhci_tegra_driver = {
317 .driver = {
318 .name = "sdhci-tegra",
319 .owner = THIS_MODULE,
Grant Likely275173b2011-08-23 12:15:33 -0600320 .of_match_table = sdhci_tegra_dt_match,
Manuel Lauss29495aa2011-11-03 11:09:45 +0100321 .pm = SDHCI_PLTFM_PMOPS,
Shawn Guo85d65092011-05-27 23:48:12 +0800322 },
323 .probe = sdhci_tegra_probe,
324 .remove = __devexit_p(sdhci_tegra_remove),
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500325};
326
Axel Lind1f81a62011-11-26 12:55:43 +0800327module_platform_driver(sdhci_tegra_driver);
Shawn Guo85d65092011-05-27 23:48:12 +0800328
329MODULE_DESCRIPTION("SDHCI driver for Tegra");
330MODULE_AUTHOR(" Google, Inc.");
331MODULE_LICENSE("GPL v2");