Thomas Gleixner | a636cd6 | 2019-05-19 15:51:34 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 2 | /* |
| 3 | * SDHCI support for SiRF primaII and marco SoCs |
| 4 | * |
| 5 | * Copyright (c) 2011 Cambridge Silicon Radio Limited, a CSR plc group company. |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/delay.h> |
| 9 | #include <linux/device.h> |
| 10 | #include <linux/mmc/host.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/of.h> |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 13 | #include <linux/mmc/slot-gpio.h> |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 14 | #include "sdhci-pltfm.h" |
| 15 | |
Minda Chen | fc0b638 | 2014-12-04 20:09:20 +0800 | [diff] [blame] | 16 | #define SDHCI_CLK_DELAY_SETTING 0x4C |
Minda Chen | 1ba4c32 | 2014-08-26 10:50:42 +0800 | [diff] [blame] | 17 | #define SDHCI_SIRF_8BITBUS BIT(3) |
Weijun Yang | d1ba44a | 2015-04-27 08:15:13 +0000 | [diff] [blame] | 18 | #define SIRF_TUNING_COUNT 16384 |
Minda Chen | 1ba4c32 | 2014-08-26 10:50:42 +0800 | [diff] [blame] | 19 | |
Minda Chen | 1ba4c32 | 2014-08-26 10:50:42 +0800 | [diff] [blame] | 20 | static void sdhci_sirf_set_bus_width(struct sdhci_host *host, int width) |
| 21 | { |
| 22 | u8 ctrl; |
| 23 | |
| 24 | ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL); |
| 25 | ctrl &= ~(SDHCI_CTRL_4BITBUS | SDHCI_SIRF_8BITBUS); |
| 26 | |
| 27 | /* |
| 28 | * CSR atlas7 and prima2 SD host version is not 3.0 |
| 29 | * 8bit-width enable bit of CSR SD hosts is 3, |
| 30 | * while stardard hosts use bit 5 |
| 31 | */ |
| 32 | if (width == MMC_BUS_WIDTH_8) |
| 33 | ctrl |= SDHCI_SIRF_8BITBUS; |
| 34 | else if (width == MMC_BUS_WIDTH_4) |
| 35 | ctrl |= SDHCI_CTRL_4BITBUS; |
| 36 | |
| 37 | sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL); |
| 38 | } |
| 39 | |
Weijun Yang | a1b0b97 | 2015-04-27 08:15:14 +0000 | [diff] [blame] | 40 | static u32 sdhci_sirf_readl_le(struct sdhci_host *host, int reg) |
| 41 | { |
| 42 | u32 val = readl(host->ioaddr + reg); |
| 43 | |
| 44 | if (unlikely((reg == SDHCI_CAPABILITIES_1) && |
| 45 | (host->mmc->caps & MMC_CAP_UHS_SDR50))) { |
| 46 | /* fake CAP_1 register */ |
Weijun Yang | 0de9125 | 2015-10-04 12:04:13 +0000 | [diff] [blame] | 47 | val = SDHCI_SUPPORT_DDR50 | |
| 48 | SDHCI_SUPPORT_SDR50 | SDHCI_USE_SDR50_TUNING; |
Weijun Yang | a1b0b97 | 2015-04-27 08:15:14 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | if (unlikely(reg == SDHCI_SLOT_INT_STATUS)) { |
| 52 | u32 prss = val; |
| 53 | /* fake chips as V3.0 host conreoller */ |
| 54 | prss &= ~(0xFF << 16); |
| 55 | val = prss | (SDHCI_SPEC_300 << 16); |
| 56 | } |
| 57 | return val; |
| 58 | } |
| 59 | |
| 60 | static u16 sdhci_sirf_readw_le(struct sdhci_host *host, int reg) |
| 61 | { |
| 62 | u16 ret = 0; |
| 63 | |
| 64 | ret = readw(host->ioaddr + reg); |
| 65 | |
| 66 | if (unlikely(reg == SDHCI_HOST_VERSION)) { |
| 67 | ret = readw(host->ioaddr + SDHCI_HOST_VERSION); |
| 68 | ret |= SDHCI_SPEC_300; |
| 69 | } |
| 70 | |
| 71 | return ret; |
| 72 | } |
| 73 | |
Minda Chen | fc0b638 | 2014-12-04 20:09:20 +0800 | [diff] [blame] | 74 | static int sdhci_sirf_execute_tuning(struct sdhci_host *host, u32 opcode) |
| 75 | { |
| 76 | int tuning_seq_cnt = 3; |
Weijun Yang | d1ba44a | 2015-04-27 08:15:13 +0000 | [diff] [blame] | 77 | int phase; |
Minda Chen | fc0b638 | 2014-12-04 20:09:20 +0800 | [diff] [blame] | 78 | u8 tuned_phase_cnt = 0; |
weijun yang | b36ac1b | 2015-02-15 23:43:51 +0800 | [diff] [blame] | 79 | int rc = 0, longest_range = 0; |
Minda Chen | fc0b638 | 2014-12-04 20:09:20 +0800 | [diff] [blame] | 80 | int start = -1, end = 0, tuning_value = -1, range = 0; |
| 81 | u16 clock_setting; |
| 82 | struct mmc_host *mmc = host->mmc; |
| 83 | |
| 84 | clock_setting = sdhci_readw(host, SDHCI_CLK_DELAY_SETTING); |
| 85 | clock_setting &= ~0x3fff; |
| 86 | |
| 87 | retry: |
| 88 | phase = 0; |
Weijun Yang | d1ba44a | 2015-04-27 08:15:13 +0000 | [diff] [blame] | 89 | tuned_phase_cnt = 0; |
Minda Chen | fc0b638 | 2014-12-04 20:09:20 +0800 | [diff] [blame] | 90 | do { |
| 91 | sdhci_writel(host, |
weijun yang | b36ac1b | 2015-02-15 23:43:51 +0800 | [diff] [blame] | 92 | clock_setting | phase, |
Minda Chen | fc0b638 | 2014-12-04 20:09:20 +0800 | [diff] [blame] | 93 | SDHCI_CLK_DELAY_SETTING); |
| 94 | |
Chaotian Jing | 9979dbe | 2015-10-27 14:24:28 +0800 | [diff] [blame] | 95 | if (!mmc_send_tuning(mmc, opcode, NULL)) { |
Minda Chen | fc0b638 | 2014-12-04 20:09:20 +0800 | [diff] [blame] | 96 | /* Tuning is successful at this tuning point */ |
Weijun Yang | d1ba44a | 2015-04-27 08:15:13 +0000 | [diff] [blame] | 97 | tuned_phase_cnt++; |
Minda Chen | fc0b638 | 2014-12-04 20:09:20 +0800 | [diff] [blame] | 98 | dev_dbg(mmc_dev(mmc), "%s: Found good phase = %d\n", |
| 99 | mmc_hostname(mmc), phase); |
| 100 | if (start == -1) |
| 101 | start = phase; |
| 102 | end = phase; |
| 103 | range++; |
| 104 | if (phase == (SIRF_TUNING_COUNT - 1) |
| 105 | && range > longest_range) |
| 106 | tuning_value = (start + end) / 2; |
| 107 | } else { |
| 108 | dev_dbg(mmc_dev(mmc), "%s: Found bad phase = %d\n", |
| 109 | mmc_hostname(mmc), phase); |
| 110 | if (range > longest_range) { |
| 111 | tuning_value = (start + end) / 2; |
| 112 | longest_range = range; |
| 113 | } |
| 114 | start = -1; |
| 115 | end = range = 0; |
| 116 | } |
Weijun Yang | d1ba44a | 2015-04-27 08:15:13 +0000 | [diff] [blame] | 117 | } while (++phase < SIRF_TUNING_COUNT); |
Minda Chen | fc0b638 | 2014-12-04 20:09:20 +0800 | [diff] [blame] | 118 | |
| 119 | if (tuned_phase_cnt && tuning_value > 0) { |
| 120 | /* |
| 121 | * Finally set the selected phase in delay |
| 122 | * line hw block. |
| 123 | */ |
| 124 | phase = tuning_value; |
| 125 | sdhci_writel(host, |
weijun yang | b36ac1b | 2015-02-15 23:43:51 +0800 | [diff] [blame] | 126 | clock_setting | phase, |
Minda Chen | fc0b638 | 2014-12-04 20:09:20 +0800 | [diff] [blame] | 127 | SDHCI_CLK_DELAY_SETTING); |
| 128 | |
| 129 | dev_dbg(mmc_dev(mmc), "%s: Setting the tuning phase to %d\n", |
| 130 | mmc_hostname(mmc), phase); |
| 131 | } else { |
| 132 | if (--tuning_seq_cnt) |
| 133 | goto retry; |
| 134 | /* Tuning failed */ |
| 135 | dev_dbg(mmc_dev(mmc), "%s: No tuning point found\n", |
| 136 | mmc_hostname(mmc)); |
| 137 | rc = -EIO; |
| 138 | } |
| 139 | |
| 140 | return rc; |
| 141 | } |
| 142 | |
Julia Lawall | 1201885 | 2017-08-07 11:50:42 +0200 | [diff] [blame] | 143 | static const struct sdhci_ops sdhci_sirf_ops = { |
Weijun Yang | a1b0b97 | 2015-04-27 08:15:14 +0000 | [diff] [blame] | 144 | .read_l = sdhci_sirf_readl_le, |
| 145 | .read_w = sdhci_sirf_readw_le, |
Minda Chen | fc0b638 | 2014-12-04 20:09:20 +0800 | [diff] [blame] | 146 | .platform_execute_tuning = sdhci_sirf_execute_tuning, |
Russell King | 1771059 | 2014-04-25 12:58:55 +0100 | [diff] [blame] | 147 | .set_clock = sdhci_set_clock, |
Kevin Hao | e46af29 | 2015-02-27 15:47:28 +0800 | [diff] [blame] | 148 | .get_max_clock = sdhci_pltfm_clk_get_max_clock, |
Minda Chen | 1ba4c32 | 2014-08-26 10:50:42 +0800 | [diff] [blame] | 149 | .set_bus_width = sdhci_sirf_set_bus_width, |
Russell King | 03231f9 | 2014-04-25 12:57:12 +0100 | [diff] [blame] | 150 | .reset = sdhci_reset, |
Russell King | 96d7b78 | 2014-04-25 12:59:26 +0100 | [diff] [blame] | 151 | .set_uhs_signaling = sdhci_set_uhs_signaling, |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 152 | }; |
| 153 | |
Julia Lawall | 1201885 | 2017-08-07 11:50:42 +0200 | [diff] [blame] | 154 | static const struct sdhci_pltfm_data sdhci_sirf_pdata = { |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 155 | .ops = &sdhci_sirf_ops, |
| 156 | .quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL | |
| 157 | SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK | |
| 158 | SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN | |
Barry Song | 1880d8f | 2015-08-12 06:59:33 +0000 | [diff] [blame] | 159 | SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS, |
| 160 | .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN, |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | static int sdhci_sirf_probe(struct platform_device *pdev) |
| 164 | { |
| 165 | struct sdhci_host *host; |
| 166 | struct sdhci_pltfm_host *pltfm_host; |
Arnd Bergmann | e2f6aac | 2013-06-27 11:17:25 -0400 | [diff] [blame] | 167 | struct clk *clk; |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 168 | int ret; |
| 169 | |
Arnd Bergmann | e2f6aac | 2013-06-27 11:17:25 -0400 | [diff] [blame] | 170 | clk = devm_clk_get(&pdev->dev, NULL); |
| 171 | if (IS_ERR(clk)) { |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 172 | dev_err(&pdev->dev, "unable to get clock"); |
Arnd Bergmann | e2f6aac | 2013-06-27 11:17:25 -0400 | [diff] [blame] | 173 | return PTR_ERR(clk); |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 174 | } |
| 175 | |
Linus Walleij | bbf57df | 2018-09-24 10:02:33 +0200 | [diff] [blame] | 176 | host = sdhci_pltfm_init(pdev, &sdhci_sirf_pdata, 0); |
Arnd Bergmann | e2f6aac | 2013-06-27 11:17:25 -0400 | [diff] [blame] | 177 | if (IS_ERR(host)) |
| 178 | return PTR_ERR(host); |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 179 | |
| 180 | pltfm_host = sdhci_priv(host); |
Kevin Hao | e46af29 | 2015-02-27 15:47:28 +0800 | [diff] [blame] | 181 | pltfm_host->clk = clk; |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 182 | |
| 183 | sdhci_get_of_property(pdev); |
| 184 | |
Kevin Hao | e46af29 | 2015-02-27 15:47:28 +0800 | [diff] [blame] | 185 | ret = clk_prepare_enable(pltfm_host->clk); |
Arnd Bergmann | e2f6aac | 2013-06-27 11:17:25 -0400 | [diff] [blame] | 186 | if (ret) |
| 187 | goto err_clk_prepare; |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 188 | |
| 189 | ret = sdhci_add_host(host); |
| 190 | if (ret) |
| 191 | goto err_sdhci_add; |
| 192 | |
| 193 | /* |
| 194 | * We must request the IRQ after sdhci_add_host(), as the tasklet only |
| 195 | * gets setup in sdhci_add_host() and we oops. |
| 196 | */ |
Michał Mirosław | d0052ad | 2019-12-11 03:40:56 +0100 | [diff] [blame] | 197 | ret = mmc_gpiod_request_cd(host->mmc, "cd", 0, false, 0); |
Linus Walleij | bbf57df | 2018-09-24 10:02:33 +0200 | [diff] [blame] | 198 | if (ret == -EPROBE_DEFER) |
| 199 | goto err_request_cd; |
| 200 | if (!ret) |
Stephen Warren | d4d1144 | 2014-09-22 09:57:42 -0600 | [diff] [blame] | 201 | mmc_gpiod_request_cd_irq(host->mmc); |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 202 | |
| 203 | return 0; |
| 204 | |
| 205 | err_request_cd: |
| 206 | sdhci_remove_host(host, 0); |
| 207 | err_sdhci_add: |
Kevin Hao | e46af29 | 2015-02-27 15:47:28 +0800 | [diff] [blame] | 208 | clk_disable_unprepare(pltfm_host->clk); |
Arnd Bergmann | e2f6aac | 2013-06-27 11:17:25 -0400 | [diff] [blame] | 209 | err_clk_prepare: |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 210 | sdhci_pltfm_free(pdev); |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 211 | return ret; |
| 212 | } |
| 213 | |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 214 | static const struct of_device_id sdhci_sirf_of_match[] = { |
| 215 | { .compatible = "sirf,prima2-sdhc" }, |
| 216 | { } |
| 217 | }; |
| 218 | MODULE_DEVICE_TABLE(of, sdhci_sirf_of_match); |
| 219 | |
| 220 | static struct platform_driver sdhci_sirf_driver = { |
| 221 | .driver = { |
| 222 | .name = "sdhci-sirf", |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 223 | .of_match_table = sdhci_sirf_of_match, |
Masahiro Yamada | 1ab0d2d | 2017-08-23 13:15:02 +0900 | [diff] [blame] | 224 | .pm = &sdhci_pltfm_pmops, |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 225 | }, |
| 226 | .probe = sdhci_sirf_probe, |
Kevin Hao | caebcae | 2015-02-27 15:47:31 +0800 | [diff] [blame] | 227 | .remove = sdhci_pltfm_unregister, |
Barry Song | b3b665b | 2013-03-21 16:27:19 +0800 | [diff] [blame] | 228 | }; |
| 229 | |
| 230 | module_platform_driver(sdhci_sirf_driver); |
| 231 | |
| 232 | MODULE_DESCRIPTION("SDHCI driver for SiRFprimaII/SiRFmarco"); |
| 233 | MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>"); |
| 234 | MODULE_LICENSE("GPL v2"); |