blob: bbdb2f02798a13187c91475696fa9f84594f8f47 [file] [log] [blame]
Russell Kinga1909012008-04-20 12:08:36 +01001/*
2 * linux/drivers/rtc/rtc-pl030.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/rtc.h>
12#include <linux/init.h>
13#include <linux/interrupt.h>
14#include <linux/amba/bus.h>
15#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Russell Kinga1909012008-04-20 12:08:36 +010017
18#define RTC_DR (0)
19#define RTC_MR (4)
20#define RTC_STAT (8)
21#define RTC_EOI (8)
22#define RTC_LR (12)
23#define RTC_CR (16)
24#define RTC_CR_MIE (1 << 0)
25
26struct pl030_rtc {
27 struct rtc_device *rtc;
28 void __iomem *base;
29};
30
31static irqreturn_t pl030_interrupt(int irq, void *dev_id)
32{
33 struct pl030_rtc *rtc = dev_id;
34 writel(0, rtc->base + RTC_EOI);
35 return IRQ_HANDLED;
36}
37
Russell Kinga1909012008-04-20 12:08:36 +010038static int pl030_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
39{
40 return -ENOIOCTLCMD;
41}
42
43static int pl030_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
44{
45 struct pl030_rtc *rtc = dev_get_drvdata(dev);
46
47 rtc_time_to_tm(readl(rtc->base + RTC_MR), &alrm->time);
48 return 0;
49}
50
51static int pl030_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
52{
53 struct pl030_rtc *rtc = dev_get_drvdata(dev);
54 unsigned long time;
55 int ret;
56
57 /*
58 * At the moment, we can only deal with non-wildcarded alarm times.
59 */
60 ret = rtc_valid_tm(&alrm->time);
61 if (ret == 0)
62 ret = rtc_tm_to_time(&alrm->time, &time);
63 if (ret == 0)
64 writel(time, rtc->base + RTC_MR);
65 return ret;
66}
67
68static int pl030_read_time(struct device *dev, struct rtc_time *tm)
69{
70 struct pl030_rtc *rtc = dev_get_drvdata(dev);
71
72 rtc_time_to_tm(readl(rtc->base + RTC_DR), tm);
73
74 return 0;
75}
76
77/*
78 * Set the RTC time. Unfortunately, we can't accurately set
79 * the point at which the counter updates.
80 *
81 * Also, since RTC_LR is transferred to RTC_CR on next rising
82 * edge of the 1Hz clock, we must write the time one second
83 * in advance.
84 */
85static int pl030_set_time(struct device *dev, struct rtc_time *tm)
86{
87 struct pl030_rtc *rtc = dev_get_drvdata(dev);
88 unsigned long time;
89 int ret;
90
91 ret = rtc_tm_to_time(tm, &time);
92 if (ret == 0)
93 writel(time + 1, rtc->base + RTC_LR);
94
95 return ret;
96}
97
98static const struct rtc_class_ops pl030_ops = {
Russell Kinga1909012008-04-20 12:08:36 +010099 .ioctl = pl030_ioctl,
100 .read_time = pl030_read_time,
101 .set_time = pl030_set_time,
102 .read_alarm = pl030_read_alarm,
103 .set_alarm = pl030_set_alarm,
104};
105
Alessandro Rubini03fbdb12009-05-20 22:39:08 +0100106static int pl030_probe(struct amba_device *dev, struct amba_id *id)
Russell Kinga1909012008-04-20 12:08:36 +0100107{
108 struct pl030_rtc *rtc;
109 int ret;
110
111 ret = amba_request_regions(dev, NULL);
112 if (ret)
113 goto err_req;
114
115 rtc = kmalloc(sizeof(*rtc), GFP_KERNEL);
116 if (!rtc) {
117 ret = -ENOMEM;
118 goto err_rtc;
119 }
120
Linus Walleijdc890c22009-06-07 23:27:31 +0100121 rtc->base = ioremap(dev->res.start, resource_size(&dev->res));
Russell Kinga1909012008-04-20 12:08:36 +0100122 if (!rtc->base) {
123 ret = -ENOMEM;
124 goto err_map;
125 }
126
127 __raw_writel(0, rtc->base + RTC_CR);
128 __raw_writel(0, rtc->base + RTC_EOI);
129
130 amba_set_drvdata(dev, rtc);
131
132 ret = request_irq(dev->irq[0], pl030_interrupt, IRQF_DISABLED,
133 "rtc-pl030", rtc);
134 if (ret)
135 goto err_irq;
136
137 rtc->rtc = rtc_device_register("pl030", &dev->dev, &pl030_ops,
138 THIS_MODULE);
139 if (IS_ERR(rtc->rtc)) {
140 ret = PTR_ERR(rtc->rtc);
141 goto err_reg;
142 }
143
144 return 0;
145
146 err_reg:
147 free_irq(dev->irq[0], rtc);
148 err_irq:
149 iounmap(rtc->base);
150 err_map:
151 kfree(rtc);
152 err_rtc:
153 amba_release_regions(dev);
154 err_req:
155 return ret;
156}
157
158static int pl030_remove(struct amba_device *dev)
159{
160 struct pl030_rtc *rtc = amba_get_drvdata(dev);
161
162 amba_set_drvdata(dev, NULL);
163
164 writel(0, rtc->base + RTC_CR);
165
166 free_irq(dev->irq[0], rtc);
167 rtc_device_unregister(rtc->rtc);
168 iounmap(rtc->base);
169 kfree(rtc);
170 amba_release_regions(dev);
171
172 return 0;
173}
174
175static struct amba_id pl030_ids[] = {
176 {
177 .id = 0x00041030,
178 .mask = 0x000fffff,
179 },
180 { 0, 0 },
181};
182
183static struct amba_driver pl030_driver = {
184 .drv = {
185 .name = "rtc-pl030",
186 },
187 .probe = pl030_probe,
188 .remove = pl030_remove,
189 .id_table = pl030_ids,
190};
191
192static int __init pl030_init(void)
193{
194 return amba_driver_register(&pl030_driver);
195}
196
197static void __exit pl030_exit(void)
198{
199 amba_driver_unregister(&pl030_driver);
200}
201
202module_init(pl030_init);
203module_exit(pl030_exit);
204
205MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
206MODULE_DESCRIPTION("ARM AMBA PL030 RTC Driver");
207MODULE_LICENSE("GPL");