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