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/io.h>
31 #include <asm/pgtable.h>
32 #include <asm/machdep.h>
33 #include <asm/time.h>
34 
35 #include "maple.h"
36 
37 #ifdef DEBUG
38 #define DBG(x...) printk(x)
39 #else
40 #define DBG(x...)
41 #endif
42 
43 static int maple_rtc_addr;
44 
45 static int maple_clock_read(int addr)
46 {
47 	outb_p(addr, maple_rtc_addr);
48 	return inb_p(maple_rtc_addr+1);
49 }
50 
51 static void maple_clock_write(unsigned long val, int addr)
52 {
53 	outb_p(addr, maple_rtc_addr);
54 	outb_p(val, maple_rtc_addr+1);
55 }
56 
57 void maple_get_rtc_time(struct rtc_time *tm)
58 {
59 	do {
60 		tm->tm_sec = maple_clock_read(RTC_SECONDS);
61 		tm->tm_min = maple_clock_read(RTC_MINUTES);
62 		tm->tm_hour = maple_clock_read(RTC_HOURS);
63 		tm->tm_mday = maple_clock_read(RTC_DAY_OF_MONTH);
64 		tm->tm_mon = maple_clock_read(RTC_MONTH);
65 		tm->tm_year = maple_clock_read(RTC_YEAR);
66 	} while (tm->tm_sec != maple_clock_read(RTC_SECONDS));
67 
68 	if (!(maple_clock_read(RTC_CONTROL) & RTC_DM_BINARY)
69 	    || RTC_ALWAYS_BCD) {
70 		tm->tm_sec = bcd2bin(tm->tm_sec);
71 		tm->tm_min = bcd2bin(tm->tm_min);
72 		tm->tm_hour = bcd2bin(tm->tm_hour);
73 		tm->tm_mday = bcd2bin(tm->tm_mday);
74 		tm->tm_mon = bcd2bin(tm->tm_mon);
75 		tm->tm_year = bcd2bin(tm->tm_year);
76 	  }
77 	if ((tm->tm_year + 1900) < 1970)
78 		tm->tm_year += 100;
79 
80 	tm->tm_wday = -1;
81 }
82 
83 int maple_set_rtc_time(struct rtc_time *tm)
84 {
85 	unsigned char save_control, save_freq_select;
86 	int sec, min, hour, mon, mday, year;
87 
88 	spin_lock(&rtc_lock);
89 
90 	save_control = maple_clock_read(RTC_CONTROL); /* tell the clock it's being set */
91 
92 	maple_clock_write((save_control|RTC_SET), RTC_CONTROL);
93 
94 	save_freq_select = maple_clock_read(RTC_FREQ_SELECT); /* stop and reset prescaler */
95 
96 	maple_clock_write((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
97 
98 	sec = tm->tm_sec;
99 	min = tm->tm_min;
100 	hour = tm->tm_hour;
101 	mon = tm->tm_mon;
102 	mday = tm->tm_mday;
103 	year = tm->tm_year;
104 
105 	if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
106 		sec = bin2bcd(sec);
107 		min = bin2bcd(min);
108 		hour = bin2bcd(hour);
109 		mon = bin2bcd(mon);
110 		mday = bin2bcd(mday);
111 		year = bin2bcd(year);
112 	}
113 	maple_clock_write(sec, RTC_SECONDS);
114 	maple_clock_write(min, RTC_MINUTES);
115 	maple_clock_write(hour, RTC_HOURS);
116 	maple_clock_write(mon, RTC_MONTH);
117 	maple_clock_write(mday, RTC_DAY_OF_MONTH);
118 	maple_clock_write(year, RTC_YEAR);
119 
120 	/* The following flags have to be released exactly in this order,
121 	 * otherwise the DS12887 (popular MC146818A clone with integrated
122 	 * battery and quartz) will not reset the oscillator and will not
123 	 * update precisely 500 ms later. You won't find this mentioned in
124 	 * the Dallas Semiconductor data sheets, but who believes data
125 	 * sheets anyway ...                           -- Markus Kuhn
126 	 */
127 	maple_clock_write(save_control, RTC_CONTROL);
128 	maple_clock_write(save_freq_select, RTC_FREQ_SELECT);
129 
130 	spin_unlock(&rtc_lock);
131 
132 	return 0;
133 }
134 
135 static struct resource rtc_iores = {
136 	.name = "rtc",
137 	.flags = IORESOURCE_BUSY,
138 };
139 
140 unsigned long __init maple_get_boot_time(void)
141 {
142 	struct rtc_time tm;
143 	struct device_node *rtcs;
144 
145 	rtcs = of_find_compatible_node(NULL, "rtc", "pnpPNP,b00");
146 	if (rtcs) {
147 		struct resource r;
148 		if (of_address_to_resource(rtcs, 0, &r)) {
149 			printk(KERN_EMERG "Maple: Unable to translate RTC"
150 			       " address\n");
151 			goto bail;
152 		}
153 		if (!(r.flags & IORESOURCE_IO)) {
154 			printk(KERN_EMERG "Maple: RTC address isn't PIO!\n");
155 			goto bail;
156 		}
157 		maple_rtc_addr = r.start;
158 		printk(KERN_INFO "Maple: Found RTC at IO 0x%x\n",
159 		       maple_rtc_addr);
160 	}
161  bail:
162 	if (maple_rtc_addr == 0) {
163 		maple_rtc_addr = RTC_PORT(0); /* legacy address */
164 		printk(KERN_INFO "Maple: No device node for RTC, assuming "
165 		       "legacy address (0x%x)\n", maple_rtc_addr);
166 	}
167 
168 	rtc_iores.start = maple_rtc_addr;
169 	rtc_iores.end = maple_rtc_addr + 7;
170 	request_resource(&ioport_resource, &rtc_iores);
171 
172 	maple_get_rtc_time(&tm);
173 	return mktime(tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
174 		      tm.tm_hour, tm.tm_min, tm.tm_sec);
175 }
176 
177