~ubuntu-branches/ubuntu/maverick/linux-mvl-dove/maverick

1 by Tim Gardner, Andy Whitcroft, Tim Gardner
[ Andy Whitcroft ]
1
	Real Time Clock (RTC) Drivers for Linux
2
	=======================================
3
4
When Linux developers talk about a "Real Time Clock", they usually mean
5
something that tracks wall clock time and is battery backed so that it
6
works even with system power off.  Such clocks will normally not track
7
the local time zone or daylight savings time -- unless they dual boot
8
with MS-Windows -- but will instead be set to Coordinated Universal Time
9
(UTC, formerly "Greenwich Mean Time").
10
11
The newest non-PC hardware tends to just count seconds, like the time(2)
12
system call reports, but RTCs also very commonly represent time using
13
the Gregorian calendar and 24 hour time, as reported by gmtime(3).
14
15
Linux has two largely-compatible userspace RTC API families you may
16
need to know about:
17
18
    *	/dev/rtc ... is the RTC provided by PC compatible systems,
19
	so it's not very portable to non-x86 systems.
20
21
    *	/dev/rtc0, /dev/rtc1 ... are part of a framework that's
22
	supported by a wide variety of RTC chips on all systems.
23
24
Programmers need to understand that the PC/AT functionality is not
25
always available, and some systems can do much more.  That is, the
26
RTCs use the same API to make requests in both RTC frameworks (using
27
different filenames of course), but the hardware may not offer the
28
same functionality.  For example, not every RTC is hooked up to an
29
IRQ, so they can't all issue alarms; and where standard PC RTCs can
30
only issue an alarm up to 24 hours in the future, other hardware may
31
be able to schedule one any time in the upcoming century.
32
33
34
	Old PC/AT-Compatible driver:  /dev/rtc
35
	--------------------------------------
36
37
All PCs (even Alpha machines) have a Real Time Clock built into them.
38
Usually they are built into the chipset of the computer, but some may
39
actually have a Motorola MC146818 (or clone) on the board. This is the
40
clock that keeps the date and time while your computer is turned off.
41
42
ACPI has standardized that MC146818 functionality, and extended it in
43
a few ways (enabling longer alarm periods, and wake-from-hibernate).
44
That functionality is NOT exposed in the old driver.
45
46
However it can also be used to generate signals from a slow 2Hz to a
47
relatively fast 8192Hz, in increments of powers of two. These signals
48
are reported by interrupt number 8. (Oh! So *that* is what IRQ 8 is
49
for...) It can also function as a 24hr alarm, raising IRQ 8 when the
50
alarm goes off. The alarm can also be programmed to only check any
51
subset of the three programmable values, meaning that it could be set to
52
ring on the 30th second of the 30th minute of every hour, for example.
53
The clock can also be set to generate an interrupt upon every clock
54
update, thus generating a 1Hz signal.
55
56
The interrupts are reported via /dev/rtc (major 10, minor 135, read only
57
character device) in the form of an unsigned long. The low byte contains
58
the type of interrupt (update-done, alarm-rang, or periodic) that was
59
raised, and the remaining bytes contain the number of interrupts since
60
the last read.  Status information is reported through the pseudo-file
61
/proc/driver/rtc if the /proc filesystem was enabled.  The driver has
62
built in locking so that only one process is allowed to have the /dev/rtc
63
interface open at a time.
64
65
A user process can monitor these interrupts by doing a read(2) or a
66
select(2) on /dev/rtc -- either will block/stop the user process until
67
the next interrupt is received. This is useful for things like
68
reasonably high frequency data acquisition where one doesn't want to
69
burn up 100% CPU by polling gettimeofday etc. etc.
70
71
At high frequencies, or under high loads, the user process should check
72
the number of interrupts received since the last read to determine if
73
there has been any interrupt "pileup" so to speak. Just for reference, a
74
typical 486-33 running a tight read loop on /dev/rtc will start to suffer
75
occasional interrupt pileup (i.e. > 1 IRQ event since last read) for
76
frequencies above 1024Hz. So you really should check the high bytes
77
of the value you read, especially at frequencies above that of the
78
normal timer interrupt, which is 100Hz.
79
80
Programming and/or enabling interrupt frequencies greater than 64Hz is
81
only allowed by root. This is perhaps a bit conservative, but we don't want
82
an evil user generating lots of IRQs on a slow 386sx-16, where it might have
83
a negative impact on performance. This 64Hz limit can be changed by writing
84
a different value to /proc/sys/dev/rtc/max-user-freq. Note that the
85
interrupt handler is only a few lines of code to minimize any possibility
86
of this effect.
87
88
Also, if the kernel time is synchronized with an external source, the 
89
kernel will write the time back to the CMOS clock every 11 minutes. In 
90
the process of doing this, the kernel briefly turns off RTC periodic 
91
interrupts, so be aware of this if you are doing serious work. If you
92
don't synchronize the kernel time with an external source (via ntp or
93
whatever) then the kernel will keep its hands off the RTC, allowing you
94
exclusive access to the device for your applications.
95
96
The alarm and/or interrupt frequency are programmed into the RTC via
97
various ioctl(2) calls as listed in ./include/linux/rtc.h
98
Rather than write 50 pages describing the ioctl() and so on, it is
99
perhaps more useful to include a small test program that demonstrates
100
how to use them, and demonstrates the features of the driver. This is
101
probably a lot more useful to people interested in writing applications
102
that will be using this driver.  See the code at the end of this document.
103
104
(The original /dev/rtc driver was written by Paul Gortmaker.)
105
106
107
	New portable "RTC Class" drivers:  /dev/rtcN
