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