Path: blob/master/arch/sh/boards/mach-dreamcast/rtc.c
15126 views
/*1* arch/sh/boards/dreamcast/rtc.c2*3* Dreamcast AICA RTC routines.4*5* Copyright (c) 2001, 2002 M. R. Brown <[email protected]>6* Copyright (c) 2002 Paul Mundt <[email protected]>7*8* Released under the terms of the GNU GPL v2.0.9*10*/1112#include <linux/time.h>13#include <asm/rtc.h>14#include <asm/io.h>1516/* The AICA RTC has an Epoch of 1/1/1950, so we must subtract 20 years (in17seconds) to get the standard Unix Epoch when getting the time, and add1820 years when setting the time. */19#define TWENTY_YEARS ((20 * 365LU + 5) * 86400)2021/* The AICA RTC is represented by a 32-bit seconds counter stored in 2 16-bit22registers.*/23#define AICA_RTC_SECS_H 0xa071000024#define AICA_RTC_SECS_L 0xa07100042526/**27* aica_rtc_gettimeofday - Get the time from the AICA RTC28* @ts: pointer to resulting timespec29*30* Grabs the current RTC seconds counter and adjusts it to the Unix Epoch.31*/32static void aica_rtc_gettimeofday(struct timespec *ts)33{34unsigned long val1, val2;3536do {37val1 = ((__raw_readl(AICA_RTC_SECS_H) & 0xffff) << 16) |38(__raw_readl(AICA_RTC_SECS_L) & 0xffff);3940val2 = ((__raw_readl(AICA_RTC_SECS_H) & 0xffff) << 16) |41(__raw_readl(AICA_RTC_SECS_L) & 0xffff);42} while (val1 != val2);4344ts->tv_sec = val1 - TWENTY_YEARS;4546/* Can't get nanoseconds with just a seconds counter. */47ts->tv_nsec = 0;48}4950/**51* aica_rtc_settimeofday - Set the AICA RTC to the current time52* @secs: contains the time_t to set53*54* Adjusts the given @tv to the AICA Epoch and sets the RTC seconds counter.55*/56static int aica_rtc_settimeofday(const time_t secs)57{58unsigned long val1, val2;59unsigned long adj = secs + TWENTY_YEARS;6061do {62__raw_writel((adj & 0xffff0000) >> 16, AICA_RTC_SECS_H);63__raw_writel((adj & 0xffff), AICA_RTC_SECS_L);6465val1 = ((__raw_readl(AICA_RTC_SECS_H) & 0xffff) << 16) |66(__raw_readl(AICA_RTC_SECS_L) & 0xffff);6768val2 = ((__raw_readl(AICA_RTC_SECS_H) & 0xffff) << 16) |69(__raw_readl(AICA_RTC_SECS_L) & 0xffff);70} while (val1 != val2);7172return 0;73}7475void aica_time_init(void)76{77rtc_sh_get_time = aica_rtc_gettimeofday;78rtc_sh_set_time = aica_rtc_settimeofday;79}80818283