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>
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
;
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
);
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
;
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
);
85 static long long rtc_lseek(struct file
*file
, long long offset
, int origin
)
90 static int rtc_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
,
93 struct rtc_time rtc_tm
;
98 get_rtc_time(&rtc_tm
);
100 if (copy_to_user((struct rtc_time
*)arg
, &rtc_tm
, sizeof(struct rtc_time
)))
107 if (!capable(CAP_SYS_TIME
))
110 if (copy_from_user(&rtc_tm
, (struct rtc_time
*)arg
, sizeof(struct rtc_time
)))
113 set_rtc_time(&rtc_tm
);
122 static int rtc_open(struct inode
*inode
, struct file
*file
)
133 static int rtc_release(struct inode
*inode
, struct file
*file
)
141 static struct file_operations rtc_fops
= {
146 release
: rtc_release
,
149 static struct miscdevice rtc_dev
= { RTC_MINOR
, "rtc", &rtc_fops
};
154 int init_module(void)
156 int __init
rtc_sun_init(void)
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");
167 error
= misc_register(&rtc_dev
);
169 printk(KERN_ERR
"rtc: unable to get misc minor for Mostek\n");
177 void cleanup_module(void)
179 misc_deregister(&rtc_dev
);