blob: 2f46f17f4a652ef90e218e71ad4a179f29c59f0e [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Michal Simek0dd222b2013-04-22 14:56:49 +02002/*
Michal Simek9ecd2682015-11-30 16:13:03 +01003 * (C) Copyright 2013 - 2015 Xilinx, Inc.
Michal Simek0dd222b2013-04-22 14:56:49 +02004 *
5 * Xilinx Zynq SD Host Controller Interface
Michal Simek0dd222b2013-04-22 14:56:49 +02006 */
7
Stefan Herbrechtsmeier739ae402017-01-17 16:27:32 +01008#include <clk.h>
Michal Simek0dd222b2013-04-22 14:56:49 +02009#include <common.h>
Michal Simek9ecd2682015-11-30 16:13:03 +010010#include <dm.h>
Michal Simekc57ba042014-02-24 11:16:31 +010011#include <fdtdec.h>
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +053012#include "mmc_private.h"
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070014#include <dm/device_compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070015#include <linux/err.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090016#include <linux/libfdt.h>
Michal Simek0dd222b2013-04-22 14:56:49 +020017#include <malloc.h>
18#include <sdhci.h>
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +053019#include <zynqmp_tap_delay.h>
Michal Simek0dd222b2013-04-22 14:56:49 +020020
Stefan Herbrechtsmeier5567b422017-01-17 16:27:33 +010021DECLARE_GLOBAL_DATA_PTR;
22
Simon Glass4cc87fb2016-07-05 17:10:15 -060023struct arasan_sdhci_plat {
24 struct mmc_config cfg;
25 struct mmc mmc;
26};
27
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +053028struct arasan_sdhci_priv {
29 struct sdhci_host *host;
30 u8 deviceid;
31 u8 bank;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +053032};
33
34#if defined(CONFIG_ARCH_ZYNQMP)
Siva Durga Prasad Paladugued9c0122018-05-29 20:03:11 +053035#define MMC_HS200_BUS_SPEED 5
36
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +053037static const u8 mode2timing[] = {
Siva Durga Prasad Paladugued9c0122018-05-29 20:03:11 +053038 [MMC_LEGACY] = UHS_SDR12_BUS_SPEED,
Siva Durga Prasad Paladugued9c0122018-05-29 20:03:11 +053039 [MMC_HS] = HIGH_SPEED_BUS_SPEED,
40 [SD_HS] = HIGH_SPEED_BUS_SPEED,
41 [MMC_HS_52] = HIGH_SPEED_BUS_SPEED,
42 [MMC_DDR_52] = HIGH_SPEED_BUS_SPEED,
43 [UHS_SDR12] = UHS_SDR12_BUS_SPEED,
44 [UHS_SDR25] = UHS_SDR25_BUS_SPEED,
45 [UHS_SDR50] = UHS_SDR50_BUS_SPEED,
46 [UHS_DDR50] = UHS_DDR50_BUS_SPEED,
47 [UHS_SDR104] = UHS_SDR104_BUS_SPEED,
48 [MMC_HS_200] = MMC_HS200_BUS_SPEED,
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +053049};
50
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +053051#define SDHCI_TUNING_LOOP_COUNT 40
52
53static void arasan_zynqmp_dll_reset(struct sdhci_host *host, u8 deviceid)
54{
55 u16 clk;
56 unsigned long timeout;
57
58 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
59 clk &= ~(SDHCI_CLOCK_CARD_EN);
60 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
61
62 /* Issue DLL Reset */
63 zynqmp_dll_reset(deviceid);
64
65 /* Wait max 20 ms */
66 timeout = 100;
67 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
68 & SDHCI_CLOCK_INT_STABLE)) {
69 if (timeout == 0) {
70 dev_err(mmc_dev(host->mmc),
71 ": Internal clock never stabilised.\n");
72 return;
73 }
74 timeout--;
75 udelay(1000);
76 }
77
78 clk |= SDHCI_CLOCK_CARD_EN;
79 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
80}
81
82static int arasan_sdhci_execute_tuning(struct mmc *mmc, u8 opcode)
83{
84 struct mmc_cmd cmd;
85 struct mmc_data data;
86 u32 ctrl;
87 struct sdhci_host *host;
88 struct arasan_sdhci_priv *priv = dev_get_priv(mmc->dev);
Michal Simek91e95ff2018-06-13 09:12:29 +020089 char tuning_loop_counter = SDHCI_TUNING_LOOP_COUNT;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +053090 u8 deviceid;
91
92 debug("%s\n", __func__);
93
94 host = priv->host;
95 deviceid = priv->deviceid;
96
Faiz Abbas2eddc002019-06-11 00:43:40 +053097 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +053098 ctrl |= SDHCI_CTRL_EXEC_TUNING;
Faiz Abbas2eddc002019-06-11 00:43:40 +053099 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530100
101 mdelay(1);
102
103 arasan_zynqmp_dll_reset(host, deviceid);
104
105 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
106 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
107
108 do {
109 cmd.cmdidx = opcode;
110 cmd.resp_type = MMC_RSP_R1;
111 cmd.cmdarg = 0;
112
113 data.blocksize = 64;
114 data.blocks = 1;
115 data.flags = MMC_DATA_READ;
116
117 if (tuning_loop_counter-- == 0)
118 break;
119
120 if (cmd.cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200 &&
121 mmc->bus_width == 8)
122 data.blocksize = 128;
123
124 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
125 data.blocksize),
126 SDHCI_BLOCK_SIZE);
127 sdhci_writew(host, data.blocks, SDHCI_BLOCK_COUNT);
128 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
129
130 mmc_send_cmd(mmc, &cmd, NULL);
Faiz Abbas2eddc002019-06-11 00:43:40 +0530131 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530132
133 if (cmd.cmdidx == MMC_CMD_SEND_TUNING_BLOCK)
134 udelay(1);
135
136 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
137
138 if (tuning_loop_counter < 0) {
139 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
Faiz Abbas2eddc002019-06-11 00:43:40 +0530140 sdhci_writel(host, ctrl, SDHCI_HOST_CONTROL2);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530141 }
142
143 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
144 printf("%s:Tuning failed\n", __func__);
145 return -1;
146 }
147
148 udelay(1);
149 arasan_zynqmp_dll_reset(host, deviceid);
150
151 /* Enable only interrupts served by the SD controller */
152 sdhci_writel(host, SDHCI_INT_DATA_MASK | SDHCI_INT_CMD_MASK,
153 SDHCI_INT_ENABLE);
154 /* Mask all sdhci interrupt sources */
155 sdhci_writel(host, 0x0, SDHCI_SIGNAL_ENABLE);
156
157 return 0;
158}
159
160static void arasan_sdhci_set_tapdelay(struct sdhci_host *host)
161{
162 struct arasan_sdhci_priv *priv = dev_get_priv(host->mmc->dev);
163 struct mmc *mmc = (struct mmc *)host->mmc;
164 u8 uhsmode;
165
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530166 uhsmode = mode2timing[mmc->selected_mode];
167
168 if (uhsmode >= UHS_SDR25_BUS_SPEED)
169 arasan_zynqmp_set_tapdelay(priv->deviceid, uhsmode,
170 priv->bank);
171}
172
173static void arasan_sdhci_set_control_reg(struct sdhci_host *host)
174{
175 struct mmc *mmc = (struct mmc *)host->mmc;
176 u32 reg;
177
Siva Durga Prasad Paladugued9c0122018-05-29 20:03:11 +0530178 if (!IS_SD(mmc))
179 return;
180
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530181 if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
Faiz Abbas2eddc002019-06-11 00:43:40 +0530182 reg = sdhci_readw(host, SDHCI_HOST_CONTROL2);
183 reg |= SDHCI_CTRL_VDD_180;
184 sdhci_writew(host, reg, SDHCI_HOST_CONTROL2);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530185 }
186
187 if (mmc->selected_mode > SD_HS &&
Faiz Abbas2eddc002019-06-11 00:43:40 +0530188 mmc->selected_mode <= UHS_DDR50)
189 sdhci_set_uhs_timing(host);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530190}
191#endif
192
Siva Durga Prasad Paladugu9a8ce6a2019-08-02 16:46:26 +0530193#if defined(CONFIG_ARCH_ZYNQMP)
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530194const struct sdhci_ops arasan_ops = {
195 .platform_execute_tuning = &arasan_sdhci_execute_tuning,
196 .set_delay = &arasan_sdhci_set_tapdelay,
197 .set_control_reg = &arasan_sdhci_set_control_reg,
198};
199#endif
200
Michal Simek9ecd2682015-11-30 16:13:03 +0100201static int arasan_sdhci_probe(struct udevice *dev)
Michal Simek0dd222b2013-04-22 14:56:49 +0200202{
Simon Glass4cc87fb2016-07-05 17:10:15 -0600203 struct arasan_sdhci_plat *plat = dev_get_platdata(dev);
Michal Simek9ecd2682015-11-30 16:13:03 +0100204 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530205 struct arasan_sdhci_priv *priv = dev_get_priv(dev);
206 struct sdhci_host *host;
Stefan Herbrechtsmeier739ae402017-01-17 16:27:32 +0100207 struct clk clk;
208 unsigned long clock;
Simon Glass4cc87fb2016-07-05 17:10:15 -0600209 int ret;
Michal Simek0dd222b2013-04-22 14:56:49 +0200210
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530211 host = priv->host;
212
Stefan Herbrechtsmeier739ae402017-01-17 16:27:32 +0100213 ret = clk_get_by_index(dev, 0, &clk);
214 if (ret < 0) {
215 dev_err(dev, "failed to get clock\n");
216 return ret;
217 }
218
219 clock = clk_get_rate(&clk);
220 if (IS_ERR_VALUE(clock)) {
221 dev_err(dev, "failed to get rate\n");
222 return clock;
223 }
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530224
Stefan Herbrechtsmeier739ae402017-01-17 16:27:32 +0100225 debug("%s: CLK %ld\n", __func__, clock);
226
227 ret = clk_enable(&clk);
228 if (ret && ret != -ENOSYS) {
229 dev_err(dev, "failed to enable clock\n");
230 return ret;
231 }
232
Siva Durga Prasad Paladugu049e0032014-07-08 15:31:04 +0530233 host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD |
Siva Durga Prasad Paladugu0d6891b2014-01-22 09:17:09 +0100234 SDHCI_QUIRK_BROKEN_R1B;
Siva Durga Prasad Paladugua1619fe2016-01-12 15:12:16 +0530235
236#ifdef CONFIG_ZYNQ_HISPD_BROKEN
Hannes Schmelzer94a5bbc2018-03-07 08:00:57 +0100237 host->quirks |= SDHCI_QUIRK_BROKEN_HISPD_MODE;
Siva Durga Prasad Paladugua1619fe2016-01-12 15:12:16 +0530238#endif
239
Benedikt Grassl529e6f02020-04-14 07:32:12 +0200240 plat->cfg.f_max = CONFIG_ZYNQ_SDHCI_MAX_FREQ;
241
242 ret = mmc_of_parse(dev, &plat->cfg);
243 if (ret)
244 return ret;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530245
Stefan Herbrechtsmeier739ae402017-01-17 16:27:32 +0100246 host->max_clk = clock;
Stefan Herbrechtsmeierbc47e0e2017-01-17 15:58:48 +0100247
Matwey V. Kornilov194b8602019-08-01 18:00:05 +0300248 host->mmc = &plat->mmc;
249 host->mmc->dev = dev;
250 host->mmc->priv = host;
251
Benedikt Grassl529e6f02020-04-14 07:32:12 +0200252 ret = sdhci_setup_cfg(&plat->cfg, host, plat->cfg.f_max,
Jaehoon Chung8a5ffbb2016-07-26 19:06:24 +0900253 CONFIG_ZYNQ_SDHCI_MIN_FREQ);
Simon Glass4cc87fb2016-07-05 17:10:15 -0600254 if (ret)
255 return ret;
Simon Glass4cc87fb2016-07-05 17:10:15 -0600256 upriv->mmc = host->mmc;
Michal Simek9ecd2682015-11-30 16:13:03 +0100257
Simon Glass4cc87fb2016-07-05 17:10:15 -0600258 return sdhci_probe(dev);
Michal Simek0dd222b2013-04-22 14:56:49 +0200259}
Michal Simek9ecd2682015-11-30 16:13:03 +0100260
261static int arasan_sdhci_ofdata_to_platdata(struct udevice *dev)
262{
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530263 struct arasan_sdhci_priv *priv = dev_get_priv(dev);
Michal Simek9ecd2682015-11-30 16:13:03 +0100264
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530265 priv->host = calloc(1, sizeof(struct sdhci_host));
266 if (!priv->host)
267 return -1;
268
269 priv->host->name = dev->name;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530270
Siva Durga Prasad Paladugu9a8ce6a2019-08-02 16:46:26 +0530271#if defined(CONFIG_ARCH_ZYNQMP)
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530272 priv->host->ops = &arasan_ops;
273#endif
Michal Simek9ecd2682015-11-30 16:13:03 +0100274
Michal Simek921a8de2018-05-16 10:57:07 +0200275 priv->host->ioaddr = (void *)dev_read_addr(dev);
276 if (IS_ERR(priv->host->ioaddr))
277 return PTR_ERR(priv->host->ioaddr);
278
279 priv->deviceid = dev_read_u32_default(dev, "xlnx,device_id", -1);
280 priv->bank = dev_read_u32_default(dev, "xlnx,mio_bank", -1);
Stefan Herbrechtsmeier5567b422017-01-17 16:27:33 +0100281
Michal Simek9ecd2682015-11-30 16:13:03 +0100282 return 0;
283}
284
Simon Glass4cc87fb2016-07-05 17:10:15 -0600285static int arasan_sdhci_bind(struct udevice *dev)
286{
287 struct arasan_sdhci_plat *plat = dev_get_platdata(dev);
Simon Glass4cc87fb2016-07-05 17:10:15 -0600288
Masahiro Yamadacdb67f32016-09-06 22:17:32 +0900289 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
Simon Glass4cc87fb2016-07-05 17:10:15 -0600290}
291
Michal Simek9ecd2682015-11-30 16:13:03 +0100292static const struct udevice_id arasan_sdhci_ids[] = {
293 { .compatible = "arasan,sdhci-8.9a" },
294 { }
295};
296
297U_BOOT_DRIVER(arasan_sdhci_drv) = {
298 .name = "arasan_sdhci",
299 .id = UCLASS_MMC,
300 .of_match = arasan_sdhci_ids,
301 .ofdata_to_platdata = arasan_sdhci_ofdata_to_platdata,
Simon Glass4cc87fb2016-07-05 17:10:15 -0600302 .ops = &sdhci_ops,
303 .bind = arasan_sdhci_bind,
Michal Simek9ecd2682015-11-30 16:13:03 +0100304 .probe = arasan_sdhci_probe,
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530305 .priv_auto_alloc_size = sizeof(struct arasan_sdhci_priv),
Simon Glass4cc87fb2016-07-05 17:10:15 -0600306 .platdata_auto_alloc_size = sizeof(struct arasan_sdhci_plat),
Michal Simek9ecd2682015-11-30 16:13:03 +0100307};