blob: 7995f79d07e10d4e1f09888ea673a8ad95986cd9 [file] [log] [blame]
Ben Dooks1add6782006-07-01 04:36:26 -07001/* drivers/rtc/rtc-s3c.c
2 *
Atul Dahiyae48add82010-07-20 12:19:14 +05303 * Copyright (c) 2010 Samsung Electronics Co., Ltd.
4 * http://www.samsung.com/
5 *
Ben Dooks1add6782006-07-01 04:36:26 -07006 * Copyright (c) 2004,2006 Simtec Electronics
7 * Ben Dooks, <ben@simtec.co.uk>
8 * http://armlinux.simtec.co.uk/
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 *
14 * S3C2410/S3C2440/S3C24XX Internal RTC Driver
15*/
16
17#include <linux/module.h>
18#include <linux/fs.h>
19#include <linux/string.h>
20#include <linux/init.h>
21#include <linux/platform_device.h>
22#include <linux/interrupt.h>
23#include <linux/rtc.h>
24#include <linux/bcd.h>
25#include <linux/clk.h>
Robert P. J. Day9974b6e2008-02-06 01:38:42 -080026#include <linux/log2.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Thomas Abraham39ce4082011-10-24 14:49:04 +020028#include <linux/of.h>
Sachin Kamatdbd9acb2012-07-30 14:41:48 -070029#include <linux/uaccess.h>
30#include <linux/io.h>
Ben Dooks1add6782006-07-01 04:36:26 -070031
Ben Dooks1add6782006-07-01 04:36:26 -070032#include <asm/irq.h>
Arnd Bergmannb9d7c5d2013-03-05 12:04:37 +010033#include "rtc-s3c.h"
Ben Dooks1add6782006-07-01 04:36:26 -070034
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -070035enum s3c_cpu_type {
36 TYPE_S3C2410,
Heiko Stuebner25c1a242011-12-24 10:52:19 +090037 TYPE_S3C2416,
38 TYPE_S3C2443,
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -070039 TYPE_S3C64XX,
40};
41
Tushar Beherac3cba922012-04-12 12:49:14 -070042struct s3c_rtc_drv_data {
43 int cpu_type;
44};
45
Ben Dooks1add6782006-07-01 04:36:26 -070046/* I have yet to find an S3C implementation with more than one
47 * of these rtc blocks in */
48
Atul Dahiyae48add82010-07-20 12:19:14 +053049static struct clk *rtc_clk;
Ben Dooks1add6782006-07-01 04:36:26 -070050static void __iomem *s3c_rtc_base;
51static int s3c_rtc_alarmno = NO_IRQ;
52static int s3c_rtc_tickno = NO_IRQ;
Ben Dooks52cd4e52011-05-11 15:13:28 -070053static bool wake_en;
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -070054static enum s3c_cpu_type s3c_rtc_cpu_type;
Ben Dooks1add6782006-07-01 04:36:26 -070055
56static DEFINE_SPINLOCK(s3c_rtc_pie_lock);
Ben Dooks1add6782006-07-01 04:36:26 -070057
Donggeun Kim88cee8f2011-09-14 16:22:19 -070058static void s3c_rtc_alarm_clk_enable(bool enable)
59{
60 static DEFINE_SPINLOCK(s3c_rtc_alarm_clk_lock);
61 static bool alarm_clk_enabled;
62 unsigned long irq_flags;
63
64 spin_lock_irqsave(&s3c_rtc_alarm_clk_lock, irq_flags);
65 if (enable) {
66 if (!alarm_clk_enabled) {
67 clk_enable(rtc_clk);
68 alarm_clk_enabled = true;
69 }
70 } else {
71 if (alarm_clk_enabled) {
72 clk_disable(rtc_clk);
73 alarm_clk_enabled = false;
74 }
75 }
76 spin_unlock_irqrestore(&s3c_rtc_alarm_clk_lock, irq_flags);
77}
78
Ben Dooks1add6782006-07-01 04:36:26 -070079/* IRQ Handlers */
80
David Howells7d12e782006-10-05 14:55:46 +010081static irqreturn_t s3c_rtc_alarmirq(int irq, void *id)
Ben Dooks1add6782006-07-01 04:36:26 -070082{
83 struct rtc_device *rdev = id;
84
Donggeun Kimcefe4fb2011-07-25 17:13:32 -070085 clk_enable(rtc_clk);
David Brownellab6a2d72007-05-08 00:33:30 -070086 rtc_update_irq(rdev, 1, RTC_AF | RTC_IRQF);
Atul Dahiya2f3478f2010-07-20 16:02:51 +053087
88 if (s3c_rtc_cpu_type == TYPE_S3C64XX)
89 writeb(S3C2410_INTP_ALM, s3c_rtc_base + S3C2410_INTP);
90
Donggeun Kimcefe4fb2011-07-25 17:13:32 -070091 clk_disable(rtc_clk);
Donggeun Kim88cee8f2011-09-14 16:22:19 -070092
93 s3c_rtc_alarm_clk_enable(false);
94
Ben Dooks1add6782006-07-01 04:36:26 -070095 return IRQ_HANDLED;
96}
97
David Howells7d12e782006-10-05 14:55:46 +010098static irqreturn_t s3c_rtc_tickirq(int irq, void *id)
Ben Dooks1add6782006-07-01 04:36:26 -070099{
100 struct rtc_device *rdev = id;
101
Donggeun Kimcefe4fb2011-07-25 17:13:32 -0700102 clk_enable(rtc_clk);
Ben Dooks773be7e2008-07-23 21:30:45 -0700103 rtc_update_irq(rdev, 1, RTC_PF | RTC_IRQF);
Atul Dahiya2f3478f2010-07-20 16:02:51 +0530104
105 if (s3c_rtc_cpu_type == TYPE_S3C64XX)
106 writeb(S3C2410_INTP_TIC, s3c_rtc_base + S3C2410_INTP);
107
Donggeun Kimcefe4fb2011-07-25 17:13:32 -0700108 clk_disable(rtc_clk);
Ben Dooks1add6782006-07-01 04:36:26 -0700109 return IRQ_HANDLED;
110}
111
112/* Update control registers */
Axel Lin2ec38a02011-03-04 17:36:19 -0800113static int s3c_rtc_setaie(struct device *dev, unsigned int enabled)
Ben Dooks1add6782006-07-01 04:36:26 -0700114{
115 unsigned int tmp;
116
Jingoo Hand4a48c22013-02-21 16:45:06 -0800117 dev_dbg(dev, "%s: aie=%d\n", __func__, enabled);
Ben Dooks1add6782006-07-01 04:36:26 -0700118
Donggeun Kimcefe4fb2011-07-25 17:13:32 -0700119 clk_enable(rtc_clk);
Ben Dooks9a654512006-08-27 01:23:22 -0700120 tmp = readb(s3c_rtc_base + S3C2410_RTCALM) & ~S3C2410_RTCALM_ALMEN;
Ben Dooks1add6782006-07-01 04:36:26 -0700121
Axel Lin2ec38a02011-03-04 17:36:19 -0800122 if (enabled)
Ben Dooks1add6782006-07-01 04:36:26 -0700123 tmp |= S3C2410_RTCALM_ALMEN;
124
Ben Dooks9a654512006-08-27 01:23:22 -0700125 writeb(tmp, s3c_rtc_base + S3C2410_RTCALM);
Donggeun Kimcefe4fb2011-07-25 17:13:32 -0700126 clk_disable(rtc_clk);
Axel Lin2ec38a02011-03-04 17:36:19 -0800127
Donggeun Kim88cee8f2011-09-14 16:22:19 -0700128 s3c_rtc_alarm_clk_enable(enabled);
129
Axel Lin2ec38a02011-03-04 17:36:19 -0800130 return 0;
Ben Dooks1add6782006-07-01 04:36:26 -0700131}
132
Ben Dooks773be7e2008-07-23 21:30:45 -0700133static int s3c_rtc_setfreq(struct device *dev, int freq)
Ben Dooks1add6782006-07-01 04:36:26 -0700134{
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700135 struct platform_device *pdev = to_platform_device(dev);
136 struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
137 unsigned int tmp = 0;
Heiko Stuebner25c1a242011-12-24 10:52:19 +0900138 int val;
Ben Dooks1add6782006-07-01 04:36:26 -0700139
Jonathan Cameron5d2a5032009-01-06 14:42:12 -0800140 if (!is_power_of_2(freq))
141 return -EINVAL;
142
Donggeun Kimcefe4fb2011-07-25 17:13:32 -0700143 clk_enable(rtc_clk);
Ben Dooks1add6782006-07-01 04:36:26 -0700144 spin_lock_irq(&s3c_rtc_pie_lock);
Ben Dooks773be7e2008-07-23 21:30:45 -0700145
Heiko Stuebner25c1a242011-12-24 10:52:19 +0900146 if (s3c_rtc_cpu_type != TYPE_S3C64XX) {
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700147 tmp = readb(s3c_rtc_base + S3C2410_TICNT);
148 tmp &= S3C2410_TICNT_ENABLE;
149 }
150
Heiko Stuebner25c1a242011-12-24 10:52:19 +0900151 val = (rtc_dev->max_user_freq / freq) - 1;
152
153 if (s3c_rtc_cpu_type == TYPE_S3C2416 || s3c_rtc_cpu_type == TYPE_S3C2443) {
154 tmp |= S3C2443_TICNT_PART(val);
155 writel(S3C2443_TICNT1_PART(val), s3c_rtc_base + S3C2443_TICNT1);
156
157 if (s3c_rtc_cpu_type == TYPE_S3C2416)
158 writel(S3C2416_TICNT2_PART(val), s3c_rtc_base + S3C2416_TICNT2);
159 } else {
160 tmp |= val;
161 }
Ben Dooks1add6782006-07-01 04:36:26 -0700162
Atul Dahiya2f3478f2010-07-20 16:02:51 +0530163 writel(tmp, s3c_rtc_base + S3C2410_TICNT);
Ben Dooks1add6782006-07-01 04:36:26 -0700164 spin_unlock_irq(&s3c_rtc_pie_lock);
Donggeun Kimcefe4fb2011-07-25 17:13:32 -0700165 clk_disable(rtc_clk);
Ben Dooks773be7e2008-07-23 21:30:45 -0700166
167 return 0;
Ben Dooks1add6782006-07-01 04:36:26 -0700168}
169
170/* Time read/write */
171
172static int s3c_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
173{
174 unsigned int have_retried = 0;
Ben Dooks9a654512006-08-27 01:23:22 -0700175 void __iomem *base = s3c_rtc_base;
Ben Dooks1add6782006-07-01 04:36:26 -0700176
Donggeun Kimcefe4fb2011-07-25 17:13:32 -0700177 clk_enable(rtc_clk);
Ben Dooks1add6782006-07-01 04:36:26 -0700178 retry_get_time:
Ben Dooks9a654512006-08-27 01:23:22 -0700179 rtc_tm->tm_min = readb(base + S3C2410_RTCMIN);
180 rtc_tm->tm_hour = readb(base + S3C2410_RTCHOUR);
181 rtc_tm->tm_mday = readb(base + S3C2410_RTCDATE);
182 rtc_tm->tm_mon = readb(base + S3C2410_RTCMON);
183 rtc_tm->tm_year = readb(base + S3C2410_RTCYEAR);
184 rtc_tm->tm_sec = readb(base + S3C2410_RTCSEC);
Ben Dooks1add6782006-07-01 04:36:26 -0700185
Adam Buchbinder48fc7f72012-09-19 21:48:00 -0400186 /* the only way to work out whether the system was mid-update
Ben Dooks1add6782006-07-01 04:36:26 -0700187 * when we read it is to check the second counter, and if it
188 * is zero, then we re-try the entire read
189 */
190
191 if (rtc_tm->tm_sec == 0 && !have_retried) {
192 have_retried = 1;
193 goto retry_get_time;
194 }
195
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700196 rtc_tm->tm_sec = bcd2bin(rtc_tm->tm_sec);
197 rtc_tm->tm_min = bcd2bin(rtc_tm->tm_min);
198 rtc_tm->tm_hour = bcd2bin(rtc_tm->tm_hour);
199 rtc_tm->tm_mday = bcd2bin(rtc_tm->tm_mday);
200 rtc_tm->tm_mon = bcd2bin(rtc_tm->tm_mon);
201 rtc_tm->tm_year = bcd2bin(rtc_tm->tm_year);
Ben Dooks1add6782006-07-01 04:36:26 -0700202
203 rtc_tm->tm_year += 100;
MyungJoo Ham4e8896c2011-08-25 15:59:22 -0700204
Jingoo Hand4a48c22013-02-21 16:45:06 -0800205 dev_dbg(dev, "read time %04d.%02d.%02d %02d:%02d:%02d\n",
MyungJoo Ham4e8896c2011-08-25 15:59:22 -0700206 1900 + rtc_tm->tm_year, rtc_tm->tm_mon, rtc_tm->tm_mday,
207 rtc_tm->tm_hour, rtc_tm->tm_min, rtc_tm->tm_sec);
208
Ben Dooks1add6782006-07-01 04:36:26 -0700209 rtc_tm->tm_mon -= 1;
210
Donggeun Kimcefe4fb2011-07-25 17:13:32 -0700211 clk_disable(rtc_clk);
Kukjin Kim5b3ffdd2010-10-27 15:33:11 -0700212 return rtc_valid_tm(rtc_tm);
Ben Dooks1add6782006-07-01 04:36:26 -0700213}
214
215static int s3c_rtc_settime(struct device *dev, struct rtc_time *tm)
216{
Ben Dooks9a654512006-08-27 01:23:22 -0700217 void __iomem *base = s3c_rtc_base;
Ben Dooks641741e2006-08-27 01:23:27 -0700218 int year = tm->tm_year - 100;
Ben Dooks1add6782006-07-01 04:36:26 -0700219
Jingoo Hand4a48c22013-02-21 16:45:06 -0800220 dev_dbg(dev, "set time %04d.%02d.%02d %02d:%02d:%02d\n",
Kukjin Kim30ffc402010-10-27 15:33:09 -0700221 1900 + tm->tm_year, tm->tm_mon, tm->tm_mday,
Ben Dooks9a654512006-08-27 01:23:22 -0700222 tm->tm_hour, tm->tm_min, tm->tm_sec);
223
Ben Dooks641741e2006-08-27 01:23:27 -0700224 /* we get around y2k by simply not supporting it */
225
226 if (year < 0 || year >= 100) {
227 dev_err(dev, "rtc only supports 100 years\n");
228 return -EINVAL;
229 }
230
Jonghwan Choi2dbcd052011-12-08 14:34:02 -0800231 clk_enable(rtc_clk);
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700232 writeb(bin2bcd(tm->tm_sec), base + S3C2410_RTCSEC);
233 writeb(bin2bcd(tm->tm_min), base + S3C2410_RTCMIN);
234 writeb(bin2bcd(tm->tm_hour), base + S3C2410_RTCHOUR);
235 writeb(bin2bcd(tm->tm_mday), base + S3C2410_RTCDATE);
236 writeb(bin2bcd(tm->tm_mon + 1), base + S3C2410_RTCMON);
237 writeb(bin2bcd(year), base + S3C2410_RTCYEAR);
Donggeun Kimcefe4fb2011-07-25 17:13:32 -0700238 clk_disable(rtc_clk);
Ben Dooks1add6782006-07-01 04:36:26 -0700239
240 return 0;
241}
242
243static int s3c_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)
244{
245 struct rtc_time *alm_tm = &alrm->time;
Ben Dooks9a654512006-08-27 01:23:22 -0700246 void __iomem *base = s3c_rtc_base;
Ben Dooks1add6782006-07-01 04:36:26 -0700247 unsigned int alm_en;
248
Donggeun Kimcefe4fb2011-07-25 17:13:32 -0700249 clk_enable(rtc_clk);
Ben Dooks9a654512006-08-27 01:23:22 -0700250 alm_tm->tm_sec = readb(base + S3C2410_ALMSEC);
251 alm_tm->tm_min = readb(base + S3C2410_ALMMIN);
252 alm_tm->tm_hour = readb(base + S3C2410_ALMHOUR);
253 alm_tm->tm_mon = readb(base + S3C2410_ALMMON);
254 alm_tm->tm_mday = readb(base + S3C2410_ALMDATE);
255 alm_tm->tm_year = readb(base + S3C2410_ALMYEAR);
Ben Dooks1add6782006-07-01 04:36:26 -0700256
Ben Dooks9a654512006-08-27 01:23:22 -0700257 alm_en = readb(base + S3C2410_RTCALM);
Ben Dooks1add6782006-07-01 04:36:26 -0700258
David Brownella2db8df2006-12-13 00:35:08 -0800259 alrm->enabled = (alm_en & S3C2410_RTCALM_ALMEN) ? 1 : 0;
260
Jingoo Hand4a48c22013-02-21 16:45:06 -0800261 dev_dbg(dev, "read alarm %d, %04d.%02d.%02d %02d:%02d:%02d\n",
Ben Dooks1add6782006-07-01 04:36:26 -0700262 alm_en,
Kukjin Kim30ffc402010-10-27 15:33:09 -0700263 1900 + alm_tm->tm_year, alm_tm->tm_mon, alm_tm->tm_mday,
Ben Dooks1add6782006-07-01 04:36:26 -0700264 alm_tm->tm_hour, alm_tm->tm_min, alm_tm->tm_sec);
265
266
267 /* decode the alarm enable field */
268
269 if (alm_en & S3C2410_RTCALM_SECEN)
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700270 alm_tm->tm_sec = bcd2bin(alm_tm->tm_sec);
Ben Dooks1add6782006-07-01 04:36:26 -0700271 else
Changhwan Youndd061d12010-10-27 15:33:08 -0700272 alm_tm->tm_sec = -1;
Ben Dooks1add6782006-07-01 04:36:26 -0700273
274 if (alm_en & S3C2410_RTCALM_MINEN)
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700275 alm_tm->tm_min = bcd2bin(alm_tm->tm_min);
Ben Dooks1add6782006-07-01 04:36:26 -0700276 else
Changhwan Youndd061d12010-10-27 15:33:08 -0700277 alm_tm->tm_min = -1;
Ben Dooks1add6782006-07-01 04:36:26 -0700278
279 if (alm_en & S3C2410_RTCALM_HOUREN)
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700280 alm_tm->tm_hour = bcd2bin(alm_tm->tm_hour);
Ben Dooks1add6782006-07-01 04:36:26 -0700281 else
Changhwan Youndd061d12010-10-27 15:33:08 -0700282 alm_tm->tm_hour = -1;
Ben Dooks1add6782006-07-01 04:36:26 -0700283
284 if (alm_en & S3C2410_RTCALM_DAYEN)
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700285 alm_tm->tm_mday = bcd2bin(alm_tm->tm_mday);
Ben Dooks1add6782006-07-01 04:36:26 -0700286 else
Changhwan Youndd061d12010-10-27 15:33:08 -0700287 alm_tm->tm_mday = -1;
Ben Dooks1add6782006-07-01 04:36:26 -0700288
289 if (alm_en & S3C2410_RTCALM_MONEN) {
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700290 alm_tm->tm_mon = bcd2bin(alm_tm->tm_mon);
Ben Dooks1add6782006-07-01 04:36:26 -0700291 alm_tm->tm_mon -= 1;
292 } else {
Changhwan Youndd061d12010-10-27 15:33:08 -0700293 alm_tm->tm_mon = -1;
Ben Dooks1add6782006-07-01 04:36:26 -0700294 }
295
296 if (alm_en & S3C2410_RTCALM_YEAREN)
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700297 alm_tm->tm_year = bcd2bin(alm_tm->tm_year);
Ben Dooks1add6782006-07-01 04:36:26 -0700298 else
Changhwan Youndd061d12010-10-27 15:33:08 -0700299 alm_tm->tm_year = -1;
Ben Dooks1add6782006-07-01 04:36:26 -0700300
Donggeun Kimcefe4fb2011-07-25 17:13:32 -0700301 clk_disable(rtc_clk);
Ben Dooks1add6782006-07-01 04:36:26 -0700302 return 0;
303}
304
305static int s3c_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
306{
307 struct rtc_time *tm = &alrm->time;
Ben Dooks9a654512006-08-27 01:23:22 -0700308 void __iomem *base = s3c_rtc_base;
Ben Dooks1add6782006-07-01 04:36:26 -0700309 unsigned int alrm_en;
310
Donggeun Kimcefe4fb2011-07-25 17:13:32 -0700311 clk_enable(rtc_clk);
Jingoo Hand4a48c22013-02-21 16:45:06 -0800312 dev_dbg(dev, "s3c_rtc_setalarm: %d, %04d.%02d.%02d %02d:%02d:%02d\n",
Ben Dooks1add6782006-07-01 04:36:26 -0700313 alrm->enabled,
MyungJoo Ham4e8896c2011-08-25 15:59:22 -0700314 1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
Kukjin Kim30ffc402010-10-27 15:33:09 -0700315 tm->tm_hour, tm->tm_min, tm->tm_sec);
Ben Dooks1add6782006-07-01 04:36:26 -0700316
Ben Dooks9a654512006-08-27 01:23:22 -0700317 alrm_en = readb(base + S3C2410_RTCALM) & S3C2410_RTCALM_ALMEN;
318 writeb(0x00, base + S3C2410_RTCALM);
Ben Dooks1add6782006-07-01 04:36:26 -0700319
320 if (tm->tm_sec < 60 && tm->tm_sec >= 0) {
321 alrm_en |= S3C2410_RTCALM_SECEN;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700322 writeb(bin2bcd(tm->tm_sec), base + S3C2410_ALMSEC);
Ben Dooks1add6782006-07-01 04:36:26 -0700323 }
324
325 if (tm->tm_min < 60 && tm->tm_min >= 0) {
326 alrm_en |= S3C2410_RTCALM_MINEN;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700327 writeb(bin2bcd(tm->tm_min), base + S3C2410_ALMMIN);
Ben Dooks1add6782006-07-01 04:36:26 -0700328 }
329
330 if (tm->tm_hour < 24 && tm->tm_hour >= 0) {
331 alrm_en |= S3C2410_RTCALM_HOUREN;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700332 writeb(bin2bcd(tm->tm_hour), base + S3C2410_ALMHOUR);
Ben Dooks1add6782006-07-01 04:36:26 -0700333 }
334
Jingoo Hand4a48c22013-02-21 16:45:06 -0800335 dev_dbg(dev, "setting S3C2410_RTCALM to %08x\n", alrm_en);
Ben Dooks1add6782006-07-01 04:36:26 -0700336
Ben Dooks9a654512006-08-27 01:23:22 -0700337 writeb(alrm_en, base + S3C2410_RTCALM);
Ben Dooks1add6782006-07-01 04:36:26 -0700338
Axel Lin2ec38a02011-03-04 17:36:19 -0800339 s3c_rtc_setaie(dev, alrm->enabled);
Ben Dooks1add6782006-07-01 04:36:26 -0700340
Donggeun Kimcefe4fb2011-07-25 17:13:32 -0700341 clk_disable(rtc_clk);
Ben Dooks1add6782006-07-01 04:36:26 -0700342 return 0;
343}
344
Ben Dooks1add6782006-07-01 04:36:26 -0700345static int s3c_rtc_proc(struct device *dev, struct seq_file *seq)
346{
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700347 unsigned int ticnt;
Ben Dooks1add6782006-07-01 04:36:26 -0700348
Donggeun Kimcefe4fb2011-07-25 17:13:32 -0700349 clk_enable(rtc_clk);
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700350 if (s3c_rtc_cpu_type == TYPE_S3C64XX) {
Changhwan Younf61ae672010-10-27 15:33:06 -0700351 ticnt = readw(s3c_rtc_base + S3C2410_RTCCON);
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700352 ticnt &= S3C64XX_RTCCON_TICEN;
353 } else {
354 ticnt = readb(s3c_rtc_base + S3C2410_TICNT);
355 ticnt &= S3C2410_TICNT_ENABLE;
356 }
357
358 seq_printf(seq, "periodic_IRQ\t: %s\n", ticnt ? "yes" : "no");
Donggeun Kimcefe4fb2011-07-25 17:13:32 -0700359 clk_disable(rtc_clk);
Ben Dooks1add6782006-07-01 04:36:26 -0700360 return 0;
361}
362
David Brownellff8371a2006-09-30 23:28:17 -0700363static const struct rtc_class_ops s3c_rtcops = {
Ben Dooks1add6782006-07-01 04:36:26 -0700364 .read_time = s3c_rtc_gettime,
365 .set_time = s3c_rtc_settime,
366 .read_alarm = s3c_rtc_getalarm,
367 .set_alarm = s3c_rtc_setalarm,
Changhwan Youne6eb5242010-10-27 15:33:09 -0700368 .proc = s3c_rtc_proc,
369 .alarm_irq_enable = s3c_rtc_setaie,
Ben Dooks1add6782006-07-01 04:36:26 -0700370};
371
372static void s3c_rtc_enable(struct platform_device *pdev, int en)
373{
Ben Dooks9a654512006-08-27 01:23:22 -0700374 void __iomem *base = s3c_rtc_base;
Ben Dooks1add6782006-07-01 04:36:26 -0700375 unsigned int tmp;
376
377 if (s3c_rtc_base == NULL)
378 return;
379
Donggeun Kimcefe4fb2011-07-25 17:13:32 -0700380 clk_enable(rtc_clk);
Ben Dooks1add6782006-07-01 04:36:26 -0700381 if (!en) {
Changhwan Younf61ae672010-10-27 15:33:06 -0700382 tmp = readw(base + S3C2410_RTCCON);
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700383 if (s3c_rtc_cpu_type == TYPE_S3C64XX)
384 tmp &= ~S3C64XX_RTCCON_TICEN;
385 tmp &= ~S3C2410_RTCCON_RTCEN;
Changhwan Younf61ae672010-10-27 15:33:06 -0700386 writew(tmp, base + S3C2410_RTCCON);
Ben Dooks1add6782006-07-01 04:36:26 -0700387
Heiko Stuebner25c1a242011-12-24 10:52:19 +0900388 if (s3c_rtc_cpu_type != TYPE_S3C64XX) {
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700389 tmp = readb(base + S3C2410_TICNT);
390 tmp &= ~S3C2410_TICNT_ENABLE;
391 writeb(tmp, base + S3C2410_TICNT);
392 }
Ben Dooks1add6782006-07-01 04:36:26 -0700393 } else {
394 /* re-enable the device, and check it is ok */
395
Changhwan Younf61ae672010-10-27 15:33:06 -0700396 if ((readw(base+S3C2410_RTCCON) & S3C2410_RTCCON_RTCEN) == 0) {
Ben Dooks1add6782006-07-01 04:36:26 -0700397 dev_info(&pdev->dev, "rtc disabled, re-enabling\n");
398
Changhwan Younf61ae672010-10-27 15:33:06 -0700399 tmp = readw(base + S3C2410_RTCCON);
400 writew(tmp | S3C2410_RTCCON_RTCEN,
401 base + S3C2410_RTCCON);
Ben Dooks1add6782006-07-01 04:36:26 -0700402 }
403
Changhwan Younf61ae672010-10-27 15:33:06 -0700404 if ((readw(base + S3C2410_RTCCON) & S3C2410_RTCCON_CNTSEL)) {
Ben Dooks1add6782006-07-01 04:36:26 -0700405 dev_info(&pdev->dev, "removing RTCCON_CNTSEL\n");
406
Changhwan Younf61ae672010-10-27 15:33:06 -0700407 tmp = readw(base + S3C2410_RTCCON);
408 writew(tmp & ~S3C2410_RTCCON_CNTSEL,
409 base + S3C2410_RTCCON);
Ben Dooks1add6782006-07-01 04:36:26 -0700410 }
411
Changhwan Younf61ae672010-10-27 15:33:06 -0700412 if ((readw(base + S3C2410_RTCCON) & S3C2410_RTCCON_CLKRST)) {
Ben Dooks1add6782006-07-01 04:36:26 -0700413 dev_info(&pdev->dev, "removing RTCCON_CLKRST\n");
414
Changhwan Younf61ae672010-10-27 15:33:06 -0700415 tmp = readw(base + S3C2410_RTCCON);
416 writew(tmp & ~S3C2410_RTCCON_CLKRST,
417 base + S3C2410_RTCCON);
Ben Dooks1add6782006-07-01 04:36:26 -0700418 }
419 }
Donggeun Kimcefe4fb2011-07-25 17:13:32 -0700420 clk_disable(rtc_clk);
Ben Dooks1add6782006-07-01 04:36:26 -0700421}
422
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800423static int s3c_rtc_remove(struct platform_device *dev)
Ben Dooks1add6782006-07-01 04:36:26 -0700424{
425 struct rtc_device *rtc = platform_get_drvdata(dev);
426
427 platform_set_drvdata(dev, NULL);
428 rtc_device_unregister(rtc);
429
Axel Lin2ec38a02011-03-04 17:36:19 -0800430 s3c_rtc_setaie(&dev->dev, 0);
Ben Dooks1add6782006-07-01 04:36:26 -0700431
Atul Dahiyae48add82010-07-20 12:19:14 +0530432 rtc_clk = NULL;
433
Ben Dooks1add6782006-07-01 04:36:26 -0700434 return 0;
435}
436
Heiko Stuebnerd2524ca2011-12-24 10:52:14 +0900437static const struct of_device_id s3c_rtc_dt_match[];
438
439static inline int s3c_rtc_get_driver_data(struct platform_device *pdev)
440{
441#ifdef CONFIG_OF
Tushar Beherac3cba922012-04-12 12:49:14 -0700442 struct s3c_rtc_drv_data *data;
Heiko Stuebnerd2524ca2011-12-24 10:52:14 +0900443 if (pdev->dev.of_node) {
444 const struct of_device_id *match;
445 match = of_match_node(s3c_rtc_dt_match, pdev->dev.of_node);
Tushar Beherac3cba922012-04-12 12:49:14 -0700446 data = (struct s3c_rtc_drv_data *) match->data;
447 return data->cpu_type;
Heiko Stuebnerd2524ca2011-12-24 10:52:14 +0900448 }
449#endif
450 return platform_get_device_id(pdev)->driver_data;
451}
452
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800453static int s3c_rtc_probe(struct platform_device *pdev)
Ben Dooks1add6782006-07-01 04:36:26 -0700454{
455 struct rtc_device *rtc;
Changhwan Youne1df9622010-10-27 15:33:10 -0700456 struct rtc_time rtc_tm;
Ben Dooks1add6782006-07-01 04:36:26 -0700457 struct resource *res;
458 int ret;
Heiko Stuebner25c1a242011-12-24 10:52:19 +0900459 int tmp;
Ben Dooks1add6782006-07-01 04:36:26 -0700460
Jingoo Hand4a48c22013-02-21 16:45:06 -0800461 dev_dbg(&pdev->dev, "%s: probe=%p\n", __func__, pdev);
Ben Dooks1add6782006-07-01 04:36:26 -0700462
463 /* find the IRQs */
464
465 s3c_rtc_tickno = platform_get_irq(pdev, 1);
466 if (s3c_rtc_tickno < 0) {
467 dev_err(&pdev->dev, "no irq for rtc tick\n");
Sachin Kamat1ee8c0c2012-10-04 17:14:14 -0700468 return s3c_rtc_tickno;
Ben Dooks1add6782006-07-01 04:36:26 -0700469 }
470
471 s3c_rtc_alarmno = platform_get_irq(pdev, 0);
472 if (s3c_rtc_alarmno < 0) {
473 dev_err(&pdev->dev, "no irq for alarm\n");
Sachin Kamat1ee8c0c2012-10-04 17:14:14 -0700474 return s3c_rtc_alarmno;
Ben Dooks1add6782006-07-01 04:36:26 -0700475 }
476
Jingoo Hand4a48c22013-02-21 16:45:06 -0800477 dev_dbg(&pdev->dev, "s3c2410_rtc: tick irq %d, alarm irq %d\n",
Ben Dooks1add6782006-07-01 04:36:26 -0700478 s3c_rtc_tickno, s3c_rtc_alarmno);
479
480 /* get the memory region */
481
482 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
483 if (res == NULL) {
484 dev_err(&pdev->dev, "failed to get memory region resource\n");
485 return -ENOENT;
486 }
487
Thierry Reding8cbce1e2013-01-21 11:09:17 +0100488 s3c_rtc_base = devm_ioremap_resource(&pdev->dev, res);
489 if (IS_ERR(s3c_rtc_base))
490 return PTR_ERR(s3c_rtc_base);
Ben Dooks1add6782006-07-01 04:36:26 -0700491
Tushar Behera1b997322012-12-17 16:02:48 -0800492 rtc_clk = devm_clk_get(&pdev->dev, "rtc");
Atul Dahiyae48add82010-07-20 12:19:14 +0530493 if (IS_ERR(rtc_clk)) {
494 dev_err(&pdev->dev, "failed to find rtc clock source\n");
495 ret = PTR_ERR(rtc_clk);
496 rtc_clk = NULL;
Tushar Behera1b997322012-12-17 16:02:48 -0800497 return ret;
Atul Dahiyae48add82010-07-20 12:19:14 +0530498 }
499
500 clk_enable(rtc_clk);
501
Ben Dooks1add6782006-07-01 04:36:26 -0700502 /* check to see if everything is setup correctly */
503
504 s3c_rtc_enable(pdev, 1);
505
Jingoo Hand4a48c22013-02-21 16:45:06 -0800506 dev_dbg(&pdev->dev, "s3c2410_rtc: RTCCON=%02x\n",
Changhwan Younf61ae672010-10-27 15:33:06 -0700507 readw(s3c_rtc_base + S3C2410_RTCCON));
Ben Dooks1add6782006-07-01 04:36:26 -0700508
Yauhen Kharuzhy51b76162008-10-29 14:01:16 -0700509 device_init_wakeup(&pdev->dev, 1);
510
Ben Dooks1add6782006-07-01 04:36:26 -0700511 /* register RTC and exit */
512
513 rtc = rtc_device_register("s3c", &pdev->dev, &s3c_rtcops,
514 THIS_MODULE);
515
516 if (IS_ERR(rtc)) {
517 dev_err(&pdev->dev, "cannot attach rtc\n");
518 ret = PTR_ERR(rtc);
519 goto err_nortc;
520 }
521
Heiko Stuebnerd2524ca2011-12-24 10:52:14 +0900522 s3c_rtc_cpu_type = s3c_rtc_get_driver_data(pdev);
Maurus Cuelenaereeaa6e4d2010-06-04 14:14:46 -0700523
Taekgyun Ko051fe542010-07-29 13:00:42 +0900524 /* Check RTC Time */
525
Changhwan Youne1df9622010-10-27 15:33:10 -0700526 s3c_rtc_gettime(NULL, &rtc_tm);
Taekgyun Ko051fe542010-07-29 13:00:42 +0900527
Changhwan Youne1df9622010-10-27 15:33:10 -0700528 if (rtc_valid_tm(&rtc_tm)) {
529 rtc_tm.tm_year = 100;
530 rtc_tm.tm_mon = 0;
531 rtc_tm.tm_mday = 1;
532 rtc_tm.tm_hour = 0;
533 rtc_tm.tm_min = 0;
534 rtc_tm.tm_sec = 0;
535
536 s3c_rtc_settime(NULL, &rtc_tm);
537
538 dev_warn(&pdev->dev, "warning: invalid RTC value so initializing it\n");
Taekgyun Ko051fe542010-07-29 13:00:42 +0900539 }
540
Heiko Stuebner25c1a242011-12-24 10:52:19 +0900541 if (s3c_rtc_cpu_type != TYPE_S3C2410)
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700542 rtc->max_user_freq = 32768;
543 else
544 rtc->max_user_freq = 128;
545
Heiko Stuebner25c1a242011-12-24 10:52:19 +0900546 if (s3c_rtc_cpu_type == TYPE_S3C2416 || s3c_rtc_cpu_type == TYPE_S3C2443) {
547 tmp = readw(s3c_rtc_base + S3C2410_RTCCON);
548 tmp |= S3C2443_RTCCON_TICSEL;
549 writew(tmp, s3c_rtc_base + S3C2410_RTCCON);
550 }
551
Ben Dooks1add6782006-07-01 04:36:26 -0700552 platform_set_drvdata(pdev, rtc);
Maurus Cuelenaeree893de52010-06-04 14:14:44 -0700553
554 s3c_rtc_setfreq(&pdev->dev, 1);
555
Tushar Behera1b997322012-12-17 16:02:48 -0800556 ret = devm_request_irq(&pdev->dev, s3c_rtc_alarmno, s3c_rtc_alarmirq,
Yong Zhang2f6e5f92012-03-23 15:02:34 -0700557 0, "s3c2410-rtc alarm", rtc);
MyungJoo Ham62d17602011-08-25 15:59:24 -0700558 if (ret) {
559 dev_err(&pdev->dev, "IRQ%d error %d\n", s3c_rtc_alarmno, ret);
560 goto err_alarm_irq;
561 }
562
Tushar Behera1b997322012-12-17 16:02:48 -0800563 ret = devm_request_irq(&pdev->dev, s3c_rtc_tickno, s3c_rtc_tickirq,
Yong Zhang2f6e5f92012-03-23 15:02:34 -0700564 0, "s3c2410-rtc tick", rtc);
MyungJoo Ham62d17602011-08-25 15:59:24 -0700565 if (ret) {
566 dev_err(&pdev->dev, "IRQ%d error %d\n", s3c_rtc_tickno, ret);
Tushar Behera1b997322012-12-17 16:02:48 -0800567 goto err_alarm_irq;
MyungJoo Ham62d17602011-08-25 15:59:24 -0700568 }
569
Donggeun Kimcefe4fb2011-07-25 17:13:32 -0700570 clk_disable(rtc_clk);
571
Ben Dooks1add6782006-07-01 04:36:26 -0700572 return 0;
573
MyungJoo Ham62d17602011-08-25 15:59:24 -0700574 err_alarm_irq:
575 platform_set_drvdata(pdev, NULL);
576 rtc_device_unregister(rtc);
577
Ben Dooks1add6782006-07-01 04:36:26 -0700578 err_nortc:
579 s3c_rtc_enable(pdev, 0);
Atul Dahiyae48add82010-07-20 12:19:14 +0530580 clk_disable(rtc_clk);
Atul Dahiyae48add82010-07-20 12:19:14 +0530581
Ben Dooks1add6782006-07-01 04:36:26 -0700582 return ret;
583}
584
585#ifdef CONFIG_PM
586
587/* RTC Power management control */
588
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700589static int ticnt_save, ticnt_en_save;
Ben Dooks1add6782006-07-01 04:36:26 -0700590
591static int s3c_rtc_suspend(struct platform_device *pdev, pm_message_t state)
592{
Donggeun Kimcefe4fb2011-07-25 17:13:32 -0700593 clk_enable(rtc_clk);
Ben Dooks1add6782006-07-01 04:36:26 -0700594 /* save TICNT for anyone using periodic interrupts */
Ben Dooks9a654512006-08-27 01:23:22 -0700595 ticnt_save = readb(s3c_rtc_base + S3C2410_TICNT);
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700596 if (s3c_rtc_cpu_type == TYPE_S3C64XX) {
Changhwan Younf61ae672010-10-27 15:33:06 -0700597 ticnt_en_save = readw(s3c_rtc_base + S3C2410_RTCCON);
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700598 ticnt_en_save &= S3C64XX_RTCCON_TICEN;
599 }
Ben Dooks1add6782006-07-01 04:36:26 -0700600 s3c_rtc_enable(pdev, 0);
Vladimir Zapolskiyf501ed52010-09-22 13:05:13 -0700601
Ben Dooks52cd4e52011-05-11 15:13:28 -0700602 if (device_may_wakeup(&pdev->dev) && !wake_en) {
603 if (enable_irq_wake(s3c_rtc_alarmno) == 0)
604 wake_en = true;
605 else
606 dev_err(&pdev->dev, "enable_irq_wake failed\n");
607 }
Donggeun Kimcefe4fb2011-07-25 17:13:32 -0700608 clk_disable(rtc_clk);
Vladimir Zapolskiyf501ed52010-09-22 13:05:13 -0700609
Ben Dooks1add6782006-07-01 04:36:26 -0700610 return 0;
611}
612
613static int s3c_rtc_resume(struct platform_device *pdev)
614{
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700615 unsigned int tmp;
616
Donggeun Kimcefe4fb2011-07-25 17:13:32 -0700617 clk_enable(rtc_clk);
Ben Dooks1add6782006-07-01 04:36:26 -0700618 s3c_rtc_enable(pdev, 1);
Ben Dooks9a654512006-08-27 01:23:22 -0700619 writeb(ticnt_save, s3c_rtc_base + S3C2410_TICNT);
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700620 if (s3c_rtc_cpu_type == TYPE_S3C64XX && ticnt_en_save) {
Changhwan Younf61ae672010-10-27 15:33:06 -0700621 tmp = readw(s3c_rtc_base + S3C2410_RTCCON);
622 writew(tmp | ticnt_en_save, s3c_rtc_base + S3C2410_RTCCON);
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700623 }
Vladimir Zapolskiyf501ed52010-09-22 13:05:13 -0700624
Ben Dooks52cd4e52011-05-11 15:13:28 -0700625 if (device_may_wakeup(&pdev->dev) && wake_en) {
Vladimir Zapolskiyf501ed52010-09-22 13:05:13 -0700626 disable_irq_wake(s3c_rtc_alarmno);
Ben Dooks52cd4e52011-05-11 15:13:28 -0700627 wake_en = false;
628 }
Donggeun Kimcefe4fb2011-07-25 17:13:32 -0700629 clk_disable(rtc_clk);
Vladimir Zapolskiyf501ed52010-09-22 13:05:13 -0700630
Ben Dooks1add6782006-07-01 04:36:26 -0700631 return 0;
632}
633#else
634#define s3c_rtc_suspend NULL
635#define s3c_rtc_resume NULL
636#endif
637
Sachin Kamatecb41a72012-05-29 15:07:40 -0700638#ifdef CONFIG_OF
Tushar Beherac3cba922012-04-12 12:49:14 -0700639static struct s3c_rtc_drv_data s3c_rtc_drv_data_array[] = {
640 [TYPE_S3C2410] = { TYPE_S3C2410 },
641 [TYPE_S3C2416] = { TYPE_S3C2416 },
642 [TYPE_S3C2443] = { TYPE_S3C2443 },
643 [TYPE_S3C64XX] = { TYPE_S3C64XX },
644};
645
Thomas Abraham39ce4082011-10-24 14:49:04 +0200646static const struct of_device_id s3c_rtc_dt_match[] = {
Heiko Stuebnerd2524ca2011-12-24 10:52:14 +0900647 {
Tushar Beheracd1e6f92012-04-12 12:49:14 -0700648 .compatible = "samsung,s3c2410-rtc",
Tushar Beherac3cba922012-04-12 12:49:14 -0700649 .data = &s3c_rtc_drv_data_array[TYPE_S3C2410],
Heiko Stuebnerd2524ca2011-12-24 10:52:14 +0900650 }, {
Tushar Beheracd1e6f92012-04-12 12:49:14 -0700651 .compatible = "samsung,s3c2416-rtc",
Tushar Beherac3cba922012-04-12 12:49:14 -0700652 .data = &s3c_rtc_drv_data_array[TYPE_S3C2416],
Heiko Stuebner25c1a242011-12-24 10:52:19 +0900653 }, {
Tushar Beheracd1e6f92012-04-12 12:49:14 -0700654 .compatible = "samsung,s3c2443-rtc",
Tushar Beherac3cba922012-04-12 12:49:14 -0700655 .data = &s3c_rtc_drv_data_array[TYPE_S3C2443],
Heiko Stuebner25c1a242011-12-24 10:52:19 +0900656 }, {
Tushar Beheracd1e6f92012-04-12 12:49:14 -0700657 .compatible = "samsung,s3c6410-rtc",
Tushar Beherac3cba922012-04-12 12:49:14 -0700658 .data = &s3c_rtc_drv_data_array[TYPE_S3C64XX],
Heiko Stuebnerd2524ca2011-12-24 10:52:14 +0900659 },
Thomas Abraham39ce4082011-10-24 14:49:04 +0200660 {},
661};
662MODULE_DEVICE_TABLE(of, s3c_rtc_dt_match);
Thomas Abraham39ce4082011-10-24 14:49:04 +0200663#endif
664
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700665static struct platform_device_id s3c_rtc_driver_ids[] = {
666 {
667 .name = "s3c2410-rtc",
668 .driver_data = TYPE_S3C2410,
669 }, {
Heiko Stuebner25c1a242011-12-24 10:52:19 +0900670 .name = "s3c2416-rtc",
671 .driver_data = TYPE_S3C2416,
672 }, {
673 .name = "s3c2443-rtc",
674 .driver_data = TYPE_S3C2443,
675 }, {
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700676 .name = "s3c64xx-rtc",
677 .driver_data = TYPE_S3C64XX,
678 },
679 { }
680};
681
682MODULE_DEVICE_TABLE(platform, s3c_rtc_driver_ids);
683
684static struct platform_driver s3c_rtc_driver = {
Ben Dooks1add6782006-07-01 04:36:26 -0700685 .probe = s3c_rtc_probe,
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800686 .remove = s3c_rtc_remove,
Ben Dooks1add6782006-07-01 04:36:26 -0700687 .suspend = s3c_rtc_suspend,
688 .resume = s3c_rtc_resume,
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700689 .id_table = s3c_rtc_driver_ids,
Ben Dooks1add6782006-07-01 04:36:26 -0700690 .driver = {
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700691 .name = "s3c-rtc",
Ben Dooks1add6782006-07-01 04:36:26 -0700692 .owner = THIS_MODULE,
Sachin Kamat04a373f2012-12-17 16:02:52 -0800693 .of_match_table = of_match_ptr(s3c_rtc_dt_match),
Ben Dooks1add6782006-07-01 04:36:26 -0700694 },
695};
696
Axel Lin0c4eae62012-01-10 15:10:48 -0800697module_platform_driver(s3c_rtc_driver);
Ben Dooks1add6782006-07-01 04:36:26 -0700698
699MODULE_DESCRIPTION("Samsung S3C RTC Driver");
700MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
701MODULE_LICENSE("GPL");
Kay Sieversad28a072008-04-10 21:29:25 -0700702MODULE_ALIAS("platform:s3c2410-rtc");