summaryrefslogtreecommitdiffstats
path: root/drivers/cadence/emmc/cdns_sdmmc.c
blob: d2cd4d6a54b0efe951987e479c9825d1bd2d709d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
/*
 * Copyright (c) 2022-2023, Intel Corporation. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <stddef.h>
#include <string.h>

#include <arch_helpers.h>
#include <common/debug.h>
#include <drivers/cadence/cdns_sdmmc.h>
#include <drivers/delay_timer.h>
#include <drivers/mmc.h>
#include <lib/mmio.h>
#include <lib/utils.h>

/* Card busy and present */
#define CARD_BUSY					1
#define CARD_NOT_BUSY					0

/* 500 ms delay to read the RINST register */
#define DELAY_MS_SRS_READ				500
#define DELAY_RES					10

/* SRS12 error mask */
#define SRS12_ERR_MASK					0xFFFF8000

/* Check DV dfi_init val=0 */
#define IO_MASK_END_DATA				0x0

/* Check DV dfi_init val=2; DDR Mode */
#define IO_MASK_END_DATA_DDR				0x2
#define IO_MASK_START_DATA				0x0
#define DATA_SELECT_OE_END_DATA				0x1

#define TIMEOUT						100000

/* General define */
#define SDHC_REG_MASK					UINT_MAX
#define SD_HOST_BLOCK_SIZE				0x200
#define DTCVVAL_DEFAULT_VAL				0xE
#define CDMMC_DMA_MAX_BUFFER_SIZE			64*1024
#define CDNSMMC_ADDRESS_MASK				U(0x0f)
#define CONFIG_CDNS_DESC_COUNT				8

void cdns_init(void);
int cdns_send_cmd(struct mmc_cmd *cmd);
int cdns_set_ios(unsigned int clk, unsigned int width);
int cdns_prepare(int lba, uintptr_t buf, size_t size);
int cdns_read(int lba, uintptr_t buf, size_t size);
int cdns_write(int lba, uintptr_t buf, size_t size);

const struct mmc_ops cdns_sdmmc_ops = {
	.init			= cdns_init,
	.send_cmd		= cdns_send_cmd,
	.set_ios		= cdns_set_ios,
	.prepare		= cdns_prepare,
	.read			= cdns_read,
	.write			= cdns_write,
};

struct cdns_sdmmc_params cdns_params;
struct cdns_sdmmc_combo_phy sdmmc_combo_phy_reg;
struct cdns_sdmmc_sdhc sdmmc_sdhc_reg;
#ifdef CONFIG_DMA_ADDR_T_64BIT
struct cdns_idmac_desc cdns_desc[CONFIG_CDNS_DESC_COUNT];
#else
struct cdns_idmac_desc cdns_desc[CONFIG_CDNS_DESC_COUNT] __aligned(32);
#endif

bool data_cmd;

int cdns_wait_ics(uint16_t timeout, uint32_t cdn_srs_res)
{
	/* Clock for sdmclk and sdclk */
	uint32_t count = 0;
	uint32_t data = 0;

	/* Wait status command response ready */
	do {
		data = mmio_read_32(cdn_srs_res);
		count++;
		if (count >= timeout) {
			return -ETIMEDOUT;
		}
	} while ((data & (1 << SDMMC_CDN_ICS)) == 0);

	return 0;
}

int cdns_busy(void)
{
	unsigned int data;

	data = mmio_read_32(MMC_REG_BASE + SDHC_CDNS_SRS09);
	return (data & STATUS_DATA_BUSY) ? CARD_BUSY : CARD_NOT_BUSY;
}

int cdns_vol_reset(void)
{
	/* Reset embedded card */
	mmio_write_32((MMC_REG_BASE + SDHC_CDNS_SRS10), (7 << SDMMC_CDN_BVS) | (1 << SDMMC_CDN_BP));
	udelay(250);
	mmio_write_32((MMC_REG_BASE + SDHC_CDNS_SRS10), (7 << SDMMC_CDN_BVS) | (0 << SDMMC_CDN_BP));
	udelay(500);

	/* Turn on supply voltage */
	/* BVS = 7, BP = 1, BP2 only in UHS2 mode */
	mmio_write_32((MMC_REG_BASE + SDHC_CDNS_SRS10), (7 << SDMMC_CDN_BVS) | (1 << SDMMC_CDN_BP));
	udelay(250);
	return 0;
}

