blob: b9a2b3d4bf3323d6a04bf7f91f6f158d01b10030 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * (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/config.h>
15#include <linux/errno.h>
16#include <linux/sched.h>
17#include <linux/kernel.h>
18#include <linux/param.h>
19#include <linux/string.h>
20#include <linux/mm.h>
21#include <linux/init.h>
22#include <linux/time.h>
23#include <linux/adb.h>
24#include <linux/pmu.h>
25#include <linux/interrupt.h>
26#include <linux/mc146818rtc.h>
27#include <linux/bcd.h>
28
29#include <asm/sections.h>
30#include <asm/prom.h>
31#include <asm/system.h>
32#include <asm/io.h>
33#include <asm/pgtable.h>
34#include <asm/machdep.h>
35#include <asm/time.h>
36
Paul Mackerras0cb7b2a2005-10-29 22:07:56 +100037#include "maple.h"
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#ifdef DEBUG
40#define DBG(x...) printk(x)
41#else
42#define DBG(x...)
43#endif
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045extern void GregorianDay(struct rtc_time * tm);
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047static int maple_rtc_addr;
48
49static int maple_clock_read(int addr)
50{
51 outb_p(addr, maple_rtc_addr);
52 return inb_p(maple_rtc_addr+1);
53}
54
55static void maple_clock_write(unsigned long val, int addr)
56{
57 outb_p(addr, maple_rtc_addr);
58 outb_p(val, maple_rtc_addr+1);
59}
60
61void maple_get_rtc_time(struct rtc_time *tm)
62{
Matt Mackall6f0d7bd2006-03-28 01:56:04 -080063 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 tm->tm_sec = maple_clock_read(RTC_SECONDS);
65 tm->tm_min = maple_clock_read(RTC_MINUTES);
66 tm->tm_hour = maple_clock_read(RTC_HOURS);
67 tm->tm_mday = maple_clock_read(RTC_DAY_OF_MONTH);
68 tm->tm_mon = maple_clock_read(RTC_MONTH);
69 tm->tm_year = maple_clock_read(RTC_YEAR);
Matt Mackall6f0d7bd2006-03-28 01:56:04 -080070 } while (tm->tm_sec != maple_clock_read(RTC_SECONDS));
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 if (!(maple_clock_read(RTC_CONTROL) & RTC_DM_BINARY)
73 || RTC_ALWAYS_BCD) {
74 BCD_TO_BIN(tm->tm_sec);
75 BCD_TO_BIN(tm->tm_min);
76 BCD_TO_BIN(tm->tm_hour);
77 BCD_TO_BIN(tm->tm_mday);
78 BCD_TO_BIN(tm->tm_mon);
79 BCD_TO_BIN(tm->tm_year);
80 }
81 if ((tm->tm_year + 1900) < 1970)
82 tm->tm_year += 100;
83
84 GregorianDay(tm);
85}
86
87int maple_set_rtc_time(struct rtc_time *tm)
88{
89 unsigned char save_control, save_freq_select;
90 int sec, min, hour, mon, mday, year;
91
92 spin_lock(&rtc_lock);
93
94 save_control = maple_clock_read(RTC_CONTROL); /* tell the clock it's being set */
95
96 maple_clock_write((save_control|RTC_SET), RTC_CONTROL);
97
98 save_freq_select = maple_clock_read(RTC_FREQ_SELECT); /* stop and reset prescaler */
99
100 maple_clock_write((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
101
102 sec = tm->tm_sec;
103 min = tm->tm_min;
104 hour = tm->tm_hour;
105 mon = tm->tm_mon;
106 mday = tm->tm_mday;
107 year = tm->tm_year;
108
109 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
110 BIN_TO_BCD(sec);
111 BIN_TO_BCD(min);
112 BIN_TO_BCD(hour);
113 BIN_TO_BCD(mon);
114 BIN_TO_BCD(mday);
115 BIN_TO_BCD(year);
116 }
117 maple_clock_write(sec, RTC_SECONDS);
118 maple_clock_write(min, RTC_MINUTES);
119 maple_clock_write(hour, RTC_HOURS);
120 maple_clock_write(mon, RTC_MONTH);
121 maple_clock_write(mday, RTC_DAY_OF_MONTH);
122 maple_clock_write(year, RTC_YEAR);
123
124 /* The following flags have to be released exactly in this order,
125 * otherwise the DS12887 (popular MC146818A clone with integrated
126 * battery and quartz) will not reset the oscillator and will not
127 * update precisely 500 ms later. You won't find this mentioned in
128 * the Dallas Semiconductor data sheets, but who believes data
129 * sheets anyway ... -- Markus Kuhn
130 */
131 maple_clock_write(save_control, RTC_CONTROL);
132 maple_clock_write(save_freq_select, RTC_FREQ_SELECT);
133
134 spin_unlock(&rtc_lock);
135
136 return 0;
137}
138
Segher Boessenkoola097a352005-11-17 22:22:14 +0100139static struct resource rtc_iores = {
140 .name = "rtc",
141 .flags = IORESOURCE_BUSY,
142};
143
Paul Mackerras143a1de2005-10-19 23:11:21 +1000144unsigned long __init maple_get_boot_time(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Paul Mackerras143a1de2005-10-19 23:11:21 +1000146 struct rtc_time tm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 struct device_node *rtcs;
148
Benjamin Herrenschmidt4c882b02006-01-14 16:35:35 +1100149 rtcs = of_find_compatible_node(NULL, "rtc", "pnpPNP,b00");
150 if (rtcs) {
151 struct resource r;
152 if (of_address_to_resource(rtcs, 0, &r)) {
153 printk(KERN_EMERG "Maple: Unable to translate RTC"
154 " address\n");
155 goto bail;
156 }
157 if (!(r.flags & IORESOURCE_IO)) {
158 printk(KERN_EMERG "Maple: RTC address isn't PIO!\n");
159 goto bail;
160 }
161 maple_rtc_addr = r.start;
162 printk(KERN_INFO "Maple: Found RTC at IO 0x%x\n",
163 maple_rtc_addr);
164 }
165 bail:
166 if (maple_rtc_addr == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 maple_rtc_addr = RTC_PORT(0); /* legacy address */
168 printk(KERN_INFO "Maple: No device node for RTC, assuming "
169 "legacy address (0x%x)\n", maple_rtc_addr);
170 }
Segher Boessenkoola097a352005-11-17 22:22:14 +0100171
172 rtc_iores.start = maple_rtc_addr;
173 rtc_iores.end = maple_rtc_addr + 7;
174 request_resource(&ioport_resource, &rtc_iores);
175
Paul Mackerras143a1de2005-10-19 23:11:21 +1000176 maple_get_rtc_time(&tm);
Paul Mackerras17a63922005-10-20 21:10:09 +1000177 return mktime(tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
178 tm.tm_hour, tm.tm_min, tm.tm_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180