1 // SPDX-License-Identifier: GPL-2.0
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
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>
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>
26 #include <asm/nvram.h>
28 #include <asm/sections.h>
31 #include <platforms/chrp/chrp.h>
33 extern spinlock_t rtc_lock
;
35 #define NVRAM_AS0 0x74
36 #define NVRAM_AS1 0x75
37 #define NVRAM_DATA 0x77
39 static int nvram_as1
= NVRAM_AS1
;
40 static int nvram_as0
= NVRAM_AS0
;
41 static int nvram_data
= NVRAM_DATA
;
43 long __init
chrp_time_init(void)
45 struct device_node
*rtcs
;
49 rtcs
= of_find_compatible_node(NULL
, "rtc", "pnpPNP,b00");
51 rtcs
= of_find_compatible_node(NULL
, "rtc", "ds1385-rtc");
54 if (of_address_to_resource(rtcs
, 0, &r
)) {
63 nvram_data
= base
+ 1;
68 static int chrp_cmos_clock_read(int addr
)
71 outb(addr
>>8, nvram_as1
);
72 outb(addr
, nvram_as0
);
73 return (inb(nvram_data
));
76 static void chrp_cmos_clock_write(unsigned long val
, int addr
)
79 outb(addr
>>8, nvram_as1
);
80 outb(addr
, nvram_as0
);
81 outb(val
, nvram_data
);
86 * Set the hardware clock. -- Cort
88 int chrp_set_rtc_time(struct rtc_time
*tmarg
)
90 unsigned char save_control
, save_freq_select
;
91 struct rtc_time tm
= *tmarg
;
95 save_control
= chrp_cmos_clock_read(RTC_CONTROL
); /* tell the clock it's being set */
97 chrp_cmos_clock_write((save_control
|RTC_SET
), RTC_CONTROL
);
99 save_freq_select
= chrp_cmos_clock_read(RTC_FREQ_SELECT
); /* stop and reset prescaler */
101 chrp_cmos_clock_write((save_freq_select
|RTC_DIV_RESET2
), RTC_FREQ_SELECT
);
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
);
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
);
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
125 chrp_cmos_clock_write(save_control
, RTC_CONTROL
);
126 chrp_cmos_clock_write(save_freq_select
, RTC_FREQ_SELECT
);
128 spin_unlock(&rtc_lock
);
132 void chrp_get_rtc_time(struct rtc_time
*tm
)
134 unsigned int year
, mon
, day
, hour
, min
, sec
;
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
));
145 if (!(chrp_cmos_clock_read(RTC_CONTROL
) & RTC_DM_BINARY
) || RTC_ALWAYS_BCD
) {
148 hour
= bcd2bin(hour
);
151 year
= bcd2bin(year
);