ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / mips / sibyte / swarm / time.c
1 /*
2  * Copyright (C) 2000, 2001 Broadcom Corporation
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  */
18
19 /*
20  * Time routines for the swarm board.  We pass all the hard stuff
21  * through to the sb1250 handling code.  Only thing we really keep
22  * track of here is what time of day we think it is.  And we don't
23  * really even do a good job of that...
24  */
25
26
27 #include <linux/bcd.h>
28 #include <linux/init.h>
29 #include <linux/time.h>
30 #include <linux/sched.h>
31 #include <linux/spinlock.h>
32 #include <asm/system.h>
33 #include <asm/addrspace.h>
34 #include <asm/io.h>
35
36 #include <asm/sibyte/sb1250.h>
37 #include <asm/sibyte/sb1250_regs.h>
38 #include <asm/sibyte/sb1250_smbus.h>
39
40 static unsigned long long sec_bias = 0;
41 static unsigned int usec_bias = 0;
42
43 /* Xicor 1241 definitions */
44
45 /*
46  * Register bits
47  */
48
49 #define X1241REG_SR_BAT 0x80            /* currently on battery power */
50 #define X1241REG_SR_RWEL 0x04           /* r/w latch is enabled, can write RTC */
51 #define X1241REG_SR_WEL 0x02            /* r/w latch is unlocked, can enable r/w now */
52 #define X1241REG_SR_RTCF 0x01           /* clock failed */
53 #define X1241REG_BL_BP2 0x80            /* block protect 2 */
54 #define X1241REG_BL_BP1 0x40            /* block protect 1 */
55 #define X1241REG_BL_BP0 0x20            /* block protect 0 */
56 #define X1241REG_BL_WD1 0x10
57 #define X1241REG_BL_WD0 0x08
58 #define X1241REG_HR_MIL 0x80            /* military time format */
59
60 /*
61  * Register numbers
62  */
63
64 #define X1241REG_BL     0x10            /* block protect bits */
65 #define X1241REG_INT    0x11            /*  */
66 #define X1241REG_SC     0x30            /* Seconds */
67 #define X1241REG_MN     0x31            /* Minutes */
68 #define X1241REG_HR     0x32            /* Hours */
69 #define X1241REG_DT     0x33            /* Day of month */
70 #define X1241REG_MO     0x34            /* Month */
71 #define X1241REG_YR     0x35            /* Year */
72 #define X1241REG_DW     0x36            /* Day of Week */
73 #define X1241REG_Y2K    0x37            /* Year 2K */
74 #define X1241REG_SR     0x3F            /* Status register */
75
76 #define X1241_CCR_ADDRESS       0x6F
77
78 #define SMB_CSR(reg) (IOADDR(A_SMB_REGISTER(1, reg)))
79
80 static int xicor_read(uint8_t addr)
81 {
82         while (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_BUSY)
83                 ;
84
85         __raw_writeq((addr >> 8) & 0x7, SMB_CSR(R_SMB_CMD));
86         __raw_writeq((addr & 0xff), SMB_CSR(R_SMB_DATA));
87         __raw_writeq((V_SMB_ADDR(X1241_CCR_ADDRESS) | V_SMB_TT_WR2BYTE), SMB_CSR(R_SMB_START));
88
89         while (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_BUSY)
90                 ;
91
92         __raw_writeq((V_SMB_ADDR(X1241_CCR_ADDRESS) | V_SMB_TT_RD1BYTE), SMB_CSR(R_SMB_START));
93
94         while (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_BUSY)
95                 ;
96
97         if (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_ERROR) {
98                 /* Clear error bit by writing a 1 */
99                 __raw_writeq(M_SMB_ERROR, SMB_CSR(R_SMB_STATUS));
100                 return -1;
101         }
102
103         return (__raw_readq(SMB_CSR(R_SMB_DATA)) & 0xff);
104 }
105
106 static int xicor_write(uint8_t addr, int b)
107 {
108         while (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_BUSY)
109                 ;
110
111         __raw_writeq(addr, SMB_CSR(R_SMB_CMD));
112         __raw_writeq((addr & 0xff) | ((b & 0xff) << 8), SMB_CSR(R_SMB_DATA));
113         __raw_writeq(V_SMB_ADDR(X1241_CCR_ADDRESS) | V_SMB_TT_WR3BYTE,
114               SMB_CSR(R_SMB_START));
115
116         while (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_BUSY)
117                 ;
118
119         if (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_ERROR) {
120                 /* Clear error bit by writing a 1 */
121                 __raw_writeq(M_SMB_ERROR, SMB_CSR(R_SMB_STATUS));
122                 return -1;
123         } else {
124                 return 0;
125         }
126 }
127
128 /*
129  * In order to set the CMOS clock precisely, set_rtc_mmss has to be
130  * called 500 ms after the second nowtime has started, because when
131  * nowtime is written into the registers of the CMOS clock, it will
132  * jump to the next second precisely 500 ms later. Check the Motorola
133  * MC146818A or Dallas DS12887 data sheet for details.
134  *
135  * BUG: This routine does not handle hour overflow properly; it just
136  *      sets the minutes. Usually you'll only notice that after reboot!
137  */
138 int set_rtc_mmss(unsigned long nowtime)
139 {
140         int retval = 0;
141         int real_seconds, real_minutes, cmos_minutes;
142
143         cmos_minutes = xicor_read(X1241REG_MN);
144         cmos_minutes = BCD2BIN(cmos_minutes);
145
146         /*
147          * since we're only adjusting minutes and seconds,
148          * don't interfere with hour overflow. This avoids
149          * messing with unknown time zones but requires your
150          * RTC not to be off by more than 15 minutes
151          */
152         real_seconds = nowtime % 60;
153         real_minutes = nowtime / 60;
154         if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
155                 real_minutes += 30;             /* correct for half hour time zone */
156         real_minutes %= 60;
157
158         /* unlock writes to the CCR */
159         xicor_write(X1241REG_SR, X1241REG_SR_WEL);
160         xicor_write(X1241REG_SR, X1241REG_SR_WEL | X1241REG_SR_RWEL);
161
162         if (abs(real_minutes - cmos_minutes) < 30) {
163                 real_seconds = BIN2BCD(real_seconds);
164                 real_minutes = BIN2BCD(real_minutes);
165                 xicor_write(X1241REG_SC, real_seconds);
166                 xicor_write(X1241REG_MN, real_minutes);
167         } else {
168                 printk(KERN_WARNING
169                        "set_rtc_mmss: can't update from %d to %d\n",
170                        cmos_minutes, real_minutes);
171                 retval = -1;
172         }
173
174         xicor_write(X1241REG_SR, 0);
175
176         printk("set_rtc_mmss: %02d:%02d\n", real_minutes, real_seconds);
177
178         return retval;
179 }
180
181 static unsigned long __init get_swarm_time(void)
182 {
183         unsigned int year, mon, day, hour, min, sec, y2k;
184
185         sec = xicor_read(X1241REG_SC);
186         min = xicor_read(X1241REG_MN);
187         hour = xicor_read(X1241REG_HR);
188
189         if (hour & X1241REG_HR_MIL) {
190                 hour &= 0x3f;
191         } else {
192                 if (hour & 0x20)
193                         hour = (hour & 0xf) + 0x12;
194         }
195
196         sec = BCD2BIN(sec);
197         min = BCD2BIN(min);
198         hour = BCD2BIN(hour);
199
200         day = xicor_read(X1241REG_DT);
201         mon = xicor_read(X1241REG_MO);
202         year = xicor_read(X1241REG_YR);
203         y2k = xicor_read(X1241REG_Y2K);
204
205         day = BCD2BIN(day);
206         mon = BCD2BIN(mon);
207         year = BCD2BIN(year);
208         y2k = BCD2BIN(y2k);
209
210         year += (y2k * 100);
211
212         return mktime(year, mon, day, hour, min, sec);
213 }
214
215 /*
216  *  Bring up the timer at 100 Hz.
217  */
218 void __init swarm_time_init(void)
219 {
220         unsigned int flags;
221         int status;
222
223         /* Set up the scd general purpose timer 0 to cpu 0 */
224         sb1250_time_init();
225
226         /* Establish communication with the Xicor 1241 RTC */
227         /* XXXKW how do I share the SMBus with the I2C subsystem? */
228
229         __raw_writeq(K_SMB_FREQ_400KHZ, SMB_CSR(R_SMB_FREQ));
230         __raw_writeq(0, SMB_CSR(R_SMB_CONTROL));
231
232         if ((status = xicor_read(X1241REG_SR_RTCF)) < 0) {
233                 printk("x1241: couldn't detect on SWARM SMBus 1\n");
234         } else {
235                 if (status & X1241REG_SR_RTCF)
236                         printk("x1241: battery failed -- time is probably wrong\n");
237                 write_seqlock_irqsave(&xtime_lock, flags);
238                 xtime.tv_sec = get_swarm_time();
239                 xtime.tv_nsec = 0;
240                 write_sequnlock_irqrestore(&xtime_lock, flags);
241         }
242 }