Correct beast manual install instructions in Windows.
[kugel-rb.git] / firmware / drivers / rtc / rtc_m41st84w.c
blobeca61c7f0f7f0d2373e8e6b0a59f1b4410aa453e
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Linus Nielsen Feltzing, Uwe Freese, Laurent Baum
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.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "config.h"
22 #include "i2c.h"
23 #include "rtc.h"
24 #include "kernel.h"
25 #include "system.h"
26 #include <stdbool.h>
28 #define RTC_ADR 0xd0
29 #define RTC_DEV_WRITE (RTC_ADR | 0x00)
30 #define RTC_DEV_READ (RTC_ADR | 0x01)
32 void rtc_init(void)
34 unsigned char data;
36 #ifdef HAVE_RTC_ALARM
37 /* Check + save alarm bit first, before the power thread starts watching */
38 rtc_check_alarm_started(false);
39 #endif
41 /* Clear the Stop bit if it is set */
42 data = rtc_read(0x01);
43 if(data & 0x80)
44 rtc_write(0x01, 0x00);
46 /* Clear the HT bit if it is set */
47 data = rtc_read(0x0c);
49 if(data & 0x40)
51 data &= ~0x40;
52 rtc_write(0x0c,data);
55 #ifdef HAVE_RTC_ALARM
57 /* Clear Trec bit, write-protecting the RTC for 200ms when shutting off */
58 /* without this, the alarm won't work! */
60 data = rtc_read(0x04);
61 if (data & 0x80)
63 data &= ~0x80;
64 rtc_write(0x04, data);
67 /* Also, make sure that the OUT bit in register 8 is 1,
68 otherwise the player can't be turned off. */
69 rtc_write(8, rtc_read(8) | 0x80);
71 #endif
74 #ifdef HAVE_RTC_ALARM
76 /* check whether the unit has been started by the RTC alarm function */
77 /* (check for AF, which => started using wakeup alarm) */
78 bool rtc_check_alarm_started(bool release_alarm)
80 static bool alarm_state, run_before;
81 bool rc;
83 if (run_before) {
84 rc = alarm_state;
85 alarm_state &= ~release_alarm;
86 } else {
87 /* This call resets AF, so we store the state for later recall */
88 rc = alarm_state = rtc_check_alarm_flag();
89 run_before = true;
92 return rc;
95 * Checks the AL register. This call resets AL once read.
97 * We're only interested if ABE is set. AL is still raised regardless
98 * even if the unit is off when the alarm occurs.
100 bool rtc_check_alarm_flag(void)
102 return ( ( (rtc_read(0x0f) & 0x40) != 0) &&
103 (rtc_read(0x0a) & 0x20) );
106 /* set alarm time registers to the given time (repeat once per day) */
107 void rtc_set_alarm(int h, int m)
109 unsigned char data;
111 /* for daily alarm, RPT5=RPT4=on, RPT1=RPT2=RPT3=off */
113 rtc_write(0x0e, 0x00); /* seconds 0 and RTP1 */
114 rtc_write(0x0d, DEC2BCD(m)); /* minutes and RPT2 */
115 rtc_write(0x0c, DEC2BCD(h)); /* hour and RPT3 */
116 rtc_write(0x0b, 0xc1); /* set date 01 and RPT4 and RTP5 */
118 /* set month to 1, if it's invalid, the rtc does an alarm every second instead */
119 data = rtc_read(0x0a);
120 data &= 0xe0;
121 data |= 0x01;
122 rtc_write(0x0a, data);
125 /* read out the current alarm time */
126 void rtc_get_alarm(int *h, int *m)
128 unsigned char data;
130 data = rtc_read(0x0c);
131 *h = BCD2DEC(data & 0x3f);
133 data = rtc_read(0x0d);
134 *m = BCD2DEC(data & 0x7f);
137 /* turn alarm on or off by setting the alarm flag enable */
138 /* the alarm is automatically disabled when the RTC gets Vcc power at startup */
139 /* avoid that an alarm occurs when the device is on because this locks the ON key forever */
140 /* returns false if alarm was set and alarm flag (output) is off */
141 /* returns true if alarm flag went on, which would lock the device, so the alarm was disabled again */
142 bool rtc_enable_alarm(bool enable)
144 unsigned char data = rtc_read(0x0a);
145 if (enable)
147 data |= 0xa0; /* turn bit d7=AFE and d5=ABE on */
149 else
150 data &= 0x5f; /* turn bit d7=AFE and d5=ABE off */
151 rtc_write(0x0a, data);
153 /* check if alarm flag AF is off (as it should be) */
154 /* in some cases enabling the alarm results in an activated AF flag */
155 /* this should not happen, but it does */
156 /* if you know why, tell me! */
157 /* for now, we try again forever in this case */
158 while (rtc_check_alarm_flag()) /* on */
160 data &= 0x5f; /* turn bit d7=AFE and d5=ABE off */
161 rtc_write(0x0a, data);
162 sleep(HZ / 10);
163 rtc_check_alarm_flag();
164 data |= 0xa0; /* turn bit d7=AFE and d5=ABE on */
165 rtc_write(0x0a, data);
168 return false; /* all ok */
171 #endif /* HAVE_RTC_ALARM */
173 int rtc_write(unsigned char address, unsigned char value)
175 int ret = 0;
176 unsigned char buf[2];
178 i2c_begin();
180 buf[0] = address;
181 buf[1] = value;
183 /* send run command */
184 if (i2c_write(RTC_DEV_WRITE,buf,2))
186 ret = -1;
189 i2c_end();
190 return ret;
193 int rtc_read(unsigned char address)
195 int value = -1;
196 unsigned char buf[1];
198 i2c_begin();
200 buf[0] = address;
202 /* send read command */
203 if (i2c_write(RTC_DEV_READ,buf,1) >= 0)
205 i2c_start();
206 i2c_outb(RTC_DEV_READ);
207 if (i2c_getack())
209 value = i2c_inb(1);
213 i2c_stop();
215 i2c_end();
216 return value;
219 int rtc_read_multiple(unsigned char address, unsigned char *buf, int numbytes)
221 int ret = 0;
222 unsigned char obuf[1];
223 int i;
225 i2c_begin();
227 obuf[0] = address;
229 /* send read command */
230 if (i2c_write(RTC_DEV_READ, obuf, 1) >= 0)
232 i2c_start();
233 i2c_outb(RTC_DEV_READ);
234 if (i2c_getack())
236 for(i = 0;i < numbytes-1;i++)
237 buf[i] = i2c_inb(0);
239 buf[i] = i2c_inb(1);
241 else
243 ret = -1;
247 i2c_stop();
249 i2c_end();
250 return ret;
253 int rtc_read_datetime(struct tm *tm)
255 int rc;
256 unsigned char buf[7];
258 rc = rtc_read_multiple(1, buf, sizeof(buf));
260 /* convert from bcd, avoid getting extra bits */
261 tm->tm_sec = BCD2DEC(buf[0] & 0x7f);
262 tm->tm_min = BCD2DEC(buf[1] & 0x7f);
263 tm->tm_hour = BCD2DEC(buf[2] & 0x3f);
264 tm->tm_wday = BCD2DEC(buf[3] & 0x3);
265 tm->tm_mday = BCD2DEC(buf[4] & 0x3f);
266 tm->tm_mon = BCD2DEC(buf[5] & 0x1f) - 1;
267 tm->tm_year = BCD2DEC(buf[6]) + 100;
269 /* Adjust weekday */
270 if (tm->tm_wday == 7)
271 tm->tm_wday = 0;
273 return rc;
276 int rtc_write_datetime(const struct tm *tm)
278 unsigned int i;
279 int rc = 0;
280 unsigned char buf[7];
282 buf[0] = tm->tm_sec;
283 buf[1] = tm->tm_min;
284 buf[2] = tm->tm_hour;
285 buf[3] = tm->tm_wday;
286 buf[4] = tm->tm_mday;
287 buf[5] = tm->tm_mon + 1;
288 buf[6] = tm->tm_year - 100;
290 /* Adjust weekday */
291 if (buf[3] == 0)
292 buf[3] = 7;
294 for (i = 0; i < sizeof(buf) ;i++)
296 rc |= rtc_write(i + 1, DEC2BCD(buf[i]));
298 rc |= rtc_write(8, 0x80); /* Out=1, calibration = 0 */
300 return rc;