diff options
Diffstat (limited to 'firmware/drivers/rtc/rtc_mr100.c')
-rw-r--r-- | firmware/drivers/rtc/rtc_mr100.c | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/firmware/drivers/rtc/rtc_mr100.c b/firmware/drivers/rtc/rtc_mr100.c new file mode 100644 index 0000000000..96d84156a7 --- /dev/null +++ b/firmware/drivers/rtc/rtc_mr100.c | |||
@@ -0,0 +1,114 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2008 by Robert Kukla | ||
11 | * | ||
12 | * All files in this archive are subject to the GNU General Public License. | ||
13 | * See the file COPYING in the source tree root for full license agreement. | ||
14 | * | ||
15 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
16 | * KIND, either express or implied. | ||
17 | * | ||
18 | ****************************************************************************/ | ||
19 | #include "rtc.h" | ||
20 | #include "logf.h" | ||
21 | #include "sw_i2c.h" | ||
22 | #include "i2c-pp.h" | ||
23 | |||
24 | /* The RTC chip is unknown, the information about it was gathered by | ||
25 | * reverse engineering the bootloader. | ||
26 | */ | ||
27 | |||
28 | #define RTC_ADDR 0x60 | ||
29 | |||
30 | #define RTC_CMD_CTRL 0 /* OF uses it with single byte 1 or 2 */ | ||
31 | #define RTC_CMD_UNKN 1 /* OF uses it with single byte 8 */ | ||
32 | #define RTC_CMD_DATA 2 | ||
33 | #define RTC_CMD_TEST 7 /* OF uses it with single byte 0xAA */ | ||
34 | |||
35 | /* private */ | ||
36 | |||
37 | static void reverse_bits(unsigned char* v, int size) { | ||
38 | |||
39 | int i,j,in,out=0; | ||
40 | |||
41 | for(j=0; j<size; j++) { | ||
42 | in = v[j]; | ||
43 | for(i=0; i<8; i++) { | ||
44 | out |= (in & 1); | ||
45 | in = in >>1; | ||
46 | out = out<<1; | ||
47 | } | ||
48 | v[j] = out>>1; | ||
49 | } | ||
50 | } | ||
51 | |||
52 | static int sw_i2c(int access, unsigned char chip, unsigned char cmd, | ||
53 | unsigned char* buf, int count) { | ||
54 | int i; | ||
55 | |||
56 | i2c_lock(); | ||
57 | GPIOC_ENABLE |= 0x00000030; | ||
58 | |||
59 | chip|=cmd<<1; | ||
60 | |||
61 | if(access == SW_I2C_READ) { | ||
62 | i = sw_i2c_read(chip, 0, buf, count); | ||
63 | reverse_bits(buf, count); | ||
64 | } else { | ||
65 | reverse_bits(buf, count); | ||
66 | i = sw_i2c_write(chip, 0, buf, count); | ||
67 | } | ||
68 | |||
69 | GPIOC_ENABLE &= ~0x00000030; | ||
70 | i2c_unlock(); | ||
71 | |||
72 | return i; | ||
73 | } | ||
74 | |||
75 | /* public */ | ||
76 | |||
77 | void rtc_init(void) | ||
78 | { | ||
79 | sw_i2c_init(); | ||
80 | |||
81 | /* to set a time while buttons are stil not working | ||
82 | unsigned char v[7] = {0x00,0x47,0x17,0x06,0x03,0x02,0x08}; | ||
83 | rtc_write_datetime(v); | ||
84 | */ | ||
85 | } | ||
86 | |||
87 | int rtc_read_datetime(unsigned char* buf) | ||
88 | { | ||
89 | int i; | ||
90 | unsigned char v[7]; | ||
91 | |||
92 | i = sw_i2c(SW_I2C_READ, RTC_ADDR, RTC_CMD_DATA, v, 7); | ||
93 | |||
94 | v[4] &= 0x3f; /* mask out p.m. flag */ | ||
95 | |||
96 | for(i=0; i<7; i++) | ||
97 | buf[i] = v[6-i]; | ||
98 | |||
99 | return i; | ||
100 | } | ||
101 | |||
102 | int rtc_write_datetime(unsigned char* buf) | ||
103 | { | ||
104 | int i; | ||
105 | unsigned char v[7]; | ||
106 | |||
107 | for(i=0; i<7; i++) | ||
108 | v[i]=buf[6-i]; | ||
109 | |||
110 | i = sw_i2c(SW_I2C_WRITE, RTC_ADDR, RTC_CMD_DATA, v, 7); | ||
111 | |||
112 | return i; | ||
113 | } | ||
114 | |||