1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2008 by Robert Kukla
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 ****************************************************************************/
26 /* The RTC chip is unknown, the information about it was gathered by
27 * reverse engineering the bootloader.
32 #define RTC_CMD_CTRL 0 /* OF uses it with single byte 1 or 2 */
33 #define RTC_CMD_UNKN 1 /* OF uses it with single byte 8 */
34 #define RTC_CMD_DATA 2
35 #define RTC_CMD_TEST 7 /* OF uses it with single byte 0xAA */
39 static void reverse_bits(unsigned char* v
, int size
) {
43 for(j
=0; j
<size
; j
++) {
55 static int sw_i2c(int access
, int cmd
, unsigned char* buf
, int count
) {
59 GPIOC_ENABLE
|= 0x00000030;
61 addr
= RTC_ADDR
| (cmd
<<1);
63 if(access
== SW_I2C_READ
) {
64 i
= sw_i2c_read(addr
, 0, buf
, count
);
65 reverse_bits(buf
, count
);
67 reverse_bits(buf
, count
);
68 i
= sw_i2c_write(addr
, 0, buf
, count
);
71 GPIOC_ENABLE
&= ~0x00000030;
84 /* init sequence from OF for reference */
85 /* currently we rely on the bootloader doing it for us */
89 unsigned char v
[7] = {0x00,0x47,0x17,0x06,0x03,0x02,0x08}; /* random time */
94 GPIOB_OUTPUT_EN
|= 0x80;
95 GPIOB_OUTPUT_VAL
&= ~0x80;
98 /* some more stuff that is not clear */
100 sw_i2c(SW_I2C_READ
, RTC_CMD_CTRL
, &data
, 1);
101 if((data
<<0x18)>>0x1e) { /* bit 7 & 6 */
104 sw_i2c(SW_I2C_WRITE
, RTC_CMD_CTRL
, &data
, 1);
107 sw_i2c(SW_I2C_WRITE
, RTC_CMD_CTRL
, &data
, 1);
110 sw_i2c(SW_I2C_WRITE
, RTC_CMD_UNKN
, &data
, 1);
112 /* more stuff, perhaps set up time array? */
114 rtc_write_datetime(v
);
118 sw_i2c(SW_I2C_WRITE
, RTC_CMD_CTRL
, &data
, 1);
122 sw_i2c(SW_I2C_WRITE
, RTC_CMD_CTRL
, &data
, 1);
127 int rtc_read_datetime(struct tm
*tm
)
131 unsigned char buf
[7];
133 rc
= sw_i2c(SW_I2C_READ
, RTC_CMD_DATA
, buf
, sizeof(buf
));
135 buf
[4] &= 0x3f; /* mask out p.m. flag */
137 for (i
= 0; i
< sizeof(buf
); i
++)
138 buf
[i
] = BCD2DEC(buf
[i
]);
142 tm
->tm_hour
= buf
[4];
143 tm
->tm_wday
= buf
[3];
144 tm
->tm_mday
= buf
[2];
145 tm
->tm_mon
= buf
[1] - 1;
146 tm
->tm_year
= buf
[0] + 100;
151 int rtc_write_datetime(const struct tm
*tm
)
155 unsigned char buf
[7];
159 buf
[4] = tm
->tm_hour
;
160 buf
[3] = tm
->tm_wday
;
161 buf
[2] = tm
->tm_mday
;
162 buf
[1] = tm
->tm_mon
+ 1;
163 buf
[0] = tm
->tm_year
- 100;
165 for (i
= 0; i
< sizeof(buf
); i
++)
166 buf
[i
] = DEC2BCD(buf
[i
]);
168 rc
= sw_i2c(SW_I2C_WRITE
, RTC_CMD_DATA
, buf
, sizeof(buf
));