108
	--------------------------------------------
109
110
Because Linux supports many non-ACPI and non-PC platforms, some of which
111
have more than one RTC style clock, it needed a more portable solution
112
than expecting a single battery-backed MC146818 clone on every system.
113
Accordingly, a new "RTC Class" framework has been defined.  It offers
114
three different userspace interfaces:
115
116
    *	/dev/rtcN ... much the same as the older /dev/rtc interface
117
118
    *	/sys/class/rtc/rtcN ... sysfs attributes support readonly
119
	access to some RTC attributes.
120
121
    *	/proc/driver/rtc ... the first RTC (rtc0) may expose itself
122
	using a procfs interface.  More information is (currently) shown
123
	here than through sysfs.
124
125
The RTC Class framework supports a wide variety of RTCs, ranging from those
126
integrated into embeddable system-on-chip (SOC) processors to discrete chips
127
using I2C, SPI, or some other bus to communicate with the host CPU.  There's
128
even support for PC-style RTCs ... including the features exposed on newer PCs
129
through ACPI.
130
131
The new framework also removes the "one RTC per system" restriction.  For
132
example, maybe the low-power battery-backed RTC is a discrete I2C chip, but
133
a high functionality RTC is integrated into the SOC.  That system might read
134
the system clock from the discrete RTC, but use the integrated one for all
135
other tasks, because of its greater functionality.
136
137
SYSFS INTERFACE
20 by Andy Whitcroft, Eric Miao, Upstream changes
[ Eric Miao ]
138
---------------
139
140
The sysfs interface under /sys/class/rtc/rtcN provides access to various
141
rtc attributes without requiring the use of ioctls. All dates and times
142
are in the RTC's timezone, rather than in system time.
143
144
date:  	   	 RTC-provided date
145
hctosys:   	 1 if the RTC provided the system time at boot via the
146
		 CONFIG_RTC_HCTOSYS kernel option, 0 otherwise
147
max_user_freq:	 The maximum interrupt rate an unprivileged user may request
148
		 from this RTC.
149
name:		 The name of the RTC corresponding to this sysfs directory
150
since_epoch:	 The number of seconds since the epoch according to the RTC
151
time:		 RTC-provided time
152
wakealarm:	 The time at which the clock will generate a system wakeup
153
		 event. This is a one shot wakeup event, so must be reset
154
		 after wake if a daily wakeup is required. Format is either
155
		 seconds since the epoch or, if there's a leading +, seconds
156
		 in the future.
