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 | fc0b638 | 2014-12-04 20:09:20 +0800 | [diff] [blame] | 18 | #define SDHCI_CLK_DELAY_SETTING 0x4C |
Minda Chen | 1ba4c32 | 2014-08-26 10:50:42 +0800 | [diff] [blame] | 19 | #define SDHCI_SIRF_8BITBUS BIT(3) |
Weijun Yang | d1ba44a | 2015-04-27 08:15:13 +0000 | [diff] [blame] | 20 | #define SIRF_TUNING_COUNT 16384 |
Minda Chen | 1ba4c32 | 2014-08-26 10:50:42 +0800 | [diff] [blame] | 21 | |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 22 | struct sdhci_sirf_priv { |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 23 | int gpio_cd; |
| 24 | }; |
| 25 | |
Minda Chen | 1ba4c32 | 2014-08-26 10:50:42 +0800 | [diff] [blame] | 26 | static void sdhci_sirf_set_bus_width(struct sdhci_host *host, int width) |
| 27 | { |
| 28 | u8 ctrl; |
| 29 | |
| 30 | ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL); |
| 31 | ctrl &= ~(SDHCI_CTRL_4BITBUS | SDHCI_SIRF_8BITBUS); |
| 32 | |
| 33 | /* |
| 34 | * CSR atlas7 and prima2 SD host version is not 3.0 |
| 35 | * 8bit-width enable bit of CSR SD hosts is 3, |
| 36 | * while stardard hosts use bit 5 |
| 37 | */ |
| 38 | if (width == MMC_BUS_WIDTH_8) |
| 39 | ctrl |= SDHCI_SIRF_8BITBUS; |
| 40 | else if (width == MMC_BUS_WIDTH_4) |
| 41 | ctrl |= SDHCI_CTRL_4BITBUS; |
| 42 | |
| 43 | sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL); |
| 44 | } |
| 45 | |
Weijun Yang | a1b0b97 | 2015-04-27 08:15:14 +0000 | [diff] [blame] | 46 | static u32 sdhci_sirf_readl_le(struct sdhci_host *host, int reg) |
| 47 | { |
| 48 | u32 val = readl(host->ioaddr + reg); |
| 49 | |
| 50 | if (unlikely((reg == SDHCI_CAPABILITIES_1) && |
| 51 | (host->mmc->caps & MMC_CAP_UHS_SDR50))) { |
| 52 | /* fake CAP_1 register */ |
Weijun Yang | 0de9125 | 2015-10-04 12:04:13 +0000 | [diff] [blame] | 53 | val = SDHCI_SUPPORT_DDR50 | |
| 54 | SDHCI_SUPPORT_SDR50 | SDHCI_USE_SDR50_TUNING; |
Weijun Yang | a1b0b97 | 2015-04-27 08:15:14 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | if (unlikely(reg == SDHCI_SLOT_INT_STATUS)) { |
| 58 | u32 prss = val; |
| 59 | /* fake chips as V3.0 host conreoller */ |
| 60 | prss &= ~(0xFF << 16); |
| 61 | val = prss | (SDHCI_SPEC_300 << 16); |
| 62 | } |
| 63 | return val; |
| 64 | } |
| 65 | |
| 66 | static u16 sdhci_sirf_readw_le(struct sdhci_host *host, int reg) |
| 67 | { |
| 68 | u16 ret = 0; |
| 69 | |
| 70 | ret = readw(host->ioaddr + reg); |
| 71 | |
| 72 | if (unlikely(reg == SDHCI_HOST_VERSION)) { |
| 73 | ret = readw(host->ioaddr + SDHCI_HOST_VERSION); |
| 74 | ret |= SDHCI_SPEC_300; |
| 75 | } |
| 76 | |
| 77 | return ret; |
| 78 | } |
| 79 | |
Minda Chen | fc0b638 | 2014-12-04 20:09:20 +0800 | [diff] [blame] | 80 | static int sdhci_sirf_execute_tuning(struct sdhci_host *host, u32 opcode) |
| 81 | { |
| 82 | int tuning_seq_cnt = 3; |
Weijun Yang | d1ba44a | 2015-04-27 08:15:13 +0000 | [diff] [blame] | 83 | int phase; |
Minda Chen | fc0b638 | 2014-12-04 20:09:20 +0800 | [diff] [blame] | 84 | u8 tuned_phase_cnt = 0; |
weijun yang | b36ac1b | 2015-02-15 23:43:51 +0800 | [diff] [blame] | 85 | int rc = 0, longest_range = 0; |
Minda Chen | fc0b638 | 2014-12-04 20:09:20 +0800 | [diff] [blame] | 86 | int start = -1, end = 0, tuning_value = -1, range = 0; |
| 87 | u16 clock_setting; |
| 88 | struct mmc_host *mmc = host->mmc; |
| 89 | |
| 90 | clock_setting = sdhci_readw(host, SDHCI_CLK_DELAY_SETTING); |
| 91 | clock_setting &= ~0x3fff; |
| 92 | |
| 93 | retry: |
| 94 | phase = 0; |
Weijun Yang | d1ba44a | 2015-04-27 08:15:13 +0000 | [diff] [blame] | 95 | tuned_phase_cnt = 0; |
Minda Chen | fc0b638 | 2014-12-04 20:09:20 +0800 | [diff] [blame] | 96 | do { |
| 97 | sdhci_writel(host, |
weijun yang | b36ac1b | 2015-02-15 23:43:51 +0800 | [diff] [blame] | 98 | clock_setting | phase, |
Minda Chen | fc0b638 | 2014-12-04 20:09:20 +0800 | [diff] [blame] | 99 | SDHCI_CLK_DELAY_SETTING); |
| 100 | |
Chaotian Jing | 9979dbe | 2015-10-27 14:24:28 +0800 | [diff] [blame] | 101 | if (!mmc_send_tuning(mmc, opcode, NULL)) { |
Minda Chen | fc0b638 | 2014-12-04 20:09:20 +0800 | [diff] [blame] | 102 | /* Tuning is successful at this tuning point */ |
Weijun Yang | d1ba44a | 2015-04-27 08:15:13 +0000 | [diff] [blame] | 103 | tuned_phase_cnt++; |
Minda Chen | fc0b638 | 2014-12-04 20:09:20 +0800 | [diff] [blame] | 104 | dev_dbg(mmc_dev(mmc), "%s: Found good phase = %d\n", |
| 105 | mmc_hostname(mmc), phase); |
| 106 | if (start == -1) |
| 107 | start = phase; |
| 108 | end = phase; |
| 109 | range++; |
| 110 | if (phase == (SIRF_TUNING_COUNT - 1) |
| 111 | && range > longest_range) |
| 112 | tuning_value = (start + end) / 2; |
| 113 | } else { |
| 114 | dev_dbg(mmc_dev(mmc), "%s: Found bad phase = %d\n", |
| 115 | mmc_hostname(mmc), phase); |
| 116 | if (range > longest_range) { |
| 117 | tuning_value = (start + end) / 2; |
| 118 | longest_range = range; |
| 119 | } |
| 120 | start = -1; |
| 121 | end = range = 0; |
| 122 | } |
Weijun Yang | d1ba44a | 2015-04-27 08:15:13 +0000 | [diff] [blame] | 123 | } while (++phase < SIRF_TUNING_COUNT); |
Minda Chen | fc0b638 | 2014-12-04 20:09:20 +0800 | [diff] [blame] | 124 | |
| 125 | if (tuned_phase_cnt && tuning_value > 0) { |
| 126 | /* |
| 127 | * Finally set the selected phase in delay |
| 128 | * line hw block. |
| 129 | */ |
| 130 | phase = tuning_value; |
| 131 | sdhci_writel(host, |
weijun yang | b36ac1b | 2015-02-15 23:43:51 +0800 | [diff] [blame] | 132 | clock_setting | phase, |
Minda Chen | fc0b638 | 2014-12-04 20:09:20 +0800 | [diff] [blame] | 133 | SDHCI_CLK_DELAY_SETTING); |
| 134 | |
| 135 | dev_dbg(mmc_dev(mmc), "%s: Setting the tuning phase to %d\n", |
| 136 | mmc_hostname(mmc), phase); |
| 137 | } else { |
| 138 | if (--tuning_seq_cnt) |
| 139 | goto retry; |
| 140 | /* Tuning failed */ |
| 141 | dev_dbg(mmc_dev(mmc), "%s: No tuning point found\n", |
| 142 | mmc_hostname(mmc)); |
| 143 | rc = -EIO; |
| 144 | } |
| 145 | |
| 146 | return rc; |
| 147 | } |
| 148 | |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 149 | static struct sdhci_ops sdhci_sirf_ops = { |
Weijun Yang | a1b0b97 | 2015-04-27 08:15:14 +0000 | [diff] [blame] | 150 | .read_l = sdhci_sirf_readl_le, |
| 151 | .read_w = sdhci_sirf_readw_le, |
Minda Chen | fc0b638 | 2014-12-04 20:09:20 +0800 | [diff] [blame] | 152 | .platform_execute_tuning = sdhci_sirf_execute_tuning, |
Russell King | 1771059 | 2014-04-25 12:58:55 +0100 | [diff] [blame] | 153 | .set_clock = sdhci_set_clock, |
Kevin Hao | e46af29 | 2015-02-27 15:47:28 +0800 | [diff] [blame] | 154 | .get_max_clock = sdhci_pltfm_clk_get_max_clock, |
Minda Chen | 1ba4c32 | 2014-08-26 10:50:42 +0800 | [diff] [blame] | 155 | .set_bus_width = sdhci_sirf_set_bus_width, |
Russell King | 03231f9 | 2014-04-25 12:57:12 +0100 | [diff] [blame] | 156 | .reset = sdhci_reset, |
Russell King | 96d7b78 | 2014-04-25 12:59:26 +0100 | [diff] [blame] | 157 | .set_uhs_signaling = sdhci_set_uhs_signaling, |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | static struct sdhci_pltfm_data sdhci_sirf_pdata = { |
| 161 | .ops = &sdhci_sirf_ops, |
| 162 | .quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL | |
| 163 | SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK | |
| 164 | SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN | |
Barry Song | 1880d8f | 2015-08-12 06:59:33 +0000 | [diff] [blame] | 165 | SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS, |
| 166 | .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN, |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | static int sdhci_sirf_probe(struct platform_device *pdev) |
| 170 | { |
| 171 | struct sdhci_host *host; |
| 172 | struct sdhci_pltfm_host *pltfm_host; |
| 173 | struct sdhci_sirf_priv *priv; |
Arnd Bergmann | e2f6aac | 2013-06-27 11:17:25 -0400 | [diff] [blame] | 174 | struct clk *clk; |
| 175 | int gpio_cd; |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 176 | int ret; |
| 177 | |
Arnd Bergmann | e2f6aac | 2013-06-27 11:17:25 -0400 | [diff] [blame] | 178 | clk = devm_clk_get(&pdev->dev, NULL); |
| 179 | if (IS_ERR(clk)) { |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 180 | dev_err(&pdev->dev, "unable to get clock"); |
Arnd Bergmann | e2f6aac | 2013-06-27 11:17:25 -0400 | [diff] [blame] | 181 | return PTR_ERR(clk); |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 182 | } |
| 183 | |
Arnd Bergmann | e2f6aac | 2013-06-27 11:17:25 -0400 | [diff] [blame] | 184 | if (pdev->dev.of_node) |
| 185 | gpio_cd = of_get_named_gpio(pdev->dev.of_node, "cd-gpios", 0); |
| 186 | else |
| 187 | gpio_cd = -EINVAL; |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 188 | |
Arnd Bergmann | e2f6aac | 2013-06-27 11:17:25 -0400 | [diff] [blame] | 189 | host = sdhci_pltfm_init(pdev, &sdhci_sirf_pdata, sizeof(struct sdhci_sirf_priv)); |
| 190 | if (IS_ERR(host)) |
| 191 | return PTR_ERR(host); |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 192 | |
| 193 | pltfm_host = sdhci_priv(host); |
Kevin Hao | e46af29 | 2015-02-27 15:47:28 +0800 | [diff] [blame] | 194 | pltfm_host->clk = clk; |
Arnd Bergmann | e2f6aac | 2013-06-27 11:17:25 -0400 | [diff] [blame] | 195 | priv = sdhci_pltfm_priv(pltfm_host); |
Arnd Bergmann | e2f6aac | 2013-06-27 11:17:25 -0400 | [diff] [blame] | 196 | priv->gpio_cd = gpio_cd; |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 197 | |
| 198 | sdhci_get_of_property(pdev); |
| 199 | |
Kevin Hao | e46af29 | 2015-02-27 15:47:28 +0800 | [diff] [blame] | 200 | ret = clk_prepare_enable(pltfm_host->clk); |
Arnd Bergmann | e2f6aac | 2013-06-27 11:17:25 -0400 | [diff] [blame] | 201 | if (ret) |
| 202 | goto err_clk_prepare; |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 203 | |
| 204 | ret = sdhci_add_host(host); |
| 205 | if (ret) |
| 206 | goto err_sdhci_add; |
| 207 | |
| 208 | /* |
| 209 | * We must request the IRQ after sdhci_add_host(), as the tasklet only |
| 210 | * gets setup in sdhci_add_host() and we oops. |
| 211 | */ |
| 212 | if (gpio_is_valid(priv->gpio_cd)) { |
Laurent Pinchart | 214fc30 | 2013-08-08 12:38:31 +0200 | [diff] [blame] | 213 | ret = mmc_gpio_request_cd(host->mmc, priv->gpio_cd, 0); |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 214 | if (ret) { |
| 215 | dev_err(&pdev->dev, "card detect irq request failed: %d\n", |
| 216 | ret); |
| 217 | goto err_request_cd; |
| 218 | } |
Stephen Warren | d4d1144 | 2014-09-22 09:57:42 -0600 | [diff] [blame] | 219 | mmc_gpiod_request_cd_irq(host->mmc); |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | return 0; |
| 223 | |
| 224 | err_request_cd: |
| 225 | sdhci_remove_host(host, 0); |
| 226 | err_sdhci_add: |
Kevin Hao | e46af29 | 2015-02-27 15:47:28 +0800 | [diff] [blame] | 227 | clk_disable_unprepare(pltfm_host->clk); |
Arnd Bergmann | e2f6aac | 2013-06-27 11:17:25 -0400 | [diff] [blame] | 228 | err_clk_prepare: |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 229 | sdhci_pltfm_free(pdev); |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 230 | return ret; |
| 231 | } |
| 232 | |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 233 | #ifdef CONFIG_PM_SLEEP |
| 234 | static int sdhci_sirf_suspend(struct device *dev) |
| 235 | { |
| 236 | struct sdhci_host *host = dev_get_drvdata(dev); |
| 237 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 238 | int ret; |
| 239 | |
| 240 | ret = sdhci_suspend_host(host); |
| 241 | if (ret) |
| 242 | return ret; |
| 243 | |
Kevin Hao | e46af29 | 2015-02-27 15:47:28 +0800 | [diff] [blame] | 244 | clk_disable(pltfm_host->clk); |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | static int sdhci_sirf_resume(struct device *dev) |
| 250 | { |
| 251 | struct sdhci_host *host = dev_get_drvdata(dev); |
| 252 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 253 | int ret; |
| 254 | |
Kevin Hao | e46af29 | 2015-02-27 15:47:28 +0800 | [diff] [blame] | 255 | ret = clk_enable(pltfm_host->clk); |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 256 | if (ret) { |
| 257 | dev_dbg(dev, "Resume: Error enabling clock\n"); |
| 258 | return ret; |
| 259 | } |
| 260 | |
| 261 | return sdhci_resume_host(host); |
| 262 | } |
Ulf Hansson | ee4cf97 | 2016-07-27 11:25:23 +0200 | [diff] [blame^] | 263 | #endif |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 264 | |
| 265 | static SIMPLE_DEV_PM_OPS(sdhci_sirf_pm_ops, sdhci_sirf_suspend, sdhci_sirf_resume); |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 266 | |
| 267 | static const struct of_device_id sdhci_sirf_of_match[] = { |
| 268 | { .compatible = "sirf,prima2-sdhc" }, |
| 269 | { } |
| 270 | }; |
| 271 | MODULE_DEVICE_TABLE(of, sdhci_sirf_of_match); |
| 272 | |
| 273 | static struct platform_driver sdhci_sirf_driver = { |
| 274 | .driver = { |
| 275 | .name = "sdhci-sirf", |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 276 | .of_match_table = sdhci_sirf_of_match, |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 277 | .pm = &sdhci_sirf_pm_ops, |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 278 | }, |
| 279 | .probe = sdhci_sirf_probe, |
Kevin Hao | caebcae | 2015-02-27 15:47:31 +0800 | [diff] [blame] | 280 | .remove = sdhci_pltfm_unregister, |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 281 | }; |
| 282 | |
| 283 | module_platform_driver(sdhci_sirf_driver); |
| 284 | |
| 285 | MODULE_DESCRIPTION("SDHCI driver for SiRFprimaII/SiRFmarco"); |
| 286 | MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>"); |
| 287 | MODULE_LICENSE("GPL v2"); |