fspi.c 24.3 KB
Newer Older
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
843
844
845
846
847
848
849
850
851
852
853
// SPDX-License-Identifier: BSD-3-Clause
/*
 * NXP FlexSpi Controller Driver.
 * Copyright 2021 NXP
 *
 */
#include <endian.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include <common/debug.h>
#include <flash_info.h>
#include "fspi.h"
#include <fspi_api.h>
#include <xspi_error_codes.h>

#ifdef DEBUG_FLEXSPI
#define PR printf("In [%s][%d]\n", __func__, __LINE__)
#define PRA(a, b) printf("In [%s][%d] %s="a"\n", __func__, __LINE__, #b, b)
#else
#define PR
#define PRA(a, b)
#endif

/*
 * This errata is valid for all NXP SoC.
 */
#define ERRATA_FLASH_A050272 1

static uintptr_t fspi_base_reg_addr;
static uintptr_t fspi_flash_base_addr;

static void fspi_RDSR(uint32_t *, const void *, uint32_t);

static void fspi_writel(uint32_t x_addr, uint32_t x_val)
{
	fspi_out32((uint32_t *)(fspi_base_reg_addr + x_addr),
		 (uint32_t) x_val);
}

static uint32_t fspi_readl(uint32_t x_addr)
{
	return fspi_in32((uint32_t *)(fspi_base_reg_addr + x_addr));
}

static void fspi_MDIS(uint8_t x_disable)
{
	uint32_t ui_reg;

	ui_reg = fspi_readl(FSPI_MCR0);
	if (x_disable != 0U) {
		ui_reg |= FSPI_MCR0_MDIS;
	} else {
		ui_reg &= (uint32_t) (~FSPI_MCR0_MDIS);
	}

	fspi_writel(FSPI_MCR0, ui_reg);
}

static void fspi_lock_LUT(void)
{
	fspi_writel(FSPI_LUTKEY, FSPI_LUTKEY_VALUE);
	VERBOSE("%s 0x%x\n", __func__, fspi_readl(FSPI_LCKCR));
	fspi_writel(FSPI_LCKCR, FSPI_LCKER_LOCK);
	VERBOSE("%s 0x%x\n", __func__, fspi_readl(FSPI_LCKCR));
}

static void fspi_unlock_LUT(void)
{
	fspi_writel(FSPI_LUTKEY,  FSPI_LUTKEY_VALUE);
	VERBOSE("%s 0x%x\n", __func__, fspi_readl(FSPI_LCKCR));
	fspi_writel(FSPI_LCKCR, FSPI_LCKER_UNLOCK);
	VERBOSE("%s 0x%x\n", __func__, fspi_readl(FSPI_LCKCR));
}

