Olof Johansson | 03d2bfc | 2011-01-01 23:52:56 -0500 | [diff] [blame] | 1 | /* |
| 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> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/clk.h> |
| 19 | #include <linux/io.h> |
| 20 | #include <linux/gpio.h> |
| 21 | #include <linux/mmc/card.h> |
| 22 | #include <linux/mmc/host.h> |
Paul Gortmaker | a4df3ae | 2011-07-03 15:15:51 -0400 | [diff] [blame] | 23 | #include <linux/module.h> |
Olof Johansson | 03d2bfc | 2011-01-01 23:52:56 -0500 | [diff] [blame] | 24 | |
| 25 | #include <mach/gpio.h> |
| 26 | #include <mach/sdhci.h> |
| 27 | |
| 28 | #include "sdhci.h" |
| 29 | #include "sdhci-pltfm.h" |
| 30 | |
| 31 | static u32 tegra_sdhci_readl(struct sdhci_host *host, int reg) |
| 32 | { |
| 33 | u32 val; |
| 34 | |
| 35 | if (unlikely(reg == SDHCI_PRESENT_STATE)) { |
| 36 | /* Use wp_gpio here instead? */ |
| 37 | val = readl(host->ioaddr + reg); |
| 38 | return val | SDHCI_WRITE_PROTECT; |
| 39 | } |
| 40 | |
| 41 | return readl(host->ioaddr + reg); |
| 42 | } |
| 43 | |
| 44 | static u16 tegra_sdhci_readw(struct sdhci_host *host, int reg) |
| 45 | { |
| 46 | if (unlikely(reg == SDHCI_HOST_VERSION)) { |
| 47 | /* Erratum: Version register is invalid in HW. */ |
| 48 | return SDHCI_SPEC_200; |
| 49 | } |
| 50 | |
| 51 | return readw(host->ioaddr + reg); |
| 52 | } |
| 53 | |
| 54 | static void tegra_sdhci_writel(struct sdhci_host *host, u32 val, int reg) |
| 55 | { |
| 56 | /* Seems like we're getting spurious timeout and crc errors, so |
| 57 | * disable signalling of them. In case of real errors software |
| 58 | * timers should take care of eventually detecting them. |
| 59 | */ |
| 60 | if (unlikely(reg == SDHCI_SIGNAL_ENABLE)) |
| 61 | val &= ~(SDHCI_INT_TIMEOUT|SDHCI_INT_CRC); |
| 62 | |
| 63 | writel(val, host->ioaddr + reg); |
| 64 | |
| 65 | if (unlikely(reg == SDHCI_INT_ENABLE)) { |
| 66 | /* Erratum: Must enable block gap interrupt detection */ |
| 67 | u8 gap_ctrl = readb(host->ioaddr + SDHCI_BLOCK_GAP_CONTROL); |
| 68 | if (val & SDHCI_INT_CARD_INT) |
| 69 | gap_ctrl |= 0x8; |
| 70 | else |
| 71 | gap_ctrl &= ~0x8; |
| 72 | writeb(gap_ctrl, host->ioaddr + SDHCI_BLOCK_GAP_CONTROL); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | static unsigned int tegra_sdhci_get_ro(struct sdhci_host *sdhci) |
| 77 | { |
| 78 | struct platform_device *pdev = to_platform_device(mmc_dev(sdhci->mmc)); |
| 79 | struct tegra_sdhci_platform_data *plat; |
| 80 | |
| 81 | plat = pdev->dev.platform_data; |
| 82 | |
| 83 | if (!gpio_is_valid(plat->wp_gpio)) |
| 84 | return -1; |
| 85 | |
| 86 | return gpio_get_value(plat->wp_gpio); |
| 87 | } |
| 88 | |
| 89 | static irqreturn_t carddetect_irq(int irq, void *data) |
| 90 | { |
| 91 | struct sdhci_host *sdhost = (struct sdhci_host *)data; |
| 92 | |
| 93 | tasklet_schedule(&sdhost->card_tasklet); |
| 94 | return IRQ_HANDLED; |
| 95 | }; |
| 96 | |
| 97 | static int tegra_sdhci_8bit(struct sdhci_host *host, int bus_width) |
| 98 | { |
| 99 | struct platform_device *pdev = to_platform_device(mmc_dev(host->mmc)); |
| 100 | struct tegra_sdhci_platform_data *plat; |
| 101 | u32 ctrl; |
| 102 | |
| 103 | plat = pdev->dev.platform_data; |
| 104 | |
| 105 | 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 | |
| 120 | |
| 121 | static int tegra_sdhci_pltfm_init(struct sdhci_host *host, |
| 122 | struct sdhci_pltfm_data *pdata) |
| 123 | { |
| 124 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 125 | struct platform_device *pdev = to_platform_device(mmc_dev(host->mmc)); |
| 126 | struct tegra_sdhci_platform_data *plat; |
| 127 | struct clk *clk; |
| 128 | int rc; |
| 129 | |
| 130 | plat = pdev->dev.platform_data; |
| 131 | if (plat == NULL) { |
| 132 | dev_err(mmc_dev(host->mmc), "missing platform data\n"); |
| 133 | return -ENXIO; |
| 134 | } |
| 135 | |
| 136 | if (gpio_is_valid(plat->power_gpio)) { |
| 137 | rc = gpio_request(plat->power_gpio, "sdhci_power"); |
| 138 | if (rc) { |
| 139 | dev_err(mmc_dev(host->mmc), |
| 140 | "failed to allocate power gpio\n"); |
| 141 | goto out; |
| 142 | } |
| 143 | tegra_gpio_enable(plat->power_gpio); |
| 144 | gpio_direction_output(plat->power_gpio, 1); |
| 145 | } |
| 146 | |
| 147 | if (gpio_is_valid(plat->cd_gpio)) { |
| 148 | rc = gpio_request(plat->cd_gpio, "sdhci_cd"); |
| 149 | if (rc) { |
| 150 | dev_err(mmc_dev(host->mmc), |
| 151 | "failed to allocate cd gpio\n"); |
| 152 | goto out_power; |
| 153 | } |
| 154 | tegra_gpio_enable(plat->cd_gpio); |
| 155 | gpio_direction_input(plat->cd_gpio); |
| 156 | |
| 157 | rc = request_irq(gpio_to_irq(plat->cd_gpio), carddetect_irq, |
| 158 | IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, |
| 159 | mmc_hostname(host->mmc), host); |
| 160 | |
| 161 | if (rc) { |
| 162 | dev_err(mmc_dev(host->mmc), "request irq error\n"); |
| 163 | goto out_cd; |
| 164 | } |
| 165 | |
| 166 | } |
| 167 | |
| 168 | if (gpio_is_valid(plat->wp_gpio)) { |
| 169 | rc = gpio_request(plat->wp_gpio, "sdhci_wp"); |
| 170 | if (rc) { |
| 171 | dev_err(mmc_dev(host->mmc), |
| 172 | "failed to allocate wp gpio\n"); |
Wolfram Sang | 8154b57 | 2011-02-10 18:07:30 +0100 | [diff] [blame] | 173 | goto out_irq; |
Olof Johansson | 03d2bfc | 2011-01-01 23:52:56 -0500 | [diff] [blame] | 174 | } |
| 175 | tegra_gpio_enable(plat->wp_gpio); |
| 176 | gpio_direction_input(plat->wp_gpio); |
| 177 | } |
| 178 | |
| 179 | clk = clk_get(mmc_dev(host->mmc), NULL); |
| 180 | if (IS_ERR(clk)) { |
| 181 | dev_err(mmc_dev(host->mmc), "clk err\n"); |
| 182 | rc = PTR_ERR(clk); |
| 183 | goto out_wp; |
| 184 | } |
| 185 | clk_enable(clk); |
| 186 | pltfm_host->clk = clk; |
| 187 | |
Venkat Rao | c7f409e | 2011-03-25 20:37:47 -0400 | [diff] [blame] | 188 | host->mmc->pm_caps = plat->pm_flags; |
| 189 | |
Olof Johansson | 03d2bfc | 2011-01-01 23:52:56 -0500 | [diff] [blame] | 190 | if (plat->is_8bit) |
| 191 | host->mmc->caps |= MMC_CAP_8_BIT_DATA; |
| 192 | |
| 193 | return 0; |
| 194 | |
| 195 | out_wp: |
| 196 | if (gpio_is_valid(plat->wp_gpio)) { |
| 197 | tegra_gpio_disable(plat->wp_gpio); |
| 198 | gpio_free(plat->wp_gpio); |
| 199 | } |
| 200 | |
Wolfram Sang | 8154b57 | 2011-02-10 18:07:30 +0100 | [diff] [blame] | 201 | out_irq: |
| 202 | if (gpio_is_valid(plat->cd_gpio)) |
| 203 | free_irq(gpio_to_irq(plat->cd_gpio), host); |
Olof Johansson | 03d2bfc | 2011-01-01 23:52:56 -0500 | [diff] [blame] | 204 | out_cd: |
| 205 | if (gpio_is_valid(plat->cd_gpio)) { |
| 206 | tegra_gpio_disable(plat->cd_gpio); |
| 207 | gpio_free(plat->cd_gpio); |
| 208 | } |
| 209 | |
| 210 | out_power: |
| 211 | if (gpio_is_valid(plat->power_gpio)) { |
| 212 | tegra_gpio_disable(plat->power_gpio); |
| 213 | gpio_free(plat->power_gpio); |
| 214 | } |
| 215 | |
| 216 | out: |
| 217 | return rc; |
| 218 | } |
| 219 | |
| 220 | static void tegra_sdhci_pltfm_exit(struct sdhci_host *host) |
| 221 | { |
| 222 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 223 | struct platform_device *pdev = to_platform_device(mmc_dev(host->mmc)); |
| 224 | struct tegra_sdhci_platform_data *plat; |
| 225 | |
| 226 | plat = pdev->dev.platform_data; |
| 227 | |
| 228 | if (gpio_is_valid(plat->wp_gpio)) { |
| 229 | tegra_gpio_disable(plat->wp_gpio); |
| 230 | gpio_free(plat->wp_gpio); |
| 231 | } |
| 232 | |
| 233 | if (gpio_is_valid(plat->cd_gpio)) { |
Wolfram Sang | 8154b57 | 2011-02-10 18:07:30 +0100 | [diff] [blame] | 234 | free_irq(gpio_to_irq(plat->cd_gpio), host); |
Olof Johansson | 03d2bfc | 2011-01-01 23:52:56 -0500 | [diff] [blame] | 235 | tegra_gpio_disable(plat->cd_gpio); |
| 236 | gpio_free(plat->cd_gpio); |
| 237 | } |
| 238 | |
| 239 | if (gpio_is_valid(plat->power_gpio)) { |
| 240 | tegra_gpio_disable(plat->power_gpio); |
| 241 | gpio_free(plat->power_gpio); |
| 242 | } |
| 243 | |
| 244 | clk_disable(pltfm_host->clk); |
| 245 | clk_put(pltfm_host->clk); |
| 246 | } |
| 247 | |
| 248 | static struct sdhci_ops tegra_sdhci_ops = { |
| 249 | .get_ro = tegra_sdhci_get_ro, |
| 250 | .read_l = tegra_sdhci_readl, |
| 251 | .read_w = tegra_sdhci_readw, |
| 252 | .write_l = tegra_sdhci_writel, |
| 253 | .platform_8bit_width = tegra_sdhci_8bit, |
| 254 | }; |
| 255 | |
| 256 | struct sdhci_pltfm_data sdhci_tegra_pdata = { |
| 257 | .quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL | |
| 258 | SDHCI_QUIRK_SINGLE_POWER_WRITE | |
| 259 | SDHCI_QUIRK_NO_HISPD_BIT | |
| 260 | SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC, |
| 261 | .ops = &tegra_sdhci_ops, |
| 262 | .init = tegra_sdhci_pltfm_init, |
| 263 | .exit = tegra_sdhci_pltfm_exit, |
| 264 | }; |