1 | /* |
---|
2 | * Real Time Clock Driver Wrapper for Libchip |
---|
3 | * |
---|
4 | * The license and distribution terms for this file may be |
---|
5 | * found in the file LICENSE in this distribution or at |
---|
6 | * http://www.OARcorp.com/rtems/license.html. |
---|
7 | * |
---|
8 | * $Id$ |
---|
9 | */ |
---|
10 | |
---|
11 | #include <rtems.h> |
---|
12 | #include <libchip/rtc.h> |
---|
13 | |
---|
14 | /* |
---|
15 | * Configuration Information |
---|
16 | */ |
---|
17 | |
---|
18 | extern unsigned long RTC_Count; |
---|
19 | extern rtems_device_minor_number RTC_Minor; |
---|
20 | |
---|
21 | int RTC_Present; |
---|
22 | |
---|
23 | /* |
---|
24 | * rtc_initialize |
---|
25 | * |
---|
26 | * Initialize the RTC driver |
---|
27 | */ |
---|
28 | |
---|
29 | rtems_device_driver rtc_initialize( |
---|
30 | rtems_device_major_number major, |
---|
31 | rtems_device_minor_number minor_arg, |
---|
32 | void *arg |
---|
33 | ) |
---|
34 | { |
---|
35 | rtems_device_minor_number minor; |
---|
36 | rtems_status_code status; |
---|
37 | |
---|
38 | for (minor=0; minor < RTC_Count ; minor++) { |
---|
39 | /* |
---|
40 | * First perform the configuration dependent probe, then the |
---|
41 | * device dependent probe |
---|
42 | */ |
---|
43 | |
---|
44 | if (RTC_Table[minor].deviceProbe && RTC_Table[minor].deviceProbe(minor)) { |
---|
45 | /* |
---|
46 | * Use this device as the primary RTC |
---|
47 | */ |
---|
48 | RTC_Minor = minor; |
---|
49 | RTC_Present = 1; |
---|
50 | break; |
---|
51 | } |
---|
52 | } |
---|
53 | |
---|
54 | if ( !RTC_Present ) { |
---|
55 | /* |
---|
56 | * Failed to find an RTC -- this is not a fatal error. |
---|
57 | */ |
---|
58 | |
---|
59 | return RTEMS_INVALID_NUMBER; |
---|
60 | } |
---|
61 | |
---|
62 | /* |
---|
63 | * Register and initialize the primary RTC's |
---|
64 | */ |
---|
65 | |
---|
66 | status = rtems_io_register_name( "/dev/rtc", major, RTC_Minor ); |
---|
67 | if (status != RTEMS_SUCCESSFUL) { |
---|
68 | rtems_fatal_error_occurred(status); |
---|
69 | } |
---|
70 | |
---|
71 | RTC_Table[minor].pDeviceFns->deviceInitialize( RTC_Minor ); |
---|
72 | |
---|
73 | /* |
---|
74 | * Now initialize any secondary RTC's |
---|
75 | */ |
---|
76 | |
---|
77 | for ( minor++ ; minor<RTC_Count ; minor++) { |
---|
78 | /* |
---|
79 | * First perform the configuration dependent probe, then the |
---|
80 | * device dependent probe |
---|
81 | */ |
---|
82 | |
---|
83 | if (RTC_Table[minor].deviceProbe && RTC_Table[minor].deviceProbe(minor)) { |
---|
84 | status = rtems_io_register_name( |
---|
85 | RTC_Table[minor].sDeviceName, |
---|
86 | major, |
---|
87 | minor ); |
---|
88 | if (status != RTEMS_SUCCESSFUL) { |
---|
89 | rtems_fatal_error_occurred(status); |
---|
90 | } |
---|
91 | |
---|
92 | /* |
---|
93 | * Initialize the hardware device. |
---|
94 | */ |
---|
95 | |
---|
96 | RTC_Table[minor].pDeviceFns->deviceInitialize(minor); |
---|
97 | |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | return RTEMS_SUCCESSFUL; |
---|
102 | } |
---|
103 | |
---|
104 | |
---|
105 | /*PAGE |
---|
106 | * |
---|
107 | * This routine copies the time from the real time clock to RTEMS |
---|
108 | * |
---|
109 | * Input parameters: NONE |
---|
110 | * |
---|
111 | * Output parameters: NONE |
---|
112 | * |
---|
113 | * Return values: NONE |
---|
114 | */ |
---|
115 | |
---|
116 | void setRealTimeToRTEMS() |
---|
117 | { |
---|
118 | rtems_time_of_day rtc_tod; |
---|
119 | |
---|
120 | if (!RTC_Present) |
---|
121 | return; |
---|
122 | |
---|
123 | RTC_Table[RTC_Minor].pDeviceFns->deviceGetTime(RTC_Minor, &rtc_tod); |
---|
124 | rtems_clock_set( &rtc_tod ); |
---|
125 | } |
---|
126 | |
---|
127 | /*PAGE |
---|
128 | * |
---|
129 | * setRealTimeFromRTEMS |
---|
130 | * |
---|
131 | * This routine copies the time from RTEMS to the real time clock |
---|
132 | * |
---|
133 | * Input parameters: NONE |
---|
134 | * |
---|
135 | * Output parameters: NONE |
---|
136 | * |
---|
137 | * Return values: NONE |
---|
138 | */ |
---|
139 | |
---|
140 | void setRealTimeFromRTEMS() |
---|
141 | { |
---|
142 | rtems_time_of_day rtems_tod; |
---|
143 | |
---|
144 | if (!RTC_Present) |
---|
145 | return; |
---|
146 | |
---|
147 | rtems_clock_get( RTEMS_CLOCK_GET_TOD, &rtems_tod ); |
---|
148 | RTC_Table[RTC_Minor].pDeviceFns->deviceSetTime(RTC_Minor, &rtems_tod); |
---|
149 | } |
---|
150 | |
---|
151 | /*PAGE |
---|
152 | * |
---|
153 | * getRealTime |
---|
154 | * |
---|
155 | * This routine reads the current time from the RTC. |
---|
156 | * |
---|
157 | * Input parameters: NONE |
---|
158 | * |
---|
159 | * Output parameters: NONE |
---|
160 | * |
---|
161 | * Return values: NONE |
---|
162 | */ |
---|
163 | |
---|
164 | void getRealTime( |
---|
165 | rtems_time_of_day *tod |
---|
166 | ) |
---|
167 | { |
---|
168 | |
---|
169 | if (!RTC_Present) |
---|
170 | return; |
---|
171 | |
---|
172 | RTC_Table[RTC_Minor].pDeviceFns->deviceGetTime(RTC_Minor, tod); |
---|
173 | } |
---|
174 | |
---|
175 | /*PAGE |
---|
176 | * |
---|
177 | * setRealTime |
---|
178 | * |
---|
179 | * This routine sets the RTC. |
---|
180 | * |
---|
181 | * Input parameters: NONE |
---|
182 | * |
---|
183 | * Output parameters: NONE |
---|
184 | * |
---|
185 | * Return values: NONE |
---|
186 | */ |
---|
187 | |
---|
188 | /* XXX this routine should be part of the public RTEMS interface */ |
---|
189 | rtems_boolean _TOD_Validate( rtems_time_of_day *tod ); |
---|
190 | |
---|
191 | int setRealTime( |
---|
192 | rtems_time_of_day *tod |
---|
193 | ) |
---|
194 | { |
---|
195 | |
---|
196 | if (!RTC_Present) |
---|
197 | return -1; |
---|
198 | |
---|
199 | if ( !_TOD_Validate(tod) ) |
---|
200 | return -1; |
---|
201 | |
---|
202 | RTC_Table[RTC_Minor].pDeviceFns->deviceSetTime(RTC_Minor, tod); |
---|
203 | return 0; |
---|
204 | } |
---|
205 | |
---|
206 | |
---|
207 | /*PAGE |
---|
208 | * |
---|
209 | * checkRealTime |
---|
210 | * |
---|
211 | * This routine reads the returns the variance betweent the real time and |
---|
212 | * rtems time. |
---|
213 | * |
---|
214 | * Input parameters: NONE |
---|
215 | * |
---|
216 | * Output parameters: NONE |
---|
217 | * |
---|
218 | * Return values: |
---|
219 | * int The differance between the real time clock and rtems time. |
---|
220 | */ |
---|
221 | |
---|
222 | /* XXX this routine should be part of the public RTEMS interface */ |
---|
223 | unsigned32 _TOD_To_seconds( rtems_time_of_day *tod ); |
---|
224 | |
---|
225 | int checkRealTime() |
---|
226 | { |
---|
227 | rtems_time_of_day rtems_tod; |
---|
228 | rtems_time_of_day rtc_tod; |
---|
229 | unsigned32 rtems_time; |
---|
230 | unsigned32 rtc_time; |
---|
231 | |
---|
232 | if (!RTC_Present) |
---|
233 | return -1; |
---|
234 | |
---|
235 | rtems_clock_get( RTEMS_CLOCK_GET_TOD, &rtems_tod ); |
---|
236 | RTC_Table[RTC_Minor].pDeviceFns->deviceGetTime(RTC_Minor, &rtc_tod); |
---|
237 | |
---|
238 | rtems_time = _TOD_To_seconds( &rtems_tod ); |
---|
239 | rtc_time = _TOD_To_seconds( &rtc_tod ); |
---|
240 | |
---|
241 | return rtems_time - rtc_time; |
---|
242 | } |
---|
243 | |
---|