Rockbox supports not only 1bpp BMPs
[kugel-rb.git] / firmware / drivers / rtc / rtc_mr100.c
blob7ec3996a871d4af82629b74af3c1e3b38f799319
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
21 #include "rtc.h"
22 #include "logf.h"
23 #include "sw_i2c.h"
24 #include "i2c-pp.h"
26 /* The RTC chip is unknown, the information about it was gathered by
27 * reverse engineering the bootloader.
30 #define RTC_ADDR 0x60
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 */
37 /* private */
39 static void reverse_bits(unsigned char* v, int size) {
41 int i,j,in,out=0;
43 for(j=0; j<size; j++) {
44 in = v[j];
45 out = in;
46 for(i=0; i<7; i++) {
47 in = in >>1;
48 out = out<<1;
49 out |= (in & 1);
51 v[j] = out;
55 static int sw_i2c(int access, int cmd, unsigned char* buf, int count) {
56 int i, addr;
58 i2c_lock();
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);
66 } else {
67 reverse_bits(buf, count);
68 i = sw_i2c_write(addr, 0, buf, count);
71 GPIOC_ENABLE &= ~0x00000030;
72 i2c_unlock();
74 return i;
77 /* public */
79 void rtc_init(void)
81 sw_i2c_init();
83 #if 0
84 /* init sequence from OF for reference */
85 /* currently we rely on the bootloader doing it for us */
87 bool flag = true;
88 unsigned char data;
89 unsigned char v[7] = {0x00,0x47,0x17,0x06,0x03,0x02,0x08}; /* random time */
91 if(flag) {
93 GPIOB_ENABLE |= 0x80;
94 GPIOB_OUTPUT_EN |= 0x80;
95 GPIOB_OUTPUT_VAL &= ~0x80;
97 DEV_EN |= 0x1000;
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 */
103 data = 1;
104 sw_i2c(SW_I2C_WRITE, RTC_CMD_CTRL, &data, 1);
106 data = 1;
107 sw_i2c(SW_I2C_WRITE, RTC_CMD_CTRL, &data, 1);
109 data = 8;
110 sw_i2c(SW_I2C_WRITE, RTC_CMD_UNKN, &data, 1);
112 /* more stuff, perhaps set up time array? */
114 rtc_write_datetime(v);
117 data = 2;
118 sw_i2c(SW_I2C_WRITE, RTC_CMD_CTRL, &data, 1);
121 data = 2;
122 sw_i2c(SW_I2C_WRITE, RTC_CMD_CTRL, &data, 1);
123 #endif
127 int rtc_read_datetime(unsigned char* buf)
129 int i;
130 unsigned char v[7];
132 i = sw_i2c(SW_I2C_READ, RTC_CMD_DATA, v, 7);
134 v[4] &= 0x3f; /* mask out p.m. flag */
136 for(i=0; i<7; i++)
137 buf[i] = v[6-i];
139 return i;
142 int rtc_write_datetime(unsigned char* buf)
144 int i;
145 unsigned char v[7];
147 for(i=0; i<7; i++)
148 v[i]=buf[6-i];
150 i = sw_i2c(SW_I2C_WRITE, RTC_CMD_DATA, v, 7);
152 return i;