dm writecache: add cond_resched to loop in persistent_memory_claim()
[linux/fpc-iii.git] / drivers / rtc / rtc-puv3.c
blob954b88d2485f32c21f99e0bec17009cf632c8a7d
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * RTC driver code specific to PKUnity SoC and UniCore ISA
5 * Maintained by GUAN Xue-tao <gxt@mprc.pku.edu.cn>
6 * Copyright (C) 2001-2010 Guan Xuetao
7 */
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>
23 #include <asm/irq.h>
24 #include <mach/hardware.h>
26 static struct resource *puv3_rtc_mem;
28 static int puv3_rtc_alarmno = IRQ_RTCAlarm;
29 static int puv3_rtc_tickno = IRQ_RTC;
31 static DEFINE_SPINLOCK(puv3_rtc_pie_lock);
33 /* IRQ Handlers */
34 static irqreturn_t puv3_rtc_alarmirq(int irq, void *id)
36 struct rtc_device *rdev = id;
38 writel(readl(RTC_RTSR) | RTC_RTSR_AL, RTC_RTSR);
39 rtc_update_irq(rdev, 1, RTC_AF | RTC_IRQF);
40 return IRQ_HANDLED;
43 static irqreturn_t puv3_rtc_tickirq(int irq, void *id)
45 struct rtc_device *rdev = id;
47 writel(readl(RTC_RTSR) | RTC_RTSR_HZ, RTC_RTSR);
48 rtc_update_irq(rdev, 1, RTC_PF | RTC_IRQF);
49 return IRQ_HANDLED;
52 /* Update control registers */
53 static void puv3_rtc_setaie(struct device *dev, int to)
55 unsigned int tmp;
57 dev_dbg(dev, "%s: aie=%d\n", __func__, to);
59 tmp = readl(RTC_RTSR) & ~RTC_RTSR_ALE;
61 if (to)
62 tmp |= RTC_RTSR_ALE;
64 writel(tmp, RTC_RTSR);
67 static int puv3_rtc_setpie(struct device *dev, int enabled)
69 unsigned int tmp;
71 dev_dbg(dev, "%s: pie=%d\n", __func__, enabled);
73 spin_lock_irq(&puv3_rtc_pie_lock);
74 tmp = readl(RTC_RTSR) & ~RTC_RTSR_HZE;
76 if (enabled)
77 tmp |= RTC_RTSR_HZE;
79 writel(tmp, RTC_RTSR);
80 spin_unlock_irq(&puv3_rtc_pie_lock);
82 return 0;
85 /* Time read/write */
86 static int puv3_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
88 rtc_time64_to_tm(readl(RTC_RCNR), rtc_tm);
90 dev_dbg(dev, "read time %ptRr\n", rtc_tm);
92 return 0;
95 static int puv3_rtc_settime(struct device *dev, struct rtc_time *tm)
97 dev_dbg(dev, "set time %ptRr\n", tm);
99 writel(rtc_tm_to_time64(tm), RTC_RCNR);
101 return 0;
104 static int puv3_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)
106 struct rtc_time *alm_tm = &alrm->time;
108 rtc_time64_to_tm(readl(RTC_RTAR), alm_tm);
110 alrm->enabled = readl(RTC_RTSR) & RTC_RTSR_ALE;
112 dev_dbg(dev, "read alarm: %d, %ptRr\n", alrm->enabled, alm_tm);
114 return 0;
117 static int puv3_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
119 struct rtc_time *tm = &alrm->time;
121 dev_dbg(dev, "set alarm: %d, %ptRr\n", alrm->enabled, tm);
123 writel(rtc_tm_to_time64(tm), RTC_RTAR);
125 puv3_rtc_setaie(dev, alrm->enabled);
127 if (alrm->enabled)
128 enable_irq_wake(puv3_rtc_alarmno);
129 else
130 disable_irq_wake(puv3_rtc_alarmno);
132 return 0;
135 static int puv3_rtc_proc(struct device *dev, struct seq_file *seq)
137 seq_printf(seq, "periodic_IRQ\t: %s\n",
138 (readl(RTC_RTSR) & RTC_RTSR_HZE) ? "yes" : "no");
139 return 0;
142 static const struct rtc_class_ops puv3_rtcops = {
143 .read_time = puv3_rtc_gettime,
144 .set_time = puv3_rtc_settime,
145 .read_alarm = puv3_rtc_getalarm,
146 .set_alarm = puv3_rtc_setalarm,
147 .proc = puv3_rtc_proc,
150 static void puv3_rtc_enable(struct device *dev, int en)
152 if (!en) {
153 writel(readl(RTC_RTSR) & ~RTC_RTSR_HZE, RTC_RTSR);
154 } else {
155 /* re-enable the device, and check it is ok */
156 if ((readl(RTC_RTSR) & RTC_RTSR_HZE) == 0) {
157 dev_info(dev, "rtc disabled, re-enabling\n");
158 writel(readl(RTC_RTSR) | RTC_RTSR_HZE, RTC_RTSR);
163 static int puv3_rtc_remove(struct platform_device *dev)
165 puv3_rtc_setpie(&dev->dev, 0);
166 puv3_rtc_setaie(&dev->dev, 0);
168 release_resource(puv3_rtc_mem);
169 kfree(puv3_rtc_mem);
171 return 0;
174 static int puv3_rtc_probe(struct platform_device *pdev)
176 struct rtc_device *rtc;
177 struct resource *res;
178 int ret;
180 dev_dbg(&pdev->dev, "%s: probe=%p\n", __func__, pdev);
182 /* find the IRQs */
183 puv3_rtc_tickno = platform_get_irq(pdev, 1);
184 if (puv3_rtc_tickno < 0)
185 return -ENOENT;
187 puv3_rtc_alarmno = platform_get_irq(pdev, 0);
188 if (puv3_rtc_alarmno < 0)
189 return -ENOENT;
191 dev_dbg(&pdev->dev, "PKUnity_rtc: tick irq %d, alarm irq %d\n",
192 puv3_rtc_tickno, puv3_rtc_alarmno);
194 rtc = devm_rtc_allocate_device(&pdev->dev);
195 if (IS_ERR(rtc))
196 return PTR_ERR(rtc);
198 ret = devm_request_irq(&pdev->dev, puv3_rtc_alarmno, puv3_rtc_alarmirq,
199 0, "pkunity-rtc alarm", rtc);
200 if (ret) {
201 dev_err(&pdev->dev, "IRQ%d error %d\n", puv3_rtc_alarmno, ret);
202 return ret;
205 ret = devm_request_irq(&pdev->dev, puv3_rtc_tickno, puv3_rtc_tickirq,
206 0, "pkunity-rtc tick", rtc);
207 if (ret) {
208 dev_err(&pdev->dev, "IRQ%d error %d\n", puv3_rtc_tickno, ret);
209 return ret;
212 /* get the memory region */
213 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
214 if (res == NULL) {
215 dev_err(&pdev->dev, "failed to get memory region resource\n");
216 return -ENOENT;
219 puv3_rtc_mem = request_mem_region(res->start, resource_size(res),
220 pdev->name);
222 if (puv3_rtc_mem == NULL) {
223 dev_err(&pdev->dev, "failed to reserve memory region\n");
224 ret = -ENOENT;
225 goto err_nores;
228 puv3_rtc_enable(&pdev->dev, 1);
230 /* register RTC and exit */
231 rtc->ops = &puv3_rtcops;
232 rtc->range_max = U32_MAX;
233 ret = rtc_register_device(rtc);
234 if (ret)
235 goto err_nortc;
237 /* platform setup code should have handled this; sigh */
238 if (!device_can_wakeup(&pdev->dev))
239 device_init_wakeup(&pdev->dev, 1);
241 platform_set_drvdata(pdev, rtc);
242 return 0;
244 err_nortc:
245 puv3_rtc_enable(&pdev->dev, 0);
246 release_resource(puv3_rtc_mem);
248 err_nores:
249 return ret;
252 #ifdef CONFIG_PM_SLEEP
253 static int ticnt_save;
255 static int puv3_rtc_suspend(struct device *dev)
257 /* save RTAR for anyone using periodic interrupts */
258 ticnt_save = readl(RTC_RTAR);
259 puv3_rtc_enable(dev, 0);
260 return 0;
263 static int puv3_rtc_resume(struct device *dev)
265 puv3_rtc_enable(dev, 1);
266 writel(ticnt_save, RTC_RTAR);
267 return 0;
269 #endif
271 static SIMPLE_DEV_PM_OPS(puv3_rtc_pm_ops, puv3_rtc_suspend, puv3_rtc_resume);
273 static struct platform_driver puv3_rtc_driver = {
274 .probe = puv3_rtc_probe,
275 .remove = puv3_rtc_remove,
276 .driver = {
277 .name = "PKUnity-v3-RTC",
278 .pm = &puv3_rtc_pm_ops,
282 module_platform_driver(puv3_rtc_driver);
284 MODULE_DESCRIPTION("RTC Driver for the PKUnity v3 chip");
285 MODULE_AUTHOR("Hu Dongliang");
286 MODULE_LICENSE("GPL v2");