1 | /* |
---|
2 | * Real Time Clock Driver Wrapper for Libchip |
---|
3 | */ |
---|
4 | |
---|
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.org/license/LICENSE. |
---|
9 | */ |
---|
10 | |
---|
11 | #include <rtems.h> |
---|
12 | #include <rtems/rtc.h> |
---|
13 | #include <rtems/tod.h> |
---|
14 | #include <rtems/libio.h> |
---|
15 | |
---|
16 | #include <libchip/rtc.h> |
---|
17 | |
---|
18 | /* |
---|
19 | * Configuration Information |
---|
20 | */ |
---|
21 | extern size_t RTC_Count; |
---|
22 | extern rtems_device_minor_number RTC_Minor; |
---|
23 | |
---|
24 | int RTC_Present; |
---|
25 | |
---|
26 | extern void setRealTimeToRTEMS(void); |
---|
27 | |
---|
28 | /* |
---|
29 | * rtc_initialize |
---|
30 | * |
---|
31 | * Initialize the RTC driver |
---|
32 | */ |
---|
33 | rtems_device_driver rtc_initialize( |
---|
34 | rtems_device_major_number major, |
---|
35 | rtems_device_minor_number minor_arg, |
---|
36 | void *arg |
---|
37 | ) |
---|
38 | { |
---|
39 | rtems_device_minor_number minor; |
---|
40 | rtems_status_code status; |
---|
41 | |
---|
42 | for (minor=0; minor < RTC_Count ; minor++) { |
---|
43 | /* |
---|
44 | * First perform the configuration dependent probe, then the |
---|
45 | * device dependent probe |
---|
46 | */ |
---|
47 | |
---|
48 | if (RTC_Table[minor].deviceProbe && RTC_Table[minor].deviceProbe(minor)) { |
---|
49 | /* |
---|
50 | * Use this device as the primary RTC |
---|
51 | */ |
---|
52 | RTC_Minor = minor; |
---|
53 | RTC_Present = 1; |
---|
54 | break; |
---|
55 | } |
---|
56 | } |
---|
57 | |
---|
58 | if ( !RTC_Present ) { |
---|
59 | /* |
---|
60 | * Failed to find an RTC -- this is not a fatal error. |
---|
61 | */ |
---|
62 | |
---|
63 | return RTEMS_INVALID_NUMBER; |
---|
64 | } |
---|
65 | |
---|
66 | /* |
---|
67 | * Register and initialize the primary RTC's |
---|
68 | */ |
---|
69 | status = rtems_io_register_name( RTC_DEVICE_NAME, major, RTC_Minor ); |
---|
70 | if (status != RTEMS_SUCCESSFUL) { |
---|
71 | rtems_fatal_error_occurred(status); |
---|
72 | } |
---|
73 | |
---|
74 | RTC_Table[minor].pDeviceFns->deviceInitialize( RTC_Minor ); |
---|
75 | |
---|
76 | /* |
---|
77 | * Now initialize any secondary RTC's |
---|
78 | */ |
---|
79 | for ( minor++ ; minor<RTC_Count ; minor++) { |
---|
80 | /* |
---|
81 | * First perform the configuration dependent probe, then the |
---|
82 | * device dependent probe |
---|
83 | */ |
---|
84 | |
---|
85 | if (RTC_Table[minor].deviceProbe && RTC_Table[minor].deviceProbe(minor)) { |
---|
86 | status = rtems_io_register_name( |
---|
87 | RTC_Table[minor].sDeviceName, |
---|
88 | major, |
---|
89 | minor ); |
---|
90 | if (status != RTEMS_SUCCESSFUL) { |
---|
91 | rtems_fatal_error_occurred(status); |
---|
92 | } |
---|
93 | |
---|
94 | /* |
---|
95 | * Initialize the hardware device. |
---|
96 | */ |
---|
97 | RTC_Table[minor].pDeviceFns->deviceInitialize(minor); |
---|
98 | |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | setRealTimeToRTEMS(); |
---|
103 | return RTEMS_SUCCESSFUL; |
---|
104 | } |
---|
105 | |
---|
106 | rtems_device_driver rtc_read( |
---|
107 | rtems_device_major_number major, |
---|
108 | rtems_device_minor_number minor, |
---|
109 | void *arg |
---|
110 | ) |
---|
111 | { |
---|
112 | int rv = 0; |
---|
113 | rtems_libio_rw_args_t *rw = arg; |
---|
114 | rtems_time_of_day *tod = (rtems_time_of_day *) rw->buffer; |
---|
115 | |
---|
116 | rw->offset = 0; |
---|
117 | rw->bytes_moved = 0; |
---|
118 | |
---|
119 | if (!RTC_Present) { |
---|
120 | return RTEMS_NOT_CONFIGURED; |
---|
121 | } |
---|
122 | |
---|
123 | if (rw->count != sizeof( rtems_time_of_day)) { |
---|
124 | return RTEMS_INVALID_SIZE; |
---|
125 | } |
---|
126 | |
---|
127 | rv = RTC_Table [RTC_Minor].pDeviceFns->deviceGetTime( |
---|
128 | RTC_Minor, |
---|
129 | tod |
---|
130 | ); |
---|
131 | if (rv != 0) { |
---|
132 | return RTEMS_IO_ERROR; |
---|
133 | } |
---|
134 | |
---|
135 | rw->bytes_moved = rw->count; |
---|
136 | |
---|
137 | return RTEMS_SUCCESSFUL; |
---|
138 | } |
---|
139 | |
---|
140 | rtems_device_driver rtc_write( |
---|
141 | rtems_device_major_number major, |
---|
142 | rtems_device_minor_number minor, |
---|
143 | void *arg |
---|
144 | ) |
---|
145 | { |
---|
146 | int rv = 0; |
---|
147 | rtems_libio_rw_args_t *rw = arg; |
---|
148 | const rtems_time_of_day *tod = (const rtems_time_of_day *) rw->buffer; |
---|
149 | |
---|
150 | rw->offset = 0; |
---|
151 | rw->bytes_moved = 0; |
---|
152 | |
---|
153 | if (!RTC_Present) { |
---|
154 | return RTEMS_NOT_CONFIGURED; |
---|
155 | } |
---|
156 | |
---|
157 | if (rw->count != sizeof( rtems_time_of_day)) { |
---|
158 | return RTEMS_INVALID_SIZE; |
---|
159 | } |
---|
160 | |
---|
161 | rv = RTC_Table [RTC_Minor].pDeviceFns->deviceSetTime( |
---|
162 | RTC_Minor, |
---|
163 | tod |
---|
164 | ); |
---|
165 | if (rv != 0) { |
---|
166 | return RTEMS_IO_ERROR; |
---|
167 | } |
---|
168 | |
---|
169 | rw->bytes_moved = rw->count; |
---|
170 | |
---|
171 | return RTEMS_SUCCESSFUL; |
---|
172 | } |
---|
173 | |
---|
174 | rtems_device_driver rtc_open( |
---|
175 | rtems_device_major_number major, |
---|
176 | rtems_device_minor_number minor, |
---|
177 | void *arg |
---|
178 | ) |
---|
179 | { |
---|
180 | return RTEMS_SUCCESSFUL; |
---|
181 | } |
---|
182 | |
---|
183 | rtems_device_driver rtc_close( |
---|
184 | rtems_device_major_number major, |
---|
185 | rtems_device_minor_number minor, |
---|
186 | void *arg |
---|
187 | ) |
---|
188 | { |
---|
189 | return RTEMS_SUCCESSFUL; |
---|
190 | } |
---|
191 | |
---|
192 | rtems_device_driver rtc_control( |
---|
193 | rtems_device_major_number major, |
---|
194 | rtems_device_minor_number minor, |
---|
195 | void *arg |
---|
196 | ) |
---|
197 | { |
---|
198 | return RTEMS_NOT_IMPLEMENTED; |
---|
199 | } |
---|
200 | |
---|
201 | /* |
---|
202 | * This routine copies the time from the real time clock to RTEMS |
---|
203 | */ |
---|
204 | void setRealTimeToRTEMS() |
---|
205 | { |
---|
206 | rtems_time_of_day rtc_tod; |
---|
207 | |
---|
208 | if (!RTC_Present) |
---|
209 | return; |
---|
210 | |
---|
211 | RTC_Table[RTC_Minor].pDeviceFns->deviceGetTime(RTC_Minor, &rtc_tod); |
---|
212 | rtems_clock_set( &rtc_tod ); |
---|
213 | } |
---|
214 | |
---|
215 | /* |
---|
216 | * setRealTimeFromRTEMS |
---|
217 | * |
---|
218 | * This routine copies the time from RTEMS to the real time clock |
---|
219 | */ |
---|
220 | void setRealTimeFromRTEMS(void) |
---|
221 | { |
---|
222 | rtems_time_of_day rtems_tod; |
---|
223 | |
---|
224 | if (!RTC_Present) |
---|
225 | return; |
---|
226 | |
---|
227 | rtems_clock_get_tod( &rtems_tod ); |
---|
228 | RTC_Table[RTC_Minor].pDeviceFns->deviceSetTime(RTC_Minor, &rtems_tod); |
---|
229 | } |
---|
230 | |
---|
231 | /* |
---|
232 | * getRealTime |
---|
233 | * |
---|
234 | * This routine reads the current time from the RTC. |
---|
235 | */ |
---|
236 | void getRealTime( |
---|
237 | rtems_time_of_day *tod |
---|
238 | ) |
---|
239 | { |
---|
240 | if (!RTC_Present) |
---|
241 | return; |
---|
242 | |
---|
243 | RTC_Table[RTC_Minor].pDeviceFns->deviceGetTime(RTC_Minor, tod); |
---|
244 | } |
---|
245 | |
---|
246 | /* |
---|
247 | * setRealTime |
---|
248 | * |
---|
249 | * This routine sets the RTC. |
---|
250 | */ |
---|
251 | int setRealTime( |
---|
252 | const rtems_time_of_day *tod |
---|
253 | ) |
---|
254 | { |
---|
255 | if (!RTC_Present) |
---|
256 | return -1; |
---|
257 | |
---|
258 | if ( !_TOD_Validate(tod) ) |
---|
259 | return -1; |
---|
260 | |
---|
261 | RTC_Table[RTC_Minor].pDeviceFns->deviceSetTime(RTC_Minor, tod); |
---|
262 | return 0; |
---|
263 | } |
---|
264 | |
---|
265 | /* |
---|
266 | * checkRealTime |
---|
267 | * |
---|
268 | * This routine reads the returns the variance betweent the real time and |
---|
269 | * RTEMS time. |
---|
270 | */ |
---|
271 | int checkRealTime(void) |
---|
272 | { |
---|
273 | rtems_time_of_day rtems_tod; |
---|
274 | rtems_time_of_day rtc_tod; |
---|
275 | uint32_t rtems_time; |
---|
276 | uint32_t rtc_time; |
---|
277 | |
---|
278 | if (!RTC_Present) |
---|
279 | return -1; |
---|
280 | |
---|
281 | rtems_clock_get_tod( &rtems_tod ); |
---|
282 | RTC_Table[RTC_Minor].pDeviceFns->deviceGetTime(RTC_Minor, &rtc_tod); |
---|
283 | |
---|
284 | rtems_time = _TOD_To_seconds( &rtems_tod ); |
---|
285 | rtc_time = _TOD_To_seconds( &rtc_tod ); |
---|
286 | |
---|
287 | return rtems_time - rtc_time; |
---|
288 | } |
---|