blob: bf638e9675879c2045ca73f20d6ab3b3af6363a2 [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>
Simon Glassdbd79542020-05-10 11:40:11 -060012#include <linux/delay.h>
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +053013#include "mmc_private.h"
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070015#include <dm/device_compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070016#include <linux/err.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090017#include <linux/libfdt.h>
Michal Simek0dd222b2013-04-22 14:56:49 +020018#include <malloc.h>
19#include <sdhci.h>
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +053020#include <zynqmp_tap_delay.h>
Michal Simek0dd222b2013-04-22 14:56:49 +020021
Ashok Reddy Soma24a51072021-07-09 05:53:41 -060022#define SDHCI_ARASAN_ITAPDLY_REGISTER 0xF0F8
23#define SDHCI_ARASAN_ITAPDLY_SEL_MASK GENMASK(7, 0)
24#define SDHCI_ARASAN_OTAPDLY_REGISTER 0xF0FC
25#define SDHCI_ARASAN_OTAPDLY_SEL_MASK GENMASK(5, 0)
26#define SDHCI_ITAPDLY_CHGWIN BIT(9)
27#define SDHCI_ITAPDLY_ENABLE BIT(8)
28#define SDHCI_OTAPDLY_ENABLE BIT(6)
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -060029
Michal Simek5b5101e2020-10-23 04:58:59 -060030#define SDHCI_TUNING_LOOP_COUNT 40
Michal Simek33a6b772020-10-23 04:59:00 -060031#define MMC_BANK2 0x2
32
33struct arasan_sdhci_clk_data {
34 int clk_phase_in[MMC_TIMING_MMC_HS400 + 1];
35 int clk_phase_out[MMC_TIMING_MMC_HS400 + 1];
36};
Michal Simek5b5101e2020-10-23 04:58:59 -060037
Simon Glass4cc87fb2016-07-05 17:10:15 -060038struct arasan_sdhci_plat {
39 struct mmc_config cfg;
40 struct mmc mmc;
41};
42
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +053043struct arasan_sdhci_priv {
44 struct sdhci_host *host;
Michal Simek33a6b772020-10-23 04:59:00 -060045 struct arasan_sdhci_clk_data clk_data;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +053046 u8 deviceid;
47 u8 bank;
Ashok Reddy Soma61e0df92020-10-23 04:58:57 -060048 u8 no_1p8;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +053049};
50
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -060051#if defined(CONFIG_ARCH_ZYNQMP) || defined(CONFIG_ARCH_VERSAL)
Michal Simek33a6b772020-10-23 04:59:00 -060052/* Default settings for ZynqMP Clock Phases */
53const u32 zynqmp_iclk_phases[] = {0, 63, 63, 0, 63, 0, 0, 183, 54, 0, 0};
54const u32 zynqmp_oclk_phases[] = {0, 72, 60, 0, 60, 72, 135, 48, 72, 135, 0};
55
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -060056/* Default settings for Versal Clock Phases */
57const u32 versal_iclk_phases[] = {0, 132, 132, 0, 132, 0, 0, 162, 90, 0, 0};
58const u32 versal_oclk_phases[] = {0, 60, 48, 0, 48, 72, 90, 36, 60, 90, 0};
59
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +053060static const u8 mode2timing[] = {
Ashok Reddy Soma0afdfe32020-10-23 04:58:58 -060061 [MMC_LEGACY] = MMC_TIMING_LEGACY,
62 [MMC_HS] = MMC_TIMING_MMC_HS,
63 [SD_HS] = MMC_TIMING_SD_HS,
64 [MMC_HS_52] = MMC_TIMING_UHS_SDR50,
65 [MMC_DDR_52] = MMC_TIMING_UHS_DDR50,
66 [UHS_SDR12] = MMC_TIMING_UHS_SDR12,
67 [UHS_SDR25] = MMC_TIMING_UHS_SDR25,
68 [UHS_SDR50] = MMC_TIMING_UHS_SDR50,
69 [UHS_DDR50] = MMC_TIMING_UHS_DDR50,
70 [UHS_SDR104] = MMC_TIMING_UHS_SDR104,
71 [MMC_HS_200] = MMC_TIMING_MMC_HS200,
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +053072};
73
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +053074static void arasan_zynqmp_dll_reset(struct sdhci_host *host, u8 deviceid)
75{
76 u16 clk;
77 unsigned long timeout;
78
79 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
80 clk &= ~(SDHCI_CLOCK_CARD_EN);
81 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
82
83 /* Issue DLL Reset */
84 zynqmp_dll_reset(deviceid);
85
86 /* Wait max 20 ms */
87 timeout = 100;
88 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
89 & SDHCI_CLOCK_INT_STABLE)) {
90 if (timeout == 0) {
91 dev_err(mmc_dev(host->mmc),
92 ": Internal clock never stabilised.\n");
93 return;
94 }
95 timeout--;
96 udelay(1000);
97 }
98
99 clk |= SDHCI_CLOCK_CARD_EN;
100 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
101}
102
103static int arasan_sdhci_execute_tuning(struct mmc *mmc, u8 opcode)
104{
105 struct mmc_cmd cmd;
106 struct mmc_data data;
107 u32 ctrl;
108 struct sdhci_host *host;
109 struct arasan_sdhci_priv *priv = dev_get_priv(mmc->dev);
Michal Simek91e95ff2018-06-13 09:12:29 +0200110 char tuning_loop_counter = SDHCI_TUNING_LOOP_COUNT;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530111 u8 deviceid;
112
113 debug("%s\n", __func__);
114
115 host = priv->host;
116 deviceid = priv->deviceid;
117
Faiz Abbas2eddc002019-06-11 00:43:40 +0530118 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530119 ctrl |= SDHCI_CTRL_EXEC_TUNING;
Faiz Abbas2eddc002019-06-11 00:43:40 +0530120 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530121
122 mdelay(1);
123
124 arasan_zynqmp_dll_reset(host, deviceid);
125
126 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
127 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
128
129 do {
130 cmd.cmdidx = opcode;
131 cmd.resp_type = MMC_RSP_R1;
132 cmd.cmdarg = 0;
133
134 data.blocksize = 64;
135 data.blocks = 1;
136 data.flags = MMC_DATA_READ;
137
138 if (tuning_loop_counter-- == 0)
139 break;
140
141 if (cmd.cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200 &&
142 mmc->bus_width == 8)
143 data.blocksize = 128;
144
145 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
146 data.blocksize),
147 SDHCI_BLOCK_SIZE);
148 sdhci_writew(host, data.blocks, SDHCI_BLOCK_COUNT);
149 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
150
151 mmc_send_cmd(mmc, &cmd, NULL);
Faiz Abbas2eddc002019-06-11 00:43:40 +0530152 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530153
154 if (cmd.cmdidx == MMC_CMD_SEND_TUNING_BLOCK)
155 udelay(1);
156
157 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
158
159 if (tuning_loop_counter < 0) {
160 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
Faiz Abbas2eddc002019-06-11 00:43:40 +0530161 sdhci_writel(host, ctrl, SDHCI_HOST_CONTROL2);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530162 }
163
164 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
165 printf("%s:Tuning failed\n", __func__);
166 return -1;
167 }
168
169 udelay(1);
170 arasan_zynqmp_dll_reset(host, deviceid);
171
172 /* Enable only interrupts served by the SD controller */
173 sdhci_writel(host, SDHCI_INT_DATA_MASK | SDHCI_INT_CMD_MASK,
174 SDHCI_INT_ENABLE);
175 /* Mask all sdhci interrupt sources */
176 sdhci_writel(host, 0x0, SDHCI_SIGNAL_ENABLE);
177
178 return 0;
179}
180
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600181/**
182 * sdhci_zynqmp_sdcardclk_set_phase - Set the SD Output Clock Tap Delays
183 *
184 * Set the SD Output Clock Tap Delays for Output path
185 *
186 * @host: Pointer to the sdhci_host structure.
187 * @degrees: The clock phase shift between 0 - 359.
Ashok Reddy Soma94a755d2021-07-09 05:53:39 -0600188 * Return: 0
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600189 */
190static int sdhci_zynqmp_sdcardclk_set_phase(struct sdhci_host *host,
191 int degrees)
192{
193 struct arasan_sdhci_priv *priv = dev_get_priv(host->mmc->dev);
194 struct mmc *mmc = (struct mmc *)host->mmc;
195 u8 tap_delay, tap_max = 0;
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600196 int timing = mode2timing[mmc->selected_mode];
197
198 /*
199 * This is applicable for SDHCI_SPEC_300 and above
200 * ZynqMP does not set phase for <=25MHz clock.
201 * If degrees is zero, no need to do anything.
202 */
Ashok Reddy Soma6f645382021-07-09 05:53:40 -0600203 if (SDHCI_GET_VERSION(host) < SDHCI_SPEC_300)
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600204 return 0;
205
206 switch (timing) {
207 case MMC_TIMING_MMC_HS:
208 case MMC_TIMING_SD_HS:
209 case MMC_TIMING_UHS_SDR25:
210 case MMC_TIMING_UHS_DDR50:
211 case MMC_TIMING_MMC_DDR52:
212 /* For 50MHz clock, 30 Taps are available */
213 tap_max = 30;
214 break;
215 case MMC_TIMING_UHS_SDR50:
216 /* For 100MHz clock, 15 Taps are available */
217 tap_max = 15;
218 break;
219 case MMC_TIMING_UHS_SDR104:
220 case MMC_TIMING_MMC_HS200:
221 /* For 200MHz clock, 8 Taps are available */
222 tap_max = 8;
223 default:
224 break;
225 }
226
227 tap_delay = (degrees * tap_max) / 360;
228
229 arasan_zynqmp_set_tapdelay(priv->deviceid, 0, tap_delay);
230
Ashok Reddy Soma94a755d2021-07-09 05:53:39 -0600231 return 0;
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600232}
233
234/**
235 * sdhci_zynqmp_sampleclk_set_phase - Set the SD Input Clock Tap Delays
236 *
237 * Set the SD Input Clock Tap Delays for Input path
238 *
239 * @host: Pointer to the sdhci_host structure.
240 * @degrees: The clock phase shift between 0 - 359.
Ashok Reddy Soma94a755d2021-07-09 05:53:39 -0600241 * Return: 0
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600242 */
243static int sdhci_zynqmp_sampleclk_set_phase(struct sdhci_host *host,
244 int degrees)
245{
246 struct arasan_sdhci_priv *priv = dev_get_priv(host->mmc->dev);
247 struct mmc *mmc = (struct mmc *)host->mmc;
248 u8 tap_delay, tap_max = 0;
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600249 int timing = mode2timing[mmc->selected_mode];
250
251 /*
252 * This is applicable for SDHCI_SPEC_300 and above
253 * ZynqMP does not set phase for <=25MHz clock.
254 * If degrees is zero, no need to do anything.
255 */
Ashok Reddy Soma6f645382021-07-09 05:53:40 -0600256 if (SDHCI_GET_VERSION(host) < SDHCI_SPEC_300)
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600257 return 0;
258
259 switch (timing) {
260 case MMC_TIMING_MMC_HS:
261 case MMC_TIMING_SD_HS:
262 case MMC_TIMING_UHS_SDR25:
263 case MMC_TIMING_UHS_DDR50:
264 case MMC_TIMING_MMC_DDR52:
265 /* For 50MHz clock, 120 Taps are available */
266 tap_max = 120;
267 break;
268 case MMC_TIMING_UHS_SDR50:
269 /* For 100MHz clock, 60 Taps are available */
270 tap_max = 60;
271 break;
272 case MMC_TIMING_UHS_SDR104:
273 case MMC_TIMING_MMC_HS200:
274 /* For 200MHz clock, 30 Taps are available */
275 tap_max = 30;
276 default:
277 break;
278 }
279
280 tap_delay = (degrees * tap_max) / 360;
281
282 arasan_zynqmp_set_tapdelay(priv->deviceid, tap_delay, 0);
283
Ashok Reddy Soma94a755d2021-07-09 05:53:39 -0600284 return 0;
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600285}
286
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600287/**
288 * sdhci_versal_sdcardclk_set_phase - Set the SD Output Clock Tap Delays
289 *
290 * Set the SD Output Clock Tap Delays for Output path
291 *
292 * @host: Pointer to the sdhci_host structure.
293 * @degrees The clock phase shift between 0 - 359.
Ashok Reddy Soma94a755d2021-07-09 05:53:39 -0600294 * Return: 0
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600295 */
296static int sdhci_versal_sdcardclk_set_phase(struct sdhci_host *host,
297 int degrees)
298{
299 struct mmc *mmc = (struct mmc *)host->mmc;
300 u8 tap_delay, tap_max = 0;
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600301 int timing = mode2timing[mmc->selected_mode];
Ashok Reddy Soma24a51072021-07-09 05:53:41 -0600302 u32 regval;
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600303
304 /*
305 * This is applicable for SDHCI_SPEC_300 and above
306 * Versal does not set phase for <=25MHz clock.
307 * If degrees is zero, no need to do anything.
308 */
Ashok Reddy Soma6f645382021-07-09 05:53:40 -0600309 if (SDHCI_GET_VERSION(host) < SDHCI_SPEC_300)
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600310 return 0;
311
312 switch (timing) {
313 case MMC_TIMING_MMC_HS:
314 case MMC_TIMING_SD_HS:
315 case MMC_TIMING_UHS_SDR25:
316 case MMC_TIMING_UHS_DDR50:
317 case MMC_TIMING_MMC_DDR52:
318 /* For 50MHz clock, 30 Taps are available */
319 tap_max = 30;
320 break;
321 case MMC_TIMING_UHS_SDR50:
322 /* For 100MHz clock, 15 Taps are available */
323 tap_max = 15;
324 break;
325 case MMC_TIMING_UHS_SDR104:
326 case MMC_TIMING_MMC_HS200:
327 /* For 200MHz clock, 8 Taps are available */
328 tap_max = 8;
329 default:
330 break;
331 }
332
333 tap_delay = (degrees * tap_max) / 360;
334
Ashok Reddy Soma24a51072021-07-09 05:53:41 -0600335 /* Limit output tap_delay value to 6 bits */
336 tap_delay &= SDHCI_ARASAN_OTAPDLY_SEL_MASK;
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600337
Ashok Reddy Soma24a51072021-07-09 05:53:41 -0600338 /* Set the Clock Phase */
339 regval = sdhci_readl(host, SDHCI_ARASAN_OTAPDLY_REGISTER);
340 regval |= SDHCI_OTAPDLY_ENABLE;
341 sdhci_writel(host, regval, SDHCI_ARASAN_OTAPDLY_REGISTER);
342 regval &= ~SDHCI_ARASAN_OTAPDLY_SEL_MASK;
343 regval |= tap_delay;
344 sdhci_writel(host, regval, SDHCI_ARASAN_OTAPDLY_REGISTER);
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600345
Ashok Reddy Soma94a755d2021-07-09 05:53:39 -0600346 return 0;
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600347}
348
349/**
350 * sdhci_versal_sampleclk_set_phase - Set the SD Input Clock Tap Delays
351 *
352 * Set the SD Input Clock Tap Delays for Input path
353 *
354 * @host: Pointer to the sdhci_host structure.
355 * @degrees The clock phase shift between 0 - 359.
Ashok Reddy Soma94a755d2021-07-09 05:53:39 -0600356 * Return: 0
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600357 */
358static int sdhci_versal_sampleclk_set_phase(struct sdhci_host *host,
359 int degrees)
360{
361 struct mmc *mmc = (struct mmc *)host->mmc;
362 u8 tap_delay, tap_max = 0;
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600363 int timing = mode2timing[mmc->selected_mode];
Ashok Reddy Soma24a51072021-07-09 05:53:41 -0600364 u32 regval;
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600365
366 /*
367 * This is applicable for SDHCI_SPEC_300 and above
368 * Versal does not set phase for <=25MHz clock.
369 * If degrees is zero, no need to do anything.
370 */
Ashok Reddy Soma6f645382021-07-09 05:53:40 -0600371 if (SDHCI_GET_VERSION(host) < SDHCI_SPEC_300)
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600372 return 0;
373
374 switch (timing) {
375 case MMC_TIMING_MMC_HS:
376 case MMC_TIMING_SD_HS:
377 case MMC_TIMING_UHS_SDR25:
378 case MMC_TIMING_UHS_DDR50:
379 case MMC_TIMING_MMC_DDR52:
380 /* For 50MHz clock, 120 Taps are available */
381 tap_max = 120;
382 break;
383 case MMC_TIMING_UHS_SDR50:
384 /* For 100MHz clock, 60 Taps are available */
385 tap_max = 60;
386 break;
387 case MMC_TIMING_UHS_SDR104:
388 case MMC_TIMING_MMC_HS200:
389 /* For 200MHz clock, 30 Taps are available */
390 tap_max = 30;
391 default:
392 break;
393 }
394
395 tap_delay = (degrees * tap_max) / 360;
396
Ashok Reddy Soma24a51072021-07-09 05:53:41 -0600397 /* Limit input tap_delay value to 8 bits */
398 tap_delay &= SDHCI_ARASAN_ITAPDLY_SEL_MASK;
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600399
Ashok Reddy Soma24a51072021-07-09 05:53:41 -0600400 /* Set the Clock Phase */
401 regval = sdhci_readl(host, SDHCI_ARASAN_ITAPDLY_REGISTER);
402 regval |= SDHCI_ITAPDLY_CHGWIN;
403 sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
404 regval |= SDHCI_ITAPDLY_ENABLE;
405 sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
406 regval &= ~SDHCI_ARASAN_ITAPDLY_SEL_MASK;
407 regval |= tap_delay;
408 sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
409 regval &= ~SDHCI_ITAPDLY_CHGWIN;
410 sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600411
Ashok Reddy Soma94a755d2021-07-09 05:53:39 -0600412 return 0;
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600413}
414
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530415static void arasan_sdhci_set_tapdelay(struct sdhci_host *host)
416{
417 struct arasan_sdhci_priv *priv = dev_get_priv(host->mmc->dev);
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600418 struct arasan_sdhci_clk_data *clk_data = &priv->clk_data;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530419 struct mmc *mmc = (struct mmc *)host->mmc;
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600420 struct udevice *dev = mmc->dev;
421 u8 timing = mode2timing[mmc->selected_mode];
422 u32 iclk_phase = clk_data->clk_phase_in[timing];
423 u32 oclk_phase = clk_data->clk_phase_out[timing];
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530424
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600425 dev_dbg(dev, "%s, host:%s, mode:%d\n", __func__, host->name, timing);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530426
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600427 if (IS_ENABLED(CONFIG_ARCH_ZYNQMP) &&
428 device_is_compatible(dev, "xlnx,zynqmp-8.9a")) {
429 sdhci_zynqmp_sampleclk_set_phase(host, iclk_phase);
430 sdhci_zynqmp_sdcardclk_set_phase(host, oclk_phase);
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600431 } else if (IS_ENABLED(CONFIG_ARCH_VERSAL) &&
432 device_is_compatible(dev, "xlnx,versal-8.9a")) {
433 sdhci_versal_sampleclk_set_phase(host, iclk_phase);
434 sdhci_versal_sdcardclk_set_phase(host, oclk_phase);
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600435 }
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530436}
437
Michal Simek33a6b772020-10-23 04:59:00 -0600438static void arasan_dt_read_clk_phase(struct udevice *dev, unsigned char timing,
439 const char *prop)
440{
441 struct arasan_sdhci_priv *priv = dev_get_priv(dev);
442 struct arasan_sdhci_clk_data *clk_data = &priv->clk_data;
443 u32 clk_phase[2] = {0};
444
445 /*
446 * Read Tap Delay values from DT, if the DT does not contain the
447 * Tap Values then use the pre-defined values
448 */
449 if (dev_read_u32_array(dev, prop, &clk_phase[0], 2)) {
450 dev_dbg(dev, "Using predefined clock phase for %s = %d %d\n",
451 prop, clk_data->clk_phase_in[timing],
452 clk_data->clk_phase_out[timing]);
453 return;
454 }
455
456 /* The values read are Input and Output Clock Delays in order */
457 clk_data->clk_phase_in[timing] = clk_phase[0];
458 clk_data->clk_phase_out[timing] = clk_phase[1];
459}
460
461/**
462 * arasan_dt_parse_clk_phases - Read Tap Delay values from DT
463 *
464 * Called at initialization to parse the values of Tap Delays.
465 *
466 * @dev: Pointer to our struct udevice.
467 */
468static void arasan_dt_parse_clk_phases(struct udevice *dev)
469{
470 struct arasan_sdhci_priv *priv = dev_get_priv(dev);
471 struct arasan_sdhci_clk_data *clk_data = &priv->clk_data;
472 int i;
473
474 if (IS_ENABLED(CONFIG_ARCH_ZYNQMP) &&
475 device_is_compatible(dev, "xlnx,zynqmp-8.9a")) {
476 for (i = 0; i <= MMC_TIMING_MMC_HS400; i++) {
477 clk_data->clk_phase_in[i] = zynqmp_iclk_phases[i];
478 clk_data->clk_phase_out[i] = zynqmp_oclk_phases[i];
479 }
480
481 if (priv->bank == MMC_BANK2) {
482 clk_data->clk_phase_out[MMC_TIMING_UHS_SDR104] = 90;
483 clk_data->clk_phase_out[MMC_TIMING_MMC_HS200] = 90;
484 }
485 }
486
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600487 if (IS_ENABLED(CONFIG_ARCH_VERSAL) &&
488 device_is_compatible(dev, "xlnx,versal-8.9a")) {
489 for (i = 0; i <= MMC_TIMING_MMC_HS400; i++) {
490 clk_data->clk_phase_in[i] = versal_iclk_phases[i];
491 clk_data->clk_phase_out[i] = versal_oclk_phases[i];
492 }
493 }
494
Michal Simek33a6b772020-10-23 04:59:00 -0600495 arasan_dt_read_clk_phase(dev, MMC_TIMING_LEGACY,
496 "clk-phase-legacy");
497 arasan_dt_read_clk_phase(dev, MMC_TIMING_MMC_HS,
498 "clk-phase-mmc-hs");
499 arasan_dt_read_clk_phase(dev, MMC_TIMING_SD_HS,
500 "clk-phase-sd-hs");
501 arasan_dt_read_clk_phase(dev, MMC_TIMING_UHS_SDR12,
502 "clk-phase-uhs-sdr12");
503 arasan_dt_read_clk_phase(dev, MMC_TIMING_UHS_SDR25,
504 "clk-phase-uhs-sdr25");
505 arasan_dt_read_clk_phase(dev, MMC_TIMING_UHS_SDR50,
506 "clk-phase-uhs-sdr50");
507 arasan_dt_read_clk_phase(dev, MMC_TIMING_UHS_SDR104,
508 "clk-phase-uhs-sdr104");
509 arasan_dt_read_clk_phase(dev, MMC_TIMING_UHS_DDR50,
510 "clk-phase-uhs-ddr50");
511 arasan_dt_read_clk_phase(dev, MMC_TIMING_MMC_DDR52,
512 "clk-phase-mmc-ddr52");
513 arasan_dt_read_clk_phase(dev, MMC_TIMING_MMC_HS200,
514 "clk-phase-mmc-hs200");
515 arasan_dt_read_clk_phase(dev, MMC_TIMING_MMC_HS400,
516 "clk-phase-mmc-hs400");
517}
518
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530519static void arasan_sdhci_set_control_reg(struct sdhci_host *host)
520{
521 struct mmc *mmc = (struct mmc *)host->mmc;
522 u32 reg;
523
Siva Durga Prasad Paladugued9c0122018-05-29 20:03:11 +0530524 if (!IS_SD(mmc))
525 return;
526
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530527 if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
Faiz Abbas2eddc002019-06-11 00:43:40 +0530528 reg = sdhci_readw(host, SDHCI_HOST_CONTROL2);
529 reg |= SDHCI_CTRL_VDD_180;
530 sdhci_writew(host, reg, SDHCI_HOST_CONTROL2);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530531 }
532
533 if (mmc->selected_mode > SD_HS &&
Ashok Reddy Soma4179ee82020-10-23 04:59:03 -0600534 mmc->selected_mode <= MMC_HS_200)
Faiz Abbas2eddc002019-06-11 00:43:40 +0530535 sdhci_set_uhs_timing(host);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530536}
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530537
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530538const struct sdhci_ops arasan_ops = {
Michal Simeke232bd72020-09-14 13:00:40 +0200539 .platform_execute_tuning = &arasan_sdhci_execute_tuning,
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530540 .set_delay = &arasan_sdhci_set_tapdelay,
541 .set_control_reg = &arasan_sdhci_set_control_reg,
542};
543#endif
544
Michal Simek9ecd2682015-11-30 16:13:03 +0100545static int arasan_sdhci_probe(struct udevice *dev)
Michal Simek0dd222b2013-04-22 14:56:49 +0200546{
Simon Glassfa20e932020-12-03 16:55:20 -0700547 struct arasan_sdhci_plat *plat = dev_get_plat(dev);
Michal Simek9ecd2682015-11-30 16:13:03 +0100548 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530549 struct arasan_sdhci_priv *priv = dev_get_priv(dev);
550 struct sdhci_host *host;
Stefan Herbrechtsmeier739ae402017-01-17 16:27:32 +0100551 struct clk clk;
552 unsigned long clock;
Simon Glass4cc87fb2016-07-05 17:10:15 -0600553 int ret;
Michal Simek0dd222b2013-04-22 14:56:49 +0200554
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530555 host = priv->host;
556
Stefan Herbrechtsmeier739ae402017-01-17 16:27:32 +0100557 ret = clk_get_by_index(dev, 0, &clk);
558 if (ret < 0) {
559 dev_err(dev, "failed to get clock\n");
560 return ret;
561 }
562
563 clock = clk_get_rate(&clk);
564 if (IS_ERR_VALUE(clock)) {
565 dev_err(dev, "failed to get rate\n");
566 return clock;
567 }
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530568
Stefan Herbrechtsmeier739ae402017-01-17 16:27:32 +0100569 debug("%s: CLK %ld\n", __func__, clock);
570
571 ret = clk_enable(&clk);
Michal Simek41710952021-02-09 15:28:15 +0100572 if (ret) {
Stefan Herbrechtsmeier739ae402017-01-17 16:27:32 +0100573 dev_err(dev, "failed to enable clock\n");
574 return ret;
575 }
576
Siva Durga Prasad Paladugu049e0032014-07-08 15:31:04 +0530577 host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD |
Siva Durga Prasad Paladugu0d6891b2014-01-22 09:17:09 +0100578 SDHCI_QUIRK_BROKEN_R1B;
Siva Durga Prasad Paladugua1619fe2016-01-12 15:12:16 +0530579
580#ifdef CONFIG_ZYNQ_HISPD_BROKEN
Hannes Schmelzer94a5bbc2018-03-07 08:00:57 +0100581 host->quirks |= SDHCI_QUIRK_BROKEN_HISPD_MODE;
Siva Durga Prasad Paladugua1619fe2016-01-12 15:12:16 +0530582#endif
583
Ashok Reddy Soma61e0df92020-10-23 04:58:57 -0600584 if (priv->no_1p8)
585 host->quirks |= SDHCI_QUIRK_NO_1_8_V;
586
Benedikt Grassl529e6f02020-04-14 07:32:12 +0200587 plat->cfg.f_max = CONFIG_ZYNQ_SDHCI_MAX_FREQ;
588
589 ret = mmc_of_parse(dev, &plat->cfg);
590 if (ret)
591 return ret;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530592
Stefan Herbrechtsmeier739ae402017-01-17 16:27:32 +0100593 host->max_clk = clock;
Stefan Herbrechtsmeierbc47e0e2017-01-17 15:58:48 +0100594
Matwey V. Kornilov194b8602019-08-01 18:00:05 +0300595 host->mmc = &plat->mmc;
596 host->mmc->dev = dev;
597 host->mmc->priv = host;
598
Benedikt Grassl529e6f02020-04-14 07:32:12 +0200599 ret = sdhci_setup_cfg(&plat->cfg, host, plat->cfg.f_max,
Jaehoon Chung8a5ffbb2016-07-26 19:06:24 +0900600 CONFIG_ZYNQ_SDHCI_MIN_FREQ);
Simon Glass4cc87fb2016-07-05 17:10:15 -0600601 if (ret)
602 return ret;
Simon Glass4cc87fb2016-07-05 17:10:15 -0600603 upriv->mmc = host->mmc;
Michal Simek9ecd2682015-11-30 16:13:03 +0100604
Simon Glass4cc87fb2016-07-05 17:10:15 -0600605 return sdhci_probe(dev);
Michal Simek0dd222b2013-04-22 14:56:49 +0200606}
Michal Simek9ecd2682015-11-30 16:13:03 +0100607
Simon Glassaad29ae2020-12-03 16:55:21 -0700608static int arasan_sdhci_of_to_plat(struct udevice *dev)
Michal Simek9ecd2682015-11-30 16:13:03 +0100609{
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530610 struct arasan_sdhci_priv *priv = dev_get_priv(dev);
Michal Simek9ecd2682015-11-30 16:13:03 +0100611
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530612 priv->host = calloc(1, sizeof(struct sdhci_host));
613 if (!priv->host)
614 return -1;
615
616 priv->host->name = dev->name;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530617
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600618#if defined(CONFIG_ARCH_ZYNQMP) || defined(CONFIG_ARCH_VERSAL)
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530619 priv->host->ops = &arasan_ops;
Michal Simek33a6b772020-10-23 04:59:00 -0600620 arasan_dt_parse_clk_phases(dev);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530621#endif
Michal Simek9ecd2682015-11-30 16:13:03 +0100622
Michal Simek921a8de2018-05-16 10:57:07 +0200623 priv->host->ioaddr = (void *)dev_read_addr(dev);
624 if (IS_ERR(priv->host->ioaddr))
625 return PTR_ERR(priv->host->ioaddr);
626
627 priv->deviceid = dev_read_u32_default(dev, "xlnx,device_id", -1);
Michal Simeke40ae572020-07-22 17:46:31 +0200628 priv->bank = dev_read_u32_default(dev, "xlnx,mio-bank", 0);
Ashok Reddy Soma61e0df92020-10-23 04:58:57 -0600629 priv->no_1p8 = dev_read_bool(dev, "no-1-8-v");
Stefan Herbrechtsmeier5567b422017-01-17 16:27:33 +0100630
Michal Simek9ecd2682015-11-30 16:13:03 +0100631 return 0;
632}
633
Simon Glass4cc87fb2016-07-05 17:10:15 -0600634static int arasan_sdhci_bind(struct udevice *dev)
635{
Simon Glassfa20e932020-12-03 16:55:20 -0700636 struct arasan_sdhci_plat *plat = dev_get_plat(dev);
Simon Glass4cc87fb2016-07-05 17:10:15 -0600637
Masahiro Yamadacdb67f32016-09-06 22:17:32 +0900638 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
Simon Glass4cc87fb2016-07-05 17:10:15 -0600639}
640
Michal Simek9ecd2682015-11-30 16:13:03 +0100641static const struct udevice_id arasan_sdhci_ids[] = {
642 { .compatible = "arasan,sdhci-8.9a" },
643 { }
644};
645
646U_BOOT_DRIVER(arasan_sdhci_drv) = {
647 .name = "arasan_sdhci",
648 .id = UCLASS_MMC,
649 .of_match = arasan_sdhci_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700650 .of_to_plat = arasan_sdhci_of_to_plat,
Simon Glass4cc87fb2016-07-05 17:10:15 -0600651 .ops = &sdhci_ops,
652 .bind = arasan_sdhci_bind,
Michal Simek9ecd2682015-11-30 16:13:03 +0100653 .probe = arasan_sdhci_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700654 .priv_auto = sizeof(struct arasan_sdhci_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700655 .plat_auto = sizeof(struct arasan_sdhci_plat),
Michal Simek9ecd2682015-11-30 16:13:03 +0100656};