Linux Kernel Selftest development
 help / color / mirror / Atom feed
* [PATCH 1/7] Subject: [PATCH] selftests/powerpc: Add RTAS real-time clock test
@ 2026-09-18  5:22 Shirisha G
  2026-09-18  5:22 ` [PATCH 2/7] Subject: [PATCH] selftests/powerpc: Add NVRAM access test Shirisha G
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: Shirisha G @ 2026-09-18  5:22 UTC (permalink / raw)
  To: linuxppc-dev; +Cc: linux-kselftest, maddy, venkat88, shirisha

Add kselftest for RTAS-based real-time clock on PowerVM systems.
Tests RTC device presence, time accuracy, and time advancement.

The test verifies:
- RTC device exists at /sys/class/rtc/rtc0/
- RTC time matches system time (within 2 seconds)
- Year is sane (>= 2020)
- Time advances correctly

Signed-off-by: Shirisha G <shirisha@linux.ibm.com>
---
 .../selftests/powerpc/rtas/rtas_rtc_test.c    | 93 +++++++++++++++++++
 1 file changed, 93 insertions(+)
 create mode 100644 tools/testing/selftests/powerpc/rtas/rtas_rtc_test.c

diff --git a/tools/testing/selftests/powerpc/rtas/rtas_rtc_test.c b/tools/testing/selftests/powerpc/rtas/rtas_rtc_test.c
new file mode 100644
index 000000000000..12d4f38230e6
--- /dev/null
+++ b/tools/testing/selftests/powerpc/rtas/rtas_rtc_test.c
@@ -0,0 +1,93 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * RTAS Real-Time Clock Test
+ * Copyright (C) 2026 IBM Corporation
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+#include <errno.h>
+#include "utils.h"
+
+#define _GNU_SOURCE
+#define RTC_PATH "/sys/class/rtc/rtc0/time"
+#define RTC_DATE_PATH "/sys/class/rtc/rtc0/date"
+
+static int read_rtc_time(time_t *rtc_time)
+{
+	FILE *fp;
+	struct tm tm = {0};
+	char buf[64];
+
+	fp = fopen(RTC_PATH, "r");
+	if (!fp) {
+		perror("fopen RTC_PATH");
+		return -1;
+	}
+
+	if (!fgets(buf, sizeof(buf), fp)) {
+		fclose(fp);
+		return -1;
+	}
+	fclose(fp);
+
+	/* Parse HH:MM:SS format */
+	if (sscanf(buf, "%d:%d:%d", &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 3)
+		return -1;
+
+	/* Read date */
+	fp = fopen(RTC_DATE_PATH, "r");
+	if (!fp)
+		return -1;
+
+	if (!fgets(buf, sizeof(buf), fp)) {
+		fclose(fp);
+		return -1;
+	}
+	fclose(fp);
+
+	/* Parse YYYY-MM-DD format */
+	if (sscanf(buf, "%d-%d-%d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday) != 3)
+		return -1;
+
+	tm.tm_year -= 1900;  /* years since 1900 */
+	tm.tm_mon -= 1;      /* months since January */
+
+	*rtc_time = timegm(&tm);
+	return 0;
+}
+
+int main(void)
+{
+	time_t rtc_time1, rtc_time2, sys_time;
+	int diff;
+
+	test_harness_set_timeout(10);
+
+	/* Test 1: Verify RTC device exists */
+	SKIP_IF(access(RTC_PATH, R_OK) != 0);
+
+	/* Test 2: Read RTC time */
+	FAIL_IF(read_rtc_time(&rtc_time1) != 0);
+
+	/* Test 3: Compare with system time */
+	sys_time = time(NULL);
+	diff = abs((int)(rtc_time1 - sys_time));
+	FAIL_IF_MSG(diff > 2, "RTC time differs from system time by %d seconds", diff);
+
+	/* Test 4: Verify year > 2020 */
+	struct tm *tm = localtime(&rtc_time1);
+	FAIL_IF_MSG(tm->tm_year + 1900 < 2020, "RTC year is %d (expected >= 2020)", 
+	            tm->tm_year + 1900);
+
+	/* Test 5: Verify time advances */
+	sleep(2);
+	FAIL_IF(read_rtc_time(&rtc_time2) != 0);
+	FAIL_IF_MSG(rtc_time2 <= rtc_time1, "RTC time did not advance");
+
+	printf("RTAS RTC test passed\n");
+	return 0;
+}
-- 
2.43.5


^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-09-18  5:24 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-09-18  5:22 [PATCH 1/7] Subject: [PATCH] selftests/powerpc: Add RTAS real-time clock test Shirisha G
2026-09-18  5:22 ` [PATCH 2/7] Subject: [PATCH] selftests/powerpc: Add NVRAM access test Shirisha G
2026-09-18  5:22 ` [PATCH 3/7] Subject: [PATCH] selftests/powerpc: Add RTAS /proc interface test Shirisha G
2026-09-18  5:22 ` [PATCH 4/7] Subject: [PATCH] selftests/powerpc: Add FADump sysfs " Shirisha G
2026-09-18  5:22 ` [PATCH 5/7] Subject: [PATCH] selftests/powerpc: Add RTAS firmware flash " Shirisha G
2026-09-18  5:22 ` [PATCH 6/7] Subject: [PATCH] selftests/powerpc: Add common test utilities header Shirisha G
2026-09-18  5:22 ` [PATCH 7/7] Subject: [PATCH] selftests/powerpc/rtas: consolidate TEST_GEN_PROGS definitions Shirisha G

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox