rtctest.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Real Time Clock Driver Test Program
  4. *
  5. * Copyright (c) 2018 Alexandre Belloni <[email protected]>
  6. */
  7. #include <errno.h>
  8. #include <fcntl.h>
  9. #include <linux/rtc.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <sys/ioctl.h>
  13. #include <sys/time.h>
  14. #include <sys/types.h>
  15. #include <time.h>
  16. #include <unistd.h>
  17. #include "../kselftest_harness.h"
  18. #define NUM_UIE 3
  19. #define ALARM_DELTA 3
  20. #define READ_LOOP_DURATION_SEC 30
  21. #define READ_LOOP_SLEEP_MS 11
  22. static char *rtc_file = "/dev/rtc0";
  23. FIXTURE(rtc) {
  24. int fd;
  25. };
  26. FIXTURE_SETUP(rtc) {
  27. self->fd = open(rtc_file, O_RDONLY);
  28. ASSERT_NE(-1, self->fd);
  29. }
  30. FIXTURE_TEARDOWN(rtc) {
  31. close(self->fd);
  32. }
  33. TEST_F(rtc, date_read) {
  34. int rc;
  35. struct rtc_time rtc_tm;
  36. /* Read the RTC time/date */
  37. rc = ioctl(self->fd, RTC_RD_TIME, &rtc_tm);
  38. ASSERT_NE(-1, rc);
  39. TH_LOG("Current RTC date/time is %02d/%02d/%02d %02d:%02d:%02d.",
  40. rtc_tm.tm_mday, rtc_tm.tm_mon + 1, rtc_tm.tm_year + 1900,
  41. rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
  42. }
  43. static time_t rtc_time_to_timestamp(struct rtc_time *rtc_time)
  44. {
  45. struct tm tm_time = {
  46. .tm_sec = rtc_time->tm_sec,
  47. .tm_min = rtc_time->tm_min,
  48. .tm_hour = rtc_time->tm_hour,
  49. .tm_mday = rtc_time->tm_mday,
  50. .tm_mon = rtc_time->tm_mon,
  51. .tm_year = rtc_time->tm_year,
  52. };
  53. return mktime(&tm_time);
  54. }
  55. static void nanosleep_with_retries(long ns)
  56. {
  57. struct timespec req = {
  58. .tv_sec = 0,
  59. .tv_nsec = ns,
  60. };
  61. struct timespec rem;
  62. while (nanosleep(&req, &rem) != 0) {
  63. req.tv_sec = rem.tv_sec;
  64. req.tv_nsec = rem.tv_nsec;
  65. }
  66. }
  67. TEST_F_TIMEOUT(rtc, date_read_loop, READ_LOOP_DURATION_SEC + 2) {
  68. int rc;
  69. long iter_count = 0;
  70. struct rtc_time rtc_tm;
  71. time_t start_rtc_read, prev_rtc_read;
  72. TH_LOG("Continuously reading RTC time for %ds (with %dms breaks after every read).",
  73. READ_LOOP_DURATION_SEC, READ_LOOP_SLEEP_MS);
  74. rc = ioctl(self->fd, RTC_RD_TIME, &rtc_tm);
  75. ASSERT_NE(-1, rc);
  76. start_rtc_read = rtc_time_to_timestamp(&rtc_tm);
  77. prev_rtc_read = start_rtc_read;
  78. do {
  79. time_t rtc_read;
  80. rc = ioctl(self->fd, RTC_RD_TIME, &rtc_tm);
  81. ASSERT_NE(-1, rc);
  82. rtc_read = rtc_time_to_timestamp(&rtc_tm);
  83. /* Time should not go backwards */
  84. ASSERT_LE(prev_rtc_read, rtc_read);
  85. /* Time should not increase more then 1s at a time */
  86. ASSERT_GE(prev_rtc_read + 1, rtc_read);
  87. /* Sleep 11ms to avoid killing / overheating the RTC */
  88. nanosleep_with_retries(READ_LOOP_SLEEP_MS * 1000000);
  89. prev_rtc_read = rtc_read;
  90. iter_count++;
  91. } while (prev_rtc_read <= start_rtc_read + READ_LOOP_DURATION_SEC);
  92. TH_LOG("Performed %ld RTC time reads.", iter_count);
  93. }
  94. TEST_F_TIMEOUT(rtc, uie_read, NUM_UIE + 2) {
  95. int i, rc, irq = 0;
  96. unsigned long data;
  97. /* Turn on update interrupts */
  98. rc = ioctl(self->fd, RTC_UIE_ON, 0);
  99. if (rc == -1) {
  100. ASSERT_EQ(EINVAL, errno);
  101. TH_LOG("skip update IRQs not supported.");
  102. return;
  103. }
  104. for (i = 0; i < NUM_UIE; i++) {
  105. /* This read will block */
  106. rc = read(self->fd, &data, sizeof(data));
  107. ASSERT_NE(-1, rc);
  108. irq++;
  109. }
  110. EXPECT_EQ(NUM_UIE, irq);
  111. rc = ioctl(self->fd, RTC_UIE_OFF, 0);
  112. ASSERT_NE(-1, rc);
  113. }
  114. TEST_F(rtc, uie_select) {
  115. int i, rc, irq = 0;
  116. unsigned long data;
  117. /* Turn on update interrupts */
  118. rc = ioctl(self->fd, RTC_UIE_ON, 0);
  119. if (rc == -1) {
  120. ASSERT_EQ(EINVAL, errno);
  121. TH_LOG("skip update IRQs not supported.");
  122. return;
  123. }
  124. for (i = 0; i < NUM_UIE; i++) {
  125. struct timeval tv = { .tv_sec = 2 };
  126. fd_set readfds;
  127. FD_ZERO(&readfds);
  128. FD_SET(self->fd, &readfds);
  129. /* The select will wait until an RTC interrupt happens. */
  130. rc = select(self->fd + 1, &readfds, NULL, NULL, &tv);
  131. ASSERT_NE(-1, rc);
  132. ASSERT_NE(0, rc);
  133. /* This read won't block */
  134. rc = read(self->fd, &data, sizeof(unsigned long));
  135. ASSERT_NE(-1, rc);
  136. irq++;
  137. }
  138. EXPECT_EQ(NUM_UIE, irq);
  139. rc = ioctl(self->fd, RTC_UIE_OFF, 0);
  140. ASSERT_NE(-1, rc);
  141. }
  142. TEST_F(rtc, alarm_alm_set) {
  143. struct timeval tv = { .tv_sec = ALARM_DELTA + 2 };
  144. unsigned long data;
  145. struct rtc_time tm;
  146. fd_set readfds;
  147. time_t secs, new;
  148. int rc;
  149. rc = ioctl(self->fd, RTC_RD_TIME, &tm);
  150. ASSERT_NE(-1, rc);
  151. secs = timegm((struct tm *)&tm) + ALARM_DELTA;
  152. gmtime_r(&secs, (struct tm *)&tm);
  153. rc = ioctl(self->fd, RTC_ALM_SET, &tm);
  154. if (rc == -1) {
  155. ASSERT_EQ(EINVAL, errno);
  156. TH_LOG("skip alarms are not supported.");
  157. return;
  158. }
  159. rc = ioctl(self->fd, RTC_ALM_READ, &tm);
  160. ASSERT_NE(-1, rc);
  161. TH_LOG("Alarm time now set to %02d:%02d:%02d.",
  162. tm.tm_hour, tm.tm_min, tm.tm_sec);
  163. /* Enable alarm interrupts */
  164. rc = ioctl(self->fd, RTC_AIE_ON, 0);
  165. ASSERT_NE(-1, rc);
  166. FD_ZERO(&readfds);
  167. FD_SET(self->fd, &readfds);
  168. rc = select(self->fd + 1, &readfds, NULL, NULL, &tv);
  169. ASSERT_NE(-1, rc);
  170. ASSERT_NE(0, rc);
  171. /* Disable alarm interrupts */
  172. rc = ioctl(self->fd, RTC_AIE_OFF, 0);
  173. ASSERT_NE(-1, rc);
  174. rc = read(self->fd, &data, sizeof(unsigned long));
  175. ASSERT_NE(-1, rc);
  176. TH_LOG("data: %lx", data);
  177. rc = ioctl(self->fd, RTC_RD_TIME, &tm);
  178. ASSERT_NE(-1, rc);
  179. new = timegm((struct tm *)&tm);
  180. ASSERT_EQ(new, secs);
  181. }
  182. TEST_F(rtc, alarm_wkalm_set) {
  183. struct timeval tv = { .tv_sec = ALARM_DELTA + 2 };
  184. struct rtc_wkalrm alarm = { 0 };
  185. struct rtc_time tm;
  186. unsigned long data;
  187. fd_set readfds;
  188. time_t secs, new;
  189. int rc;
  190. rc = ioctl(self->fd, RTC_RD_TIME, &alarm.time);
  191. ASSERT_NE(-1, rc);
  192. secs = timegm((struct tm *)&alarm.time) + ALARM_DELTA;
  193. gmtime_r(&secs, (struct tm *)&alarm.time);
  194. alarm.enabled = 1;
  195. rc = ioctl(self->fd, RTC_WKALM_SET, &alarm);
  196. if (rc == -1) {
  197. ASSERT_EQ(EINVAL, errno);
  198. TH_LOG("skip alarms are not supported.");
  199. return;
  200. }
  201. rc = ioctl(self->fd, RTC_WKALM_RD, &alarm);
  202. ASSERT_NE(-1, rc);
  203. TH_LOG("Alarm time now set to %02d/%02d/%02d %02d:%02d:%02d.",
  204. alarm.time.tm_mday, alarm.time.tm_mon + 1,
  205. alarm.time.tm_year + 1900, alarm.time.tm_hour,
  206. alarm.time.tm_min, alarm.time.tm_sec);
  207. FD_ZERO(&readfds);
  208. FD_SET(self->fd, &readfds);
  209. rc = select(self->fd + 1, &readfds, NULL, NULL, &tv);
  210. ASSERT_NE(-1, rc);
  211. ASSERT_NE(0, rc);
  212. rc = read(self->fd, &data, sizeof(unsigned long));
  213. ASSERT_NE(-1, rc);
  214. rc = ioctl(self->fd, RTC_RD_TIME, &tm);
  215. ASSERT_NE(-1, rc);
  216. new = timegm((struct tm *)&tm);
  217. ASSERT_EQ(new, secs);
  218. }
  219. TEST_F_TIMEOUT(rtc, alarm_alm_set_minute, 65) {
  220. struct timeval tv = { .tv_sec = 62 };
  221. unsigned long data;
  222. struct rtc_time tm;
  223. fd_set readfds;
  224. time_t secs, new;
  225. int rc;
  226. rc = ioctl(self->fd, RTC_RD_TIME, &tm);
  227. ASSERT_NE(-1, rc);
  228. secs = timegm((struct tm *)&tm) + 60 - tm.tm_sec;
  229. gmtime_r(&secs, (struct tm *)&tm);
  230. rc = ioctl(self->fd, RTC_ALM_SET, &tm);
  231. if (rc == -1) {
  232. ASSERT_EQ(EINVAL, errno);
  233. TH_LOG("skip alarms are not supported.");
  234. return;
  235. }
  236. rc = ioctl(self->fd, RTC_ALM_READ, &tm);
  237. ASSERT_NE(-1, rc);
  238. TH_LOG("Alarm time now set to %02d:%02d:%02d.",
  239. tm.tm_hour, tm.tm_min, tm.tm_sec);
  240. /* Enable alarm interrupts */
  241. rc = ioctl(self->fd, RTC_AIE_ON, 0);
  242. ASSERT_NE(-1, rc);
  243. FD_ZERO(&readfds);
  244. FD_SET(self->fd, &readfds);
  245. rc = select(self->fd + 1, &readfds, NULL, NULL, &tv);
  246. ASSERT_NE(-1, rc);
  247. ASSERT_NE(0, rc);
  248. /* Disable alarm interrupts */
  249. rc = ioctl(self->fd, RTC_AIE_OFF, 0);
  250. ASSERT_NE(-1, rc);
  251. rc = read(self->fd, &data, sizeof(unsigned long));
  252. ASSERT_NE(-1, rc);
  253. TH_LOG("data: %lx", data);
  254. rc = ioctl(self->fd, RTC_RD_TIME, &tm);
  255. ASSERT_NE(-1, rc);
  256. new = timegm((struct tm *)&tm);
  257. ASSERT_EQ(new, secs);
  258. }
  259. TEST_F_TIMEOUT(rtc, alarm_wkalm_set_minute, 65) {
  260. struct timeval tv = { .tv_sec = 62 };
  261. struct rtc_wkalrm alarm = { 0 };
  262. struct rtc_time tm;
  263. unsigned long data;
  264. fd_set readfds;
  265. time_t secs, new;
  266. int rc;
  267. rc = ioctl(self->fd, RTC_RD_TIME, &alarm.time);
  268. ASSERT_NE(-1, rc);
  269. secs = timegm((struct tm *)&alarm.time) + 60 - alarm.time.tm_sec;
  270. gmtime_r(&secs, (struct tm *)&alarm.time);
  271. alarm.enabled = 1;
  272. rc = ioctl(self->fd, RTC_WKALM_SET, &alarm);
  273. if (rc == -1) {
  274. ASSERT_EQ(EINVAL, errno);
  275. TH_LOG("skip alarms are not supported.");
  276. return;
  277. }
  278. rc = ioctl(self->fd, RTC_WKALM_RD, &alarm);
  279. ASSERT_NE(-1, rc);
  280. TH_LOG("Alarm time now set to %02d/%02d/%02d %02d:%02d:%02d.",
  281. alarm.time.tm_mday, alarm.time.tm_mon + 1,
  282. alarm.time.tm_year + 1900, alarm.time.tm_hour,
  283. alarm.time.tm_min, alarm.time.tm_sec);
  284. FD_ZERO(&readfds);
  285. FD_SET(self->fd, &readfds);
  286. rc = select(self->fd + 1, &readfds, NULL, NULL, &tv);
  287. ASSERT_NE(-1, rc);
  288. ASSERT_NE(0, rc);
  289. rc = read(self->fd, &data, sizeof(unsigned long));
  290. ASSERT_NE(-1, rc);
  291. rc = ioctl(self->fd, RTC_RD_TIME, &tm);
  292. ASSERT_NE(-1, rc);
  293. new = timegm((struct tm *)&tm);
  294. ASSERT_EQ(new, secs);
  295. }
  296. static void __attribute__((constructor))
  297. __constructor_order_last(void)
  298. {
  299. if (!__constructor_order)
  300. __constructor_order = _CONSTRUCTOR_ORDER_BACKWARD;
  301. }
  302. int main(int argc, char **argv)
  303. {
  304. switch (argc) {
  305. case 2:
  306. rtc_file = argv[1];
  307. /* FALLTHROUGH */
  308. case 1:
  309. break;
  310. default:
  311. fprintf(stderr, "usage: %s [rtcdev]\n", argv[0]);
  312. return 1;
  313. }
  314. return test_harness_run(argc, argv);
  315. }