clock.c revision 1.10
1/*	$NetBSD: clock.c,v 1.10 2014/11/17 02:15:48 christos Exp $ */
2
3#include <sys/types.h>
4#include <machine/prom.h>
5
6#include <lib/libsa/stand.h>
7#include <lib/libsa/net.h>
8#include "libsa.h"
9
10/*
11 * BCD to decimal and decimal to BCD.
12 */
13#define FROMBCD(x)      (int)((((unsigned int)(x)) >> 4) * 10 +\
14				(((unsigned int)(x)) & 0xf))
15#define TOBCD(x)        (int)((((unsigned int)(x)) / 10 * 16) +\
16				(((unsigned int)(x)) % 10))
17
18#define YEAR0		68
19
20/*
21 * This code is defunct after 2068.
22 * Will Unix still be here then??
23 */
24const short dayyr[12] =
25    {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
26
27u_long
28chiptotime(int sec, int min, int hour, int day, int mon, int year)
29{
30	int days, yr;
31
32	sec = FROMBCD(sec);
33	min = FROMBCD(min);
34	hour = FROMBCD(hour);
35	day = FROMBCD(day);
36	mon = FROMBCD(mon);
37	year = FROMBCD(year) + YEAR0;
38	if (year < 70)
39		year = 70;
40
41	/* simple sanity checks */
42	if (year < 70 || mon < 1 || mon > 12 || day < 1 || day > 31)
43		return (0);
44	days = 0;
45	for (yr = 70; yr < year; yr++)
46		days += days_per_year(yr);
47	days += dayyr[mon - 1] + day - 1;
48	if (is_leap_year(yr) && mon > 2)
49		days++;
50	/* now have days since Jan 1, 1970; the rest is easy... */
51	return days * SECS_PER_DAY + hour * SECS_PER_HOUR
52	    + min * SECS_PER_MINUTE + sec;
53}
54
55satime_t
56getsecs(void)
57{
58	struct mvmeprom_time m;
59
60	mvmeprom_rtc_rd(&m);
61	return chiptotime(m.sec_BCD, m.min_BCD, m.hour_BCD, m.day_BCD,
62	    m.month_BCD, m.year_BCD);
63}
64