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 | |
| 10 | #include <common.h> |
| 11 | #include <clk.h> |
| 12 | #include <dm.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 13 | #include <malloc.h> |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 14 | #include <sdhci.h> |
| 15 | #include <wait_bit.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 16 | #include <asm/global_data.h> |
Mateusz Kulikowski | a00b0c0 | 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 | 1e2dc03 | 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 | a00b0c0 | 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 | |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 36 | /* Non standard (?) SDHCI register */ |
| 37 | #define SDHCI_VENDOR_SPEC_CAPABILITIES0 0x11c |
| 38 | |
Simon Glass | 8ef0765 | 2016-06-12 23:30:29 -0600 | [diff] [blame] | 39 | struct msm_sdhc_plat { |
| 40 | struct mmc_config cfg; |
| 41 | struct mmc mmc; |
| 42 | }; |
| 43 | |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 44 | struct msm_sdhc { |
| 45 | struct sdhci_host host; |
| 46 | void *base; |
Caleb Connolly | fb782f5 | 2024-02-26 17:26:07 +0000 | [diff] [blame] | 47 | struct clk_bulk clks; |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 48 | }; |
| 49 | |
Sumit Garg | 1e2dc03 | 2022-07-12 12:42:09 +0530 | [diff] [blame] | 50 | struct msm_sdhc_variant_info { |
| 51 | bool mci_removed; |
| 52 | }; |
| 53 | |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 54 | DECLARE_GLOBAL_DATA_PTR; |
| 55 | |
| 56 | static int msm_sdc_clk_init(struct udevice *dev) |
| 57 | { |
Caleb Connolly | fb782f5 | 2024-02-26 17:26:07 +0000 | [diff] [blame] | 58 | struct msm_sdhc *prv = dev_get_priv(dev); |
| 59 | ofnode node = dev_ofnode(dev); |
| 60 | ulong clk_rate; |
| 61 | int ret, i = 0, n_clks; |
| 62 | const char *clk_name; |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 63 | |
Caleb Connolly | fb782f5 | 2024-02-26 17:26:07 +0000 | [diff] [blame] | 64 | ret = ofnode_read_u32(node, "clock-frequency", (uint *)(&clk_rate)); |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 65 | if (ret) |
Caleb Connolly | fb782f5 | 2024-02-26 17:26:07 +0000 | [diff] [blame] | 66 | clk_rate = 400000; |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 67 | |
Caleb Connolly | fb782f5 | 2024-02-26 17:26:07 +0000 | [diff] [blame] | 68 | ret = clk_get_bulk(dev, &prv->clks); |
| 69 | if (ret) { |
| 70 | log_warning("Couldn't get mmc clocks: %d\n", ret); |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 71 | return ret; |
Caleb Connolly | fb782f5 | 2024-02-26 17:26:07 +0000 | [diff] [blame] | 72 | } |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 73 | |
Caleb Connolly | fb782f5 | 2024-02-26 17:26:07 +0000 | [diff] [blame] | 74 | ret = clk_enable_bulk(&prv->clks); |
| 75 | if (ret) { |
| 76 | log_warning("Couldn't enable mmc clocks: %d\n", ret); |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 77 | return ret; |
Caleb Connolly | fb782f5 | 2024-02-26 17:26:07 +0000 | [diff] [blame] | 78 | } |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 79 | |
Caleb Connolly | fb782f5 | 2024-02-26 17:26:07 +0000 | [diff] [blame] | 80 | /* If clock-names is unspecified, then the first clock is the core clock */ |
| 81 | if (!ofnode_get_property(node, "clock-names", &n_clks)) { |
| 82 | if (!clk_set_rate(&prv->clks.clks[0], clk_rate)) { |
| 83 | log_warning("Couldn't set core clock rate: %d\n", ret); |
| 84 | return -EINVAL; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /* Find the index of the "core" clock */ |
| 89 | while (i < n_clks) { |
| 90 | ofnode_read_string_index(node, "clock-names", i, &clk_name); |
| 91 | if (!strcmp(clk_name, "core")) |
| 92 | break; |
| 93 | i++; |
| 94 | } |
| 95 | |
| 96 | if (i >= prv->clks.count) { |
| 97 | log_warning("Couldn't find core clock (index %d but only have %d clocks)\n", i, |
| 98 | prv->clks.count); |
| 99 | return -EINVAL; |
| 100 | } |
| 101 | |
| 102 | /* The clock is already enabled by the clk_bulk above */ |
| 103 | clk_rate = clk_set_rate(&prv->clks.clks[i], clk_rate); |
| 104 | /* If we get a rate of 0 then something has probably gone wrong. */ |
| 105 | if (clk_rate == 0 || IS_ERR((void *)clk_rate)) { |
| 106 | log_warning("Couldn't set MMC core clock rate: %dE\n", clk_rate ? (int)PTR_ERR((void *)clk_rate) : 0); |
| 107 | return -EINVAL; |
| 108 | } |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
Sumit Garg | 1e2dc03 | 2022-07-12 12:42:09 +0530 | [diff] [blame] | 113 | static int msm_sdc_mci_init(struct msm_sdhc *prv) |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 114 | { |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 115 | /* Reset the core and Enable SDHC mode */ |
| 116 | writel(readl(prv->base + SDCC_MCI_POWER) | SDCC_MCI_POWER_SW_RST, |
| 117 | prv->base + SDCC_MCI_POWER); |
| 118 | |
| 119 | |
| 120 | /* Wait for reset to be written to register */ |
Álvaro Fernández Rojas | 918de03 | 2018-01-23 17:14:55 +0100 | [diff] [blame] | 121 | if (wait_for_bit_le32(prv->base + SDCC_MCI_STATUS2, |
| 122 | SDCC_MCI_STATUS2_MCI_ACT, false, 10, false)) { |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 123 | printf("msm_sdhci: reset request failed\n"); |
| 124 | return -EIO; |
| 125 | } |
| 126 | |
| 127 | /* SW reset can take upto 10HCLK + 15MCLK cycles. (min 40us) */ |
Álvaro Fernández Rojas | 918de03 | 2018-01-23 17:14:55 +0100 | [diff] [blame] | 128 | if (wait_for_bit_le32(prv->base + SDCC_MCI_POWER, |
| 129 | SDCC_MCI_POWER_SW_RST, false, 2, false)) { |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 130 | printf("msm_sdhci: stuck in reset\n"); |
| 131 | return -ETIMEDOUT; |
| 132 | } |
| 133 | |
| 134 | /* Enable host-controller mode */ |
| 135 | writel(1, prv->base + SDCC_MCI_HC_MODE); |
| 136 | |
Sumit Garg | 1e2dc03 | 2022-07-12 12:42:09 +0530 | [diff] [blame] | 137 | return 0; |
| 138 | } |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 139 | |
Sumit Garg | 1e2dc03 | 2022-07-12 12:42:09 +0530 | [diff] [blame] | 140 | static int msm_sdc_probe(struct udevice *dev) |
| 141 | { |
| 142 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
| 143 | struct msm_sdhc_plat *plat = dev_get_plat(dev); |
| 144 | struct msm_sdhc *prv = dev_get_priv(dev); |
| 145 | const struct msm_sdhc_variant_info *var_info; |
| 146 | struct sdhci_host *host = &prv->host; |
| 147 | u32 core_version, core_minor, core_major; |
| 148 | u32 caps; |
| 149 | int ret; |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 150 | |
Sumit Garg | 1e2dc03 | 2022-07-12 12:42:09 +0530 | [diff] [blame] | 151 | host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD | SDHCI_QUIRK_BROKEN_R1B; |
| 152 | |
| 153 | host->max_clk = 0; |
| 154 | |
| 155 | /* Init clocks */ |
| 156 | ret = msm_sdc_clk_init(dev); |
| 157 | if (ret) |
| 158 | return ret; |
| 159 | |
| 160 | var_info = (void *)dev_get_driver_data(dev); |
| 161 | if (!var_info->mci_removed) { |
| 162 | ret = msm_sdc_mci_init(prv); |
| 163 | if (ret) |
| 164 | return ret; |
| 165 | } |
| 166 | |
| 167 | if (!var_info->mci_removed) |
| 168 | core_version = readl(prv->base + SDCC_MCI_VERSION); |
| 169 | else |
| 170 | core_version = readl(host->ioaddr + SDCC_V5_VERSION); |
| 171 | |
| 172 | core_major = (core_version & SDCC_VERSION_MAJOR_MASK); |
| 173 | core_major >>= SDCC_VERSION_MAJOR_SHIFT; |
| 174 | |
| 175 | core_minor = core_version & SDCC_VERSION_MINOR_MASK; |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 176 | |
| 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; |
| 184 | writel(caps, host->ioaddr + SDHCI_VENDOR_SPEC_CAPABILITIES0); |
| 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 */ |
| 210 | if (!var_info->mci_removed) |
| 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 | { |
| 220 | struct udevice *parent = dev->parent; |
| 221 | struct msm_sdhc *priv = dev_get_priv(dev); |
| 222 | struct sdhci_host *host = &priv->host; |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 223 | int node = dev_of_offset(dev); |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 224 | |
| 225 | host->name = strdup(dev->name); |
Masahiro Yamada | 1096ae1 | 2020-07-17 14:36:46 +0900 | [diff] [blame] | 226 | host->ioaddr = dev_read_addr_ptr(dev); |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 227 | host->bus_width = fdtdec_get_int(gd->fdt_blob, node, "bus-width", 4); |
| 228 | host->index = fdtdec_get_uint(gd->fdt_blob, node, "index", 0); |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 229 | priv->base = (void *)fdtdec_get_addr_size_auto_parent(gd->fdt_blob, |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 230 | dev_of_offset(parent), node, "reg", 1, NULL, false); |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 231 | if (priv->base == (void *)FDT_ADDR_T_NONE || |
| 232 | host->ioaddr == (void *)FDT_ADDR_T_NONE) |
| 233 | return -EINVAL; |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
Simon Glass | 8ef0765 | 2016-06-12 23:30:29 -0600 | [diff] [blame] | 238 | static int msm_sdc_bind(struct udevice *dev) |
| 239 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 240 | struct msm_sdhc_plat *plat = dev_get_plat(dev); |
Simon Glass | 8ef0765 | 2016-06-12 23:30:29 -0600 | [diff] [blame] | 241 | |
Masahiro Yamada | cdb67f3 | 2016-09-06 22:17:32 +0900 | [diff] [blame] | 242 | return sdhci_bind(dev, &plat->mmc, &plat->cfg); |
Simon Glass | 8ef0765 | 2016-06-12 23:30:29 -0600 | [diff] [blame] | 243 | } |
| 244 | |
Sumit Garg | 1e2dc03 | 2022-07-12 12:42:09 +0530 | [diff] [blame] | 245 | static const struct msm_sdhc_variant_info msm_sdhc_mci_var = { |
| 246 | .mci_removed = false, |
| 247 | }; |
| 248 | |
| 249 | static const struct msm_sdhc_variant_info msm_sdhc_v5_var = { |
| 250 | .mci_removed = true, |
| 251 | }; |
| 252 | |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 253 | static const struct udevice_id msm_mmc_ids[] = { |
Sumit Garg | 1e2dc03 | 2022-07-12 12:42:09 +0530 | [diff] [blame] | 254 | { .compatible = "qcom,sdhci-msm-v4", .data = (ulong)&msm_sdhc_mci_var }, |
| 255 | { .compatible = "qcom,sdhci-msm-v5", .data = (ulong)&msm_sdhc_v5_var }, |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 256 | { } |
| 257 | }; |
| 258 | |
| 259 | U_BOOT_DRIVER(msm_sdc_drv) = { |
| 260 | .name = "msm_sdc", |
| 261 | .id = UCLASS_MMC, |
| 262 | .of_match = msm_mmc_ids, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 263 | .of_to_plat = msm_of_to_plat, |
Simon Glass | 8ef0765 | 2016-06-12 23:30:29 -0600 | [diff] [blame] | 264 | .ops = &sdhci_ops, |
Simon Glass | 8ef0765 | 2016-06-12 23:30:29 -0600 | [diff] [blame] | 265 | .bind = msm_sdc_bind, |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 266 | .probe = msm_sdc_probe, |
| 267 | .remove = msm_sdc_remove, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 268 | .priv_auto = sizeof(struct msm_sdhc), |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 269 | .plat_auto = sizeof(struct msm_sdhc_plat), |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 270 | }; |