static void fspi_op_setup(uint32_t fspi_op_seq_id, bool ignore_flash_sz)
{
	uint32_t x_addr, x_instr0 = 0, x_instr1 = 0, x_instr2 = 0;
	uint32_t cmd_id1, cmd_id2;

	VERBOSE("In func %s\n", __func__);

	switch (fspi_op_seq_id) {
	case FSPI_READ_SEQ_ID:
		cmd_id1 = FSPI_NOR_CMD_READ;
		cmd_id2 = FSPI_NOR_CMD_READ_4B;
		x_instr2 = FSPI_INSTR_OPRND0(0) | FSPI_INSTR_PAD0(FSPI_LUT_PAD1)
				| FSPI_INSTR_OPCODE0(FSPI_LUT_READ);
		break;
	case FSPI_FASTREAD_SEQ_ID:
		cmd_id1 = FSPI_NOR_CMD_FASTREAD;
		cmd_id2 = FSPI_NOR_CMD_FASTREAD_4B;
		x_instr2 = FSPI_INSTR_OPRND0(8) | FSPI_INSTR_PAD0(FSPI_LUT_PAD1)
				| FSPI_INSTR_OPCODE0(FSPI_DUMMY_SDR)
				| FSPI_INSTR_OPRND1(0)
				| FSPI_INSTR_PAD1(FSPI_LUT_PAD1)
				| FSPI_INSTR_OPCODE1(FSPI_LUT_READ);
		break;
	case FSPI_WRITE_SEQ_ID:
		cmd_id1 = FSPI_NOR_CMD_PP;
		cmd_id2 = FSPI_NOR_CMD_PP_4B;
		x_instr2 = FSPI_INSTR_OPRND0(0) | FSPI_INSTR_PAD0(FSPI_LUT_PAD1)
				| FSPI_INSTR_OPCODE0(FSPI_LUT_WRITE);
		break;
	case FSPI_WREN_SEQ_ID:
		cmd_id1 = FSPI_NOR_CMD_WREN;
		cmd_id2 = FSPI_NOR_CMD_WREN;
		break;
	case FSPI_SE_SEQ_ID:
		cmd_id1 = FSPI_NOR_CMD_SE_64K;
		cmd_id2 = FSPI_NOR_CMD_SE_64K_4B;
		break;
	case FSPI_4K_SEQ_ID:
		cmd_id1 = FSPI_NOR_CMD_SE_4K;
		cmd_id2 = FSPI_NOR_CMD_SE_4K_4B;
		break;
	case FSPI_BE_SEQ_ID:
		cmd_id1 = FSPI_NOR_CMD_BE;
		cmd_id2 = FSPI_NOR_CMD_BE;
		break;
	case FSPI_RDSR_SEQ_ID:
		cmd_id1 = FSPI_NOR_CMD_RDSR;
		cmd_id2 = FSPI_NOR_CMD_RDSR;
		break;
	}

	x_addr = FSPI_LUTREG_OFFSET + (uint32_t)(0x10 * fspi_op_seq_id);
	if ((F_FLASH_SIZE_BYTES <= SZ_16M_BYTES) || (ignore_flash_sz)) {
		x_instr0 = FSPI_INSTR_OPRND0(cmd_id1);
		x_instr1 = FSPI_INSTR_OPRND1(FSPI_LUT_ADDR24BIT);
		VERBOSE("CMD_ID = %x offset = 0x%x\n", cmd_id1, x_addr);
	} else {
		x_instr0 = FSPI_INSTR_OPRND0(cmd_id2);
		x_instr1 = FSPI_INSTR_OPRND1(FSPI_LUT_ADDR32BIT);
		VERBOSE("CMD_ID = %x offset = 0x%x\n", cmd_id2, x_addr);
	}
	x_instr0 |= FSPI_INSTR_PAD0(FSPI_LUT_PAD1)
		| FSPI_INSTR_OPCODE0(FSPI_LUT_CMD);

	x_instr1 |= FSPI_INSTR_PAD1(FSPI_LUT_PAD1)
		| FSPI_INSTR_OPCODE1(FSPI_LUT_ADDR);

	if (fspi_op_seq_id == FSPI_RDSR_SEQ_ID) {
		x_instr0 |= FSPI_INSTR_OPRND1(1) | FSPI_INSTR_PAD1(FSPI_LUT_PAD1)
					| FSPI_INSTR_OPCODE1(FSPI_LUT_READ);
	} else if ((fspi_op_seq_id != FSPI_BE_SEQ_ID)
			&& (fspi_op_seq_id != FSPI_WREN_SEQ_ID)) {
		x_instr0 |= x_instr1;
	}

	fspi_writel((x_addr), x_instr0);
	fspi_writel((x_addr + U(0x4)), x_instr2);
	fspi_writel((x_addr + U(0x8)), (uint32_t) 0x0);	/* STOP command */
	fspi_writel((x_addr + U(0xc)), (uint32_t) 0x0);	/* STOP command */
}

static void fspi_setup_LUT(void)
{
	VERBOSE("In func %s\n", __func__);
	fspi_unlock_LUT();

	/* LUT Setup for READ Command 3-Byte low Frequency */
	fspi_op_setup(FSPI_READ_SEQ_ID, false);

	/* LUT Setup for FAST READ Command 3-Byte/4-Byte high Frequency */
	fspi_op_setup(FSPI_FASTREAD_SEQ_ID, false);

	/* LUT Setup for Page Program */
	fspi_op_setup(FSPI_WRITE_SEQ_ID, false);

	/* LUT Setup for WREN */
	fspi_op_setup(FSPI_WREN_SEQ_ID, true);

	/* LUT Setup for Sector_Erase */
	fspi_op_setup(FSPI_SE_SEQ_ID, false);

	/* LUT Setup for Sub Sector 4K Erase */
	fspi_op_setup(FSPI_4K_SEQ_ID, false);

	/* LUT Setup for Bulk_Erase */
	fspi_op_setup(FSPI_BE_SEQ_ID, true);

	/* Read Status */
	fspi_op_setup(FSPI_RDSR_SEQ_ID, true);

	fspi_lock_LUT();
}

static inline void fspi_ahb_invalidate(void)
{
	uint32_t reg;

	VERBOSE("In func %s %d\n", __func__, __LINE__);
	reg = fspi_readl(FSPI_MCR0);
	reg |= FSPI_MCR0_SWRST;
	fspi_writel(FSPI_MCR0, reg);
	while ((fspi_readl(FSPI_MCR0) & FSPI_MCR0_SWRST) != 0)
		;  /* FSPI_MCR0_SWRESET_MASK */
	VERBOSE("In func %s %d\n", __func__, __LINE__);
}

