blob: 2f1212a25f7e9b0ab3d64dc65db669035d137767 [file] [log] [blame]
Yoichi Yuasa8417eb72006-04-10 22:54:47 -07001/*
2 * Driver for NEC VR4100 series Real Time Clock unit.
3 *
Yoichi Yuasaada8e952009-07-03 00:39:38 +09004 * Copyright (C) 2003-2008 Yoichi Yuasa <yuasa@linux-mips.org>
Yoichi Yuasa8417eb72006-04-10 22:54:47 -07005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
Yoichi Yuasabd0765092007-05-11 21:18:48 +090020#include <linux/err.h>
Yoichi Yuasa8417eb72006-04-10 22:54:47 -070021#include <linux/fs.h>
22#include <linux/init.h>
Sachin Kamat4271e7f2013-11-12 15:10:34 -080023#include <linux/io.h>
Yoichi Yuasa8417eb72006-04-10 22:54:47 -070024#include <linux/ioport.h>
Yoichi Yuasabd0765092007-05-11 21:18:48 +090025#include <linux/interrupt.h>
Yoichi Yuasa8417eb72006-04-10 22:54:47 -070026#include <linux/module.h>
27#include <linux/platform_device.h>
28#include <linux/rtc.h>
29#include <linux/spinlock.h>
30#include <linux/types.h>
Sachin Kamat4271e7f2013-11-12 15:10:34 -080031#include <linux/uaccess.h>
Jonathan Cameron5d2a5032009-01-06 14:42:12 -080032#include <linux/log2.h>
Yoichi Yuasa8417eb72006-04-10 22:54:47 -070033
34#include <asm/div64.h>
Yoichi Yuasa8417eb72006-04-10 22:54:47 -070035
Yoichi Yuasaada8e952009-07-03 00:39:38 +090036MODULE_AUTHOR("Yoichi Yuasa <yuasa@linux-mips.org>");
Yoichi Yuasa8417eb72006-04-10 22:54:47 -070037MODULE_DESCRIPTION("NEC VR4100 series RTC driver");
Yoichi Yuasa4cad4432008-07-23 21:30:48 -070038MODULE_LICENSE("GPL v2");
Yoichi Yuasa8417eb72006-04-10 22:54:47 -070039
Yoichi Yuasa8417eb72006-04-10 22:54:47 -070040/* RTC 1 registers */
41#define ETIMELREG 0x00
42#define ETIMEMREG 0x02
43#define ETIMEHREG 0x04
44/* RFU */
45#define ECMPLREG 0x08
46#define ECMPMREG 0x0a
47#define ECMPHREG 0x0c
48/* RFU */
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
57
58/* RTC 2 registers */
59#define TCLKLREG 0x00
60#define TCLKHREG 0x02
61#define TCLKCNTLREG 0x04
62#define TCLKCNTHREG 0x06
63/* RFU */
64#define RTCINTREG 0x1e
65 #define TCLOCK_INT 0x08
66 #define RTCLONG2_INT 0x04
67 #define RTCLONG1_INT 0x02
68 #define ELAPSEDTIME_INT 0x01
69
70#define RTC_FREQUENCY 32768
71#define MAX_PERIODIC_RATE 6553
Yoichi Yuasa8417eb72006-04-10 22:54:47 -070072
73static void __iomem *rtc1_base;
74static void __iomem *rtc2_base;
75
76#define rtc1_read(offset) readw(rtc1_base + (offset))
77#define rtc1_write(offset, value) writew((value), rtc1_base + (offset))
78
79#define rtc2_read(offset) readw(rtc2_base + (offset))
80#define rtc2_write(offset, value) writew((value), rtc2_base + (offset))
81
82static unsigned long epoch = 1970; /* Jan 1 1970 00:00:00 */
83
Ingo Molnar34af9462006-06-27 02:53:55 -070084static DEFINE_SPINLOCK(rtc_lock);
Yoichi Yuasa8417eb72006-04-10 22:54:47 -070085static char rtc_name[] = "RTC";
Yoichi Yuasa8417eb72006-04-10 22:54:47 -070086static unsigned long periodic_count;
Yoichi Yuasa9b5ef642007-05-08 00:33:50 -070087static unsigned int alarm_enabled;
Anton Vorontsov2fac6672009-01-06 14:42:11 -080088static int aie_irq;
89static int pie_irq;
Yoichi Yuasa8417eb72006-04-10 22:54:47 -070090
Arnd Bergmann0d1c6552018-04-20 18:14:24 +020091static inline time64_t read_elapsed_second(void)
Yoichi Yuasa8417eb72006-04-10 22:54:47 -070092{
93
94 unsigned long first_low, first_mid, first_high;
95
96 unsigned long second_low, second_mid, second_high;
97
98 do {
99 first_low = rtc1_read(ETIMELREG);
100 first_mid = rtc1_read(ETIMEMREG);
101 first_high = rtc1_read(ETIMEHREG);
102 second_low = rtc1_read(ETIMELREG);
103 second_mid = rtc1_read(ETIMEMREG);
104 second_high = rtc1_read(ETIMEHREG);
105 } while (first_low != second_low || first_mid != second_mid ||
Sachin Kamat0218bcf2013-07-03 15:06:06 -0700106 first_high != second_high);
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700107
Arnd Bergmann0d1c6552018-04-20 18:14:24 +0200108 return ((u64)first_high << 17) | (first_mid << 1) | (first_low >> 15);
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700109}
110
Arnd Bergmann0d1c6552018-04-20 18:14:24 +0200111static inline void write_elapsed_second(time64_t sec)
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700112{
113 spin_lock_irq(&rtc_lock);
114
115 rtc1_write(ETIMELREG, (uint16_t)(sec << 15));
116 rtc1_write(ETIMEMREG, (uint16_t)(sec >> 1));
117 rtc1_write(ETIMEHREG, (uint16_t)(sec >> 17));
118
119 spin_unlock_irq(&rtc_lock);
120}
121
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700122static int vr41xx_rtc_read_time(struct device *dev, struct rtc_time *time)
123{
Arnd Bergmann0d1c6552018-04-20 18:14:24 +0200124 time64_t epoch_sec, elapsed_sec;
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700125
Arnd Bergmann0d1c6552018-04-20 18:14:24 +0200126 epoch_sec = mktime64(epoch, 1, 1, 0, 0, 0);
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700127 elapsed_sec = read_elapsed_second();
128
Arnd Bergmann0d1c6552018-04-20 18:14:24 +0200129 rtc_time64_to_tm(epoch_sec + elapsed_sec, time);
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700130
131 return 0;
132}
133
134static int vr41xx_rtc_set_time(struct device *dev, struct rtc_time *time)
135{
Arnd Bergmann0d1c6552018-04-20 18:14:24 +0200136 time64_t epoch_sec, current_sec;
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700137
Arnd Bergmann0d1c6552018-04-20 18:14:24 +0200138 epoch_sec = mktime64(epoch, 1, 1, 0, 0, 0);
139 current_sec = mktime64(time->tm_year + 1900, time->tm_mon + 1, time->tm_mday,
Sachin Kamat0218bcf2013-07-03 15:06:06 -0700140 time->tm_hour, time->tm_min, time->tm_sec);
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700141
142 write_elapsed_second(current_sec - epoch_sec);
143
144 return 0;
145}
146
147static int vr41xx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
148{
149 unsigned long low, mid, high;
150 struct rtc_time *time = &wkalrm->time;
151
152 spin_lock_irq(&rtc_lock);
153
154 low = rtc1_read(ECMPLREG);
155 mid = rtc1_read(ECMPMREG);
156 high = rtc1_read(ECMPHREG);
Yoichi Yuasa9b5ef642007-05-08 00:33:50 -0700157 wkalrm->enabled = alarm_enabled;
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700158
159 spin_unlock_irq(&rtc_lock);
160
161 rtc_time_to_tm((high << 17) | (mid << 1) | (low >> 15), time);
162
163 return 0;
164}
165
166static int vr41xx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
167{
Arnd Bergmann0d1c6552018-04-20 18:14:24 +0200168 time64_t alarm_sec;
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700169 struct rtc_time *time = &wkalrm->time;
170
Arnd Bergmann0d1c6552018-04-20 18:14:24 +0200171 alarm_sec = mktime64(time->tm_year + 1900, time->tm_mon + 1, time->tm_mday,
172 time->tm_hour, time->tm_min, time->tm_sec);
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700173
174 spin_lock_irq(&rtc_lock);
175
Yoichi Yuasa9b5ef642007-05-08 00:33:50 -0700176 if (alarm_enabled)
Yoichi Yuasabd0765092007-05-11 21:18:48 +0900177 disable_irq(aie_irq);
Yoichi Yuasa9b5ef642007-05-08 00:33:50 -0700178
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700179 rtc1_write(ECMPLREG, (uint16_t)(alarm_sec << 15));
180 rtc1_write(ECMPMREG, (uint16_t)(alarm_sec >> 1));
181 rtc1_write(ECMPHREG, (uint16_t)(alarm_sec >> 17));
182
Yoichi Yuasa9b5ef642007-05-08 00:33:50 -0700183 if (wkalrm->enabled)
Yoichi Yuasabd0765092007-05-11 21:18:48 +0900184 enable_irq(aie_irq);
Yoichi Yuasa9b5ef642007-05-08 00:33:50 -0700185
186 alarm_enabled = wkalrm->enabled;
187
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700188 spin_unlock_irq(&rtc_lock);
189
190 return 0;
191}
192
Yoichi Yuasa4cad4432008-07-23 21:30:48 -0700193static int vr41xx_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
194{
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700195 switch (cmd) {
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700196 case RTC_EPOCH_READ:
197 return put_user(epoch, (unsigned long __user *)arg);
198 case RTC_EPOCH_SET:
199 /* Doesn't support before 1900 */
200 if (arg < 1900)
201 return -EINVAL;
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700202 epoch = arg;
203 break;
204 default:
Alessandro Zummob3969e52006-05-20 15:00:29 -0700205 return -ENOIOCTLCMD;
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700206 }
207
208 return 0;
209}
210
John Stultz16380c12011-02-02 17:02:41 -0800211static int vr41xx_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
212{
213 spin_lock_irq(&rtc_lock);
214 if (enabled) {
215 if (!alarm_enabled) {
216 enable_irq(aie_irq);
217 alarm_enabled = 1;
218 }
219 } else {
220 if (alarm_enabled) {
221 disable_irq(aie_irq);
222 alarm_enabled = 0;
223 }
224 }
225 spin_unlock_irq(&rtc_lock);
226 return 0;
227}
228
David Howells7d12e782006-10-05 14:55:46 +0100229static irqreturn_t elapsedtime_interrupt(int irq, void *dev_id)
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700230{
231 struct platform_device *pdev = (struct platform_device *)dev_id;
232 struct rtc_device *rtc = platform_get_drvdata(pdev);
233
234 rtc2_write(RTCINTREG, ELAPSEDTIME_INT);
235
David Brownellab6a2d72007-05-08 00:33:30 -0700236 rtc_update_irq(rtc, 1, RTC_AF);
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700237
238 return IRQ_HANDLED;
239}
240
David Howells7d12e782006-10-05 14:55:46 +0100241static irqreturn_t rtclong1_interrupt(int irq, void *dev_id)
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700242{
243 struct platform_device *pdev = (struct platform_device *)dev_id;
244 struct rtc_device *rtc = platform_get_drvdata(pdev);
245 unsigned long count = periodic_count;
246
247 rtc2_write(RTCINTREG, RTCLONG1_INT);
248
249 rtc1_write(RTCL1LREG, count);
250 rtc1_write(RTCL1HREG, count >> 16);
251
David Brownellab6a2d72007-05-08 00:33:30 -0700252 rtc_update_irq(rtc, 1, RTC_PF);
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700253
254 return IRQ_HANDLED;
255}
256
David Brownellff8371a2006-09-30 23:28:17 -0700257static const struct rtc_class_ops vr41xx_rtc_ops = {
Geert Uytterhoevena25f4a92016-03-01 09:50:01 +0100258 .ioctl = vr41xx_rtc_ioctl,
259 .read_time = vr41xx_rtc_read_time,
260 .set_time = vr41xx_rtc_set_time,
261 .read_alarm = vr41xx_rtc_read_alarm,
262 .set_alarm = vr41xx_rtc_set_alarm,
263 .alarm_irq_enable = vr41xx_rtc_alarm_irq_enable,
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700264};
265
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800266static int rtc_probe(struct platform_device *pdev)
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700267{
Yoichi Yuasabd0765092007-05-11 21:18:48 +0900268 struct resource *res;
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700269 struct rtc_device *rtc;
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700270 int retval;
271
Yoichi Yuasabd0765092007-05-11 21:18:48 +0900272 if (pdev->num_resources != 4)
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700273 return -EBUSY;
274
Yoichi Yuasabd0765092007-05-11 21:18:48 +0900275 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
276 if (!res)
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700277 return -EBUSY;
278
Jingoo Hanbf6ce1a2014-01-23 15:55:07 -0800279 rtc1_base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
Yoichi Yuasabd0765092007-05-11 21:18:48 +0900280 if (!rtc1_base)
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700281 return -EBUSY;
Yoichi Yuasabd0765092007-05-11 21:18:48 +0900282
283 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
284 if (!res) {
285 retval = -EBUSY;
286 goto err_rtc1_iounmap;
287 }
288
Jingoo Hanbf6ce1a2014-01-23 15:55:07 -0800289 rtc2_base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
Yoichi Yuasabd0765092007-05-11 21:18:48 +0900290 if (!rtc2_base) {
291 retval = -EBUSY;
292 goto err_rtc1_iounmap;
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700293 }
294
Alexandre Belloni9a992472018-05-17 22:47:05 +0200295 rtc = devm_rtc_allocate_device(&pdev->dev);
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700296 if (IS_ERR(rtc)) {
Yoichi Yuasabd0765092007-05-11 21:18:48 +0900297 retval = PTR_ERR(rtc);
298 goto err_iounmap_all;
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700299 }
300
Alexandre Belloni9a992472018-05-17 22:47:05 +0200301 rtc->ops = &vr41xx_rtc_ops;
302
Yoichi Yuasa4cad4432008-07-23 21:30:48 -0700303 rtc->max_user_freq = MAX_PERIODIC_RATE;
304
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700305 spin_lock_irq(&rtc_lock);
306
307 rtc1_write(ECMPLREG, 0);
308 rtc1_write(ECMPMREG, 0);
309 rtc1_write(ECMPHREG, 0);
310 rtc1_write(RTCL1LREG, 0);
311 rtc1_write(RTCL1HREG, 0);
312
313 spin_unlock_irq(&rtc_lock);
314
Yoichi Yuasabd0765092007-05-11 21:18:48 +0900315 aie_irq = platform_get_irq(pdev, 0);
Anton Vorontsov2fac6672009-01-06 14:42:11 -0800316 if (aie_irq <= 0) {
Yoichi Yuasabd0765092007-05-11 21:18:48 +0900317 retval = -EBUSY;
Jingoo Hanbf6ce1a2014-01-23 15:55:07 -0800318 goto err_iounmap_all;
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700319 }
320
Jingoo Hanbf6ce1a2014-01-23 15:55:07 -0800321 retval = devm_request_irq(&pdev->dev, aie_irq, elapsedtime_interrupt, 0,
322 "elapsed_time", pdev);
Yoichi Yuasabd0765092007-05-11 21:18:48 +0900323 if (retval < 0)
Jingoo Hanbf6ce1a2014-01-23 15:55:07 -0800324 goto err_iounmap_all;
Yoichi Yuasabd0765092007-05-11 21:18:48 +0900325
326 pie_irq = platform_get_irq(pdev, 1);
Wei Yongjun812f1472013-07-03 15:07:04 -0700327 if (pie_irq <= 0) {
328 retval = -EBUSY;
Jingoo Hanbf6ce1a2014-01-23 15:55:07 -0800329 goto err_iounmap_all;
Wei Yongjun812f1472013-07-03 15:07:04 -0700330 }
Yoichi Yuasabd0765092007-05-11 21:18:48 +0900331
Jingoo Hanbf6ce1a2014-01-23 15:55:07 -0800332 retval = devm_request_irq(&pdev->dev, pie_irq, rtclong1_interrupt, 0,
333 "rtclong1", pdev);
Yoichi Yuasabd0765092007-05-11 21:18:48 +0900334 if (retval < 0)
Jingoo Hanbf6ce1a2014-01-23 15:55:07 -0800335 goto err_iounmap_all;
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700336
337 platform_set_drvdata(pdev, rtc);
338
Yoichi Yuasabd0765092007-05-11 21:18:48 +0900339 disable_irq(aie_irq);
340 disable_irq(pie_irq);
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700341
Jingoo Hand75dcd32013-02-21 16:45:30 -0800342 dev_info(&pdev->dev, "Real Time Clock of NEC VR4100 series\n");
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700343
Alexandre Belloni9a992472018-05-17 22:47:05 +0200344 retval = rtc_register_device(rtc);
345 if (retval)
346 goto err_iounmap_all;
347
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700348 return 0;
Yoichi Yuasabd0765092007-05-11 21:18:48 +0900349
Yoichi Yuasabd0765092007-05-11 21:18:48 +0900350err_iounmap_all:
Yoichi Yuasabd0765092007-05-11 21:18:48 +0900351 rtc2_base = NULL;
352
353err_rtc1_iounmap:
Yoichi Yuasabd0765092007-05-11 21:18:48 +0900354 rtc1_base = NULL;
355
356 return retval;
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700357}
358
Kay Sieversad28a072008-04-10 21:29:25 -0700359/* work with hotplug and coldplug */
360MODULE_ALIAS("platform:RTC");
361
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700362static struct platform_driver rtc_platform_driver = {
363 .probe = rtc_probe,
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700364 .driver = {
365 .name = rtc_name,
Yoichi Yuasa8417eb72006-04-10 22:54:47 -0700366 },
367};
368
Axel Lin0c4eae62012-01-10 15:10:48 -0800369module_platform_driver(rtc_platform_driver);