blob: e55a7635ae5f6d651f5b272d25b4619e66060c88 [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"
Alessandro Zummo5c9740a2009-08-20 13:25:11 +090033#define DRV_VERSION "0.2.3"
Paul Mundt317a6102006-09-27 17:13:19 +090034
35#define RTC_REG(r) ((r) * rtc_reg_size)
36
Jamie Lenehan31ccb082006-12-07 17:23:50 +090037#define R64CNT RTC_REG(0)
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +090038
39#define RSECCNT RTC_REG(1) /* RTC sec */
40#define RMINCNT RTC_REG(2) /* RTC min */
41#define RHRCNT RTC_REG(3) /* RTC hour */
42#define RWKCNT RTC_REG(4) /* RTC week */
43#define RDAYCNT RTC_REG(5) /* RTC day */
44#define RMONCNT RTC_REG(6) /* RTC month */
45#define RYRCNT RTC_REG(7) /* RTC year */
46#define RSECAR RTC_REG(8) /* ALARM sec */
47#define RMINAR RTC_REG(9) /* ALARM min */
48#define RHRAR RTC_REG(10) /* ALARM hour */
49#define RWKAR RTC_REG(11) /* ALARM week */
50#define RDAYAR RTC_REG(12) /* ALARM day */
51#define RMONAR RTC_REG(13) /* ALARM month */
52#define RCR1 RTC_REG(14) /* Control */
53#define RCR2 RTC_REG(15) /* Control */
54
Paul Mundtff1b7502007-11-26 17:56:31 +090055/*
56 * Note on RYRAR and RCR3: Up until this point most of the register
57 * definitions are consistent across all of the available parts. However,
58 * the placement of the optional RYRAR and RCR3 (the RYRAR control
59 * register used to control RYRCNT/RYRAR compare) varies considerably
60 * across various parts, occasionally being mapped in to a completely
61 * unrelated address space. For proper RYRAR support a separate resource
62 * would have to be handed off, but as this is purely optional in
63 * practice, we simply opt not to support it, thereby keeping the code
64 * quite a bit more simplified.
65 */
66
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +090067/* ALARM Bits - or with BCD encoded value */
68#define AR_ENB 0x80 /* Enable for alarm cmp */
Paul Mundt317a6102006-09-27 17:13:19 +090069
Angelo Castellob420b1a2008-03-06 12:50:53 +090070/* Period Bits */
71#define PF_HP 0x100 /* Enable Half Period to support 8,32,128Hz */
72#define PF_COUNT 0x200 /* Half periodic counter */
73#define PF_OXS 0x400 /* Periodic One x Second */
74#define PF_KOU 0x800 /* Kernel or User periodic request 1=kernel */
75#define PF_MASK 0xf00
76
Paul Mundt317a6102006-09-27 17:13:19 +090077/* RCR1 Bits */
78#define RCR1_CF 0x80 /* Carry Flag */
79#define RCR1_CIE 0x10 /* Carry Interrupt Enable */
80#define RCR1_AIE 0x08 /* Alarm Interrupt Enable */
81#define RCR1_AF 0x01 /* Alarm Flag */
82
83/* RCR2 Bits */
84#define RCR2_PEF 0x80 /* PEriodic interrupt Flag */
85#define RCR2_PESMASK 0x70 /* Periodic interrupt Set */
86#define RCR2_RTCEN 0x08 /* ENable RTC */
87#define RCR2_ADJ 0x04 /* ADJustment (30-second) */
88#define RCR2_RESET 0x02 /* Reset bit */
89#define RCR2_START 0x01 /* Start bit */
90
91struct sh_rtc {
Paul Mundt063adc72009-04-16 14:12:22 +090092 void __iomem *regbase;
93 unsigned long regsize;
94 struct resource *res;
95 int alarm_irq;
96 int periodic_irq;
97 int carry_irq;
98 struct clk *clk;
99 struct rtc_device *rtc_dev;
100 spinlock_t lock;
101 unsigned long capabilities; /* See asm/rtc.h for cap bits */
102 unsigned short periodic_freq;
Paul Mundt317a6102006-09-27 17:13:19 +0900103};
104
Magnus Damm5e084a12009-02-24 22:11:03 +0900105static int __sh_rtc_interrupt(struct sh_rtc *rtc)
Paul Mundt317a6102006-09-27 17:13:19 +0900106{
Magnus Damm5e084a12009-02-24 22:11:03 +0900107 unsigned int tmp, pending;
Paul Mundt317a6102006-09-27 17:13:19 +0900108
109 tmp = readb(rtc->regbase + RCR1);
Magnus Damm5e084a12009-02-24 22:11:03 +0900110 pending = tmp & RCR1_CF;
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900111 tmp &= ~RCR1_CF;
Paul Mundt317a6102006-09-27 17:13:19 +0900112 writeb(tmp, rtc->regbase + RCR1);
113
Angelo Castellob420b1a2008-03-06 12:50:53 +0900114 /* Users have requested One x Second IRQ */
Magnus Damm5e084a12009-02-24 22:11:03 +0900115 if (pending && rtc->periodic_freq & PF_OXS)
Angelo Castellob420b1a2008-03-06 12:50:53 +0900116 rtc_update_irq(rtc->rtc_dev, 1, RTC_UF | RTC_IRQF);
Paul Mundt317a6102006-09-27 17:13:19 +0900117
Magnus Damm5e084a12009-02-24 22:11:03 +0900118 return pending;
Paul Mundt317a6102006-09-27 17:13:19 +0900119}
120
Magnus Damm5e084a12009-02-24 22:11:03 +0900121static int __sh_rtc_alarm(struct sh_rtc *rtc)
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900122{
Magnus Damm5e084a12009-02-24 22:11:03 +0900123 unsigned int tmp, pending;
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900124
125 tmp = readb(rtc->regbase + RCR1);
Magnus Damm5e084a12009-02-24 22:11:03 +0900126 pending = tmp & RCR1_AF;
Angelo Castellob420b1a2008-03-06 12:50:53 +0900127 tmp &= ~(RCR1_AF | RCR1_AIE);
Magnus Damm5e084a12009-02-24 22:11:03 +0900128 writeb(tmp, rtc->regbase + RCR1);
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900129
Magnus Damm5e084a12009-02-24 22:11:03 +0900130 if (pending)
131 rtc_update_irq(rtc->rtc_dev, 1, RTC_AF | RTC_IRQF);
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900132
Magnus Damm5e084a12009-02-24 22:11:03 +0900133 return pending;
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900134}
135
Magnus Damm5e084a12009-02-24 22:11:03 +0900136static int __sh_rtc_periodic(struct sh_rtc *rtc)
Paul Mundt317a6102006-09-27 17:13:19 +0900137{
Angelo Castellob420b1a2008-03-06 12:50:53 +0900138 struct rtc_device *rtc_dev = rtc->rtc_dev;
Magnus Damm5e084a12009-02-24 22:11:03 +0900139 struct rtc_task *irq_task;
140 unsigned int tmp, pending;
Paul Mundt317a6102006-09-27 17:13:19 +0900141
Angelo Castellob420b1a2008-03-06 12:50:53 +0900142 tmp = readb(rtc->regbase + RCR2);
Magnus Damm5e084a12009-02-24 22:11:03 +0900143 pending = tmp & RCR2_PEF;
Angelo Castellob420b1a2008-03-06 12:50:53 +0900144 tmp &= ~RCR2_PEF;
145 writeb(tmp, rtc->regbase + RCR2);
146
Magnus Damm5e084a12009-02-24 22:11:03 +0900147 if (!pending)
148 return 0;
149
Angelo Castellob420b1a2008-03-06 12:50:53 +0900150 /* Half period enabled than one skipped and the next notified */
151 if ((rtc->periodic_freq & PF_HP) && (rtc->periodic_freq & PF_COUNT))
152 rtc->periodic_freq &= ~PF_COUNT;
153 else {
154 if (rtc->periodic_freq & PF_HP)
155 rtc->periodic_freq |= PF_COUNT;
156 if (rtc->periodic_freq & PF_KOU) {
157 spin_lock(&rtc_dev->irq_task_lock);
Magnus Damm5e084a12009-02-24 22:11:03 +0900158 irq_task = rtc_dev->irq_task;
159 if (irq_task)
160 irq_task->func(irq_task->private_data);
Angelo Castellob420b1a2008-03-06 12:50:53 +0900161 spin_unlock(&rtc_dev->irq_task_lock);
162 } else
163 rtc_update_irq(rtc->rtc_dev, 1, RTC_PF | RTC_IRQF);
164 }
Paul Mundt317a6102006-09-27 17:13:19 +0900165
Magnus Damm5e084a12009-02-24 22:11:03 +0900166 return pending;
167}
168
169static irqreturn_t sh_rtc_interrupt(int irq, void *dev_id)
170{
171 struct sh_rtc *rtc = dev_id;
172 int ret;
173
174 spin_lock(&rtc->lock);
175 ret = __sh_rtc_interrupt(rtc);
Paul Mundt317a6102006-09-27 17:13:19 +0900176 spin_unlock(&rtc->lock);
177
Magnus Damm5e084a12009-02-24 22:11:03 +0900178 return IRQ_RETVAL(ret);
179}
180
181static irqreturn_t sh_rtc_alarm(int irq, void *dev_id)
182{
183 struct sh_rtc *rtc = dev_id;
184 int ret;
185
186 spin_lock(&rtc->lock);
187 ret = __sh_rtc_alarm(rtc);
188 spin_unlock(&rtc->lock);
189
190 return IRQ_RETVAL(ret);
191}
192
193static irqreturn_t sh_rtc_periodic(int irq, void *dev_id)
194{
195 struct sh_rtc *rtc = dev_id;
196 int ret;
197
198 spin_lock(&rtc->lock);
199 ret = __sh_rtc_periodic(rtc);
200 spin_unlock(&rtc->lock);
201
202 return IRQ_RETVAL(ret);
203}
204
205static irqreturn_t sh_rtc_shared(int irq, void *dev_id)
206{
207 struct sh_rtc *rtc = dev_id;
208 int ret;
209
210 spin_lock(&rtc->lock);
211 ret = __sh_rtc_interrupt(rtc);
212 ret |= __sh_rtc_alarm(rtc);
213 ret |= __sh_rtc_periodic(rtc);
214 spin_unlock(&rtc->lock);
215
216 return IRQ_RETVAL(ret);
Paul Mundt317a6102006-09-27 17:13:19 +0900217}
218
Alessandro Zummo5c9740a2009-08-20 13:25:11 +0900219static int sh_rtc_irq_set_state(struct device *dev, int enable)
Paul Mundt317a6102006-09-27 17:13:19 +0900220{
221 struct sh_rtc *rtc = dev_get_drvdata(dev);
222 unsigned int tmp;
223
224 spin_lock_irq(&rtc->lock);
225
226 tmp = readb(rtc->regbase + RCR2);
227
228 if (enable) {
Alessandro Zummo5c9740a2009-08-20 13:25:11 +0900229 rtc->periodic_freq |= PF_KOU;
Angelo Castellob420b1a2008-03-06 12:50:53 +0900230 tmp &= ~RCR2_PEF; /* Clear PES bit */
231 tmp |= (rtc->periodic_freq & ~PF_HP); /* Set PES2-0 */
Alessandro Zummo5c9740a2009-08-20 13:25:11 +0900232 } else {
233 rtc->periodic_freq &= ~PF_KOU;
Paul Mundt317a6102006-09-27 17:13:19 +0900234 tmp &= ~(RCR2_PESMASK | RCR2_PEF);
Alessandro Zummo5c9740a2009-08-20 13:25:11 +0900235 }
Paul Mundt317a6102006-09-27 17:13:19 +0900236
237 writeb(tmp, rtc->regbase + RCR2);
238
239 spin_unlock_irq(&rtc->lock);
Alessandro Zummo5c9740a2009-08-20 13:25:11 +0900240
241 return 0;
Paul Mundt317a6102006-09-27 17:13:19 +0900242}
243
Alessandro Zummo5c9740a2009-08-20 13:25:11 +0900244static int sh_rtc_irq_set_freq(struct device *dev, int freq)
Angelo Castellob420b1a2008-03-06 12:50:53 +0900245{
246 struct sh_rtc *rtc = dev_get_drvdata(dev);
247 int tmp, ret = 0;
248
249 spin_lock_irq(&rtc->lock);
250 tmp = rtc->periodic_freq & PF_MASK;
251
252 switch (freq) {
253 case 0:
254 rtc->periodic_freq = 0x00;
255 break;
256 case 1:
257 rtc->periodic_freq = 0x60;
258 break;
259 case 2:
260 rtc->periodic_freq = 0x50;
261 break;
262 case 4:
263 rtc->periodic_freq = 0x40;
264 break;
265 case 8:
266 rtc->periodic_freq = 0x30 | PF_HP;
267 break;
268 case 16:
269 rtc->periodic_freq = 0x30;
270 break;
271 case 32:
272 rtc->periodic_freq = 0x20 | PF_HP;
273 break;
274 case 64:
275 rtc->periodic_freq = 0x20;
276 break;
277 case 128:
278 rtc->periodic_freq = 0x10 | PF_HP;
279 break;
280 case 256:
281 rtc->periodic_freq = 0x10;
282 break;
283 default:
284 ret = -ENOTSUPP;
285 }
286
Paul Mundt1043bf52009-09-09 12:13:01 +0900287 if (ret == 0)
Angelo Castellob420b1a2008-03-06 12:50:53 +0900288 rtc->periodic_freq |= tmp;
Angelo Castellob420b1a2008-03-06 12:50:53 +0900289
290 spin_unlock_irq(&rtc->lock);
291 return ret;
292}
293
Paul Mundt317a6102006-09-27 17:13:19 +0900294static inline void sh_rtc_setaie(struct device *dev, unsigned int enable)
295{
296 struct sh_rtc *rtc = dev_get_drvdata(dev);
297 unsigned int tmp;
298
299 spin_lock_irq(&rtc->lock);
300
301 tmp = readb(rtc->regbase + RCR1);
302
Paul Mundt063adc72009-04-16 14:12:22 +0900303 if (enable)
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900304 tmp |= RCR1_AIE;
Paul Mundt063adc72009-04-16 14:12:22 +0900305 else
306 tmp &= ~RCR1_AIE;
Paul Mundt317a6102006-09-27 17:13:19 +0900307
308 writeb(tmp, rtc->regbase + RCR1);
309
310 spin_unlock_irq(&rtc->lock);
311}
312
Paul Mundt317a6102006-09-27 17:13:19 +0900313static int sh_rtc_proc(struct device *dev, struct seq_file *seq)
314{
315 struct sh_rtc *rtc = dev_get_drvdata(dev);
316 unsigned int tmp;
317
318 tmp = readb(rtc->regbase + RCR1);
Angelo Castellob420b1a2008-03-06 12:50:53 +0900319 seq_printf(seq, "carry_IRQ\t: %s\n", (tmp & RCR1_CIE) ? "yes" : "no");
Paul Mundt317a6102006-09-27 17:13:19 +0900320
321 tmp = readb(rtc->regbase + RCR2);
322 seq_printf(seq, "periodic_IRQ\t: %s\n",
Angelo Castellob420b1a2008-03-06 12:50:53 +0900323 (tmp & RCR2_PESMASK) ? "yes" : "no");
Paul Mundt317a6102006-09-27 17:13:19 +0900324
325 return 0;
326}
327
Magnus Damm9cd88b92009-03-19 10:05:58 +0000328static inline void sh_rtc_setcie(struct device *dev, unsigned int enable)
329{
330 struct sh_rtc *rtc = dev_get_drvdata(dev);
331 unsigned int tmp;
332
333 spin_lock_irq(&rtc->lock);
334
335 tmp = readb(rtc->regbase + RCR1);
336
337 if (!enable)
338 tmp &= ~RCR1_CIE;
339 else
340 tmp |= RCR1_CIE;
341
342 writeb(tmp, rtc->regbase + RCR1);
343
344 spin_unlock_irq(&rtc->lock);
345}
346
John Stultz16380c12011-02-02 17:02:41 -0800347static int sh_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
348{
349 sh_rtc_setaie(dev, enabled);
350 return 0;
351}
352
Paul Mundt317a6102006-09-27 17:13:19 +0900353static int sh_rtc_read_time(struct device *dev, struct rtc_time *tm)
354{
355 struct platform_device *pdev = to_platform_device(dev);
356 struct sh_rtc *rtc = platform_get_drvdata(pdev);
357 unsigned int sec128, sec2, yr, yr100, cf_bit;
358
359 do {
360 unsigned int tmp;
361
362 spin_lock_irq(&rtc->lock);
363
364 tmp = readb(rtc->regbase + RCR1);
365 tmp &= ~RCR1_CF; /* Clear CF-bit */
366 tmp |= RCR1_CIE;
367 writeb(tmp, rtc->regbase + RCR1);
368
369 sec128 = readb(rtc->regbase + R64CNT);
370
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700371 tm->tm_sec = bcd2bin(readb(rtc->regbase + RSECCNT));
372 tm->tm_min = bcd2bin(readb(rtc->regbase + RMINCNT));
373 tm->tm_hour = bcd2bin(readb(rtc->regbase + RHRCNT));
374 tm->tm_wday = bcd2bin(readb(rtc->regbase + RWKCNT));
375 tm->tm_mday = bcd2bin(readb(rtc->regbase + RDAYCNT));
376 tm->tm_mon = bcd2bin(readb(rtc->regbase + RMONCNT)) - 1;
Paul Mundt317a6102006-09-27 17:13:19 +0900377
Paul Mundtad89f872007-08-03 14:19:58 +0900378 if (rtc->capabilities & RTC_CAP_4_DIGIT_YEAR) {
379 yr = readw(rtc->regbase + RYRCNT);
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700380 yr100 = bcd2bin(yr >> 8);
Paul Mundtad89f872007-08-03 14:19:58 +0900381 yr &= 0xff;
382 } else {
383 yr = readb(rtc->regbase + RYRCNT);
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700384 yr100 = bcd2bin((yr == 0x99) ? 0x19 : 0x20);
Paul Mundtad89f872007-08-03 14:19:58 +0900385 }
Paul Mundt317a6102006-09-27 17:13:19 +0900386
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700387 tm->tm_year = (yr100 * 100 + bcd2bin(yr)) - 1900;
Paul Mundt317a6102006-09-27 17:13:19 +0900388
389 sec2 = readb(rtc->regbase + R64CNT);
390 cf_bit = readb(rtc->regbase + RCR1) & RCR1_CF;
391
392 spin_unlock_irq(&rtc->lock);
393 } while (cf_bit != 0 || ((sec128 ^ sec2) & RTC_BIT_INVERTED) != 0);
394
395#if RTC_BIT_INVERTED != 0
396 if ((sec128 & RTC_BIT_INVERTED))
397 tm->tm_sec--;
398#endif
399
Magnus Damm9cd88b92009-03-19 10:05:58 +0000400 /* only keep the carry interrupt enabled if UIE is on */
401 if (!(rtc->periodic_freq & PF_OXS))
402 sh_rtc_setcie(dev, 0);
403
Paul Mundt435c55d2007-05-08 11:56:27 +0900404 dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
Paul Mundt317a6102006-09-27 17:13:19 +0900405 "mday=%d, mon=%d, year=%d, wday=%d\n",
Harvey Harrison2a4e2b82008-04-28 02:12:00 -0700406 __func__,
Paul Mundt317a6102006-09-27 17:13:19 +0900407 tm->tm_sec, tm->tm_min, tm->tm_hour,
Jamie Lenehana1614792006-12-08 14:49:30 +0900408 tm->tm_mday, tm->tm_mon + 1, tm->tm_year, tm->tm_wday);
Paul Mundt317a6102006-09-27 17:13:19 +0900409
Magnus Dammedf22472009-03-19 10:10:44 +0000410 return rtc_valid_tm(tm);
Paul Mundt317a6102006-09-27 17:13:19 +0900411}
412
413static int sh_rtc_set_time(struct device *dev, struct rtc_time *tm)
414{
415 struct platform_device *pdev = to_platform_device(dev);
416 struct sh_rtc *rtc = platform_get_drvdata(pdev);
417 unsigned int tmp;
418 int year;
419
420 spin_lock_irq(&rtc->lock);
421
422 /* Reset pre-scaler & stop RTC */
423 tmp = readb(rtc->regbase + RCR2);
424 tmp |= RCR2_RESET;
Markus Brunner699bc662007-07-26 17:31:28 +0900425 tmp &= ~RCR2_START;
Paul Mundt317a6102006-09-27 17:13:19 +0900426 writeb(tmp, rtc->regbase + RCR2);
427
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700428 writeb(bin2bcd(tm->tm_sec), rtc->regbase + RSECCNT);
429 writeb(bin2bcd(tm->tm_min), rtc->regbase + RMINCNT);
430 writeb(bin2bcd(tm->tm_hour), rtc->regbase + RHRCNT);
431 writeb(bin2bcd(tm->tm_wday), rtc->regbase + RWKCNT);
432 writeb(bin2bcd(tm->tm_mday), rtc->regbase + RDAYCNT);
433 writeb(bin2bcd(tm->tm_mon + 1), rtc->regbase + RMONCNT);
Paul Mundt317a6102006-09-27 17:13:19 +0900434
Paul Mundtad89f872007-08-03 14:19:58 +0900435 if (rtc->capabilities & RTC_CAP_4_DIGIT_YEAR) {
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700436 year = (bin2bcd((tm->tm_year + 1900) / 100) << 8) |
437 bin2bcd(tm->tm_year % 100);
Paul Mundtad89f872007-08-03 14:19:58 +0900438 writew(year, rtc->regbase + RYRCNT);
439 } else {
440 year = tm->tm_year % 100;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700441 writeb(bin2bcd(year), rtc->regbase + RYRCNT);
Paul Mundtad89f872007-08-03 14:19:58 +0900442 }
Paul Mundt317a6102006-09-27 17:13:19 +0900443
444 /* Start RTC */
445 tmp = readb(rtc->regbase + RCR2);
446 tmp &= ~RCR2_RESET;
447 tmp |= RCR2_RTCEN | RCR2_START;
448 writeb(tmp, rtc->regbase + RCR2);
449
450 spin_unlock_irq(&rtc->lock);
451
452 return 0;
453}
454
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900455static inline int sh_rtc_read_alarm_value(struct sh_rtc *rtc, int reg_off)
456{
457 unsigned int byte;
458 int value = 0xff; /* return 0xff for ignored values */
459
460 byte = readb(rtc->regbase + reg_off);
461 if (byte & AR_ENB) {
462 byte &= ~AR_ENB; /* strip the enable bit */
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700463 value = bcd2bin(byte);
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900464 }
465
466 return value;
467}
468
469static int sh_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
470{
471 struct platform_device *pdev = to_platform_device(dev);
472 struct sh_rtc *rtc = platform_get_drvdata(pdev);
Angelo Castellob420b1a2008-03-06 12:50:53 +0900473 struct rtc_time *tm = &wkalrm->time;
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900474
475 spin_lock_irq(&rtc->lock);
476
477 tm->tm_sec = sh_rtc_read_alarm_value(rtc, RSECAR);
478 tm->tm_min = sh_rtc_read_alarm_value(rtc, RMINAR);
479 tm->tm_hour = sh_rtc_read_alarm_value(rtc, RHRAR);
480 tm->tm_wday = sh_rtc_read_alarm_value(rtc, RWKAR);
481 tm->tm_mday = sh_rtc_read_alarm_value(rtc, RDAYAR);
482 tm->tm_mon = sh_rtc_read_alarm_value(rtc, RMONAR);
483 if (tm->tm_mon > 0)
484 tm->tm_mon -= 1; /* RTC is 1-12, tm_mon is 0-11 */
485 tm->tm_year = 0xffff;
486
David Brownell0d103e92007-01-10 23:15:32 -0800487 wkalrm->enabled = (readb(rtc->regbase + RCR1) & RCR1_AIE) ? 1 : 0;
488
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900489 spin_unlock_irq(&rtc->lock);
490
491 return 0;
492}
493
494static inline void sh_rtc_write_alarm_value(struct sh_rtc *rtc,
495 int value, int reg_off)
496{
497 /* < 0 for a value that is ignored */
498 if (value < 0)
499 writeb(0, rtc->regbase + reg_off);
500 else
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700501 writeb(bin2bcd(value) | AR_ENB, rtc->regbase + reg_off);
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900502}
503
Angelo Castellob420b1a2008-03-06 12:50:53 +0900504static int sh_rtc_check_alarm(struct rtc_time *tm)
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900505{
506 /*
507 * The original rtc says anything > 0xc0 is "don't care" or "match
508 * all" - most users use 0xff but rtc-dev uses -1 for the same thing.
509 * The original rtc doesn't support years - some things use -1 and
510 * some 0xffff. We use -1 to make out tests easier.
511 */
512 if (tm->tm_year == 0xffff)
513 tm->tm_year = -1;
514 if (tm->tm_mon >= 0xff)
515 tm->tm_mon = -1;
516 if (tm->tm_mday >= 0xff)
517 tm->tm_mday = -1;
518 if (tm->tm_wday >= 0xff)
519 tm->tm_wday = -1;
520 if (tm->tm_hour >= 0xff)
521 tm->tm_hour = -1;
522 if (tm->tm_min >= 0xff)
523 tm->tm_min = -1;
524 if (tm->tm_sec >= 0xff)
525 tm->tm_sec = -1;
526
527 if (tm->tm_year > 9999 ||
528 tm->tm_mon >= 12 ||
529 tm->tm_mday == 0 || tm->tm_mday >= 32 ||
530 tm->tm_wday >= 7 ||
531 tm->tm_hour >= 24 ||
532 tm->tm_min >= 60 ||
533 tm->tm_sec >= 60)
534 return -EINVAL;
535
536 return 0;
537}
538
539static int sh_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
540{
541 struct platform_device *pdev = to_platform_device(dev);
542 struct sh_rtc *rtc = platform_get_drvdata(pdev);
543 unsigned int rcr1;
544 struct rtc_time *tm = &wkalrm->time;
545 int mon, err;
546
547 err = sh_rtc_check_alarm(tm);
548 if (unlikely(err < 0))
549 return err;
550
551 spin_lock_irq(&rtc->lock);
552
Jamie Lenehan15c945c2007-01-22 20:40:41 -0800553 /* disable alarm interrupt and clear the alarm flag */
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900554 rcr1 = readb(rtc->regbase + RCR1);
Angelo Castellob420b1a2008-03-06 12:50:53 +0900555 rcr1 &= ~(RCR1_AF | RCR1_AIE);
Jamie Lenehan15c945c2007-01-22 20:40:41 -0800556 writeb(rcr1, rtc->regbase + RCR1);
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900557
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900558 /* set alarm time */
559 sh_rtc_write_alarm_value(rtc, tm->tm_sec, RSECAR);
560 sh_rtc_write_alarm_value(rtc, tm->tm_min, RMINAR);
561 sh_rtc_write_alarm_value(rtc, tm->tm_hour, RHRAR);
562 sh_rtc_write_alarm_value(rtc, tm->tm_wday, RWKAR);
563 sh_rtc_write_alarm_value(rtc, tm->tm_mday, RDAYAR);
564 mon = tm->tm_mon;
565 if (mon >= 0)
566 mon += 1;
567 sh_rtc_write_alarm_value(rtc, mon, RMONAR);
568
Jamie Lenehan15c945c2007-01-22 20:40:41 -0800569 if (wkalrm->enabled) {
570 rcr1 |= RCR1_AIE;
571 writeb(rcr1, rtc->regbase + RCR1);
572 }
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900573
574 spin_unlock_irq(&rtc->lock);
575
576 return 0;
577}
578
Paul Mundt317a6102006-09-27 17:13:19 +0900579static struct rtc_class_ops sh_rtc_ops = {
Paul Mundt317a6102006-09-27 17:13:19 +0900580 .read_time = sh_rtc_read_time,
581 .set_time = sh_rtc_set_time,
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900582 .read_alarm = sh_rtc_read_alarm,
583 .set_alarm = sh_rtc_set_alarm,
Paul Mundt317a6102006-09-27 17:13:19 +0900584 .proc = sh_rtc_proc,
John Stultz16380c12011-02-02 17:02:41 -0800585 .alarm_irq_enable = sh_rtc_alarm_irq_enable,
Paul Mundt317a6102006-09-27 17:13:19 +0900586};
587
Alessandro Zummo5c9740a2009-08-20 13:25:11 +0900588static int __init sh_rtc_probe(struct platform_device *pdev)
Paul Mundt317a6102006-09-27 17:13:19 +0900589{
590 struct sh_rtc *rtc;
591 struct resource *res;
Magnus Dammedf22472009-03-19 10:10:44 +0000592 struct rtc_time r;
Paul Mundt063adc72009-04-16 14:12:22 +0900593 char clk_name[6];
594 int clk_id, ret;
Paul Mundt317a6102006-09-27 17:13:19 +0900595
596 rtc = kzalloc(sizeof(struct sh_rtc), GFP_KERNEL);
597 if (unlikely(!rtc))
598 return -ENOMEM;
599
600 spin_lock_init(&rtc->lock);
601
Angelo Castellob420b1a2008-03-06 12:50:53 +0900602 /* get periodic/carry/alarm irqs */
roel kluin2641dc92008-09-10 19:34:44 +0200603 ret = platform_get_irq(pdev, 0);
Anton Vorontsov2fac6672009-01-06 14:42:11 -0800604 if (unlikely(ret <= 0)) {
roel kluin2641dc92008-09-10 19:34:44 +0200605 ret = -ENOENT;
Magnus Damm5e084a12009-02-24 22:11:03 +0900606 dev_err(&pdev->dev, "No IRQ resource\n");
Paul Mundt317a6102006-09-27 17:13:19 +0900607 goto err_badres;
608 }
Paul Mundt063adc72009-04-16 14:12:22 +0900609
roel kluin2641dc92008-09-10 19:34:44 +0200610 rtc->periodic_irq = ret;
Magnus Damm5e084a12009-02-24 22:11:03 +0900611 rtc->carry_irq = platform_get_irq(pdev, 1);
612 rtc->alarm_irq = platform_get_irq(pdev, 2);
Paul Mundt317a6102006-09-27 17:13:19 +0900613
614 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
615 if (unlikely(res == NULL)) {
roel kluin2641dc92008-09-10 19:34:44 +0200616 ret = -ENOENT;
Paul Mundt317a6102006-09-27 17:13:19 +0900617 dev_err(&pdev->dev, "No IO resource\n");
618 goto err_badres;
619 }
620
Paul Mundt063adc72009-04-16 14:12:22 +0900621 rtc->regsize = resource_size(res);
Paul Mundt317a6102006-09-27 17:13:19 +0900622
623 rtc->res = request_mem_region(res->start, rtc->regsize, pdev->name);
624 if (unlikely(!rtc->res)) {
625 ret = -EBUSY;
626 goto err_badres;
627 }
628
Paul Mundt03057942008-04-25 17:58:42 +0900629 rtc->regbase = ioremap_nocache(rtc->res->start, rtc->regsize);
Paul Mundt317a6102006-09-27 17:13:19 +0900630 if (unlikely(!rtc->regbase)) {
631 ret = -EINVAL;
632 goto err_badmap;
633 }
634
Paul Mundt063adc72009-04-16 14:12:22 +0900635 clk_id = pdev->id;
636 /* With a single device, the clock id is still "rtc0" */
637 if (clk_id < 0)
638 clk_id = 0;
639
640 snprintf(clk_name, sizeof(clk_name), "rtc%d", clk_id);
641
642 rtc->clk = clk_get(&pdev->dev, clk_name);
643 if (IS_ERR(rtc->clk)) {
644 /*
645 * No error handling for rtc->clk intentionally, not all
646 * platforms will have a unique clock for the RTC, and
647 * the clk API can handle the struct clk pointer being
648 * NULL.
649 */
650 rtc->clk = NULL;
651 }
652
653 clk_enable(rtc->clk);
654
Paul Mundtad89f872007-08-03 14:19:58 +0900655 rtc->capabilities = RTC_DEF_CAPABILITIES;
656 if (pdev->dev.platform_data) {
657 struct sh_rtc_platform_info *pinfo = pdev->dev.platform_data;
658
659 /*
660 * Some CPUs have special capabilities in addition to the
661 * default set. Add those in here.
662 */
663 rtc->capabilities |= pinfo->capabilities;
664 }
665
Magnus Damm5e084a12009-02-24 22:11:03 +0900666 if (rtc->carry_irq <= 0) {
667 /* register shared periodic/carry/alarm irq */
668 ret = request_irq(rtc->periodic_irq, sh_rtc_shared,
Yong Zhang2f6e5f92012-03-23 15:02:34 -0700669 0, "sh-rtc", rtc);
Magnus Damm5e084a12009-02-24 22:11:03 +0900670 if (unlikely(ret)) {
671 dev_err(&pdev->dev,
672 "request IRQ failed with %d, IRQ %d\n", ret,
673 rtc->periodic_irq);
674 goto err_unmap;
675 }
676 } else {
677 /* register periodic/carry/alarm irqs */
678 ret = request_irq(rtc->periodic_irq, sh_rtc_periodic,
Yong Zhang2f6e5f92012-03-23 15:02:34 -0700679 0, "sh-rtc period", rtc);
Magnus Damm5e084a12009-02-24 22:11:03 +0900680 if (unlikely(ret)) {
681 dev_err(&pdev->dev,
682 "request period IRQ failed with %d, IRQ %d\n",
683 ret, rtc->periodic_irq);
684 goto err_unmap;
685 }
Angelo Castellob420b1a2008-03-06 12:50:53 +0900686
Magnus Damm5e084a12009-02-24 22:11:03 +0900687 ret = request_irq(rtc->carry_irq, sh_rtc_interrupt,
Yong Zhang2f6e5f92012-03-23 15:02:34 -0700688 0, "sh-rtc carry", rtc);
Magnus Damm5e084a12009-02-24 22:11:03 +0900689 if (unlikely(ret)) {
690 dev_err(&pdev->dev,
691 "request carry IRQ failed with %d, IRQ %d\n",
692 ret, rtc->carry_irq);
693 free_irq(rtc->periodic_irq, rtc);
694 goto err_unmap;
695 }
Angelo Castellob420b1a2008-03-06 12:50:53 +0900696
Magnus Damm5e084a12009-02-24 22:11:03 +0900697 ret = request_irq(rtc->alarm_irq, sh_rtc_alarm,
Yong Zhang2f6e5f92012-03-23 15:02:34 -0700698 0, "sh-rtc alarm", rtc);
Magnus Damm5e084a12009-02-24 22:11:03 +0900699 if (unlikely(ret)) {
700 dev_err(&pdev->dev,
701 "request alarm IRQ failed with %d, IRQ %d\n",
702 ret, rtc->alarm_irq);
703 free_irq(rtc->carry_irq, rtc);
704 free_irq(rtc->periodic_irq, rtc);
705 goto err_unmap;
706 }
Angelo Castellob420b1a2008-03-06 12:50:53 +0900707 }
708
Alessandro Zummo5c9740a2009-08-20 13:25:11 +0900709 platform_set_drvdata(pdev, rtc);
710
Magnus Damm9cd88b92009-03-19 10:05:58 +0000711 /* everything disabled by default */
Alessandro Zummo5c9740a2009-08-20 13:25:11 +0900712 sh_rtc_irq_set_freq(&pdev->dev, 0);
713 sh_rtc_irq_set_state(&pdev->dev, 0);
Magnus Damm9cd88b92009-03-19 10:05:58 +0000714 sh_rtc_setaie(&pdev->dev, 0);
715 sh_rtc_setcie(&pdev->dev, 0);
Magnus Dammedf22472009-03-19 10:10:44 +0000716
Alessandro Zummo5c9740a2009-08-20 13:25:11 +0900717 rtc->rtc_dev = rtc_device_register("sh", &pdev->dev,
718 &sh_rtc_ops, THIS_MODULE);
719 if (IS_ERR(rtc->rtc_dev)) {
720 ret = PTR_ERR(rtc->rtc_dev);
721 free_irq(rtc->periodic_irq, rtc);
722 free_irq(rtc->carry_irq, rtc);
723 free_irq(rtc->alarm_irq, rtc);
724 goto err_unmap;
725 }
726
727 rtc->rtc_dev->max_user_freq = 256;
728
Magnus Dammedf22472009-03-19 10:10:44 +0000729 /* reset rtc to epoch 0 if time is invalid */
730 if (rtc_read_time(rtc->rtc_dev, &r) < 0) {
731 rtc_time_to_tm(0, &r);
732 rtc_set_time(rtc->rtc_dev, &r);
733 }
734
Magnus Damm7a8fe8e2009-03-19 10:14:41 +0000735 device_init_wakeup(&pdev->dev, 1);
Paul Mundt317a6102006-09-27 17:13:19 +0900736 return 0;
737
Paul Mundt03057942008-04-25 17:58:42 +0900738err_unmap:
Paul Mundt063adc72009-04-16 14:12:22 +0900739 clk_disable(rtc->clk);
740 clk_put(rtc->clk);
Paul Mundt03057942008-04-25 17:58:42 +0900741 iounmap(rtc->regbase);
Paul Mundt317a6102006-09-27 17:13:19 +0900742err_badmap:
Axel Lin899be962010-11-08 13:35:10 +0800743 release_mem_region(rtc->res->start, rtc->regsize);
Paul Mundt317a6102006-09-27 17:13:19 +0900744err_badres:
745 kfree(rtc);
746
747 return ret;
748}
749
Alessandro Zummo5c9740a2009-08-20 13:25:11 +0900750static int __exit sh_rtc_remove(struct platform_device *pdev)
Paul Mundt317a6102006-09-27 17:13:19 +0900751{
752 struct sh_rtc *rtc = platform_get_drvdata(pdev);
753
Alessandro Zummo5c9740a2009-08-20 13:25:11 +0900754 rtc_device_unregister(rtc->rtc_dev);
755 sh_rtc_irq_set_state(&pdev->dev, 0);
Paul Mundt317a6102006-09-27 17:13:19 +0900756
Paul Mundt317a6102006-09-27 17:13:19 +0900757 sh_rtc_setaie(&pdev->dev, 0);
Magnus Damm9cd88b92009-03-19 10:05:58 +0000758 sh_rtc_setcie(&pdev->dev, 0);
Paul Mundt317a6102006-09-27 17:13:19 +0900759
Angelo Castellob420b1a2008-03-06 12:50:53 +0900760 free_irq(rtc->periodic_irq, rtc);
Paul Mundt063adc72009-04-16 14:12:22 +0900761
Magnus Damm5e084a12009-02-24 22:11:03 +0900762 if (rtc->carry_irq > 0) {
763 free_irq(rtc->carry_irq, rtc);
764 free_irq(rtc->alarm_irq, rtc);
765 }
Angelo Castellob420b1a2008-03-06 12:50:53 +0900766
Paul Mundt03057942008-04-25 17:58:42 +0900767 iounmap(rtc->regbase);
Axel Lin899be962010-11-08 13:35:10 +0800768 release_mem_region(rtc->res->start, rtc->regsize);
Paul Mundt03057942008-04-25 17:58:42 +0900769
Paul Mundt063adc72009-04-16 14:12:22 +0900770 clk_disable(rtc->clk);
771 clk_put(rtc->clk);
772
Paul Mundt317a6102006-09-27 17:13:19 +0900773 platform_set_drvdata(pdev, NULL);
774
775 kfree(rtc);
776
777 return 0;
778}
Magnus Dammfaa9fa82009-04-01 14:45:17 +0000779
780static void sh_rtc_set_irq_wake(struct device *dev, int enabled)
781{
782 struct platform_device *pdev = to_platform_device(dev);
783 struct sh_rtc *rtc = platform_get_drvdata(pdev);
784
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200785 irq_set_irq_wake(rtc->periodic_irq, enabled);
Paul Mundt063adc72009-04-16 14:12:22 +0900786
Magnus Dammfaa9fa82009-04-01 14:45:17 +0000787 if (rtc->carry_irq > 0) {
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200788 irq_set_irq_wake(rtc->carry_irq, enabled);
789 irq_set_irq_wake(rtc->alarm_irq, enabled);
Magnus Dammfaa9fa82009-04-01 14:45:17 +0000790 }
Magnus Dammfaa9fa82009-04-01 14:45:17 +0000791}
792
793static int sh_rtc_suspend(struct device *dev)
794{
795 if (device_may_wakeup(dev))
796 sh_rtc_set_irq_wake(dev, 1);
797
798 return 0;
799}
800
801static int sh_rtc_resume(struct device *dev)
802{
803 if (device_may_wakeup(dev))
804 sh_rtc_set_irq_wake(dev, 0);
805
806 return 0;
807}
808
Alexey Dobriyan47145212009-12-14 18:00:08 -0800809static const struct dev_pm_ops sh_rtc_dev_pm_ops = {
Magnus Dammfaa9fa82009-04-01 14:45:17 +0000810 .suspend = sh_rtc_suspend,
811 .resume = sh_rtc_resume,
812};
813
Paul Mundt317a6102006-09-27 17:13:19 +0900814static struct platform_driver sh_rtc_platform_driver = {
815 .driver = {
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900816 .name = DRV_NAME,
Paul Mundt317a6102006-09-27 17:13:19 +0900817 .owner = THIS_MODULE,
Magnus Dammfaa9fa82009-04-01 14:45:17 +0000818 .pm = &sh_rtc_dev_pm_ops,
Paul Mundt317a6102006-09-27 17:13:19 +0900819 },
Alessandro Zummo5c9740a2009-08-20 13:25:11 +0900820 .remove = __exit_p(sh_rtc_remove),
Paul Mundt317a6102006-09-27 17:13:19 +0900821};
822
823static int __init sh_rtc_init(void)
824{
Alessandro Zummo5c9740a2009-08-20 13:25:11 +0900825 return platform_driver_probe(&sh_rtc_platform_driver, sh_rtc_probe);
Paul Mundt317a6102006-09-27 17:13:19 +0900826}
827
828static void __exit sh_rtc_exit(void)
829{
830 platform_driver_unregister(&sh_rtc_platform_driver);
831}
832
833module_init(sh_rtc_init);
834module_exit(sh_rtc_exit);
835
836MODULE_DESCRIPTION("SuperH on-chip RTC driver");
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900837MODULE_VERSION(DRV_VERSION);
Angelo Castellob420b1a2008-03-06 12:50:53 +0900838MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>, "
839 "Jamie Lenehan <lenehan@twibble.org>, "
840 "Angelo Castello <angelo.castello@st.com>");
Paul Mundt317a6102006-09-27 17:13:19 +0900841MODULE_LICENSE("GPL");
Kay Sieversad28a072008-04-10 21:29:25 -0700842MODULE_ALIAS("platform:" DRV_NAME);