xref: /openbmc/linux/arch/sh/boards/mach-sh03/rtc.c (revision f8bade6c9a6213c2c5ba6e5bf32415ecab6e41e5)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2da2014a2SPaul Mundt /*
3da2014a2SPaul Mundt  * linux/arch/sh/boards/sh03/rtc.c -- CTP/PCI-SH03 on-chip RTC support
4da2014a2SPaul Mundt  *
5da2014a2SPaul Mundt  *  Copyright (C) 2004  Saito.K & Jeanne(ksaito@interface.co.jp)
6da2014a2SPaul Mundt  *
7da2014a2SPaul Mundt  */
8da2014a2SPaul Mundt 
9da2014a2SPaul Mundt #include <linux/init.h>
10da2014a2SPaul Mundt #include <linux/kernel.h>
11da2014a2SPaul Mundt #include <linux/sched.h>
12da2014a2SPaul Mundt #include <linux/time.h>
13da2014a2SPaul Mundt #include <linux/bcd.h>
14da2014a2SPaul Mundt #include <linux/spinlock.h>
15*09e81263SArnd Bergmann #include <linux/io.h>
16*09e81263SArnd Bergmann #include <linux/rtc.h>
17*09e81263SArnd Bergmann #include <linux/platform_device.h>
18da2014a2SPaul Mundt 
19da2014a2SPaul Mundt #define RTC_BASE	0xb0000000
20da2014a2SPaul Mundt #define RTC_SEC1	(RTC_BASE + 0)
21da2014a2SPaul Mundt #define RTC_SEC10	(RTC_BASE + 1)
22da2014a2SPaul Mundt #define RTC_MIN1	(RTC_BASE + 2)
23da2014a2SPaul Mundt #define RTC_MIN10	(RTC_BASE + 3)
24da2014a2SPaul Mundt #define RTC_HOU1	(RTC_BASE + 4)
25da2014a2SPaul Mundt #define RTC_HOU10	(RTC_BASE + 5)
26da2014a2SPaul Mundt #define RTC_WEE1	(RTC_BASE + 6)
27da2014a2SPaul Mundt #define RTC_DAY1	(RTC_BASE + 7)
28da2014a2SPaul Mundt #define RTC_DAY10	(RTC_BASE + 8)
29da2014a2SPaul Mundt #define RTC_MON1	(RTC_BASE + 9)
30da2014a2SPaul Mundt #define RTC_MON10	(RTC_BASE + 10)
31da2014a2SPaul Mundt #define RTC_YEA1	(RTC_BASE + 11)
32da2014a2SPaul Mundt #define RTC_YEA10	(RTC_BASE + 12)
33da2014a2SPaul Mundt #define RTC_YEA100	(RTC_BASE + 13)
34da2014a2SPaul Mundt #define RTC_YEA1000	(RTC_BASE + 14)
35da2014a2SPaul Mundt #define RTC_CTL		(RTC_BASE + 15)
36da2014a2SPaul Mundt #define RTC_BUSY	1
37da2014a2SPaul Mundt #define RTC_STOP	2
38da2014a2SPaul Mundt 
39cd1408f2SPaul Mundt static DEFINE_SPINLOCK(sh03_rtc_lock);
40da2014a2SPaul Mundt 
sh03_rtc_gettimeofday(struct device * dev,struct rtc_time * tm)41*09e81263SArnd Bergmann static int sh03_rtc_gettimeofday(struct device *dev, struct rtc_time *tm)
42da2014a2SPaul Mundt {
43da2014a2SPaul Mundt 	unsigned int year, mon, day, hour, min, sec;
44da2014a2SPaul Mundt 
45cd1408f2SPaul Mundt 	spin_lock(&sh03_rtc_lock);
46da2014a2SPaul Mundt  again:
47da2014a2SPaul Mundt 	do {
489d56dd3bSPaul Mundt 		sec  = (__raw_readb(RTC_SEC1) & 0xf) + (__raw_readb(RTC_SEC10) & 0x7) * 10;
499d56dd3bSPaul Mundt 		min  = (__raw_readb(RTC_MIN1) & 0xf) + (__raw_readb(RTC_MIN10) & 0xf) * 10;
509d56dd3bSPaul Mundt 		hour = (__raw_readb(RTC_HOU1) & 0xf) + (__raw_readb(RTC_HOU10) & 0xf) * 10;
519d56dd3bSPaul Mundt 		day  = (__raw_readb(RTC_DAY1) & 0xf) + (__raw_readb(RTC_DAY10) & 0xf) * 10;
529d56dd3bSPaul Mundt 		mon  = (__raw_readb(RTC_MON1) & 0xf) + (__raw_readb(RTC_MON10) & 0xf) * 10;
539d56dd3bSPaul Mundt 		year = (__raw_readb(RTC_YEA1) & 0xf) + (__raw_readb(RTC_YEA10) & 0xf) * 10
549d56dd3bSPaul Mundt 		     + (__raw_readb(RTC_YEA100 ) & 0xf) * 100
559d56dd3bSPaul Mundt 		     + (__raw_readb(RTC_YEA1000) & 0xf) * 1000;
569d56dd3bSPaul Mundt 	} while (sec != (__raw_readb(RTC_SEC1) & 0xf) + (__raw_readb(RTC_SEC10) & 0x7) * 10);
57da2014a2SPaul Mundt 	if (year == 0 || mon < 1 || mon > 12 || day > 31 || day < 1 ||
58da2014a2SPaul Mundt 	    hour > 23 || min > 59 || sec > 59) {
59da2014a2SPaul Mundt 		printk(KERN_ERR
60da2014a2SPaul Mundt 		       "SH-03 RTC: invalid value, resetting to 1 Jan 2000\n");
61da2014a2SPaul Mundt 		printk("year=%d, mon=%d, day=%d, hour=%d, min=%d, sec=%d\n",
62da2014a2SPaul Mundt 		       year, mon, day, hour, min, sec);
63da2014a2SPaul Mundt 
649d56dd3bSPaul Mundt 		__raw_writeb(0, RTC_SEC1); __raw_writeb(0, RTC_SEC10);
659d56dd3bSPaul Mundt 		__raw_writeb(0, RTC_MIN1); __raw_writeb(0, RTC_MIN10);
669d56dd3bSPaul Mundt 		__raw_writeb(0, RTC_HOU1); __raw_writeb(0, RTC_HOU10);
679d56dd3bSPaul Mundt 		__raw_writeb(6, RTC_WEE1);
689d56dd3bSPaul Mundt 		__raw_writeb(1, RTC_DAY1); __raw_writeb(0, RTC_DAY10);
699d56dd3bSPaul Mundt 		__raw_writeb(1, RTC_MON1); __raw_writeb(0, RTC_MON10);
709d56dd3bSPaul Mundt 		__raw_writeb(0, RTC_YEA1); __raw_writeb(0, RTC_YEA10);
719d56dd3bSPaul Mundt 		__raw_writeb(0, RTC_YEA100);
729d56dd3bSPaul Mundt 		__raw_writeb(2, RTC_YEA1000);
739d56dd3bSPaul Mundt 		__raw_writeb(0, RTC_CTL);
74da2014a2SPaul Mundt 		goto again;
75da2014a2SPaul Mundt 	}
76da2014a2SPaul Mundt 
77cd1408f2SPaul Mundt 	spin_unlock(&sh03_rtc_lock);
78*09e81263SArnd Bergmann 
79*09e81263SArnd Bergmann 	tm->tm_sec  = sec;
80*09e81263SArnd Bergmann 	tm->tm_min  = min;
81*09e81263SArnd Bergmann 	tm->tm_hour = hour;
82*09e81263SArnd Bergmann 	tm->tm_mday = day;
83*09e81263SArnd Bergmann 	tm->tm_mon  = mon;
84*09e81263SArnd Bergmann 	tm->tm_year = year - 1900;
85*09e81263SArnd Bergmann 
86*09e81263SArnd Bergmann 	return 0;
87da2014a2SPaul Mundt }
88da2014a2SPaul Mundt 
set_rtc_mmss(struct rtc_time * tm)89*09e81263SArnd Bergmann static int set_rtc_mmss(struct rtc_time *tm)
90da2014a2SPaul Mundt {
91da2014a2SPaul Mundt 	int retval = 0;
92da2014a2SPaul Mundt 	int real_seconds, real_minutes, cmos_minutes;
93da2014a2SPaul Mundt 	int i;
94da2014a2SPaul Mundt 
95da2014a2SPaul Mundt 	/* gets recalled with irq locally disabled */
96cd1408f2SPaul Mundt 	spin_lock(&sh03_rtc_lock);
97da2014a2SPaul Mundt 	for (i = 0 ; i < 1000000 ; i++)	/* may take up to 1 second... */
989d56dd3bSPaul Mundt 		if (!(__raw_readb(RTC_CTL) & RTC_BUSY))
99da2014a2SPaul Mundt 			break;
1009d56dd3bSPaul Mundt 	cmos_minutes = (__raw_readb(RTC_MIN1) & 0xf) + (__raw_readb(RTC_MIN10) & 0xf) * 10;
101*09e81263SArnd Bergmann 	real_seconds = tm->tm_sec;
102*09e81263SArnd Bergmann 	real_minutes = tm->tm_min;
103da2014a2SPaul Mundt 	if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
104da2014a2SPaul Mundt 		real_minutes += 30;		/* correct for half hour time zone */
105da2014a2SPaul Mundt 	real_minutes %= 60;
106da2014a2SPaul Mundt 
107da2014a2SPaul Mundt 	if (abs(real_minutes - cmos_minutes) < 30) {
1089d56dd3bSPaul Mundt 		__raw_writeb(real_seconds % 10, RTC_SEC1);
1099d56dd3bSPaul Mundt 		__raw_writeb(real_seconds / 10, RTC_SEC10);
1109d56dd3bSPaul Mundt 		__raw_writeb(real_minutes % 10, RTC_MIN1);
1119d56dd3bSPaul Mundt 		__raw_writeb(real_minutes / 10, RTC_MIN10);
112da2014a2SPaul Mundt 	} else {
1133e5c1240SStephen Hemminger 		printk_once(KERN_NOTICE
114da2014a2SPaul Mundt 		       "set_rtc_mmss: can't update from %d to %d\n",
115da2014a2SPaul Mundt 		       cmos_minutes, real_minutes);
116*09e81263SArnd Bergmann 		retval = -EINVAL;
117da2014a2SPaul Mundt 	}
118cd1408f2SPaul Mundt 	spin_unlock(&sh03_rtc_lock);
119da2014a2SPaul Mundt 
120da2014a2SPaul Mundt 	return retval;
121da2014a2SPaul Mundt }
122da2014a2SPaul Mundt 
sh03_rtc_settimeofday(struct device * dev,struct rtc_time * tm)123*09e81263SArnd Bergmann int sh03_rtc_settimeofday(struct device *dev, struct rtc_time *tm)
124da2014a2SPaul Mundt {
125*09e81263SArnd Bergmann 	return set_rtc_mmss(tm);
126da2014a2SPaul Mundt }
127da2014a2SPaul Mundt 
128*09e81263SArnd Bergmann static const struct rtc_class_ops rtc_generic_ops = {
129*09e81263SArnd Bergmann 	.read_time = sh03_rtc_gettimeofday,
130*09e81263SArnd Bergmann 	.set_time = sh03_rtc_settimeofday,
131*09e81263SArnd Bergmann };
132*09e81263SArnd Bergmann 
sh03_time_init(void)133*09e81263SArnd Bergmann static int __init sh03_time_init(void)
134da2014a2SPaul Mundt {
135*09e81263SArnd Bergmann 	struct platform_device *pdev;
136*09e81263SArnd Bergmann 
137*09e81263SArnd Bergmann 	pdev = platform_device_register_data(NULL, "rtc-generic", -1,
138*09e81263SArnd Bergmann 					     &rtc_generic_ops,
139*09e81263SArnd Bergmann 					     sizeof(rtc_generic_ops));
140*09e81263SArnd Bergmann 
141*09e81263SArnd Bergmann 	return PTR_ERR_OR_ZERO(pdev);
142da2014a2SPaul Mundt }
143*09e81263SArnd Bergmann arch_initcall(sh03_time_init);
144