]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - arch/powerpc/platforms/maple/time.c
[PATCH] ppc32: Make platform devices being able to assign functions
[mirror_ubuntu-artful-kernel.git] / arch / powerpc / platforms / maple / time.c
1 /*
2 * arch/ppc64/kernel/maple_time.c
3 *
4 * (c) Copyright 2004 Benjamin Herrenschmidt (benh@kernel.crashing.org),
5 * IBM Corp.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14 #undef DEBUG
15
16 #include <linux/config.h>
17 #include <linux/errno.h>
18 #include <linux/sched.h>
19 #include <linux/kernel.h>
20 #include <linux/param.h>
21 #include <linux/string.h>
22 #include <linux/mm.h>
23 #include <linux/init.h>
24 #include <linux/time.h>
25 #include <linux/adb.h>
26 #include <linux/pmu.h>
27 #include <linux/interrupt.h>
28 #include <linux/mc146818rtc.h>
29 #include <linux/bcd.h>
30
31 #include <asm/sections.h>
32 #include <asm/prom.h>
33 #include <asm/system.h>
34 #include <asm/io.h>
35 #include <asm/pgtable.h>
36 #include <asm/machdep.h>
37 #include <asm/time.h>
38
39 #include "maple.h"
40
41 #ifdef DEBUG
42 #define DBG(x...) printk(x)
43 #else
44 #define DBG(x...)
45 #endif
46
47 extern void GregorianDay(struct rtc_time * tm);
48
49 static int maple_rtc_addr;
50
51 static int maple_clock_read(int addr)
52 {
53 outb_p(addr, maple_rtc_addr);
54 return inb_p(maple_rtc_addr+1);
55 }
56
57 static void maple_clock_write(unsigned long val, int addr)
58 {
59 outb_p(addr, maple_rtc_addr);
60 outb_p(val, maple_rtc_addr+1);
61 }
62
63 void maple_get_rtc_time(struct rtc_time *tm)
64 {
65 int uip, i;
66
67 /* The Linux interpretation of the CMOS clock register contents:
68 * When the Update-In-Progress (UIP) flag goes from 1 to 0, the
69 * RTC registers show the second which has precisely just started.
70 * Let's hope other operating systems interpret the RTC the same way.
71 */
72
73 /* Since the UIP flag is set for about 2.2 ms and the clock
74 * is typically written with a precision of 1 jiffy, trying
75 * to obtain a precision better than a few milliseconds is
76 * an illusion. Only consistency is interesting, this also
77 * allows to use the routine for /dev/rtc without a potential
78 * 1 second kernel busy loop triggered by any reader of /dev/rtc.
79 */
80
81 for (i = 0; i<1000000; i++) {
82 uip = maple_clock_read(RTC_FREQ_SELECT);
83 tm->tm_sec = maple_clock_read(RTC_SECONDS);
84 tm->tm_min = maple_clock_read(RTC_MINUTES);
85 tm->tm_hour = maple_clock_read(RTC_HOURS);
86 tm->tm_mday = maple_clock_read(RTC_DAY_OF_MONTH);
87 tm->tm_mon = maple_clock_read(RTC_MONTH);
88 tm->tm_year = maple_clock_read(RTC_YEAR);
89 uip |= maple_clock_read(RTC_FREQ_SELECT);
90 if ((uip & RTC_UIP)==0)
91 break;
92 }
93
94 if (!(maple_clock_read(RTC_CONTROL) & RTC_DM_BINARY)
95 || RTC_ALWAYS_BCD) {
96 BCD_TO_BIN(tm->tm_sec);
97 BCD_TO_BIN(tm->tm_min);
98 BCD_TO_BIN(tm->tm_hour);
99 BCD_TO_BIN(tm->tm_mday);
100 BCD_TO_BIN(tm->tm_mon);
101 BCD_TO_BIN(tm->tm_year);
102 }
103 if ((tm->tm_year + 1900) < 1970)
104 tm->tm_year += 100;
105
106 GregorianDay(tm);
107 }
108
109 int maple_set_rtc_time(struct rtc_time *tm)
110 {
111 unsigned char save_control, save_freq_select;
112 int sec, min, hour, mon, mday, year;
113
114 spin_lock(&rtc_lock);
115
116 save_control = maple_clock_read(RTC_CONTROL); /* tell the clock it's being set */
117
118 maple_clock_write((save_control|RTC_SET), RTC_CONTROL);
119
120 save_freq_select = maple_clock_read(RTC_FREQ_SELECT); /* stop and reset prescaler */
121
122 maple_clock_write((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
123
124 sec = tm->tm_sec;
125 min = tm->tm_min;
126 hour = tm->tm_hour;
127 mon = tm->tm_mon;
128 mday = tm->tm_mday;
129 year = tm->tm_year;
130
131 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
132 BIN_TO_BCD(sec);
133 BIN_TO_BCD(min);
134 BIN_TO_BCD(hour);
135 BIN_TO_BCD(mon);
136 BIN_TO_BCD(mday);
137 BIN_TO_BCD(year);
138 }
139 maple_clock_write(sec, RTC_SECONDS);
140 maple_clock_write(min, RTC_MINUTES);
141 maple_clock_write(hour, RTC_HOURS);
142 maple_clock_write(mon, RTC_MONTH);
143 maple_clock_write(mday, RTC_DAY_OF_MONTH);
144 maple_clock_write(year, RTC_YEAR);
145
146 /* The following flags have to be released exactly in this order,
147 * otherwise the DS12887 (popular MC146818A clone with integrated
148 * battery and quartz) will not reset the oscillator and will not
149 * update precisely 500 ms later. You won't find this mentioned in
150 * the Dallas Semiconductor data sheets, but who believes data
151 * sheets anyway ... -- Markus Kuhn
152 */
153 maple_clock_write(save_control, RTC_CONTROL);
154 maple_clock_write(save_freq_select, RTC_FREQ_SELECT);
155
156 spin_unlock(&rtc_lock);
157
158 return 0;
159 }
160
161 static struct resource rtc_iores = {
162 .name = "rtc",
163 .flags = IORESOURCE_BUSY,
164 };
165
166 unsigned long __init maple_get_boot_time(void)
167 {
168 struct rtc_time tm;
169 struct device_node *rtcs;
170
171 rtcs = of_find_compatible_node(NULL, "rtc", "pnpPNP,b00");
172 if (rtcs) {
173 struct resource r;
174 if (of_address_to_resource(rtcs, 0, &r)) {
175 printk(KERN_EMERG "Maple: Unable to translate RTC"
176 " address\n");
177 goto bail;
178 }
179 if (!(r.flags & IORESOURCE_IO)) {
180 printk(KERN_EMERG "Maple: RTC address isn't PIO!\n");
181 goto bail;
182 }
183 maple_rtc_addr = r.start;
184 printk(KERN_INFO "Maple: Found RTC at IO 0x%x\n",
185 maple_rtc_addr);
186 }
187 bail:
188 if (maple_rtc_addr == 0) {
189 maple_rtc_addr = RTC_PORT(0); /* legacy address */
190 printk(KERN_INFO "Maple: No device node for RTC, assuming "
191 "legacy address (0x%x)\n", maple_rtc_addr);
192 }
193
194 rtc_iores.start = maple_rtc_addr;
195 rtc_iores.end = maple_rtc_addr + 7;
196 request_resource(&ioport_resource, &rtc_iores);
197
198 maple_get_rtc_time(&tm);
199 return mktime(tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
200 tm.tm_hour, tm.tm_min, tm.tm_sec);
201 }
202