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