1 // SPDX-License-Identifier: GPL-2.0+
3 * Functional tests for UCLASS_FFA class
16 #include <linux/bitops.h>
17 #include <test/test.h>
21 /* NVMXIP devices described in the device tree */
22 #define SANDBOX_NVMXIP_DEVICES 2
24 /* reference device tree data for the probed devices */
25 static struct nvmxip_plat nvmqspi_refdata[SANDBOX_NVMXIP_DEVICES] = {
26 {0x08000000, 9, 4096}, {0x08200000, 9, 2048}
29 #define NVMXIP_BLK_START_PATTERN 0x1122334455667788ULL
30 #define NVMXIP_BLK_END_PATTERN 0xa1a2a3a4a5a6a7a8ULL
33 * dm_nvmxip_flash_sanity() - check flash data
35 * @device_idx: the NVMXIP device index
36 * @buffer: the user buffer where the blocks data is copied to
38 * Mode 1: When buffer is NULL, initialize the flash with pattern data at the start
39 * and at the end of each block. This pattern data will be used to check data consistency
40 * when verifying the data read.
41 * Mode 2: When the user buffer is provided in the argument (not NULL), compare the data
42 * of the start and the end of each block in the user buffer with the expected pattern data.
43 * Return an error when the check fails.
47 * 0 on success. Otherwise, failure
49 static int dm_nvmxip_flash_sanity(struct unit_test_state *uts, u8 device_idx, void *buffer)
56 blksz = BIT(nvmqspi_refdata[device_idx].lba_shift);
59 /* Mode 1: point at the flash start address. Pattern data will be written */
60 base = map_sysmem(nvmqspi_refdata[device_idx].phys_base, 0);
62 /* Mode 2: point at the user buffer containing the data read and to be verified */
66 for (i = 0; i < nvmqspi_refdata[device_idx].lba ; i++) {
67 ptr = (u64 *)(base + i * blksz);
69 /* write an 8 bytes pattern at the start of the current block */
71 *ptr = NVMXIP_BLK_START_PATTERN;
73 ut_asserteq_64(NVMXIP_BLK_START_PATTERN, *ptr);
75 ptr = (u64 *)((u8 *)ptr + blksz - sizeof(u64));
77 /* write an 8 bytes pattern at the end of the current block */
79 *ptr = NVMXIP_BLK_END_PATTERN;
81 ut_asserteq_64(NVMXIP_BLK_END_PATTERN, *ptr);
91 * dm_test_nvmxip() - check flash data
95 * CMD_RET_SUCCESS on success. Otherwise, failure
97 static int dm_test_nvmxip(struct unit_test_state *uts)
99 struct nvmxip_plat *plat_data = NULL;
100 struct udevice *dev = NULL, *bdev = NULL;
103 unsigned long flashsz;
105 sandbox_set_enable_memio(true);
107 /* set the flash content first for both devices */
108 dm_nvmxip_flash_sanity(uts, 0, NULL);
109 dm_nvmxip_flash_sanity(uts, 1, NULL);
111 /* probing all NVM XIP QSPI devices */
112 for (device_idx = 0, uclass_first_device(UCLASS_NVMXIP, &dev);
114 uclass_next_device(&dev), device_idx++) {
115 plat_data = dev_get_plat(dev);
117 /* device tree entries checks */
118 ut_assertok(nvmqspi_refdata[device_idx].phys_base != plat_data->phys_base);
119 ut_assertok(nvmqspi_refdata[device_idx].lba_shift != plat_data->lba_shift);
120 ut_assertok(nvmqspi_refdata[device_idx].lba != plat_data->lba);
122 /* before reading all the flash blocks, let's calculate the flash size */
123 flashsz = plat_data->lba << plat_data->lba_shift;
125 /* allocate the user buffer where to copy the blocks data to */
126 buffer = calloc(flashsz, 1);
127 ut_assertok(!buffer);
129 /* the block device is the child of the parent device probed with DT */
130 ut_assertok(device_find_first_child(dev, &bdev));
132 /* reading all the flash blocks */
133 ut_asserteq(plat_data->lba, blk_read(bdev, 0, plat_data->lba, buffer));
135 /* compare the data read from flash with the expected data */
136 dm_nvmxip_flash_sanity(uts, device_idx, buffer);
141 ut_assertok(device_idx != SANDBOX_NVMXIP_DEVICES);
143 return CMD_RET_SUCCESS;
146 DM_TEST(dm_test_nvmxip, UT_TESTF_SCAN_FDT | UT_TESTF_CONSOLE_REC);