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