summaryrefslogtreecommitdiffstats
path: root/drivers/brcm/emmc/emmc_csl_sdcmd.c
blob: c62886cf6c2abd94b6124f96a9cc698d4fbc6199 (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
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
/*
 * Copyright (c) 2016 - 2020, Broadcom
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <stdlib.h>
#include <stddef.h>

#include "bcm_emmc.h"
#include "emmc_chal_types.h"
#include "emmc_chal_sd.h"
#include "emmc_csl_sdprot.h"
#include "emmc_csl_sdcmd.h"
#include "emmc_csl_sd.h"
#include "emmc_chal_sd.h"
#include "emmc_pboot_hal_memory_drv.h"

int sd_cmd0(struct sd_handle *handle)
{
	int res;
	uint32_t argument = 0x0; /* Go to IDLE state. */

	/* send cmd and parse result */
	res = send_cmd(handle, SD_CMD_GO_IDLE_STATE, argument, 0, NULL);

	if (res == SD_OK) {
		/* Clear all other interrupts */
		chal_sd_clear_irq((void *)handle->device, 0xffffffff);
	}

	return res;
}

int sd_cmd1(struct sd_handle *handle, uint32_t ocr, uint32_t *ocr_output)
{
	int res;
	uint32_t options;
	struct sd_resp resp;

	options = SD_CMDR_RSP_TYPE_R3_4 << SD_CMDR_RSP_TYPE_S;

	if (ocr_output == NULL) {
		EMMC_TRACE("Invalid args\n");
		return SD_FAIL;
	}

	/* send cmd and parse result */
	res = send_cmd(handle, SD_CMD_SEND_OPCOND, ocr, options, &resp);

	if (res == SD_OK)
		*ocr_output = resp.data.r3.ocr;

	return res;
}

int sd_cmd2(struct sd_handle *handle)
{
	uint32_t options;
	struct sd_resp resp;

	/* send cmd and parse result */
	options = SD_CMDR_RSP_TYPE_R2 << SD_CMDR_RSP_TYPE_S;

	return send_cmd(handle, SD_CMD_ALL_SEND_CID, 0, options, &resp);
}

int sd_cmd3(struct sd_handle *handle)
{
	int res;
	uint32_t options = 0;
	uint32_t argument;
	struct sd_resp resp;

	/* use non zero and non 0x1 value for rca */
	handle->device->ctrl.rca = 0x5;
	argument = handle->device->ctrl.rca << SD_CMD7_ARG_RCA_SHIFT;

	options = SD_CMDR_RSP_TYPE_R1_5_6 << SD_CMDR_RSP_TYPE_S |
		  SD4_EMMC_TOP_CMD_CCHK_EN_MASK |
		  SD4_EMMC_TOP_CMD_CRC_EN_MASK;

	/* send cmd and parse result */
	res = send_cmd(handle, SD_CMD_MMC_SET_RCA, argument, options, &resp);

	if (res != SD_OK)
		handle->device->ctrl.rca = 0;

	return res;
}

int sd_cmd7(struct sd_handle *handle, uint32_t rca)
{
	int res;
	uint32_t argument, options;
	struct sd_resp resp;

	argument = (rca << SD_CMD7_ARG_RCA_SHIFT);

	/*
	 * Response to CMD7 is:
	 * R1 while selectiing from Stand-By State to Transfer State
	 * R1b while selecting from Disconnected State to Programming State.
	 *
	 * In this driver, we only issue a CMD7 once, to go to transfer mode
	 * during init_mmc_card().
	 */
	options = SD_CMDR_RSP_TYPE_R1_5_6 << SD_CMDR_RSP_TYPE_S |
		  SD4_EMMC_TOP_CMD_CCHK_EN_MASK |
		  SD4_EMMC_TOP_CMD_CRC_EN_MASK;

	/* send cmd and parse result */
	res = send_cmd(handle, SD_CMD_SELECT_DESELECT_CARD, argument, options,
		       &resp);

	if (res == SD_OK)
		/* Clear all other interrupts */
		chal_sd_clear_irq((void *)handle->device, 0xffffffff);

	return res;
}


