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
9 *! ---------------------------------------------------------------------------
11 *! (C) Copyright 1999-2007 Axis Communications AB, LUND, SWEDEN
13 *!***************************************************************************/
17 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/miscdevice.h>
21 #include <linux/delay.h>
22 #include <linux/smp_lock.h>
23 #include <linux/bcd.h>
24 #include <linux/capability.h>
26 #include <asm/uaccess.h>
27 #include <asm/system.h>
28 #include <arch/svinto.h>
31 #include <arch/io_interface_mux.h>
35 #define RTC_MAJOR_NR 121 /* local major, change later */
37 static const char ds1302_name
[] = "ds1302";
39 /* The DS1302 might be connected to different bits on different products.
40 * It has three signals - SDA, SCL and RST. RST and SCL are always outputs,
41 * but SDA can have a selected direction.
42 * For now, only PORT_PB is hardcoded.
45 /* The RST bit may be on either the Generic Port or Port PB. */
46 #ifdef CONFIG_ETRAX_DS1302_RST_ON_GENERIC_PORT
47 #define TK_RST_OUT(x) REG_SHADOW_SET(R_PORT_G_DATA, port_g_data_shadow, CONFIG_ETRAX_DS1302_RSTBIT, x)
50 #define TK_RST_OUT(x) REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, CONFIG_ETRAX_DS1302_RSTBIT, x)
51 #define TK_RST_DIR(x) REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, CONFIG_ETRAX_DS1302_RSTBIT, x)
55 #define TK_SDA_OUT(x) REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, CONFIG_ETRAX_DS1302_SDABIT, x)
56 #define TK_SCL_OUT(x) REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, CONFIG_ETRAX_DS1302_SCLBIT, x)
58 #define TK_SDA_IN() ((*R_PORT_PB_READ >> CONFIG_ETRAX_DS1302_SDABIT) & 1)
59 /* 1 is out, 0 is in */
60 #define TK_SDA_DIR(x) REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, CONFIG_ETRAX_DS1302_SDABIT, x)
61 #define TK_SCL_DIR(x) REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, CONFIG_ETRAX_DS1302_SCLBIT, x)
65 * The reason for tempudelay and not udelay is that loops_per_usec
66 * (used in udelay) is not set when functions here are called from time.c
69 static void tempudelay(int usecs
)
73 for(loops
= usecs
* 12; loops
> 0; loops
--)
80 out_byte(unsigned char x
)
85 /* The chip latches incoming bits on the rising edge of SCL. */
102 /* Read byte. Bits come LSB first, on the falling edge of SCL.
103 * Assume SDA is in input direction already.
111 x
|= (TK_SDA_IN() << 7);
119 /* Prepares for a transaction by de-activating RST (active-low). */
131 /* Ends a transaction by taking RST active again. */
140 /* Enable writing. */
146 out_byte(0x8e); /* Write control register */
147 out_byte(0x00); /* Disable write protect bit 7 = 0 */
151 /* Disable writing. */
154 ds1302_wdisable(void)
157 out_byte(0x8e); /* Write control register */
158 out_byte(0x80); /* Disable write protect bit 7 = 0 */
164 /* Read a byte from the selected register in the DS1302. */
167 ds1302_readreg(int reg
)
172 out_byte(0x81 | (reg
<< 1)); /* read register */
179 /* Write a byte to the selected register. */
182 ds1302_writereg(int reg
, unsigned char val
)
184 #ifndef CONFIG_ETRAX_RTC_READONLY
189 if (reg
== RTC_TRICKLECHARGER
)
196 out_byte(0x80 | (reg
<< 1)); /* write register */
204 get_rtc_time(struct rtc_time
*rtc_tm
)
208 local_irq_save(flags
);
210 rtc_tm
->tm_sec
= CMOS_READ(RTC_SECONDS
);
211 rtc_tm
->tm_min
= CMOS_READ(RTC_MINUTES
);
212 rtc_tm
->tm_hour
= CMOS_READ(RTC_HOURS
);
213 rtc_tm
->tm_mday
= CMOS_READ(RTC_DAY_OF_MONTH
);
214 rtc_tm
->tm_mon
= CMOS_READ(RTC_MONTH
);
215 rtc_tm
->tm_year
= CMOS_READ(RTC_YEAR
);
217 local_irq_restore(flags
);
219 rtc_tm
->tm_sec
= bcd2bin(rtc_tm
->tm_sec
);
220 rtc_tm
->tm_min
= bcd2bin(rtc_tm
->tm_min
);
221 rtc_tm
->tm_hour
= bcd2bin(rtc_tm
->tm_hour
);
222 rtc_tm
->tm_mday
= bcd2bin(rtc_tm
->tm_mday
);
223 rtc_tm
->tm_mon
= bcd2bin(rtc_tm
->tm_mon
);
224 rtc_tm
->tm_year
= bcd2bin(rtc_tm
->tm_year
);
227 * Account for differences between how the RTC uses the values
228 * and how they are defined in a struct rtc_time;
231 if (rtc_tm
->tm_year
<= 69)
232 rtc_tm
->tm_year
+= 100;
237 static unsigned char days_in_mo
[] =
238 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
240 /* ioctl that supports RTC_RD_TIME and RTC_SET_TIME (read and set time/date). */
242 static int rtc_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
247 case RTC_RD_TIME
: /* read the time/date from RTC */
249 struct rtc_time rtc_tm
;
251 memset(&rtc_tm
, 0, sizeof (struct rtc_time
));
252 get_rtc_time(&rtc_tm
);
253 if (copy_to_user((struct rtc_time
*)arg
, &rtc_tm
, sizeof(struct rtc_time
)))
258 case RTC_SET_TIME
: /* set the RTC */
260 struct rtc_time rtc_tm
;
261 unsigned char mon
, day
, hrs
, min
, sec
, leap_yr
;
264 if (!capable(CAP_SYS_TIME
))
267 if (copy_from_user(&rtc_tm
, (struct rtc_time
*)arg
, sizeof(struct rtc_time
)))
270 yrs
= rtc_tm
.tm_year
+ 1900;
271 mon
= rtc_tm
.tm_mon
+ 1; /* tm_mon starts at zero */
272 day
= rtc_tm
.tm_mday
;
273 hrs
= rtc_tm
.tm_hour
;
278 if ((yrs
< 1970) || (yrs
> 2069))
281 leap_yr
= ((!(yrs
% 4) && (yrs
% 100)) || !(yrs
% 400));
283 if ((mon
> 12) || (day
== 0))
286 if (day
> (days_in_mo
[mon
] + ((mon
== 2) && leap_yr
)))
289 if ((hrs
>= 24) || (min
>= 60) || (sec
>= 60))
293 yrs
-= 2000; /* RTC (0, 1, ... 69) */
295 yrs
-= 1900; /* RTC (70, 71, ... 99) */
304 local_irq_save(flags
);
305 CMOS_WRITE(yrs
, RTC_YEAR
);
306 CMOS_WRITE(mon
, RTC_MONTH
);
307 CMOS_WRITE(day
, RTC_DAY_OF_MONTH
);
308 CMOS_WRITE(hrs
, RTC_HOURS
);
309 CMOS_WRITE(min
, RTC_MINUTES
);
310 CMOS_WRITE(sec
, RTC_SECONDS
);
311 local_irq_restore(flags
);
313 /* Notice that at this point, the RTC is updated but
314 * the kernel is still running with the old time.
315 * You need to set that separately with settimeofday
321 case RTC_SET_CHARGE
: /* set the RTC TRICKLE CHARGE register */
325 if (!capable(CAP_SYS_TIME
))
328 if(copy_from_user(&tcs_val
, (int*)arg
, sizeof(int)))
331 tcs_val
= RTC_TCR_PATTERN
| (tcs_val
& 0x0F);
332 ds1302_writereg(RTC_TRICKLECHARGER
, tcs_val
);
338 * Implement voltage low detection support
340 printk(KERN_WARNING
"DS1302: RTC Voltage Low detection"
341 " is not supported\n");
347 * Nothing to do since Voltage Low detection is not supported
356 static long rtc_unlocked_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
361 ret
= rtc_ioctl(file
, cmd
, arg
);
368 print_rtc_status(void)
375 * There is no way to tell if the luser has the RTC set for local
376 * time or for Universal Standard Time (GMT). Probably local though.
379 printk(KERN_INFO
"rtc_time\t: %02d:%02d:%02d\n",
380 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
);
381 printk(KERN_INFO
"rtc_date\t: %04d-%02d-%02d\n",
382 tm
.tm_year
+ 1900, tm
.tm_mon
+ 1, tm
.tm_mday
);
385 /* The various file operations we support. */
387 static const struct file_operations rtc_fops
= {
388 .owner
= THIS_MODULE
,
389 .unlocked_ioctl
= rtc_unlocked_ioctl
,
392 /* Probe for the chip by writing something to its RAM and try reading it back. */
394 #define MAGIC_PATTERN 0x42
405 /* Try to talk to timekeeper. */
409 out_byte(0xc0); /* write RAM byte 0 */
410 out_byte(MAGIC_PATTERN
); /* write something magic */
412 out_byte(0xc1); /* read RAM byte 0 */
414 if((res
= in_byte()) == MAGIC_PATTERN
) {
417 printk(KERN_INFO
"%s: RTC found.\n", ds1302_name
);
418 printk(KERN_INFO
"%s: SDA, SCL, RST on PB%i, PB%i, %s%i\n",
420 CONFIG_ETRAX_DS1302_SDABIT
,
421 CONFIG_ETRAX_DS1302_SCLBIT
,
422 #ifdef CONFIG_ETRAX_DS1302_RST_ON_GENERIC_PORT
427 CONFIG_ETRAX_DS1302_RSTBIT
);
439 /* Just probe for the RTC and register the device to handle the ioctl needed. */
444 #ifdef CONFIG_ETRAX_I2C
448 if (!ds1302_probe()) {
449 #ifdef CONFIG_ETRAX_DS1302_RST_ON_GENERIC_PORT
450 #if CONFIG_ETRAX_DS1302_RSTBIT == 27
452 * The only way to set g27 to output is to enable ATA.
454 * Make sure that R_GEN_CONFIG is setup correct.
456 /* Allocating the ATA interface will grab almost all
457 * pins in I/O groups a, b, c and d. A consequence of
458 * allocating the ATA interface is that the fixed
459 * interfaces shared RAM, parallel port 0, parallel
460 * port 1, parallel port W, SCSI-8 port 0, SCSI-8 port
461 * 1, SCSI-W, serial port 2, serial port 3,
462 * synchronous serial port 3 and USB port 2 and almost
463 * all GPIO pins on port g cannot be used.
465 if (cris_request_io_interface(if_ata
, "ds1302/ATA")) {
466 printk(KERN_WARNING
"ds1302: Failed to get IO interface\n");
470 #elif CONFIG_ETRAX_DS1302_RSTBIT == 0
471 if (cris_io_interface_allocate_pins(if_gpio_grp_a
,
473 CONFIG_ETRAX_DS1302_RSTBIT
,
474 CONFIG_ETRAX_DS1302_RSTBIT
)) {
475 printk(KERN_WARNING
"ds1302: Failed to get IO interface\n");
479 /* Set the direction of this bit to out. */
480 genconfig_shadow
= ((genconfig_shadow
&
481 ~IO_MASK(R_GEN_CONFIG
, g0dir
)) |
482 (IO_STATE(R_GEN_CONFIG
, g0dir
, out
)));
483 *R_GEN_CONFIG
= genconfig_shadow
;
485 if (!ds1302_probe()) {
486 printk(KERN_WARNING
"%s: RTC not found.\n", ds1302_name
);
490 printk(KERN_WARNING
"%s: RTC not found.\n", ds1302_name
);
494 /* Initialise trickle charger */
495 ds1302_writereg(RTC_TRICKLECHARGER
,
496 RTC_TCR_PATTERN
|(CONFIG_ETRAX_DS1302_TRICKLE_CHARGE
& 0x0F));
497 /* Start clock by resetting CLOCK_HALT */
498 ds1302_writereg(RTC_SECONDS
, (ds1302_readreg(RTC_SECONDS
) & 0x7F));
502 static int __init
ds1302_register(void)
505 if (register_chrdev(RTC_MAJOR_NR
, ds1302_name
, &rtc_fops
)) {
506 printk(KERN_INFO
"%s: unable to get major %d for rtc\n",
507 ds1302_name
, RTC_MAJOR_NR
);
514 module_init(ds1302_register
);