dff9ff476b0dd4769fa86c9ec8f578e962a00c6b
[profile/ivi/kernel-adaptation-intel-automotive.git] / drivers / rtc / rtc-omap.c
1 /*
2  * TI OMAP1 Real Time Clock interface for Linux
3  *
4  * Copyright (C) 2003 MontaVista Software, Inc.
5  * Author: George G. Davis <gdavis@mvista.com> or <source@mvista.com>
6  *
7  * Copyright (C) 2006 David Brownell (new RTC framework)
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version
12  * 2 of the License, or (at your option) any later version.
13  */
14
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/ioport.h>
19 #include <linux/delay.h>
20 #include <linux/rtc.h>
21 #include <linux/bcd.h>
22 #include <linux/platform_device.h>
23 #include <linux/of.h>
24 #include <linux/of_device.h>
25
26 #include <asm/io.h>
27
28
29 /* The OMAP1 RTC is a year/month/day/hours/minutes/seconds BCD clock
30  * with century-range alarm matching, driven by the 32kHz clock.
31  *
32  * The main user-visible ways it differs from PC RTCs are by omitting
33  * "don't care" alarm fields and sub-second periodic IRQs, and having
34  * an autoadjust mechanism to calibrate to the true oscillator rate.
35  *
36  * Board-specific wiring options include using split power mode with
37  * RTC_OFF_NOFF used as the reset signal (so the RTC won't be reset),
38  * and wiring RTC_WAKE_INT (so the RTC alarm can wake the system from
39  * low power modes) for OMAP1 boards (OMAP-L138 has this built into
40  * the SoC). See the BOARD-SPECIFIC CUSTOMIZATION comment.
41  */
42
43 #define DRIVER_NAME                     "omap_rtc"
44
45 #define OMAP_RTC_BASE                   0xfffb4800
46
47 /* RTC registers */
48 #define OMAP_RTC_SECONDS_REG            0x00
49 #define OMAP_RTC_MINUTES_REG            0x04
50 #define OMAP_RTC_HOURS_REG              0x08
51 #define OMAP_RTC_DAYS_REG               0x0C
52 #define OMAP_RTC_MONTHS_REG             0x10
53 #define OMAP_RTC_YEARS_REG              0x14
54 #define OMAP_RTC_WEEKS_REG              0x18
55
56 #define OMAP_RTC_ALARM_SECONDS_REG      0x20
57 #define OMAP_RTC_ALARM_MINUTES_REG      0x24
58 #define OMAP_RTC_ALARM_HOURS_REG        0x28
59 #define OMAP_RTC_ALARM_DAYS_REG         0x2c
60 #define OMAP_RTC_ALARM_MONTHS_REG       0x30
61 #define OMAP_RTC_ALARM_YEARS_REG        0x34
62
63 #define OMAP_RTC_CTRL_REG               0x40
64 #define OMAP_RTC_STATUS_REG             0x44
65 #define OMAP_RTC_INTERRUPTS_REG         0x48
66
67 #define OMAP_RTC_COMP_LSB_REG           0x4c
68 #define OMAP_RTC_COMP_MSB_REG           0x50
69 #define OMAP_RTC_OSC_REG                0x54
70
71 #define OMAP_RTC_KICK0_REG              0x6c
72 #define OMAP_RTC_KICK1_REG              0x70
73
74 /* OMAP_RTC_CTRL_REG bit fields: */
75 #define OMAP_RTC_CTRL_SPLIT             (1<<7)
76 #define OMAP_RTC_CTRL_DISABLE           (1<<6)
77 #define OMAP_RTC_CTRL_SET_32_COUNTER    (1<<5)
78 #define OMAP_RTC_CTRL_TEST              (1<<4)
79 #define OMAP_RTC_CTRL_MODE_12_24        (1<<3)
80 #define OMAP_RTC_CTRL_AUTO_COMP         (1<<2)
81 #define OMAP_RTC_CTRL_ROUND_30S         (1<<1)
82 #define OMAP_RTC_CTRL_STOP              (1<<0)
83
84 /* OMAP_RTC_STATUS_REG bit fields: */
85 #define OMAP_RTC_STATUS_POWER_UP        (1<<7)
86 #define OMAP_RTC_STATUS_ALARM           (1<<6)
87 #define OMAP_RTC_STATUS_1D_EVENT        (1<<5)
88 #define OMAP_RTC_STATUS_1H_EVENT        (1<<4)
89 #define OMAP_RTC_STATUS_1M_EVENT        (1<<3)
90 #define OMAP_RTC_STATUS_1S_EVENT        (1<<2)
91 #define OMAP_RTC_STATUS_RUN             (1<<1)
92 #define OMAP_RTC_STATUS_BUSY            (1<<0)
93
94 /* OMAP_RTC_INTERRUPTS_REG bit fields: */
95 #define OMAP_RTC_INTERRUPTS_IT_ALARM    (1<<3)
96 #define OMAP_RTC_INTERRUPTS_IT_TIMER    (1<<2)
97
98 /* OMAP_RTC_KICKER values */
99 #define KICK0_VALUE                     0x83e70b13
100 #define KICK1_VALUE                     0x95a4f1e0
101
102 #define OMAP_RTC_HAS_KICKER             0x1
103
104 static void __iomem     *rtc_base;
105
106 #define rtc_read(addr)          readb(rtc_base + (addr))
107 #define rtc_write(val, addr)    writeb(val, rtc_base + (addr))
108
109 #define rtc_writel(val, addr)   writel(val, rtc_base + (addr))
110
111
112 /* we rely on the rtc framework to handle locking (rtc->ops_lock),
113  * so the only other requirement is that register accesses which
114  * require BUSY to be clear are made with IRQs locally disabled
115  */
116 static void rtc_wait_not_busy(void)
117 {
118         int     count = 0;
119         u8      status;
120
121         /* BUSY may stay active for 1/32768 second (~30 usec) */
122         for (count = 0; count < 50; count++) {
123                 status = rtc_read(OMAP_RTC_STATUS_REG);
124                 if ((status & (u8)OMAP_RTC_STATUS_BUSY) == 0)
125                         break;
126                 udelay(1);
127         }
128         /* now we have ~15 usec to read/write various registers */
129 }
130
131 static irqreturn_t rtc_irq(int irq, void *rtc)
132 {
133         unsigned long           events = 0;
134         u8                      irq_data;
135
136         irq_data = rtc_read(OMAP_RTC_STATUS_REG);
137
138         /* alarm irq? */
139         if (irq_data & OMAP_RTC_STATUS_ALARM) {
140                 rtc_write(OMAP_RTC_STATUS_ALARM, OMAP_RTC_STATUS_REG);
141                 events |= RTC_IRQF | RTC_AF;
142         }
143
144         /* 1/sec periodic/update irq? */
145         if (irq_data & OMAP_RTC_STATUS_1S_EVENT)
146                 events |= RTC_IRQF | RTC_UF;
147
148         rtc_update_irq(rtc, 1, events);
149
150         return IRQ_HANDLED;
151 }
152
153 static int omap_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
154 {
155         u8 reg;
156
157         local_irq_disable();
158         rtc_wait_not_busy();
159         reg = rtc_read(OMAP_RTC_INTERRUPTS_REG);
160         if (enabled)
161                 reg |= OMAP_RTC_INTERRUPTS_IT_ALARM;
162         else
163                 reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM;
164         rtc_wait_not_busy();
165         rtc_write(reg, OMAP_RTC_INTERRUPTS_REG);
166         local_irq_enable();
167
168         return 0;
169 }
170
171 /* this hardware doesn't support "don't care" alarm fields */
172 static int tm2bcd(struct rtc_time *tm)
173 {
174         if (rtc_valid_tm(tm) != 0)
175                 return -EINVAL;
176
177         tm->tm_sec = bin2bcd(tm->tm_sec);
178         tm->tm_min = bin2bcd(tm->tm_min);
179         tm->tm_hour = bin2bcd(tm->tm_hour);
180         tm->tm_mday = bin2bcd(tm->tm_mday);
181
182         tm->tm_mon = bin2bcd(tm->tm_mon + 1);
183
184         /* epoch == 1900 */
185         if (tm->tm_year < 100 || tm->tm_year > 199)
186                 return -EINVAL;
187         tm->tm_year = bin2bcd(tm->tm_year - 100);
188
189         return 0;
190 }
191
192 static void bcd2tm(struct rtc_time *tm)
193 {
194         tm->tm_sec = bcd2bin(tm->tm_sec);
195         tm->tm_min = bcd2bin(tm->tm_min);
196         tm->tm_hour = bcd2bin(tm->tm_hour);
197         tm->tm_mday = bcd2bin(tm->tm_mday);
198         tm->tm_mon = bcd2bin(tm->tm_mon) - 1;
199         /* epoch == 1900 */
200         tm->tm_year = bcd2bin(tm->tm_year) + 100;
201 }
202
203
204 static int omap_rtc_read_time(struct device *dev, struct rtc_time *tm)
205 {
206         /* we don't report wday/yday/isdst ... */
207         local_irq_disable();
208         rtc_wait_not_busy();
209
210         tm->tm_sec = rtc_read(OMAP_RTC_SECONDS_REG);
211         tm->tm_min = rtc_read(OMAP_RTC_MINUTES_REG);
212         tm->tm_hour = rtc_read(OMAP_RTC_HOURS_REG);
213         tm->tm_mday = rtc_read(OMAP_RTC_DAYS_REG);
214         tm->tm_mon = rtc_read(OMAP_RTC_MONTHS_REG);
215         tm->tm_year = rtc_read(OMAP_RTC_YEARS_REG);
216
217         local_irq_enable();
218
219         bcd2tm(tm);
220         return 0;
221 }
222
223 static int omap_rtc_set_time(struct device *dev, struct rtc_time *tm)
224 {
225         if (tm2bcd(tm) < 0)
226                 return -EINVAL;
227         local_irq_disable();
228         rtc_wait_not_busy();
229
230         rtc_write(tm->tm_year, OMAP_RTC_YEARS_REG);
231         rtc_write(tm->tm_mon, OMAP_RTC_MONTHS_REG);
232         rtc_write(tm->tm_mday, OMAP_RTC_DAYS_REG);
233         rtc_write(tm->tm_hour, OMAP_RTC_HOURS_REG);
234         rtc_write(tm->tm_min, OMAP_RTC_MINUTES_REG);
235         rtc_write(tm->tm_sec, OMAP_RTC_SECONDS_REG);
236
237         local_irq_enable();
238
239         return 0;
240 }
241
242 static int omap_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
243 {
244         local_irq_disable();
245         rtc_wait_not_busy();
246
247         alm->time.tm_sec = rtc_read(OMAP_RTC_ALARM_SECONDS_REG);
248         alm->time.tm_min = rtc_read(OMAP_RTC_ALARM_MINUTES_REG);
249         alm->time.tm_hour = rtc_read(OMAP_RTC_ALARM_HOURS_REG);
250         alm->time.tm_mday = rtc_read(OMAP_RTC_ALARM_DAYS_REG);
251         alm->time.tm_mon = rtc_read(OMAP_RTC_ALARM_MONTHS_REG);
252         alm->time.tm_year = rtc_read(OMAP_RTC_ALARM_YEARS_REG);
253
254         local_irq_enable();
255
256         bcd2tm(&alm->time);
257         alm->enabled = !!(rtc_read(OMAP_RTC_INTERRUPTS_REG)
258                         & OMAP_RTC_INTERRUPTS_IT_ALARM);
259
260         return 0;
261 }
262
263 static int omap_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
264 {
265         u8 reg;
266
267         if (tm2bcd(&alm->time) < 0)
268                 return -EINVAL;
269
270         local_irq_disable();
271         rtc_wait_not_busy();
272
273         rtc_write(alm->time.tm_year, OMAP_RTC_ALARM_YEARS_REG);
274         rtc_write(alm->time.tm_mon, OMAP_RTC_ALARM_MONTHS_REG);
275         rtc_write(alm->time.tm_mday, OMAP_RTC_ALARM_DAYS_REG);
276         rtc_write(alm->time.tm_hour, OMAP_RTC_ALARM_HOURS_REG);
277         rtc_write(alm->time.tm_min, OMAP_RTC_ALARM_MINUTES_REG);
278         rtc_write(alm->time.tm_sec, OMAP_RTC_ALARM_SECONDS_REG);
279
280         reg = rtc_read(OMAP_RTC_INTERRUPTS_REG);
281         if (alm->enabled)
282                 reg |= OMAP_RTC_INTERRUPTS_IT_ALARM;
283         else
284                 reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM;
285         rtc_write(reg, OMAP_RTC_INTERRUPTS_REG);
286
287         local_irq_enable();
288
289         return 0;
290 }
291
292 static struct rtc_class_ops omap_rtc_ops = {
293         .read_time      = omap_rtc_read_time,
294         .set_time       = omap_rtc_set_time,
295         .read_alarm     = omap_rtc_read_alarm,
296         .set_alarm      = omap_rtc_set_alarm,
297         .alarm_irq_enable = omap_rtc_alarm_irq_enable,
298 };
299
300 static int omap_rtc_alarm;
301 static int omap_rtc_timer;
302
303 #define OMAP_RTC_DATA_DA830_IDX 1
304
305 static struct platform_device_id omap_rtc_devtype[] = {
306         {
307                 .name   = DRIVER_NAME,
308         }, {
309                 .name   = "da830-rtc",
310                 .driver_data = OMAP_RTC_HAS_KICKER,
311         },
312         {},
313 };
314 MODULE_DEVICE_TABLE(platform, omap_rtc_devtype);
315
316 static const struct of_device_id omap_rtc_of_match[] = {
317         {       .compatible     = "ti,da830-rtc",
318                 .data           = &omap_rtc_devtype[OMAP_RTC_DATA_DA830_IDX],
319         },
320         {},
321 };
322 MODULE_DEVICE_TABLE(of, omap_rtc_of_match);
323
324 static int __init omap_rtc_probe(struct platform_device *pdev)
325 {
326         struct resource         *res, *mem;
327         struct rtc_device       *rtc;
328         u8                      reg, new_ctrl;
329         const struct platform_device_id *id_entry;
330         const struct of_device_id *of_id;
331
332         of_id = of_match_device(omap_rtc_of_match, &pdev->dev);
333         if (of_id)
334                 pdev->id_entry = of_id->data;
335
336         omap_rtc_timer = platform_get_irq(pdev, 0);
337         if (omap_rtc_timer <= 0) {
338                 pr_debug("%s: no update irq?\n", pdev->name);
339                 return -ENOENT;
340         }
341
342         omap_rtc_alarm = platform_get_irq(pdev, 1);
343         if (omap_rtc_alarm <= 0) {
344                 pr_debug("%s: no alarm irq?\n", pdev->name);
345                 return -ENOENT;
346         }
347
348         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
349         if (!res) {
350                 pr_debug("%s: RTC resource data missing\n", pdev->name);
351                 return -ENOENT;
352         }
353
354         mem = request_mem_region(res->start, resource_size(res), pdev->name);
355         if (!mem) {
356                 pr_debug("%s: RTC registers at %08x are not free\n",
357                         pdev->name, res->start);
358                 return -EBUSY;
359         }
360
361         rtc_base = ioremap(res->start, resource_size(res));
362         if (!rtc_base) {
363                 pr_debug("%s: RTC registers can't be mapped\n", pdev->name);
364                 goto fail;
365         }
366
367         id_entry = platform_get_device_id(pdev);
368         if (id_entry && (id_entry->driver_data & OMAP_RTC_HAS_KICKER)) {
369                 rtc_writel(KICK0_VALUE, OMAP_RTC_KICK0_REG);
370                 rtc_writel(KICK1_VALUE, OMAP_RTC_KICK1_REG);
371         }
372
373         rtc = rtc_device_register(pdev->name, &pdev->dev,
374                         &omap_rtc_ops, THIS_MODULE);
375         if (IS_ERR(rtc)) {
376                 pr_debug("%s: can't register RTC device, err %ld\n",
377                         pdev->name, PTR_ERR(rtc));
378                 goto fail0;
379         }
380         platform_set_drvdata(pdev, rtc);
381         dev_set_drvdata(&rtc->dev, mem);
382
383         /* clear pending irqs, and set 1/second periodic,
384          * which we'll use instead of update irqs
385          */
386         rtc_write(0, OMAP_RTC_INTERRUPTS_REG);
387
388         /* clear old status */
389         reg = rtc_read(OMAP_RTC_STATUS_REG);
390         if (reg & (u8) OMAP_RTC_STATUS_POWER_UP) {
391                 pr_info("%s: RTC power up reset detected\n",
392                         pdev->name);
393                 rtc_write(OMAP_RTC_STATUS_POWER_UP, OMAP_RTC_STATUS_REG);
394         }
395         if (reg & (u8) OMAP_RTC_STATUS_ALARM)
396                 rtc_write(OMAP_RTC_STATUS_ALARM, OMAP_RTC_STATUS_REG);
397
398         /* handle periodic and alarm irqs */
399         if (request_irq(omap_rtc_timer, rtc_irq, 0,
400                         dev_name(&rtc->dev), rtc)) {
401                 pr_debug("%s: RTC timer interrupt IRQ%d already claimed\n",
402                         pdev->name, omap_rtc_timer);
403                 goto fail1;
404         }
405         if ((omap_rtc_timer != omap_rtc_alarm) &&
406                 (request_irq(omap_rtc_alarm, rtc_irq, 0,
407                         dev_name(&rtc->dev), rtc))) {
408                 pr_debug("%s: RTC alarm interrupt IRQ%d already claimed\n",
409                         pdev->name, omap_rtc_alarm);
410                 goto fail2;
411         }
412
413         /* On boards with split power, RTC_ON_NOFF won't reset the RTC */
414         reg = rtc_read(OMAP_RTC_CTRL_REG);
415         if (reg & (u8) OMAP_RTC_CTRL_STOP)
416                 pr_info("%s: already running\n", pdev->name);
417
418         /* force to 24 hour mode */
419         new_ctrl = reg & (OMAP_RTC_CTRL_SPLIT|OMAP_RTC_CTRL_AUTO_COMP);
420         new_ctrl |= OMAP_RTC_CTRL_STOP;
421
422         /* BOARD-SPECIFIC CUSTOMIZATION CAN GO HERE:
423          *
424          *  - Device wake-up capability setting should come through chip
425          *    init logic. OMAP1 boards should initialize the "wakeup capable"
426          *    flag in the platform device if the board is wired right for
427          *    being woken up by RTC alarm. For OMAP-L138, this capability
428          *    is built into the SoC by the "Deep Sleep" capability.
429          *
430          *  - Boards wired so RTC_ON_nOFF is used as the reset signal,
431          *    rather than nPWRON_RESET, should forcibly enable split
432          *    power mode.  (Some chip errata report that RTC_CTRL_SPLIT
433          *    is write-only, and always reads as zero...)
434          */
435
436         if (new_ctrl & (u8) OMAP_RTC_CTRL_SPLIT)
437                 pr_info("%s: split power mode\n", pdev->name);
438
439         if (reg != new_ctrl)
440                 rtc_write(new_ctrl, OMAP_RTC_CTRL_REG);
441
442         return 0;
443
444 fail2:
445         free_irq(omap_rtc_timer, rtc);
446 fail1:
447         rtc_device_unregister(rtc);
448 fail0:
449         if (id_entry && (id_entry->driver_data & OMAP_RTC_HAS_KICKER))
450                 rtc_writel(0, OMAP_RTC_KICK0_REG);
451         iounmap(rtc_base);
452 fail:
453         release_mem_region(mem->start, resource_size(mem));
454         return -EIO;
455 }
456
457 static int __exit omap_rtc_remove(struct platform_device *pdev)
458 {
459         struct rtc_device       *rtc = platform_get_drvdata(pdev);
460         struct resource         *mem = dev_get_drvdata(&rtc->dev);
461         const struct platform_device_id *id_entry =
462                                 platform_get_device_id(pdev);
463
464         device_init_wakeup(&pdev->dev, 0);
465
466         /* leave rtc running, but disable irqs */
467         rtc_write(0, OMAP_RTC_INTERRUPTS_REG);
468
469         free_irq(omap_rtc_timer, rtc);
470
471         if (omap_rtc_timer != omap_rtc_alarm)
472                 free_irq(omap_rtc_alarm, rtc);
473
474         rtc_device_unregister(rtc);
475         if (id_entry && (id_entry->driver_data & OMAP_RTC_HAS_KICKER))
476                 rtc_writel(0, OMAP_RTC_KICK0_REG);
477         iounmap(rtc_base);
478         release_mem_region(mem->start, resource_size(mem));
479         return 0;
480 }
481
482 #ifdef CONFIG_PM
483
484 static u8 irqstat;
485
486 static int omap_rtc_suspend(struct platform_device *pdev, pm_message_t state)
487 {
488         irqstat = rtc_read(OMAP_RTC_INTERRUPTS_REG);
489
490         /* FIXME the RTC alarm is not currently acting as a wakeup event
491          * source, and in fact this enable() call is just saving a flag
492          * that's never used...
493          */
494         if (device_may_wakeup(&pdev->dev))
495                 enable_irq_wake(omap_rtc_alarm);
496         else
497                 rtc_write(0, OMAP_RTC_INTERRUPTS_REG);
498
499         return 0;
500 }
501
502 static int omap_rtc_resume(struct platform_device *pdev)
503 {
504         if (device_may_wakeup(&pdev->dev))
505                 disable_irq_wake(omap_rtc_alarm);
506         else
507                 rtc_write(irqstat, OMAP_RTC_INTERRUPTS_REG);
508         return 0;
509 }
510
511 #else
512 #define omap_rtc_suspend NULL
513 #define omap_rtc_resume  NULL
514 #endif
515
516 static void omap_rtc_shutdown(struct platform_device *pdev)
517 {
518         rtc_write(0, OMAP_RTC_INTERRUPTS_REG);
519 }
520
521 MODULE_ALIAS("platform:omap_rtc");
522 static struct platform_driver omap_rtc_driver = {
523         .remove         = __exit_p(omap_rtc_remove),
524         .suspend        = omap_rtc_suspend,
525         .resume         = omap_rtc_resume,
526         .shutdown       = omap_rtc_shutdown,
527         .driver         = {
528                 .name   = DRIVER_NAME,
529                 .owner  = THIS_MODULE,
530                 .of_match_table = of_match_ptr(omap_rtc_of_match),
531         },
532         .id_table       = omap_rtc_devtype,
533 };
534
535 static int __init rtc_init(void)
536 {
537         return platform_driver_probe(&omap_rtc_driver, omap_rtc_probe);
538 }
539 module_init(rtc_init);
540
541 static void __exit rtc_exit(void)
542 {
543         platform_driver_unregister(&omap_rtc_driver);
544 }
545 module_exit(rtc_exit);
546
547 MODULE_AUTHOR("George G. Davis (and others)");
548 MODULE_LICENSE("GPL");