/*
 * CMD8 Get CSD_EXT
 */
int mmc_cmd8(struct sd_handle *handle, uint8_t *extCsdReg)
{
	uint32_t res, options;
	struct sd_resp resp;

	data_xfer_setup(handle, extCsdReg, CEATA_EXT_CSDBLOCK_SIZE,
			    SD_XFER_CARD_TO_HOST);

	options = SD_CMDR_RSP_TYPE_R1_5_6 << SD_CMDR_RSP_TYPE_S |
		  SD4_EMMC_TOP_CMD_DPS_MASK | SD4_EMMC_TOP_CMD_DTDS_MASK |
		  SD4_EMMC_TOP_CMD_CCHK_EN_MASK | SD4_EMMC_TOP_CMD_CRC_EN_MASK;

	/* send cmd and parse result */
	res = send_cmd(handle, SD_CMD_READ_EXT_CSD, 0, options, &resp);

	if (res == SD_OK)
		res = process_data_xfer(handle, extCsdReg, 0,
					CEATA_EXT_CSDBLOCK_SIZE,
					SD_XFER_CARD_TO_HOST);

	return res;
}

int sd_cmd9(struct sd_handle *handle, struct sd_card_data *card)
{
	int res;
	uint32_t argument, options, iBlkNum, multiFactor = 1;
	uint32_t maxReadBlockLen = 1, maxWriteBlockLen = 1;
	struct sd_resp resp;

	argument = handle->device->ctrl.rca << SD_CMD7_ARG_RCA_SHIFT;

	options = SD_CMDR_RSP_TYPE_R2 << SD_CMDR_RSP_TYPE_S |
		  SD4_EMMC_TOP_CMD_CRC_EN_MASK;

	/* send cmd and parse result */
	res = send_cmd(handle, SD_CMD_SEND_CSD, argument, options, &resp);

	if (res != SD_OK)
		return res;

	if (handle->card->type == SD_CARD_MMC) {
		card->csd.mmc.structure = (resp.data.r2.rsp4 >> 22) & 0x3;
		card->csd.mmc.csdSpecVer = (resp.data.r2.rsp4 >> 18) & 0x0f;
		card->csd.mmc.taac = (resp.data.r2.rsp4 >> 8) & 0xff;
		card->csd.mmc.nsac = resp.data.r2.rsp4 & 0xff;
		card->csd.mmc.speed = resp.data.r2.rsp3 >> 24;
		card->csd.mmc.classes = (resp.data.r2.rsp3 >> 12) & 0xfff;
		card->csd.mmc.rdBlkLen = (resp.data.r2.rsp3 >> 8) & 0xf;
		card->csd.mmc.rdBlkPartial = (resp.data.r2.rsp3 >> 7) & 0x01;
		card->csd.mmc.wrBlkMisalign = (resp.data.r2.rsp3 >> 6) & 0x1;
		card->csd.mmc.rdBlkMisalign = (resp.data.r2.rsp3 >> 5) & 0x1;
		card->csd.mmc.dsr = (resp.data.r2.rsp2 >> 4) & 0x01;
		card->csd.mmc.size =
		    ((resp.data.r2.rsp3 & 0x3) << 10) +
		    ((resp.data.r2.rsp2 >> 22) & 0x3ff);
		card->csd.mmc.vddRdCurrMin = (resp.data.r2.rsp2 >> 19) & 0x7;
		card->csd.mmc.vddRdCurrMax = (resp.data.r2.rsp2 >> 16) & 0x7;
		card->csd.mmc.vddWrCurrMin = (resp.data.r2.rsp2 >> 13) & 0x7;
		card->csd.mmc.vddWrCurrMax = (resp.data.r2.rsp2 >> 10) & 0x7;
		card->csd.mmc.devSizeMulti = (resp.data.r2.rsp2 >> 7) & 0x7;
		card->csd.mmc.eraseGrpSize = (resp.data.r2.rsp2 >> 2) & 0x1f;
		card->csd.mmc.eraseGrpSizeMulti =
		    ((resp.data.r2.rsp2 & 0x3) << 3) +
		    ((resp.data.r2.rsp1 >> 29) & 0x7);
		card->csd.mmc.wrProtGroupSize =
		    ((resp.data.r2.rsp1 >> 24) & 0x1f);
		card->csd.mmc.wrProtGroupEnable =
		    (resp.data.r2.rsp1 >> 23) & 0x1;
		card->csd.mmc.manuDefEcc = (resp.data.r2.rsp1 >> 21) & 0x3;
		card->csd.mmc.wrSpeedFactor = (resp.data.r2.rsp1 >> 18) & 0x7;
		card->csd.mmc.wrBlkLen = (resp.data.r2.rsp1 >> 14) & 0xf;
		card->csd.mmc.wrBlkPartial = (resp.data.r2.rsp1 >> 13) & 0x1;
		card->csd.mmc.protAppl = (resp.data.r2.rsp1 >> 8) & 0x1;
		card->csd.mmc.copyFlag = (resp.data.r2.rsp1 >> 7) & 0x1;
		card->csd.mmc.permWrProt = (resp.data.r2.rsp1 >> 6) & 0x1;
		card->csd.mmc.tmpWrProt = (resp.data.r2.rsp1 >> 5) & 0x1;
		card->csd.mmc.fileFormat = (resp.data.r2.rsp1 >> 4) & 0x03;
		card->csd.mmc.eccCode = resp.data.r2.rsp1 & 0x03;
		maxReadBlockLen <<= card->csd.mmc.rdBlkLen;
		maxWriteBlockLen <<= card->csd.mmc.wrBlkLen;

		iBlkNum = card->csd.mmc.size + 1;
		multiFactor = (1 << (card->csd.mmc.devSizeMulti + 2));

		handle->card->size =
		    iBlkNum * multiFactor * (1 << card->csd.mmc.rdBlkLen);
	}

	handle->card->maxRdBlkLen = maxReadBlockLen;
	handle->card->maxWtBlkLen = maxWriteBlockLen;

	if (handle->card->size < 0xA00000) {
		/*
		 * 10MB Too small size mean, cmd9 response is wrong,
		 * Use default value 1G
		 */
		handle->card->size = 0x40000000;
		handle->card->maxRdBlkLen = 512;
		handle->card->maxWtBlkLen = 512;
	}

	if ((handle->card->maxRdBlkLen > 512) ||
	    (handle->card->maxWtBlkLen > 512)) {
		handle->card->maxRdBlkLen = 512;
		handle->card->maxWtBlkLen = 512;
	} else if ((handle->card->maxRdBlkLen == 0) ||
		   (handle->card->maxWtBlkLen == 0)) {
		handle->card->maxRdBlkLen = 512;
		handle->card->maxWtBlkLen = 512;
	}

	handle->device->cfg.blockSize = handle->card->maxRdBlkLen;

	return res;
}

