blob: f803f4b8ab6f21cebd6c5de9781ccc72c2a42106 [file] [log] [blame]
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001/*
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10002 * Copyright (C) 1991, 1992, 1995 Linus Torvalds
3 *
4 * Adapted for PowerPC (PReP) by Gary Thomas
5 * Modified by Cort Dougan (cort@cs.nmt.edu).
6 * Copied and modified from arch/i386/kernel/time.c
7 *
8 */
9#include <linux/errno.h>
10#include <linux/sched.h>
11#include <linux/kernel.h>
12#include <linux/param.h>
13#include <linux/string.h>
14#include <linux/mm.h>
15#include <linux/interrupt.h>
16#include <linux/time.h>
17#include <linux/timex.h>
18#include <linux/kernel_stat.h>
19#include <linux/mc146818rtc.h>
20#include <linux/init.h>
21#include <linux/bcd.h>
David Woodhouse575e3212006-01-14 00:13:49 +000022#include <linux/ioport.h>
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +100023
24#include <asm/io.h>
25#include <asm/nvram.h>
26#include <asm/prom.h>
27#include <asm/sections.h>
28#include <asm/time.h>
29
30extern spinlock_t rtc_lock;
31
Benjamin Herrenschmidt74d51d02010-07-29 14:45:24 +100032#define NVRAM_AS0 0x74
33#define NVRAM_AS1 0x75
34#define NVRAM_DATA 0x77
35
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +100036static int nvram_as1 = NVRAM_AS1;
37static int nvram_as0 = NVRAM_AS0;
38static int nvram_data = NVRAM_DATA;
39
40long __init chrp_time_init(void)
41{
42 struct device_node *rtcs;
David Woodhouse575e3212006-01-14 00:13:49 +000043 struct resource r;
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +100044 int base;
45
Stephen Rothwell4bf56e12007-04-24 13:48:41 +100046 rtcs = of_find_compatible_node(NULL, "rtc", "pnpPNP,b00");
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +100047 if (rtcs == NULL)
Stephen Rothwell4bf56e12007-04-24 13:48:41 +100048 rtcs = of_find_compatible_node(NULL, "rtc", "ds1385-rtc");
49 if (rtcs == NULL)
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +100050 return 0;
Stephen Rothwell4bf56e12007-04-24 13:48:41 +100051 if (of_address_to_resource(rtcs, 0, &r)) {
52 of_node_put(rtcs);
53 return 0;
54 }
55 of_node_put(rtcs);
56
David Woodhouse575e3212006-01-14 00:13:49 +000057 base = r.start;
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +100058 nvram_as1 = 0;
59 nvram_as0 = base;
60 nvram_data = base + 1;
61
62 return 0;
63}
64
65int chrp_cmos_clock_read(int addr)
66{
67 if (nvram_as1 != 0)
68 outb(addr>>8, nvram_as1);
69 outb(addr, nvram_as0);
70 return (inb(nvram_data));
71}
72
73void chrp_cmos_clock_write(unsigned long val, int addr)
74{
75 if (nvram_as1 != 0)
76 outb(addr>>8, nvram_as1);
77 outb(addr, nvram_as0);
78 outb(val, nvram_data);
79 return;
80}
81
82/*
83 * Set the hardware clock. -- Cort
84 */
85int chrp_set_rtc_time(struct rtc_time *tmarg)
86{
87 unsigned char save_control, save_freq_select;
88 struct rtc_time tm = *tmarg;
89
90 spin_lock(&rtc_lock);
91
92 save_control = chrp_cmos_clock_read(RTC_CONTROL); /* tell the clock it's being set */
93
94 chrp_cmos_clock_write((save_control|RTC_SET), RTC_CONTROL);
95
96 save_freq_select = chrp_cmos_clock_read(RTC_FREQ_SELECT); /* stop and reset prescaler */
97
98 chrp_cmos_clock_write((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
99
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000100 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
Adrian Bunk8f6ba492008-08-09 02:34:53 +1000101 tm.tm_sec = bin2bcd(tm.tm_sec);
102 tm.tm_min = bin2bcd(tm.tm_min);
103 tm.tm_hour = bin2bcd(tm.tm_hour);
104 tm.tm_mon = bin2bcd(tm.tm_mon);
105 tm.tm_mday = bin2bcd(tm.tm_mday);
106 tm.tm_year = bin2bcd(tm.tm_year);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000107 }
108 chrp_cmos_clock_write(tm.tm_sec,RTC_SECONDS);
109 chrp_cmos_clock_write(tm.tm_min,RTC_MINUTES);
110 chrp_cmos_clock_write(tm.tm_hour,RTC_HOURS);
111 chrp_cmos_clock_write(tm.tm_mon,RTC_MONTH);
112 chrp_cmos_clock_write(tm.tm_mday,RTC_DAY_OF_MONTH);
113 chrp_cmos_clock_write(tm.tm_year,RTC_YEAR);
114
115 /* The following flags have to be released exactly in this order,
116 * otherwise the DS12887 (popular MC146818A clone with integrated
117 * battery and quartz) will not reset the oscillator and will not
118 * update precisely 500 ms later. You won't find this mentioned in
119 * the Dallas Semiconductor data sheets, but who believes data
120 * sheets anyway ... -- Markus Kuhn
121 */
122 chrp_cmos_clock_write(save_control, RTC_CONTROL);
123 chrp_cmos_clock_write(save_freq_select, RTC_FREQ_SELECT);
124
125 spin_unlock(&rtc_lock);
126 return 0;
127}
128
129void chrp_get_rtc_time(struct rtc_time *tm)
130{
131 unsigned int year, mon, day, hour, min, sec;
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000132
Matt Mackall4dc12ec2006-03-28 01:56:03 -0800133 do {
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000134 sec = chrp_cmos_clock_read(RTC_SECONDS);
135 min = chrp_cmos_clock_read(RTC_MINUTES);
136 hour = chrp_cmos_clock_read(RTC_HOURS);
137 day = chrp_cmos_clock_read(RTC_DAY_OF_MONTH);
138 mon = chrp_cmos_clock_read(RTC_MONTH);
139 year = chrp_cmos_clock_read(RTC_YEAR);
Matt Mackall4dc12ec2006-03-28 01:56:03 -0800140 } while (sec != chrp_cmos_clock_read(RTC_SECONDS));
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000141
142 if (!(chrp_cmos_clock_read(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
Adrian Bunk8f6ba492008-08-09 02:34:53 +1000143 sec = bcd2bin(sec);
144 min = bcd2bin(min);
145 hour = bcd2bin(hour);
146 day = bcd2bin(day);
147 mon = bcd2bin(mon);
148 year = bcd2bin(year);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000149 }
Paul Mackerras49e16b72005-11-18 15:52:38 +1100150 if (year < 70)
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +1000151 year += 100;
152 tm->tm_sec = sec;
153 tm->tm_min = min;
154 tm->tm_hour = hour;
155 tm->tm_mday = day;
156 tm->tm_mon = mon;
157 tm->tm_year = year;
158}