2 * Copyright (C) 2010 Google, Inc.
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.
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.
15 #include <linux/err.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/platform_device.h>
19 #include <linux/clk.h>
22 #include <linux/of_device.h>
23 #include <linux/of_gpio.h>
24 #include <linux/gpio.h>
25 #include <linux/mmc/card.h>
26 #include <linux/mmc/host.h>
30 #include <mach/gpio-tegra.h>
31 #include <mach/sdhci.h>
33 #include "sdhci-pltfm.h"
35 /* Tegra SDHOST controller vendor register definitions */
36 #define SDHCI_TEGRA_VENDOR_MISC_CTRL 0x120
37 #define SDHCI_MISC_CTRL_ENABLE_SDHCI_SPEC_300 0x20
39 #define NVQUIRK_FORCE_SDHCI_SPEC_200 BIT(0)
40 #define NVQUIRK_ENABLE_BLOCK_GAP_DET BIT(1)
41 #define NVQUIRK_ENABLE_SDHCI_SPEC_300 BIT(2)
43 struct sdhci_tegra_soc_data {
44 struct sdhci_pltfm_data *pdata;
49 const struct tegra_sdhci_platform_data *plat;
50 const struct sdhci_tegra_soc_data *soc_data;
53 static u32 tegra_sdhci_readl(struct sdhci_host *host, int reg)
57 if (unlikely(reg == SDHCI_PRESENT_STATE)) {
58 /* Use wp_gpio here instead? */
59 val = readl(host->ioaddr + reg);
60 return val | SDHCI_WRITE_PROTECT;
63 return readl(host->ioaddr + reg);
66 static u16 tegra_sdhci_readw(struct sdhci_host *host, int reg)
68 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
69 struct sdhci_tegra *tegra_host = pltfm_host->priv;
70 const struct sdhci_tegra_soc_data *soc_data = tegra_host->soc_data;
72 if (unlikely((soc_data->nvquirks & NVQUIRK_FORCE_SDHCI_SPEC_200) &&
73 (reg == SDHCI_HOST_VERSION))) {
74 /* Erratum: Version register is invalid in HW. */
75 return SDHCI_SPEC_200;
78 return readw(host->ioaddr + reg);
81 static void tegra_sdhci_writel(struct sdhci_host *host, u32 val, int reg)
83 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
84 struct sdhci_tegra *tegra_host = pltfm_host->priv;
85 const struct sdhci_tegra_soc_data *soc_data = tegra_host->soc_data;
87 /* Seems like we're getting spurious timeout and crc errors, so
88 * disable signalling of them. In case of real errors software
89 * timers should take care of eventually detecting them.
91 if (unlikely(reg == SDHCI_SIGNAL_ENABLE))
92 val &= ~(SDHCI_INT_TIMEOUT|SDHCI_INT_CRC);
94 writel(val, host->ioaddr + reg);
96 if (unlikely((soc_data->nvquirks & NVQUIRK_ENABLE_BLOCK_GAP_DET) &&
97 (reg == SDHCI_INT_ENABLE))) {
98 /* Erratum: Must enable block gap interrupt detection */
99 u8 gap_ctrl = readb(host->ioaddr + SDHCI_BLOCK_GAP_CONTROL);
100 if (val & SDHCI_INT_CARD_INT)
104 writeb(gap_ctrl, host->ioaddr + SDHCI_BLOCK_GAP_CONTROL);
108 static unsigned int tegra_sdhci_get_ro(struct sdhci_host *host)
110 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
111 struct sdhci_tegra *tegra_host = pltfm_host->priv;
112 const struct tegra_sdhci_platform_data *plat = tegra_host->plat;
114 if (!gpio_is_valid(plat->wp_gpio))
117 return gpio_get_value(plat->wp_gpio);
120 static irqreturn_t carddetect_irq(int irq, void *data)
122 struct sdhci_host *sdhost = (struct sdhci_host *)data;
124 tasklet_schedule(&sdhost->card_tasklet);
128 static void tegra_sdhci_reset_exit(struct sdhci_host *host, u8 mask)
130 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
131 struct sdhci_tegra *tegra_host = pltfm_host->priv;
132 const struct sdhci_tegra_soc_data *soc_data = tegra_host->soc_data;
134 if (!(mask & SDHCI_RESET_ALL))
137 /* Erratum: Enable SDHCI spec v3.00 support */
138 if (soc_data->nvquirks & NVQUIRK_ENABLE_SDHCI_SPEC_300) {
141 misc_ctrl = sdhci_readb(host, SDHCI_TEGRA_VENDOR_MISC_CTRL);
142 misc_ctrl |= SDHCI_MISC_CTRL_ENABLE_SDHCI_SPEC_300;
143 sdhci_writeb(host, misc_ctrl, SDHCI_TEGRA_VENDOR_MISC_CTRL);
147 static int tegra_sdhci_8bit(struct sdhci_host *host, int bus_width)
149 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
150 struct sdhci_tegra *tegra_host = pltfm_host->priv;
151 const struct tegra_sdhci_platform_data *plat = tegra_host->plat;
154 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
155 if (plat->is_8bit && bus_width == MMC_BUS_WIDTH_8) {
156 ctrl &= ~SDHCI_CTRL_4BITBUS;
157 ctrl |= SDHCI_CTRL_8BITBUS;
159 ctrl &= ~SDHCI_CTRL_8BITBUS;
160 if (bus_width == MMC_BUS_WIDTH_4)
161 ctrl |= SDHCI_CTRL_4BITBUS;
163 ctrl &= ~SDHCI_CTRL_4BITBUS;
165 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
169 static struct sdhci_ops tegra_sdhci_ops = {
170 .get_ro = tegra_sdhci_get_ro,
171 .read_l = tegra_sdhci_readl,
172 .read_w = tegra_sdhci_readw,
173 .write_l = tegra_sdhci_writel,
174 .platform_8bit_width = tegra_sdhci_8bit,
175 .platform_reset_exit = tegra_sdhci_reset_exit,
178 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
179 static struct sdhci_pltfm_data sdhci_tegra20_pdata = {
180 .quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
181 SDHCI_QUIRK_SINGLE_POWER_WRITE |
182 SDHCI_QUIRK_NO_HISPD_BIT |
183 SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC,
184 .ops = &tegra_sdhci_ops,
187 static struct sdhci_tegra_soc_data soc_data_tegra20 = {
188 .pdata = &sdhci_tegra20_pdata,
189 .nvquirks = NVQUIRK_FORCE_SDHCI_SPEC_200 |
190 NVQUIRK_ENABLE_BLOCK_GAP_DET,
194 #ifdef CONFIG_ARCH_TEGRA_3x_SOC
195 static struct sdhci_pltfm_data sdhci_tegra30_pdata = {
196 .quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
197 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
198 SDHCI_QUIRK_SINGLE_POWER_WRITE |
199 SDHCI_QUIRK_NO_HISPD_BIT |
200 SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC,
201 .ops = &tegra_sdhci_ops,
204 static struct sdhci_tegra_soc_data soc_data_tegra30 = {
205 .pdata = &sdhci_tegra30_pdata,
206 .nvquirks = NVQUIRK_ENABLE_SDHCI_SPEC_300,
210 static const struct of_device_id sdhci_tegra_dt_match[] __devinitdata = {
211 #ifdef CONFIG_ARCH_TEGRA_3x_SOC
212 { .compatible = "nvidia,tegra30-sdhci", .data = &soc_data_tegra30 },
214 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
215 { .compatible = "nvidia,tegra20-sdhci", .data = &soc_data_tegra20 },
219 MODULE_DEVICE_TABLE(of, sdhci_dt_ids);
221 static struct tegra_sdhci_platform_data * __devinit sdhci_tegra_dt_parse_pdata(
222 struct platform_device *pdev)
224 struct tegra_sdhci_platform_data *plat;
225 struct device_node *np = pdev->dev.of_node;
230 plat = devm_kzalloc(&pdev->dev, sizeof(*plat), GFP_KERNEL);
232 dev_err(&pdev->dev, "Can't allocate platform data\n");
236 plat->cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
237 plat->wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
238 plat->power_gpio = of_get_named_gpio(np, "power-gpios", 0);
239 if (of_find_property(np, "support-8bit", NULL))
245 static int __devinit sdhci_tegra_probe(struct platform_device *pdev)
247 const struct of_device_id *match;
248 const struct sdhci_tegra_soc_data *soc_data;
249 struct sdhci_host *host;
250 struct sdhci_pltfm_host *pltfm_host;
251 struct tegra_sdhci_platform_data *plat;
252 struct sdhci_tegra *tegra_host;
256 match = of_match_device(sdhci_tegra_dt_match, &pdev->dev);
258 soc_data = match->data;
260 soc_data = &soc_data_tegra20;
262 host = sdhci_pltfm_init(pdev, soc_data->pdata);
264 return PTR_ERR(host);
266 pltfm_host = sdhci_priv(host);
268 plat = pdev->dev.platform_data;
271 plat = sdhci_tegra_dt_parse_pdata(pdev);
274 dev_err(mmc_dev(host->mmc), "missing platform data\n");
279 tegra_host = devm_kzalloc(&pdev->dev, sizeof(*tegra_host), GFP_KERNEL);
281 dev_err(mmc_dev(host->mmc), "failed to allocate tegra_host\n");
286 tegra_host->plat = plat;
287 tegra_host->soc_data = soc_data;
289 pltfm_host->priv = tegra_host;
291 if (gpio_is_valid(plat->power_gpio)) {
292 rc = gpio_request(plat->power_gpio, "sdhci_power");
294 dev_err(mmc_dev(host->mmc),
295 "failed to allocate power gpio\n");
298 gpio_direction_output(plat->power_gpio, 1);
301 if (gpio_is_valid(plat->cd_gpio)) {
302 rc = gpio_request(plat->cd_gpio, "sdhci_cd");
304 dev_err(mmc_dev(host->mmc),
305 "failed to allocate cd gpio\n");
308 gpio_direction_input(plat->cd_gpio);
310 rc = request_irq(gpio_to_irq(plat->cd_gpio), carddetect_irq,
311 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
312 mmc_hostname(host->mmc), host);
315 dev_err(mmc_dev(host->mmc), "request irq error\n");
321 if (gpio_is_valid(plat->wp_gpio)) {
322 rc = gpio_request(plat->wp_gpio, "sdhci_wp");
324 dev_err(mmc_dev(host->mmc),
325 "failed to allocate wp gpio\n");
328 gpio_direction_input(plat->wp_gpio);
331 clk = clk_get(mmc_dev(host->mmc), NULL);
333 dev_err(mmc_dev(host->mmc), "clk err\n");
338 pltfm_host->clk = clk;
340 host->mmc->pm_caps = plat->pm_flags;
343 host->mmc->caps |= MMC_CAP_8_BIT_DATA;
345 rc = sdhci_add_host(host);
352 clk_disable(pltfm_host->clk);
353 clk_put(pltfm_host->clk);
355 if (gpio_is_valid(plat->wp_gpio))
356 gpio_free(plat->wp_gpio);
358 if (gpio_is_valid(plat->cd_gpio))
359 free_irq(gpio_to_irq(plat->cd_gpio), host);
361 if (gpio_is_valid(plat->cd_gpio))
362 gpio_free(plat->cd_gpio);
364 if (gpio_is_valid(plat->power_gpio))
365 gpio_free(plat->power_gpio);
368 sdhci_pltfm_free(pdev);
372 static int __devexit sdhci_tegra_remove(struct platform_device *pdev)
374 struct sdhci_host *host = platform_get_drvdata(pdev);
375 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
376 struct sdhci_tegra *tegra_host = pltfm_host->priv;
377 const struct tegra_sdhci_platform_data *plat = tegra_host->plat;
378 int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
380 sdhci_remove_host(host, dead);
382 if (gpio_is_valid(plat->wp_gpio))
383 gpio_free(plat->wp_gpio);
385 if (gpio_is_valid(plat->cd_gpio)) {
386 free_irq(gpio_to_irq(plat->cd_gpio), host);
387 gpio_free(plat->cd_gpio);
390 if (gpio_is_valid(plat->power_gpio))
391 gpio_free(plat->power_gpio);
393 clk_disable(pltfm_host->clk);
394 clk_put(pltfm_host->clk);
396 sdhci_pltfm_free(pdev);
401 static struct platform_driver sdhci_tegra_driver = {
403 .name = "sdhci-tegra",
404 .owner = THIS_MODULE,
405 .of_match_table = sdhci_tegra_dt_match,
406 .pm = SDHCI_PLTFM_PMOPS,
408 .probe = sdhci_tegra_probe,
409 .remove = __devexit_p(sdhci_tegra_remove),
412 module_platform_driver(sdhci_tegra_driver);
414 MODULE_DESCRIPTION("SDHCI driver for Tegra");
415 MODULE_AUTHOR("Google, Inc.");
416 MODULE_LICENSE("GPL v2");