int sd_cmd13(struct sd_handle *handle, uint32_t *status)
{
	int res;
	uint32_t argument, options;
	struct sd_resp resp;

	argument = handle->device->ctrl.rca << SD_CMD7_ARG_RCA_SHIFT;

	options = SD_CMDR_RSP_TYPE_R1_5_6 << SD_CMDR_RSP_TYPE_S |
		  SD4_EMMC_TOP_CMD_CCHK_EN_MASK |
		  SD4_EMMC_TOP_CMD_CRC_EN_MASK;

	/* send cmd and parse result */
	res = send_cmd(handle, SD_CMD_SEND_STATUS, argument, options, &resp);

	if (res == SD_OK) {
		*status = resp.cardStatus;
	}

	return res;
}

int sd_cmd16(struct sd_handle *handle, uint32_t length)
{
	int res;
	uint32_t argument, options, ntry;
	struct sd_resp resp;

	argument = length;

	options = SD_CMDR_RSP_TYPE_R1_5_6 << SD_CMDR_RSP_TYPE_S |
		  SD4_EMMC_TOP_CMD_CRC_EN_MASK |
		  SD4_EMMC_TOP_CMD_CCHK_EN_MASK;

	ntry = 0;
	do {
		res = sd_cmd13(handle, &resp.cardStatus);
		if (res != SD_OK) {
			EMMC_TRACE(
				"cmd13 failed before cmd16: rca 0x%0x, return %d, response 0x%0x\n",
				handle->device->ctrl.rca, res, resp.cardStatus);
			return res;
		}

		if (resp.cardStatus & 0x100)
			break;

		EMMC_TRACE("cmd13 rsp:0x%08x before cmd16\n", resp.cardStatus);

		if (ntry > handle->device->cfg.retryLimit) {
			EMMC_TRACE("cmd13 retry reach limit %d\n",
				   handle->device->cfg.retryLimit);
			return SD_CMD_TIMEOUT;
		}

		ntry++;
		EMMC_TRACE("cmd13 retry %d\n", ntry);

		SD_US_DELAY(1000);

	} while (1);

	/* send cmd and parse result */
	res = send_cmd(handle, SD_CMD_SET_BLOCKLEN, argument, options, &resp);

	return res;
}

