1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2013 - 2015 Xilinx, Inc.
5 * Xilinx Zynq SD Host Controller Interface
12 #include "mmc_private.h"
13 #include <linux/libfdt.h>
16 #include <zynqmp_tap_delay.h>
18 DECLARE_GLOBAL_DATA_PTR;
20 struct arasan_sdhci_plat {
21 struct mmc_config cfg;
26 struct arasan_sdhci_priv {
27 struct sdhci_host *host;
34 #if defined(CONFIG_ARCH_ZYNQMP)
35 #define MMC_HS200_BUS_SPEED 5
37 static const u8 mode2timing[] = {
38 [MMC_LEGACY] = UHS_SDR12_BUS_SPEED,
39 [SD_LEGACY] = UHS_SDR12_BUS_SPEED,
40 [MMC_HS] = HIGH_SPEED_BUS_SPEED,
41 [SD_HS] = HIGH_SPEED_BUS_SPEED,
42 [MMC_HS_52] = HIGH_SPEED_BUS_SPEED,
43 [MMC_DDR_52] = HIGH_SPEED_BUS_SPEED,
44 [UHS_SDR12] = UHS_SDR12_BUS_SPEED,
45 [UHS_SDR25] = UHS_SDR25_BUS_SPEED,
46 [UHS_SDR50] = UHS_SDR50_BUS_SPEED,
47 [UHS_DDR50] = UHS_DDR50_BUS_SPEED,
48 [UHS_SDR104] = UHS_SDR104_BUS_SPEED,
49 [MMC_HS_200] = MMC_HS200_BUS_SPEED,
52 #define SDHCI_HOST_CTRL2 0x3E
53 #define SDHCI_CTRL2_MODE_MASK 0x7
54 #define SDHCI_18V_SIGNAL 0x8
55 #define SDHCI_CTRL_EXEC_TUNING 0x0040
56 #define SDHCI_CTRL_TUNED_CLK 0x80
57 #define SDHCI_TUNING_LOOP_COUNT 40
59 static void arasan_zynqmp_dll_reset(struct sdhci_host *host, u8 deviceid)
62 unsigned long timeout;
64 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
65 clk &= ~(SDHCI_CLOCK_CARD_EN);
66 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
69 zynqmp_dll_reset(deviceid);
73 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
74 & SDHCI_CLOCK_INT_STABLE)) {
76 dev_err(mmc_dev(host->mmc),
77 ": Internal clock never stabilised.\n");
84 clk |= SDHCI_CLOCK_CARD_EN;
85 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
88 static int arasan_sdhci_execute_tuning(struct mmc *mmc, u8 opcode)
93 struct sdhci_host *host;
94 struct arasan_sdhci_priv *priv = dev_get_priv(mmc->dev);
95 char tuning_loop_counter = SDHCI_TUNING_LOOP_COUNT;
98 debug("%s\n", __func__);
101 deviceid = priv->deviceid;
103 ctrl = sdhci_readw(host, SDHCI_HOST_CTRL2);
104 ctrl |= SDHCI_CTRL_EXEC_TUNING;
105 sdhci_writew(host, ctrl, SDHCI_HOST_CTRL2);
109 arasan_zynqmp_dll_reset(host, deviceid);
111 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
112 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
116 cmd.resp_type = MMC_RSP_R1;
121 data.flags = MMC_DATA_READ;
123 if (tuning_loop_counter-- == 0)
126 if (cmd.cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200 &&
128 data.blocksize = 128;
130 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
133 sdhci_writew(host, data.blocks, SDHCI_BLOCK_COUNT);
134 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
136 mmc_send_cmd(mmc, &cmd, NULL);
137 ctrl = sdhci_readw(host, SDHCI_HOST_CTRL2);
139 if (cmd.cmdidx == MMC_CMD_SEND_TUNING_BLOCK)
142 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
144 if (tuning_loop_counter < 0) {
145 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
146 sdhci_writel(host, ctrl, SDHCI_HOST_CTRL2);
149 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
150 printf("%s:Tuning failed\n", __func__);
155 arasan_zynqmp_dll_reset(host, deviceid);
157 /* Enable only interrupts served by the SD controller */
158 sdhci_writel(host, SDHCI_INT_DATA_MASK | SDHCI_INT_CMD_MASK,
160 /* Mask all sdhci interrupt sources */
161 sdhci_writel(host, 0x0, SDHCI_SIGNAL_ENABLE);
166 static void arasan_sdhci_set_tapdelay(struct sdhci_host *host)
168 struct arasan_sdhci_priv *priv = dev_get_priv(host->mmc->dev);
169 struct mmc *mmc = (struct mmc *)host->mmc;
172 uhsmode = mode2timing[mmc->selected_mode];
174 if (uhsmode >= UHS_SDR25_BUS_SPEED)
175 arasan_zynqmp_set_tapdelay(priv->deviceid, uhsmode,
179 static void arasan_sdhci_set_control_reg(struct sdhci_host *host)
181 struct mmc *mmc = (struct mmc *)host->mmc;
187 if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
188 reg = sdhci_readw(host, SDHCI_HOST_CTRL2);
189 reg |= SDHCI_18V_SIGNAL;
190 sdhci_writew(host, reg, SDHCI_HOST_CTRL2);
193 if (mmc->selected_mode > SD_HS &&
194 mmc->selected_mode <= UHS_DDR50) {
195 reg = sdhci_readw(host, SDHCI_HOST_CTRL2);
196 reg &= ~SDHCI_CTRL2_MODE_MASK;
197 switch (mmc->selected_mode) {
199 reg |= UHS_SDR12_BUS_SPEED;
202 reg |= UHS_SDR25_BUS_SPEED;
205 reg |= UHS_SDR50_BUS_SPEED;
208 reg |= UHS_SDR104_BUS_SPEED;
211 reg |= UHS_DDR50_BUS_SPEED;
216 sdhci_writew(host, reg, SDHCI_HOST_CTRL2);
221 #if defined(CONFIG_DM_MMC) && defined(CONFIG_ARCH_ZYNQMP)
222 const struct sdhci_ops arasan_ops = {
223 .platform_execute_tuning = &arasan_sdhci_execute_tuning,
224 .set_delay = &arasan_sdhci_set_tapdelay,
225 .set_control_reg = &arasan_sdhci_set_control_reg,
229 static int arasan_sdhci_probe(struct udevice *dev)
231 struct arasan_sdhci_plat *plat = dev_get_platdata(dev);
232 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
233 struct arasan_sdhci_priv *priv = dev_get_priv(dev);
234 struct sdhci_host *host;
241 ret = clk_get_by_index(dev, 0, &clk);
243 dev_err(dev, "failed to get clock\n");
247 clock = clk_get_rate(&clk);
248 if (IS_ERR_VALUE(clock)) {
249 dev_err(dev, "failed to get rate\n");
253 debug("%s: CLK %ld\n", __func__, clock);
255 ret = clk_enable(&clk);
256 if (ret && ret != -ENOSYS) {
257 dev_err(dev, "failed to enable clock\n");
261 host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD |
262 SDHCI_QUIRK_BROKEN_R1B;
264 #ifdef CONFIG_ZYNQ_HISPD_BROKEN
265 host->quirks |= SDHCI_QUIRK_BROKEN_HISPD_MODE;
269 host->quirks |= SDHCI_QUIRK_NO_1_8_V;
271 host->max_clk = clock;
273 ret = sdhci_setup_cfg(&plat->cfg, host, plat->f_max,
274 CONFIG_ZYNQ_SDHCI_MIN_FREQ);
275 host->mmc = &plat->mmc;
278 host->mmc->priv = host;
279 host->mmc->dev = dev;
280 upriv->mmc = host->mmc;
282 return sdhci_probe(dev);
285 static int arasan_sdhci_ofdata_to_platdata(struct udevice *dev)
287 struct arasan_sdhci_plat *plat = dev_get_platdata(dev);
288 struct arasan_sdhci_priv *priv = dev_get_priv(dev);
290 priv->host = calloc(1, sizeof(struct sdhci_host));
294 priv->host->name = dev->name;
296 #if defined(CONFIG_DM_MMC) && defined(CONFIG_ARCH_ZYNQMP)
297 priv->host->ops = &arasan_ops;
300 priv->host->ioaddr = (void *)dev_read_addr(dev);
301 if (IS_ERR(priv->host->ioaddr))
302 return PTR_ERR(priv->host->ioaddr);
304 priv->deviceid = dev_read_u32_default(dev, "xlnx,device_id", -1);
305 priv->bank = dev_read_u32_default(dev, "xlnx,mio_bank", -1);
306 priv->no_1p8 = dev_read_bool(dev, "no-1-8-v");
308 plat->f_max = dev_read_u32_default(dev, "max-frequency",
309 CONFIG_ZYNQ_SDHCI_MAX_FREQ);
313 static int arasan_sdhci_bind(struct udevice *dev)
315 struct arasan_sdhci_plat *plat = dev_get_platdata(dev);
317 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
320 static const struct udevice_id arasan_sdhci_ids[] = {
321 { .compatible = "arasan,sdhci-8.9a" },
325 U_BOOT_DRIVER(arasan_sdhci_drv) = {
326 .name = "arasan_sdhci",
328 .of_match = arasan_sdhci_ids,
329 .ofdata_to_platdata = arasan_sdhci_ofdata_to_platdata,
331 .bind = arasan_sdhci_bind,
332 .probe = arasan_sdhci_probe,
333 .priv_auto_alloc_size = sizeof(struct arasan_sdhci_priv),
334 .platdata_auto_alloc_size = sizeof(struct arasan_sdhci_plat),