#if defined(CONFIG_FSPI_AHB)
static void fspi_init_ahb(void)
{
	uint32_t i, x_flash_cr2, seq_id;

	x_flash_cr2 = 0;
	/* Reset AHB RX buffer CR configuration */
	for (i = 0; i < 8; i++) {
		fspi_writel((FSPI_AHBRX_BUF0CR0 + 4 * i), 0U);
	}

	/* Set ADATSZ with the maximum AHB buffer size */
	fspi_writel(FSPI_AHBRX_BUF7CR0,
			((uint32_t) ((FSPI_RX_MAX_AHBBUF_SIZE / 8U) |
				    FSPI_AHBRXBUF0CR7_PREF)));

	/* Known limitation handling: prefetch and
	 * no start address alignment.*/
	fspi_writel(FSPI_AHBCR, FSPI_AHBCR_PREF_EN);
	INFO("xAhbcr=0x%x\n", fspi_readl(FSPI_AHBCR));

	// Setup AHB READ sequenceID for all flashes.
	x_flash_cr2 = fspi_readl(FSPI_FLSHA1CR2);
	INFO("x_flash_cr2=0x%x\n", x_flash_cr2);

	seq_id = CONFIG_FSPI_FASTREAD ?
			FSPI_FASTREAD_SEQ_ID : FSPI_READ_SEQ_ID;
	x_flash_cr2 |= ((seq_id << FSPI_FLSHXCR2_ARDSEQI_SHIFT) & 0x1f);

	INFO("x_flash_cr2=0x%x\n", x_flash_cr2);

	fspi_writel(FSPI_FLSHA1CR2,  x_flash_cr2);
	x_flash_cr2 = fspi_readl(FSPI_FLSHA1CR2);
	INFO("x_flash_cr2=0x%x\n", x_flash_cr2);
}
#endif

int xspi_read(uint32_t pc_rx_addr, uint32_t *pc_rx_buf, uint32_t x_size_bytes)
{
	if (x_size_bytes == 0) {
		ERROR("Zero length reads are not allowed\n");
		return XSPI_READ_FAIL;
	}

#if defined(CONFIG_FSPI_AHB)
	return xspi_ahb_read(pc_rx_addr, pc_rx_buf, x_size_bytes);
#else
	return xspi_ip_read(pc_rx_addr, pc_rx_buf, x_size_bytes);
#endif
}
#if defined(CONFIG_FSPI_AHB)
int xspi_ahb_read(uint32_t pc_rx_addr, uint32_t *pc_rx_buf, uint32_t x_size_bytes)
{
	VERBOSE("In func %s 0x%x\n", __func__, (pc_rx_addr));

	if (F_FLASH_SIZE_BYTES <= SZ_16M_BYTES) {
		pc_rx_addr = ((uint32_t)(pcRxAddr & MASK_24BIT_ADDRESS));
	} else {
		pc_rx_addr = ((uint32_t)(pcRxAddr & MASK_32BIT_ADDRESS));
	}

	pc_rx_addr = ((uint32_t)(pcRxAddr + fspi_flash_base_addr));

	if (((pc_rx_addr % 4) != 0) || (((uintptr_t)pc_rx_buf % 4) != 0)) {
		WARN("%s: unaligned Start Address src=%ld dst=0x%p\n",
		     __func__, (pc_rx_addr - fspi_flash_base_addr), pc_rx_buf);
	}

	/* Directly copy from AHB Buffer */
	memcpy(pc_rx_buf, (void *)(uintptr_t)pc_rx_addr, x_size_bytes);

	fspi_ahb_invalidate();
	return XSPI_SUCCESS;
}
#endif