int sd_cmd17(struct sd_handle *handle,
	     uint32_t addr, uint32_t len, uint8_t *buffer)
{
	int res;
	uint32_t argument, options, ntry;
	struct sd_resp resp;

	ntry = 0;
	do {
		res = sd_cmd13(handle, &resp.cardStatus);
		if (res != SD_OK) {
			EMMC_TRACE(
				"cmd 13 failed before cmd17: rca 0x%0x, return %d, response 0x%0x\n",
				handle->device->ctrl.rca, res, resp.cardStatus);
			return res;
		}

		if (resp.cardStatus & 0x100)
			break;

		EMMC_TRACE("cmd13 rsp:0x%08x before cmd17\n", resp.cardStatus);

		if (ntry > handle->device->cfg.retryLimit) {
			EMMC_TRACE("cmd13 retry reach limit %d\n",
				   handle->device->cfg.retryLimit);
			return SD_CMD_TIMEOUT;
		}

		ntry++;
		EMMC_TRACE("cmd13 retry %d\n", ntry);

		SD_US_DELAY(1000);

	} while (1);

	data_xfer_setup(handle, buffer, len, SD_XFER_CARD_TO_HOST);

	/* send cmd and parse result */
	argument = addr;
	options = SD_CMDR_RSP_TYPE_R1_5_6 << SD_CMDR_RSP_TYPE_S |
		  SD4_EMMC_TOP_CMD_DPS_MASK | SD4_EMMC_TOP_CMD_DTDS_MASK |
		  SD4_EMMC_TOP_CMD_CRC_EN_MASK | SD4_EMMC_TOP_CMD_CCHK_EN_MASK;

	res = send_cmd(handle, SD_CMD_READ_SINGLE_BLOCK, argument, options,
		       &resp);

	if (res != SD_OK)
		return res;

	res = process_data_xfer(handle, buffer, addr, len, SD_XFER_CARD_TO_HOST);

	return res;
}

