blob: d417b203cbc553eb25ab0cbf0eb493bcd84c9d46 [file] [log] [blame]
Kuninori Morimoto4ff6f022018-11-08 06:43:22 +00001// SPDX-License-Identifier: GPL-2.0
Paul Mundt317a6102006-09-27 17:13:19 +09002/*
3 * SuperH On-Chip RTC Support
4 *
Paul Mundt063adc72009-04-16 14:12:22 +09005 * Copyright (C) 2006 - 2009 Paul Mundt
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +09006 * Copyright (C) 2006 Jamie Lenehan
Angelo Castellob420b1a2008-03-06 12:50:53 +09007 * Copyright (C) 2008 Angelo Castello
Paul Mundt317a6102006-09-27 17:13:19 +09008 *
9 * Based on the old arch/sh/kernel/cpu/rtc.c by:
10 *
11 * Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org>
12 * Copyright (C) 1999 Tetsuya Okada & Niibe Yutaka
Paul Mundt317a6102006-09-27 17:13:19 +090013 */
14#include <linux/module.h>
Randy Dunlapac316722018-06-19 22:47:28 -070015#include <linux/mod_devicetable.h>
Paul Mundt317a6102006-09-27 17:13:19 +090016#include <linux/kernel.h>
17#include <linux/bcd.h>
18#include <linux/rtc.h>
19#include <linux/init.h>
20#include <linux/platform_device.h>
21#include <linux/seq_file.h>
22#include <linux/interrupt.h>
23#include <linux/spinlock.h>
Jamie Lenehan31ccb082006-12-07 17:23:50 +090024#include <linux/io.h>
Jonathan Cameron5d2a5032009-01-06 14:42:12 -080025#include <linux/log2.h>
Paul Mundt063adc72009-04-16 14:12:22 +090026#include <linux/clk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Chris Brandtdab5aec2017-03-29 10:30:29 -070028#ifdef CONFIG_SUPERH
Paul Mundtad89f872007-08-03 14:19:58 +090029#include <asm/rtc.h>
Chris Brandtdab5aec2017-03-29 10:30:29 -070030#else
31/* Default values for RZ/A RTC */
32#define rtc_reg_size sizeof(u16)
33#define RTC_BIT_INVERTED 0 /* no chip bugs */
34#define RTC_CAP_4_DIGIT_YEAR (1 << 0)
35#define RTC_DEF_CAPABILITIES RTC_CAP_4_DIGIT_YEAR
36#endif
Paul Mundt317a6102006-09-27 17:13:19 +090037
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +090038#define DRV_NAME "sh-rtc"
Paul Mundt317a6102006-09-27 17:13:19 +090039
40#define RTC_REG(r) ((r) * rtc_reg_size)
41
Jamie Lenehan31ccb082006-12-07 17:23:50 +090042#define R64CNT RTC_REG(0)
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +090043
44#define RSECCNT RTC_REG(1) /* RTC sec */
45#define RMINCNT RTC_REG(2) /* RTC min */
46#define RHRCNT RTC_REG(3) /* RTC hour */
47#define RWKCNT RTC_REG(4) /* RTC week */
48#define RDAYCNT RTC_REG(5) /* RTC day */
49#define RMONCNT RTC_REG(6) /* RTC month */
50#define RYRCNT RTC_REG(7) /* RTC year */
51#define RSECAR RTC_REG(8) /* ALARM sec */
52#define RMINAR RTC_REG(9) /* ALARM min */
53#define RHRAR RTC_REG(10) /* ALARM hour */
54#define RWKAR RTC_REG(11) /* ALARM week */
55#define RDAYAR RTC_REG(12) /* ALARM day */
56#define RMONAR RTC_REG(13) /* ALARM month */
57#define RCR1 RTC_REG(14) /* Control */
58#define RCR2 RTC_REG(15) /* Control */
59
Paul Mundtff1b7502007-11-26 17:56:31 +090060/*
61 * Note on RYRAR and RCR3: Up until this point most of the register
62 * definitions are consistent across all of the available parts. However,
63 * the placement of the optional RYRAR and RCR3 (the RYRAR control
64 * register used to control RYRCNT/RYRAR compare) varies considerably
65 * across various parts, occasionally being mapped in to a completely
66 * unrelated address space. For proper RYRAR support a separate resource
67 * would have to be handed off, but as this is purely optional in
68 * practice, we simply opt not to support it, thereby keeping the code
69 * quite a bit more simplified.
70 */
71
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +090072/* ALARM Bits - or with BCD encoded value */
73#define AR_ENB 0x80 /* Enable for alarm cmp */
Paul Mundt317a6102006-09-27 17:13:19 +090074
Angelo Castellob420b1a2008-03-06 12:50:53 +090075/* Period Bits */
76#define PF_HP 0x100 /* Enable Half Period to support 8,32,128Hz */
77#define PF_COUNT 0x200 /* Half periodic counter */
78#define PF_OXS 0x400 /* Periodic One x Second */
79#define PF_KOU 0x800 /* Kernel or User periodic request 1=kernel */
80#define PF_MASK 0xf00
81
Paul Mundt317a6102006-09-27 17:13:19 +090082/* RCR1 Bits */
83#define RCR1_CF 0x80 /* Carry Flag */
84#define RCR1_CIE 0x10 /* Carry Interrupt Enable */
85#define RCR1_AIE 0x08 /* Alarm Interrupt Enable */
86#define RCR1_AF 0x01 /* Alarm Flag */
87
88/* RCR2 Bits */
89#define RCR2_PEF 0x80 /* PEriodic interrupt Flag */
90#define RCR2_PESMASK 0x70 /* Periodic interrupt Set */
91#define RCR2_RTCEN 0x08 /* ENable RTC */
92#define RCR2_ADJ 0x04 /* ADJustment (30-second) */
93#define RCR2_RESET 0x02 /* Reset bit */
94#define RCR2_START 0x01 /* Start bit */
95
96struct sh_rtc {
Paul Mundt063adc72009-04-16 14:12:22 +090097 void __iomem *regbase;
98 unsigned long regsize;
99 struct resource *res;
100 int alarm_irq;
101 int periodic_irq;
102 int carry_irq;
103 struct clk *clk;
104 struct rtc_device *rtc_dev;
105 spinlock_t lock;
106 unsigned long capabilities; /* See asm/rtc.h for cap bits */
107 unsigned short periodic_freq;
Paul Mundt317a6102006-09-27 17:13:19 +0900108};
109
Magnus Damm5e084a12009-02-24 22:11:03 +0900110static int __sh_rtc_interrupt(struct sh_rtc *rtc)
Paul Mundt317a6102006-09-27 17:13:19 +0900111{
Magnus Damm5e084a12009-02-24 22:11:03 +0900112 unsigned int tmp, pending;
Paul Mundt317a6102006-09-27 17:13:19 +0900113
114 tmp = readb(rtc->regbase + RCR1);
Magnus Damm5e084a12009-02-24 22:11:03 +0900115 pending = tmp & RCR1_CF;
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900116 tmp &= ~RCR1_CF;
Paul Mundt317a6102006-09-27 17:13:19 +0900117 writeb(tmp, rtc->regbase + RCR1);
118
Angelo Castellob420b1a2008-03-06 12:50:53 +0900119 /* Users have requested One x Second IRQ */
Magnus Damm5e084a12009-02-24 22:11:03 +0900120 if (pending && rtc->periodic_freq & PF_OXS)
Angelo Castellob420b1a2008-03-06 12:50:53 +0900121 rtc_update_irq(rtc->rtc_dev, 1, RTC_UF | RTC_IRQF);
Paul Mundt317a6102006-09-27 17:13:19 +0900122
Magnus Damm5e084a12009-02-24 22:11:03 +0900123 return pending;
Paul Mundt317a6102006-09-27 17:13:19 +0900124}
125
Magnus Damm5e084a12009-02-24 22:11:03 +0900126static int __sh_rtc_alarm(struct sh_rtc *rtc)
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900127{
Magnus Damm5e084a12009-02-24 22:11:03 +0900128 unsigned int tmp, pending;
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900129
130 tmp = readb(rtc->regbase + RCR1);
Magnus Damm5e084a12009-02-24 22:11:03 +0900131 pending = tmp & RCR1_AF;
Angelo Castellob420b1a2008-03-06 12:50:53 +0900132 tmp &= ~(RCR1_AF | RCR1_AIE);
Magnus Damm5e084a12009-02-24 22:11:03 +0900133 writeb(tmp, rtc->regbase + RCR1);
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900134
Magnus Damm5e084a12009-02-24 22:11:03 +0900135 if (pending)
136 rtc_update_irq(rtc->rtc_dev, 1, RTC_AF | RTC_IRQF);
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900137
Magnus Damm5e084a12009-02-24 22:11:03 +0900138 return pending;
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900139}
140
Magnus Damm5e084a12009-02-24 22:11:03 +0900141static int __sh_rtc_periodic(struct sh_rtc *rtc)
Paul Mundt317a6102006-09-27 17:13:19 +0900142{
Magnus Damm5e084a12009-02-24 22:11:03 +0900143 unsigned int tmp, pending;
Paul Mundt317a6102006-09-27 17:13:19 +0900144
Angelo Castellob420b1a2008-03-06 12:50:53 +0900145 tmp = readb(rtc->regbase + RCR2);
Magnus Damm5e084a12009-02-24 22:11:03 +0900146 pending = tmp & RCR2_PEF;
Angelo Castellob420b1a2008-03-06 12:50:53 +0900147 tmp &= ~RCR2_PEF;
148 writeb(tmp, rtc->regbase + RCR2);
149
Magnus Damm5e084a12009-02-24 22:11:03 +0900150 if (!pending)
151 return 0;
152
Angelo Castellob420b1a2008-03-06 12:50:53 +0900153 /* Half period enabled than one skipped and the next notified */
154 if ((rtc->periodic_freq & PF_HP) && (rtc->periodic_freq & PF_COUNT))
155 rtc->periodic_freq &= ~PF_COUNT;
156 else {
157 if (rtc->periodic_freq & PF_HP)
158 rtc->periodic_freq |= PF_COUNT;
Alexandre Belloniec623ff2018-07-25 16:45:39 +0200159 rtc_update_irq(rtc->rtc_dev, 1, RTC_PF | RTC_IRQF);
Angelo Castellob420b1a2008-03-06 12:50:53 +0900160 }
Paul Mundt317a6102006-09-27 17:13:19 +0900161
Magnus Damm5e084a12009-02-24 22:11:03 +0900162 return pending;
163}
164
165static irqreturn_t sh_rtc_interrupt(int irq, void *dev_id)
166{
167 struct sh_rtc *rtc = dev_id;
168 int ret;
169
170 spin_lock(&rtc->lock);
171 ret = __sh_rtc_interrupt(rtc);
Paul Mundt317a6102006-09-27 17:13:19 +0900172 spin_unlock(&rtc->lock);
173
Magnus Damm5e084a12009-02-24 22:11:03 +0900174 return IRQ_RETVAL(ret);
175}
176
177static irqreturn_t sh_rtc_alarm(int irq, void *dev_id)
178{
179 struct sh_rtc *rtc = dev_id;
180 int ret;
181
182 spin_lock(&rtc->lock);
183 ret = __sh_rtc_alarm(rtc);
184 spin_unlock(&rtc->lock);
185
186 return IRQ_RETVAL(ret);
187}
188
189static irqreturn_t sh_rtc_periodic(int irq, void *dev_id)
190{
191 struct sh_rtc *rtc = dev_id;
192 int ret;
193
194 spin_lock(&rtc->lock);
195 ret = __sh_rtc_periodic(rtc);
196 spin_unlock(&rtc->lock);
197
198 return IRQ_RETVAL(ret);
199}
200
201static irqreturn_t sh_rtc_shared(int irq, void *dev_id)
202{
203 struct sh_rtc *rtc = dev_id;
204 int ret;
205
206 spin_lock(&rtc->lock);
207 ret = __sh_rtc_interrupt(rtc);
208 ret |= __sh_rtc_alarm(rtc);
209 ret |= __sh_rtc_periodic(rtc);
210 spin_unlock(&rtc->lock);
211
212 return IRQ_RETVAL(ret);
Paul Mundt317a6102006-09-27 17:13:19 +0900213}
214
Paul Mundt317a6102006-09-27 17:13:19 +0900215static inline void sh_rtc_setaie(struct device *dev, unsigned int enable)
216{
217 struct sh_rtc *rtc = dev_get_drvdata(dev);
218 unsigned int tmp;
219
220 spin_lock_irq(&rtc->lock);
221
222 tmp = readb(rtc->regbase + RCR1);
223
Paul Mundt063adc72009-04-16 14:12:22 +0900224 if (enable)
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900225 tmp |= RCR1_AIE;
Paul Mundt063adc72009-04-16 14:12:22 +0900226 else
227 tmp &= ~RCR1_AIE;
Paul Mundt317a6102006-09-27 17:13:19 +0900228
229 writeb(tmp, rtc->regbase + RCR1);
230
231 spin_unlock_irq(&rtc->lock);
232}
233
Paul Mundt317a6102006-09-27 17:13:19 +0900234static int sh_rtc_proc(struct device *dev, struct seq_file *seq)
235{
236 struct sh_rtc *rtc = dev_get_drvdata(dev);
237 unsigned int tmp;
238
239 tmp = readb(rtc->regbase + RCR1);
Angelo Castellob420b1a2008-03-06 12:50:53 +0900240 seq_printf(seq, "carry_IRQ\t: %s\n", (tmp & RCR1_CIE) ? "yes" : "no");
Paul Mundt317a6102006-09-27 17:13:19 +0900241
242 tmp = readb(rtc->regbase + RCR2);
243 seq_printf(seq, "periodic_IRQ\t: %s\n",
Angelo Castellob420b1a2008-03-06 12:50:53 +0900244 (tmp & RCR2_PESMASK) ? "yes" : "no");
Paul Mundt317a6102006-09-27 17:13:19 +0900245
246 return 0;
247}
248
Magnus Damm9cd88b92009-03-19 10:05:58 +0000249static inline void sh_rtc_setcie(struct device *dev, unsigned int enable)
250{
251 struct sh_rtc *rtc = dev_get_drvdata(dev);
252 unsigned int tmp;
253
254 spin_lock_irq(&rtc->lock);
255
256 tmp = readb(rtc->regbase + RCR1);
257
258 if (!enable)
259 tmp &= ~RCR1_CIE;
260 else
261 tmp |= RCR1_CIE;
262
263 writeb(tmp, rtc->regbase + RCR1);
264
265 spin_unlock_irq(&rtc->lock);
266}
267
John Stultz16380c12011-02-02 17:02:41 -0800268static int sh_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
269{
270 sh_rtc_setaie(dev, enabled);
271 return 0;
272}
273
Paul Mundt317a6102006-09-27 17:13:19 +0900274static int sh_rtc_read_time(struct device *dev, struct rtc_time *tm)
275{
Wolfram Sang85368bb2018-04-19 16:06:14 +0200276 struct sh_rtc *rtc = dev_get_drvdata(dev);
Paul Mundt317a6102006-09-27 17:13:19 +0900277 unsigned int sec128, sec2, yr, yr100, cf_bit;
278
279 do {
280 unsigned int tmp;
281
282 spin_lock_irq(&rtc->lock);
283
284 tmp = readb(rtc->regbase + RCR1);
285 tmp &= ~RCR1_CF; /* Clear CF-bit */
286 tmp |= RCR1_CIE;
287 writeb(tmp, rtc->regbase + RCR1);
288
289 sec128 = readb(rtc->regbase + R64CNT);
290
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700291 tm->tm_sec = bcd2bin(readb(rtc->regbase + RSECCNT));
292 tm->tm_min = bcd2bin(readb(rtc->regbase + RMINCNT));
293 tm->tm_hour = bcd2bin(readb(rtc->regbase + RHRCNT));
294 tm->tm_wday = bcd2bin(readb(rtc->regbase + RWKCNT));
295 tm->tm_mday = bcd2bin(readb(rtc->regbase + RDAYCNT));
296 tm->tm_mon = bcd2bin(readb(rtc->regbase + RMONCNT)) - 1;
Paul Mundt317a6102006-09-27 17:13:19 +0900297
Paul Mundtad89f872007-08-03 14:19:58 +0900298 if (rtc->capabilities & RTC_CAP_4_DIGIT_YEAR) {
299 yr = readw(rtc->regbase + RYRCNT);
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700300 yr100 = bcd2bin(yr >> 8);
Paul Mundtad89f872007-08-03 14:19:58 +0900301 yr &= 0xff;
302 } else {
303 yr = readb(rtc->regbase + RYRCNT);
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700304 yr100 = bcd2bin((yr == 0x99) ? 0x19 : 0x20);
Paul Mundtad89f872007-08-03 14:19:58 +0900305 }
Paul Mundt317a6102006-09-27 17:13:19 +0900306
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700307 tm->tm_year = (yr100 * 100 + bcd2bin(yr)) - 1900;
Paul Mundt317a6102006-09-27 17:13:19 +0900308
309 sec2 = readb(rtc->regbase + R64CNT);
310 cf_bit = readb(rtc->regbase + RCR1) & RCR1_CF;
311
312 spin_unlock_irq(&rtc->lock);
313 } while (cf_bit != 0 || ((sec128 ^ sec2) & RTC_BIT_INVERTED) != 0);
314
315#if RTC_BIT_INVERTED != 0
316 if ((sec128 & RTC_BIT_INVERTED))
317 tm->tm_sec--;
318#endif
319
Magnus Damm9cd88b92009-03-19 10:05:58 +0000320 /* only keep the carry interrupt enabled if UIE is on */
321 if (!(rtc->periodic_freq & PF_OXS))
322 sh_rtc_setcie(dev, 0);
323
Paul Mundt435c55d2007-05-08 11:56:27 +0900324 dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
Paul Mundt317a6102006-09-27 17:13:19 +0900325 "mday=%d, mon=%d, year=%d, wday=%d\n",
Harvey Harrison2a4e2b8782008-04-28 02:12:00 -0700326 __func__,
Paul Mundt317a6102006-09-27 17:13:19 +0900327 tm->tm_sec, tm->tm_min, tm->tm_hour,
Jamie Lenehana1614792006-12-08 14:49:30 +0900328 tm->tm_mday, tm->tm_mon + 1, tm->tm_year, tm->tm_wday);
Paul Mundt317a6102006-09-27 17:13:19 +0900329
Alexandre Belloni22652ba2018-02-19 16:23:56 +0100330 return 0;
Paul Mundt317a6102006-09-27 17:13:19 +0900331}
332
333static int sh_rtc_set_time(struct device *dev, struct rtc_time *tm)
334{
Wolfram Sang85368bb2018-04-19 16:06:14 +0200335 struct sh_rtc *rtc = dev_get_drvdata(dev);
Paul Mundt317a6102006-09-27 17:13:19 +0900336 unsigned int tmp;
337 int year;
338
339 spin_lock_irq(&rtc->lock);
340
341 /* Reset pre-scaler & stop RTC */
342 tmp = readb(rtc->regbase + RCR2);
343 tmp |= RCR2_RESET;
Markus Brunner699bc662007-07-26 17:31:28 +0900344 tmp &= ~RCR2_START;
Paul Mundt317a6102006-09-27 17:13:19 +0900345 writeb(tmp, rtc->regbase + RCR2);
346
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700347 writeb(bin2bcd(tm->tm_sec), rtc->regbase + RSECCNT);
348 writeb(bin2bcd(tm->tm_min), rtc->regbase + RMINCNT);
349 writeb(bin2bcd(tm->tm_hour), rtc->regbase + RHRCNT);
350 writeb(bin2bcd(tm->tm_wday), rtc->regbase + RWKCNT);
351 writeb(bin2bcd(tm->tm_mday), rtc->regbase + RDAYCNT);
352 writeb(bin2bcd(tm->tm_mon + 1), rtc->regbase + RMONCNT);
Paul Mundt317a6102006-09-27 17:13:19 +0900353
Paul Mundtad89f872007-08-03 14:19:58 +0900354 if (rtc->capabilities & RTC_CAP_4_DIGIT_YEAR) {
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700355 year = (bin2bcd((tm->tm_year + 1900) / 100) << 8) |
356 bin2bcd(tm->tm_year % 100);
Paul Mundtad89f872007-08-03 14:19:58 +0900357 writew(year, rtc->regbase + RYRCNT);
358 } else {
359 year = tm->tm_year % 100;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700360 writeb(bin2bcd(year), rtc->regbase + RYRCNT);
Paul Mundtad89f872007-08-03 14:19:58 +0900361 }
Paul Mundt317a6102006-09-27 17:13:19 +0900362
363 /* Start RTC */
364 tmp = readb(rtc->regbase + RCR2);
365 tmp &= ~RCR2_RESET;
366 tmp |= RCR2_RTCEN | RCR2_START;
367 writeb(tmp, rtc->regbase + RCR2);
368
369 spin_unlock_irq(&rtc->lock);
370
371 return 0;
372}
373
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900374static inline int sh_rtc_read_alarm_value(struct sh_rtc *rtc, int reg_off)
375{
376 unsigned int byte;
377 int value = 0xff; /* return 0xff for ignored values */
378
379 byte = readb(rtc->regbase + reg_off);
380 if (byte & AR_ENB) {
381 byte &= ~AR_ENB; /* strip the enable bit */
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700382 value = bcd2bin(byte);
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900383 }
384
385 return value;
386}
387
388static int sh_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
389{
Wolfram Sang85368bb2018-04-19 16:06:14 +0200390 struct sh_rtc *rtc = dev_get_drvdata(dev);
Angelo Castellob420b1a2008-03-06 12:50:53 +0900391 struct rtc_time *tm = &wkalrm->time;
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900392
393 spin_lock_irq(&rtc->lock);
394
395 tm->tm_sec = sh_rtc_read_alarm_value(rtc, RSECAR);
396 tm->tm_min = sh_rtc_read_alarm_value(rtc, RMINAR);
397 tm->tm_hour = sh_rtc_read_alarm_value(rtc, RHRAR);
398 tm->tm_wday = sh_rtc_read_alarm_value(rtc, RWKAR);
399 tm->tm_mday = sh_rtc_read_alarm_value(rtc, RDAYAR);
400 tm->tm_mon = sh_rtc_read_alarm_value(rtc, RMONAR);
401 if (tm->tm_mon > 0)
402 tm->tm_mon -= 1; /* RTC is 1-12, tm_mon is 0-11 */
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900403
David Brownell0d103e92007-01-10 23:15:32 -0800404 wkalrm->enabled = (readb(rtc->regbase + RCR1) & RCR1_AIE) ? 1 : 0;
405
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900406 spin_unlock_irq(&rtc->lock);
407
408 return 0;
409}
410
411static inline void sh_rtc_write_alarm_value(struct sh_rtc *rtc,
412 int value, int reg_off)
413{
414 /* < 0 for a value that is ignored */
415 if (value < 0)
416 writeb(0, rtc->regbase + reg_off);
417 else
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700418 writeb(bin2bcd(value) | AR_ENB, rtc->regbase + reg_off);
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900419}
420
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900421static int sh_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
422{
Wolfram Sang85368bb2018-04-19 16:06:14 +0200423 struct sh_rtc *rtc = dev_get_drvdata(dev);
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900424 unsigned int rcr1;
425 struct rtc_time *tm = &wkalrm->time;
Uwe Kleine-König84411892016-06-28 10:43:48 +0200426 int mon;
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900427
428 spin_lock_irq(&rtc->lock);
429
Jamie Lenehan15c945c2007-01-22 20:40:41 -0800430 /* disable alarm interrupt and clear the alarm flag */
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900431 rcr1 = readb(rtc->regbase + RCR1);
Angelo Castellob420b1a2008-03-06 12:50:53 +0900432 rcr1 &= ~(RCR1_AF | RCR1_AIE);
Jamie Lenehan15c945c2007-01-22 20:40:41 -0800433 writeb(rcr1, rtc->regbase + RCR1);
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900434
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900435 /* set alarm time */
436 sh_rtc_write_alarm_value(rtc, tm->tm_sec, RSECAR);
437 sh_rtc_write_alarm_value(rtc, tm->tm_min, RMINAR);
438 sh_rtc_write_alarm_value(rtc, tm->tm_hour, RHRAR);
439 sh_rtc_write_alarm_value(rtc, tm->tm_wday, RWKAR);
440 sh_rtc_write_alarm_value(rtc, tm->tm_mday, RDAYAR);
441 mon = tm->tm_mon;
442 if (mon >= 0)
443 mon += 1;
444 sh_rtc_write_alarm_value(rtc, mon, RMONAR);
445
Jamie Lenehan15c945c2007-01-22 20:40:41 -0800446 if (wkalrm->enabled) {
447 rcr1 |= RCR1_AIE;
448 writeb(rcr1, rtc->regbase + RCR1);
449 }
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900450
451 spin_unlock_irq(&rtc->lock);
452
453 return 0;
454}
455
Bhumika Goyal8bc57e72017-01-05 22:25:05 +0530456static const struct rtc_class_ops sh_rtc_ops = {
Paul Mundt317a6102006-09-27 17:13:19 +0900457 .read_time = sh_rtc_read_time,
458 .set_time = sh_rtc_set_time,
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900459 .read_alarm = sh_rtc_read_alarm,
460 .set_alarm = sh_rtc_set_alarm,
Paul Mundt317a6102006-09-27 17:13:19 +0900461 .proc = sh_rtc_proc,
John Stultz16380c12011-02-02 17:02:41 -0800462 .alarm_irq_enable = sh_rtc_alarm_irq_enable,
Paul Mundt317a6102006-09-27 17:13:19 +0900463};
464
Alessandro Zummo5c9740a2009-08-20 13:25:11 +0900465static int __init sh_rtc_probe(struct platform_device *pdev)
Paul Mundt317a6102006-09-27 17:13:19 +0900466{
467 struct sh_rtc *rtc;
468 struct resource *res;
Magnus Dammedf22472009-03-19 10:10:44 +0000469 struct rtc_time r;
Paul Mundt063adc72009-04-16 14:12:22 +0900470 char clk_name[6];
471 int clk_id, ret;
Paul Mundt317a6102006-09-27 17:13:19 +0900472
Jingoo Han0209aff2013-07-03 15:07:11 -0700473 rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
Paul Mundt317a6102006-09-27 17:13:19 +0900474 if (unlikely(!rtc))
475 return -ENOMEM;
476
477 spin_lock_init(&rtc->lock);
478
Angelo Castellob420b1a2008-03-06 12:50:53 +0900479 /* get periodic/carry/alarm irqs */
roel kluin2641dc92008-09-10 19:34:44 +0200480 ret = platform_get_irq(pdev, 0);
Anton Vorontsov2fac6672009-01-06 14:42:11 -0800481 if (unlikely(ret <= 0)) {
Magnus Damm5e084a12009-02-24 22:11:03 +0900482 dev_err(&pdev->dev, "No IRQ resource\n");
Jingoo Han0209aff2013-07-03 15:07:11 -0700483 return -ENOENT;
Paul Mundt317a6102006-09-27 17:13:19 +0900484 }
Paul Mundt063adc72009-04-16 14:12:22 +0900485
roel kluin2641dc92008-09-10 19:34:44 +0200486 rtc->periodic_irq = ret;
Magnus Damm5e084a12009-02-24 22:11:03 +0900487 rtc->carry_irq = platform_get_irq(pdev, 1);
488 rtc->alarm_irq = platform_get_irq(pdev, 2);
Paul Mundt317a6102006-09-27 17:13:19 +0900489
490 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Chris Brandtdab5aec2017-03-29 10:30:29 -0700491 if (!res)
492 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Paul Mundt317a6102006-09-27 17:13:19 +0900493 if (unlikely(res == NULL)) {
494 dev_err(&pdev->dev, "No IO resource\n");
Jingoo Han0209aff2013-07-03 15:07:11 -0700495 return -ENOENT;
Paul Mundt317a6102006-09-27 17:13:19 +0900496 }
497
Paul Mundt063adc72009-04-16 14:12:22 +0900498 rtc->regsize = resource_size(res);
Paul Mundt317a6102006-09-27 17:13:19 +0900499
Jingoo Han0209aff2013-07-03 15:07:11 -0700500 rtc->res = devm_request_mem_region(&pdev->dev, res->start,
501 rtc->regsize, pdev->name);
502 if (unlikely(!rtc->res))
503 return -EBUSY;
Paul Mundt317a6102006-09-27 17:13:19 +0900504
Jingoo Han0209aff2013-07-03 15:07:11 -0700505 rtc->regbase = devm_ioremap_nocache(&pdev->dev, rtc->res->start,
506 rtc->regsize);
507 if (unlikely(!rtc->regbase))
508 return -EINVAL;
Paul Mundt317a6102006-09-27 17:13:19 +0900509
Chris Brandtdab5aec2017-03-29 10:30:29 -0700510 if (!pdev->dev.of_node) {
511 clk_id = pdev->id;
512 /* With a single device, the clock id is still "rtc0" */
513 if (clk_id < 0)
514 clk_id = 0;
Paul Mundt063adc72009-04-16 14:12:22 +0900515
Chris Brandtdab5aec2017-03-29 10:30:29 -0700516 snprintf(clk_name, sizeof(clk_name), "rtc%d", clk_id);
517 } else
518 snprintf(clk_name, sizeof(clk_name), "fck");
Paul Mundt063adc72009-04-16 14:12:22 +0900519
Jingoo Han0209aff2013-07-03 15:07:11 -0700520 rtc->clk = devm_clk_get(&pdev->dev, clk_name);
Paul Mundt063adc72009-04-16 14:12:22 +0900521 if (IS_ERR(rtc->clk)) {
522 /*
523 * No error handling for rtc->clk intentionally, not all
524 * platforms will have a unique clock for the RTC, and
525 * the clk API can handle the struct clk pointer being
526 * NULL.
527 */
528 rtc->clk = NULL;
529 }
530
531 clk_enable(rtc->clk);
532
Paul Mundtad89f872007-08-03 14:19:58 +0900533 rtc->capabilities = RTC_DEF_CAPABILITIES;
Chris Brandtdab5aec2017-03-29 10:30:29 -0700534
535#ifdef CONFIG_SUPERH
Jingoo Hane58c18d2013-11-12 15:10:52 -0800536 if (dev_get_platdata(&pdev->dev)) {
537 struct sh_rtc_platform_info *pinfo =
538 dev_get_platdata(&pdev->dev);
Paul Mundtad89f872007-08-03 14:19:58 +0900539
540 /*
541 * Some CPUs have special capabilities in addition to the
542 * default set. Add those in here.
543 */
544 rtc->capabilities |= pinfo->capabilities;
545 }
Chris Brandtdab5aec2017-03-29 10:30:29 -0700546#endif
Paul Mundtad89f872007-08-03 14:19:58 +0900547
Magnus Damm5e084a12009-02-24 22:11:03 +0900548 if (rtc->carry_irq <= 0) {
549 /* register shared periodic/carry/alarm irq */
Jingoo Han0209aff2013-07-03 15:07:11 -0700550 ret = devm_request_irq(&pdev->dev, rtc->periodic_irq,
551 sh_rtc_shared, 0, "sh-rtc", rtc);
Magnus Damm5e084a12009-02-24 22:11:03 +0900552 if (unlikely(ret)) {
553 dev_err(&pdev->dev,
554 "request IRQ failed with %d, IRQ %d\n", ret,
555 rtc->periodic_irq);
556 goto err_unmap;
557 }
558 } else {
559 /* register periodic/carry/alarm irqs */
Jingoo Han0209aff2013-07-03 15:07:11 -0700560 ret = devm_request_irq(&pdev->dev, rtc->periodic_irq,
561 sh_rtc_periodic, 0, "sh-rtc period", rtc);
Magnus Damm5e084a12009-02-24 22:11:03 +0900562 if (unlikely(ret)) {
563 dev_err(&pdev->dev,
564 "request period IRQ failed with %d, IRQ %d\n",
565 ret, rtc->periodic_irq);
566 goto err_unmap;
567 }
Angelo Castellob420b1a2008-03-06 12:50:53 +0900568
Jingoo Han0209aff2013-07-03 15:07:11 -0700569 ret = devm_request_irq(&pdev->dev, rtc->carry_irq,
570 sh_rtc_interrupt, 0, "sh-rtc carry", rtc);
Magnus Damm5e084a12009-02-24 22:11:03 +0900571 if (unlikely(ret)) {
572 dev_err(&pdev->dev,
573 "request carry IRQ failed with %d, IRQ %d\n",
574 ret, rtc->carry_irq);
Magnus Damm5e084a12009-02-24 22:11:03 +0900575 goto err_unmap;
576 }
Angelo Castellob420b1a2008-03-06 12:50:53 +0900577
Jingoo Han0209aff2013-07-03 15:07:11 -0700578 ret = devm_request_irq(&pdev->dev, rtc->alarm_irq,
579 sh_rtc_alarm, 0, "sh-rtc alarm", rtc);
Magnus Damm5e084a12009-02-24 22:11:03 +0900580 if (unlikely(ret)) {
581 dev_err(&pdev->dev,
582 "request alarm IRQ failed with %d, IRQ %d\n",
583 ret, rtc->alarm_irq);
Magnus Damm5e084a12009-02-24 22:11:03 +0900584 goto err_unmap;
585 }
Angelo Castellob420b1a2008-03-06 12:50:53 +0900586 }
587
Alessandro Zummo5c9740a2009-08-20 13:25:11 +0900588 platform_set_drvdata(pdev, rtc);
589
Magnus Damm9cd88b92009-03-19 10:05:58 +0000590 /* everything disabled by default */
Magnus Damm9cd88b92009-03-19 10:05:58 +0000591 sh_rtc_setaie(&pdev->dev, 0);
592 sh_rtc_setcie(&pdev->dev, 0);
Magnus Dammedf22472009-03-19 10:10:44 +0000593
Jingoo Han0209aff2013-07-03 15:07:11 -0700594 rtc->rtc_dev = devm_rtc_device_register(&pdev->dev, "sh",
Alessandro Zummo5c9740a2009-08-20 13:25:11 +0900595 &sh_rtc_ops, THIS_MODULE);
596 if (IS_ERR(rtc->rtc_dev)) {
597 ret = PTR_ERR(rtc->rtc_dev);
Alessandro Zummo5c9740a2009-08-20 13:25:11 +0900598 goto err_unmap;
599 }
600
601 rtc->rtc_dev->max_user_freq = 256;
602
Magnus Dammedf22472009-03-19 10:10:44 +0000603 /* reset rtc to epoch 0 if time is invalid */
604 if (rtc_read_time(rtc->rtc_dev, &r) < 0) {
605 rtc_time_to_tm(0, &r);
606 rtc_set_time(rtc->rtc_dev, &r);
607 }
608
Magnus Damm7a8fe8e2009-03-19 10:14:41 +0000609 device_init_wakeup(&pdev->dev, 1);
Paul Mundt317a6102006-09-27 17:13:19 +0900610 return 0;
611
Paul Mundt03057942008-04-25 17:58:42 +0900612err_unmap:
Paul Mundt063adc72009-04-16 14:12:22 +0900613 clk_disable(rtc->clk);
Paul Mundt317a6102006-09-27 17:13:19 +0900614
615 return ret;
616}
617
Alessandro Zummo5c9740a2009-08-20 13:25:11 +0900618static int __exit sh_rtc_remove(struct platform_device *pdev)
Paul Mundt317a6102006-09-27 17:13:19 +0900619{
620 struct sh_rtc *rtc = platform_get_drvdata(pdev);
621
Paul Mundt317a6102006-09-27 17:13:19 +0900622 sh_rtc_setaie(&pdev->dev, 0);
Magnus Damm9cd88b92009-03-19 10:05:58 +0000623 sh_rtc_setcie(&pdev->dev, 0);
Paul Mundt317a6102006-09-27 17:13:19 +0900624
Paul Mundt063adc72009-04-16 14:12:22 +0900625 clk_disable(rtc->clk);
Paul Mundt317a6102006-09-27 17:13:19 +0900626
627 return 0;
628}
Magnus Dammfaa9fa82009-04-01 14:45:17 +0000629
630static void sh_rtc_set_irq_wake(struct device *dev, int enabled)
631{
Wolfram Sang85368bb2018-04-19 16:06:14 +0200632 struct sh_rtc *rtc = dev_get_drvdata(dev);
Magnus Dammfaa9fa82009-04-01 14:45:17 +0000633
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200634 irq_set_irq_wake(rtc->periodic_irq, enabled);
Paul Mundt063adc72009-04-16 14:12:22 +0900635
Magnus Dammfaa9fa82009-04-01 14:45:17 +0000636 if (rtc->carry_irq > 0) {
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200637 irq_set_irq_wake(rtc->carry_irq, enabled);
638 irq_set_irq_wake(rtc->alarm_irq, enabled);
Magnus Dammfaa9fa82009-04-01 14:45:17 +0000639 }
Magnus Dammfaa9fa82009-04-01 14:45:17 +0000640}
641
Arnd Bergmann5d05e812017-04-19 19:52:43 +0200642static int __maybe_unused sh_rtc_suspend(struct device *dev)
Magnus Dammfaa9fa82009-04-01 14:45:17 +0000643{
644 if (device_may_wakeup(dev))
645 sh_rtc_set_irq_wake(dev, 1);
646
647 return 0;
648}
649
Arnd Bergmann5d05e812017-04-19 19:52:43 +0200650static int __maybe_unused sh_rtc_resume(struct device *dev)
Magnus Dammfaa9fa82009-04-01 14:45:17 +0000651{
652 if (device_may_wakeup(dev))
653 sh_rtc_set_irq_wake(dev, 0);
654
655 return 0;
656}
657
Jingoo Han0ed50542013-04-29 16:20:00 -0700658static SIMPLE_DEV_PM_OPS(sh_rtc_pm_ops, sh_rtc_suspend, sh_rtc_resume);
Magnus Dammfaa9fa82009-04-01 14:45:17 +0000659
Chris Brandtdab5aec2017-03-29 10:30:29 -0700660static const struct of_device_id sh_rtc_of_match[] = {
661 { .compatible = "renesas,sh-rtc", },
662 { /* sentinel */ }
663};
664MODULE_DEVICE_TABLE(of, sh_rtc_of_match);
665
Paul Mundt317a6102006-09-27 17:13:19 +0900666static struct platform_driver sh_rtc_platform_driver = {
667 .driver = {
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900668 .name = DRV_NAME,
Jingoo Han0ed50542013-04-29 16:20:00 -0700669 .pm = &sh_rtc_pm_ops,
Chris Brandtdab5aec2017-03-29 10:30:29 -0700670 .of_match_table = sh_rtc_of_match,
Paul Mundt317a6102006-09-27 17:13:19 +0900671 },
Alessandro Zummo5c9740a2009-08-20 13:25:11 +0900672 .remove = __exit_p(sh_rtc_remove),
Paul Mundt317a6102006-09-27 17:13:19 +0900673};
674
Jingoo Handeed5a92013-04-29 16:18:51 -0700675module_platform_driver_probe(sh_rtc_platform_driver, sh_rtc_probe);
Paul Mundt317a6102006-09-27 17:13:19 +0900676
677MODULE_DESCRIPTION("SuperH on-chip RTC driver");
Angelo Castellob420b1a2008-03-06 12:50:53 +0900678MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>, "
679 "Jamie Lenehan <lenehan@twibble.org>, "
680 "Angelo Castello <angelo.castello@st.com>");
Kuninori Morimoto4ff6f022018-11-08 06:43:22 +0000681MODULE_LICENSE("GPL v2");
Kay Sieversad28a072008-04-10 21:29:25 -0700682MODULE_ALIAS("platform:" DRV_NAME);