1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for NEC VR4100 series Real Time Clock unit.
5 * Copyright (C) 2003-2008 Yoichi Yuasa <yuasa@linux-mips.org>
9 #include <linux/init.h>
11 #include <linux/ioport.h>
12 #include <linux/interrupt.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/rtc.h>
16 #include <linux/spinlock.h>
17 #include <linux/types.h>
18 #include <linux/uaccess.h>
19 #include <linux/log2.h>
21 #include <asm/div64.h>
23 MODULE_AUTHOR("Yoichi Yuasa <yuasa@linux-mips.org>");
24 MODULE_DESCRIPTION("NEC VR4100 series RTC driver");
25 MODULE_LICENSE("GPL v2");
28 #define ETIMELREG 0x00
29 #define ETIMEMREG 0x02
30 #define ETIMEHREG 0x04
36 #define RTCL1LREG 0x10
37 #define RTCL1HREG 0x12
38 #define RTCL1CNTLREG 0x14
39 #define RTCL1CNTHREG 0x16
40 #define RTCL2LREG 0x18
41 #define RTCL2HREG 0x1a
42 #define RTCL2CNTLREG 0x1c
43 #define RTCL2CNTHREG 0x1e
48 #define TCLKCNTLREG 0x04
49 #define TCLKCNTHREG 0x06
51 #define RTCINTREG 0x1e
52 #define TCLOCK_INT 0x08
53 #define RTCLONG2_INT 0x04
54 #define RTCLONG1_INT 0x02
55 #define ELAPSEDTIME_INT 0x01
57 #define RTC_FREQUENCY 32768
58 #define MAX_PERIODIC_RATE 6553
60 static void __iomem
*rtc1_base
;
61 static void __iomem
*rtc2_base
;
63 #define rtc1_read(offset) readw(rtc1_base + (offset))
64 #define rtc1_write(offset, value) writew((value), rtc1_base + (offset))
66 #define rtc2_read(offset) readw(rtc2_base + (offset))
67 #define rtc2_write(offset, value) writew((value), rtc2_base + (offset))
69 static unsigned long epoch
= 1970; /* Jan 1 1970 00:00:00 */
71 static DEFINE_SPINLOCK(rtc_lock
);
72 static char rtc_name
[] = "RTC";
73 static unsigned long periodic_count
;
74 static unsigned int alarm_enabled
;
78 static inline time64_t
read_elapsed_second(void)
81 unsigned long first_low
, first_mid
, first_high
;
83 unsigned long second_low
, second_mid
, second_high
;
86 first_low
= rtc1_read(ETIMELREG
);
87 first_mid
= rtc1_read(ETIMEMREG
);
88 first_high
= rtc1_read(ETIMEHREG
);
89 second_low
= rtc1_read(ETIMELREG
);
90 second_mid
= rtc1_read(ETIMEMREG
);
91 second_high
= rtc1_read(ETIMEHREG
);
92 } while (first_low
!= second_low
|| first_mid
!= second_mid
||
93 first_high
!= second_high
);
95 return ((u64
)first_high
<< 17) | (first_mid
<< 1) | (first_low
>> 15);
98 static inline void write_elapsed_second(time64_t sec
)
100 spin_lock_irq(&rtc_lock
);
102 rtc1_write(ETIMELREG
, (uint16_t)(sec
<< 15));
103 rtc1_write(ETIMEMREG
, (uint16_t)(sec
>> 1));
104 rtc1_write(ETIMEHREG
, (uint16_t)(sec
>> 17));
106 spin_unlock_irq(&rtc_lock
);
109 static int vr41xx_rtc_read_time(struct device
*dev
, struct rtc_time
*time
)
111 time64_t epoch_sec
, elapsed_sec
;
113 epoch_sec
= mktime64(epoch
, 1, 1, 0, 0, 0);
114 elapsed_sec
= read_elapsed_second();
116 rtc_time64_to_tm(epoch_sec
+ elapsed_sec
, time
);
121 static int vr41xx_rtc_set_time(struct device
*dev
, struct rtc_time
*time
)
123 time64_t epoch_sec
, current_sec
;
125 epoch_sec
= mktime64(epoch
, 1, 1, 0, 0, 0);
126 current_sec
= rtc_tm_to_time64(time
);
128 write_elapsed_second(current_sec
- epoch_sec
);
133 static int vr41xx_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*wkalrm
)
135 unsigned long low
, mid
, high
;
136 struct rtc_time
*time
= &wkalrm
->time
;
138 spin_lock_irq(&rtc_lock
);
140 low
= rtc1_read(ECMPLREG
);
141 mid
= rtc1_read(ECMPMREG
);
142 high
= rtc1_read(ECMPHREG
);
143 wkalrm
->enabled
= alarm_enabled
;
145 spin_unlock_irq(&rtc_lock
);
147 rtc_time64_to_tm((high
<< 17) | (mid
<< 1) | (low
>> 15), time
);
152 static int vr41xx_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*wkalrm
)
156 alarm_sec
= rtc_tm_to_time64(&wkalrm
->time
);
158 spin_lock_irq(&rtc_lock
);
161 disable_irq(aie_irq
);
163 rtc1_write(ECMPLREG
, (uint16_t)(alarm_sec
<< 15));
164 rtc1_write(ECMPMREG
, (uint16_t)(alarm_sec
>> 1));
165 rtc1_write(ECMPHREG
, (uint16_t)(alarm_sec
>> 17));
170 alarm_enabled
= wkalrm
->enabled
;
172 spin_unlock_irq(&rtc_lock
);
177 static int vr41xx_rtc_ioctl(struct device
*dev
, unsigned int cmd
, unsigned long arg
)
181 return put_user(epoch
, (unsigned long __user
*)arg
);
183 /* Doesn't support before 1900 */
195 static int vr41xx_rtc_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
197 spin_lock_irq(&rtc_lock
);
199 if (!alarm_enabled
) {
205 disable_irq(aie_irq
);
209 spin_unlock_irq(&rtc_lock
);
213 static irqreturn_t
elapsedtime_interrupt(int irq
, void *dev_id
)
215 struct platform_device
*pdev
= (struct platform_device
*)dev_id
;
216 struct rtc_device
*rtc
= platform_get_drvdata(pdev
);
218 rtc2_write(RTCINTREG
, ELAPSEDTIME_INT
);
220 rtc_update_irq(rtc
, 1, RTC_AF
);
225 static irqreturn_t
rtclong1_interrupt(int irq
, void *dev_id
)
227 struct platform_device
*pdev
= (struct platform_device
*)dev_id
;
228 struct rtc_device
*rtc
= platform_get_drvdata(pdev
);
229 unsigned long count
= periodic_count
;
231 rtc2_write(RTCINTREG
, RTCLONG1_INT
);
233 rtc1_write(RTCL1LREG
, count
);
234 rtc1_write(RTCL1HREG
, count
>> 16);
236 rtc_update_irq(rtc
, 1, RTC_PF
);
241 static const struct rtc_class_ops vr41xx_rtc_ops
= {
242 .ioctl
= vr41xx_rtc_ioctl
,
243 .read_time
= vr41xx_rtc_read_time
,
244 .set_time
= vr41xx_rtc_set_time
,
245 .read_alarm
= vr41xx_rtc_read_alarm
,
246 .set_alarm
= vr41xx_rtc_set_alarm
,
247 .alarm_irq_enable
= vr41xx_rtc_alarm_irq_enable
,
250 static int rtc_probe(struct platform_device
*pdev
)
252 struct resource
*res
;
253 struct rtc_device
*rtc
;
256 if (pdev
->num_resources
!= 4)
259 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
263 rtc1_base
= devm_ioremap(&pdev
->dev
, res
->start
, resource_size(res
));
267 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
270 goto err_rtc1_iounmap
;
273 rtc2_base
= devm_ioremap(&pdev
->dev
, res
->start
, resource_size(res
));
276 goto err_rtc1_iounmap
;
279 rtc
= devm_rtc_allocate_device(&pdev
->dev
);
281 retval
= PTR_ERR(rtc
);
282 goto err_iounmap_all
;
285 rtc
->ops
= &vr41xx_rtc_ops
;
287 /* 48-bit counter at 32.768 kHz */
288 rtc
->range_max
= (1ULL << 33) - 1;
289 rtc
->max_user_freq
= MAX_PERIODIC_RATE
;
291 spin_lock_irq(&rtc_lock
);
293 rtc1_write(ECMPLREG
, 0);
294 rtc1_write(ECMPMREG
, 0);
295 rtc1_write(ECMPHREG
, 0);
296 rtc1_write(RTCL1LREG
, 0);
297 rtc1_write(RTCL1HREG
, 0);
299 spin_unlock_irq(&rtc_lock
);
301 aie_irq
= platform_get_irq(pdev
, 0);
304 goto err_iounmap_all
;
307 retval
= devm_request_irq(&pdev
->dev
, aie_irq
, elapsedtime_interrupt
, 0,
308 "elapsed_time", pdev
);
310 goto err_iounmap_all
;
312 pie_irq
= platform_get_irq(pdev
, 1);
315 goto err_iounmap_all
;
318 retval
= devm_request_irq(&pdev
->dev
, pie_irq
, rtclong1_interrupt
, 0,
321 goto err_iounmap_all
;
323 platform_set_drvdata(pdev
, rtc
);
325 disable_irq(aie_irq
);
326 disable_irq(pie_irq
);
328 dev_info(&pdev
->dev
, "Real Time Clock of NEC VR4100 series\n");
330 retval
= rtc_register_device(rtc
);
332 goto err_iounmap_all
;
345 /* work with hotplug and coldplug */
346 MODULE_ALIAS("platform:RTC");
348 static struct platform_driver rtc_platform_driver
= {
355 module_platform_driver(rtc_platform_driver
);