int sd_cmd18(struct sd_handle *handle,
	     uint32_t addr, uint32_t len, uint8_t *buffer)
{
	int res;
	uint32_t argument, options, ntry;
	struct sd_resp resp;

	ntry = 0;
	do {
		res = sd_cmd13(handle, &resp.cardStatus);
		if (res != SD_OK) {
			EMMC_TRACE(
				"cmd 13 failed before cmd18: rca 0x%0x, return %d, response 0x%0x\n",
				handle->device->ctrl.rca, res, resp.cardStatus);
			return res;
		}

		if (resp.cardStatus & 0x100)
			break;

		EMMC_TRACE("cmd13 rsp:0x%08x before cmd18\n", resp.cardStatus);

		if (ntry > handle->device->cfg.retryLimit) {
			EMMC_TRACE("cmd13 retry reach limit %d\n",
				   handle->device->cfg.retryLimit);
			return SD_CMD_TIMEOUT;
		}

		ntry++;
		EMMC_TRACE("cmd13 retry %d\n", ntry);

		SD_US_DELAY(1000);
	} while (1);

	data_xfer_setup(handle, buffer, len, SD_XFER_CARD_TO_HOST);

	argument = addr;

	options = SD_CMDR_RSP_TYPE_R1_5_6 << SD_CMDR_RSP_TYPE_S |
		  SD4_EMMC_TOP_CMD_DPS_MASK | SD4_EMMC_TOP_CMD_DTDS_MASK |
		  SD4_EMMC_TOP_CMD_MSBS_MASK | SD4_EMMC_TOP_CMD_CCHK_EN_MASK |
		  SD4_EMMC_TOP_CMD_BCEN_MASK | SD4_EMMC_TOP_CMD_CRC_EN_MASK |
		  BIT(SD4_EMMC_TOP_CMD_ACMDEN_SHIFT);

	/* send cmd and parse result */
	res = send_cmd(handle, SD_CMD_READ_MULTIPLE_BLOCK, argument, options,
		       &resp);

	if (res != SD_OK)
		return res;

	res = process_data_xfer(handle, buffer, addr, len, SD_XFER_CARD_TO_HOST);

	return res;
}

#ifdef INCLUDE_EMMC_DRIVER_ERASE_CODE
static int card_sts_resp(struct sd_handle *handle, uint32_t *status)
{
	int res;
	uint32_t ntry = 0;

	do {
		res = sd_cmd13(handle, status);
		if (res != SD_OK) {
			EMMC_TRACE(
				"cmd 13 failed before cmd35: rca 0x%0x, return %d\n",
				handle->device->ctrl.rca, res);
			return res;
		}

		if (*status & 0x100)
			break;

		EMMC_TRACE("cmd13 rsp:0x%08x before cmd35\n", resp.cardStatus);

		if (ntry > handle->device->cfg.retryLimit) {
			EMMC_TRACE("cmd13 retry reach limit %d\n",
				   handle->device->cfg.retryLimit);
			return SD_CMD_TIMEOUT;
		}

		ntry++;
		EMMC_TRACE("cmd13 retry %d\n", ntry);

		SD_US_DELAY(1000);
	} while (1);

	return SD_OK;
}

int sd_cmd35(struct sd_handle *handle, uint32_t start)
{
	int res;
	uint32_t argument, options;
	struct sd_resp resp;

	res = card_sts_resp(handle, &resp.cardStatus);
	if (res != SD_OK)
		return res;

	argument = start;

	options = SD_CMDR_RSP_TYPE_R1_5_6 << SD_CMDR_RSP_TYPE_S |
		  SD4_EMMC_TOP_CMD_CRC_EN_MASK |
		  SD4_EMMC_TOP_CMD_CCHK_EN_MASK;

	/* send cmd and parse result */
	res = send_cmd(handle, SD_CMD_ERASE_GROUP_START,
		       argument, options, &resp);

	if (res != SD_OK)
		return res;

	return res;
}

int sd_cmd36(struct sd_handle *handle, uint32_t end)
{
	int res;
	uint32_t argument, options;
	struct sd_resp resp;

	res = card_sts_resp(handle, &resp.cardStatus);
	if (res != SD_OK)
		return res;

	argument = end;

	options = SD_CMDR_RSP_TYPE_R1_5_6 << SD_CMDR_RSP_TYPE_S |
		  SD4_EMMC_TOP_CMD_CRC_EN_MASK |
		  SD4_EMMC_TOP_CMD_CCHK_EN_MASK;

	/* send cmd and parse result */
	res = send_cmd(handle, SD_CMD_ERASE_GROUP_END,
		       argument, options, &resp);

	if (res != SD_OK)
		return res;

	return res;
}

