aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/rtc
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/rtc')
-rw-r--r--drivers/rtc/Kconfig10
-rw-r--r--drivers/rtc/Makefile1
-rw-r--r--drivers/rtc/rtc-mpc5121.c387
3 files changed, 398 insertions, 0 deletions
diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
index 8167e9e6827a..2bb8a8b7ffaf 100644
--- a/drivers/rtc/Kconfig
+++ b/drivers/rtc/Kconfig
@@ -868,4 +868,14 @@ config RTC_DRV_MC13783
868 help 868 help
869 This enables support for the Freescale MC13783 PMIC RTC 869 This enables support for the Freescale MC13783 PMIC RTC
870 870
871config RTC_DRV_MPC5121
872 tristate "Freescale MPC5121 built-in RTC"
873 depends on PPC_MPC512x && RTC_CLASS
874 help
875 If you say yes here you will get support for the
876 built-in RTC MPC5121.
877
878 This driver can also be built as a module. If so, the module
879 will be called rtc-mpc5121.
880
871endif # RTC_CLASS 881endif # RTC_CLASS
diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
index e5160fddc446..b7148afb8f55 100644
--- a/drivers/rtc/Makefile
+++ b/drivers/rtc/Makefile
@@ -55,6 +55,7 @@ obj-$(CONFIG_RTC_DRV_MAX6900) += rtc-max6900.o
55obj-$(CONFIG_RTC_DRV_MAX6902) += rtc-max6902.o 55obj-$(CONFIG_RTC_DRV_MAX6902) += rtc-max6902.o
56obj-$(CONFIG_RTC_DRV_MC13783) += rtc-mc13783.o 56obj-$(CONFIG_RTC_DRV_MC13783) += rtc-mc13783.o
57obj-$(CONFIG_RTC_DRV_MSM6242) += rtc-msm6242.o 57obj-$(CONFIG_RTC_DRV_MSM6242) += rtc-msm6242.o
58obj-$(CONFIG_RTC_DRV_MPC5121) += rtc-mpc5121.o
58obj-$(CONFIG_RTC_DRV_MV) += rtc-mv.o 59obj-$(CONFIG_RTC_DRV_MV) += rtc-mv.o
59obj-$(CONFIG_RTC_DRV_NUC900) += rtc-nuc900.o 60obj-$(CONFIG_RTC_DRV_NUC900) += rtc-nuc900.o
60obj-$(CONFIG_RTC_DRV_OMAP) += rtc-omap.o 61obj-$(CONFIG_RTC_DRV_OMAP) += rtc-omap.o
diff --git a/drivers/rtc/rtc-mpc5121.c b/drivers/rtc/rtc-mpc5121.c
new file mode 100644
index 000000000000..4313ca03a96d
--- /dev/null
+++ b/drivers/rtc/rtc-mpc5121.c
@@ -0,0 +1,387 @@
1/*
2 * Real-time clock driver for MPC5121
3 *
4 * Copyright 2007, Domen Puncer <domen.puncer@telargo.com>
5 * Copyright 2008, Freescale Semiconductor, Inc. All rights reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/rtc.h>
15#include <linux/of_device.h>
16#include <linux/of_platform.h>
17#include <linux/io.h>
18
19struct mpc5121_rtc_regs {
20 u8 set_time; /* RTC + 0x00 */
21 u8 hour_set; /* RTC + 0x01 */
22 u8 minute_set; /* RTC + 0x02 */
23 u8 second_set; /* RTC + 0x03 */
24
25 u8 set_date; /* RTC + 0x04 */
26 u8 month_set; /* RTC + 0x05 */
27 u8 weekday_set; /* RTC + 0x06 */
28 u8 date_set; /* RTC + 0x07 */
29
30 u8 write_sw; /* RTC + 0x08 */
31 u8 sw_set; /* RTC + 0x09 */
32 u16 year_set; /* RTC + 0x0a */
33
34 u8 alm_enable; /* RTC + 0x0c */
35 u8 alm_hour_set; /* RTC + 0x0d */
36 u8 alm_min_set; /* RTC + 0x0e */
37 u8 int_enable; /* RTC + 0x0f */
38
39 u8 reserved1;
40 u8 hour; /* RTC + 0x11 */
41 u8 minute; /* RTC + 0x12 */
42 u8 second; /* RTC + 0x13 */
43
44 u8 month; /* RTC + 0x14 */
45 u8 wday_mday; /* RTC + 0x15 */
46 u16 year; /* RTC + 0x16 */
47
48 u8 int_alm; /* RTC + 0x18 */
49 u8 int_sw; /* RTC + 0x19 */
50 u8 alm_status; /* RTC + 0x1a */
51 u8 sw_minute; /* RTC + 0x1b */
52
53 u8 bus_error_1; /* RTC + 0x1c */
54 u8 int_day; /* RTC + 0x1d */
55 u8 int_min; /* RTC + 0x1e */
56 u8 int_sec; /* RTC + 0x1f */
57
58 /*
59 * target_time:
60 * intended to be used for hibernation but hibernation
61 * does not work on silicon rev 1.5 so use it for non-volatile
62 * storage of offset between the actual_time register and linux
63 * time
64 */
65 u32 target_time; /* RTC + 0x20 */
66 /*
67 * actual_time:
68 * readonly time since VBAT_RTC was last connected
69 */
70 u32 actual_time; /* RTC + 0x24 */
71 u32 keep_alive; /* RTC + 0x28 */
72};
73
74struct mpc5121_rtc_data {
75 unsigned irq;
76 unsigned irq_periodic;
77 struct mpc5121_rtc_regs __iomem *regs;
78 struct rtc_device *rtc;
79 struct rtc_wkalrm wkalarm;
80};
81
82/*
83 * Update second/minute/hour registers.
84 *
85 * This is just so alarm will work.
86 */
87static void mpc5121_rtc_update_smh(struct mpc5121_rtc_regs __iomem *regs,
88 struct rtc_time *tm)
89{
90 out_8(&regs->second_set, tm->tm_sec);
91 out_8(&regs->minute_set, tm->tm_min);
92 out_8(&regs->hour_set, tm->tm_hour);
93
94 /* set time sequence */
95 out_8(&regs->set_time, 0x1);
96 out_8(&regs->set_time, 0x3);
97 out_8(&regs->set_time, 0x1);
98 out_8(&regs->set_time, 0x0);
99}
100
101static int mpc5121_rtc_read_time(struct device *dev, struct rtc_time *tm)
102{
103 struct mpc5121_rtc_data *rtc = dev_get_drvdata(dev);
104 struct mpc5121_rtc_regs __iomem *regs = rtc->regs;
105 unsigned long now;
106
107 /*
108 * linux time is actual_time plus the offset saved in target_time
109 */
110 now = in_be32(&regs->actual_time) + in_be32(&regs->target_time);
111
112 rtc_time_to_tm(now, tm);
113
114 /*
115 * update second minute hour registers
116 * so alarms will work
117 */
118 mpc5121_rtc_update_smh(regs, tm);
119
120 return rtc_valid_tm(tm);
121}
122
123static int mpc5121_rtc_set_time(struct device *dev, struct rtc_time *tm)
124{
125 struct mpc5121_rtc_data *rtc = dev_get_drvdata(dev);
126 struct mpc5121_rtc_regs __iomem *regs = rtc->regs;
127 int ret;
128 unsigned long now;
129
130 /*
131 * The actual_time register is read only so we write the offset
132 * between it and linux time to the target_time register.
133 */
134 ret = rtc_tm_to_time(tm, &now);
135 if (ret == 0)
136 out_be32(&regs->target_time, now - in_be32(&regs->actual_time));
137
138 /*
139 * update second minute hour registers
140 * so alarms will work
141 */
142 mpc5121_rtc_update_smh(regs, tm);
143
144 return 0;
145}
146
147static int mpc5121_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
148{
149 struct mpc5121_rtc_data *rtc = dev_get_drvdata(dev);
150 struct mpc5121_rtc_regs __iomem *regs = rtc->regs;
151
152 *alarm = rtc->wkalarm;
153
154 alarm->pending = in_8(&regs->alm_status);
155
156 return 0;
157}
158
159static int mpc5121_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
160{
161 struct mpc5121_rtc_data *rtc = dev_get_drvdata(dev);
162 struct mpc5121_rtc_regs __iomem *regs = rtc->regs;
163
164 /*
165 * the alarm has no seconds so deal with it
166 */
167 if (alarm->time.tm_sec) {
168 alarm->time.tm_sec = 0;
169 alarm->time.tm_min++;
170 if (alarm->time.tm_min >= 60) {
171 alarm->time.tm_min = 0;
172 alarm->time.tm_hour++;
173 if (alarm->time.tm_hour >= 24)
174 alarm->time.tm_hour = 0;
175 }
176 }
177
178 alarm->time.tm_mday = -1;
179 alarm->time.tm_mon = -1;
180 alarm->time.tm_year = -1;
181
182 out_8(&regs->alm_min_set, alarm->time.tm_min);
183 out_8(&regs->alm_hour_set, alarm->time.tm_hour);
184
185 out_8(&regs->alm_enable, alarm->enabled);
186
187 rtc->wkalarm = *alarm;
188 return 0;
189}
190
191static irqreturn_t mpc5121_rtc_handler(int irq, void *dev)
192{
193 struct mpc5121_rtc_data *rtc = dev_get_drvdata((struct device *)dev);
194 struct mpc5121_rtc_regs __iomem *regs = rtc->regs;
195
196 if (in_8(&regs->int_alm)) {
197 /* acknowledge and clear status */
198 out_8(&regs->int_alm, 1);
199 out_8(&regs->alm_status, 1);
200
201 rtc_update_irq(rtc->rtc, 1, RTC_IRQF | RTC_AF);
202 return IRQ_HANDLED;
203 }
204
205 return IRQ_NONE;
206}
207
208static irqreturn_t mpc5121_rtc_handler_upd(int irq, void *dev)
209{
210 struct mpc5121_rtc_data *rtc = dev_get_drvdata((struct device *)dev);
211 struct mpc5121_rtc_regs __iomem *regs = rtc->regs;
212
213 if (in_8(&regs->int_sec) && (in_8(&regs->int_enable) & 0x1)) {
214 /* acknowledge */
215 out_8(&regs->int_sec, 1);
216
217 rtc_update_irq(rtc->rtc, 1, RTC_IRQF | RTC_UF);
218 return IRQ_HANDLED;
219 }
220
221 return IRQ_NONE;
222}
223
224static int mpc5121_rtc_alarm_irq_enable(struct device *dev,
225 unsigned int enabled)
226{
227 struct mpc5121_rtc_data *rtc = dev_get_drvdata(dev);
228 struct mpc5121_rtc_regs __iomem *regs = rtc->regs;
229 int val;
230
231 if (enabled)
232 val = 1;
233 else
234 val = 0;
235
236 out_8(&regs->alm_enable, val);
237 rtc->wkalarm.enabled = val;
238
239 return 0;
240}
241
242static int mpc5121_rtc_update_irq_enable(struct device *dev,
243 unsigned int enabled)
244{
245 struct mpc5121_rtc_data *rtc = dev_get_drvdata(dev);
246 struct mpc5121_rtc_regs __iomem *regs = rtc->regs;
247 int val;
248
249 val = in_8(&regs->int_enable);
250
251 if (enabled)
252 val = (val & ~0x8) | 0x1;
253 else
254 val &= ~0x1;
255
256 out_8(&regs->int_enable, val);
257
258 return 0;
259}
260
261static const struct rtc_class_ops mpc5121_rtc_ops = {
262 .read_time = mpc5121_rtc_read_time,
263 .set_time = mpc5121_rtc_set_time,
264 .read_alarm = mpc5121_rtc_read_alarm,
265 .set_alarm = mpc5121_rtc_set_alarm,
266 .alarm_irq_enable = mpc5121_rtc_alarm_irq_enable,
267 .update_irq_enable = mpc5121_rtc_update_irq_enable,
268};
269
270static int __devinit mpc5121_rtc_probe(struct of_device *op,
271 const struct of_device_id *match)
272{
273 struct mpc5121_rtc_data *rtc;
274 int err = 0;
275 u32 ka;
276
277 rtc = kzalloc(sizeof(*rtc), GFP_KERNEL);
278 if (!rtc)
279 return -ENOMEM;
280
281 rtc->regs = of_iomap(op->node, 0);
282 if (!rtc->regs) {
283 dev_err(&op->dev, "%s: couldn't map io space\n", __func__);
284 err = -ENOSYS;
285 goto out_free;
286 }
287
288 device_init_wakeup(&op->dev, 1);
289
290 dev_set_drvdata(&op->dev, rtc);
291
292 rtc->irq = irq_of_parse_and_map(op->node, 1);
293 err = request_irq(rtc->irq, mpc5121_rtc_handler, IRQF_DISABLED,
294 "mpc5121-rtc", &op->dev);
295 if (err) {
296 dev_err(&op->dev, "%s: could not request irq: %i\n",
297 __func__, rtc->irq);
298 goto out_dispose;
299 }
300
301 rtc->irq_periodic = irq_of_parse_and_map(op->node, 0);
302 err = request_irq(rtc->irq_periodic, mpc5121_rtc_handler_upd,
303 IRQF_DISABLED, "mpc5121-rtc_upd", &op->dev);
304 if (err) {
305 dev_err(&op->dev, "%s: could not request irq: %i\n",
306 __func__, rtc->irq_periodic);
307 goto out_dispose2;
308 }
309
310 ka = in_be32(&rtc->regs->keep_alive);
311 if (ka & 0x02) {
312 dev_warn(&op->dev,
313 "mpc5121-rtc: Battery or oscillator failure!\n");
314 out_be32(&rtc->regs->keep_alive, ka);
315 }
316
317 rtc->rtc = rtc_device_register("mpc5121-rtc", &op->dev,
318 &mpc5121_rtc_ops, THIS_MODULE);
319 if (IS_ERR(rtc->rtc)) {
320 err = PTR_ERR(rtc->rtc);
321 goto out_free_irq;
322 }
323
324 return 0;
325
326out_free_irq:
327 free_irq(rtc->irq_periodic, &op->dev);
328out_dispose2:
329 irq_dispose_mapping(rtc->irq_periodic);
330 free_irq(rtc->irq, &op->dev);
331out_dispose:
332 irq_dispose_mapping(rtc->irq);
333 iounmap(rtc->regs);
334out_free:
335 kfree(rtc);
336
337 return err;
338}
339
340static int __devexit mpc5121_rtc_remove(struct of_device *op)
341{
342 struct mpc5121_rtc_data *rtc = dev_get_drvdata(&op->dev);
343 struct mpc5121_rtc_regs __iomem *regs = rtc->regs;
344
345 /* disable interrupt, so there are no nasty surprises */
346 out_8(&regs->alm_enable, 0);
347 out_8(&regs->int_enable, in_8(&regs->int_enable) & ~0x1);
348
349 rtc_device_unregister(rtc->rtc);
350 iounmap(rtc->regs);
351 free_irq(rtc->irq, &op->dev);
352 free_irq(rtc->irq_periodic, &op->dev);
353 irq_dispose_mapping(rtc->irq);
354 irq_dispose_mapping(rtc->irq_periodic);
355 dev_set_drvdata(&op->dev, NULL);
356 kfree(rtc);
357
358 return 0;
359}
360
361static struct of_device_id mpc5121_rtc_match[] __devinitdata = {
362 { .compatible = "fsl,mpc5121-rtc", },
363 {},
364};
365
366static struct of_platform_driver mpc5121_rtc_driver = {
367 .owner = THIS_MODULE,
368 .name = "mpc5121-rtc",
369 .match_table = mpc5121_rtc_match,
370 .probe = mpc5121_rtc_probe,
371 .remove = __devexit_p(mpc5121_rtc_remove),
372};
373
374static int __init mpc5121_rtc_init(void)
375{
376 return of_register_platform_driver(&mpc5121_rtc_driver);
377}
378module_init(mpc5121_rtc_init);
379
380static void __exit mpc5121_rtc_exit(void)
381{
382 of_unregister_platform_driver(&mpc5121_rtc_driver);
383}
384module_exit(mpc5121_rtc_exit);
385
386MODULE_LICENSE("GPL");
387MODULE_AUTHOR("John Rigby <jcrigby@gmail.com>");