blob: a59d96c6bdadf41b067d70e36cdc7ccb5abcc8d5 [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;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +053064 u8 deviceid;
65 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
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -060082#if defined(CONFIG_ARCH_ZYNQMP) || defined(CONFIG_ARCH_VERSAL)
Michal Simek33a6b772020-10-23 04:59:00 -060083/* Default settings for ZynqMP Clock Phases */
Michal Simek635cf4a2021-07-09 05:53:44 -060084static const u32 zynqmp_iclk_phases[] = {0, 63, 63, 0, 63, 0,
85 0, 183, 54, 0, 0};
86static const u32 zynqmp_oclk_phases[] = {0, 72, 60, 0, 60, 72,
87 135, 48, 72, 135, 0};
Michal Simek33a6b772020-10-23 04:59:00 -060088
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -060089/* Default settings for Versal Clock Phases */
Michal Simek635cf4a2021-07-09 05:53:44 -060090static const u32 versal_iclk_phases[] = {0, 132, 132, 0, 132,
91 0, 0, 162, 90, 0, 0};
92static const u32 versal_oclk_phases[] = {0, 60, 48, 0, 48, 72,
93 90, 36, 60, 90, 0};
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -060094
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +053095static const u8 mode2timing[] = {
Ashok Reddy Soma0afdfe32020-10-23 04:58:58 -060096 [MMC_LEGACY] = MMC_TIMING_LEGACY,
97 [MMC_HS] = MMC_TIMING_MMC_HS,
98 [SD_HS] = MMC_TIMING_SD_HS,
99 [MMC_HS_52] = MMC_TIMING_UHS_SDR50,
100 [MMC_DDR_52] = MMC_TIMING_UHS_DDR50,
101 [UHS_SDR12] = MMC_TIMING_UHS_SDR12,
102 [UHS_SDR25] = MMC_TIMING_UHS_SDR25,
103 [UHS_SDR50] = MMC_TIMING_UHS_SDR50,
104 [UHS_DDR50] = MMC_TIMING_UHS_DDR50,
105 [UHS_SDR104] = MMC_TIMING_UHS_SDR104,
106 [MMC_HS_200] = MMC_TIMING_MMC_HS200,
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530107};
108
Ashok Reddy Soma7a5f8102021-08-02 23:20:44 -0600109static inline int arasan_zynqmp_set_in_tapdelay(u8 node_id, u32 itap_delay)
110{
111 int ret;
112
113 if (IS_ENABLED(CONFIG_SPL_BUILD) || current_el() == 3) {
114 if (node_id == NODE_SD_0) {
115 ret = zynqmp_mmio_write(SD_ITAP_DLY, SD0_ITAPCHGWIN,
116 SD0_ITAPCHGWIN);
117 if (ret)
118 return ret;
119
120 ret = zynqmp_mmio_write(SD_ITAP_DLY, SD0_ITAPDLYENA,
121 SD0_ITAPDLYENA);
122 if (ret)
123 return ret;
124
125 ret = zynqmp_mmio_write(SD_ITAP_DLY, SD0_ITAPDLYSEL_MASK,
126 itap_delay);
127 if (ret)
128 return ret;
129
130 ret = zynqmp_mmio_write(SD_ITAP_DLY, SD0_ITAPCHGWIN, 0);
131 if (ret)
132 return ret;
133 }
134 ret = zynqmp_mmio_write(SD_ITAP_DLY, SD1_ITAPCHGWIN,
135 SD1_ITAPCHGWIN);
136 if (ret)
137 return ret;
138
139 ret = zynqmp_mmio_write(SD_ITAP_DLY, SD1_ITAPDLYENA,
140 SD1_ITAPDLYENA);
141 if (ret)
142 return ret;
143
144 ret = zynqmp_mmio_write(SD_ITAP_DLY, SD1_ITAPDLYSEL_MASK,
145 (itap_delay << 16));
146 if (ret)
147 return ret;
148
149 ret = zynqmp_mmio_write(SD_ITAP_DLY, SD1_ITAPCHGWIN, 0);
150 if (ret)
151 return ret;
152 } else {
153 return xilinx_pm_request(PM_IOCTL, (u32)node_id,
154 IOCTL_SET_SD_TAPDELAY,
155 PM_TAPDELAY_INPUT, itap_delay, NULL);
156 }
157
158 return 0;
159}
160
161static inline int arasan_zynqmp_set_out_tapdelay(u8 node_id, u32 otap_delay)
162{
163 if (IS_ENABLED(CONFIG_SPL_BUILD) || current_el() == 3) {
164 if (node_id == NODE_SD_0)
165 return zynqmp_mmio_write(SD_OTAP_DLY,
166 SD0_OTAPDLYSEL_MASK,
167 otap_delay);
168
169 return zynqmp_mmio_write(SD_OTAP_DLY, SD1_OTAPDLYSEL_MASK,
170 (otap_delay << 16));
171 } else {
172 return xilinx_pm_request(PM_IOCTL, (u32)node_id,
173 IOCTL_SET_SD_TAPDELAY,
174 PM_TAPDELAY_OUTPUT, otap_delay, NULL);
175 }
176}
177
178static inline int zynqmp_dll_reset(u8 node_id, u32 type)
179{
180 if (IS_ENABLED(CONFIG_SPL_BUILD) || current_el() == 3) {
181 if (node_id == NODE_SD_0)
182 return zynqmp_mmio_write(SD_DLL_CTRL, SD0_DLL_RST,
183 type == PM_DLL_RESET_ASSERT ?
184 SD0_DLL_RST : 0);
185
186 return zynqmp_mmio_write(SD_DLL_CTRL, SD1_DLL_RST,
187 type == PM_DLL_RESET_ASSERT ?
188 SD1_DLL_RST : 0);
189 } else {
190 return xilinx_pm_request(PM_IOCTL, (u32)node_id,
191 IOCTL_SD_DLL_RESET, type, 0, NULL);
192 }
193}
194
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600195static int arasan_zynqmp_dll_reset(struct sdhci_host *host, u8 node_id)
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530196{
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600197 struct mmc *mmc = (struct mmc *)host->mmc;
198 struct udevice *dev = mmc->dev;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530199 unsigned long timeout;
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600200 int ret;
201 u16 clk;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530202
203 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
204 clk &= ~(SDHCI_CLOCK_CARD_EN);
205 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
206
207 /* Issue DLL Reset */
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600208 ret = zynqmp_dll_reset(node_id, PM_DLL_RESET_ASSERT);
209 if (ret) {
210 dev_err(dev, "dll_reset assert failed with err: %d\n", ret);
211 return ret;
212 }
213
214 /* Allow atleast 1ms delay for proper DLL reset */
215 mdelay(1);
216 ret = zynqmp_dll_reset(node_id, PM_DLL_RESET_RELEASE);
217 if (ret) {
218 dev_err(dev, "dll_reset release failed with err: %d\n", ret);
219 return ret;
220 }
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530221
222 /* Wait max 20 ms */
223 timeout = 100;
224 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
225 & SDHCI_CLOCK_INT_STABLE)) {
226 if (timeout == 0) {
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600227 dev_err(dev, ": Internal clock never stabilised.\n");
228 return -EBUSY;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530229 }
230 timeout--;
231 udelay(1000);
232 }
233
234 clk |= SDHCI_CLOCK_CARD_EN;
235 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600236
237 return 0;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530238}
239
240static int arasan_sdhci_execute_tuning(struct mmc *mmc, u8 opcode)
241{
242 struct mmc_cmd cmd;
243 struct mmc_data data;
244 u32 ctrl;
245 struct sdhci_host *host;
246 struct arasan_sdhci_priv *priv = dev_get_priv(mmc->dev);
Michal Simek91e95ff2018-06-13 09:12:29 +0200247 char tuning_loop_counter = SDHCI_TUNING_LOOP_COUNT;
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600248 u8 node_id = priv->deviceid ? NODE_SD_1 : NODE_SD_0;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530249
Ashok Reddy Somaca7d8472022-02-23 15:36:05 +0100250 dev_dbg(mmc->dev, "%s\n", __func__);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530251
252 host = priv->host;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530253
Faiz Abbas2eddc002019-06-11 00:43:40 +0530254 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530255 ctrl |= SDHCI_CTRL_EXEC_TUNING;
Faiz Abbas2eddc002019-06-11 00:43:40 +0530256 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530257
258 mdelay(1);
259
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600260 arasan_zynqmp_dll_reset(host, node_id);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530261
262 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
263 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
264
265 do {
266 cmd.cmdidx = opcode;
267 cmd.resp_type = MMC_RSP_R1;
268 cmd.cmdarg = 0;
269
270 data.blocksize = 64;
271 data.blocks = 1;
272 data.flags = MMC_DATA_READ;
273
274 if (tuning_loop_counter-- == 0)
275 break;
276
277 if (cmd.cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200 &&
278 mmc->bus_width == 8)
279 data.blocksize = 128;
280
281 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
282 data.blocksize),
283 SDHCI_BLOCK_SIZE);
284 sdhci_writew(host, data.blocks, SDHCI_BLOCK_COUNT);
285 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
286
287 mmc_send_cmd(mmc, &cmd, NULL);
Faiz Abbas2eddc002019-06-11 00:43:40 +0530288 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530289
290 if (cmd.cmdidx == MMC_CMD_SEND_TUNING_BLOCK)
291 udelay(1);
292
293 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
294
295 if (tuning_loop_counter < 0) {
296 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
Faiz Abbas2eddc002019-06-11 00:43:40 +0530297 sdhci_writel(host, ctrl, SDHCI_HOST_CONTROL2);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530298 }
299
300 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
301 printf("%s:Tuning failed\n", __func__);
302 return -1;
303 }
304
305 udelay(1);
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600306 arasan_zynqmp_dll_reset(host, node_id);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530307
308 /* Enable only interrupts served by the SD controller */
309 sdhci_writel(host, SDHCI_INT_DATA_MASK | SDHCI_INT_CMD_MASK,
310 SDHCI_INT_ENABLE);
311 /* Mask all sdhci interrupt sources */
312 sdhci_writel(host, 0x0, SDHCI_SIGNAL_ENABLE);
313
314 return 0;
315}
316
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600317/**
318 * sdhci_zynqmp_sdcardclk_set_phase - Set the SD Output Clock Tap Delays
319 *
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600320 * @host: Pointer to the sdhci_host structure.
321 * @degrees: The clock phase shift between 0 - 359.
Ashok Reddy Soma94a755d2021-07-09 05:53:39 -0600322 * Return: 0
Michal Simek2a43ab82021-07-09 05:53:43 -0600323 *
324 * Set the SD Output Clock Tap Delays for Output path
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600325 */
326static int sdhci_zynqmp_sdcardclk_set_phase(struct sdhci_host *host,
327 int degrees)
328{
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600329 struct mmc *mmc = (struct mmc *)host->mmc;
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600330 struct udevice *dev = mmc->dev;
331 struct arasan_sdhci_priv *priv = dev_get_priv(mmc->dev);
332 u8 node_id = priv->deviceid ? NODE_SD_1 : NODE_SD_0;
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600333 u8 tap_delay, tap_max = 0;
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600334 int timing = mode2timing[mmc->selected_mode];
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600335 int ret;
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600336
337 /*
338 * This is applicable for SDHCI_SPEC_300 and above
339 * ZynqMP does not set phase for <=25MHz clock.
340 * If degrees is zero, no need to do anything.
341 */
Ashok Reddy Soma6f645382021-07-09 05:53:40 -0600342 if (SDHCI_GET_VERSION(host) < SDHCI_SPEC_300)
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600343 return 0;
344
345 switch (timing) {
346 case MMC_TIMING_MMC_HS:
347 case MMC_TIMING_SD_HS:
348 case MMC_TIMING_UHS_SDR25:
349 case MMC_TIMING_UHS_DDR50:
350 case MMC_TIMING_MMC_DDR52:
351 /* For 50MHz clock, 30 Taps are available */
352 tap_max = 30;
353 break;
354 case MMC_TIMING_UHS_SDR50:
355 /* For 100MHz clock, 15 Taps are available */
356 tap_max = 15;
357 break;
358 case MMC_TIMING_UHS_SDR104:
359 case MMC_TIMING_MMC_HS200:
360 /* For 200MHz clock, 8 Taps are available */
361 tap_max = 8;
362 default:
363 break;
364 }
365
366 tap_delay = (degrees * tap_max) / 360;
367
Ashok Reddy Soma39a177a2021-07-09 05:53:42 -0600368 /* Limit output tap_delay value to 6 bits */
369 tap_delay &= SDHCI_ARASAN_OTAPDLY_SEL_MASK;
370
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600371 /* Set the Clock Phase */
372 ret = arasan_zynqmp_set_out_tapdelay(node_id, tap_delay);
373 if (ret) {
374 dev_err(dev, "Error setting output Tap Delay\n");
375 return ret;
376 }
377
378 /* Release DLL Reset */
379 ret = zynqmp_dll_reset(node_id, PM_DLL_RESET_RELEASE);
380 if (ret) {
381 dev_err(dev, "dll_reset release failed with err: %d\n", ret);
382 return ret;
383 }
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600384
Ashok Reddy Soma94a755d2021-07-09 05:53:39 -0600385 return 0;
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600386}
387
388/**
389 * sdhci_zynqmp_sampleclk_set_phase - Set the SD Input Clock Tap Delays
390 *
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600391 * @host: Pointer to the sdhci_host structure.
392 * @degrees: The clock phase shift between 0 - 359.
Ashok Reddy Soma94a755d2021-07-09 05:53:39 -0600393 * Return: 0
Michal Simek2a43ab82021-07-09 05:53:43 -0600394 *
395 * Set the SD Input Clock Tap Delays for Input path
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600396 */
397static int sdhci_zynqmp_sampleclk_set_phase(struct sdhci_host *host,
398 int degrees)
399{
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600400 struct mmc *mmc = (struct mmc *)host->mmc;
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600401 struct udevice *dev = mmc->dev;
402 struct arasan_sdhci_priv *priv = dev_get_priv(mmc->dev);
403 u8 node_id = priv->deviceid ? NODE_SD_1 : NODE_SD_0;
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600404 u8 tap_delay, tap_max = 0;
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600405 int timing = mode2timing[mmc->selected_mode];
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600406 int ret;
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600407
408 /*
409 * This is applicable for SDHCI_SPEC_300 and above
410 * ZynqMP does not set phase for <=25MHz clock.
411 * If degrees is zero, no need to do anything.
412 */
Ashok Reddy Soma6f645382021-07-09 05:53:40 -0600413 if (SDHCI_GET_VERSION(host) < SDHCI_SPEC_300)
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600414 return 0;
415
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600416 /* Assert DLL Reset */
417 ret = zynqmp_dll_reset(node_id, PM_DLL_RESET_ASSERT);
418 if (ret) {
419 dev_err(dev, "dll_reset assert failed with err: %d\n", ret);
420 return ret;
421 }
422
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600423 switch (timing) {
424 case MMC_TIMING_MMC_HS:
425 case MMC_TIMING_SD_HS:
426 case MMC_TIMING_UHS_SDR25:
427 case MMC_TIMING_UHS_DDR50:
428 case MMC_TIMING_MMC_DDR52:
429 /* For 50MHz clock, 120 Taps are available */
430 tap_max = 120;
431 break;
432 case MMC_TIMING_UHS_SDR50:
433 /* For 100MHz clock, 60 Taps are available */
434 tap_max = 60;
435 break;
436 case MMC_TIMING_UHS_SDR104:
437 case MMC_TIMING_MMC_HS200:
438 /* For 200MHz clock, 30 Taps are available */
439 tap_max = 30;
440 default:
441 break;
442 }
443
444 tap_delay = (degrees * tap_max) / 360;
445
Ashok Reddy Soma39a177a2021-07-09 05:53:42 -0600446 /* Limit input tap_delay value to 8 bits */
447 tap_delay &= SDHCI_ARASAN_ITAPDLY_SEL_MASK;
448
Ashok Reddy Soma467d0782021-08-02 23:20:43 -0600449 ret = arasan_zynqmp_set_in_tapdelay(node_id, tap_delay);
450 if (ret) {
451 dev_err(dev, "Error setting Input Tap Delay\n");
452 return ret;
453 }
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600454
Ashok Reddy Soma94a755d2021-07-09 05:53:39 -0600455 return 0;
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600456}
457
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600458/**
459 * sdhci_versal_sdcardclk_set_phase - Set the SD Output Clock Tap Delays
460 *
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600461 * @host: Pointer to the sdhci_host structure.
Michal Simek2a43ab82021-07-09 05:53:43 -0600462 * @degrees: The clock phase shift between 0 - 359.
Ashok Reddy Soma94a755d2021-07-09 05:53:39 -0600463 * Return: 0
Michal Simek2a43ab82021-07-09 05:53:43 -0600464 *
465 * Set the SD Output Clock Tap Delays for Output path
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600466 */
467static int sdhci_versal_sdcardclk_set_phase(struct sdhci_host *host,
468 int degrees)
469{
470 struct mmc *mmc = (struct mmc *)host->mmc;
471 u8 tap_delay, tap_max = 0;
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600472 int timing = mode2timing[mmc->selected_mode];
Ashok Reddy Soma24a51072021-07-09 05:53:41 -0600473 u32 regval;
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600474
475 /*
476 * This is applicable for SDHCI_SPEC_300 and above
477 * Versal does not set phase for <=25MHz clock.
478 * If degrees is zero, no need to do anything.
479 */
Ashok Reddy Soma6f645382021-07-09 05:53:40 -0600480 if (SDHCI_GET_VERSION(host) < SDHCI_SPEC_300)
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600481 return 0;
482
483 switch (timing) {
484 case MMC_TIMING_MMC_HS:
485 case MMC_TIMING_SD_HS:
486 case MMC_TIMING_UHS_SDR25:
487 case MMC_TIMING_UHS_DDR50:
488 case MMC_TIMING_MMC_DDR52:
489 /* For 50MHz clock, 30 Taps are available */
490 tap_max = 30;
491 break;
492 case MMC_TIMING_UHS_SDR50:
493 /* For 100MHz clock, 15 Taps are available */
494 tap_max = 15;
495 break;
496 case MMC_TIMING_UHS_SDR104:
497 case MMC_TIMING_MMC_HS200:
498 /* For 200MHz clock, 8 Taps are available */
499 tap_max = 8;
500 default:
501 break;
502 }
503
504 tap_delay = (degrees * tap_max) / 360;
505
Ashok Reddy Soma24a51072021-07-09 05:53:41 -0600506 /* Limit output tap_delay value to 6 bits */
507 tap_delay &= SDHCI_ARASAN_OTAPDLY_SEL_MASK;
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600508
Ashok Reddy Soma24a51072021-07-09 05:53:41 -0600509 /* Set the Clock Phase */
510 regval = sdhci_readl(host, SDHCI_ARASAN_OTAPDLY_REGISTER);
511 regval |= SDHCI_OTAPDLY_ENABLE;
512 sdhci_writel(host, regval, SDHCI_ARASAN_OTAPDLY_REGISTER);
513 regval &= ~SDHCI_ARASAN_OTAPDLY_SEL_MASK;
514 regval |= tap_delay;
515 sdhci_writel(host, regval, SDHCI_ARASAN_OTAPDLY_REGISTER);
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600516
Ashok Reddy Soma94a755d2021-07-09 05:53:39 -0600517 return 0;
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600518}
519
520/**
521 * sdhci_versal_sampleclk_set_phase - Set the SD Input Clock Tap Delays
522 *
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600523 * @host: Pointer to the sdhci_host structure.
Michal Simek2a43ab82021-07-09 05:53:43 -0600524 * @degrees: The clock phase shift between 0 - 359.
Ashok Reddy Soma94a755d2021-07-09 05:53:39 -0600525 * Return: 0
Michal Simek2a43ab82021-07-09 05:53:43 -0600526 *
527 * Set the SD Input Clock Tap Delays for Input path
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600528 */
529static int sdhci_versal_sampleclk_set_phase(struct sdhci_host *host,
530 int degrees)
531{
532 struct mmc *mmc = (struct mmc *)host->mmc;
533 u8 tap_delay, tap_max = 0;
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600534 int timing = mode2timing[mmc->selected_mode];
Ashok Reddy Soma24a51072021-07-09 05:53:41 -0600535 u32 regval;
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600536
537 /*
538 * This is applicable for SDHCI_SPEC_300 and above
539 * Versal does not set phase for <=25MHz clock.
540 * If degrees is zero, no need to do anything.
541 */
Ashok Reddy Soma6f645382021-07-09 05:53:40 -0600542 if (SDHCI_GET_VERSION(host) < SDHCI_SPEC_300)
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600543 return 0;
544
545 switch (timing) {
546 case MMC_TIMING_MMC_HS:
547 case MMC_TIMING_SD_HS:
548 case MMC_TIMING_UHS_SDR25:
549 case MMC_TIMING_UHS_DDR50:
550 case MMC_TIMING_MMC_DDR52:
551 /* For 50MHz clock, 120 Taps are available */
552 tap_max = 120;
553 break;
554 case MMC_TIMING_UHS_SDR50:
555 /* For 100MHz clock, 60 Taps are available */
556 tap_max = 60;
557 break;
558 case MMC_TIMING_UHS_SDR104:
559 case MMC_TIMING_MMC_HS200:
560 /* For 200MHz clock, 30 Taps are available */
561 tap_max = 30;
562 default:
563 break;
564 }
565
566 tap_delay = (degrees * tap_max) / 360;
567
Ashok Reddy Soma24a51072021-07-09 05:53:41 -0600568 /* Limit input tap_delay value to 8 bits */
569 tap_delay &= SDHCI_ARASAN_ITAPDLY_SEL_MASK;
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600570
Ashok Reddy Soma24a51072021-07-09 05:53:41 -0600571 /* Set the Clock Phase */
572 regval = sdhci_readl(host, SDHCI_ARASAN_ITAPDLY_REGISTER);
573 regval |= SDHCI_ITAPDLY_CHGWIN;
574 sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
575 regval |= SDHCI_ITAPDLY_ENABLE;
576 sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
577 regval &= ~SDHCI_ARASAN_ITAPDLY_SEL_MASK;
578 regval |= tap_delay;
579 sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
580 regval &= ~SDHCI_ITAPDLY_CHGWIN;
581 sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600582
Ashok Reddy Soma94a755d2021-07-09 05:53:39 -0600583 return 0;
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600584}
585
Ashok Reddy Soma21fd7632021-08-02 23:20:40 -0600586static int arasan_sdhci_set_tapdelay(struct sdhci_host *host)
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530587{
588 struct arasan_sdhci_priv *priv = dev_get_priv(host->mmc->dev);
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600589 struct arasan_sdhci_clk_data *clk_data = &priv->clk_data;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530590 struct mmc *mmc = (struct mmc *)host->mmc;
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600591 struct udevice *dev = mmc->dev;
592 u8 timing = mode2timing[mmc->selected_mode];
593 u32 iclk_phase = clk_data->clk_phase_in[timing];
594 u32 oclk_phase = clk_data->clk_phase_out[timing];
Ashok Reddy Soma21fd7632021-08-02 23:20:40 -0600595 int ret;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530596
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600597 dev_dbg(dev, "%s, host:%s, mode:%d\n", __func__, host->name, timing);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530598
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600599 if (IS_ENABLED(CONFIG_ARCH_ZYNQMP) &&
600 device_is_compatible(dev, "xlnx,zynqmp-8.9a")) {
Ashok Reddy Soma21fd7632021-08-02 23:20:40 -0600601 ret = sdhci_zynqmp_sampleclk_set_phase(host, iclk_phase);
602 if (ret)
603 return ret;
604
605 ret = sdhci_zynqmp_sdcardclk_set_phase(host, oclk_phase);
606 if (ret)
607 return ret;
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600608 } else if (IS_ENABLED(CONFIG_ARCH_VERSAL) &&
609 device_is_compatible(dev, "xlnx,versal-8.9a")) {
Ashok Reddy Soma21fd7632021-08-02 23:20:40 -0600610 ret = sdhci_versal_sampleclk_set_phase(host, iclk_phase);
611 if (ret)
612 return ret;
613
614 ret = sdhci_versal_sdcardclk_set_phase(host, oclk_phase);
615 if (ret)
616 return ret;
Ashok Reddy Soma8f629862020-10-23 04:59:01 -0600617 }
Ashok Reddy Soma21fd7632021-08-02 23:20:40 -0600618
619 return 0;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530620}
621
Michal Simek33a6b772020-10-23 04:59:00 -0600622static void arasan_dt_read_clk_phase(struct udevice *dev, unsigned char timing,
623 const char *prop)
624{
625 struct arasan_sdhci_priv *priv = dev_get_priv(dev);
626 struct arasan_sdhci_clk_data *clk_data = &priv->clk_data;
627 u32 clk_phase[2] = {0};
628
629 /*
630 * Read Tap Delay values from DT, if the DT does not contain the
631 * Tap Values then use the pre-defined values
632 */
633 if (dev_read_u32_array(dev, prop, &clk_phase[0], 2)) {
634 dev_dbg(dev, "Using predefined clock phase for %s = %d %d\n",
635 prop, clk_data->clk_phase_in[timing],
636 clk_data->clk_phase_out[timing]);
637 return;
638 }
639
640 /* The values read are Input and Output Clock Delays in order */
641 clk_data->clk_phase_in[timing] = clk_phase[0];
642 clk_data->clk_phase_out[timing] = clk_phase[1];
643}
644
645/**
646 * arasan_dt_parse_clk_phases - Read Tap Delay values from DT
647 *
Michal Simek33a6b772020-10-23 04:59:00 -0600648 * @dev: Pointer to our struct udevice.
Michal Simek2a43ab82021-07-09 05:53:43 -0600649 *
650 * Called at initialization to parse the values of Tap Delays.
Michal Simek33a6b772020-10-23 04:59:00 -0600651 */
652static void arasan_dt_parse_clk_phases(struct udevice *dev)
653{
654 struct arasan_sdhci_priv *priv = dev_get_priv(dev);
655 struct arasan_sdhci_clk_data *clk_data = &priv->clk_data;
656 int i;
657
658 if (IS_ENABLED(CONFIG_ARCH_ZYNQMP) &&
659 device_is_compatible(dev, "xlnx,zynqmp-8.9a")) {
660 for (i = 0; i <= MMC_TIMING_MMC_HS400; i++) {
661 clk_data->clk_phase_in[i] = zynqmp_iclk_phases[i];
662 clk_data->clk_phase_out[i] = zynqmp_oclk_phases[i];
663 }
664
665 if (priv->bank == MMC_BANK2) {
666 clk_data->clk_phase_out[MMC_TIMING_UHS_SDR104] = 90;
667 clk_data->clk_phase_out[MMC_TIMING_MMC_HS200] = 90;
668 }
669 }
670
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600671 if (IS_ENABLED(CONFIG_ARCH_VERSAL) &&
672 device_is_compatible(dev, "xlnx,versal-8.9a")) {
673 for (i = 0; i <= MMC_TIMING_MMC_HS400; i++) {
674 clk_data->clk_phase_in[i] = versal_iclk_phases[i];
675 clk_data->clk_phase_out[i] = versal_oclk_phases[i];
676 }
677 }
678
Michal Simek33a6b772020-10-23 04:59:00 -0600679 arasan_dt_read_clk_phase(dev, MMC_TIMING_LEGACY,
680 "clk-phase-legacy");
681 arasan_dt_read_clk_phase(dev, MMC_TIMING_MMC_HS,
682 "clk-phase-mmc-hs");
683 arasan_dt_read_clk_phase(dev, MMC_TIMING_SD_HS,
684 "clk-phase-sd-hs");
685 arasan_dt_read_clk_phase(dev, MMC_TIMING_UHS_SDR12,
686 "clk-phase-uhs-sdr12");
687 arasan_dt_read_clk_phase(dev, MMC_TIMING_UHS_SDR25,
688 "clk-phase-uhs-sdr25");
689 arasan_dt_read_clk_phase(dev, MMC_TIMING_UHS_SDR50,
690 "clk-phase-uhs-sdr50");
691 arasan_dt_read_clk_phase(dev, MMC_TIMING_UHS_SDR104,
692 "clk-phase-uhs-sdr104");
693 arasan_dt_read_clk_phase(dev, MMC_TIMING_UHS_DDR50,
694 "clk-phase-uhs-ddr50");
695 arasan_dt_read_clk_phase(dev, MMC_TIMING_MMC_DDR52,
696 "clk-phase-mmc-ddr52");
697 arasan_dt_read_clk_phase(dev, MMC_TIMING_MMC_HS200,
698 "clk-phase-mmc-hs200");
699 arasan_dt_read_clk_phase(dev, MMC_TIMING_MMC_HS400,
700 "clk-phase-mmc-hs400");
701}
702
Michal Simek635cf4a2021-07-09 05:53:44 -0600703static const struct sdhci_ops arasan_ops = {
704 .platform_execute_tuning = &arasan_sdhci_execute_tuning,
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530705 .set_delay = &arasan_sdhci_set_tapdelay,
Ashok Reddy Soma8ee49f02021-08-02 23:20:46 -0600706 .set_control_reg = &sdhci_set_control_reg,
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530707};
708#endif
709
Ashok Reddy Somaca7d8472022-02-23 15:36:05 +0100710#if defined(CONFIG_ARCH_ZYNQMP)
711static int sdhci_zynqmp_set_dynamic_config(struct arasan_sdhci_priv *priv,
712 struct udevice *dev)
713{
714 int ret;
715 u32 node_id = priv->deviceid ? NODE_SD_1 : NODE_SD_0;
716 struct clk clk;
717 unsigned long clock, mhz;
718
719 ret = xilinx_pm_request(PM_REQUEST_NODE, node_id, ZYNQMP_PM_CAPABILITY_ACCESS,
720 ZYNQMP_PM_MAX_QOS, ZYNQMP_PM_REQUEST_ACK_NO, NULL);
721 if (ret) {
722 dev_err(dev, "Request node failed for %d\n", node_id);
723 return ret;
724 }
725
726 ret = reset_get_bulk(dev, &priv->resets);
727 if (ret == -ENOTSUPP || ret == -ENOENT) {
728 dev_err(dev, "Reset not found\n");
729 return 0;
730 } else if (ret) {
731 dev_err(dev, "Reset failed\n");
732 return ret;
733 }
734
735 ret = reset_assert_bulk(&priv->resets);
736 if (ret) {
737 dev_err(dev, "Reset assert failed\n");
738 return ret;
739 }
740
741 ret = zynqmp_pm_set_sd_config(node_id, SD_CONFIG_FIXED, 0);
742 if (ret) {
743 dev_err(dev, "SD_CONFIG_FIXED failed\n");
744 return ret;
745 }
746
747 ret = zynqmp_pm_set_sd_config(node_id, SD_CONFIG_EMMC_SEL,
748 dev_read_bool(dev, "non-removable"));
749 if (ret) {
750 dev_err(dev, "SD_CONFIG_EMMC_SEL failed\n");
751 return ret;
752 }
753
754 ret = clk_get_by_index(dev, 0, &clk);
755 if (ret < 0) {
756 dev_err(dev, "failed to get clock\n");
757 return ret;
758 }
759
760 clock = clk_get_rate(&clk);
761 if (IS_ERR_VALUE(clock)) {
762 dev_err(dev, "failed to get rate\n");
763 return clock;
764 }
765
766 mhz = DIV64_U64_ROUND_UP(clock, 1000000);
767
Ashok Reddy Soma088febe2022-03-25 13:11:10 +0100768 if (mhz > 100 && mhz <= 200)
769 mhz = 200;
770 else if (mhz > 50 && mhz <= 100)
771 mhz = 100;
772 else if (mhz > 25 && mhz <= 50)
773 mhz = 50;
774 else
775 mhz = 25;
776
Ashok Reddy Somaca7d8472022-02-23 15:36:05 +0100777 ret = zynqmp_pm_set_sd_config(node_id, SD_CONFIG_BASECLK, mhz);
778 if (ret) {
779 dev_err(dev, "SD_CONFIG_BASECLK failed\n");
780 return ret;
781 }
782
783 ret = zynqmp_pm_set_sd_config(node_id, SD_CONFIG_8BIT,
784 (dev_read_u32_default(dev, "bus-width", 1) == 8));
785 if (ret) {
786 dev_err(dev, "SD_CONFIG_8BIT failed\n");
787 return ret;
788 }
789
790 ret = reset_deassert_bulk(&priv->resets);
791 if (ret) {
792 dev_err(dev, "Reset release failed\n");
793 return ret;
794 }
795
796 return 0;
797}
798#endif
799
Michal Simek9ecd2682015-11-30 16:13:03 +0100800static int arasan_sdhci_probe(struct udevice *dev)
Michal Simek0dd222b2013-04-22 14:56:49 +0200801{
Simon Glassfa20e932020-12-03 16:55:20 -0700802 struct arasan_sdhci_plat *plat = dev_get_plat(dev);
Michal Simek9ecd2682015-11-30 16:13:03 +0100803 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530804 struct arasan_sdhci_priv *priv = dev_get_priv(dev);
805 struct sdhci_host *host;
Stefan Herbrechtsmeier739ae402017-01-17 16:27:32 +0100806 struct clk clk;
807 unsigned long clock;
Simon Glass4cc87fb2016-07-05 17:10:15 -0600808 int ret;
Michal Simek0dd222b2013-04-22 14:56:49 +0200809
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530810 host = priv->host;
811
Ashok Reddy Somaca7d8472022-02-23 15:36:05 +0100812#if defined(CONFIG_ARCH_ZYNQMP)
813 if (device_is_compatible(dev, "xlnx,zynqmp-8.9a")) {
814 ret = zynqmp_pm_is_function_supported(PM_IOCTL,
815 IOCTL_SET_SD_CONFIG);
816 if (!ret) {
817 ret = sdhci_zynqmp_set_dynamic_config(priv, dev);
818 if (ret)
819 return ret;
820 }
821 }
822#endif
823
Stefan Herbrechtsmeier739ae402017-01-17 16:27:32 +0100824 ret = clk_get_by_index(dev, 0, &clk);
825 if (ret < 0) {
826 dev_err(dev, "failed to get clock\n");
827 return ret;
828 }
829
830 clock = clk_get_rate(&clk);
831 if (IS_ERR_VALUE(clock)) {
832 dev_err(dev, "failed to get rate\n");
833 return clock;
834 }
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530835
Ashok Reddy Somaca7d8472022-02-23 15:36:05 +0100836 dev_dbg(dev, "%s: CLK %ld\n", __func__, clock);
Stefan Herbrechtsmeier739ae402017-01-17 16:27:32 +0100837
838 ret = clk_enable(&clk);
Michal Simek41710952021-02-09 15:28:15 +0100839 if (ret) {
Stefan Herbrechtsmeier739ae402017-01-17 16:27:32 +0100840 dev_err(dev, "failed to enable clock\n");
841 return ret;
842 }
843
Siva Durga Prasad Paladugu049e0032014-07-08 15:31:04 +0530844 host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD |
Siva Durga Prasad Paladugu0d6891b2014-01-22 09:17:09 +0100845 SDHCI_QUIRK_BROKEN_R1B;
Siva Durga Prasad Paladugua1619fe2016-01-12 15:12:16 +0530846
847#ifdef CONFIG_ZYNQ_HISPD_BROKEN
Hannes Schmelzer94a5bbc2018-03-07 08:00:57 +0100848 host->quirks |= SDHCI_QUIRK_BROKEN_HISPD_MODE;
Siva Durga Prasad Paladugua1619fe2016-01-12 15:12:16 +0530849#endif
850
Ashok Reddy Soma61e0df92020-10-23 04:58:57 -0600851 if (priv->no_1p8)
852 host->quirks |= SDHCI_QUIRK_NO_1_8_V;
853
Benedikt Grassl529e6f02020-04-14 07:32:12 +0200854 plat->cfg.f_max = CONFIG_ZYNQ_SDHCI_MAX_FREQ;
855
856 ret = mmc_of_parse(dev, &plat->cfg);
857 if (ret)
858 return ret;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530859
Stefan Herbrechtsmeier739ae402017-01-17 16:27:32 +0100860 host->max_clk = clock;
Stefan Herbrechtsmeierbc47e0e2017-01-17 15:58:48 +0100861
Matwey V. Kornilov194b8602019-08-01 18:00:05 +0300862 host->mmc = &plat->mmc;
863 host->mmc->dev = dev;
864 host->mmc->priv = host;
865
Benedikt Grassl529e6f02020-04-14 07:32:12 +0200866 ret = sdhci_setup_cfg(&plat->cfg, host, plat->cfg.f_max,
Jaehoon Chung8a5ffbb2016-07-26 19:06:24 +0900867 CONFIG_ZYNQ_SDHCI_MIN_FREQ);
Simon Glass4cc87fb2016-07-05 17:10:15 -0600868 if (ret)
869 return ret;
Simon Glass4cc87fb2016-07-05 17:10:15 -0600870 upriv->mmc = host->mmc;
Michal Simek9ecd2682015-11-30 16:13:03 +0100871
T Karthik Reddyab0a7492021-08-02 23:20:45 -0600872 /*
873 * WORKAROUND: Versal platforms have an issue with card detect state.
874 * Due to this, host controller is switching off voltage to sd card
875 * causing sd card timeout error. Workaround this by adding a wait for
876 * 1000msec till the card detect state gets stable.
877 */
Ashok Reddy Soma6556bff2022-02-23 15:13:32 +0100878 if (IS_ENABLED(CONFIG_ARCH_ZYNQMP) || IS_ENABLED(CONFIG_ARCH_VERSAL)) {
Ashok Reddy Somadd745772022-02-23 15:13:31 +0100879 u32 timeout = 1000000;
T Karthik Reddyab0a7492021-08-02 23:20:45 -0600880
881 while (((sdhci_readl(host, SDHCI_PRESENT_STATE) &
Ashok Reddy Soma09f38902022-02-23 15:13:30 +0100882 SDHCI_CARD_STATE_STABLE) == 0) && timeout) {
Ashok Reddy Somadd745772022-02-23 15:13:31 +0100883 udelay(1);
Ashok Reddy Soma09f38902022-02-23 15:13:30 +0100884 timeout--;
T Karthik Reddyab0a7492021-08-02 23:20:45 -0600885 }
886 if (!timeout) {
887 dev_err(dev, "Sdhci card detect state not stable\n");
888 return -ETIMEDOUT;
889 }
890 }
891
Simon Glass4cc87fb2016-07-05 17:10:15 -0600892 return sdhci_probe(dev);
Michal Simek0dd222b2013-04-22 14:56:49 +0200893}
Michal Simek9ecd2682015-11-30 16:13:03 +0100894
Simon Glassaad29ae2020-12-03 16:55:21 -0700895static int arasan_sdhci_of_to_plat(struct udevice *dev)
Michal Simek9ecd2682015-11-30 16:13:03 +0100896{
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530897 struct arasan_sdhci_priv *priv = dev_get_priv(dev);
Michal Simek9ecd2682015-11-30 16:13:03 +0100898
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530899 priv->host = calloc(1, sizeof(struct sdhci_host));
900 if (!priv->host)
901 return -1;
902
903 priv->host->name = dev->name;
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530904
Ashok Reddy Soma568edfd2020-10-23 04:59:02 -0600905#if defined(CONFIG_ARCH_ZYNQMP) || defined(CONFIG_ARCH_VERSAL)
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530906 priv->host->ops = &arasan_ops;
Michal Simek33a6b772020-10-23 04:59:00 -0600907 arasan_dt_parse_clk_phases(dev);
Siva Durga Prasad Paladugucad14a82018-04-19 12:37:09 +0530908#endif
Michal Simek9ecd2682015-11-30 16:13:03 +0100909
Michal Simek921a8de2018-05-16 10:57:07 +0200910 priv->host->ioaddr = (void *)dev_read_addr(dev);
911 if (IS_ERR(priv->host->ioaddr))
912 return PTR_ERR(priv->host->ioaddr);
913
914 priv->deviceid = dev_read_u32_default(dev, "xlnx,device_id", -1);
Michal Simeke40ae572020-07-22 17:46:31 +0200915 priv->bank = dev_read_u32_default(dev, "xlnx,mio-bank", 0);
Ashok Reddy Soma61e0df92020-10-23 04:58:57 -0600916 priv->no_1p8 = dev_read_bool(dev, "no-1-8-v");
Stefan Herbrechtsmeier5567b422017-01-17 16:27:33 +0100917
Michal Simek9ecd2682015-11-30 16:13:03 +0100918 return 0;
919}
920
Simon Glass4cc87fb2016-07-05 17:10:15 -0600921static int arasan_sdhci_bind(struct udevice *dev)
922{
Simon Glassfa20e932020-12-03 16:55:20 -0700923 struct arasan_sdhci_plat *plat = dev_get_plat(dev);
Simon Glass4cc87fb2016-07-05 17:10:15 -0600924
Masahiro Yamadacdb67f32016-09-06 22:17:32 +0900925 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
Simon Glass4cc87fb2016-07-05 17:10:15 -0600926}
927
Michal Simek9ecd2682015-11-30 16:13:03 +0100928static const struct udevice_id arasan_sdhci_ids[] = {
929 { .compatible = "arasan,sdhci-8.9a" },
930 { }
931};
932
933U_BOOT_DRIVER(arasan_sdhci_drv) = {
934 .name = "arasan_sdhci",
935 .id = UCLASS_MMC,
936 .of_match = arasan_sdhci_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700937 .of_to_plat = arasan_sdhci_of_to_plat,
Simon Glass4cc87fb2016-07-05 17:10:15 -0600938 .ops = &sdhci_ops,
939 .bind = arasan_sdhci_bind,
Michal Simek9ecd2682015-11-30 16:13:03 +0100940 .probe = arasan_sdhci_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700941 .priv_auto = sizeof(struct arasan_sdhci_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700942 .plat_auto = sizeof(struct arasan_sdhci_plat),
Michal Simek9ecd2682015-11-30 16:13:03 +0100943};