1 /* $Id: rtc.c,v 1.28 2001/10/08 22:19:51 davem Exp $
3 * Linux/SPARC Real Time Clock Driver
4 * Copyright (C) 1996 Thomas K. Dyas (tdyas@eden.rutgers.edu)
6 * This is a little driver that lets a user-level program access
7 * the SPARC Mostek real time clock chip. It is no use unless you
8 * use the modified clock utility.
10 * Get the modified clock utility from:
11 * ftp://vger.kernel.org/pub/linux/Sparc/userland/clock.c
14 #include <linux/module.h>
15 #include <linux/types.h>
16 #include <linux/errno.h>
17 #include <linux/miscdevice.h>
18 #include <linux/slab.h>
19 #include <linux/fcntl.h>
20 #include <linux/poll.h>
21 #include <linux/init.h>
22 #include <linux/smp_lock.h>
23 #include <asm/mostek.h>
24 #include <asm/system.h>
25 #include <asm/uaccess.h>
28 static int rtc_busy = 0;
30 /* Retrieve the current date and time from the real time clock. */
31 static void get_rtc_time(struct rtc_time *t)
33 unsigned long regs = mstk48t02_regs;
36 spin_lock_irq(&mostek_lock);
38 tmp = mostek_read(regs + MOSTEK_CREG);
39 tmp |= MSTK_CREG_READ;
40 mostek_write(regs + MOSTEK_CREG, tmp);
42 t->sec = MSTK_REG_SEC(regs);
43 t->min = MSTK_REG_MIN(regs);
44 t->hour = MSTK_REG_HOUR(regs);
45 t->dow = MSTK_REG_DOW(regs);
46 t->dom = MSTK_REG_DOM(regs);
47 t->month = MSTK_REG_MONTH(regs);
48 t->year = MSTK_CVT_YEAR( MSTK_REG_YEAR(regs) );
50 tmp = mostek_read(regs + MOSTEK_CREG);
51 tmp &= ~MSTK_CREG_READ;
52 mostek_write(regs + MOSTEK_CREG, tmp);
54 spin_unlock_irq(&mostek_lock);
57 /* Set the current date and time inthe real time clock. */
58 void set_rtc_time(struct rtc_time *t)
60 unsigned long regs = mstk48t02_regs;
63 spin_lock_irq(&mostek_lock);
65 tmp = mostek_read(regs + MOSTEK_CREG);
66 tmp |= MSTK_CREG_WRITE;
67 mostek_write(regs + MOSTEK_CREG, tmp);
69 MSTK_SET_REG_SEC(regs,t->sec);
70 MSTK_SET_REG_MIN(regs,t->min);
71 MSTK_SET_REG_HOUR(regs,t->hour);
72 MSTK_SET_REG_DOW(regs,t->dow);
73 MSTK_SET_REG_DOM(regs,t->dom);
74 MSTK_SET_REG_MONTH(regs,t->month);
75 MSTK_SET_REG_YEAR(regs,t->year - MSTK_YEAR_ZERO);
77 tmp = mostek_read(regs + MOSTEK_CREG);
78 tmp &= ~MSTK_CREG_WRITE;
79 mostek_write(regs + MOSTEK_CREG, tmp);
81 spin_unlock_irq(&mostek_lock);
84 static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
87 struct rtc_time rtc_tm;
92 memset(&rtc_tm, 0, sizeof(struct rtc_time));
93 get_rtc_time(&rtc_tm);
95 if (copy_to_user((struct rtc_time*)arg, &rtc_tm, sizeof(struct rtc_time)))
102 if (!capable(CAP_SYS_TIME))
105 if (copy_from_user(&rtc_tm, (struct rtc_time*)arg, sizeof(struct rtc_time)))
108 set_rtc_time(&rtc_tm);
117 static int rtc_open(struct inode *inode, struct file *file)
121 spin_lock_irq(&mostek_lock);
128 spin_unlock_irq(&mostek_lock);
133 static int rtc_release(struct inode *inode, struct file *file)
140 static struct file_operations rtc_fops = {
141 .owner = THIS_MODULE,
145 .release = rtc_release,
148 static struct miscdevice rtc_dev = { RTC_MINOR, "rtc", &rtc_fops };
150 static int __init rtc_sun_init(void)
154 /* It is possible we are being driven by some other RTC chip
155 * and thus another RTC driver is handling things.
157 if (mstk48t02_regs == 0)
160 error = misc_register(&rtc_dev);
162 printk(KERN_ERR "rtc: unable to get misc minor for Mostek\n");
169 static void __exit rtc_sun_cleanup(void)
171 misc_deregister(&rtc_dev);
174 module_init(rtc_sun_init);
175 module_exit(rtc_sun_cleanup);
176 MODULE_LICENSE("GPL");