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