ARM: amba: Make driver_override output consistent with other buses
[linux/fpc-iii.git] / arch / powerpc / platforms / chrp / time.c
blob03d115aaa1916638d01e782326a8f3fc6412f730
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 1991, 1992, 1995 Linus Torvalds
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
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>
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>
31 extern spinlock_t rtc_lock;
33 #define NVRAM_AS0 0x74
34 #define NVRAM_AS1 0x75
35 #define NVRAM_DATA 0x77
37 static int nvram_as1 = NVRAM_AS1;
38 static int nvram_as0 = NVRAM_AS0;
39 static int nvram_data = NVRAM_DATA;
41 long __init chrp_time_init(void)
43 struct device_node *rtcs;
44 struct resource r;
45 int base;
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;
56 of_node_put(rtcs);
58 base = r.start;
59 nvram_as1 = 0;
60 nvram_as0 = base;
61 nvram_data = base + 1;
63 return 0;
66 int chrp_cmos_clock_read(int addr)
68 if (nvram_as1 != 0)
69 outb(addr>>8, nvram_as1);
70 outb(addr, nvram_as0);
71 return (inb(nvram_data));
74 void chrp_cmos_clock_write(unsigned long val, int addr)
76 if (nvram_as1 != 0)
77 outb(addr>>8, nvram_as1);
78 outb(addr, nvram_as0);
79 outb(val, nvram_data);
80 return;
84 * Set the hardware clock. -- Cort
86 int chrp_set_rtc_time(struct rtc_time *tmarg)
88 unsigned char save_control, save_freq_select;
89 struct rtc_time tm = *tmarg;
91 spin_lock(&rtc_lock);
93 save_control = chrp_cmos_clock_read(RTC_CONTROL); /* tell the clock it's being set */
95 chrp_cmos_clock_write((save_control|RTC_SET), RTC_CONTROL);
97 save_freq_select = chrp_cmos_clock_read(RTC_FREQ_SELECT); /* stop and reset prescaler */
99 chrp_cmos_clock_write((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
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);
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);
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
123 chrp_cmos_clock_write(save_control, RTC_CONTROL);
124 chrp_cmos_clock_write(save_freq_select, RTC_FREQ_SELECT);
126 spin_unlock(&rtc_lock);
127 return 0;
130 void chrp_get_rtc_time(struct rtc_time *tm)
132 unsigned int year, mon, day, hour, min, sec;
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));
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);
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;