blob: 7dcf6ad84220fa7b0115de8de9c475ba70d2ad5f [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>
Ashok Reddy Somaca7d8472022-02-23 15:36:05 +010015#include <reset.h>
Simon Glass9bc15642020-02-03 07:36:16 -070016#include <dm/device_compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070017#include <linux/err.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090018#include <linux/libfdt.h>
Ashok Reddy Somaca7d8472022-02-23 15:36:05 +010019#include <asm/types.h>
20#include <linux/math64.h>
Ashok Reddy Soma7a5f8102021-08-02 23:20:44 -060021#include <asm/cache.h>
Michal Simek0dd222b2013-04-22 14:56:49 +020022#include <malloc.h>
23#include <sdhci.h>
Ashok Reddy Soma467d0782021-08-02 23:20:43 -060024#include <zynqmp_firmware.h>
Michal Simek0dd222b2013-04-22 14:56:49 +020025
Ashok Reddy Soma24a51072021-07-09 05:53:41 -060026#define SDHCI_ARASAN_ITAPDLY_REGISTER 0xF0F8
27#define SDHCI_ARASAN_ITAPDLY_SEL_MASK GENMASK(7, 0)
28#define SDHCI_ARASAN_OTAPDLY_REGISTER 0xF0FC
29#define SDHCI_ARASAN_OTAPDLY_SEL_MASK GENMASK(5, 0)
30#define SDHCI_ITAPDLY_CHGWIN BIT(9)
31#define SDHCI_ITAPDLY_ENABLE BIT(8)
32#define SDHCI_OTAPDLY_ENABLE BIT(6)
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -060033
Michal Simek5b5101e2020-10-23 04:58:59 -060034#define SDHCI_TUNING_LOOP_COUNT 40
Michal Simek33a6b772020-10-23 04:59:00 -060035#define MMC_BANK2 0x2
36
Ashok Reddy Soma7a5f8102021-08-02 23:20:44 -060037#define SD_DLL_CTRL 0xFF180358
38#define SD_ITAP_DLY 0xFF180314
39#define SD_OTAP_DLY 0xFF180318
40#define SD0_DLL_RST BIT(2)
41#define SD1_DLL_RST BIT(18)
42#define SD0_ITAPCHGWIN BIT(9)
43#define SD1_ITAPCHGWIN BIT(25)
44#define SD0_ITAPDLYENA BIT(8)
45#define SD1_ITAPDLYENA BIT(24)
46#define SD0_ITAPDLYSEL_MASK GENMASK(7, 0)
47#define SD1_ITAPDLYSEL_MASK GENMASK(23, 16)
48#define SD0_OTAPDLYSEL_MASK GENMASK(5, 0)
49#define SD1_OTAPDLYSEL_MASK GENMASK(21, 16)
50
Michal Simek33a6b772020-10-23 04:59:00 -060051struct arasan_sdhci_clk_data {
52 int clk_phase_in[MMC_TIMING_MMC_HS400 + 1];
53 int clk_phase_out[MMC_TIMING_MMC_HS400 + 1];
54};
Michal Simek5b5101e2020-10-23 04:58:59 -060055
Simon Glass4cc87fb2016-07-05 17:10:15 -060056struct arasan_sdhci_plat {
57 struct mmc_config cfg;
58 struct mmc mmc;
59};
60
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +053061struct arasan_sdhci_priv {
62 struct sdhci_host *host;
Michal Simek33a6b772020-10-23 04:59:00 -060063 struct arasan_sdhci_clk_data clk_data;
Ashok Reddy Soma33154532022-09-30 03:25:47 -060064 u32 node_id;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +053065 u8 bank;
Ashok Reddy Soma61e0df92020-10-23 04:58:57 -060066 u8 no_1p8;
Ashok Reddy Somaca7d8472022-02-23 15:36:05 +010067 struct reset_ctl_bulk resets;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +053068};
69
Ashok Reddy Soma7a5f8102021-08-02 23:20:44 -060070/* For Versal platforms zynqmp_mmio_write() won't be available */
71__weak int zynqmp_mmio_write(const u32 address, const u32 mask, const u32 value)
72{
73 return 0;
74}
75
T Karthik Reddyd0618272021-10-01 16:38:38 +053076__weak int xilinx_pm_request(u32 api_id, u32 arg0, u32 arg1, u32 arg2,
77 u32 arg3, u32 *ret_payload)
78{
79 return 0;
80}
81
T Karthik Reddy0b35fa22022-04-27 10:27:12 +020082__weak int zynqmp_pm_is_function_supported(const u32 api_id, const u32 id)
83{
84 return 1;
85}
86
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -060087#if defined(CONFIG_ARCH_ZYNQMP) || defined(CONFIG_ARCH_VERSAL)
Michal Simek33a6b772020-10-23 04:59:00 -060088/* Default settings for ZynqMP Clock Phases */
Michal Simek635cf4a2021-07-09 05:53:44 -060089static const u32 zynqmp_iclk_phases[] = {0, 63, 63, 0, 63, 0,
90 0, 183, 54, 0, 0};
91static const u32 zynqmp_oclk_phases[] = {0, 72, 60, 0, 60, 72,
92 135, 48, 72, 135, 0};
Michal Simek33a6b772020-10-23 04:59:00 -060093
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -060094/* Default settings for Versal Clock Phases */
Michal Simek635cf4a2021-07-09 05:53:44 -060095static const u32 versal_iclk_phases[] = {0, 132, 132, 0, 132,
96 0, 0, 162, 90, 0, 0};
97static const u32 versal_oclk_phases[] = {0, 60, 48, 0, 48, 72,
98 90, 36, 60, 90, 0};
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -060099
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530100static const u8 mode2timing[] = {
Ashok Reddy Soma0afdfe32020-10-23 04:58:58 -0600101 [MMC_LEGACY] = MMC_TIMING_LEGACY,
102 [MMC_HS] = MMC_TIMING_MMC_HS,
103 [SD_HS] = MMC_TIMING_SD_HS,
Ashok Reddy Soma5ff7acf2022-06-27 14:22:45 +0530104 [MMC_HS_52] = MMC_TIMING_MMC_HS,
105 [MMC_DDR_52] = MMC_TIMING_MMC_DDR52,
Ashok Reddy Soma0afdfe32020-10-23 04:58:58 -0600106 [UHS_SDR12] = MMC_TIMING_UHS_SDR12,
107 [UHS_SDR25] = MMC_TIMING_UHS_SDR25,
108 [UHS_SDR50] = MMC_TIMING_UHS_SDR50,
109 [UHS_DDR50] = MMC_TIMING_UHS_DDR50,
110 [UHS_SDR104] = MMC_TIMING_UHS_SDR104,
111 [MMC_HS_200] = MMC_TIMING_MMC_HS200,
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530112};
113
Ashok Reddy Somaa23e6ee2022-09-30 03:25:46 -0600114static inline int arasan_zynqmp_set_in_tapdelay(u32 node_id, u32 itap_delay)
Ashok Reddy Soma7a5f8102021-08-02 23:20:44 -0600115{
116 int ret;
117
118 if (IS_ENABLED(CONFIG_SPL_BUILD) || current_el() == 3) {
119 if (node_id == NODE_SD_0) {
120 ret = zynqmp_mmio_write(SD_ITAP_DLY, SD0_ITAPCHGWIN,
121 SD0_ITAPCHGWIN);
122 if (ret)
123 return ret;
124
125 ret = zynqmp_mmio_write(SD_ITAP_DLY, SD0_ITAPDLYENA,
126 SD0_ITAPDLYENA);
127 if (ret)
128 return ret;
129
130 ret = zynqmp_mmio_write(SD_ITAP_DLY, SD0_ITAPDLYSEL_MASK,
131 itap_delay);
132 if (ret)
133 return ret;
134
135 ret = zynqmp_mmio_write(SD_ITAP_DLY, SD0_ITAPCHGWIN, 0);
136 if (ret)
137 return ret;
138 }
139 ret = zynqmp_mmio_write(SD_ITAP_DLY, SD1_ITAPCHGWIN,
140 SD1_ITAPCHGWIN);
141 if (ret)
142 return ret;
143
144 ret = zynqmp_mmio_write(SD_ITAP_DLY, SD1_ITAPDLYENA,
145 SD1_ITAPDLYENA);
146 if (ret)
147 return ret;
148
149 ret = zynqmp_mmio_write(SD_ITAP_DLY, SD1_ITAPDLYSEL_MASK,
150 (itap_delay << 16));
151 if (ret)
152 return ret;
153
154 ret = zynqmp_mmio_write(SD_ITAP_DLY, SD1_ITAPCHGWIN, 0);
155 if (ret)
156 return ret;
157 } else {
Ashok Reddy Somaa23e6ee2022-09-30 03:25:46 -0600158 return xilinx_pm_request(PM_IOCTL, node_id,
Ashok Reddy Soma7a5f8102021-08-02 23:20:44 -0600159 IOCTL_SET_SD_TAPDELAY,
160 PM_TAPDELAY_INPUT, itap_delay, NULL);
161 }
162
163 return 0;
164}
165
Ashok Reddy Somaa23e6ee2022-09-30 03:25:46 -0600166static inline int arasan_zynqmp_set_out_tapdelay(u32 node_id, u32 otap_delay)
Ashok Reddy Soma7a5f8102021-08-02 23:20:44 -0600167{
168 if (IS_ENABLED(CONFIG_SPL_BUILD) || current_el() == 3) {
169 if (node_id == NODE_SD_0)
170 return zynqmp_mmio_write(SD_OTAP_DLY,
171 SD0_OTAPDLYSEL_MASK,
172 otap_delay);
173
174 return zynqmp_mmio_write(SD_OTAP_DLY, SD1_OTAPDLYSEL_MASK,
175 (otap_delay << 16));
176 } else {
Ashok Reddy Somaa23e6ee2022-09-30 03:25:46 -0600177 return xilinx_pm_request(PM_IOCTL, node_id,
Ashok Reddy Soma7a5f8102021-08-02 23:20:44 -0600178 IOCTL_SET_SD_TAPDELAY,
179 PM_TAPDELAY_OUTPUT, otap_delay, NULL);
180 }
181}
182
Ashok Reddy Somaa23e6ee2022-09-30 03:25:46 -0600183static inline int zynqmp_dll_reset(u32 node_id, u32 type)
Ashok Reddy Soma7a5f8102021-08-02 23:20:44 -0600184{
185 if (IS_ENABLED(CONFIG_SPL_BUILD) || current_el() == 3) {
186 if (node_id == NODE_SD_0)
187 return zynqmp_mmio_write(SD_DLL_CTRL, SD0_DLL_RST,
188 type == PM_DLL_RESET_ASSERT ?
189 SD0_DLL_RST : 0);
190
191 return zynqmp_mmio_write(SD_DLL_CTRL, SD1_DLL_RST,
192 type == PM_DLL_RESET_ASSERT ?
193 SD1_DLL_RST : 0);
194 } else {
Ashok Reddy Somaa23e6ee2022-09-30 03:25:46 -0600195 return xilinx_pm_request(PM_IOCTL, node_id,
Ashok Reddy Soma7a5f8102021-08-02 23:20:44 -0600196 IOCTL_SD_DLL_RESET, type, 0, NULL);
197 }
198}
199
Ashok Reddy Somaa23e6ee2022-09-30 03:25:46 -0600200static int arasan_zynqmp_dll_reset(struct sdhci_host *host, u32 node_id)
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530201{
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600202 struct mmc *mmc = (struct mmc *)host->mmc;
203 struct udevice *dev = mmc->dev;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530204 unsigned long timeout;
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600205 int ret;
206 u16 clk;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530207
208 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
209 clk &= ~(SDHCI_CLOCK_CARD_EN);
210 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
211
212 /* Issue DLL Reset */
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600213 ret = zynqmp_dll_reset(node_id, PM_DLL_RESET_ASSERT);
214 if (ret) {
215 dev_err(dev, "dll_reset assert failed with err: %d\n", ret);
216 return ret;
217 }
218
219 /* Allow atleast 1ms delay for proper DLL reset */
220 mdelay(1);
221 ret = zynqmp_dll_reset(node_id, PM_DLL_RESET_RELEASE);
222 if (ret) {
223 dev_err(dev, "dll_reset release failed with err: %d\n", ret);
224 return ret;
225 }
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530226
227 /* Wait max 20 ms */
228 timeout = 100;
229 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
230 & SDHCI_CLOCK_INT_STABLE)) {
231 if (timeout == 0) {
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600232 dev_err(dev, ": Internal clock never stabilised.\n");
233 return -EBUSY;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530234 }
235 timeout--;
236 udelay(1000);
237 }
238
239 clk |= SDHCI_CLOCK_CARD_EN;
240 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600241
242 return 0;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530243}
244
245static int arasan_sdhci_execute_tuning(struct mmc *mmc, u8 opcode)
246{
247 struct mmc_cmd cmd;
248 struct mmc_data data;
249 u32 ctrl;
250 struct sdhci_host *host;
251 struct arasan_sdhci_priv *priv = dev_get_priv(mmc->dev);
Michal Simek91e95ff2018-06-13 09:12:29 +0200252 char tuning_loop_counter = SDHCI_TUNING_LOOP_COUNT;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530253
Ashok Reddy Somaca7d8472022-02-23 15:36:05 +0100254 dev_dbg(mmc->dev, "%s\n", __func__);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530255
256 host = priv->host;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530257
Faiz Abbas2eddc002019-06-11 00:43:40 +0530258 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530259 ctrl |= SDHCI_CTRL_EXEC_TUNING;
Faiz Abbas2eddc002019-06-11 00:43:40 +0530260 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530261
262 mdelay(1);
263
Ashok Reddy Soma33154532022-09-30 03:25:47 -0600264 arasan_zynqmp_dll_reset(host, priv->node_id);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530265
266 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
267 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
268
269 do {
270 cmd.cmdidx = opcode;
271 cmd.resp_type = MMC_RSP_R1;
272 cmd.cmdarg = 0;
273
274 data.blocksize = 64;
275 data.blocks = 1;
276 data.flags = MMC_DATA_READ;
277
278 if (tuning_loop_counter-- == 0)
279 break;
280
281 if (cmd.cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200 &&
282 mmc->bus_width == 8)
283 data.blocksize = 128;
284
285 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
286 data.blocksize),
287 SDHCI_BLOCK_SIZE);
288 sdhci_writew(host, data.blocks, SDHCI_BLOCK_COUNT);
289 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
290
291 mmc_send_cmd(mmc, &cmd, NULL);
Faiz Abbas2eddc002019-06-11 00:43:40 +0530292 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530293
294 if (cmd.cmdidx == MMC_CMD_SEND_TUNING_BLOCK)
295 udelay(1);
296
297 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
298
299 if (tuning_loop_counter < 0) {
300 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
Faiz Abbas2eddc002019-06-11 00:43:40 +0530301 sdhci_writel(host, ctrl, SDHCI_HOST_CONTROL2);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530302 }
303
304 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
305 printf("%s:Tuning failed\n", __func__);
306 return -1;
307 }
308
309 udelay(1);
Ashok Reddy Soma33154532022-09-30 03:25:47 -0600310 arasan_zynqmp_dll_reset(host, priv->node_id);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530311
312 /* Enable only interrupts served by the SD controller */
313 sdhci_writel(host, SDHCI_INT_DATA_MASK | SDHCI_INT_CMD_MASK,
314 SDHCI_INT_ENABLE);
315 /* Mask all sdhci interrupt sources */
316 sdhci_writel(host, 0x0, SDHCI_SIGNAL_ENABLE);
317
318 return 0;
319}
320
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600321/**
322 * sdhci_zynqmp_sdcardclk_set_phase - Set the SD Output Clock Tap Delays
323 *
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600324 * @host: Pointer to the sdhci_host structure.
325 * @degrees: The clock phase shift between 0 - 359.
Ashok Reddy Soma94a755d2021-07-09 05:53:39 -0600326 * Return: 0
Michal Simek2a43ab82021-07-09 05:53:43 -0600327 *
328 * Set the SD Output Clock Tap Delays for Output path
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600329 */
330static int sdhci_zynqmp_sdcardclk_set_phase(struct sdhci_host *host,
331 int degrees)
332{
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600333 struct mmc *mmc = (struct mmc *)host->mmc;
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600334 struct udevice *dev = mmc->dev;
335 struct arasan_sdhci_priv *priv = dev_get_priv(mmc->dev);
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600336 u8 tap_delay, tap_max = 0;
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600337 int timing = mode2timing[mmc->selected_mode];
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600338 int ret;
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600339
340 /*
341 * This is applicable for SDHCI_SPEC_300 and above
342 * ZynqMP does not set phase for <=25MHz clock.
343 * If degrees is zero, no need to do anything.
344 */
Ashok Reddy Soma6f645382021-07-09 05:53:40 -0600345 if (SDHCI_GET_VERSION(host) < SDHCI_SPEC_300)
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600346 return 0;
347
348 switch (timing) {
349 case MMC_TIMING_MMC_HS:
350 case MMC_TIMING_SD_HS:
351 case MMC_TIMING_UHS_SDR25:
352 case MMC_TIMING_UHS_DDR50:
353 case MMC_TIMING_MMC_DDR52:
354 /* For 50MHz clock, 30 Taps are available */
355 tap_max = 30;
356 break;
357 case MMC_TIMING_UHS_SDR50:
358 /* For 100MHz clock, 15 Taps are available */
359 tap_max = 15;
360 break;
361 case MMC_TIMING_UHS_SDR104:
362 case MMC_TIMING_MMC_HS200:
363 /* For 200MHz clock, 8 Taps are available */
364 tap_max = 8;
365 default:
366 break;
367 }
368
369 tap_delay = (degrees * tap_max) / 360;
370
Ashok Reddy Soma39a177a2021-07-09 05:53:42 -0600371 /* Limit output tap_delay value to 6 bits */
372 tap_delay &= SDHCI_ARASAN_OTAPDLY_SEL_MASK;
373
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600374 /* Set the Clock Phase */
Ashok Reddy Soma33154532022-09-30 03:25:47 -0600375 ret = arasan_zynqmp_set_out_tapdelay(priv->node_id, tap_delay);
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600376 if (ret) {
377 dev_err(dev, "Error setting output Tap Delay\n");
378 return ret;
379 }
380
381 /* Release DLL Reset */
Ashok Reddy Soma33154532022-09-30 03:25:47 -0600382 ret = zynqmp_dll_reset(priv->node_id, PM_DLL_RESET_RELEASE);
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600383 if (ret) {
384 dev_err(dev, "dll_reset release failed with err: %d\n", ret);
385 return ret;
386 }
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600387
Ashok Reddy Soma94a755d2021-07-09 05:53:39 -0600388 return 0;
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600389}
390
391/**
392 * sdhci_zynqmp_sampleclk_set_phase - Set the SD Input Clock Tap Delays
393 *
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600394 * @host: Pointer to the sdhci_host structure.
395 * @degrees: The clock phase shift between 0 - 359.
Ashok Reddy Soma94a755d2021-07-09 05:53:39 -0600396 * Return: 0
Michal Simek2a43ab82021-07-09 05:53:43 -0600397 *
398 * Set the SD Input Clock Tap Delays for Input path
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600399 */
400static int sdhci_zynqmp_sampleclk_set_phase(struct sdhci_host *host,
401 int degrees)
402{
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600403 struct mmc *mmc = (struct mmc *)host->mmc;
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600404 struct udevice *dev = mmc->dev;
405 struct arasan_sdhci_priv *priv = dev_get_priv(mmc->dev);
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600406 u8 tap_delay, tap_max = 0;
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600407 int timing = mode2timing[mmc->selected_mode];
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600408 int ret;
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600409
410 /*
411 * This is applicable for SDHCI_SPEC_300 and above
412 * ZynqMP does not set phase for <=25MHz clock.
413 * If degrees is zero, no need to do anything.
414 */
Ashok Reddy Soma6f645382021-07-09 05:53:40 -0600415 if (SDHCI_GET_VERSION(host) < SDHCI_SPEC_300)
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600416 return 0;
417
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600418 /* Assert DLL Reset */
Ashok Reddy Soma33154532022-09-30 03:25:47 -0600419 ret = zynqmp_dll_reset(priv->node_id, PM_DLL_RESET_ASSERT);
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600420 if (ret) {
421 dev_err(dev, "dll_reset assert failed with err: %d\n", ret);
422 return ret;
423 }
424
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600425 switch (timing) {
426 case MMC_TIMING_MMC_HS:
427 case MMC_TIMING_SD_HS:
428 case MMC_TIMING_UHS_SDR25:
429 case MMC_TIMING_UHS_DDR50:
430 case MMC_TIMING_MMC_DDR52:
431 /* For 50MHz clock, 120 Taps are available */
432 tap_max = 120;
433 break;
434 case MMC_TIMING_UHS_SDR50:
435 /* For 100MHz clock, 60 Taps are available */
436 tap_max = 60;
437 break;
438 case MMC_TIMING_UHS_SDR104:
439 case MMC_TIMING_MMC_HS200:
440 /* For 200MHz clock, 30 Taps are available */
441 tap_max = 30;
442 default:
443 break;
444 }
445
446 tap_delay = (degrees * tap_max) / 360;
447
Ashok Reddy Soma39a177a2021-07-09 05:53:42 -0600448 /* Limit input tap_delay value to 8 bits */
449 tap_delay &= SDHCI_ARASAN_ITAPDLY_SEL_MASK;
450
Ashok Reddy Soma33154532022-09-30 03:25:47 -0600451 ret = arasan_zynqmp_set_in_tapdelay(priv->node_id, tap_delay);
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600452 if (ret) {
453 dev_err(dev, "Error setting Input Tap Delay\n");
454 return ret;
455 }
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600456
Ashok Reddy Soma94a755d2021-07-09 05:53:39 -0600457 return 0;
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600458}
459
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600460/**
461 * sdhci_versal_sdcardclk_set_phase - Set the SD Output Clock Tap Delays
462 *
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600463 * @host: Pointer to the sdhci_host structure.
Michal Simek2a43ab82021-07-09 05:53:43 -0600464 * @degrees: The clock phase shift between 0 - 359.
Ashok Reddy Soma94a755d2021-07-09 05:53:39 -0600465 * Return: 0
Michal Simek2a43ab82021-07-09 05:53:43 -0600466 *
467 * Set the SD Output Clock Tap Delays for Output path
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600468 */
469static int sdhci_versal_sdcardclk_set_phase(struct sdhci_host *host,
470 int degrees)
471{
472 struct mmc *mmc = (struct mmc *)host->mmc;
473 u8 tap_delay, tap_max = 0;
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600474 int timing = mode2timing[mmc->selected_mode];
Ashok Reddy Soma24a51072021-07-09 05:53:41 -0600475 u32 regval;
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600476
477 /*
478 * This is applicable for SDHCI_SPEC_300 and above
479 * Versal does not set phase for <=25MHz clock.
480 * If degrees is zero, no need to do anything.
481 */
Ashok Reddy Soma6f645382021-07-09 05:53:40 -0600482 if (SDHCI_GET_VERSION(host) < SDHCI_SPEC_300)
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600483 return 0;
484
485 switch (timing) {
486 case MMC_TIMING_MMC_HS:
487 case MMC_TIMING_SD_HS:
488 case MMC_TIMING_UHS_SDR25:
489 case MMC_TIMING_UHS_DDR50:
490 case MMC_TIMING_MMC_DDR52:
491 /* For 50MHz clock, 30 Taps are available */
492 tap_max = 30;
493 break;
494 case MMC_TIMING_UHS_SDR50:
495 /* For 100MHz clock, 15 Taps are available */
496 tap_max = 15;
497 break;
498 case MMC_TIMING_UHS_SDR104:
499 case MMC_TIMING_MMC_HS200:
500 /* For 200MHz clock, 8 Taps are available */
501 tap_max = 8;
502 default:
503 break;
504 }
505
506 tap_delay = (degrees * tap_max) / 360;
507
Ashok Reddy Soma24a51072021-07-09 05:53:41 -0600508 /* Limit output tap_delay value to 6 bits */
509 tap_delay &= SDHCI_ARASAN_OTAPDLY_SEL_MASK;
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600510
Ashok Reddy Soma24a51072021-07-09 05:53:41 -0600511 /* Set the Clock Phase */
512 regval = sdhci_readl(host, SDHCI_ARASAN_OTAPDLY_REGISTER);
513 regval |= SDHCI_OTAPDLY_ENABLE;
514 sdhci_writel(host, regval, SDHCI_ARASAN_OTAPDLY_REGISTER);
515 regval &= ~SDHCI_ARASAN_OTAPDLY_SEL_MASK;
516 regval |= tap_delay;
517 sdhci_writel(host, regval, SDHCI_ARASAN_OTAPDLY_REGISTER);
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600518
Ashok Reddy Soma94a755d2021-07-09 05:53:39 -0600519 return 0;
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600520}
521
522/**
523 * sdhci_versal_sampleclk_set_phase - Set the SD Input Clock Tap Delays
524 *
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600525 * @host: Pointer to the sdhci_host structure.
Michal Simek2a43ab82021-07-09 05:53:43 -0600526 * @degrees: The clock phase shift between 0 - 359.
Ashok Reddy Soma94a755d2021-07-09 05:53:39 -0600527 * Return: 0
Michal Simek2a43ab82021-07-09 05:53:43 -0600528 *
529 * Set the SD Input Clock Tap Delays for Input path
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600530 */
531static int sdhci_versal_sampleclk_set_phase(struct sdhci_host *host,
532 int degrees)
533{
534 struct mmc *mmc = (struct mmc *)host->mmc;
535 u8 tap_delay, tap_max = 0;
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600536 int timing = mode2timing[mmc->selected_mode];
Ashok Reddy Soma24a51072021-07-09 05:53:41 -0600537 u32 regval;
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600538
539 /*
540 * This is applicable for SDHCI_SPEC_300 and above
541 * Versal does not set phase for <=25MHz clock.
542 * If degrees is zero, no need to do anything.
543 */
Ashok Reddy Soma6f645382021-07-09 05:53:40 -0600544 if (SDHCI_GET_VERSION(host) < SDHCI_SPEC_300)
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600545 return 0;
546
547 switch (timing) {
548 case MMC_TIMING_MMC_HS:
549 case MMC_TIMING_SD_HS:
550 case MMC_TIMING_UHS_SDR25:
551 case MMC_TIMING_UHS_DDR50:
552 case MMC_TIMING_MMC_DDR52:
553 /* For 50MHz clock, 120 Taps are available */
554 tap_max = 120;
555 break;
556 case MMC_TIMING_UHS_SDR50:
557 /* For 100MHz clock, 60 Taps are available */
558 tap_max = 60;
559 break;
560 case MMC_TIMING_UHS_SDR104:
561 case MMC_TIMING_MMC_HS200:
562 /* For 200MHz clock, 30 Taps are available */
563 tap_max = 30;
564 default:
565 break;
566 }
567
568 tap_delay = (degrees * tap_max) / 360;
569
Ashok Reddy Soma24a51072021-07-09 05:53:41 -0600570 /* Limit input tap_delay value to 8 bits */
571 tap_delay &= SDHCI_ARASAN_ITAPDLY_SEL_MASK;
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600572
Ashok Reddy Soma24a51072021-07-09 05:53:41 -0600573 /* Set the Clock Phase */
574 regval = sdhci_readl(host, SDHCI_ARASAN_ITAPDLY_REGISTER);
575 regval |= SDHCI_ITAPDLY_CHGWIN;
576 sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
577 regval |= SDHCI_ITAPDLY_ENABLE;
578 sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
579 regval &= ~SDHCI_ARASAN_ITAPDLY_SEL_MASK;
580 regval |= tap_delay;
581 sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
582 regval &= ~SDHCI_ITAPDLY_CHGWIN;
583 sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600584
Ashok Reddy Soma94a755d2021-07-09 05:53:39 -0600585 return 0;
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600586}
587
Ashok Reddy Soma21fd7632021-08-02 23:20:40 -0600588static int arasan_sdhci_set_tapdelay(struct sdhci_host *host)
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530589{
590 struct arasan_sdhci_priv *priv = dev_get_priv(host->mmc->dev);
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600591 struct arasan_sdhci_clk_data *clk_data = &priv->clk_data;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530592 struct mmc *mmc = (struct mmc *)host->mmc;
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600593 struct udevice *dev = mmc->dev;
594 u8 timing = mode2timing[mmc->selected_mode];
595 u32 iclk_phase = clk_data->clk_phase_in[timing];
596 u32 oclk_phase = clk_data->clk_phase_out[timing];
Ashok Reddy Soma21fd7632021-08-02 23:20:40 -0600597 int ret;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530598
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600599 dev_dbg(dev, "%s, host:%s, mode:%d\n", __func__, host->name, timing);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530600
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600601 if (IS_ENABLED(CONFIG_ARCH_ZYNQMP) &&
602 device_is_compatible(dev, "xlnx,zynqmp-8.9a")) {
Ashok Reddy Soma21fd7632021-08-02 23:20:40 -0600603 ret = sdhci_zynqmp_sampleclk_set_phase(host, iclk_phase);
604 if (ret)
605 return ret;
606
607 ret = sdhci_zynqmp_sdcardclk_set_phase(host, oclk_phase);
608 if (ret)
609 return ret;
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600610 } else if (IS_ENABLED(CONFIG_ARCH_VERSAL) &&
611 device_is_compatible(dev, "xlnx,versal-8.9a")) {
Ashok Reddy Soma21fd7632021-08-02 23:20:40 -0600612 ret = sdhci_versal_sampleclk_set_phase(host, iclk_phase);
613 if (ret)
614 return ret;
615
616 ret = sdhci_versal_sdcardclk_set_phase(host, oclk_phase);
617 if (ret)
618 return ret;
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600619 }
Ashok Reddy Soma21fd7632021-08-02 23:20:40 -0600620
621 return 0;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530622}
623
Michal Simek33a6b772020-10-23 04:59:00 -0600624static void arasan_dt_read_clk_phase(struct udevice *dev, unsigned char timing,
625 const char *prop)
626{
627 struct arasan_sdhci_priv *priv = dev_get_priv(dev);
628 struct arasan_sdhci_clk_data *clk_data = &priv->clk_data;
629 u32 clk_phase[2] = {0};
630
631 /*
632 * Read Tap Delay values from DT, if the DT does not contain the
633 * Tap Values then use the pre-defined values
634 */
635 if (dev_read_u32_array(dev, prop, &clk_phase[0], 2)) {
636 dev_dbg(dev, "Using predefined clock phase for %s = %d %d\n",
637 prop, clk_data->clk_phase_in[timing],
638 clk_data->clk_phase_out[timing]);
639 return;
640 }
641
642 /* The values read are Input and Output Clock Delays in order */
643 clk_data->clk_phase_in[timing] = clk_phase[0];
644 clk_data->clk_phase_out[timing] = clk_phase[1];
645}
646
647/**
648 * arasan_dt_parse_clk_phases - Read Tap Delay values from DT
649 *
Michal Simek33a6b772020-10-23 04:59:00 -0600650 * @dev: Pointer to our struct udevice.
Michal Simek2a43ab82021-07-09 05:53:43 -0600651 *
652 * Called at initialization to parse the values of Tap Delays.
Michal Simek33a6b772020-10-23 04:59:00 -0600653 */
654static void arasan_dt_parse_clk_phases(struct udevice *dev)
655{
656 struct arasan_sdhci_priv *priv = dev_get_priv(dev);
657 struct arasan_sdhci_clk_data *clk_data = &priv->clk_data;
658 int i;
659
660 if (IS_ENABLED(CONFIG_ARCH_ZYNQMP) &&
661 device_is_compatible(dev, "xlnx,zynqmp-8.9a")) {
662 for (i = 0; i <= MMC_TIMING_MMC_HS400; i++) {
663 clk_data->clk_phase_in[i] = zynqmp_iclk_phases[i];
664 clk_data->clk_phase_out[i] = zynqmp_oclk_phases[i];
665 }
666
667 if (priv->bank == MMC_BANK2) {
668 clk_data->clk_phase_out[MMC_TIMING_UHS_SDR104] = 90;
669 clk_data->clk_phase_out[MMC_TIMING_MMC_HS200] = 90;
670 }
671 }
672
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600673 if (IS_ENABLED(CONFIG_ARCH_VERSAL) &&
674 device_is_compatible(dev, "xlnx,versal-8.9a")) {
675 for (i = 0; i <= MMC_TIMING_MMC_HS400; i++) {
676 clk_data->clk_phase_in[i] = versal_iclk_phases[i];
677 clk_data->clk_phase_out[i] = versal_oclk_phases[i];
678 }
679 }
680
Michal Simek33a6b772020-10-23 04:59:00 -0600681 arasan_dt_read_clk_phase(dev, MMC_TIMING_LEGACY,
682 "clk-phase-legacy");
683 arasan_dt_read_clk_phase(dev, MMC_TIMING_MMC_HS,
684 "clk-phase-mmc-hs");
685 arasan_dt_read_clk_phase(dev, MMC_TIMING_SD_HS,
686 "clk-phase-sd-hs");
687 arasan_dt_read_clk_phase(dev, MMC_TIMING_UHS_SDR12,
688 "clk-phase-uhs-sdr12");
689 arasan_dt_read_clk_phase(dev, MMC_TIMING_UHS_SDR25,
690 "clk-phase-uhs-sdr25");
691 arasan_dt_read_clk_phase(dev, MMC_TIMING_UHS_SDR50,
692 "clk-phase-uhs-sdr50");
693 arasan_dt_read_clk_phase(dev, MMC_TIMING_UHS_SDR104,
694 "clk-phase-uhs-sdr104");
695 arasan_dt_read_clk_phase(dev, MMC_TIMING_UHS_DDR50,
696 "clk-phase-uhs-ddr50");
697 arasan_dt_read_clk_phase(dev, MMC_TIMING_MMC_DDR52,
698 "clk-phase-mmc-ddr52");
699 arasan_dt_read_clk_phase(dev, MMC_TIMING_MMC_HS200,
700 "clk-phase-mmc-hs200");
701 arasan_dt_read_clk_phase(dev, MMC_TIMING_MMC_HS400,
702 "clk-phase-mmc-hs400");
703}
704
Michal Simek635cf4a2021-07-09 05:53:44 -0600705static const struct sdhci_ops arasan_ops = {
706 .platform_execute_tuning = &arasan_sdhci_execute_tuning,
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530707 .set_delay = &arasan_sdhci_set_tapdelay,
Ashok Reddy Soma8ee49f02021-08-02 23:20:46 -0600708 .set_control_reg = &sdhci_set_control_reg,
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530709};
710#endif
711
Ashok Reddy Somaca7d8472022-02-23 15:36:05 +0100712#if defined(CONFIG_ARCH_ZYNQMP)
713static int sdhci_zynqmp_set_dynamic_config(struct arasan_sdhci_priv *priv,
714 struct udevice *dev)
715{
716 int ret;
Ashok Reddy Somaca7d8472022-02-23 15:36:05 +0100717 struct clk clk;
718 unsigned long clock, mhz;
719
Ashok Reddy Soma33154532022-09-30 03:25:47 -0600720 ret = xilinx_pm_request(PM_REQUEST_NODE, priv->node_id,
721 ZYNQMP_PM_CAPABILITY_ACCESS, ZYNQMP_PM_MAX_QOS,
722 ZYNQMP_PM_REQUEST_ACK_NO, NULL);
Ashok Reddy Somaca7d8472022-02-23 15:36:05 +0100723 if (ret) {
Ashok Reddy Soma33154532022-09-30 03:25:47 -0600724 dev_err(dev, "Request node failed for %d\n", priv->node_id);
Ashok Reddy Somaca7d8472022-02-23 15:36:05 +0100725 return ret;
726 }
727
728 ret = reset_get_bulk(dev, &priv->resets);
729 if (ret == -ENOTSUPP || ret == -ENOENT) {
730 dev_err(dev, "Reset not found\n");
731 return 0;
732 } else if (ret) {
733 dev_err(dev, "Reset failed\n");
734 return ret;
735 }
736
737 ret = reset_assert_bulk(&priv->resets);
738 if (ret) {
739 dev_err(dev, "Reset assert failed\n");
740 return ret;
741 }
742
Ashok Reddy Soma33154532022-09-30 03:25:47 -0600743 ret = zynqmp_pm_set_sd_config(priv->node_id, SD_CONFIG_FIXED, 0);
Ashok Reddy Somaca7d8472022-02-23 15:36:05 +0100744 if (ret) {
745 dev_err(dev, "SD_CONFIG_FIXED failed\n");
746 return ret;
747 }
748
Ashok Reddy Soma33154532022-09-30 03:25:47 -0600749 ret = zynqmp_pm_set_sd_config(priv->node_id, SD_CONFIG_EMMC_SEL,
Ashok Reddy Somaca7d8472022-02-23 15:36:05 +0100750 dev_read_bool(dev, "non-removable"));
751 if (ret) {
752 dev_err(dev, "SD_CONFIG_EMMC_SEL failed\n");
753 return ret;
754 }
755
756 ret = clk_get_by_index(dev, 0, &clk);
757 if (ret < 0) {
758 dev_err(dev, "failed to get clock\n");
759 return ret;
760 }
761
762 clock = clk_get_rate(&clk);
763 if (IS_ERR_VALUE(clock)) {
764 dev_err(dev, "failed to get rate\n");
765 return clock;
766 }
767
768 mhz = DIV64_U64_ROUND_UP(clock, 1000000);
769
Ashok Reddy Soma088febe2022-03-25 13:11:10 +0100770 if (mhz > 100 && mhz <= 200)
771 mhz = 200;
772 else if (mhz > 50 && mhz <= 100)
773 mhz = 100;
774 else if (mhz > 25 && mhz <= 50)
775 mhz = 50;
776 else
777 mhz = 25;
778
Ashok Reddy Soma33154532022-09-30 03:25:47 -0600779 ret = zynqmp_pm_set_sd_config(priv->node_id, SD_CONFIG_BASECLK, mhz);
Ashok Reddy Somaca7d8472022-02-23 15:36:05 +0100780 if (ret) {
781 dev_err(dev, "SD_CONFIG_BASECLK failed\n");
782 return ret;
783 }
784
Ashok Reddy Soma33154532022-09-30 03:25:47 -0600785 ret = zynqmp_pm_set_sd_config(priv->node_id, SD_CONFIG_8BIT,
Ashok Reddy Somaca7d8472022-02-23 15:36:05 +0100786 (dev_read_u32_default(dev, "bus-width", 1) == 8));
787 if (ret) {
788 dev_err(dev, "SD_CONFIG_8BIT failed\n");
789 return ret;
790 }
791
792 ret = reset_deassert_bulk(&priv->resets);
793 if (ret) {
794 dev_err(dev, "Reset release failed\n");
795 return ret;
796 }
797
798 return 0;
799}
800#endif
801
Michal Simek9ecd2682015-11-30 16:13:03 +0100802static int arasan_sdhci_probe(struct udevice *dev)
Michal Simek0dd222b2013-04-22 14:56:49 +0200803{
Simon Glassfa20e932020-12-03 16:55:20 -0700804 struct arasan_sdhci_plat *plat = dev_get_plat(dev);
Michal Simek9ecd2682015-11-30 16:13:03 +0100805 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530806 struct arasan_sdhci_priv *priv = dev_get_priv(dev);
807 struct sdhci_host *host;
Stefan Herbrechtsmeier739ae402017-01-17 16:27:32 +0100808 struct clk clk;
809 unsigned long clock;
Simon Glass4cc87fb2016-07-05 17:10:15 -0600810 int ret;
Michal Simek0dd222b2013-04-22 14:56:49 +0200811
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530812 host = priv->host;
813
Ashok Reddy Somaca7d8472022-02-23 15:36:05 +0100814#if defined(CONFIG_ARCH_ZYNQMP)
815 if (device_is_compatible(dev, "xlnx,zynqmp-8.9a")) {
816 ret = zynqmp_pm_is_function_supported(PM_IOCTL,
817 IOCTL_SET_SD_CONFIG);
818 if (!ret) {
819 ret = sdhci_zynqmp_set_dynamic_config(priv, dev);
820 if (ret)
821 return ret;
822 }
823 }
824#endif
825
Stefan Herbrechtsmeier739ae402017-01-17 16:27:32 +0100826 ret = clk_get_by_index(dev, 0, &clk);
827 if (ret < 0) {
828 dev_err(dev, "failed to get clock\n");
829 return ret;
830 }
831
832 clock = clk_get_rate(&clk);
833 if (IS_ERR_VALUE(clock)) {
834 dev_err(dev, "failed to get rate\n");
835 return clock;
836 }
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530837
Ashok Reddy Somaca7d8472022-02-23 15:36:05 +0100838 dev_dbg(dev, "%s: CLK %ld\n", __func__, clock);
Stefan Herbrechtsmeier739ae402017-01-17 16:27:32 +0100839
840 ret = clk_enable(&clk);
Michal Simek41710952021-02-09 15:28:15 +0100841 if (ret) {
Stefan Herbrechtsmeier739ae402017-01-17 16:27:32 +0100842 dev_err(dev, "failed to enable clock\n");
843 return ret;
844 }
845
Siva Durga Prasad Paladugu049e0032014-07-08 15:31:04 +0530846 host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD |
Siva Durga Prasad Paladugu0d6891b2014-01-22 09:17:09 +0100847 SDHCI_QUIRK_BROKEN_R1B;
Siva Durga Prasad Paladugua1619fe2016-01-12 15:12:16 +0530848
849#ifdef CONFIG_ZYNQ_HISPD_BROKEN
Hannes Schmelzer94a5bbc2018-03-07 08:00:57 +0100850 host->quirks |= SDHCI_QUIRK_BROKEN_HISPD_MODE;
Siva Durga Prasad Paladugua1619fe2016-01-12 15:12:16 +0530851#endif
852
Ashok Reddy Soma61e0df92020-10-23 04:58:57 -0600853 if (priv->no_1p8)
854 host->quirks |= SDHCI_QUIRK_NO_1_8_V;
855
Benedikt Grassl529e6f02020-04-14 07:32:12 +0200856 plat->cfg.f_max = CONFIG_ZYNQ_SDHCI_MAX_FREQ;
857
858 ret = mmc_of_parse(dev, &plat->cfg);
859 if (ret)
860 return ret;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530861
Stefan Herbrechtsmeier739ae402017-01-17 16:27:32 +0100862 host->max_clk = clock;
Stefan Herbrechtsmeierbc47e0e2017-01-17 15:58:48 +0100863
Matwey V. Kornilov194b8602019-08-01 18:00:05 +0300864 host->mmc = &plat->mmc;
865 host->mmc->dev = dev;
866 host->mmc->priv = host;
867
Benedikt Grassl529e6f02020-04-14 07:32:12 +0200868 ret = sdhci_setup_cfg(&plat->cfg, host, plat->cfg.f_max,
Jaehoon Chung8a5ffbb2016-07-26 19:06:24 +0900869 CONFIG_ZYNQ_SDHCI_MIN_FREQ);
Simon Glass4cc87fb2016-07-05 17:10:15 -0600870 if (ret)
871 return ret;
Simon Glass4cc87fb2016-07-05 17:10:15 -0600872 upriv->mmc = host->mmc;
Michal Simek9ecd2682015-11-30 16:13:03 +0100873
T Karthik Reddyab0a7492021-08-02 23:20:45 -0600874 /*
875 * WORKAROUND: Versal platforms have an issue with card detect state.
876 * Due to this, host controller is switching off voltage to sd card
877 * causing sd card timeout error. Workaround this by adding a wait for
878 * 1000msec till the card detect state gets stable.
879 */
Ashok Reddy Soma6556bff2022-02-23 15:13:32 +0100880 if (IS_ENABLED(CONFIG_ARCH_ZYNQMP) || IS_ENABLED(CONFIG_ARCH_VERSAL)) {
Ashok Reddy Somadd745772022-02-23 15:13:31 +0100881 u32 timeout = 1000000;
T Karthik Reddyab0a7492021-08-02 23:20:45 -0600882
883 while (((sdhci_readl(host, SDHCI_PRESENT_STATE) &
Ashok Reddy Soma09f38902022-02-23 15:13:30 +0100884 SDHCI_CARD_STATE_STABLE) == 0) && timeout) {
Ashok Reddy Somadd745772022-02-23 15:13:31 +0100885 udelay(1);
Ashok Reddy Soma09f38902022-02-23 15:13:30 +0100886 timeout--;
T Karthik Reddyab0a7492021-08-02 23:20:45 -0600887 }
888 if (!timeout) {
889 dev_err(dev, "Sdhci card detect state not stable\n");
890 return -ETIMEDOUT;
891 }
892 }
893
Simon Glass4cc87fb2016-07-05 17:10:15 -0600894 return sdhci_probe(dev);
Michal Simek0dd222b2013-04-22 14:56:49 +0200895}
Michal Simek9ecd2682015-11-30 16:13:03 +0100896
Simon Glassaad29ae2020-12-03 16:55:21 -0700897static int arasan_sdhci_of_to_plat(struct udevice *dev)
Michal Simek9ecd2682015-11-30 16:13:03 +0100898{
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530899 struct arasan_sdhci_priv *priv = dev_get_priv(dev);
Ashok Reddy Soma33154532022-09-30 03:25:47 -0600900 u32 pm_info[2];
Michal Simek9ecd2682015-11-30 16:13:03 +0100901
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530902 priv->host = calloc(1, sizeof(struct sdhci_host));
903 if (!priv->host)
904 return -1;
905
906 priv->host->name = dev->name;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530907
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600908#if defined(CONFIG_ARCH_ZYNQMP) || defined(CONFIG_ARCH_VERSAL)
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530909 priv->host->ops = &arasan_ops;
Michal Simek33a6b772020-10-23 04:59:00 -0600910 arasan_dt_parse_clk_phases(dev);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530911#endif
Michal Simek9ecd2682015-11-30 16:13:03 +0100912
Michal Simek921a8de2018-05-16 10:57:07 +0200913 priv->host->ioaddr = (void *)dev_read_addr(dev);
914 if (IS_ERR(priv->host->ioaddr))
915 return PTR_ERR(priv->host->ioaddr);
916
Michal Simeke40ae572020-07-22 17:46:31 +0200917 priv->bank = dev_read_u32_default(dev, "xlnx,mio-bank", 0);
Ashok Reddy Soma61e0df92020-10-23 04:58:57 -0600918 priv->no_1p8 = dev_read_bool(dev, "no-1-8-v");
Stefan Herbrechtsmeier5567b422017-01-17 16:27:33 +0100919
Ashok Reddy Soma33154532022-09-30 03:25:47 -0600920 priv->node_id = 0;
921 if (!dev_read_u32_array(dev, "power-domains", pm_info, ARRAY_SIZE(pm_info)))
922 priv->node_id = pm_info[1];
923
Michal Simek9ecd2682015-11-30 16:13:03 +0100924 return 0;
925}
926
Simon Glass4cc87fb2016-07-05 17:10:15 -0600927static int arasan_sdhci_bind(struct udevice *dev)
928{
Simon Glassfa20e932020-12-03 16:55:20 -0700929 struct arasan_sdhci_plat *plat = dev_get_plat(dev);
Simon Glass4cc87fb2016-07-05 17:10:15 -0600930
Masahiro Yamadacdb67f32016-09-06 22:17:32 +0900931 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
Simon Glass4cc87fb2016-07-05 17:10:15 -0600932}
933
Michal Simek9ecd2682015-11-30 16:13:03 +0100934static const struct udevice_id arasan_sdhci_ids[] = {
935 { .compatible = "arasan,sdhci-8.9a" },
936 { }
937};
938
939U_BOOT_DRIVER(arasan_sdhci_drv) = {
940 .name = "arasan_sdhci",
941 .id = UCLASS_MMC,
942 .of_match = arasan_sdhci_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700943 .of_to_plat = arasan_sdhci_of_to_plat,
Simon Glass4cc87fb2016-07-05 17:10:15 -0600944 .ops = &sdhci_ops,
945 .bind = arasan_sdhci_bind,
Michal Simek9ecd2682015-11-30 16:13:03 +0100946 .probe = arasan_sdhci_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700947 .priv_auto = sizeof(struct arasan_sdhci_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700948 .plat_auto = sizeof(struct arasan_sdhci_plat),
Michal Simek9ecd2682015-11-30 16:13:03 +0100949};