blob: 91e309d2752f22b3963bd2c6a40d9e1ff16b17c8 [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/*
Ashok Reddy Soma0ab38fd2023-01-10 04:31:21 -07003 * (C) Copyright 2013 - 2022, Xilinx, Inc.
4 * (C) Copyright 2022, Advanced Micro Devices, Inc.
Michal Simek0dd222b2013-04-22 14:56:49 +02005 *
6 * Xilinx Zynq SD Host Controller Interface
Michal Simek0dd222b2013-04-22 14:56:49 +02007 */
8
Stefan Herbrechtsmeier739ae402017-01-17 16:27:32 +01009#include <clk.h>
Michal Simek0dd222b2013-04-22 14:56:49 +020010#include <common.h>
Michal Simek9ecd2682015-11-30 16:13:03 +010011#include <dm.h>
Michal Simekc57ba042014-02-24 11:16:31 +010012#include <fdtdec.h>
Simon Glassdbd79542020-05-10 11:40:11 -060013#include <linux/delay.h>
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +053014#include "mmc_private.h"
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Ashok Reddy Somaca7d8472022-02-23 15:36:05 +010016#include <reset.h>
Simon Glass9bc15642020-02-03 07:36:16 -070017#include <dm/device_compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070018#include <linux/err.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090019#include <linux/libfdt.h>
Ashok Reddy Soma0ab38fd2023-01-10 04:31:21 -070020#include <linux/iopoll.h>
Ashok Reddy Somaca7d8472022-02-23 15:36:05 +010021#include <asm/types.h>
22#include <linux/math64.h>
Ashok Reddy Soma7a5f8102021-08-02 23:20:44 -060023#include <asm/cache.h>
Michal Simek0dd222b2013-04-22 14:56:49 +020024#include <malloc.h>
25#include <sdhci.h>
Ashok Reddy Soma467d0782021-08-02 23:20:43 -060026#include <zynqmp_firmware.h>
Michal Simek0dd222b2013-04-22 14:56:49 +020027
Ashok Reddy Soma24a51072021-07-09 05:53:41 -060028#define SDHCI_ARASAN_ITAPDLY_REGISTER 0xF0F8
29#define SDHCI_ARASAN_ITAPDLY_SEL_MASK GENMASK(7, 0)
30#define SDHCI_ARASAN_OTAPDLY_REGISTER 0xF0FC
31#define SDHCI_ARASAN_OTAPDLY_SEL_MASK GENMASK(5, 0)
32#define SDHCI_ITAPDLY_CHGWIN BIT(9)
33#define SDHCI_ITAPDLY_ENABLE BIT(8)
34#define SDHCI_OTAPDLY_ENABLE BIT(6)
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -060035
Michal Simek5b5101e2020-10-23 04:58:59 -060036#define SDHCI_TUNING_LOOP_COUNT 40
Michal Simek33a6b772020-10-23 04:59:00 -060037#define MMC_BANK2 0x2
38
Ashok Reddy Soma7a5f8102021-08-02 23:20:44 -060039#define SD_DLL_CTRL 0xFF180358
40#define SD_ITAP_DLY 0xFF180314
41#define SD_OTAP_DLY 0xFF180318
42#define SD0_DLL_RST BIT(2)
43#define SD1_DLL_RST BIT(18)
44#define SD0_ITAPCHGWIN BIT(9)
45#define SD1_ITAPCHGWIN BIT(25)
46#define SD0_ITAPDLYENA BIT(8)
47#define SD1_ITAPDLYENA BIT(24)
48#define SD0_ITAPDLYSEL_MASK GENMASK(7, 0)
49#define SD1_ITAPDLYSEL_MASK GENMASK(23, 16)
50#define SD0_OTAPDLYSEL_MASK GENMASK(5, 0)
51#define SD1_OTAPDLYSEL_MASK GENMASK(21, 16)
52
Ashok Reddy Soma0ab38fd2023-01-10 04:31:21 -070053#define MIN_PHY_CLK_HZ 50000000
54
55#define PHY_CTRL_REG1 0x270
56#define PHY_CTRL_ITAPDLY_ENA_MASK BIT(0)
57#define PHY_CTRL_ITAPDLY_SEL_MASK GENMASK(5, 1)
58#define PHY_CTRL_ITAPDLY_SEL_SHIFT 1
59#define PHY_CTRL_ITAP_CHG_WIN_MASK BIT(6)
60#define PHY_CTRL_OTAPDLY_ENA_MASK BIT(8)
61#define PHY_CTRL_OTAPDLY_SEL_MASK GENMASK(15, 12)
62#define PHY_CTRL_OTAPDLY_SEL_SHIFT 12
63#define PHY_CTRL_STRB_SEL_MASK GENMASK(23, 16)
64#define PHY_CTRL_STRB_SEL_SHIFT 16
65#define PHY_CTRL_TEST_CTRL_MASK GENMASK(31, 24)
66
67#define PHY_CTRL_REG2 0x274
68#define PHY_CTRL_EN_DLL_MASK BIT(0)
69#define PHY_CTRL_DLL_RDY_MASK BIT(1)
70#define PHY_CTRL_FREQ_SEL_MASK GENMASK(6, 4)
71#define PHY_CTRL_FREQ_SEL_SHIFT 4
72#define PHY_CTRL_SEL_DLY_TX_MASK BIT(16)
73#define PHY_CTRL_SEL_DLY_RX_MASK BIT(17)
74#define FREQSEL_200M_170M 0x0
75#define FREQSEL_170M_140M 0x1
76#define FREQSEL_140M_110M 0x2
77#define FREQSEL_110M_80M 0x3
78#define FREQSEL_80M_50M 0x4
79#define FREQSEL_275M_250M 0x5
80#define FREQSEL_250M_225M 0x6
81#define FREQSEL_225M_200M 0x7
82#define PHY_DLL_TIMEOUT_MS 100
83
84#define VERSAL_NET_EMMC_ICLK_PHASE_DDR52_DLY_CHAIN 39
85#define VERSAL_NET_EMMC_ICLK_PHASE_DDR52_DLL 146
86#define VERSAL_NET_PHY_CTRL_STRB90_STRB180_VAL 0X77
87
Michal Simek33a6b772020-10-23 04:59:00 -060088struct arasan_sdhci_clk_data {
89 int clk_phase_in[MMC_TIMING_MMC_HS400 + 1];
90 int clk_phase_out[MMC_TIMING_MMC_HS400 + 1];
91};
Michal Simek5b5101e2020-10-23 04:58:59 -060092
Simon Glass4cc87fb2016-07-05 17:10:15 -060093struct arasan_sdhci_plat {
94 struct mmc_config cfg;
95 struct mmc mmc;
96};
97
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +053098struct arasan_sdhci_priv {
99 struct sdhci_host *host;
Michal Simek33a6b772020-10-23 04:59:00 -0600100 struct arasan_sdhci_clk_data clk_data;
Ashok Reddy Soma33154532022-09-30 03:25:47 -0600101 u32 node_id;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530102 u8 bank;
Ashok Reddy Soma61e0df92020-10-23 04:58:57 -0600103 u8 no_1p8;
Ashok Reddy Soma0ab38fd2023-01-10 04:31:21 -0700104 bool internal_phy_reg;
Ashok Reddy Somaca7d8472022-02-23 15:36:05 +0100105 struct reset_ctl_bulk resets;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530106};
107
Ashok Reddy Soma7a5f8102021-08-02 23:20:44 -0600108/* For Versal platforms zynqmp_mmio_write() won't be available */
109__weak int zynqmp_mmio_write(const u32 address, const u32 mask, const u32 value)
110{
111 return 0;
112}
113
T Karthik Reddyd0618272021-10-01 16:38:38 +0530114__weak int xilinx_pm_request(u32 api_id, u32 arg0, u32 arg1, u32 arg2,
115 u32 arg3, u32 *ret_payload)
116{
117 return 0;
118}
119
T Karthik Reddy0b35fa22022-04-27 10:27:12 +0200120__weak int zynqmp_pm_is_function_supported(const u32 api_id, const u32 id)
121{
122 return 1;
123}
124
Ashok Reddy Soma0ab38fd2023-01-10 04:31:21 -0700125#if defined(CONFIG_ARCH_ZYNQMP) || defined(CONFIG_ARCH_VERSAL) || defined(CONFIG_ARCH_VERSAL_NET)
Michal Simek33a6b772020-10-23 04:59:00 -0600126/* Default settings for ZynqMP Clock Phases */
Michal Simek635cf4a2021-07-09 05:53:44 -0600127static const u32 zynqmp_iclk_phases[] = {0, 63, 63, 0, 63, 0,
128 0, 183, 54, 0, 0};
129static const u32 zynqmp_oclk_phases[] = {0, 72, 60, 0, 60, 72,
130 135, 48, 72, 135, 0};
Michal Simek33a6b772020-10-23 04:59:00 -0600131
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600132/* Default settings for Versal Clock Phases */
Michal Simek635cf4a2021-07-09 05:53:44 -0600133static const u32 versal_iclk_phases[] = {0, 132, 132, 0, 132,
134 0, 0, 162, 90, 0, 0};
135static const u32 versal_oclk_phases[] = {0, 60, 48, 0, 48, 72,
136 90, 36, 60, 90, 0};
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600137
Ashok Reddy Soma0ab38fd2023-01-10 04:31:21 -0700138/* Default settings for versal-net eMMC Clock Phases */
139static const u32 versal_net_emmc_iclk_phases[] = {0, 0, 0, 0, 0, 0, 0, 0, 39,
140 0, 0};
141static const u32 versal_net_emmc_oclk_phases[] = {0, 113, 0, 0, 0, 0, 0, 0,
142 113, 79, 45};
143
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530144static const u8 mode2timing[] = {
Ashok Reddy Soma0afdfe32020-10-23 04:58:58 -0600145 [MMC_LEGACY] = MMC_TIMING_LEGACY,
146 [MMC_HS] = MMC_TIMING_MMC_HS,
147 [SD_HS] = MMC_TIMING_SD_HS,
Ashok Reddy Soma5ff7acf2022-06-27 14:22:45 +0530148 [MMC_HS_52] = MMC_TIMING_MMC_HS,
149 [MMC_DDR_52] = MMC_TIMING_MMC_DDR52,
Ashok Reddy Soma0afdfe32020-10-23 04:58:58 -0600150 [UHS_SDR12] = MMC_TIMING_UHS_SDR12,
151 [UHS_SDR25] = MMC_TIMING_UHS_SDR25,
152 [UHS_SDR50] = MMC_TIMING_UHS_SDR50,
153 [UHS_DDR50] = MMC_TIMING_UHS_DDR50,
154 [UHS_SDR104] = MMC_TIMING_UHS_SDR104,
155 [MMC_HS_200] = MMC_TIMING_MMC_HS200,
Ashok Reddy Soma407a5612023-01-10 04:31:24 -0700156 [MMC_HS_400] = MMC_TIMING_MMC_HS400,
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530157};
158
Ashok Reddy Soma0ab38fd2023-01-10 04:31:21 -0700159#if defined(CONFIG_ARCH_VERSAL_NET)
160/**
161 * arasan_phy_set_delaychain - Set eMMC delay chain based Input/Output clock
162 *
163 * @host: Pointer to the sdhci_host structure
164 * @enable: Enable or disable Delay chain based Tx and Rx clock
165 * Return: None
166 *
167 * Enable or disable eMMC delay chain based Input and Output clock in
168 * PHY_CTRL_REG2
169 */
170static void arasan_phy_set_delaychain(struct sdhci_host *host, bool enable)
171{
172 u32 reg;
173
174 reg = sdhci_readw(host, PHY_CTRL_REG2);
175 if (enable)
176 reg |= PHY_CTRL_SEL_DLY_TX_MASK | PHY_CTRL_SEL_DLY_RX_MASK;
177 else
178 reg &= ~(PHY_CTRL_SEL_DLY_TX_MASK | PHY_CTRL_SEL_DLY_RX_MASK);
179
180 sdhci_writew(host, reg, PHY_CTRL_REG2);
181}
182
183/**
184 * arasan_phy_set_dll - Set eMMC DLL clock
185 *
186 * @host: Pointer to the sdhci_host structure
187 * @enable: Enable or disable DLL clock
188 * Return: 0 if success or timeout error
189 *
190 * Enable or disable eMMC DLL clock in PHY_CTRL_REG2. When DLL enable is
191 * set, wait till DLL is locked
192 */
193static int arasan_phy_set_dll(struct sdhci_host *host, bool enable)
194{
195 u32 reg;
196
197 reg = sdhci_readw(host, PHY_CTRL_REG2);
198 if (enable)
199 reg |= PHY_CTRL_EN_DLL_MASK;
200 else
201 reg &= ~PHY_CTRL_EN_DLL_MASK;
202
203 sdhci_writew(host, reg, PHY_CTRL_REG2);
204
205 /* If DLL is disabled return success */
206 if (!enable)
207 return 0;
208
209 /* If DLL is enabled wait till DLL loop is locked, which is
210 * indicated by dll_rdy bit(bit1) in PHY_CTRL_REG2
211 */
212 return readl_relaxed_poll_timeout(host->ioaddr + PHY_CTRL_REG2, reg,
213 (reg & PHY_CTRL_DLL_RDY_MASK),
214 1000 * PHY_DLL_TIMEOUT_MS);
215}
216
217/**
218 * arasan_phy_dll_set_freq - Select frequency range of DLL for eMMC
219 *
220 * @host: Pointer to the sdhci_host structure
221 * @clock: clock value
222 * Return: None
223 *
224 * Set frequency range bits based on the selected clock for eMMC
225 */
226static void arasan_phy_dll_set_freq(struct sdhci_host *host, int clock)
227{
228 u32 reg, freq_sel, freq;
229
230 freq = DIV_ROUND_CLOSEST(clock, 1000000);
231 if (freq <= 200 && freq > 170)
232 freq_sel = FREQSEL_200M_170M;
233 else if (freq <= 170 && freq > 140)
234 freq_sel = FREQSEL_170M_140M;
235 else if (freq <= 140 && freq > 110)
236 freq_sel = FREQSEL_140M_110M;
237 else if (freq <= 110 && freq > 80)
238 freq_sel = FREQSEL_110M_80M;
239 else
240 freq_sel = FREQSEL_80M_50M;
241
242 reg = sdhci_readw(host, PHY_CTRL_REG2);
243 reg &= ~PHY_CTRL_FREQ_SEL_MASK;
244 reg |= (freq_sel << PHY_CTRL_FREQ_SEL_SHIFT);
245 sdhci_writew(host, reg, PHY_CTRL_REG2);
246}
247
248static int arasan_sdhci_config_dll(struct sdhci_host *host, unsigned int clock, bool enable)
249{
250 struct mmc *mmc = (struct mmc *)host->mmc;
251 struct arasan_sdhci_priv *priv = dev_get_priv(mmc->dev);
252
253 if (enable) {
254 if (priv->internal_phy_reg && clock >= MIN_PHY_CLK_HZ && enable)
255 arasan_phy_set_dll(host, 1);
256 return 0;
257 }
258
259 if (priv->internal_phy_reg && clock >= MIN_PHY_CLK_HZ) {
260 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
261 arasan_phy_set_dll(host, 0);
262 arasan_phy_set_delaychain(host, 0);
263 arasan_phy_dll_set_freq(host, clock);
264 return 0;
265 }
266
267 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
268 arasan_phy_set_delaychain(host, 1);
269
270 return 0;
271}
272#endif
273
Ashok Reddy Somaa23e6ee2022-09-30 03:25:46 -0600274static inline int arasan_zynqmp_set_in_tapdelay(u32 node_id, u32 itap_delay)
Ashok Reddy Soma7a5f8102021-08-02 23:20:44 -0600275{
276 int ret;
277
278 if (IS_ENABLED(CONFIG_SPL_BUILD) || current_el() == 3) {
279 if (node_id == NODE_SD_0) {
280 ret = zynqmp_mmio_write(SD_ITAP_DLY, SD0_ITAPCHGWIN,
281 SD0_ITAPCHGWIN);
282 if (ret)
283 return ret;
284
285 ret = zynqmp_mmio_write(SD_ITAP_DLY, SD0_ITAPDLYENA,
286 SD0_ITAPDLYENA);
287 if (ret)
288 return ret;
289
290 ret = zynqmp_mmio_write(SD_ITAP_DLY, SD0_ITAPDLYSEL_MASK,
291 itap_delay);
292 if (ret)
293 return ret;
294
295 ret = zynqmp_mmio_write(SD_ITAP_DLY, SD0_ITAPCHGWIN, 0);
296 if (ret)
297 return ret;
298 }
299 ret = zynqmp_mmio_write(SD_ITAP_DLY, SD1_ITAPCHGWIN,
300 SD1_ITAPCHGWIN);
301 if (ret)
302 return ret;
303
304 ret = zynqmp_mmio_write(SD_ITAP_DLY, SD1_ITAPDLYENA,
305 SD1_ITAPDLYENA);
306 if (ret)
307 return ret;
308
309 ret = zynqmp_mmio_write(SD_ITAP_DLY, SD1_ITAPDLYSEL_MASK,
310 (itap_delay << 16));
311 if (ret)
312 return ret;
313
314 ret = zynqmp_mmio_write(SD_ITAP_DLY, SD1_ITAPCHGWIN, 0);
315 if (ret)
316 return ret;
317 } else {
Ashok Reddy Somaa23e6ee2022-09-30 03:25:46 -0600318 return xilinx_pm_request(PM_IOCTL, node_id,
Ashok Reddy Soma7a5f8102021-08-02 23:20:44 -0600319 IOCTL_SET_SD_TAPDELAY,
320 PM_TAPDELAY_INPUT, itap_delay, NULL);
321 }
322
323 return 0;
324}
325
Ashok Reddy Somaa23e6ee2022-09-30 03:25:46 -0600326static inline int arasan_zynqmp_set_out_tapdelay(u32 node_id, u32 otap_delay)
Ashok Reddy Soma7a5f8102021-08-02 23:20:44 -0600327{
328 if (IS_ENABLED(CONFIG_SPL_BUILD) || current_el() == 3) {
329 if (node_id == NODE_SD_0)
330 return zynqmp_mmio_write(SD_OTAP_DLY,
331 SD0_OTAPDLYSEL_MASK,
332 otap_delay);
333
334 return zynqmp_mmio_write(SD_OTAP_DLY, SD1_OTAPDLYSEL_MASK,
335 (otap_delay << 16));
336 } else {
Ashok Reddy Somaa23e6ee2022-09-30 03:25:46 -0600337 return xilinx_pm_request(PM_IOCTL, node_id,
Ashok Reddy Soma7a5f8102021-08-02 23:20:44 -0600338 IOCTL_SET_SD_TAPDELAY,
339 PM_TAPDELAY_OUTPUT, otap_delay, NULL);
340 }
341}
342
Ashok Reddy Somaa23e6ee2022-09-30 03:25:46 -0600343static inline int zynqmp_dll_reset(u32 node_id, u32 type)
Ashok Reddy Soma7a5f8102021-08-02 23:20:44 -0600344{
345 if (IS_ENABLED(CONFIG_SPL_BUILD) || current_el() == 3) {
346 if (node_id == NODE_SD_0)
347 return zynqmp_mmio_write(SD_DLL_CTRL, SD0_DLL_RST,
348 type == PM_DLL_RESET_ASSERT ?
349 SD0_DLL_RST : 0);
350
351 return zynqmp_mmio_write(SD_DLL_CTRL, SD1_DLL_RST,
352 type == PM_DLL_RESET_ASSERT ?
353 SD1_DLL_RST : 0);
354 } else {
Ashok Reddy Somaa23e6ee2022-09-30 03:25:46 -0600355 return xilinx_pm_request(PM_IOCTL, node_id,
Ashok Reddy Soma7a5f8102021-08-02 23:20:44 -0600356 IOCTL_SD_DLL_RESET, type, 0, NULL);
357 }
358}
359
Ashok Reddy Somaa23e6ee2022-09-30 03:25:46 -0600360static int arasan_zynqmp_dll_reset(struct sdhci_host *host, u32 node_id)
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530361{
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600362 struct mmc *mmc = (struct mmc *)host->mmc;
363 struct udevice *dev = mmc->dev;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530364 unsigned long timeout;
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600365 int ret;
366 u16 clk;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530367
368 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
369 clk &= ~(SDHCI_CLOCK_CARD_EN);
370 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
371
372 /* Issue DLL Reset */
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600373 ret = zynqmp_dll_reset(node_id, PM_DLL_RESET_ASSERT);
374 if (ret) {
375 dev_err(dev, "dll_reset assert failed with err: %d\n", ret);
376 return ret;
377 }
378
379 /* Allow atleast 1ms delay for proper DLL reset */
380 mdelay(1);
381 ret = zynqmp_dll_reset(node_id, PM_DLL_RESET_RELEASE);
382 if (ret) {
383 dev_err(dev, "dll_reset release failed with err: %d\n", ret);
384 return ret;
385 }
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530386
387 /* Wait max 20 ms */
388 timeout = 100;
389 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
390 & SDHCI_CLOCK_INT_STABLE)) {
391 if (timeout == 0) {
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600392 dev_err(dev, ": Internal clock never stabilised.\n");
393 return -EBUSY;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530394 }
395 timeout--;
396 udelay(1000);
397 }
398
399 clk |= SDHCI_CLOCK_CARD_EN;
400 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600401
402 return 0;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530403}
404
405static int arasan_sdhci_execute_tuning(struct mmc *mmc, u8 opcode)
406{
407 struct mmc_cmd cmd;
408 struct mmc_data data;
409 u32 ctrl;
410 struct sdhci_host *host;
411 struct arasan_sdhci_priv *priv = dev_get_priv(mmc->dev);
Algapally Santosh Sagar99a98ca2023-01-19 22:36:17 -0700412 int tuning_loop_counter = SDHCI_TUNING_LOOP_COUNT;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530413
Ashok Reddy Somaca7d8472022-02-23 15:36:05 +0100414 dev_dbg(mmc->dev, "%s\n", __func__);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530415
416 host = priv->host;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530417
Faiz Abbas2eddc002019-06-11 00:43:40 +0530418 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530419 ctrl |= SDHCI_CTRL_EXEC_TUNING;
Faiz Abbas2eddc002019-06-11 00:43:40 +0530420 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530421
422 mdelay(1);
423
Ashok Reddy Soma33154532022-09-30 03:25:47 -0600424 arasan_zynqmp_dll_reset(host, priv->node_id);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530425
426 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
427 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
428
429 do {
430 cmd.cmdidx = opcode;
431 cmd.resp_type = MMC_RSP_R1;
432 cmd.cmdarg = 0;
433
434 data.blocksize = 64;
435 data.blocks = 1;
436 data.flags = MMC_DATA_READ;
437
438 if (tuning_loop_counter-- == 0)
439 break;
440
441 if (cmd.cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200 &&
442 mmc->bus_width == 8)
443 data.blocksize = 128;
444
445 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
446 data.blocksize),
447 SDHCI_BLOCK_SIZE);
448 sdhci_writew(host, data.blocks, SDHCI_BLOCK_COUNT);
449 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
450
451 mmc_send_cmd(mmc, &cmd, NULL);
Faiz Abbas2eddc002019-06-11 00:43:40 +0530452 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530453
454 if (cmd.cmdidx == MMC_CMD_SEND_TUNING_BLOCK)
455 udelay(1);
456
457 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
458
459 if (tuning_loop_counter < 0) {
460 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
Faiz Abbas2eddc002019-06-11 00:43:40 +0530461 sdhci_writel(host, ctrl, SDHCI_HOST_CONTROL2);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530462 }
463
464 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
465 printf("%s:Tuning failed\n", __func__);
466 return -1;
467 }
468
469 udelay(1);
Ashok Reddy Soma33154532022-09-30 03:25:47 -0600470 arasan_zynqmp_dll_reset(host, priv->node_id);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530471
472 /* Enable only interrupts served by the SD controller */
473 sdhci_writel(host, SDHCI_INT_DATA_MASK | SDHCI_INT_CMD_MASK,
474 SDHCI_INT_ENABLE);
475 /* Mask all sdhci interrupt sources */
476 sdhci_writel(host, 0x0, SDHCI_SIGNAL_ENABLE);
477
478 return 0;
479}
480
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600481/**
482 * sdhci_zynqmp_sdcardclk_set_phase - Set the SD Output Clock Tap Delays
483 *
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600484 * @host: Pointer to the sdhci_host structure.
485 * @degrees: The clock phase shift between 0 - 359.
Ashok Reddy Soma94a755d2021-07-09 05:53:39 -0600486 * Return: 0
Michal Simek2a43ab82021-07-09 05:53:43 -0600487 *
488 * Set the SD Output Clock Tap Delays for Output path
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600489 */
490static int sdhci_zynqmp_sdcardclk_set_phase(struct sdhci_host *host,
491 int degrees)
492{
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600493 struct mmc *mmc = (struct mmc *)host->mmc;
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600494 struct udevice *dev = mmc->dev;
495 struct arasan_sdhci_priv *priv = dev_get_priv(mmc->dev);
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600496 u8 tap_delay, tap_max = 0;
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600497 int timing = mode2timing[mmc->selected_mode];
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600498 int ret;
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600499
500 /*
501 * This is applicable for SDHCI_SPEC_300 and above
502 * ZynqMP does not set phase for <=25MHz clock.
503 * If degrees is zero, no need to do anything.
504 */
Ashok Reddy Soma6f645382021-07-09 05:53:40 -0600505 if (SDHCI_GET_VERSION(host) < SDHCI_SPEC_300)
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600506 return 0;
507
508 switch (timing) {
509 case MMC_TIMING_MMC_HS:
510 case MMC_TIMING_SD_HS:
511 case MMC_TIMING_UHS_SDR25:
512 case MMC_TIMING_UHS_DDR50:
513 case MMC_TIMING_MMC_DDR52:
514 /* For 50MHz clock, 30 Taps are available */
515 tap_max = 30;
516 break;
517 case MMC_TIMING_UHS_SDR50:
518 /* For 100MHz clock, 15 Taps are available */
519 tap_max = 15;
520 break;
521 case MMC_TIMING_UHS_SDR104:
522 case MMC_TIMING_MMC_HS200:
523 /* For 200MHz clock, 8 Taps are available */
524 tap_max = 8;
525 default:
526 break;
527 }
528
529 tap_delay = (degrees * tap_max) / 360;
530
Ashok Reddy Soma39a177a2021-07-09 05:53:42 -0600531 /* Limit output tap_delay value to 6 bits */
532 tap_delay &= SDHCI_ARASAN_OTAPDLY_SEL_MASK;
533
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600534 /* Set the Clock Phase */
Ashok Reddy Soma33154532022-09-30 03:25:47 -0600535 ret = arasan_zynqmp_set_out_tapdelay(priv->node_id, tap_delay);
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600536 if (ret) {
537 dev_err(dev, "Error setting output Tap Delay\n");
538 return ret;
539 }
540
541 /* Release DLL Reset */
Ashok Reddy Soma33154532022-09-30 03:25:47 -0600542 ret = zynqmp_dll_reset(priv->node_id, PM_DLL_RESET_RELEASE);
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600543 if (ret) {
544 dev_err(dev, "dll_reset release failed with err: %d\n", ret);
545 return ret;
546 }
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600547
Ashok Reddy Soma94a755d2021-07-09 05:53:39 -0600548 return 0;
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600549}
550
551/**
552 * sdhci_zynqmp_sampleclk_set_phase - Set the SD Input Clock Tap Delays
553 *
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600554 * @host: Pointer to the sdhci_host structure.
555 * @degrees: The clock phase shift between 0 - 359.
Ashok Reddy Soma94a755d2021-07-09 05:53:39 -0600556 * Return: 0
Michal Simek2a43ab82021-07-09 05:53:43 -0600557 *
558 * Set the SD Input Clock Tap Delays for Input path
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600559 */
560static int sdhci_zynqmp_sampleclk_set_phase(struct sdhci_host *host,
561 int degrees)
562{
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600563 struct mmc *mmc = (struct mmc *)host->mmc;
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600564 struct udevice *dev = mmc->dev;
565 struct arasan_sdhci_priv *priv = dev_get_priv(mmc->dev);
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600566 u8 tap_delay, tap_max = 0;
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600567 int timing = mode2timing[mmc->selected_mode];
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600568 int ret;
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600569
570 /*
571 * This is applicable for SDHCI_SPEC_300 and above
572 * ZynqMP does not set phase for <=25MHz clock.
573 * If degrees is zero, no need to do anything.
574 */
Ashok Reddy Soma6f645382021-07-09 05:53:40 -0600575 if (SDHCI_GET_VERSION(host) < SDHCI_SPEC_300)
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600576 return 0;
577
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600578 /* Assert DLL Reset */
Ashok Reddy Soma33154532022-09-30 03:25:47 -0600579 ret = zynqmp_dll_reset(priv->node_id, PM_DLL_RESET_ASSERT);
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600580 if (ret) {
581 dev_err(dev, "dll_reset assert failed with err: %d\n", ret);
582 return ret;
583 }
584
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600585 switch (timing) {
586 case MMC_TIMING_MMC_HS:
587 case MMC_TIMING_SD_HS:
588 case MMC_TIMING_UHS_SDR25:
589 case MMC_TIMING_UHS_DDR50:
590 case MMC_TIMING_MMC_DDR52:
591 /* For 50MHz clock, 120 Taps are available */
592 tap_max = 120;
593 break;
594 case MMC_TIMING_UHS_SDR50:
595 /* For 100MHz clock, 60 Taps are available */
596 tap_max = 60;
597 break;
598 case MMC_TIMING_UHS_SDR104:
599 case MMC_TIMING_MMC_HS200:
600 /* For 200MHz clock, 30 Taps are available */
601 tap_max = 30;
602 default:
603 break;
604 }
605
606 tap_delay = (degrees * tap_max) / 360;
607
Ashok Reddy Soma39a177a2021-07-09 05:53:42 -0600608 /* Limit input tap_delay value to 8 bits */
609 tap_delay &= SDHCI_ARASAN_ITAPDLY_SEL_MASK;
610
Ashok Reddy Soma33154532022-09-30 03:25:47 -0600611 ret = arasan_zynqmp_set_in_tapdelay(priv->node_id, tap_delay);
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600612 if (ret) {
613 dev_err(dev, "Error setting Input Tap Delay\n");
614 return ret;
615 }
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600616
Ashok Reddy Soma94a755d2021-07-09 05:53:39 -0600617 return 0;
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600618}
619
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600620/**
621 * sdhci_versal_sdcardclk_set_phase - Set the SD Output Clock Tap Delays
622 *
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600623 * @host: Pointer to the sdhci_host structure.
Michal Simek2a43ab82021-07-09 05:53:43 -0600624 * @degrees: The clock phase shift between 0 - 359.
Ashok Reddy Soma94a755d2021-07-09 05:53:39 -0600625 * Return: 0
Michal Simek2a43ab82021-07-09 05:53:43 -0600626 *
627 * Set the SD Output Clock Tap Delays for Output path
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600628 */
629static int sdhci_versal_sdcardclk_set_phase(struct sdhci_host *host,
630 int degrees)
631{
632 struct mmc *mmc = (struct mmc *)host->mmc;
633 u8 tap_delay, tap_max = 0;
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600634 int timing = mode2timing[mmc->selected_mode];
Ashok Reddy Soma24a51072021-07-09 05:53:41 -0600635 u32 regval;
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600636
637 /*
638 * This is applicable for SDHCI_SPEC_300 and above
639 * Versal does not set phase for <=25MHz clock.
640 * If degrees is zero, no need to do anything.
641 */
Ashok Reddy Soma6f645382021-07-09 05:53:40 -0600642 if (SDHCI_GET_VERSION(host) < SDHCI_SPEC_300)
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600643 return 0;
644
645 switch (timing) {
646 case MMC_TIMING_MMC_HS:
647 case MMC_TIMING_SD_HS:
648 case MMC_TIMING_UHS_SDR25:
649 case MMC_TIMING_UHS_DDR50:
650 case MMC_TIMING_MMC_DDR52:
651 /* For 50MHz clock, 30 Taps are available */
652 tap_max = 30;
653 break;
654 case MMC_TIMING_UHS_SDR50:
655 /* For 100MHz clock, 15 Taps are available */
656 tap_max = 15;
657 break;
658 case MMC_TIMING_UHS_SDR104:
659 case MMC_TIMING_MMC_HS200:
660 /* For 200MHz clock, 8 Taps are available */
661 tap_max = 8;
662 default:
663 break;
664 }
665
666 tap_delay = (degrees * tap_max) / 360;
667
Ashok Reddy Soma24a51072021-07-09 05:53:41 -0600668 /* Limit output tap_delay value to 6 bits */
669 tap_delay &= SDHCI_ARASAN_OTAPDLY_SEL_MASK;
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600670
Ashok Reddy Soma24a51072021-07-09 05:53:41 -0600671 /* Set the Clock Phase */
672 regval = sdhci_readl(host, SDHCI_ARASAN_OTAPDLY_REGISTER);
673 regval |= SDHCI_OTAPDLY_ENABLE;
674 sdhci_writel(host, regval, SDHCI_ARASAN_OTAPDLY_REGISTER);
675 regval &= ~SDHCI_ARASAN_OTAPDLY_SEL_MASK;
676 regval |= tap_delay;
677 sdhci_writel(host, regval, SDHCI_ARASAN_OTAPDLY_REGISTER);
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600678
Ashok Reddy Soma94a755d2021-07-09 05:53:39 -0600679 return 0;
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600680}
681
682/**
683 * sdhci_versal_sampleclk_set_phase - Set the SD Input Clock Tap Delays
684 *
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600685 * @host: Pointer to the sdhci_host structure.
Michal Simek2a43ab82021-07-09 05:53:43 -0600686 * @degrees: The clock phase shift between 0 - 359.
Ashok Reddy Soma94a755d2021-07-09 05:53:39 -0600687 * Return: 0
Michal Simek2a43ab82021-07-09 05:53:43 -0600688 *
689 * Set the SD Input Clock Tap Delays for Input path
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600690 */
691static int sdhci_versal_sampleclk_set_phase(struct sdhci_host *host,
692 int degrees)
693{
694 struct mmc *mmc = (struct mmc *)host->mmc;
695 u8 tap_delay, tap_max = 0;
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600696 int timing = mode2timing[mmc->selected_mode];
Ashok Reddy Soma24a51072021-07-09 05:53:41 -0600697 u32 regval;
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600698
699 /*
700 * This is applicable for SDHCI_SPEC_300 and above
701 * Versal does not set phase for <=25MHz clock.
702 * If degrees is zero, no need to do anything.
703 */
Ashok Reddy Soma6f645382021-07-09 05:53:40 -0600704 if (SDHCI_GET_VERSION(host) < SDHCI_SPEC_300)
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600705 return 0;
706
707 switch (timing) {
708 case MMC_TIMING_MMC_HS:
709 case MMC_TIMING_SD_HS:
710 case MMC_TIMING_UHS_SDR25:
711 case MMC_TIMING_UHS_DDR50:
712 case MMC_TIMING_MMC_DDR52:
713 /* For 50MHz clock, 120 Taps are available */
714 tap_max = 120;
715 break;
716 case MMC_TIMING_UHS_SDR50:
717 /* For 100MHz clock, 60 Taps are available */
718 tap_max = 60;
719 break;
720 case MMC_TIMING_UHS_SDR104:
721 case MMC_TIMING_MMC_HS200:
722 /* For 200MHz clock, 30 Taps are available */
723 tap_max = 30;
724 default:
725 break;
726 }
727
728 tap_delay = (degrees * tap_max) / 360;
729
Ashok Reddy Soma24a51072021-07-09 05:53:41 -0600730 /* Limit input tap_delay value to 8 bits */
731 tap_delay &= SDHCI_ARASAN_ITAPDLY_SEL_MASK;
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600732
Ashok Reddy Soma24a51072021-07-09 05:53:41 -0600733 /* Set the Clock Phase */
734 regval = sdhci_readl(host, SDHCI_ARASAN_ITAPDLY_REGISTER);
735 regval |= SDHCI_ITAPDLY_CHGWIN;
736 sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
737 regval |= SDHCI_ITAPDLY_ENABLE;
738 sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
739 regval &= ~SDHCI_ARASAN_ITAPDLY_SEL_MASK;
740 regval |= tap_delay;
741 sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
742 regval &= ~SDHCI_ITAPDLY_CHGWIN;
743 sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600744
Ashok Reddy Soma94a755d2021-07-09 05:53:39 -0600745 return 0;
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600746}
747
Ashok Reddy Soma0ab38fd2023-01-10 04:31:21 -0700748/**
749 * sdhci_versal_net_emmc_sdcardclk_set_phase - Set eMMC Output Clock Tap Delays
750 *
751 * @host: Pointer to the sdhci_host structure.
752 * @degrees: The clock phase shift between 0 - 359.
753 * Return: 0
754 *
755 * Set eMMC Output Clock Tap Delays for Output path
756 */
757static int sdhci_versal_net_emmc_sdcardclk_set_phase(struct sdhci_host *host, int degrees)
758{
759 struct mmc *mmc = (struct mmc *)host->mmc;
760 int timing = mode2timing[mmc->selected_mode];
761 u8 tap_delay, tap_max = 0;
762 u32 regval;
763
764 switch (timing) {
765 case MMC_TIMING_MMC_HS:
766 case MMC_TIMING_MMC_DDR52:
767 tap_max = 16;
768 break;
769 case MMC_TIMING_MMC_HS200:
770 case MMC_TIMING_MMC_HS400:
771 /* For 200MHz clock, 32 Taps are available */
772 tap_max = 32;
773 break;
774 default:
775 break;
776 }
777
778 tap_delay = (degrees * tap_max) / 360;
779 /* Set the Clock Phase */
780 if (tap_delay) {
781 regval = sdhci_readl(host, PHY_CTRL_REG1);
782 regval |= PHY_CTRL_OTAPDLY_ENA_MASK;
783 sdhci_writel(host, regval, PHY_CTRL_REG1);
784 regval &= ~PHY_CTRL_OTAPDLY_SEL_MASK;
785 regval |= tap_delay << PHY_CTRL_OTAPDLY_SEL_SHIFT;
786 sdhci_writel(host, regval, PHY_CTRL_REG1);
787 }
788
789 return 0;
790}
791
792/**
793 * sdhci_versal_net_emmc_sampleclk_set_phase - Set eMMC Input Clock Tap Delays
794 *
795 * @host: Pointer to the sdhci_host structure.
796 * @degrees: The clock phase shift between 0 - 359.
797 * Return: 0
798 *
799 * Set eMMC Input Clock Tap Delays for Input path. If HS400 is selected,
800 * set strobe90 and strobe180 in PHY_CTRL_REG1.
801 */
802static int sdhci_versal_net_emmc_sampleclk_set_phase(struct sdhci_host *host, int degrees)
803{
804 struct mmc *mmc = (struct mmc *)host->mmc;
805 int timing = mode2timing[mmc->selected_mode];
806 u8 tap_delay, tap_max = 0;
807 u32 regval;
808
809 switch (timing) {
810 case MMC_TIMING_MMC_HS:
811 case MMC_TIMING_MMC_DDR52:
812 tap_max = 32;
813 break;
814 case MMC_TIMING_MMC_HS400:
815 /* Strobe select tap point for strb90 and strb180 */
816 regval = sdhci_readl(host, PHY_CTRL_REG1);
817 regval &= ~PHY_CTRL_STRB_SEL_MASK;
818 regval |= VERSAL_NET_PHY_CTRL_STRB90_STRB180_VAL << PHY_CTRL_STRB_SEL_SHIFT;
819 sdhci_writel(host, regval, PHY_CTRL_REG1);
820 break;
821 default:
822 break;
823 }
824
825 tap_delay = (degrees * tap_max) / 360;
826 /* Set the Clock Phase */
827 if (tap_delay) {
828 regval = sdhci_readl(host, PHY_CTRL_REG1);
829 regval |= PHY_CTRL_ITAP_CHG_WIN_MASK;
830 sdhci_writel(host, regval, PHY_CTRL_REG1);
831 regval |= PHY_CTRL_ITAPDLY_ENA_MASK;
832 sdhci_writel(host, regval, PHY_CTRL_REG1);
833 regval &= ~PHY_CTRL_ITAPDLY_SEL_MASK;
834 regval |= tap_delay << PHY_CTRL_ITAPDLY_SEL_SHIFT;
835 sdhci_writel(host, regval, PHY_CTRL_REG1);
836 regval &= ~PHY_CTRL_ITAP_CHG_WIN_MASK;
837 sdhci_writel(host, regval, PHY_CTRL_REG1);
838 }
839
840 return 0;
841}
842
Ashok Reddy Soma21fd7632021-08-02 23:20:40 -0600843static int arasan_sdhci_set_tapdelay(struct sdhci_host *host)
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530844{
845 struct arasan_sdhci_priv *priv = dev_get_priv(host->mmc->dev);
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600846 struct arasan_sdhci_clk_data *clk_data = &priv->clk_data;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530847 struct mmc *mmc = (struct mmc *)host->mmc;
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600848 struct udevice *dev = mmc->dev;
849 u8 timing = mode2timing[mmc->selected_mode];
850 u32 iclk_phase = clk_data->clk_phase_in[timing];
851 u32 oclk_phase = clk_data->clk_phase_out[timing];
Ashok Reddy Soma21fd7632021-08-02 23:20:40 -0600852 int ret;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530853
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600854 dev_dbg(dev, "%s, host:%s, mode:%d\n", __func__, host->name, timing);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530855
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600856 if (IS_ENABLED(CONFIG_ARCH_ZYNQMP) &&
857 device_is_compatible(dev, "xlnx,zynqmp-8.9a")) {
Ashok Reddy Soma21fd7632021-08-02 23:20:40 -0600858 ret = sdhci_zynqmp_sampleclk_set_phase(host, iclk_phase);
859 if (ret)
860 return ret;
861
862 ret = sdhci_zynqmp_sdcardclk_set_phase(host, oclk_phase);
863 if (ret)
864 return ret;
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600865 } else if (IS_ENABLED(CONFIG_ARCH_VERSAL) &&
866 device_is_compatible(dev, "xlnx,versal-8.9a")) {
Ashok Reddy Soma21fd7632021-08-02 23:20:40 -0600867 ret = sdhci_versal_sampleclk_set_phase(host, iclk_phase);
868 if (ret)
869 return ret;
870
871 ret = sdhci_versal_sdcardclk_set_phase(host, oclk_phase);
872 if (ret)
873 return ret;
Ashok Reddy Soma0ab38fd2023-01-10 04:31:21 -0700874 } else if (IS_ENABLED(CONFIG_ARCH_VERSAL_NET) &&
875 device_is_compatible(dev, "xlnx,versal-net-5.1-emmc")) {
876 if (mmc->clock >= MIN_PHY_CLK_HZ)
877 if (iclk_phase == VERSAL_NET_EMMC_ICLK_PHASE_DDR52_DLY_CHAIN)
878 iclk_phase = VERSAL_NET_EMMC_ICLK_PHASE_DDR52_DLL;
879
880 ret = sdhci_versal_net_emmc_sampleclk_set_phase(host, iclk_phase);
881 if (ret)
882 return ret;
883
884 ret = sdhci_versal_net_emmc_sdcardclk_set_phase(host, oclk_phase);
885 if (ret)
886 return ret;
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600887 }
Ashok Reddy Soma21fd7632021-08-02 23:20:40 -0600888
889 return 0;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530890}
891
Michal Simek33a6b772020-10-23 04:59:00 -0600892static void arasan_dt_read_clk_phase(struct udevice *dev, unsigned char timing,
893 const char *prop)
894{
895 struct arasan_sdhci_priv *priv = dev_get_priv(dev);
896 struct arasan_sdhci_clk_data *clk_data = &priv->clk_data;
897 u32 clk_phase[2] = {0};
898
899 /*
900 * Read Tap Delay values from DT, if the DT does not contain the
901 * Tap Values then use the pre-defined values
902 */
903 if (dev_read_u32_array(dev, prop, &clk_phase[0], 2)) {
904 dev_dbg(dev, "Using predefined clock phase for %s = %d %d\n",
905 prop, clk_data->clk_phase_in[timing],
906 clk_data->clk_phase_out[timing]);
907 return;
908 }
909
910 /* The values read are Input and Output Clock Delays in order */
911 clk_data->clk_phase_in[timing] = clk_phase[0];
912 clk_data->clk_phase_out[timing] = clk_phase[1];
913}
914
915/**
916 * arasan_dt_parse_clk_phases - Read Tap Delay values from DT
917 *
Michal Simek33a6b772020-10-23 04:59:00 -0600918 * @dev: Pointer to our struct udevice.
Michal Simek2a43ab82021-07-09 05:53:43 -0600919 *
920 * Called at initialization to parse the values of Tap Delays.
Michal Simek33a6b772020-10-23 04:59:00 -0600921 */
922static void arasan_dt_parse_clk_phases(struct udevice *dev)
923{
924 struct arasan_sdhci_priv *priv = dev_get_priv(dev);
925 struct arasan_sdhci_clk_data *clk_data = &priv->clk_data;
926 int i;
927
928 if (IS_ENABLED(CONFIG_ARCH_ZYNQMP) &&
929 device_is_compatible(dev, "xlnx,zynqmp-8.9a")) {
930 for (i = 0; i <= MMC_TIMING_MMC_HS400; i++) {
931 clk_data->clk_phase_in[i] = zynqmp_iclk_phases[i];
932 clk_data->clk_phase_out[i] = zynqmp_oclk_phases[i];
933 }
934
935 if (priv->bank == MMC_BANK2) {
936 clk_data->clk_phase_out[MMC_TIMING_UHS_SDR104] = 90;
937 clk_data->clk_phase_out[MMC_TIMING_MMC_HS200] = 90;
938 }
939 }
940
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600941 if (IS_ENABLED(CONFIG_ARCH_VERSAL) &&
942 device_is_compatible(dev, "xlnx,versal-8.9a")) {
943 for (i = 0; i <= MMC_TIMING_MMC_HS400; i++) {
944 clk_data->clk_phase_in[i] = versal_iclk_phases[i];
945 clk_data->clk_phase_out[i] = versal_oclk_phases[i];
946 }
947 }
948
Ashok Reddy Soma0ab38fd2023-01-10 04:31:21 -0700949 if (IS_ENABLED(CONFIG_ARCH_VERSAL_NET) &&
950 device_is_compatible(dev, "xlnx,versal-net-5.1-emmc")) {
951 for (i = 0; i <= MMC_TIMING_MMC_HS400; i++) {
952 clk_data->clk_phase_in[i] = versal_net_emmc_iclk_phases[i];
953 clk_data->clk_phase_out[i] = versal_net_emmc_oclk_phases[i];
954 }
955 }
956
Michal Simek33a6b772020-10-23 04:59:00 -0600957 arasan_dt_read_clk_phase(dev, MMC_TIMING_LEGACY,
958 "clk-phase-legacy");
959 arasan_dt_read_clk_phase(dev, MMC_TIMING_MMC_HS,
960 "clk-phase-mmc-hs");
961 arasan_dt_read_clk_phase(dev, MMC_TIMING_SD_HS,
962 "clk-phase-sd-hs");
963 arasan_dt_read_clk_phase(dev, MMC_TIMING_UHS_SDR12,
964 "clk-phase-uhs-sdr12");
965 arasan_dt_read_clk_phase(dev, MMC_TIMING_UHS_SDR25,
966 "clk-phase-uhs-sdr25");
967 arasan_dt_read_clk_phase(dev, MMC_TIMING_UHS_SDR50,
968 "clk-phase-uhs-sdr50");
969 arasan_dt_read_clk_phase(dev, MMC_TIMING_UHS_SDR104,
970 "clk-phase-uhs-sdr104");
971 arasan_dt_read_clk_phase(dev, MMC_TIMING_UHS_DDR50,
972 "clk-phase-uhs-ddr50");
973 arasan_dt_read_clk_phase(dev, MMC_TIMING_MMC_DDR52,
974 "clk-phase-mmc-ddr52");
975 arasan_dt_read_clk_phase(dev, MMC_TIMING_MMC_HS200,
976 "clk-phase-mmc-hs200");
977 arasan_dt_read_clk_phase(dev, MMC_TIMING_MMC_HS400,
978 "clk-phase-mmc-hs400");
979}
980
Michal Simek635cf4a2021-07-09 05:53:44 -0600981static const struct sdhci_ops arasan_ops = {
982 .platform_execute_tuning = &arasan_sdhci_execute_tuning,
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530983 .set_delay = &arasan_sdhci_set_tapdelay,
Ashok Reddy Soma8ee49f02021-08-02 23:20:46 -0600984 .set_control_reg = &sdhci_set_control_reg,
Ashok Reddy Soma0ab38fd2023-01-10 04:31:21 -0700985#if defined(CONFIG_ARCH_VERSAL_NET)
986 .config_dll = &arasan_sdhci_config_dll,
987#endif
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530988};
989#endif
990
Ashok Reddy Somaca7d8472022-02-23 15:36:05 +0100991#if defined(CONFIG_ARCH_ZYNQMP)
992static int sdhci_zynqmp_set_dynamic_config(struct arasan_sdhci_priv *priv,
993 struct udevice *dev)
994{
995 int ret;
Ashok Reddy Somaca7d8472022-02-23 15:36:05 +0100996 struct clk clk;
997 unsigned long clock, mhz;
998
Ashok Reddy Soma33154532022-09-30 03:25:47 -0600999 ret = xilinx_pm_request(PM_REQUEST_NODE, priv->node_id,
1000 ZYNQMP_PM_CAPABILITY_ACCESS, ZYNQMP_PM_MAX_QOS,
1001 ZYNQMP_PM_REQUEST_ACK_NO, NULL);
Ashok Reddy Somaca7d8472022-02-23 15:36:05 +01001002 if (ret) {
Ashok Reddy Soma33154532022-09-30 03:25:47 -06001003 dev_err(dev, "Request node failed for %d\n", priv->node_id);
Ashok Reddy Somaca7d8472022-02-23 15:36:05 +01001004 return ret;
1005 }
1006
1007 ret = reset_get_bulk(dev, &priv->resets);
1008 if (ret == -ENOTSUPP || ret == -ENOENT) {
1009 dev_err(dev, "Reset not found\n");
1010 return 0;
1011 } else if (ret) {
1012 dev_err(dev, "Reset failed\n");
1013 return ret;
1014 }
1015
1016 ret = reset_assert_bulk(&priv->resets);
1017 if (ret) {
1018 dev_err(dev, "Reset assert failed\n");
1019 return ret;
1020 }
1021
Ashok Reddy Soma33154532022-09-30 03:25:47 -06001022 ret = zynqmp_pm_set_sd_config(priv->node_id, SD_CONFIG_FIXED, 0);
Ashok Reddy Somaca7d8472022-02-23 15:36:05 +01001023 if (ret) {
1024 dev_err(dev, "SD_CONFIG_FIXED failed\n");
1025 return ret;
1026 }
1027
Ashok Reddy Soma33154532022-09-30 03:25:47 -06001028 ret = zynqmp_pm_set_sd_config(priv->node_id, SD_CONFIG_EMMC_SEL,
Ashok Reddy Somaca7d8472022-02-23 15:36:05 +01001029 dev_read_bool(dev, "non-removable"));
1030 if (ret) {
1031 dev_err(dev, "SD_CONFIG_EMMC_SEL failed\n");
1032 return ret;
1033 }
1034
1035 ret = clk_get_by_index(dev, 0, &clk);
1036 if (ret < 0) {
1037 dev_err(dev, "failed to get clock\n");
1038 return ret;
1039 }
1040
1041 clock = clk_get_rate(&clk);
1042 if (IS_ERR_VALUE(clock)) {
1043 dev_err(dev, "failed to get rate\n");
1044 return clock;
1045 }
1046
1047 mhz = DIV64_U64_ROUND_UP(clock, 1000000);
1048
Ashok Reddy Soma088febe2022-03-25 13:11:10 +01001049 if (mhz > 100 && mhz <= 200)
1050 mhz = 200;
1051 else if (mhz > 50 && mhz <= 100)
1052 mhz = 100;
1053 else if (mhz > 25 && mhz <= 50)
1054 mhz = 50;
1055 else
1056 mhz = 25;
1057
Ashok Reddy Soma33154532022-09-30 03:25:47 -06001058 ret = zynqmp_pm_set_sd_config(priv->node_id, SD_CONFIG_BASECLK, mhz);
Ashok Reddy Somaca7d8472022-02-23 15:36:05 +01001059 if (ret) {
1060 dev_err(dev, "SD_CONFIG_BASECLK failed\n");
1061 return ret;
1062 }
1063
Ashok Reddy Soma33154532022-09-30 03:25:47 -06001064 ret = zynqmp_pm_set_sd_config(priv->node_id, SD_CONFIG_8BIT,
Ashok Reddy Somaca7d8472022-02-23 15:36:05 +01001065 (dev_read_u32_default(dev, "bus-width", 1) == 8));
1066 if (ret) {
1067 dev_err(dev, "SD_CONFIG_8BIT failed\n");
1068 return ret;
1069 }
1070
1071 ret = reset_deassert_bulk(&priv->resets);
1072 if (ret) {
1073 dev_err(dev, "Reset release failed\n");
1074 return ret;
1075 }
1076
1077 return 0;
1078}
1079#endif
1080
Michal Simek9ecd2682015-11-30 16:13:03 +01001081static int arasan_sdhci_probe(struct udevice *dev)
Michal Simek0dd222b2013-04-22 14:56:49 +02001082{
Simon Glassfa20e932020-12-03 16:55:20 -07001083 struct arasan_sdhci_plat *plat = dev_get_plat(dev);
Michal Simek9ecd2682015-11-30 16:13:03 +01001084 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +05301085 struct arasan_sdhci_priv *priv = dev_get_priv(dev);
1086 struct sdhci_host *host;
Stefan Herbrechtsmeier739ae402017-01-17 16:27:32 +01001087 struct clk clk;
1088 unsigned long clock;
Simon Glass4cc87fb2016-07-05 17:10:15 -06001089 int ret;
Michal Simek0dd222b2013-04-22 14:56:49 +02001090
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +05301091 host = priv->host;
1092
Ashok Reddy Somaca7d8472022-02-23 15:36:05 +01001093#if defined(CONFIG_ARCH_ZYNQMP)
1094 if (device_is_compatible(dev, "xlnx,zynqmp-8.9a")) {
1095 ret = zynqmp_pm_is_function_supported(PM_IOCTL,
1096 IOCTL_SET_SD_CONFIG);
1097 if (!ret) {
1098 ret = sdhci_zynqmp_set_dynamic_config(priv, dev);
1099 if (ret)
1100 return ret;
1101 }
1102 }
1103#endif
Ashok Reddy Soma0ab38fd2023-01-10 04:31:21 -07001104 if (device_is_compatible(dev, "xlnx,versal-net-5.1-emmc"))
1105 priv->internal_phy_reg = true;
Ashok Reddy Somaca7d8472022-02-23 15:36:05 +01001106
Stefan Herbrechtsmeier739ae402017-01-17 16:27:32 +01001107 ret = clk_get_by_index(dev, 0, &clk);
1108 if (ret < 0) {
1109 dev_err(dev, "failed to get clock\n");
1110 return ret;
1111 }
1112
1113 clock = clk_get_rate(&clk);
1114 if (IS_ERR_VALUE(clock)) {
1115 dev_err(dev, "failed to get rate\n");
1116 return clock;
1117 }
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +05301118
Ashok Reddy Somaca7d8472022-02-23 15:36:05 +01001119 dev_dbg(dev, "%s: CLK %ld\n", __func__, clock);
Stefan Herbrechtsmeier739ae402017-01-17 16:27:32 +01001120
1121 ret = clk_enable(&clk);
Michal Simek41710952021-02-09 15:28:15 +01001122 if (ret) {
Stefan Herbrechtsmeier739ae402017-01-17 16:27:32 +01001123 dev_err(dev, "failed to enable clock\n");
1124 return ret;
1125 }
1126
Siva Durga Prasad Paladugu049e0032014-07-08 15:31:04 +05301127 host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD |
Siva Durga Prasad Paladugu0d6891b2014-01-22 09:17:09 +01001128 SDHCI_QUIRK_BROKEN_R1B;
Siva Durga Prasad Paladugua1619fe2016-01-12 15:12:16 +05301129
1130#ifdef CONFIG_ZYNQ_HISPD_BROKEN
Hannes Schmelzer94a5bbc2018-03-07 08:00:57 +01001131 host->quirks |= SDHCI_QUIRK_BROKEN_HISPD_MODE;
Siva Durga Prasad Paladugua1619fe2016-01-12 15:12:16 +05301132#endif
1133
Ashok Reddy Soma61e0df92020-10-23 04:58:57 -06001134 if (priv->no_1p8)
1135 host->quirks |= SDHCI_QUIRK_NO_1_8_V;
1136
Ashok Reddy Soma407a5612023-01-10 04:31:24 -07001137 if (CONFIG_IS_ENABLED(ARCH_VERSAL_NET) &&
1138 device_is_compatible(dev, "xlnx,versal-net-5.1-emmc"))
1139 host->quirks |= SDHCI_QUIRK_CAPS_BIT63_FOR_HS400;
1140
Benedikt Grassl529e6f02020-04-14 07:32:12 +02001141 plat->cfg.f_max = CONFIG_ZYNQ_SDHCI_MAX_FREQ;
1142
1143 ret = mmc_of_parse(dev, &plat->cfg);
1144 if (ret)
1145 return ret;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +05301146
Stefan Herbrechtsmeier739ae402017-01-17 16:27:32 +01001147 host->max_clk = clock;
Stefan Herbrechtsmeierbc47e0e2017-01-17 15:58:48 +01001148
Matwey V. Kornilov194b8602019-08-01 18:00:05 +03001149 host->mmc = &plat->mmc;
1150 host->mmc->dev = dev;
1151 host->mmc->priv = host;
1152
Benedikt Grassl529e6f02020-04-14 07:32:12 +02001153 ret = sdhci_setup_cfg(&plat->cfg, host, plat->cfg.f_max,
Jaehoon Chung8a5ffbb2016-07-26 19:06:24 +09001154 CONFIG_ZYNQ_SDHCI_MIN_FREQ);
Simon Glass4cc87fb2016-07-05 17:10:15 -06001155 if (ret)
1156 return ret;
Simon Glass4cc87fb2016-07-05 17:10:15 -06001157 upriv->mmc = host->mmc;
Michal Simek9ecd2682015-11-30 16:13:03 +01001158
T Karthik Reddyab0a7492021-08-02 23:20:45 -06001159 /*
1160 * WORKAROUND: Versal platforms have an issue with card detect state.
1161 * Due to this, host controller is switching off voltage to sd card
1162 * causing sd card timeout error. Workaround this by adding a wait for
1163 * 1000msec till the card detect state gets stable.
1164 */
Ashok Reddy Soma6556bff2022-02-23 15:13:32 +01001165 if (IS_ENABLED(CONFIG_ARCH_ZYNQMP) || IS_ENABLED(CONFIG_ARCH_VERSAL)) {
Ashok Reddy Somadd745772022-02-23 15:13:31 +01001166 u32 timeout = 1000000;
T Karthik Reddyab0a7492021-08-02 23:20:45 -06001167
1168 while (((sdhci_readl(host, SDHCI_PRESENT_STATE) &
Ashok Reddy Soma09f38902022-02-23 15:13:30 +01001169 SDHCI_CARD_STATE_STABLE) == 0) && timeout) {
Ashok Reddy Somadd745772022-02-23 15:13:31 +01001170 udelay(1);
Ashok Reddy Soma09f38902022-02-23 15:13:30 +01001171 timeout--;
T Karthik Reddyab0a7492021-08-02 23:20:45 -06001172 }
1173 if (!timeout) {
1174 dev_err(dev, "Sdhci card detect state not stable\n");
1175 return -ETIMEDOUT;
1176 }
1177 }
1178
Simon Glass4cc87fb2016-07-05 17:10:15 -06001179 return sdhci_probe(dev);
Michal Simek0dd222b2013-04-22 14:56:49 +02001180}
Michal Simek9ecd2682015-11-30 16:13:03 +01001181
Simon Glassaad29ae2020-12-03 16:55:21 -07001182static int arasan_sdhci_of_to_plat(struct udevice *dev)
Michal Simek9ecd2682015-11-30 16:13:03 +01001183{
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +05301184 struct arasan_sdhci_priv *priv = dev_get_priv(dev);
Ashok Reddy Soma33154532022-09-30 03:25:47 -06001185 u32 pm_info[2];
Michal Simek9ecd2682015-11-30 16:13:03 +01001186
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +05301187 priv->host = calloc(1, sizeof(struct sdhci_host));
1188 if (!priv->host)
1189 return -1;
1190
1191 priv->host->name = dev->name;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +05301192
Ashok Reddy Soma0ab38fd2023-01-10 04:31:21 -07001193#if defined(CONFIG_ARCH_ZYNQMP) || defined(CONFIG_ARCH_VERSAL) || defined(CONFIG_ARCH_VERSAL_NET)
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +05301194 priv->host->ops = &arasan_ops;
Michal Simek33a6b772020-10-23 04:59:00 -06001195 arasan_dt_parse_clk_phases(dev);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +05301196#endif
Michal Simek9ecd2682015-11-30 16:13:03 +01001197
Michal Simek921a8de2018-05-16 10:57:07 +02001198 priv->host->ioaddr = (void *)dev_read_addr(dev);
1199 if (IS_ERR(priv->host->ioaddr))
1200 return PTR_ERR(priv->host->ioaddr);
1201
Michal Simeke40ae572020-07-22 17:46:31 +02001202 priv->bank = dev_read_u32_default(dev, "xlnx,mio-bank", 0);
Ashok Reddy Soma61e0df92020-10-23 04:58:57 -06001203 priv->no_1p8 = dev_read_bool(dev, "no-1-8-v");
Stefan Herbrechtsmeier5567b422017-01-17 16:27:33 +01001204
Ashok Reddy Soma33154532022-09-30 03:25:47 -06001205 priv->node_id = 0;
1206 if (!dev_read_u32_array(dev, "power-domains", pm_info, ARRAY_SIZE(pm_info)))
1207 priv->node_id = pm_info[1];
1208
Michal Simek9ecd2682015-11-30 16:13:03 +01001209 return 0;
1210}
1211
Simon Glass4cc87fb2016-07-05 17:10:15 -06001212static int arasan_sdhci_bind(struct udevice *dev)
1213{
Simon Glassfa20e932020-12-03 16:55:20 -07001214 struct arasan_sdhci_plat *plat = dev_get_plat(dev);
Simon Glass4cc87fb2016-07-05 17:10:15 -06001215
Masahiro Yamadacdb67f32016-09-06 22:17:32 +09001216 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
Simon Glass4cc87fb2016-07-05 17:10:15 -06001217}
1218
Michal Simek9ecd2682015-11-30 16:13:03 +01001219static const struct udevice_id arasan_sdhci_ids[] = {
1220 { .compatible = "arasan,sdhci-8.9a" },
Ashok Reddy Soma0ab38fd2023-01-10 04:31:21 -07001221 { .compatible = "xlnx,versal-net-5.1-emmc" },
Michal Simek9ecd2682015-11-30 16:13:03 +01001222 { }
1223};
1224
1225U_BOOT_DRIVER(arasan_sdhci_drv) = {
1226 .name = "arasan_sdhci",
1227 .id = UCLASS_MMC,
1228 .of_match = arasan_sdhci_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -07001229 .of_to_plat = arasan_sdhci_of_to_plat,
Simon Glass4cc87fb2016-07-05 17:10:15 -06001230 .ops = &sdhci_ops,
1231 .bind = arasan_sdhci_bind,
Michal Simek9ecd2682015-11-30 16:13:03 +01001232 .probe = arasan_sdhci_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001233 .priv_auto = sizeof(struct arasan_sdhci_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -07001234 .plat_auto = sizeof(struct arasan_sdhci_plat),
Michal Simek9ecd2682015-11-30 16:13:03 +01001235};