25 #include <linux/module.h>
27 #include <linux/rtc.h>
29 #include <linux/types.h>
32 #include <asm/div64.h>
34 #include <asm/uaccess.h>
41 #define ETIMELREG 0x00
42 #define ETIMEMREG 0x02
43 #define ETIMEHREG 0x04
49 #define RTCL1LREG 0x10
50 #define RTCL1HREG 0x12
51 #define RTCL1CNTLREG 0x14
52 #define RTCL1CNTHREG 0x16
53 #define RTCL2LREG 0x18
54 #define RTCL2HREG 0x1a
55 #define RTCL2CNTLREG 0x1c
56 #define RTCL2CNTHREG 0x1e
61 #define TCLKCNTLREG 0x04
62 #define TCLKCNTHREG 0x06
64 #define RTCINTREG 0x1e
65 #define TCLOCK_INT 0x08
66 #define RTCLONG2_INT 0x04
67 #define RTCLONG1_INT 0x02
68 #define ELAPSEDTIME_INT 0x01
70 #define RTC_FREQUENCY 32768
71 #define MAX_PERIODIC_RATE 6553
76 #define rtc1_read(offset) readw(rtc1_base + (offset))
77 #define rtc1_write(offset, value) writew((value), rtc1_base + (offset))
79 #define rtc2_read(offset) readw(rtc2_base + (offset))
80 #define rtc2_write(offset, value) writew((value), rtc2_base + (offset))
82 static unsigned long epoch = 1970;
85 static char rtc_name[] =
"RTC";
86 static unsigned long periodic_count;
87 static unsigned int alarm_enabled;
91 static inline unsigned long read_elapsed_second(
void)
94 unsigned long first_low, first_mid, first_high;
96 unsigned long second_low, second_mid, second_high;
105 }
while (first_low != second_low || first_mid != second_mid ||
106 first_high != second_high);
108 return (first_high << 17) | (first_mid << 1) | (first_low >> 15);
111 static inline void write_elapsed_second(
unsigned long sec)
113 spin_lock_irq(&rtc_lock);
119 spin_unlock_irq(&rtc_lock);
122 static void vr41xx_rtc_release(
struct device *
dev)
125 spin_lock_irq(&rtc_lock);
133 spin_unlock_irq(&rtc_lock);
141 unsigned long epoch_sec, elapsed_sec;
144 elapsed_sec = read_elapsed_second();
153 unsigned long epoch_sec, current_sec;
159 write_elapsed_second(current_sec - epoch_sec);
169 spin_lock_irq(&rtc_lock);
174 wkalrm->
enabled = alarm_enabled;
176 spin_unlock_irq(&rtc_lock);
183 static int vr41xx_rtc_set_alarm(
struct device *dev,
struct rtc_wkalrm *wkalrm)
185 unsigned long alarm_sec;
191 spin_lock_irq(&rtc_lock);
203 alarm_enabled = wkalrm->
enabled;
205 spin_unlock_irq(&rtc_lock);
210 static int vr41xx_rtc_ioctl(
struct device *dev,
unsigned int cmd,
unsigned long arg)
228 static int vr41xx_rtc_alarm_irq_enable(
struct device *dev,
unsigned int enabled)
230 spin_lock_irq(&rtc_lock);
232 if (!alarm_enabled) {
242 spin_unlock_irq(&rtc_lock);
262 unsigned long count = periodic_count;
275 .release = vr41xx_rtc_release,
276 .ioctl = vr41xx_rtc_ioctl,
277 .read_time = vr41xx_rtc_read_time,
278 .set_time = vr41xx_rtc_set_time,
279 .read_alarm = vr41xx_rtc_read_alarm,
280 .set_alarm = vr41xx_rtc_set_alarm,
303 goto err_rtc1_iounmap;
309 goto err_rtc1_iounmap;
314 retval = PTR_ERR(rtc);
315 goto err_iounmap_all;
320 spin_lock_irq(&rtc_lock);
328 spin_unlock_irq(&rtc_lock);
333 goto err_device_unregister;
336 retval =
request_irq(aie_irq, elapsedtime_interrupt, 0,
337 "elapsed_time", pdev);
339 goto err_device_unregister;
345 retval =
request_irq(pie_irq, rtclong1_interrupt, 0,
350 platform_set_drvdata(pdev, rtc);
362 err_device_unregister:
380 rtc = platform_get_drvdata(pdev);
384 platform_set_drvdata(pdev,
NULL);