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>
7 #include <linux/compat.h>
10 #include <linux/init.h>
12 #include <linux/ioport.h>
13 #include <linux/interrupt.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/rtc.h>
17 #include <linux/spinlock.h>
18 #include <linux/types.h>
19 #include <linux/uaccess.h>
20 #include <linux/log2.h>
22 #include <asm/div64.h>
24 MODULE_AUTHOR("Yoichi Yuasa <yuasa@linux-mips.org>");
25 MODULE_DESCRIPTION("NEC VR4100 series RTC driver");
26 MODULE_LICENSE("GPL v2");
29 #define ETIMELREG 0x00
30 #define ETIMEMREG 0x02
31 #define ETIMEHREG 0x04
37 #define RTCL1LREG 0x10
38 #define RTCL1HREG 0x12
39 #define RTCL1CNTLREG 0x14
40 #define RTCL1CNTHREG 0x16
41 #define RTCL2LREG 0x18
42 #define RTCL2HREG 0x1a
43 #define RTCL2CNTLREG 0x1c
44 #define RTCL2CNTHREG 0x1e
49 #define TCLKCNTLREG 0x04
50 #define TCLKCNTHREG 0x06
52 #define RTCINTREG 0x1e
53 #define TCLOCK_INT 0x08
54 #define RTCLONG2_INT 0x04
55 #define RTCLONG1_INT 0x02
56 #define ELAPSEDTIME_INT 0x01
58 #define RTC_FREQUENCY 32768
59 #define MAX_PERIODIC_RATE 6553
61 static void __iomem
*rtc1_base
;
62 static void __iomem
*rtc2_base
;
64 #define rtc1_read(offset) readw(rtc1_base + (offset))
65 #define rtc1_write(offset, value) writew((value), rtc1_base + (offset))
67 #define rtc2_read(offset) readw(rtc2_base + (offset))
68 #define rtc2_write(offset, value) writew((value), rtc2_base + (offset))
70 /* 32-bit compat for ioctls that nobody else uses */
71 #define RTC_EPOCH_READ32 _IOR('p', 0x0d, __u32)
73 static unsigned long epoch
= 1970; /* Jan 1 1970 00:00:00 */
75 static DEFINE_SPINLOCK(rtc_lock
);
76 static char rtc_name
[] = "RTC";
77 static unsigned long periodic_count
;
78 static unsigned int alarm_enabled
;
82 static inline time64_t
read_elapsed_second(void)
85 unsigned long first_low
, first_mid
, first_high
;
87 unsigned long second_low
, second_mid
, second_high
;
90 first_low
= rtc1_read(ETIMELREG
);
91 first_mid
= rtc1_read(ETIMEMREG
);
92 first_high
= rtc1_read(ETIMEHREG
);
93 second_low
= rtc1_read(ETIMELREG
);
94 second_mid
= rtc1_read(ETIMEMREG
);
95 second_high
= rtc1_read(ETIMEHREG
);
96 } while (first_low
!= second_low
|| first_mid
!= second_mid
||
97 first_high
!= second_high
);
99 return ((u64
)first_high
<< 17) | (first_mid
<< 1) | (first_low
>> 15);
102 static inline void write_elapsed_second(time64_t sec
)
104 spin_lock_irq(&rtc_lock
);
106 rtc1_write(ETIMELREG
, (uint16_t)(sec
<< 15));
107 rtc1_write(ETIMEMREG
, (uint16_t)(sec
>> 1));
108 rtc1_write(ETIMEHREG
, (uint16_t)(sec
>> 17));
110 spin_unlock_irq(&rtc_lock
);
113 static int vr41xx_rtc_read_time(struct device
*dev
, struct rtc_time
*time
)
115 time64_t epoch_sec
, elapsed_sec
;
117 epoch_sec
= mktime64(epoch
, 1, 1, 0, 0, 0);
118 elapsed_sec
= read_elapsed_second();
120 rtc_time64_to_tm(epoch_sec
+ elapsed_sec
, time
);
125 static int vr41xx_rtc_set_time(struct device
*dev
, struct rtc_time
*time
)
127 time64_t epoch_sec
, current_sec
;
129 epoch_sec
= mktime64(epoch
, 1, 1, 0, 0, 0);
130 current_sec
= rtc_tm_to_time64(time
);
132 write_elapsed_second(current_sec
- epoch_sec
);
137 static int vr41xx_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*wkalrm
)
139 unsigned long low
, mid
, high
;
140 struct rtc_time
*time
= &wkalrm
->time
;
142 spin_lock_irq(&rtc_lock
);
144 low
= rtc1_read(ECMPLREG
);
145 mid
= rtc1_read(ECMPMREG
);
146 high
= rtc1_read(ECMPHREG
);
147 wkalrm
->enabled
= alarm_enabled
;
149 spin_unlock_irq(&rtc_lock
);
151 rtc_time64_to_tm((high
<< 17) | (mid
<< 1) | (low
>> 15), time
);
156 static int vr41xx_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*wkalrm
)
160 alarm_sec
= rtc_tm_to_time64(&wkalrm
->time
);
162 spin_lock_irq(&rtc_lock
);
165 disable_irq(aie_irq
);
167 rtc1_write(ECMPLREG
, (uint16_t)(alarm_sec
<< 15));
168 rtc1_write(ECMPMREG
, (uint16_t)(alarm_sec
>> 1));
169 rtc1_write(ECMPHREG
, (uint16_t)(alarm_sec
>> 17));
174 alarm_enabled
= wkalrm
->enabled
;
176 spin_unlock_irq(&rtc_lock
);
181 static int vr41xx_rtc_ioctl(struct device
*dev
, unsigned int cmd
, unsigned long arg
)
185 return put_user(epoch
, (unsigned long __user
*)arg
);
187 case RTC_EPOCH_READ32
:
188 return put_user(epoch
, (unsigned int __user
*)arg
);
191 /* Doesn't support before 1900 */
203 static int vr41xx_rtc_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
205 spin_lock_irq(&rtc_lock
);
207 if (!alarm_enabled
) {
213 disable_irq(aie_irq
);
217 spin_unlock_irq(&rtc_lock
);
221 static irqreturn_t
elapsedtime_interrupt(int irq
, void *dev_id
)
223 struct platform_device
*pdev
= (struct platform_device
*)dev_id
;
224 struct rtc_device
*rtc
= platform_get_drvdata(pdev
);
226 rtc2_write(RTCINTREG
, ELAPSEDTIME_INT
);
228 rtc_update_irq(rtc
, 1, RTC_AF
);
233 static irqreturn_t
rtclong1_interrupt(int irq
, void *dev_id
)
235 struct platform_device
*pdev
= (struct platform_device
*)dev_id
;
236 struct rtc_device
*rtc
= platform_get_drvdata(pdev
);
237 unsigned long count
= periodic_count
;
239 rtc2_write(RTCINTREG
, RTCLONG1_INT
);
241 rtc1_write(RTCL1LREG
, count
);
242 rtc1_write(RTCL1HREG
, count
>> 16);
244 rtc_update_irq(rtc
, 1, RTC_PF
);
249 static const struct rtc_class_ops vr41xx_rtc_ops
= {
250 .ioctl
= vr41xx_rtc_ioctl
,
251 .read_time
= vr41xx_rtc_read_time
,
252 .set_time
= vr41xx_rtc_set_time
,
253 .read_alarm
= vr41xx_rtc_read_alarm
,
254 .set_alarm
= vr41xx_rtc_set_alarm
,
255 .alarm_irq_enable
= vr41xx_rtc_alarm_irq_enable
,
258 static int rtc_probe(struct platform_device
*pdev
)
260 struct resource
*res
;
261 struct rtc_device
*rtc
;
264 if (pdev
->num_resources
!= 4)
267 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
271 rtc1_base
= devm_ioremap(&pdev
->dev
, res
->start
, resource_size(res
));
275 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
278 goto err_rtc1_iounmap
;
281 rtc2_base
= devm_ioremap(&pdev
->dev
, res
->start
, resource_size(res
));
284 goto err_rtc1_iounmap
;
287 rtc
= devm_rtc_allocate_device(&pdev
->dev
);
289 retval
= PTR_ERR(rtc
);
290 goto err_iounmap_all
;
293 rtc
->ops
= &vr41xx_rtc_ops
;
295 /* 48-bit counter at 32.768 kHz */
296 rtc
->range_max
= (1ULL << 33) - 1;
297 rtc
->max_user_freq
= MAX_PERIODIC_RATE
;
299 spin_lock_irq(&rtc_lock
);
301 rtc1_write(ECMPLREG
, 0);
302 rtc1_write(ECMPMREG
, 0);
303 rtc1_write(ECMPHREG
, 0);
304 rtc1_write(RTCL1LREG
, 0);
305 rtc1_write(RTCL1HREG
, 0);
307 spin_unlock_irq(&rtc_lock
);
309 aie_irq
= platform_get_irq(pdev
, 0);
312 goto err_iounmap_all
;
315 retval
= devm_request_irq(&pdev
->dev
, aie_irq
, elapsedtime_interrupt
, 0,
316 "elapsed_time", pdev
);
318 goto err_iounmap_all
;
320 pie_irq
= platform_get_irq(pdev
, 1);
323 goto err_iounmap_all
;
326 retval
= devm_request_irq(&pdev
->dev
, pie_irq
, rtclong1_interrupt
, 0,
329 goto err_iounmap_all
;
331 platform_set_drvdata(pdev
, rtc
);
333 disable_irq(aie_irq
);
334 disable_irq(pie_irq
);
336 dev_info(&pdev
->dev
, "Real Time Clock of NEC VR4100 series\n");
338 retval
= rtc_register_device(rtc
);
340 goto err_iounmap_all
;
353 /* work with hotplug and coldplug */
354 MODULE_ALIAS("platform:RTC");
356 static struct platform_driver rtc_platform_driver
= {
363 module_platform_driver(rtc_platform_driver
);