blob: 63b9e73fb97d7d584761751feb42b603b79f3a79 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
GuanXuetao02b2ee12011-01-15 18:19:03 +08002/*
Guan Xuetao2809e802011-05-26 16:43:27 +08003 * RTC driver code specific to PKUnity SoC and UniCore ISA
GuanXuetao02b2ee12011-01-15 18:19:03 +08004 *
5 * Maintained by GUAN Xue-tao <gxt@mprc.pku.edu.cn>
6 * Copyright (C) 2001-2010 Guan Xuetao
GuanXuetao02b2ee12011-01-15 18:19:03 +08007 */
8
9#include <linux/module.h>
10#include <linux/fs.h>
11#include <linux/string.h>
12#include <linux/init.h>
13#include <linux/platform_device.h>
14#include <linux/interrupt.h>
15#include <linux/rtc.h>
16#include <linux/bcd.h>
17#include <linux/clk.h>
18#include <linux/log2.h>
19#include <linux/slab.h>
20#include <linux/uaccess.h>
21#include <linux/io.h>
22
23#include <asm/irq.h>
24#include <mach/hardware.h>
25
26static struct resource *puv3_rtc_mem;
27
28static int puv3_rtc_alarmno = IRQ_RTCAlarm;
29static int puv3_rtc_tickno = IRQ_RTC;
30
31static DEFINE_SPINLOCK(puv3_rtc_pie_lock);
32
33/* IRQ Handlers */
GuanXuetao02b2ee12011-01-15 18:19:03 +080034static irqreturn_t puv3_rtc_alarmirq(int irq, void *id)
35{
36 struct rtc_device *rdev = id;
37
GuanXuetaoe5abf782011-02-26 21:21:18 +080038 writel(readl(RTC_RTSR) | RTC_RTSR_AL, RTC_RTSR);
GuanXuetao02b2ee12011-01-15 18:19:03 +080039 rtc_update_irq(rdev, 1, RTC_AF | RTC_IRQF);
40 return IRQ_HANDLED;
41}
42
43static irqreturn_t puv3_rtc_tickirq(int irq, void *id)
44{
45 struct rtc_device *rdev = id;
46
GuanXuetaoe5abf782011-02-26 21:21:18 +080047 writel(readl(RTC_RTSR) | RTC_RTSR_HZ, RTC_RTSR);
GuanXuetao02b2ee12011-01-15 18:19:03 +080048 rtc_update_irq(rdev, 1, RTC_PF | RTC_IRQF);
49 return IRQ_HANDLED;
50}
51
52/* Update control registers */
Sangjung Woo1fbc4c42013-11-12 15:11:00 -080053static void puv3_rtc_setaie(struct device *dev, int to)
GuanXuetao02b2ee12011-01-15 18:19:03 +080054{
55 unsigned int tmp;
56
Sangjung Woo1fbc4c42013-11-12 15:11:00 -080057 dev_dbg(dev, "%s: aie=%d\n", __func__, to);
GuanXuetao02b2ee12011-01-15 18:19:03 +080058
GuanXuetaoe5abf782011-02-26 21:21:18 +080059 tmp = readl(RTC_RTSR) & ~RTC_RTSR_ALE;
GuanXuetao02b2ee12011-01-15 18:19:03 +080060
61 if (to)
62 tmp |= RTC_RTSR_ALE;
63
GuanXuetaoe5abf782011-02-26 21:21:18 +080064 writel(tmp, RTC_RTSR);
GuanXuetao02b2ee12011-01-15 18:19:03 +080065}
66
67static int puv3_rtc_setpie(struct device *dev, int enabled)
68{
69 unsigned int tmp;
70
Chen Gangc8638102014-05-03 13:07:57 +080071 dev_dbg(dev, "%s: pie=%d\n", __func__, enabled);
GuanXuetao02b2ee12011-01-15 18:19:03 +080072
73 spin_lock_irq(&puv3_rtc_pie_lock);
GuanXuetaoe5abf782011-02-26 21:21:18 +080074 tmp = readl(RTC_RTSR) & ~RTC_RTSR_HZE;
GuanXuetao02b2ee12011-01-15 18:19:03 +080075
76 if (enabled)
77 tmp |= RTC_RTSR_HZE;
78
GuanXuetaoe5abf782011-02-26 21:21:18 +080079 writel(tmp, RTC_RTSR);
GuanXuetao02b2ee12011-01-15 18:19:03 +080080 spin_unlock_irq(&puv3_rtc_pie_lock);
81
82 return 0;
83}
84
GuanXuetao02b2ee12011-01-15 18:19:03 +080085/* Time read/write */
GuanXuetao02b2ee12011-01-15 18:19:03 +080086static int puv3_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
87{
GuanXuetaoe5abf782011-02-26 21:21:18 +080088 rtc_time_to_tm(readl(RTC_RCNR), rtc_tm);
GuanXuetao02b2ee12011-01-15 18:19:03 +080089
Andy Shevchenkob2db0a22018-12-04 23:23:21 +020090 dev_dbg(dev, "read time %ptRr\n", rtc_tm);
GuanXuetao02b2ee12011-01-15 18:19:03 +080091
92 return 0;
93}
94
95static int puv3_rtc_settime(struct device *dev, struct rtc_time *tm)
96{
97 unsigned long rtc_count = 0;
98
Andy Shevchenkob2db0a22018-12-04 23:23:21 +020099 dev_dbg(dev, "set time %ptRr\n", tm);
GuanXuetao02b2ee12011-01-15 18:19:03 +0800100
101 rtc_tm_to_time(tm, &rtc_count);
GuanXuetaoe5abf782011-02-26 21:21:18 +0800102 writel(rtc_count, RTC_RCNR);
GuanXuetao02b2ee12011-01-15 18:19:03 +0800103
104 return 0;
105}
106
107static int puv3_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)
108{
109 struct rtc_time *alm_tm = &alrm->time;
110
GuanXuetaoe5abf782011-02-26 21:21:18 +0800111 rtc_time_to_tm(readl(RTC_RTAR), alm_tm);
GuanXuetao02b2ee12011-01-15 18:19:03 +0800112
GuanXuetaoe5abf782011-02-26 21:21:18 +0800113 alrm->enabled = readl(RTC_RTSR) & RTC_RTSR_ALE;
GuanXuetao02b2ee12011-01-15 18:19:03 +0800114
Andy Shevchenkob2db0a22018-12-04 23:23:21 +0200115 dev_dbg(dev, "read alarm: %d, %ptRr\n", alrm->enabled, alm_tm);
GuanXuetao02b2ee12011-01-15 18:19:03 +0800116
117 return 0;
118}
119
120static int puv3_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
121{
122 struct rtc_time *tm = &alrm->time;
123 unsigned long rtcalarm_count = 0;
124
Andy Shevchenkob2db0a22018-12-04 23:23:21 +0200125 dev_dbg(dev, "set alarm: %d, %ptRr\n", alrm->enabled, tm);
GuanXuetao02b2ee12011-01-15 18:19:03 +0800126
127 rtc_tm_to_time(tm, &rtcalarm_count);
GuanXuetaoe5abf782011-02-26 21:21:18 +0800128 writel(rtcalarm_count, RTC_RTAR);
GuanXuetao02b2ee12011-01-15 18:19:03 +0800129
Chen Gang73fa5402014-05-03 13:09:02 +0800130 puv3_rtc_setaie(dev, alrm->enabled);
GuanXuetao02b2ee12011-01-15 18:19:03 +0800131
132 if (alrm->enabled)
133 enable_irq_wake(puv3_rtc_alarmno);
134 else
135 disable_irq_wake(puv3_rtc_alarmno);
136
137 return 0;
138}
139
140static int puv3_rtc_proc(struct device *dev, struct seq_file *seq)
141{
142 seq_printf(seq, "periodic_IRQ\t: %s\n",
GuanXuetaoe5abf782011-02-26 21:21:18 +0800143 (readl(RTC_RTSR) & RTC_RTSR_HZE) ? "yes" : "no");
GuanXuetao02b2ee12011-01-15 18:19:03 +0800144 return 0;
145}
146
GuanXuetao02b2ee12011-01-15 18:19:03 +0800147static const struct rtc_class_ops puv3_rtcops = {
GuanXuetao02b2ee12011-01-15 18:19:03 +0800148 .read_time = puv3_rtc_gettime,
149 .set_time = puv3_rtc_settime,
150 .read_alarm = puv3_rtc_getalarm,
151 .set_alarm = puv3_rtc_setalarm,
GuanXuetao02b2ee12011-01-15 18:19:03 +0800152 .proc = puv3_rtc_proc,
153};
154
Jingoo Han5936fdb2013-04-29 16:21:02 -0700155static void puv3_rtc_enable(struct device *dev, int en)
GuanXuetao02b2ee12011-01-15 18:19:03 +0800156{
157 if (!en) {
GuanXuetaoe5abf782011-02-26 21:21:18 +0800158 writel(readl(RTC_RTSR) & ~RTC_RTSR_HZE, RTC_RTSR);
GuanXuetao02b2ee12011-01-15 18:19:03 +0800159 } else {
160 /* re-enable the device, and check it is ok */
GuanXuetaoe5abf782011-02-26 21:21:18 +0800161 if ((readl(RTC_RTSR) & RTC_RTSR_HZE) == 0) {
Jingoo Han5936fdb2013-04-29 16:21:02 -0700162 dev_info(dev, "rtc disabled, re-enabling\n");
GuanXuetaoe5abf782011-02-26 21:21:18 +0800163 writel(readl(RTC_RTSR) | RTC_RTSR_HZE, RTC_RTSR);
GuanXuetao02b2ee12011-01-15 18:19:03 +0800164 }
165 }
166}
167
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800168static int puv3_rtc_remove(struct platform_device *dev)
GuanXuetao02b2ee12011-01-15 18:19:03 +0800169{
GuanXuetao02b2ee12011-01-15 18:19:03 +0800170 puv3_rtc_setpie(&dev->dev, 0);
Sangjung Woo1fbc4c42013-11-12 15:11:00 -0800171 puv3_rtc_setaie(&dev->dev, 0);
GuanXuetao02b2ee12011-01-15 18:19:03 +0800172
173 release_resource(puv3_rtc_mem);
174 kfree(puv3_rtc_mem);
175
176 return 0;
177}
178
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800179static int puv3_rtc_probe(struct platform_device *pdev)
GuanXuetao02b2ee12011-01-15 18:19:03 +0800180{
181 struct rtc_device *rtc;
182 struct resource *res;
183 int ret;
184
Sangjung Woo1fbc4c42013-11-12 15:11:00 -0800185 dev_dbg(&pdev->dev, "%s: probe=%p\n", __func__, pdev);
GuanXuetao02b2ee12011-01-15 18:19:03 +0800186
187 /* find the IRQs */
GuanXuetao02b2ee12011-01-15 18:19:03 +0800188 puv3_rtc_tickno = platform_get_irq(pdev, 1);
189 if (puv3_rtc_tickno < 0) {
190 dev_err(&pdev->dev, "no irq for rtc tick\n");
191 return -ENOENT;
192 }
193
194 puv3_rtc_alarmno = platform_get_irq(pdev, 0);
195 if (puv3_rtc_alarmno < 0) {
196 dev_err(&pdev->dev, "no irq for alarm\n");
197 return -ENOENT;
198 }
199
Sangjung Woo1fbc4c42013-11-12 15:11:00 -0800200 dev_dbg(&pdev->dev, "PKUnity_rtc: tick irq %d, alarm irq %d\n",
GuanXuetao02b2ee12011-01-15 18:19:03 +0800201 puv3_rtc_tickno, puv3_rtc_alarmno);
202
Alexandre Belloni5539ba52017-08-22 11:48:41 +0200203 rtc = devm_rtc_allocate_device(&pdev->dev);
204 if (IS_ERR(rtc))
205 return PTR_ERR(rtc);
206
Alexandre Belloni60d34552017-08-22 11:59:07 +0200207 ret = devm_request_irq(&pdev->dev, puv3_rtc_alarmno, puv3_rtc_alarmirq,
208 0, "pkunity-rtc alarm", rtc);
209 if (ret) {
210 dev_err(&pdev->dev, "IRQ%d error %d\n", puv3_rtc_alarmno, ret);
211 return ret;
212 }
213
214 ret = devm_request_irq(&pdev->dev, puv3_rtc_tickno, puv3_rtc_tickirq,
215 0, "pkunity-rtc tick", rtc);
216 if (ret) {
217 dev_err(&pdev->dev, "IRQ%d error %d\n", puv3_rtc_tickno, ret);
218 return ret;
219 }
220
GuanXuetao02b2ee12011-01-15 18:19:03 +0800221 /* get the memory region */
GuanXuetao02b2ee12011-01-15 18:19:03 +0800222 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
223 if (res == NULL) {
224 dev_err(&pdev->dev, "failed to get memory region resource\n");
225 return -ENOENT;
226 }
227
Joe Perches28f65c112011-06-09 09:13:32 -0700228 puv3_rtc_mem = request_mem_region(res->start, resource_size(res),
229 pdev->name);
GuanXuetao02b2ee12011-01-15 18:19:03 +0800230
231 if (puv3_rtc_mem == NULL) {
232 dev_err(&pdev->dev, "failed to reserve memory region\n");
233 ret = -ENOENT;
234 goto err_nores;
235 }
236
Jingoo Han5936fdb2013-04-29 16:21:02 -0700237 puv3_rtc_enable(&pdev->dev, 1);
GuanXuetao02b2ee12011-01-15 18:19:03 +0800238
GuanXuetao02b2ee12011-01-15 18:19:03 +0800239 /* register RTC and exit */
Alexandre Belloni5539ba52017-08-22 11:48:41 +0200240 rtc->ops = &puv3_rtcops;
241 ret = rtc_register_device(rtc);
242 if (ret) {
GuanXuetao02b2ee12011-01-15 18:19:03 +0800243 dev_err(&pdev->dev, "cannot attach rtc\n");
GuanXuetao02b2ee12011-01-15 18:19:03 +0800244 goto err_nortc;
245 }
246
247 /* platform setup code should have handled this; sigh */
248 if (!device_can_wakeup(&pdev->dev))
249 device_init_wakeup(&pdev->dev, 1);
250
251 platform_set_drvdata(pdev, rtc);
252 return 0;
253
254 err_nortc:
Jingoo Han5936fdb2013-04-29 16:21:02 -0700255 puv3_rtc_enable(&pdev->dev, 0);
GuanXuetao02b2ee12011-01-15 18:19:03 +0800256 release_resource(puv3_rtc_mem);
257
258 err_nores:
259 return ret;
260}
261
Jingoo Han5936fdb2013-04-29 16:21:02 -0700262#ifdef CONFIG_PM_SLEEP
GuanXuetao02b2ee12011-01-15 18:19:03 +0800263static int ticnt_save;
264
Jingoo Han5936fdb2013-04-29 16:21:02 -0700265static int puv3_rtc_suspend(struct device *dev)
GuanXuetao02b2ee12011-01-15 18:19:03 +0800266{
267 /* save RTAR for anyone using periodic interrupts */
GuanXuetaoe5abf782011-02-26 21:21:18 +0800268 ticnt_save = readl(RTC_RTAR);
Jingoo Han5936fdb2013-04-29 16:21:02 -0700269 puv3_rtc_enable(dev, 0);
GuanXuetao02b2ee12011-01-15 18:19:03 +0800270 return 0;
271}
272
Jingoo Han5936fdb2013-04-29 16:21:02 -0700273static int puv3_rtc_resume(struct device *dev)
GuanXuetao02b2ee12011-01-15 18:19:03 +0800274{
Jingoo Han5936fdb2013-04-29 16:21:02 -0700275 puv3_rtc_enable(dev, 1);
GuanXuetaoe5abf782011-02-26 21:21:18 +0800276 writel(ticnt_save, RTC_RTAR);
GuanXuetao02b2ee12011-01-15 18:19:03 +0800277 return 0;
278}
GuanXuetao02b2ee12011-01-15 18:19:03 +0800279#endif
280
Jingoo Han5936fdb2013-04-29 16:21:02 -0700281static SIMPLE_DEV_PM_OPS(puv3_rtc_pm_ops, puv3_rtc_suspend, puv3_rtc_resume);
282
Guan Xuetaob3a0aa32011-12-28 09:24:29 +0800283static struct platform_driver puv3_rtc_driver = {
GuanXuetao02b2ee12011-01-15 18:19:03 +0800284 .probe = puv3_rtc_probe,
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800285 .remove = puv3_rtc_remove,
GuanXuetao02b2ee12011-01-15 18:19:03 +0800286 .driver = {
287 .name = "PKUnity-v3-RTC",
Jingoo Han5936fdb2013-04-29 16:21:02 -0700288 .pm = &puv3_rtc_pm_ops,
GuanXuetao02b2ee12011-01-15 18:19:03 +0800289 }
290};
291
Guan Xuetaob3a0aa32011-12-28 09:24:29 +0800292module_platform_driver(puv3_rtc_driver);
GuanXuetao02b2ee12011-01-15 18:19:03 +0800293
294MODULE_DESCRIPTION("RTC Driver for the PKUnity v3 chip");
295MODULE_AUTHOR("Hu Dongliang");
296MODULE_LICENSE("GPL v2");