1 /* $Id: rtc.c,v 1.18 1999/08/31 18:51:36 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.rutgers.edu/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 <asm/mostek.h>
23 #include <asm/system.h>
24 #include <asm/uaccess.h>
27 static int rtc_busy
= 0;
29 /* Retrieve the current date and time from the real time clock. */
30 void get_rtc_time(struct rtc_time
*t
)
32 unsigned long regs
= mstk48t02_regs
;
39 tmp
= mostek_read(regs
+ MOSTEK_CREG
);
40 tmp
|= MSTK_CREG_READ
;
41 mostek_write(regs
+ MOSTEK_CREG
, tmp
);
43 t
->sec
= MSTK_REG_SEC(regs
);
44 t
->min
= MSTK_REG_MIN(regs
);
45 t
->hour
= MSTK_REG_HOUR(regs
);
46 t
->dow
= MSTK_REG_DOW(regs
);
47 t
->dom
= MSTK_REG_DOM(regs
);
48 t
->month
= MSTK_REG_MONTH(regs
);
49 t
->year
= MSTK_CVT_YEAR( MSTK_REG_YEAR(regs
) );
51 tmp
= mostek_read(regs
+ MOSTEK_CREG
);
52 tmp
&= ~MSTK_CREG_READ
;
53 mostek_write(regs
+ MOSTEK_CREG
, tmp
);
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
;
66 tmp
= mostek_read(regs
+ MOSTEK_CREG
);
67 tmp
|= MSTK_CREG_WRITE
;
68 mostek_write(regs
+ MOSTEK_CREG
, tmp
);
70 MSTK_SET_REG_SEC(regs
,t
->sec
);
71 MSTK_SET_REG_MIN(regs
,t
->min
);
72 MSTK_SET_REG_HOUR(regs
,t
->hour
);
73 MSTK_SET_REG_DOW(regs
,t
->dow
);
74 MSTK_SET_REG_DOM(regs
,t
->dom
);
75 MSTK_SET_REG_MONTH(regs
,t
->month
);
76 MSTK_SET_REG_YEAR(regs
,t
->year
- MSTK_YEAR_ZERO
);
78 tmp
= mostek_read(regs
+ MOSTEK_CREG
);
79 tmp
&= ~MSTK_CREG_WRITE
;
80 mostek_write(regs
+ MOSTEK_CREG
, tmp
);
84 static long long rtc_lseek(struct file
*file
, long long offset
, int origin
)
89 static int rtc_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
,
92 struct rtc_time rtc_tm
;
97 get_rtc_time(&rtc_tm
);
99 copy_to_user_ret((struct rtc_time
*)arg
, &rtc_tm
, sizeof(struct rtc_time
), -EFAULT
);
105 if (!capable(CAP_SYS_TIME
))
108 copy_from_user_ret(&rtc_tm
, (struct rtc_time
*)arg
, sizeof(struct rtc_time
), -EFAULT
);
110 set_rtc_time(&rtc_tm
);
119 static int rtc_open(struct inode
*inode
, struct file
*file
)
132 static int rtc_release(struct inode
*inode
, struct file
*file
)
139 static struct file_operations rtc_fops
= {
142 NULL
, /* rtc_write */
143 NULL
, /* rtc_readdir */
152 static struct miscdevice rtc_dev
= { RTC_MINOR
, "rtc", &rtc_fops
};
157 int init_module(void)
159 int __init
rtc_sun_init(void)
164 if (mstk48t02_regs
== 0) {
165 /* This diagnostic is a debugging aid... But a useful one. */
166 printk(KERN_ERR
"rtc: no Mostek in this computer\n");
170 error
= misc_register(&rtc_dev
);
172 printk(KERN_ERR
"rtc: unable to get misc minor for Mostek\n");
180 void cleanup_module(void)
182 misc_deregister(&rtc_dev
);