18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  Copyright (C) 2020 Loongson Technology Co., Ltd.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/init.h>
78c2ecf20Sopenharmony_ci#include <linux/kernel.h>
88c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
98c2ecf20Sopenharmony_ci#include <loongson.h>
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#define RTC_TOYREAD0    0x2C
128c2ecf20Sopenharmony_ci#define RTC_YEAR        0x30
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ciunsigned long loongson_get_rtc_time(void)
158c2ecf20Sopenharmony_ci{
168c2ecf20Sopenharmony_ci	unsigned int year, mon, day, hour, min, sec;
178c2ecf20Sopenharmony_ci	unsigned int value;
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci	value = ls7a_readl(LS7A_RTC_REG_BASE + RTC_TOYREAD0);
208c2ecf20Sopenharmony_ci	sec = (value >> 4) & 0x3f;
218c2ecf20Sopenharmony_ci	min = (value >> 10) & 0x3f;
228c2ecf20Sopenharmony_ci	hour = (value >> 16) & 0x1f;
238c2ecf20Sopenharmony_ci	day = (value >> 21) & 0x1f;
248c2ecf20Sopenharmony_ci	mon = (value >> 26) & 0x3f;
258c2ecf20Sopenharmony_ci	year = ls7a_readl(LS7A_RTC_REG_BASE + RTC_YEAR);
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci	year = 1900 + year;
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci	return mktime64(year, mon, day, hour, min, sec);
308c2ecf20Sopenharmony_ci}
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_civoid read_persistent_clock64(struct timespec64 *ts)
338c2ecf20Sopenharmony_ci{
348c2ecf20Sopenharmony_ci	ts->tv_sec = loongson_get_rtc_time();
358c2ecf20Sopenharmony_ci	ts->tv_nsec = 0;
368c2ecf20Sopenharmony_ci}
37