powerpc/mm: Drop the unnecessary region check
[linux-2.6-block.git] / drivers / rtc / rtc-vr41xx.c
CommitLineData
1da177e4 1/*
8417eb7a 2 * Driver for NEC VR4100 series Real Time Clock unit.
1da177e4 3 *
ada8e951 4 * Copyright (C) 2003-2008 Yoichi Yuasa <yuasa@linux-mips.org>
1da177e4
LT
5 *
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 */
bd076509 20#include <linux/err.h>
1da177e4
LT
21#include <linux/fs.h>
22#include <linux/init.h>
4271e7ff 23#include <linux/io.h>
1da177e4 24#include <linux/ioport.h>
bd076509 25#include <linux/interrupt.h>
1da177e4 26#include <linux/module.h>
8417eb7a 27#include <linux/platform_device.h>
1da177e4
LT
28#include <linux/rtc.h>
29#include <linux/spinlock.h>
30#include <linux/types.h>
4271e7ff 31#include <linux/uaccess.h>
5d2a5037 32#include <linux/log2.h>
1da177e4
LT
33
34#include <asm/div64.h>
1da177e4 35
ada8e951 36MODULE_AUTHOR("Yoichi Yuasa <yuasa@linux-mips.org>");
1da177e4 37MODULE_DESCRIPTION("NEC VR4100 series RTC driver");
4cad4431 38MODULE_LICENSE("GPL v2");
1da177e4 39
1da177e4
LT
40/* 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
1da177e4
LT
72
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
34af946a 84static DEFINE_SPINLOCK(rtc_lock);
1da177e4 85static char rtc_name[] = "RTC";
1da177e4 86static unsigned long periodic_count;
9b5ef64a 87static unsigned int alarm_enabled;
2fac6674
AV
88static int aie_irq;
89static int pie_irq;
1da177e4 90
0d1c6553 91static inline time64_t read_elapsed_second(void)
1da177e4 92{
8417eb7a 93
1da177e4 94 unsigned long first_low, first_mid, first_high;
8417eb7a 95
1da177e4
LT
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 ||
0218bcf6 106 first_high != second_high);
1da177e4 107
0d1c6553 108 return ((u64)first_high << 17) | (first_mid << 1) | (first_low >> 15);
1da177e4
LT
109}
110
0d1c6553 111static inline void write_elapsed_second(time64_t sec)
1da177e4
LT
112{
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
8417eb7a 122static int vr41xx_rtc_read_time(struct device *dev, struct rtc_time *time)
1da177e4 123{
0d1c6553 124 time64_t epoch_sec, elapsed_sec;
1da177e4 125
0d1c6553 126 epoch_sec = mktime64(epoch, 1, 1, 0, 0, 0);
1da177e4
LT
127 elapsed_sec = read_elapsed_second();
128
0d1c6553 129 rtc_time64_to_tm(epoch_sec + elapsed_sec, time);
8417eb7a
YY
130
131 return 0;
1da177e4
LT
132}
133
8417eb7a 134static int vr41xx_rtc_set_time(struct device *dev, struct rtc_time *time)
1da177e4 135{
0d1c6553 136 time64_t epoch_sec, current_sec;
1da177e4 137
0d1c6553 138 epoch_sec = mktime64(epoch, 1, 1, 0, 0, 0);
89e27ce4 139 current_sec = rtc_tm_to_time64(time);
1da177e4
LT
140
141 write_elapsed_second(current_sec - epoch_sec);
8417eb7a
YY
142
143 return 0;
1da177e4
LT
144}
145
8417eb7a 146static int vr41xx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
1da177e4 147{
8417eb7a
YY
148 unsigned long low, mid, high;
149 struct rtc_time *time = &wkalrm->time;
1da177e4 150
8417eb7a 151 spin_lock_irq(&rtc_lock);
1da177e4 152
8417eb7a
YY
153 low = rtc1_read(ECMPLREG);
154 mid = rtc1_read(ECMPMREG);
155 high = rtc1_read(ECMPHREG);
9b5ef64a 156 wkalrm->enabled = alarm_enabled;
1da177e4 157
8417eb7a 158 spin_unlock_irq(&rtc_lock);
1da177e4 159
89e27ce4 160 rtc_time64_to_tm((high << 17) | (mid << 1) | (low >> 15), time);
1da177e4 161
8417eb7a
YY
162 return 0;
163}
1da177e4 164
8417eb7a
YY
165static int vr41xx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
166{
0d1c6553 167 time64_t alarm_sec;
1da177e4 168
89e27ce4 169 alarm_sec = rtc_tm_to_time64(&wkalrm->time);
1da177e4 170
8417eb7a 171 spin_lock_irq(&rtc_lock);
1da177e4 172
9b5ef64a 173 if (alarm_enabled)
bd076509 174 disable_irq(aie_irq);
9b5ef64a 175
8417eb7a
YY
176 rtc1_write(ECMPLREG, (uint16_t)(alarm_sec << 15));
177 rtc1_write(ECMPMREG, (uint16_t)(alarm_sec >> 1));
178 rtc1_write(ECMPHREG, (uint16_t)(alarm_sec >> 17));
1da177e4 179
9b5ef64a 180 if (wkalrm->enabled)
bd076509 181 enable_irq(aie_irq);
9b5ef64a
YY
182
183 alarm_enabled = wkalrm->enabled;
184
8417eb7a 185 spin_unlock_irq(&rtc_lock);
1da177e4
LT
186
187 return 0;
188}
189
4cad4431
YY
190static int vr41xx_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
191{
1da177e4 192 switch (cmd) {
1da177e4
LT
193 case RTC_EPOCH_READ:
194 return put_user(epoch, (unsigned long __user *)arg);
195 case RTC_EPOCH_SET:
196 /* Doesn't support before 1900 */
197 if (arg < 1900)
198 return -EINVAL;
1da177e4
LT
199 epoch = arg;
200 break;
201 default:
b3969e58 202 return -ENOIOCTLCMD;
1da177e4
LT
203 }
204
205 return 0;
206}
207
16380c15
JS
208static int vr41xx_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
209{
210 spin_lock_irq(&rtc_lock);
211 if (enabled) {
212 if (!alarm_enabled) {
213 enable_irq(aie_irq);
214 alarm_enabled = 1;
215 }
216 } else {
217 if (alarm_enabled) {
218 disable_irq(aie_irq);
219 alarm_enabled = 0;
220 }
221 }
222 spin_unlock_irq(&rtc_lock);
223 return 0;
224}
225
7d12e780 226static irqreturn_t elapsedtime_interrupt(int irq, void *dev_id)
1da177e4 227{
8417eb7a
YY
228 struct platform_device *pdev = (struct platform_device *)dev_id;
229 struct rtc_device *rtc = platform_get_drvdata(pdev);
1da177e4 230
8417eb7a 231 rtc2_write(RTCINTREG, ELAPSEDTIME_INT);
1da177e4 232
ab6a2d70 233 rtc_update_irq(rtc, 1, RTC_AF);
1da177e4
LT
234
235 return IRQ_HANDLED;
236}
237
7d12e780 238static irqreturn_t rtclong1_interrupt(int irq, void *dev_id)
1da177e4 239{
8417eb7a
YY
240 struct platform_device *pdev = (struct platform_device *)dev_id;
241 struct rtc_device *rtc = platform_get_drvdata(pdev);
1da177e4
LT
242 unsigned long count = periodic_count;
243
1da177e4
LT
244 rtc2_write(RTCINTREG, RTCLONG1_INT);
245
246 rtc1_write(RTCL1LREG, count);
247 rtc1_write(RTCL1HREG, count >> 16);
248
ab6a2d70 249 rtc_update_irq(rtc, 1, RTC_PF);
1da177e4
LT
250
251 return IRQ_HANDLED;
252}
253
ff8371ac 254static const struct rtc_class_ops vr41xx_rtc_ops = {
a25f4a95
GU
255 .ioctl = vr41xx_rtc_ioctl,
256 .read_time = vr41xx_rtc_read_time,
257 .set_time = vr41xx_rtc_set_time,
258 .read_alarm = vr41xx_rtc_read_alarm,
259 .set_alarm = vr41xx_rtc_set_alarm,
260 .alarm_irq_enable = vr41xx_rtc_alarm_irq_enable,
1da177e4
LT
261};
262
5a167f45 263static int rtc_probe(struct platform_device *pdev)
1da177e4 264{
bd076509 265 struct resource *res;
8417eb7a 266 struct rtc_device *rtc;
1da177e4
LT
267 int retval;
268
bd076509 269 if (pdev->num_resources != 4)
1da177e4
LT
270 return -EBUSY;
271
bd076509
YY
272 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
273 if (!res)
1da177e4
LT
274 return -EBUSY;
275
bf6ce1a1 276 rtc1_base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
bd076509 277 if (!rtc1_base)
1da177e4 278 return -EBUSY;
bd076509
YY
279
280 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
281 if (!res) {
282 retval = -EBUSY;
283 goto err_rtc1_iounmap;
284 }
285
bf6ce1a1 286 rtc2_base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
bd076509
YY
287 if (!rtc2_base) {
288 retval = -EBUSY;
289 goto err_rtc1_iounmap;
1da177e4
LT
290 }
291
9a99247c 292 rtc = devm_rtc_allocate_device(&pdev->dev);
8417eb7a 293 if (IS_ERR(rtc)) {
bd076509
YY
294 retval = PTR_ERR(rtc);
295 goto err_iounmap_all;
1da177e4
LT
296 }
297
9a99247c
AB
298 rtc->ops = &vr41xx_rtc_ops;
299
30d7891a
AB
300 /* 48-bit counter at 32.768 kHz */
301 rtc->range_max = (1ULL << 33) - 1;
4cad4431
YY
302 rtc->max_user_freq = MAX_PERIODIC_RATE;
303
1da177e4
LT
304 spin_lock_irq(&rtc_lock);
305
306 rtc1_write(ECMPLREG, 0);
307 rtc1_write(ECMPMREG, 0);
308 rtc1_write(ECMPHREG, 0);
309 rtc1_write(RTCL1LREG, 0);
310 rtc1_write(RTCL1HREG, 0);
311
1da177e4
LT
312 spin_unlock_irq(&rtc_lock);
313
bd076509 314 aie_irq = platform_get_irq(pdev, 0);
2fac6674 315 if (aie_irq <= 0) {
bd076509 316 retval = -EBUSY;
bf6ce1a1 317 goto err_iounmap_all;
1da177e4
LT
318 }
319
bf6ce1a1
JH
320 retval = devm_request_irq(&pdev->dev, aie_irq, elapsedtime_interrupt, 0,
321 "elapsed_time", pdev);
bd076509 322 if (retval < 0)
bf6ce1a1 323 goto err_iounmap_all;
bd076509
YY
324
325 pie_irq = platform_get_irq(pdev, 1);
812f1478
WY
326 if (pie_irq <= 0) {
327 retval = -EBUSY;
bf6ce1a1 328 goto err_iounmap_all;
812f1478 329 }
bd076509 330
bf6ce1a1
JH
331 retval = devm_request_irq(&pdev->dev, pie_irq, rtclong1_interrupt, 0,
332 "rtclong1", pdev);
bd076509 333 if (retval < 0)
bf6ce1a1 334 goto err_iounmap_all;
1da177e4 335
8417eb7a
YY
336 platform_set_drvdata(pdev, rtc);
337
bd076509
YY
338 disable_irq(aie_irq);
339 disable_irq(pie_irq);
1da177e4 340
d75dcd33 341 dev_info(&pdev->dev, "Real Time Clock of NEC VR4100 series\n");
1da177e4 342
9a99247c
AB
343 retval = rtc_register_device(rtc);
344 if (retval)
345 goto err_iounmap_all;
346
1da177e4 347 return 0;
bd076509 348
bd076509 349err_iounmap_all:
bd076509
YY
350 rtc2_base = NULL;
351
352err_rtc1_iounmap:
bd076509
YY
353 rtc1_base = NULL;
354
355 return retval;
1da177e4
LT
356}
357
ad28a07b
KS
358/* work with hotplug and coldplug */
359MODULE_ALIAS("platform:RTC");
360
8417eb7a 361static struct platform_driver rtc_platform_driver = {
1da177e4 362 .probe = rtc_probe,
3ae5eaec
RK
363 .driver = {
364 .name = rtc_name,
365 },
1da177e4
LT
366};
367
0c4eae66 368module_platform_driver(rtc_platform_driver);