diff mbox series

[-next,13/14] kddv/tests/mtd: Add test cases for mchp23k256 driver

Message ID 20231118104040.386381-14-zhangxiaoxu@huaweicloud.com (mailing list archive)
State New, archived
Headers show
Series Implement a ligth weight device driver test framework | expand

Commit Message

huaweicloud Nov. 18, 2023, 10:40 a.m. UTC
From: Zhang Xiaoxu <zhangxiaoxu5@huawei.com>

This add test case for mchp23k256 mtd driver, test for
create mchp23k256 device, default size of the device and
read/write data from device.

Signed-off-by: Wei Yongjun <weiyongjun1@huawei.com>
Signed-off-by: Zhang Xiaoxu <zhangxiaoxu@huawei.com>
---
 .../kddv/kddv/data/bpf/mtd/mtd-mchp23k256.c   | 72 +++++++++++++++++++
 .../kddv/kddv/tests/mtd/test_mchp23k256.py    | 41 +++++++++++
 2 files changed, 113 insertions(+)
 create mode 100644 tools/testing/kddv/kddv/data/bpf/mtd/mtd-mchp23k256.c
 create mode 100755 tools/testing/kddv/kddv/tests/mtd/test_mchp23k256.py
diff mbox series

Patch

diff --git a/tools/testing/kddv/kddv/data/bpf/mtd/mtd-mchp23k256.c b/tools/testing/kddv/kddv/data/bpf/mtd/mtd-mchp23k256.c
new file mode 100644
index 000000000000..b1aa8a25edc0
--- /dev/null
+++ b/tools/testing/kddv/kddv/data/bpf/mtd/mtd-mchp23k256.c
@@ -0,0 +1,72 @@ 
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2022-2023 Huawei Technologies Co., Ltd */
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include <errno.h>
+
+#include "spi-xfer-base.h"
+
+#define MCHP23K256_CMD_WRITE_STATUS	0x01
+#define MCHP23K256_CMD_WRITE		0x02
+#define MCHP23K256_CMD_READ		0x03
+
+#define CHIP_REGS_SIZE			0x20000
+
+#define MAX_CMD_SIZE			4
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__uint(max_entries, CHIP_REGS_SIZE);
+	__type(key, __u32);
+	__type(value, __u8);
+} regs_mtd_mchp23k256 SEC(".maps");
+
+static unsigned int chip_reg;
+
+static int spi_transfer_read(struct spi_msg_ctx *msg, unsigned int len)
+{
+	return spi_xfer_read_u8(msg, len, &regs_mtd_mchp23k256, chip_reg, 0);
+}
+
+static int spi_transfer_write(struct spi_msg_ctx *msg, unsigned int len)
+{
+	u8 opcode = msg->data[0];
+	int i;
+
+	switch (opcode) {
+	case MCHP23K256_CMD_READ:
+	case MCHP23K256_CMD_WRITE:
+		if (len < 2)
+			return -EINVAL;
+
+		chip_reg = 0;
+		for (i = 0; i < MAX_CMD_SIZE && i < len - 1; i++)
+			chip_reg = (chip_reg << 8) + msg->data[1 + i];
+
+		return 0;
+	case MCHP23K256_CMD_WRITE_STATUS:
+		// ignore write status
+		return 0;
+	default:
+		break;
+	}
+
+	return spi_xfer_write_u8(msg, len, &regs_mtd_mchp23k256, chip_reg, 0);
+}
+
+SEC("raw_tp.w/spi_transfer_writeable")
+int BPF_PROG(mtd_mchp23k256, struct spi_msg_ctx *msg, u8 chip, unsigned int len)
+{
+	int ret = 0;
+
+	if (msg->tx_nbits)
+		ret = spi_transfer_write(msg, len);
+	else if (msg->rx_nbits)
+		ret = spi_transfer_read(msg, len);
+
+	return ret;
+}
+
+char LICENSE[] SEC("license") = "GPL";
diff --git a/tools/testing/kddv/kddv/tests/mtd/test_mchp23k256.py b/tools/testing/kddv/kddv/tests/mtd/test_mchp23k256.py
new file mode 100755
index 000000000000..eefad9d70483
--- /dev/null
+++ b/tools/testing/kddv/kddv/tests/mtd/test_mchp23k256.py
@@ -0,0 +1,41 @@ 
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+#
+# Kernel device driver verification
+#
+# Copyright (C) 2022-2023 Huawei Technologies Co., Ltd
+# Author: Wei Yongjun <weiyongjun1@huawei.com>
+
+from kddv.core import SPIDriverTest
+from . import MTDDriver
+
+MCHP23K256_TEST_DATA = [0x78] * 16
+
+class TestMCHP23K256(SPIDriverTest, MTDDriver):
+    name = "mchp23k256"
+
+    @property
+    def bpf(self):
+        return f"mtd-{self.name}"
+
+    def test_device_probe(self):
+        with self.assertRaisesFault():
+            with self.device() as _:
+                pass
+
+    def test_device_size(self):
+        with self.device() as dev:
+            size = self.mtd_read_attr(dev, 'size')
+            self.assertEqual(size, '32768')
+
+    def test_read_data(self):
+        with self.device() as dev:
+            self.write_regs(0x00, MCHP23K256_TEST_DATA)
+            data = self.mtd_read_bytes(dev, 16)
+            self.assertEqual(data, bytes(MCHP23K256_TEST_DATA))
+
+    def test_write_data(self):
+        with self.device() as dev:
+            self.write_regs(0x00, [0] * 16)
+            self.mtd_write_bytes(dev, bytes(MCHP23K256_TEST_DATA))
+            self.assertRegsEqual(0x00, MCHP23K256_TEST_DATA)