xref: /openbmc/linux/arch/powerpc/platforms/chrp/time.c (revision b8d312aa)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  Copyright (C) 1991, 1992, 1995  Linus Torvalds
4  *
5  * Adapted for PowerPC (PReP) by Gary Thomas
6  * Modified by Cort Dougan (cort@cs.nmt.edu).
7  * Copied and modified from arch/i386/kernel/time.c
8  *
9  */
10 #include <linux/errno.h>
11 #include <linux/sched.h>
12 #include <linux/kernel.h>
13 #include <linux/param.h>
14 #include <linux/string.h>
15 #include <linux/mm.h>
16 #include <linux/interrupt.h>
17 #include <linux/time.h>
18 #include <linux/timex.h>
19 #include <linux/kernel_stat.h>
20 #include <linux/mc146818rtc.h>
21 #include <linux/init.h>
22 #include <linux/bcd.h>
23 #include <linux/ioport.h>
24 
25 #include <asm/io.h>
26 #include <asm/nvram.h>
27 #include <asm/prom.h>
28 #include <asm/sections.h>
29 #include <asm/time.h>
30 
31 #include <platforms/chrp/chrp.h>
32 
33 extern spinlock_t rtc_lock;
34 
35 #define NVRAM_AS0  0x74
36 #define NVRAM_AS1  0x75
37 #define NVRAM_DATA 0x77
38 
39 static int nvram_as1 = NVRAM_AS1;
40 static int nvram_as0 = NVRAM_AS0;
41 static int nvram_data = NVRAM_DATA;
42 
43 long __init chrp_time_init(void)
44 {
45 	struct device_node *rtcs;
46 	struct resource r;
47 	int base;
48 
49 	rtcs = of_find_compatible_node(NULL, "rtc", "pnpPNP,b00");
50 	if (rtcs == NULL)
51 		rtcs = of_find_compatible_node(NULL, "rtc", "ds1385-rtc");
52 	if (rtcs == NULL)
53 		return 0;
54 	if (of_address_to_resource(rtcs, 0, &r)) {
55 		of_node_put(rtcs);
56 		return 0;
57 	}
58 	of_node_put(rtcs);
59 
60 	base = r.start;
61 	nvram_as1 = 0;
62 	nvram_as0 = base;
63 	nvram_data = base + 1;
64 
65 	return 0;
66 }
67 
68 static int chrp_cmos_clock_read(int addr)
69 {
70 	if (nvram_as1 != 0)
71 		outb(addr>>8, nvram_as1);
72 	outb(addr, nvram_as0);
73 	return (inb(nvram_data));
74 }
75 
76 static void chrp_cmos_clock_write(unsigned long val, int addr)
77 {
78 	if (nvram_as1 != 0)
79 		outb(addr>>8, nvram_as1);
80 	outb(addr, nvram_as0);
81 	outb(val, nvram_data);
82 	return;
83 }
84 
85 /*
86  * Set the hardware clock. -- Cort
87  */
88 int chrp_set_rtc_time(struct rtc_time *tmarg)
89 {
90 	unsigned char save_control, save_freq_select;
91 	struct rtc_time tm = *tmarg;
92 
93 	spin_lock(&rtc_lock);
94 
95 	save_control = chrp_cmos_clock_read(RTC_CONTROL); /* tell the clock it's being set */
96 
97 	chrp_cmos_clock_write((save_control|RTC_SET), RTC_CONTROL);
98 
99 	save_freq_select = chrp_cmos_clock_read(RTC_FREQ_SELECT); /* stop and reset prescaler */
100 
101 	chrp_cmos_clock_write((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
102 
103 	if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
104 		tm.tm_sec = bin2bcd(tm.tm_sec);
105 		tm.tm_min = bin2bcd(tm.tm_min);
106 		tm.tm_hour = bin2bcd(tm.tm_hour);
107 		tm.tm_mon = bin2bcd(tm.tm_mon);
108 		tm.tm_mday = bin2bcd(tm.tm_mday);
109 		tm.tm_year = bin2bcd(tm.tm_year);
110 	}
111 	chrp_cmos_clock_write(tm.tm_sec,RTC_SECONDS);
112 	chrp_cmos_clock_write(tm.tm_min,RTC_MINUTES);
113 	chrp_cmos_clock_write(tm.tm_hour,RTC_HOURS);
114 	chrp_cmos_clock_write(tm.tm_mon,RTC_MONTH);
115 	chrp_cmos_clock_write(tm.tm_mday,RTC_DAY_OF_MONTH);
116 	chrp_cmos_clock_write(tm.tm_year,RTC_YEAR);
117 
118 	/* The following flags have to be released exactly in this order,
119 	 * otherwise the DS12887 (popular MC146818A clone with integrated
120 	 * battery and quartz) will not reset the oscillator and will not
121 	 * update precisely 500 ms later. You won't find this mentioned in
122 	 * the Dallas Semiconductor data sheets, but who believes data
123 	 * sheets anyway ...                           -- Markus Kuhn
124 	 */
125 	chrp_cmos_clock_write(save_control, RTC_CONTROL);
126 	chrp_cmos_clock_write(save_freq_select, RTC_FREQ_SELECT);
127 
128 	spin_unlock(&rtc_lock);
129 	return 0;
130 }
131 
132 void chrp_get_rtc_time(struct rtc_time *tm)
133 {
134 	unsigned int year, mon, day, hour, min, sec;
135 
136 	do {
137 		sec = chrp_cmos_clock_read(RTC_SECONDS);
138 		min = chrp_cmos_clock_read(RTC_MINUTES);
139 		hour = chrp_cmos_clock_read(RTC_HOURS);
140 		day = chrp_cmos_clock_read(RTC_DAY_OF_MONTH);
141 		mon = chrp_cmos_clock_read(RTC_MONTH);
142 		year = chrp_cmos_clock_read(RTC_YEAR);
143 	} while (sec != chrp_cmos_clock_read(RTC_SECONDS));
144 
145 	if (!(chrp_cmos_clock_read(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
146 		sec = bcd2bin(sec);
147 		min = bcd2bin(min);
148 		hour = bcd2bin(hour);
149 		day = bcd2bin(day);
150 		mon = bcd2bin(mon);
151 		year = bcd2bin(year);
152 	}
153 	if (year < 70)
154 		year += 100;
155 	tm->tm_sec = sec;
156 	tm->tm_min = min;
157 	tm->tm_hour = hour;
158 	tm->tm_mday = day;
159 	tm->tm_mon = mon;
160 	tm->tm_year = year;
161 }
162