move lots of kernel related packages to the new system/ folder
[openwrt/staging/chunkeey.git] / package / system / rtc-rv5c386a / src / rtc.c
1 /*
2 * Real Time Clock driver for WL-HDD
3 *
4 * Copyright (C) 2007 Andreas Engel
5 *
6 * Hacked together mostly by copying the relevant code parts from:
7 * drivers/i2c/i2c-bcm5365.c
8 * drivers/i2c/i2c-algo-bit.c
9 * drivers/char/rtc.c
10 *
11 * Note 1:
12 * This module uses the standard char device (10,135), while the Asus module
13 * rtcdrv.o uses (12,0). So, both can coexist which might be handy during
14 * development (but see the comment in rtc_open()).
15 *
16 * Note 2:
17 * You might need to set the clock once after loading the driver the first
18 * time because the driver switches the chip into 24h mode if it is running
19 * in 12h mode.
20 *
21 * Usage:
22 * For compatibility reasons with the original asus driver, the time can be
23 * read and set via the /dev/rtc device entry. The only accepted data format
24 * is "YYYY:MM:DD:W:HH:MM:SS\n". See OpenWrt wiki for a script which handles
25 * this format.
26 *
27 * In addition, this driver supports the standard ioctl() calls for setting
28 * and reading the hardware clock, so the ordinary hwclock utility can also
29 * be used.
30 *
31 * This program is free software; you can redistribute it and/or
32 * modify it under the terms of the GNU General Public License
33 * as published by the Free Software Foundation; either version
34 * 2 of the License, or (at your option) any later version.
35 *
36 * TODO:
37 * - add a /proc/driver/rtc interface?
38 * - make the battery failure bit available through the /proc interface?
39 *
40 * $Id: rtc.c 7 2007-05-25 19:37:01Z ae $
41 */
42
43 #include <linux/module.h>
44 #include <linux/kmod.h>
45 #include <linux/kernel.h>
46 #include <linux/types.h>
47 #include <linux/miscdevice.h>
48 #include <linux/ioport.h>
49 #include <linux/fcntl.h>
50 #include <linux/mc146818rtc.h>
51 #include <linux/init.h>
52 #include <linux/spinlock.h>
53 #include <linux/rtc.h>
54 #include <linux/delay.h>
55 #include <linux/version.h>
56 #include <linux/gpio.h>
57 #include <linux/uaccess.h>
58
59 #include <asm/current.h>
60 #include <asm/system.h>
61
62 #include <bcm47xx.h>
63 #include <nvram.h>
64
65 #define RTC_IS_OPEN 0x01 /* Means /dev/rtc is in use. */
66
67 /* Can be changed via a module parameter. */
68 static int rtc_debug = 0;
69
70 static unsigned long rtc_status = 0; /* Bitmapped status byte. */
71
72 /* These settings are platform dependents. */
73 unsigned int sda_index = 0;
74 unsigned int scl_index = 0;
75
76 #define I2C_READ_MASK 1
77 #define I2C_WRITE_MASK 0
78
79 #define I2C_ACK 1
80 #define I2C_NAK 0
81
82 #define RTC_EPOCH 1900
83 #define RTC_I2C_ADDRESS (0x32 << 1)
84 #define RTC_24HOUR_MODE_MASK 0x20
85 #define RTC_PM_MASK 0x20
86 #define RTC_VDET_MASK 0x40
87 #define RTC_Y2K_MASK 0x80
88
89 /*
90 * Delay in microseconds for generating the pulses on the I2C bus. We use
91 * a rather conservative setting here. See datasheet of the RTC chip.
92 */
93 #define ADAP_DELAY 50
94
95 /* Avoid spurious compiler warnings. */
96 #define UNUSED __attribute__((unused))
97
98 MODULE_AUTHOR("Andreas Engel");
99 MODULE_LICENSE("GPL");
100
101 /* Test stolen from switch-adm.c. */
102 module_param(rtc_debug, int, 0);
103
104 static inline void sdalo(void)
105 {
106 gpio_direction_output(sda_index, 1);
107 udelay(ADAP_DELAY);
108 }
109
110 static inline void sdahi(void)
111 {
112 gpio_direction_input(sda_index);
113 udelay(ADAP_DELAY);
114 }
115
116 static inline void scllo(void)
117 {
118 gpio_direction_output(scl_index, 1);
119 udelay(ADAP_DELAY);
120 }
121
122 static inline int getscl(void)
123 {
124 return (gpio_get_value(scl_index));
125 }
126
127 static inline int getsda(void)
128 {
129 return (gpio_get_value(sda_index));
130 }
131
132 /*
133 * We shouldn't simply set the SCL pin to high. Like SDA, the SCL line is
134 * bidirectional too. According to the I2C spec, the slave is allowed to
135 * pull down the SCL line to slow down the clock, so we need to check this.
136 * Generally, we'd need a timeout here, but in our case, we just check the
137 * line, assuming the RTC chip behaves well.
138 */
139 static int sclhi(void)
140 {
141 gpio_direction_input(scl_index);
142 udelay(ADAP_DELAY);
143 if (!getscl()) {
144 printk(KERN_ERR "SCL pin should be low\n");
145 return -ETIMEDOUT;
146 }
147 return 0;
148 }
149
150 static void i2c_start(void)
151 {
152 sdalo();
153 scllo();
154 }
155
156 static void i2c_stop(void)
157 {
158 sdalo();
159 sclhi();
160 sdahi();
161 }
162
163 static int i2c_outb(int c)
164 {
165 int i;
166 int ack;
167
168 /* assert: scl is low */
169 for (i = 7; i >= 0; i--) {
170 if (c & ( 1 << i )) {
171 sdahi();
172 } else {
173 sdalo();
174 }
175 if (sclhi() < 0) { /* timed out */
176 sdahi(); /* we don't want to block the net */
177 return -ETIMEDOUT;
178 };
179 scllo();
180 }
181 sdahi();
182 if (sclhi() < 0) {
183 return -ETIMEDOUT;
184 };
185 /* read ack: SDA should be pulled down by slave */
186 ack = getsda() == 0; /* ack: sda is pulled low ->success. */
187 scllo();
188
189 if (rtc_debug)
190 printk(KERN_DEBUG "i2c_outb(0x%02x) -> %s\n",
191 c, ack ? "ACK": "NAK");
192
193 return ack; /* return 1 if device acked */
194 /* assert: scl is low (sda undef) */
195 }
196
197 static int i2c_inb(int ack)
198 {
199 int i;
200 unsigned int indata = 0;
201
202 /* assert: scl is low */
203
204 sdahi();
205 for (i = 0; i < 8; i++) {
206 if (sclhi() < 0) {
207 return -ETIMEDOUT;
208 };
209 indata *= 2;
210 if (getsda())
211 indata |= 0x01;
212 scllo();
213 }
214 if (ack) {
215 sdalo();
216 } else {
217 sdahi();
218 }
219
220 if (sclhi() < 0) {
221 sdahi();
222 return -ETIMEDOUT;
223 }
224 scllo();
225 sdahi();
226
227 if (rtc_debug)
228 printk(KERN_DEBUG "i2c_inb() -> 0x%02x\n", indata);
229
230 /* assert: scl is low */
231 return indata & 0xff;
232 }
233
234 static void i2c_init(void)
235 {
236 /* no gpio_control for EXTIF */
237 // ssb_gpio_control(&ssb, sda_mask | scl_mask, 0);
238
239 gpio_set_value(sda_index, 0);
240 gpio_set_value(scl_index, 0);
241 sdahi();
242 sclhi();
243 }
244
245 static int rtc_open(UNUSED struct inode *inode, UNUSED struct file *filp)
246 {
247 spin_lock_irq(&rtc_lock);
248
249 if (rtc_status & RTC_IS_OPEN) {
250 spin_unlock_irq(&rtc_lock);
251 return -EBUSY;
252 }
253
254 rtc_status |= RTC_IS_OPEN;
255
256 /*
257 * The following call is only necessary if we use both this driver and
258 * the proprietary one from asus at the same time (which, b.t.w. only
259 * makes sense during development). Otherwise, each access via the asus
260 * driver will make access via this driver impossible.
261 */
262 i2c_init();
263
264 spin_unlock_irq(&rtc_lock);
265
266 return 0;
267 }
268
269 static int rtc_release(UNUSED struct inode *inode, UNUSED struct file *filp)
270 {
271 /* No need for locking here. */
272 rtc_status &= ~RTC_IS_OPEN;
273 return 0;
274 }
275
276 static int from_bcd(int bcdnum)
277 {
278 int fac, num = 0;
279
280 for (fac = 1; bcdnum; fac *= 10) {
281 num += (bcdnum % 16) * fac;
282 bcdnum /= 16;
283 }
284
285 return num;
286 }
287
288 static int to_bcd(int decnum)
289 {
290 int fac, num = 0;
291
292 for (fac = 1; decnum; fac *= 16) {
293 num += (decnum % 10) * fac;
294 decnum /= 10;
295 }
296
297 return num;
298 }
299
300 static void get_rtc_time(struct rtc_time *rtc_tm)
301 {
302 int cr2;
303
304 /*
305 * Read date and time from the RTC. We use read method (3).
306 */
307
308 spin_lock_irq(&rtc_lock);
309 i2c_start();
310 i2c_outb(RTC_I2C_ADDRESS | I2C_READ_MASK);
311 cr2 = i2c_inb(I2C_ACK);
312 rtc_tm->tm_sec = i2c_inb(I2C_ACK);
313 rtc_tm->tm_min = i2c_inb(I2C_ACK);
314 rtc_tm->tm_hour = i2c_inb(I2C_ACK);
315 rtc_tm->tm_wday = i2c_inb(I2C_ACK);
316 rtc_tm->tm_mday = i2c_inb(I2C_ACK);
317 rtc_tm->tm_mon = i2c_inb(I2C_ACK);
318 rtc_tm->tm_year = i2c_inb(I2C_NAK);
319 i2c_stop();
320 spin_unlock_irq(&rtc_lock);
321
322 if (cr2 & RTC_VDET_MASK) {
323 printk(KERN_WARNING "***RTC BATTERY FAILURE***\n");
324 }
325
326 /* Handle century bit */
327 if (rtc_tm->tm_mon & RTC_Y2K_MASK) {
328 rtc_tm->tm_mon &= ~RTC_Y2K_MASK;
329 rtc_tm->tm_year += 0x100;
330 }
331
332 rtc_tm->tm_sec = from_bcd(rtc_tm->tm_sec);
333 rtc_tm->tm_min = from_bcd(rtc_tm->tm_min);
334 rtc_tm->tm_hour = from_bcd(rtc_tm->tm_hour);
335 rtc_tm->tm_mday = from_bcd(rtc_tm->tm_mday);
336 rtc_tm->tm_mon = from_bcd(rtc_tm->tm_mon) - 1;
337 rtc_tm->tm_year = from_bcd(rtc_tm->tm_year);
338
339 rtc_tm->tm_isdst = -1; /* DST not known */
340 }
341
342 static void set_rtc_time(struct rtc_time *rtc_tm)
343 {
344 rtc_tm->tm_sec = to_bcd(rtc_tm->tm_sec);
345 rtc_tm->tm_min = to_bcd(rtc_tm->tm_min);
346 rtc_tm->tm_hour = to_bcd(rtc_tm->tm_hour);
347 rtc_tm->tm_mday = to_bcd(rtc_tm->tm_mday);
348 rtc_tm->tm_mon = to_bcd(rtc_tm->tm_mon + 1);
349 rtc_tm->tm_year = to_bcd(rtc_tm->tm_year);
350
351 if (rtc_tm->tm_year >= 0x100) {
352 rtc_tm->tm_year -= 0x100;
353 rtc_tm->tm_mon |= RTC_Y2K_MASK;
354 }
355
356 spin_lock_irq(&rtc_lock);
357 i2c_start();
358 i2c_outb(RTC_I2C_ADDRESS | I2C_WRITE_MASK);
359 i2c_outb(0x00); /* set starting register to 0 (=seconds) */
360 i2c_outb(rtc_tm->tm_sec);
361 i2c_outb(rtc_tm->tm_min);
362 i2c_outb(rtc_tm->tm_hour);
363 i2c_outb(rtc_tm->tm_wday);
364 i2c_outb(rtc_tm->tm_mday);
365 i2c_outb(rtc_tm->tm_mon);
366 i2c_outb(rtc_tm->tm_year);
367 i2c_stop();
368 spin_unlock_irq(&rtc_lock);
369 }
370
371 static ssize_t rtc_write(UNUSED struct file *filp, const char *buf,
372 size_t count, loff_t *ppos)
373 {
374 struct rtc_time rtc_tm;
375 char buffer[23];
376 char *p;
377
378 if (!capable(CAP_SYS_TIME))
379 return -EACCES;
380
381 if (ppos != &filp->f_pos)
382 return -ESPIPE;
383
384 /*
385 * For simplicity, the only acceptable format is:
386 * YYYY:MM:DD:W:HH:MM:SS\n
387 */
388
389 if (count != 22)
390 goto err_out;
391
392 if (copy_from_user(buffer, buf, count))
393 return -EFAULT;
394
395 buffer[sizeof(buffer)-1] = '\0';
396
397 p = &buffer[0];
398
399 rtc_tm.tm_year = simple_strtoul(p, &p, 10);
400 if (*p++ != ':') goto err_out;
401
402 rtc_tm.tm_mon = simple_strtoul(p, &p, 10) - 1;
403 if (*p++ != ':') goto err_out;
404
405 rtc_tm.tm_mday = simple_strtoul(p, &p, 10);
406 if (*p++ != ':') goto err_out;
407
408 rtc_tm.tm_wday = simple_strtoul(p, &p, 10);
409 if (*p++ != ':') goto err_out;
410
411 rtc_tm.tm_hour = simple_strtoul(p, &p, 10);
412 if (*p++ != ':') goto err_out;
413
414 rtc_tm.tm_min = simple_strtoul(p, &p, 10);
415 if (*p++ != ':') goto err_out;
416
417 rtc_tm.tm_sec = simple_strtoul(p, &p, 10);
418 if (*p != '\n') goto err_out;
419
420 rtc_tm.tm_year -= RTC_EPOCH;
421
422 set_rtc_time(&rtc_tm);
423
424 *ppos += count;
425
426 return count;
427
428 err_out:
429 printk(KERN_ERR "invalid format: use YYYY:MM:DD:W:HH:MM:SS\\n\n");
430 return -EINVAL;
431 }
432
433
434 static ssize_t rtc_read(UNUSED struct file *filp, char *buf, size_t count,
435 loff_t *ppos)
436 {
437 char wbuf[23];
438 struct rtc_time tm;
439 ssize_t len;
440
441 if (count == 0 || *ppos != 0)
442 return 0;
443
444 get_rtc_time(&tm);
445
446 len = sprintf(wbuf, "%04d:%02d:%02d:%d:%02d:%02d:%02d\n",
447 tm.tm_year + RTC_EPOCH,
448 tm.tm_mon + 1,
449 tm.tm_mday,
450 tm.tm_wday,
451 tm.tm_hour,
452 tm.tm_min,
453 tm.tm_sec);
454
455 if (len > (ssize_t)count)
456 len = count;
457
458 if (copy_to_user(buf, wbuf, len))
459 return -EFAULT;
460
461 *ppos += len;
462
463 return len;
464 }
465
466 static int rtc_do_ioctl(unsigned int cmd, unsigned long arg)
467 {
468 struct rtc_time rtc_tm;
469
470 switch (cmd) {
471 case RTC_RD_TIME:
472 memset(&rtc_tm, 0, sizeof(struct rtc_time));
473 get_rtc_time(&rtc_tm);
474 if (copy_to_user((void *)arg, &rtc_tm, sizeof(rtc_tm)))
475 return -EFAULT;
476 break;
477
478 case RTC_SET_TIME:
479 if (!capable(CAP_SYS_TIME))
480 return -EACCES;
481
482 if (copy_from_user(&rtc_tm, (struct rtc_time *)arg,
483 sizeof(struct rtc_time)))
484 return -EFAULT;
485
486 set_rtc_time(&rtc_tm);
487 break;
488
489 default:
490 return -ENOTTY;
491 }
492
493 return 0;
494 }
495
496 static long rtc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
497 {
498 long ret;
499 ret = rtc_do_ioctl(cmd, arg);
500 return ret;
501 }
502
503 static const struct file_operations rtc_fops = {
504 .owner = THIS_MODULE,
505 .llseek = no_llseek,
506 .read = rtc_read,
507 .write = rtc_write,
508 .unlocked_ioctl = rtc_ioctl,
509 .open = rtc_open,
510 .release = rtc_release,
511 };
512
513 static struct miscdevice rtc_dev = {
514 .minor = RTC_MINOR,
515 .name = "rtc",
516 .fops = &rtc_fops,
517 };
518
519 /* Savagely ripped from diag.c. */
520 static inline int startswith (char *source, char *cmp)
521 {
522 return !strncmp(source, cmp, strlen(cmp));
523 }
524
525 static void platform_detect(void)
526 {
527 char buf[20];
528 int et0phyaddr, et1phyaddr;
529
530 /* Based on "model_no". */
531 if (nvram_getenv("model_no", buf, sizeof(buf)) >= 0) {
532 if (startswith(buf, "WL700")) { /* WL700* */
533 sda_index = 2;
534 scl_index = 5;
535 return;
536 }
537 }
538
539 if (nvram_getenv("et0phyaddr", buf, sizeof(buf)) >= 0 )
540 et0phyaddr = simple_strtoul(buf, NULL, 0);
541 if (nvram_getenv("et1phyaddr", buf, sizeof(buf)) >= 0 )
542 et1phyaddr = simple_strtoul(buf, NULL, 0);
543
544 if (nvram_getenv("hardware_version", buf, sizeof(buf)) >= 0) {
545 /* Either WL-300g or WL-HDD, do more extensive checks */
546 if (startswith(buf, "WL300-") && et0phyaddr == 0 && et1phyaddr == 1) {
547 sda_index = 4;
548 scl_index = 5;
549 return;
550 }
551 }
552 /* not found */
553 }
554
555 static int __init rtc_init(void)
556 {
557 int cr1;
558
559 platform_detect();
560
561 if (sda_index == scl_index) {
562 printk(KERN_ERR "RTC-RV5C386A: unrecognized platform!\n");
563 return -ENODEV;
564 }
565
566 i2c_init();
567
568 /*
569 * Switch RTC to 24h mode
570 */
571 spin_lock_irq(&rtc_lock);
572 i2c_start();
573 i2c_outb(RTC_I2C_ADDRESS | I2C_WRITE_MASK);
574 i2c_outb(0xE4); /* start at address 0xE, transmission mode 4 */
575 cr1 = i2c_inb(I2C_NAK);
576 i2c_stop();
577 spin_unlock_irq(&rtc_lock);
578 if ((cr1 & RTC_24HOUR_MODE_MASK) == 0) {
579 /* RTC is running in 12h mode */
580 printk(KERN_INFO "rtc.o: switching to 24h mode\n");
581 spin_lock_irq(&rtc_lock);
582 i2c_start();
583 i2c_outb(RTC_I2C_ADDRESS | I2C_WRITE_MASK);
584 i2c_outb(0xE0);
585 i2c_outb(cr1 | RTC_24HOUR_MODE_MASK);
586 i2c_stop();
587 spin_unlock_irq(&rtc_lock);
588 }
589
590 misc_register(&rtc_dev);
591
592 printk(KERN_INFO "RV5C386A Real Time Clock Driver loaded\n");
593
594 return 0;
595 }
596
597 static void __exit rtc_exit (void)
598 {
599 misc_deregister(&rtc_dev);
600 printk(KERN_INFO "Successfully removed RTC RV5C386A driver\n");
601 }
602
603 module_init(rtc_init);
604 module_exit(rtc_exit);
605
606 /*
607 * Local Variables:
608 * indent-tabs-mode:t
609 * c-basic-offset:8
610 * End:
611 */