1 /*
2  * Copyright 2021 NXP
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #include <stdint.h>
9 #include <stdio.h>
10 
11 #include <common/debug.h>
12 #include <flash_info.h>
13 #include "fspi.h"
14 #include <fspi_api.h>
15 
16 /*
17  * The macros are defined to be used as test vector for testing fspi.
18  */
19 #define	SIZE_BUFFER			0x250
20 
21 /*
22  * You may choose fspi_swap based on core endianness and flexspi IP/AHB
23  * buffer endianness set in MCR.
24  */
25 #define fspi_swap32(A)			(A)
26 
fspi_test(uint32_t fspi_test_addr,uint32_t size,int extra)27 void fspi_test(uint32_t fspi_test_addr, uint32_t size, int extra)
28 {
29 	uint32_t buffer[SIZE_BUFFER];
30 	uint32_t count = 1;
31 	uint32_t failed, i;
32 
33 	NOTICE("-------------------------- %d----------------------------------\n", count++);
34 	INFO("Sector Erase size: 0x%08x, size: %d\n", F_SECTOR_ERASE_SZ, size);
35 	/* Test Sector Erase */
36 	xspi_sector_erase(fspi_test_addr - fspi_test_addr % F_SECTOR_ERASE_SZ,
37 			  F_SECTOR_ERASE_SZ);
38 
39 	/* Test Erased data using IP read */
40 	xspi_ip_read((fspi_test_addr), buffer, size * 4);
41 
42 	failed = 0;
43 	for (i = 0; i < size; i++)
44 		if (fspi_swap32(0xffffffff) != buffer[i]) {
45 			failed = 1;
46 			break;
47 		}
48 
49 	if (failed == 0) {
50 		NOTICE("[%d]: Success Erase: data in buffer[%d] 0x%08x\n", __LINE__, i-3, buffer[i-3]);
51 	} else {
52 		ERROR("Erase: Failed  -->xxx with buffer[%d]=0x%08x\n", i, buffer[i]);
53 	}
54 
55 	for (i = 0; i < SIZE_BUFFER; i++)
56 		buffer[i] = 0x12345678;
57 
58 	/* Write data from buffer to flash */
59 	xspi_write(fspi_test_addr, (void *)buffer, (size * 4 + extra));
60 	/* Check written data using IP read */
61 	xspi_ip_read(fspi_test_addr, buffer, (size * 4 + extra));
62 	failed = 0;
63 	for (i = 0; i < size; i++)
64 		if (fspi_swap32(0x12345678) != buffer[i]) {
65 			failed = 1;
66 			break;
67 		}
68 
69 	if (failed == 0) {
70 		NOTICE("[%d]: Success IpWrite with IP READ in buffer[%d] 0x%08x\n", __LINE__, i-3, buffer[i-3]);
71 	} else {
72 		ERROR("Write: Failed  -->xxxx with IP READ in buffer[%d]=0x%08x\n", i, buffer[i]);
73 		return;
74 	}
75 
76 	/* xspi_read may use AHB read */
77 	xspi_read((fspi_test_addr), buffer, (size * 4 + extra));
78 	failed = 0;
79 	for (i = 0; i < size; i++)
80 		if (fspi_swap32(0x12345678) != buffer[i]) {
81 			failed = 1;
82 			break;
83 		}
84 
85 	if (failed == 0) {
86 		NOTICE("[%d]: Success IpWrite with AHB OR IP READ on buffer[%d] 0x%08x\n", __LINE__, i-3, buffer[i-3]);
87 	} else {
88 		ERROR("Write: Failed  -->xxxx with AHB READ on buffer[%d]=0x%08x\n", i, buffer[i]);
89 		return;
90 	}
91 }
92