Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * arch/ppc64/kernel/maple_time.c |
| 3 | * |
| 4 | * (c) Copyright 2004 Benjamin Herrenschmidt (benh@kernel.crashing.org), |
| 5 | * IBM Corp. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #undef DEBUG |
| 15 | |
| 16 | #include <linux/config.h> |
| 17 | #include <linux/errno.h> |
| 18 | #include <linux/sched.h> |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/param.h> |
| 21 | #include <linux/string.h> |
| 22 | #include <linux/mm.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/time.h> |
| 25 | #include <linux/adb.h> |
| 26 | #include <linux/pmu.h> |
| 27 | #include <linux/interrupt.h> |
| 28 | #include <linux/mc146818rtc.h> |
| 29 | #include <linux/bcd.h> |
| 30 | |
| 31 | #include <asm/sections.h> |
| 32 | #include <asm/prom.h> |
| 33 | #include <asm/system.h> |
| 34 | #include <asm/io.h> |
| 35 | #include <asm/pgtable.h> |
| 36 | #include <asm/machdep.h> |
| 37 | #include <asm/time.h> |
| 38 | |
| 39 | #ifdef DEBUG |
| 40 | #define DBG(x...) printk(x) |
| 41 | #else |
| 42 | #define DBG(x...) |
| 43 | #endif |
| 44 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | extern void GregorianDay(struct rtc_time * tm); |
| 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | static int maple_rtc_addr; |
| 48 | |
| 49 | static int maple_clock_read(int addr) |
| 50 | { |
| 51 | outb_p(addr, maple_rtc_addr); |
| 52 | return inb_p(maple_rtc_addr+1); |
| 53 | } |
| 54 | |
| 55 | static void maple_clock_write(unsigned long val, int addr) |
| 56 | { |
| 57 | outb_p(addr, maple_rtc_addr); |
| 58 | outb_p(val, maple_rtc_addr+1); |
| 59 | } |
| 60 | |
| 61 | void maple_get_rtc_time(struct rtc_time *tm) |
| 62 | { |
| 63 | int uip, i; |
| 64 | |
| 65 | /* The Linux interpretation of the CMOS clock register contents: |
| 66 | * When the Update-In-Progress (UIP) flag goes from 1 to 0, the |
| 67 | * RTC registers show the second which has precisely just started. |
| 68 | * Let's hope other operating systems interpret the RTC the same way. |
| 69 | */ |
| 70 | |
| 71 | /* Since the UIP flag is set for about 2.2 ms and the clock |
| 72 | * is typically written with a precision of 1 jiffy, trying |
| 73 | * to obtain a precision better than a few milliseconds is |
| 74 | * an illusion. Only consistency is interesting, this also |
| 75 | * allows to use the routine for /dev/rtc without a potential |
| 76 | * 1 second kernel busy loop triggered by any reader of /dev/rtc. |
| 77 | */ |
| 78 | |
| 79 | for (i = 0; i<1000000; i++) { |
| 80 | uip = maple_clock_read(RTC_FREQ_SELECT); |
| 81 | tm->tm_sec = maple_clock_read(RTC_SECONDS); |
| 82 | tm->tm_min = maple_clock_read(RTC_MINUTES); |
| 83 | tm->tm_hour = maple_clock_read(RTC_HOURS); |
| 84 | tm->tm_mday = maple_clock_read(RTC_DAY_OF_MONTH); |
| 85 | tm->tm_mon = maple_clock_read(RTC_MONTH); |
| 86 | tm->tm_year = maple_clock_read(RTC_YEAR); |
| 87 | uip |= maple_clock_read(RTC_FREQ_SELECT); |
| 88 | if ((uip & RTC_UIP)==0) |
| 89 | break; |
| 90 | } |
| 91 | |
| 92 | if (!(maple_clock_read(RTC_CONTROL) & RTC_DM_BINARY) |
| 93 | || RTC_ALWAYS_BCD) { |
| 94 | BCD_TO_BIN(tm->tm_sec); |
| 95 | BCD_TO_BIN(tm->tm_min); |
| 96 | BCD_TO_BIN(tm->tm_hour); |
| 97 | BCD_TO_BIN(tm->tm_mday); |
| 98 | BCD_TO_BIN(tm->tm_mon); |
| 99 | BCD_TO_BIN(tm->tm_year); |
| 100 | } |
| 101 | if ((tm->tm_year + 1900) < 1970) |
| 102 | tm->tm_year += 100; |
| 103 | |
| 104 | GregorianDay(tm); |
| 105 | } |
| 106 | |
| 107 | int maple_set_rtc_time(struct rtc_time *tm) |
| 108 | { |
| 109 | unsigned char save_control, save_freq_select; |
| 110 | int sec, min, hour, mon, mday, year; |
| 111 | |
| 112 | spin_lock(&rtc_lock); |
| 113 | |
| 114 | save_control = maple_clock_read(RTC_CONTROL); /* tell the clock it's being set */ |
| 115 | |
| 116 | maple_clock_write((save_control|RTC_SET), RTC_CONTROL); |
| 117 | |
| 118 | save_freq_select = maple_clock_read(RTC_FREQ_SELECT); /* stop and reset prescaler */ |
| 119 | |
| 120 | maple_clock_write((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT); |
| 121 | |
| 122 | sec = tm->tm_sec; |
| 123 | min = tm->tm_min; |
| 124 | hour = tm->tm_hour; |
| 125 | mon = tm->tm_mon; |
| 126 | mday = tm->tm_mday; |
| 127 | year = tm->tm_year; |
| 128 | |
| 129 | if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) { |
| 130 | BIN_TO_BCD(sec); |
| 131 | BIN_TO_BCD(min); |
| 132 | BIN_TO_BCD(hour); |
| 133 | BIN_TO_BCD(mon); |
| 134 | BIN_TO_BCD(mday); |
| 135 | BIN_TO_BCD(year); |
| 136 | } |
| 137 | maple_clock_write(sec, RTC_SECONDS); |
| 138 | maple_clock_write(min, RTC_MINUTES); |
| 139 | maple_clock_write(hour, RTC_HOURS); |
| 140 | maple_clock_write(mon, RTC_MONTH); |
| 141 | maple_clock_write(mday, RTC_DAY_OF_MONTH); |
| 142 | maple_clock_write(year, RTC_YEAR); |
| 143 | |
| 144 | /* The following flags have to be released exactly in this order, |
| 145 | * otherwise the DS12887 (popular MC146818A clone with integrated |
| 146 | * battery and quartz) will not reset the oscillator and will not |
| 147 | * update precisely 500 ms later. You won't find this mentioned in |
| 148 | * the Dallas Semiconductor data sheets, but who believes data |
| 149 | * sheets anyway ... -- Markus Kuhn |
| 150 | */ |
| 151 | maple_clock_write(save_control, RTC_CONTROL); |
| 152 | maple_clock_write(save_freq_select, RTC_FREQ_SELECT); |
| 153 | |
| 154 | spin_unlock(&rtc_lock); |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
Paul Mackerras | 143a1de | 2005-10-19 23:11:21 +1000 | [diff] [blame^] | 159 | unsigned long __init maple_get_boot_time(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | { |
Paul Mackerras | 143a1de | 2005-10-19 23:11:21 +1000 | [diff] [blame^] | 161 | struct rtc_time tm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | struct device_node *rtcs; |
| 163 | |
| 164 | rtcs = find_compatible_devices("rtc", "pnpPNP,b00"); |
| 165 | if (rtcs && rtcs->addrs) { |
| 166 | maple_rtc_addr = rtcs->addrs[0].address; |
| 167 | printk(KERN_INFO "Maple: Found RTC at 0x%x\n", maple_rtc_addr); |
| 168 | } else { |
| 169 | maple_rtc_addr = RTC_PORT(0); /* legacy address */ |
| 170 | printk(KERN_INFO "Maple: No device node for RTC, assuming " |
| 171 | "legacy address (0x%x)\n", maple_rtc_addr); |
| 172 | } |
| 173 | |
Paul Mackerras | 143a1de | 2005-10-19 23:11:21 +1000 | [diff] [blame^] | 174 | maple_get_rtc_time(&tm); |
| 175 | return mktime(time->tm_year+1900, time->tm_mon+1, time->tm_mday, |
| 176 | time->tm_hour, time->tm_min, time->tm_sec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | } |
| 178 | |