int xspi_ip_read(uint32_t pc_rx_addr, uint32_t *pv_rx_buf, uint32_t ui_len)
{

	uint32_t i = 0U, j = 0U, x_rem = 0U;
	uint32_t x_iteration = 0U, x_size_rx = 0U, x_size_wm, temp_size;
	uint32_t data = 0U;
	uint32_t x_len_bytes;
	uint32_t x_addr, sts0, intr, seq_id;

	x_addr = (uint32_t) pc_rx_addr;
	x_len_bytes = ui_len;

	/* Watermark level : 8 bytes. (BY DEFAULT) */
	x_size_wm = 8U;

	/* Clear  RX Watermark interrupt in INT register, if any existing.  */
	fspi_writel(FSPI_INTR, FSPI_INTR_IPRXWA);
	PRA("0x%x", fspi_readl(FSPI_INTR));
	/* Invalid the RXFIFO, to run next IP Command */
	/* Clears data entries in IP Rx FIFOs, Also reset R/W pointers */
	fspi_writel(FSPI_IPRXFCR, FSPI_IPRXFCR_CLR);
	fspi_writel(FSPI_INTR, FSPI_INTEN_IPCMDDONE);

	while (x_len_bytes) {

		/* FlexSPI can store no more than  FSPI_RX_IPBUF_SIZE */
		x_size_rx = (x_len_bytes >  FSPI_RX_IPBUF_SIZE) ?
			   FSPI_RX_IPBUF_SIZE : x_len_bytes;

		/* IP Control Register0 - SF Address to be read */
		fspi_writel(FSPI_IPCR0, x_addr);
		PRA("0x%x", fspi_readl(FSPI_IPCR0));
		/* IP Control Register1 - SEQID_READ operation, Size */

		seq_id = CONFIG_FSPI_FASTREAD ?
				FSPI_FASTREAD_SEQ_ID : FSPI_READ_SEQ_ID;

		fspi_writel(FSPI_IPCR1,
			    (uint32_t)(seq_id << FSPI_IPCR1_ISEQID_SHIFT) |
			    (uint16_t) x_size_rx);

		PRA("0x%x", fspi_readl(FSPI_IPCR1));

		do {
			sts0 = fspi_readl(FSPI_STS0);
		} while (((sts0 & FSPI_STS0_ARB_IDLE) == 0) &&
			 ((sts0 & FSPI_STS0_SEQ_IDLE) == 0));

		/* Trigger IP Read Command */
		fspi_writel(FSPI_IPCMD, FSPI_IPCMD_TRG_MASK);
		PRA("0x%x", fspi_readl(FSPI_IPCMD));

		intr = fspi_readl(FSPI_INTR);
		if (((intr & FSPI_INTR_IPCMDGE) != 0) ||
		    ((intr & FSPI_INTR_IPCMDERR) != 0)) {
			ERROR("Error in IP READ INTR=0x%x\n", intr);
			return -XSPI_IP_READ_FAIL;
		}
		/* Will read in n iterations of each 8 FIFO's(WM level) */
		x_iteration = x_size_rx / x_size_wm;
		for (i = 0U; i < x_iteration; i++) {
			if ((fspi_readl(FSPI_INTR) & FSPI_INTR_IPRXWA_MASK) == 0) {
				PRA("0x%x", fspi_readl(FSPI_INTR));
			}
			/* Wait for IP Rx Watermark Fill event */
			while (!(fspi_readl(FSPI_INTR) & FSPI_INTR_IPRXWA_MASK)) {
				PRA("0x%x", fspi_readl(FSPI_INTR));
			}

			/* Read RX FIFO's(upto WM level) & copy to rxbuffer */
			for (j = 0U; j < x_size_wm; j += 4U) {
				/* Read FIFO Data Register */
				data = fspi_readl(FSPI_RFDR + j);
#if FSPI_IPDATA_SWAP /* Just In case you want swap */
				data = bswap32(data);
#endif
				memcpy(pv_rx_buf++, &data, 4);
			}

			/* Clear IP_RX_WATERMARK Event in INTR register */
			/* Reset FIFO Read pointer for next iteration.*/
			fspi_writel(FSPI_INTR, FSPI_INTR_IPRXWA);
		}

		x_rem = x_size_rx % x_size_wm;

		if (x_rem != 0U) {
			/* Wait for data filled */
			while (!(fspi_readl(FSPI_IPRXFSTS) & FSPI_IPRXFSTS_FILL_MASK)) {
				PRA("0x%x", fspi_readl(FSPI_IPRXFSTS));
			}

			temp_size = 0;
			j = 0U;
			while (x_rem > 0U) {
				data = 0U;
				data =  fspi_readl(FSPI_RFDR + j);
#if FSPI_IPDATA_SWAP /* Just In case you want swap */
				data = bswap32(data);
#endif
				temp_size = (x_rem < 4) ? x_rem : 4;
				memcpy(pv_rx_buf++, &data, temp_size);
				x_rem -= temp_size;
			}
		}


		while (!(fspi_readl(FSPI_INTR) & FSPI_INTR_IPCMDDONE_MASK)) {
			PRA("0x%x", fspi_readl(FSPI_INTR));
		}

		/* Invalid the RX FIFO, to run next IP Command */
		fspi_writel(FSPI_IPRXFCR, FSPI_IPRXFCR_CLR);
		/* Clear IP Command Done flag in interrupt register*/
		fspi_writel(FSPI_INTR, FSPI_INTR_IPCMDDONE_MASK);

		/* Update remaining len, Increment x_addr read pointer. */
		x_len_bytes -= x_size_rx;
		x_addr += x_size_rx;
	}
	PR;
	return XSPI_SUCCESS;
}

