- pre2
[davej-history.git] / drivers / sbus / char / rtc.c
blobd8454cf25860ebb1e7b5b01bb5578d7729287be9
1 /* $Id: rtc.c,v 1.23 2000/08/29 07:01:55 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/malloc.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>
26 #include <asm/rtc.h>
28 static int rtc_busy = 0;
30 /* Retrieve the current date and time from the real time clock. */
31 void get_rtc_time(struct rtc_time *t)
33 unsigned long regs = mstk48t02_regs;
34 unsigned long flags;
35 u8 tmp;
37 save_flags(flags);
38 cli();
40 tmp = mostek_read(regs + MOSTEK_CREG);
41 tmp |= MSTK_CREG_READ;
42 mostek_write(regs + MOSTEK_CREG, tmp);
44 t->sec = MSTK_REG_SEC(regs);
45 t->min = MSTK_REG_MIN(regs);
46 t->hour = MSTK_REG_HOUR(regs);
47 t->dow = MSTK_REG_DOW(regs);
48 t->dom = MSTK_REG_DOM(regs);
49 t->month = MSTK_REG_MONTH(regs);
50 t->year = MSTK_CVT_YEAR( MSTK_REG_YEAR(regs) );
52 tmp = mostek_read(regs + MOSTEK_CREG);
53 tmp &= ~MSTK_CREG_READ;
54 mostek_write(regs + MOSTEK_CREG, tmp);
55 restore_flags(flags);
58 /* Set the current date and time inthe real time clock. */
59 void set_rtc_time(struct rtc_time *t)
61 unsigned long regs = mstk48t02_regs;
62 unsigned long flags;
63 u8 tmp;
65 save_flags(flags);
66 cli();
67 tmp = mostek_read(regs + MOSTEK_CREG);
68 tmp |= MSTK_CREG_WRITE;
69 mostek_write(regs + MOSTEK_CREG, tmp);
71 MSTK_SET_REG_SEC(regs,t->sec);
72 MSTK_SET_REG_MIN(regs,t->min);
73 MSTK_SET_REG_HOUR(regs,t->hour);
74 MSTK_SET_REG_DOW(regs,t->dow);
75 MSTK_SET_REG_DOM(regs,t->dom);
76 MSTK_SET_REG_MONTH(regs,t->month);
77 MSTK_SET_REG_YEAR(regs,t->year - MSTK_YEAR_ZERO);
79 tmp = mostek_read(regs + MOSTEK_CREG);
80 tmp &= ~MSTK_CREG_WRITE;
81 mostek_write(regs + MOSTEK_CREG, tmp);
82 restore_flags(flags);
85 static long long rtc_lseek(struct file *file, long long offset, int origin)
87 return -ESPIPE;
90 static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
91 unsigned long arg)
93 struct rtc_time rtc_tm;
95 switch (cmd)
97 case RTCGET:
98 get_rtc_time(&rtc_tm);
100 if (copy_to_user((struct rtc_time*)arg, &rtc_tm, sizeof(struct rtc_time)))
101 return -EFAULT;
103 return 0;
106 case RTCSET:
107 if (!capable(CAP_SYS_TIME))
108 return -EPERM;
110 if (copy_from_user(&rtc_tm, (struct rtc_time*)arg, sizeof(struct rtc_time)))
111 return -EFAULT;
113 set_rtc_time(&rtc_tm);
115 return 0;
117 default:
118 return -EINVAL;
122 static int rtc_open(struct inode *inode, struct file *file)
125 if (rtc_busy)
126 return -EBUSY;
128 rtc_busy = 1;
130 return 0;
133 static int rtc_release(struct inode *inode, struct file *file)
135 lock_kernel();
136 rtc_busy = 0;
137 unlock_kernel();
138 return 0;
141 static struct file_operations rtc_fops = {
142 owner: THIS_MODULE,
143 llseek: rtc_lseek,
144 ioctl: rtc_ioctl,
145 open: rtc_open,
146 release: rtc_release,
149 static struct miscdevice rtc_dev = { RTC_MINOR, "rtc", &rtc_fops };
151 EXPORT_NO_SYMBOLS;
153 #ifdef MODULE
154 int init_module(void)
155 #else
156 int __init rtc_sun_init(void)
157 #endif
159 int error;
161 if (mstk48t02_regs == 0) {
162 /* This diagnostic is a debugging aid... But a useful one. */
163 printk(KERN_ERR "rtc: no Mostek in this computer\n");
164 return -ENODEV;
167 error = misc_register(&rtc_dev);
168 if (error) {
169 printk(KERN_ERR "rtc: unable to get misc minor for Mostek\n");
170 return error;
173 return 0;
176 #ifdef MODULE
177 void cleanup_module(void)
179 misc_deregister(&rtc_dev);
181 #endif