Annotation of sys/arch/aviion/stand/libsa/clock.c, Revision 1.1.1.1
1.1 nbrk 1: /* $OpenBSD: clock.c,v 1.1 2006/05/16 22:48:18 miod Exp $ */
2:
3:
4: #include <sys/types.h>
5: #include <machine/prom.h>
6:
7: #include "stand.h"
8: #include "libsa.h"
9:
10: #include "nvramreg.h"
11:
12: /*
13: * BCD to decimal and decimal to BCD.
14: */
15: #define FROMBCD(x) (((x) >> 4) * 10 + ((x) & 0xf))
16: #define TOBCD(x) (((x) / 10 * 16) + ((x) % 10))
17:
18: #define SECDAY (24 * 60 * 60)
19: #define SECYR (SECDAY * 365)
20: #define LEAPYEAR(y) (((y) & 3) == 0)
21:
22: /*
23: * This code is defunct after 2068.
24: * Will Unix still be here then??
25: */
26: const int dayyr[12] =
27: { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
28:
29: u_long
30: chiptotime(sec, min, hour, day, mon, year)
31: int sec, min, hour, day, mon, year;
32: {
33: int days, yr;
34:
35: sec = FROMBCD(sec);
36: min = FROMBCD(min);
37: hour = FROMBCD(hour);
38: day = FROMBCD(day);
39: mon = FROMBCD(mon);
40: year = FROMBCD(year) + YEAR0;
41:
42: /* simple sanity checks */
43: if (year > 164 || mon < 1 || mon > 12 || day < 1 || day > 31)
44: return (0);
45: yr = 70;
46: days = 0;
47:
48: if (year < 70) {
49: for (; yr < year; yr++)
50: days += LEAPYEAR(yr) ? 366 : 365;
51: yr = 0;
52: }
53:
54: for (; yr < year; yr++)
55: days += LEAPYEAR(yr) ? 366 : 365;
56:
57: days += dayyr[mon - 1] + day - 1;
58:
59: if (LEAPYEAR(yr) && mon > 2)
60: days++;
61:
62: /* now have days since Jan 1, 1970; the rest is easy... */
63: return (days * SECDAY + hour * 3600 + min * 60 + sec);
64: }
65:
66: time_t
67: getsecs()
68: {
69: int sec, min, hour, day, mon, year;
70: #define TOD_BASE (0xfff80000 + AV400_NVRAM_TOD_OFF)
71:
72: *(volatile u_int32_t *)(TOD_BASE + (CLK_CSR << 2)) = CLK_READ |
73: *(volatile u_int32_t *)(TOD_BASE + (CLK_CSR << 2));
74: sec = *(volatile u_int32_t *)(TOD_BASE + (CLK_SEC << 2)) & 0xff;
75: min = *(volatile u_int32_t *)(TOD_BASE + (CLK_MIN << 2)) & 0xff;
76: hour = *(volatile u_int32_t *)(TOD_BASE + (CLK_HOUR << 2)) & 0xff;
77: day = *(volatile u_int32_t *)(TOD_BASE + (CLK_DAY << 2)) & 0xff;
78: mon = *(volatile u_int32_t *)(TOD_BASE + (CLK_MONTH << 2)) & 0xff;
79: year = *(volatile u_int32_t *)(TOD_BASE + (CLK_YEAR << 2)) & 0xff;
80: *(volatile u_int32_t *)(TOD_BASE + (CLK_CSR << 2)) = ~CLK_READ &
81: *(volatile u_int32_t *)(TOD_BASE + (CLK_CSR << 2));
82:
83: return (chiptotime(sec, min, hour, day, mon, year));
84: }
CVSweb