patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / arch / cris / arch-v10 / drivers / ds1302.c
1 /*!***************************************************************************
2 *!
3 *! FILE NAME  : ds1302.c
4 *!
5 *! DESCRIPTION: Implements an interface for the DS1302 RTC through Etrax I/O
6 *!
7 *! Functions exported: ds1302_readreg, ds1302_writereg, ds1302_init
8 *!
9 *! $Log: ds1302.c,v $
10 *! Revision 1.13  2004/05/28 09:26:59  starvik
11 *! Modified I2C initialization to work in 2.6.
12 *!
13 *! Revision 1.12  2004/05/14 07:58:03  starvik
14 *! Merge of changes from 2.4
15 *!
16 *! Revision 1.10  2004/02/04 09:25:12  starvik
17 *! Merge of Linux 2.6.2
18 *!
19 *! Revision 1.9  2003/07/04 08:27:37  starvik
20 *! Merge of Linux 2.5.74
21 *!
22 *! Revision 1.8  2003/04/09 05:20:47  starvik
23 *! Merge of Linux 2.5.67
24 *!
25 *! Revision 1.6  2003/01/09 14:42:51  starvik
26 *! Merge of Linux 2.5.55
27 *!
28 *! Revision 1.4  2002/12/11 13:13:57  starvik
29 *! Added arch/ to v10 specific includes
30 *! Added fix from Linux 2.4 in serial.c (flush_to_flip_buffer)
31 *!
32 *! Revision 1.3  2002/11/20 11:56:10  starvik
33 *! Merge of Linux 2.5.48
34 *!
35 *! Revision 1.2  2002/11/18 13:16:06  starvik
36 *! Linux 2.5 port of latest 2.4 drivers
37 *!
38 *! Revision 1.15  2002/10/11 16:14:33  johana
39 *! Added CONFIG_ETRAX_DS1302_TRICKLE_CHARGE and initial setting of the
40 *! trcklecharge register.
41 *!
42 *! Revision 1.14  2002/10/10 12:15:38  magnusmn
43 *! Added support for having the RST signal on bit g0
44 *!
45 *! Revision 1.13  2002/05/29 15:16:08  johana
46 *! Removed unused variables.
47 *!
48 *! Revision 1.12  2002/04/10 15:35:25  johana
49 *! Moved probe function closer to init function and marked it __init.
50 *!
51 *! Revision 1.11  2001/06/14 12:35:52  jonashg
52 *! The ATA hack is back. It is unfortunately the only way to set g27 to output.
53 *!
54 *! Revision 1.9  2001/06/14 10:00:14  jonashg
55 *! No need for tempudelay to be inline anymore (had to adjust the usec to
56 *! loops conversion because of this to make it slow enough to be a udelay).
57 *!
58 *! Revision 1.8  2001/06/14 08:06:32  jonashg
59 *! Made tempudelay delay usecs (well, just a tad more).
60 *!
61 *! Revision 1.7  2001/06/13 14:18:11  jonashg
62 *! Only allow processes with SYS_TIME capability to set time and charge.
63 *!
64 *! Revision 1.6  2001/06/12 15:22:07  jonashg
65 *! * Made init function __init.
66 *! * Parameter to out_byte() is unsigned char.
67 *! * The magic number 42 has got a name.
68 *! * Removed comment about /proc (nothing is exported there).
69 *!
70 *! Revision 1.5  2001/06/12 14:35:13  jonashg
71 *! Gave the module a name and added it to printk's.
72 *!
73 *! Revision 1.4  2001/05/31 14:53:40  jonashg
74 *! Made tempudelay() inline so that the watchdog doesn't reset (see
75 *! function comment).
76 *!
77 *! Revision 1.3  2001/03/26 16:03:06  bjornw
78 *! Needs linux/config.h
79 *!
80 *! Revision 1.2  2001/03/20 19:42:00  bjornw
81 *! Use the ETRAX prefix on the DS1302 options
82 *!
83 *! Revision 1.1  2001/03/20 09:13:50  magnusmn
84 *! Linux 2.4 port
85 *!
86 *! Revision 1.10  2000/07/05 15:38:23  bjornw
87 *! Dont update kernel time when a RTC_SET_TIME is done
88 *!
89 *! Revision 1.9  2000/03/02 15:42:59  macce
90 *! * Hack to make RTC work on all 2100/2400
91 *!
92 *! Revision 1.8  2000/02/23 16:59:18  torbjore
93 *! added setup of R_GEN_CONFIG when RTC is connected to the generic port.
94 *!
95 *! Revision 1.7  2000/01/17 15:51:43  johana
96 *! Added RTC_SET_CHARGE ioctl to enable trickle charger.
97 *!
98 *! Revision 1.6  1999/10/27 13:19:47  bjornw
99 *! Added update_xtime_from_cmos which reads back the updated RTC into the kernel.
100 *! /dev/rtc calls it now.
101 *!
102 *! Revision 1.5  1999/10/27 12:39:37  bjornw
103 *! Disabled superuser check. Anyone can now set the time.
104 *!
105 *! Revision 1.4  1999/09/02 13:27:46  pkj
106 *! Added shadow for R_PORT_PB_CONFIG.
107 *! Renamed port_g_shadow to port_g_data_shadow.
108 *!
109 *! Revision 1.3  1999/09/02 08:28:06  pkj
110 *! Made it possible to select either port PB or the generic port for the RST
111 *! signal line to the DS1302 RTC.
112 *! Also make sure the RST bit is configured as output on Port PB (if used).
113 *!
114 *! Revision 1.2  1999/09/01 14:47:20  bjornw
115 *! Added support for /dev/rtc operations with ioctl RD_TIME and SET_TIME to read
116 *! and set the date. Register as major 121.
117 *!
118 *! Revision 1.1  1999/09/01 09:45:29  bjornw
119 *! Implemented a DS1302 RTC driver.
120 *!
121 *!
122 *! ---------------------------------------------------------------------------
123 *!
124 *! (C) Copyright 1999, 2000, 2001  Axis Communications AB, LUND, SWEDEN
125 *!
126 *! $Id: ds1302.c,v 1.13 2004/05/28 09:26:59 starvik Exp $
127 *!
128 *!***************************************************************************/
129
130 #include <linux/config.h>
131
132 #include <linux/fs.h>
133 #include <linux/init.h>
134 #include <linux/mm.h>
135 #include <linux/module.h>
136 #include <linux/miscdevice.h>
137 #include <linux/delay.h>
138 #include <linux/bcd.h>
139
140 #include <asm/uaccess.h>
141 #include <asm/system.h>
142 #include <asm/arch/svinto.h>
143 #include <asm/io.h>
144 #include <asm/rtc.h>
145
146 #define RTC_MAJOR_NR 121 /* local major, change later */
147
148 static const char ds1302_name[] = "ds1302";
149
150 /* The DS1302 might be connected to different bits on different products. 
151  * It has three signals - SDA, SCL and RST. RST and SCL are always outputs,
152  * but SDA can have a selected direction.
153  * For now, only PORT_PB is hardcoded.
154  */
155
156 /* The RST bit may be on either the Generic Port or Port PB. */
157 #ifdef CONFIG_ETRAX_DS1302_RST_ON_GENERIC_PORT
158 #define TK_RST_OUT(x) REG_SHADOW_SET(R_PORT_G_DATA,  port_g_data_shadow,  CONFIG_ETRAX_DS1302_RSTBIT, x)
159 #define TK_RST_DIR(x)
160 #else
161 #define TK_RST_OUT(x) REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, CONFIG_ETRAX_DS1302_RSTBIT, x)
162 #define TK_RST_DIR(x) REG_SHADOW_SET(R_PORT_PB_DIR,  port_pb_dir_shadow,  CONFIG_ETRAX_DS1302_RSTBIT, x)
163 #endif
164
165
166 #define TK_SDA_OUT(x) REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, CONFIG_ETRAX_DS1302_SDABIT, x)
167 #define TK_SCL_OUT(x) REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, CONFIG_ETRAX_DS1302_SCLBIT, x)
168
169 #define TK_SDA_IN()   ((*R_PORT_PB_READ >> CONFIG_ETRAX_DS1302_SDABIT) & 1)
170 /* 1 is out, 0 is in */
171 #define TK_SDA_DIR(x) REG_SHADOW_SET(R_PORT_PB_DIR,  port_pb_dir_shadow,  CONFIG_ETRAX_DS1302_SDABIT, x)
172 #define TK_SCL_DIR(x) REG_SHADOW_SET(R_PORT_PB_DIR,  port_pb_dir_shadow,  CONFIG_ETRAX_DS1302_SCLBIT, x)
173
174
175 /*
176  * The reason for tempudelay and not udelay is that loops_per_usec
177  * (used in udelay) is not set when functions here are called from time.c 
178  */
179
180 static void tempudelay(int usecs) 
181 {
182         volatile int loops;
183
184         for(loops = usecs * 12; loops > 0; loops--)
185                 /* nothing */;  
186 }
187
188
189 /* Send 8 bits. */
190 static void
191 out_byte(unsigned char x) 
192 {
193         int i;
194         TK_SDA_DIR(1);
195         for (i = 8; i--;) {
196                 /* The chip latches incoming bits on the rising edge of SCL. */
197                 TK_SCL_OUT(0);
198                 TK_SDA_OUT(x & 1);
199                 tempudelay(1);
200                 TK_SCL_OUT(1);
201                 tempudelay(1);
202                 x >>= 1;
203         }
204         TK_SDA_DIR(0);
205 }
206
207 static unsigned char
208 in_byte(void) 
209 {
210         unsigned char x = 0;
211         int i;
212
213         /* Read byte. Bits come LSB first, on the falling edge of SCL.
214          * Assume SDA is in input direction already.
215          */
216         TK_SDA_DIR(0);
217
218         for (i = 8; i--;) {
219                 TK_SCL_OUT(0);
220                 tempudelay(1);
221                 x >>= 1;
222                 x |= (TK_SDA_IN() << 7);
223                 TK_SCL_OUT(1);
224                 tempudelay(1);
225         }
226
227         return x;
228 }
229
230 /* Prepares for a transaction by de-activating RST (active-low). */
231
232 static void
233 start(void) 
234 {
235         TK_SCL_OUT(0);
236         tempudelay(1);
237         TK_RST_OUT(0);
238         tempudelay(5);
239         TK_RST_OUT(1);  
240 }
241
242 /* Ends a transaction by taking RST active again. */
243
244 static void
245 stop(void) 
246 {
247         tempudelay(2);
248         TK_RST_OUT(0);
249 }
250
251 /* Enable writing. */
252
253 static void
254 ds1302_wenable(void) 
255 {
256         start();        
257         out_byte(0x8e); /* Write control register  */
258         out_byte(0x00); /* Disable write protect bit 7 = 0 */
259         stop();
260 }
261
262 /* Disable writing. */
263
264 static void
265 ds1302_wdisable(void) 
266 {
267         start();
268         out_byte(0x8e); /* Write control register  */
269         out_byte(0x80); /* Disable write protect bit 7 = 0 */
270         stop();
271 }
272
273
274
275 /* Read a byte from the selected register in the DS1302. */
276
277 unsigned char
278 ds1302_readreg(int reg) 
279 {
280         unsigned char x;
281
282         start();
283         out_byte(0x81 | (reg << 1)); /* read register */
284         x = in_byte();
285         stop();
286
287         return x;
288 }
289
290 /* Write a byte to the selected register. */
291
292 void
293 ds1302_writereg(int reg, unsigned char val) 
294 {
295 #ifndef CONFIG_ETRAX_RTC_READONLY
296         int do_writereg = 1;
297 #else
298         int do_writereg = 0;
299
300         if (reg == RTC_TRICKLECHARGER)
301                 do_writereg = 1;
302 #endif
303
304         if (do_writereg) {
305                 ds1302_wenable();
306                 start();
307                 out_byte(0x80 | (reg << 1)); /* write register */
308                 out_byte(val);
309                 stop();
310                 ds1302_wdisable();
311         }
312 }
313
314 void
315 get_rtc_time(struct rtc_time *rtc_tm) 
316 {
317         unsigned long flags;
318
319         local_irq_save(flags);
320         local_irq_disable();
321
322         rtc_tm->tm_sec = CMOS_READ(RTC_SECONDS);
323         rtc_tm->tm_min = CMOS_READ(RTC_MINUTES);
324         rtc_tm->tm_hour = CMOS_READ(RTC_HOURS);
325         rtc_tm->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
326         rtc_tm->tm_mon = CMOS_READ(RTC_MONTH);
327         rtc_tm->tm_year = CMOS_READ(RTC_YEAR);
328
329         local_irq_restore(flags);
330         
331         BCD_TO_BIN(rtc_tm->tm_sec);
332         BCD_TO_BIN(rtc_tm->tm_min);
333         BCD_TO_BIN(rtc_tm->tm_hour);
334         BCD_TO_BIN(rtc_tm->tm_mday);
335         BCD_TO_BIN(rtc_tm->tm_mon);
336         BCD_TO_BIN(rtc_tm->tm_year);
337
338         /*
339          * Account for differences between how the RTC uses the values
340          * and how they are defined in a struct rtc_time;
341          */
342
343         if (rtc_tm->tm_year <= 69)
344                 rtc_tm->tm_year += 100;
345
346         rtc_tm->tm_mon--;
347 }
348
349 static unsigned char days_in_mo[] = 
350     {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
351
352 /* ioctl that supports RTC_RD_TIME and RTC_SET_TIME (read and set time/date). */
353
354 static int
355 rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
356           unsigned long arg) 
357 {
358         unsigned long flags;
359
360         switch(cmd) {
361                 case RTC_RD_TIME:       /* read the time/date from RTC  */
362                 {
363                         struct rtc_time rtc_tm;
364                                                 
365                         memset(&rtc_tm, 0, sizeof (struct rtc_time));
366                         get_rtc_time(&rtc_tm);                                          
367                         if (copy_to_user((struct rtc_time*)arg, &rtc_tm, sizeof(struct rtc_time)))
368                                 return -EFAULT; 
369                         return 0;
370                 }
371
372                 case RTC_SET_TIME:      /* set the RTC */
373                 {
374                         struct rtc_time rtc_tm;
375                         unsigned char mon, day, hrs, min, sec, leap_yr;
376                         unsigned int yrs;
377
378                         if (!capable(CAP_SYS_TIME))
379                                 return -EPERM;
380
381                         if (copy_from_user(&rtc_tm, (struct rtc_time*)arg, sizeof(struct rtc_time)))
382                                 return -EFAULT;         
383
384                         yrs = rtc_tm.tm_year + 1900;
385                         mon = rtc_tm.tm_mon + 1;   /* tm_mon starts at zero */
386                         day = rtc_tm.tm_mday;
387                         hrs = rtc_tm.tm_hour;
388                         min = rtc_tm.tm_min;
389                         sec = rtc_tm.tm_sec;
390                         
391                         
392                         if ((yrs < 1970) || (yrs > 2069))
393                                 return -EINVAL;
394
395                         leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
396
397                         if ((mon > 12) || (day == 0))
398                                 return -EINVAL;
399
400                         if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
401                                 return -EINVAL;
402                         
403                         if ((hrs >= 24) || (min >= 60) || (sec >= 60))
404                                 return -EINVAL;
405
406                         if (yrs >= 2000)
407                                 yrs -= 2000;    /* RTC (0, 1, ... 69) */
408                         else
409                                 yrs -= 1900;    /* RTC (70, 71, ... 99) */
410
411                         BIN_TO_BCD(sec);
412                         BIN_TO_BCD(min);
413                         BIN_TO_BCD(hrs);
414                         BIN_TO_BCD(day);
415                         BIN_TO_BCD(mon);
416                         BIN_TO_BCD(yrs);
417
418                         local_irq_save(flags);
419                         local_irq_disable();
420                         CMOS_WRITE(yrs, RTC_YEAR);
421                         CMOS_WRITE(mon, RTC_MONTH);
422                         CMOS_WRITE(day, RTC_DAY_OF_MONTH);
423                         CMOS_WRITE(hrs, RTC_HOURS);
424                         CMOS_WRITE(min, RTC_MINUTES);
425                         CMOS_WRITE(sec, RTC_SECONDS);
426                         local_irq_restore(flags);
427
428                         /* Notice that at this point, the RTC is updated but
429                          * the kernel is still running with the old time.
430                          * You need to set that separately with settimeofday
431                          * or adjtimex.
432                          */
433                         return 0;
434                 }
435
436                 case RTC_SET_CHARGE: /* set the RTC TRICKLE CHARGE register */
437                 {
438                         int tcs_val;                        
439
440                         if (!capable(CAP_SYS_TIME))
441                                 return -EPERM;
442                         
443                         if(copy_from_user(&tcs_val, (int*)arg, sizeof(int)))
444                                 return -EFAULT;
445
446                         tcs_val = RTC_TCR_PATTERN | (tcs_val & 0x0F);
447                         ds1302_writereg(RTC_TRICKLECHARGER, tcs_val);
448                         return 0;
449                 }
450                 case RTC_VLOW_RD:
451                 {
452                         /* TODO:
453                          * Implement voltage low detection support
454                          */
455                         printk(KERN_WARNING "DS1302: RTC Voltage Low detection"
456                                " is not supported\n");
457                         return 0;
458                 }
459                 case RTC_VLOW_SET:
460                 {
461                         /* TODO:
462                          * Nothing to do since Voltage Low detection is not supported
463                          */
464                         return 0;
465                 }
466                 default:
467                         return -ENOIOCTLCMD;
468         }
469 }
470
471 static void
472 print_rtc_status(void)
473 {
474         struct rtc_time tm;
475
476         get_rtc_time(&tm);
477
478         /*
479          * There is no way to tell if the luser has the RTC set for local
480          * time or for Universal Standard Time (GMT). Probably local though.
481          */
482
483         printk(KERN_INFO "rtc_time\t: %02d:%02d:%02d\n",
484                tm.tm_hour, tm.tm_min, tm.tm_sec);
485         printk(KERN_INFO "rtc_date\t: %04d-%02d-%02d\n",
486                tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
487 }
488
489 /* The various file operations we support. */
490
491 static struct file_operations rtc_fops = {
492         .owner          = THIS_MODULE,
493         .ioctl          = rtc_ioctl,    
494 }; 
495
496 /* Probe for the chip by writing something to its RAM and try reading it back. */
497
498 #define MAGIC_PATTERN 0x42
499
500 static int __init
501 ds1302_probe(void) 
502 {
503         int retval, res; 
504
505         TK_RST_DIR(1);
506         TK_SCL_DIR(1);
507         TK_SDA_DIR(0);
508         
509         /* Try to talk to timekeeper. */
510
511         ds1302_wenable();  
512         start();
513         out_byte(0xc0); /* write RAM byte 0 */  
514         out_byte(MAGIC_PATTERN); /* write something magic */
515         start();
516         out_byte(0xc1); /* read RAM byte 0 */
517
518         if((res = in_byte()) == MAGIC_PATTERN) {
519                 stop();
520                 ds1302_wdisable();
521                 printk(KERN_INFO "%s: RTC found.\n", ds1302_name);
522                 printk(KERN_INFO "%s: SDA, SCL, RST on PB%i, PB%i, %s%i\n",
523                        ds1302_name,
524                        CONFIG_ETRAX_DS1302_SDABIT,
525                        CONFIG_ETRAX_DS1302_SCLBIT,
526 #ifdef CONFIG_ETRAX_DS1302_RST_ON_GENERIC_PORT
527                        "GENIO",
528 #else
529                        "PB",
530 #endif
531                        CONFIG_ETRAX_DS1302_RSTBIT);
532                 print_rtc_status();
533                 retval = 1;
534         } else {
535                 stop();
536                 retval = 0;
537         }
538
539         return retval;
540 }
541
542
543 /* Just probe for the RTC and register the device to handle the ioctl needed. */
544
545 int __init
546 ds1302_init(void) 
547 {
548         i2c_init();
549
550         if (!ds1302_probe()) {
551 #ifdef CONFIG_ETRAX_DS1302_RST_ON_GENERIC_PORT
552 #if CONFIG_ETRAX_DS1302_RSTBIT == 27
553                 /*
554                  * The only way to set g27 to output is to enable ATA.
555                  *
556                  * Make sure that R_GEN_CONFIG is setup correct.
557                  */
558                 genconfig_shadow = ((genconfig_shadow &
559                                      ~IO_MASK(R_GEN_CONFIG, ata)) | 
560                                    (IO_STATE(R_GEN_CONFIG, ata, select)));    
561                 *R_GEN_CONFIG = genconfig_shadow;
562 #elif CONFIG_ETRAX_DS1302_RSTBIT == 0
563                 
564                 /* Set the direction of this bit to out. */             
565                 genconfig_shadow = ((genconfig_shadow &
566                                      ~IO_MASK(R_GEN_CONFIG, g0dir)) | 
567                                    (IO_STATE(R_GEN_CONFIG, g0dir, out)));    
568                 *R_GEN_CONFIG = genconfig_shadow;
569 #endif
570                 if (!ds1302_probe()) {
571                         printk(KERN_WARNING "%s: RTC not found.\n", ds1302_name);
572                         return -1;
573                 }
574 #else
575                 printk(KERN_WARNING "%s: RTC not found.\n", ds1302_name);
576                 return -1;
577 #endif
578         }
579         /* Initialise trickle charger */
580         ds1302_writereg(RTC_TRICKLECHARGER,
581                         RTC_TCR_PATTERN |(CONFIG_ETRAX_DS1302_TRICKLE_CHARGE & 0x0F));
582         /* Start clock by resetting CLOCK_HALT */
583         ds1302_writereg(RTC_SECONDS, (ds1302_readreg(RTC_SECONDS) & 0x7F));
584         return 0;
585 }
586
587 static int __init ds1302_register(void)
588 {
589         ds1302_init();
590         if (register_chrdev(RTC_MAJOR_NR, ds1302_name, &rtc_fops)) {
591                 printk(KERN_INFO "%s: unable to get major %d for rtc\n", 
592                        ds1302_name, RTC_MAJOR_NR);
593                 return -1;
594         }
595         return 0;
596
597 }
598
599 module_init(ds1302_register);