Paul Mackerras | bbd0abda | 2005-10-26 21:45:56 +1000 | [diff] [blame] | 1 | /* |
Paul Mackerras | bbd0abda | 2005-10-26 21:45:56 +1000 | [diff] [blame] | 2 | * Copyright (C) 1991, 1992, 1995 Linus Torvalds |
| 3 | * |
| 4 | * Adapted for PowerPC (PReP) by Gary Thomas |
| 5 | * Modified by Cort Dougan (cort@cs.nmt.edu). |
| 6 | * Copied and modified from arch/i386/kernel/time.c |
| 7 | * |
| 8 | */ |
| 9 | #include <linux/errno.h> |
| 10 | #include <linux/sched.h> |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/param.h> |
| 13 | #include <linux/string.h> |
| 14 | #include <linux/mm.h> |
| 15 | #include <linux/interrupt.h> |
| 16 | #include <linux/time.h> |
| 17 | #include <linux/timex.h> |
| 18 | #include <linux/kernel_stat.h> |
| 19 | #include <linux/mc146818rtc.h> |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/bcd.h> |
David Woodhouse | 575e321 | 2006-01-14 00:13:49 +0000 | [diff] [blame] | 22 | #include <linux/ioport.h> |
Paul Mackerras | bbd0abda | 2005-10-26 21:45:56 +1000 | [diff] [blame] | 23 | |
| 24 | #include <asm/io.h> |
| 25 | #include <asm/nvram.h> |
| 26 | #include <asm/prom.h> |
| 27 | #include <asm/sections.h> |
| 28 | #include <asm/time.h> |
| 29 | |
| 30 | extern spinlock_t rtc_lock; |
| 31 | |
| 32 | static int nvram_as1 = NVRAM_AS1; |
| 33 | static int nvram_as0 = NVRAM_AS0; |
| 34 | static int nvram_data = NVRAM_DATA; |
| 35 | |
| 36 | long __init chrp_time_init(void) |
| 37 | { |
| 38 | struct device_node *rtcs; |
David Woodhouse | 575e321 | 2006-01-14 00:13:49 +0000 | [diff] [blame] | 39 | struct resource r; |
Paul Mackerras | bbd0abda | 2005-10-26 21:45:56 +1000 | [diff] [blame] | 40 | int base; |
| 41 | |
Stephen Rothwell | 4bf56e1 | 2007-04-24 13:48:41 +1000 | [diff] [blame] | 42 | rtcs = of_find_compatible_node(NULL, "rtc", "pnpPNP,b00"); |
Paul Mackerras | bbd0abda | 2005-10-26 21:45:56 +1000 | [diff] [blame] | 43 | if (rtcs == NULL) |
Stephen Rothwell | 4bf56e1 | 2007-04-24 13:48:41 +1000 | [diff] [blame] | 44 | rtcs = of_find_compatible_node(NULL, "rtc", "ds1385-rtc"); |
| 45 | if (rtcs == NULL) |
Paul Mackerras | bbd0abda | 2005-10-26 21:45:56 +1000 | [diff] [blame] | 46 | return 0; |
Stephen Rothwell | 4bf56e1 | 2007-04-24 13:48:41 +1000 | [diff] [blame] | 47 | if (of_address_to_resource(rtcs, 0, &r)) { |
| 48 | of_node_put(rtcs); |
| 49 | return 0; |
| 50 | } |
| 51 | of_node_put(rtcs); |
| 52 | |
David Woodhouse | 575e321 | 2006-01-14 00:13:49 +0000 | [diff] [blame] | 53 | base = r.start; |
Paul Mackerras | bbd0abda | 2005-10-26 21:45:56 +1000 | [diff] [blame] | 54 | nvram_as1 = 0; |
| 55 | nvram_as0 = base; |
| 56 | nvram_data = base + 1; |
| 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | int chrp_cmos_clock_read(int addr) |
| 62 | { |
| 63 | if (nvram_as1 != 0) |
| 64 | outb(addr>>8, nvram_as1); |
| 65 | outb(addr, nvram_as0); |
| 66 | return (inb(nvram_data)); |
| 67 | } |
| 68 | |
| 69 | void chrp_cmos_clock_write(unsigned long val, int addr) |
| 70 | { |
| 71 | if (nvram_as1 != 0) |
| 72 | outb(addr>>8, nvram_as1); |
| 73 | outb(addr, nvram_as0); |
| 74 | outb(val, nvram_data); |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * Set the hardware clock. -- Cort |
| 80 | */ |
| 81 | int chrp_set_rtc_time(struct rtc_time *tmarg) |
| 82 | { |
| 83 | unsigned char save_control, save_freq_select; |
| 84 | struct rtc_time tm = *tmarg; |
| 85 | |
| 86 | spin_lock(&rtc_lock); |
| 87 | |
| 88 | save_control = chrp_cmos_clock_read(RTC_CONTROL); /* tell the clock it's being set */ |
| 89 | |
| 90 | chrp_cmos_clock_write((save_control|RTC_SET), RTC_CONTROL); |
| 91 | |
| 92 | save_freq_select = chrp_cmos_clock_read(RTC_FREQ_SELECT); /* stop and reset prescaler */ |
| 93 | |
| 94 | chrp_cmos_clock_write((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT); |
| 95 | |
Paul Mackerras | bbd0abda | 2005-10-26 21:45:56 +1000 | [diff] [blame] | 96 | if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) { |
Adrian Bunk | 8f6ba49 | 2008-08-09 02:34:53 +1000 | [diff] [blame] | 97 | tm.tm_sec = bin2bcd(tm.tm_sec); |
| 98 | tm.tm_min = bin2bcd(tm.tm_min); |
| 99 | tm.tm_hour = bin2bcd(tm.tm_hour); |
| 100 | tm.tm_mon = bin2bcd(tm.tm_mon); |
| 101 | tm.tm_mday = bin2bcd(tm.tm_mday); |
| 102 | tm.tm_year = bin2bcd(tm.tm_year); |
Paul Mackerras | bbd0abda | 2005-10-26 21:45:56 +1000 | [diff] [blame] | 103 | } |
| 104 | chrp_cmos_clock_write(tm.tm_sec,RTC_SECONDS); |
| 105 | chrp_cmos_clock_write(tm.tm_min,RTC_MINUTES); |
| 106 | chrp_cmos_clock_write(tm.tm_hour,RTC_HOURS); |
| 107 | chrp_cmos_clock_write(tm.tm_mon,RTC_MONTH); |
| 108 | chrp_cmos_clock_write(tm.tm_mday,RTC_DAY_OF_MONTH); |
| 109 | chrp_cmos_clock_write(tm.tm_year,RTC_YEAR); |
| 110 | |
| 111 | /* The following flags have to be released exactly in this order, |
| 112 | * otherwise the DS12887 (popular MC146818A clone with integrated |
| 113 | * battery and quartz) will not reset the oscillator and will not |
| 114 | * update precisely 500 ms later. You won't find this mentioned in |
| 115 | * the Dallas Semiconductor data sheets, but who believes data |
| 116 | * sheets anyway ... -- Markus Kuhn |
| 117 | */ |
| 118 | chrp_cmos_clock_write(save_control, RTC_CONTROL); |
| 119 | chrp_cmos_clock_write(save_freq_select, RTC_FREQ_SELECT); |
| 120 | |
| 121 | spin_unlock(&rtc_lock); |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | void chrp_get_rtc_time(struct rtc_time *tm) |
| 126 | { |
| 127 | unsigned int year, mon, day, hour, min, sec; |
Paul Mackerras | bbd0abda | 2005-10-26 21:45:56 +1000 | [diff] [blame] | 128 | |
Matt Mackall | 4dc12ec | 2006-03-28 01:56:03 -0800 | [diff] [blame] | 129 | do { |
Paul Mackerras | bbd0abda | 2005-10-26 21:45:56 +1000 | [diff] [blame] | 130 | sec = chrp_cmos_clock_read(RTC_SECONDS); |
| 131 | min = chrp_cmos_clock_read(RTC_MINUTES); |
| 132 | hour = chrp_cmos_clock_read(RTC_HOURS); |
| 133 | day = chrp_cmos_clock_read(RTC_DAY_OF_MONTH); |
| 134 | mon = chrp_cmos_clock_read(RTC_MONTH); |
| 135 | year = chrp_cmos_clock_read(RTC_YEAR); |
Matt Mackall | 4dc12ec | 2006-03-28 01:56:03 -0800 | [diff] [blame] | 136 | } while (sec != chrp_cmos_clock_read(RTC_SECONDS)); |
Paul Mackerras | bbd0abda | 2005-10-26 21:45:56 +1000 | [diff] [blame] | 137 | |
| 138 | if (!(chrp_cmos_clock_read(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) { |
Adrian Bunk | 8f6ba49 | 2008-08-09 02:34:53 +1000 | [diff] [blame] | 139 | sec = bcd2bin(sec); |
| 140 | min = bcd2bin(min); |
| 141 | hour = bcd2bin(hour); |
| 142 | day = bcd2bin(day); |
| 143 | mon = bcd2bin(mon); |
| 144 | year = bcd2bin(year); |
Paul Mackerras | bbd0abda | 2005-10-26 21:45:56 +1000 | [diff] [blame] | 145 | } |
Paul Mackerras | 49e16b7 | 2005-11-18 15:52:38 +1100 | [diff] [blame] | 146 | if (year < 70) |
Paul Mackerras | bbd0abda | 2005-10-26 21:45:56 +1000 | [diff] [blame] | 147 | year += 100; |
| 148 | tm->tm_sec = sec; |
| 149 | tm->tm_min = min; |
| 150 | tm->tm_hour = hour; |
| 151 | tm->tm_mday = day; |
| 152 | tm->tm_mon = mon; |
| 153 | tm->tm_year = year; |
| 154 | } |