1 | /* |
---|
2 | * Real Time Clock (Harris ICM7170) for RTEMS |
---|
3 | * |
---|
4 | * This part is found on the second generation of this board. |
---|
5 | * |
---|
6 | * The license and distribution terms for this file may be |
---|
7 | * found in the file LICENSE in this distribution or at |
---|
8 | * http://www.rtems.com/license/LICENSE. |
---|
9 | * |
---|
10 | * $Id$ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <rtems.h> |
---|
14 | #include <tod.h> |
---|
15 | #include <bsp.h> |
---|
16 | |
---|
17 | /* |
---|
18 | * These values are programed into a register and must not be changed. |
---|
19 | */ |
---|
20 | |
---|
21 | #define ICM1770_CRYSTAL_FREQ_32K 0x00 |
---|
22 | #define ICM1770_CRYSTAL_FREQ_1M 0x01 |
---|
23 | #define ICM1770_CRYSTAL_FREQ_2M 0x02 |
---|
24 | #define ICM1770_CRYSTAL_FREQ_4M 0x03 |
---|
25 | |
---|
26 | void ICM7170_GetTOD( |
---|
27 | volatile unsigned char *imc1770_regs, |
---|
28 | uint8_t icm1770_freq, |
---|
29 | rtems_time_of_day *rtc_tod |
---|
30 | ); |
---|
31 | void ICM7170_SetTOD( |
---|
32 | volatile unsigned char *imc1770_regs, |
---|
33 | uint8_t icm1770_freq, |
---|
34 | rtems_time_of_day *rtc_tod |
---|
35 | ); |
---|
36 | |
---|
37 | /* |
---|
38 | * This code is dependent on the Vista 603e's use of the ICM7170 RTC/NVRAM |
---|
39 | * and should remain in this file. |
---|
40 | */ |
---|
41 | |
---|
42 | void setRealTimeToRTEMS() |
---|
43 | { |
---|
44 | rtems_time_of_day rtc_tod; |
---|
45 | |
---|
46 | ICM7170_GetTOD( SCORE603E_RTC_ADDRESS, SCORE_RTC_FREQUENCY, &rtc_tod ); |
---|
47 | rtems_clock_set( &rtc_tod ); |
---|
48 | } |
---|
49 | |
---|
50 | void setRealTimeFromRTEMS() |
---|
51 | { |
---|
52 | rtems_time_of_day rtems_tod; |
---|
53 | |
---|
54 | rtems_clock_get( RTEMS_CLOCK_GET_TOD, &rtems_tod ); |
---|
55 | ICM7170_SetTOD( SCORE603E_RTC_ADDRESS, SCORE_RTC_FREQUENCY, &rtems_tod ); |
---|
56 | } |
---|
57 | |
---|
58 | int checkRealTime() |
---|
59 | { |
---|
60 | rtems_time_of_day rtems_tod; |
---|
61 | rtems_time_of_day rtc_tod; |
---|
62 | |
---|
63 | ICM7170_GetTOD( SCORE603E_RTC_ADDRESS, SCORE_RTC_FREQUENCY, &rtc_tod ); |
---|
64 | rtems_clock_get( RTEMS_CLOCK_GET_TOD, &rtems_tod ); |
---|
65 | |
---|
66 | if( rtems_tod.year == rtc_tod.year && |
---|
67 | rtems_tod.month == rtc_tod.month && |
---|
68 | rtems_tod.day == rtc_tod.day ) { |
---|
69 | return ((rtems_tod.hour - rtc_tod.hour) * 3600) + |
---|
70 | ((rtems_tod.minute - rtc_tod.minute) * 60) + |
---|
71 | (rtems_tod.second - rtc_tod.second); |
---|
72 | } |
---|
73 | return 9999; |
---|
74 | } |
---|
75 | |
---|
76 | /* |
---|
77 | * These routines are ICM7170 should be in |
---|
78 | * a separate support library. |
---|
79 | * XXX Make static |
---|
80 | */ |
---|
81 | static int ICM7170_GetField( |
---|
82 | volatile unsigned char *imc1770_regs, |
---|
83 | int reg |
---|
84 | ) |
---|
85 | { |
---|
86 | unsigned char x; |
---|
87 | |
---|
88 | x = imc1770_regs[reg*4]; |
---|
89 | |
---|
90 | return x; |
---|
91 | } |
---|
92 | |
---|
93 | static void ICM7170_SetField( |
---|
94 | volatile unsigned char *imc1770_regs, |
---|
95 | int reg, |
---|
96 | unsigned char d |
---|
97 | ) |
---|
98 | { |
---|
99 | imc1770_regs[reg*4] = d; |
---|
100 | } |
---|
101 | |
---|
102 | void ICM7170_GetTOD( |
---|
103 | volatile unsigned char *imc1770_regs, |
---|
104 | uint8_t icm1770_freq, |
---|
105 | rtems_time_of_day *rtc_tod |
---|
106 | ) |
---|
107 | { |
---|
108 | int year; |
---|
109 | int usec; |
---|
110 | static rtems_boolean init = TRUE; |
---|
111 | |
---|
112 | /* Initialize the clock at once prior to reading */ |
---|
113 | if (init ) { |
---|
114 | ICM7170_SetField( imc1770_regs, 0x11, (0x0c | icm1770_freq) ); |
---|
115 | init = FALSE; |
---|
116 | } |
---|
117 | |
---|
118 | /* Latch times */ |
---|
119 | /* rtc_tod->ticks = */ |
---|
120 | |
---|
121 | usec = ICM7170_GetField( imc1770_regs, 0x00 ); |
---|
122 | |
---|
123 | year = ICM7170_GetField( imc1770_regs, 0x06 ); |
---|
124 | if ( year >= 88 ) |
---|
125 | year += 1900; |
---|
126 | else |
---|
127 | year += 2000; |
---|
128 | |
---|
129 | rtc_tod->year = year; |
---|
130 | rtc_tod->month = ICM7170_GetField( imc1770_regs, 0x04 ); |
---|
131 | rtc_tod->day = ICM7170_GetField( imc1770_regs, 0x05 ); |
---|
132 | rtc_tod->hour = ICM7170_GetField( imc1770_regs, 0x01 ); |
---|
133 | rtc_tod->minute = ICM7170_GetField( imc1770_regs, 0x02 ); |
---|
134 | rtc_tod->second = ICM7170_GetField( imc1770_regs, 0x03 ); |
---|
135 | rtc_tod->ticks = ICM7170_GetField( imc1770_regs, 0x00 ); |
---|
136 | } |
---|
137 | |
---|
138 | void ICM7170_SetTOD( |
---|
139 | volatile unsigned char *imc1770_regs, |
---|
140 | uint8_t icm1770_freq, |
---|
141 | rtems_time_of_day *rtc_tod |
---|
142 | ) |
---|
143 | { |
---|
144 | int year; |
---|
145 | |
---|
146 | year = rtc_tod->year; |
---|
147 | if ( year >= 2088 ) /* plan ahead :) */ |
---|
148 | rtems_fatal_error_occurred( 0xBAD0BAD0 ); |
---|
149 | |
---|
150 | if ( year >= 2000 ) |
---|
151 | year -= 2000; |
---|
152 | else |
---|
153 | year -= 1900; |
---|
154 | |
---|
155 | ICM7170_SetField( imc1770_regs, 0x11, (0x04 |icm1770_freq ) ); |
---|
156 | |
---|
157 | ICM7170_SetField( imc1770_regs, 0x06, year ); |
---|
158 | ICM7170_SetField( imc1770_regs, 0x04, rtc_tod->month ); |
---|
159 | ICM7170_SetField( imc1770_regs, 0x05, rtc_tod->day ); |
---|
160 | ICM7170_SetField( imc1770_regs, 0x01, rtc_tod->hour ); |
---|
161 | ICM7170_SetField( imc1770_regs, 0x02, rtc_tod->minute ); |
---|
162 | ICM7170_SetField( imc1770_regs, 0x03, rtc_tod->second ); |
---|
163 | |
---|
164 | /* |
---|
165 | * I don't know which day of week is |
---|
166 | * |
---|
167 | */ |
---|
168 | ICM7170_SetField( imc1770_regs, 0x07, 1 ); |
---|
169 | |
---|
170 | ICM7170_SetField( imc1770_regs, 0x11, (0x0c | icm1770_freq) ); |
---|
171 | } |
---|