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 <dm/device_compat.h>
14 #include <linux/err.h>
15 #include <linux/libfdt.h>
18 #include <zynqmp_tap_delay.h>
20 DECLARE_GLOBAL_DATA_PTR;
22 struct arasan_sdhci_plat {
23 struct mmc_config cfg;
27 struct arasan_sdhci_priv {
28 struct sdhci_host *host;
33 #if defined(CONFIG_ARCH_ZYNQMP)
34 #define MMC_HS200_BUS_SPEED 5
36 static const u8 mode2timing[] = {
37 [MMC_LEGACY] = UHS_SDR12_BUS_SPEED,
38 [MMC_HS] = HIGH_SPEED_BUS_SPEED,
39 [SD_HS] = HIGH_SPEED_BUS_SPEED,
40 [MMC_HS_52] = HIGH_SPEED_BUS_SPEED,
41 [MMC_DDR_52] = HIGH_SPEED_BUS_SPEED,
42 [UHS_SDR12] = UHS_SDR12_BUS_SPEED,
43 [UHS_SDR25] = UHS_SDR25_BUS_SPEED,
44 [UHS_SDR50] = UHS_SDR50_BUS_SPEED,
45 [UHS_DDR50] = UHS_DDR50_BUS_SPEED,
46 [UHS_SDR104] = UHS_SDR104_BUS_SPEED,
47 [MMC_HS_200] = MMC_HS200_BUS_SPEED,
50 #define SDHCI_TUNING_LOOP_COUNT 40
52 static void arasan_zynqmp_dll_reset(struct sdhci_host *host, u8 deviceid)
55 unsigned long timeout;
57 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
58 clk &= ~(SDHCI_CLOCK_CARD_EN);
59 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
62 zynqmp_dll_reset(deviceid);
66 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
67 & SDHCI_CLOCK_INT_STABLE)) {
69 dev_err(mmc_dev(host->mmc),
70 ": Internal clock never stabilised.\n");
77 clk |= SDHCI_CLOCK_CARD_EN;
78 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
81 static int arasan_sdhci_execute_tuning(struct mmc *mmc, u8 opcode)
86 struct sdhci_host *host;
87 struct arasan_sdhci_priv *priv = dev_get_priv(mmc->dev);
88 char tuning_loop_counter = SDHCI_TUNING_LOOP_COUNT;
91 debug("%s\n", __func__);
94 deviceid = priv->deviceid;
96 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
97 ctrl |= SDHCI_CTRL_EXEC_TUNING;
98 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
102 arasan_zynqmp_dll_reset(host, deviceid);
104 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
105 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
109 cmd.resp_type = MMC_RSP_R1;
114 data.flags = MMC_DATA_READ;
116 if (tuning_loop_counter-- == 0)
119 if (cmd.cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200 &&
121 data.blocksize = 128;
123 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
126 sdhci_writew(host, data.blocks, SDHCI_BLOCK_COUNT);
127 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
129 mmc_send_cmd(mmc, &cmd, NULL);
130 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
132 if (cmd.cmdidx == MMC_CMD_SEND_TUNING_BLOCK)
135 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
137 if (tuning_loop_counter < 0) {
138 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
139 sdhci_writel(host, ctrl, SDHCI_HOST_CONTROL2);
142 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
143 printf("%s:Tuning failed\n", __func__);
148 arasan_zynqmp_dll_reset(host, deviceid);
150 /* Enable only interrupts served by the SD controller */
151 sdhci_writel(host, SDHCI_INT_DATA_MASK | SDHCI_INT_CMD_MASK,
153 /* Mask all sdhci interrupt sources */
154 sdhci_writel(host, 0x0, SDHCI_SIGNAL_ENABLE);
159 static void arasan_sdhci_set_tapdelay(struct sdhci_host *host)
161 struct arasan_sdhci_priv *priv = dev_get_priv(host->mmc->dev);
162 struct mmc *mmc = (struct mmc *)host->mmc;
165 uhsmode = mode2timing[mmc->selected_mode];
167 if (uhsmode >= UHS_SDR25_BUS_SPEED)
168 arasan_zynqmp_set_tapdelay(priv->deviceid, uhsmode,
172 static void arasan_sdhci_set_control_reg(struct sdhci_host *host)
174 struct mmc *mmc = (struct mmc *)host->mmc;
180 if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
181 reg = sdhci_readw(host, SDHCI_HOST_CONTROL2);
182 reg |= SDHCI_CTRL_VDD_180;
183 sdhci_writew(host, reg, SDHCI_HOST_CONTROL2);
186 if (mmc->selected_mode > SD_HS &&
187 mmc->selected_mode <= UHS_DDR50)
188 sdhci_set_uhs_timing(host);
192 #if defined(CONFIG_ARCH_ZYNQMP)
193 const struct sdhci_ops arasan_ops = {
194 .platform_execute_tuning = &arasan_sdhci_execute_tuning,
195 .set_delay = &arasan_sdhci_set_tapdelay,
196 .set_control_reg = &arasan_sdhci_set_control_reg,
200 static int arasan_sdhci_probe(struct udevice *dev)
202 struct arasan_sdhci_plat *plat = dev_get_platdata(dev);
203 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
204 struct arasan_sdhci_priv *priv = dev_get_priv(dev);
205 struct sdhci_host *host;
212 ret = clk_get_by_index(dev, 0, &clk);
214 dev_err(dev, "failed to get clock\n");
218 clock = clk_get_rate(&clk);
219 if (IS_ERR_VALUE(clock)) {
220 dev_err(dev, "failed to get rate\n");
224 debug("%s: CLK %ld\n", __func__, clock);
226 ret = clk_enable(&clk);
227 if (ret && ret != -ENOSYS) {
228 dev_err(dev, "failed to enable clock\n");
232 host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD |
233 SDHCI_QUIRK_BROKEN_R1B;
235 #ifdef CONFIG_ZYNQ_HISPD_BROKEN
236 host->quirks |= SDHCI_QUIRK_BROKEN_HISPD_MODE;
239 plat->cfg.f_max = CONFIG_ZYNQ_SDHCI_MAX_FREQ;
241 ret = mmc_of_parse(dev, &plat->cfg);
245 host->max_clk = clock;
247 host->mmc = &plat->mmc;
248 host->mmc->dev = dev;
249 host->mmc->priv = host;
251 ret = sdhci_setup_cfg(&plat->cfg, host, plat->cfg.f_max,
252 CONFIG_ZYNQ_SDHCI_MIN_FREQ);
255 upriv->mmc = host->mmc;
257 return sdhci_probe(dev);
260 static int arasan_sdhci_ofdata_to_platdata(struct udevice *dev)
262 struct arasan_sdhci_priv *priv = dev_get_priv(dev);
264 priv->host = calloc(1, sizeof(struct sdhci_host));
268 priv->host->name = dev->name;
270 #if defined(CONFIG_ARCH_ZYNQMP)
271 priv->host->ops = &arasan_ops;
274 priv->host->ioaddr = (void *)dev_read_addr(dev);
275 if (IS_ERR(priv->host->ioaddr))
276 return PTR_ERR(priv->host->ioaddr);
278 priv->deviceid = dev_read_u32_default(dev, "xlnx,device_id", -1);
279 priv->bank = dev_read_u32_default(dev, "xlnx,mio_bank", -1);
284 static int arasan_sdhci_bind(struct udevice *dev)
286 struct arasan_sdhci_plat *plat = dev_get_platdata(dev);
288 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
291 static const struct udevice_id arasan_sdhci_ids[] = {
292 { .compatible = "arasan,sdhci-8.9a" },
296 U_BOOT_DRIVER(arasan_sdhci_drv) = {
297 .name = "arasan_sdhci",
299 .of_match = arasan_sdhci_ids,
300 .ofdata_to_platdata = arasan_sdhci_ofdata_to_platdata,
302 .bind = arasan_sdhci_bind,
303 .probe = arasan_sdhci_probe,
304 .priv_auto_alloc_size = sizeof(struct arasan_sdhci_priv),
305 .platdata_auto_alloc_size = sizeof(struct arasan_sdhci_plat),