void xspi_ip_write(uint32_t pc_wr_addr, uint32_t *pv_wr_buf, uint32_t ui_len)
{

	uint32_t x_iteration = 0U, x_rem = 0U;
	uint32_t x_size_tx = 0U, x_size_wm, temp_size;
	uint32_t i = 0U, j = 0U;
	uint32_t ui_data = 0U;
	uint32_t x_addr, x_len_bytes;


	x_size_wm = 8U;	/* Default TX WaterMark level: 8 Bytes. */
	x_addr = (uint32_t)pc_wr_addr;
	x_len_bytes = ui_len;
	VERBOSE("In func %s[%d] x_addr =0x%x xLen_bytes=%d\n",
			__func__, __LINE__, x_addr, x_len_bytes);

	while (x_len_bytes != 0U) {

		x_size_tx = (x_len_bytes >  FSPI_TX_IPBUF_SIZE) ?
				FSPI_TX_IPBUF_SIZE : x_len_bytes;

		/* IP Control Register0 - SF Address to be read */
		fspi_writel(FSPI_IPCR0, x_addr);
		INFO("In func %s[%d] x_addr =0x%x xLen_bytes=%d\n",
				__func__, __LINE__, x_addr, x_len_bytes);

		/*
		 * Fill TX FIFO's..
		 *
		 */

		x_iteration = x_size_tx / x_size_wm;
		for (i = 0U; i < x_iteration; i++) {

			/* Ensure TX FIFO Watermark Available */
			while ((fspi_readl(FSPI_INTR) & FSPI_INTR_IPTXWE_MASK) == 0)
				;


			/* Fill TxFIFO's ( upto watermark level) */
			for (j = 0U; j < x_size_wm; j += 4U) {
				memcpy(&ui_data, pv_wr_buf++,  4);
				/* Write TX FIFO Data Register */
				fspi_writel((FSPI_TFDR + j), ui_data);

			}

			/* Clear IP_TX_WATERMARK Event in INTR register */
			/* Reset the FIFO Write pointer for next iteration */
			fspi_writel(FSPI_INTR, FSPI_INTR_IPTXWE);
		}

		x_rem = x_size_tx % x_size_wm;

		if (x_rem != 0U) {
			/* Wait for TXFIFO empty */
			while (!(fspi_readl(FSPI_INTR) & FSPI_INTR_IPTXWE))
				;

			temp_size = 0U;
			j = 0U;
			while (x_rem > 0U) {
				ui_data = 0U;
				temp_size = (x_rem < 4U) ? x_rem : 4U;
				memcpy(&ui_data, pv_wr_buf++, temp_size);
				INFO("%d ---> pv_wr_buf=0x%p\n", __LINE__, pv_wr_buf);
				fspi_writel((FSPI_TFDR + j), ui_data);
				x_rem -= temp_size;
				j += 4U ; /* TODO: May not be needed*/
			}
			/* Clear IP_TX_WATERMARK Event in INTR register */
			/* Reset FIFO's Write pointer for next iteration.*/
			fspi_writel(FSPI_INTR, FSPI_INTR_IPTXWE);
		}

		/* IP Control Register1 - SEQID_WRITE operation, Size */
		fspi_writel(FSPI_IPCR1, (uint32_t)(FSPI_WRITE_SEQ_ID << FSPI_IPCR1_ISEQID_SHIFT) | (uint16_t) x_size_tx);
		/* Trigger IP Write Command */
		fspi_writel(FSPI_IPCMD, FSPI_IPCMD_TRG_MASK);

		/* Wait for IP Write command done */
		while (!(fspi_readl(FSPI_INTR) & FSPI_INTR_IPCMDDONE_MASK))
			;

		/* Invalidate TX FIFOs & acknowledge IP_CMD_DONE event */
		fspi_writel(FSPI_IPTXFCR, FSPI_IPTXFCR_CLR);
		fspi_writel(FSPI_INTR, FSPI_INTR_IPCMDDONE_MASK);

		/* for next iteration */
		x_len_bytes  -=  x_size_tx;
		x_addr += x_size_tx;
	}

}

int xspi_write(uint32_t pc_wr_addr, void *pv_wr_buf, uint32_t ui_len)
{

	uint32_t x_addr;
	uint32_t x_page1_len = 0U, x_page_l_len = 0U;
	uint32_t i, j = 0U;
	void *buf = pv_wr_buf;

	VERBOSE("\nIn func %s\n", __func__);

	x_addr = (uint32_t)(pc_wr_addr);
	if ((ui_len <= F_PAGE_256) && ((x_addr % F_PAGE_256) == 0)) {
		x_page1_len = ui_len;
		INFO("%d ---> x_page1_len=0x%x x_page_l_len =0x%x j=0x%x\n", __LINE__, x_page1_len, x_page_l_len, j);
	} else if ((ui_len <= F_PAGE_256) && ((x_addr % F_PAGE_256) != 0)) {
		x_page1_len = (F_PAGE_256 - (x_addr % F_PAGE_256));
		if (ui_len > x_page1_len) {
			x_page_l_len = (ui_len - x_page1_len) % F_PAGE_256;
		} else {
			x_page1_len = ui_len;
			x_page_l_len = 0;
		}
		j = 0U;
		INFO("%d 0x%x 0x%x\n", x_addr % F_PAGE_256, x_addr % F_PAGE_256, F_PAGE_256);
		INFO("%d ---> x_page1_len=0x%x x_page_l_len =0x%x j=0x%x\n", __LINE__, x_page1_len, x_page_l_len, j);
	} else if ((ui_len > F_PAGE_256) && ((x_addr % F_PAGE_256) == 0)) {
		j = ui_len / F_PAGE_256;
		x_page_l_len = ui_len % F_PAGE_256;
		INFO("%d ---> x_page1_len=0x%x x_page_l_len =0x%x j=0x%x\n", __LINE__, x_page1_len, x_page_l_len, j);
	} else if ((ui_len > F_PAGE_256) && ((x_addr % F_PAGE_256) != 0)) {
		x_page1_len = (F_PAGE_256 - (x_addr % F_PAGE_256));
		j = (ui_len - x_page1_len) / F_PAGE_256;
		x_page_l_len = (ui_len - x_page1_len) % F_PAGE_256;
		INFO("%d ---> x_page1_len=0x%x x_page_l_len =0x%x j=0x%x\n", __LINE__, x_page1_len, x_page_l_len, j);
	}

	if (x_page1_len != 0U) {
		xspi_wren(x_addr);
		xspi_ip_write(x_addr, (uint32_t *)buf, x_page1_len);
		while (is_flash_busy())
			;
		INFO("%d Initial pc_wr_addr=0x%x, Final x_addr=0x%x, Initial ui_len=0x%x Final ui_len=0x%x\n",
		     __LINE__, pc_wr_addr, x_addr, ui_len, (x_addr-pc_wr_addr));
		INFO("Initial Buf pv_wr_buf=%p, final Buf=%p\n", pv_wr_buf, buf);
		x_addr += x_page1_len;
		/* TODO What is buf start is not 4 aligned */
		buf = buf + x_page1_len;
	}

	for (i = 0U; i < j; i++) {
		INFO("In for loop Buf pv_wr_buf=%p, final Buf=%p x_addr=0x%x offset_buf %d.\n",
				pv_wr_buf, buf, x_addr, x_page1_len/4);
		xspi_wren(x_addr);
		xspi_ip_write(x_addr, (uint32_t *)buf, F_PAGE_256);
		while (is_flash_busy())
			;
		INFO("%d Initial pc_wr_addr=0x%x, Final x_addr=0x%x, Initial ui_len=0x%x Final ui_len=0x%x\n",
		     __LINE__, pc_wr_addr, x_addr, ui_len, (x_addr-pc_wr_addr));
		x_addr += F_PAGE_256;
		/* TODO What is buf start is not 4 aligned */
		buf = buf + F_PAGE_256;
		INFO("Initial Buf pv_wr_buf=%p, final Buf=%p\n", pv_wr_buf, buf);
	}

	if (x_page_l_len != 0U) {
		INFO("%d Initial Buf pv_wr_buf=%p, final Buf=%p x_page_l_len=0x%x\n", __LINE__, pv_wr_buf, buf, x_page_l_len);
		xspi_wren(x_addr);
		xspi_ip_write(x_addr, (uint32_t *)buf, x_page_l_len);
		while (is_flash_busy())
			;
		INFO("%d Initial pc_wr_addr=0x%x, Final x_addr=0x%x, Initial ui_len=0x%x Final ui_len=0x%x\n",
				__LINE__, pc_wr_addr, x_addr, ui_len, (x_addr-pc_wr_addr));
	}

	VERBOSE("Now calling func call Invalidate%s\n", __func__);
	fspi_ahb_invalidate();
	return XSPI_SUCCESS;
}

