summaryrefslogtreecommitdiff
path: root/sys/arch/kbus/stand/sk/clock.c
blob: f0cdb090c27ceac754c4dda5e84fae4c535e19fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <sys/types.h>

#include "clockreg.h"
#include "config.h"
#include "clock.h"

/*
 * BCD to decimal and decimal to BCD.
 */
#define FROM_BCD(x,y)      ((x) + 10 * (y))

#define SECDAY          (24 * 60 * 60)
#define SECYR           (SECDAY * 365)
#define LEAPYEAR(y)     (((y) & 3) == 0)

/*
 * This code is defunct after 2068.
 * Will Unix still be here then??
 */
const short dayyr[12] =
{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};

static  u_long
chiptotime(sec, min, hour, day, mon, year)
	register int sec, min, hour, day, mon, year;
{
	register int days, yr;

	if (year < 70)
		year = 70;

	/* simple sanity checks */
	if (year < 70 || mon < 1 || mon > 12 || day < 1 || day > 31)
		return (0);
	days = 0;
	for (yr = 70; yr < year; yr++)
		days += LEAPYEAR(yr) ? 366 : 365;
	days += dayyr[mon - 1] + day - 1;
	if (LEAPYEAR(yr) && mon > 2)
		days++;
	/* now have days since Jan 1, 1970; the rest is easy... */
	return (days * SECDAY + hour * 3600 + min * 60 + sec);
}

static u_char *rtc_byte = (u_char *) 0x17020000;

static u_char
rtc_read_reg (int reg)
{
  u_char res;

  reg &= 0x0f;
  *rtc_byte = RTC_WRITE_ADDR | reg;
  *rtc_byte = reg;
  *rtc_byte = RTC_READ | 0x0f;
  res = *rtc_byte & 0x0f;
  *rtc_byte = RTC_WRITE_ADDR | 0x0f;
  *rtc_byte = 0x0f;
  return res;
}

time_t
getsecs()
{
  int     sec, min, hour, hourl, hourh, day, mon, year;

  sec = FROM_BCD (rtc_read_reg (RTC_SEC_LOW),
		  rtc_read_reg (RTC_SEC_HIGH));
  min = FROM_BCD (rtc_read_reg (RTC_MIN_LOW),
		  rtc_read_reg (RTC_MIN_HIGH));
  day = FROM_BCD (rtc_read_reg (RTC_DAY_LOW),
		  rtc_read_reg (RTC_DAY_HIGH));
  mon = FROM_BCD (rtc_read_reg (RTC_MON_LOW),
		  rtc_read_reg (RTC_MON_HIGH));
  year = RTC_YEAR_BASE + FROM_BCD (rtc_read_reg (RTC_YEAR_LOW),
				   rtc_read_reg (RTC_YEAR_HIGH));
  hourl = rtc_read_reg (RTC_HOUR_LOW);
  hourh = rtc_read_reg (RTC_HOUR_HIGH);
  if (hourh & RTC_HOUR_PM)
    hour = 12 + (hourh & 1) * 10 + hourl;
  else
    hour = (hourh & 0x03) * 10 + hourl;
  return (chiptotime(sec, min, hour, day, mon, year));
}

void
disp_date (void)
{
  static char *days[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
  int hour, hourl, hourh, day, wday, mon, year;

  wday = rtc_read_reg (RTC_WEEK_DAY);
  day = FROM_BCD (rtc_read_reg (RTC_DAY_LOW),
		  rtc_read_reg (RTC_DAY_HIGH));
  mon = FROM_BCD (rtc_read_reg (RTC_MON_LOW),
		  rtc_read_reg (RTC_MON_HIGH));
  year = RTC_YEAR_BASE + FROM_BCD (rtc_read_reg (RTC_YEAR_LOW),
				   rtc_read_reg (RTC_YEAR_HIGH));
  hourl = rtc_read_reg (RTC_HOUR_LOW);
  hourh = rtc_read_reg (RTC_HOUR_HIGH);

  if (hourh & RTC_HOUR_PM)
    hour = 12 + (hourh & 1) * 10 + hourl;
  else
    hour = (hourh & 0x03) * 10 + hourl;

  printf ("Date: %s %d-%d-%d %d:%d%d:%d%d\n",
	  days[wday],
	  1900 + year, mon, day,
	  hour,
	  rtc_read_reg (RTC_MIN_HIGH),
	  rtc_read_reg (RTC_MIN_LOW),
	  rtc_read_reg (RTC_SEC_HIGH),
	  rtc_read_reg (RTC_SEC_LOW));
}

int
getticks()
{
	return getsecs() * 100;
}