Correct beast manual install instructions in Windows.
[kugel-rb.git] / firmware / drivers / rtc / rtc_mr100.c
blob209845cea82b40612e91045e153b2b9593a75deb
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(struct tm *tm)
129 unsigned int i;
130 int rc;
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]);
140 tm->tm_sec = buf[6];
141 tm->tm_min = buf[5];
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;
148 return rc;
151 int rtc_write_datetime(const struct tm *tm)
153 unsigned int i;
154 int rc;
155 unsigned char buf[7];
157 buf[6] = tm->tm_sec;
158 buf[5] = tm->tm_min;
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));
170 return rc;