2003-10-24 14:21:10 +04:00
|
|
|
/*
|
2004-11-08 14:48:04 +03:00
|
|
|
** Copyright 2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
|
2003-10-24 14:21:10 +04:00
|
|
|
** Copyright 2003, Jeff Ward, jeff@r2d2.stcloudstate.edu. All rights reserved.
|
2004-11-08 14:48:04 +03:00
|
|
|
** Distributed under the terms of the Haiku License.
|
2003-10-24 14:21:10 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2004-11-08 14:48:04 +03:00
|
|
|
#include <KernelExport.h>
|
2003-10-24 14:21:10 +04:00
|
|
|
|
|
|
|
#include <arch/real_time_clock.h>
|
|
|
|
#include <real_time_clock.h>
|
2004-11-08 14:48:04 +03:00
|
|
|
#include <real_time_data.h>
|
|
|
|
#include <vm_types.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2003-10-24 14:21:10 +04:00
|
|
|
|
2003-10-24 16:01:25 +04:00
|
|
|
|
2004-11-08 14:48:04 +03:00
|
|
|
static struct real_time_data *sRealTimeData;
|
2003-10-24 16:01:25 +04:00
|
|
|
|
2003-10-24 14:21:10 +04:00
|
|
|
|
|
|
|
/** Write the system time to CMOS. */
|
|
|
|
|
2003-10-24 16:01:25 +04:00
|
|
|
static void
|
|
|
|
rtc_system_to_hw(void)
|
|
|
|
{
|
2003-10-24 14:21:10 +04:00
|
|
|
uint32 seconds;
|
2003-10-24 16:01:25 +04:00
|
|
|
|
2004-11-08 14:48:04 +03:00
|
|
|
seconds = (sRealTimeData->boot_time + system_time()) / 1000000;
|
2003-10-24 14:21:10 +04:00
|
|
|
arch_rtc_set_hw_time(seconds);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Read the CMOS clock and update the system time accordingly. */
|
|
|
|
|
2003-10-24 16:01:25 +04:00
|
|
|
static void
|
|
|
|
rtc_hw_to_system(void)
|
|
|
|
{
|
2003-10-24 14:21:10 +04:00
|
|
|
uint32 current_time;
|
2003-10-24 16:01:25 +04:00
|
|
|
|
2003-10-24 14:21:10 +04:00
|
|
|
current_time = arch_rtc_get_hw_time();
|
2003-10-28 16:12:13 +03:00
|
|
|
set_real_time_clock(current_time);
|
2003-10-24 14:21:10 +04:00
|
|
|
}
|
|
|
|
|
2003-10-24 16:01:25 +04:00
|
|
|
|
2003-10-24 14:21:10 +04:00
|
|
|
bigtime_t
|
2003-10-24 16:01:25 +04:00
|
|
|
rtc_boot_time(void)
|
|
|
|
{
|
2004-11-08 14:48:04 +03:00
|
|
|
return sRealTimeData->boot_time;
|
2003-10-24 14:21:10 +04:00
|
|
|
}
|
2003-10-24 16:09:20 +04:00
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
rtc_print(void)
|
|
|
|
{
|
|
|
|
uint32 currentTime;
|
|
|
|
|
2004-11-08 14:48:04 +03:00
|
|
|
currentTime = (sRealTimeData->boot_time + system_time()) / 1000000;
|
2004-03-15 00:53:48 +03:00
|
|
|
dprintf("system_time: %Ld\n", system_time());
|
2004-11-08 14:48:04 +03:00
|
|
|
dprintf("boot_time: %Ld\n", sRealTimeData->boot_time);
|
2004-03-29 03:52:11 +04:00
|
|
|
dprintf("current_time: %lu\n", currentTime);
|
2003-10-24 16:09:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
rtc_debug(int argc, char **argv)
|
|
|
|
{
|
|
|
|
if (argc < 2) {
|
|
|
|
// If no arguments were given, output all usefull data.
|
|
|
|
rtc_print();
|
|
|
|
} else {
|
|
|
|
// If there was an argument, reset the system and hw time.
|
2003-10-28 16:12:13 +03:00
|
|
|
set_real_time_clock(strtoul(argv[1], NULL, 10));
|
2003-10-24 16:09:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
2004-11-09 16:50:52 +03:00
|
|
|
rtc_init(kernel_args *args)
|
2003-10-24 16:09:20 +04:00
|
|
|
{
|
2004-11-09 15:44:53 +03:00
|
|
|
void *clonedRealTimeData;
|
2004-11-09 16:50:52 +03:00
|
|
|
|
2004-11-08 14:48:04 +03:00
|
|
|
area_id area = create_area("real time data", (void **)&sRealTimeData, B_ANY_KERNEL_ADDRESS,
|
|
|
|
PAGE_ALIGN(sizeof(struct real_time_data)), B_FULL_LOCK,
|
|
|
|
B_KERNEL_READ_AREA | B_KERNEL_WRITE_AREA);
|
|
|
|
if (area < B_OK) {
|
2004-11-09 15:44:53 +03:00
|
|
|
panic("rtc_init: error creating real time data area\n");
|
2004-11-08 14:48:04 +03:00
|
|
|
return area;
|
2004-11-06 20:37:00 +03:00
|
|
|
}
|
2004-11-09 15:44:53 +03:00
|
|
|
|
2004-11-09 16:50:52 +03:00
|
|
|
// On some systems like x86, a page cannot be read-only in userland and writable
|
|
|
|
// in the kernel. Therefore, we clone the real time data area here for user
|
|
|
|
// access; it doesn't hurt on other platforms, too.
|
|
|
|
// The area is used to share time critical information, such as the system
|
|
|
|
// time conversion factor which can change at any time.
|
|
|
|
|
|
|
|
if (clone_area("real time data userland", &clonedRealTimeData, B_ANY_KERNEL_ADDRESS,
|
2004-11-09 15:44:53 +03:00
|
|
|
B_READ_AREA, area) < B_OK) {
|
|
|
|
dprintf("rtc_init: error creating real time data userland area\n");
|
|
|
|
// we don't panic because it's not kernel critical
|
|
|
|
}
|
2004-11-06 20:37:00 +03:00
|
|
|
|
2004-11-09 16:50:52 +03:00
|
|
|
// ToDo: initialization of the system time conversion factor is an x86 thing
|
|
|
|
// and should be moved there
|
|
|
|
sRealTimeData->system_time_conversion_factor = args->arch_args.system_time_cv_factor;
|
|
|
|
|
2003-10-24 16:09:20 +04:00
|
|
|
rtc_hw_to_system();
|
|
|
|
|
2004-11-08 14:48:04 +03:00
|
|
|
add_debugger_command("rtc", &rtc_debug, "Set and test the real-time clock");
|
2003-10-24 16:09:20 +04:00
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
2003-10-24 18:53:02 +04:00
|
|
|
|
|
|
|
// #pragma mark -
|
|
|
|
// public kernel API
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
set_real_time_clock(uint32 currentTime)
|
|
|
|
{
|
2004-11-08 14:48:04 +03:00
|
|
|
sRealTimeData->boot_time = currentTime * 1000000LL - system_time();
|
2004-03-15 00:53:48 +03:00
|
|
|
rtc_system_to_hw();
|
2003-10-24 18:53:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-08 05:13:19 +03:00
|
|
|
uint32
|
|
|
|
real_time_clock(void)
|
|
|
|
{
|
|
|
|
// ToDo: implement me - they might be used directly from libroot/os/time.c
|
2004-11-08 14:48:04 +03:00
|
|
|
return (sRealTimeData->boot_time + system_time()) / 1000000;
|
2003-11-08 05:13:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bigtime_t
|
|
|
|
real_time_clock_usecs(void)
|
|
|
|
{
|
|
|
|
// ToDo: implement me - they might be used directly from libroot/os/time.c
|
2004-11-08 14:48:04 +03:00
|
|
|
return sRealTimeData->boot_time + system_time();
|
2003-11-08 05:13:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-24 18:53:02 +04:00
|
|
|
// #pragma mark -
|
|
|
|
// public userland API
|
|
|
|
|
|
|
|
|
2004-11-05 19:08:00 +03:00
|
|
|
status_t
|
2003-10-24 18:53:02 +04:00
|
|
|
_user_set_real_time_clock(uint32 time)
|
|
|
|
{
|
2004-11-05 19:08:00 +03:00
|
|
|
if (geteuid() != 0)
|
|
|
|
return B_NOT_ALLOWED;
|
|
|
|
|
2003-10-24 18:53:02 +04:00
|
|
|
set_real_time_clock(time);
|
2004-11-05 19:08:00 +03:00
|
|
|
return B_OK;
|
2003-10-24 18:53:02 +04:00
|
|
|
}
|
|
|
|
|