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>
25 #include <asm/addrspace.h>
27 #include <asm/mc146818rtc.h>
28 #include <asm/debug.h>
33 #define BCD_TO_BIN(val) (((val)&15) + ((val)>>4)*10)
36 #define BIN_TO_BCD(val) ((((val)/10)<<4) + (val)%10)
38 #define READ_RTC(x) *(volatile unsigned char*)(rtc_base+x)
39 #define WRITE_RTC(x, y) *(volatile unsigned char*)(rtc_base+x) = y
41 static unsigned long rtc_base;
44 rtc_ds1386_get_time(void)
48 unsigned int year, month, day, hour, minute, second;
50 /* let us freeze external registers */
56 year = BCD_TO_BIN(READ_RTC(0xA)) + EPOCH;
57 month = BCD_TO_BIN(READ_RTC(0x9) & 0x1f);
58 day = BCD_TO_BIN(READ_RTC(0x8));
59 minute = BCD_TO_BIN(READ_RTC(0x2));
60 second = BCD_TO_BIN(READ_RTC(0x1));
62 /* hour is special - deal with it later */
65 /* enable time transfer */
72 hour = BCD_TO_BIN(temp & 0x1f);
73 if (temp & 0x20) hour += 12; /* PM */
76 hour = BCD_TO_BIN(temp & 0x3f);
79 return mktime(year, month, day, hour, minute, second);
83 rtc_ds1386_set_time(unsigned long t)
88 u8 year, month, day, hour, minute, second;
90 /* let us freeze external registers */
99 /* check each field one by one */
100 year = BIN_TO_BCD(tm.tm_year - EPOCH);
101 if (year != READ_RTC(0xA)) {
102 WRITE_RTC(0xA, year);
105 temp = READ_RTC(0x9);
106 month = BIN_TO_BCD(tm.tm_mon+1); /* tm_mon starts from 0 to 11 */
107 if (month != (temp & 0x1f)) {
109 (month & 0x1f) | (temp & ~0x1f) );
112 day = BIN_TO_BCD(tm.tm_mday);
113 if (day != READ_RTC(0x8)) {
117 temp = READ_RTC(0x4);
121 if (tm.tm_hour > 12) {
122 hour |= 0x20 | (BIN_TO_BCD(hour-12) & 0x1f);
124 hour |= BIN_TO_BCD(tm.tm_hour);
128 hour = BIN_TO_BCD(tm.tm_hour) & 0x3f;
130 if (hour != temp) WRITE_RTC(0x4, hour);
132 minute = BIN_TO_BCD(tm.tm_min);
133 if (minute != READ_RTC(0x2)) {
134 WRITE_RTC(0x2, minute);
137 second = BIN_TO_BCD(tm.tm_sec);
138 if (second != READ_RTC(0x1)) {
139 WRITE_RTC(0x1, second);
146 rtc_ds1386_init(unsigned long base)
150 /* remember the base */
152 db_assert((rtc_base & 0xe0000000) == KSEG1);
154 /* turn on RTC if it is not on */
155 byte = READ_RTC(0x9);
158 WRITE_RTC(0x9, byte);
161 /* enable time transfer */
162 byte = READ_RTC(0xB);
164 WRITE_RTC(0xB, byte);
166 /* set the function pointers */
167 rtc_get_time = rtc_ds1386_get_time;
168 rtc_set_time = rtc_ds1386_set_time;