blob: cf953ecbfca934e8b9c7058f976610ff092016ae [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>
Ben Dooks1add6782006-07-01 04:36:26 -070028
Russell Kinga09e64f2008-08-05 16:14:15 +010029#include <mach/hardware.h>
Ben Dooks1add6782006-07-01 04:36:26 -070030#include <asm/uaccess.h>
31#include <asm/io.h>
32#include <asm/irq.h>
Ben Dookse2cd00c2008-10-30 10:14:34 +000033#include <plat/regs-rtc.h>
Ben Dooks1add6782006-07-01 04:36:26 -070034
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -070035enum s3c_cpu_type {
36 TYPE_S3C2410,
37 TYPE_S3C64XX,
38};
39
Ben Dooks1add6782006-07-01 04:36:26 -070040/* I have yet to find an S3C implementation with more than one
41 * of these rtc blocks in */
42
43static struct resource *s3c_rtc_mem;
44
Atul Dahiyae48add82010-07-20 12:19:14 +053045static struct clk *rtc_clk;
Ben Dooks1add6782006-07-01 04:36:26 -070046static void __iomem *s3c_rtc_base;
47static int s3c_rtc_alarmno = NO_IRQ;
48static int s3c_rtc_tickno = NO_IRQ;
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -070049static enum s3c_cpu_type s3c_rtc_cpu_type;
Ben Dooks1add6782006-07-01 04:36:26 -070050
51static DEFINE_SPINLOCK(s3c_rtc_pie_lock);
Ben Dooks1add6782006-07-01 04:36:26 -070052
53/* IRQ Handlers */
54
David Howells7d12e782006-10-05 14:55:46 +010055static irqreturn_t s3c_rtc_alarmirq(int irq, void *id)
Ben Dooks1add6782006-07-01 04:36:26 -070056{
57 struct rtc_device *rdev = id;
58
David Brownellab6a2d72007-05-08 00:33:30 -070059 rtc_update_irq(rdev, 1, RTC_AF | RTC_IRQF);
Atul Dahiya2f3478f2010-07-20 16:02:51 +053060
61 if (s3c_rtc_cpu_type == TYPE_S3C64XX)
62 writeb(S3C2410_INTP_ALM, s3c_rtc_base + S3C2410_INTP);
63
Ben Dooks1add6782006-07-01 04:36:26 -070064 return IRQ_HANDLED;
65}
66
David Howells7d12e782006-10-05 14:55:46 +010067static irqreturn_t s3c_rtc_tickirq(int irq, void *id)
Ben Dooks1add6782006-07-01 04:36:26 -070068{
69 struct rtc_device *rdev = id;
70
Ben Dooks773be7e2008-07-23 21:30:45 -070071 rtc_update_irq(rdev, 1, RTC_PF | RTC_IRQF);
Atul Dahiya2f3478f2010-07-20 16:02:51 +053072
73 if (s3c_rtc_cpu_type == TYPE_S3C64XX)
74 writeb(S3C2410_INTP_TIC, s3c_rtc_base + S3C2410_INTP);
75
Ben Dooks1add6782006-07-01 04:36:26 -070076 return IRQ_HANDLED;
77}
78
79/* Update control registers */
80static void s3c_rtc_setaie(int to)
81{
82 unsigned int tmp;
83
Harvey Harrison2a4e2b82008-04-28 02:12:00 -070084 pr_debug("%s: aie=%d\n", __func__, to);
Ben Dooks1add6782006-07-01 04:36:26 -070085
Ben Dooks9a654512006-08-27 01:23:22 -070086 tmp = readb(s3c_rtc_base + S3C2410_RTCALM) & ~S3C2410_RTCALM_ALMEN;
Ben Dooks1add6782006-07-01 04:36:26 -070087
88 if (to)
89 tmp |= S3C2410_RTCALM_ALMEN;
90
Ben Dooks9a654512006-08-27 01:23:22 -070091 writeb(tmp, s3c_rtc_base + S3C2410_RTCALM);
Ben Dooks1add6782006-07-01 04:36:26 -070092}
93
Ben Dooks773be7e2008-07-23 21:30:45 -070094static int s3c_rtc_setpie(struct device *dev, int enabled)
Ben Dooks1add6782006-07-01 04:36:26 -070095{
96 unsigned int tmp;
97
Ben Dooks773be7e2008-07-23 21:30:45 -070098 pr_debug("%s: pie=%d\n", __func__, enabled);
Ben Dooks1add6782006-07-01 04:36:26 -070099
100 spin_lock_irq(&s3c_rtc_pie_lock);
Ben Dooks1add6782006-07-01 04:36:26 -0700101
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700102 if (s3c_rtc_cpu_type == TYPE_S3C64XX) {
Changhwan Younf61ae672010-10-27 15:33:06 -0700103 tmp = readw(s3c_rtc_base + S3C2410_RTCCON);
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700104 tmp &= ~S3C64XX_RTCCON_TICEN;
Ben Dooks1add6782006-07-01 04:36:26 -0700105
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700106 if (enabled)
107 tmp |= S3C64XX_RTCCON_TICEN;
108
Atul Dahiya2f3478f2010-07-20 16:02:51 +0530109 writew(tmp, s3c_rtc_base + S3C2410_RTCCON);
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700110 } else {
111 tmp = readb(s3c_rtc_base + S3C2410_TICNT);
112 tmp &= ~S3C2410_TICNT_ENABLE;
113
114 if (enabled)
115 tmp |= S3C2410_TICNT_ENABLE;
116
117 writeb(tmp, s3c_rtc_base + S3C2410_TICNT);
118 }
119
Ben Dooks1add6782006-07-01 04:36:26 -0700120 spin_unlock_irq(&s3c_rtc_pie_lock);
Ben Dooks773be7e2008-07-23 21:30:45 -0700121
122 return 0;
Ben Dooks1add6782006-07-01 04:36:26 -0700123}
124
Ben Dooks773be7e2008-07-23 21:30:45 -0700125static int s3c_rtc_setfreq(struct device *dev, int freq)
Ben Dooks1add6782006-07-01 04:36:26 -0700126{
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700127 struct platform_device *pdev = to_platform_device(dev);
128 struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
129 unsigned int tmp = 0;
Ben Dooks1add6782006-07-01 04:36:26 -0700130
Jonathan Cameron5d2a5032009-01-06 14:42:12 -0800131 if (!is_power_of_2(freq))
132 return -EINVAL;
133
Ben Dooks1add6782006-07-01 04:36:26 -0700134 spin_lock_irq(&s3c_rtc_pie_lock);
Ben Dooks773be7e2008-07-23 21:30:45 -0700135
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700136 if (s3c_rtc_cpu_type == TYPE_S3C2410) {
137 tmp = readb(s3c_rtc_base + S3C2410_TICNT);
138 tmp &= S3C2410_TICNT_ENABLE;
139 }
140
141 tmp |= (rtc_dev->max_user_freq / freq)-1;
Ben Dooks1add6782006-07-01 04:36:26 -0700142
Atul Dahiya2f3478f2010-07-20 16:02:51 +0530143 writel(tmp, s3c_rtc_base + S3C2410_TICNT);
Ben Dooks1add6782006-07-01 04:36:26 -0700144 spin_unlock_irq(&s3c_rtc_pie_lock);
Ben Dooks773be7e2008-07-23 21:30:45 -0700145
146 return 0;
Ben Dooks1add6782006-07-01 04:36:26 -0700147}
148
149/* Time read/write */
150
151static int s3c_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
152{
153 unsigned int have_retried = 0;
Ben Dooks9a654512006-08-27 01:23:22 -0700154 void __iomem *base = s3c_rtc_base;
Ben Dooks1add6782006-07-01 04:36:26 -0700155
156 retry_get_time:
Ben Dooks9a654512006-08-27 01:23:22 -0700157 rtc_tm->tm_min = readb(base + S3C2410_RTCMIN);
158 rtc_tm->tm_hour = readb(base + S3C2410_RTCHOUR);
159 rtc_tm->tm_mday = readb(base + S3C2410_RTCDATE);
160 rtc_tm->tm_mon = readb(base + S3C2410_RTCMON);
161 rtc_tm->tm_year = readb(base + S3C2410_RTCYEAR);
162 rtc_tm->tm_sec = readb(base + S3C2410_RTCSEC);
Ben Dooks1add6782006-07-01 04:36:26 -0700163
164 /* the only way to work out wether the system was mid-update
165 * when we read it is to check the second counter, and if it
166 * is zero, then we re-try the entire read
167 */
168
169 if (rtc_tm->tm_sec == 0 && !have_retried) {
170 have_retried = 1;
171 goto retry_get_time;
172 }
173
Kukjin Kim30ffc402010-10-27 15:33:09 -0700174 pr_debug("read time %04d.%02d.%02d %02d:%02d:%02d\n",
175 1900 + rtc_tm->tm_year, rtc_tm->tm_mon, rtc_tm->tm_mday,
Ben Dooks1add6782006-07-01 04:36:26 -0700176 rtc_tm->tm_hour, rtc_tm->tm_min, rtc_tm->tm_sec);
177
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700178 rtc_tm->tm_sec = bcd2bin(rtc_tm->tm_sec);
179 rtc_tm->tm_min = bcd2bin(rtc_tm->tm_min);
180 rtc_tm->tm_hour = bcd2bin(rtc_tm->tm_hour);
181 rtc_tm->tm_mday = bcd2bin(rtc_tm->tm_mday);
182 rtc_tm->tm_mon = bcd2bin(rtc_tm->tm_mon);
183 rtc_tm->tm_year = bcd2bin(rtc_tm->tm_year);
Ben Dooks1add6782006-07-01 04:36:26 -0700184
185 rtc_tm->tm_year += 100;
186 rtc_tm->tm_mon -= 1;
187
Kukjin Kim5b3ffdd2010-10-27 15:33:11 -0700188 return rtc_valid_tm(rtc_tm);
Ben Dooks1add6782006-07-01 04:36:26 -0700189}
190
191static int s3c_rtc_settime(struct device *dev, struct rtc_time *tm)
192{
Ben Dooks9a654512006-08-27 01:23:22 -0700193 void __iomem *base = s3c_rtc_base;
Ben Dooks641741e2006-08-27 01:23:27 -0700194 int year = tm->tm_year - 100;
Ben Dooks1add6782006-07-01 04:36:26 -0700195
Kukjin Kim30ffc402010-10-27 15:33:09 -0700196 pr_debug("set time %04d.%02d.%02d %02d:%02d:%02d\n",
197 1900 + tm->tm_year, tm->tm_mon, tm->tm_mday,
Ben Dooks9a654512006-08-27 01:23:22 -0700198 tm->tm_hour, tm->tm_min, tm->tm_sec);
199
Ben Dooks641741e2006-08-27 01:23:27 -0700200 /* we get around y2k by simply not supporting it */
201
202 if (year < 0 || year >= 100) {
203 dev_err(dev, "rtc only supports 100 years\n");
204 return -EINVAL;
205 }
206
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700207 writeb(bin2bcd(tm->tm_sec), base + S3C2410_RTCSEC);
208 writeb(bin2bcd(tm->tm_min), base + S3C2410_RTCMIN);
209 writeb(bin2bcd(tm->tm_hour), base + S3C2410_RTCHOUR);
210 writeb(bin2bcd(tm->tm_mday), base + S3C2410_RTCDATE);
211 writeb(bin2bcd(tm->tm_mon + 1), base + S3C2410_RTCMON);
212 writeb(bin2bcd(year), base + S3C2410_RTCYEAR);
Ben Dooks1add6782006-07-01 04:36:26 -0700213
214 return 0;
215}
216
217static int s3c_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)
218{
219 struct rtc_time *alm_tm = &alrm->time;
Ben Dooks9a654512006-08-27 01:23:22 -0700220 void __iomem *base = s3c_rtc_base;
Ben Dooks1add6782006-07-01 04:36:26 -0700221 unsigned int alm_en;
222
Ben Dooks9a654512006-08-27 01:23:22 -0700223 alm_tm->tm_sec = readb(base + S3C2410_ALMSEC);
224 alm_tm->tm_min = readb(base + S3C2410_ALMMIN);
225 alm_tm->tm_hour = readb(base + S3C2410_ALMHOUR);
226 alm_tm->tm_mon = readb(base + S3C2410_ALMMON);
227 alm_tm->tm_mday = readb(base + S3C2410_ALMDATE);
228 alm_tm->tm_year = readb(base + S3C2410_ALMYEAR);
Ben Dooks1add6782006-07-01 04:36:26 -0700229
Ben Dooks9a654512006-08-27 01:23:22 -0700230 alm_en = readb(base + S3C2410_RTCALM);
Ben Dooks1add6782006-07-01 04:36:26 -0700231
David Brownella2db8df2006-12-13 00:35:08 -0800232 alrm->enabled = (alm_en & S3C2410_RTCALM_ALMEN) ? 1 : 0;
233
Kukjin Kim30ffc402010-10-27 15:33:09 -0700234 pr_debug("read alarm %d, %04d.%02d.%02d %02d:%02d:%02d\n",
Ben Dooks1add6782006-07-01 04:36:26 -0700235 alm_en,
Kukjin Kim30ffc402010-10-27 15:33:09 -0700236 1900 + alm_tm->tm_year, alm_tm->tm_mon, alm_tm->tm_mday,
Ben Dooks1add6782006-07-01 04:36:26 -0700237 alm_tm->tm_hour, alm_tm->tm_min, alm_tm->tm_sec);
238
239
240 /* decode the alarm enable field */
241
242 if (alm_en & S3C2410_RTCALM_SECEN)
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700243 alm_tm->tm_sec = bcd2bin(alm_tm->tm_sec);
Ben Dooks1add6782006-07-01 04:36:26 -0700244 else
Changhwan Youndd061d12010-10-27 15:33:08 -0700245 alm_tm->tm_sec = -1;
Ben Dooks1add6782006-07-01 04:36:26 -0700246
247 if (alm_en & S3C2410_RTCALM_MINEN)
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700248 alm_tm->tm_min = bcd2bin(alm_tm->tm_min);
Ben Dooks1add6782006-07-01 04:36:26 -0700249 else
Changhwan Youndd061d12010-10-27 15:33:08 -0700250 alm_tm->tm_min = -1;
Ben Dooks1add6782006-07-01 04:36:26 -0700251
252 if (alm_en & S3C2410_RTCALM_HOUREN)
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700253 alm_tm->tm_hour = bcd2bin(alm_tm->tm_hour);
Ben Dooks1add6782006-07-01 04:36:26 -0700254 else
Changhwan Youndd061d12010-10-27 15:33:08 -0700255 alm_tm->tm_hour = -1;
Ben Dooks1add6782006-07-01 04:36:26 -0700256
257 if (alm_en & S3C2410_RTCALM_DAYEN)
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700258 alm_tm->tm_mday = bcd2bin(alm_tm->tm_mday);
Ben Dooks1add6782006-07-01 04:36:26 -0700259 else
Changhwan Youndd061d12010-10-27 15:33:08 -0700260 alm_tm->tm_mday = -1;
Ben Dooks1add6782006-07-01 04:36:26 -0700261
262 if (alm_en & S3C2410_RTCALM_MONEN) {
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700263 alm_tm->tm_mon = bcd2bin(alm_tm->tm_mon);
Ben Dooks1add6782006-07-01 04:36:26 -0700264 alm_tm->tm_mon -= 1;
265 } else {
Changhwan Youndd061d12010-10-27 15:33:08 -0700266 alm_tm->tm_mon = -1;
Ben Dooks1add6782006-07-01 04:36:26 -0700267 }
268
269 if (alm_en & S3C2410_RTCALM_YEAREN)
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700270 alm_tm->tm_year = bcd2bin(alm_tm->tm_year);
Ben Dooks1add6782006-07-01 04:36:26 -0700271 else
Changhwan Youndd061d12010-10-27 15:33:08 -0700272 alm_tm->tm_year = -1;
Ben Dooks1add6782006-07-01 04:36:26 -0700273
274 return 0;
275}
276
277static int s3c_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
278{
279 struct rtc_time *tm = &alrm->time;
Ben Dooks9a654512006-08-27 01:23:22 -0700280 void __iomem *base = s3c_rtc_base;
Ben Dooks1add6782006-07-01 04:36:26 -0700281 unsigned int alrm_en;
282
Kukjin Kim30ffc402010-10-27 15:33:09 -0700283 pr_debug("s3c_rtc_setalarm: %d, %04d.%02d.%02d %02d:%02d:%02d\n",
Ben Dooks1add6782006-07-01 04:36:26 -0700284 alrm->enabled,
Kukjin Kim30ffc402010-10-27 15:33:09 -0700285 1900 + tm->tm_year, tm->tm_mon, tm->tm_mday,
286 tm->tm_hour, tm->tm_min, tm->tm_sec);
Ben Dooks1add6782006-07-01 04:36:26 -0700287
288
Ben Dooks9a654512006-08-27 01:23:22 -0700289 alrm_en = readb(base + S3C2410_RTCALM) & S3C2410_RTCALM_ALMEN;
290 writeb(0x00, base + S3C2410_RTCALM);
Ben Dooks1add6782006-07-01 04:36:26 -0700291
292 if (tm->tm_sec < 60 && tm->tm_sec >= 0) {
293 alrm_en |= S3C2410_RTCALM_SECEN;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700294 writeb(bin2bcd(tm->tm_sec), base + S3C2410_ALMSEC);
Ben Dooks1add6782006-07-01 04:36:26 -0700295 }
296
297 if (tm->tm_min < 60 && tm->tm_min >= 0) {
298 alrm_en |= S3C2410_RTCALM_MINEN;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700299 writeb(bin2bcd(tm->tm_min), base + S3C2410_ALMMIN);
Ben Dooks1add6782006-07-01 04:36:26 -0700300 }
301
302 if (tm->tm_hour < 24 && tm->tm_hour >= 0) {
303 alrm_en |= S3C2410_RTCALM_HOUREN;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700304 writeb(bin2bcd(tm->tm_hour), base + S3C2410_ALMHOUR);
Ben Dooks1add6782006-07-01 04:36:26 -0700305 }
306
307 pr_debug("setting S3C2410_RTCALM to %08x\n", alrm_en);
308
Ben Dooks9a654512006-08-27 01:23:22 -0700309 writeb(alrm_en, base + S3C2410_RTCALM);
Ben Dooks1add6782006-07-01 04:36:26 -0700310
Ben Dooks773be7e2008-07-23 21:30:45 -0700311 s3c_rtc_setaie(alrm->enabled);
Ben Dooks1add6782006-07-01 04:36:26 -0700312
Ben Dooks1add6782006-07-01 04:36:26 -0700313 return 0;
314}
315
Ben Dooks1add6782006-07-01 04:36:26 -0700316static int s3c_rtc_proc(struct device *dev, struct seq_file *seq)
317{
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700318 unsigned int ticnt;
Ben Dooks1add6782006-07-01 04:36:26 -0700319
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700320 if (s3c_rtc_cpu_type == TYPE_S3C64XX) {
Changhwan Younf61ae672010-10-27 15:33:06 -0700321 ticnt = readw(s3c_rtc_base + S3C2410_RTCCON);
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700322 ticnt &= S3C64XX_RTCCON_TICEN;
323 } else {
324 ticnt = readb(s3c_rtc_base + S3C2410_TICNT);
325 ticnt &= S3C2410_TICNT_ENABLE;
326 }
327
328 seq_printf(seq, "periodic_IRQ\t: %s\n", ticnt ? "yes" : "no");
Ben Dooks1add6782006-07-01 04:36:26 -0700329 return 0;
330}
331
332static int s3c_rtc_open(struct device *dev)
333{
334 struct platform_device *pdev = to_platform_device(dev);
335 struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
336 int ret;
337
338 ret = request_irq(s3c_rtc_alarmno, s3c_rtc_alarmirq,
Thomas Gleixner38515e92007-02-14 00:33:16 -0800339 IRQF_DISABLED, "s3c2410-rtc alarm", rtc_dev);
Ben Dooks1add6782006-07-01 04:36:26 -0700340
341 if (ret) {
342 dev_err(dev, "IRQ%d error %d\n", s3c_rtc_alarmno, ret);
343 return ret;
344 }
345
346 ret = request_irq(s3c_rtc_tickno, s3c_rtc_tickirq,
Thomas Gleixner38515e92007-02-14 00:33:16 -0800347 IRQF_DISABLED, "s3c2410-rtc tick", rtc_dev);
Ben Dooks1add6782006-07-01 04:36:26 -0700348
349 if (ret) {
350 dev_err(dev, "IRQ%d error %d\n", s3c_rtc_tickno, ret);
351 goto tick_err;
352 }
353
354 return ret;
355
356 tick_err:
357 free_irq(s3c_rtc_alarmno, rtc_dev);
358 return ret;
359}
360
361static void s3c_rtc_release(struct device *dev)
362{
363 struct platform_device *pdev = to_platform_device(dev);
364 struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
365
366 /* do not clear AIE here, it may be needed for wake */
367
Ben Dooks773be7e2008-07-23 21:30:45 -0700368 s3c_rtc_setpie(dev, 0);
Ben Dooks1add6782006-07-01 04:36:26 -0700369 free_irq(s3c_rtc_alarmno, rtc_dev);
370 free_irq(s3c_rtc_tickno, rtc_dev);
371}
372
David Brownellff8371a2006-09-30 23:28:17 -0700373static const struct rtc_class_ops s3c_rtcops = {
Ben Dooks1add6782006-07-01 04:36:26 -0700374 .open = s3c_rtc_open,
375 .release = s3c_rtc_release,
Ben Dooks1add6782006-07-01 04:36:26 -0700376 .read_time = s3c_rtc_gettime,
377 .set_time = s3c_rtc_settime,
378 .read_alarm = s3c_rtc_getalarm,
379 .set_alarm = s3c_rtc_setalarm,
Ben Dooks773be7e2008-07-23 21:30:45 -0700380 .irq_set_freq = s3c_rtc_setfreq,
381 .irq_set_state = s3c_rtc_setpie,
Changhwan Youne6eb5242010-10-27 15:33:09 -0700382 .proc = s3c_rtc_proc,
383 .alarm_irq_enable = s3c_rtc_setaie,
Ben Dooks1add6782006-07-01 04:36:26 -0700384};
385
386static void s3c_rtc_enable(struct platform_device *pdev, int en)
387{
Ben Dooks9a654512006-08-27 01:23:22 -0700388 void __iomem *base = s3c_rtc_base;
Ben Dooks1add6782006-07-01 04:36:26 -0700389 unsigned int tmp;
390
391 if (s3c_rtc_base == NULL)
392 return;
393
394 if (!en) {
Changhwan Younf61ae672010-10-27 15:33:06 -0700395 tmp = readw(base + S3C2410_RTCCON);
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700396 if (s3c_rtc_cpu_type == TYPE_S3C64XX)
397 tmp &= ~S3C64XX_RTCCON_TICEN;
398 tmp &= ~S3C2410_RTCCON_RTCEN;
Changhwan Younf61ae672010-10-27 15:33:06 -0700399 writew(tmp, base + S3C2410_RTCCON);
Ben Dooks1add6782006-07-01 04:36:26 -0700400
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700401 if (s3c_rtc_cpu_type == TYPE_S3C2410) {
402 tmp = readb(base + S3C2410_TICNT);
403 tmp &= ~S3C2410_TICNT_ENABLE;
404 writeb(tmp, base + S3C2410_TICNT);
405 }
Ben Dooks1add6782006-07-01 04:36:26 -0700406 } else {
407 /* re-enable the device, and check it is ok */
408
Changhwan Younf61ae672010-10-27 15:33:06 -0700409 if ((readw(base+S3C2410_RTCCON) & S3C2410_RTCCON_RTCEN) == 0) {
Ben Dooks1add6782006-07-01 04:36:26 -0700410 dev_info(&pdev->dev, "rtc disabled, re-enabling\n");
411
Changhwan Younf61ae672010-10-27 15:33:06 -0700412 tmp = readw(base + S3C2410_RTCCON);
413 writew(tmp | S3C2410_RTCCON_RTCEN,
414 base + S3C2410_RTCCON);
Ben Dooks1add6782006-07-01 04:36:26 -0700415 }
416
Changhwan Younf61ae672010-10-27 15:33:06 -0700417 if ((readw(base + S3C2410_RTCCON) & S3C2410_RTCCON_CNTSEL)) {
Ben Dooks1add6782006-07-01 04:36:26 -0700418 dev_info(&pdev->dev, "removing RTCCON_CNTSEL\n");
419
Changhwan Younf61ae672010-10-27 15:33:06 -0700420 tmp = readw(base + S3C2410_RTCCON);
421 writew(tmp & ~S3C2410_RTCCON_CNTSEL,
422 base + S3C2410_RTCCON);
Ben Dooks1add6782006-07-01 04:36:26 -0700423 }
424
Changhwan Younf61ae672010-10-27 15:33:06 -0700425 if ((readw(base + S3C2410_RTCCON) & S3C2410_RTCCON_CLKRST)) {
Ben Dooks1add6782006-07-01 04:36:26 -0700426 dev_info(&pdev->dev, "removing RTCCON_CLKRST\n");
427
Changhwan Younf61ae672010-10-27 15:33:06 -0700428 tmp = readw(base + S3C2410_RTCCON);
429 writew(tmp & ~S3C2410_RTCCON_CLKRST,
430 base + S3C2410_RTCCON);
Ben Dooks1add6782006-07-01 04:36:26 -0700431 }
432 }
433}
434
Ben Dooks4cd0c5c2008-07-23 21:30:44 -0700435static int __devexit s3c_rtc_remove(struct platform_device *dev)
Ben Dooks1add6782006-07-01 04:36:26 -0700436{
437 struct rtc_device *rtc = platform_get_drvdata(dev);
438
439 platform_set_drvdata(dev, NULL);
440 rtc_device_unregister(rtc);
441
Ben Dooks773be7e2008-07-23 21:30:45 -0700442 s3c_rtc_setpie(&dev->dev, 0);
Ben Dooks1add6782006-07-01 04:36:26 -0700443 s3c_rtc_setaie(0);
444
Atul Dahiyae48add82010-07-20 12:19:14 +0530445 clk_disable(rtc_clk);
446 clk_put(rtc_clk);
447 rtc_clk = NULL;
448
Ben Dooks1add6782006-07-01 04:36:26 -0700449 iounmap(s3c_rtc_base);
450 release_resource(s3c_rtc_mem);
451 kfree(s3c_rtc_mem);
452
453 return 0;
454}
455
Ben Dooks4cd0c5c2008-07-23 21:30:44 -0700456static int __devinit s3c_rtc_probe(struct platform_device *pdev)
Ben Dooks1add6782006-07-01 04:36:26 -0700457{
458 struct rtc_device *rtc;
Changhwan Youne1df9622010-10-27 15:33:10 -0700459 struct rtc_time rtc_tm;
Ben Dooks1add6782006-07-01 04:36:26 -0700460 struct resource *res;
461 int ret;
462
Harvey Harrison2a4e2b82008-04-28 02:12:00 -0700463 pr_debug("%s: probe=%p\n", __func__, pdev);
Ben Dooks1add6782006-07-01 04:36:26 -0700464
465 /* find the IRQs */
466
467 s3c_rtc_tickno = platform_get_irq(pdev, 1);
468 if (s3c_rtc_tickno < 0) {
469 dev_err(&pdev->dev, "no irq for rtc tick\n");
470 return -ENOENT;
471 }
472
473 s3c_rtc_alarmno = platform_get_irq(pdev, 0);
474 if (s3c_rtc_alarmno < 0) {
475 dev_err(&pdev->dev, "no irq for alarm\n");
476 return -ENOENT;
477 }
478
479 pr_debug("s3c2410_rtc: tick irq %d, alarm irq %d\n",
480 s3c_rtc_tickno, s3c_rtc_alarmno);
481
482 /* get the memory region */
483
484 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
485 if (res == NULL) {
486 dev_err(&pdev->dev, "failed to get memory region resource\n");
487 return -ENOENT;
488 }
489
490 s3c_rtc_mem = request_mem_region(res->start,
Ben Dooks9a654512006-08-27 01:23:22 -0700491 res->end-res->start+1,
492 pdev->name);
Ben Dooks1add6782006-07-01 04:36:26 -0700493
494 if (s3c_rtc_mem == NULL) {
495 dev_err(&pdev->dev, "failed to reserve memory region\n");
496 ret = -ENOENT;
497 goto err_nores;
498 }
499
500 s3c_rtc_base = ioremap(res->start, res->end - res->start + 1);
501 if (s3c_rtc_base == NULL) {
502 dev_err(&pdev->dev, "failed ioremap()\n");
503 ret = -EINVAL;
504 goto err_nomap;
505 }
506
Atul Dahiyae48add82010-07-20 12:19:14 +0530507 rtc_clk = clk_get(&pdev->dev, "rtc");
508 if (IS_ERR(rtc_clk)) {
509 dev_err(&pdev->dev, "failed to find rtc clock source\n");
510 ret = PTR_ERR(rtc_clk);
511 rtc_clk = NULL;
512 goto err_clk;
513 }
514
515 clk_enable(rtc_clk);
516
Ben Dooks1add6782006-07-01 04:36:26 -0700517 /* check to see if everything is setup correctly */
518
519 s3c_rtc_enable(pdev, 1);
520
Changhwan Younf61ae672010-10-27 15:33:06 -0700521 pr_debug("s3c2410_rtc: RTCCON=%02x\n",
522 readw(s3c_rtc_base + S3C2410_RTCCON));
Ben Dooks1add6782006-07-01 04:36:26 -0700523
Yauhen Kharuzhy51b76162008-10-29 14:01:16 -0700524 device_init_wakeup(&pdev->dev, 1);
525
Ben Dooks1add6782006-07-01 04:36:26 -0700526 /* register RTC and exit */
527
528 rtc = rtc_device_register("s3c", &pdev->dev, &s3c_rtcops,
529 THIS_MODULE);
530
531 if (IS_ERR(rtc)) {
532 dev_err(&pdev->dev, "cannot attach rtc\n");
533 ret = PTR_ERR(rtc);
534 goto err_nortc;
535 }
536
Maurus Cuelenaereeaa6e4d2010-06-04 14:14:46 -0700537 s3c_rtc_cpu_type = platform_get_device_id(pdev)->driver_data;
538
Taekgyun Ko051fe542010-07-29 13:00:42 +0900539 /* Check RTC Time */
540
Changhwan Youne1df9622010-10-27 15:33:10 -0700541 s3c_rtc_gettime(NULL, &rtc_tm);
Taekgyun Ko051fe542010-07-29 13:00:42 +0900542
Changhwan Youne1df9622010-10-27 15:33:10 -0700543 if (rtc_valid_tm(&rtc_tm)) {
544 rtc_tm.tm_year = 100;
545 rtc_tm.tm_mon = 0;
546 rtc_tm.tm_mday = 1;
547 rtc_tm.tm_hour = 0;
548 rtc_tm.tm_min = 0;
549 rtc_tm.tm_sec = 0;
550
551 s3c_rtc_settime(NULL, &rtc_tm);
552
553 dev_warn(&pdev->dev, "warning: invalid RTC value so initializing it\n");
Taekgyun Ko051fe542010-07-29 13:00:42 +0900554 }
555
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700556 if (s3c_rtc_cpu_type == TYPE_S3C64XX)
557 rtc->max_user_freq = 32768;
558 else
559 rtc->max_user_freq = 128;
560
Ben Dooks1add6782006-07-01 04:36:26 -0700561 platform_set_drvdata(pdev, rtc);
Maurus Cuelenaeree893de52010-06-04 14:14:44 -0700562
563 s3c_rtc_setfreq(&pdev->dev, 1);
564
Ben Dooks1add6782006-07-01 04:36:26 -0700565 return 0;
566
567 err_nortc:
568 s3c_rtc_enable(pdev, 0);
Atul Dahiyae48add82010-07-20 12:19:14 +0530569 clk_disable(rtc_clk);
570 clk_put(rtc_clk);
571
572 err_clk:
Ben Dooks1add6782006-07-01 04:36:26 -0700573 iounmap(s3c_rtc_base);
574
575 err_nomap:
576 release_resource(s3c_rtc_mem);
577
578 err_nores:
579 return ret;
580}
581
582#ifdef CONFIG_PM
583
584/* RTC Power management control */
585
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700586static int ticnt_save, ticnt_en_save;
Ben Dooks1add6782006-07-01 04:36:26 -0700587
588static int s3c_rtc_suspend(struct platform_device *pdev, pm_message_t state)
589{
Ben Dooks1add6782006-07-01 04:36:26 -0700590 /* save TICNT for anyone using periodic interrupts */
Ben Dooks9a654512006-08-27 01:23:22 -0700591 ticnt_save = readb(s3c_rtc_base + S3C2410_TICNT);
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700592 if (s3c_rtc_cpu_type == TYPE_S3C64XX) {
Changhwan Younf61ae672010-10-27 15:33:06 -0700593 ticnt_en_save = readw(s3c_rtc_base + S3C2410_RTCCON);
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700594 ticnt_en_save &= S3C64XX_RTCCON_TICEN;
595 }
Ben Dooks1add6782006-07-01 04:36:26 -0700596 s3c_rtc_enable(pdev, 0);
Vladimir Zapolskiyf501ed52010-09-22 13:05:13 -0700597
598 if (device_may_wakeup(&pdev->dev))
599 enable_irq_wake(s3c_rtc_alarmno);
600
Ben Dooks1add6782006-07-01 04:36:26 -0700601 return 0;
602}
603
604static int s3c_rtc_resume(struct platform_device *pdev)
605{
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700606 unsigned int tmp;
607
Ben Dooks1add6782006-07-01 04:36:26 -0700608 s3c_rtc_enable(pdev, 1);
Ben Dooks9a654512006-08-27 01:23:22 -0700609 writeb(ticnt_save, s3c_rtc_base + S3C2410_TICNT);
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700610 if (s3c_rtc_cpu_type == TYPE_S3C64XX && ticnt_en_save) {
Changhwan Younf61ae672010-10-27 15:33:06 -0700611 tmp = readw(s3c_rtc_base + S3C2410_RTCCON);
612 writew(tmp | ticnt_en_save, s3c_rtc_base + S3C2410_RTCCON);
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700613 }
Vladimir Zapolskiyf501ed52010-09-22 13:05:13 -0700614
615 if (device_may_wakeup(&pdev->dev))
616 disable_irq_wake(s3c_rtc_alarmno);
617
Ben Dooks1add6782006-07-01 04:36:26 -0700618 return 0;
619}
620#else
621#define s3c_rtc_suspend NULL
622#define s3c_rtc_resume NULL
623#endif
624
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700625static struct platform_device_id s3c_rtc_driver_ids[] = {
626 {
627 .name = "s3c2410-rtc",
628 .driver_data = TYPE_S3C2410,
629 }, {
630 .name = "s3c64xx-rtc",
631 .driver_data = TYPE_S3C64XX,
632 },
633 { }
634};
635
636MODULE_DEVICE_TABLE(platform, s3c_rtc_driver_ids);
637
638static struct platform_driver s3c_rtc_driver = {
Ben Dooks1add6782006-07-01 04:36:26 -0700639 .probe = s3c_rtc_probe,
Ben Dooks4cd0c5c2008-07-23 21:30:44 -0700640 .remove = __devexit_p(s3c_rtc_remove),
Ben Dooks1add6782006-07-01 04:36:26 -0700641 .suspend = s3c_rtc_suspend,
642 .resume = s3c_rtc_resume,
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700643 .id_table = s3c_rtc_driver_ids,
Ben Dooks1add6782006-07-01 04:36:26 -0700644 .driver = {
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700645 .name = "s3c-rtc",
Ben Dooks1add6782006-07-01 04:36:26 -0700646 .owner = THIS_MODULE,
647 },
648};
649
650static char __initdata banner[] = "S3C24XX RTC, (c) 2004,2006 Simtec Electronics\n";
651
652static int __init s3c_rtc_init(void)
653{
654 printk(banner);
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700655 return platform_driver_register(&s3c_rtc_driver);
Ben Dooks1add6782006-07-01 04:36:26 -0700656}
657
658static void __exit s3c_rtc_exit(void)
659{
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700660 platform_driver_unregister(&s3c_rtc_driver);
Ben Dooks1add6782006-07-01 04:36:26 -0700661}
662
663module_init(s3c_rtc_init);
664module_exit(s3c_rtc_exit);
665
666MODULE_DESCRIPTION("Samsung S3C RTC Driver");
667MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
668MODULE_LICENSE("GPL");
Kay Sieversad28a072008-04-10 21:29:25 -0700669MODULE_ALIAS("platform:s3c2410-rtc");