1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include <linux/fs.h>
17#include <linux/init.h>
18#include <linux/mm.h>
19#include <linux/module.h>
20#include <linux/miscdevice.h>
21#include <linux/delay.h>
22#include <linux/bcd.h>
23#include <linux/mutex.h>
24#include <linux/uaccess.h>
25#include <linux/io.h>
26
27#include <asm/system.h>
28#include <asm/rtc.h>
29#if defined(CONFIG_M32R)
30#include <asm/m32r.h>
31#endif
32
33#define RTC_MAJOR_NR 121
34
35static DEFINE_MUTEX(rtc_mutex);
36static const char ds1302_name[] = "ds1302";
37
38
39static void
40out_byte_rtc(unsigned int reg_addr, unsigned char x)
41{
42
43 outw(0x0001,(unsigned long)PLD_RTCRSTODT);
44
45 outw(((x<<8)|(reg_addr&0xff)),(unsigned long)PLD_RTCWRDATA);
46
47 outw(0x0002,(unsigned long)PLD_RTCCR);
48
49 while(inw((unsigned long)PLD_RTCCR));
50
51
52 outw(0x0000,(unsigned long)PLD_RTCRSTODT);
53
54}
55
56static unsigned char
57in_byte_rtc(unsigned int reg_addr)
58{
59 unsigned char retval;
60
61
62 outw(0x0001,(unsigned long)PLD_RTCRSTODT);
63
64 outw((reg_addr&0xff),(unsigned long)PLD_RTCRDDATA);
65
66 outw(0x0001,(unsigned long)PLD_RTCCR);
67
68 while(inw((unsigned long)PLD_RTCCR));
69
70
71 retval=(inw((unsigned long)PLD_RTCRDDATA) & 0xff00)>>8;
72
73
74 outw(0x0000,(unsigned long)PLD_RTCRSTODT);
75
76 return retval;
77}
78
79
80
81static void
82ds1302_wenable(void)
83{
84 out_byte_rtc(0x8e,0x00);
85}
86
87
88
89static void
90ds1302_wdisable(void)
91{
92 out_byte_rtc(0x8e,0x80);
93}
94
95
96
97
98
99unsigned char
100ds1302_readreg(int reg)
101{
102 unsigned char x;
103
104 x=in_byte_rtc((0x81 | (reg << 1)));
105
106 return x;
107}
108
109
110
111void
112ds1302_writereg(int reg, unsigned char val)
113{
114 ds1302_wenable();
115 out_byte_rtc((0x80 | (reg << 1)),val);
116 ds1302_wdisable();
117}
118
119void
120get_rtc_time(struct rtc_time *rtc_tm)
121{
122 unsigned long flags;
123
124 local_irq_save(flags);
125
126 rtc_tm->tm_sec = CMOS_READ(RTC_SECONDS);
127 rtc_tm->tm_min = CMOS_READ(RTC_MINUTES);
128 rtc_tm->tm_hour = CMOS_READ(RTC_HOURS);
129 rtc_tm->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
130 rtc_tm->tm_mon = CMOS_READ(RTC_MONTH);
131 rtc_tm->tm_year = CMOS_READ(RTC_YEAR);
132
133 local_irq_restore(flags);
134
135 rtc_tm->tm_sec = bcd2bin(rtc_tm->tm_sec);
136 rtc_tm->tm_min = bcd2bin(rtc_tm->tm_min);
137 rtc_tm->tm_hour = bcd2bin(rtc_tm->tm_hour);
138 rtc_tm->tm_mday = bcd2bin(rtc_tm->tm_mday);
139 rtc_tm->tm_mon = bcd2bin(rtc_tm->tm_mon);
140 rtc_tm->tm_year = bcd2bin(rtc_tm->tm_year);
141
142
143
144
145
146
147 if (rtc_tm->tm_year <= 69)
148 rtc_tm->tm_year += 100;
149
150 rtc_tm->tm_mon--;
151}
152
153static unsigned char days_in_mo[] =
154 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
155
156
157
158static long rtc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
159{
160 unsigned long flags;
161
162 switch(cmd) {
163 case RTC_RD_TIME:
164 {
165 struct rtc_time rtc_tm;
166
167 memset(&rtc_tm, 0, sizeof (struct rtc_time));
168 mutex_lock(&rtc_mutex);
169 get_rtc_time(&rtc_tm);
170 mutex_unlock(&rtc_mutex);
171 if (copy_to_user((struct rtc_time*)arg, &rtc_tm, sizeof(struct rtc_time)))
172 return -EFAULT;
173 return 0;
174 }
175
176 case RTC_SET_TIME:
177 {
178 struct rtc_time rtc_tm;
179 unsigned char mon, day, hrs, min, sec, leap_yr;
180 unsigned int yrs;
181
182 if (!capable(CAP_SYS_TIME))
183 return -EPERM;
184
185 if (copy_from_user(&rtc_tm, (struct rtc_time*)arg, sizeof(struct rtc_time)))
186 return -EFAULT;
187
188 yrs = rtc_tm.tm_year + 1900;
189 mon = rtc_tm.tm_mon + 1;
190 day = rtc_tm.tm_mday;
191 hrs = rtc_tm.tm_hour;
192 min = rtc_tm.tm_min;
193 sec = rtc_tm.tm_sec;
194
195
196 if ((yrs < 1970) || (yrs > 2069))
197 return -EINVAL;
198
199 leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
200
201 if ((mon > 12) || (day == 0))
202 return -EINVAL;
203
204 if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
205 return -EINVAL;
206
207 if ((hrs >= 24) || (min >= 60) || (sec >= 60))
208 return -EINVAL;
209
210 if (yrs >= 2000)
211 yrs -= 2000;
212 else
213 yrs -= 1900;
214
215 sec = bin2bcd(sec);
216 min = bin2bcd(min);
217 hrs = bin2bcd(hrs);
218 day = bin2bcd(day);
219 mon = bin2bcd(mon);
220 yrs = bin2bcd(yrs);
221
222 mutex_lock(&rtc_mutex);
223 local_irq_save(flags);
224 CMOS_WRITE(yrs, RTC_YEAR);
225 CMOS_WRITE(mon, RTC_MONTH);
226 CMOS_WRITE(day, RTC_DAY_OF_MONTH);
227 CMOS_WRITE(hrs, RTC_HOURS);
228 CMOS_WRITE(min, RTC_MINUTES);
229 CMOS_WRITE(sec, RTC_SECONDS);
230 local_irq_restore(flags);
231 mutex_unlock(&rtc_mutex);
232
233
234
235
236
237
238 return 0;
239 }
240
241 case RTC_SET_CHARGE:
242 {
243 int tcs_val;
244
245 if (!capable(CAP_SYS_TIME))
246 return -EPERM;
247
248 if(copy_from_user(&tcs_val, (int*)arg, sizeof(int)))
249 return -EFAULT;
250
251 mutex_lock(&rtc_mutex);
252 tcs_val = RTC_TCR_PATTERN | (tcs_val & 0x0F);
253 ds1302_writereg(RTC_TRICKLECHARGER, tcs_val);
254 mutex_unlock(&rtc_mutex);
255 return 0;
256 }
257 default:
258 return -EINVAL;
259 }
260}
261
262int
263get_rtc_status(char *buf)
264{
265 char *p;
266 struct rtc_time tm;
267
268 p = buf;
269
270 get_rtc_time(&tm);
271
272
273
274
275
276
277 p += sprintf(p,
278 "rtc_time\t: %02d:%02d:%02d\n"
279 "rtc_date\t: %04d-%02d-%02d\n",
280 tm.tm_hour, tm.tm_min, tm.tm_sec,
281 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
282
283 return p - buf;
284}
285
286
287
288
289static const struct file_operations rtc_fops = {
290 .owner = THIS_MODULE,
291 .unlocked_ioctl = rtc_ioctl,
292 .llseek = noop_llseek,
293};
294
295
296
297#define MAGIC_PATTERN 0x42
298
299static int __init
300ds1302_probe(void)
301{
302 int retval, res, baur;
303
304 baur=(boot_cpu_data.bus_clock/(2*1000*1000));
305
306 printk("%s: Set PLD_RTCBAUR = %d\n", ds1302_name,baur);
307
308 outw(0x0000,(unsigned long)PLD_RTCCR);
309 outw(0x0000,(unsigned long)PLD_RTCRSTODT);
310 outw(baur,(unsigned long)PLD_RTCBAUR);
311
312
313
314 ds1302_wenable();
315
316
317 out_byte_rtc(0xc0,MAGIC_PATTERN);
318
319
320 if((res = in_byte_rtc(0xc1)) == MAGIC_PATTERN) {
321 char buf[100];
322 ds1302_wdisable();
323 printk("%s: RTC found.\n", ds1302_name);
324 get_rtc_status(buf);
325 printk(buf);
326 retval = 1;
327 } else {
328 printk("%s: RTC not found.\n", ds1302_name);
329 retval = 0;
330 }
331
332 return retval;
333}
334
335
336
337
338int __init
339ds1302_init(void)
340{
341 if (!ds1302_probe()) {
342 return -1;
343 }
344 return 0;
345}
346
347static int __init ds1302_register(void)
348{
349 ds1302_init();
350 if (register_chrdev(RTC_MAJOR_NR, ds1302_name, &rtc_fops)) {
351 printk(KERN_INFO "%s: unable to get major %d for rtc\n",
352 ds1302_name, RTC_MAJOR_NR);
353 return -1;
354 }
355 return 0;
356}
357
358module_init(ds1302_register);
359