1 /*!***************************************************************************
3 *! FILE NAME : ds1302.c
5 *! DESCRIPTION: Implements an interface for the DS1302 RTC through Etrax I/O
7 *! Functions exported: ds1302_readreg, ds1302_writereg, ds1302_init
10 *! Revision 1.14 2004/08/24 06:48:43 starvik
13 *! Revision 1.13 2004/05/28 09:26:59 starvik
14 *! Modified I2C initialization to work in 2.6.
16 *! Revision 1.12 2004/05/14 07:58:03 starvik
17 *! Merge of changes from 2.4
19 *! Revision 1.10 2004/02/04 09:25:12 starvik
20 *! Merge of Linux 2.6.2
22 *! Revision 1.9 2003/07/04 08:27:37 starvik
23 *! Merge of Linux 2.5.74
25 *! Revision 1.8 2003/04/09 05:20:47 starvik
26 *! Merge of Linux 2.5.67
28 *! Revision 1.6 2003/01/09 14:42:51 starvik
29 *! Merge of Linux 2.5.55
31 *! Revision 1.4 2002/12/11 13:13:57 starvik
32 *! Added arch/ to v10 specific includes
33 *! Added fix from Linux 2.4 in serial.c (flush_to_flip_buffer)
35 *! Revision 1.3 2002/11/20 11:56:10 starvik
36 *! Merge of Linux 2.5.48
38 *! Revision 1.2 2002/11/18 13:16:06 starvik
39 *! Linux 2.5 port of latest 2.4 drivers
41 *! Revision 1.15 2002/10/11 16:14:33 johana
42 *! Added CONFIG_ETRAX_DS1302_TRICKLE_CHARGE and initial setting of the
43 *! trcklecharge register.
45 *! Revision 1.14 2002/10/10 12:15:38 magnusmn
46 *! Added support for having the RST signal on bit g0
48 *! Revision 1.13 2002/05/29 15:16:08 johana
49 *! Removed unused variables.
51 *! Revision 1.12 2002/04/10 15:35:25 johana
52 *! Moved probe function closer to init function and marked it __init.
54 *! Revision 1.11 2001/06/14 12:35:52 jonashg
55 *! The ATA hack is back. It is unfortunately the only way to set g27 to output.
57 *! Revision 1.9 2001/06/14 10:00:14 jonashg
58 *! No need for tempudelay to be inline anymore (had to adjust the usec to
59 *! loops conversion because of this to make it slow enough to be a udelay).
61 *! Revision 1.8 2001/06/14 08:06:32 jonashg
62 *! Made tempudelay delay usecs (well, just a tad more).
64 *! Revision 1.7 2001/06/13 14:18:11 jonashg
65 *! Only allow processes with SYS_TIME capability to set time and charge.
67 *! Revision 1.6 2001/06/12 15:22:07 jonashg
68 *! * Made init function __init.
69 *! * Parameter to out_byte() is unsigned char.
70 *! * The magic number 42 has got a name.
71 *! * Removed comment about /proc (nothing is exported there).
73 *! Revision 1.5 2001/06/12 14:35:13 jonashg
74 *! Gave the module a name and added it to printk's.
76 *! Revision 1.4 2001/05/31 14:53:40 jonashg
77 *! Made tempudelay() inline so that the watchdog doesn't reset (see
80 *! Revision 1.3 2001/03/26 16:03:06 bjornw
81 *! Needs linux/config.h
83 *! Revision 1.2 2001/03/20 19:42:00 bjornw
84 *! Use the ETRAX prefix on the DS1302 options
86 *! Revision 1.1 2001/03/20 09:13:50 magnusmn
89 *! Revision 1.10 2000/07/05 15:38:23 bjornw
90 *! Dont update kernel time when a RTC_SET_TIME is done
92 *! Revision 1.9 2000/03/02 15:42:59 macce
93 *! * Hack to make RTC work on all 2100/2400
95 *! Revision 1.8 2000/02/23 16:59:18 torbjore
96 *! added setup of R_GEN_CONFIG when RTC is connected to the generic port.
98 *! Revision 1.7 2000/01/17 15:51:43 johana
99 *! Added RTC_SET_CHARGE ioctl to enable trickle charger.
101 *! Revision 1.6 1999/10/27 13:19:47 bjornw
102 *! Added update_xtime_from_cmos which reads back the updated RTC into the kernel.
103 *! /dev/rtc calls it now.
105 *! Revision 1.5 1999/10/27 12:39:37 bjornw
106 *! Disabled superuser check. Anyone can now set the time.
108 *! Revision 1.4 1999/09/02 13:27:46 pkj
109 *! Added shadow for R_PORT_PB_CONFIG.
110 *! Renamed port_g_shadow to port_g_data_shadow.
112 *! Revision 1.3 1999/09/02 08:28:06 pkj
113 *! Made it possible to select either port PB or the generic port for the RST
114 *! signal line to the DS1302 RTC.
115 *! Also make sure the RST bit is configured as output on Port PB (if used).
117 *! Revision 1.2 1999/09/01 14:47:20 bjornw
118 *! Added support for /dev/rtc operations with ioctl RD_TIME and SET_TIME to read
119 *! and set the date. Register as major 121.
121 *! Revision 1.1 1999/09/01 09:45:29 bjornw
122 *! Implemented a DS1302 RTC driver.
125 *! ---------------------------------------------------------------------------
127 *! (C) Copyright 1999, 2000, 2001 Axis Communications AB, LUND, SWEDEN
129 *! $Id: ds1302.c,v 1.14 2004/08/24 06:48:43 starvik Exp $
131 *!***************************************************************************/
133 #include <linux/config.h>
135 #include <linux/fs.h>
136 #include <linux/init.h>
137 #include <linux/mm.h>
138 #include <linux/module.h>
139 #include <linux/miscdevice.h>
140 #include <linux/delay.h>
141 #include <linux/bcd.h>
143 #include <asm/uaccess.h>
144 #include <asm/system.h>
145 #include <asm/arch/svinto.h>
149 #define RTC_MAJOR_NR 121 /* local major, change later */
151 static const char ds1302_name
[] = "ds1302";
153 /* The DS1302 might be connected to different bits on different products.
154 * It has three signals - SDA, SCL and RST. RST and SCL are always outputs,
155 * but SDA can have a selected direction.
156 * For now, only PORT_PB is hardcoded.
159 /* The RST bit may be on either the Generic Port or Port PB. */
160 #ifdef CONFIG_ETRAX_DS1302_RST_ON_GENERIC_PORT
161 #define TK_RST_OUT(x) REG_SHADOW_SET(R_PORT_G_DATA, port_g_data_shadow, CONFIG_ETRAX_DS1302_RSTBIT, x)
162 #define TK_RST_DIR(x)
164 #define TK_RST_OUT(x) REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, CONFIG_ETRAX_DS1302_RSTBIT, x)
165 #define TK_RST_DIR(x) REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, CONFIG_ETRAX_DS1302_RSTBIT, x)
169 #define TK_SDA_OUT(x) REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, CONFIG_ETRAX_DS1302_SDABIT, x)
170 #define TK_SCL_OUT(x) REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, CONFIG_ETRAX_DS1302_SCLBIT, x)
172 #define TK_SDA_IN() ((*R_PORT_PB_READ >> CONFIG_ETRAX_DS1302_SDABIT) & 1)
173 /* 1 is out, 0 is in */
174 #define TK_SDA_DIR(x) REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, CONFIG_ETRAX_DS1302_SDABIT, x)
175 #define TK_SCL_DIR(x) REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, CONFIG_ETRAX_DS1302_SCLBIT, x)
179 * The reason for tempudelay and not udelay is that loops_per_usec
180 * (used in udelay) is not set when functions here are called from time.c
183 static void tempudelay(int usecs
)
187 for(loops
= usecs
* 12; loops
> 0; loops
--)
194 out_byte(unsigned char x
)
199 /* The chip latches incoming bits on the rising edge of SCL. */
216 /* Read byte. Bits come LSB first, on the falling edge of SCL.
217 * Assume SDA is in input direction already.
225 x
|= (TK_SDA_IN() << 7);
233 /* Prepares for a transaction by de-activating RST (active-low). */
245 /* Ends a transaction by taking RST active again. */
254 /* Enable writing. */
260 out_byte(0x8e); /* Write control register */
261 out_byte(0x00); /* Disable write protect bit 7 = 0 */
265 /* Disable writing. */
268 ds1302_wdisable(void)
271 out_byte(0x8e); /* Write control register */
272 out_byte(0x80); /* Disable write protect bit 7 = 0 */
278 /* Read a byte from the selected register in the DS1302. */
281 ds1302_readreg(int reg
)
286 out_byte(0x81 | (reg
<< 1)); /* read register */
293 /* Write a byte to the selected register. */
296 ds1302_writereg(int reg
, unsigned char val
)
298 #ifndef CONFIG_ETRAX_RTC_READONLY
303 if (reg
== RTC_TRICKLECHARGER
)
310 out_byte(0x80 | (reg
<< 1)); /* write register */
318 get_rtc_time(struct rtc_time
*rtc_tm
)
322 local_irq_save(flags
);
325 rtc_tm
->tm_sec
= CMOS_READ(RTC_SECONDS
);
326 rtc_tm
->tm_min
= CMOS_READ(RTC_MINUTES
);
327 rtc_tm
->tm_hour
= CMOS_READ(RTC_HOURS
);
328 rtc_tm
->tm_mday
= CMOS_READ(RTC_DAY_OF_MONTH
);
329 rtc_tm
->tm_mon
= CMOS_READ(RTC_MONTH
);
330 rtc_tm
->tm_year
= CMOS_READ(RTC_YEAR
);
332 local_irq_restore(flags
);
334 BCD_TO_BIN(rtc_tm
->tm_sec
);
335 BCD_TO_BIN(rtc_tm
->tm_min
);
336 BCD_TO_BIN(rtc_tm
->tm_hour
);
337 BCD_TO_BIN(rtc_tm
->tm_mday
);
338 BCD_TO_BIN(rtc_tm
->tm_mon
);
339 BCD_TO_BIN(rtc_tm
->tm_year
);
342 * Account for differences between how the RTC uses the values
343 * and how they are defined in a struct rtc_time;
346 if (rtc_tm
->tm_year
<= 69)
347 rtc_tm
->tm_year
+= 100;
352 static unsigned char days_in_mo
[] =
353 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
355 /* ioctl that supports RTC_RD_TIME and RTC_SET_TIME (read and set time/date). */
358 rtc_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
,
364 case RTC_RD_TIME
: /* read the time/date from RTC */
366 struct rtc_time rtc_tm
;
368 memset(&rtc_tm
, 0, sizeof (struct rtc_time
));
369 get_rtc_time(&rtc_tm
);
370 if (copy_to_user((struct rtc_time
*)arg
, &rtc_tm
, sizeof(struct rtc_time
)))
375 case RTC_SET_TIME
: /* set the RTC */
377 struct rtc_time rtc_tm
;
378 unsigned char mon
, day
, hrs
, min
, sec
, leap_yr
;
381 if (!capable(CAP_SYS_TIME
))
384 if (copy_from_user(&rtc_tm
, (struct rtc_time
*)arg
, sizeof(struct rtc_time
)))
387 yrs
= rtc_tm
.tm_year
+ 1900;
388 mon
= rtc_tm
.tm_mon
+ 1; /* tm_mon starts at zero */
389 day
= rtc_tm
.tm_mday
;
390 hrs
= rtc_tm
.tm_hour
;
395 if ((yrs
< 1970) || (yrs
> 2069))
398 leap_yr
= ((!(yrs
% 4) && (yrs
% 100)) || !(yrs
% 400));
400 if ((mon
> 12) || (day
== 0))
403 if (day
> (days_in_mo
[mon
] + ((mon
== 2) && leap_yr
)))
406 if ((hrs
>= 24) || (min
>= 60) || (sec
>= 60))
410 yrs
-= 2000; /* RTC (0, 1, ... 69) */
412 yrs
-= 1900; /* RTC (70, 71, ... 99) */
421 local_irq_save(flags
);
423 CMOS_WRITE(yrs
, RTC_YEAR
);
424 CMOS_WRITE(mon
, RTC_MONTH
);
425 CMOS_WRITE(day
, RTC_DAY_OF_MONTH
);
426 CMOS_WRITE(hrs
, RTC_HOURS
);
427 CMOS_WRITE(min
, RTC_MINUTES
);
428 CMOS_WRITE(sec
, RTC_SECONDS
);
429 local_irq_restore(flags
);
431 /* Notice that at this point, the RTC is updated but
432 * the kernel is still running with the old time.
433 * You need to set that separately with settimeofday
439 case RTC_SET_CHARGE
: /* set the RTC TRICKLE CHARGE register */
443 if (!capable(CAP_SYS_TIME
))
446 if(copy_from_user(&tcs_val
, (int*)arg
, sizeof(int)))
449 tcs_val
= RTC_TCR_PATTERN
| (tcs_val
& 0x0F);
450 ds1302_writereg(RTC_TRICKLECHARGER
, tcs_val
);
456 * Implement voltage low detection support
458 printk(KERN_WARNING
"DS1302: RTC Voltage Low detection"
459 " is not supported\n");
465 * Nothing to do since Voltage Low detection is not supported
475 print_rtc_status(void)
482 * There is no way to tell if the luser has the RTC set for local
483 * time or for Universal Standard Time (GMT). Probably local though.
486 printk(KERN_INFO
"rtc_time\t: %02d:%02d:%02d\n",
487 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
);
488 printk(KERN_INFO
"rtc_date\t: %04d-%02d-%02d\n",
489 tm
.tm_year
+ 1900, tm
.tm_mon
+ 1, tm
.tm_mday
);
492 /* The various file operations we support. */
494 static struct file_operations rtc_fops
= {
495 .owner
= THIS_MODULE
,
499 /* Probe for the chip by writing something to its RAM and try reading it back. */
501 #define MAGIC_PATTERN 0x42
512 /* Try to talk to timekeeper. */
516 out_byte(0xc0); /* write RAM byte 0 */
517 out_byte(MAGIC_PATTERN
); /* write something magic */
519 out_byte(0xc1); /* read RAM byte 0 */
521 if((res
= in_byte()) == MAGIC_PATTERN
) {
524 printk(KERN_INFO
"%s: RTC found.\n", ds1302_name
);
525 printk(KERN_INFO
"%s: SDA, SCL, RST on PB%i, PB%i, %s%i\n",
527 CONFIG_ETRAX_DS1302_SDABIT
,
528 CONFIG_ETRAX_DS1302_SCLBIT
,
529 #ifdef CONFIG_ETRAX_DS1302_RST_ON_GENERIC_PORT
534 CONFIG_ETRAX_DS1302_RSTBIT
);
546 /* Just probe for the RTC and register the device to handle the ioctl needed. */
553 if (!ds1302_probe()) {
554 #ifdef CONFIG_ETRAX_DS1302_RST_ON_GENERIC_PORT
555 #if CONFIG_ETRAX_DS1302_RSTBIT == 27
557 * The only way to set g27 to output is to enable ATA.
559 * Make sure that R_GEN_CONFIG is setup correct.
561 genconfig_shadow
= ((genconfig_shadow
&
562 ~IO_MASK(R_GEN_CONFIG
, ata
)) |
563 (IO_STATE(R_GEN_CONFIG
, ata
, select
)));
564 *R_GEN_CONFIG
= genconfig_shadow
;
565 #elif CONFIG_ETRAX_DS1302_RSTBIT == 0
567 /* Set the direction of this bit to out. */
568 genconfig_shadow
= ((genconfig_shadow
&
569 ~IO_MASK(R_GEN_CONFIG
, g0dir
)) |
570 (IO_STATE(R_GEN_CONFIG
, g0dir
, out
)));
571 *R_GEN_CONFIG
= genconfig_shadow
;
573 if (!ds1302_probe()) {
574 printk(KERN_WARNING
"%s: RTC not found.\n", ds1302_name
);
578 printk(KERN_WARNING
"%s: RTC not found.\n", ds1302_name
);
582 /* Initialise trickle charger */
583 ds1302_writereg(RTC_TRICKLECHARGER
,
584 RTC_TCR_PATTERN
|(CONFIG_ETRAX_DS1302_TRICKLE_CHARGE
& 0x0F));
585 /* Start clock by resetting CLOCK_HALT */
586 ds1302_writereg(RTC_SECONDS
, (ds1302_readreg(RTC_SECONDS
) & 0x7F));
590 static int __init
ds1302_register(void)
593 if (register_chrdev(RTC_MAJOR_NR
, ds1302_name
, &rtc_fops
)) {
594 printk(KERN_INFO
"%s: unable to get major %d for rtc\n",
595 ds1302_name
, RTC_MAJOR_NR
);
602 module_init(ds1302_register
);