[POWERPC] Support for "weird" MPICs and fixup mpc7448_hpc2
[linux-2.6] / arch / powerpc / platforms / maple / time.c
1 /*
2  *  (c) Copyright 2004 Benjamin Herrenschmidt (benh@kernel.crashing.org),
3  *                     IBM Corp. 
4  *
5  *  This program is free software; you can redistribute it and/or
6  *  modify it under the terms of the GNU General Public License
7  *  as published by the Free Software Foundation; either version
8  *  2 of the License, or (at your option) any later version.
9  *
10  */
11
12 #undef DEBUG
13
14 #include <linux/errno.h>
15 #include <linux/sched.h>
16 #include <linux/kernel.h>
17 #include <linux/param.h>
18 #include <linux/string.h>
19 #include <linux/mm.h>
20 #include <linux/init.h>
21 #include <linux/time.h>
22 #include <linux/adb.h>
23 #include <linux/pmu.h>
24 #include <linux/interrupt.h>
25 #include <linux/mc146818rtc.h>
26 #include <linux/bcd.h>
27
28 #include <asm/sections.h>
29 #include <asm/prom.h>
30 #include <asm/system.h>
31 #include <asm/io.h>
32 #include <asm/pgtable.h>
33 #include <asm/machdep.h>
34 #include <asm/time.h>
35
36 #include "maple.h"
37
38 #ifdef DEBUG
39 #define DBG(x...) printk(x)
40 #else
41 #define DBG(x...)
42 #endif
43
44 extern void GregorianDay(struct rtc_time * tm);
45
46 static int maple_rtc_addr;
47
48 static int maple_clock_read(int addr)
49 {
50         outb_p(addr, maple_rtc_addr);
51         return inb_p(maple_rtc_addr+1);
52 }
53
54 static void maple_clock_write(unsigned long val, int addr)
55 {
56         outb_p(addr, maple_rtc_addr);
57         outb_p(val, maple_rtc_addr+1);
58 }
59
60 void maple_get_rtc_time(struct rtc_time *tm)
61 {
62         do {
63                 tm->tm_sec = maple_clock_read(RTC_SECONDS);
64                 tm->tm_min = maple_clock_read(RTC_MINUTES);
65                 tm->tm_hour = maple_clock_read(RTC_HOURS);
66                 tm->tm_mday = maple_clock_read(RTC_DAY_OF_MONTH);
67                 tm->tm_mon = maple_clock_read(RTC_MONTH);
68                 tm->tm_year = maple_clock_read(RTC_YEAR);
69         } while (tm->tm_sec != maple_clock_read(RTC_SECONDS));
70
71         if (!(maple_clock_read(RTC_CONTROL) & RTC_DM_BINARY)
72             || RTC_ALWAYS_BCD) {
73                 BCD_TO_BIN(tm->tm_sec);
74                 BCD_TO_BIN(tm->tm_min);
75                 BCD_TO_BIN(tm->tm_hour);
76                 BCD_TO_BIN(tm->tm_mday);
77                 BCD_TO_BIN(tm->tm_mon);
78                 BCD_TO_BIN(tm->tm_year);
79           }
80         if ((tm->tm_year + 1900) < 1970)
81                 tm->tm_year += 100;
82
83         GregorianDay(tm);
84 }
85
86 int maple_set_rtc_time(struct rtc_time *tm)
87 {
88         unsigned char save_control, save_freq_select;
89         int sec, min, hour, mon, mday, year;
90
91         spin_lock(&rtc_lock);
92
93         save_control = maple_clock_read(RTC_CONTROL); /* tell the clock it's being set */
94
95         maple_clock_write((save_control|RTC_SET), RTC_CONTROL);
96
97         save_freq_select = maple_clock_read(RTC_FREQ_SELECT); /* stop and reset prescaler */
98
99         maple_clock_write((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
100
101         sec = tm->tm_sec;
102         min = tm->tm_min;
103         hour = tm->tm_hour;
104         mon = tm->tm_mon;
105         mday = tm->tm_mday;
106         year = tm->tm_year;
107
108         if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
109                 BIN_TO_BCD(sec);
110                 BIN_TO_BCD(min);
111                 BIN_TO_BCD(hour);
112                 BIN_TO_BCD(mon);
113                 BIN_TO_BCD(mday);
114                 BIN_TO_BCD(year);
115         }
116         maple_clock_write(sec, RTC_SECONDS);
117         maple_clock_write(min, RTC_MINUTES);
118         maple_clock_write(hour, RTC_HOURS);
119         maple_clock_write(mon, RTC_MONTH);
120         maple_clock_write(mday, RTC_DAY_OF_MONTH);
121         maple_clock_write(year, RTC_YEAR);
122
123         /* The following flags have to be released exactly in this order,
124          * otherwise the DS12887 (popular MC146818A clone with integrated
125          * battery and quartz) will not reset the oscillator and will not
126          * update precisely 500 ms later. You won't find this mentioned in
127          * the Dallas Semiconductor data sheets, but who believes data
128          * sheets anyway ...                           -- Markus Kuhn
129          */
130         maple_clock_write(save_control, RTC_CONTROL);
131         maple_clock_write(save_freq_select, RTC_FREQ_SELECT);
132
133         spin_unlock(&rtc_lock);
134
135         return 0;
136 }
137
138 static struct resource rtc_iores = {
139         .name = "rtc",
140         .flags = IORESOURCE_BUSY,
141 };
142
143 unsigned long __init maple_get_boot_time(void)
144 {
145         struct rtc_time tm;
146         struct device_node *rtcs;
147
148         rtcs = of_find_compatible_node(NULL, "rtc", "pnpPNP,b00");
149         if (rtcs) {
150                 struct resource r;
151                 if (of_address_to_resource(rtcs, 0, &r)) {
152                         printk(KERN_EMERG "Maple: Unable to translate RTC"
153                                " address\n");
154                         goto bail;
155                 }
156                 if (!(r.flags & IORESOURCE_IO)) {
157                         printk(KERN_EMERG "Maple: RTC address isn't PIO!\n");
158                         goto bail;
159                 }
160                 maple_rtc_addr = r.start;
161                 printk(KERN_INFO "Maple: Found RTC at IO 0x%x\n",
162                        maple_rtc_addr);
163         }
164  bail:
165         if (maple_rtc_addr == 0) {
166                 maple_rtc_addr = RTC_PORT(0); /* legacy address */
167                 printk(KERN_INFO "Maple: No device node for RTC, assuming "
168                        "legacy address (0x%x)\n", maple_rtc_addr);
169         }
170
171         rtc_iores.start = maple_rtc_addr;
172         rtc_iores.end = maple_rtc_addr + 7;
173         request_resource(&ioport_resource, &rtc_iores);
174
175         maple_get_rtc_time(&tm);
176         return mktime(tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
177                       tm.tm_hour, tm.tm_min, tm.tm_sec);
178 }
179