void cdns_set_sdmmc_var(struct cdns_sdmmc_combo_phy *combo_phy_reg,
	struct cdns_sdmmc_sdhc *sdhc_reg)
{
	/* Values are taken by the reference of cadence IP documents */
	combo_phy_reg->cp_clk_wr_delay = 0;
	combo_phy_reg->cp_clk_wrdqs_delay = 0;
	combo_phy_reg->cp_data_select_oe_end = 0;
	combo_phy_reg->cp_dll_bypass_mode = 1;
	combo_phy_reg->cp_dll_locked_mode = 0;
	combo_phy_reg->cp_dll_start_point = 0;
	combo_phy_reg->cp_gate_cfg_always_on = 1;
	combo_phy_reg->cp_io_mask_always_on = 0;
	combo_phy_reg->cp_io_mask_end = 0;
	combo_phy_reg->cp_io_mask_start = 0;
	combo_phy_reg->cp_rd_del_sel = 52;
	combo_phy_reg->cp_read_dqs_cmd_delay = 0;
	combo_phy_reg->cp_read_dqs_delay = 0;
	combo_phy_reg->cp_sw_half_cycle_shift = 0;
	combo_phy_reg->cp_sync_method = 1;
	combo_phy_reg->cp_underrun_suppress = 1;
	combo_phy_reg->cp_use_ext_lpbk_dqs = 1;
	combo_phy_reg->cp_use_lpbk_dqs = 1;
	combo_phy_reg->cp_use_phony_dqs = 1;
	combo_phy_reg->cp_use_phony_dqs_cmd = 1;

	sdhc_reg->sdhc_extended_rd_mode = 1;
	sdhc_reg->sdhc_extended_wr_mode = 1;
	sdhc_reg->sdhc_hcsdclkadj = 0;
	sdhc_reg->sdhc_idelay_val = 0;
	sdhc_reg->sdhc_rdcmd_en = 1;
	sdhc_reg->sdhc_rddata_en = 1;
	sdhc_reg->sdhc_rw_compensate = 9;
	sdhc_reg->sdhc_sdcfsh = 0;
	sdhc_reg->sdhc_sdcfsl = 1;
	sdhc_reg->sdhc_wrcmd0_dly = 1;
	sdhc_reg->sdhc_wrcmd0_sdclk_dly = 0;
	sdhc_reg->sdhc_wrcmd1_dly = 0;
	sdhc_reg->sdhc_wrcmd1_sdclk_dly = 0;
	sdhc_reg->sdhc_wrdata0_dly = 1;
	sdhc_reg->sdhc_wrdata0_sdclk_dly = 0;
	sdhc_reg->sdhc_wrdata1_dly = 0;
	sdhc_reg->sdhc_wrdata1_sdclk_dly = 0;
}

static int cdns_program_phy_reg(struct cdns_sdmmc_combo_phy *combo_phy_reg,
	struct cdns_sdmmc_sdhc *sdhc_reg)
{
	uint32_t value = 0;
	int ret = 0;

	/* program PHY_DQS_TIMING_REG */
	value = (CP_USE_EXT_LPBK_DQS(combo_phy_reg->cp_use_ext_lpbk_dqs)) |
		(CP_USE_LPBK_DQS(combo_phy_reg->cp_use_lpbk_dqs)) |
		(CP_USE_PHONY_DQS(combo_phy_reg->cp_use_phony_dqs)) |
		(CP_USE_PHONY_DQS_CMD(combo_phy_reg->cp_use_phony_dqs_cmd));
	ret = cdns_sdmmc_write_phy_reg(MMC_REG_BASE + SDHC_CDNS_HRS04,
			COMBO_PHY_REG + PHY_DQS_TIMING_REG, MMC_REG_BASE +
			SDHC_CDNS_HRS05, value);
	if (ret != 0) {
		return ret;
	}

	/* program PHY_GATE_LPBK_CTRL_REG */
	value = (CP_SYNC_METHOD(combo_phy_reg->cp_sync_method)) |
		(CP_SW_HALF_CYCLE_SHIFT(combo_phy_reg->cp_sw_half_cycle_shift)) |
		(CP_RD_DEL_SEL(combo_phy_reg->cp_rd_del_sel)) |
		(CP_UNDERRUN_SUPPRESS(combo_phy_reg->cp_underrun_suppress)) |
		(CP_GATE_CFG_ALWAYS_ON(combo_phy_reg->cp_gate_cfg_always_on));
	ret = cdns_sdmmc_write_phy_reg(MMC_REG_BASE + SDHC_CDNS_HRS04,
			COMBO_PHY_REG + PHY_GATE_LPBK_CTRL_REG, MMC_REG_BASE +
			SDHC_CDNS_HRS05, value);
	if (ret != 0) {
		return ret;
	}

	/* program PHY_DLL_MASTER_CTRL_REG */
	value = (CP_DLL_BYPASS_MODE(combo_phy_reg->cp_dll_bypass_mode))
			| (CP_DLL_START_POINT(combo_phy_reg->cp_dll_start_point));
	ret = cdns_sdmmc_write_phy_reg(MMC_REG_BASE + SDHC_CDNS_HRS04,
			COMBO_PHY_REG + PHY_DLL_MASTER_CTRL_REG, MMC_REG_BASE
			+ SDHC_CDNS_HRS05, value);
	if (ret != 0) {
		return ret;
	}

