summaryrefslogtreecommitdiffstats
path: root/arch/sh/boards/landisk/rtc.c
blob: 35ba726a097962718206fa62c53134c0fb035715 (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
/*
 * arch/sh/boards/landisk/rtc.c --  RTC support
 *
 *  Copyright (C) 2000  Philipp Rumpf <prumpf@tux.org>
 *  Copyright (C) 1999  Tetsuya Okada & Niibe Yutaka
 */
/*
 * modifed by kogiidena
 * 2005.09.16
 */

#include <linux/config.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/time.h>
#include <linux/delay.h>
#include <linux/spinlock.h>
#include <linux/bcd.h>
#include <asm/rtc.h>

extern spinlock_t rtc_lock;

extern void
rs5c313_set_cmos_time(unsigned int BCD_yr, unsigned int BCD_mon,
		      unsigned int BCD_day, unsigned int BCD_hr,
		      unsigned int BCD_min, unsigned int BCD_sec);

extern unsigned long
rs5c313_get_cmos_time(unsigned int *BCD_yr, unsigned int *BCD_mon,
		      unsigned int *BCD_day, unsigned int *BCD_hr,
		      unsigned int *BCD_min, unsigned int *BCD_sec);

void landisk_rtc_gettimeofday(struct timespec *tv)
{
	unsigned int BCD_yr, BCD_mon, BCD_day, BCD_hr, BCD_min, BCD_sec;
	unsigned long flags;

	spin_lock_irqsave(&rtc_lock, flags);
	tv->tv_sec = rs5c313_get_cmos_time
	    (&BCD_yr, &BCD_mon, &BCD_day, &BCD_hr, &BCD_min, &BCD_sec);
	tv->tv_nsec = 0;
	spin_unlock_irqrestore(&rtc_lock, flags);
}

int landisk_rtc_settimeofday(const time_t secs)
{
	int retval = 0;
	int real_seconds, real_minutes, cmos_minutes;
	unsigned long flags;
	unsigned long nowtime = secs;
	unsigned int BCD_yr, BCD_mon, BCD_day, BCD_hr, BCD_min, BCD_sec;

	spin_lock_irqsave(&rtc_lock, flags);

	rs5c313_get_cmos_time
	  (&BCD_yr, &BCD_mon, &BCD_day, &BCD_hr, &BCD_min, &BCD_sec);
	cmos_minutes = BCD_min;
	BCD_TO_BIN(cmos_minutes);

	/*
	 * since we're only adjusting minutes and seconds,
	 * don't interfere with hour overflow. This avoids
	 * messing with unknown time zones but requires your
	 * RTC not to be off by more than 15 minutes
	 */
	real_seconds = nowtime % 60;
	real_minutes = nowtime / 60;
	if (((abs(real_minutes - cmos_minutes) + 15) / 30) & 1)
		real_minutes += 30;	/* correct for half hour time zone */
	real_minutes %= 60;

	if (abs(real_minutes - cmos_minutes) < 30) {
		BIN_TO_BCD(real_seconds);
		BIN_TO_BCD(real_minutes);
		rs5c313_set_cmos_time(BCD_yr, BCD_mon, BCD_day, BCD_hr,
				      real_minutes, real_seconds);
	} else {
		printk(KERN_WARNING
		       "set_rtc_time: can't update from %d to %d\n",
		       cmos_minutes, real_minutes);
		retval = -1;
	}

	spin_unlock_irqrestore(&rtc_lock, flags);
	return retval;
}

void landisk_time_init(void)
{
	rtc_sh_get_time = landisk_rtc_gettimeofday;
	rtc_sh_set_time = landisk_rtc_settimeofday;
}
OpenPOWER on IntegriCloud