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