1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/drivers/rtc/rtc-pl030.c
5 * Copyright (C) 2000-2001 Deep Blue Solutions Ltd.
7 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/interrupt.h>
11 #include <linux/amba/bus.h>
13 #include <linux/slab.h>
21 #define RTC_CR_MIE (1 << 0)
24 struct rtc_device
*rtc
;
28 static irqreturn_t
pl030_interrupt(int irq
, void *dev_id
)
30 struct pl030_rtc
*rtc
= dev_id
;
31 writel(0, rtc
->base
+ RTC_EOI
);
35 static int pl030_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
37 struct pl030_rtc
*rtc
= dev_get_drvdata(dev
);
39 rtc_time64_to_tm(readl(rtc
->base
+ RTC_MR
), &alrm
->time
);
43 static int pl030_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
45 struct pl030_rtc
*rtc
= dev_get_drvdata(dev
);
47 writel(rtc_tm_to_time64(&alrm
->time
), rtc
->base
+ RTC_MR
);
52 static int pl030_read_time(struct device
*dev
, struct rtc_time
*tm
)
54 struct pl030_rtc
*rtc
= dev_get_drvdata(dev
);
56 rtc_time64_to_tm(readl(rtc
->base
+ RTC_DR
), tm
);
62 * Set the RTC time. Unfortunately, we can't accurately set
63 * the point at which the counter updates.
65 * Also, since RTC_LR is transferred to RTC_CR on next rising
66 * edge of the 1Hz clock, we must write the time one second
69 static int pl030_set_time(struct device
*dev
, struct rtc_time
*tm
)
71 struct pl030_rtc
*rtc
= dev_get_drvdata(dev
);
73 writel(rtc_tm_to_time64(tm
) + 1, rtc
->base
+ RTC_LR
);
78 static const struct rtc_class_ops pl030_ops
= {
79 .read_time
= pl030_read_time
,
80 .set_time
= pl030_set_time
,
81 .read_alarm
= pl030_read_alarm
,
82 .set_alarm
= pl030_set_alarm
,
85 static int pl030_probe(struct amba_device
*dev
, const struct amba_id
*id
)
87 struct pl030_rtc
*rtc
;
90 ret
= amba_request_regions(dev
, NULL
);
94 rtc
= devm_kzalloc(&dev
->dev
, sizeof(*rtc
), GFP_KERNEL
);
100 rtc
->rtc
= devm_rtc_allocate_device(&dev
->dev
);
101 if (IS_ERR(rtc
->rtc
)) {
102 ret
= PTR_ERR(rtc
->rtc
);
106 rtc
->rtc
->ops
= &pl030_ops
;
107 rtc
->rtc
->range_max
= U32_MAX
;
108 rtc
->base
= ioremap(dev
->res
.start
, resource_size(&dev
->res
));
114 __raw_writel(0, rtc
->base
+ RTC_CR
);
115 __raw_writel(0, rtc
->base
+ RTC_EOI
);
117 amba_set_drvdata(dev
, rtc
);
119 ret
= request_irq(dev
->irq
[0], pl030_interrupt
, 0,
124 ret
= rtc_register_device(rtc
->rtc
);
131 free_irq(dev
->irq
[0], rtc
);
135 amba_release_regions(dev
);
140 static int pl030_remove(struct amba_device
*dev
)
142 struct pl030_rtc
*rtc
= amba_get_drvdata(dev
);
144 writel(0, rtc
->base
+ RTC_CR
);
146 free_irq(dev
->irq
[0], rtc
);
148 amba_release_regions(dev
);
153 static struct amba_id pl030_ids
[] = {
161 MODULE_DEVICE_TABLE(amba
, pl030_ids
);
163 static struct amba_driver pl030_driver
= {
167 .probe
= pl030_probe
,
168 .remove
= pl030_remove
,
169 .id_table
= pl030_ids
,
172 module_amba_driver(pl030_driver
);
174 MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
175 MODULE_DESCRIPTION("ARM AMBA PL030 RTC Driver");
176 MODULE_LICENSE("GPL");