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