Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 1 | /* |
| 2 | * SDHCI support for SiRF primaII and marco SoCs |
| 3 | * |
| 4 | * Copyright (c) 2011 Cambridge Silicon Radio Limited, a CSR plc group company. |
| 5 | * |
| 6 | * Licensed under GPLv2 or later. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/delay.h> |
| 10 | #include <linux/device.h> |
| 11 | #include <linux/mmc/host.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/of.h> |
| 14 | #include <linux/of_gpio.h> |
| 15 | #include <linux/mmc/slot-gpio.h> |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 16 | #include "sdhci-pltfm.h" |
| 17 | |
Minda Chen | 1ba4c32 | 2014-08-26 10:50:42 +0800 | [diff] [blame^] | 18 | #define SDHCI_SIRF_8BITBUS BIT(3) |
| 19 | |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 20 | struct sdhci_sirf_priv { |
| 21 | struct clk *clk; |
| 22 | int gpio_cd; |
| 23 | }; |
| 24 | |
| 25 | static unsigned int sdhci_sirf_get_max_clk(struct sdhci_host *host) |
| 26 | { |
| 27 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
Arnd Bergmann | e2f6aac | 2013-06-27 11:17:25 -0400 | [diff] [blame] | 28 | struct sdhci_sirf_priv *priv = sdhci_pltfm_priv(pltfm_host); |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 29 | return clk_get_rate(priv->clk); |
| 30 | } |
| 31 | |
Minda Chen | 1ba4c32 | 2014-08-26 10:50:42 +0800 | [diff] [blame^] | 32 | static void sdhci_sirf_set_bus_width(struct sdhci_host *host, int width) |
| 33 | { |
| 34 | u8 ctrl; |
| 35 | |
| 36 | ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL); |
| 37 | ctrl &= ~(SDHCI_CTRL_4BITBUS | SDHCI_SIRF_8BITBUS); |
| 38 | |
| 39 | /* |
| 40 | * CSR atlas7 and prima2 SD host version is not 3.0 |
| 41 | * 8bit-width enable bit of CSR SD hosts is 3, |
| 42 | * while stardard hosts use bit 5 |
| 43 | */ |
| 44 | if (width == MMC_BUS_WIDTH_8) |
| 45 | ctrl |= SDHCI_SIRF_8BITBUS; |
| 46 | else if (width == MMC_BUS_WIDTH_4) |
| 47 | ctrl |= SDHCI_CTRL_4BITBUS; |
| 48 | |
| 49 | sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL); |
| 50 | } |
| 51 | |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 52 | static struct sdhci_ops sdhci_sirf_ops = { |
Russell King | 1771059 | 2014-04-25 12:58:55 +0100 | [diff] [blame] | 53 | .set_clock = sdhci_set_clock, |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 54 | .get_max_clock = sdhci_sirf_get_max_clk, |
Minda Chen | 1ba4c32 | 2014-08-26 10:50:42 +0800 | [diff] [blame^] | 55 | .set_bus_width = sdhci_sirf_set_bus_width, |
Russell King | 03231f9 | 2014-04-25 12:57:12 +0100 | [diff] [blame] | 56 | .reset = sdhci_reset, |
Russell King | 96d7b78 | 2014-04-25 12:59:26 +0100 | [diff] [blame] | 57 | .set_uhs_signaling = sdhci_set_uhs_signaling, |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | static struct sdhci_pltfm_data sdhci_sirf_pdata = { |
| 61 | .ops = &sdhci_sirf_ops, |
| 62 | .quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL | |
| 63 | SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK | |
| 64 | SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN | |
| 65 | SDHCI_QUIRK_INVERTED_WRITE_PROTECT | |
| 66 | SDHCI_QUIRK_DELAY_AFTER_POWER, |
| 67 | }; |
| 68 | |
| 69 | static int sdhci_sirf_probe(struct platform_device *pdev) |
| 70 | { |
| 71 | struct sdhci_host *host; |
| 72 | struct sdhci_pltfm_host *pltfm_host; |
| 73 | struct sdhci_sirf_priv *priv; |
Arnd Bergmann | e2f6aac | 2013-06-27 11:17:25 -0400 | [diff] [blame] | 74 | struct clk *clk; |
| 75 | int gpio_cd; |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 76 | int ret; |
| 77 | |
Arnd Bergmann | e2f6aac | 2013-06-27 11:17:25 -0400 | [diff] [blame] | 78 | clk = devm_clk_get(&pdev->dev, NULL); |
| 79 | if (IS_ERR(clk)) { |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 80 | dev_err(&pdev->dev, "unable to get clock"); |
Arnd Bergmann | e2f6aac | 2013-06-27 11:17:25 -0400 | [diff] [blame] | 81 | return PTR_ERR(clk); |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 82 | } |
| 83 | |
Arnd Bergmann | e2f6aac | 2013-06-27 11:17:25 -0400 | [diff] [blame] | 84 | if (pdev->dev.of_node) |
| 85 | gpio_cd = of_get_named_gpio(pdev->dev.of_node, "cd-gpios", 0); |
| 86 | else |
| 87 | gpio_cd = -EINVAL; |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 88 | |
Arnd Bergmann | e2f6aac | 2013-06-27 11:17:25 -0400 | [diff] [blame] | 89 | host = sdhci_pltfm_init(pdev, &sdhci_sirf_pdata, sizeof(struct sdhci_sirf_priv)); |
| 90 | if (IS_ERR(host)) |
| 91 | return PTR_ERR(host); |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 92 | |
| 93 | pltfm_host = sdhci_priv(host); |
Arnd Bergmann | e2f6aac | 2013-06-27 11:17:25 -0400 | [diff] [blame] | 94 | priv = sdhci_pltfm_priv(pltfm_host); |
| 95 | priv->clk = clk; |
| 96 | priv->gpio_cd = gpio_cd; |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 97 | |
| 98 | sdhci_get_of_property(pdev); |
| 99 | |
Arnd Bergmann | e2f6aac | 2013-06-27 11:17:25 -0400 | [diff] [blame] | 100 | ret = clk_prepare_enable(priv->clk); |
| 101 | if (ret) |
| 102 | goto err_clk_prepare; |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 103 | |
| 104 | ret = sdhci_add_host(host); |
| 105 | if (ret) |
| 106 | goto err_sdhci_add; |
| 107 | |
| 108 | /* |
| 109 | * We must request the IRQ after sdhci_add_host(), as the tasklet only |
| 110 | * gets setup in sdhci_add_host() and we oops. |
| 111 | */ |
| 112 | if (gpio_is_valid(priv->gpio_cd)) { |
Laurent Pinchart | 214fc30 | 2013-08-08 12:38:31 +0200 | [diff] [blame] | 113 | ret = mmc_gpio_request_cd(host->mmc, priv->gpio_cd, 0); |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 114 | if (ret) { |
| 115 | dev_err(&pdev->dev, "card detect irq request failed: %d\n", |
| 116 | ret); |
| 117 | goto err_request_cd; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | return 0; |
| 122 | |
| 123 | err_request_cd: |
| 124 | sdhci_remove_host(host, 0); |
| 125 | err_sdhci_add: |
| 126 | clk_disable_unprepare(priv->clk); |
Arnd Bergmann | e2f6aac | 2013-06-27 11:17:25 -0400 | [diff] [blame] | 127 | err_clk_prepare: |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 128 | sdhci_pltfm_free(pdev); |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 129 | return ret; |
| 130 | } |
| 131 | |
| 132 | static int sdhci_sirf_remove(struct platform_device *pdev) |
| 133 | { |
| 134 | struct sdhci_host *host = platform_get_drvdata(pdev); |
| 135 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
Arnd Bergmann | e2f6aac | 2013-06-27 11:17:25 -0400 | [diff] [blame] | 136 | struct sdhci_sirf_priv *priv = sdhci_pltfm_priv(pltfm_host); |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 137 | |
| 138 | sdhci_pltfm_unregister(pdev); |
| 139 | |
| 140 | if (gpio_is_valid(priv->gpio_cd)) |
| 141 | mmc_gpio_free_cd(host->mmc); |
| 142 | |
| 143 | clk_disable_unprepare(priv->clk); |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | #ifdef CONFIG_PM_SLEEP |
| 148 | static int sdhci_sirf_suspend(struct device *dev) |
| 149 | { |
| 150 | struct sdhci_host *host = dev_get_drvdata(dev); |
| 151 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
Arnd Bergmann | e2f6aac | 2013-06-27 11:17:25 -0400 | [diff] [blame] | 152 | struct sdhci_sirf_priv *priv = sdhci_pltfm_priv(pltfm_host); |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 153 | int ret; |
| 154 | |
| 155 | ret = sdhci_suspend_host(host); |
| 156 | if (ret) |
| 157 | return ret; |
| 158 | |
| 159 | clk_disable(priv->clk); |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | static int sdhci_sirf_resume(struct device *dev) |
| 165 | { |
| 166 | struct sdhci_host *host = dev_get_drvdata(dev); |
| 167 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
Arnd Bergmann | e2f6aac | 2013-06-27 11:17:25 -0400 | [diff] [blame] | 168 | struct sdhci_sirf_priv *priv = sdhci_pltfm_priv(pltfm_host); |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 169 | int ret; |
| 170 | |
| 171 | ret = clk_enable(priv->clk); |
| 172 | if (ret) { |
| 173 | dev_dbg(dev, "Resume: Error enabling clock\n"); |
| 174 | return ret; |
| 175 | } |
| 176 | |
| 177 | return sdhci_resume_host(host); |
| 178 | } |
| 179 | |
| 180 | static SIMPLE_DEV_PM_OPS(sdhci_sirf_pm_ops, sdhci_sirf_suspend, sdhci_sirf_resume); |
| 181 | #endif |
| 182 | |
| 183 | static const struct of_device_id sdhci_sirf_of_match[] = { |
| 184 | { .compatible = "sirf,prima2-sdhc" }, |
| 185 | { } |
| 186 | }; |
| 187 | MODULE_DEVICE_TABLE(of, sdhci_sirf_of_match); |
| 188 | |
| 189 | static struct platform_driver sdhci_sirf_driver = { |
| 190 | .driver = { |
| 191 | .name = "sdhci-sirf", |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 192 | .of_match_table = sdhci_sirf_of_match, |
| 193 | #ifdef CONFIG_PM_SLEEP |
| 194 | .pm = &sdhci_sirf_pm_ops, |
| 195 | #endif |
| 196 | }, |
| 197 | .probe = sdhci_sirf_probe, |
| 198 | .remove = sdhci_sirf_remove, |
| 199 | }; |
| 200 | |
| 201 | module_platform_driver(sdhci_sirf_driver); |
| 202 | |
| 203 | MODULE_DESCRIPTION("SDHCI driver for SiRFprimaII/SiRFmarco"); |
| 204 | MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>"); |
| 205 | MODULE_LICENSE("GPL v2"); |