rtc: Add support to fun_offline() / fun_online()
[helenos.git] / uspace / drv / time / cmos-rtc / cmos-rtc.c
blob8bb04708a697f858915a62341d3e51f0265a2a65
1 /*
2 * Copyright (c) 2012 Maurizio Lombardi
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
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.
29 /**
30 * @defgroup CMOS RTC driver.
31 * @brief HelenOS RTC driver.
32 * @{
35 /** @file
38 #include <errno.h>
39 #include <ddi.h>
40 #include <as.h>
41 #include <sysinfo.h>
42 #include <libarch/ddi.h>
43 #include <libarch/barrier.h>
44 #include <stdio.h>
45 #include <ddf/driver.h>
46 #include <ddf/log.h>
47 #include <ops/clock_dev.h>
48 #include <ops/battery_dev.h>
49 #include <fibril_synch.h>
50 #include <device/hw_res.h>
51 #include <macros.h>
52 #include <time.h>
54 #include "cmos-regs.h"
56 #define NAME "cmos-rtc"
58 #define REG_COUNT 2
60 #define REG_SEL_PORT(port) (port)
61 #define REG_RW_PORT(port) ((port) + 1)
63 typedef struct rtc {
64 /** DDF device node */
65 ddf_dev_t *dev;
66 /** DDF function node */
67 ddf_fun_t *fun;
68 /** The fibril mutex for synchronizing the access to the device */
69 fibril_mutex_t mutex;
70 /** The base I/O address of the device registers */
71 ioport8_t *io_addr;
72 /** The I/O port used to access the CMOS registers */
73 ioport8_t *port;
74 /** true if device is removed */
75 bool removed;
76 /** number of connected clients */
77 int clients_connected;
78 /** time at which the system booted */
79 time_t boottime;
80 } rtc_t;
82 static rtc_t *dev_rtc(ddf_dev_t *dev);
83 static rtc_t *fun_rtc(ddf_fun_t *fun);
84 static int
85 rtc_battery_status_get(ddf_fun_t *fun, battery_status_t *status);
86 static int rtc_time_get(ddf_fun_t *fun, struct tm *t);
87 static int rtc_time_set(ddf_fun_t *fun, struct tm *t);
88 static int rtc_dev_add(ddf_dev_t *dev);
89 static int rtc_dev_initialize(rtc_t *rtc);
90 static bool rtc_pio_enable(rtc_t *rtc);
91 static void rtc_dev_cleanup(rtc_t *rtc);
92 static int rtc_open(ddf_fun_t *fun);
93 static void rtc_close(ddf_fun_t *fun);
94 static bool rtc_update_in_progress(rtc_t *rtc);
95 static int rtc_register_read(rtc_t *rtc, int reg);
96 static unsigned bcd2bin(unsigned bcd);
97 static unsigned bin2bcd(unsigned binary);
98 static int rtc_dev_remove(ddf_dev_t *dev);
99 static void rtc_register_write(rtc_t *rtc, int reg, int data);
100 static time_t uptime_get(void);
101 static bool is_battery_ok(rtc_t *rtc);
102 static int rtc_fun_online(ddf_fun_t *fun);
103 static int rtc_fun_offline(ddf_fun_t *fun);
105 static ddf_dev_ops_t rtc_dev_ops;
107 /** The RTC device driver's standard operations */
108 static driver_ops_t rtc_ops = {
109 .dev_add = rtc_dev_add,
110 .dev_remove = rtc_dev_remove,
111 .fun_online = rtc_fun_online,
112 .fun_offline = rtc_fun_offline,
115 /** The RTC device driver structure */
116 static driver_t rtc_driver = {
117 .name = NAME,
118 .driver_ops = &rtc_ops,
121 /** Clock interface */
122 static clock_dev_ops_t rtc_clock_dev_ops = {
123 .time_get = rtc_time_get,
124 .time_set = rtc_time_set,
127 /** Battery powered device interface */
128 static battery_dev_ops_t rtc_battery_dev_ops = {
129 .battery_status_get = rtc_battery_status_get,
130 .battery_charge_level_get = NULL,
133 /** Obtain soft state structure from device node */
134 static rtc_t *
135 dev_rtc(ddf_dev_t *dev)
137 return ddf_dev_data_get(dev);
140 /** Obtain soft state structure from function node */
141 static rtc_t *
142 fun_rtc(ddf_fun_t *fun)
144 return dev_rtc(ddf_fun_get_dev(fun));
147 /** Initialize the RTC driver */
148 static void
149 rtc_init(void)
151 ddf_log_init(NAME);
153 rtc_dev_ops.open = rtc_open;
154 rtc_dev_ops.close = rtc_close;
156 rtc_dev_ops.interfaces[CLOCK_DEV_IFACE] = &rtc_clock_dev_ops;
157 rtc_dev_ops.interfaces[BATTERY_DEV_IFACE] = &rtc_battery_dev_ops;
158 rtc_dev_ops.default_handler = NULL;
161 /** Clean up the RTC soft state
163 * @param rtc The RTC device
165 static void
166 rtc_dev_cleanup(rtc_t *rtc)
170 /** Enable the I/O ports of the device
172 * @param rtc The real time clock device
174 * @return true in case of success, false otherwise
176 static bool
177 rtc_pio_enable(rtc_t *rtc)
179 if (pio_enable((void *) rtc->io_addr, REG_COUNT,
180 (void **) &rtc->port)) {
182 ddf_msg(LVL_ERROR, "Cannot map the port %lx"
183 " for device %s", (long unsigned int)rtc->io_addr,
184 ddf_dev_get_name(rtc->dev));
185 return false;
188 return true;
191 /** Initialize the RTC device
193 * @param rtc Pointer to the RTC device
195 * @return EOK on success or a negative error code
197 static int
198 rtc_dev_initialize(rtc_t *rtc)
200 int rc;
201 size_t i;
202 hw_resource_t *res;
203 bool ioport = false;
204 async_sess_t *parent_sess;
206 ddf_msg(LVL_DEBUG, "rtc_dev_initialize %s", ddf_dev_get_name(rtc->dev));
208 rtc->boottime = 0;
209 rtc->clients_connected = 0;
211 hw_resource_list_t hw_resources;
212 memset(&hw_resources, 0, sizeof(hw_resource_list_t));
214 /* Connect to the parent's driver */
216 parent_sess = ddf_dev_parent_sess_create(rtc->dev, EXCHANGE_SERIALIZE);
217 if (!parent_sess) {
218 ddf_msg(LVL_ERROR, "Failed to connect to parent driver\
219 of device %s.", ddf_dev_get_name(rtc->dev));
220 rc = ENOENT;
221 goto error;
224 /* Get the HW resources */
225 rc = hw_res_get_resource_list(parent_sess, &hw_resources);
226 if (rc != EOK) {
227 ddf_msg(LVL_ERROR, "Failed to get HW resources\
228 for device %s", ddf_dev_get_name(rtc->dev));
229 goto error;
232 for (i = 0; i < hw_resources.count; ++i) {
233 res = &hw_resources.resources[i];
235 if (res->res.io_range.size < REG_COUNT) {
236 ddf_msg(LVL_ERROR, "I/O range assigned to \
237 device %s is too small",
238 ddf_dev_get_name(rtc->dev));
239 rc = ELIMIT;
240 continue;
243 rtc->io_addr = (ioport8_t *) (long) res->res.io_range.address;
244 ioport = true;
245 ddf_msg(LVL_NOTE, "Device %s was assigned I/O address "
246 "0x%lx", ddf_dev_get_name(rtc->dev),
247 (unsigned long int) rtc->io_addr);
248 rc = EOK;
249 break;
252 if (rc != EOK)
253 goto error;
255 if (!ioport) {
256 /* No I/O address assigned to this device */
257 ddf_msg(LVL_ERROR, "Missing HW resource for device %s",
258 ddf_dev_get_name(rtc->dev));
259 rc = ENOENT;
260 goto error;
263 hw_res_clean_resource_list(&hw_resources);
265 return EOK;
267 error:
268 rtc_dev_cleanup(rtc);
269 hw_res_clean_resource_list(&hw_resources);
271 return rc;
274 /** Read a register from the CMOS memory
276 * @param rtc The rtc device
277 * @param reg The index of the register to read
279 * @return The value of the register
281 static int
282 rtc_register_read(rtc_t *rtc, int reg)
284 pio_write_8(REG_SEL_PORT(rtc->port), reg);
285 return pio_read_8(REG_RW_PORT(rtc->port));
288 /** Write a register to the CMOS memory
290 * @param rtc The rtc device
291 * @param reg The index of the register to write
292 * @param data The data to write
294 static void
295 rtc_register_write(rtc_t *rtc, int reg, int data)
297 pio_write_8(REG_SEL_PORT(rtc->port), reg);
298 pio_write_8(REG_RW_PORT(rtc->port), data);
301 /** Check if an update is in progress
303 * @param rtc The rtc device
305 * @return true if an update is in progress, false otherwise
307 static bool
308 rtc_update_in_progress(rtc_t *rtc)
310 return rtc_register_read(rtc, RTC_STATUS_A) & RTC_A_UPDATE;
313 /** Read the current time from the CMOS
315 * @param fun The RTC function
316 * @param t Pointer to the time variable
318 * @return EOK on success or a negative error code
320 static int
321 rtc_time_get(ddf_fun_t *fun, struct tm *t)
323 bool bcd_mode;
324 bool pm_mode = false;
325 rtc_t *rtc = fun_rtc(fun);
327 fibril_mutex_lock(&rtc->mutex);
329 if (rtc->boottime != 0) {
330 /* There is no need to read the current time from the
331 * device because it has already been cached.
334 time_t cur_time = rtc->boottime + uptime_get();
336 fibril_mutex_unlock(&rtc->mutex);
338 return time_local2tm(cur_time, t);
341 /* Check if the RTC battery is OK */
342 if (!is_battery_ok(rtc)) {
343 fibril_mutex_unlock(&rtc->mutex);
344 return EIO;
347 /* now read the registers */
348 do {
349 /* Suspend until the update process has finished */
350 while (rtc_update_in_progress(rtc));
352 t->tm_sec = rtc_register_read(rtc, RTC_SEC);
353 t->tm_min = rtc_register_read(rtc, RTC_MIN);
354 t->tm_hour = rtc_register_read(rtc, RTC_HOUR);
355 t->tm_mday = rtc_register_read(rtc, RTC_DAY);
356 t->tm_mon = rtc_register_read(rtc, RTC_MON);
357 t->tm_year = rtc_register_read(rtc, RTC_YEAR);
359 /* Now check if it is stable */
360 } while(t->tm_sec != rtc_register_read(rtc, RTC_SEC) ||
361 t->tm_min != rtc_register_read(rtc, RTC_MIN) ||
362 t->tm_mday != rtc_register_read(rtc, RTC_DAY) ||
363 t->tm_mon != rtc_register_read(rtc, RTC_MON) ||
364 t->tm_year != rtc_register_read(rtc, RTC_YEAR));
366 /* Check if the RTC is working in 12h mode */
367 bool _12h_mode = !(rtc_register_read(rtc, RTC_STATUS_B) &
368 RTC_B_24H);
370 if (_12h_mode) {
371 /* The RTC is working in 12h mode, check if it is AM or PM */
372 if (t->tm_hour & 0x80) {
373 /* PM flag is active, it must be cleared */
374 t->tm_hour &= ~0x80;
375 pm_mode = true;
379 /* Check if the RTC is working in BCD mode */
380 bcd_mode = !(rtc_register_read(rtc, RTC_STATUS_B) & RTC_B_BCD);
382 if (bcd_mode) {
383 t->tm_sec = bcd2bin(t->tm_sec);
384 t->tm_min = bcd2bin(t->tm_min);
385 t->tm_hour = bcd2bin(t->tm_hour);
386 t->tm_mday = bcd2bin(t->tm_mday);
387 t->tm_mon = bcd2bin(t->tm_mon);
388 t->tm_year = bcd2bin(t->tm_year);
391 if (_12h_mode) {
392 /* Convert to 24h mode */
393 if (pm_mode) {
394 if (t->tm_hour < 12)
395 t->tm_hour += 12;
396 } else if (t->tm_hour == 12)
397 t->tm_hour = 0;
400 /* Count the months starting from 0, not from 1 */
401 t->tm_mon--;
403 if (t->tm_year < 100) {
404 /* tm_year is the number of years since 1900 but the
405 * RTC epoch is 2000.
407 t->tm_year += 100;
410 /* Try to normalize the content of the tm structure */
411 time_t r = mktime(t);
413 rtc->boottime = r - uptime_get();
415 fibril_mutex_unlock(&rtc->mutex);
417 return r < 0 ? EINVAL : EOK;
420 /** Set the time in the RTC
422 * @param fun The RTC function
423 * @param t The time value to set
425 * @return EOK or a negative error code
427 static int
428 rtc_time_set(ddf_fun_t *fun, struct tm *t)
430 bool bcd_mode;
431 time_t norm_time;
432 time_t uptime;
433 int reg_b;
434 int reg_a;
435 int epoch;
436 rtc_t *rtc = fun_rtc(fun);
438 /* Try to normalize the content of the tm structure */
439 if ((norm_time = mktime(t)) < 0)
440 return EINVAL;
442 uptime = uptime_get();
443 if (norm_time <= uptime) {
444 /* This is not acceptable */
445 return EINVAL;
448 fibril_mutex_lock(&rtc->mutex);
450 if (!is_battery_ok(rtc)) {
451 fibril_mutex_unlock(&rtc->mutex);
452 return EIO;
455 /* boottime must be recomputed */
456 rtc->boottime = 0;
458 /* Detect the RTC epoch */
459 if (rtc_register_read(rtc, RTC_YEAR) < 100)
460 epoch = 2000;
461 else
462 epoch = 1900;
464 if (epoch == 2000 && t->tm_year < 100) {
465 /* Can't set a year before the epoch */
466 fibril_mutex_unlock(&rtc->mutex);
467 return EINVAL;
470 t->tm_mon++; /* counts from 1, not from 0 */
472 reg_b = rtc_register_read(rtc, RTC_STATUS_B);
474 if (!(reg_b & RTC_B_24H)) {
475 /* Force 24h mode of operation */
476 reg_b |= RTC_B_24H;
477 rtc_register_write(rtc, RTC_STATUS_B, reg_b);
480 if (epoch == 2000) {
481 /* The RTC epoch is year 2000 but the tm_year
482 * field counts years since 1900.
484 t->tm_year -= 100;
487 /* Check if the rtc is working in bcd mode */
488 bcd_mode = !(reg_b & RTC_B_BCD);
489 if (bcd_mode) {
490 /* Convert the tm struct fields in BCD mode */
491 t->tm_sec = bin2bcd(t->tm_sec);
492 t->tm_min = bin2bcd(t->tm_min);
493 t->tm_hour = bin2bcd(t->tm_hour);
494 t->tm_mday = bin2bcd(t->tm_mday);
495 t->tm_mon = bin2bcd(t->tm_mon);
496 t->tm_year = bin2bcd(t->tm_year);
499 /* Inhibit updates */
500 rtc_register_write(rtc, RTC_STATUS_B, reg_b | RTC_B_INH);
502 /* Write current time to RTC */
503 rtc_register_write(rtc, RTC_SEC, t->tm_sec);
504 rtc_register_write(rtc, RTC_MIN, t->tm_min);
505 rtc_register_write(rtc, RTC_HOUR, t->tm_hour);
506 rtc_register_write(rtc, RTC_DAY, t->tm_mday);
507 rtc_register_write(rtc, RTC_MON, t->tm_mon);
508 rtc_register_write(rtc, RTC_YEAR, t->tm_year);
510 /* Stop the clock */
511 reg_a = rtc_register_read(rtc, RTC_STATUS_A);
512 rtc_register_write(rtc, RTC_STATUS_A, RTC_A_CLK_STOP | reg_a);
514 /* Enable updates */
515 rtc_register_write(rtc, RTC_STATUS_B, reg_b);
516 rtc_register_write(rtc, RTC_STATUS_A, reg_a);
518 fibril_mutex_unlock(&rtc->mutex);
520 return EOK;
523 /** Get the status of the real time clock battery
525 * @param fun The RTC function
526 * @param status The status of the battery
528 * @return EOK on success or a negative error code
530 static int
531 rtc_battery_status_get(ddf_fun_t *fun, battery_status_t *status)
533 rtc_t *rtc = fun_rtc(fun);
535 fibril_mutex_lock(&rtc->mutex);
536 const bool batt_ok = is_battery_ok(rtc);
537 fibril_mutex_unlock(&rtc->mutex);
539 *status = batt_ok ? BATTERY_OK : BATTERY_LOW;
541 return EOK;
544 /** Check if the battery is working properly or not.
545 * The caller already holds the rtc->mutex lock.
547 * @param rtc The RTC instance.
549 * @return true if the battery is ok, false otherwise.
551 static bool
552 is_battery_ok(rtc_t *rtc)
554 return rtc_register_read(rtc, RTC_STATUS_D) & RTC_D_BATTERY_OK;
557 /** The dev_add callback of the rtc driver
559 * @param dev The RTC device
561 * @return EOK on success or a negative error code
563 static int
564 rtc_dev_add(ddf_dev_t *dev)
566 rtc_t *rtc;
567 ddf_fun_t *fun = NULL;
568 int rc;
569 bool need_cleanup = false;
571 ddf_msg(LVL_DEBUG, "rtc_dev_add %s (handle = %d)",
572 ddf_dev_get_name(dev), (int) ddf_dev_get_handle(dev));
574 rtc = ddf_dev_data_alloc(dev, sizeof(rtc_t));
575 if (!rtc)
576 return ENOMEM;
578 rtc->dev = dev;
579 fibril_mutex_initialize(&rtc->mutex);
581 rc = rtc_dev_initialize(rtc);
582 if (rc != EOK)
583 goto error;
585 need_cleanup = true;
587 if (!rtc_pio_enable(rtc)) {
588 rc = EADDRNOTAVAIL;
589 goto error;
592 fun = ddf_fun_create(dev, fun_exposed, "a");
593 if (!fun) {
594 ddf_msg(LVL_ERROR, "Failed creating function");
595 rc = ENOENT;
596 goto error;
599 ddf_fun_set_ops(fun, &rtc_dev_ops);
600 rc = ddf_fun_bind(fun);
601 if (rc != EOK) {
602 ddf_msg(LVL_ERROR, "Failed binding function");
603 goto error;
606 rtc->fun = fun;
608 ddf_fun_add_to_category(fun, "clock");
610 ddf_msg(LVL_NOTE, "Device %s successfully initialized",
611 ddf_dev_get_name(dev));
613 return rc;
615 error:
616 if (fun)
617 ddf_fun_destroy(fun);
618 if (need_cleanup)
619 rtc_dev_cleanup(rtc);
620 return rc;
623 /** The dev_remove callback for the rtc driver
625 * @param dev The RTC device
627 * @return EOK on success or a negative error code
629 static int
630 rtc_dev_remove(ddf_dev_t *dev)
632 rtc_t *rtc = dev_rtc(dev);
633 int rc;
635 fibril_mutex_lock(&rtc->mutex);
636 if (rtc->clients_connected > 0) {
637 fibril_mutex_unlock(&rtc->mutex);
638 return EBUSY;
641 rtc->removed = true;
642 fibril_mutex_unlock(&rtc->mutex);
644 rc = rtc_fun_offline(rtc->fun);
645 if (rc != EOK) {
646 ddf_msg(LVL_ERROR, "Failed to offline function");
647 return rc;
650 rc = ddf_fun_unbind(rtc->fun);
651 if (rc != EOK) {
652 ddf_msg(LVL_ERROR, "Failed to unbind function");
653 return rc;
656 ddf_fun_destroy(rtc->fun);
657 rtc_dev_cleanup(rtc);
659 return rc;
662 /** Open the device
664 * @param fun The function node
666 * @return EOK on success or a negative error code
668 static int
669 rtc_open(ddf_fun_t *fun)
671 int rc;
672 rtc_t *rtc = fun_rtc(fun);
674 fibril_mutex_lock(&rtc->mutex);
676 if (rtc->removed)
677 rc = ENXIO;
678 else {
679 rc = EOK;
680 rtc->clients_connected++;
683 fibril_mutex_unlock(&rtc->mutex);
684 return rc;
687 /** Close the device
689 * @param fun The function node
691 static void
692 rtc_close(ddf_fun_t *fun)
694 rtc_t *rtc = fun_rtc(fun);
696 fibril_mutex_lock(&rtc->mutex);
698 rtc->clients_connected--;
699 assert(rtc->clients_connected >= 0);
701 fibril_mutex_unlock(&rtc->mutex);
704 /** Convert from BCD mode to binary mode
706 * @param bcd The number in BCD format to convert
708 * @return The converted value
710 static unsigned
711 bcd2bin(unsigned bcd)
713 return ((bcd & 0xF0) >> 1) + ((bcd & 0xF0) >> 3) + (bcd & 0xf);
716 /** Convert from binary mode to BCD mode
718 * @param bcd The number in binary mode to convert
720 * @return The converted value
722 static unsigned
723 bin2bcd(unsigned binary)
725 return ((binary / 10) << 4) + (binary % 10);
728 static time_t
729 uptime_get(void)
731 struct timeval tv;
733 getuptime(&tv);
735 return tv.tv_sec;
738 static int
739 rtc_fun_online(ddf_fun_t *fun)
741 ddf_msg(LVL_DEBUG, "rtc_fun_online()");
742 return ddf_fun_online(fun);
745 static int
746 rtc_fun_offline(ddf_fun_t *fun)
748 ddf_msg(LVL_DEBUG, "rtc_fun_offline()");
749 return ddf_fun_offline(fun);
753 main(int argc, char **argv)
755 printf(NAME ": HelenOS RTC driver\n");
756 rtc_init();
757 return ddf_driver_main(&rtc_driver);
761 * @}