int xspi_wren(uint32_t pc_wr_addr)
{
	VERBOSE("In func %s Addr=0x%x\n", __func__, pc_wr_addr);

	fspi_writel(FSPI_IPTXFCR, FSPI_IPTXFCR_CLR);

	fspi_writel(FSPI_IPCR0, (uint32_t)pc_wr_addr);
	fspi_writel(FSPI_IPCR1, ((FSPI_WREN_SEQ_ID << FSPI_IPCR1_ISEQID_SHIFT) |  0));
	fspi_writel(FSPI_IPCMD, FSPI_IPCMD_TRG_MASK);

	while ((fspi_readl(FSPI_INTR) & FSPI_INTR_IPCMDDONE_MASK) == 0)
		;

	fspi_writel(FSPI_INTR, FSPI_INTR_IPCMDDONE_MASK);
	return XSPI_SUCCESS;
}

static void fspi_bbluk_er(void)
{
	VERBOSE("In func %s\n", __func__);
	fspi_writel(FSPI_IPCR0, 0x0);
	fspi_writel(FSPI_IPCR1, ((FSPI_BE_SEQ_ID << FSPI_IPCR1_ISEQID_SHIFT) | 20));
	fspi_writel(FSPI_IPCMD, FSPI_IPCMD_TRG_MASK);

	while ((fspi_readl(FSPI_INTR) & FSPI_INTR_IPCMDDONE_MASK) == 0)
		;
	fspi_writel(FSPI_INTR, FSPI_INTR_IPCMDDONE_MASK);

}

static void fspi_RDSR(uint32_t *rxbuf, const void *p_addr, uint32_t size)
{
	uint32_t iprxfcr = 0U;
	uint32_t data = 0U;

	iprxfcr = fspi_readl(FSPI_IPRXFCR);
	/* IP RX FIFO would be read by processor */
	iprxfcr = iprxfcr & (uint32_t)~FSPI_IPRXFCR_CLR;
	/* Invalid data entries in IP RX FIFO */
	iprxfcr = iprxfcr | FSPI_IPRXFCR_CLR;
	fspi_writel(FSPI_IPRXFCR, iprxfcr);

	fspi_writel(FSPI_IPCR0, (uintptr_t) p_addr);
	fspi_writel(FSPI_IPCR1,
		    (uint32_t) ((FSPI_RDSR_SEQ_ID << FSPI_IPCR1_ISEQID_SHIFT)
		    | (uint16_t) size));
	/* Trigger the command */
	fspi_writel(FSPI_IPCMD, FSPI_IPCMD_TRG_MASK);
	/* Wait for command done */
	while ((fspi_readl(FSPI_INTR) & FSPI_INTR_IPCMDDONE_MASK) == 0)
		;
	fspi_writel(FSPI_INTR, FSPI_INTR_IPCMDDONE_MASK);

	data = fspi_readl(FSPI_RFDR);
	memcpy(rxbuf, &data, size);

	/* Rx FIFO invalidation needs to be done prior w1c of INTR.IPRXWA bit */
	fspi_writel(FSPI_IPRXFCR, FSPI_IPRXFCR_CLR);
	fspi_writel(FSPI_INTR, FSPI_INTR_IPRXWA_MASK);
	fspi_writel(FSPI_INTR, FSPI_INTR_IPCMDDONE_MASK);

}

