/* MN10300 RTC management1*2* Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.3* Written by David Howells ([email protected])4*5* This program is free software; you can redistribute it and/or6* modify it under the terms of the GNU General Public Licence7* as published by the Free Software Foundation; either version8* 2 of the Licence, or (at your option) any later version.9*/10#include <linux/kernel.h>11#include <linux/module.h>12#include <linux/init.h>13#include <linux/mc146818rtc.h>14#include <linux/bcd.h>15#include <linux/timex.h>16#include <asm/rtc-regs.h>17#include <asm/rtc.h>1819DEFINE_SPINLOCK(rtc_lock);20EXPORT_SYMBOL(rtc_lock);2122/*23* Read the current RTC time24*/25void read_persistent_clock(struct timespec *ts)26{27struct rtc_time tm;2829get_rtc_time(&tm);3031ts->tv_nsec = 0;32ts->tv_sec = mktime(tm.tm_year, tm.tm_mon, tm.tm_mday,33tm.tm_hour, tm.tm_min, tm.tm_sec);3435/* if rtc is way off in the past, set something reasonable */36if (ts->tv_sec < 0)37ts->tv_sec = mktime(2009, 1, 1, 12, 0, 0);38}3940/*41* In order to set the CMOS clock precisely, set_rtc_mmss has to be called 50042* ms after the second nowtime has started, because when nowtime is written43* into the registers of the CMOS clock, it will jump to the next second44* precisely 500 ms later. Check the Motorola MC146818A or Dallas DS12887 data45* sheet for details.46*47* BUG: This routine does not handle hour overflow properly; it just48* sets the minutes. Usually you'll only notice that after reboot!49*/50static int set_rtc_mmss(unsigned long nowtime)51{52unsigned char save_control, save_freq_select;53int retval = 0;54int real_seconds, real_minutes, cmos_minutes;5556/* gets recalled with irq locally disabled */57spin_lock(&rtc_lock);58save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being59* set */60CMOS_WRITE(save_control | RTC_SET, RTC_CONTROL);6162save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset63* prescaler */64CMOS_WRITE(save_freq_select | RTC_DIV_RESET2, RTC_FREQ_SELECT);6566cmos_minutes = CMOS_READ(RTC_MINUTES);67if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)68cmos_minutes = bcd2bin(cmos_minutes);6970/*71* since we're only adjusting minutes and seconds,72* don't interfere with hour overflow. This avoids73* messing with unknown time zones but requires your74* RTC not to be off by more than 15 minutes75*/76real_seconds = nowtime % 60;77real_minutes = nowtime / 60;78if (((abs(real_minutes - cmos_minutes) + 15) / 30) & 1)79/* correct for half hour time zone */80real_minutes += 30;81real_minutes %= 60;8283if (abs(real_minutes - cmos_minutes) < 30) {84if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {85real_seconds = bin2bcd(real_seconds);86real_minutes = bin2bcd(real_minutes);87}88CMOS_WRITE(real_seconds, RTC_SECONDS);89CMOS_WRITE(real_minutes, RTC_MINUTES);90} else {91printk_once(KERN_NOTICE92"set_rtc_mmss: can't update from %d to %d\n",93cmos_minutes, real_minutes);94retval = -1;95}9697/* The following flags have to be released exactly in this order,98* otherwise the DS12887 (popular MC146818A clone with integrated99* battery and quartz) will not reset the oscillator and will not100* update precisely 500 ms later. You won't find this mentioned in101* the Dallas Semiconductor data sheets, but who believes data102* sheets anyway ... -- Markus Kuhn103*/104CMOS_WRITE(save_control, RTC_CONTROL);105CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);106spin_unlock(&rtc_lock);107108return retval;109}110111int update_persistent_clock(struct timespec now)112{113return set_rtc_mmss(now.tv_sec);114}115116/*117* calibrate the TSC clock against the RTC118*/119void __init calibrate_clock(void)120{121unsigned char status;122123/* make sure the RTC is running and is set to operate in 24hr mode */124status = RTSRC;125RTCRB |= RTCRB_SET;126RTCRB |= RTCRB_TM_24HR;127RTCRB &= ~RTCRB_DM_BINARY;128RTCRA |= RTCRA_DVR;129RTCRA &= ~RTCRA_DVR;130RTCRB &= ~RTCRB_SET;131}132133134