Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / rtc / rtc-pl030.c
blob8448eeb9d675f8ca37c79e506ba63143ea7bf1a2
1 /*
2 * linux/drivers/rtc/rtc-pl030.c
4 * Copyright (C) 2000-2001 Deep Blue Solutions Ltd.
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/rtc.h>
12 #include <linux/init.h>
13 #include <linux/interrupt.h>
14 #include <linux/amba/bus.h>
15 #include <linux/io.h>
17 #define RTC_DR (0)
18 #define RTC_MR (4)
19 #define RTC_STAT (8)
20 #define RTC_EOI (8)
21 #define RTC_LR (12)
22 #define RTC_CR (16)
23 #define RTC_CR_MIE (1 << 0)
25 struct pl030_rtc {
26 struct rtc_device *rtc;
27 void __iomem *base;
30 static irqreturn_t pl030_interrupt(int irq, void *dev_id)
32 struct pl030_rtc *rtc = dev_id;
33 writel(0, rtc->base + RTC_EOI);
34 return IRQ_HANDLED;
37 static int pl030_open(struct device *dev)
39 return 0;
42 static void pl030_release(struct device *dev)
46 static int pl030_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
48 return -ENOIOCTLCMD;
51 static int pl030_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
53 struct pl030_rtc *rtc = dev_get_drvdata(dev);
55 rtc_time_to_tm(readl(rtc->base + RTC_MR), &alrm->time);
56 return 0;
59 static int pl030_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
61 struct pl030_rtc *rtc = dev_get_drvdata(dev);
62 unsigned long time;
63 int ret;
66 * At the moment, we can only deal with non-wildcarded alarm times.
68 ret = rtc_valid_tm(&alrm->time);
69 if (ret == 0)
70 ret = rtc_tm_to_time(&alrm->time, &time);
71 if (ret == 0)
72 writel(time, rtc->base + RTC_MR);
73 return ret;
76 static int pl030_read_time(struct device *dev, struct rtc_time *tm)
78 struct pl030_rtc *rtc = dev_get_drvdata(dev);
80 rtc_time_to_tm(readl(rtc->base + RTC_DR), tm);
82 return 0;
86 * Set the RTC time. Unfortunately, we can't accurately set
87 * the point at which the counter updates.
89 * Also, since RTC_LR is transferred to RTC_CR on next rising
90 * edge of the 1Hz clock, we must write the time one second
91 * in advance.
93 static int pl030_set_time(struct device *dev, struct rtc_time *tm)
95 struct pl030_rtc *rtc = dev_get_drvdata(dev);
96 unsigned long time;
97 int ret;
99 ret = rtc_tm_to_time(tm, &time);
100 if (ret == 0)
101 writel(time + 1, rtc->base + RTC_LR);
103 return ret;
106 static const struct rtc_class_ops pl030_ops = {
107 .open = pl030_open,
108 .release = pl030_release,
109 .ioctl = pl030_ioctl,
110 .read_time = pl030_read_time,
111 .set_time = pl030_set_time,
112 .read_alarm = pl030_read_alarm,
113 .set_alarm = pl030_set_alarm,
116 static int pl030_probe(struct amba_device *dev, void *id)
118 struct pl030_rtc *rtc;
119 int ret;
121 ret = amba_request_regions(dev, NULL);
122 if (ret)
123 goto err_req;
125 rtc = kmalloc(sizeof(*rtc), GFP_KERNEL);
126 if (!rtc) {
127 ret = -ENOMEM;
128 goto err_rtc;
131 rtc->base = ioremap(dev->res.start, SZ_4K);
132 if (!rtc->base) {
133 ret = -ENOMEM;
134 goto err_map;
137 __raw_writel(0, rtc->base + RTC_CR);
138 __raw_writel(0, rtc->base + RTC_EOI);
140 amba_set_drvdata(dev, rtc);
142 ret = request_irq(dev->irq[0], pl030_interrupt, IRQF_DISABLED,
143 "rtc-pl030", rtc);
144 if (ret)
145 goto err_irq;
147 rtc->rtc = rtc_device_register("pl030", &dev->dev, &pl030_ops,
148 THIS_MODULE);
149 if (IS_ERR(rtc->rtc)) {
150 ret = PTR_ERR(rtc->rtc);
151 goto err_reg;
154 return 0;
156 err_reg:
157 free_irq(dev->irq[0], rtc);
158 err_irq:
159 iounmap(rtc->base);
160 err_map:
161 kfree(rtc);
162 err_rtc:
163 amba_release_regions(dev);
164 err_req:
165 return ret;
168 static int pl030_remove(struct amba_device *dev)
170 struct pl030_rtc *rtc = amba_get_drvdata(dev);
172 amba_set_drvdata(dev, NULL);
174 writel(0, rtc->base + RTC_CR);
176 free_irq(dev->irq[0], rtc);
177 rtc_device_unregister(rtc->rtc);
178 iounmap(rtc->base);
179 kfree(rtc);
180 amba_release_regions(dev);
182 return 0;
185 static struct amba_id pl030_ids[] = {
187 .id = 0x00041030,
188 .mask = 0x000fffff,
190 { 0, 0 },
193 static struct amba_driver pl030_driver = {
194 .drv = {
195 .name = "rtc-pl030",
197 .probe = pl030_probe,
198 .remove = pl030_remove,
199 .id_table = pl030_ids,
202 static int __init pl030_init(void)
204 return amba_driver_register(&pl030_driver);
207 static void __exit pl030_exit(void)
209 amba_driver_unregister(&pl030_driver);
212 module_init(pl030_init);
213 module_exit(pl030_exit);
215 MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
216 MODULE_DESCRIPTION("ARM AMBA PL030 RTC Driver");
217 MODULE_LICENSE("GPL");