Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Mateusz Kulikowski | 9d11d12 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Qualcomm SDHCI driver - SD/eMMC controller |
| 4 | * |
| 5 | * (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com> |
| 6 | * |
| 7 | * Based on Linux driver |
Mateusz Kulikowski | 9d11d12 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <common.h> |
| 11 | #include <clk.h> |
| 12 | #include <dm.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 13 | #include <malloc.h> |
Mateusz Kulikowski | 9d11d12 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 14 | #include <sdhci.h> |
| 15 | #include <wait_bit.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 16 | #include <asm/global_data.h> |
Mateusz Kulikowski | 9d11d12 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 17 | #include <asm/io.h> |
| 18 | #include <linux/bitops.h> |
| 19 | |
| 20 | /* Non-standard registers needed for SDHCI startup */ |
| 21 | #define SDCC_MCI_POWER 0x0 |
| 22 | #define SDCC_MCI_POWER_SW_RST BIT(7) |
| 23 | |
| 24 | /* This is undocumented register */ |
Sumit Garg | 364c22a | 2022-07-12 12:42:09 +0530 | [diff] [blame] | 25 | #define SDCC_MCI_VERSION 0x50 |
| 26 | #define SDCC_V5_VERSION 0x318 |
| 27 | |
| 28 | #define SDCC_VERSION_MAJOR_SHIFT 28 |
| 29 | #define SDCC_VERSION_MAJOR_MASK (0xf << SDCC_VERSION_MAJOR_SHIFT) |
| 30 | #define SDCC_VERSION_MINOR_MASK 0xff |
Mateusz Kulikowski | 9d11d12 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 31 | |
| 32 | #define SDCC_MCI_STATUS2 0x6C |
| 33 | #define SDCC_MCI_STATUS2_MCI_ACT 0x1 |
| 34 | #define SDCC_MCI_HC_MODE 0x78 |
| 35 | |
Simon Glass | 12293f6 | 2016-06-12 23:30:29 -0600 | [diff] [blame] | 36 | struct msm_sdhc_plat { |
| 37 | struct mmc_config cfg; |
| 38 | struct mmc mmc; |
| 39 | }; |
| 40 | |
Mateusz Kulikowski | 9d11d12 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 41 | struct msm_sdhc { |
| 42 | struct sdhci_host host; |
| 43 | void *base; |
Caleb Connolly | 5f3c4cc | 2024-02-26 17:26:07 +0000 | [diff] [blame] | 44 | struct clk_bulk clks; |
Mateusz Kulikowski | 9d11d12 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 45 | }; |
| 46 | |
Sumit Garg | 364c22a | 2022-07-12 12:42:09 +0530 | [diff] [blame] | 47 | struct msm_sdhc_variant_info { |
| 48 | bool mci_removed; |
Caleb Connolly | a535d71 | 2024-04-09 20:03:00 +0200 | [diff] [blame] | 49 | |
| 50 | u32 core_vendor_spec_capabilities0; |
Sumit Garg | 364c22a | 2022-07-12 12:42:09 +0530 | [diff] [blame] | 51 | }; |
| 52 | |
Mateusz Kulikowski | 9d11d12 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 53 | DECLARE_GLOBAL_DATA_PTR; |
| 54 | |
| 55 | static int msm_sdc_clk_init(struct udevice *dev) |
| 56 | { |
Caleb Connolly | 5f3c4cc | 2024-02-26 17:26:07 +0000 | [diff] [blame] | 57 | struct msm_sdhc *prv = dev_get_priv(dev); |
| 58 | ofnode node = dev_ofnode(dev); |
| 59 | ulong clk_rate; |
| 60 | int ret, i = 0, n_clks; |
| 61 | const char *clk_name; |
Mateusz Kulikowski | 9d11d12 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 62 | |
Caleb Connolly | 5f3c4cc | 2024-02-26 17:26:07 +0000 | [diff] [blame] | 63 | ret = ofnode_read_u32(node, "clock-frequency", (uint *)(&clk_rate)); |
Mateusz Kulikowski | 9d11d12 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 64 | if (ret) |
Caleb Connolly | 86c6093 | 2024-04-09 20:03:03 +0200 | [diff] [blame^] | 65 | clk_rate = 201500000; |
Mateusz Kulikowski | 9d11d12 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 66 | |
Caleb Connolly | 5f3c4cc | 2024-02-26 17:26:07 +0000 | [diff] [blame] | 67 | ret = clk_get_bulk(dev, &prv->clks); |
| 68 | if (ret) { |
| 69 | log_warning("Couldn't get mmc clocks: %d\n", ret); |
Mateusz Kulikowski | 9d11d12 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 70 | return ret; |
Caleb Connolly | 5f3c4cc | 2024-02-26 17:26:07 +0000 | [diff] [blame] | 71 | } |
Mateusz Kulikowski | 9d11d12 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 72 | |
Caleb Connolly | 5f3c4cc | 2024-02-26 17:26:07 +0000 | [diff] [blame] | 73 | ret = clk_enable_bulk(&prv->clks); |
| 74 | if (ret) { |
| 75 | log_warning("Couldn't enable mmc clocks: %d\n", ret); |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 76 | return ret; |
Caleb Connolly | 5f3c4cc | 2024-02-26 17:26:07 +0000 | [diff] [blame] | 77 | } |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 78 | |
Caleb Connolly | 5f3c4cc | 2024-02-26 17:26:07 +0000 | [diff] [blame] | 79 | /* If clock-names is unspecified, then the first clock is the core clock */ |
| 80 | if (!ofnode_get_property(node, "clock-names", &n_clks)) { |
| 81 | if (!clk_set_rate(&prv->clks.clks[0], clk_rate)) { |
| 82 | log_warning("Couldn't set core clock rate: %d\n", ret); |
| 83 | return -EINVAL; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /* Find the index of the "core" clock */ |
| 88 | while (i < n_clks) { |
| 89 | ofnode_read_string_index(node, "clock-names", i, &clk_name); |
| 90 | if (!strcmp(clk_name, "core")) |
| 91 | break; |
| 92 | i++; |
| 93 | } |
| 94 | |
| 95 | if (i >= prv->clks.count) { |
| 96 | log_warning("Couldn't find core clock (index %d but only have %d clocks)\n", i, |
| 97 | prv->clks.count); |
| 98 | return -EINVAL; |
| 99 | } |
| 100 | |
| 101 | /* The clock is already enabled by the clk_bulk above */ |
| 102 | clk_rate = clk_set_rate(&prv->clks.clks[i], clk_rate); |
| 103 | /* If we get a rate of 0 then something has probably gone wrong. */ |
| 104 | if (clk_rate == 0 || IS_ERR((void *)clk_rate)) { |
| 105 | log_warning("Couldn't set MMC core clock rate: %dE\n", clk_rate ? (int)PTR_ERR((void *)clk_rate) : 0); |
| 106 | return -EINVAL; |
| 107 | } |
Mateusz Kulikowski | 9d11d12 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
Sumit Garg | 364c22a | 2022-07-12 12:42:09 +0530 | [diff] [blame] | 112 | static int msm_sdc_mci_init(struct msm_sdhc *prv) |
Mateusz Kulikowski | 9d11d12 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 113 | { |
Mateusz Kulikowski | 9d11d12 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 114 | /* Reset the core and Enable SDHC mode */ |
| 115 | writel(readl(prv->base + SDCC_MCI_POWER) | SDCC_MCI_POWER_SW_RST, |
| 116 | prv->base + SDCC_MCI_POWER); |
| 117 | |
| 118 | |
| 119 | /* Wait for reset to be written to register */ |
Álvaro Fernández Rojas | 4826350 | 2018-01-23 17:14:55 +0100 | [diff] [blame] | 120 | if (wait_for_bit_le32(prv->base + SDCC_MCI_STATUS2, |
| 121 | SDCC_MCI_STATUS2_MCI_ACT, false, 10, false)) { |
Mateusz Kulikowski | 9d11d12 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 122 | printf("msm_sdhci: reset request failed\n"); |
| 123 | return -EIO; |
| 124 | } |
| 125 | |
| 126 | /* SW reset can take upto 10HCLK + 15MCLK cycles. (min 40us) */ |
Álvaro Fernández Rojas | 4826350 | 2018-01-23 17:14:55 +0100 | [diff] [blame] | 127 | if (wait_for_bit_le32(prv->base + SDCC_MCI_POWER, |
| 128 | SDCC_MCI_POWER_SW_RST, false, 2, false)) { |
Mateusz Kulikowski | 9d11d12 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 129 | printf("msm_sdhci: stuck in reset\n"); |
| 130 | return -ETIMEDOUT; |
| 131 | } |
| 132 | |
| 133 | /* Enable host-controller mode */ |
| 134 | writel(1, prv->base + SDCC_MCI_HC_MODE); |
| 135 | |
Sumit Garg | 364c22a | 2022-07-12 12:42:09 +0530 | [diff] [blame] | 136 | return 0; |
| 137 | } |
Mateusz Kulikowski | 9d11d12 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 138 | |
Sumit Garg | 364c22a | 2022-07-12 12:42:09 +0530 | [diff] [blame] | 139 | static int msm_sdc_probe(struct udevice *dev) |
| 140 | { |
| 141 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
| 142 | struct msm_sdhc_plat *plat = dev_get_plat(dev); |
| 143 | struct msm_sdhc *prv = dev_get_priv(dev); |
| 144 | const struct msm_sdhc_variant_info *var_info; |
| 145 | struct sdhci_host *host = &prv->host; |
| 146 | u32 core_version, core_minor, core_major; |
| 147 | u32 caps; |
| 148 | int ret; |
Mateusz Kulikowski | 9d11d12 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 149 | |
Sumit Garg | 364c22a | 2022-07-12 12:42:09 +0530 | [diff] [blame] | 150 | host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD | SDHCI_QUIRK_BROKEN_R1B; |
| 151 | |
| 152 | host->max_clk = 0; |
| 153 | |
| 154 | /* Init clocks */ |
| 155 | ret = msm_sdc_clk_init(dev); |
| 156 | if (ret) |
| 157 | return ret; |
| 158 | |
| 159 | var_info = (void *)dev_get_driver_data(dev); |
| 160 | if (!var_info->mci_removed) { |
| 161 | ret = msm_sdc_mci_init(prv); |
| 162 | if (ret) |
| 163 | return ret; |
| 164 | } |
| 165 | |
| 166 | if (!var_info->mci_removed) |
| 167 | core_version = readl(prv->base + SDCC_MCI_VERSION); |
| 168 | else |
| 169 | core_version = readl(host->ioaddr + SDCC_V5_VERSION); |
| 170 | |
| 171 | core_major = (core_version & SDCC_VERSION_MAJOR_MASK); |
| 172 | core_major >>= SDCC_VERSION_MAJOR_SHIFT; |
| 173 | |
| 174 | core_minor = core_version & SDCC_VERSION_MINOR_MASK; |
Mateusz Kulikowski | 9d11d12 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 175 | |
Caleb Connolly | c2c1b03 | 2024-04-09 20:03:02 +0200 | [diff] [blame] | 176 | log_debug("SDCC version %d.%d\n", core_major, core_minor); |
| 177 | |
Mateusz Kulikowski | 9d11d12 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 178 | /* |
| 179 | * Support for some capabilities is not advertised by newer |
| 180 | * controller versions and must be explicitly enabled. |
| 181 | */ |
| 182 | if (core_major >= 1 && core_minor != 0x11 && core_minor != 0x12) { |
Simon Glass | 12293f6 | 2016-06-12 23:30:29 -0600 | [diff] [blame] | 183 | caps = readl(host->ioaddr + SDHCI_CAPABILITIES); |
Mateusz Kulikowski | 9d11d12 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 184 | caps |= SDHCI_CAN_VDD_300 | SDHCI_CAN_DO_8BIT; |
Caleb Connolly | a535d71 | 2024-04-09 20:03:00 +0200 | [diff] [blame] | 185 | writel(caps, host->ioaddr + var_info->core_vendor_spec_capabilities0); |
Mateusz Kulikowski | 9d11d12 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 186 | } |
| 187 | |
Manivannan Sadhasivam | 8505147 | 2020-07-16 14:37:26 +0530 | [diff] [blame] | 188 | ret = mmc_of_parse(dev, &plat->cfg); |
| 189 | if (ret) |
| 190 | return ret; |
| 191 | |
Simon Glass | 12293f6 | 2016-06-12 23:30:29 -0600 | [diff] [blame] | 192 | host->mmc = &plat->mmc; |
Peng Fan | 6904e1b | 2019-08-06 02:47:53 +0000 | [diff] [blame] | 193 | host->mmc->dev = dev; |
| 194 | ret = sdhci_setup_cfg(&plat->cfg, host, 0, 0); |
Mateusz Kulikowski | eb9d3ca | 2016-06-26 22:43:55 +0200 | [diff] [blame] | 195 | if (ret) |
| 196 | return ret; |
Simon Glass | 12293f6 | 2016-06-12 23:30:29 -0600 | [diff] [blame] | 197 | host->mmc->priv = &prv->host; |
Simon Glass | 12293f6 | 2016-06-12 23:30:29 -0600 | [diff] [blame] | 198 | upriv->mmc = host->mmc; |
Mateusz Kulikowski | eb9d3ca | 2016-06-26 22:43:55 +0200 | [diff] [blame] | 199 | |
Simon Glass | 12293f6 | 2016-06-12 23:30:29 -0600 | [diff] [blame] | 200 | return sdhci_probe(dev); |
Mateusz Kulikowski | 9d11d12 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | static int msm_sdc_remove(struct udevice *dev) |
| 204 | { |
| 205 | struct msm_sdhc *priv = dev_get_priv(dev); |
Sumit Garg | 364c22a | 2022-07-12 12:42:09 +0530 | [diff] [blame] | 206 | const struct msm_sdhc_variant_info *var_info; |
Mateusz Kulikowski | 9d11d12 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 207 | |
Sumit Garg | 364c22a | 2022-07-12 12:42:09 +0530 | [diff] [blame] | 208 | var_info = (void *)dev_get_driver_data(dev); |
| 209 | |
| 210 | /* Disable host-controller mode */ |
Caleb Connolly | f67f224 | 2024-04-09 20:03:01 +0200 | [diff] [blame] | 211 | if (!var_info->mci_removed && priv->base) |
Sumit Garg | 364c22a | 2022-07-12 12:42:09 +0530 | [diff] [blame] | 212 | writel(0, priv->base + SDCC_MCI_HC_MODE); |
Mateusz Kulikowski | 9d11d12 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 213 | |
Caleb Connolly | 5f3c4cc | 2024-02-26 17:26:07 +0000 | [diff] [blame] | 214 | clk_release_bulk(&priv->clks); |
| 215 | |
Mateusz Kulikowski | 9d11d12 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 216 | return 0; |
| 217 | } |
| 218 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 219 | static int msm_of_to_plat(struct udevice *dev) |
Mateusz Kulikowski | 9d11d12 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 220 | { |
Mateusz Kulikowski | 9d11d12 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 221 | struct msm_sdhc *priv = dev_get_priv(dev); |
Caleb Connolly | f67f224 | 2024-04-09 20:03:01 +0200 | [diff] [blame] | 222 | const struct msm_sdhc_variant_info *var_info; |
Mateusz Kulikowski | 9d11d12 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 223 | struct sdhci_host *host = &priv->host; |
Caleb Connolly | f67f224 | 2024-04-09 20:03:01 +0200 | [diff] [blame] | 224 | int ret; |
| 225 | |
| 226 | var_info = (void*)dev_get_driver_data(dev); |
Mateusz Kulikowski | 9d11d12 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 227 | |
| 228 | host->name = strdup(dev->name); |
Masahiro Yamada | 8613c8d | 2020-07-17 14:36:46 +0900 | [diff] [blame] | 229 | host->ioaddr = dev_read_addr_ptr(dev); |
Caleb Connolly | f67f224 | 2024-04-09 20:03:01 +0200 | [diff] [blame] | 230 | ret = dev_read_u32(dev, "bus-width", &host->bus_width); |
| 231 | if (ret) |
| 232 | host->bus_width = 4; |
| 233 | ret = dev_read_u32(dev, "index", &host->index); |
| 234 | if (ret) |
| 235 | host->index = 0; |
| 236 | priv->base = dev_read_addr_index_ptr(dev, 1); |
| 237 | |
| 238 | if (!host->ioaddr) |
Mateusz Kulikowski | 9d11d12 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 239 | return -EINVAL; |
| 240 | |
Caleb Connolly | f67f224 | 2024-04-09 20:03:01 +0200 | [diff] [blame] | 241 | if (!var_info->mci_removed && !priv->base) { |
| 242 | printf("msm_sdhci: MCI base address not found\n"); |
| 243 | return -EINVAL; |
| 244 | } |
| 245 | |
Mateusz Kulikowski | 9d11d12 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 246 | return 0; |
| 247 | } |
| 248 | |
Simon Glass | 12293f6 | 2016-06-12 23:30:29 -0600 | [diff] [blame] | 249 | static int msm_sdc_bind(struct udevice *dev) |
| 250 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 251 | struct msm_sdhc_plat *plat = dev_get_plat(dev); |
Simon Glass | 12293f6 | 2016-06-12 23:30:29 -0600 | [diff] [blame] | 252 | |
Masahiro Yamada | 24f5aec | 2016-09-06 22:17:32 +0900 | [diff] [blame] | 253 | return sdhci_bind(dev, &plat->mmc, &plat->cfg); |
Simon Glass | 12293f6 | 2016-06-12 23:30:29 -0600 | [diff] [blame] | 254 | } |
| 255 | |
Sumit Garg | 364c22a | 2022-07-12 12:42:09 +0530 | [diff] [blame] | 256 | static const struct msm_sdhc_variant_info msm_sdhc_mci_var = { |
| 257 | .mci_removed = false, |
Caleb Connolly | a535d71 | 2024-04-09 20:03:00 +0200 | [diff] [blame] | 258 | |
| 259 | .core_vendor_spec_capabilities0 = 0x21c, |
Sumit Garg | 364c22a | 2022-07-12 12:42:09 +0530 | [diff] [blame] | 260 | }; |
| 261 | |
| 262 | static const struct msm_sdhc_variant_info msm_sdhc_v5_var = { |
| 263 | .mci_removed = true, |
Caleb Connolly | a535d71 | 2024-04-09 20:03:00 +0200 | [diff] [blame] | 264 | |
| 265 | .core_vendor_spec_capabilities0 = 0x11c, |
Sumit Garg | 364c22a | 2022-07-12 12:42:09 +0530 | [diff] [blame] | 266 | }; |
| 267 | |
Mateusz Kulikowski | 9d11d12 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 268 | static const struct udevice_id msm_mmc_ids[] = { |
Sumit Garg | 364c22a | 2022-07-12 12:42:09 +0530 | [diff] [blame] | 269 | { .compatible = "qcom,sdhci-msm-v4", .data = (ulong)&msm_sdhc_mci_var }, |
| 270 | { .compatible = "qcom,sdhci-msm-v5", .data = (ulong)&msm_sdhc_v5_var }, |
Mateusz Kulikowski | 9d11d12 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 271 | { } |
| 272 | }; |
| 273 | |
| 274 | U_BOOT_DRIVER(msm_sdc_drv) = { |
| 275 | .name = "msm_sdc", |
| 276 | .id = UCLASS_MMC, |
| 277 | .of_match = msm_mmc_ids, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 278 | .of_to_plat = msm_of_to_plat, |
Simon Glass | 12293f6 | 2016-06-12 23:30:29 -0600 | [diff] [blame] | 279 | .ops = &sdhci_ops, |
Simon Glass | 12293f6 | 2016-06-12 23:30:29 -0600 | [diff] [blame] | 280 | .bind = msm_sdc_bind, |
Mateusz Kulikowski | 9d11d12 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 281 | .probe = msm_sdc_probe, |
| 282 | .remove = msm_sdc_remove, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 283 | .priv_auto = sizeof(struct msm_sdhc), |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 284 | .plat_auto = sizeof(struct msm_sdhc_plat), |
Mateusz Kulikowski | 9d11d12 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 285 | }; |