Linux-2.6.12-rc2
[pandora-kernel.git] / arch / arm / mach-integrator / time.c
1 /*
2  *  linux/arch/arm/mach-integrator/time.c
3  *
4  *  Copyright (C) 2000-2001 Deep Blue Solutions Ltd.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/time.h>
13 #include <linux/mc146818rtc.h>
14 #include <linux/interrupt.h>
15 #include <linux/init.h>
16 #include <linux/device.h>
17
18 #include <asm/hardware/amba.h>
19 #include <asm/hardware.h>
20 #include <asm/io.h>
21 #include <asm/uaccess.h>
22 #include <asm/rtc.h>
23
24 #include <asm/mach/time.h>
25
26 #define RTC_DR          (0)
27 #define RTC_MR          (4)
28 #define RTC_STAT        (8)
29 #define RTC_EOI         (8)
30 #define RTC_LR          (12)
31 #define RTC_CR          (16)
32 #define RTC_CR_MIE      (1 << 0)
33
34 extern int (*set_rtc)(void);
35 static void __iomem *rtc_base;
36
37 static int integrator_set_rtc(void)
38 {
39         __raw_writel(xtime.tv_sec, rtc_base + RTC_LR);
40         return 1;
41 }
42
43 static void rtc_read_alarm(struct rtc_wkalrm *alrm)
44 {
45         rtc_time_to_tm(readl(rtc_base + RTC_MR), &alrm->time);
46 }
47
48 static int rtc_set_alarm(struct rtc_wkalrm *alrm)
49 {
50         unsigned long time;
51         int ret;
52
53         ret = rtc_tm_to_time(&alrm->time, &time);
54         if (ret == 0)
55                 writel(time, rtc_base + RTC_MR);
56         return ret;
57 }
58
59 static void rtc_read_time(struct rtc_time *tm)
60 {
61         rtc_time_to_tm(readl(rtc_base + RTC_DR), tm);
62 }
63
64 /*
65  * Set the RTC time.  Unfortunately, we can't accurately set
66  * the point at which the counter updates.
67  *
68  * Also, since RTC_LR is transferred to RTC_CR on next rising
69  * edge of the 1Hz clock, we must write the time one second
70  * in advance.
71  */
72 static int rtc_set_time(struct rtc_time *tm)
73 {
74         unsigned long time;
75         int ret;
76
77         ret = rtc_tm_to_time(tm, &time);
78         if (ret == 0)
79                 writel(time + 1, rtc_base + RTC_LR);
80
81         return ret;
82 }
83
84 static struct rtc_ops rtc_ops = {
85         .owner          = THIS_MODULE,
86         .read_time      = rtc_read_time,
87         .set_time       = rtc_set_time,
88         .read_alarm     = rtc_read_alarm,
89         .set_alarm      = rtc_set_alarm,
90 };
91
92 static irqreturn_t rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
93 {
94         writel(0, rtc_base + RTC_EOI);
95         return IRQ_HANDLED;
96 }
97
98 static int rtc_probe(struct amba_device *dev, void *id)
99 {
100         int ret;
101
102         if (rtc_base)
103                 return -EBUSY;
104
105         ret = amba_request_regions(dev, NULL);
106         if (ret)
107                 goto out;
108
109         rtc_base = ioremap(dev->res.start, SZ_4K);
110         if (!rtc_base) {
111                 ret = -ENOMEM;
112                 goto res_out;
113         }
114
115         __raw_writel(0, rtc_base + RTC_CR);
116         __raw_writel(0, rtc_base + RTC_EOI);
117
118         xtime.tv_sec = __raw_readl(rtc_base + RTC_DR);
119
120         ret = request_irq(dev->irq[0], rtc_interrupt, SA_INTERRUPT,
121                           "rtc-pl030", dev);
122         if (ret)
123                 goto map_out;
124
125         ret = register_rtc(&rtc_ops);
126         if (ret)
127                 goto irq_out;
128
129         set_rtc = integrator_set_rtc;
130         return 0;
131
132  irq_out:
133         free_irq(dev->irq[0], dev);
134  map_out:
135         iounmap(rtc_base);
136         rtc_base = NULL;
137  res_out:
138         amba_release_regions(dev);
139  out:
140         return ret;
141 }
142
143 static int rtc_remove(struct amba_device *dev)
144 {
145         set_rtc = NULL;
146
147         writel(0, rtc_base + RTC_CR);
148
149         free_irq(dev->irq[0], dev);
150         unregister_rtc(&rtc_ops);
151
152         iounmap(rtc_base);
153         rtc_base = NULL;
154         amba_release_regions(dev);
155
156         return 0;
157 }
158
159 static struct timespec rtc_delta;
160
161 static int rtc_suspend(struct amba_device *dev, pm_message_t state)
162 {
163         struct timespec rtc;
164
165         rtc.tv_sec = readl(rtc_base + RTC_DR);
166         rtc.tv_nsec = 0;
167         save_time_delta(&rtc_delta, &rtc);
168
169         return 0;
170 }
171
172 static int rtc_resume(struct amba_device *dev)
173 {
174         struct timespec rtc;
175
176         rtc.tv_sec = readl(rtc_base + RTC_DR);
177         rtc.tv_nsec = 0;
178         restore_time_delta(&rtc_delta, &rtc);
179
180         return 0;
181 }
182
183 static struct amba_id rtc_ids[] = {
184         {
185                 .id     = 0x00041030,
186                 .mask   = 0x000fffff,
187         },
188         { 0, 0 },
189 };
190
191 static struct amba_driver rtc_driver = {
192         .drv            = {
193                 .name   = "rtc-pl030",
194         },
195         .probe          = rtc_probe,
196         .remove         = rtc_remove,
197         .suspend        = rtc_suspend,
198         .resume         = rtc_resume,
199         .id_table       = rtc_ids,
200 };
201
202 static int __init integrator_rtc_init(void)
203 {
204         return amba_driver_register(&rtc_driver);
205 }
206
207 static void __exit integrator_rtc_exit(void)
208 {
209         amba_driver_unregister(&rtc_driver);
210 }
211
212 module_init(integrator_rtc_init);
213 module_exit(integrator_rtc_exit);