1 | /*
|
---|
2 | * Copyright (c) 2012 Maurizio Lombardi
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * @addtogroup cmos-rtc
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 |
|
---|
34 | /** @file
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <errno.h>
|
---|
38 | #include <ddi.h>
|
---|
39 | #include <as.h>
|
---|
40 | #include <barrier.h>
|
---|
41 | #include <stdio.h>
|
---|
42 | #include <ddf/driver.h>
|
---|
43 | #include <ddf/log.h>
|
---|
44 | #include <ops/clock_dev.h>
|
---|
45 | #include <ops/battery_dev.h>
|
---|
46 | #include <fibril_synch.h>
|
---|
47 | #include <device/hw_res.h>
|
---|
48 | #include <macros.h>
|
---|
49 | #include <time.h>
|
---|
50 |
|
---|
51 | #include "cmos-regs.h"
|
---|
52 |
|
---|
53 | #define NAME "cmos-rtc"
|
---|
54 |
|
---|
55 | #define REG_COUNT 2
|
---|
56 |
|
---|
57 | #define REG_SEL_PORT(port) (port)
|
---|
58 | #define REG_RW_PORT(port) ((port) + 1)
|
---|
59 |
|
---|
60 | typedef struct rtc {
|
---|
61 | /** DDF device node */
|
---|
62 | ddf_dev_t *dev;
|
---|
63 | /** DDF function node */
|
---|
64 | ddf_fun_t *fun;
|
---|
65 | /** The fibril mutex for synchronizing the access to the device */
|
---|
66 | fibril_mutex_t mutex;
|
---|
67 | /** The base I/O address of the device registers */
|
---|
68 | ioport8_t *io_addr;
|
---|
69 | /** The I/O port used to access the CMOS registers */
|
---|
70 | ioport8_t *port;
|
---|
71 | /** true if device is removed */
|
---|
72 | bool removed;
|
---|
73 | /** number of connected clients */
|
---|
74 | int clients_connected;
|
---|
75 | /** time at which the system booted */
|
---|
76 | struct timespec boot_time;
|
---|
77 | } rtc_t;
|
---|
78 |
|
---|
79 | static rtc_t *dev_rtc(ddf_dev_t *dev);
|
---|
80 | static rtc_t *fun_rtc(ddf_fun_t *fun);
|
---|
81 | static errno_t
|
---|
82 | rtc_battery_status_get(ddf_fun_t *fun, battery_status_t *status);
|
---|
83 | static errno_t rtc_time_get(ddf_fun_t *fun, struct tm *t);
|
---|
84 | static errno_t rtc_time_set(ddf_fun_t *fun, struct tm *t);
|
---|
85 | static errno_t rtc_dev_add(ddf_dev_t *dev);
|
---|
86 | static errno_t rtc_dev_initialize(rtc_t *rtc);
|
---|
87 | static bool rtc_pio_enable(rtc_t *rtc);
|
---|
88 | static void rtc_dev_cleanup(rtc_t *rtc);
|
---|
89 | static errno_t rtc_open(ddf_fun_t *fun);
|
---|
90 | static void rtc_close(ddf_fun_t *fun);
|
---|
91 | static bool rtc_update_in_progress(rtc_t *rtc);
|
---|
92 | static int rtc_register_read(rtc_t *rtc, int reg);
|
---|
93 | static unsigned bcd2bin(unsigned bcd);
|
---|
94 | static unsigned bin2bcd(unsigned binary);
|
---|
95 | static errno_t rtc_dev_remove(ddf_dev_t *dev);
|
---|
96 | static void rtc_register_write(rtc_t *rtc, int reg, int data);
|
---|
97 | static bool is_battery_ok(rtc_t *rtc);
|
---|
98 | static errno_t rtc_fun_online(ddf_fun_t *fun);
|
---|
99 | static errno_t rtc_fun_offline(ddf_fun_t *fun);
|
---|
100 |
|
---|
101 | static ddf_dev_ops_t rtc_dev_ops;
|
---|
102 |
|
---|
103 | /** The RTC device driver's standard operations */
|
---|
104 | static driver_ops_t rtc_ops = {
|
---|
105 | .dev_add = rtc_dev_add,
|
---|
106 | .dev_remove = rtc_dev_remove,
|
---|
107 | .fun_online = rtc_fun_online,
|
---|
108 | .fun_offline = rtc_fun_offline,
|
---|
109 | };
|
---|
110 |
|
---|
111 | /** The RTC device driver structure */
|
---|
112 | static driver_t rtc_driver = {
|
---|
113 | .name = NAME,
|
---|
114 | .driver_ops = &rtc_ops,
|
---|
115 | };
|
---|
116 |
|
---|
117 | /** Clock interface */
|
---|
118 | static clock_dev_ops_t rtc_clock_dev_ops = {
|
---|
119 | .time_get = rtc_time_get,
|
---|
120 | .time_set = rtc_time_set,
|
---|
121 | };
|
---|
122 |
|
---|
123 | /** Battery powered device interface */
|
---|
124 | static battery_dev_ops_t rtc_battery_dev_ops = {
|
---|
125 | .battery_status_get = rtc_battery_status_get,
|
---|
126 | .battery_charge_level_get = NULL,
|
---|
127 | };
|
---|
128 |
|
---|
129 | /** Obtain soft state structure from device node */
|
---|
130 | static rtc_t *
|
---|
131 | dev_rtc(ddf_dev_t *dev)
|
---|
132 | {
|
---|
133 | return ddf_dev_data_get(dev);
|
---|
134 | }
|
---|
135 |
|
---|
136 | /** Obtain soft state structure from function node */
|
---|
137 | static rtc_t *
|
---|
138 | fun_rtc(ddf_fun_t *fun)
|
---|
139 | {
|
---|
140 | return dev_rtc(ddf_fun_get_dev(fun));
|
---|
141 | }
|
---|
142 |
|
---|
143 | /** Initialize the RTC driver */
|
---|
144 | static void
|
---|
145 | rtc_init(void)
|
---|
146 | {
|
---|
147 | ddf_log_init(NAME);
|
---|
148 |
|
---|
149 | rtc_dev_ops.open = rtc_open;
|
---|
150 | rtc_dev_ops.close = rtc_close;
|
---|
151 |
|
---|
152 | rtc_dev_ops.interfaces[CLOCK_DEV_IFACE] = &rtc_clock_dev_ops;
|
---|
153 | rtc_dev_ops.interfaces[BATTERY_DEV_IFACE] = &rtc_battery_dev_ops;
|
---|
154 | rtc_dev_ops.default_handler = NULL;
|
---|
155 | }
|
---|
156 |
|
---|
157 | /** Clean up the RTC soft state
|
---|
158 | *
|
---|
159 | * @param rtc The RTC device
|
---|
160 | */
|
---|
161 | static void
|
---|
162 | rtc_dev_cleanup(rtc_t *rtc)
|
---|
163 | {
|
---|
164 | }
|
---|
165 |
|
---|
166 | /** Enable the I/O ports of the device
|
---|
167 | *
|
---|
168 | * @param rtc The real time clock device
|
---|
169 | *
|
---|
170 | * @return true in case of success, false otherwise
|
---|
171 | */
|
---|
172 | static bool
|
---|
173 | rtc_pio_enable(rtc_t *rtc)
|
---|
174 | {
|
---|
175 | if (pio_enable((void *) rtc->io_addr, REG_COUNT,
|
---|
176 | (void **) &rtc->port)) {
|
---|
177 |
|
---|
178 | ddf_msg(LVL_ERROR, "Cannot map the port %lx"
|
---|
179 | " for device %s", (long unsigned int)rtc->io_addr,
|
---|
180 | ddf_dev_get_name(rtc->dev));
|
---|
181 | return false;
|
---|
182 | }
|
---|
183 |
|
---|
184 | return true;
|
---|
185 | }
|
---|
186 |
|
---|
187 | /** Initialize the RTC device
|
---|
188 | *
|
---|
189 | * @param rtc Pointer to the RTC device
|
---|
190 | *
|
---|
191 | * @return EOK on success or an error code
|
---|
192 | */
|
---|
193 | static errno_t
|
---|
194 | rtc_dev_initialize(rtc_t *rtc)
|
---|
195 | {
|
---|
196 | errno_t rc;
|
---|
197 | size_t i;
|
---|
198 | hw_resource_t *res;
|
---|
199 | bool ioport = false;
|
---|
200 | async_sess_t *parent_sess;
|
---|
201 |
|
---|
202 | ddf_msg(LVL_DEBUG, "rtc_dev_initialize %s", ddf_dev_get_name(rtc->dev));
|
---|
203 |
|
---|
204 | rtc->boot_time.tv_sec = 0;
|
---|
205 | rtc->boot_time.tv_nsec = 0;
|
---|
206 | rtc->clients_connected = 0;
|
---|
207 |
|
---|
208 | hw_resource_list_t hw_resources;
|
---|
209 | memset(&hw_resources, 0, sizeof(hw_resource_list_t));
|
---|
210 |
|
---|
211 | /* Connect to the parent's driver */
|
---|
212 |
|
---|
213 | parent_sess = ddf_dev_parent_sess_get(rtc->dev);
|
---|
214 | if (parent_sess == NULL) {
|
---|
215 | ddf_msg(LVL_ERROR, "Failed to connect to parent driver\
|
---|
216 | of device %s.", ddf_dev_get_name(rtc->dev));
|
---|
217 | rc = ENOENT;
|
---|
218 | goto error;
|
---|
219 | }
|
---|
220 |
|
---|
221 | /* Get the HW resources */
|
---|
222 | rc = hw_res_get_resource_list(parent_sess, &hw_resources);
|
---|
223 | if (rc != EOK) {
|
---|
224 | ddf_msg(LVL_ERROR, "Failed to get HW resources\
|
---|
225 | for device %s", ddf_dev_get_name(rtc->dev));
|
---|
226 | goto error;
|
---|
227 | }
|
---|
228 |
|
---|
229 | for (i = 0; i < hw_resources.count; ++i) {
|
---|
230 | res = &hw_resources.resources[i];
|
---|
231 |
|
---|
232 | if (res->res.io_range.size < REG_COUNT) {
|
---|
233 | ddf_msg(LVL_ERROR, "I/O range assigned to \
|
---|
234 | device %s is too small",
|
---|
235 | ddf_dev_get_name(rtc->dev));
|
---|
236 | rc = ELIMIT;
|
---|
237 | continue;
|
---|
238 | }
|
---|
239 |
|
---|
240 | rtc->io_addr = (ioport8_t *) (long) res->res.io_range.address;
|
---|
241 | ioport = true;
|
---|
242 | ddf_msg(LVL_NOTE, "Device %s was assigned I/O address "
|
---|
243 | "0x%lx", ddf_dev_get_name(rtc->dev),
|
---|
244 | (unsigned long int) rtc->io_addr);
|
---|
245 | rc = EOK;
|
---|
246 | break;
|
---|
247 | }
|
---|
248 |
|
---|
249 | if (rc != EOK)
|
---|
250 | goto error;
|
---|
251 |
|
---|
252 | if (!ioport) {
|
---|
253 | /* No I/O address assigned to this device */
|
---|
254 | ddf_msg(LVL_ERROR, "Missing HW resource for device %s",
|
---|
255 | ddf_dev_get_name(rtc->dev));
|
---|
256 | rc = ENOENT;
|
---|
257 | goto error;
|
---|
258 | }
|
---|
259 |
|
---|
260 | hw_res_clean_resource_list(&hw_resources);
|
---|
261 |
|
---|
262 | return EOK;
|
---|
263 |
|
---|
264 | error:
|
---|
265 | rtc_dev_cleanup(rtc);
|
---|
266 | hw_res_clean_resource_list(&hw_resources);
|
---|
267 |
|
---|
268 | return rc;
|
---|
269 | }
|
---|
270 |
|
---|
271 | /** Read a register from the CMOS memory
|
---|
272 | *
|
---|
273 | * @param rtc The rtc device
|
---|
274 | * @param reg The index of the register to read
|
---|
275 | *
|
---|
276 | * @return The value of the register
|
---|
277 | */
|
---|
278 | static int
|
---|
279 | rtc_register_read(rtc_t *rtc, int reg)
|
---|
280 | {
|
---|
281 | pio_write_8(REG_SEL_PORT(rtc->port), reg);
|
---|
282 | return pio_read_8(REG_RW_PORT(rtc->port));
|
---|
283 | }
|
---|
284 |
|
---|
285 | /** Write a register to the CMOS memory
|
---|
286 | *
|
---|
287 | * @param rtc The rtc device
|
---|
288 | * @param reg The index of the register to write
|
---|
289 | * @param data The data to write
|
---|
290 | */
|
---|
291 | static void
|
---|
292 | rtc_register_write(rtc_t *rtc, int reg, int data)
|
---|
293 | {
|
---|
294 | pio_write_8(REG_SEL_PORT(rtc->port), reg);
|
---|
295 | pio_write_8(REG_RW_PORT(rtc->port), data);
|
---|
296 | }
|
---|
297 |
|
---|
298 | /** Check if an update is in progress
|
---|
299 | *
|
---|
300 | * @param rtc The rtc device
|
---|
301 | *
|
---|
302 | * @return true if an update is in progress, false otherwise
|
---|
303 | */
|
---|
304 | static bool
|
---|
305 | rtc_update_in_progress(rtc_t *rtc)
|
---|
306 | {
|
---|
307 | return rtc_register_read(rtc, RTC_STATUS_A) & RTC_A_UPDATE;
|
---|
308 | }
|
---|
309 |
|
---|
310 | /** Read the current time from the CMOS
|
---|
311 | *
|
---|
312 | * @param fun The RTC function
|
---|
313 | * @param t Pointer to the time variable
|
---|
314 | *
|
---|
315 | * @return EOK on success or an error code
|
---|
316 | */
|
---|
317 | static errno_t
|
---|
318 | rtc_time_get(ddf_fun_t *fun, struct tm *t)
|
---|
319 | {
|
---|
320 | bool bcd_mode;
|
---|
321 | bool pm_mode = false;
|
---|
322 | rtc_t *rtc = fun_rtc(fun);
|
---|
323 |
|
---|
324 | fibril_mutex_lock(&rtc->mutex);
|
---|
325 |
|
---|
326 | if (rtc->boot_time.tv_sec) {
|
---|
327 | /*
|
---|
328 | * There is no need to read the current time from the
|
---|
329 | * device because it has already been cached.
|
---|
330 | */
|
---|
331 |
|
---|
332 | struct timespec curtime;
|
---|
333 |
|
---|
334 | getuptime(&curtime);
|
---|
335 | ts_add(&curtime, &rtc->boot_time);
|
---|
336 | fibril_mutex_unlock(&rtc->mutex);
|
---|
337 |
|
---|
338 | return time_ts2tm(&curtime, t);
|
---|
339 | }
|
---|
340 |
|
---|
341 | /* Check if the RTC battery is OK */
|
---|
342 | if (!is_battery_ok(rtc)) {
|
---|
343 | fibril_mutex_unlock(&rtc->mutex);
|
---|
344 | return EIO;
|
---|
345 | }
|
---|
346 |
|
---|
347 | /* Nanoseconds are below RTC's resolution, assume 0. */
|
---|
348 | t->tm_nsec = 0;
|
---|
349 |
|
---|
350 | /* now read the registers */
|
---|
351 | do {
|
---|
352 | /* Suspend until the update process has finished */
|
---|
353 | while (rtc_update_in_progress(rtc))
|
---|
354 | ;
|
---|
355 |
|
---|
356 | t->tm_sec = rtc_register_read(rtc, RTC_SEC);
|
---|
357 | t->tm_min = rtc_register_read(rtc, RTC_MIN);
|
---|
358 | t->tm_hour = rtc_register_read(rtc, RTC_HOUR);
|
---|
359 | t->tm_mday = rtc_register_read(rtc, RTC_DAY);
|
---|
360 | t->tm_mon = rtc_register_read(rtc, RTC_MON);
|
---|
361 | t->tm_year = rtc_register_read(rtc, RTC_YEAR);
|
---|
362 |
|
---|
363 | /* Now check if it is stable */
|
---|
364 | } while (t->tm_sec != rtc_register_read(rtc, RTC_SEC) ||
|
---|
365 | t->tm_min != rtc_register_read(rtc, RTC_MIN) ||
|
---|
366 | t->tm_mday != rtc_register_read(rtc, RTC_DAY) ||
|
---|
367 | t->tm_mon != rtc_register_read(rtc, RTC_MON) ||
|
---|
368 | t->tm_year != rtc_register_read(rtc, RTC_YEAR));
|
---|
369 |
|
---|
370 | /* Check if the RTC is working in 12h mode */
|
---|
371 | bool _12h_mode = !(rtc_register_read(rtc, RTC_STATUS_B) &
|
---|
372 | RTC_B_24H);
|
---|
373 | if (_12h_mode) {
|
---|
374 | /* The RTC is working in 12h mode, check if it is AM or PM */
|
---|
375 | if (t->tm_hour & 0x80) {
|
---|
376 | /* PM flag is active, it must be cleared */
|
---|
377 | t->tm_hour &= ~0x80;
|
---|
378 | pm_mode = true;
|
---|
379 | }
|
---|
380 | }
|
---|
381 |
|
---|
382 | /* Check if the RTC is working in BCD mode */
|
---|
383 | bcd_mode = !(rtc_register_read(rtc, RTC_STATUS_B) & RTC_B_BCD);
|
---|
384 | if (bcd_mode) {
|
---|
385 | t->tm_sec = bcd2bin(t->tm_sec);
|
---|
386 | t->tm_min = bcd2bin(t->tm_min);
|
---|
387 | t->tm_hour = bcd2bin(t->tm_hour);
|
---|
388 | t->tm_mday = bcd2bin(t->tm_mday);
|
---|
389 | t->tm_mon = bcd2bin(t->tm_mon);
|
---|
390 | t->tm_year = bcd2bin(t->tm_year);
|
---|
391 | }
|
---|
392 |
|
---|
393 | if (_12h_mode) {
|
---|
394 | /* Convert to 24h mode */
|
---|
395 | if (pm_mode) {
|
---|
396 | if (t->tm_hour < 12)
|
---|
397 | t->tm_hour += 12;
|
---|
398 | } else if (t->tm_hour == 12)
|
---|
399 | t->tm_hour = 0;
|
---|
400 | }
|
---|
401 |
|
---|
402 | /* Count the months starting from 0, not from 1 */
|
---|
403 | t->tm_mon--;
|
---|
404 |
|
---|
405 | if (t->tm_year < 100) {
|
---|
406 | /*
|
---|
407 | * tm_year is the number of years since 1900 but the
|
---|
408 | * RTC epoch is 2000.
|
---|
409 | */
|
---|
410 | t->tm_year += 100;
|
---|
411 | }
|
---|
412 |
|
---|
413 | /* Try to normalize the content of the tm structure */
|
---|
414 | time_t r = mktime(t);
|
---|
415 | errno_t result;
|
---|
416 |
|
---|
417 | if (r < 0)
|
---|
418 | result = EINVAL;
|
---|
419 | else {
|
---|
420 | struct timespec uptime;
|
---|
421 |
|
---|
422 | getuptime(&uptime);
|
---|
423 | rtc->boot_time.tv_sec = r;
|
---|
424 | rtc->boot_time.tv_nsec = t->tm_nsec; /* normalized */
|
---|
425 | ts_sub(&rtc->boot_time, &uptime);
|
---|
426 | result = EOK;
|
---|
427 | }
|
---|
428 |
|
---|
429 | fibril_mutex_unlock(&rtc->mutex);
|
---|
430 |
|
---|
431 | return result;
|
---|
432 | }
|
---|
433 |
|
---|
434 | /** Set the time in the RTC
|
---|
435 | *
|
---|
436 | * @param fun The RTC function
|
---|
437 | * @param t The time value to set
|
---|
438 | *
|
---|
439 | * @return EOK or an error code
|
---|
440 | */
|
---|
441 | static errno_t
|
---|
442 | rtc_time_set(ddf_fun_t *fun, struct tm *t)
|
---|
443 | {
|
---|
444 | bool bcd_mode;
|
---|
445 | time_t norm_time;
|
---|
446 | struct timespec uptime;
|
---|
447 | struct timespec ntv;
|
---|
448 | int reg_b;
|
---|
449 | int reg_a;
|
---|
450 | int epoch;
|
---|
451 | rtc_t *rtc = fun_rtc(fun);
|
---|
452 |
|
---|
453 | /* Try to normalize the content of the tm structure */
|
---|
454 | if ((norm_time = mktime(t)) < 0)
|
---|
455 | return EINVAL;
|
---|
456 |
|
---|
457 | ntv.tv_sec = norm_time;
|
---|
458 | ntv.tv_nsec = t->tm_nsec;
|
---|
459 | getuptime(&uptime);
|
---|
460 |
|
---|
461 | if (ts_gteq(&uptime, &ntv)) {
|
---|
462 | /* This is not acceptable */
|
---|
463 | return EINVAL;
|
---|
464 | }
|
---|
465 |
|
---|
466 | fibril_mutex_lock(&rtc->mutex);
|
---|
467 |
|
---|
468 | if (!is_battery_ok(rtc)) {
|
---|
469 | fibril_mutex_unlock(&rtc->mutex);
|
---|
470 | return EIO;
|
---|
471 | }
|
---|
472 |
|
---|
473 | /* boot_time must be recomputed */
|
---|
474 | rtc->boot_time.tv_sec = 0;
|
---|
475 | rtc->boot_time.tv_nsec = 0;
|
---|
476 |
|
---|
477 | /* Detect the RTC epoch */
|
---|
478 | if (rtc_register_read(rtc, RTC_YEAR) < 100)
|
---|
479 | epoch = 2000;
|
---|
480 | else
|
---|
481 | epoch = 1900;
|
---|
482 |
|
---|
483 | if (epoch == 2000 && t->tm_year < 100) {
|
---|
484 | /* Can't set a year before the epoch */
|
---|
485 | fibril_mutex_unlock(&rtc->mutex);
|
---|
486 | return EINVAL;
|
---|
487 | }
|
---|
488 |
|
---|
489 | t->tm_mon++; /* counts from 1, not from 0 */
|
---|
490 |
|
---|
491 | reg_b = rtc_register_read(rtc, RTC_STATUS_B);
|
---|
492 |
|
---|
493 | if (!(reg_b & RTC_B_24H)) {
|
---|
494 | /* Force 24h mode of operation */
|
---|
495 | reg_b |= RTC_B_24H;
|
---|
496 | rtc_register_write(rtc, RTC_STATUS_B, reg_b);
|
---|
497 | }
|
---|
498 |
|
---|
499 | if (epoch == 2000) {
|
---|
500 | /*
|
---|
501 | * The RTC epoch is year 2000 but the tm_year
|
---|
502 | * field counts years since 1900.
|
---|
503 | */
|
---|
504 | t->tm_year -= 100;
|
---|
505 | }
|
---|
506 |
|
---|
507 | /* Check if the rtc is working in bcd mode */
|
---|
508 | bcd_mode = !(reg_b & RTC_B_BCD);
|
---|
509 | if (bcd_mode) {
|
---|
510 | /* Convert the tm struct fields in BCD mode */
|
---|
511 | t->tm_sec = bin2bcd(t->tm_sec);
|
---|
512 | t->tm_min = bin2bcd(t->tm_min);
|
---|
513 | t->tm_hour = bin2bcd(t->tm_hour);
|
---|
514 | t->tm_mday = bin2bcd(t->tm_mday);
|
---|
515 | t->tm_mon = bin2bcd(t->tm_mon);
|
---|
516 | t->tm_year = bin2bcd(t->tm_year);
|
---|
517 | }
|
---|
518 |
|
---|
519 | /* Inhibit updates */
|
---|
520 | rtc_register_write(rtc, RTC_STATUS_B, reg_b | RTC_B_INH);
|
---|
521 |
|
---|
522 | /* Write current time to RTC */
|
---|
523 | rtc_register_write(rtc, RTC_SEC, t->tm_sec);
|
---|
524 | rtc_register_write(rtc, RTC_MIN, t->tm_min);
|
---|
525 | rtc_register_write(rtc, RTC_HOUR, t->tm_hour);
|
---|
526 | rtc_register_write(rtc, RTC_DAY, t->tm_mday);
|
---|
527 | rtc_register_write(rtc, RTC_MON, t->tm_mon);
|
---|
528 | rtc_register_write(rtc, RTC_YEAR, t->tm_year);
|
---|
529 |
|
---|
530 | /* Stop the clock */
|
---|
531 | reg_a = rtc_register_read(rtc, RTC_STATUS_A);
|
---|
532 | rtc_register_write(rtc, RTC_STATUS_A, RTC_A_CLK_STOP | reg_a);
|
---|
533 |
|
---|
534 | /* Enable updates */
|
---|
535 | rtc_register_write(rtc, RTC_STATUS_B, reg_b);
|
---|
536 | rtc_register_write(rtc, RTC_STATUS_A, reg_a);
|
---|
537 |
|
---|
538 | fibril_mutex_unlock(&rtc->mutex);
|
---|
539 |
|
---|
540 | return EOK;
|
---|
541 | }
|
---|
542 |
|
---|
543 | /** Get the status of the real time clock battery
|
---|
544 | *
|
---|
545 | * @param fun The RTC function
|
---|
546 | * @param status The status of the battery
|
---|
547 | *
|
---|
548 | * @return EOK on success or an error code
|
---|
549 | */
|
---|
550 | static errno_t
|
---|
551 | rtc_battery_status_get(ddf_fun_t *fun, battery_status_t *status)
|
---|
552 | {
|
---|
553 | rtc_t *rtc = fun_rtc(fun);
|
---|
554 |
|
---|
555 | fibril_mutex_lock(&rtc->mutex);
|
---|
556 | const bool batt_ok = is_battery_ok(rtc);
|
---|
557 | fibril_mutex_unlock(&rtc->mutex);
|
---|
558 |
|
---|
559 | *status = batt_ok ? BATTERY_OK : BATTERY_LOW;
|
---|
560 |
|
---|
561 | return EOK;
|
---|
562 | }
|
---|
563 |
|
---|
564 | /** Check if the battery is working properly or not.
|
---|
565 | * The caller already holds the rtc->mutex lock.
|
---|
566 | *
|
---|
567 | * @param rtc The RTC instance.
|
---|
568 | *
|
---|
569 | * @return true if the battery is ok, false otherwise.
|
---|
570 | */
|
---|
571 | static bool
|
---|
572 | is_battery_ok(rtc_t *rtc)
|
---|
573 | {
|
---|
574 | return rtc_register_read(rtc, RTC_STATUS_D) & RTC_D_BATTERY_OK;
|
---|
575 | }
|
---|
576 |
|
---|
577 | /** The dev_add callback of the rtc driver
|
---|
578 | *
|
---|
579 | * @param dev The RTC device
|
---|
580 | *
|
---|
581 | * @return EOK on success or an error code
|
---|
582 | */
|
---|
583 | static errno_t
|
---|
584 | rtc_dev_add(ddf_dev_t *dev)
|
---|
585 | {
|
---|
586 | rtc_t *rtc;
|
---|
587 | ddf_fun_t *fun = NULL;
|
---|
588 | errno_t rc;
|
---|
589 | bool need_cleanup = false;
|
---|
590 | bool bound = false;
|
---|
591 |
|
---|
592 | ddf_msg(LVL_DEBUG, "rtc_dev_add %s (handle = %d)",
|
---|
593 | ddf_dev_get_name(dev), (int) ddf_dev_get_handle(dev));
|
---|
594 |
|
---|
595 | rtc = ddf_dev_data_alloc(dev, sizeof(rtc_t));
|
---|
596 | if (!rtc)
|
---|
597 | return ENOMEM;
|
---|
598 |
|
---|
599 | rtc->dev = dev;
|
---|
600 | fibril_mutex_initialize(&rtc->mutex);
|
---|
601 |
|
---|
602 | rc = rtc_dev_initialize(rtc);
|
---|
603 | if (rc != EOK)
|
---|
604 | goto error;
|
---|
605 |
|
---|
606 | need_cleanup = true;
|
---|
607 |
|
---|
608 | if (!rtc_pio_enable(rtc)) {
|
---|
609 | rc = EADDRNOTAVAIL;
|
---|
610 | goto error;
|
---|
611 | }
|
---|
612 |
|
---|
613 | fun = ddf_fun_create(dev, fun_exposed, "a");
|
---|
614 | if (!fun) {
|
---|
615 | ddf_msg(LVL_ERROR, "Failed creating function");
|
---|
616 | rc = ENOENT;
|
---|
617 | goto error;
|
---|
618 | }
|
---|
619 |
|
---|
620 | ddf_fun_set_ops(fun, &rtc_dev_ops);
|
---|
621 | rc = ddf_fun_bind(fun);
|
---|
622 | if (rc != EOK) {
|
---|
623 | ddf_msg(LVL_ERROR, "Failed binding function");
|
---|
624 | goto error;
|
---|
625 | }
|
---|
626 |
|
---|
627 | bound = true;
|
---|
628 | rtc->fun = fun;
|
---|
629 |
|
---|
630 | rc = ddf_fun_add_to_category(fun, "clock");
|
---|
631 | if (rc != EOK) {
|
---|
632 | ddf_msg(LVL_ERROR, "Failed adding service to clock category.");
|
---|
633 | goto error;
|
---|
634 | }
|
---|
635 |
|
---|
636 | ddf_msg(LVL_NOTE, "Device %s successfully initialized",
|
---|
637 | ddf_dev_get_name(dev));
|
---|
638 |
|
---|
639 | return rc;
|
---|
640 |
|
---|
641 | error:
|
---|
642 | if (bound)
|
---|
643 | ddf_fun_unbind(fun);
|
---|
644 | if (fun)
|
---|
645 | ddf_fun_destroy(fun);
|
---|
646 | if (need_cleanup)
|
---|
647 | rtc_dev_cleanup(rtc);
|
---|
648 | return rc;
|
---|
649 | }
|
---|
650 |
|
---|
651 | /** The dev_remove callback for the rtc driver
|
---|
652 | *
|
---|
653 | * @param dev The RTC device
|
---|
654 | *
|
---|
655 | * @return EOK on success or an error code
|
---|
656 | */
|
---|
657 | static errno_t
|
---|
658 | rtc_dev_remove(ddf_dev_t *dev)
|
---|
659 | {
|
---|
660 | rtc_t *rtc = dev_rtc(dev);
|
---|
661 | errno_t rc;
|
---|
662 |
|
---|
663 | fibril_mutex_lock(&rtc->mutex);
|
---|
664 | if (rtc->clients_connected > 0) {
|
---|
665 | fibril_mutex_unlock(&rtc->mutex);
|
---|
666 | return EBUSY;
|
---|
667 | }
|
---|
668 |
|
---|
669 | rtc->removed = true;
|
---|
670 | fibril_mutex_unlock(&rtc->mutex);
|
---|
671 |
|
---|
672 | rc = rtc_fun_offline(rtc->fun);
|
---|
673 | if (rc != EOK) {
|
---|
674 | ddf_msg(LVL_ERROR, "Failed to offline function");
|
---|
675 | return rc;
|
---|
676 | }
|
---|
677 |
|
---|
678 | rc = ddf_fun_unbind(rtc->fun);
|
---|
679 | if (rc != EOK) {
|
---|
680 | ddf_msg(LVL_ERROR, "Failed to unbind function");
|
---|
681 | return rc;
|
---|
682 | }
|
---|
683 |
|
---|
684 | ddf_fun_destroy(rtc->fun);
|
---|
685 | rtc_dev_cleanup(rtc);
|
---|
686 |
|
---|
687 | return rc;
|
---|
688 | }
|
---|
689 |
|
---|
690 | /** Open the device
|
---|
691 | *
|
---|
692 | * @param fun The function node
|
---|
693 | *
|
---|
694 | * @return EOK on success or an error code
|
---|
695 | */
|
---|
696 | static errno_t
|
---|
697 | rtc_open(ddf_fun_t *fun)
|
---|
698 | {
|
---|
699 | errno_t rc;
|
---|
700 | rtc_t *rtc = fun_rtc(fun);
|
---|
701 |
|
---|
702 | fibril_mutex_lock(&rtc->mutex);
|
---|
703 |
|
---|
704 | if (rtc->removed)
|
---|
705 | rc = ENXIO;
|
---|
706 | else {
|
---|
707 | rc = EOK;
|
---|
708 | rtc->clients_connected++;
|
---|
709 | }
|
---|
710 |
|
---|
711 | fibril_mutex_unlock(&rtc->mutex);
|
---|
712 | return rc;
|
---|
713 | }
|
---|
714 |
|
---|
715 | /** Close the device
|
---|
716 | *
|
---|
717 | * @param fun The function node
|
---|
718 | */
|
---|
719 | static void
|
---|
720 | rtc_close(ddf_fun_t *fun)
|
---|
721 | {
|
---|
722 | rtc_t *rtc = fun_rtc(fun);
|
---|
723 |
|
---|
724 | fibril_mutex_lock(&rtc->mutex);
|
---|
725 |
|
---|
726 | rtc->clients_connected--;
|
---|
727 | assert(rtc->clients_connected >= 0);
|
---|
728 |
|
---|
729 | fibril_mutex_unlock(&rtc->mutex);
|
---|
730 | }
|
---|
731 |
|
---|
732 | /** Convert from BCD mode to binary mode
|
---|
733 | *
|
---|
734 | * @param bcd The number in BCD format to convert
|
---|
735 | *
|
---|
736 | * @return The converted value
|
---|
737 | */
|
---|
738 | static unsigned
|
---|
739 | bcd2bin(unsigned bcd)
|
---|
740 | {
|
---|
741 | return ((bcd & 0xF0) >> 1) + ((bcd & 0xF0) >> 3) + (bcd & 0xf);
|
---|
742 | }
|
---|
743 |
|
---|
744 | /** Convert from binary mode to BCD mode
|
---|
745 | *
|
---|
746 | * @param bcd The number in binary mode to convert
|
---|
747 | *
|
---|
748 | * @return The converted value
|
---|
749 | */
|
---|
750 | static unsigned
|
---|
751 | bin2bcd(unsigned binary)
|
---|
752 | {
|
---|
753 | return ((binary / 10) << 4) + (binary % 10);
|
---|
754 | }
|
---|
755 |
|
---|
756 | static errno_t
|
---|
757 | rtc_fun_online(ddf_fun_t *fun)
|
---|
758 | {
|
---|
759 | errno_t rc;
|
---|
760 |
|
---|
761 | ddf_msg(LVL_DEBUG, "rtc_fun_online()");
|
---|
762 |
|
---|
763 | rc = ddf_fun_online(fun);
|
---|
764 | if (rc == EOK) {
|
---|
765 | // XXX This should be probably handled by the framework
|
---|
766 | rc = ddf_fun_add_to_category(fun, "clock");
|
---|
767 | }
|
---|
768 |
|
---|
769 | return rc;
|
---|
770 | }
|
---|
771 |
|
---|
772 | static errno_t
|
---|
773 | rtc_fun_offline(ddf_fun_t *fun)
|
---|
774 | {
|
---|
775 | ddf_msg(LVL_DEBUG, "rtc_fun_offline()");
|
---|
776 | return ddf_fun_offline(fun);
|
---|
777 | }
|
---|
778 |
|
---|
779 | int
|
---|
780 | main(int argc, char **argv)
|
---|
781 | {
|
---|
782 | printf(NAME ": HelenOS RTC driver\n");
|
---|
783 | rtc_init();
|
---|
784 | return ddf_driver_main(&rtc_driver);
|
---|
785 | }
|
---|
786 |
|
---|
787 | /**
|
---|
788 | * @}
|
---|
789 | */
|
---|