bool is_flash_busy(void)
{
#define FSPI_ONE_BYTE 1
	uint8_t data[4];

	VERBOSE("In func %s\n\n", __func__);
	fspi_RDSR((uint32_t *) data, 0, FSPI_ONE_BYTE);

	return !!((uint32_t) data[0] & FSPI_NOR_SR_WIP_MASK);
}

int xspi_bulk_erase(void)
{
	VERBOSE("In func %s\n", __func__);
	xspi_wren((uint32_t) 0x0);
	fspi_bbluk_er();
	while (is_flash_busy())
		;
	fspi_ahb_invalidate();
	return XSPI_SUCCESS;
}

static void fspi_sec_er(uint32_t pc_wr_addr)
{
	uint32_t x_addr;

	VERBOSE("In func %s\n", __func__);
	x_addr = (uint32_t)(pc_wr_addr);

	fspi_writel(FSPI_IPCR0, x_addr);
	INFO("In [%s][%d] Erase address 0x%x\n", __func__, __LINE__, (x_addr));
#if CONFIG_FSPI_ERASE_4K
	fspi_writel(FSPI_IPCR1, ((FSPI_4K_SEQ_ID << FSPI_IPCR1_ISEQID_SHIFT) | 0));
#else
	fspi_writel(FSPI_IPCR1, ((FSPI_SE_SEQ_ID << FSPI_IPCR1_ISEQID_SHIFT) | 0));
#endif
	fspi_writel(FSPI_IPCMD, FSPI_IPCMD_TRG_MASK);

	while ((fspi_readl(FSPI_INTR) & FSPI_INTR_IPCMDDONE_MASK) == 0) {
		PRA("0x%x", fspi_readl(FSPI_INTR));
	}
	fspi_writel(FSPI_INTR, FSPI_INTR_IPCMDDONE_MASK);
}

int xspi_sector_erase(uint32_t pc_wr_addr, uint32_t ui_len)
{
	uint32_t x_addr, x_len_bytes, i, num_sector = 0U;

	VERBOSE("In func %s\n", __func__);
	x_addr = (uint32_t)(pc_wr_addr);
	if ((x_addr % F_SECTOR_ERASE_SZ) != 0) {
		ERROR("!!! In func %s, unalinged start address can only be in multiples of 0x%x\n",
		      __func__, F_SECTOR_ERASE_SZ);
		return -XSPI_ERASE_FAIL;
	}

	x_len_bytes = ui_len * 1;
	if (x_len_bytes < F_SECTOR_ERASE_SZ) {
		ERROR("!!! In func %s, Less than 1 sector can only be in multiples of 0x%x\n",
				__func__, F_SECTOR_ERASE_SZ);
		return -XSPI_ERASE_FAIL;
	}

	num_sector = x_len_bytes/F_SECTOR_ERASE_SZ;
	num_sector += x_len_bytes % F_SECTOR_ERASE_SZ ? 1U : 0U;
	INFO("F_SECTOR_ERASE_SZ: 0x%08x, num_sector: %d\n", F_SECTOR_ERASE_SZ, num_sector);

	for (i = 0U; i < num_sector ; i++) {
		xspi_wren(x_addr + (F_SECTOR_ERASE_SZ * i));
		fspi_sec_er(x_addr + (F_SECTOR_ERASE_SZ * i));
		while (is_flash_busy())
			;
	}
	fspi_ahb_invalidate();
	return XSPI_SUCCESS;
}


__attribute__((unused)) static void  fspi_delay_ms(uint32_t x)
{
	volatile unsigned long  ul_count;

	for (ul_count = 0U; ul_count < (30U * x); ul_count++)
		;

}


#if defined(DEBUG_FLEXSPI)
static void fspi_dump_regs(void)
{
	uint32_t i;

	VERBOSE("\nRegisters Dump:\n");
	VERBOSE("Flexspi: Register FSPI_MCR0(0x%x) = 0x%08x\n", FSPI_MCR0, fspi_readl(FSPI_MCR0));
	VERBOSE("Flexspi: Register FSPI_MCR2(0x%x) = 0x%08x\n", FSPI_MCR2, fspi_readl(FSPI_MCR2));
	VERBOSE("Flexspi: Register FSPI_DLL_A_CR(0x%x) = 0x%08x\n", FSPI_DLLACR, fspi_readl(FSPI_DLLACR));
	VERBOSE("\n");

	for (i = 0U; i < 8U; i++) {
		VERBOSE("Flexspi: Register FSPI_AHBRX_BUF0CR0(0x%x) = 0x%08x\n", FSPI_AHBRX_BUF0CR0 + i * 4, fspi_readl((FSPI_AHBRX_BUF0CR0 + i * 4)));
	}
	VERBOSE("\n");

	VERBOSE("Flexspi: Register FSPI_AHBRX_BUF7CR0(0x%x) = 0x%08x\n", FSPI_AHBRX_BUF7CR0, fspi_readl(FSPI_AHBRX_BUF7CR0));
	VERBOSE("Flexspi: Register FSPI_AHB_CR(0x%x) \t  = 0x%08x\n", FSPI_AHBCR, fspi_readl(FSPI_AHBCR));
	VERBOSE("\n");

	for (i = 0U; i < 4U; i++) {
		VERBOSE("Flexspi: Register FSPI_FLSH_A1_CR2,(0x%x) = 0x%08x\n", FSPI_FLSHA1CR2 + i * 4, fspi_readl(FSPI_FLSHA1CR2 + i * 4));
	}
}
#endif

