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