2 * ds1302.c - Support for the Dallas Semiconductor DS1302 Timekeeping Chip
4 * Rex G. Feany <rfeany@zumanetworks.com>
17 /* Happy Fun Defines(tm) */
18 #define RESET rtc_go_low(RST), rtc_go_low(SCLK)
19 #define N_RESET rtc_go_high(RST), rtc_go_low(SCLK)
21 #define CLOCK_HIGH rtc_go_high(SCLK)
22 #define CLOCK_LOW rtc_go_low(SCLK)
24 #define DATA_HIGH rtc_go_high(DATA)
25 #define DATA_LOW rtc_go_low(DATA)
26 #define DATA_READ (GTREGREAD(GPP_VALUE) & DATA)
31 # define DPRINTF(x,args...) printf("ds1302: " x , ##args)
32 static inline void DUMP(const char *ptr, int num)
34 while (num--) printf("%x ", *ptr++);
38 # define DPRINTF(x,args...)
39 # define DUMP(ptr, num)
42 /* time data format for DS1302 */
45 unsigned char CH:1; /* clock halt 1=stop 0=start */
46 unsigned char sec10:3;
49 unsigned char zero0:1;
50 unsigned char min10:3;
53 unsigned char fmt:1; /* 1=12 hour 0=24 hour */
54 unsigned char zero1:1;
55 unsigned char hr10:2; /* 10 (0-2) or am/pm (am/pm, 0-1) */
58 unsigned char zero2:2;
59 unsigned char date10:2;
62 unsigned char zero3:3;
63 unsigned char month10:1;
64 unsigned char month:4;
66 unsigned char zero4:5;
67 unsigned char day:3; /* day of week */
69 unsigned char year10:4;
72 unsigned char WP:1; /* write protect 1=protect 0=unprot */
73 unsigned char zero5:7;
76 static int ds1302_initted=0;
80 rtc_go_high(unsigned int mask)
82 unsigned int f = GTREGREAD(GPP_VALUE) | mask;
84 GT_REG_WRITE(GPP_VALUE, f);
88 rtc_go_low(unsigned int mask)
90 unsigned int f = GTREGREAD(GPP_VALUE) & ~mask;
92 GT_REG_WRITE(GPP_VALUE, f);
96 rtc_go_input(unsigned int mask)
98 unsigned int f = GTREGREAD(GPP_IO_CONTROL) & ~mask;
100 GT_REG_WRITE(GPP_IO_CONTROL, f);
104 rtc_go_output(unsigned int mask)
106 unsigned int f = GTREGREAD(GPP_IO_CONTROL) | mask;
108 GT_REG_WRITE(GPP_IO_CONTROL, f);
111 /* Access data in RTC */
114 write_byte(unsigned char b)
117 unsigned char mask=1;
120 CLOCK_LOW; /* Lower clock */
121 (b&mask)?DATA_HIGH:DATA_LOW; /* set data */
123 CLOCK_HIGH; /* latch data with rising clock */
133 unsigned char mask=1;
139 if (DATA_READ) b|=mask; /* if this bit is high, set in b */
140 CLOCK_HIGH; /* clock out next bit */
148 read_ser_drv(unsigned char addr, unsigned char *buf, int count)
155 DPRINTF("READ 0x%x bytes @ 0x%x [ ", count, addr);
161 rtc_go_input(DATA); /* Put gpp pin into input mode */
163 for(i=0;i<count;i++) *(buf++)=read_byte();
165 rtc_go_output(DATA);/* Reset gpp for output */
172 write_ser_drv(unsigned char addr, unsigned char *buf, int count)
176 DPRINTF("WRITE 0x%x bytes @ 0x%x [ ", count, addr);
179 addr&=~1; /* WRITE */
183 for(i=0;i<count;i++) write_byte(*(buf++));
192 struct ds1302_st bbclk;
198 rtc_go_output(DATA|SCLK|RST);
200 /* disable write protect */
202 write_ser_drv(0x8e,&b,1);
205 b = 0xa5; /* 1010.0101 */
206 write_ser_drv(0x90,&b,1);
209 read_ser_drv(0xbe, (unsigned char *)&bbclk, 8);
214 printf("ds1302: Clock was halted, starting clock\n");
220 printf("ds1302: Clock was in 12 hour mode, fixing\n");
226 printf("ds1302: Year was corrupted, fixing\n");
227 bbclk.year10=100/10; /* 2000 - why not? ;) */
232 /* Write out the changes if needed */
234 /* enable write protect */
236 write_ser_drv(0xbe,(unsigned char *)&bbclk,8);
238 /* Else just turn write protect on */
240 write_ser_drv(0x8e,&b,1);
242 DPRINTF("init done\n");
250 if(!ds1302_initted) rtc_init();
255 rtc_get(struct rtc_time *tmp)
258 struct ds1302_st bbclk;
260 if(!ds1302_initted) rtc_init();
262 read_ser_drv(0xbe,(unsigned char *)&bbclk, 8); /* read burst */
265 printf("ds1302: rtc_get: Clock was halted, clock probably "
270 tmp->tm_sec=10*bbclk.sec10+bbclk.sec;
271 tmp->tm_min=10*bbclk.min10+bbclk.min;
272 tmp->tm_hour=10*bbclk.hr10+bbclk.hr;
273 tmp->tm_wday=bbclk.day;
274 tmp->tm_mday=10*bbclk.date10+bbclk.date;
275 tmp->tm_mon=10*bbclk.month10+bbclk.month;
276 tmp->tm_year=10*bbclk.year10+bbclk.year + 1900;
281 DPRINTF("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
282 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
283 tmp->tm_hour, tmp->tm_min, tmp->tm_sec );
288 int rtc_set(struct rtc_time *tmp)
290 struct ds1302_st bbclk;
293 if(!ds1302_initted) rtc_init();
295 DPRINTF("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
296 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
297 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
299 memset(&bbclk,0,sizeof(bbclk));
300 bbclk.CH=0; /* dont halt */
301 bbclk.WP=1; /* write protect when we're done */
303 bbclk.sec10=tmp->tm_sec/10;
304 bbclk.sec=tmp->tm_sec%10;
306 bbclk.min10=tmp->tm_min/10;
307 bbclk.min=tmp->tm_min%10;
309 bbclk.hr10=tmp->tm_hour/10;
310 bbclk.hr=tmp->tm_hour%10;
312 bbclk.day=tmp->tm_wday;
314 bbclk.date10=tmp->tm_mday/10;
315 bbclk.date=tmp->tm_mday%10;
317 bbclk.month10=tmp->tm_mon/10;
318 bbclk.month=tmp->tm_mon%10;
320 tmp->tm_year -= 1900;
321 bbclk.year10=tmp->tm_year/10;
322 bbclk.year=tmp->tm_year%10;
324 write_ser_drv(0x8e,&b,1); /* disable write protect */
325 write_ser_drv(0xbe,(unsigned char *)&bbclk, 8); /* write burst */