1 /*
2 * Copyright 2022 NXP
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/kernel.h>
8 #include <string.h>
9 #include <zephyr/drivers/mspi.h>
10
11 #if DT_HAS_COMPAT_STATUS_OKAY(nxp_imx_flexspi)
12 /* Use memc API to get AHB base address for the device */
13 #include "memc_mcux_flexspi.h"
14 #define FLEXSPI_DEV DEVICE_DT_GET(DT_PARENT(DT_ALIAS(sram_ext)))
15 #define MEMC_PORT DT_REG_ADDR(DT_ALIAS(sram_ext))
16 #define MEMC_BASE ((void *)memc_flexspi_get_ahb_address(FLEXSPI_DEV, MEMC_PORT, 0))
17 #define MEMC_SIZE (DT_PROP(DT_ALIAS(sram_ext), size) / 8)
18 #elif DT_HAS_COMPAT_STATUS_OKAY(renesas_smartbond_nor_psram)
19 #include <da1469x_config.h>
20 #define MEMC_BASE ((void *)MCU_QSPIR_M_BASE)
21 #define MEMC_SIZE (DT_PROP(DT_ALIAS(sram_ext), dev_size) / 8)
22 #elif DT_HAS_COMPAT_STATUS_OKAY(ambiq_mspi_controller)
23 #define MSPI_ DT_BUS(DT_ALIAS(psram0))
24 #define mspi_get_xip_address(controller) DT_REG_ADDR_BY_IDX(controller, 1)
25 #define MEMC_BASE (void *)(mspi_get_xip_address(MSPI_))
26 #define MEMC_SIZE (DT_PROP(DT_ALIAS(psram0), size) / 8)
27 #else
28 #error At least one driver should be selected!
29 #endif
30
dump_memory(uint8_t * p,uint32_t size)31 void dump_memory(uint8_t *p, uint32_t size)
32 {
33 uint32_t i, j;
34
35 for (i = 0, j = 0; j < (size / 16); i += 16, j++) {
36 printk("%02x %02x %02x %02x %02x %02x %02x %02x ",
37 p[i], p[i+1], p[i+2], p[i+3],
38 p[i+4], p[i+5], p[i+6], p[i+7]);
39 printk("%02x %02x %02x %02x %02x %02x %02x %02x\n",
40 p[i+8], p[i+9], p[i+10], p[i+11],
41 p[i+12], p[i+13], p[i+14], p[i+15]);
42 /* Split dump at 256B boundaries */
43 if (((i + 16) & 0xFF) == 0) {
44 printk("\n");
45 }
46 }
47 /* Dump any remaining data after 16 byte blocks */
48 for (; i < size; i++) {
49 printk("%02x ", p[i]);
50 }
51 printk("\n");
52 }
53
54 #define BUF_SIZE 1024
55
56 uint8_t memc_write_buffer[BUF_SIZE];
57 uint8_t memc_read_buffer[BUF_SIZE];
58
main(void)59 int main(void)
60 {
61 uint8_t *memc = (uint8_t *)MEMC_BASE;
62 uint32_t i, j;
63
64 /* Initialize write buffer */
65 for (i = 0; i < BUF_SIZE; i++) {
66 memc_write_buffer[i] = (uint8_t)i;
67 }
68 printk("Writing to memory region with base %p, size 0x%0x\n\n",
69 memc, MEMC_SIZE);
70 /* Copy write buffer into memc region */
71 for (i = 0, j = 0; j < (MEMC_SIZE / BUF_SIZE); i += BUF_SIZE, j++) {
72 memcpy(memc + i, memc_write_buffer, BUF_SIZE);
73 }
74 /* Copy any remaining space bytewise */
75 for (; i < MEMC_SIZE; i++) {
76 memc[i] = memc_write_buffer[i];
77 }
78 /* Read from memc region into buffer */
79 for (i = 0, j = 0; j < (MEMC_SIZE / BUF_SIZE); i += BUF_SIZE, j++) {
80 memcpy(memc_read_buffer, memc + i, BUF_SIZE);
81 /* Compare memory */
82 if (memcmp(memc_read_buffer, memc_write_buffer, BUF_SIZE)) {
83 printk("Error: read data differs in range [0x%x- 0x%x]\n",
84 i, i + (BUF_SIZE - 1));
85 dump_memory(memc_write_buffer, BUF_SIZE);
86 dump_memory(memc_read_buffer, BUF_SIZE);
87 return 0;
88 }
89 printk("Check (%i/%i) passed!\n", j, (MEMC_SIZE / BUF_SIZE) - 1);
90 }
91 /* Copy any remaining space bytewise */
92 for (; i < MEMC_SIZE; i++) {
93 memc_read_buffer[i] = memc[i];
94 if (memc_write_buffer[i] != memc_read_buffer[i]) {
95 printk("Error: read data differs at offset 0x%x\n", i);
96 return 0;
97 }
98 }
99 printk("First 1KB of Data in memory:\n");
100 printk("===========================\n");
101 dump_memory(memc, MIN(MEMC_SIZE, KB(1)));
102 printk("Read data matches written data\n");
103 return 0;
104 }
105