int fspi_init(uint32_t base_reg_addr, uint32_t flash_start_addr)
{
	uint32_t	mcrx;
	uint32_t	flash_size;

	if (fspi_base_reg_addr != 0U) {
		INFO("FSPI is already initialized.\n");
		return XSPI_SUCCESS;
	}

	fspi_base_reg_addr = base_reg_addr;
	fspi_flash_base_addr = flash_start_addr;

	INFO("Flexspi driver: Version v1.0\n");
	INFO("Flexspi: Default MCR0 = 0x%08x, before reset\n", fspi_readl(FSPI_MCR0));
	VERBOSE("Flexspi: Resetting controller...\n");

	/* Reset FlexSpi Controller */
	fspi_writel(FSPI_MCR0, FSPI_MCR0_SWRST);
	while ((fspi_readl(FSPI_MCR0) & FSPI_MCR0_SWRST))
		;  /* FSPI_MCR0_SWRESET_MASK */


	/* Disable Controller Module before programming its registersi, especially MCR0 (Master Control Register0) */
	fspi_MDIS(1);
	/*
	 * Program MCR0 with default values, AHB Timeout(0xff), IP Timeout(0xff).  {FSPI_MCR0- 0xFFFF0000}
	 */

	/* Timeout wait cycle for AHB command grant */
	mcrx = fspi_readl(FSPI_MCR0);
	mcrx |= (uint32_t)((FSPI_MAX_TIMEOUT_AHBCMD << FSPI_MCR0_AHBGRANTWAIT_SHIFT) & (FSPI_MCR0_AHBGRANTWAIT_MASK));

	/* Time out wait cycle for IP command grant*/
	mcrx |= (uint32_t) (FSPI_MAX_TIMEOUT_IPCMD << FSPI_MCR0_IPGRANTWAIT_SHIFT) & (FSPI_MCR0_IPGRANTWAIT_MASK);

	/* TODO why BE64 set BE32*/
	mcrx |= (uint32_t) (FSPI_ENDCFG_LE64 << FSPI_MCR0_ENDCFG_SHIFT) & FSPI_MCR0_ENDCFG_MASK;

	fspi_writel(FSPI_MCR0, mcrx);

	/* Reset the DLL register to default value */
	fspi_writel(FSPI_DLLACR, FSPI_DLLACR_OVRDEN);
	fspi_writel(FSPI_DLLBCR, FSPI_DLLBCR_OVRDEN);

#if ERRATA_FLASH_A050272	/* ERRATA DLL */
	for (uint8_t delay = 100U; delay > 0U; delay--)	{
		__asm__ volatile ("nop");
	}
#endif

	/* Configure flash control registers for different chip select */
	flash_size = (F_FLASH_SIZE_BYTES * FLASH_NUM) / FSPI_BYTES_PER_KBYTES;
	fspi_writel(FSPI_FLSHA1CR0, flash_size);
	fspi_writel(FSPI_FLSHA2CR0, 0U);
	fspi_writel(FSPI_FLSHB1CR0, 0U);
	fspi_writel(FSPI_FLSHB2CR0, 0U);

#if defined(CONFIG_FSPI_AHB)
	fspi_init_ahb();
#endif
	/* RE-Enable Controller Module */
	fspi_MDIS(0);
	INFO("Flexspi: After MCR0 = 0x%08x,\n", fspi_readl(FSPI_MCR0));
	fspi_setup_LUT();

	/* Dump of all registers, ensure controller not disabled anymore*/
#if defined(DEBUG_FLEXSPI)
	fspi_dump_regs();
#endif

	INFO("Flexspi: Init done!!\n");

#if DEBUG_FLEXSPI

	uint32_t xspi_addr = SZ_57M;

	/*
	 * Second argument of fspi_test is the size of buffer(s) passed
	 * to the function.
	 * SIZE_BUFFER defined in test_fspi.c is kept large enough to
	 * accommodate variety of sizes for regressive tests.
	 */
	fspi_test(xspi_addr, 0x40, 0);
	fspi_test(xspi_addr, 0x15, 2);
	fspi_test(xspi_addr, 0x80, 0);
	fspi_test(xspi_addr, 0x81, 0);
	fspi_test(xspi_addr, 0x79, 3);

	fspi_test(xspi_addr + 0x11, 0x15, 0);
	fspi_test(xspi_addr + 0x11, 0x40, 0);
	fspi_test(xspi_addr + 0xff, 0x40, 1);
	fspi_test(xspi_addr + 0x25, 0x81, 2);
	fspi_test(xspi_addr + 0xef, 0x6f, 3);

	fspi_test((xspi_addr - F_SECTOR_ERASE_SZ), 0x229, 0);
#endif

	return XSPI_SUCCESS;
}