xref: /openbmc/linux/drivers/rtc/rtc-pl030.c (revision d2912cb15bdda8ba4a5dd73396ad62641af2f520)
1*d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2a190901cSRussell King /*
3a190901cSRussell King  *  linux/drivers/rtc/rtc-pl030.c
4a190901cSRussell King  *
5a190901cSRussell King  *  Copyright (C) 2000-2001 Deep Blue Solutions Ltd.
6a190901cSRussell King  */
7a190901cSRussell King #include <linux/module.h>
8a190901cSRussell King #include <linux/rtc.h>
9a190901cSRussell King #include <linux/init.h>
10a190901cSRussell King #include <linux/interrupt.h>
11a190901cSRussell King #include <linux/amba/bus.h>
12a190901cSRussell King #include <linux/io.h>
135a0e3ad6STejun Heo #include <linux/slab.h>
14a190901cSRussell King 
15a190901cSRussell King #define RTC_DR		(0)
16a190901cSRussell King #define RTC_MR		(4)
17a190901cSRussell King #define RTC_STAT	(8)
18a190901cSRussell King #define RTC_EOI		(8)
19a190901cSRussell King #define RTC_LR		(12)
20a190901cSRussell King #define RTC_CR		(16)
21a190901cSRussell King #define RTC_CR_MIE	(1 << 0)
22a190901cSRussell King 
23a190901cSRussell King struct pl030_rtc {
24a190901cSRussell King 	struct rtc_device	*rtc;
25a190901cSRussell King 	void __iomem		*base;
26a190901cSRussell King };
27a190901cSRussell King 
28a190901cSRussell King static irqreturn_t pl030_interrupt(int irq, void *dev_id)
29a190901cSRussell King {
30a190901cSRussell King 	struct pl030_rtc *rtc = dev_id;
31a190901cSRussell King 	writel(0, rtc->base + RTC_EOI);
32a190901cSRussell King 	return IRQ_HANDLED;
33a190901cSRussell King }
34a190901cSRussell King 
35a190901cSRussell King static int pl030_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
36a190901cSRussell King {
37a190901cSRussell King 	struct pl030_rtc *rtc = dev_get_drvdata(dev);
38a190901cSRussell King 
39a190901cSRussell King 	rtc_time_to_tm(readl(rtc->base + RTC_MR), &alrm->time);
40a190901cSRussell King 	return 0;
41a190901cSRussell King }
42a190901cSRussell King 
43a190901cSRussell King static int pl030_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
44a190901cSRussell King {
45a190901cSRussell King 	struct pl030_rtc *rtc = dev_get_drvdata(dev);
46a190901cSRussell King 	unsigned long time;
47a190901cSRussell King 	int ret;
48a190901cSRussell King 
49a190901cSRussell King 	/*
50a190901cSRussell King 	 * At the moment, we can only deal with non-wildcarded alarm times.
51a190901cSRussell King 	 */
52a190901cSRussell King 	ret = rtc_valid_tm(&alrm->time);
53a190901cSRussell King 	if (ret == 0)
54a190901cSRussell King 		ret = rtc_tm_to_time(&alrm->time, &time);
55a190901cSRussell King 	if (ret == 0)
56a190901cSRussell King 		writel(time, rtc->base + RTC_MR);
57a190901cSRussell King 	return ret;
58a190901cSRussell King }
59a190901cSRussell King 
60a190901cSRussell King static int pl030_read_time(struct device *dev, struct rtc_time *tm)
61a190901cSRussell King {
62a190901cSRussell King 	struct pl030_rtc *rtc = dev_get_drvdata(dev);
63a190901cSRussell King 
64a190901cSRussell King 	rtc_time_to_tm(readl(rtc->base + RTC_DR), tm);
65a190901cSRussell King 
66a190901cSRussell King 	return 0;
67a190901cSRussell King }
68a190901cSRussell King 
69a190901cSRussell King /*
70a190901cSRussell King  * Set the RTC time.  Unfortunately, we can't accurately set
71a190901cSRussell King  * the point at which the counter updates.
72a190901cSRussell King  *
73a190901cSRussell King  * Also, since RTC_LR is transferred to RTC_CR on next rising
74a190901cSRussell King  * edge of the 1Hz clock, we must write the time one second
75a190901cSRussell King  * in advance.
76a190901cSRussell King  */
77a190901cSRussell King static int pl030_set_time(struct device *dev, struct rtc_time *tm)
78a190901cSRussell King {
79a190901cSRussell King 	struct pl030_rtc *rtc = dev_get_drvdata(dev);
80a190901cSRussell King 	unsigned long time;
81a190901cSRussell King 	int ret;
82a190901cSRussell King 
83a190901cSRussell King 	ret = rtc_tm_to_time(tm, &time);
84a190901cSRussell King 	if (ret == 0)
85a190901cSRussell King 		writel(time + 1, rtc->base + RTC_LR);
86a190901cSRussell King 
87a190901cSRussell King 	return ret;
88a190901cSRussell King }
89a190901cSRussell King 
90a190901cSRussell King static const struct rtc_class_ops pl030_ops = {
91a190901cSRussell King 	.read_time	= pl030_read_time,
92a190901cSRussell King 	.set_time	= pl030_set_time,
93a190901cSRussell King 	.read_alarm	= pl030_read_alarm,
94a190901cSRussell King 	.set_alarm	= pl030_set_alarm,
95a190901cSRussell King };
96a190901cSRussell King 
97aa25afadSRussell King static int pl030_probe(struct amba_device *dev, const struct amba_id *id)
98a190901cSRussell King {
99a190901cSRussell King 	struct pl030_rtc *rtc;
100a190901cSRussell King 	int ret;
101a190901cSRussell King 
102a190901cSRussell King 	ret = amba_request_regions(dev, NULL);
103a190901cSRussell King 	if (ret)
104a190901cSRussell King 		goto err_req;
105a190901cSRussell King 
10658c181c8SSangjung Woo 	rtc = devm_kzalloc(&dev->dev, sizeof(*rtc), GFP_KERNEL);
107a190901cSRussell King 	if (!rtc) {
108a190901cSRussell King 		ret = -ENOMEM;
109a190901cSRussell King 		goto err_rtc;
110a190901cSRussell King 	}
111a190901cSRussell King 
112c778ec85SAlexandre Belloni 	rtc->rtc = devm_rtc_allocate_device(&dev->dev);
113c778ec85SAlexandre Belloni 	if (IS_ERR(rtc->rtc)) {
114c778ec85SAlexandre Belloni 		ret = PTR_ERR(rtc->rtc);
115c778ec85SAlexandre Belloni 		goto err_rtc;
116c778ec85SAlexandre Belloni 	}
117c778ec85SAlexandre Belloni 
118c778ec85SAlexandre Belloni 	rtc->rtc->ops = &pl030_ops;
119dc890c2dSLinus Walleij 	rtc->base = ioremap(dev->res.start, resource_size(&dev->res));
120a190901cSRussell King 	if (!rtc->base) {
121a190901cSRussell King 		ret = -ENOMEM;
12258c181c8SSangjung Woo 		goto err_rtc;
123a190901cSRussell King 	}
124a190901cSRussell King 
125a190901cSRussell King 	__raw_writel(0, rtc->base + RTC_CR);
126a190901cSRussell King 	__raw_writel(0, rtc->base + RTC_EOI);
127a190901cSRussell King 
128a190901cSRussell King 	amba_set_drvdata(dev, rtc);
129a190901cSRussell King 
1302f6e5f94SYong Zhang 	ret = request_irq(dev->irq[0], pl030_interrupt, 0,
131a190901cSRussell King 			  "rtc-pl030", rtc);
132a190901cSRussell King 	if (ret)
133a190901cSRussell King 		goto err_irq;
134a190901cSRussell King 
135c778ec85SAlexandre Belloni 	ret = rtc_register_device(rtc->rtc);
136c778ec85SAlexandre Belloni 	if (ret)
137a190901cSRussell King 		goto err_reg;
138a190901cSRussell King 
139a190901cSRussell King 	return 0;
140a190901cSRussell King 
141a190901cSRussell King  err_reg:
142a190901cSRussell King 	free_irq(dev->irq[0], rtc);
143a190901cSRussell King  err_irq:
144a190901cSRussell King 	iounmap(rtc->base);
145a190901cSRussell King  err_rtc:
146a190901cSRussell King 	amba_release_regions(dev);
147a190901cSRussell King  err_req:
148a190901cSRussell King 	return ret;
149a190901cSRussell King }
150a190901cSRussell King 
151a190901cSRussell King static int pl030_remove(struct amba_device *dev)
152a190901cSRussell King {
153a190901cSRussell King 	struct pl030_rtc *rtc = amba_get_drvdata(dev);
154a190901cSRussell King 
155a190901cSRussell King 	writel(0, rtc->base + RTC_CR);
156a190901cSRussell King 
157a190901cSRussell King 	free_irq(dev->irq[0], rtc);
158a190901cSRussell King 	iounmap(rtc->base);
159a190901cSRussell King 	amba_release_regions(dev);
160a190901cSRussell King 
161a190901cSRussell King 	return 0;
162a190901cSRussell King }
163a190901cSRussell King 
164a190901cSRussell King static struct amba_id pl030_ids[] = {
165a190901cSRussell King 	{
166a190901cSRussell King 		.id	= 0x00041030,
167a190901cSRussell King 		.mask	= 0x000fffff,
168a190901cSRussell King 	},
169a190901cSRussell King 	{ 0, 0 },
170a190901cSRussell King };
171a190901cSRussell King 
17266bce591SDave Martin MODULE_DEVICE_TABLE(amba, pl030_ids);
17366bce591SDave Martin 
174a190901cSRussell King static struct amba_driver pl030_driver = {
175a190901cSRussell King 	.drv		= {
176a190901cSRussell King 		.name	= "rtc-pl030",
177a190901cSRussell King 	},
178a190901cSRussell King 	.probe		= pl030_probe,
179a190901cSRussell King 	.remove		= pl030_remove,
180a190901cSRussell King 	.id_table	= pl030_ids,
181a190901cSRussell King };
182a190901cSRussell King 
1839e5ed094Sviresh kumar module_amba_driver(pl030_driver);
184a190901cSRussell King 
185a190901cSRussell King MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
186a190901cSRussell King MODULE_DESCRIPTION("ARM AMBA PL030 RTC Driver");
187a190901cSRussell King MODULE_LICENSE("GPL");
188