1 /*!***************************************************************************
3 *! FILE NAME : ds1302.c
5 *! DESCRIPTION: Implements an interface for the DS1302 RTC
7 *! Functions exported: ds1302_readreg, ds1302_writereg, ds1302_init, get_rtc_status
9 *! ---------------------------------------------------------------------------
11 *! (C) Copyright 1999, 2000, 2001 Axis Communications AB, LUND, SWEDEN
13 *!***************************************************************************/
17 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/miscdevice.h>
21 #include <linux/delay.h>
22 #include <linux/bcd.h>
23 #include <linux/mutex.h>
24 #include <linux/uaccess.h>
27 #include <asm/system.h>
29 #if defined(CONFIG_M32R)
33 #define RTC_MAJOR_NR 121 /* local major, change later */
35 static DEFINE_MUTEX(rtc_mutex);
36 static const char ds1302_name[] = "ds1302";
40 out_byte_rtc(unsigned int reg_addr, unsigned char x)
43 outw(0x0001,(unsigned long)PLD_RTCRSTODT);
45 outw(((x<<8)|(reg_addr&0xff)),(unsigned long)PLD_RTCWRDATA);
47 outw(0x0002,(unsigned long)PLD_RTCCR);
49 while(inw((unsigned long)PLD_RTCCR));
52 outw(0x0000,(unsigned long)PLD_RTCRSTODT);
57 in_byte_rtc(unsigned int reg_addr)
62 outw(0x0001,(unsigned long)PLD_RTCRSTODT);
64 outw((reg_addr&0xff),(unsigned long)PLD_RTCRDDATA);
66 outw(0x0001,(unsigned long)PLD_RTCCR);
68 while(inw((unsigned long)PLD_RTCCR));
71 retval=(inw((unsigned long)PLD_RTCRDDATA) & 0xff00)>>8;
74 outw(0x0000,(unsigned long)PLD_RTCRSTODT);
84 out_byte_rtc(0x8e,0x00);
87 /* Disable writing. */
92 out_byte_rtc(0x8e,0x80);
97 /* Read a byte from the selected register in the DS1302. */
100 ds1302_readreg(int reg)
104 x=in_byte_rtc((0x81 | (reg << 1))); /* read register */
109 /* Write a byte to the selected register. */
112 ds1302_writereg(int reg, unsigned char val)
115 out_byte_rtc((0x80 | (reg << 1)),val);
120 get_rtc_time(struct rtc_time *rtc_tm)
124 local_irq_save(flags);
126 rtc_tm->tm_sec = CMOS_READ(RTC_SECONDS);
127 rtc_tm->tm_min = CMOS_READ(RTC_MINUTES);
128 rtc_tm->tm_hour = CMOS_READ(RTC_HOURS);
129 rtc_tm->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
130 rtc_tm->tm_mon = CMOS_READ(RTC_MONTH);
131 rtc_tm->tm_year = CMOS_READ(RTC_YEAR);
133 local_irq_restore(flags);
135 rtc_tm->tm_sec = bcd2bin(rtc_tm->tm_sec);
136 rtc_tm->tm_min = bcd2bin(rtc_tm->tm_min);
137 rtc_tm->tm_hour = bcd2bin(rtc_tm->tm_hour);
138 rtc_tm->tm_mday = bcd2bin(rtc_tm->tm_mday);
139 rtc_tm->tm_mon = bcd2bin(rtc_tm->tm_mon);
140 rtc_tm->tm_year = bcd2bin(rtc_tm->tm_year);
143 * Account for differences between how the RTC uses the values
144 * and how they are defined in a struct rtc_time;
147 if (rtc_tm->tm_year <= 69)
148 rtc_tm->tm_year += 100;
153 static unsigned char days_in_mo[] =
154 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
156 /* ioctl that supports RTC_RD_TIME and RTC_SET_TIME (read and set time/date). */
158 static long rtc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
163 case RTC_RD_TIME: /* read the time/date from RTC */
165 struct rtc_time rtc_tm;
167 memset(&rtc_tm, 0, sizeof (struct rtc_time));
168 mutex_lock(&rtc_mutex);
169 get_rtc_time(&rtc_tm);
170 mutex_unlock(&rtc_mutex);
171 if (copy_to_user((struct rtc_time*)arg, &rtc_tm, sizeof(struct rtc_time)))
176 case RTC_SET_TIME: /* set the RTC */
178 struct rtc_time rtc_tm;
179 unsigned char mon, day, hrs, min, sec, leap_yr;
182 if (!capable(CAP_SYS_TIME))
185 if (copy_from_user(&rtc_tm, (struct rtc_time*)arg, sizeof(struct rtc_time)))
188 yrs = rtc_tm.tm_year + 1900;
189 mon = rtc_tm.tm_mon + 1; /* tm_mon starts at zero */
190 day = rtc_tm.tm_mday;
191 hrs = rtc_tm.tm_hour;
196 if ((yrs < 1970) || (yrs > 2069))
199 leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
201 if ((mon > 12) || (day == 0))
204 if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
207 if ((hrs >= 24) || (min >= 60) || (sec >= 60))
211 yrs -= 2000; /* RTC (0, 1, ... 69) */
213 yrs -= 1900; /* RTC (70, 71, ... 99) */
222 mutex_lock(&rtc_mutex);
223 local_irq_save(flags);
224 CMOS_WRITE(yrs, RTC_YEAR);
225 CMOS_WRITE(mon, RTC_MONTH);
226 CMOS_WRITE(day, RTC_DAY_OF_MONTH);
227 CMOS_WRITE(hrs, RTC_HOURS);
228 CMOS_WRITE(min, RTC_MINUTES);
229 CMOS_WRITE(sec, RTC_SECONDS);
230 local_irq_restore(flags);
231 mutex_unlock(&rtc_mutex);
233 /* Notice that at this point, the RTC is updated but
234 * the kernel is still running with the old time.
235 * You need to set that separately with settimeofday
241 case RTC_SET_CHARGE: /* set the RTC TRICKLE CHARGE register */
245 if (!capable(CAP_SYS_TIME))
248 if(copy_from_user(&tcs_val, (int*)arg, sizeof(int)))
251 mutex_lock(&rtc_mutex);
252 tcs_val = RTC_TCR_PATTERN | (tcs_val & 0x0F);
253 ds1302_writereg(RTC_TRICKLECHARGER, tcs_val);
254 mutex_unlock(&rtc_mutex);
263 get_rtc_status(char *buf)
273 * There is no way to tell if the luser has the RTC set for local
274 * time or for Universal Standard Time (GMT). Probably local though.
278 "rtc_time\t: %02d:%02d:%02d\n"
279 "rtc_date\t: %04d-%02d-%02d\n",
280 tm.tm_hour, tm.tm_min, tm.tm_sec,
281 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
287 /* The various file operations we support. */
289 static const struct file_operations rtc_fops = {
290 .owner = THIS_MODULE,
291 .unlocked_ioctl = rtc_ioctl,
292 .llseek = noop_llseek,
295 /* Probe for the chip by writing something to its RAM and try reading it back. */
297 #define MAGIC_PATTERN 0x42
302 int retval, res, baur;
304 baur=(boot_cpu_data.bus_clock/(2*1000*1000));
306 printk("%s: Set PLD_RTCBAUR = %d\n", ds1302_name,baur);
308 outw(0x0000,(unsigned long)PLD_RTCCR);
309 outw(0x0000,(unsigned long)PLD_RTCRSTODT);
310 outw(baur,(unsigned long)PLD_RTCBAUR);
312 /* Try to talk to timekeeper. */
315 /* write RAM byte 0 */
316 /* write something magic */
317 out_byte_rtc(0xc0,MAGIC_PATTERN);
319 /* read RAM byte 0 */
320 if((res = in_byte_rtc(0xc1)) == MAGIC_PATTERN) {
323 printk("%s: RTC found.\n", ds1302_name);
328 printk("%s: RTC not found.\n", ds1302_name);
336 /* Just probe for the RTC and register the device to handle the ioctl needed. */
341 if (!ds1302_probe()) {
347 static int __init ds1302_register(void)
350 if (register_chrdev(RTC_MAJOR_NR, ds1302_name, &rtc_fops)) {
351 printk(KERN_INFO "%s: unable to get major %d for rtc\n",
352 ds1302_name, RTC_MAJOR_NR);
358 module_init(ds1302_register);