2 * Real-time clock driver for MPC5121
4 * Copyright 2007, Domen Puncer <domen.puncer@telargo.com>
5 * Copyright 2008, Freescale Semiconductor, Inc. All rights reserved.
6 * Copyright 2011, Dmitry Eremin-Solenikov
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/rtc.h>
17 #include <linux/of_device.h>
18 #include <linux/of_platform.h>
20 #include <linux/slab.h>
22 struct mpc5121_rtc_regs {
23 u8 set_time; /* RTC + 0x00 */
24 u8 hour_set; /* RTC + 0x01 */
25 u8 minute_set; /* RTC + 0x02 */
26 u8 second_set; /* RTC + 0x03 */
28 u8 set_date; /* RTC + 0x04 */
29 u8 month_set; /* RTC + 0x05 */
30 u8 weekday_set; /* RTC + 0x06 */
31 u8 date_set; /* RTC + 0x07 */
33 u8 write_sw; /* RTC + 0x08 */
34 u8 sw_set; /* RTC + 0x09 */
35 u16 year_set; /* RTC + 0x0a */
37 u8 alm_enable; /* RTC + 0x0c */
38 u8 alm_hour_set; /* RTC + 0x0d */
39 u8 alm_min_set; /* RTC + 0x0e */
40 u8 int_enable; /* RTC + 0x0f */
43 u8 hour; /* RTC + 0x11 */
44 u8 minute; /* RTC + 0x12 */
45 u8 second; /* RTC + 0x13 */
47 u8 month; /* RTC + 0x14 */
48 u8 wday_mday; /* RTC + 0x15 */
49 u16 year; /* RTC + 0x16 */
51 u8 int_alm; /* RTC + 0x18 */
52 u8 int_sw; /* RTC + 0x19 */
53 u8 alm_status; /* RTC + 0x1a */
54 u8 sw_minute; /* RTC + 0x1b */
56 u8 bus_error_1; /* RTC + 0x1c */
57 u8 int_day; /* RTC + 0x1d */
58 u8 int_min; /* RTC + 0x1e */
59 u8 int_sec; /* RTC + 0x1f */
63 * intended to be used for hibernation but hibernation
64 * does not work on silicon rev 1.5 so use it for non-volatile
65 * storage of offset between the actual_time register and linux
68 u32 target_time; /* RTC + 0x20 */
71 * readonly time since VBAT_RTC was last connected
73 u32 actual_time; /* RTC + 0x24 */
74 u32 keep_alive; /* RTC + 0x28 */
77 struct mpc5121_rtc_data {
79 unsigned irq_periodic;
80 struct mpc5121_rtc_regs __iomem *regs;
81 struct rtc_device *rtc;
82 struct rtc_wkalrm wkalarm;
86 * Update second/minute/hour registers.
88 * This is just so alarm will work.
90 static void mpc5121_rtc_update_smh(struct mpc5121_rtc_regs __iomem *regs,
93 out_8(®s->second_set, tm->tm_sec);
94 out_8(®s->minute_set, tm->tm_min);
95 out_8(®s->hour_set, tm->tm_hour);
97 /* set time sequence */
98 out_8(®s->set_time, 0x1);
99 out_8(®s->set_time, 0x3);
100 out_8(®s->set_time, 0x1);
101 out_8(®s->set_time, 0x0);
104 static int mpc5121_rtc_read_time(struct device *dev, struct rtc_time *tm)
106 struct mpc5121_rtc_data *rtc = dev_get_drvdata(dev);
107 struct mpc5121_rtc_regs __iomem *regs = rtc->regs;
111 * linux time is actual_time plus the offset saved in target_time
113 now = in_be32(®s->actual_time) + in_be32(®s->target_time);
115 rtc_time_to_tm(now, tm);
118 * update second minute hour registers
119 * so alarms will work
121 mpc5121_rtc_update_smh(regs, tm);
123 return rtc_valid_tm(tm);
126 static int mpc5121_rtc_set_time(struct device *dev, struct rtc_time *tm)
128 struct mpc5121_rtc_data *rtc = dev_get_drvdata(dev);
129 struct mpc5121_rtc_regs __iomem *regs = rtc->regs;
134 * The actual_time register is read only so we write the offset
135 * between it and linux time to the target_time register.
137 ret = rtc_tm_to_time(tm, &now);
139 out_be32(®s->target_time, now - in_be32(®s->actual_time));
142 * update second minute hour registers
143 * so alarms will work
145 mpc5121_rtc_update_smh(regs, tm);
150 static int mpc5200_rtc_read_time(struct device *dev, struct rtc_time *tm)
152 struct mpc5121_rtc_data *rtc = dev_get_drvdata(dev);
153 struct mpc5121_rtc_regs __iomem *regs = rtc->regs;
156 tm->tm_sec = in_8(®s->second);
157 tm->tm_min = in_8(®s->minute);
159 /* 12 hour format? */
160 if (in_8(®s->hour) & 0x20)
161 tm->tm_hour = (in_8(®s->hour) >> 1) +
162 (in_8(®s->hour) & 1 ? 12 : 0);
164 tm->tm_hour = in_8(®s->hour);
166 tmp = in_8(®s->wday_mday);
167 tm->tm_mday = tmp & 0x1f;
168 tm->tm_mon = in_8(®s->month) - 1;
169 tm->tm_year = in_be16(®s->year) - 1900;
170 tm->tm_wday = (tmp >> 5) % 7;
171 tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year);
177 static int mpc5200_rtc_set_time(struct device *dev, struct rtc_time *tm)
179 struct mpc5121_rtc_data *rtc = dev_get_drvdata(dev);
180 struct mpc5121_rtc_regs __iomem *regs = rtc->regs;
182 mpc5121_rtc_update_smh(regs, tm);
185 out_8(®s->month_set, tm->tm_mon + 1);
186 out_8(®s->weekday_set, tm->tm_wday ? tm->tm_wday : 7);
187 out_8(®s->date_set, tm->tm_mday);
188 out_be16(®s->year_set, tm->tm_year + 1900);
190 /* set date sequence */
191 out_8(®s->set_date, 0x1);
192 out_8(®s->set_date, 0x3);
193 out_8(®s->set_date, 0x1);
194 out_8(®s->set_date, 0x0);
199 static int mpc5121_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
201 struct mpc5121_rtc_data *rtc = dev_get_drvdata(dev);
202 struct mpc5121_rtc_regs __iomem *regs = rtc->regs;
204 *alarm = rtc->wkalarm;
206 alarm->pending = in_8(®s->alm_status);
211 static int mpc5121_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
213 struct mpc5121_rtc_data *rtc = dev_get_drvdata(dev);
214 struct mpc5121_rtc_regs __iomem *regs = rtc->regs;
217 * the alarm has no seconds so deal with it
219 if (alarm->time.tm_sec) {
220 alarm->time.tm_sec = 0;
221 alarm->time.tm_min++;
222 if (alarm->time.tm_min >= 60) {
223 alarm->time.tm_min = 0;
224 alarm->time.tm_hour++;
225 if (alarm->time.tm_hour >= 24)
226 alarm->time.tm_hour = 0;
230 alarm->time.tm_mday = -1;
231 alarm->time.tm_mon = -1;
232 alarm->time.tm_year = -1;
234 out_8(®s->alm_min_set, alarm->time.tm_min);
235 out_8(®s->alm_hour_set, alarm->time.tm_hour);
237 out_8(®s->alm_enable, alarm->enabled);
239 rtc->wkalarm = *alarm;
243 static irqreturn_t mpc5121_rtc_handler(int irq, void *dev)
245 struct mpc5121_rtc_data *rtc = dev_get_drvdata((struct device *)dev);
246 struct mpc5121_rtc_regs __iomem *regs = rtc->regs;
248 if (in_8(®s->int_alm)) {
249 /* acknowledge and clear status */
250 out_8(®s->int_alm, 1);
251 out_8(®s->alm_status, 1);
253 rtc_update_irq(rtc->rtc, 1, RTC_IRQF | RTC_AF);
260 static irqreturn_t mpc5121_rtc_handler_upd(int irq, void *dev)
262 struct mpc5121_rtc_data *rtc = dev_get_drvdata((struct device *)dev);
263 struct mpc5121_rtc_regs __iomem *regs = rtc->regs;
265 if (in_8(®s->int_sec) && (in_8(®s->int_enable) & 0x1)) {
267 out_8(®s->int_sec, 1);
269 rtc_update_irq(rtc->rtc, 1, RTC_IRQF | RTC_UF);
276 static int mpc5121_rtc_alarm_irq_enable(struct device *dev,
277 unsigned int enabled)
279 struct mpc5121_rtc_data *rtc = dev_get_drvdata(dev);
280 struct mpc5121_rtc_regs __iomem *regs = rtc->regs;
288 out_8(®s->alm_enable, val);
289 rtc->wkalarm.enabled = val;
294 static const struct rtc_class_ops mpc5121_rtc_ops = {
295 .read_time = mpc5121_rtc_read_time,
296 .set_time = mpc5121_rtc_set_time,
297 .read_alarm = mpc5121_rtc_read_alarm,
298 .set_alarm = mpc5121_rtc_set_alarm,
299 .alarm_irq_enable = mpc5121_rtc_alarm_irq_enable,
302 static const struct rtc_class_ops mpc5200_rtc_ops = {
303 .read_time = mpc5200_rtc_read_time,
304 .set_time = mpc5200_rtc_set_time,
305 .read_alarm = mpc5121_rtc_read_alarm,
306 .set_alarm = mpc5121_rtc_set_alarm,
307 .alarm_irq_enable = mpc5121_rtc_alarm_irq_enable,
310 static int mpc5121_rtc_probe(struct platform_device *op)
312 struct mpc5121_rtc_data *rtc;
315 rtc = devm_kzalloc(&op->dev, sizeof(*rtc), GFP_KERNEL);
319 rtc->regs = of_iomap(op->dev.of_node, 0);
321 dev_err(&op->dev, "%s: couldn't map io space\n", __func__);
325 device_init_wakeup(&op->dev, 1);
327 platform_set_drvdata(op, rtc);
329 rtc->irq = irq_of_parse_and_map(op->dev.of_node, 1);
330 err = request_irq(rtc->irq, mpc5121_rtc_handler, 0,
331 "mpc5121-rtc", &op->dev);
333 dev_err(&op->dev, "%s: could not request irq: %i\n",
338 rtc->irq_periodic = irq_of_parse_and_map(op->dev.of_node, 0);
339 err = request_irq(rtc->irq_periodic, mpc5121_rtc_handler_upd,
340 0, "mpc5121-rtc_upd", &op->dev);
342 dev_err(&op->dev, "%s: could not request irq: %i\n",
343 __func__, rtc->irq_periodic);
347 if (of_device_is_compatible(op->dev.of_node, "fsl,mpc5121-rtc")) {
349 ka = in_be32(&rtc->regs->keep_alive);
352 "mpc5121-rtc: Battery or oscillator failure!\n");
353 out_be32(&rtc->regs->keep_alive, ka);
356 rtc->rtc = devm_rtc_device_register(&op->dev, "mpc5121-rtc",
357 &mpc5121_rtc_ops, THIS_MODULE);
359 rtc->rtc = devm_rtc_device_register(&op->dev, "mpc5200-rtc",
360 &mpc5200_rtc_ops, THIS_MODULE);
363 if (IS_ERR(rtc->rtc)) {
364 err = PTR_ERR(rtc->rtc);
367 rtc->rtc->uie_unsupported = 1;
372 free_irq(rtc->irq_periodic, &op->dev);
374 irq_dispose_mapping(rtc->irq_periodic);
375 free_irq(rtc->irq, &op->dev);
377 irq_dispose_mapping(rtc->irq);
383 static int mpc5121_rtc_remove(struct platform_device *op)
385 struct mpc5121_rtc_data *rtc = platform_get_drvdata(op);
386 struct mpc5121_rtc_regs __iomem *regs = rtc->regs;
388 /* disable interrupt, so there are no nasty surprises */
389 out_8(®s->alm_enable, 0);
390 out_8(®s->int_enable, in_8(®s->int_enable) & ~0x1);
393 free_irq(rtc->irq, &op->dev);
394 free_irq(rtc->irq_periodic, &op->dev);
395 irq_dispose_mapping(rtc->irq);
396 irq_dispose_mapping(rtc->irq_periodic);
402 static struct of_device_id mpc5121_rtc_match[] = {
403 { .compatible = "fsl,mpc5121-rtc", },
404 { .compatible = "fsl,mpc5200-rtc", },
409 static struct platform_driver mpc5121_rtc_driver = {
411 .name = "mpc5121-rtc",
412 .owner = THIS_MODULE,
413 .of_match_table = of_match_ptr(mpc5121_rtc_match),
415 .probe = mpc5121_rtc_probe,
416 .remove = mpc5121_rtc_remove,
419 module_platform_driver(mpc5121_rtc_driver);
421 MODULE_LICENSE("GPL");
422 MODULE_AUTHOR("John Rigby <jcrigby@gmail.com>");