blob: ebc1649d45d61ef068792d479bdc7deb438ad5c8 [file] [log] [blame]
Saeed Bisharadefb4512009-01-06 14:42:24 -08001/*
2 * Driver for the RTC in Marvell SoCs.
3 *
4 * This file is licensed under the terms of the GNU General Public
5 * License version 2. This program is licensed "as is" without any
6 * warranty of any kind, whether express or implied.
7 */
8
9#include <linux/init.h>
10#include <linux/kernel.h>
11#include <linux/rtc.h>
12#include <linux/bcd.h>
13#include <linux/io.h>
14#include <linux/platform_device.h>
Jason Cooperea983ede2012-03-06 23:53:57 +000015#include <linux/of.h>
Nicolas Pitre9d1d4f92009-03-23 20:42:29 -040016#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/gfp.h>
Paul Gortmaker21138522011-05-27 09:57:25 -040018#include <linux/module.h>
Saeed Bisharadefb4512009-01-06 14:42:24 -080019
20
21#define RTC_TIME_REG_OFFS 0
22#define RTC_SECONDS_OFFS 0
23#define RTC_MINUTES_OFFS 8
24#define RTC_HOURS_OFFS 16
25#define RTC_WDAY_OFFS 24
26#define RTC_HOURS_12H_MODE (1 << 22) /* 12 hours mode */
27
28#define RTC_DATE_REG_OFFS 4
29#define RTC_MDAY_OFFS 0
30#define RTC_MONTH_OFFS 8
31#define RTC_YEAR_OFFS 16
32
Saeed Bisharaaeedaca2009-12-15 16:46:08 -080033#define RTC_ALARM_TIME_REG_OFFS 8
34#define RTC_ALARM_DATE_REG_OFFS 0xc
35#define RTC_ALARM_VALID (1 << 7)
36
37#define RTC_ALARM_INTERRUPT_MASK_REG_OFFS 0x10
38#define RTC_ALARM_INTERRUPT_CASUE_REG_OFFS 0x14
Saeed Bisharadefb4512009-01-06 14:42:24 -080039
40struct rtc_plat_data {
41 struct rtc_device *rtc;
42 void __iomem *ioaddr;
Saeed Bisharaaeedaca2009-12-15 16:46:08 -080043 int irq;
Saeed Bisharadefb4512009-01-06 14:42:24 -080044};
45
46static int mv_rtc_set_time(struct device *dev, struct rtc_time *tm)
47{
48 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
49 void __iomem *ioaddr = pdata->ioaddr;
50 u32 rtc_reg;
51
52 rtc_reg = (bin2bcd(tm->tm_sec) << RTC_SECONDS_OFFS) |
53 (bin2bcd(tm->tm_min) << RTC_MINUTES_OFFS) |
54 (bin2bcd(tm->tm_hour) << RTC_HOURS_OFFS) |
55 (bin2bcd(tm->tm_wday) << RTC_WDAY_OFFS);
56 writel(rtc_reg, ioaddr + RTC_TIME_REG_OFFS);
57
58 rtc_reg = (bin2bcd(tm->tm_mday) << RTC_MDAY_OFFS) |
59 (bin2bcd(tm->tm_mon + 1) << RTC_MONTH_OFFS) |
60 (bin2bcd(tm->tm_year % 100) << RTC_YEAR_OFFS);
61 writel(rtc_reg, ioaddr + RTC_DATE_REG_OFFS);
62
63 return 0;
64}
65
66static int mv_rtc_read_time(struct device *dev, struct rtc_time *tm)
67{
68 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
69 void __iomem *ioaddr = pdata->ioaddr;
70 u32 rtc_time, rtc_date;
71 unsigned int year, month, day, hour, minute, second, wday;
72
73 rtc_time = readl(ioaddr + RTC_TIME_REG_OFFS);
74 rtc_date = readl(ioaddr + RTC_DATE_REG_OFFS);
75
76 second = rtc_time & 0x7f;
77 minute = (rtc_time >> RTC_MINUTES_OFFS) & 0x7f;
78 hour = (rtc_time >> RTC_HOURS_OFFS) & 0x3f; /* assume 24 hours mode */
79 wday = (rtc_time >> RTC_WDAY_OFFS) & 0x7;
80
81 day = rtc_date & 0x3f;
82 month = (rtc_date >> RTC_MONTH_OFFS) & 0x3f;
83 year = (rtc_date >> RTC_YEAR_OFFS) & 0xff;
84
85 tm->tm_sec = bcd2bin(second);
86 tm->tm_min = bcd2bin(minute);
87 tm->tm_hour = bcd2bin(hour);
88 tm->tm_mday = bcd2bin(day);
89 tm->tm_wday = bcd2bin(wday);
90 tm->tm_mon = bcd2bin(month) - 1;
91 /* hw counts from year 2000, but tm_year is relative to 1900 */
92 tm->tm_year = bcd2bin(year) + 100;
93
94 return rtc_valid_tm(tm);
95}
96
Saeed Bisharaaeedaca2009-12-15 16:46:08 -080097static int mv_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
98{
99 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
100 void __iomem *ioaddr = pdata->ioaddr;
101 u32 rtc_time, rtc_date;
102 unsigned int year, month, day, hour, minute, second, wday;
103
104 rtc_time = readl(ioaddr + RTC_ALARM_TIME_REG_OFFS);
105 rtc_date = readl(ioaddr + RTC_ALARM_DATE_REG_OFFS);
106
107 second = rtc_time & 0x7f;
108 minute = (rtc_time >> RTC_MINUTES_OFFS) & 0x7f;
109 hour = (rtc_time >> RTC_HOURS_OFFS) & 0x3f; /* assume 24 hours mode */
110 wday = (rtc_time >> RTC_WDAY_OFFS) & 0x7;
111
112 day = rtc_date & 0x3f;
113 month = (rtc_date >> RTC_MONTH_OFFS) & 0x3f;
114 year = (rtc_date >> RTC_YEAR_OFFS) & 0xff;
115
116 alm->time.tm_sec = bcd2bin(second);
117 alm->time.tm_min = bcd2bin(minute);
118 alm->time.tm_hour = bcd2bin(hour);
119 alm->time.tm_mday = bcd2bin(day);
120 alm->time.tm_wday = bcd2bin(wday);
121 alm->time.tm_mon = bcd2bin(month) - 1;
122 /* hw counts from year 2000, but tm_year is relative to 1900 */
123 alm->time.tm_year = bcd2bin(year) + 100;
124
125 if (rtc_valid_tm(&alm->time) < 0) {
126 dev_err(dev, "retrieved alarm date/time is not valid.\n");
127 rtc_time_to_tm(0, &alm->time);
128 }
129
130 alm->enabled = !!readl(ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
131 return 0;
132}
133
134static int mv_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
135{
136 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
137 void __iomem *ioaddr = pdata->ioaddr;
138 u32 rtc_reg = 0;
139
140 if (alm->time.tm_sec >= 0)
141 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_sec))
142 << RTC_SECONDS_OFFS;
143 if (alm->time.tm_min >= 0)
144 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_min))
145 << RTC_MINUTES_OFFS;
146 if (alm->time.tm_hour >= 0)
147 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_hour))
148 << RTC_HOURS_OFFS;
149
150 writel(rtc_reg, ioaddr + RTC_ALARM_TIME_REG_OFFS);
151
152 if (alm->time.tm_mday >= 0)
153 rtc_reg = (RTC_ALARM_VALID | bin2bcd(alm->time.tm_mday))
154 << RTC_MDAY_OFFS;
155 else
156 rtc_reg = 0;
157
158 if (alm->time.tm_mon >= 0)
159 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_mon + 1))
160 << RTC_MONTH_OFFS;
161
162 if (alm->time.tm_year >= 0)
163 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_year % 100))
164 << RTC_YEAR_OFFS;
165
166 writel(rtc_reg, ioaddr + RTC_ALARM_DATE_REG_OFFS);
167 writel(0, ioaddr + RTC_ALARM_INTERRUPT_CASUE_REG_OFFS);
168 writel(alm->enabled ? 1 : 0,
169 ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
170
171 return 0;
172}
173
John Stultz16380c12011-02-02 17:02:41 -0800174static int mv_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800175{
176 struct platform_device *pdev = to_platform_device(dev);
177 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
178 void __iomem *ioaddr = pdata->ioaddr;
179
180 if (pdata->irq < 0)
John Stultz16380c12011-02-02 17:02:41 -0800181 return -EINVAL; /* fall back into rtc-dev's emulation */
182
183 if (enabled)
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800184 writel(1, ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
John Stultz16380c12011-02-02 17:02:41 -0800185 else
186 writel(0, ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800187 return 0;
188}
189
190static irqreturn_t mv_rtc_interrupt(int irq, void *data)
191{
192 struct rtc_plat_data *pdata = data;
193 void __iomem *ioaddr = pdata->ioaddr;
194
195 /* alarm irq? */
196 if (!readl(ioaddr + RTC_ALARM_INTERRUPT_CASUE_REG_OFFS))
197 return IRQ_NONE;
198
199 /* clear interrupt */
200 writel(0, ioaddr + RTC_ALARM_INTERRUPT_CASUE_REG_OFFS);
201 rtc_update_irq(pdata->rtc, 1, RTC_IRQF | RTC_AF);
202 return IRQ_HANDLED;
203}
204
Saeed Bisharadefb4512009-01-06 14:42:24 -0800205static const struct rtc_class_ops mv_rtc_ops = {
206 .read_time = mv_rtc_read_time,
207 .set_time = mv_rtc_set_time,
208};
209
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800210static const struct rtc_class_ops mv_rtc_alarm_ops = {
211 .read_time = mv_rtc_read_time,
212 .set_time = mv_rtc_set_time,
213 .read_alarm = mv_rtc_read_alarm,
214 .set_alarm = mv_rtc_set_alarm,
John Stultz16380c12011-02-02 17:02:41 -0800215 .alarm_irq_enable = mv_rtc_alarm_irq_enable,
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800216};
217
218static int __devinit mv_rtc_probe(struct platform_device *pdev)
Saeed Bisharadefb4512009-01-06 14:42:24 -0800219{
220 struct resource *res;
221 struct rtc_plat_data *pdata;
222 resource_size_t size;
223 u32 rtc_time;
224
225 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
226 if (!res)
227 return -ENODEV;
228
229 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
230 if (!pdata)
231 return -ENOMEM;
232
233 size = resource_size(res);
234 if (!devm_request_mem_region(&pdev->dev, res->start, size,
235 pdev->name))
236 return -EBUSY;
237
238 pdata->ioaddr = devm_ioremap(&pdev->dev, res->start, size);
239 if (!pdata->ioaddr)
240 return -ENOMEM;
241
242 /* make sure the 24 hours mode is enabled */
243 rtc_time = readl(pdata->ioaddr + RTC_TIME_REG_OFFS);
244 if (rtc_time & RTC_HOURS_12H_MODE) {
245 dev_err(&pdev->dev, "24 Hours mode not supported.\n");
246 return -EINVAL;
247 }
248
Nicolas Pitre9d1d4f92009-03-23 20:42:29 -0400249 /* make sure it is actually functional */
250 if (rtc_time == 0x01000000) {
251 ssleep(1);
252 rtc_time = readl(pdata->ioaddr + RTC_TIME_REG_OFFS);
253 if (rtc_time == 0x01000000) {
254 dev_err(&pdev->dev, "internal RTC not ticking\n");
255 return -ENODEV;
256 }
257 }
258
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800259 pdata->irq = platform_get_irq(pdev, 0);
260
Saeed Bisharadefb4512009-01-06 14:42:24 -0800261 platform_set_drvdata(pdev, pdata);
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800262
263 if (pdata->irq >= 0) {
264 device_init_wakeup(&pdev->dev, 1);
265 pdata->rtc = rtc_device_register(pdev->name, &pdev->dev,
266 &mv_rtc_alarm_ops,
267 THIS_MODULE);
268 } else
269 pdata->rtc = rtc_device_register(pdev->name, &pdev->dev,
270 &mv_rtc_ops, THIS_MODULE);
Saeed Bisharadefb4512009-01-06 14:42:24 -0800271 if (IS_ERR(pdata->rtc))
272 return PTR_ERR(pdata->rtc);
273
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800274 if (pdata->irq >= 0) {
275 writel(0, pdata->ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
276 if (devm_request_irq(&pdev->dev, pdata->irq, mv_rtc_interrupt,
Yong Zhang2f6e5f92012-03-23 15:02:34 -0700277 IRQF_SHARED,
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800278 pdev->name, pdata) < 0) {
279 dev_warn(&pdev->dev, "interrupt not available.\n");
280 pdata->irq = -1;
281 }
282 }
283
Saeed Bisharadefb4512009-01-06 14:42:24 -0800284 return 0;
285}
286
287static int __exit mv_rtc_remove(struct platform_device *pdev)
288{
289 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
290
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800291 if (pdata->irq >= 0)
292 device_init_wakeup(&pdev->dev, 0);
293
Saeed Bisharadefb4512009-01-06 14:42:24 -0800294 rtc_device_unregister(pdata->rtc);
295 return 0;
296}
297
Jason Cooperea983ede2012-03-06 23:53:57 +0000298#ifdef CONFIG_OF
299static struct of_device_id rtc_mv_of_match_table[] = {
Andrew Lunn77843502012-07-18 19:22:54 +0200300 { .compatible = "marvell,orion-rtc", },
Jason Cooperea983ede2012-03-06 23:53:57 +0000301 {}
302};
303#endif
304
Saeed Bisharadefb4512009-01-06 14:42:24 -0800305static struct platform_driver mv_rtc_driver = {
306 .remove = __exit_p(mv_rtc_remove),
307 .driver = {
308 .name = "rtc-mv",
309 .owner = THIS_MODULE,
Jason Cooperea983ede2012-03-06 23:53:57 +0000310 .of_match_table = of_match_ptr(rtc_mv_of_match_table),
Saeed Bisharadefb4512009-01-06 14:42:24 -0800311 },
312};
313
314static __init int mv_init(void)
315{
316 return platform_driver_probe(&mv_rtc_driver, mv_rtc_probe);
317}
318
319static __exit void mv_exit(void)
320{
321 platform_driver_unregister(&mv_rtc_driver);
322}
323
324module_init(mv_init);
325module_exit(mv_exit);
326
327MODULE_AUTHOR("Saeed Bishara <saeed@marvell.com>");
328MODULE_DESCRIPTION("Marvell RTC driver");
329MODULE_LICENSE("GPL");
330MODULE_ALIAS("platform:rtc-mv");