	/* program PHY_DLL_SLAVE_CTRL_REG */
	value = (CP_READ_DQS_CMD_DELAY(combo_phy_reg->cp_read_dqs_cmd_delay))
		| (CP_CLK_WRDQS_DELAY(combo_phy_reg->cp_clk_wrdqs_delay))
		| (CP_CLK_WR_DELAY(combo_phy_reg->cp_clk_wr_delay))
		| (CP_READ_DQS_DELAY(combo_phy_reg->cp_read_dqs_delay));
	ret = cdns_sdmmc_write_phy_reg(MMC_REG_BASE + SDHC_CDNS_HRS04,
			COMBO_PHY_REG + PHY_DLL_SLAVE_CTRL_REG, MMC_REG_BASE
			+ SDHC_CDNS_HRS05, value);
	if (ret != 0) {
		return ret;
	}

	/* program PHY_CTRL_REG */
	mmio_write_32(MMC_REG_BASE + SDHC_CDNS_HRS04, COMBO_PHY_REG
			+ PHY_CTRL_REG);
	value = mmio_read_32(MMC_REG_BASE + SDHC_CDNS_HRS05);

	/* phony_dqs_timing=0 */
	value &= ~(CP_PHONY_DQS_TIMING_MASK << CP_PHONY_DQS_TIMING_SHIFT);
	mmio_write_32(MMC_REG_BASE + SDHC_CDNS_HRS05, value);

	/* switch off DLL_RESET */
	do {
		value = mmio_read_32(MMC_REG_BASE + SDHC_CDNS_HRS09);
		value |= SDHC_PHY_SW_RESET;
		mmio_write_32(MMC_REG_BASE + SDHC_CDNS_HRS09, value);
		value = mmio_read_32(MMC_REG_BASE + SDHC_CDNS_HRS09);
	/* polling PHY_INIT_COMPLETE */
	} while ((value & SDHC_PHY_INIT_COMPLETE) != SDHC_PHY_INIT_COMPLETE);

	/* program PHY_DQ_TIMING_REG */
	combo_phy_reg->cp_io_mask_end = 0U;
	value = (CP_IO_MASK_ALWAYS_ON(combo_phy_reg->cp_io_mask_always_on))
		| (CP_IO_MASK_END(combo_phy_reg->cp_io_mask_end))
		| (CP_IO_MASK_START(combo_phy_reg->cp_io_mask_start))
		| (CP_DATA_SELECT_OE_END(combo_phy_reg->cp_data_select_oe_end));

	ret = cdns_sdmmc_write_phy_reg(MMC_REG_BASE + SDHC_CDNS_HRS04,
			COMBO_PHY_REG + PHY_DQ_TIMING_REG, MMC_REG_BASE
			+ SDHC_CDNS_HRS05, value);
	if (ret != 0) {
		return ret;
	}
	return 0;
}

int cdns_read(int lba, uintptr_t buf, size_t size)
{
	inv_dcache_range(buf, size);

	return 0;
}

void cdns_init(void)
{
	/* Dummy function pointer for cdns_init. */
}

int cdns_prepare(int dma_start_addr, uintptr_t dma_buff, size_t size)
{
	data_cmd = true;
	struct cdns_idmac_desc *desc;
	uint32_t desc_cnt, i;
	uint64_t desc_base;

	assert(((dma_buff & CDNSMMC_ADDRESS_MASK) == 0) &&
			(cdns_params.desc_size > 0) &&
			((MMC_REG_BASE & MMC_BLOCK_MASK) == 0) &&
			((cdns_params.desc_base & MMC_BLOCK_MASK) == 0) &&
			((cdns_params.desc_size & MMC_BLOCK_MASK) == 0));

	flush_dcache_range(dma_buff, size);

	desc_cnt = (size + (CDMMC_DMA_MAX_BUFFER_SIZE) - 1) / (CDMMC_DMA_MAX_BUFFER_SIZE);
	assert(desc_cnt * sizeof(struct cdns_idmac_desc) < cdns_params.desc_size);

	if (desc_cnt > CONFIG_CDNS_DESC_COUNT) {
		ERROR("Requested data transfer length %ld is greater than configured length %d",
				size, (CONFIG_CDNS_DESC_COUNT * CDMMC_DMA_MAX_BUFFER_SIZE));
		return -EINVAL;
	}

	desc = (struct cdns_idmac_desc *)cdns_params.desc_base;
	desc_base = (uint64_t)desc;
	i = 0;

	while ((i + 1) < desc_cnt) {
		desc->attr = ADMA_DESC_ATTR_VALID | ADMA_DESC_TRANSFER_DATA;
		desc->reserved = 0;
		desc->len = MAX_64KB_PAGE;
		desc->addr_lo = (dma_buff & UINT_MAX) + (CDMMC_DMA_MAX_BUFFER_SIZE * i);
#if CONFIG_DMA_ADDR_T_64BIT == 1
		desc->addr_hi = (dma_buff >> 32) & 0xffffffff;
#endif
		size -= CDMMC_DMA_MAX_BUFFER_SIZE;
		desc++;
		i++;
	}

	desc->attr = ADMA_DESC_ATTR_VALID | ADMA_DESC_TRANSFER_DATA |
			ADMA_DESC_ATTR_END;
	desc->reserved = 0;
	desc->len = size;
#if CONFIG_DMA_ADDR_T_64BIT == 1
	desc->addr_lo = (dma_buff & UINT_MAX) + (CDMMC_DMA_MAX_BUFFER_SIZE * i);
	desc->addr_hi = (dma_buff >> 32) & UINT_MAX;
#else
	desc->addr_lo = (dma_buff & UINT_MAX);
#endif

	mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS22, (uint32_t)desc_base);
	mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS23, (uint32_t)(desc_base >> 32));
	flush_dcache_range(cdns_params.desc_base,
				desc_cnt * CDMMC_DMA_MAX_BUFFER_SIZE);

	mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS01,
			((512 << BLOCK_SIZE) | ((size/512) << BLK_COUNT_CT) | SDMA_BUF));
	return 0;
}

