Mike Rapoport | 985b1aa | 2010-11-07 16:57:12 -0500 | [diff] [blame] | 1 | /* |
| 2 | * sdhci-dove.c Support for SDHCI on Marvell's Dove SoC |
| 3 | * |
| 4 | * Author: Saeed Bishara <saeed@marvell.com> |
| 5 | * Mike Rapoport <mike@compulab.co.il> |
| 6 | * Based on sdhci-cns3xxx.c |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | */ |
| 21 | |
Sebastian Hesselbarth | 30b87c6 | 2012-07-05 12:14:01 +0200 | [diff] [blame] | 22 | #include <linux/clk.h> |
| 23 | #include <linux/err.h> |
Russell King | f8ec589 | 2012-11-22 23:55:51 +0000 | [diff] [blame] | 24 | #include <linux/gpio.h> |
| 25 | #include <linux/io.h> |
Mike Rapoport | 985b1aa | 2010-11-07 16:57:12 -0500 | [diff] [blame] | 26 | #include <linux/mmc/host.h> |
Russell King | f8ec589 | 2012-11-22 23:55:51 +0000 | [diff] [blame] | 27 | #include <linux/module.h> |
Sebastian Hesselbarth | 4ee7ed0 | 2012-07-31 10:12:59 +0200 | [diff] [blame] | 28 | #include <linux/of.h> |
Russell King | f8ec589 | 2012-11-22 23:55:51 +0000 | [diff] [blame] | 29 | #include <linux/of_gpio.h> |
Mike Rapoport | 985b1aa | 2010-11-07 16:57:12 -0500 | [diff] [blame] | 30 | |
Mike Rapoport | 985b1aa | 2010-11-07 16:57:12 -0500 | [diff] [blame] | 31 | #include "sdhci-pltfm.h" |
| 32 | |
Sebastian Hesselbarth | 30b87c6 | 2012-07-05 12:14:01 +0200 | [diff] [blame] | 33 | struct sdhci_dove_priv { |
| 34 | struct clk *clk; |
Russell King | f8ec589 | 2012-11-22 23:55:51 +0000 | [diff] [blame] | 35 | int gpio_cd; |
Sebastian Hesselbarth | 30b87c6 | 2012-07-05 12:14:01 +0200 | [diff] [blame] | 36 | }; |
| 37 | |
Russell King | f8ec589 | 2012-11-22 23:55:51 +0000 | [diff] [blame] | 38 | static irqreturn_t sdhci_dove_carddetect_irq(int irq, void *data) |
| 39 | { |
| 40 | struct sdhci_host *host = data; |
| 41 | |
| 42 | tasklet_schedule(&host->card_tasklet); |
| 43 | return IRQ_HANDLED; |
| 44 | } |
| 45 | |
Mike Rapoport | 985b1aa | 2010-11-07 16:57:12 -0500 | [diff] [blame] | 46 | static u16 sdhci_dove_readw(struct sdhci_host *host, int reg) |
| 47 | { |
| 48 | u16 ret; |
| 49 | |
| 50 | switch (reg) { |
| 51 | case SDHCI_HOST_VERSION: |
| 52 | case SDHCI_SLOT_INT_STATUS: |
| 53 | /* those registers don't exist */ |
| 54 | return 0; |
| 55 | default: |
| 56 | ret = readw(host->ioaddr + reg); |
| 57 | } |
| 58 | return ret; |
| 59 | } |
| 60 | |
| 61 | static u32 sdhci_dove_readl(struct sdhci_host *host, int reg) |
| 62 | { |
Russell King | f8ec589 | 2012-11-22 23:55:51 +0000 | [diff] [blame] | 63 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 64 | struct sdhci_dove_priv *priv = pltfm_host->priv; |
Mike Rapoport | 985b1aa | 2010-11-07 16:57:12 -0500 | [diff] [blame] | 65 | u32 ret; |
| 66 | |
Russell King | f8ec589 | 2012-11-22 23:55:51 +0000 | [diff] [blame] | 67 | ret = readl(host->ioaddr + reg); |
| 68 | |
Mike Rapoport | 985b1aa | 2010-11-07 16:57:12 -0500 | [diff] [blame] | 69 | switch (reg) { |
| 70 | case SDHCI_CAPABILITIES: |
Mike Rapoport | 985b1aa | 2010-11-07 16:57:12 -0500 | [diff] [blame] | 71 | /* Mask the support for 3.0V */ |
| 72 | ret &= ~SDHCI_CAN_VDD_300; |
| 73 | break; |
Russell King | f8ec589 | 2012-11-22 23:55:51 +0000 | [diff] [blame] | 74 | case SDHCI_PRESENT_STATE: |
| 75 | if (gpio_is_valid(priv->gpio_cd)) { |
| 76 | if (gpio_get_value(priv->gpio_cd) == 0) |
| 77 | ret |= SDHCI_CARD_PRESENT; |
| 78 | else |
| 79 | ret &= ~SDHCI_CARD_PRESENT; |
| 80 | } |
| 81 | break; |
Mike Rapoport | 985b1aa | 2010-11-07 16:57:12 -0500 | [diff] [blame] | 82 | } |
| 83 | return ret; |
| 84 | } |
| 85 | |
Lars-Peter Clausen | c915568 | 2013-03-13 19:26:05 +0100 | [diff] [blame] | 86 | static const struct sdhci_ops sdhci_dove_ops = { |
Mike Rapoport | 985b1aa | 2010-11-07 16:57:12 -0500 | [diff] [blame] | 87 | .read_w = sdhci_dove_readw, |
| 88 | .read_l = sdhci_dove_readl, |
| 89 | }; |
| 90 | |
Lars-Peter Clausen | 1db5eeb | 2013-03-13 19:26:03 +0100 | [diff] [blame] | 91 | static const struct sdhci_pltfm_data sdhci_dove_pdata = { |
Mike Rapoport | 985b1aa | 2010-11-07 16:57:12 -0500 | [diff] [blame] | 92 | .ops = &sdhci_dove_ops, |
| 93 | .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER | |
| 94 | SDHCI_QUIRK_NO_BUSY_IRQ | |
| 95 | SDHCI_QUIRK_BROKEN_TIMEOUT_VAL | |
Sebastian Hesselbarth | a9ca1d5 | 2012-07-21 19:26:19 -0400 | [diff] [blame] | 96 | SDHCI_QUIRK_FORCE_DMA | |
| 97 | SDHCI_QUIRK_NO_HISPD_BIT, |
Mike Rapoport | 985b1aa | 2010-11-07 16:57:12 -0500 | [diff] [blame] | 98 | }; |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame] | 99 | |
Bill Pemberton | c3be1ef | 2012-11-19 13:23:06 -0500 | [diff] [blame] | 100 | static int sdhci_dove_probe(struct platform_device *pdev) |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame] | 101 | { |
Sebastian Hesselbarth | 30b87c6 | 2012-07-05 12:14:01 +0200 | [diff] [blame] | 102 | struct sdhci_host *host; |
| 103 | struct sdhci_pltfm_host *pltfm_host; |
| 104 | struct sdhci_dove_priv *priv; |
| 105 | int ret; |
| 106 | |
Sebastian Hesselbarth | 30b87c6 | 2012-07-05 12:14:01 +0200 | [diff] [blame] | 107 | priv = devm_kzalloc(&pdev->dev, sizeof(struct sdhci_dove_priv), |
| 108 | GFP_KERNEL); |
| 109 | if (!priv) { |
| 110 | dev_err(&pdev->dev, "unable to allocate private data"); |
Russell King - ARM Linux | ee3298a | 2012-10-29 21:43:07 +0000 | [diff] [blame] | 111 | return -ENOMEM; |
Sebastian Hesselbarth | 30b87c6 | 2012-07-05 12:14:01 +0200 | [diff] [blame] | 112 | } |
| 113 | |
Russell King | 7430e77 | 2012-11-22 23:55:10 +0000 | [diff] [blame] | 114 | priv->clk = devm_clk_get(&pdev->dev, NULL); |
Russell King - ARM Linux | ee3298a | 2012-10-29 21:43:07 +0000 | [diff] [blame] | 115 | |
Russell King | f8ec589 | 2012-11-22 23:55:51 +0000 | [diff] [blame] | 116 | if (pdev->dev.of_node) { |
| 117 | priv->gpio_cd = of_get_named_gpio(pdev->dev.of_node, |
| 118 | "cd-gpios", 0); |
| 119 | } else { |
| 120 | priv->gpio_cd = -EINVAL; |
| 121 | } |
| 122 | |
| 123 | if (gpio_is_valid(priv->gpio_cd)) { |
| 124 | ret = gpio_request(priv->gpio_cd, "sdhci-cd"); |
| 125 | if (ret) { |
| 126 | dev_err(&pdev->dev, "card detect gpio request failed: %d\n", |
| 127 | ret); |
| 128 | return ret; |
| 129 | } |
| 130 | gpio_direction_input(priv->gpio_cd); |
| 131 | } |
| 132 | |
Christian Daudt | 0e74823 | 2013-05-29 13:50:05 -0700 | [diff] [blame] | 133 | host = sdhci_pltfm_init(pdev, &sdhci_dove_pdata, 0); |
Russell King | c430689 | 2012-11-22 23:55:30 +0000 | [diff] [blame] | 134 | if (IS_ERR(host)) { |
| 135 | ret = PTR_ERR(host); |
| 136 | goto err_sdhci_pltfm_init; |
| 137 | } |
Russell King - ARM Linux | ee3298a | 2012-10-29 21:43:07 +0000 | [diff] [blame] | 138 | |
Sebastian Hesselbarth | 30b87c6 | 2012-07-05 12:14:01 +0200 | [diff] [blame] | 139 | pltfm_host = sdhci_priv(host); |
| 140 | pltfm_host->priv = priv; |
| 141 | |
Russell King | c430689 | 2012-11-22 23:55:30 +0000 | [diff] [blame] | 142 | if (!IS_ERR(priv->clk)) |
| 143 | clk_prepare_enable(priv->clk); |
| 144 | |
| 145 | sdhci_get_of_property(pdev); |
| 146 | |
| 147 | ret = sdhci_add_host(host); |
| 148 | if (ret) |
| 149 | goto err_sdhci_add; |
| 150 | |
Russell King | f8ec589 | 2012-11-22 23:55:51 +0000 | [diff] [blame] | 151 | /* |
| 152 | * We must request the IRQ after sdhci_add_host(), as the tasklet only |
| 153 | * gets setup in sdhci_add_host() and we oops. |
| 154 | */ |
| 155 | if (gpio_is_valid(priv->gpio_cd)) { |
| 156 | ret = request_irq(gpio_to_irq(priv->gpio_cd), |
| 157 | sdhci_dove_carddetect_irq, |
| 158 | IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, |
| 159 | mmc_hostname(host->mmc), host); |
| 160 | if (ret) { |
| 161 | dev_err(&pdev->dev, "card detect irq request failed: %d\n", |
| 162 | ret); |
| 163 | goto err_request_irq; |
| 164 | } |
| 165 | } |
| 166 | |
Sebastian Hesselbarth | 30b87c6 | 2012-07-05 12:14:01 +0200 | [diff] [blame] | 167 | return 0; |
| 168 | |
Russell King | f8ec589 | 2012-11-22 23:55:51 +0000 | [diff] [blame] | 169 | err_request_irq: |
| 170 | sdhci_remove_host(host, 0); |
Russell King | c430689 | 2012-11-22 23:55:30 +0000 | [diff] [blame] | 171 | err_sdhci_add: |
Russell King | 7430e77 | 2012-11-22 23:55:10 +0000 | [diff] [blame] | 172 | if (!IS_ERR(priv->clk)) |
Russell King - ARM Linux | ee3298a | 2012-10-29 21:43:07 +0000 | [diff] [blame] | 173 | clk_disable_unprepare(priv->clk); |
Russell King | c430689 | 2012-11-22 23:55:30 +0000 | [diff] [blame] | 174 | sdhci_pltfm_free(pdev); |
| 175 | err_sdhci_pltfm_init: |
Russell King | f8ec589 | 2012-11-22 23:55:51 +0000 | [diff] [blame] | 176 | if (gpio_is_valid(priv->gpio_cd)) |
| 177 | gpio_free(priv->gpio_cd); |
Sebastian Hesselbarth | 30b87c6 | 2012-07-05 12:14:01 +0200 | [diff] [blame] | 178 | return ret; |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame] | 179 | } |
| 180 | |
Bill Pemberton | 6e0ee71 | 2012-11-19 13:26:03 -0500 | [diff] [blame] | 181 | static int sdhci_dove_remove(struct platform_device *pdev) |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame] | 182 | { |
Sebastian Hesselbarth | 30b87c6 | 2012-07-05 12:14:01 +0200 | [diff] [blame] | 183 | struct sdhci_host *host = platform_get_drvdata(pdev); |
| 184 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 185 | struct sdhci_dove_priv *priv = pltfm_host->priv; |
| 186 | |
Russell King - ARM Linux | ee3298a | 2012-10-29 21:43:07 +0000 | [diff] [blame] | 187 | sdhci_pltfm_unregister(pdev); |
| 188 | |
Russell King | f8ec589 | 2012-11-22 23:55:51 +0000 | [diff] [blame] | 189 | if (gpio_is_valid(priv->gpio_cd)) { |
| 190 | free_irq(gpio_to_irq(priv->gpio_cd), host); |
| 191 | gpio_free(priv->gpio_cd); |
| 192 | } |
| 193 | |
Russell King | 7430e77 | 2012-11-22 23:55:10 +0000 | [diff] [blame] | 194 | if (!IS_ERR(priv->clk)) |
Russell King - ARM Linux | ee3298a | 2012-10-29 21:43:07 +0000 | [diff] [blame] | 195 | clk_disable_unprepare(priv->clk); |
Russell King | 7430e77 | 2012-11-22 23:55:10 +0000 | [diff] [blame] | 196 | |
Russell King - ARM Linux | ee3298a | 2012-10-29 21:43:07 +0000 | [diff] [blame] | 197 | return 0; |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame] | 198 | } |
| 199 | |
Bill Pemberton | 498d83e | 2012-11-19 13:24:22 -0500 | [diff] [blame] | 200 | static const struct of_device_id sdhci_dove_of_match_table[] = { |
Sebastian Hesselbarth | 4ee7ed0 | 2012-07-31 10:12:59 +0200 | [diff] [blame] | 201 | { .compatible = "marvell,dove-sdhci", }, |
| 202 | {} |
| 203 | }; |
| 204 | MODULE_DEVICE_TABLE(of, sdhci_dove_of_match_table); |
| 205 | |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame] | 206 | static struct platform_driver sdhci_dove_driver = { |
| 207 | .driver = { |
| 208 | .name = "sdhci-dove", |
| 209 | .owner = THIS_MODULE, |
Manuel Lauss | 29495aa | 2011-11-03 11:09:45 +0100 | [diff] [blame] | 210 | .pm = SDHCI_PLTFM_PMOPS, |
Sachin Kamat | 5941fd0 | 2014-02-25 15:18:24 +0530 | [diff] [blame^] | 211 | .of_match_table = sdhci_dove_of_match_table, |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame] | 212 | }, |
| 213 | .probe = sdhci_dove_probe, |
Bill Pemberton | 0433c14 | 2012-11-19 13:20:26 -0500 | [diff] [blame] | 214 | .remove = sdhci_dove_remove, |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame] | 215 | }; |
| 216 | |
Axel Lin | d1f81a6 | 2011-11-26 12:55:43 +0800 | [diff] [blame] | 217 | module_platform_driver(sdhci_dove_driver); |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame] | 218 | |
| 219 | MODULE_DESCRIPTION("SDHCI driver for Dove"); |
| 220 | MODULE_AUTHOR("Saeed Bishara <saeed@marvell.com>, " |
| 221 | "Mike Rapoport <mike@compulab.co.il>"); |
| 222 | MODULE_LICENSE("GPL v2"); |