int sd_cmd38(struct sd_handle *handle)
{
	int res;
	uint32_t argument, options;
	struct sd_resp resp;

	res = card_sts_resp(handle, &resp.cardStatus);
	if (res != SD_OK)
		return res;

	argument = 0;

	options = (SD_CMDR_RSP_TYPE_R1b_5b << SD_CMDR_RSP_TYPE_S) |
		  SD4_EMMC_TOP_CMD_CRC_EN_MASK |
		  SD4_EMMC_TOP_CMD_CCHK_EN_MASK;

	/* send cmd and parse result */
	res = send_cmd(handle, SD_CMD_ERASE, argument, options, &resp);

	if (res != SD_OK)
		return res;

	return res;
}
#endif

#ifdef INCLUDE_EMMC_DRIVER_WRITE_CODE

int sd_cmd24(struct sd_handle *handle,
	     uint32_t addr, uint32_t len, uint8_t *buffer)
{
	int res;
	uint32_t argument, options, ntry;
	struct sd_resp resp;

	ntry = 0;
	do {
		res = sd_cmd13(handle, &resp.cardStatus);
		if (res != SD_OK) {
			EMMC_TRACE(
				"cmd 13 failed before cmd24: rca 0x%0x, return %d, response 0x%0x\n",
				handle->device->ctrl.rca, res, &resp.cardStatus);
			return res;
		}

		if (resp.cardStatus & 0x100)
			break;

		EMMC_TRACE("cmd13 rsp:0x%08x before cmd24\n", resp.cardStatus);

		if (ntry > handle->device->cfg.retryLimit) {
			EMMC_TRACE("cmd13 retry reach limit %d\n",
				   handle->device->cfg.retryLimit);
			return SD_CMD_TIMEOUT;
		}

		ntry++;
		EMMC_TRACE("cmd13 retry %d\n", ntry);

		SD_US_DELAY(1000);

	} while (1);

	data_xfer_setup(handle, buffer, len, SD_XFER_HOST_TO_CARD);

	argument = addr;

	options = SD_CMDR_RSP_TYPE_R1_5_6 << SD_CMDR_RSP_TYPE_S |
		  SD4_EMMC_TOP_CMD_DPS_MASK | SD4_EMMC_TOP_CMD_CRC_EN_MASK |
		  SD4_EMMC_TOP_CMD_CCHK_EN_MASK;

	/* send cmd and parse result */
	res = send_cmd(handle, SD_CMD_WRITE_BLOCK, argument, options, &resp);

	if (res != SD_OK)
		return res;

	res = process_data_xfer(handle, buffer, addr, len, SD_XFER_HOST_TO_CARD);

	return res;
}

int sd_cmd25(struct sd_handle *handle,
	     uint32_t addr, uint32_t len, uint8_t *buffer)
{
	int res = SD_OK;
	uint32_t argument, options, ntry;
	struct sd_resp resp;

	ntry = 0;
	do {
		res = sd_cmd13(handle, &resp.cardStatus);
		if (res != SD_OK) {
			EMMC_TRACE(
				"cmd 13 failed before cmd25: rca 0x%0x, return %d, response 0x%0x\n",
				handle->device->ctrl.rca, res, &resp.cardStatus);
			return res;
		}

		if (resp.cardStatus & 0x100)
			break;

		EMMC_TRACE("cmd13 rsp:0x%08x before cmd25\n", resp.cardStatus);

		if (ntry > handle->device->cfg.retryLimit) {
			EMMC_TRACE("cmd13 retry reach limit %d\n",
				   handle->device->cfg.retryLimit);
			return SD_CMD_TIMEOUT;
		}

		ntry++;
		EMMC_TRACE("cmd13 retry %d\n", ntry);

		SD_US_DELAY(1000);
	} while (1);

	data_xfer_setup(handle, buffer, len, SD_XFER_HOST_TO_CARD);

	argument = addr;

	options = SD_CMDR_RSP_TYPE_R1_5_6 << SD_CMDR_RSP_TYPE_S |
		  SD4_EMMC_TOP_CMD_DPS_MASK | SD4_EMMC_TOP_CMD_MSBS_MASK |
		  SD4_EMMC_TOP_CMD_CCHK_EN_MASK | SD4_EMMC_TOP_CMD_BCEN_MASK |
		  SD4_EMMC_TOP_CMD_CRC_EN_MASK |
		  BIT(SD4_EMMC_TOP_CMD_ACMDEN_SHIFT);

	/* send cmd and parse result */
	res = send_cmd(handle, SD_CMD_WRITE_MULTIPLE_BLOCK,
		       argument, options, &resp);

	if (res != SD_OK)
		return res;

	res = process_data_xfer(handle, buffer, addr, len, SD_XFER_HOST_TO_CARD);

	return res;
}
#endif /* INCLUDE_EMMC_DRIVER_WRITE_CODE */

