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