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(unsigned char* buf
)
132 i
= sw_i2c(SW_I2C_READ
, RTC_CMD_DATA
, v
, 7);
134 v
[4] &= 0x3f; /* mask out p.m. flag */
142 int rtc_write_datetime(unsigned char* buf
)
150 i
= sw_i2c(SW_I2C_WRITE
, RTC_CMD_DATA
, v
, 7);