HelenOS sources
This source file includes following definitions.
- dev_rtc
- fun_rtc
- rtc_init
- rtc_dev_cleanup
- rtc_pio_enable
- rtc_dev_initialize
- rtc_register_read
- rtc_register_write
- rtc_update_in_progress
- rtc_time_get
- rtc_time_set
- rtc_battery_status_get
- is_battery_ok
- rtc_dev_add
- rtc_dev_remove
- rtc_open
- rtc_close
- bcd2bin
- bin2bcd
- rtc_fun_online
- rtc_fun_offline
- main
#include <errno.h>
#include <ddi.h>
#include <as.h>
#include <barrier.h>
#include <stdio.h>
#include <ddf/driver.h>
#include <ddf/log.h>
#include <ops/clock_dev.h>
#include <ops/battery_dev.h>
#include <fibril_synch.h>
#include <device/hw_res.h>
#include <macros.h>
#include <time.h>
#include "cmos-regs.h"
#define NAME "cmos-rtc"
#define REG_COUNT 2
#define REG_SEL_PORT(port) (port)
#define REG_RW_PORT(port) ((port) + 1)
typedef struct rtc {
ddf_dev_t *dev;
ddf_fun_t *fun;
fibril_mutex_t mutex;
ioport8_t *io_addr;
ioport8_t *port;
bool removed;
int clients_connected;
struct timespec boot_time;
} rtc_t;
static rtc_t *dev_rtc(ddf_dev_t *dev);
static rtc_t *fun_rtc(ddf_fun_t *fun);
static errno_t
rtc_battery_status_get(ddf_fun_t *fun, battery_status_t *status);
static errno_t rtc_time_get(ddf_fun_t *fun, struct tm *t);
static errno_t rtc_time_set(ddf_fun_t *fun, struct tm *t);
static errno_t rtc_dev_add(ddf_dev_t *dev);
static errno_t rtc_dev_initialize(rtc_t *rtc);
static bool rtc_pio_enable(rtc_t *rtc);
static void rtc_dev_cleanup(rtc_t *rtc);
static errno_t rtc_open(ddf_fun_t *fun);
static void rtc_close(ddf_fun_t *fun);
static bool rtc_update_in_progress(rtc_t *rtc);
static int rtc_register_read(rtc_t *rtc, int reg);
static unsigned bcd2bin(unsigned bcd);
static unsigned bin2bcd(unsigned binary);
static errno_t rtc_dev_remove(ddf_dev_t *dev);
static void rtc_register_write(rtc_t *rtc, int reg, int data);
static bool is_battery_ok(rtc_t *rtc);
static errno_t rtc_fun_online(ddf_fun_t *fun);
static errno_t rtc_fun_offline(ddf_fun_t *fun);
static ddf_dev_ops_t rtc_dev_ops;
static driver_ops_t rtc_ops = {
.dev_add = rtc_dev_add,
.dev_remove = rtc_dev_remove,
.fun_online = rtc_fun_online,
.fun_offline = rtc_fun_offline,
};
static driver_t rtc_driver = {
.name = NAME,
.driver_ops = &rtc_ops,
};
static clock_dev_ops_t rtc_clock_dev_ops = {
.time_get = rtc_time_get,
.time_set = rtc_time_set,
};
static battery_dev_ops_t rtc_battery_dev_ops = {
.battery_status_get = rtc_battery_status_get,
.battery_charge_level_get = NULL,
};
static rtc_t *
dev_rtc(ddf_dev_t *dev)
{
return ddf_dev_data_get(dev);
}
static rtc_t *
fun_rtc(ddf_fun_t *fun)
{
return dev_rtc(ddf_fun_get_dev(fun));
}
static void
rtc_init(void)
{
ddf_log_init(NAME);
rtc_dev_ops.open = rtc_open;
rtc_dev_ops.close = rtc_close;
rtc_dev_ops.interfaces[CLOCK_DEV_IFACE] = &rtc_clock_dev_ops;
rtc_dev_ops.interfaces[BATTERY_DEV_IFACE] = &rtc_battery_dev_ops;
rtc_dev_ops.default_handler = NULL;
}
static void
rtc_dev_cleanup(rtc_t *rtc)
{
}
static bool
rtc_pio_enable(rtc_t *rtc)
{
if (pio_enable((void *) rtc->io_addr, REG_COUNT,
(void **) &rtc->port)) {
ddf_msg(LVL_ERROR, "Cannot map the port %lx"
" for device %s", (long unsigned int)rtc->io_addr,
ddf_dev_get_name(rtc->dev));
return false;
}
return true;
}
static errno_t
rtc_dev_initialize(rtc_t *rtc)
{
errno_t rc;
size_t i;
hw_resource_t *res;
bool ioport = false;
async_sess_t *parent_sess;
ddf_msg(LVL_DEBUG, "rtc_dev_initialize %s", ddf_dev_get_name(rtc->dev));
rtc->boot_time.tv_sec = 0;
rtc->boot_time.tv_nsec = 0;
rtc->clients_connected = 0;
hw_resource_list_t hw_resources;
memset(&hw_resources, 0, sizeof(hw_resource_list_t));
parent_sess = ddf_dev_parent_sess_get(rtc->dev);
if (parent_sess == NULL) {
ddf_msg(LVL_ERROR, "Failed to connect to parent driver\
of device %s.", ddf_dev_get_name(rtc->dev));
rc = ENOENT;
goto error;
}
rc = hw_res_get_resource_list(parent_sess, &hw_resources);
if (rc != EOK) {
ddf_msg(LVL_ERROR, "Failed to get HW resources\
for device %s", ddf_dev_get_name(rtc->dev));
goto error;
}
for (i = 0; i < hw_resources.count; ++i) {
res = &hw_resources.resources[i];
if (res->res.io_range.size < REG_COUNT) {
ddf_msg(LVL_ERROR, "I/O range assigned to \
device %s is too small",
ddf_dev_get_name(rtc->dev));
rc = ELIMIT;
continue;
}
rtc->io_addr = (ioport8_t *) (long) res->res.io_range.address;
ioport = true;
ddf_msg(LVL_NOTE, "Device %s was assigned I/O address "
"0x%lx", ddf_dev_get_name(rtc->dev),
(unsigned long int) rtc->io_addr);
rc = EOK;
break;
}
if (rc != EOK)
goto error;
if (!ioport) {
ddf_msg(LVL_ERROR, "Missing HW resource for device %s",
ddf_dev_get_name(rtc->dev));
rc = ENOENT;
goto error;
}
hw_res_clean_resource_list(&hw_resources);
return EOK;
error:
rtc_dev_cleanup(rtc);
hw_res_clean_resource_list(&hw_resources);
return rc;
}
static int
rtc_register_read(rtc_t *rtc, int reg)
{
pio_write_8(REG_SEL_PORT(rtc->port), reg);
return pio_read_8(REG_RW_PORT(rtc->port));
}
static void
rtc_register_write(rtc_t *rtc, int reg, int data)
{
pio_write_8(REG_SEL_PORT(rtc->port), reg);
pio_write_8(REG_RW_PORT(rtc->port), data);
}
static bool
rtc_update_in_progress(rtc_t *rtc)
{
return rtc_register_read(rtc, RTC_STATUS_A) & RTC_A_UPDATE;
}
static errno_t
rtc_time_get(ddf_fun_t *fun, struct tm *t)
{
bool bcd_mode;
bool pm_mode = false;
rtc_t *rtc = fun_rtc(fun);
fibril_mutex_lock(&rtc->mutex);
if (rtc->boot_time.tv_sec) {
struct timespec curtime;
getuptime(&curtime);
ts_add(&curtime, &rtc->boot_time);
fibril_mutex_unlock(&rtc->mutex);
return time_ts2tm(&curtime, t);
}
if (!is_battery_ok(rtc)) {
fibril_mutex_unlock(&rtc->mutex);
return EIO;
}
t->tm_nsec = 0;
do {
while (rtc_update_in_progress(rtc))
;
t->tm_sec = rtc_register_read(rtc, RTC_SEC);
t->tm_min = rtc_register_read(rtc, RTC_MIN);
t->tm_hour = rtc_register_read(rtc, RTC_HOUR);
t->tm_mday = rtc_register_read(rtc, RTC_DAY);
t->tm_mon = rtc_register_read(rtc, RTC_MON);
t->tm_year = rtc_register_read(rtc, RTC_YEAR);
} while (t->tm_sec != rtc_register_read(rtc, RTC_SEC) ||
t->tm_min != rtc_register_read(rtc, RTC_MIN) ||
t->tm_mday != rtc_register_read(rtc, RTC_DAY) ||
t->tm_mon != rtc_register_read(rtc, RTC_MON) ||
t->tm_year != rtc_register_read(rtc, RTC_YEAR));
bool _12h_mode = !(rtc_register_read(rtc, RTC_STATUS_B) &
RTC_B_24H);
if (_12h_mode) {
if (t->tm_hour & 0x80) {
t->tm_hour &= ~0x80;
pm_mode = true;
}
}
bcd_mode = !(rtc_register_read(rtc, RTC_STATUS_B) & RTC_B_BCD);
if (bcd_mode) {
t->tm_sec = bcd2bin(t->tm_sec);
t->tm_min = bcd2bin(t->tm_min);
t->tm_hour = bcd2bin(t->tm_hour);
t->tm_mday = bcd2bin(t->tm_mday);
t->tm_mon = bcd2bin(t->tm_mon);
t->tm_year = bcd2bin(t->tm_year);
}
if (_12h_mode) {
if (pm_mode) {
if (t->tm_hour < 12)
t->tm_hour += 12;
} else if (t->tm_hour == 12)
t->tm_hour = 0;
}
t->tm_mon--;
if (t->tm_year < 100) {
t->tm_year += 100;
}
time_t r = mktime(t);
errno_t result;
if (r < 0)
result = EINVAL;
else {
struct timespec uptime;
getuptime(&uptime);
rtc->boot_time.tv_sec = r;
rtc->boot_time.tv_nsec = t->tm_nsec;
ts_sub(&rtc->boot_time, &uptime);
result = EOK;
}
fibril_mutex_unlock(&rtc->mutex);
return result;
}
static errno_t
rtc_time_set(ddf_fun_t *fun, struct tm *t)
{
bool bcd_mode;
time_t norm_time;
struct timespec uptime;
struct timespec ntv;
int reg_b;
int reg_a;
int epoch;
rtc_t *rtc = fun_rtc(fun);
if ((norm_time = mktime(t)) < 0)
return EINVAL;
ntv.tv_sec = norm_time;
ntv.tv_nsec = t->tm_nsec;
getuptime(&uptime);
if (ts_gteq(&uptime, &ntv)) {
return EINVAL;
}
fibril_mutex_lock(&rtc->mutex);
if (!is_battery_ok(rtc)) {
fibril_mutex_unlock(&rtc->mutex);
return EIO;
}
rtc->boot_time.tv_sec = 0;
rtc->boot_time.tv_nsec = 0;
if (rtc_register_read(rtc, RTC_YEAR) < 100)
epoch = 2000;
else
epoch = 1900;
if (epoch == 2000 && t->tm_year < 100) {
fibril_mutex_unlock(&rtc->mutex);
return EINVAL;
}
t->tm_mon++;
reg_b = rtc_register_read(rtc, RTC_STATUS_B);
if (!(reg_b & RTC_B_24H)) {
reg_b |= RTC_B_24H;
rtc_register_write(rtc, RTC_STATUS_B, reg_b);
}
if (epoch == 2000) {
t->tm_year -= 100;
}
bcd_mode = !(reg_b & RTC_B_BCD);
if (bcd_mode) {
t->tm_sec = bin2bcd(t->tm_sec);
t->tm_min = bin2bcd(t->tm_min);
t->tm_hour = bin2bcd(t->tm_hour);
t->tm_mday = bin2bcd(t->tm_mday);
t->tm_mon = bin2bcd(t->tm_mon);
t->tm_year = bin2bcd(t->tm_year);
}
rtc_register_write(rtc, RTC_STATUS_B, reg_b | RTC_B_INH);
rtc_register_write(rtc, RTC_SEC, t->tm_sec);
rtc_register_write(rtc, RTC_MIN, t->tm_min);
rtc_register_write(rtc, RTC_HOUR, t->tm_hour);
rtc_register_write(rtc, RTC_DAY, t->tm_mday);
rtc_register_write(rtc, RTC_MON, t->tm_mon);
rtc_register_write(rtc, RTC_YEAR, t->tm_year);
reg_a = rtc_register_read(rtc, RTC_STATUS_A);
rtc_register_write(rtc, RTC_STATUS_A, RTC_A_CLK_STOP | reg_a);
rtc_register_write(rtc, RTC_STATUS_B, reg_b);
rtc_register_write(rtc, RTC_STATUS_A, reg_a);
fibril_mutex_unlock(&rtc->mutex);
return EOK;
}
static errno_t
rtc_battery_status_get(ddf_fun_t *fun, battery_status_t *status)
{
rtc_t *rtc = fun_rtc(fun);
fibril_mutex_lock(&rtc->mutex);
const bool batt_ok = is_battery_ok(rtc);
fibril_mutex_unlock(&rtc->mutex);
*status = batt_ok ? BATTERY_OK : BATTERY_LOW;
return EOK;
}
static bool
is_battery_ok(rtc_t *rtc)
{
return rtc_register_read(rtc, RTC_STATUS_D) & RTC_D_BATTERY_OK;
}
static errno_t
rtc_dev_add(ddf_dev_t *dev)
{
rtc_t *rtc;
ddf_fun_t *fun = NULL;
errno_t rc;
bool need_cleanup = false;
bool bound = false;
ddf_msg(LVL_DEBUG, "rtc_dev_add %s (handle = %d)",
ddf_dev_get_name(dev), (int) ddf_dev_get_handle(dev));
rtc = ddf_dev_data_alloc(dev, sizeof(rtc_t));
if (!rtc)
return ENOMEM;
rtc->dev = dev;
fibril_mutex_initialize(&rtc->mutex);
rc = rtc_dev_initialize(rtc);
if (rc != EOK)
goto error;
need_cleanup = true;
if (!rtc_pio_enable(rtc)) {
rc = EADDRNOTAVAIL;
goto error;
}
fun = ddf_fun_create(dev, fun_exposed, "a");
if (!fun) {
ddf_msg(LVL_ERROR, "Failed creating function");
rc = ENOENT;
goto error;
}
ddf_fun_set_ops(fun, &rtc_dev_ops);
rc = ddf_fun_bind(fun);
if (rc != EOK) {
ddf_msg(LVL_ERROR, "Failed binding function");
goto error;
}
bound = true;
rtc->fun = fun;
rc = ddf_fun_add_to_category(fun, "clock");
if (rc != EOK) {
ddf_msg(LVL_ERROR, "Failed adding service to clock category.");
goto error;
}
ddf_msg(LVL_NOTE, "Device %s successfully initialized",
ddf_dev_get_name(dev));
return rc;
error:
if (bound)
ddf_fun_unbind(fun);
if (fun)
ddf_fun_destroy(fun);
if (need_cleanup)
rtc_dev_cleanup(rtc);
return rc;
}
static errno_t
rtc_dev_remove(ddf_dev_t *dev)
{
rtc_t *rtc = dev_rtc(dev);
errno_t rc;
fibril_mutex_lock(&rtc->mutex);
if (rtc->clients_connected > 0) {
fibril_mutex_unlock(&rtc->mutex);
return EBUSY;
}
rtc->removed = true;
fibril_mutex_unlock(&rtc->mutex);
rc = rtc_fun_offline(rtc->fun);
if (rc != EOK) {
ddf_msg(LVL_ERROR, "Failed to offline function");
return rc;
}
rc = ddf_fun_unbind(rtc->fun);
if (rc != EOK) {
ddf_msg(LVL_ERROR, "Failed to unbind function");
return rc;
}
ddf_fun_destroy(rtc->fun);
rtc_dev_cleanup(rtc);
return rc;
}
static errno_t
rtc_open(ddf_fun_t *fun)
{
errno_t rc;
rtc_t *rtc = fun_rtc(fun);
fibril_mutex_lock(&rtc->mutex);
if (rtc->removed)
rc = ENXIO;
else {
rc = EOK;
rtc->clients_connected++;
}
fibril_mutex_unlock(&rtc->mutex);
return rc;
}
static void
rtc_close(ddf_fun_t *fun)
{
rtc_t *rtc = fun_rtc(fun);
fibril_mutex_lock(&rtc->mutex);
rtc->clients_connected--;
assert(rtc->clients_connected >= 0);
fibril_mutex_unlock(&rtc->mutex);
}
static unsigned
bcd2bin(unsigned bcd)
{
return ((bcd & 0xF0) >> 1) + ((bcd & 0xF0) >> 3) + (bcd & 0xf);
}
static unsigned
bin2bcd(unsigned binary)
{
return ((binary / 10) << 4) + (binary % 10);
}
static errno_t
rtc_fun_online(ddf_fun_t *fun)
{
errno_t rc;
ddf_msg(LVL_DEBUG, "rtc_fun_online()");
rc = ddf_fun_online(fun);
if (rc == EOK) {
rc = ddf_fun_add_to_category(fun, "clock");
}
return rc;
}
static errno_t
rtc_fun_offline(ddf_fun_t *fun)
{
ddf_msg(LVL_DEBUG, "rtc_fun_offline()");
return ddf_fun_offline(fun);
}
int
main(int argc, char **argv)
{
printf(NAME ": HelenOS RTC driver\n");
rtc_init();
return ddf_driver_main(&rtc_driver);
}
HelenOS homepage, sources at GitHub