blob: abbce362f4df94ea90e199a6c41c3e4eb54bc8da [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>
Chris Brandtdab5aec2017-03-29 10:30:29 -070030#ifdef CONFIG_SUPERH
Paul Mundtad89f872007-08-03 14:19:58 +090031#include <asm/rtc.h>
Chris Brandtdab5aec2017-03-29 10:30:29 -070032#else
33/* Default values for RZ/A RTC */
34#define rtc_reg_size sizeof(u16)
35#define RTC_BIT_INVERTED 0 /* no chip bugs */
36#define RTC_CAP_4_DIGIT_YEAR (1 << 0)
37#define RTC_DEF_CAPABILITIES RTC_CAP_4_DIGIT_YEAR
38#endif
Paul Mundt317a6102006-09-27 17:13:19 +090039
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +090040#define DRV_NAME "sh-rtc"
Paul Mundt317a6102006-09-27 17:13:19 +090041
42#define RTC_REG(r) ((r) * rtc_reg_size)
43
Jamie Lenehan31ccb082006-12-07 17:23:50 +090044#define R64CNT RTC_REG(0)
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +090045
46#define RSECCNT RTC_REG(1) /* RTC sec */
47#define RMINCNT RTC_REG(2) /* RTC min */
48#define RHRCNT RTC_REG(3) /* RTC hour */
49#define RWKCNT RTC_REG(4) /* RTC week */
50#define RDAYCNT RTC_REG(5) /* RTC day */
51#define RMONCNT RTC_REG(6) /* RTC month */
52#define RYRCNT RTC_REG(7) /* RTC year */
53#define RSECAR RTC_REG(8) /* ALARM sec */
54#define RMINAR RTC_REG(9) /* ALARM min */
55#define RHRAR RTC_REG(10) /* ALARM hour */
56#define RWKAR RTC_REG(11) /* ALARM week */
57#define RDAYAR RTC_REG(12) /* ALARM day */
58#define RMONAR RTC_REG(13) /* ALARM month */
59#define RCR1 RTC_REG(14) /* Control */
60#define RCR2 RTC_REG(15) /* Control */
61
Paul Mundtff1b7502007-11-26 17:56:31 +090062/*
63 * Note on RYRAR and RCR3: Up until this point most of the register
64 * definitions are consistent across all of the available parts. However,
65 * the placement of the optional RYRAR and RCR3 (the RYRAR control
66 * register used to control RYRCNT/RYRAR compare) varies considerably
67 * across various parts, occasionally being mapped in to a completely
68 * unrelated address space. For proper RYRAR support a separate resource
69 * would have to be handed off, but as this is purely optional in
70 * practice, we simply opt not to support it, thereby keeping the code
71 * quite a bit more simplified.
72 */
73
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +090074/* ALARM Bits - or with BCD encoded value */
75#define AR_ENB 0x80 /* Enable for alarm cmp */
Paul Mundt317a6102006-09-27 17:13:19 +090076
Angelo Castellob420b1a2008-03-06 12:50:53 +090077/* Period Bits */
78#define PF_HP 0x100 /* Enable Half Period to support 8,32,128Hz */
79#define PF_COUNT 0x200 /* Half periodic counter */
80#define PF_OXS 0x400 /* Periodic One x Second */
81#define PF_KOU 0x800 /* Kernel or User periodic request 1=kernel */
82#define PF_MASK 0xf00
83
Paul Mundt317a6102006-09-27 17:13:19 +090084/* RCR1 Bits */
85#define RCR1_CF 0x80 /* Carry Flag */
86#define RCR1_CIE 0x10 /* Carry Interrupt Enable */
87#define RCR1_AIE 0x08 /* Alarm Interrupt Enable */
88#define RCR1_AF 0x01 /* Alarm Flag */
89
90/* RCR2 Bits */
91#define RCR2_PEF 0x80 /* PEriodic interrupt Flag */
92#define RCR2_PESMASK 0x70 /* Periodic interrupt Set */
93#define RCR2_RTCEN 0x08 /* ENable RTC */
94#define RCR2_ADJ 0x04 /* ADJustment (30-second) */
95#define RCR2_RESET 0x02 /* Reset bit */
96#define RCR2_START 0x01 /* Start bit */
97
98struct sh_rtc {
Paul Mundt063adc72009-04-16 14:12:22 +090099 void __iomem *regbase;
100 unsigned long regsize;
101 struct resource *res;
102 int alarm_irq;
103 int periodic_irq;
104 int carry_irq;
105 struct clk *clk;
106 struct rtc_device *rtc_dev;
107 spinlock_t lock;
108 unsigned long capabilities; /* See asm/rtc.h for cap bits */
109 unsigned short periodic_freq;
Paul Mundt317a6102006-09-27 17:13:19 +0900110};
111
Magnus Damm5e084a12009-02-24 22:11:03 +0900112static int __sh_rtc_interrupt(struct sh_rtc *rtc)
Paul Mundt317a6102006-09-27 17:13:19 +0900113{
Magnus Damm5e084a12009-02-24 22:11:03 +0900114 unsigned int tmp, pending;
Paul Mundt317a6102006-09-27 17:13:19 +0900115
116 tmp = readb(rtc->regbase + RCR1);
Magnus Damm5e084a12009-02-24 22:11:03 +0900117 pending = tmp & RCR1_CF;
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900118 tmp &= ~RCR1_CF;
Paul Mundt317a6102006-09-27 17:13:19 +0900119 writeb(tmp, rtc->regbase + RCR1);
120
Angelo Castellob420b1a2008-03-06 12:50:53 +0900121 /* Users have requested One x Second IRQ */
Magnus Damm5e084a12009-02-24 22:11:03 +0900122 if (pending && rtc->periodic_freq & PF_OXS)
Angelo Castellob420b1a2008-03-06 12:50:53 +0900123 rtc_update_irq(rtc->rtc_dev, 1, RTC_UF | RTC_IRQF);
Paul Mundt317a6102006-09-27 17:13:19 +0900124
Magnus Damm5e084a12009-02-24 22:11:03 +0900125 return pending;
Paul Mundt317a6102006-09-27 17:13:19 +0900126}
127
Magnus Damm5e084a12009-02-24 22:11:03 +0900128static int __sh_rtc_alarm(struct sh_rtc *rtc)
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900129{
Magnus Damm5e084a12009-02-24 22:11:03 +0900130 unsigned int tmp, pending;
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900131
132 tmp = readb(rtc->regbase + RCR1);
Magnus Damm5e084a12009-02-24 22:11:03 +0900133 pending = tmp & RCR1_AF;
Angelo Castellob420b1a2008-03-06 12:50:53 +0900134 tmp &= ~(RCR1_AF | RCR1_AIE);
Magnus Damm5e084a12009-02-24 22:11:03 +0900135 writeb(tmp, rtc->regbase + RCR1);
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900136
Magnus Damm5e084a12009-02-24 22:11:03 +0900137 if (pending)
138 rtc_update_irq(rtc->rtc_dev, 1, RTC_AF | RTC_IRQF);
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900139
Magnus Damm5e084a12009-02-24 22:11:03 +0900140 return pending;
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900141}
142
Magnus Damm5e084a12009-02-24 22:11:03 +0900143static int __sh_rtc_periodic(struct sh_rtc *rtc)
Paul Mundt317a6102006-09-27 17:13:19 +0900144{
Angelo Castellob420b1a2008-03-06 12:50:53 +0900145 struct rtc_device *rtc_dev = rtc->rtc_dev;
Magnus Damm5e084a12009-02-24 22:11:03 +0900146 unsigned int tmp, pending;
Paul Mundt317a6102006-09-27 17:13:19 +0900147
Angelo Castellob420b1a2008-03-06 12:50:53 +0900148 tmp = readb(rtc->regbase + RCR2);
Magnus Damm5e084a12009-02-24 22:11:03 +0900149 pending = tmp & RCR2_PEF;
Angelo Castellob420b1a2008-03-06 12:50:53 +0900150 tmp &= ~RCR2_PEF;
151 writeb(tmp, rtc->regbase + RCR2);
152
Magnus Damm5e084a12009-02-24 22:11:03 +0900153 if (!pending)
154 return 0;
155
Angelo Castellob420b1a2008-03-06 12:50:53 +0900156 /* Half period enabled than one skipped and the next notified */
157 if ((rtc->periodic_freq & PF_HP) && (rtc->periodic_freq & PF_COUNT))
158 rtc->periodic_freq &= ~PF_COUNT;
159 else {
160 if (rtc->periodic_freq & PF_HP)
161 rtc->periodic_freq |= PF_COUNT;
Alexandre Belloniec623ff2018-07-25 16:45:39 +0200162 rtc_update_irq(rtc->rtc_dev, 1, RTC_PF | RTC_IRQF);
Angelo Castellob420b1a2008-03-06 12:50:53 +0900163 }
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
Paul Mundt317a6102006-09-27 17:13:19 +0900218static inline void sh_rtc_setaie(struct device *dev, unsigned int enable)
219{
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 + RCR1);
226
Paul Mundt063adc72009-04-16 14:12:22 +0900227 if (enable)
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900228 tmp |= RCR1_AIE;
Paul Mundt063adc72009-04-16 14:12:22 +0900229 else
230 tmp &= ~RCR1_AIE;
Paul Mundt317a6102006-09-27 17:13:19 +0900231
232 writeb(tmp, rtc->regbase + RCR1);
233
234 spin_unlock_irq(&rtc->lock);
235}
236
Paul Mundt317a6102006-09-27 17:13:19 +0900237static int sh_rtc_proc(struct device *dev, struct seq_file *seq)
238{
239 struct sh_rtc *rtc = dev_get_drvdata(dev);
240 unsigned int tmp;
241
242 tmp = readb(rtc->regbase + RCR1);
Angelo Castellob420b1a2008-03-06 12:50:53 +0900243 seq_printf(seq, "carry_IRQ\t: %s\n", (tmp & RCR1_CIE) ? "yes" : "no");
Paul Mundt317a6102006-09-27 17:13:19 +0900244
245 tmp = readb(rtc->regbase + RCR2);
246 seq_printf(seq, "periodic_IRQ\t: %s\n",
Angelo Castellob420b1a2008-03-06 12:50:53 +0900247 (tmp & RCR2_PESMASK) ? "yes" : "no");
Paul Mundt317a6102006-09-27 17:13:19 +0900248
249 return 0;
250}
251
Magnus Damm9cd88b92009-03-19 10:05:58 +0000252static inline void sh_rtc_setcie(struct device *dev, unsigned int enable)
253{
254 struct sh_rtc *rtc = dev_get_drvdata(dev);
255 unsigned int tmp;
256
257 spin_lock_irq(&rtc->lock);
258
259 tmp = readb(rtc->regbase + RCR1);
260
261 if (!enable)
262 tmp &= ~RCR1_CIE;
263 else
264 tmp |= RCR1_CIE;
265
266 writeb(tmp, rtc->regbase + RCR1);
267
268 spin_unlock_irq(&rtc->lock);
269}
270
John Stultz16380c12011-02-02 17:02:41 -0800271static int sh_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
272{
273 sh_rtc_setaie(dev, enabled);
274 return 0;
275}
276
Paul Mundt317a6102006-09-27 17:13:19 +0900277static int sh_rtc_read_time(struct device *dev, struct rtc_time *tm)
278{
Wolfram Sang85368bb2018-04-19 16:06:14 +0200279 struct sh_rtc *rtc = dev_get_drvdata(dev);
Paul Mundt317a6102006-09-27 17:13:19 +0900280 unsigned int sec128, sec2, yr, yr100, cf_bit;
281
282 do {
283 unsigned int tmp;
284
285 spin_lock_irq(&rtc->lock);
286
287 tmp = readb(rtc->regbase + RCR1);
288 tmp &= ~RCR1_CF; /* Clear CF-bit */
289 tmp |= RCR1_CIE;
290 writeb(tmp, rtc->regbase + RCR1);
291
292 sec128 = readb(rtc->regbase + R64CNT);
293
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700294 tm->tm_sec = bcd2bin(readb(rtc->regbase + RSECCNT));
295 tm->tm_min = bcd2bin(readb(rtc->regbase + RMINCNT));
296 tm->tm_hour = bcd2bin(readb(rtc->regbase + RHRCNT));
297 tm->tm_wday = bcd2bin(readb(rtc->regbase + RWKCNT));
298 tm->tm_mday = bcd2bin(readb(rtc->regbase + RDAYCNT));
299 tm->tm_mon = bcd2bin(readb(rtc->regbase + RMONCNT)) - 1;
Paul Mundt317a6102006-09-27 17:13:19 +0900300
Paul Mundtad89f872007-08-03 14:19:58 +0900301 if (rtc->capabilities & RTC_CAP_4_DIGIT_YEAR) {
302 yr = readw(rtc->regbase + RYRCNT);
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700303 yr100 = bcd2bin(yr >> 8);
Paul Mundtad89f872007-08-03 14:19:58 +0900304 yr &= 0xff;
305 } else {
306 yr = readb(rtc->regbase + RYRCNT);
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700307 yr100 = bcd2bin((yr == 0x99) ? 0x19 : 0x20);
Paul Mundtad89f872007-08-03 14:19:58 +0900308 }
Paul Mundt317a6102006-09-27 17:13:19 +0900309
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700310 tm->tm_year = (yr100 * 100 + bcd2bin(yr)) - 1900;
Paul Mundt317a6102006-09-27 17:13:19 +0900311
312 sec2 = readb(rtc->regbase + R64CNT);
313 cf_bit = readb(rtc->regbase + RCR1) & RCR1_CF;
314
315 spin_unlock_irq(&rtc->lock);
316 } while (cf_bit != 0 || ((sec128 ^ sec2) & RTC_BIT_INVERTED) != 0);
317
318#if RTC_BIT_INVERTED != 0
319 if ((sec128 & RTC_BIT_INVERTED))
320 tm->tm_sec--;
321#endif
322
Magnus Damm9cd88b92009-03-19 10:05:58 +0000323 /* only keep the carry interrupt enabled if UIE is on */
324 if (!(rtc->periodic_freq & PF_OXS))
325 sh_rtc_setcie(dev, 0);
326
Paul Mundt435c55d2007-05-08 11:56:27 +0900327 dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
Paul Mundt317a6102006-09-27 17:13:19 +0900328 "mday=%d, mon=%d, year=%d, wday=%d\n",
Harvey Harrison2a4e2b8782008-04-28 02:12:00 -0700329 __func__,
Paul Mundt317a6102006-09-27 17:13:19 +0900330 tm->tm_sec, tm->tm_min, tm->tm_hour,
Jamie Lenehana1614792006-12-08 14:49:30 +0900331 tm->tm_mday, tm->tm_mon + 1, tm->tm_year, tm->tm_wday);
Paul Mundt317a6102006-09-27 17:13:19 +0900332
Alexandre Belloni22652ba2018-02-19 16:23:56 +0100333 return 0;
Paul Mundt317a6102006-09-27 17:13:19 +0900334}
335
336static int sh_rtc_set_time(struct device *dev, struct rtc_time *tm)
337{
Wolfram Sang85368bb2018-04-19 16:06:14 +0200338 struct sh_rtc *rtc = dev_get_drvdata(dev);
Paul Mundt317a6102006-09-27 17:13:19 +0900339 unsigned int tmp;
340 int year;
341
342 spin_lock_irq(&rtc->lock);
343
344 /* Reset pre-scaler & stop RTC */
345 tmp = readb(rtc->regbase + RCR2);
346 tmp |= RCR2_RESET;
Markus Brunner699bc662007-07-26 17:31:28 +0900347 tmp &= ~RCR2_START;
Paul Mundt317a6102006-09-27 17:13:19 +0900348 writeb(tmp, rtc->regbase + RCR2);
349
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700350 writeb(bin2bcd(tm->tm_sec), rtc->regbase + RSECCNT);
351 writeb(bin2bcd(tm->tm_min), rtc->regbase + RMINCNT);
352 writeb(bin2bcd(tm->tm_hour), rtc->regbase + RHRCNT);
353 writeb(bin2bcd(tm->tm_wday), rtc->regbase + RWKCNT);
354 writeb(bin2bcd(tm->tm_mday), rtc->regbase + RDAYCNT);
355 writeb(bin2bcd(tm->tm_mon + 1), rtc->regbase + RMONCNT);
Paul Mundt317a6102006-09-27 17:13:19 +0900356
Paul Mundtad89f872007-08-03 14:19:58 +0900357 if (rtc->capabilities & RTC_CAP_4_DIGIT_YEAR) {
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700358 year = (bin2bcd((tm->tm_year + 1900) / 100) << 8) |
359 bin2bcd(tm->tm_year % 100);
Paul Mundtad89f872007-08-03 14:19:58 +0900360 writew(year, rtc->regbase + RYRCNT);
361 } else {
362 year = tm->tm_year % 100;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700363 writeb(bin2bcd(year), rtc->regbase + RYRCNT);
Paul Mundtad89f872007-08-03 14:19:58 +0900364 }
Paul Mundt317a6102006-09-27 17:13:19 +0900365
366 /* Start RTC */
367 tmp = readb(rtc->regbase + RCR2);
368 tmp &= ~RCR2_RESET;
369 tmp |= RCR2_RTCEN | RCR2_START;
370 writeb(tmp, rtc->regbase + RCR2);
371
372 spin_unlock_irq(&rtc->lock);
373
374 return 0;
375}
376
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900377static inline int sh_rtc_read_alarm_value(struct sh_rtc *rtc, int reg_off)
378{
379 unsigned int byte;
380 int value = 0xff; /* return 0xff for ignored values */
381
382 byte = readb(rtc->regbase + reg_off);
383 if (byte & AR_ENB) {
384 byte &= ~AR_ENB; /* strip the enable bit */
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700385 value = bcd2bin(byte);
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900386 }
387
388 return value;
389}
390
391static int sh_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
392{
Wolfram Sang85368bb2018-04-19 16:06:14 +0200393 struct sh_rtc *rtc = dev_get_drvdata(dev);
Angelo Castellob420b1a2008-03-06 12:50:53 +0900394 struct rtc_time *tm = &wkalrm->time;
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900395
396 spin_lock_irq(&rtc->lock);
397
398 tm->tm_sec = sh_rtc_read_alarm_value(rtc, RSECAR);
399 tm->tm_min = sh_rtc_read_alarm_value(rtc, RMINAR);
400 tm->tm_hour = sh_rtc_read_alarm_value(rtc, RHRAR);
401 tm->tm_wday = sh_rtc_read_alarm_value(rtc, RWKAR);
402 tm->tm_mday = sh_rtc_read_alarm_value(rtc, RDAYAR);
403 tm->tm_mon = sh_rtc_read_alarm_value(rtc, RMONAR);
404 if (tm->tm_mon > 0)
405 tm->tm_mon -= 1; /* RTC is 1-12, tm_mon is 0-11 */
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900406
David Brownell0d103e92007-01-10 23:15:32 -0800407 wkalrm->enabled = (readb(rtc->regbase + RCR1) & RCR1_AIE) ? 1 : 0;
408
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900409 spin_unlock_irq(&rtc->lock);
410
411 return 0;
412}
413
414static inline void sh_rtc_write_alarm_value(struct sh_rtc *rtc,
415 int value, int reg_off)
416{
417 /* < 0 for a value that is ignored */
418 if (value < 0)
419 writeb(0, rtc->regbase + reg_off);
420 else
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700421 writeb(bin2bcd(value) | AR_ENB, rtc->regbase + reg_off);
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900422}
423
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900424static int sh_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
425{
Wolfram Sang85368bb2018-04-19 16:06:14 +0200426 struct sh_rtc *rtc = dev_get_drvdata(dev);
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900427 unsigned int rcr1;
428 struct rtc_time *tm = &wkalrm->time;
Uwe Kleine-König84411892016-06-28 10:43:48 +0200429 int mon;
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900430
431 spin_lock_irq(&rtc->lock);
432
Jamie Lenehan15c945c2007-01-22 20:40:41 -0800433 /* disable alarm interrupt and clear the alarm flag */
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900434 rcr1 = readb(rtc->regbase + RCR1);
Angelo Castellob420b1a2008-03-06 12:50:53 +0900435 rcr1 &= ~(RCR1_AF | RCR1_AIE);
Jamie Lenehan15c945c2007-01-22 20:40:41 -0800436 writeb(rcr1, rtc->regbase + RCR1);
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900437
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900438 /* set alarm time */
439 sh_rtc_write_alarm_value(rtc, tm->tm_sec, RSECAR);
440 sh_rtc_write_alarm_value(rtc, tm->tm_min, RMINAR);
441 sh_rtc_write_alarm_value(rtc, tm->tm_hour, RHRAR);
442 sh_rtc_write_alarm_value(rtc, tm->tm_wday, RWKAR);
443 sh_rtc_write_alarm_value(rtc, tm->tm_mday, RDAYAR);
444 mon = tm->tm_mon;
445 if (mon >= 0)
446 mon += 1;
447 sh_rtc_write_alarm_value(rtc, mon, RMONAR);
448
Jamie Lenehan15c945c2007-01-22 20:40:41 -0800449 if (wkalrm->enabled) {
450 rcr1 |= RCR1_AIE;
451 writeb(rcr1, rtc->regbase + RCR1);
452 }
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900453
454 spin_unlock_irq(&rtc->lock);
455
456 return 0;
457}
458
Bhumika Goyal8bc57e72017-01-05 22:25:05 +0530459static const struct rtc_class_ops sh_rtc_ops = {
Paul Mundt317a6102006-09-27 17:13:19 +0900460 .read_time = sh_rtc_read_time,
461 .set_time = sh_rtc_set_time,
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900462 .read_alarm = sh_rtc_read_alarm,
463 .set_alarm = sh_rtc_set_alarm,
Paul Mundt317a6102006-09-27 17:13:19 +0900464 .proc = sh_rtc_proc,
John Stultz16380c12011-02-02 17:02:41 -0800465 .alarm_irq_enable = sh_rtc_alarm_irq_enable,
Paul Mundt317a6102006-09-27 17:13:19 +0900466};
467
Alessandro Zummo5c9740a2009-08-20 13:25:11 +0900468static int __init sh_rtc_probe(struct platform_device *pdev)
Paul Mundt317a6102006-09-27 17:13:19 +0900469{
470 struct sh_rtc *rtc;
471 struct resource *res;
Magnus Dammedf22472009-03-19 10:10:44 +0000472 struct rtc_time r;
Paul Mundt063adc72009-04-16 14:12:22 +0900473 char clk_name[6];
474 int clk_id, ret;
Paul Mundt317a6102006-09-27 17:13:19 +0900475
Jingoo Han0209aff2013-07-03 15:07:11 -0700476 rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
Paul Mundt317a6102006-09-27 17:13:19 +0900477 if (unlikely(!rtc))
478 return -ENOMEM;
479
480 spin_lock_init(&rtc->lock);
481
Angelo Castellob420b1a2008-03-06 12:50:53 +0900482 /* get periodic/carry/alarm irqs */
roel kluin2641dc92008-09-10 19:34:44 +0200483 ret = platform_get_irq(pdev, 0);
Anton Vorontsov2fac6672009-01-06 14:42:11 -0800484 if (unlikely(ret <= 0)) {
Magnus Damm5e084a12009-02-24 22:11:03 +0900485 dev_err(&pdev->dev, "No IRQ resource\n");
Jingoo Han0209aff2013-07-03 15:07:11 -0700486 return -ENOENT;
Paul Mundt317a6102006-09-27 17:13:19 +0900487 }
Paul Mundt063adc72009-04-16 14:12:22 +0900488
roel kluin2641dc92008-09-10 19:34:44 +0200489 rtc->periodic_irq = ret;
Magnus Damm5e084a12009-02-24 22:11:03 +0900490 rtc->carry_irq = platform_get_irq(pdev, 1);
491 rtc->alarm_irq = platform_get_irq(pdev, 2);
Paul Mundt317a6102006-09-27 17:13:19 +0900492
493 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Chris Brandtdab5aec2017-03-29 10:30:29 -0700494 if (!res)
495 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Paul Mundt317a6102006-09-27 17:13:19 +0900496 if (unlikely(res == NULL)) {
497 dev_err(&pdev->dev, "No IO resource\n");
Jingoo Han0209aff2013-07-03 15:07:11 -0700498 return -ENOENT;
Paul Mundt317a6102006-09-27 17:13:19 +0900499 }
500
Paul Mundt063adc72009-04-16 14:12:22 +0900501 rtc->regsize = resource_size(res);
Paul Mundt317a6102006-09-27 17:13:19 +0900502
Jingoo Han0209aff2013-07-03 15:07:11 -0700503 rtc->res = devm_request_mem_region(&pdev->dev, res->start,
504 rtc->regsize, pdev->name);
505 if (unlikely(!rtc->res))
506 return -EBUSY;
Paul Mundt317a6102006-09-27 17:13:19 +0900507
Jingoo Han0209aff2013-07-03 15:07:11 -0700508 rtc->regbase = devm_ioremap_nocache(&pdev->dev, rtc->res->start,
509 rtc->regsize);
510 if (unlikely(!rtc->regbase))
511 return -EINVAL;
Paul Mundt317a6102006-09-27 17:13:19 +0900512
Chris Brandtdab5aec2017-03-29 10:30:29 -0700513 if (!pdev->dev.of_node) {
514 clk_id = pdev->id;
515 /* With a single device, the clock id is still "rtc0" */
516 if (clk_id < 0)
517 clk_id = 0;
Paul Mundt063adc72009-04-16 14:12:22 +0900518
Chris Brandtdab5aec2017-03-29 10:30:29 -0700519 snprintf(clk_name, sizeof(clk_name), "rtc%d", clk_id);
520 } else
521 snprintf(clk_name, sizeof(clk_name), "fck");
Paul Mundt063adc72009-04-16 14:12:22 +0900522
Jingoo Han0209aff2013-07-03 15:07:11 -0700523 rtc->clk = devm_clk_get(&pdev->dev, clk_name);
Paul Mundt063adc72009-04-16 14:12:22 +0900524 if (IS_ERR(rtc->clk)) {
525 /*
526 * No error handling for rtc->clk intentionally, not all
527 * platforms will have a unique clock for the RTC, and
528 * the clk API can handle the struct clk pointer being
529 * NULL.
530 */
531 rtc->clk = NULL;
532 }
533
534 clk_enable(rtc->clk);
535
Paul Mundtad89f872007-08-03 14:19:58 +0900536 rtc->capabilities = RTC_DEF_CAPABILITIES;
Chris Brandtdab5aec2017-03-29 10:30:29 -0700537
538#ifdef CONFIG_SUPERH
Jingoo Hane58c18d2013-11-12 15:10:52 -0800539 if (dev_get_platdata(&pdev->dev)) {
540 struct sh_rtc_platform_info *pinfo =
541 dev_get_platdata(&pdev->dev);
Paul Mundtad89f872007-08-03 14:19:58 +0900542
543 /*
544 * Some CPUs have special capabilities in addition to the
545 * default set. Add those in here.
546 */
547 rtc->capabilities |= pinfo->capabilities;
548 }
Chris Brandtdab5aec2017-03-29 10:30:29 -0700549#endif
Paul Mundtad89f872007-08-03 14:19:58 +0900550
Magnus Damm5e084a12009-02-24 22:11:03 +0900551 if (rtc->carry_irq <= 0) {
552 /* register shared periodic/carry/alarm irq */
Jingoo Han0209aff2013-07-03 15:07:11 -0700553 ret = devm_request_irq(&pdev->dev, rtc->periodic_irq,
554 sh_rtc_shared, 0, "sh-rtc", rtc);
Magnus Damm5e084a12009-02-24 22:11:03 +0900555 if (unlikely(ret)) {
556 dev_err(&pdev->dev,
557 "request IRQ failed with %d, IRQ %d\n", ret,
558 rtc->periodic_irq);
559 goto err_unmap;
560 }
561 } else {
562 /* register periodic/carry/alarm irqs */
Jingoo Han0209aff2013-07-03 15:07:11 -0700563 ret = devm_request_irq(&pdev->dev, rtc->periodic_irq,
564 sh_rtc_periodic, 0, "sh-rtc period", rtc);
Magnus Damm5e084a12009-02-24 22:11:03 +0900565 if (unlikely(ret)) {
566 dev_err(&pdev->dev,
567 "request period IRQ failed with %d, IRQ %d\n",
568 ret, rtc->periodic_irq);
569 goto err_unmap;
570 }
Angelo Castellob420b1a2008-03-06 12:50:53 +0900571
Jingoo Han0209aff2013-07-03 15:07:11 -0700572 ret = devm_request_irq(&pdev->dev, rtc->carry_irq,
573 sh_rtc_interrupt, 0, "sh-rtc carry", rtc);
Magnus Damm5e084a12009-02-24 22:11:03 +0900574 if (unlikely(ret)) {
575 dev_err(&pdev->dev,
576 "request carry IRQ failed with %d, IRQ %d\n",
577 ret, rtc->carry_irq);
Magnus Damm5e084a12009-02-24 22:11:03 +0900578 goto err_unmap;
579 }
Angelo Castellob420b1a2008-03-06 12:50:53 +0900580
Jingoo Han0209aff2013-07-03 15:07:11 -0700581 ret = devm_request_irq(&pdev->dev, rtc->alarm_irq,
582 sh_rtc_alarm, 0, "sh-rtc alarm", rtc);
Magnus Damm5e084a12009-02-24 22:11:03 +0900583 if (unlikely(ret)) {
584 dev_err(&pdev->dev,
585 "request alarm IRQ failed with %d, IRQ %d\n",
586 ret, rtc->alarm_irq);
Magnus Damm5e084a12009-02-24 22:11:03 +0900587 goto err_unmap;
588 }
Angelo Castellob420b1a2008-03-06 12:50:53 +0900589 }
590
Alessandro Zummo5c9740a2009-08-20 13:25:11 +0900591 platform_set_drvdata(pdev, rtc);
592
Magnus Damm9cd88b92009-03-19 10:05:58 +0000593 /* everything disabled by default */
Magnus Damm9cd88b92009-03-19 10:05:58 +0000594 sh_rtc_setaie(&pdev->dev, 0);
595 sh_rtc_setcie(&pdev->dev, 0);
Magnus Dammedf22472009-03-19 10:10:44 +0000596
Jingoo Han0209aff2013-07-03 15:07:11 -0700597 rtc->rtc_dev = devm_rtc_device_register(&pdev->dev, "sh",
Alessandro Zummo5c9740a2009-08-20 13:25:11 +0900598 &sh_rtc_ops, THIS_MODULE);
599 if (IS_ERR(rtc->rtc_dev)) {
600 ret = PTR_ERR(rtc->rtc_dev);
Alessandro Zummo5c9740a2009-08-20 13:25:11 +0900601 goto err_unmap;
602 }
603
604 rtc->rtc_dev->max_user_freq = 256;
605
Magnus Dammedf22472009-03-19 10:10:44 +0000606 /* reset rtc to epoch 0 if time is invalid */
607 if (rtc_read_time(rtc->rtc_dev, &r) < 0) {
608 rtc_time_to_tm(0, &r);
609 rtc_set_time(rtc->rtc_dev, &r);
610 }
611
Magnus Damm7a8fe8e2009-03-19 10:14:41 +0000612 device_init_wakeup(&pdev->dev, 1);
Paul Mundt317a6102006-09-27 17:13:19 +0900613 return 0;
614
Paul Mundt03057942008-04-25 17:58:42 +0900615err_unmap:
Paul Mundt063adc72009-04-16 14:12:22 +0900616 clk_disable(rtc->clk);
Paul Mundt317a6102006-09-27 17:13:19 +0900617
618 return ret;
619}
620
Alessandro Zummo5c9740a2009-08-20 13:25:11 +0900621static int __exit sh_rtc_remove(struct platform_device *pdev)
Paul Mundt317a6102006-09-27 17:13:19 +0900622{
623 struct sh_rtc *rtc = platform_get_drvdata(pdev);
624
Paul Mundt317a6102006-09-27 17:13:19 +0900625 sh_rtc_setaie(&pdev->dev, 0);
Magnus Damm9cd88b92009-03-19 10:05:58 +0000626 sh_rtc_setcie(&pdev->dev, 0);
Paul Mundt317a6102006-09-27 17:13:19 +0900627
Paul Mundt063adc72009-04-16 14:12:22 +0900628 clk_disable(rtc->clk);
Paul Mundt317a6102006-09-27 17:13:19 +0900629
630 return 0;
631}
Magnus Dammfaa9fa82009-04-01 14:45:17 +0000632
633static void sh_rtc_set_irq_wake(struct device *dev, int enabled)
634{
Wolfram Sang85368bb2018-04-19 16:06:14 +0200635 struct sh_rtc *rtc = dev_get_drvdata(dev);
Magnus Dammfaa9fa82009-04-01 14:45:17 +0000636
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200637 irq_set_irq_wake(rtc->periodic_irq, enabled);
Paul Mundt063adc72009-04-16 14:12:22 +0900638
Magnus Dammfaa9fa82009-04-01 14:45:17 +0000639 if (rtc->carry_irq > 0) {
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200640 irq_set_irq_wake(rtc->carry_irq, enabled);
641 irq_set_irq_wake(rtc->alarm_irq, enabled);
Magnus Dammfaa9fa82009-04-01 14:45:17 +0000642 }
Magnus Dammfaa9fa82009-04-01 14:45:17 +0000643}
644
Arnd Bergmann5d05e812017-04-19 19:52:43 +0200645static int __maybe_unused sh_rtc_suspend(struct device *dev)
Magnus Dammfaa9fa82009-04-01 14:45:17 +0000646{
647 if (device_may_wakeup(dev))
648 sh_rtc_set_irq_wake(dev, 1);
649
650 return 0;
651}
652
Arnd Bergmann5d05e812017-04-19 19:52:43 +0200653static int __maybe_unused sh_rtc_resume(struct device *dev)
Magnus Dammfaa9fa82009-04-01 14:45:17 +0000654{
655 if (device_may_wakeup(dev))
656 sh_rtc_set_irq_wake(dev, 0);
657
658 return 0;
659}
660
Jingoo Han0ed50542013-04-29 16:20:00 -0700661static SIMPLE_DEV_PM_OPS(sh_rtc_pm_ops, sh_rtc_suspend, sh_rtc_resume);
Magnus Dammfaa9fa82009-04-01 14:45:17 +0000662
Chris Brandtdab5aec2017-03-29 10:30:29 -0700663static const struct of_device_id sh_rtc_of_match[] = {
664 { .compatible = "renesas,sh-rtc", },
665 { /* sentinel */ }
666};
667MODULE_DEVICE_TABLE(of, sh_rtc_of_match);
668
Paul Mundt317a6102006-09-27 17:13:19 +0900669static struct platform_driver sh_rtc_platform_driver = {
670 .driver = {
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900671 .name = DRV_NAME,
Jingoo Han0ed50542013-04-29 16:20:00 -0700672 .pm = &sh_rtc_pm_ops,
Chris Brandtdab5aec2017-03-29 10:30:29 -0700673 .of_match_table = sh_rtc_of_match,
Paul Mundt317a6102006-09-27 17:13:19 +0900674 },
Alessandro Zummo5c9740a2009-08-20 13:25:11 +0900675 .remove = __exit_p(sh_rtc_remove),
Paul Mundt317a6102006-09-27 17:13:19 +0900676};
677
Jingoo Handeed5a92013-04-29 16:18:51 -0700678module_platform_driver_probe(sh_rtc_platform_driver, sh_rtc_probe);
Paul Mundt317a6102006-09-27 17:13:19 +0900679
680MODULE_DESCRIPTION("SuperH on-chip RTC driver");
Angelo Castellob420b1a2008-03-06 12:50:53 +0900681MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>, "
682 "Jamie Lenehan <lenehan@twibble.org>, "
683 "Angelo Castello <angelo.castello@st.com>");
Paul Mundt317a6102006-09-27 17:13:19 +0900684MODULE_LICENSE("GPL");
Kay Sieversad28a072008-04-10 21:29:25 -0700685MODULE_ALIAS("platform:" DRV_NAME);