1/*
2 * Copyright 2006, Ingo Weinhold <bonefish@cs.tu-berlin.de>.
3 * Copyright 2010, Andreas F��rber <andreas.faerber@web.de>
4 * All rights reserved. Distributed under the terms of the MIT License.
5 */
6
7
8#include "real_time_clock.h"
9
10#include <stdio.h>
11
12#include <boot/kernel_args.h>
13#include <boot/stage2.h>
14#include <platform/openfirmware/devices.h>
15#include <platform/openfirmware/openfirmware.h>
16
17
18static int sHandle = OF_FAILED;
19
20
21status_t
22init_real_time_clock(void)
23{
24	// find RTC
25	intptr_t rtcCookie = 0;
26	if (of_get_next_device(&rtcCookie, 0, "rtc",
27			gKernelArgs.platform_args.rtc_path,
28			sizeof(gKernelArgs.platform_args.rtc_path)) != B_OK) {
29		printf("init_real_time_clock(): Found no RTC device!");
30		return B_ERROR;
31	}
32
33	sHandle = of_open(gKernelArgs.platform_args.rtc_path);
34	if (sHandle == OF_FAILED) {
35		printf("%s(): Could not open RTC device!\n", __func__);
36		return B_ERROR;
37	}
38
39	return B_OK;
40}
41
42
43bigtime_t
44real_time_clock_usecs(void)
45{
46	if (sHandle == OF_FAILED)
47		return OF_FAILED;
48	int second, minute, hour, day, month, year;
49	if (of_call_method(sHandle, "get-time", 0, 6, &year, &month, &day,
50			&hour, &minute, &second) == OF_FAILED)
51		return OF_FAILED;
52	int days = day;
53		// TODO: Apply algorithm from kernel
54		// to assure monotonically increasing date.
55	return (((days * 24 + hour) * 60ULL + minute) * 60ULL + second)
56		* 1000000ULL;
57}
58