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;
46 spin_lock_irqsave(&rtc_lock, flags);
47 /* let us freeze external registers */
53 year = BCD2BIN(READ_RTC(0xA)) + EPOCH;
54 month = BCD2BIN(READ_RTC(0x9) & 0x1f);
55 day = BCD2BIN(READ_RTC(0x8));
56 minute = BCD2BIN(READ_RTC(0x2));
57 second = BCD2BIN(READ_RTC(0x1));
59 /* hour is special - deal with it later */
62 /* enable time transfer */
65 spin_unlock_irqrestore(&rtc_lock, flags);
70 hour = BCD2BIN(temp & 0x1f);
71 if (temp & 0x20) hour += 12; /* PM */
74 hour = BCD2BIN(temp & 0x3f);
77 return mktime(year, month, day, hour, minute, second);
81 rtc_ds1386_set_time(unsigned long t)
86 u8 year, month, day, hour, minute, second;
89 spin_lock_irqsave(&rtc_lock, flags);
90 /* let us freeze external registers */
99 /* check each field one by one */
100 year = BIN2BCD(tm.tm_year - EPOCH);
101 if (year != READ_RTC(0xA)) {
102 WRITE_RTC(0xA, year);
105 temp = READ_RTC(0x9);
106 month = BIN2BCD(tm.tm_mon+1); /* tm_mon starts from 0 to 11 */
107 if (month != (temp & 0x1f)) {
109 (month & 0x1f) | (temp & ~0x1f) );
112 day = BIN2BCD(tm.tm_mday);
113 if (day != READ_RTC(0x8)) {
117 temp = READ_RTC(0x4);
121 if (tm.tm_hour > 12) {
122 hour |= 0x20 | (BIN2BCD(hour-12) & 0x1f);
124 hour |= BIN2BCD(tm.tm_hour);
128 hour = BIN2BCD(tm.tm_hour) & 0x3f;
130 if (hour != temp) WRITE_RTC(0x4, hour);
132 minute = BIN2BCD(tm.tm_min);
133 if (minute != READ_RTC(0x2)) {
134 WRITE_RTC(0x2, minute);
137 second = BIN2BCD(tm.tm_sec);
138 if (second != READ_RTC(0x1)) {
139 WRITE_RTC(0x1, second);
141 spin_unlock_irqrestore(&rtc_lock, flags);
147 rtc_ds1386_init(unsigned long base)
151 /* remember the base */
153 db_assert((rtc_base & 0xe0000000) == KSEG1);
155 /* turn on RTC if it is not on */
156 byte = READ_RTC(0x9);
159 WRITE_RTC(0x9, byte);
162 /* enable time transfer */
163 byte = READ_RTC(0xB);
165 WRITE_RTC(0xB, byte);
167 /* set the function pointers */
168 rtc_mips_get_time = rtc_ds1386_get_time;
169 rtc_mips_set_time = rtc_ds1386_set_time;