int mmc_cmd6(struct sd_handle *handle, uint32_t argument)
{
	int res;
	uint32_t options;
	struct sd_resp resp;

	options = SD_CMDR_RSP_TYPE_R1b_5b << SD_CMDR_RSP_TYPE_S |
		  SD4_EMMC_TOP_CMD_CCHK_EN_MASK | SD4_EMMC_TOP_CMD_CRC_EN_MASK;

	EMMC_TRACE("Sending CMD6 with argument 0x%X\n", argument);

	/* send cmd and parse result */
	res = send_cmd(handle, SD_ACMD_SET_BUS_WIDTH, argument, options, &resp);

	/*
	 * For R1b type response:
	 * controller issues a COMMAND COMPLETE interrupt when the R1
	 * response is received,
	 * then controller monitors DAT0 for busy status,
	 * controller issues a TRANSFER COMPLETE interrupt when busy signal
	 * clears.
	 */
	wait_for_event(handle,
		       SD4_EMMC_TOP_INTR_TXDONE_MASK | SD_ERR_INTERRUPTS,
		       handle->device->cfg.wfe_retry);

	if (res == SD_OK) {
		/* Check result of Cmd6 using Cmd13 to check card status */

		/* Check status using Cmd13 */
		res = sd_cmd13(handle, &resp.cardStatus);

		if (res == SD_OK) {
			/* Check bit 7 (SWITCH_ERROR) in card status */
			if ((resp.cardStatus & 0x80) != 0) {
				EMMC_TRACE("cmd6 failed: SWITCH_ERROR\n");
				res = SD_FAIL;
			}
		} else {
			EMMC_TRACE("cmd13 failed after cmd6: ");
			EMMC_TRACE("rca 0x%0x, return %d, response 0x%0x\n",
				handle->device->ctrl.rca, res, resp.cardStatus);
		}
	}

	return res;
}


#define SD_BUSY_CHECK		0x00203000
#define DAT0_LEVEL_MASK		0x100000	/* bit20 in PSTATE */
#define DEV_BUSY_TIMEOUT	600000		/* 60 Sec : 600000 * 100us */

