2 * Driver for the RTC in Marvell SoCs.
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.
9 #include <linux/init.h>
10 #include <linux/kernel.h>
11 #include <linux/rtc.h>
12 #include <linux/bcd.h>
14 #include <linux/platform_device.h>
15 #include <linux/delay.h>
16 #include <linux/gfp.h>
17 #include <linux/module.h>
20 #define RTC_TIME_REG_OFFS 0
21 #define RTC_SECONDS_OFFS 0
22 #define RTC_MINUTES_OFFS 8
23 #define RTC_HOURS_OFFS 16
24 #define RTC_WDAY_OFFS 24
25 #define RTC_HOURS_12H_MODE (1 << 22) /* 12 hours mode */
27 #define RTC_DATE_REG_OFFS 4
28 #define RTC_MDAY_OFFS 0
29 #define RTC_MONTH_OFFS 8
30 #define RTC_YEAR_OFFS 16
32 #define RTC_ALARM_TIME_REG_OFFS 8
33 #define RTC_ALARM_DATE_REG_OFFS 0xc
34 #define RTC_ALARM_VALID (1 << 7)
36 #define RTC_ALARM_INTERRUPT_MASK_REG_OFFS 0x10
37 #define RTC_ALARM_INTERRUPT_CASUE_REG_OFFS 0x14
39 struct rtc_plat_data
{
40 struct rtc_device
*rtc
;
45 static int mv_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
47 struct rtc_plat_data
*pdata
= dev_get_drvdata(dev
);
48 void __iomem
*ioaddr
= pdata
->ioaddr
;
51 rtc_reg
= (bin2bcd(tm
->tm_sec
) << RTC_SECONDS_OFFS
) |
52 (bin2bcd(tm
->tm_min
) << RTC_MINUTES_OFFS
) |
53 (bin2bcd(tm
->tm_hour
) << RTC_HOURS_OFFS
) |
54 (bin2bcd(tm
->tm_wday
) << RTC_WDAY_OFFS
);
55 writel(rtc_reg
, ioaddr
+ RTC_TIME_REG_OFFS
);
57 rtc_reg
= (bin2bcd(tm
->tm_mday
) << RTC_MDAY_OFFS
) |
58 (bin2bcd(tm
->tm_mon
+ 1) << RTC_MONTH_OFFS
) |
59 (bin2bcd(tm
->tm_year
% 100) << RTC_YEAR_OFFS
);
60 writel(rtc_reg
, ioaddr
+ RTC_DATE_REG_OFFS
);
65 static int mv_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
67 struct rtc_plat_data
*pdata
= dev_get_drvdata(dev
);
68 void __iomem
*ioaddr
= pdata
->ioaddr
;
69 u32 rtc_time
, rtc_date
;
70 unsigned int year
, month
, day
, hour
, minute
, second
, wday
;
72 rtc_time
= readl(ioaddr
+ RTC_TIME_REG_OFFS
);
73 rtc_date
= readl(ioaddr
+ RTC_DATE_REG_OFFS
);
75 second
= rtc_time
& 0x7f;
76 minute
= (rtc_time
>> RTC_MINUTES_OFFS
) & 0x7f;
77 hour
= (rtc_time
>> RTC_HOURS_OFFS
) & 0x3f; /* assume 24 hours mode */
78 wday
= (rtc_time
>> RTC_WDAY_OFFS
) & 0x7;
80 day
= rtc_date
& 0x3f;
81 month
= (rtc_date
>> RTC_MONTH_OFFS
) & 0x3f;
82 year
= (rtc_date
>> RTC_YEAR_OFFS
) & 0xff;
84 tm
->tm_sec
= bcd2bin(second
);
85 tm
->tm_min
= bcd2bin(minute
);
86 tm
->tm_hour
= bcd2bin(hour
);
87 tm
->tm_mday
= bcd2bin(day
);
88 tm
->tm_wday
= bcd2bin(wday
);
89 tm
->tm_mon
= bcd2bin(month
) - 1;
90 /* hw counts from year 2000, but tm_year is relative to 1900 */
91 tm
->tm_year
= bcd2bin(year
) + 100;
93 return rtc_valid_tm(tm
);
96 static int mv_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alm
)
98 struct rtc_plat_data
*pdata
= dev_get_drvdata(dev
);
99 void __iomem
*ioaddr
= pdata
->ioaddr
;
100 u32 rtc_time
, rtc_date
;
101 unsigned int year
, month
, day
, hour
, minute
, second
, wday
;
103 rtc_time
= readl(ioaddr
+ RTC_ALARM_TIME_REG_OFFS
);
104 rtc_date
= readl(ioaddr
+ RTC_ALARM_DATE_REG_OFFS
);
106 second
= rtc_time
& 0x7f;
107 minute
= (rtc_time
>> RTC_MINUTES_OFFS
) & 0x7f;
108 hour
= (rtc_time
>> RTC_HOURS_OFFS
) & 0x3f; /* assume 24 hours mode */
109 wday
= (rtc_time
>> RTC_WDAY_OFFS
) & 0x7;
111 day
= rtc_date
& 0x3f;
112 month
= (rtc_date
>> RTC_MONTH_OFFS
) & 0x3f;
113 year
= (rtc_date
>> RTC_YEAR_OFFS
) & 0xff;
115 alm
->time
.tm_sec
= bcd2bin(second
);
116 alm
->time
.tm_min
= bcd2bin(minute
);
117 alm
->time
.tm_hour
= bcd2bin(hour
);
118 alm
->time
.tm_mday
= bcd2bin(day
);
119 alm
->time
.tm_wday
= bcd2bin(wday
);
120 alm
->time
.tm_mon
= bcd2bin(month
) - 1;
121 /* hw counts from year 2000, but tm_year is relative to 1900 */
122 alm
->time
.tm_year
= bcd2bin(year
) + 100;
124 if (rtc_valid_tm(&alm
->time
) < 0) {
125 dev_err(dev
, "retrieved alarm date/time is not valid.\n");
126 rtc_time_to_tm(0, &alm
->time
);
129 alm
->enabled
= !!readl(ioaddr
+ RTC_ALARM_INTERRUPT_MASK_REG_OFFS
);
133 static int mv_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alm
)
135 struct rtc_plat_data
*pdata
= dev_get_drvdata(dev
);
136 void __iomem
*ioaddr
= pdata
->ioaddr
;
139 if (alm
->time
.tm_sec
>= 0)
140 rtc_reg
|= (RTC_ALARM_VALID
| bin2bcd(alm
->time
.tm_sec
))
142 if (alm
->time
.tm_min
>= 0)
143 rtc_reg
|= (RTC_ALARM_VALID
| bin2bcd(alm
->time
.tm_min
))
145 if (alm
->time
.tm_hour
>= 0)
146 rtc_reg
|= (RTC_ALARM_VALID
| bin2bcd(alm
->time
.tm_hour
))
149 writel(rtc_reg
, ioaddr
+ RTC_ALARM_TIME_REG_OFFS
);
151 if (alm
->time
.tm_mday
>= 0)
152 rtc_reg
= (RTC_ALARM_VALID
| bin2bcd(alm
->time
.tm_mday
))
157 if (alm
->time
.tm_mon
>= 0)
158 rtc_reg
|= (RTC_ALARM_VALID
| bin2bcd(alm
->time
.tm_mon
+ 1))
161 if (alm
->time
.tm_year
>= 0)
162 rtc_reg
|= (RTC_ALARM_VALID
| bin2bcd(alm
->time
.tm_year
% 100))
165 writel(rtc_reg
, ioaddr
+ RTC_ALARM_DATE_REG_OFFS
);
166 writel(0, ioaddr
+ RTC_ALARM_INTERRUPT_CASUE_REG_OFFS
);
167 writel(alm
->enabled
? 1 : 0,
168 ioaddr
+ RTC_ALARM_INTERRUPT_MASK_REG_OFFS
);
173 static int mv_rtc_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
175 struct platform_device
*pdev
= to_platform_device(dev
);
176 struct rtc_plat_data
*pdata
= platform_get_drvdata(pdev
);
177 void __iomem
*ioaddr
= pdata
->ioaddr
;
180 return -EINVAL
; /* fall back into rtc-dev's emulation */
183 writel(1, ioaddr
+ RTC_ALARM_INTERRUPT_MASK_REG_OFFS
);
185 writel(0, ioaddr
+ RTC_ALARM_INTERRUPT_MASK_REG_OFFS
);
189 static irqreturn_t
mv_rtc_interrupt(int irq
, void *data
)
191 struct rtc_plat_data
*pdata
= data
;
192 void __iomem
*ioaddr
= pdata
->ioaddr
;
195 if (!readl(ioaddr
+ RTC_ALARM_INTERRUPT_CASUE_REG_OFFS
))
198 /* clear interrupt */
199 writel(0, ioaddr
+ RTC_ALARM_INTERRUPT_CASUE_REG_OFFS
);
200 rtc_update_irq(pdata
->rtc
, 1, RTC_IRQF
| RTC_AF
);
204 static const struct rtc_class_ops mv_rtc_ops
= {
205 .read_time
= mv_rtc_read_time
,
206 .set_time
= mv_rtc_set_time
,
209 static const struct rtc_class_ops mv_rtc_alarm_ops
= {
210 .read_time
= mv_rtc_read_time
,
211 .set_time
= mv_rtc_set_time
,
212 .read_alarm
= mv_rtc_read_alarm
,
213 .set_alarm
= mv_rtc_set_alarm
,
214 .alarm_irq_enable
= mv_rtc_alarm_irq_enable
,
217 static int __devinit
mv_rtc_probe(struct platform_device
*pdev
)
219 struct resource
*res
;
220 struct rtc_plat_data
*pdata
;
221 resource_size_t size
;
224 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
228 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
232 size
= resource_size(res
);
233 if (!devm_request_mem_region(&pdev
->dev
, res
->start
, size
,
237 pdata
->ioaddr
= devm_ioremap(&pdev
->dev
, res
->start
, size
);
241 /* make sure the 24 hours mode is enabled */
242 rtc_time
= readl(pdata
->ioaddr
+ RTC_TIME_REG_OFFS
);
243 if (rtc_time
& RTC_HOURS_12H_MODE
) {
244 dev_err(&pdev
->dev
, "24 Hours mode not supported.\n");
248 /* make sure it is actually functional */
249 if (rtc_time
== 0x01000000) {
251 rtc_time
= readl(pdata
->ioaddr
+ RTC_TIME_REG_OFFS
);
252 if (rtc_time
== 0x01000000) {
253 dev_err(&pdev
->dev
, "internal RTC not ticking\n");
258 pdata
->irq
= platform_get_irq(pdev
, 0);
260 platform_set_drvdata(pdev
, pdata
);
262 if (pdata
->irq
>= 0) {
263 device_init_wakeup(&pdev
->dev
, 1);
264 pdata
->rtc
= rtc_device_register(pdev
->name
, &pdev
->dev
,
268 pdata
->rtc
= rtc_device_register(pdev
->name
, &pdev
->dev
,
269 &mv_rtc_ops
, THIS_MODULE
);
270 if (IS_ERR(pdata
->rtc
))
271 return PTR_ERR(pdata
->rtc
);
273 if (pdata
->irq
>= 0) {
274 writel(0, pdata
->ioaddr
+ RTC_ALARM_INTERRUPT_MASK_REG_OFFS
);
275 if (devm_request_irq(&pdev
->dev
, pdata
->irq
, mv_rtc_interrupt
,
276 IRQF_DISABLED
| IRQF_SHARED
,
277 pdev
->name
, pdata
) < 0) {
278 dev_warn(&pdev
->dev
, "interrupt not available.\n");
286 static int __exit
mv_rtc_remove(struct platform_device
*pdev
)
288 struct rtc_plat_data
*pdata
= platform_get_drvdata(pdev
);
291 device_init_wakeup(&pdev
->dev
, 0);
293 rtc_device_unregister(pdata
->rtc
);
297 static struct platform_driver mv_rtc_driver
= {
298 .remove
= __exit_p(mv_rtc_remove
),
301 .owner
= THIS_MODULE
,
305 static __init
int mv_init(void)
307 return platform_driver_probe(&mv_rtc_driver
, mv_rtc_probe
);
310 static __exit
void mv_exit(void)
312 platform_driver_unregister(&mv_rtc_driver
);
315 module_init(mv_init
);
316 module_exit(mv_exit
);
318 MODULE_AUTHOR("Saeed Bishara <saeed@marvell.com>");
319 MODULE_DESCRIPTION("Marvell RTC driver");
320 MODULE_LICENSE("GPL");
321 MODULE_ALIAS("platform:rtc-mv");