static void cdns_host_set_clk(int clk)
{
	uint32_t ret = 0;
	uint32_t sdclkfsval = 0;
	uint32_t dtcvval = DTCVVAL_DEFAULT_VAL;

	sdclkfsval = (cdns_params.clk_rate / 2000) / clk;
	mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS11, 0);
	mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS11, (dtcvval << SDMMC_CDN_DTCV) |
			(sdclkfsval << SDMMC_CDN_SDCLKFS) | (1 << SDMMC_CDN_ICE));

	ret = cdns_wait_ics(5000, MMC_REG_BASE + SDHC_CDNS_SRS11);
	if (ret != 0U) {
		ERROR("Waiting SDMMC_CDN_ICS timeout");
	}

	/* Enable DLL reset */
	mmio_write_32(MMC_REG_BASE + SDHC_CDNS_HRS09, mmio_read_32(MMC_REG_BASE + SDHC_CDNS_HRS09) &
			~SDHC_DLL_RESET_MASK);
	/* Set extended_wr_mode */
	mmio_write_32(MMC_REG_BASE + SDHC_CDNS_HRS09, (mmio_read_32(MMC_REG_BASE + SDHC_CDNS_HRS09)
			& SDHC_EXTENDED_WR_MODE_MASK) | (1 << EXTENDED_WR_MODE));
	/* Release DLL reset */
	mmio_write_32(MMC_REG_BASE + SDHC_CDNS_HRS09, mmio_read_32(MMC_REG_BASE
			+ SDHC_CDNS_HRS09) | 1);
	mmio_write_32(MMC_REG_BASE + SDHC_CDNS_HRS09, mmio_read_32(MMC_REG_BASE
			+ SDHC_CDNS_HRS09) | (3 << RDCMD_EN));

	do {
		mmio_read_32(MMC_REG_BASE + SDHC_CDNS_HRS09);
	} while (~mmio_read_32(MMC_REG_BASE + SDHC_CDNS_HRS09) & (1 << 1));

	mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS11, (dtcvval << SDMMC_CDN_DTCV) |
	(sdclkfsval << SDMMC_CDN_SDCLKFS) | (1 << SDMMC_CDN_ICE) | (1 << SDMMC_CDN_SDCE));
	mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS13, UINT_MAX);
}

int cdns_set_ios(unsigned int clk, unsigned int width)
{

	switch (width) {
	case MMC_BUS_WIDTH_1:
		mmio_write_32((MMC_REG_BASE + SDHC_CDNS_SRS10), LEDC_OFF);
		break;
	case MMC_BUS_WIDTH_4:
		mmio_write_32((MMC_REG_BASE + SDHC_CDNS_SRS10), DTW_4BIT);
		break;
	case MMC_BUS_WIDTH_8:
		mmio_write_32((MMC_REG_BASE + SDHC_CDNS_SRS10), EDTW_8BIT);
		break;
	default:
		assert(0);
		break;
	}
	cdns_host_set_clk(clk);

	return 0;
}

int cdns_sdmmc_write_sd_host_reg(uint32_t addr, uint32_t data)
{
	uint32_t value = 0;

	value = mmio_read_32(addr);
	value &= ~SDHC_REG_MASK;
	value |= data;
	mmio_write_32(addr, value);
	value = mmio_read_32(addr);
	if (value != data) {
		ERROR("SD host address is not set properly\n");
		return -ENXIO;
	}

	return 0;
}

int cdns_write(int lba, uintptr_t buf, size_t size)
{
	return 0;
}

