Linux-2.6.12-rc2
[linux-2.6/next.git] / drivers / char / ds1302.c
bloba75e8609be01f65f605d13a8f063ac6f5f93496d
1 /*!***************************************************************************
2 *!
3 *! FILE NAME : ds1302.c
4 *!
5 *! DESCRIPTION: Implements an interface for the DS1302 RTC
6 *!
7 *! Functions exported: ds1302_readreg, ds1302_writereg, ds1302_init, get_rtc_status
8 *!
9 *! ---------------------------------------------------------------------------
11 *! (C) Copyright 1999, 2000, 2001 Axis Communications AB, LUND, SWEDEN
13 *!***************************************************************************/
15 #include <linux/config.h>
17 #include <linux/fs.h>
18 #include <linux/init.h>
19 #include <linux/mm.h>
20 #include <linux/module.h>
21 #include <linux/miscdevice.h>
22 #include <linux/delay.h>
23 #include <linux/bcd.h>
25 #include <asm/uaccess.h>
26 #include <asm/system.h>
27 #include <asm/io.h>
28 #include <asm/rtc.h>
29 #if defined(CONFIG_M32R)
30 #include <asm/m32r.h>
31 #endif
33 #define RTC_MAJOR_NR 121 /* local major, change later */
35 static const char ds1302_name[] = "ds1302";
37 /* Send 8 bits. */
38 static void
39 out_byte_rtc(unsigned int reg_addr, unsigned char x)
41 //RST H
42 outw(0x0001,(unsigned long)PLD_RTCRSTODT);
43 //write data
44 outw(((x<<8)|(reg_addr&0xff)),(unsigned long)PLD_RTCWRDATA);
45 //WE
46 outw(0x0002,(unsigned long)PLD_RTCCR);
47 //wait
48 while(inw((unsigned long)PLD_RTCCR));
50 //RST L
51 outw(0x0000,(unsigned long)PLD_RTCRSTODT);
55 static unsigned char
56 in_byte_rtc(unsigned int reg_addr)
58 unsigned char retval;
60 //RST H
61 outw(0x0001,(unsigned long)PLD_RTCRSTODT);
62 //write data
63 outw((reg_addr&0xff),(unsigned long)PLD_RTCRDDATA);
64 //RE
65 outw(0x0001,(unsigned long)PLD_RTCCR);
66 //wait
67 while(inw((unsigned long)PLD_RTCCR));
69 //read data
70 retval=(inw((unsigned long)PLD_RTCRDDATA) & 0xff00)>>8;
72 //RST L
73 outw(0x0000,(unsigned long)PLD_RTCRSTODT);
75 return retval;
78 /* Enable writing. */
80 static void
81 ds1302_wenable(void)
83 out_byte_rtc(0x8e,0x00);
86 /* Disable writing. */
88 static void
89 ds1302_wdisable(void)
91 out_byte_rtc(0x8e,0x80);
96 /* Read a byte from the selected register in the DS1302. */
98 unsigned char
99 ds1302_readreg(int reg)
101 unsigned char x;
103 x=in_byte_rtc((0x81 | (reg << 1))); /* read register */
105 return x;
108 /* Write a byte to the selected register. */
110 void
111 ds1302_writereg(int reg, unsigned char val)
113 ds1302_wenable();
114 out_byte_rtc((0x80 | (reg << 1)),val);
115 ds1302_wdisable();
118 void
119 get_rtc_time(struct rtc_time *rtc_tm)
121 unsigned long flags;
123 local_irq_save(flags);
124 local_irq_disable();
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 BCD_TO_BIN(rtc_tm->tm_sec);
136 BCD_TO_BIN(rtc_tm->tm_min);
137 BCD_TO_BIN(rtc_tm->tm_hour);
138 BCD_TO_BIN(rtc_tm->tm_mday);
139 BCD_TO_BIN(rtc_tm->tm_mon);
140 BCD_TO_BIN(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;
150 rtc_tm->tm_mon--;
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 int
159 rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
160 unsigned long arg)
162 unsigned long flags;
164 switch(cmd) {
165 case RTC_RD_TIME: /* read the time/date from RTC */
167 struct rtc_time rtc_tm;
169 memset(&rtc_tm, 0, sizeof (struct rtc_time));
170 get_rtc_time(&rtc_tm);
171 if (copy_to_user((struct rtc_time*)arg, &rtc_tm, sizeof(struct rtc_time)))
172 return -EFAULT;
173 return 0;
176 case RTC_SET_TIME: /* set the RTC */
178 struct rtc_time rtc_tm;
179 unsigned char mon, day, hrs, min, sec, leap_yr;
180 unsigned int yrs;
182 if (!capable(CAP_SYS_TIME))
183 return -EPERM;
185 if (copy_from_user(&rtc_tm, (struct rtc_time*)arg, sizeof(struct rtc_time)))
186 return -EFAULT;
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;
192 min = rtc_tm.tm_min;
193 sec = rtc_tm.tm_sec;
196 if ((yrs < 1970) || (yrs > 2069))
197 return -EINVAL;
199 leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
201 if ((mon > 12) || (day == 0))
202 return -EINVAL;
204 if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
205 return -EINVAL;
207 if ((hrs >= 24) || (min >= 60) || (sec >= 60))
208 return -EINVAL;
210 if (yrs >= 2000)
211 yrs -= 2000; /* RTC (0, 1, ... 69) */
212 else
213 yrs -= 1900; /* RTC (70, 71, ... 99) */
215 BIN_TO_BCD(sec);
216 BIN_TO_BCD(min);
217 BIN_TO_BCD(hrs);
218 BIN_TO_BCD(day);
219 BIN_TO_BCD(mon);
220 BIN_TO_BCD(yrs);
222 local_irq_save(flags);
223 local_irq_disable();
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);
232 /* Notice that at this point, the RTC is updated but
233 * the kernel is still running with the old time.
234 * You need to set that separately with settimeofday
235 * or adjtimex.
237 return 0;
240 case RTC_SET_CHARGE: /* set the RTC TRICKLE CHARGE register */
242 int tcs_val;
244 if (!capable(CAP_SYS_TIME))
245 return -EPERM;
247 if(copy_from_user(&tcs_val, (int*)arg, sizeof(int)))
248 return -EFAULT;
250 tcs_val = RTC_TCR_PATTERN | (tcs_val & 0x0F);
251 ds1302_writereg(RTC_TRICKLECHARGER, tcs_val);
252 return 0;
254 default:
255 return -EINVAL;
260 get_rtc_status(char *buf)
262 char *p;
263 struct rtc_time tm;
265 p = buf;
267 get_rtc_time(&tm);
270 * There is no way to tell if the luser has the RTC set for local
271 * time or for Universal Standard Time (GMT). Probably local though.
274 p += sprintf(p,
275 "rtc_time\t: %02d:%02d:%02d\n"
276 "rtc_date\t: %04d-%02d-%02d\n",
277 tm.tm_hour, tm.tm_min, tm.tm_sec,
278 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
280 return p - buf;
284 /* The various file operations we support. */
286 static struct file_operations rtc_fops = {
287 .owner = THIS_MODULE,
288 .ioctl = rtc_ioctl,
291 /* Probe for the chip by writing something to its RAM and try reading it back. */
293 #define MAGIC_PATTERN 0x42
295 static int __init
296 ds1302_probe(void)
298 int retval, res, baur;
300 baur=(boot_cpu_data.bus_clock/(2*1000*1000));
302 printk("%s: Set PLD_RTCBAUR = %d\n", ds1302_name,baur);
304 outw(0x0000,(unsigned long)PLD_RTCCR);
305 outw(0x0000,(unsigned long)PLD_RTCRSTODT);
306 outw(baur,(unsigned long)PLD_RTCBAUR);
308 /* Try to talk to timekeeper. */
310 ds1302_wenable();
311 /* write RAM byte 0 */
312 /* write something magic */
313 out_byte_rtc(0xc0,MAGIC_PATTERN);
315 /* read RAM byte 0 */
316 if((res = in_byte_rtc(0xc1)) == MAGIC_PATTERN) {
317 char buf[100];
318 ds1302_wdisable();
319 printk("%s: RTC found.\n", ds1302_name);
320 get_rtc_status(buf);
321 printk(buf);
322 retval = 1;
323 } else {
324 printk("%s: RTC not found.\n", ds1302_name);
325 retval = 0;
328 return retval;
332 /* Just probe for the RTC and register the device to handle the ioctl needed. */
334 int __init
335 ds1302_init(void)
337 if (!ds1302_probe()) {
338 return -1;
340 return 0;
343 static int __init ds1302_register(void)
345 ds1302_init();
346 if (register_chrdev(RTC_MAJOR_NR, ds1302_name, &rtc_fops)) {
347 printk(KERN_INFO "%s: unable to get major %d for rtc\n",
348 ds1302_name, RTC_MAJOR_NR);
349 return -1;
351 return 0;
354 module_init(ds1302_register);