int send_cmd(struct sd_handle *handle, uint32_t cmdIndex, uint32_t argument,
	     uint32_t options, struct sd_resp *resp)
{
	int status = SD_OK;
	uint32_t event = 0, present, timeout = 0, retry = 0, mask = 3;
	uint32_t temp_resp[4];

	if (handle == NULL) {
		EMMC_TRACE("Invalid handle for cmd%d\n", cmdIndex);
		return SD_INVALID_HANDLE;
	}

	mask = (SD_BUSY_CHECK & options) ? 3 : 1;

RETRY_WRITE_CMD:
	do {
		/* Make sure it is ok to send command */
		present =
		    chal_sd_get_present_status((CHAL_HANDLE *) handle->device);
		timeout++;

		if (present & mask)
			SD_US_DELAY(1000);
		else
			break;

	} while (timeout < EMMC_BUSY_CMD_TIMEOUT_MS);

	if (timeout >= EMMC_BUSY_CMD_TIMEOUT_MS) {
		status = SD_CMD_MISSING;
		EMMC_TRACE("cmd%d timedout %dms\n", cmdIndex, timeout);
	}

	/* Reset both DAT and CMD line if only of them are stuck */
	if (present & mask)
		check_error(handle, SD4_EMMC_TOP_INTR_CMDERROR_MASK);

	handle->device->ctrl.argReg = argument;
	chal_sd_send_cmd((CHAL_HANDLE *) handle->device, cmdIndex,
			 handle->device->ctrl.argReg, options);

	handle->device->ctrl.cmdIndex = cmdIndex;

	event = wait_for_event(handle,
			       (SD4_EMMC_TOP_INTR_CMDDONE_MASK |
				SD_ERR_INTERRUPTS),
			       handle->device->cfg.wfe_retry);

	if (handle->device->ctrl.cmdStatus == SD_CMD_MISSING) {
		retry++;

		if (retry >= handle->device->cfg.retryLimit) {
			status = SD_CMD_MISSING;
			EMMC_TRACE("cmd%d retry reaches the limit %d\n",
				   cmdIndex, retry);
		} else {
			/* reset both DAT & CMD line if one of them is stuck */
			present = chal_sd_get_present_status((CHAL_HANDLE *)
							     handle->device);

			if (present & mask)
				check_error(handle,
					    SD4_EMMC_TOP_INTR_CMDERROR_MASK);

			EMMC_TRACE("cmd%d retry %d PSTATE[0x%08x]\n",
				   cmdIndex, retry,
				   chal_sd_get_present_status((CHAL_HANDLE *)
							      handle->device));
			goto RETRY_WRITE_CMD;
		}
	}

	if (handle->device->ctrl.cmdStatus == SD_OK) {
		if (resp != NULL) {
			status =
			    chal_sd_get_response((CHAL_HANDLE *) handle->device,
						 temp_resp);
			process_cmd_response(handle,
					     handle->device->ctrl.cmdIndex,
					     temp_resp[0], temp_resp[1],
					     temp_resp[2], temp_resp[3], resp);
		}

		/* Check Device busy after CMD */
		if ((cmdIndex == 5) || (cmdIndex == 6) || (cmdIndex == 7) ||
		    (cmdIndex == 28) || (cmdIndex == 29) || (cmdIndex == 38)) {

			timeout = 0;
			do {
				present =
				    chal_sd_get_present_status((CHAL_HANDLE *)
							       handle->device);

				timeout++;

				/* Dat[0]:bit20 low means device busy */
				if ((present & DAT0_LEVEL_MASK) == 0) {
					EMMC_TRACE("Device busy: ");
					EMMC_TRACE(
					  "cmd%d arg:0x%08x: PSTATE[0x%08x]\n",
					  cmdIndex, argument, present);
					SD_US_DELAY(100);
				} else {
					break;
				}
			} while (timeout < DEV_BUSY_TIMEOUT);
		}
	} else if (handle->device->ctrl.cmdStatus &&
		   handle->device->ctrl.cmdStatus != SD_CMD_MISSING) {
		retry++;
		status = check_error(handle, handle->device->ctrl.cmdStatus);

		EMMC_TRACE(
			"cmd%d error: cmdStatus:0x%08x error_status:0x%08x\n",
			cmdIndex, handle->device->ctrl.cmdStatus, status);

		if ((handle->device->ctrl.cmdIndex == 1) ||
		    (handle->device->ctrl.cmdIndex == 5)) {
			status = event;
		} else if ((handle->device->ctrl.cmdIndex == 7) ||
			   (handle->device->ctrl.cmdIndex == 41)) {
			status = event;
		} else if ((status == SD_ERROR_RECOVERABLE) &&
			   (retry < handle->device->cfg.retryLimit)) {
			EMMC_TRACE("cmd%d recoverable error ", cmdIndex);
			EMMC_TRACE("retry %d PSTATE[0x%08x].\n", retry,
				   chal_sd_get_present_status((CHAL_HANDLE *)
							      handle->device));
			goto RETRY_WRITE_CMD;
		} else {
			EMMC_TRACE("cmd%d retry reaches the limit %d\n",
				   cmdIndex, retry);
			status = event;
		}
	}

	handle->device->ctrl.blkReg = 0;
	/* clear error status for next command */
	handle->device->ctrl.cmdStatus = 0;

	return status;
}