11.11Schristos/*	$NetBSD: clock.c,v 1.11 2014/11/20 16:34:25 christos Exp $ */
21.1Schuck
31.1Schuck#include <sys/types.h>
41.11Schristos#include <dev/clock_subr.h>
51.2Schuck#include <machine/prom.h>
61.1Schuck
71.6Sjunyoung#include <lib/libsa/stand.h>
81.9Stsutsui#include <lib/libsa/net.h>
91.2Schuck#include "libsa.h"
101.1Schuck
111.1Schuck/*
121.1Schuck * BCD to decimal and decimal to BCD.
131.1Schuck */
141.2Schuck#define YEAR0		68
151.1Schuck
161.1Schuck/*
171.1Schuck * This code is defunct after 2068.
181.1Schuck * Will Unix still be here then??
191.1Schuck */
201.1Schuckconst short dayyr[12] =
211.8Stsutsui    {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
221.1Schuck
231.4Sjdoleceku_long
241.5Sjunyoungchiptotime(int sec, int min, int hour, int day, int mon, int year)
251.1Schuck{
261.3Sscw	int days, yr;
271.1Schuck
281.11Schristos	sec = bcdtobin(sec);
291.11Schristos	min = bcdtobin(min);
301.11Schristos	hour = bcdtobin(hour);
311.11Schristos	day = bcdtobin(day);
321.11Schristos	mon = bcdtobin(mon);
331.11Schristos	year = bcdtobin(year) + YEAR0;
341.2Schuck	if (year < 70)
351.2Schuck		year = 70;
361.1Schuck
371.1Schuck	/* simple sanity checks */
381.1Schuck	if (year < 70 || mon < 1 || mon > 12 || day < 1 || day > 31)
391.1Schuck		return (0);
401.1Schuck	days = 0;
411.1Schuck	for (yr = 70; yr < year; yr++)
421.10Schristos		days += days_per_year(yr);
431.1Schuck	days += dayyr[mon - 1] + day - 1;
441.10Schristos	if (is_leap_year(yr) && mon > 2)
451.1Schuck		days++;
461.1Schuck	/* now have days since Jan 1, 1970; the rest is easy... */
471.10Schristos	return days * SECS_PER_DAY + hour * SECS_PER_HOUR
481.10Schristos	    + min * SECS_PER_MINUTE + sec;
491.1Schuck}
501.1Schuck
511.9Stsutsuisatime_t
521.5Sjunyounggetsecs(void)
531.1Schuck{
541.2Schuck	struct mvmeprom_time m;
551.1Schuck
561.2Schuck	mvmeprom_rtc_rd(&m);
571.5Sjunyoung	return chiptotime(m.sec_BCD, m.min_BCD, m.hour_BCD, m.day_BCD,
581.8Stsutsui	    m.month_BCD, m.year_BCD);
591.1Schuck}
60