1/* 2 * BK Id: %F% %I% %G% %U% %#% 3 */ 4/* 5 * arch/ppc/platforms/chrp_time.c 6 * 7 * Copyright (C) 1991, 1992, 1995 Linus Torvalds 8 * 9 * Adapted for PowerPC (PReP) by Gary Thomas 10 * Modified by Cort Dougan (cort@cs.nmt.edu). 11 * Copied and modified from arch/i386/kernel/time.c 12 * 13 */ 14#include <linux/errno.h> 15#include <linux/sched.h> 16#include <linux/kernel.h> 17#include <linux/param.h> 18#include <linux/string.h> 19#include <linux/mm.h> 20#include <linux/interrupt.h> 21#include <linux/time.h> 22#include <linux/timex.h> 23#include <linux/kernel_stat.h> 24#include <linux/mc146818rtc.h> 25#include <linux/init.h> 26 27#include <asm/segment.h> 28#include <asm/io.h> 29#include <asm/processor.h> 30#include <asm/nvram.h> 31#include <asm/prom.h> 32#include <asm/sections.h> 33#include <asm/time.h> 34 35extern spinlock_t rtc_lock; 36 37static int nvram_as1 = NVRAM_AS1; 38static int nvram_as0 = NVRAM_AS0; 39static int nvram_data = NVRAM_DATA; 40 41long __init chrp_time_init(void) 42{ 43 struct device_node *rtcs; 44 int base; 45 46 rtcs = find_compatible_devices("rtc", "pnpPNP,b00"); 47 if (rtcs == NULL || rtcs->addrs == NULL) 48 return 0; 49 base = rtcs->addrs[0].address; 50 nvram_as1 = 0; 51 nvram_as0 = base; 52 nvram_data = base + 1; 53 54 return 0; 55} 56 57int __chrp chrp_cmos_clock_read(int addr) 58{ 59 if (nvram_as1 != 0) 60 outb(addr>>8, nvram_as1); 61 outb(addr, nvram_as0); 62 return (inb(nvram_data)); 63} 64 65void __chrp chrp_cmos_clock_write(unsigned long val, int addr) 66{ 67 if (nvram_as1 != 0) 68 outb(addr>>8, nvram_as1); 69 outb(addr, nvram_as0); 70 outb(val, nvram_data); 71 return; 72} 73 74/* 75 * Set the hardware clock. -- Cort 76 */ 77int __chrp chrp_set_rtc_time(unsigned long nowtime) 78{ 79 unsigned char save_control, save_freq_select; 80 struct rtc_time tm; 81 82 spin_lock(&rtc_lock); 83 to_tm(nowtime, &tm); 84 85 save_control = chrp_cmos_clock_read(RTC_CONTROL); /* tell the clock it's being set */ 86 87 chrp_cmos_clock_write((save_control|RTC_SET), RTC_CONTROL); 88 89 save_freq_select = chrp_cmos_clock_read(RTC_FREQ_SELECT); /* stop and reset prescaler */ 90 91 chrp_cmos_clock_write((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT); 92 93 tm.tm_year -= 1900; 94 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) { 95 BIN_TO_BCD(tm.tm_sec); 96 BIN_TO_BCD(tm.tm_min); 97 BIN_TO_BCD(tm.tm_hour); 98 BIN_TO_BCD(tm.tm_mon); 99 BIN_TO_BCD(tm.tm_mday); 100 BIN_TO_BCD(tm.tm_year); 101 } 102 chrp_cmos_clock_write(tm.tm_sec,RTC_SECONDS); 103 chrp_cmos_clock_write(tm.tm_min,RTC_MINUTES); 104 chrp_cmos_clock_write(tm.tm_hour,RTC_HOURS); 105 chrp_cmos_clock_write(tm.tm_mon,RTC_MONTH); 106 chrp_cmos_clock_write(tm.tm_mday,RTC_DAY_OF_MONTH); 107 chrp_cmos_clock_write(tm.tm_year,RTC_YEAR); 108 109 /* The following flags have to be released exactly in this order, 110 * otherwise the DS12887 (popular MC146818A clone with integrated 111 * battery and quartz) will not reset the oscillator and will not 112 * update precisely 500 ms later. You won't find this mentioned in 113 * the Dallas Semiconductor data sheets, but who believes data 114 * sheets anyway ... -- Markus Kuhn 115 */ 116 chrp_cmos_clock_write(save_control, RTC_CONTROL); 117 chrp_cmos_clock_write(save_freq_select, RTC_FREQ_SELECT); 118 119 if ( (time_state == TIME_ERROR) || (time_state == TIME_BAD) ) 120 time_state = TIME_OK; 121 spin_unlock(&rtc_lock); 122 return 0; 123} 124 125unsigned long __chrp chrp_get_rtc_time(void) 126{ 127 unsigned int year, mon, day, hour, min, sec; 128 int uip, i; 129 130 /* The Linux interpretation of the CMOS clock register contents: 131 * When the Update-In-Progress (UIP) flag goes from 1 to 0, the 132 * RTC registers show the second which has precisely just started. 133 * Let's hope other operating systems interpret the RTC the same way. 134 */ 135 136 /* Since the UIP flag is set for about 2.2 ms and the clock 137 * is typically written with a precision of 1 jiffy, trying 138 * to obtain a precision better than a few milliseconds is 139 * an illusion. Only consistency is interesting, this also 140 * allows to use the routine for /dev/rtc without a potential 141 * 1 second kernel busy loop triggered by any reader of /dev/rtc. 142 */ 143 144 for ( i = 0; i<1000000; i++) { 145 uip = chrp_cmos_clock_read(RTC_FREQ_SELECT); 146 sec = chrp_cmos_clock_read(RTC_SECONDS); 147 min = chrp_cmos_clock_read(RTC_MINUTES); 148 hour = chrp_cmos_clock_read(RTC_HOURS); 149 day = chrp_cmos_clock_read(RTC_DAY_OF_MONTH); 150 mon = chrp_cmos_clock_read(RTC_MONTH); 151 year = chrp_cmos_clock_read(RTC_YEAR); 152 uip |= chrp_cmos_clock_read(RTC_FREQ_SELECT); 153 if ((uip & RTC_UIP)==0) break; 154 } 155 156 if (!(chrp_cmos_clock_read(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) 157 { 158 BCD_TO_BIN(sec); 159 BCD_TO_BIN(min); 160 BCD_TO_BIN(hour); 161 BCD_TO_BIN(day); 162 BCD_TO_BIN(mon); 163 BCD_TO_BIN(year); 164 } 165 if ((year += 1900) < 1970) 166 year += 100; 167 return mktime(year, mon, day, hour, min, sec); 168} 169 170 171void __init chrp_calibrate_decr(void) 172{ 173 struct device_node *cpu; 174 unsigned int freq, *fp; 175 176 if (via_calibrate_decr()) 177 return; 178 179 /* 180 * The cpu node should have a timebase-frequency property 181 * to tell us the rate at which the decrementer counts. 182 */ 183 freq = 16666000; /* hardcoded default */ 184 cpu = find_type_devices("cpu"); 185 if (cpu != 0) { 186 fp = (unsigned int *) 187 get_property(cpu, "timebase-frequency", NULL); 188 if (fp != 0) 189 freq = *fp; 190 } 191 printk("time_init: decrementer frequency = %u.%.6u MHz\n", 192 freq/1000000, freq%1000000); 193 tb_ticks_per_jiffy = freq / HZ; 194 tb_to_us = mulhwu_scale_factor(freq, 1000000); 195} 196