Root/
1 | /*!*************************************************************************** |
2 | *! |
3 | *! FILE NAME : ds1302.c |
4 | *! |
5 | *! DESCRIPTION: Implements an interface for the DS1302 RTC |
6 | *! |
7 | *! Functions exported: ds1302_readreg, ds1302_writereg, ds1302_init, get_rtc_status |
8 | *! |
9 | *! --------------------------------------------------------------------------- |
10 | *! |
11 | *! (C) Copyright 1999, 2000, 2001 Axis Communications AB, LUND, SWEDEN |
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/rtc.h> |
28 | #if defined(CONFIG_M32R) |
29 | #include <asm/m32r.h> |
30 | #endif |
31 | |
32 | #define RTC_MAJOR_NR 121 /* local major, change later */ |
33 | |
34 | static DEFINE_MUTEX(rtc_mutex); |
35 | static const char ds1302_name[] = "ds1302"; |
36 | |
37 | /* Send 8 bits. */ |
38 | static void |
39 | out_byte_rtc(unsigned int reg_addr, unsigned char x) |
40 | { |
41 | //RST H |
42 | outw(0x0001,(unsigned long)PLD_RTCRSTODT); |
43 | //write data |
44 | outw(((x<<8)|(reg_addr&0xff)),(unsigned long)PLD_RTCWRDATA); |
45 | //WE |
46 | outw(0x0002,(unsigned long)PLD_RTCCR); |
47 | //wait |
48 | while(inw((unsigned long)PLD_RTCCR)); |
49 | |
50 | //RST L |
51 | outw(0x0000,(unsigned long)PLD_RTCRSTODT); |
52 | |
53 | } |
54 | |
55 | static unsigned char |
56 | in_byte_rtc(unsigned int reg_addr) |
57 | { |
58 | unsigned char retval; |
59 | |
60 | //RST H |
61 | outw(0x0001,(unsigned long)PLD_RTCRSTODT); |
62 | //write data |
63 | outw((reg_addr&0xff),(unsigned long)PLD_RTCRDDATA); |
64 | //RE |
65 | outw(0x0001,(unsigned long)PLD_RTCCR); |
66 | //wait |
67 | while(inw((unsigned long)PLD_RTCCR)); |
68 | |
69 | //read data |
70 | retval=(inw((unsigned long)PLD_RTCRDDATA) & 0xff00)>>8; |
71 | |
72 | //RST L |
73 | outw(0x0000,(unsigned long)PLD_RTCRSTODT); |
74 | |
75 | return retval; |
76 | } |
77 | |
78 | /* Enable writing. */ |
79 | |
80 | static void |
81 | ds1302_wenable(void) |
82 | { |
83 | out_byte_rtc(0x8e,0x00); |
84 | } |
85 | |
86 | /* Disable writing. */ |
87 | |
88 | static void |
89 | ds1302_wdisable(void) |
90 | { |
91 | out_byte_rtc(0x8e,0x80); |
92 | } |
93 | |
94 | |
95 | |
96 | /* Read a byte from the selected register in the DS1302. */ |
97 | |
98 | unsigned char |
99 | ds1302_readreg(int reg) |
100 | { |
101 | unsigned char x; |
102 | |
103 | x=in_byte_rtc((0x81 | (reg << 1))); /* read register */ |
104 | |
105 | return x; |
106 | } |
107 | |
108 | /* Write a byte to the selected register. */ |
109 | |
110 | void |
111 | ds1302_writereg(int reg, unsigned char val) |
112 | { |
113 | ds1302_wenable(); |
114 | out_byte_rtc((0x80 | (reg << 1)),val); |
115 | ds1302_wdisable(); |
116 | } |
117 | |
118 | void |
119 | get_rtc_time(struct rtc_time *rtc_tm) |
120 | { |
121 | unsigned long flags; |
122 | |
123 | local_irq_save(flags); |
124 | |
125 | rtc_tm->tm_sec = CMOS_READ(RTC_SECONDS); |
126 | rtc_tm->tm_min = CMOS_READ(RTC_MINUTES); |
127 | rtc_tm->tm_hour = CMOS_READ(RTC_HOURS); |
128 | rtc_tm->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH); |
129 | rtc_tm->tm_mon = CMOS_READ(RTC_MONTH); |
130 | rtc_tm->tm_year = CMOS_READ(RTC_YEAR); |
131 | |
132 | local_irq_restore(flags); |
133 | |
134 | rtc_tm->tm_sec = bcd2bin(rtc_tm->tm_sec); |
135 | rtc_tm->tm_min = bcd2bin(rtc_tm->tm_min); |
136 | rtc_tm->tm_hour = bcd2bin(rtc_tm->tm_hour); |
137 | rtc_tm->tm_mday = bcd2bin(rtc_tm->tm_mday); |
138 | rtc_tm->tm_mon = bcd2bin(rtc_tm->tm_mon); |
139 | rtc_tm->tm_year = bcd2bin(rtc_tm->tm_year); |
140 | |
141 | /* |
142 | * Account for differences between how the RTC uses the values |
143 | * and how they are defined in a struct rtc_time; |
144 | */ |
145 | |
146 | if (rtc_tm->tm_year <= 69) |
147 | rtc_tm->tm_year += 100; |
148 | |
149 | rtc_tm->tm_mon--; |
150 | } |
151 | |
152 | static unsigned char days_in_mo[] = |
153 | {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; |
154 | |
155 | /* ioctl that supports RTC_RD_TIME and RTC_SET_TIME (read and set time/date). */ |
156 | |
157 | static long rtc_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
158 | { |
159 | unsigned long flags; |
160 | |
161 | switch(cmd) { |
162 | case RTC_RD_TIME: /* read the time/date from RTC */ |
163 | { |
164 | struct rtc_time rtc_tm; |
165 | |
166 | memset(&rtc_tm, 0, sizeof (struct rtc_time)); |
167 | mutex_lock(&rtc_mutex); |
168 | get_rtc_time(&rtc_tm); |
169 | mutex_unlock(&rtc_mutex); |
170 | if (copy_to_user((struct rtc_time*)arg, &rtc_tm, sizeof(struct rtc_time))) |
171 | return -EFAULT; |
172 | return 0; |
173 | } |
174 | |
175 | case RTC_SET_TIME: /* set the RTC */ |
176 | { |
177 | struct rtc_time rtc_tm; |
178 | unsigned char mon, day, hrs, min, sec, leap_yr; |
179 | unsigned int yrs; |
180 | |
181 | if (!capable(CAP_SYS_TIME)) |
182 | return -EPERM; |
183 | |
184 | if (copy_from_user(&rtc_tm, (struct rtc_time*)arg, sizeof(struct rtc_time))) |
185 | return -EFAULT; |
186 | |
187 | yrs = rtc_tm.tm_year + 1900; |
188 | mon = rtc_tm.tm_mon + 1; /* tm_mon starts at zero */ |
189 | day = rtc_tm.tm_mday; |
190 | hrs = rtc_tm.tm_hour; |
191 | min = rtc_tm.tm_min; |
192 | sec = rtc_tm.tm_sec; |
193 | |
194 | |
195 | if ((yrs < 1970) || (yrs > 2069)) |
196 | return -EINVAL; |
197 | |
198 | leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400)); |
199 | |
200 | if ((mon > 12) || (day == 0)) |
201 | return -EINVAL; |
202 | |
203 | if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr))) |
204 | return -EINVAL; |
205 | |
206 | if ((hrs >= 24) || (min >= 60) || (sec >= 60)) |
207 | return -EINVAL; |
208 | |
209 | if (yrs >= 2000) |
210 | yrs -= 2000; /* RTC (0, 1, ... 69) */ |
211 | else |
212 | yrs -= 1900; /* RTC (70, 71, ... 99) */ |
213 | |
214 | sec = bin2bcd(sec); |
215 | min = bin2bcd(min); |
216 | hrs = bin2bcd(hrs); |
217 | day = bin2bcd(day); |
218 | mon = bin2bcd(mon); |
219 | yrs = bin2bcd(yrs); |
220 | |
221 | mutex_lock(&rtc_mutex); |
222 | local_irq_save(flags); |
223 | CMOS_WRITE(yrs, RTC_YEAR); |
224 | CMOS_WRITE(mon, RTC_MONTH); |
225 | CMOS_WRITE(day, RTC_DAY_OF_MONTH); |
226 | CMOS_WRITE(hrs, RTC_HOURS); |
227 | CMOS_WRITE(min, RTC_MINUTES); |
228 | CMOS_WRITE(sec, RTC_SECONDS); |
229 | local_irq_restore(flags); |
230 | mutex_unlock(&rtc_mutex); |
231 | |
232 | /* Notice that at this point, the RTC is updated but |
233 | * the kernel is still running with the old time. |
234 | * You need to set that separately with settimeofday |
235 | * or adjtimex. |
236 | */ |
237 | return 0; |
238 | } |
239 | |
240 | case RTC_SET_CHARGE: /* set the RTC TRICKLE CHARGE register */ |
241 | { |
242 | int tcs_val; |
243 | |
244 | if (!capable(CAP_SYS_TIME)) |
245 | return -EPERM; |
246 | |
247 | if(copy_from_user(&tcs_val, (int*)arg, sizeof(int))) |
248 | return -EFAULT; |
249 | |
250 | mutex_lock(&rtc_mutex); |
251 | tcs_val = RTC_TCR_PATTERN | (tcs_val & 0x0F); |
252 | ds1302_writereg(RTC_TRICKLECHARGER, tcs_val); |
253 | mutex_unlock(&rtc_mutex); |
254 | return 0; |
255 | } |
256 | default: |
257 | return -EINVAL; |
258 | } |
259 | } |
260 | |
261 | int |
262 | get_rtc_status(char *buf) |
263 | { |
264 | char *p; |
265 | struct rtc_time tm; |
266 | |
267 | p = buf; |
268 | |
269 | get_rtc_time(&tm); |
270 | |
271 | /* |
272 | * There is no way to tell if the luser has the RTC set for local |
273 | * time or for Universal Standard Time (GMT). Probably local though. |
274 | */ |
275 | |
276 | p += sprintf(p, |
277 | "rtc_time\t: %02d:%02d:%02d\n" |
278 | "rtc_date\t: %04d-%02d-%02d\n", |
279 | tm.tm_hour, tm.tm_min, tm.tm_sec, |
280 | tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday); |
281 | |
282 | return p - buf; |
283 | } |
284 | |
285 | |
286 | /* The various file operations we support. */ |
287 | |
288 | static const struct file_operations rtc_fops = { |
289 | .owner = THIS_MODULE, |
290 | .unlocked_ioctl = rtc_ioctl, |
291 | .llseek = noop_llseek, |
292 | }; |
293 | |
294 | /* Probe for the chip by writing something to its RAM and try reading it back. */ |
295 | |
296 | #define MAGIC_PATTERN 0x42 |
297 | |
298 | static int __init |
299 | ds1302_probe(void) |
300 | { |
301 | int retval, res, baur; |
302 | |
303 | baur=(boot_cpu_data.bus_clock/(2*1000*1000)); |
304 | |
305 | printk("%s: Set PLD_RTCBAUR = %d\n", ds1302_name,baur); |
306 | |
307 | outw(0x0000,(unsigned long)PLD_RTCCR); |
308 | outw(0x0000,(unsigned long)PLD_RTCRSTODT); |
309 | outw(baur,(unsigned long)PLD_RTCBAUR); |
310 | |
311 | /* Try to talk to timekeeper. */ |
312 | |
313 | ds1302_wenable(); |
314 | /* write RAM byte 0 */ |
315 | /* write something magic */ |
316 | out_byte_rtc(0xc0,MAGIC_PATTERN); |
317 | |
318 | /* read RAM byte 0 */ |
319 | if((res = in_byte_rtc(0xc1)) == MAGIC_PATTERN) { |
320 | char buf[100]; |
321 | ds1302_wdisable(); |
322 | printk("%s: RTC found.\n", ds1302_name); |
323 | get_rtc_status(buf); |
324 | printk(buf); |
325 | retval = 1; |
326 | } else { |
327 | printk("%s: RTC not found.\n", ds1302_name); |
328 | retval = 0; |
329 | } |
330 | |
331 | return retval; |
332 | } |
333 | |
334 | |
335 | /* Just probe for the RTC and register the device to handle the ioctl needed. */ |
336 | |
337 | int __init |
338 | ds1302_init(void) |
339 | { |
340 | if (!ds1302_probe()) { |
341 | return -1; |
342 | } |
343 | return 0; |
344 | } |
345 | |
346 | static int __init ds1302_register(void) |
347 | { |
348 | ds1302_init(); |
349 | if (register_chrdev(RTC_MAJOR_NR, ds1302_name, &rtc_fops)) { |
350 | printk(KERN_INFO "%s: unable to get major %d for rtc\n", |
351 | ds1302_name, RTC_MAJOR_NR); |
352 | return -1; |
353 | } |
354 | return 0; |
355 | } |
356 | |
357 | module_init(ds1302_register); |
358 |
Branches:
ben-wpan
ben-wpan-stefan
javiroman/ks7010
jz-2.6.34
jz-2.6.34-rc5
jz-2.6.34-rc6
jz-2.6.34-rc7
jz-2.6.35
jz-2.6.36
jz-2.6.37
jz-2.6.38
jz-2.6.39
jz-3.0
jz-3.1
jz-3.11
jz-3.12
jz-3.13
jz-3.15
jz-3.16
jz-3.18-dt
jz-3.2
jz-3.3
jz-3.4
jz-3.5
jz-3.6
jz-3.6-rc2-pwm
jz-3.9
jz-3.9-clk
jz-3.9-rc8
jz47xx
jz47xx-2.6.38
master
Tags:
od-2011-09-04
od-2011-09-18
v2.6.34-rc5
v2.6.34-rc6
v2.6.34-rc7
v3.9