static int cdns_init_hrs_io(struct cdns_sdmmc_combo_phy *combo_phy_reg,
	struct cdns_sdmmc_sdhc *sdhc_reg)
{
	uint32_t value = 0;
	int ret = 0;

	/* program HRS09, register 42 */
	value = (SDHC_RDDATA_EN(sdhc_reg->sdhc_rddata_en))
		| (SDHC_RDCMD_EN(sdhc_reg->sdhc_rdcmd_en))
		| (SDHC_EXTENDED_WR_MODE(sdhc_reg->sdhc_extended_wr_mode))
		| (SDHC_EXTENDED_RD_MODE(sdhc_reg->sdhc_extended_rd_mode));
	ret = cdns_sdmmc_write_sd_host_reg(MMC_REG_BASE + SDHC_CDNS_HRS09, value);
	if (ret != 0) {
		ERROR("Program HRS09 failed");
		return ret;
	}

	/* program HRS10, register 43 */
	value = (SDHC_HCSDCLKADJ(sdhc_reg->sdhc_hcsdclkadj));
	ret = cdns_sdmmc_write_sd_host_reg(MMC_REG_BASE + SDHC_CDNS_HRS10, value);
	if (ret != 0) {
		ERROR("Program HRS10 failed");
		return ret;
	}

	/* program HRS16, register 48 */
	value = (SDHC_WRDATA1_SDCLK_DLY(sdhc_reg->sdhc_wrdata1_sdclk_dly))
		| (SDHC_WRDATA0_SDCLK_DLY(sdhc_reg->sdhc_wrdata0_sdclk_dly))
		| (SDHC_WRCMD1_SDCLK_DLY(sdhc_reg->sdhc_wrcmd1_sdclk_dly))
		| (SDHC_WRCMD0_SDCLK_DLY(sdhc_reg->sdhc_wrcmd0_sdclk_dly))
		| (SDHC_WRDATA1_DLY(sdhc_reg->sdhc_wrdata1_dly))
		| (SDHC_WRDATA0_DLY(sdhc_reg->sdhc_wrdata0_dly))
		| (SDHC_WRCMD1_DLY(sdhc_reg->sdhc_wrcmd1_dly))
		| (SDHC_WRCMD0_DLY(sdhc_reg->sdhc_wrcmd0_dly));
	ret = cdns_sdmmc_write_sd_host_reg(MMC_REG_BASE + SDHC_CDNS_HRS16, value);
	if (ret != 0) {
		ERROR("Program HRS16 failed");
		return ret;
	}

	/* program HRS07, register 40 */
	value = (SDHC_RW_COMPENSATE(sdhc_reg->sdhc_rw_compensate))
		| (SDHC_IDELAY_VAL(sdhc_reg->sdhc_idelay_val));
	ret = cdns_sdmmc_write_sd_host_reg(MMC_REG_BASE + SDHC_CDNS_HRS07, value);
	if (ret != 0) {
		ERROR("Program HRS07 failed");
		return ret;
	}

	return ret;
}