157
158
IOCTL INTERFACE
159
---------------
160
161
The ioctl() calls supported by /dev/rtc are also supported by the RTC class
1 by Tim Gardner, Andy Whitcroft, Tim Gardner
[ Andy Whitcroft ]
162
framework.  However, because the chips and systems are not standardized,
163
some PC/AT functionality might not be provided.  And in the same way, some
164
newer features -- including those enabled by ACPI -- are exposed by the
165
RTC class framework, but can't be supported by the older driver.
166
167
    *	RTC_RD_TIME, RTC_SET_TIME ... every RTC supports at least reading
168
	time, returning the result as a Gregorian calendar date and 24 hour
169
	wall clock time.  To be most useful, this time may also be updated.
170
171
    *	RTC_AIE_ON, RTC_AIE_OFF, RTC_ALM_SET, RTC_ALM_READ ... when the RTC
172
	is connected to an IRQ line, it can often issue an alarm IRQ up to
173
	24 hours in the future.  (Use RTC_WKALM_* by preference.)
174
175
    *	RTC_WKALM_SET, RTC_WKALM_RD ... RTCs that can issue alarms beyond
176
	the next 24 hours use a slightly more powerful API, which supports
177
	setting the longer alarm time and enabling its IRQ using a single
178
	request (using the same model as EFI firmware).
179
180
    *	RTC_UIE_ON, RTC_UIE_OFF ... if the RTC offers IRQs, it probably
181
	also offers update IRQs whenever the "seconds" counter changes.
182
	If needed, the RTC framework can emulate this mechanism.
183
184
    *	RTC_PIE_ON, RTC_PIE_OFF, RTC_IRQP_SET, RTC_IRQP_READ ... another
185
	feature often accessible with an IRQ line is a periodic IRQ, issued
186
	at settable frequencies (usually 2^N Hz).
187
188
In many cases, the RTC alarm can be a system wake event, used to force
189
Linux out of a low power sleep state (or hibernation) back to a fully
190
operational state.  For example, a system could enter a deep power saving
191
state until it's time to execute some scheduled tasks.
192
193
Note that many of these ioctls need not actually be implemented by your
194
driver.  The common rtc-dev interface handles many of these nicely if your
195
driver returns ENOIOCTLCMD.  Some common examples:
196
197
    *	RTC_RD_TIME, RTC_SET_TIME: the read_time/set_time functions will be
198
	called with appropriate values.
199
200
    *	RTC_ALM_SET, RTC_ALM_READ, RTC_WKALM_SET, RTC_WKALM_RD: the
201
	set_alarm/read_alarm functions will be called.
202
203
    *	RTC_IRQP_SET, RTC_IRQP_READ: the irq_set_freq function will be called
204
	to set the frequency while the framework will handle the read for you
205
	since the frequency is stored in the irq_freq member of the rtc_device
206
	structure.  Your driver needs to initialize the irq_freq member during
207
	init.  Make sure you check the requested frequency is in range of your
208
	hardware in the irq_set_freq function.  If it isn't, return -EINVAL.  If
209
	you cannot actually change the frequency, do not define irq_set_freq.
210
211
    *	RTC_PIE_ON, RTC_PIE_OFF: the irq_set_state function will be called.
20 by Andy Whitcroft, Eric Miao, Upstream changes
[ Eric Miao ]
212
213
If all else fails, check out the rtc-test.c driver!
1 by Tim Gardner, Andy Whitcroft, Tim Gardner
[ Andy Whitcroft ]
214
215
216
-------------------- 8< ---------------- 8< -----------------------------
217
218
/*
219
 *      Real Time Clock Driver Test/Example Program
220
 *
221
 *      Compile with:
222
 *		     gcc -s -Wall -Wstrict-prototypes rtctest.c -o rtctest
223
 *
224
 *      Copyright (C) 1996, Paul Gortmaker.
225
 *
226
 *      Released under the GNU General Public License, version 2,
227
 *      included herein by reference.
228
 *
229
 */
230
231
#include <stdio.h>
232
#include <linux/rtc.h>
233
#include <sys/ioctl.h>
234
#include <sys/time.h>
235
#include <sys/types.h>
236
#include <fcntl.h>
237
#include <unistd.h>
238
#include <stdlib.h>
239
#include <errno.h>
240
241
242
/*
243
 * This expects the new RTC class driver framework, working with
244
 * clocks that will often not be clones of what the PC-AT had.
245
 * Use the command line to specify another RTC if you need one.
246
 */
