blob: 2a58f985795ad4b28ad4bc86ebcca10527e8c14e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001
2 Real Time Clock Driver for Linux
3 ================================
4
5All PCs (even Alpha machines) have a Real Time Clock built into them.
6Usually they are built into the chipset of the computer, but some may
7actually have a Motorola MC146818 (or clone) on the board. This is the
8clock that keeps the date and time while your computer is turned off.
9
10However it can also be used to generate signals from a slow 2Hz to a
11relatively fast 8192Hz, in increments of powers of two. These signals
12are reported by interrupt number 8. (Oh! So *that* is what IRQ 8 is
13for...) It can also function as a 24hr alarm, raising IRQ 8 when the
14alarm goes off. The alarm can also be programmed to only check any
15subset of the three programmable values, meaning that it could be set to
16ring on the 30th second of the 30th minute of every hour, for example.
17The clock can also be set to generate an interrupt upon every clock
18update, thus generating a 1Hz signal.
19
20The interrupts are reported via /dev/rtc (major 10, minor 135, read only
21character device) in the form of an unsigned long. The low byte contains
22the type of interrupt (update-done, alarm-rang, or periodic) that was
23raised, and the remaining bytes contain the number of interrupts since
24the last read. Status information is reported through the pseudo-file
25/proc/driver/rtc if the /proc filesystem was enabled. The driver has
26built in locking so that only one process is allowed to have the /dev/rtc
27interface open at a time.
28
29A user process can monitor these interrupts by doing a read(2) or a
30select(2) on /dev/rtc -- either will block/stop the user process until
31the next interrupt is received. This is useful for things like
32reasonably high frequency data acquisition where one doesn't want to
33burn up 100% CPU by polling gettimeofday etc. etc.
34
35At high frequencies, or under high loads, the user process should check
36the number of interrupts received since the last read to determine if
37there has been any interrupt "pileup" so to speak. Just for reference, a
38typical 486-33 running a tight read loop on /dev/rtc will start to suffer
39occasional interrupt pileup (i.e. > 1 IRQ event since last read) for
40frequencies above 1024Hz. So you really should check the high bytes
41of the value you read, especially at frequencies above that of the
42normal timer interrupt, which is 100Hz.
43
44Programming and/or enabling interrupt frequencies greater than 64Hz is
45only allowed by root. This is perhaps a bit conservative, but we don't want
46an evil user generating lots of IRQs on a slow 386sx-16, where it might have
Jean Delvare9be05b52006-06-25 05:48:23 -070047a negative impact on performance. This 64Hz limit can be changed by writing
48a different value to /proc/sys/dev/rtc/max-user-freq. Note that the
49interrupt handler is only a few lines of code to minimize any possibility
50of this effect.
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52Also, if the kernel time is synchronized with an external source, the
53kernel will write the time back to the CMOS clock every 11 minutes. In
54the process of doing this, the kernel briefly turns off RTC periodic
55interrupts, so be aware of this if you are doing serious work. If you
56don't synchronize the kernel time with an external source (via ntp or
57whatever) then the kernel will keep its hands off the RTC, allowing you
58exclusive access to the device for your applications.
59
60The alarm and/or interrupt frequency are programmed into the RTC via
61various ioctl(2) calls as listed in ./include/linux/rtc.h
62Rather than write 50 pages describing the ioctl() and so on, it is
63perhaps more useful to include a small test program that demonstrates
64how to use them, and demonstrates the features of the driver. This is
65probably a lot more useful to people interested in writing applications
66that will be using this driver.
67
68 Paul Gortmaker
69
70-------------------- 8< ---------------- 8< -----------------------------
71
72/*
73 * Real Time Clock Driver Test/Example Program
74 *
75 * Compile with:
76 * gcc -s -Wall -Wstrict-prototypes rtctest.c -o rtctest
77 *
78 * Copyright (C) 1996, Paul Gortmaker.
79 *
80 * Released under the GNU General Public License, version 2,
81 * included herein by reference.
82 *
83 */
84
85#include <stdio.h>
Jean Delvare9be05b52006-06-25 05:48:23 -070086#include <stdlib.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#include <linux/rtc.h>
88#include <sys/ioctl.h>
89#include <sys/time.h>
90#include <sys/types.h>
91#include <fcntl.h>
92#include <unistd.h>
93#include <errno.h>
94
95int main(void) {
96
97int i, fd, retval, irqcount = 0;
98unsigned long tmp, data;
99struct rtc_time rtc_tm;
100
101fd = open ("/dev/rtc", O_RDONLY);
102
103if (fd == -1) {
104 perror("/dev/rtc");
105 exit(errno);
106}
107
108fprintf(stderr, "\n\t\t\tRTC Driver Test Example.\n\n");
109
110/* Turn on update interrupts (one per second) */
111retval = ioctl(fd, RTC_UIE_ON, 0);
112if (retval == -1) {
113 perror("ioctl");
114 exit(errno);
115}
116
117fprintf(stderr, "Counting 5 update (1/sec) interrupts from reading /dev/rtc:");
118fflush(stderr);
119for (i=1; i<6; i++) {
120 /* This read will block */
121 retval = read(fd, &data, sizeof(unsigned long));
122 if (retval == -1) {
123 perror("read");
124 exit(errno);
125 }
126 fprintf(stderr, " %d",i);
127 fflush(stderr);
128 irqcount++;
129}
130
131fprintf(stderr, "\nAgain, from using select(2) on /dev/rtc:");
132fflush(stderr);
133for (i=1; i<6; i++) {
134 struct timeval tv = {5, 0}; /* 5 second timeout on select */
135 fd_set readfds;
136
137 FD_ZERO(&readfds);
138 FD_SET(fd, &readfds);
139 /* The select will wait until an RTC interrupt happens. */
140 retval = select(fd+1, &readfds, NULL, NULL, &tv);
141 if (retval == -1) {
142 perror("select");
143 exit(errno);
144 }
145 /* This read won't block unlike the select-less case above. */
146 retval = read(fd, &data, sizeof(unsigned long));
147 if (retval == -1) {
148 perror("read");
149 exit(errno);
150 }
151 fprintf(stderr, " %d",i);
152 fflush(stderr);
153 irqcount++;
154}
155
156/* Turn off update interrupts */
157retval = ioctl(fd, RTC_UIE_OFF, 0);
158if (retval == -1) {
159 perror("ioctl");
160 exit(errno);
161}
162
163/* Read the RTC time/date */
164retval = ioctl(fd, RTC_RD_TIME, &rtc_tm);
165if (retval == -1) {
166 perror("ioctl");
167 exit(errno);
168}
169
170fprintf(stderr, "\n\nCurrent RTC date/time is %d-%d-%d, %02d:%02d:%02d.\n",
171 rtc_tm.tm_mday, rtc_tm.tm_mon + 1, rtc_tm.tm_year + 1900,
172 rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
173
174/* Set the alarm to 5 sec in the future, and check for rollover */
175rtc_tm.tm_sec += 5;
176if (rtc_tm.tm_sec >= 60) {
177 rtc_tm.tm_sec %= 60;
178 rtc_tm.tm_min++;
179}
180if (rtc_tm.tm_min == 60) {
181 rtc_tm.tm_min = 0;
182 rtc_tm.tm_hour++;
183}
184if (rtc_tm.tm_hour == 24)
185 rtc_tm.tm_hour = 0;
186
187retval = ioctl(fd, RTC_ALM_SET, &rtc_tm);
188if (retval == -1) {
189 perror("ioctl");
190 exit(errno);
191}
192
193/* Read the current alarm settings */
194retval = ioctl(fd, RTC_ALM_READ, &rtc_tm);
195if (retval == -1) {
196 perror("ioctl");
197 exit(errno);
198}
199
200fprintf(stderr, "Alarm time now set to %02d:%02d:%02d.\n",
201 rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
202
203/* Enable alarm interrupts */
204retval = ioctl(fd, RTC_AIE_ON, 0);
205if (retval == -1) {
206 perror("ioctl");
207 exit(errno);
208}
209
210fprintf(stderr, "Waiting 5 seconds for alarm...");
211fflush(stderr);
212/* This blocks until the alarm ring causes an interrupt */
213retval = read(fd, &data, sizeof(unsigned long));
214if (retval == -1) {
215 perror("read");
216 exit(errno);
217}
218irqcount++;
219fprintf(stderr, " okay. Alarm rang.\n");
220
221/* Disable alarm interrupts */
222retval = ioctl(fd, RTC_AIE_OFF, 0);
223if (retval == -1) {
224 perror("ioctl");
225 exit(errno);
226}
227
228/* Read periodic IRQ rate */
229retval = ioctl(fd, RTC_IRQP_READ, &tmp);
230if (retval == -1) {
231 perror("ioctl");
232 exit(errno);
233}
234fprintf(stderr, "\nPeriodic IRQ rate was %ldHz.\n", tmp);
235
236fprintf(stderr, "Counting 20 interrupts at:");
237fflush(stderr);
238
239/* The frequencies 128Hz, 256Hz, ... 8192Hz are only allowed for root. */
240for (tmp=2; tmp<=64; tmp*=2) {
241
242 retval = ioctl(fd, RTC_IRQP_SET, tmp);
243 if (retval == -1) {
244 perror("ioctl");
245 exit(errno);
246 }
247
248 fprintf(stderr, "\n%ldHz:\t", tmp);
249 fflush(stderr);
250
251 /* Enable periodic interrupts */
252 retval = ioctl(fd, RTC_PIE_ON, 0);
253 if (retval == -1) {
254 perror("ioctl");
255 exit(errno);
256 }
257
258 for (i=1; i<21; i++) {
259 /* This blocks */
260 retval = read(fd, &data, sizeof(unsigned long));
261 if (retval == -1) {
262 perror("read");
263 exit(errno);
264 }
265 fprintf(stderr, " %d",i);
266 fflush(stderr);
267 irqcount++;
268 }
269
270 /* Disable periodic interrupts */
271 retval = ioctl(fd, RTC_PIE_OFF, 0);
272 if (retval == -1) {
273 perror("ioctl");
274 exit(errno);
275 }
276}
277
278fprintf(stderr, "\n\n\t\t\t *** Test complete ***\n");
279fprintf(stderr, "\nTyping \"cat /proc/interrupts\" will show %d more events on IRQ 8.\n\n",
280 irqcount);
281
282close(fd);
283return 0;
284
285} /* end main */