1/*
2 *  linux/include/asm-arm/arch-ebsa285/time.h
3 *
4 *  Copyright (C) 1998 Russell King.
5 *  Copyright (C) 1998 Phil Blundell
6 *
7 * CATS has a real-time clock, though the evaluation board doesn't.
8 *
9 * Changelog:
10 *  21-Mar-1998	RMK	Created
11 *  27-Aug-1998	PJB	CATS support
12 *  28-Dec-1998	APH	Made leds optional
13 *  20-Jan-1999	RMK	Started merge of EBSA285, CATS and NetWinder
14 *  16-Mar-1999	RMK	More support for EBSA285-like machines with RTCs in
15 */
16
17#define RTC_PORT(x)		(rtc_base+(x))
18#define RTC_ALWAYS_BCD		0
19
20#include <linux/timex.h>
21#include <linux/init.h>
22#include <linux/sched.h>
23#include <linux/mc146818rtc.h>
24#include <linux/bcd.h>
25
26#include <asm/hardware.h>
27#include <asm/io.h>
28
29#include <asm/mach/time.h>
30#include "common.h"
31
32static int rtc_base;
33
34static unsigned long __init get_isa_cmos_time(void)
35{
36	unsigned int year, mon, day, hour, min, sec;
37
38	// check to see if the RTC makes sense.....
39	if ((CMOS_READ(RTC_VALID) & RTC_VRT) == 0)
40		return mktime(1970, 1, 1, 0, 0, 0);
41
42	do {
43		sec  = CMOS_READ(RTC_SECONDS);
44		min  = CMOS_READ(RTC_MINUTES);
45		hour = CMOS_READ(RTC_HOURS);
46		day  = CMOS_READ(RTC_DAY_OF_MONTH);
47		mon  = CMOS_READ(RTC_MONTH);
48		year = CMOS_READ(RTC_YEAR);
49	} while (sec != CMOS_READ(RTC_SECONDS));
50
51	if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
52		BCD_TO_BIN(sec);
53		BCD_TO_BIN(min);
54		BCD_TO_BIN(hour);
55		BCD_TO_BIN(day);
56		BCD_TO_BIN(mon);
57		BCD_TO_BIN(year);
58	}
59	if ((year += 1900) < 1970)
60		year += 100;
61	return mktime(year, mon, day, hour, min, sec);
62}
63
64static int set_isa_cmos_time(void)
65{
66	int retval = 0;
67	int real_seconds, real_minutes, cmos_minutes;
68	unsigned char save_control, save_freq_select;
69	unsigned long nowtime = xtime.tv_sec;
70
71	save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being set */
72	CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
73
74	save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset prescaler */
75	CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
76
77	cmos_minutes = CMOS_READ(RTC_MINUTES);
78	if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
79		BCD_TO_BIN(cmos_minutes);
80
81	/*
82	 * since we're only adjusting minutes and seconds,
83	 * don't interfere with hour overflow. This avoids
84	 * messing with unknown time zones but requires your
85	 * RTC not to be off by more than 15 minutes
86	 */
87	real_seconds = nowtime % 60;
88	real_minutes = nowtime / 60;
89	if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
90		real_minutes += 30;		/* correct for half hour time zone */
91	real_minutes %= 60;
92
93	if (abs(real_minutes - cmos_minutes) < 30) {
94		if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
95			BIN_TO_BCD(real_seconds);
96			BIN_TO_BCD(real_minutes);
97		}
98		CMOS_WRITE(real_seconds,RTC_SECONDS);
99		CMOS_WRITE(real_minutes,RTC_MINUTES);
100	} else
101		retval = -1;
102
103	/* The following flags have to be released exactly in this order,
104	 * otherwise the DS12887 (popular MC146818A clone with integrated
105	 * battery and quartz) will not reset the oscillator and will not
106	 * update precisely 500 ms later. You won't find this mentioned in
107	 * the Dallas Semiconductor data sheets, but who believes data
108	 * sheets anyway ...                           -- Markus Kuhn
109	 */
110	CMOS_WRITE(save_control, RTC_CONTROL);
111	CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
112
113	return retval;
114}
115
116void __init isa_rtc_init(void)
117{
118	if (machine_is_co285() ||
119	    machine_is_personal_server())
120		/*
121		 * Add-in 21285s shouldn't access the RTC
122		 */
123		rtc_base = 0;
124	else
125		rtc_base = 0x70;
126
127	if (rtc_base) {
128		int reg_d, reg_b;
129
130		/*
131		 * Probe for the RTC.
132		 */
133		reg_d = CMOS_READ(RTC_REG_D);
134
135		/*
136		 * make sure the divider is set
137		 */
138		CMOS_WRITE(RTC_REF_CLCK_32KHZ, RTC_REG_A);
139
140		/*
141		 * Set control reg B
142		 *   (24 hour mode, update enabled)
143		 */
144		reg_b = CMOS_READ(RTC_REG_B) & 0x7f;
145		reg_b |= 2;
146		CMOS_WRITE(reg_b, RTC_REG_B);
147
148		if ((CMOS_READ(RTC_REG_A) & 0x7f) == RTC_REF_CLCK_32KHZ &&
149		    CMOS_READ(RTC_REG_B) == reg_b) {
150			struct timespec tv;
151
152			/*
153			 * We have a RTC.  Check the battery
154			 */
155			if ((reg_d & 0x80) == 0)
156				printk(KERN_WARNING "RTC: *** warning: CMOS battery bad\n");
157
158			tv.tv_nsec = 0;
159			tv.tv_sec = get_isa_cmos_time();
160			do_settimeofday(&tv);
161			set_rtc = set_isa_cmos_time;
162		} else
163			rtc_base = 0;
164	}
165}
166