blob: 4e5c932c071f6bcb9ab9918d0ab6916d89a2d14e [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +02002/*
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 Kulikowskia00b0c02016-03-31 23:12:16 +02008 */
9
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +020010#include <clk.h>
11#include <dm.h>
Simon Glass9bc15642020-02-03 07:36:16 -070012#include <malloc.h>
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +020013#include <sdhci.h>
14#include <wait_bit.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060015#include <asm/global_data.h>
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +020016#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 Garg1e2dc032022-07-12 12:42:09 +053024#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 Kulikowskia00b0c02016-03-31 23:12:16 +020030
31#define SDCC_MCI_STATUS2 0x6C
32#define SDCC_MCI_STATUS2_MCI_ACT 0x1
33#define SDCC_MCI_HC_MODE 0x78
34
Caleb Connolly3459a452024-06-21 03:53:09 +020035#define CORE_VENDOR_SPEC_POR_VAL 0xa9c
36
Simon Glass8ef07652016-06-12 23:30:29 -060037struct msm_sdhc_plat {
38 struct mmc_config cfg;
39 struct mmc mmc;
40};
41
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +020042struct msm_sdhc {
43 struct sdhci_host host;
44 void *base;
Caleb Connollyfb782f52024-02-26 17:26:07 +000045 struct clk_bulk clks;
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +020046};
47
Sumit Garg1e2dc032022-07-12 12:42:09 +053048struct msm_sdhc_variant_info {
49 bool mci_removed;
Caleb Connollyc1f71d22024-04-09 20:03:00 +020050
Caleb Connolly3459a452024-06-21 03:53:09 +020051 u32 core_vendor_spec;
Caleb Connollyc1f71d22024-04-09 20:03:00 +020052 u32 core_vendor_spec_capabilities0;
Sumit Garg1e2dc032022-07-12 12:42:09 +053053};
54
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +020055DECLARE_GLOBAL_DATA_PTR;
56
57static int msm_sdc_clk_init(struct udevice *dev)
58{
Caleb Connollyfb782f52024-02-26 17:26:07 +000059 struct msm_sdhc *prv = dev_get_priv(dev);
Caleb Connolly3459a452024-06-21 03:53:09 +020060 const struct msm_sdhc_variant_info *var_info;
Caleb Connollyfb782f52024-02-26 17:26:07 +000061 ofnode node = dev_ofnode(dev);
62 ulong clk_rate;
63 int ret, i = 0, n_clks;
64 const char *clk_name;
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +020065
Caleb Connolly3459a452024-06-21 03:53:09 +020066 var_info = (void *)dev_get_driver_data(dev);
67
Caleb Connollyfb782f52024-02-26 17:26:07 +000068 ret = ofnode_read_u32(node, "clock-frequency", (uint *)(&clk_rate));
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +020069 if (ret)
Caleb Connolly66dfa562024-04-09 20:03:03 +020070 clk_rate = 201500000;
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +020071
Caleb Connollyfb782f52024-02-26 17:26:07 +000072 ret = clk_get_bulk(dev, &prv->clks);
73 if (ret) {
74 log_warning("Couldn't get mmc clocks: %d\n", ret);
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +020075 return ret;
Caleb Connollyfb782f52024-02-26 17:26:07 +000076 }
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +020077
Caleb Connollyfb782f52024-02-26 17:26:07 +000078 ret = clk_enable_bulk(&prv->clks);
79 if (ret) {
80 log_warning("Couldn't enable mmc clocks: %d\n", ret);
Stephen Warrena9622432016-06-17 09:44:00 -060081 return ret;
Caleb Connollyfb782f52024-02-26 17:26:07 +000082 }
Stephen Warrena9622432016-06-17 09:44:00 -060083
Caleb Connollyfb782f52024-02-26 17:26:07 +000084 /* If clock-names is unspecified, then the first clock is the core clock */
85 if (!ofnode_get_property(node, "clock-names", &n_clks)) {
86 if (!clk_set_rate(&prv->clks.clks[0], clk_rate)) {
87 log_warning("Couldn't set core clock rate: %d\n", ret);
88 return -EINVAL;
89 }
90 }
91
92 /* Find the index of the "core" clock */
93 while (i < n_clks) {
94 ofnode_read_string_index(node, "clock-names", i, &clk_name);
95 if (!strcmp(clk_name, "core"))
96 break;
97 i++;
98 }
99
100 if (i >= prv->clks.count) {
101 log_warning("Couldn't find core clock (index %d but only have %d clocks)\n", i,
102 prv->clks.count);
103 return -EINVAL;
104 }
105
106 /* The clock is already enabled by the clk_bulk above */
107 clk_rate = clk_set_rate(&prv->clks.clks[i], clk_rate);
108 /* If we get a rate of 0 then something has probably gone wrong. */
109 if (clk_rate == 0 || IS_ERR((void *)clk_rate)) {
110 log_warning("Couldn't set MMC core clock rate: %dE\n", clk_rate ? (int)PTR_ERR((void *)clk_rate) : 0);
111 return -EINVAL;
112 }
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200113
Caleb Connolly3459a452024-06-21 03:53:09 +0200114 writel_relaxed(CORE_VENDOR_SPEC_POR_VAL,
115 prv->host.ioaddr + var_info->core_vendor_spec);
116
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200117 return 0;
118}
119
Sumit Garg1e2dc032022-07-12 12:42:09 +0530120static int msm_sdc_mci_init(struct msm_sdhc *prv)
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200121{
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200122 /* Reset the core and Enable SDHC mode */
123 writel(readl(prv->base + SDCC_MCI_POWER) | SDCC_MCI_POWER_SW_RST,
124 prv->base + SDCC_MCI_POWER);
125
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200126 /* Wait for reset to be written to register */
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100127 if (wait_for_bit_le32(prv->base + SDCC_MCI_STATUS2,
128 SDCC_MCI_STATUS2_MCI_ACT, false, 10, false)) {
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200129 printf("msm_sdhci: reset request failed\n");
130 return -EIO;
131 }
132
133 /* SW reset can take upto 10HCLK + 15MCLK cycles. (min 40us) */
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100134 if (wait_for_bit_le32(prv->base + SDCC_MCI_POWER,
135 SDCC_MCI_POWER_SW_RST, false, 2, false)) {
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200136 printf("msm_sdhci: stuck in reset\n");
137 return -ETIMEDOUT;
138 }
139
140 /* Enable host-controller mode */
141 writel(1, prv->base + SDCC_MCI_HC_MODE);
142
Sumit Garg1e2dc032022-07-12 12:42:09 +0530143 return 0;
144}
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200145
Sumit Garg1e2dc032022-07-12 12:42:09 +0530146static int msm_sdc_probe(struct udevice *dev)
147{
148 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
149 struct msm_sdhc_plat *plat = dev_get_plat(dev);
150 struct msm_sdhc *prv = dev_get_priv(dev);
151 const struct msm_sdhc_variant_info *var_info;
152 struct sdhci_host *host = &prv->host;
153 u32 core_version, core_minor, core_major;
154 u32 caps;
155 int ret;
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200156
Sumit Garg1e2dc032022-07-12 12:42:09 +0530157 host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD | SDHCI_QUIRK_BROKEN_R1B;
158
159 host->max_clk = 0;
160
161 /* Init clocks */
162 ret = msm_sdc_clk_init(dev);
163 if (ret)
164 return ret;
165
166 var_info = (void *)dev_get_driver_data(dev);
167 if (!var_info->mci_removed) {
168 ret = msm_sdc_mci_init(prv);
169 if (ret)
170 return ret;
171 }
172
173 if (!var_info->mci_removed)
174 core_version = readl(prv->base + SDCC_MCI_VERSION);
175 else
176 core_version = readl(host->ioaddr + SDCC_V5_VERSION);
177
178 core_major = (core_version & SDCC_VERSION_MAJOR_MASK);
179 core_major >>= SDCC_VERSION_MAJOR_SHIFT;
180
181 core_minor = core_version & SDCC_VERSION_MINOR_MASK;
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200182
Caleb Connolly790d4122024-04-09 20:03:02 +0200183 log_debug("SDCC version %d.%d\n", core_major, core_minor);
184
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200185 /*
186 * Support for some capabilities is not advertised by newer
187 * controller versions and must be explicitly enabled.
188 */
189 if (core_major >= 1 && core_minor != 0x11 && core_minor != 0x12) {
Simon Glass8ef07652016-06-12 23:30:29 -0600190 caps = readl(host->ioaddr + SDHCI_CAPABILITIES);
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200191 caps |= SDHCI_CAN_VDD_300 | SDHCI_CAN_DO_8BIT;
Caleb Connollyc1f71d22024-04-09 20:03:00 +0200192 writel(caps, host->ioaddr + var_info->core_vendor_spec_capabilities0);
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200193 }
194
Manivannan Sadhasivam6b36ab52020-07-16 14:37:26 +0530195 ret = mmc_of_parse(dev, &plat->cfg);
196 if (ret)
197 return ret;
198
Simon Glass8ef07652016-06-12 23:30:29 -0600199 host->mmc = &plat->mmc;
Peng Fanf92f7b62019-08-06 02:47:53 +0000200 host->mmc->dev = dev;
201 ret = sdhci_setup_cfg(&plat->cfg, host, 0, 0);
Mateusz Kulikowskic012e572016-06-26 22:43:55 +0200202 if (ret)
203 return ret;
Simon Glass8ef07652016-06-12 23:30:29 -0600204 host->mmc->priv = &prv->host;
Simon Glass8ef07652016-06-12 23:30:29 -0600205 upriv->mmc = host->mmc;
Mateusz Kulikowskic012e572016-06-26 22:43:55 +0200206
Simon Glass8ef07652016-06-12 23:30:29 -0600207 return sdhci_probe(dev);
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200208}
209
210static int msm_sdc_remove(struct udevice *dev)
211{
212 struct msm_sdhc *priv = dev_get_priv(dev);
Sumit Garg1e2dc032022-07-12 12:42:09 +0530213 const struct msm_sdhc_variant_info *var_info;
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200214
Sumit Garg1e2dc032022-07-12 12:42:09 +0530215 var_info = (void *)dev_get_driver_data(dev);
216
217 /* Disable host-controller mode */
Caleb Connolly6d32da32024-04-09 20:03:01 +0200218 if (!var_info->mci_removed && priv->base)
Sumit Garg1e2dc032022-07-12 12:42:09 +0530219 writel(0, priv->base + SDCC_MCI_HC_MODE);
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200220
Caleb Connollyfb782f52024-02-26 17:26:07 +0000221 clk_release_bulk(&priv->clks);
222
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200223 return 0;
224}
225
Simon Glassaad29ae2020-12-03 16:55:21 -0700226static int msm_of_to_plat(struct udevice *dev)
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200227{
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200228 struct msm_sdhc *priv = dev_get_priv(dev);
Caleb Connolly6d32da32024-04-09 20:03:01 +0200229 const struct msm_sdhc_variant_info *var_info;
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200230 struct sdhci_host *host = &priv->host;
Caleb Connolly6d32da32024-04-09 20:03:01 +0200231 int ret;
232
233 var_info = (void*)dev_get_driver_data(dev);
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200234
235 host->name = strdup(dev->name);
Masahiro Yamada1096ae12020-07-17 14:36:46 +0900236 host->ioaddr = dev_read_addr_ptr(dev);
Caleb Connolly6d32da32024-04-09 20:03:01 +0200237 ret = dev_read_u32(dev, "bus-width", &host->bus_width);
238 if (ret)
239 host->bus_width = 4;
240 ret = dev_read_u32(dev, "index", &host->index);
241 if (ret)
242 host->index = 0;
243 priv->base = dev_read_addr_index_ptr(dev, 1);
244
245 if (!host->ioaddr)
246 return -EINVAL;
247
248 if (!var_info->mci_removed && !priv->base) {
249 printf("msm_sdhci: MCI base address not found\n");
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200250 return -EINVAL;
Caleb Connolly6d32da32024-04-09 20:03:01 +0200251 }
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200252
253 return 0;
254}
255
Simon Glass8ef07652016-06-12 23:30:29 -0600256static int msm_sdc_bind(struct udevice *dev)
257{
Simon Glassfa20e932020-12-03 16:55:20 -0700258 struct msm_sdhc_plat *plat = dev_get_plat(dev);
Simon Glass8ef07652016-06-12 23:30:29 -0600259
Masahiro Yamadacdb67f32016-09-06 22:17:32 +0900260 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
Simon Glass8ef07652016-06-12 23:30:29 -0600261}
262
Sumit Garg1e2dc032022-07-12 12:42:09 +0530263static const struct msm_sdhc_variant_info msm_sdhc_mci_var = {
264 .mci_removed = false,
Caleb Connollyc1f71d22024-04-09 20:03:00 +0200265
Caleb Connolly3459a452024-06-21 03:53:09 +0200266 .core_vendor_spec = 0x10c,
Caleb Connolly5d8b5752024-04-12 20:10:21 +0200267 .core_vendor_spec_capabilities0 = 0x11c,
Sumit Garg1e2dc032022-07-12 12:42:09 +0530268};
269
270static const struct msm_sdhc_variant_info msm_sdhc_v5_var = {
271 .mci_removed = true,
Caleb Connollyc1f71d22024-04-09 20:03:00 +0200272
Caleb Connolly3459a452024-06-21 03:53:09 +0200273 .core_vendor_spec = 0x20c,
Caleb Connolly5d8b5752024-04-12 20:10:21 +0200274 .core_vendor_spec_capabilities0 = 0x21c,
Sumit Garg1e2dc032022-07-12 12:42:09 +0530275};
276
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200277static const struct udevice_id msm_mmc_ids[] = {
Sumit Garg1e2dc032022-07-12 12:42:09 +0530278 { .compatible = "qcom,sdhci-msm-v4", .data = (ulong)&msm_sdhc_mci_var },
279 { .compatible = "qcom,sdhci-msm-v5", .data = (ulong)&msm_sdhc_v5_var },
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200280 { }
281};
282
283U_BOOT_DRIVER(msm_sdc_drv) = {
284 .name = "msm_sdc",
285 .id = UCLASS_MMC,
286 .of_match = msm_mmc_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700287 .of_to_plat = msm_of_to_plat,
Simon Glass8ef07652016-06-12 23:30:29 -0600288 .ops = &sdhci_ops,
Simon Glass8ef07652016-06-12 23:30:29 -0600289 .bind = msm_sdc_bind,
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200290 .probe = msm_sdc_probe,
291 .remove = msm_sdc_remove,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700292 .priv_auto = sizeof(struct msm_sdhc),
Simon Glass71fa5b42020-12-03 16:55:18 -0700293 .plat_auto = sizeof(struct msm_sdhc_plat),
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200294};