blob: f5e9930c7997afb50ec7c193b655b33bedea36b7 [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
Simon Glass8ef07652016-06-12 23:30:29 -060035struct msm_sdhc_plat {
36 struct mmc_config cfg;
37 struct mmc mmc;
38};
39
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +020040struct msm_sdhc {
41 struct sdhci_host host;
42 void *base;
Caleb Connollyfb782f52024-02-26 17:26:07 +000043 struct clk_bulk clks;
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +020044};
45
Sumit Garg1e2dc032022-07-12 12:42:09 +053046struct msm_sdhc_variant_info {
47 bool mci_removed;
Caleb Connollyc1f71d22024-04-09 20:03:00 +020048
49 u32 core_vendor_spec_capabilities0;
Sumit Garg1e2dc032022-07-12 12:42:09 +053050};
51
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +020052DECLARE_GLOBAL_DATA_PTR;
53
54static int msm_sdc_clk_init(struct udevice *dev)
55{
Caleb Connollyfb782f52024-02-26 17:26:07 +000056 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 Kulikowskia00b0c02016-03-31 23:12:16 +020061
Caleb Connollyfb782f52024-02-26 17:26:07 +000062 ret = ofnode_read_u32(node, "clock-frequency", (uint *)(&clk_rate));
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +020063 if (ret)
Caleb Connolly66dfa562024-04-09 20:03:03 +020064 clk_rate = 201500000;
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +020065
Caleb Connollyfb782f52024-02-26 17:26:07 +000066 ret = clk_get_bulk(dev, &prv->clks);
67 if (ret) {
68 log_warning("Couldn't get mmc clocks: %d\n", ret);
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +020069 return ret;
Caleb Connollyfb782f52024-02-26 17:26:07 +000070 }
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +020071
Caleb Connollyfb782f52024-02-26 17:26:07 +000072 ret = clk_enable_bulk(&prv->clks);
73 if (ret) {
74 log_warning("Couldn't enable mmc clocks: %d\n", ret);
Stephen Warrena9622432016-06-17 09:44:00 -060075 return ret;
Caleb Connollyfb782f52024-02-26 17:26:07 +000076 }
Stephen Warrena9622432016-06-17 09:44:00 -060077
Caleb Connollyfb782f52024-02-26 17:26:07 +000078 /* 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 Kulikowskia00b0c02016-03-31 23:12:16 +0200107
108 return 0;
109}
110
Sumit Garg1e2dc032022-07-12 12:42:09 +0530111static int msm_sdc_mci_init(struct msm_sdhc *prv)
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200112{
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200113 /* 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
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200117 /* Wait for reset to be written to register */
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100118 if (wait_for_bit_le32(prv->base + SDCC_MCI_STATUS2,
119 SDCC_MCI_STATUS2_MCI_ACT, false, 10, false)) {
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200120 printf("msm_sdhci: reset request failed\n");
121 return -EIO;
122 }
123
124 /* SW reset can take upto 10HCLK + 15MCLK cycles. (min 40us) */
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100125 if (wait_for_bit_le32(prv->base + SDCC_MCI_POWER,
126 SDCC_MCI_POWER_SW_RST, false, 2, false)) {
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200127 printf("msm_sdhci: stuck in reset\n");
128 return -ETIMEDOUT;
129 }
130
131 /* Enable host-controller mode */
132 writel(1, prv->base + SDCC_MCI_HC_MODE);
133
Sumit Garg1e2dc032022-07-12 12:42:09 +0530134 return 0;
135}
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200136
Sumit Garg1e2dc032022-07-12 12:42:09 +0530137static int msm_sdc_probe(struct udevice *dev)
138{
139 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
140 struct msm_sdhc_plat *plat = dev_get_plat(dev);
141 struct msm_sdhc *prv = dev_get_priv(dev);
142 const struct msm_sdhc_variant_info *var_info;
143 struct sdhci_host *host = &prv->host;
144 u32 core_version, core_minor, core_major;
145 u32 caps;
146 int ret;
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200147
Sumit Garg1e2dc032022-07-12 12:42:09 +0530148 host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD | SDHCI_QUIRK_BROKEN_R1B;
149
150 host->max_clk = 0;
151
152 /* Init clocks */
153 ret = msm_sdc_clk_init(dev);
154 if (ret)
155 return ret;
156
157 var_info = (void *)dev_get_driver_data(dev);
158 if (!var_info->mci_removed) {
159 ret = msm_sdc_mci_init(prv);
160 if (ret)
161 return ret;
162 }
163
164 if (!var_info->mci_removed)
165 core_version = readl(prv->base + SDCC_MCI_VERSION);
166 else
167 core_version = readl(host->ioaddr + SDCC_V5_VERSION);
168
169 core_major = (core_version & SDCC_VERSION_MAJOR_MASK);
170 core_major >>= SDCC_VERSION_MAJOR_SHIFT;
171
172 core_minor = core_version & SDCC_VERSION_MINOR_MASK;
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200173
Caleb Connolly790d4122024-04-09 20:03:02 +0200174 log_debug("SDCC version %d.%d\n", core_major, core_minor);
175
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200176 /*
177 * Support for some capabilities is not advertised by newer
178 * controller versions and must be explicitly enabled.
179 */
180 if (core_major >= 1 && core_minor != 0x11 && core_minor != 0x12) {
Simon Glass8ef07652016-06-12 23:30:29 -0600181 caps = readl(host->ioaddr + SDHCI_CAPABILITIES);
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200182 caps |= SDHCI_CAN_VDD_300 | SDHCI_CAN_DO_8BIT;
Caleb Connollyc1f71d22024-04-09 20:03:00 +0200183 writel(caps, host->ioaddr + var_info->core_vendor_spec_capabilities0);
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200184 }
185
Manivannan Sadhasivam6b36ab52020-07-16 14:37:26 +0530186 ret = mmc_of_parse(dev, &plat->cfg);
187 if (ret)
188 return ret;
189
Simon Glass8ef07652016-06-12 23:30:29 -0600190 host->mmc = &plat->mmc;
Peng Fanf92f7b62019-08-06 02:47:53 +0000191 host->mmc->dev = dev;
192 ret = sdhci_setup_cfg(&plat->cfg, host, 0, 0);
Mateusz Kulikowskic012e572016-06-26 22:43:55 +0200193 if (ret)
194 return ret;
Simon Glass8ef07652016-06-12 23:30:29 -0600195 host->mmc->priv = &prv->host;
Simon Glass8ef07652016-06-12 23:30:29 -0600196 upriv->mmc = host->mmc;
Mateusz Kulikowskic012e572016-06-26 22:43:55 +0200197
Simon Glass8ef07652016-06-12 23:30:29 -0600198 return sdhci_probe(dev);
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200199}
200
201static int msm_sdc_remove(struct udevice *dev)
202{
203 struct msm_sdhc *priv = dev_get_priv(dev);
Sumit Garg1e2dc032022-07-12 12:42:09 +0530204 const struct msm_sdhc_variant_info *var_info;
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200205
Sumit Garg1e2dc032022-07-12 12:42:09 +0530206 var_info = (void *)dev_get_driver_data(dev);
207
208 /* Disable host-controller mode */
Caleb Connolly6d32da32024-04-09 20:03:01 +0200209 if (!var_info->mci_removed && priv->base)
Sumit Garg1e2dc032022-07-12 12:42:09 +0530210 writel(0, priv->base + SDCC_MCI_HC_MODE);
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200211
Caleb Connollyfb782f52024-02-26 17:26:07 +0000212 clk_release_bulk(&priv->clks);
213
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200214 return 0;
215}
216
Simon Glassaad29ae2020-12-03 16:55:21 -0700217static int msm_of_to_plat(struct udevice *dev)
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200218{
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200219 struct msm_sdhc *priv = dev_get_priv(dev);
Caleb Connolly6d32da32024-04-09 20:03:01 +0200220 const struct msm_sdhc_variant_info *var_info;
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200221 struct sdhci_host *host = &priv->host;
Caleb Connolly6d32da32024-04-09 20:03:01 +0200222 int ret;
223
224 var_info = (void*)dev_get_driver_data(dev);
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200225
226 host->name = strdup(dev->name);
Masahiro Yamada1096ae12020-07-17 14:36:46 +0900227 host->ioaddr = dev_read_addr_ptr(dev);
Caleb Connolly6d32da32024-04-09 20:03:01 +0200228 ret = dev_read_u32(dev, "bus-width", &host->bus_width);
229 if (ret)
230 host->bus_width = 4;
231 ret = dev_read_u32(dev, "index", &host->index);
232 if (ret)
233 host->index = 0;
234 priv->base = dev_read_addr_index_ptr(dev, 1);
235
236 if (!host->ioaddr)
237 return -EINVAL;
238
239 if (!var_info->mci_removed && !priv->base) {
240 printf("msm_sdhci: MCI base address not found\n");
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200241 return -EINVAL;
Caleb Connolly6d32da32024-04-09 20:03:01 +0200242 }
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200243
244 return 0;
245}
246
Simon Glass8ef07652016-06-12 23:30:29 -0600247static int msm_sdc_bind(struct udevice *dev)
248{
Simon Glassfa20e932020-12-03 16:55:20 -0700249 struct msm_sdhc_plat *plat = dev_get_plat(dev);
Simon Glass8ef07652016-06-12 23:30:29 -0600250
Masahiro Yamadacdb67f32016-09-06 22:17:32 +0900251 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
Simon Glass8ef07652016-06-12 23:30:29 -0600252}
253
Sumit Garg1e2dc032022-07-12 12:42:09 +0530254static const struct msm_sdhc_variant_info msm_sdhc_mci_var = {
255 .mci_removed = false,
Caleb Connollyc1f71d22024-04-09 20:03:00 +0200256
Caleb Connolly5d8b5752024-04-12 20:10:21 +0200257 .core_vendor_spec_capabilities0 = 0x11c,
Sumit Garg1e2dc032022-07-12 12:42:09 +0530258};
259
260static const struct msm_sdhc_variant_info msm_sdhc_v5_var = {
261 .mci_removed = true,
Caleb Connollyc1f71d22024-04-09 20:03:00 +0200262
Caleb Connolly5d8b5752024-04-12 20:10:21 +0200263 .core_vendor_spec_capabilities0 = 0x21c,
Sumit Garg1e2dc032022-07-12 12:42:09 +0530264};
265
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200266static const struct udevice_id msm_mmc_ids[] = {
Sumit Garg1e2dc032022-07-12 12:42:09 +0530267 { .compatible = "qcom,sdhci-msm-v4", .data = (ulong)&msm_sdhc_mci_var },
268 { .compatible = "qcom,sdhci-msm-v5", .data = (ulong)&msm_sdhc_v5_var },
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200269 { }
270};
271
272U_BOOT_DRIVER(msm_sdc_drv) = {
273 .name = "msm_sdc",
274 .id = UCLASS_MMC,
275 .of_match = msm_mmc_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700276 .of_to_plat = msm_of_to_plat,
Simon Glass8ef07652016-06-12 23:30:29 -0600277 .ops = &sdhci_ops,
Simon Glass8ef07652016-06-12 23:30:29 -0600278 .bind = msm_sdc_bind,
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200279 .probe = msm_sdc_probe,
280 .remove = msm_sdc_remove,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700281 .priv_auto = sizeof(struct msm_sdhc),
Simon Glass71fa5b42020-12-03 16:55:18 -0700282 .plat_auto = sizeof(struct msm_sdhc_plat),
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200283};