blob: eac569dee27cc0d576069ddbd933520121ff681d [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#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
Paul Mackerras0cb7b2a2005-10-29 22:07:56 +100036#include "maple.h"
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#ifdef DEBUG
39#define DBG(x...) printk(x)
40#else
41#define DBG(x...)
42#endif
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static int maple_rtc_addr;
45
46static int maple_clock_read(int addr)
47{
48 outb_p(addr, maple_rtc_addr);
49 return inb_p(maple_rtc_addr+1);
50}
51
52static void maple_clock_write(unsigned long val, int addr)
53{
54 outb_p(addr, maple_rtc_addr);
55 outb_p(val, maple_rtc_addr+1);
56}
57
58void maple_get_rtc_time(struct rtc_time *tm)
59{
Matt Mackall6f0d7bd2006-03-28 01:56:04 -080060 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 tm->tm_sec = maple_clock_read(RTC_SECONDS);
62 tm->tm_min = maple_clock_read(RTC_MINUTES);
63 tm->tm_hour = maple_clock_read(RTC_HOURS);
64 tm->tm_mday = maple_clock_read(RTC_DAY_OF_MONTH);
65 tm->tm_mon = maple_clock_read(RTC_MONTH);
66 tm->tm_year = maple_clock_read(RTC_YEAR);
Matt Mackall6f0d7bd2006-03-28 01:56:04 -080067 } while (tm->tm_sec != maple_clock_read(RTC_SECONDS));
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 if (!(maple_clock_read(RTC_CONTROL) & RTC_DM_BINARY)
70 || RTC_ALWAYS_BCD) {
Adrian Bunk8f6ba492008-08-09 02:34:53 +100071 tm->tm_sec = bcd2bin(tm->tm_sec);
72 tm->tm_min = bcd2bin(tm->tm_min);
73 tm->tm_hour = bcd2bin(tm->tm_hour);
74 tm->tm_mday = bcd2bin(tm->tm_mday);
75 tm->tm_mon = bcd2bin(tm->tm_mon);
76 tm->tm_year = bcd2bin(tm->tm_year);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 }
78 if ((tm->tm_year + 1900) < 1970)
79 tm->tm_year += 100;
80
81 GregorianDay(tm);
82}
83
84int maple_set_rtc_time(struct rtc_time *tm)
85{
86 unsigned char save_control, save_freq_select;
87 int sec, min, hour, mon, mday, year;
88
89 spin_lock(&rtc_lock);
90
91 save_control = maple_clock_read(RTC_CONTROL); /* tell the clock it's being set */
92
93 maple_clock_write((save_control|RTC_SET), RTC_CONTROL);
94
95 save_freq_select = maple_clock_read(RTC_FREQ_SELECT); /* stop and reset prescaler */
96
97 maple_clock_write((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
98
99 sec = tm->tm_sec;
100 min = tm->tm_min;
101 hour = tm->tm_hour;
102 mon = tm->tm_mon;
103 mday = tm->tm_mday;
104 year = tm->tm_year;
105
106 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
Adrian Bunk8f6ba492008-08-09 02:34:53 +1000107 sec = bin2bcd(sec);
108 min = bin2bcd(min);
109 hour = bin2bcd(hour);
110 mon = bin2bcd(mon);
111 mday = bin2bcd(mday);
112 year = bin2bcd(year);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 }
114 maple_clock_write(sec, RTC_SECONDS);
115 maple_clock_write(min, RTC_MINUTES);
116 maple_clock_write(hour, RTC_HOURS);
117 maple_clock_write(mon, RTC_MONTH);
118 maple_clock_write(mday, RTC_DAY_OF_MONTH);
119 maple_clock_write(year, RTC_YEAR);
120
121 /* The following flags have to be released exactly in this order,
122 * otherwise the DS12887 (popular MC146818A clone with integrated
123 * battery and quartz) will not reset the oscillator and will not
124 * update precisely 500 ms later. You won't find this mentioned in
125 * the Dallas Semiconductor data sheets, but who believes data
126 * sheets anyway ... -- Markus Kuhn
127 */
128 maple_clock_write(save_control, RTC_CONTROL);
129 maple_clock_write(save_freq_select, RTC_FREQ_SELECT);
130
131 spin_unlock(&rtc_lock);
132
133 return 0;
134}
135
Segher Boessenkoola097a352005-11-17 22:22:14 +0100136static struct resource rtc_iores = {
137 .name = "rtc",
138 .flags = IORESOURCE_BUSY,
139};
140
Paul Mackerras143a1de2005-10-19 23:11:21 +1000141unsigned long __init maple_get_boot_time(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Paul Mackerras143a1de2005-10-19 23:11:21 +1000143 struct rtc_time tm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 struct device_node *rtcs;
145
Benjamin Herrenschmidt4c882b02006-01-14 16:35:35 +1100146 rtcs = of_find_compatible_node(NULL, "rtc", "pnpPNP,b00");
147 if (rtcs) {
148 struct resource r;
149 if (of_address_to_resource(rtcs, 0, &r)) {
150 printk(KERN_EMERG "Maple: Unable to translate RTC"
151 " address\n");
152 goto bail;
153 }
154 if (!(r.flags & IORESOURCE_IO)) {
155 printk(KERN_EMERG "Maple: RTC address isn't PIO!\n");
156 goto bail;
157 }
158 maple_rtc_addr = r.start;
159 printk(KERN_INFO "Maple: Found RTC at IO 0x%x\n",
160 maple_rtc_addr);
161 }
162 bail:
163 if (maple_rtc_addr == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 maple_rtc_addr = RTC_PORT(0); /* legacy address */
165 printk(KERN_INFO "Maple: No device node for RTC, assuming "
166 "legacy address (0x%x)\n", maple_rtc_addr);
167 }
Segher Boessenkoola097a352005-11-17 22:22:14 +0100168
169 rtc_iores.start = maple_rtc_addr;
170 rtc_iores.end = maple_rtc_addr + 7;
171 request_resource(&ioport_resource, &rtc_iores);
172
Paul Mackerras143a1de2005-10-19 23:11:21 +1000173 maple_get_rtc_time(&tm);
Paul Mackerras17a63922005-10-20 21:10:09 +1000174 return mktime(tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
175 tm.tm_hour, tm.tm_min, tm.tm_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177