blob: f77ef282f013b093780979efd56c714b28d1ec5d [file] [log] [blame]
GuanXuetao02b2ee12011-01-15 18:19:03 +08001/*
Guan Xuetao2809e802011-05-26 16:43:27 +08002 * RTC driver code specific to PKUnity SoC and UniCore ISA
GuanXuetao02b2ee12011-01-15 18:19:03 +08003 *
4 * Maintained by GUAN Xue-tao <gxt@mprc.pku.edu.cn>
5 * Copyright (C) 2001-2010 Guan Xuetao
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#include <linux/module.h>
13#include <linux/fs.h>
14#include <linux/string.h>
15#include <linux/init.h>
16#include <linux/platform_device.h>
17#include <linux/interrupt.h>
18#include <linux/rtc.h>
19#include <linux/bcd.h>
20#include <linux/clk.h>
21#include <linux/log2.h>
22#include <linux/slab.h>
23#include <linux/uaccess.h>
24#include <linux/io.h>
25
26#include <asm/irq.h>
27#include <mach/hardware.h>
28
29static struct resource *puv3_rtc_mem;
30
31static int puv3_rtc_alarmno = IRQ_RTCAlarm;
32static int puv3_rtc_tickno = IRQ_RTC;
33
34static DEFINE_SPINLOCK(puv3_rtc_pie_lock);
35
36/* IRQ Handlers */
GuanXuetao02b2ee12011-01-15 18:19:03 +080037static irqreturn_t puv3_rtc_alarmirq(int irq, void *id)
38{
39 struct rtc_device *rdev = id;
40
GuanXuetaoe5abf782011-02-26 21:21:18 +080041 writel(readl(RTC_RTSR) | RTC_RTSR_AL, RTC_RTSR);
GuanXuetao02b2ee12011-01-15 18:19:03 +080042 rtc_update_irq(rdev, 1, RTC_AF | RTC_IRQF);
43 return IRQ_HANDLED;
44}
45
46static irqreturn_t puv3_rtc_tickirq(int irq, void *id)
47{
48 struct rtc_device *rdev = id;
49
GuanXuetaoe5abf782011-02-26 21:21:18 +080050 writel(readl(RTC_RTSR) | RTC_RTSR_HZ, RTC_RTSR);
GuanXuetao02b2ee12011-01-15 18:19:03 +080051 rtc_update_irq(rdev, 1, RTC_PF | RTC_IRQF);
52 return IRQ_HANDLED;
53}
54
55/* Update control registers */
Sangjung Woo1fbc4c42013-11-12 15:11:00 -080056static void puv3_rtc_setaie(struct device *dev, int to)
GuanXuetao02b2ee12011-01-15 18:19:03 +080057{
58 unsigned int tmp;
59
Sangjung Woo1fbc4c42013-11-12 15:11:00 -080060 dev_dbg(dev, "%s: aie=%d\n", __func__, to);
GuanXuetao02b2ee12011-01-15 18:19:03 +080061
GuanXuetaoe5abf782011-02-26 21:21:18 +080062 tmp = readl(RTC_RTSR) & ~RTC_RTSR_ALE;
GuanXuetao02b2ee12011-01-15 18:19:03 +080063
64 if (to)
65 tmp |= RTC_RTSR_ALE;
66
GuanXuetaoe5abf782011-02-26 21:21:18 +080067 writel(tmp, RTC_RTSR);
GuanXuetao02b2ee12011-01-15 18:19:03 +080068}
69
70static int puv3_rtc_setpie(struct device *dev, int enabled)
71{
72 unsigned int tmp;
73
Chen Gangc8638102014-05-03 13:07:57 +080074 dev_dbg(dev, "%s: pie=%d\n", __func__, enabled);
GuanXuetao02b2ee12011-01-15 18:19:03 +080075
76 spin_lock_irq(&puv3_rtc_pie_lock);
GuanXuetaoe5abf782011-02-26 21:21:18 +080077 tmp = readl(RTC_RTSR) & ~RTC_RTSR_HZE;
GuanXuetao02b2ee12011-01-15 18:19:03 +080078
79 if (enabled)
80 tmp |= RTC_RTSR_HZE;
81
GuanXuetaoe5abf782011-02-26 21:21:18 +080082 writel(tmp, RTC_RTSR);
GuanXuetao02b2ee12011-01-15 18:19:03 +080083 spin_unlock_irq(&puv3_rtc_pie_lock);
84
85 return 0;
86}
87
GuanXuetao02b2ee12011-01-15 18:19:03 +080088/* Time read/write */
GuanXuetao02b2ee12011-01-15 18:19:03 +080089static int puv3_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
90{
GuanXuetaoe5abf782011-02-26 21:21:18 +080091 rtc_time_to_tm(readl(RTC_RCNR), rtc_tm);
GuanXuetao02b2ee12011-01-15 18:19:03 +080092
Andy Shevchenkob2db0a22018-12-04 23:23:21 +020093 dev_dbg(dev, "read time %ptRr\n", rtc_tm);
GuanXuetao02b2ee12011-01-15 18:19:03 +080094
95 return 0;
96}
97
98static int puv3_rtc_settime(struct device *dev, struct rtc_time *tm)
99{
100 unsigned long rtc_count = 0;
101
Andy Shevchenkob2db0a22018-12-04 23:23:21 +0200102 dev_dbg(dev, "set time %ptRr\n", tm);
GuanXuetao02b2ee12011-01-15 18:19:03 +0800103
104 rtc_tm_to_time(tm, &rtc_count);
GuanXuetaoe5abf782011-02-26 21:21:18 +0800105 writel(rtc_count, RTC_RCNR);
GuanXuetao02b2ee12011-01-15 18:19:03 +0800106
107 return 0;
108}
109
110static int puv3_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)
111{
112 struct rtc_time *alm_tm = &alrm->time;
113
GuanXuetaoe5abf782011-02-26 21:21:18 +0800114 rtc_time_to_tm(readl(RTC_RTAR), alm_tm);
GuanXuetao02b2ee12011-01-15 18:19:03 +0800115
GuanXuetaoe5abf782011-02-26 21:21:18 +0800116 alrm->enabled = readl(RTC_RTSR) & RTC_RTSR_ALE;
GuanXuetao02b2ee12011-01-15 18:19:03 +0800117
Andy Shevchenkob2db0a22018-12-04 23:23:21 +0200118 dev_dbg(dev, "read alarm: %d, %ptRr\n", alrm->enabled, alm_tm);
GuanXuetao02b2ee12011-01-15 18:19:03 +0800119
120 return 0;
121}
122
123static int puv3_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
124{
125 struct rtc_time *tm = &alrm->time;
126 unsigned long rtcalarm_count = 0;
127
Andy Shevchenkob2db0a22018-12-04 23:23:21 +0200128 dev_dbg(dev, "set alarm: %d, %ptRr\n", alrm->enabled, tm);
GuanXuetao02b2ee12011-01-15 18:19:03 +0800129
130 rtc_tm_to_time(tm, &rtcalarm_count);
GuanXuetaoe5abf782011-02-26 21:21:18 +0800131 writel(rtcalarm_count, RTC_RTAR);
GuanXuetao02b2ee12011-01-15 18:19:03 +0800132
Chen Gang73fa5402014-05-03 13:09:02 +0800133 puv3_rtc_setaie(dev, alrm->enabled);
GuanXuetao02b2ee12011-01-15 18:19:03 +0800134
135 if (alrm->enabled)
136 enable_irq_wake(puv3_rtc_alarmno);
137 else
138 disable_irq_wake(puv3_rtc_alarmno);
139
140 return 0;
141}
142
143static int puv3_rtc_proc(struct device *dev, struct seq_file *seq)
144{
145 seq_printf(seq, "periodic_IRQ\t: %s\n",
GuanXuetaoe5abf782011-02-26 21:21:18 +0800146 (readl(RTC_RTSR) & RTC_RTSR_HZE) ? "yes" : "no");
GuanXuetao02b2ee12011-01-15 18:19:03 +0800147 return 0;
148}
149
GuanXuetao02b2ee12011-01-15 18:19:03 +0800150static const struct rtc_class_ops puv3_rtcops = {
GuanXuetao02b2ee12011-01-15 18:19:03 +0800151 .read_time = puv3_rtc_gettime,
152 .set_time = puv3_rtc_settime,
153 .read_alarm = puv3_rtc_getalarm,
154 .set_alarm = puv3_rtc_setalarm,
GuanXuetao02b2ee12011-01-15 18:19:03 +0800155 .proc = puv3_rtc_proc,
156};
157
Jingoo Han5936fdb2013-04-29 16:21:02 -0700158static void puv3_rtc_enable(struct device *dev, int en)
GuanXuetao02b2ee12011-01-15 18:19:03 +0800159{
160 if (!en) {
GuanXuetaoe5abf782011-02-26 21:21:18 +0800161 writel(readl(RTC_RTSR) & ~RTC_RTSR_HZE, RTC_RTSR);
GuanXuetao02b2ee12011-01-15 18:19:03 +0800162 } else {
163 /* re-enable the device, and check it is ok */
GuanXuetaoe5abf782011-02-26 21:21:18 +0800164 if ((readl(RTC_RTSR) & RTC_RTSR_HZE) == 0) {
Jingoo Han5936fdb2013-04-29 16:21:02 -0700165 dev_info(dev, "rtc disabled, re-enabling\n");
GuanXuetaoe5abf782011-02-26 21:21:18 +0800166 writel(readl(RTC_RTSR) | RTC_RTSR_HZE, RTC_RTSR);
GuanXuetao02b2ee12011-01-15 18:19:03 +0800167 }
168 }
169}
170
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800171static int puv3_rtc_remove(struct platform_device *dev)
GuanXuetao02b2ee12011-01-15 18:19:03 +0800172{
GuanXuetao02b2ee12011-01-15 18:19:03 +0800173 puv3_rtc_setpie(&dev->dev, 0);
Sangjung Woo1fbc4c42013-11-12 15:11:00 -0800174 puv3_rtc_setaie(&dev->dev, 0);
GuanXuetao02b2ee12011-01-15 18:19:03 +0800175
176 release_resource(puv3_rtc_mem);
177 kfree(puv3_rtc_mem);
178
179 return 0;
180}
181
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800182static int puv3_rtc_probe(struct platform_device *pdev)
GuanXuetao02b2ee12011-01-15 18:19:03 +0800183{
184 struct rtc_device *rtc;
185 struct resource *res;
186 int ret;
187
Sangjung Woo1fbc4c42013-11-12 15:11:00 -0800188 dev_dbg(&pdev->dev, "%s: probe=%p\n", __func__, pdev);
GuanXuetao02b2ee12011-01-15 18:19:03 +0800189
190 /* find the IRQs */
GuanXuetao02b2ee12011-01-15 18:19:03 +0800191 puv3_rtc_tickno = platform_get_irq(pdev, 1);
192 if (puv3_rtc_tickno < 0) {
193 dev_err(&pdev->dev, "no irq for rtc tick\n");
194 return -ENOENT;
195 }
196
197 puv3_rtc_alarmno = platform_get_irq(pdev, 0);
198 if (puv3_rtc_alarmno < 0) {
199 dev_err(&pdev->dev, "no irq for alarm\n");
200 return -ENOENT;
201 }
202
Sangjung Woo1fbc4c42013-11-12 15:11:00 -0800203 dev_dbg(&pdev->dev, "PKUnity_rtc: tick irq %d, alarm irq %d\n",
GuanXuetao02b2ee12011-01-15 18:19:03 +0800204 puv3_rtc_tickno, puv3_rtc_alarmno);
205
Alexandre Belloni5539ba52017-08-22 11:48:41 +0200206 rtc = devm_rtc_allocate_device(&pdev->dev);
207 if (IS_ERR(rtc))
208 return PTR_ERR(rtc);
209
Alexandre Belloni60d34552017-08-22 11:59:07 +0200210 ret = devm_request_irq(&pdev->dev, puv3_rtc_alarmno, puv3_rtc_alarmirq,
211 0, "pkunity-rtc alarm", rtc);
212 if (ret) {
213 dev_err(&pdev->dev, "IRQ%d error %d\n", puv3_rtc_alarmno, ret);
214 return ret;
215 }
216
217 ret = devm_request_irq(&pdev->dev, puv3_rtc_tickno, puv3_rtc_tickirq,
218 0, "pkunity-rtc tick", rtc);
219 if (ret) {
220 dev_err(&pdev->dev, "IRQ%d error %d\n", puv3_rtc_tickno, ret);
221 return ret;
222 }
223
GuanXuetao02b2ee12011-01-15 18:19:03 +0800224 /* get the memory region */
GuanXuetao02b2ee12011-01-15 18:19:03 +0800225 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
226 if (res == NULL) {
227 dev_err(&pdev->dev, "failed to get memory region resource\n");
228 return -ENOENT;
229 }
230
Joe Perches28f65c112011-06-09 09:13:32 -0700231 puv3_rtc_mem = request_mem_region(res->start, resource_size(res),
232 pdev->name);
GuanXuetao02b2ee12011-01-15 18:19:03 +0800233
234 if (puv3_rtc_mem == NULL) {
235 dev_err(&pdev->dev, "failed to reserve memory region\n");
236 ret = -ENOENT;
237 goto err_nores;
238 }
239
Jingoo Han5936fdb2013-04-29 16:21:02 -0700240 puv3_rtc_enable(&pdev->dev, 1);
GuanXuetao02b2ee12011-01-15 18:19:03 +0800241
GuanXuetao02b2ee12011-01-15 18:19:03 +0800242 /* register RTC and exit */
Alexandre Belloni5539ba52017-08-22 11:48:41 +0200243 rtc->ops = &puv3_rtcops;
244 ret = rtc_register_device(rtc);
245 if (ret) {
GuanXuetao02b2ee12011-01-15 18:19:03 +0800246 dev_err(&pdev->dev, "cannot attach rtc\n");
GuanXuetao02b2ee12011-01-15 18:19:03 +0800247 goto err_nortc;
248 }
249
250 /* platform setup code should have handled this; sigh */
251 if (!device_can_wakeup(&pdev->dev))
252 device_init_wakeup(&pdev->dev, 1);
253
254 platform_set_drvdata(pdev, rtc);
255 return 0;
256
257 err_nortc:
Jingoo Han5936fdb2013-04-29 16:21:02 -0700258 puv3_rtc_enable(&pdev->dev, 0);
GuanXuetao02b2ee12011-01-15 18:19:03 +0800259 release_resource(puv3_rtc_mem);
260
261 err_nores:
262 return ret;
263}
264
Jingoo Han5936fdb2013-04-29 16:21:02 -0700265#ifdef CONFIG_PM_SLEEP
GuanXuetao02b2ee12011-01-15 18:19:03 +0800266static int ticnt_save;
267
Jingoo Han5936fdb2013-04-29 16:21:02 -0700268static int puv3_rtc_suspend(struct device *dev)
GuanXuetao02b2ee12011-01-15 18:19:03 +0800269{
270 /* save RTAR for anyone using periodic interrupts */
GuanXuetaoe5abf782011-02-26 21:21:18 +0800271 ticnt_save = readl(RTC_RTAR);
Jingoo Han5936fdb2013-04-29 16:21:02 -0700272 puv3_rtc_enable(dev, 0);
GuanXuetao02b2ee12011-01-15 18:19:03 +0800273 return 0;
274}
275
Jingoo Han5936fdb2013-04-29 16:21:02 -0700276static int puv3_rtc_resume(struct device *dev)
GuanXuetao02b2ee12011-01-15 18:19:03 +0800277{
Jingoo Han5936fdb2013-04-29 16:21:02 -0700278 puv3_rtc_enable(dev, 1);
GuanXuetaoe5abf782011-02-26 21:21:18 +0800279 writel(ticnt_save, RTC_RTAR);
GuanXuetao02b2ee12011-01-15 18:19:03 +0800280 return 0;
281}
GuanXuetao02b2ee12011-01-15 18:19:03 +0800282#endif
283
Jingoo Han5936fdb2013-04-29 16:21:02 -0700284static SIMPLE_DEV_PM_OPS(puv3_rtc_pm_ops, puv3_rtc_suspend, puv3_rtc_resume);
285
Guan Xuetaob3a0aa32011-12-28 09:24:29 +0800286static struct platform_driver puv3_rtc_driver = {
GuanXuetao02b2ee12011-01-15 18:19:03 +0800287 .probe = puv3_rtc_probe,
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800288 .remove = puv3_rtc_remove,
GuanXuetao02b2ee12011-01-15 18:19:03 +0800289 .driver = {
290 .name = "PKUnity-v3-RTC",
Jingoo Han5936fdb2013-04-29 16:21:02 -0700291 .pm = &puv3_rtc_pm_ops,
GuanXuetao02b2ee12011-01-15 18:19:03 +0800292 }
293};
294
Guan Xuetaob3a0aa32011-12-28 09:24:29 +0800295module_platform_driver(puv3_rtc_driver);
GuanXuetao02b2ee12011-01-15 18:19:03 +0800296
297MODULE_DESCRIPTION("RTC Driver for the PKUnity v3 chip");
298MODULE_AUTHOR("Hu Dongliang");
299MODULE_LICENSE("GPL v2");