#include <sys/types.h>
#include <err.h>
#include <new.h>
#include <platform/platform.h>
#include <platform/osk5912/platform_p.h>
#include <platform/console.h>
#include <dev/Timer.h>
#include <dev/DeviceManager.h>
#include <kernel/Spinlock.h>
#include <kernel/AutoSpinlock.h>

namespace OS {
namespace Platform {

class PlatformTimer : public Dev::Timer {
public:
	PlatformTimer(const char *name);
	virtual ~PlatformTimer() {};

	virtual status_t SetPeriodic(Dev::timerCallback callback, time_t interval);

private:
	Kernel::Spinlock mLock;
};

PlatformTimer::PlatformTimer(const char *name)
	: Dev::Timer(name)
{
}

status_t PlatformTimer::SetPeriodic(Dev::timerCallback callback, time_t interval) 
{
	Kernel::AutoSpinlock al(mLock);

	PANIC_UNIMPLEMENTED;

	mCallback = callback;

#if 0
	*REG(PIT_CLEAR) = 1;
	*REG(PIT_INTERVAL) = interval;
	*REG(PIT_START_PERIODIC) = 1;

	UnmaskInterrupt();
#endif

	return NO_ERROR;
}

time_t GetCurrentTime()
{
	time_t time;

	PANIC_UNIMPLEMENTED;

#if 0
	*REG(SYSINFO_TIME_LATCH) = 1;
	time = *REG(SYSINFO_TIME_SECS) * 1000;
	time += *REG(SYSINFO_TIME_USECS) / 1000;
#endif
			
	return time;
}

void InitPlatformTimer()
{
#if 1
	PlatformTimer *t = new PlatformTimer("timer0");
	t->InstallIntHandler(Dev::PIC(), INT_PIT);
	Dev::Root()->AddChild(t);
	Dev::SetOSTimer(t);
#endif
}

}} // namespace OS::Platform