static int cdns_hc_set_clk(struct cdns_sdmmc_params *cdn_sdmmc_dev_mode_params)
{
	uint32_t ret = 0;
	uint32_t dtcvval, sdclkfsval;

	dtcvval = DTC_VAL;
	sdclkfsval = 0;

	if ((cdn_sdmmc_dev_mode_params->cdn_sdmmc_dev_mode == SD_DS) ||
		(cdn_sdmmc_dev_mode_params->cdn_sdmmc_dev_mode == SD_UHS_SDR12) ||
		(cdn_sdmmc_dev_mode_params->cdn_sdmmc_dev_mode == EMMC_SDR_BC)) {
		sdclkfsval = 4;
	} else if ((cdn_sdmmc_dev_mode_params->cdn_sdmmc_dev_mode == SD_HS) ||
		(cdn_sdmmc_dev_mode_params->cdn_sdmmc_dev_mode == SD_UHS_SDR25) ||
		(cdn_sdmmc_dev_mode_params->cdn_sdmmc_dev_mode == SD_UHS_DDR50) ||
		(cdn_sdmmc_dev_mode_params->cdn_sdmmc_dev_mode == EMMC_SDR)) {
		sdclkfsval = 2;
	} else if ((cdn_sdmmc_dev_mode_params->cdn_sdmmc_dev_mode == SD_UHS_SDR50) ||
		(cdn_sdmmc_dev_mode_params->cdn_sdmmc_dev_mode == EMMC_DDR) ||
		(cdn_sdmmc_dev_mode_params->cdn_sdmmc_dev_mode == EMMC_HS400) ||
		(cdn_sdmmc_dev_mode_params->cdn_sdmmc_dev_mode == EMMC_HS400es)) {
		sdclkfsval = 1;
	} else if ((cdn_sdmmc_dev_mode_params->cdn_sdmmc_dev_mode == SD_UHS_SDR104) ||
		(cdn_sdmmc_dev_mode_params->cdn_sdmmc_dev_mode == EMMC_HS200)) {
		sdclkfsval = 0;
	}

	mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS11, 0);
	mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS11, (dtcvval << SDMMC_CDN_DTCV) |
		(sdclkfsval << SDMMC_CDN_SDCLKFS) | (1 << SDMMC_CDN_ICE));
	ret = cdns_wait_ics(5000, MMC_REG_BASE + SDHC_CDNS_SRS11);
	if (ret != 0U) {
		ERROR("Waiting SDMMC_CDN_ICS timeout");
		return ret;
	}

	/* Enable DLL reset */
	mmio_write_32((MMC_REG_BASE + SDHC_CDNS_HRS09), mmio_read_32(MMC_REG_BASE
			+ SDHC_CDNS_HRS09) & ~SDHC_DLL_RESET_MASK);
	/* Set extended_wr_mode */
	mmio_write_32((MMC_REG_BASE + SDHC_CDNS_HRS09),
	(mmio_read_32(MMC_REG_BASE + SDHC_CDNS_HRS09) &	SDHC_EXTENDED_WR_MODE_MASK) |
			(1 << EXTENDED_WR_MODE));
	/* Release DLL reset */
	mmio_write_32(MMC_REG_BASE + SDHC_CDNS_HRS09, mmio_read_32(MMC_REG_BASE
			+ SDHC_CDNS_HRS09) | 1);
	mmio_write_32(MMC_REG_BASE + SDHC_CDNS_HRS09, mmio_read_32(MMC_REG_BASE
			+ SDHC_CDNS_HRS09) | (3 << RDCMD_EN));
	do {
		mmio_read_32(MMC_REG_BASE + SDHC_CDNS_HRS09);
	} while (~mmio_read_32(MMC_REG_BASE + SDHC_CDNS_HRS09) & (1 << 1));

	mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS11, (dtcvval << SDMMC_CDN_DTCV) |
		(sdclkfsval << SDMMC_CDN_SDCLKFS) | (1 << SDMMC_CDN_ICE) | (1 << SDMMC_CDN_SDCE));

	mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS13, UINT_MAX);
	return 0;
}

int cdns_reset(void)
{
	uint32_t data = 0;
	uint32_t count = 0;
	uint32_t value = 0;

	value = mmio_read_32(MMC_REG_BASE + SDHC_CDNS_SRS11);
	value &= ~(0xFFFF);
	value |= 0x0;
	mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS11, value);
	udelay(500);

	/* Software reset */
	mmio_write_32(MMC_REG_BASE + SDHC_CDNS_HRS00, 1);
	/* Wait status command response ready */
	do {
		data = mmio_read_32(MMC_REG_BASE + SDHC_CDNS_HRS00);
		count++;
		if (count >= 5000) {
			return -ETIMEDOUT;
		}
	/* Wait for HRS00.SWR */
	} while ((data & 1) == 1);

	/* Step 1, switch on DLL_RESET */
	value = mmio_read_32(MMC_REG_BASE + SDHC_CDNS_HRS09);
	value &= ~SDHC_PHY_SW_RESET;
	mmio_write_32(MMC_REG_BASE + SDHC_CDNS_HRS09, value);

	return 0;
}

int cdns_sd_host_init(struct cdns_sdmmc_combo_phy *mmc_combo_phy_reg,
struct cdns_sdmmc_sdhc *mmc_sdhc_reg)
{
	int ret = 0;

	ret = cdns_reset();
	if (ret != 0) {
		ERROR("Program phy reg init failed");
		return ret;
	}

	ret = cdns_program_phy_reg(&sdmmc_combo_phy_reg, &sdmmc_sdhc_reg);
	if (ret != 0) {
		ERROR("Program phy reg init failed");
		return ret;
	}

	ret = cdns_init_hrs_io(&sdmmc_combo_phy_reg, &sdmmc_sdhc_reg);
	if (ret != 0) {
		ERROR("Program init for HRS reg is failed");
		return ret;
	}

	ret = cdns_sd_card_detect();
	if (ret != 0) {
		ERROR("SD card does not detect");
		return ret;
	}

	ret = cdns_vol_reset();
	if (ret != 0) {
		ERROR("eMMC card reset failed");
		return ret;
	}

	ret = cdns_hc_set_clk(&cdns_params);
	if (ret != 0) {
		ERROR("hc set clk failed");
		return ret;
	}

	return 0;
}

void cdns_srs10_value_toggle(uint8_t write_val, uint8_t prev_val)
{
	uint32_t data_op = 0U;

	data_op = mmio_read_32(MMC_REG_BASE + SDHC_CDNS_SRS10);
	mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS10, (data_op & (prev_val << 0)));
	mmio_read_32(MMC_REG_BASE + SDHC_CDNS_SRS10);
	mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS10, data_op | (write_val << 0));
}

