bus: ti-sysc: Fix watchdog quirk handling
[linux-2.6-microblaze.git] / drivers / rtc / rtc-puv3.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * RTC driver code specific to PKUnity SoC and UniCore ISA
4  *
5  *      Maintained by GUAN Xue-tao <gxt@mprc.pku.edu.cn>
6  *      Copyright (C) 2001-2010 Guan Xuetao
7  */
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
26 static struct resource *puv3_rtc_mem;
27
28 static int puv3_rtc_alarmno = IRQ_RTCAlarm;
29 static int puv3_rtc_tickno  = IRQ_RTC;
30
31 static DEFINE_SPINLOCK(puv3_rtc_pie_lock);
32
33 /* IRQ Handlers */
34 static irqreturn_t puv3_rtc_alarmirq(int irq, void *id)
35 {
36         struct rtc_device *rdev = id;
37
38         writel(readl(RTC_RTSR) | RTC_RTSR_AL, RTC_RTSR);
39         rtc_update_irq(rdev, 1, RTC_AF | RTC_IRQF);
40         return IRQ_HANDLED;
41 }
42
43 static irqreturn_t puv3_rtc_tickirq(int irq, void *id)
44 {
45         struct rtc_device *rdev = id;
46
47         writel(readl(RTC_RTSR) | RTC_RTSR_HZ, RTC_RTSR);
48         rtc_update_irq(rdev, 1, RTC_PF | RTC_IRQF);
49         return IRQ_HANDLED;
50 }
51
52 /* Update control registers */
53 static void puv3_rtc_setaie(struct device *dev, int to)
54 {
55         unsigned int tmp;
56
57         dev_dbg(dev, "%s: aie=%d\n", __func__, to);
58
59         tmp = readl(RTC_RTSR) & ~RTC_RTSR_ALE;
60
61         if (to)
62                 tmp |= RTC_RTSR_ALE;
63
64         writel(tmp, RTC_RTSR);
65 }
66
67 static int puv3_rtc_setpie(struct device *dev, int enabled)
68 {
69         unsigned int tmp;
70
71         dev_dbg(dev, "%s: pie=%d\n", __func__, enabled);
72
73         spin_lock_irq(&puv3_rtc_pie_lock);
74         tmp = readl(RTC_RTSR) & ~RTC_RTSR_HZE;
75
76         if (enabled)
77                 tmp |= RTC_RTSR_HZE;
78
79         writel(tmp, RTC_RTSR);
80         spin_unlock_irq(&puv3_rtc_pie_lock);
81
82         return 0;
83 }
84
85 /* Time read/write */
86 static int puv3_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
87 {
88         rtc_time_to_tm(readl(RTC_RCNR), rtc_tm);
89
90         dev_dbg(dev, "read time %ptRr\n", rtc_tm);
91
92         return 0;
93 }
94
95 static int puv3_rtc_settime(struct device *dev, struct rtc_time *tm)
96 {
97         unsigned long rtc_count = 0;
98
99         dev_dbg(dev, "set time %ptRr\n", tm);
100
101         rtc_tm_to_time(tm, &rtc_count);
102         writel(rtc_count, RTC_RCNR);
103
104         return 0;
105 }
106
107 static int puv3_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)
108 {
109         struct rtc_time *alm_tm = &alrm->time;
110
111         rtc_time_to_tm(readl(RTC_RTAR), alm_tm);
112
113         alrm->enabled = readl(RTC_RTSR) & RTC_RTSR_ALE;
114
115         dev_dbg(dev, "read alarm: %d, %ptRr\n", alrm->enabled, alm_tm);
116
117         return 0;
118 }
119
120 static 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
125         dev_dbg(dev, "set alarm: %d, %ptRr\n", alrm->enabled, tm);
126
127         rtc_tm_to_time(tm, &rtcalarm_count);
128         writel(rtcalarm_count, RTC_RTAR);
129
130         puv3_rtc_setaie(dev, alrm->enabled);
131
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
140 static int puv3_rtc_proc(struct device *dev, struct seq_file *seq)
141 {
142         seq_printf(seq, "periodic_IRQ\t: %s\n",
143                      (readl(RTC_RTSR) & RTC_RTSR_HZE) ? "yes" : "no");
144         return 0;
145 }
146
147 static const struct rtc_class_ops puv3_rtcops = {
148         .read_time      = puv3_rtc_gettime,
149         .set_time       = puv3_rtc_settime,
150         .read_alarm     = puv3_rtc_getalarm,
151         .set_alarm      = puv3_rtc_setalarm,
152         .proc           = puv3_rtc_proc,
153 };
154
155 static void puv3_rtc_enable(struct device *dev, int en)
156 {
157         if (!en) {
158                 writel(readl(RTC_RTSR) & ~RTC_RTSR_HZE, RTC_RTSR);
159         } else {
160                 /* re-enable the device, and check it is ok */
161                 if ((readl(RTC_RTSR) & RTC_RTSR_HZE) == 0) {
162                         dev_info(dev, "rtc disabled, re-enabling\n");
163                         writel(readl(RTC_RTSR) | RTC_RTSR_HZE, RTC_RTSR);
164                 }
165         }
166 }
167
168 static int puv3_rtc_remove(struct platform_device *dev)
169 {
170         puv3_rtc_setpie(&dev->dev, 0);
171         puv3_rtc_setaie(&dev->dev, 0);
172
173         release_resource(puv3_rtc_mem);
174         kfree(puv3_rtc_mem);
175
176         return 0;
177 }
178
179 static int puv3_rtc_probe(struct platform_device *pdev)
180 {
181         struct rtc_device *rtc;
182         struct resource *res;
183         int ret;
184
185         dev_dbg(&pdev->dev, "%s: probe=%p\n", __func__, pdev);
186
187         /* find the IRQs */
188         puv3_rtc_tickno = platform_get_irq(pdev, 1);
189         if (puv3_rtc_tickno < 0)
190                 return -ENOENT;
191
192         puv3_rtc_alarmno = platform_get_irq(pdev, 0);
193         if (puv3_rtc_alarmno < 0)
194                 return -ENOENT;
195
196         dev_dbg(&pdev->dev, "PKUnity_rtc: tick irq %d, alarm irq %d\n",
197                  puv3_rtc_tickno, puv3_rtc_alarmno);
198
199         rtc = devm_rtc_allocate_device(&pdev->dev);
200         if (IS_ERR(rtc))
201                 return PTR_ERR(rtc);
202
203         ret = devm_request_irq(&pdev->dev, puv3_rtc_alarmno, puv3_rtc_alarmirq,
204                                0, "pkunity-rtc alarm", rtc);
205         if (ret) {
206                 dev_err(&pdev->dev, "IRQ%d error %d\n", puv3_rtc_alarmno, ret);
207                 return ret;
208         }
209
210         ret = devm_request_irq(&pdev->dev, puv3_rtc_tickno, puv3_rtc_tickirq,
211                                0, "pkunity-rtc tick", rtc);
212         if (ret) {
213                 dev_err(&pdev->dev, "IRQ%d error %d\n", puv3_rtc_tickno, ret);
214                 return ret;
215         }
216
217         /* get the memory region */
218         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
219         if (res == NULL) {
220                 dev_err(&pdev->dev, "failed to get memory region resource\n");
221                 return -ENOENT;
222         }
223
224         puv3_rtc_mem = request_mem_region(res->start, resource_size(res),
225                                           pdev->name);
226
227         if (puv3_rtc_mem == NULL) {
228                 dev_err(&pdev->dev, "failed to reserve memory region\n");
229                 ret = -ENOENT;
230                 goto err_nores;
231         }
232
233         puv3_rtc_enable(&pdev->dev, 1);
234
235         /* register RTC and exit */
236         rtc->ops = &puv3_rtcops;
237         ret = rtc_register_device(rtc);
238         if (ret)
239                 goto err_nortc;
240
241         /* platform setup code should have handled this; sigh */
242         if (!device_can_wakeup(&pdev->dev))
243                 device_init_wakeup(&pdev->dev, 1);
244
245         platform_set_drvdata(pdev, rtc);
246         return 0;
247
248  err_nortc:
249         puv3_rtc_enable(&pdev->dev, 0);
250         release_resource(puv3_rtc_mem);
251
252  err_nores:
253         return ret;
254 }
255
256 #ifdef CONFIG_PM_SLEEP
257 static int ticnt_save;
258
259 static int puv3_rtc_suspend(struct device *dev)
260 {
261         /* save RTAR for anyone using periodic interrupts */
262         ticnt_save = readl(RTC_RTAR);
263         puv3_rtc_enable(dev, 0);
264         return 0;
265 }
266
267 static int puv3_rtc_resume(struct device *dev)
268 {
269         puv3_rtc_enable(dev, 1);
270         writel(ticnt_save, RTC_RTAR);
271         return 0;
272 }
273 #endif
274
275 static SIMPLE_DEV_PM_OPS(puv3_rtc_pm_ops, puv3_rtc_suspend, puv3_rtc_resume);
276
277 static struct platform_driver puv3_rtc_driver = {
278         .probe          = puv3_rtc_probe,
279         .remove         = puv3_rtc_remove,
280         .driver         = {
281                 .name   = "PKUnity-v3-RTC",
282                 .pm     = &puv3_rtc_pm_ops,
283         }
284 };
285
286 module_platform_driver(puv3_rtc_driver);
287
288 MODULE_DESCRIPTION("RTC Driver for the PKUnity v3 chip");
289 MODULE_AUTHOR("Hu Dongliang");
290 MODULE_LICENSE("GPL v2");