blob: a45845a571e51a693d79143333d84f28bce28455 [file] [log] [blame]
Paul Mundt317a6102006-09-27 17:13:19 +09001/*
2 * SuperH On-Chip RTC Support
3 *
Paul Mundt063adc72009-04-16 14:12:22 +09004 * Copyright (C) 2006 - 2009 Paul Mundt
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +09005 * Copyright (C) 2006 Jamie Lenehan
Angelo Castellob420b1a2008-03-06 12:50:53 +09006 * Copyright (C) 2008 Angelo Castello
Paul Mundt317a6102006-09-27 17:13:19 +09007 *
8 * Based on the old arch/sh/kernel/cpu/rtc.c by:
9 *
10 * Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org>
11 * Copyright (C) 1999 Tetsuya Okada & Niibe Yutaka
12 *
13 * This file is subject to the terms and conditions of the GNU General Public
14 * License. See the file "COPYING" in the main directory of this archive
15 * for more details.
16 */
17#include <linux/module.h>
18#include <linux/kernel.h>
19#include <linux/bcd.h>
20#include <linux/rtc.h>
21#include <linux/init.h>
22#include <linux/platform_device.h>
23#include <linux/seq_file.h>
24#include <linux/interrupt.h>
25#include <linux/spinlock.h>
Jamie Lenehan31ccb082006-12-07 17:23:50 +090026#include <linux/io.h>
Jonathan Cameron5d2a5032009-01-06 14:42:12 -080027#include <linux/log2.h>
Paul Mundt063adc72009-04-16 14:12:22 +090028#include <linux/clk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Paul Mundtad89f872007-08-03 14:19:58 +090030#include <asm/rtc.h>
Paul Mundt317a6102006-09-27 17:13:19 +090031
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +090032#define DRV_NAME "sh-rtc"
Paul Mundt317a6102006-09-27 17:13:19 +090033
34#define RTC_REG(r) ((r) * rtc_reg_size)
35
Jamie Lenehan31ccb082006-12-07 17:23:50 +090036#define R64CNT RTC_REG(0)
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +090037
38#define RSECCNT RTC_REG(1) /* RTC sec */
39#define RMINCNT RTC_REG(2) /* RTC min */
40#define RHRCNT RTC_REG(3) /* RTC hour */
41#define RWKCNT RTC_REG(4) /* RTC week */
42#define RDAYCNT RTC_REG(5) /* RTC day */
43#define RMONCNT RTC_REG(6) /* RTC month */
44#define RYRCNT RTC_REG(7) /* RTC year */
45#define RSECAR RTC_REG(8) /* ALARM sec */
46#define RMINAR RTC_REG(9) /* ALARM min */
47#define RHRAR RTC_REG(10) /* ALARM hour */
48#define RWKAR RTC_REG(11) /* ALARM week */
49#define RDAYAR RTC_REG(12) /* ALARM day */
50#define RMONAR RTC_REG(13) /* ALARM month */
51#define RCR1 RTC_REG(14) /* Control */
52#define RCR2 RTC_REG(15) /* Control */
53
Paul Mundtff1b7502007-11-26 17:56:31 +090054/*
55 * Note on RYRAR and RCR3: Up until this point most of the register
56 * definitions are consistent across all of the available parts. However,
57 * the placement of the optional RYRAR and RCR3 (the RYRAR control
58 * register used to control RYRCNT/RYRAR compare) varies considerably
59 * across various parts, occasionally being mapped in to a completely
60 * unrelated address space. For proper RYRAR support a separate resource
61 * would have to be handed off, but as this is purely optional in
62 * practice, we simply opt not to support it, thereby keeping the code
63 * quite a bit more simplified.
64 */
65
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +090066/* ALARM Bits - or with BCD encoded value */
67#define AR_ENB 0x80 /* Enable for alarm cmp */
Paul Mundt317a6102006-09-27 17:13:19 +090068
Angelo Castellob420b1a2008-03-06 12:50:53 +090069/* Period Bits */
70#define PF_HP 0x100 /* Enable Half Period to support 8,32,128Hz */
71#define PF_COUNT 0x200 /* Half periodic counter */
72#define PF_OXS 0x400 /* Periodic One x Second */
73#define PF_KOU 0x800 /* Kernel or User periodic request 1=kernel */
74#define PF_MASK 0xf00
75
Paul Mundt317a6102006-09-27 17:13:19 +090076/* RCR1 Bits */
77#define RCR1_CF 0x80 /* Carry Flag */
78#define RCR1_CIE 0x10 /* Carry Interrupt Enable */
79#define RCR1_AIE 0x08 /* Alarm Interrupt Enable */
80#define RCR1_AF 0x01 /* Alarm Flag */
81
82/* RCR2 Bits */
83#define RCR2_PEF 0x80 /* PEriodic interrupt Flag */
84#define RCR2_PESMASK 0x70 /* Periodic interrupt Set */
85#define RCR2_RTCEN 0x08 /* ENable RTC */
86#define RCR2_ADJ 0x04 /* ADJustment (30-second) */
87#define RCR2_RESET 0x02 /* Reset bit */
88#define RCR2_START 0x01 /* Start bit */
89
90struct sh_rtc {
Paul Mundt063adc72009-04-16 14:12:22 +090091 void __iomem *regbase;
92 unsigned long regsize;
93 struct resource *res;
94 int alarm_irq;
95 int periodic_irq;
96 int carry_irq;
97 struct clk *clk;
98 struct rtc_device *rtc_dev;
99 spinlock_t lock;
100 unsigned long capabilities; /* See asm/rtc.h for cap bits */
101 unsigned short periodic_freq;
Paul Mundt317a6102006-09-27 17:13:19 +0900102};
103
Magnus Damm5e084a12009-02-24 22:11:03 +0900104static int __sh_rtc_interrupt(struct sh_rtc *rtc)
Paul Mundt317a6102006-09-27 17:13:19 +0900105{
Magnus Damm5e084a12009-02-24 22:11:03 +0900106 unsigned int tmp, pending;
Paul Mundt317a6102006-09-27 17:13:19 +0900107
108 tmp = readb(rtc->regbase + RCR1);
Magnus Damm5e084a12009-02-24 22:11:03 +0900109 pending = tmp & RCR1_CF;
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900110 tmp &= ~RCR1_CF;
Paul Mundt317a6102006-09-27 17:13:19 +0900111 writeb(tmp, rtc->regbase + RCR1);
112
Angelo Castellob420b1a2008-03-06 12:50:53 +0900113 /* Users have requested One x Second IRQ */
Magnus Damm5e084a12009-02-24 22:11:03 +0900114 if (pending && rtc->periodic_freq & PF_OXS)
Angelo Castellob420b1a2008-03-06 12:50:53 +0900115 rtc_update_irq(rtc->rtc_dev, 1, RTC_UF | RTC_IRQF);
Paul Mundt317a6102006-09-27 17:13:19 +0900116
Magnus Damm5e084a12009-02-24 22:11:03 +0900117 return pending;
Paul Mundt317a6102006-09-27 17:13:19 +0900118}
119
Magnus Damm5e084a12009-02-24 22:11:03 +0900120static int __sh_rtc_alarm(struct sh_rtc *rtc)
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900121{
Magnus Damm5e084a12009-02-24 22:11:03 +0900122 unsigned int tmp, pending;
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900123
124 tmp = readb(rtc->regbase + RCR1);
Magnus Damm5e084a12009-02-24 22:11:03 +0900125 pending = tmp & RCR1_AF;
Angelo Castellob420b1a2008-03-06 12:50:53 +0900126 tmp &= ~(RCR1_AF | RCR1_AIE);
Magnus Damm5e084a12009-02-24 22:11:03 +0900127 writeb(tmp, rtc->regbase + RCR1);
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900128
Magnus Damm5e084a12009-02-24 22:11:03 +0900129 if (pending)
130 rtc_update_irq(rtc->rtc_dev, 1, RTC_AF | RTC_IRQF);
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900131
Magnus Damm5e084a12009-02-24 22:11:03 +0900132 return pending;
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900133}
134
Magnus Damm5e084a12009-02-24 22:11:03 +0900135static int __sh_rtc_periodic(struct sh_rtc *rtc)
Paul Mundt317a6102006-09-27 17:13:19 +0900136{
Angelo Castellob420b1a2008-03-06 12:50:53 +0900137 struct rtc_device *rtc_dev = rtc->rtc_dev;
Magnus Damm5e084a12009-02-24 22:11:03 +0900138 struct rtc_task *irq_task;
139 unsigned int tmp, pending;
Paul Mundt317a6102006-09-27 17:13:19 +0900140
Angelo Castellob420b1a2008-03-06 12:50:53 +0900141 tmp = readb(rtc->regbase + RCR2);
Magnus Damm5e084a12009-02-24 22:11:03 +0900142 pending = tmp & RCR2_PEF;
Angelo Castellob420b1a2008-03-06 12:50:53 +0900143 tmp &= ~RCR2_PEF;
144 writeb(tmp, rtc->regbase + RCR2);
145
Magnus Damm5e084a12009-02-24 22:11:03 +0900146 if (!pending)
147 return 0;
148
Angelo Castellob420b1a2008-03-06 12:50:53 +0900149 /* Half period enabled than one skipped and the next notified */
150 if ((rtc->periodic_freq & PF_HP) && (rtc->periodic_freq & PF_COUNT))
151 rtc->periodic_freq &= ~PF_COUNT;
152 else {
153 if (rtc->periodic_freq & PF_HP)
154 rtc->periodic_freq |= PF_COUNT;
155 if (rtc->periodic_freq & PF_KOU) {
156 spin_lock(&rtc_dev->irq_task_lock);
Magnus Damm5e084a12009-02-24 22:11:03 +0900157 irq_task = rtc_dev->irq_task;
158 if (irq_task)
159 irq_task->func(irq_task->private_data);
Angelo Castellob420b1a2008-03-06 12:50:53 +0900160 spin_unlock(&rtc_dev->irq_task_lock);
161 } else
162 rtc_update_irq(rtc->rtc_dev, 1, RTC_PF | RTC_IRQF);
163 }
Paul Mundt317a6102006-09-27 17:13:19 +0900164
Magnus Damm5e084a12009-02-24 22:11:03 +0900165 return pending;
166}
167
168static irqreturn_t sh_rtc_interrupt(int irq, void *dev_id)
169{
170 struct sh_rtc *rtc = dev_id;
171 int ret;
172
173 spin_lock(&rtc->lock);
174 ret = __sh_rtc_interrupt(rtc);
Paul Mundt317a6102006-09-27 17:13:19 +0900175 spin_unlock(&rtc->lock);
176
Magnus Damm5e084a12009-02-24 22:11:03 +0900177 return IRQ_RETVAL(ret);
178}
179
180static irqreturn_t sh_rtc_alarm(int irq, void *dev_id)
181{
182 struct sh_rtc *rtc = dev_id;
183 int ret;
184
185 spin_lock(&rtc->lock);
186 ret = __sh_rtc_alarm(rtc);
187 spin_unlock(&rtc->lock);
188
189 return IRQ_RETVAL(ret);
190}
191
192static irqreturn_t sh_rtc_periodic(int irq, void *dev_id)
193{
194 struct sh_rtc *rtc = dev_id;
195 int ret;
196
197 spin_lock(&rtc->lock);
198 ret = __sh_rtc_periodic(rtc);
199 spin_unlock(&rtc->lock);
200
201 return IRQ_RETVAL(ret);
202}
203
204static irqreturn_t sh_rtc_shared(int irq, void *dev_id)
205{
206 struct sh_rtc *rtc = dev_id;
207 int ret;
208
209 spin_lock(&rtc->lock);
210 ret = __sh_rtc_interrupt(rtc);
211 ret |= __sh_rtc_alarm(rtc);
212 ret |= __sh_rtc_periodic(rtc);
213 spin_unlock(&rtc->lock);
214
215 return IRQ_RETVAL(ret);
Paul Mundt317a6102006-09-27 17:13:19 +0900216}
217
Alessandro Zummo5c9740a2009-08-20 13:25:11 +0900218static int sh_rtc_irq_set_state(struct device *dev, int enable)
Paul Mundt317a6102006-09-27 17:13:19 +0900219{
220 struct sh_rtc *rtc = dev_get_drvdata(dev);
221 unsigned int tmp;
222
223 spin_lock_irq(&rtc->lock);
224
225 tmp = readb(rtc->regbase + RCR2);
226
227 if (enable) {
Alessandro Zummo5c9740a2009-08-20 13:25:11 +0900228 rtc->periodic_freq |= PF_KOU;
Angelo Castellob420b1a2008-03-06 12:50:53 +0900229 tmp &= ~RCR2_PEF; /* Clear PES bit */
230 tmp |= (rtc->periodic_freq & ~PF_HP); /* Set PES2-0 */
Alessandro Zummo5c9740a2009-08-20 13:25:11 +0900231 } else {
232 rtc->periodic_freq &= ~PF_KOU;
Paul Mundt317a6102006-09-27 17:13:19 +0900233 tmp &= ~(RCR2_PESMASK | RCR2_PEF);
Alessandro Zummo5c9740a2009-08-20 13:25:11 +0900234 }
Paul Mundt317a6102006-09-27 17:13:19 +0900235
236 writeb(tmp, rtc->regbase + RCR2);
237
238 spin_unlock_irq(&rtc->lock);
Alessandro Zummo5c9740a2009-08-20 13:25:11 +0900239
240 return 0;
Paul Mundt317a6102006-09-27 17:13:19 +0900241}
242
Alessandro Zummo5c9740a2009-08-20 13:25:11 +0900243static int sh_rtc_irq_set_freq(struct device *dev, int freq)
Angelo Castellob420b1a2008-03-06 12:50:53 +0900244{
245 struct sh_rtc *rtc = dev_get_drvdata(dev);
246 int tmp, ret = 0;
247
248 spin_lock_irq(&rtc->lock);
249 tmp = rtc->periodic_freq & PF_MASK;
250
251 switch (freq) {
252 case 0:
253 rtc->periodic_freq = 0x00;
254 break;
255 case 1:
256 rtc->periodic_freq = 0x60;
257 break;
258 case 2:
259 rtc->periodic_freq = 0x50;
260 break;
261 case 4:
262 rtc->periodic_freq = 0x40;
263 break;
264 case 8:
265 rtc->periodic_freq = 0x30 | PF_HP;
266 break;
267 case 16:
268 rtc->periodic_freq = 0x30;
269 break;
270 case 32:
271 rtc->periodic_freq = 0x20 | PF_HP;
272 break;
273 case 64:
274 rtc->periodic_freq = 0x20;
275 break;
276 case 128:
277 rtc->periodic_freq = 0x10 | PF_HP;
278 break;
279 case 256:
280 rtc->periodic_freq = 0x10;
281 break;
282 default:
283 ret = -ENOTSUPP;
284 }
285
Paul Mundt1043bf52009-09-09 12:13:01 +0900286 if (ret == 0)
Angelo Castellob420b1a2008-03-06 12:50:53 +0900287 rtc->periodic_freq |= tmp;
Angelo Castellob420b1a2008-03-06 12:50:53 +0900288
289 spin_unlock_irq(&rtc->lock);
290 return ret;
291}
292
Paul Mundt317a6102006-09-27 17:13:19 +0900293static inline void sh_rtc_setaie(struct device *dev, unsigned int enable)
294{
295 struct sh_rtc *rtc = dev_get_drvdata(dev);
296 unsigned int tmp;
297
298 spin_lock_irq(&rtc->lock);
299
300 tmp = readb(rtc->regbase + RCR1);
301
Paul Mundt063adc72009-04-16 14:12:22 +0900302 if (enable)
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900303 tmp |= RCR1_AIE;
Paul Mundt063adc72009-04-16 14:12:22 +0900304 else
305 tmp &= ~RCR1_AIE;
Paul Mundt317a6102006-09-27 17:13:19 +0900306
307 writeb(tmp, rtc->regbase + RCR1);
308
309 spin_unlock_irq(&rtc->lock);
310}
311
Paul Mundt317a6102006-09-27 17:13:19 +0900312static int sh_rtc_proc(struct device *dev, struct seq_file *seq)
313{
314 struct sh_rtc *rtc = dev_get_drvdata(dev);
315 unsigned int tmp;
316
317 tmp = readb(rtc->regbase + RCR1);
Angelo Castellob420b1a2008-03-06 12:50:53 +0900318 seq_printf(seq, "carry_IRQ\t: %s\n", (tmp & RCR1_CIE) ? "yes" : "no");
Paul Mundt317a6102006-09-27 17:13:19 +0900319
320 tmp = readb(rtc->regbase + RCR2);
321 seq_printf(seq, "periodic_IRQ\t: %s\n",
Angelo Castellob420b1a2008-03-06 12:50:53 +0900322 (tmp & RCR2_PESMASK) ? "yes" : "no");
Paul Mundt317a6102006-09-27 17:13:19 +0900323
324 return 0;
325}
326
Magnus Damm9cd88b92009-03-19 10:05:58 +0000327static inline void sh_rtc_setcie(struct device *dev, unsigned int enable)
328{
329 struct sh_rtc *rtc = dev_get_drvdata(dev);
330 unsigned int tmp;
331
332 spin_lock_irq(&rtc->lock);
333
334 tmp = readb(rtc->regbase + RCR1);
335
336 if (!enable)
337 tmp &= ~RCR1_CIE;
338 else
339 tmp |= RCR1_CIE;
340
341 writeb(tmp, rtc->regbase + RCR1);
342
343 spin_unlock_irq(&rtc->lock);
344}
345
John Stultz16380c12011-02-02 17:02:41 -0800346static int sh_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
347{
348 sh_rtc_setaie(dev, enabled);
349 return 0;
350}
351
Paul Mundt317a6102006-09-27 17:13:19 +0900352static int sh_rtc_read_time(struct device *dev, struct rtc_time *tm)
353{
354 struct platform_device *pdev = to_platform_device(dev);
355 struct sh_rtc *rtc = platform_get_drvdata(pdev);
356 unsigned int sec128, sec2, yr, yr100, cf_bit;
357
358 do {
359 unsigned int tmp;
360
361 spin_lock_irq(&rtc->lock);
362
363 tmp = readb(rtc->regbase + RCR1);
364 tmp &= ~RCR1_CF; /* Clear CF-bit */
365 tmp |= RCR1_CIE;
366 writeb(tmp, rtc->regbase + RCR1);
367
368 sec128 = readb(rtc->regbase + R64CNT);
369
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700370 tm->tm_sec = bcd2bin(readb(rtc->regbase + RSECCNT));
371 tm->tm_min = bcd2bin(readb(rtc->regbase + RMINCNT));
372 tm->tm_hour = bcd2bin(readb(rtc->regbase + RHRCNT));
373 tm->tm_wday = bcd2bin(readb(rtc->regbase + RWKCNT));
374 tm->tm_mday = bcd2bin(readb(rtc->regbase + RDAYCNT));
375 tm->tm_mon = bcd2bin(readb(rtc->regbase + RMONCNT)) - 1;
Paul Mundt317a6102006-09-27 17:13:19 +0900376
Paul Mundtad89f872007-08-03 14:19:58 +0900377 if (rtc->capabilities & RTC_CAP_4_DIGIT_YEAR) {
378 yr = readw(rtc->regbase + RYRCNT);
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700379 yr100 = bcd2bin(yr >> 8);
Paul Mundtad89f872007-08-03 14:19:58 +0900380 yr &= 0xff;
381 } else {
382 yr = readb(rtc->regbase + RYRCNT);
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700383 yr100 = bcd2bin((yr == 0x99) ? 0x19 : 0x20);
Paul Mundtad89f872007-08-03 14:19:58 +0900384 }
Paul Mundt317a6102006-09-27 17:13:19 +0900385
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700386 tm->tm_year = (yr100 * 100 + bcd2bin(yr)) - 1900;
Paul Mundt317a6102006-09-27 17:13:19 +0900387
388 sec2 = readb(rtc->regbase + R64CNT);
389 cf_bit = readb(rtc->regbase + RCR1) & RCR1_CF;
390
391 spin_unlock_irq(&rtc->lock);
392 } while (cf_bit != 0 || ((sec128 ^ sec2) & RTC_BIT_INVERTED) != 0);
393
394#if RTC_BIT_INVERTED != 0
395 if ((sec128 & RTC_BIT_INVERTED))
396 tm->tm_sec--;
397#endif
398
Magnus Damm9cd88b92009-03-19 10:05:58 +0000399 /* only keep the carry interrupt enabled if UIE is on */
400 if (!(rtc->periodic_freq & PF_OXS))
401 sh_rtc_setcie(dev, 0);
402
Paul Mundt435c55d2007-05-08 11:56:27 +0900403 dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
Paul Mundt317a6102006-09-27 17:13:19 +0900404 "mday=%d, mon=%d, year=%d, wday=%d\n",
Harvey Harrison2a4e2b82008-04-28 02:12:00 -0700405 __func__,
Paul Mundt317a6102006-09-27 17:13:19 +0900406 tm->tm_sec, tm->tm_min, tm->tm_hour,
Jamie Lenehana1614792006-12-08 14:49:30 +0900407 tm->tm_mday, tm->tm_mon + 1, tm->tm_year, tm->tm_wday);
Paul Mundt317a6102006-09-27 17:13:19 +0900408
Magnus Dammedf22472009-03-19 10:10:44 +0000409 return rtc_valid_tm(tm);
Paul Mundt317a6102006-09-27 17:13:19 +0900410}
411
412static int sh_rtc_set_time(struct device *dev, struct rtc_time *tm)
413{
414 struct platform_device *pdev = to_platform_device(dev);
415 struct sh_rtc *rtc = platform_get_drvdata(pdev);
416 unsigned int tmp;
417 int year;
418
419 spin_lock_irq(&rtc->lock);
420
421 /* Reset pre-scaler & stop RTC */
422 tmp = readb(rtc->regbase + RCR2);
423 tmp |= RCR2_RESET;
Markus Brunner699bc662007-07-26 17:31:28 +0900424 tmp &= ~RCR2_START;
Paul Mundt317a6102006-09-27 17:13:19 +0900425 writeb(tmp, rtc->regbase + RCR2);
426
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700427 writeb(bin2bcd(tm->tm_sec), rtc->regbase + RSECCNT);
428 writeb(bin2bcd(tm->tm_min), rtc->regbase + RMINCNT);
429 writeb(bin2bcd(tm->tm_hour), rtc->regbase + RHRCNT);
430 writeb(bin2bcd(tm->tm_wday), rtc->regbase + RWKCNT);
431 writeb(bin2bcd(tm->tm_mday), rtc->regbase + RDAYCNT);
432 writeb(bin2bcd(tm->tm_mon + 1), rtc->regbase + RMONCNT);
Paul Mundt317a6102006-09-27 17:13:19 +0900433
Paul Mundtad89f872007-08-03 14:19:58 +0900434 if (rtc->capabilities & RTC_CAP_4_DIGIT_YEAR) {
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700435 year = (bin2bcd((tm->tm_year + 1900) / 100) << 8) |
436 bin2bcd(tm->tm_year % 100);
Paul Mundtad89f872007-08-03 14:19:58 +0900437 writew(year, rtc->regbase + RYRCNT);
438 } else {
439 year = tm->tm_year % 100;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700440 writeb(bin2bcd(year), rtc->regbase + RYRCNT);
Paul Mundtad89f872007-08-03 14:19:58 +0900441 }
Paul Mundt317a6102006-09-27 17:13:19 +0900442
443 /* Start RTC */
444 tmp = readb(rtc->regbase + RCR2);
445 tmp &= ~RCR2_RESET;
446 tmp |= RCR2_RTCEN | RCR2_START;
447 writeb(tmp, rtc->regbase + RCR2);
448
449 spin_unlock_irq(&rtc->lock);
450
451 return 0;
452}
453
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900454static inline int sh_rtc_read_alarm_value(struct sh_rtc *rtc, int reg_off)
455{
456 unsigned int byte;
457 int value = 0xff; /* return 0xff for ignored values */
458
459 byte = readb(rtc->regbase + reg_off);
460 if (byte & AR_ENB) {
461 byte &= ~AR_ENB; /* strip the enable bit */
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700462 value = bcd2bin(byte);
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900463 }
464
465 return value;
466}
467
468static int sh_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
469{
470 struct platform_device *pdev = to_platform_device(dev);
471 struct sh_rtc *rtc = platform_get_drvdata(pdev);
Angelo Castellob420b1a2008-03-06 12:50:53 +0900472 struct rtc_time *tm = &wkalrm->time;
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900473
474 spin_lock_irq(&rtc->lock);
475
476 tm->tm_sec = sh_rtc_read_alarm_value(rtc, RSECAR);
477 tm->tm_min = sh_rtc_read_alarm_value(rtc, RMINAR);
478 tm->tm_hour = sh_rtc_read_alarm_value(rtc, RHRAR);
479 tm->tm_wday = sh_rtc_read_alarm_value(rtc, RWKAR);
480 tm->tm_mday = sh_rtc_read_alarm_value(rtc, RDAYAR);
481 tm->tm_mon = sh_rtc_read_alarm_value(rtc, RMONAR);
482 if (tm->tm_mon > 0)
483 tm->tm_mon -= 1; /* RTC is 1-12, tm_mon is 0-11 */
484 tm->tm_year = 0xffff;
485
David Brownell0d103e92007-01-10 23:15:32 -0800486 wkalrm->enabled = (readb(rtc->regbase + RCR1) & RCR1_AIE) ? 1 : 0;
487
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900488 spin_unlock_irq(&rtc->lock);
489
490 return 0;
491}
492
493static inline void sh_rtc_write_alarm_value(struct sh_rtc *rtc,
494 int value, int reg_off)
495{
496 /* < 0 for a value that is ignored */
497 if (value < 0)
498 writeb(0, rtc->regbase + reg_off);
499 else
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700500 writeb(bin2bcd(value) | AR_ENB, rtc->regbase + reg_off);
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900501}
502
Angelo Castellob420b1a2008-03-06 12:50:53 +0900503static int sh_rtc_check_alarm(struct rtc_time *tm)
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900504{
505 /*
506 * The original rtc says anything > 0xc0 is "don't care" or "match
507 * all" - most users use 0xff but rtc-dev uses -1 for the same thing.
508 * The original rtc doesn't support years - some things use -1 and
509 * some 0xffff. We use -1 to make out tests easier.
510 */
511 if (tm->tm_year == 0xffff)
512 tm->tm_year = -1;
513 if (tm->tm_mon >= 0xff)
514 tm->tm_mon = -1;
515 if (tm->tm_mday >= 0xff)
516 tm->tm_mday = -1;
517 if (tm->tm_wday >= 0xff)
518 tm->tm_wday = -1;
519 if (tm->tm_hour >= 0xff)
520 tm->tm_hour = -1;
521 if (tm->tm_min >= 0xff)
522 tm->tm_min = -1;
523 if (tm->tm_sec >= 0xff)
524 tm->tm_sec = -1;
525
526 if (tm->tm_year > 9999 ||
527 tm->tm_mon >= 12 ||
528 tm->tm_mday == 0 || tm->tm_mday >= 32 ||
529 tm->tm_wday >= 7 ||
530 tm->tm_hour >= 24 ||
531 tm->tm_min >= 60 ||
532 tm->tm_sec >= 60)
533 return -EINVAL;
534
535 return 0;
536}
537
538static int sh_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
539{
540 struct platform_device *pdev = to_platform_device(dev);
541 struct sh_rtc *rtc = platform_get_drvdata(pdev);
542 unsigned int rcr1;
543 struct rtc_time *tm = &wkalrm->time;
544 int mon, err;
545
546 err = sh_rtc_check_alarm(tm);
547 if (unlikely(err < 0))
548 return err;
549
550 spin_lock_irq(&rtc->lock);
551
Jamie Lenehan15c945c2007-01-22 20:40:41 -0800552 /* disable alarm interrupt and clear the alarm flag */
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900553 rcr1 = readb(rtc->regbase + RCR1);
Angelo Castellob420b1a2008-03-06 12:50:53 +0900554 rcr1 &= ~(RCR1_AF | RCR1_AIE);
Jamie Lenehan15c945c2007-01-22 20:40:41 -0800555 writeb(rcr1, rtc->regbase + RCR1);
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900556
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900557 /* set alarm time */
558 sh_rtc_write_alarm_value(rtc, tm->tm_sec, RSECAR);
559 sh_rtc_write_alarm_value(rtc, tm->tm_min, RMINAR);
560 sh_rtc_write_alarm_value(rtc, tm->tm_hour, RHRAR);
561 sh_rtc_write_alarm_value(rtc, tm->tm_wday, RWKAR);
562 sh_rtc_write_alarm_value(rtc, tm->tm_mday, RDAYAR);
563 mon = tm->tm_mon;
564 if (mon >= 0)
565 mon += 1;
566 sh_rtc_write_alarm_value(rtc, mon, RMONAR);
567
Jamie Lenehan15c945c2007-01-22 20:40:41 -0800568 if (wkalrm->enabled) {
569 rcr1 |= RCR1_AIE;
570 writeb(rcr1, rtc->regbase + RCR1);
571 }
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900572
573 spin_unlock_irq(&rtc->lock);
574
575 return 0;
576}
577
Paul Mundt317a6102006-09-27 17:13:19 +0900578static struct rtc_class_ops sh_rtc_ops = {
Paul Mundt317a6102006-09-27 17:13:19 +0900579 .read_time = sh_rtc_read_time,
580 .set_time = sh_rtc_set_time,
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900581 .read_alarm = sh_rtc_read_alarm,
582 .set_alarm = sh_rtc_set_alarm,
Paul Mundt317a6102006-09-27 17:13:19 +0900583 .proc = sh_rtc_proc,
John Stultz16380c12011-02-02 17:02:41 -0800584 .alarm_irq_enable = sh_rtc_alarm_irq_enable,
Paul Mundt317a6102006-09-27 17:13:19 +0900585};
586
Alessandro Zummo5c9740a2009-08-20 13:25:11 +0900587static int __init sh_rtc_probe(struct platform_device *pdev)
Paul Mundt317a6102006-09-27 17:13:19 +0900588{
589 struct sh_rtc *rtc;
590 struct resource *res;
Magnus Dammedf22472009-03-19 10:10:44 +0000591 struct rtc_time r;
Paul Mundt063adc72009-04-16 14:12:22 +0900592 char clk_name[6];
593 int clk_id, ret;
Paul Mundt317a6102006-09-27 17:13:19 +0900594
Jingoo Han0209aff2013-07-03 15:07:11 -0700595 rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
Paul Mundt317a6102006-09-27 17:13:19 +0900596 if (unlikely(!rtc))
597 return -ENOMEM;
598
599 spin_lock_init(&rtc->lock);
600
Angelo Castellob420b1a2008-03-06 12:50:53 +0900601 /* get periodic/carry/alarm irqs */
roel kluin2641dc92008-09-10 19:34:44 +0200602 ret = platform_get_irq(pdev, 0);
Anton Vorontsov2fac6672009-01-06 14:42:11 -0800603 if (unlikely(ret <= 0)) {
Magnus Damm5e084a12009-02-24 22:11:03 +0900604 dev_err(&pdev->dev, "No IRQ resource\n");
Jingoo Han0209aff2013-07-03 15:07:11 -0700605 return -ENOENT;
Paul Mundt317a6102006-09-27 17:13:19 +0900606 }
Paul Mundt063adc72009-04-16 14:12:22 +0900607
roel kluin2641dc92008-09-10 19:34:44 +0200608 rtc->periodic_irq = ret;
Magnus Damm5e084a12009-02-24 22:11:03 +0900609 rtc->carry_irq = platform_get_irq(pdev, 1);
610 rtc->alarm_irq = platform_get_irq(pdev, 2);
Paul Mundt317a6102006-09-27 17:13:19 +0900611
612 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
613 if (unlikely(res == NULL)) {
614 dev_err(&pdev->dev, "No IO resource\n");
Jingoo Han0209aff2013-07-03 15:07:11 -0700615 return -ENOENT;
Paul Mundt317a6102006-09-27 17:13:19 +0900616 }
617
Paul Mundt063adc72009-04-16 14:12:22 +0900618 rtc->regsize = resource_size(res);
Paul Mundt317a6102006-09-27 17:13:19 +0900619
Jingoo Han0209aff2013-07-03 15:07:11 -0700620 rtc->res = devm_request_mem_region(&pdev->dev, res->start,
621 rtc->regsize, pdev->name);
622 if (unlikely(!rtc->res))
623 return -EBUSY;
Paul Mundt317a6102006-09-27 17:13:19 +0900624
Jingoo Han0209aff2013-07-03 15:07:11 -0700625 rtc->regbase = devm_ioremap_nocache(&pdev->dev, rtc->res->start,
626 rtc->regsize);
627 if (unlikely(!rtc->regbase))
628 return -EINVAL;
Paul Mundt317a6102006-09-27 17:13:19 +0900629
Paul Mundt063adc72009-04-16 14:12:22 +0900630 clk_id = pdev->id;
631 /* With a single device, the clock id is still "rtc0" */
632 if (clk_id < 0)
633 clk_id = 0;
634
635 snprintf(clk_name, sizeof(clk_name), "rtc%d", clk_id);
636
Jingoo Han0209aff2013-07-03 15:07:11 -0700637 rtc->clk = devm_clk_get(&pdev->dev, clk_name);
Paul Mundt063adc72009-04-16 14:12:22 +0900638 if (IS_ERR(rtc->clk)) {
639 /*
640 * No error handling for rtc->clk intentionally, not all
641 * platforms will have a unique clock for the RTC, and
642 * the clk API can handle the struct clk pointer being
643 * NULL.
644 */
645 rtc->clk = NULL;
646 }
647
648 clk_enable(rtc->clk);
649
Paul Mundtad89f872007-08-03 14:19:58 +0900650 rtc->capabilities = RTC_DEF_CAPABILITIES;
Jingoo Hane58c18d2013-11-12 15:10:52 -0800651 if (dev_get_platdata(&pdev->dev)) {
652 struct sh_rtc_platform_info *pinfo =
653 dev_get_platdata(&pdev->dev);
Paul Mundtad89f872007-08-03 14:19:58 +0900654
655 /*
656 * Some CPUs have special capabilities in addition to the
657 * default set. Add those in here.
658 */
659 rtc->capabilities |= pinfo->capabilities;
660 }
661
Magnus Damm5e084a12009-02-24 22:11:03 +0900662 if (rtc->carry_irq <= 0) {
663 /* register shared periodic/carry/alarm irq */
Jingoo Han0209aff2013-07-03 15:07:11 -0700664 ret = devm_request_irq(&pdev->dev, rtc->periodic_irq,
665 sh_rtc_shared, 0, "sh-rtc", rtc);
Magnus Damm5e084a12009-02-24 22:11:03 +0900666 if (unlikely(ret)) {
667 dev_err(&pdev->dev,
668 "request IRQ failed with %d, IRQ %d\n", ret,
669 rtc->periodic_irq);
670 goto err_unmap;
671 }
672 } else {
673 /* register periodic/carry/alarm irqs */
Jingoo Han0209aff2013-07-03 15:07:11 -0700674 ret = devm_request_irq(&pdev->dev, rtc->periodic_irq,
675 sh_rtc_periodic, 0, "sh-rtc period", rtc);
Magnus Damm5e084a12009-02-24 22:11:03 +0900676 if (unlikely(ret)) {
677 dev_err(&pdev->dev,
678 "request period IRQ failed with %d, IRQ %d\n",
679 ret, rtc->periodic_irq);
680 goto err_unmap;
681 }
Angelo Castellob420b1a2008-03-06 12:50:53 +0900682
Jingoo Han0209aff2013-07-03 15:07:11 -0700683 ret = devm_request_irq(&pdev->dev, rtc->carry_irq,
684 sh_rtc_interrupt, 0, "sh-rtc carry", rtc);
Magnus Damm5e084a12009-02-24 22:11:03 +0900685 if (unlikely(ret)) {
686 dev_err(&pdev->dev,
687 "request carry IRQ failed with %d, IRQ %d\n",
688 ret, rtc->carry_irq);
Magnus Damm5e084a12009-02-24 22:11:03 +0900689 goto err_unmap;
690 }
Angelo Castellob420b1a2008-03-06 12:50:53 +0900691
Jingoo Han0209aff2013-07-03 15:07:11 -0700692 ret = devm_request_irq(&pdev->dev, rtc->alarm_irq,
693 sh_rtc_alarm, 0, "sh-rtc alarm", rtc);
Magnus Damm5e084a12009-02-24 22:11:03 +0900694 if (unlikely(ret)) {
695 dev_err(&pdev->dev,
696 "request alarm IRQ failed with %d, IRQ %d\n",
697 ret, rtc->alarm_irq);
Magnus Damm5e084a12009-02-24 22:11:03 +0900698 goto err_unmap;
699 }
Angelo Castellob420b1a2008-03-06 12:50:53 +0900700 }
701
Alessandro Zummo5c9740a2009-08-20 13:25:11 +0900702 platform_set_drvdata(pdev, rtc);
703
Magnus Damm9cd88b92009-03-19 10:05:58 +0000704 /* everything disabled by default */
Alessandro Zummo5c9740a2009-08-20 13:25:11 +0900705 sh_rtc_irq_set_freq(&pdev->dev, 0);
706 sh_rtc_irq_set_state(&pdev->dev, 0);
Magnus Damm9cd88b92009-03-19 10:05:58 +0000707 sh_rtc_setaie(&pdev->dev, 0);
708 sh_rtc_setcie(&pdev->dev, 0);
Magnus Dammedf22472009-03-19 10:10:44 +0000709
Jingoo Han0209aff2013-07-03 15:07:11 -0700710 rtc->rtc_dev = devm_rtc_device_register(&pdev->dev, "sh",
Alessandro Zummo5c9740a2009-08-20 13:25:11 +0900711 &sh_rtc_ops, THIS_MODULE);
712 if (IS_ERR(rtc->rtc_dev)) {
713 ret = PTR_ERR(rtc->rtc_dev);
Alessandro Zummo5c9740a2009-08-20 13:25:11 +0900714 goto err_unmap;
715 }
716
717 rtc->rtc_dev->max_user_freq = 256;
718
Magnus Dammedf22472009-03-19 10:10:44 +0000719 /* reset rtc to epoch 0 if time is invalid */
720 if (rtc_read_time(rtc->rtc_dev, &r) < 0) {
721 rtc_time_to_tm(0, &r);
722 rtc_set_time(rtc->rtc_dev, &r);
723 }
724
Magnus Damm7a8fe8e2009-03-19 10:14:41 +0000725 device_init_wakeup(&pdev->dev, 1);
Paul Mundt317a6102006-09-27 17:13:19 +0900726 return 0;
727
Paul Mundt03057942008-04-25 17:58:42 +0900728err_unmap:
Paul Mundt063adc72009-04-16 14:12:22 +0900729 clk_disable(rtc->clk);
Paul Mundt317a6102006-09-27 17:13:19 +0900730
731 return ret;
732}
733
Alessandro Zummo5c9740a2009-08-20 13:25:11 +0900734static int __exit sh_rtc_remove(struct platform_device *pdev)
Paul Mundt317a6102006-09-27 17:13:19 +0900735{
736 struct sh_rtc *rtc = platform_get_drvdata(pdev);
737
Alessandro Zummo5c9740a2009-08-20 13:25:11 +0900738 sh_rtc_irq_set_state(&pdev->dev, 0);
Paul Mundt317a6102006-09-27 17:13:19 +0900739
Paul Mundt317a6102006-09-27 17:13:19 +0900740 sh_rtc_setaie(&pdev->dev, 0);
Magnus Damm9cd88b92009-03-19 10:05:58 +0000741 sh_rtc_setcie(&pdev->dev, 0);
Paul Mundt317a6102006-09-27 17:13:19 +0900742
Paul Mundt063adc72009-04-16 14:12:22 +0900743 clk_disable(rtc->clk);
Paul Mundt317a6102006-09-27 17:13:19 +0900744
745 return 0;
746}
Magnus Dammfaa9fa82009-04-01 14:45:17 +0000747
748static void sh_rtc_set_irq_wake(struct device *dev, int enabled)
749{
750 struct platform_device *pdev = to_platform_device(dev);
751 struct sh_rtc *rtc = platform_get_drvdata(pdev);
752
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200753 irq_set_irq_wake(rtc->periodic_irq, enabled);
Paul Mundt063adc72009-04-16 14:12:22 +0900754
Magnus Dammfaa9fa82009-04-01 14:45:17 +0000755 if (rtc->carry_irq > 0) {
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200756 irq_set_irq_wake(rtc->carry_irq, enabled);
757 irq_set_irq_wake(rtc->alarm_irq, enabled);
Magnus Dammfaa9fa82009-04-01 14:45:17 +0000758 }
Magnus Dammfaa9fa82009-04-01 14:45:17 +0000759}
760
Jingoo Han0ed50542013-04-29 16:20:00 -0700761#ifdef CONFIG_PM_SLEEP
Magnus Dammfaa9fa82009-04-01 14:45:17 +0000762static int sh_rtc_suspend(struct device *dev)
763{
764 if (device_may_wakeup(dev))
765 sh_rtc_set_irq_wake(dev, 1);
766
767 return 0;
768}
769
770static int sh_rtc_resume(struct device *dev)
771{
772 if (device_may_wakeup(dev))
773 sh_rtc_set_irq_wake(dev, 0);
774
775 return 0;
776}
Jingoo Han0ed50542013-04-29 16:20:00 -0700777#endif
Magnus Dammfaa9fa82009-04-01 14:45:17 +0000778
Jingoo Han0ed50542013-04-29 16:20:00 -0700779static SIMPLE_DEV_PM_OPS(sh_rtc_pm_ops, sh_rtc_suspend, sh_rtc_resume);
Magnus Dammfaa9fa82009-04-01 14:45:17 +0000780
Paul Mundt317a6102006-09-27 17:13:19 +0900781static struct platform_driver sh_rtc_platform_driver = {
782 .driver = {
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900783 .name = DRV_NAME,
Jingoo Han0ed50542013-04-29 16:20:00 -0700784 .pm = &sh_rtc_pm_ops,
Paul Mundt317a6102006-09-27 17:13:19 +0900785 },
Alessandro Zummo5c9740a2009-08-20 13:25:11 +0900786 .remove = __exit_p(sh_rtc_remove),
Paul Mundt317a6102006-09-27 17:13:19 +0900787};
788
Jingoo Handeed5a92013-04-29 16:18:51 -0700789module_platform_driver_probe(sh_rtc_platform_driver, sh_rtc_probe);
Paul Mundt317a6102006-09-27 17:13:19 +0900790
791MODULE_DESCRIPTION("SuperH on-chip RTC driver");
Angelo Castellob420b1a2008-03-06 12:50:53 +0900792MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>, "
793 "Jamie Lenehan <lenehan@twibble.org>, "
794 "Angelo Castello <angelo.castello@st.com>");
Paul Mundt317a6102006-09-27 17:13:19 +0900795MODULE_LICENSE("GPL");
Kay Sieversad28a072008-04-10 21:29:25 -0700796MODULE_ALIAS("platform:" DRV_NAME);