void cdns_srs11_srs15_config(uint32_t srs11_val, uint32_t srs15_val)
{
	uint32_t data = 0U;

	data = mmio_read_32(MMC_REG_BASE + SDHC_CDNS_SRS11);
	mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS11, (data | srs11_val));
	data = mmio_read_32(MMC_REG_BASE + SDHC_CDNS_SRS15);
	mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS15, (data | srs15_val));
}

int cdns_send_cmd(struct mmc_cmd *cmd)
{
	uint32_t op = 0, ret = 0;
	uint8_t write_value = 0, prev_val = 0;
	uint32_t value;
	int32_t timeout;
	uint32_t cmd_indx;
	uint32_t status = 0, srs15_val = 0, srs11_val = 0;
	uint32_t status_check = 0;

	assert(cmd);
	cmd_indx = (cmd->cmd_idx) << COM_IDX;

	if (data_cmd) {
		switch (cmd->cmd_idx) {
		case SD_SWITCH:
			op = DATA_PRESENT;
			write_value = ADMA2_32 | DT_WIDTH;
			prev_val = ADMA2_32 | DT_WIDTH;
			cdns_srs10_value_toggle(write_value, prev_val);
			srs11_val = READ_CLK | SDMMC_CDN_ICE | SDMMC_CDN_ICS | SDMMC_CDN_SDCE;
			srs15_val = BIT_AD_64 | HV4E | V18SE;
			cdns_srs11_srs15_config(srs11_val, srs15_val);
			break;

		case SD_WRITE_SINGLE_BLOCK:
		case SD_READ_SINGLE_BLOCK:
			op = DATA_PRESENT;
			write_value = ADMA2_32 | HS_EN | DT_WIDTH | LEDC;
			prev_val = ADMA2_32 | HS_EN | DT_WIDTH;
			cdns_srs10_value_toggle(write_value, prev_val);
			srs15_val = PVE | BIT_AD_64 | HV4E | SDR104_MODE | V18SE;
			srs11_val = READ_CLK | SDMMC_CDN_ICE | SDMMC_CDN_ICS | SDMMC_CDN_SDCE;
			cdns_srs11_srs15_config(srs11_val, srs15_val);
			mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS00, SAAR);
			break;

		case SD_WRITE_MULTIPLE_BLOCK:
		case SD_READ_MULTIPLE_BLOCK:
			op = DATA_PRESENT | AUTO_CMD_EN | MULTI_BLK_READ;
			write_value = ADMA2_32 | HS_EN | DT_WIDTH | LEDC;
			prev_val = ADMA2_32 | HS_EN | DT_WIDTH;
			cdns_srs10_value_toggle(write_value, prev_val);
			srs15_val = PVE | BIT_AD_64 | HV4E | SDR104_MODE | V18SE;
			srs11_val = READ_CLK | SDMMC_CDN_ICE | SDMMC_CDN_ICS | SDMMC_CDN_SDCE;
			cdns_srs11_srs15_config(srs11_val, srs15_val);
			mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS00, SAAR);
			break;

		case SD_APP_SEND_SCR:
			op = DATA_PRESENT;
			write_value = ADMA2_32 | LEDC;
			prev_val = LEDC;
			cdns_srs10_value_toggle(write_value, prev_val);
			srs15_val = BIT_AD_64 | HV4E | V18SE;
			srs11_val = READ_CLK | SDMMC_CDN_ICE | SDMMC_CDN_ICS | SDMMC_CDN_SDCE;
			cdns_srs11_srs15_config(srs11_val, srs15_val);
			break;

		case SD_SEND_IF_COND:
			op = DATA_PRESENT | CMD_IDX_CHK_ENABLE;
			write_value = LEDC;
			prev_val = 0x0;
			cdns_srs10_value_toggle(write_value, prev_val);
			srs15_val = HV4E;
			srs11_val = READ_CLK | SDMMC_CDN_ICE | SDMMC_CDN_ICS | SDMMC_CDN_SDCE;
			cdns_srs11_srs15_config(srs11_val, srs15_val);
			break;

		default:
			write_value = LEDC;
			prev_val = 0x0;
			cdns_srs10_value_toggle(write_value, prev_val);
			op = 0;
			break;
		}
	} else {
		switch (cmd->cmd_idx) {
		case SD_GO_IDLE_STATE:
			write_value = LEDC;
			prev_val = 0x0;
			cdns_srs10_value_toggle(write_value, prev_val);
			srs15_val = HV4E;
			srs11_val = SDMMC_CDN_ICE | SDMMC_CDN_ICS | SDMMC_CDN_SDCE;
			cdns_srs11_srs15_config(srs11_val, srs15_val);
			break;

		case SD_ALL_SEND_CID:
			write_value = LEDC;
			prev_val = 0x0;
			cdns_srs10_value_toggle(write_value, prev_val);
			srs15_val = HV4E | V18SE;
			srs11_val = SDMMC_CDN_ICE | SDMMC_CDN_ICS | SDMMC_CDN_SDCE;
			cdns_srs11_srs15_config(srs11_val, srs15_val);
			break;

		case SD_SEND_IF_COND:
			op = CMD_IDX_CHK_ENABLE;
			write_value = LEDC;
			prev_val = 0x0;
			cdns_srs10_value_toggle(write_value, prev_val);
			srs15_val = HV4E;
			srs11_val = READ_CLK | SDMMC_CDN_ICE | SDMMC_CDN_ICS | SDMMC_CDN_SDCE;
			cdns_srs11_srs15_config(srs11_val, srs15_val);
			break;

		case SD_STOP_TRANSMISSION:
			op = CMD_STOP_ABORT_CMD;
			break;

		case SD_SEND_STATUS:
			break;

		case 1:
			cmd->cmd_arg = 0;
			break;

		case SD_SELECT_CARD:
			op = MULTI_BLK_READ;
			break;

		case SD_APP_CMD:
		default:
			write_value = LEDC;
			prev_val = 0x0;
			cdns_srs10_value_toggle(write_value, prev_val);
			op = 0;
			break;
		}
	}

	switch (cmd->resp_type) {
	case MMC_RESPONSE_NONE:
		op |= CMD_READ | MULTI_BLK_READ | DMA_ENABLED | BLK_CNT_EN;
		break;

	case MMC_RESPONSE_R2:
		op |= CMD_READ | MULTI_BLK_READ | DMA_ENABLED | BLK_CNT_EN |
			RES_TYPE_SEL_136 | CMD_CHECK_RESP_CRC;
		break;

	case MMC_RESPONSE_R3:
		op |= CMD_READ | MULTI_BLK_READ | DMA_ENABLED | BLK_CNT_EN |
			RES_TYPE_SEL_48;
		break;

	case MMC_RESPONSE_R1:
		if ((cmd->cmd_idx == SD_WRITE_SINGLE_BLOCK) || (cmd->cmd_idx
			== SD_WRITE_MULTIPLE_BLOCK)) {
			op |= DMA_ENABLED | BLK_CNT_EN | RES_TYPE_SEL_48
			| CMD_CHECK_RESP_CRC | CMD_IDX_CHK_ENABLE;
		} else {
			op |= DMA_ENABLED | BLK_CNT_EN | CMD_READ | RES_TYPE_SEL_48
			| CMD_CHECK_RESP_CRC | CMD_IDX_CHK_ENABLE;
		}
		break;

	default:
		op |= DMA_ENABLED | BLK_CNT_EN | CMD_READ | MULTI_BLK_READ |
			RES_TYPE_SEL_48 | CMD_CHECK_RESP_CRC | CMD_IDX_CHK_ENABLE;
		break;
	}

	timeout = TIMEOUT;
	do {
		udelay(100);
		ret = cdns_busy();
		if (--timeout <= 0) {
			udelay(50);
			panic();
		}
	} while (ret);

	mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS12, UINT_MAX);

	mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS02, cmd->cmd_arg);
	mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS14, 0x00000000);
	if (cmd_indx == 1)
		mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS03, SDHC_CDNS_SRS03_VALUE);
	else
		mmio_write_32(MMC_REG_BASE + SDHC_CDNS_SRS03, op | cmd_indx);

	timeout = TIMEOUT;
	do {
		udelay(500);
		value = mmio_read_32(MMC_REG_BASE + SDHC_CDNS_SRS12);
	} while (((value & (INT_CMD_DONE | ERROR_INT)) == 0) && (timeout-- > 0));

	timeout = TIMEOUT;

	if (data_cmd) {
		data_cmd = false;
		do {
			udelay(250);
		} while (((value & TRAN_COMP) == 0) && (timeout-- > 0));
	}

	status_check = value & SRS12_ERR_MASK;
	if (status_check != 0U) {
		ERROR("SD host controller send command failed, SRS12 = %x", status);
		return -1;
	}

	if ((op & RES_TYPE_SEL_48) || (op & RES_TYPE_SEL_136)) {
		cmd->resp_data[0] = mmio_read_32(MMC_REG_BASE + SDHC_CDNS_SRS04);
		if (op & RES_TYPE_SEL_136) {
			cmd->resp_data[1] = mmio_read_32(MMC_REG_BASE + SDHC_CDNS_SRS05);
			cmd->resp_data[2] = mmio_read_32(MMC_REG_BASE + SDHC_CDNS_SRS06);
			cmd->resp_data[3] = mmio_read_32(MMC_REG_BASE + SDHC_CDNS_SRS07);
		}
	}

	return 0;
}