blob: 4ce0de6c47d869f7b4f409b6e27b39903111784e [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
117
118 /* Wait for reset to be written to register */
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100119 if (wait_for_bit_le32(prv->base + SDCC_MCI_STATUS2,
120 SDCC_MCI_STATUS2_MCI_ACT, false, 10, false)) {
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200121 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 Rojas918de032018-01-23 17:14:55 +0100126 if (wait_for_bit_le32(prv->base + SDCC_MCI_POWER,
127 SDCC_MCI_POWER_SW_RST, false, 2, false)) {
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200128 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 Garg1e2dc032022-07-12 12:42:09 +0530135 return 0;
136}
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200137
Sumit Garg1e2dc032022-07-12 12:42:09 +0530138static 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 Kulikowskia00b0c02016-03-31 23:12:16 +0200148
Sumit Garg1e2dc032022-07-12 12:42:09 +0530149 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 Kulikowskia00b0c02016-03-31 23:12:16 +0200174
Caleb Connolly790d4122024-04-09 20:03:02 +0200175 log_debug("SDCC version %d.%d\n", core_major, core_minor);
176
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200177 /*
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 Glass8ef07652016-06-12 23:30:29 -0600182 caps = readl(host->ioaddr + SDHCI_CAPABILITIES);
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200183 caps |= SDHCI_CAN_VDD_300 | SDHCI_CAN_DO_8BIT;
Caleb Connollyc1f71d22024-04-09 20:03:00 +0200184 writel(caps, host->ioaddr + var_info->core_vendor_spec_capabilities0);
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200185 }
186
Manivannan Sadhasivam6b36ab52020-07-16 14:37:26 +0530187 ret = mmc_of_parse(dev, &plat->cfg);
188 if (ret)
189 return ret;
190
Simon Glass8ef07652016-06-12 23:30:29 -0600191 host->mmc = &plat->mmc;
Peng Fanf92f7b62019-08-06 02:47:53 +0000192 host->mmc->dev = dev;
193 ret = sdhci_setup_cfg(&plat->cfg, host, 0, 0);
Mateusz Kulikowskic012e572016-06-26 22:43:55 +0200194 if (ret)
195 return ret;
Simon Glass8ef07652016-06-12 23:30:29 -0600196 host->mmc->priv = &prv->host;
Simon Glass8ef07652016-06-12 23:30:29 -0600197 upriv->mmc = host->mmc;
Mateusz Kulikowskic012e572016-06-26 22:43:55 +0200198
Simon Glass8ef07652016-06-12 23:30:29 -0600199 return sdhci_probe(dev);
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200200}
201
202static int msm_sdc_remove(struct udevice *dev)
203{
204 struct msm_sdhc *priv = dev_get_priv(dev);
Sumit Garg1e2dc032022-07-12 12:42:09 +0530205 const struct msm_sdhc_variant_info *var_info;
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200206
Sumit Garg1e2dc032022-07-12 12:42:09 +0530207 var_info = (void *)dev_get_driver_data(dev);
208
209 /* Disable host-controller mode */
Caleb Connolly6d32da32024-04-09 20:03:01 +0200210 if (!var_info->mci_removed && priv->base)
Sumit Garg1e2dc032022-07-12 12:42:09 +0530211 writel(0, priv->base + SDCC_MCI_HC_MODE);
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200212
Caleb Connollyfb782f52024-02-26 17:26:07 +0000213 clk_release_bulk(&priv->clks);
214
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200215 return 0;
216}
217
Simon Glassaad29ae2020-12-03 16:55:21 -0700218static int msm_of_to_plat(struct udevice *dev)
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200219{
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200220 struct msm_sdhc *priv = dev_get_priv(dev);
Caleb Connolly6d32da32024-04-09 20:03:01 +0200221 const struct msm_sdhc_variant_info *var_info;
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200222 struct sdhci_host *host = &priv->host;
Caleb Connolly6d32da32024-04-09 20:03:01 +0200223 int ret;
224
225 var_info = (void*)dev_get_driver_data(dev);
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200226
227 host->name = strdup(dev->name);
Masahiro Yamada1096ae12020-07-17 14:36:46 +0900228 host->ioaddr = dev_read_addr_ptr(dev);
Caleb Connolly6d32da32024-04-09 20:03:01 +0200229 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 Kulikowskia00b0c02016-03-31 23:12:16 +0200242 return -EINVAL;
Caleb Connolly6d32da32024-04-09 20:03:01 +0200243 }
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200244
245 return 0;
246}
247
Simon Glass8ef07652016-06-12 23:30:29 -0600248static int msm_sdc_bind(struct udevice *dev)
249{
Simon Glassfa20e932020-12-03 16:55:20 -0700250 struct msm_sdhc_plat *plat = dev_get_plat(dev);
Simon Glass8ef07652016-06-12 23:30:29 -0600251
Masahiro Yamadacdb67f32016-09-06 22:17:32 +0900252 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
Simon Glass8ef07652016-06-12 23:30:29 -0600253}
254
Sumit Garg1e2dc032022-07-12 12:42:09 +0530255static const struct msm_sdhc_variant_info msm_sdhc_mci_var = {
256 .mci_removed = false,
Caleb Connollyc1f71d22024-04-09 20:03:00 +0200257
Caleb Connolly5d8b5752024-04-12 20:10:21 +0200258 .core_vendor_spec_capabilities0 = 0x11c,
Sumit Garg1e2dc032022-07-12 12:42:09 +0530259};
260
261static const struct msm_sdhc_variant_info msm_sdhc_v5_var = {
262 .mci_removed = true,
Caleb Connollyc1f71d22024-04-09 20:03:00 +0200263
Caleb Connolly5d8b5752024-04-12 20:10:21 +0200264 .core_vendor_spec_capabilities0 = 0x21c,
Sumit Garg1e2dc032022-07-12 12:42:09 +0530265};
266
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200267static const struct udevice_id msm_mmc_ids[] = {
Sumit Garg1e2dc032022-07-12 12:42:09 +0530268 { .compatible = "qcom,sdhci-msm-v4", .data = (ulong)&msm_sdhc_mci_var },
269 { .compatible = "qcom,sdhci-msm-v5", .data = (ulong)&msm_sdhc_v5_var },
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200270 { }
271};
272
273U_BOOT_DRIVER(msm_sdc_drv) = {
274 .name = "msm_sdc",
275 .id = UCLASS_MMC,
276 .of_match = msm_mmc_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700277 .of_to_plat = msm_of_to_plat,
Simon Glass8ef07652016-06-12 23:30:29 -0600278 .ops = &sdhci_ops,
Simon Glass8ef07652016-06-12 23:30:29 -0600279 .bind = msm_sdc_bind,
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200280 .probe = msm_sdc_probe,
281 .remove = msm_sdc_remove,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700282 .priv_auto = sizeof(struct msm_sdhc),
Simon Glass71fa5b42020-12-03 16:55:18 -0700283 .plat_auto = sizeof(struct msm_sdhc_plat),
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200284};