247
static const char default_rtc[] = "/dev/rtc0";
248
249
250
int main(int argc, char **argv)
251
{
252
	int i, fd, retval, irqcount = 0;
253
	unsigned long tmp, data;
254
	struct rtc_time rtc_tm;
255
	const char *rtc = default_rtc;
256
257
	switch (argc) {
258
	case 2:
259
		rtc = argv[1];
260
		/* FALLTHROUGH */
261
	case 1:
262
		break;
263
	default:
264
		fprintf(stderr, "usage:  rtctest [rtcdev]\n");
265
		return 1;
266
	}
267
268
	fd = open(rtc, O_RDONLY);
269
270
	if (fd ==  -1) {
271
		perror(rtc);
272
		exit(errno);
273
	}
274
275
	fprintf(stderr, "\n\t\t\tRTC Driver Test Example.\n\n");
276
277
	/* Turn on update interrupts (one per second) */
278
	retval = ioctl(fd, RTC_UIE_ON, 0);
279
	if (retval == -1) {
280
		if (errno == ENOTTY) {
281
			fprintf(stderr,
282
				"\n...Update IRQs not supported.\n");
283
			goto test_READ;
284
		}
285
		perror("RTC_UIE_ON ioctl");
286
		exit(errno);
287
	}
288
289
	fprintf(stderr, "Counting 5 update (1/sec) interrupts from reading %s:",
290
			rtc);
291
	fflush(stderr);
292
	for (i=1; i<6; i++) {
293
		/* This read will block */
294
		retval = read(fd, &data, sizeof(unsigned long));
295
		if (retval == -1) {
296
			perror("read");
297
			exit(errno);
298
		}
299
		fprintf(stderr, " %d",i);
300
		fflush(stderr);
301
		irqcount++;
302
	}
303
304
	fprintf(stderr, "\nAgain, from using select(2) on /dev/rtc:");
305
	fflush(stderr);
306
	for (i=1; i<6; i++) {
307
		struct timeval tv = {5, 0};     /* 5 second timeout on select */
308
		fd_set readfds;
309
310
		FD_ZERO(&readfds);
311
		FD_SET(fd, &readfds);
312
		/* The select will wait until an RTC interrupt happens. */
313
		retval = select(fd+1, &readfds, NULL, NULL, &tv);
314
		if (retval == -1) {
315
		        perror("select");
316
		        exit(errno);
317
		}
318
		/* This read won't block unlike the select-less case above. */
319
		retval = read(fd, &data, sizeof(unsigned long));
320
		if (retval == -1) {
321
		        perror("read");
322
		        exit(errno);
323
		}
324
		fprintf(stderr, " %d",i);
325
		fflush(stderr);
326
		irqcount++;
327
	}
328
329
	/* Turn off update interrupts */
330
	retval = ioctl(fd, RTC_UIE_OFF, 0);
331
	if (retval == -1) {
332
		perror("RTC_UIE_OFF ioctl");
333
		exit(errno);
334
	}
335
336
test_READ:
337
	/* Read the RTC time/date */
338
	retval = ioctl(fd, RTC_RD_TIME, &rtc_tm);
339
	if (retval == -1) {
340
		perror("RTC_RD_TIME ioctl");
341
		exit(errno);
342
	}
343
344
	fprintf(stderr, "\n\nCurrent RTC date/time is %d-%d-%d, %02d:%02d:%02d.\n",
345
		rtc_tm.tm_mday, rtc_tm.tm_mon + 1, rtc_tm.tm_year + 1900,
346
		rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
347
348
	/* Set the alarm to 5 sec in the future, and check for rollover */
349
	rtc_tm.tm_sec += 5;
350
	if (rtc_tm.tm_sec >= 60) {
351
		rtc_tm.tm_sec %= 60;
352
		rtc_tm.tm_min++;
353
	}
354
	if (rtc_tm.tm_min == 60) {
355
		rtc_tm.tm_min = 0;
356
		rtc_tm.tm_hour++;
357
	}
358
	if (rtc_tm.tm_hour == 24)
359
		rtc_tm.tm_hour = 0;
360
361
	retval = ioctl(fd, RTC_ALM_SET, &rtc_tm);
362
	if (retval == -1) {
363
		if (errno == ENOTTY) {
364
			fprintf(stderr,
365
				"\n...Alarm IRQs not supported.\n");
366
			goto test_PIE;
367
		}
368
		perror("RTC_ALM_SET ioctl");
369
		exit(errno);
370
	}
371
372
	/* Read the current alarm settings */
373
	retval = ioctl(fd, RTC_ALM_READ, &rtc_tm);
374
	if (retval == -1) {
375
		perror("RTC_ALM_READ ioctl");
376
		exit(errno);
377
	}
378
379
	fprintf(stderr, "Alarm time now set to %02d:%02d:%02d.\n",
380
		rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
381
382
	/* Enable alarm interrupts */
383
	retval = ioctl(fd, RTC_AIE_ON, 0);
384
	if (retval == -1) {
385
		perror("RTC_AIE_ON ioctl");
386
		exit(errno);
387
	}
388
389
	fprintf(stderr, "Waiting 5 seconds for alarm...");
390
	fflush(stderr);
391
	/* This blocks until the alarm ring causes an interrupt */
392
	retval = read(fd, &data, sizeof(unsigned long));
393
	if (retval == -1) {
394
		perror("read");
395
		exit(errno);
396
	}
397
	irqcount++;
398
	fprintf(stderr, " okay. Alarm rang.\n");
399
400
	/* Disable alarm interrupts */
401
	retval = ioctl(fd, RTC_AIE_OFF, 0);
402
	if (retval == -1) {
403
		perror("RTC_AIE_OFF ioctl");
404
		exit(errno);
405
	}
406
407
test_PIE:
408
	/* Read periodic IRQ rate */
409
	retval = ioctl(fd, RTC_IRQP_READ, &tmp);
410
	if (retval == -1) {
411
		/* not all RTCs support periodic IRQs */
412
		if (errno == ENOTTY) {
413
			fprintf(stderr, "\nNo periodic IRQ support\n");
414
			goto done;
415
		}
416
		perror("RTC_IRQP_READ ioctl");
417
		exit(errno);
418
	}
419
	fprintf(stderr, "\nPeriodic IRQ rate is %ldHz.\n", tmp);
420
421
	fprintf(stderr, "Counting 20 interrupts at:");
422
	fflush(stderr);
423
424
	/* The frequencies 128Hz, 256Hz, ... 8192Hz are only allowed for root. */
425
	for (tmp=2; tmp<=64; tmp*=2) {
426
427
		retval = ioctl(fd, RTC_IRQP_SET, tmp);
428
		if (retval == -1) {
429
			/* not all RTCs can change their periodic IRQ rate */
430
			if (errno == ENOTTY) {
431
				fprintf(stderr,
432
					"\n...Periodic IRQ rate is fixed\n");
433
				goto done;
434
			}
435
			perror("RTC_IRQP_SET ioctl");
436
			exit(errno);
437
		}
438
439
		fprintf(stderr, "\n%ldHz:\t", tmp);
440
		fflush(stderr);
441
442
		/* Enable periodic interrupts */
443
		retval = ioctl(fd, RTC_PIE_ON, 0);
444
		if (retval == -1) {
445
			perror("RTC_PIE_ON ioctl");
446
			exit(errno);
447
		}
448
449
		for (i=1; i<21; i++) {
450
			/* This blocks */
451
			retval = read(fd, &data, sizeof(unsigned long));
452
			if (retval == -1) {
453
				perror("read");
454
				exit(errno);
455
			}
456
			fprintf(stderr, " %d",i);
457
			fflush(stderr);
458
			irqcount++;
459
		}
460
461
		/* Disable periodic interrupts */
462
		retval = ioctl(fd, RTC_PIE_OFF, 0);
463
		if (retval == -1) {
464
			perror("RTC_PIE_OFF ioctl");
465
			exit(errno);
466
		}
467
	}
468
469
done:
470
	fprintf(stderr, "\n\n\t\t\t *** Test complete ***\n");
471
472
	close(fd);
473
474
	return 0;
475
}
476