1// SPDX-License-Identifier: GPL-2.0
2/*
3 *  Copyright (C) 2020 Loongson Technology Co., Ltd.
4 */
5
6#include <linux/init.h>
7#include <linux/kernel.h>
8#include <linux/platform_device.h>
9#include <loongson.h>
10
11#define RTC_TOYREAD0    0x2C
12#define RTC_YEAR        0x30
13
14unsigned long loongson_get_rtc_time(void)
15{
16	unsigned int year, mon, day, hour, min, sec;
17	unsigned int value;
18
19	value = ls7a_readl(LS7A_RTC_REG_BASE + RTC_TOYREAD0);
20	sec = (value >> 4) & 0x3f;
21	min = (value >> 10) & 0x3f;
22	hour = (value >> 16) & 0x1f;
23	day = (value >> 21) & 0x1f;
24	mon = (value >> 26) & 0x3f;
25	year = ls7a_readl(LS7A_RTC_REG_BASE + RTC_YEAR);
26
27	year = 1900 + year;
28
29	return mktime64(year, mon, day, hour, min, sec);
30}
31
32void read_persistent_clock64(struct timespec64 *ts)
33{
34	ts->tv_sec = loongson_get_rtc_time();
35	ts->tv_nsec = 0;
36}
37