2 * Copyright 2001 MontaVista Software Inc.
3 * Author: jsun@mvista.com or jsun@junsun.net
5 * arch/mips/ddb5xxx/common/rtc_ds1386.c
6 * low-level RTC hookups for s for Dallas 1396 chip.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
16 * This file exports a function, rtc_ds1386_init(), which expects an
17 * uncached base address as the argument. It will set the two function
18 * pointers expected by the MIPS generic timer code.
21 #include <linux/types.h>
22 #include <linux/time.h>
23 #include <linux/bcd.h>
26 #include <asm/addrspace.h>
28 #include <asm/mc146818rtc.h>
29 #include <asm/debug.h>
33 #define READ_RTC(x) *(volatile unsigned char*)(rtc_base+x)
34 #define WRITE_RTC(x, y) *(volatile unsigned char*)(rtc_base+x) = y
36 static unsigned long rtc_base
;
39 rtc_ds1386_get_time(void)
43 unsigned int year
, month
, day
, hour
, minute
, second
;
45 /* let us freeze external registers */
51 year
= BCD2BIN(READ_RTC(0xA)) + EPOCH
;
52 month
= BCD2BIN(READ_RTC(0x9) & 0x1f);
53 day
= BCD2BIN(READ_RTC(0x8));
54 minute
= BCD2BIN(READ_RTC(0x2));
55 second
= BCD2BIN(READ_RTC(0x1));
57 /* hour is special - deal with it later */
60 /* enable time transfer */
67 hour
= BCD2BIN(temp
& 0x1f);
68 if (temp
& 0x20) hour
+= 12; /* PM */
71 hour
= BCD2BIN(temp
& 0x3f);
74 return mktime(year
, month
, day
, hour
, minute
, second
);
78 rtc_ds1386_set_time(unsigned long t
)
83 u8 year
, month
, day
, hour
, minute
, second
;
85 /* let us freeze external registers */
94 /* check each field one by one */
95 year
= BIN2BCD(tm
.tm_year
- EPOCH
);
96 if (year
!= READ_RTC(0xA)) {
100 temp
= READ_RTC(0x9);
101 month
= BIN2BCD(tm
.tm_mon
+1); /* tm_mon starts from 0 to 11 */
102 if (month
!= (temp
& 0x1f)) {
104 (month
& 0x1f) | (temp
& ~0x1f) );
107 day
= BIN2BCD(tm
.tm_mday
);
108 if (day
!= READ_RTC(0x8)) {
112 temp
= READ_RTC(0x4);
116 if (tm
.tm_hour
> 12) {
117 hour
|= 0x20 | (BIN2BCD(hour
-12) & 0x1f);
119 hour
|= BIN2BCD(tm
.tm_hour
);
123 hour
= BIN2BCD(tm
.tm_hour
) & 0x3f;
125 if (hour
!= temp
) WRITE_RTC(0x4, hour
);
127 minute
= BIN2BCD(tm
.tm_min
);
128 if (minute
!= READ_RTC(0x2)) {
129 WRITE_RTC(0x2, minute
);
132 second
= BIN2BCD(tm
.tm_sec
);
133 if (second
!= READ_RTC(0x1)) {
134 WRITE_RTC(0x1, second
);
141 rtc_ds1386_init(unsigned long base
)
145 /* remember the base */
147 db_assert((rtc_base
& 0xe0000000) == KSEG1
);
149 /* turn on RTC if it is not on */
150 byte
= READ_RTC(0x9);
153 WRITE_RTC(0x9, byte
);
156 /* enable time transfer */
157 byte
= READ_RTC(0xB);
159 WRITE_RTC(0xB, byte
);
161 /* set the function pointers */
162 rtc_get_time
= rtc_ds1386_get_time
;
163 rtc_set_time
= rtc_ds1386_set_time
;