2 * ds1302.c - Support for the Dallas Semiconductor DS1302 Timekeeping Chip
11 #include <linux/delay.h>
18 /* Happy Fun Defines(tm) */
19 #define RESET rtc_go_low(RST), rtc_go_low(SCLK)
20 #define N_RESET rtc_go_high(RST), rtc_go_low(SCLK)
22 #define CLOCK_HIGH rtc_go_high(SCLK)
23 #define CLOCK_LOW rtc_go_low(SCLK)
25 #define DATA_HIGH rtc_go_high(DATA)
26 #define DATA_LOW rtc_go_low(DATA)
27 #define DATA_READ (GTREGREAD(GPP_VALUE) & DATA)
32 # define DPRINTF(x,args...) printf("ds1302: " x , ##args)
33 static inline void DUMP(const char *ptr, int num)
35 while (num--) printf("%x ", *ptr++);
39 # define DPRINTF(x,args...)
40 # define DUMP(ptr, num)
43 /* time data format for DS1302 */
46 unsigned char CH:1; /* clock halt 1=stop 0=start */
47 unsigned char sec10:3;
50 unsigned char zero0:1;
51 unsigned char min10:3;
54 unsigned char fmt:1; /* 1=12 hour 0=24 hour */
55 unsigned char zero1:1;
56 unsigned char hr10:2; /* 10 (0-2) or am/pm (am/pm, 0-1) */
59 unsigned char zero2:2;
60 unsigned char date10:2;
63 unsigned char zero3:3;
64 unsigned char month10:1;
65 unsigned char month:4;
67 unsigned char zero4:5;
68 unsigned char day:3; /* day of week */
70 unsigned char year10:4;
73 unsigned char WP:1; /* write protect 1=protect 0=unprot */
74 unsigned char zero5:7;
77 static int ds1302_initted=0;
81 rtc_go_high(unsigned int mask)
83 unsigned int f = GTREGREAD(GPP_VALUE) | mask;
85 GT_REG_WRITE(GPP_VALUE, f);
89 rtc_go_low(unsigned int mask)
91 unsigned int f = GTREGREAD(GPP_VALUE) & ~mask;
93 GT_REG_WRITE(GPP_VALUE, f);
97 rtc_go_input(unsigned int mask)
99 unsigned int f = GTREGREAD(GPP_IO_CONTROL) & ~mask;
101 GT_REG_WRITE(GPP_IO_CONTROL, f);
105 rtc_go_output(unsigned int mask)
107 unsigned int f = GTREGREAD(GPP_IO_CONTROL) | mask;
109 GT_REG_WRITE(GPP_IO_CONTROL, f);
112 /* Access data in RTC */
115 write_byte(unsigned char b)
118 unsigned char mask=1;
121 CLOCK_LOW; /* Lower clock */
122 (b&mask)?DATA_HIGH:DATA_LOW; /* set data */
124 CLOCK_HIGH; /* latch data with rising clock */
134 unsigned char mask=1;
140 if (DATA_READ) b|=mask; /* if this bit is high, set in b */
141 CLOCK_HIGH; /* clock out next bit */
149 read_ser_drv(unsigned char addr, unsigned char *buf, int count)
156 DPRINTF("READ 0x%x bytes @ 0x%x [ ", count, addr);
162 rtc_go_input(DATA); /* Put gpp pin into input mode */
164 for(i=0;i<count;i++) *(buf++)=read_byte();
166 rtc_go_output(DATA);/* Reset gpp for output */
173 write_ser_drv(unsigned char addr, unsigned char *buf, int count)
177 DPRINTF("WRITE 0x%x bytes @ 0x%x [ ", count, addr);
180 addr&=~1; /* WRITE */
184 for(i=0;i<count;i++) write_byte(*(buf++));
193 struct ds1302_st bbclk;
199 rtc_go_output(DATA|SCLK|RST);
201 /* disable write protect */
203 write_ser_drv(0x8e,&b,1);
206 b = 0xa5; /* 1010.0101 */
207 write_ser_drv(0x90,&b,1);
210 read_ser_drv(0xbe, (unsigned char *)&bbclk, 8);
215 printf("ds1302: Clock was halted, starting clock\n");
221 printf("ds1302: Clock was in 12 hour mode, fixing\n");
227 printf("ds1302: Year was corrupted, fixing\n");
228 bbclk.year10=100/10; /* 2000 - why not? ;) */
233 /* Write out the changes if needed */
235 /* enable write protect */
237 write_ser_drv(0xbe,(unsigned char *)&bbclk,8);
239 /* Else just turn write protect on */
241 write_ser_drv(0x8e,&b,1);
243 DPRINTF("init done\n");
251 if(!ds1302_initted) rtc_init();
256 rtc_get(struct rtc_time *tmp)
259 struct ds1302_st bbclk;
261 if(!ds1302_initted) rtc_init();
263 read_ser_drv(0xbe,(unsigned char *)&bbclk, 8); /* read burst */
266 printf("ds1302: rtc_get: Clock was halted, clock probably "
271 tmp->tm_sec=10*bbclk.sec10+bbclk.sec;
272 tmp->tm_min=10*bbclk.min10+bbclk.min;
273 tmp->tm_hour=10*bbclk.hr10+bbclk.hr;
274 tmp->tm_wday=bbclk.day;
275 tmp->tm_mday=10*bbclk.date10+bbclk.date;
276 tmp->tm_mon=10*bbclk.month10+bbclk.month;
277 tmp->tm_year=10*bbclk.year10+bbclk.year + 1900;
282 DPRINTF("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
283 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
284 tmp->tm_hour, tmp->tm_min, tmp->tm_sec );
289 int rtc_set(struct rtc_time *tmp)
291 struct ds1302_st bbclk;
294 if(!ds1302_initted) rtc_init();
296 DPRINTF("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
297 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
298 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
300 memset(&bbclk,0,sizeof(bbclk));
301 bbclk.CH=0; /* dont halt */
302 bbclk.WP=1; /* write protect when we're done */
304 bbclk.sec10=tmp->tm_sec/10;
305 bbclk.sec=tmp->tm_sec%10;
307 bbclk.min10=tmp->tm_min/10;
308 bbclk.min=tmp->tm_min%10;
310 bbclk.hr10=tmp->tm_hour/10;
311 bbclk.hr=tmp->tm_hour%10;
313 bbclk.day=tmp->tm_wday;
315 bbclk.date10=tmp->tm_mday/10;
316 bbclk.date=tmp->tm_mday%10;
318 bbclk.month10=tmp->tm_mon/10;
319 bbclk.month=tmp->tm_mon%10;
321 tmp->tm_year -= 1900;
322 bbclk.year10=tmp->tm_year/10;
323 bbclk.year=tmp->tm_year%10;
325 write_ser_drv(0x8e,&b,1); /* disable write protect */
326 write_ser_drv(0xbe,(unsigned char *)&bbclk, 8); /* write burst */