1/*	$NetBSD: clock.c,v 1.12 2023/11/05 21:54:27 andvar Exp $	*/
2
3/*-
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__KERNEL_RCSID(0, "$NetBSD: clock.c,v 1.12 2023/11/05 21:54:27 andvar Exp $");
31
32#include "debug_playstation2.h"
33
34#include <sys/param.h>
35#include <sys/systm.h>
36#include <sys/kernel.h>		/* time */
37#include <sys/proc.h>
38
39#include <mips/locore.h>
40#include <mips/mips3_clock.h>
41
42#include <dev/clock_subr.h>
43#include <machine/bootinfo.h>
44
45#include <playstation2/ee/timervar.h>
46
47static int get_bootinfo_tod(todr_chip_handle_t, struct clock_ymdhms *);
48
49void
50cpu_initclocks(void)
51{
52	struct todr_chip_handle	todr = {
53		.todr_gettime_ymdhms = get_bootinfo_tod,
54	};
55
56	/*
57	 *  PS2 R5900 CPU clock is 294.912 MHz = (1 << 15) * 9 * 1000
58	 */
59	curcpu()->ci_cpu_freq = 294912000;
60
61	hz = 100;
62
63	/* Install clock interrupt */
64	timer_clock_init();
65
66	todr_attach(&todr);
67
68	mips3_init_tc();
69}
70
71void
72setstatclockrate(int arg)
73{
74	/* not yet */
75}
76
77static int
78get_bootinfo_tod(todr_chip_handle_t tch, struct clock_ymdhms *dt)
79{
80	time_t utc;
81	struct bootinfo_rtc *rtc =
82	    (void *)MIPS_PHYS_TO_KSEG1(BOOTINFO_BLOCK_BASE + BOOTINFO_RTC);
83
84	/* PS2 RTC is JST */
85	dt->dt_year = bcdtobin(rtc->year) + 2000;
86	dt->dt_mon = bcdtobin(rtc->mon);
87	dt->dt_day = bcdtobin(rtc->day);
88	dt->dt_hour = bcdtobin(rtc->hour);
89	dt->dt_min = bcdtobin(rtc->min);
90	dt->dt_sec = bcdtobin(rtc->sec);
91
92	/* convert to UTC */
93	utc = clock_ymdhms_to_secs(dt) - 9*60*60;
94	clock_secs_to_ymdhms(utc, dt);
95#ifdef DEBUG
96        printf("bootinfo: %lld/%d/%d/%d/%d/%d rtc_offset %d\n", dt->dt_year,
97	    dt->dt_mon, dt->dt_day, dt->dt_hour, dt->dt_min, dt->dt_sec,
98	    rtc_offset);
99#endif
100	return 0;
101}
102