Marcoen Hirschberg | a45e632 | 2006-11-27 09:44:56 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2002 by Linus Nielsen Feltzing, Uwe Freese, Laurent Baum |
| 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame^] | 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version 2 |
| 15 | * of the License, or (at your option) any later version. |
Marcoen Hirschberg | a45e632 | 2006-11-27 09:44:56 +0000 | [diff] [blame] | 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
| 21 | #include "config.h" |
| 22 | #include "i2c.h" |
| 23 | #include "rtc.h" |
| 24 | #include "kernel.h" |
| 25 | #include "system.h" |
| 26 | #include "pcf50605.h" |
| 27 | #include <stdbool.h> |
| 28 | |
Thom Johansen | 8fd6d65 | 2007-02-28 21:55:11 +0000 | [diff] [blame] | 29 | /* Values which each disable one alarm time register */ |
Thom Johansen | 1ef5dad | 2007-09-28 15:09:54 +0000 | [diff] [blame] | 30 | static const char alarm_disable[] = { |
| 31 | 0x7f, 0x7f, 0x3f, 0x07, 0x3f, 0x1f, 0xff |
| 32 | }; |
Thom Johansen | 8fd6d65 | 2007-02-28 21:55:11 +0000 | [diff] [blame] | 33 | |
Marcoen Hirschberg | a45e632 | 2006-11-27 09:44:56 +0000 | [diff] [blame] | 34 | void rtc_init(void) |
| 35 | { |
Thom Johansen | 8fd6d65 | 2007-02-28 21:55:11 +0000 | [diff] [blame] | 36 | rtc_check_alarm_started(false); |
Marcoen Hirschberg | a45e632 | 2006-11-27 09:44:56 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | int rtc_read_datetime(unsigned char* buf) |
| 40 | { |
| 41 | return pcf50605_read_multiple(0x0a, buf, 7); |
| 42 | } |
| 43 | |
| 44 | int rtc_write_datetime(unsigned char* buf) |
| 45 | { |
Thom Johansen | 8fd6d65 | 2007-02-28 21:55:11 +0000 | [diff] [blame] | 46 | pcf50605_write_multiple(0x0a, buf, 7); |
Marcoen Hirschberg | a45e632 | 2006-11-27 09:44:56 +0000 | [diff] [blame] | 47 | return 1; |
| 48 | } |
Thom Johansen | 8fd6d65 | 2007-02-28 21:55:11 +0000 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * Checks the PCF interrupt 1 register bit 7 to see if an alarm interrupt has |
| 52 | * triggered since last we checked. |
| 53 | */ |
| 54 | bool rtc_check_alarm_flag(void) |
| 55 | { |
| 56 | return pcf50605_read(0x02) & 0x80; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Enables or disables the alarm. |
| 61 | * The Ipod bootloader clears all PCF interrupt registers and always enables |
| 62 | * the "wake on RTC" bit on OOCC1, so we have to rely on other means to find |
| 63 | * out if we just woke from an alarm. |
| 64 | * Return value is always false for us. |
| 65 | */ |
| 66 | bool rtc_enable_alarm(bool enable) |
| 67 | { |
| 68 | if (enable) { |
| 69 | /* Tell the PCF to ignore everything but second, minute and hour, so |
| 70 | * that an alarm will trigger the next time the alarm time occurs. |
| 71 | */ |
| 72 | pcf50605_write_multiple(0x14, alarm_disable + 3, 4); |
| 73 | /* Unmask the alarm interrupt (might be unneeded) */ |
| 74 | pcf50605_write(0x5, pcf50605_read(0x5) & ~0x80); |
Thom Johansen | 1ef5dad | 2007-09-28 15:09:54 +0000 | [diff] [blame] | 75 | /* Make sure wake on RTC is set when shutting down */ |
| 76 | pcf50605_wakeup_flags |= 0x10; |
Thom Johansen | 8fd6d65 | 2007-02-28 21:55:11 +0000 | [diff] [blame] | 77 | } else { |
| 78 | /* We use this year to indicate a disabled alarm. If you happen to live |
| 79 | * around this time and are annoyed by this, feel free to seek out my |
| 80 | * grave and do something nasty to it. |
| 81 | */ |
| 82 | pcf50605_write(0x17, 0x99); |
Thom Johansen | 1ef5dad | 2007-09-28 15:09:54 +0000 | [diff] [blame] | 83 | /* Make sure we don't wake on RTC after shutting down */ |
| 84 | pcf50605_wakeup_flags &= ~0x10; |
Thom Johansen | 8fd6d65 | 2007-02-28 21:55:11 +0000 | [diff] [blame] | 85 | } |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Check if alarm caused unit to start. |
| 91 | */ |
| 92 | bool rtc_check_alarm_started(bool release_alarm) |
| 93 | { |
| 94 | static bool run_before = false, alarm_state; |
| 95 | bool rc; |
| 96 | |
| 97 | if (run_before) { |
| 98 | rc = alarm_state; |
| 99 | alarm_state &= ~release_alarm; |
| 100 | } else { |
| 101 | char rt[3], at[3]; |
| 102 | /* The Ipod bootloader seems to read (and thus clear) the PCF interrupt |
| 103 | * registers, so we need to find some other way to detect if an alarm |
| 104 | * just happened |
| 105 | */ |
| 106 | pcf50605_read_multiple(0x0a, rt, 3); |
| 107 | pcf50605_read_multiple(0x11, at, 3); |
| 108 | |
| 109 | /* If alarm time and real time match within 10 seconds of each other, we |
| 110 | * assume an alarm just triggered |
| 111 | */ |
| 112 | rc = alarm_state = rt[1] == at[1] && rt[2] == at[2] |
| 113 | && (rt[0] - at[0]) <= 10; |
| 114 | run_before = true; |
| 115 | } |
| 116 | return rc; |
| 117 | } |
| 118 | |
| 119 | void rtc_set_alarm(int h, int m) |
| 120 | { |
| 121 | /* Set us to wake at the first second of the specified time */ |
| 122 | pcf50605_write(0x11, 0); |
| 123 | /* Convert to BCD */ |
| 124 | pcf50605_write(0x12, ((m/10) << 4) | m%10); |
| 125 | pcf50605_write(0x13, ((h/10) << 4) | h%10); |
| 126 | } |
| 127 | |
| 128 | void rtc_get_alarm(int *h, int *m) |
| 129 | { |
| 130 | char buf[2]; |
| 131 | |
| 132 | pcf50605_read_multiple(0x12, buf, 2); |
| 133 | /* Convert from BCD */ |
| 134 | *m = ((buf[0] >> 4) & 0x7)*10 + (buf[0] & 0x0f); |
| 135 | *h = ((buf[1] >> 4) & 0x3)*10 + (buf[1] & 0x0f); |
| 136 | } |
| 137 | |