can: c_can: fix race condition in c_can_open()
[linux/fpc-iii.git] / drivers / rtc / rtc-pl030.c
blob1d28d4451dae548b26e6bdffb2d22e3dbb062fd7
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>
16 #include <linux/slab.h>
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)
26 struct pl030_rtc {
27 struct rtc_device *rtc;
28 void __iomem *base;
31 static irqreturn_t pl030_interrupt(int irq, void *dev_id)
33 struct pl030_rtc *rtc = dev_id;
34 writel(0, rtc->base + RTC_EOI);
35 return IRQ_HANDLED;
38 static int pl030_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
40 struct pl030_rtc *rtc = dev_get_drvdata(dev);
42 rtc_time_to_tm(readl(rtc->base + RTC_MR), &alrm->time);
43 return 0;
46 static int pl030_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
48 struct pl030_rtc *rtc = dev_get_drvdata(dev);
49 unsigned long time;
50 int ret;
53 * At the moment, we can only deal with non-wildcarded alarm times.
55 ret = rtc_valid_tm(&alrm->time);
56 if (ret == 0)
57 ret = rtc_tm_to_time(&alrm->time, &time);
58 if (ret == 0)
59 writel(time, rtc->base + RTC_MR);
60 return ret;
63 static int pl030_read_time(struct device *dev, struct rtc_time *tm)
65 struct pl030_rtc *rtc = dev_get_drvdata(dev);
67 rtc_time_to_tm(readl(rtc->base + RTC_DR), tm);
69 return 0;
73 * Set the RTC time. Unfortunately, we can't accurately set
74 * the point at which the counter updates.
76 * Also, since RTC_LR is transferred to RTC_CR on next rising
77 * edge of the 1Hz clock, we must write the time one second
78 * in advance.
80 static int pl030_set_time(struct device *dev, struct rtc_time *tm)
82 struct pl030_rtc *rtc = dev_get_drvdata(dev);
83 unsigned long time;
84 int ret;
86 ret = rtc_tm_to_time(tm, &time);
87 if (ret == 0)
88 writel(time + 1, rtc->base + RTC_LR);
90 return ret;
93 static const struct rtc_class_ops pl030_ops = {
94 .read_time = pl030_read_time,
95 .set_time = pl030_set_time,
96 .read_alarm = pl030_read_alarm,
97 .set_alarm = pl030_set_alarm,
100 static int pl030_probe(struct amba_device *dev, const struct amba_id *id)
102 struct pl030_rtc *rtc;
103 int ret;
105 ret = amba_request_regions(dev, NULL);
106 if (ret)
107 goto err_req;
109 rtc = kmalloc(sizeof(*rtc), GFP_KERNEL);
110 if (!rtc) {
111 ret = -ENOMEM;
112 goto err_rtc;
115 rtc->base = ioremap(dev->res.start, resource_size(&dev->res));
116 if (!rtc->base) {
117 ret = -ENOMEM;
118 goto err_map;
121 __raw_writel(0, rtc->base + RTC_CR);
122 __raw_writel(0, rtc->base + RTC_EOI);
124 amba_set_drvdata(dev, rtc);
126 ret = request_irq(dev->irq[0], pl030_interrupt, IRQF_DISABLED,
127 "rtc-pl030", rtc);
128 if (ret)
129 goto err_irq;
131 rtc->rtc = rtc_device_register("pl030", &dev->dev, &pl030_ops,
132 THIS_MODULE);
133 if (IS_ERR(rtc->rtc)) {
134 ret = PTR_ERR(rtc->rtc);
135 goto err_reg;
138 return 0;
140 err_reg:
141 free_irq(dev->irq[0], rtc);
142 err_irq:
143 iounmap(rtc->base);
144 err_map:
145 kfree(rtc);
146 err_rtc:
147 amba_release_regions(dev);
148 err_req:
149 return ret;
152 static int pl030_remove(struct amba_device *dev)
154 struct pl030_rtc *rtc = amba_get_drvdata(dev);
156 amba_set_drvdata(dev, NULL);
158 writel(0, rtc->base + RTC_CR);
160 free_irq(dev->irq[0], rtc);
161 rtc_device_unregister(rtc->rtc);
162 iounmap(rtc->base);
163 kfree(rtc);
164 amba_release_regions(dev);
166 return 0;
169 static struct amba_id pl030_ids[] = {
171 .id = 0x00041030,
172 .mask = 0x000fffff,
174 { 0, 0 },
177 static struct amba_driver pl030_driver = {
178 .drv = {
179 .name = "rtc-pl030",
181 .probe = pl030_probe,
182 .remove = pl030_remove,
183 .id_table = pl030_ids,
186 static int __init pl030_init(void)
188 return amba_driver_register(&pl030_driver);
191 static void __exit pl030_exit(void)
193 amba_driver_unregister(&pl030_driver);
196 module_init(pl030_init);
197 module_exit(pl030_exit);
199 MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
200 MODULE_DESCRIPTION("ARM AMBA PL030 RTC Driver");
201 MODULE_LICENSE("GPL");