Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Support for periodic interrupts (100 per second) and for getting |
| 3 | * the current time from the RTC on Power Macintoshes. |
| 4 | * |
| 5 | * We use the decrementer register for our periodic interrupts. |
| 6 | * |
| 7 | * Paul Mackerras August 1996. |
| 8 | * Copyright (C) 1996 Paul Mackerras. |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 9 | * Copyright (C) 2003-2005 Benjamin Herrenschmidt. |
| 10 | * |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 11 | */ |
| 12 | #include <linux/config.h> |
| 13 | #include <linux/errno.h> |
| 14 | #include <linux/sched.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/param.h> |
| 17 | #include <linux/string.h> |
| 18 | #include <linux/mm.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/time.h> |
| 21 | #include <linux/adb.h> |
| 22 | #include <linux/cuda.h> |
| 23 | #include <linux/pmu.h> |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 24 | #include <linux/interrupt.h> |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 25 | #include <linux/hardirq.h> |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 26 | #include <linux/rtc.h> |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 27 | |
| 28 | #include <asm/sections.h> |
| 29 | #include <asm/prom.h> |
| 30 | #include <asm/system.h> |
| 31 | #include <asm/io.h> |
| 32 | #include <asm/pgtable.h> |
| 33 | #include <asm/machdep.h> |
| 34 | #include <asm/time.h> |
| 35 | #include <asm/nvram.h> |
| 36 | |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 37 | #undef DEBUG |
| 38 | |
| 39 | #ifdef DEBUG |
| 40 | #define DBG(x...) printk(x) |
| 41 | #else |
| 42 | #define DBG(x...) |
| 43 | #endif |
| 44 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 45 | /* Apparently the RTC stores seconds since 1 Jan 1904 */ |
| 46 | #define RTC_OFFSET 2082844800 |
| 47 | |
| 48 | /* |
| 49 | * Calibrate the decrementer frequency with the VIA timer 1. |
| 50 | */ |
| 51 | #define VIA_TIMER_FREQ_6 4700000 /* time 1 frequency * 6 */ |
| 52 | |
| 53 | /* VIA registers */ |
| 54 | #define RS 0x200 /* skip between registers */ |
| 55 | #define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */ |
| 56 | #define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */ |
| 57 | #define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */ |
| 58 | #define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */ |
| 59 | #define ACR (11*RS) /* Auxiliary control register */ |
| 60 | #define IFR (13*RS) /* Interrupt flag register */ |
| 61 | |
| 62 | /* Bits in ACR */ |
| 63 | #define T1MODE 0xc0 /* Timer 1 mode */ |
| 64 | #define T1MODE_CONT 0x40 /* continuous interrupts */ |
| 65 | |
| 66 | /* Bits in IFR and IER */ |
| 67 | #define T1_INT 0x40 /* Timer 1 interrupt */ |
| 68 | |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 69 | long __init pmac_time_init(void) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 70 | { |
| 71 | #ifdef CONFIG_NVRAM |
| 72 | s32 delta = 0; |
| 73 | int dst; |
| 74 | |
| 75 | delta = ((s32)pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0x9)) << 16; |
| 76 | delta |= ((s32)pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0xa)) << 8; |
| 77 | delta |= pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0xb); |
| 78 | if (delta & 0x00800000UL) |
| 79 | delta |= 0xFF000000UL; |
| 80 | dst = ((pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0x8) & 0x80) != 0); |
| 81 | printk("GMT Delta read from XPRAM: %d minutes, DST: %s\n", delta/60, |
| 82 | dst ? "on" : "off"); |
| 83 | return delta; |
| 84 | #else |
| 85 | return 0; |
| 86 | #endif |
| 87 | } |
| 88 | |
Paul Mackerras | 143a1de | 2005-10-19 23:11:21 +1000 | [diff] [blame] | 89 | unsigned long pmac_get_boot_time(void) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 90 | { |
| 91 | #if defined(CONFIG_ADB_CUDA) || defined(CONFIG_ADB_PMU) |
| 92 | struct adb_request req; |
| 93 | unsigned long now; |
| 94 | #endif |
| 95 | |
| 96 | /* Get the time from the RTC */ |
| 97 | switch (sys_ctrler) { |
| 98 | #ifdef CONFIG_ADB_CUDA |
| 99 | case SYS_CTRLER_CUDA: |
| 100 | if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_GET_TIME) < 0) |
| 101 | return 0; |
| 102 | while (!req.complete) |
| 103 | cuda_poll(); |
| 104 | if (req.reply_len != 7) |
| 105 | printk(KERN_ERR "pmac_get_rtc_time: got %d byte reply\n", |
| 106 | req.reply_len); |
| 107 | now = (req.reply[3] << 24) + (req.reply[4] << 16) |
| 108 | + (req.reply[5] << 8) + req.reply[6]; |
| 109 | return now - RTC_OFFSET; |
| 110 | #endif /* CONFIG_ADB_CUDA */ |
| 111 | #ifdef CONFIG_ADB_PMU |
| 112 | case SYS_CTRLER_PMU: |
| 113 | if (pmu_request(&req, NULL, 1, PMU_READ_RTC) < 0) |
| 114 | return 0; |
| 115 | while (!req.complete) |
| 116 | pmu_poll(); |
| 117 | if (req.reply_len != 4) |
| 118 | printk(KERN_ERR "pmac_get_rtc_time: got %d byte reply\n", |
| 119 | req.reply_len); |
| 120 | now = (req.reply[0] << 24) + (req.reply[1] << 16) |
| 121 | + (req.reply[2] << 8) + req.reply[3]; |
| 122 | return now - RTC_OFFSET; |
| 123 | #endif /* CONFIG_ADB_PMU */ |
| 124 | default: ; |
| 125 | } |
| 126 | return 0; |
| 127 | } |
| 128 | |
Paul Mackerras | 143a1de | 2005-10-19 23:11:21 +1000 | [diff] [blame] | 129 | void pmac_get_rtc_time(struct rtc_time *tm) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 130 | { |
Paul Mackerras | 143a1de | 2005-10-19 23:11:21 +1000 | [diff] [blame] | 131 | unsigned long now; |
| 132 | |
| 133 | now = pmac_get_boot_time(); |
| 134 | to_tm(now, tm); |
| 135 | tm->tm_year -= 1900; |
| 136 | tm->tm_mon -= 1; /* month is 0-based */ |
| 137 | } |
| 138 | |
| 139 | int pmac_set_rtc_time(struct rtc_time *tm) |
| 140 | { |
| 141 | unsigned long nowtime; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 142 | #if defined(CONFIG_ADB_CUDA) || defined(CONFIG_ADB_PMU) |
| 143 | struct adb_request req; |
| 144 | #endif |
| 145 | |
Paul Mackerras | 143a1de | 2005-10-19 23:11:21 +1000 | [diff] [blame] | 146 | nowtime = mktime(tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, |
| 147 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 148 | nowtime += RTC_OFFSET; |
| 149 | |
| 150 | switch (sys_ctrler) { |
| 151 | #ifdef CONFIG_ADB_CUDA |
| 152 | case SYS_CTRLER_CUDA: |
| 153 | if (cuda_request(&req, NULL, 6, CUDA_PACKET, CUDA_SET_TIME, |
Paul Mackerras | 143a1de | 2005-10-19 23:11:21 +1000 | [diff] [blame] | 154 | nowtime >> 24, nowtime >> 16, nowtime >> 8, |
| 155 | nowtime) < 0) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 156 | return 0; |
| 157 | while (!req.complete) |
| 158 | cuda_poll(); |
| 159 | if ((req.reply_len != 3) && (req.reply_len != 7)) |
| 160 | printk(KERN_ERR "pmac_set_rtc_time: got %d byte reply\n", |
| 161 | req.reply_len); |
| 162 | return 1; |
| 163 | #endif /* CONFIG_ADB_CUDA */ |
| 164 | #ifdef CONFIG_ADB_PMU |
| 165 | case SYS_CTRLER_PMU: |
| 166 | if (pmu_request(&req, NULL, 5, PMU_SET_RTC, |
| 167 | nowtime >> 24, nowtime >> 16, nowtime >> 8, nowtime) < 0) |
| 168 | return 0; |
| 169 | while (!req.complete) |
| 170 | pmu_poll(); |
| 171 | if (req.reply_len != 0) |
| 172 | printk(KERN_ERR "pmac_set_rtc_time: got %d byte reply\n", |
| 173 | req.reply_len); |
| 174 | return 1; |
| 175 | #endif /* CONFIG_ADB_PMU */ |
| 176 | default: |
| 177 | return 0; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | /* |
| 182 | * Calibrate the decrementer register using VIA timer 1. |
| 183 | * This is used both on powermacs and CHRP machines. |
| 184 | */ |
| 185 | int __init |
| 186 | via_calibrate_decr(void) |
| 187 | { |
| 188 | struct device_node *vias; |
| 189 | volatile unsigned char __iomem *via; |
| 190 | int count = VIA_TIMER_FREQ_6 / 100; |
| 191 | unsigned int dstart, dend; |
| 192 | |
| 193 | vias = find_devices("via-cuda"); |
| 194 | if (vias == 0) |
| 195 | vias = find_devices("via-pmu"); |
| 196 | if (vias == 0) |
| 197 | vias = find_devices("via"); |
| 198 | if (vias == 0 || vias->n_addrs == 0) |
| 199 | return 0; |
| 200 | via = ioremap(vias->addrs[0].address, vias->addrs[0].size); |
| 201 | |
| 202 | /* set timer 1 for continuous interrupts */ |
| 203 | out_8(&via[ACR], (via[ACR] & ~T1MODE) | T1MODE_CONT); |
| 204 | /* set the counter to a small value */ |
| 205 | out_8(&via[T1CH], 2); |
| 206 | /* set the latch to `count' */ |
| 207 | out_8(&via[T1LL], count); |
| 208 | out_8(&via[T1LH], count >> 8); |
| 209 | /* wait until it hits 0 */ |
| 210 | while ((in_8(&via[IFR]) & T1_INT) == 0) |
| 211 | ; |
| 212 | dstart = get_dec(); |
| 213 | /* clear the interrupt & wait until it hits 0 again */ |
| 214 | in_8(&via[T1CL]); |
| 215 | while ((in_8(&via[IFR]) & T1_INT) == 0) |
| 216 | ; |
| 217 | dend = get_dec(); |
| 218 | |
Paul Mackerras | 5d14a18 | 2005-10-20 22:33:06 +1000 | [diff] [blame^] | 219 | ppc_tb_freq = (dstart - dend) * 100 / 6; |
Paul Mackerras | 5629d41 | 2005-10-12 17:01:50 +1000 | [diff] [blame] | 220 | tb_ticks_per_jiffy = (dstart - dend) / ((6 * HZ)/100); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 221 | |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 222 | printk(KERN_INFO "via_calibrate_decr: ticks per jiffy = %lu (%u ticks)\n", |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 223 | tb_ticks_per_jiffy, dstart - dend); |
| 224 | |
| 225 | iounmap(via); |
| 226 | |
| 227 | return 1; |
| 228 | } |
| 229 | |
| 230 | #ifdef CONFIG_PM |
| 231 | /* |
| 232 | * Reset the time after a sleep. |
| 233 | */ |
| 234 | static int |
| 235 | time_sleep_notify(struct pmu_sleep_notifier *self, int when) |
| 236 | { |
| 237 | static unsigned long time_diff; |
| 238 | unsigned long flags; |
| 239 | unsigned long seq; |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 240 | struct timespec tv; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 241 | |
| 242 | switch (when) { |
| 243 | case PBOOK_SLEEP_NOW: |
| 244 | do { |
| 245 | seq = read_seqbegin_irqsave(&xtime_lock, flags); |
Paul Mackerras | 143a1de | 2005-10-19 23:11:21 +1000 | [diff] [blame] | 246 | time_diff = xtime.tv_sec - pmac_get_boot_time(); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 247 | } while (read_seqretry_irqrestore(&xtime_lock, seq, flags)); |
| 248 | break; |
| 249 | case PBOOK_WAKE: |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 250 | tv.tv_sec = pmac_get_boot_time() + time_diff; |
| 251 | tv.tv_nsec = 0; |
| 252 | do_settimeofday(&tv); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 253 | break; |
| 254 | } |
| 255 | return PBOOK_SLEEP_OK; |
| 256 | } |
| 257 | |
| 258 | static struct pmu_sleep_notifier time_sleep_notifier = { |
| 259 | time_sleep_notify, SLEEP_LEVEL_MISC, |
| 260 | }; |
| 261 | #endif /* CONFIG_PM */ |
| 262 | |
| 263 | /* |
| 264 | * Query the OF and get the decr frequency. |
| 265 | * This was taken from the pmac time_init() when merging the prep/pmac |
| 266 | * time functions. |
| 267 | */ |
| 268 | void __init |
| 269 | pmac_calibrate_decr(void) |
| 270 | { |
| 271 | struct device_node *cpu; |
| 272 | unsigned int freq, *fp; |
| 273 | |
| 274 | #ifdef CONFIG_PM |
| 275 | pmu_register_sleep_notifier(&time_sleep_notifier); |
| 276 | #endif /* CONFIG_PM */ |
| 277 | |
| 278 | /* We assume MacRISC2 machines have correct device-tree |
| 279 | * calibration. That's better since the VIA itself seems |
| 280 | * to be slightly off. --BenH |
| 281 | */ |
| 282 | if (!machine_is_compatible("MacRISC2") && |
| 283 | !machine_is_compatible("MacRISC3") && |
| 284 | !machine_is_compatible("MacRISC4")) |
| 285 | if (via_calibrate_decr()) |
| 286 | return; |
| 287 | |
| 288 | /* Special case: QuickSilver G4s seem to have a badly calibrated |
| 289 | * timebase-frequency in OF, VIA is much better on these. We should |
| 290 | * probably implement calibration based on the KL timer on these |
| 291 | * machines anyway... -BenH |
| 292 | */ |
| 293 | if (machine_is_compatible("PowerMac3,5")) |
| 294 | if (via_calibrate_decr()) |
| 295 | return; |
| 296 | /* |
| 297 | * The cpu node should have a timebase-frequency property |
| 298 | * to tell us the rate at which the decrementer counts. |
| 299 | */ |
| 300 | cpu = find_type_devices("cpu"); |
| 301 | if (cpu == 0) |
| 302 | panic("can't find cpu node in time_init"); |
| 303 | fp = (unsigned int *) get_property(cpu, "timebase-frequency", NULL); |
| 304 | if (fp == 0) |
| 305 | panic("can't get cpu timebase frequency"); |
| 306 | freq = *fp; |
| 307 | printk("time_init: decrementer frequency = %u.%.6u MHz\n", |
| 308 | freq/1000000, freq%1000000); |
Paul Mackerras | 5d14a18 | 2005-10-20 22:33:06 +1000 | [diff] [blame^] | 309 | ppc_tb_freq = freq; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 310 | } |