2 * Copyright (c) 2012 Maurizio Lombardi
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
30 * @defgroup CMOS RTC driver.
31 * @brief HelenOS RTC driver.
42 #include <libarch/ddi.h>
43 #include <libarch/barrier.h>
45 #include <ddf/driver.h>
47 #include <ops/clock_dev.h>
48 #include <ops/battery_dev.h>
49 #include <fibril_synch.h>
50 #include <device/hw_res.h>
54 #include "cmos-regs.h"
56 #define NAME "cmos-rtc"
60 #define REG_SEL_PORT(port) (port)
61 #define REG_RW_PORT(port) ((port) + 1)
64 /** DDF device node */
66 /** DDF function node */
68 /** The fibril mutex for synchronizing the access to the device */
70 /** The base I/O address of the device registers */
72 /** The I/O port used to access the CMOS registers */
74 /** true if device is removed */
76 /** number of connected clients */
77 int clients_connected
;
78 /** time at which the system booted */
82 static rtc_t
*dev_rtc(ddf_dev_t
*dev
);
83 static rtc_t
*fun_rtc(ddf_fun_t
*fun
);
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
= {
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 */
135 dev_rtc(ddf_dev_t
*dev
)
137 return ddf_dev_data_get(dev
);
140 /** Obtain soft state structure from function node */
142 fun_rtc(ddf_fun_t
*fun
)
144 return dev_rtc(ddf_fun_get_dev(fun
));
147 /** Initialize the RTC driver */
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
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
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
));
191 /** Initialize the RTC device
193 * @param rtc Pointer to the RTC device
195 * @return EOK on success or a negative error code
198 rtc_dev_initialize(rtc_t
*rtc
)
204 async_sess_t
*parent_sess
;
206 ddf_msg(LVL_DEBUG
, "rtc_dev_initialize %s", ddf_dev_get_name(rtc
->dev
));
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
);
218 ddf_msg(LVL_ERROR
, "Failed to connect to parent driver\
219 of device %s.", ddf_dev_get_name(rtc
->dev
));
224 /* Get the HW resources */
225 rc
= hw_res_get_resource_list(parent_sess
, &hw_resources
);
227 ddf_msg(LVL_ERROR
, "Failed to get HW resources\
228 for device %s", ddf_dev_get_name(rtc
->dev
));
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
));
243 rtc
->io_addr
= (ioport8_t
*) (long) res
->res
.io_range
.address
;
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
);
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
));
263 hw_res_clean_resource_list(&hw_resources
);
268 rtc_dev_cleanup(rtc
);
269 hw_res_clean_resource_list(&hw_resources
);
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
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
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
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
321 rtc_time_get(ddf_fun_t
*fun
, struct tm
*t
)
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
);
347 /* now read the registers */
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
) &
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 */
379 /* Check if the RTC is working in BCD mode */
380 bcd_mode
= !(rtc_register_read(rtc
, RTC_STATUS_B
) & RTC_B_BCD
);
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
);
392 /* Convert to 24h mode */
396 } else if (t
->tm_hour
== 12)
400 /* Count the months starting from 0, not from 1 */
403 if (t
->tm_year
< 100) {
404 /* tm_year is the number of years since 1900 but the
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
428 rtc_time_set(ddf_fun_t
*fun
, struct tm
*t
)
436 rtc_t
*rtc
= fun_rtc(fun
);
438 /* Try to normalize the content of the tm structure */
439 if ((norm_time
= mktime(t
)) < 0)
442 uptime
= uptime_get();
443 if (norm_time
<= uptime
) {
444 /* This is not acceptable */
448 fibril_mutex_lock(&rtc
->mutex
);
450 if (!is_battery_ok(rtc
)) {
451 fibril_mutex_unlock(&rtc
->mutex
);
455 /* boottime must be recomputed */
458 /* Detect the RTC epoch */
459 if (rtc_register_read(rtc
, RTC_YEAR
) < 100)
464 if (epoch
== 2000 && t
->tm_year
< 100) {
465 /* Can't set a year before the epoch */
466 fibril_mutex_unlock(&rtc
->mutex
);
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 */
477 rtc_register_write(rtc
, RTC_STATUS_B
, reg_b
);
481 /* The RTC epoch is year 2000 but the tm_year
482 * field counts years since 1900.
487 /* Check if the rtc is working in bcd mode */
488 bcd_mode
= !(reg_b
& RTC_B_BCD
);
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
);
511 reg_a
= rtc_register_read(rtc
, RTC_STATUS_A
);
512 rtc_register_write(rtc
, RTC_STATUS_A
, RTC_A_CLK_STOP
| reg_a
);
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
);
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
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
;
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.
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
564 rtc_dev_add(ddf_dev_t
*dev
)
567 ddf_fun_t
*fun
= NULL
;
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
));
579 fibril_mutex_initialize(&rtc
->mutex
);
581 rc
= rtc_dev_initialize(rtc
);
587 if (!rtc_pio_enable(rtc
)) {
592 fun
= ddf_fun_create(dev
, fun_exposed
, "a");
594 ddf_msg(LVL_ERROR
, "Failed creating function");
599 ddf_fun_set_ops(fun
, &rtc_dev_ops
);
600 rc
= ddf_fun_bind(fun
);
602 ddf_msg(LVL_ERROR
, "Failed binding function");
608 ddf_fun_add_to_category(fun
, "clock");
610 ddf_msg(LVL_NOTE
, "Device %s successfully initialized",
611 ddf_dev_get_name(dev
));
617 ddf_fun_destroy(fun
);
619 rtc_dev_cleanup(rtc
);
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
630 rtc_dev_remove(ddf_dev_t
*dev
)
632 rtc_t
*rtc
= dev_rtc(dev
);
635 fibril_mutex_lock(&rtc
->mutex
);
636 if (rtc
->clients_connected
> 0) {
637 fibril_mutex_unlock(&rtc
->mutex
);
642 fibril_mutex_unlock(&rtc
->mutex
);
644 rc
= rtc_fun_offline(rtc
->fun
);
646 ddf_msg(LVL_ERROR
, "Failed to offline function");
650 rc
= ddf_fun_unbind(rtc
->fun
);
652 ddf_msg(LVL_ERROR
, "Failed to unbind function");
656 ddf_fun_destroy(rtc
->fun
);
657 rtc_dev_cleanup(rtc
);
664 * @param fun The function node
666 * @return EOK on success or a negative error code
669 rtc_open(ddf_fun_t
*fun
)
672 rtc_t
*rtc
= fun_rtc(fun
);
674 fibril_mutex_lock(&rtc
->mutex
);
680 rtc
->clients_connected
++;
683 fibril_mutex_unlock(&rtc
->mutex
);
689 * @param fun The function node
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
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
723 bin2bcd(unsigned binary
)
725 return ((binary
/ 10) << 4) + (binary
% 10);
739 rtc_fun_online(ddf_fun_t
*fun
)
741 ddf_msg(LVL_DEBUG
, "rtc_fun_online()");
742 return ddf_fun_online(fun
);
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");
757 return ddf_driver_main(&rtc_driver
);