]> git.saurik.com Git - apple/xnu.git/blame - pexpert/ppc/pe_clock_speed.c
xnu-792.10.96.tar.gz
[apple/xnu.git] / pexpert / ppc / pe_clock_speed.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
37839358
A
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
1c79356b 11 *
37839358
A
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1c79356b
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
37839358
A
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
1c79356b
A
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/*
23 * pe_clock_speed.c - Determine the best guess for the processor and bus
24 * speed buy using the values returned by run_clock_test.
25 *
90556fb8 26 * (c) Apple Computer, Inc. 1998-2002
1c79356b
A
27 *
28 * Writen by: Josh de Cesare
29 *
30 */
31
32#include <pexpert/pexpert.h>
33
34#include <ppc/machine_routines.h>
35
36// prototypes
37extern void pe_run_clock_test(void *tmp);
38void pe_do_clock_test(unsigned int via_addr,
39 int num_speeds, unsigned long *speed_list);
40
41// Threshold for bus speed matches.
42#define kMaxFreqDiff (30000)
43
44// This is the structure for the data that get passed to pe_run_clock_test.
45struct clock_test_data {
46 unsigned int via_addr;
47 unsigned int via_ticks;
48 unsigned int dec_ticks;
49};
50
51// glocal variables to simplify some stuff.
52static long bus_freq_num, bus_freq_den, cpu_pll;
53
54// PE_Determine_Clock_Speeds is called by the via driver in IOKit
55// It uses the numbers generated by pe_do_clock_test and reports
56// the cleaned up values to the rest of the OS.
57void PE_Determine_Clock_Speeds(unsigned int via_addr, int num_speeds,
58 unsigned long *speed_list)
59{
60 boolean_t oldLevel;
61 unsigned long tmp_bus_speed, tmp_cpu_speed;
62 unsigned long long tmp;
63
64 oldLevel = ml_set_interrupts_enabled(FALSE);
65 pe_do_clock_test(via_addr, num_speeds, speed_list);
66 ml_set_interrupts_enabled(oldLevel);
67
68 tmp_bus_speed = bus_freq_num / bus_freq_den;
69 tmp = ((unsigned long long)bus_freq_num * cpu_pll) / (bus_freq_den * 2);
70 tmp_cpu_speed = (unsigned long)tmp;
71
72 // Report the bus clock rate as is.
73 gPEClockFrequencyInfo.bus_clock_rate_num = bus_freq_num;
74 gPEClockFrequencyInfo.bus_clock_rate_den = bus_freq_den;
75
76 // pll multipliers are in halfs so set the denominator to 2.
77 gPEClockFrequencyInfo.bus_to_cpu_rate_num = cpu_pll;
78 gPEClockFrequencyInfo.bus_to_cpu_rate_den = 2;
79
80 // The decrementer rate is one fourth the bus rate.
81 gPEClockFrequencyInfo.bus_to_dec_rate_num = 1;
82 gPEClockFrequencyInfo.bus_to_dec_rate_den = 4;
83
90556fb8
A
84 // Assume that the timebase frequency is derived from the bus clock.
85 gPEClockFrequencyInfo.timebase_frequency_num = bus_freq_num;
86 gPEClockFrequencyInfo.timebase_frequency_den = bus_freq_den * 4;
87
1c79356b
A
88 // Set the truncated numbers in gPEClockFrequencyInfo.
89 gPEClockFrequencyInfo.bus_clock_rate_hz = tmp_bus_speed;
90 gPEClockFrequencyInfo.cpu_clock_rate_hz = tmp_cpu_speed;
91 gPEClockFrequencyInfo.dec_clock_rate_hz = tmp_bus_speed / 4;
90556fb8 92 gPEClockFrequencyInfo.timebase_frequency_hz = tmp_bus_speed / 4;
1c79356b 93
43866e37
A
94 gPEClockFrequencyInfo.bus_frequency_hz = tmp_bus_speed;
95 gPEClockFrequencyInfo.bus_frequency_min_hz = tmp_bus_speed;
96 gPEClockFrequencyInfo.bus_frequency_max_hz = tmp_bus_speed;
97 gPEClockFrequencyInfo.cpu_frequency_hz = tmp_cpu_speed;
98 gPEClockFrequencyInfo.cpu_frequency_min_hz = tmp_cpu_speed;
99 gPEClockFrequencyInfo.cpu_frequency_max_hz = tmp_cpu_speed;
100
1c79356b
A
101 PE_call_timebase_callback();
102}
103
104// pe_do_clock_test uses the number from pe_run_clock_test to
105// find a best fit guess for the bus speed.
106void pe_do_clock_test(unsigned int via_addr,
107 int num_speeds, unsigned long *speed_list)
108{
109 struct clock_test_data clock_test_data;
110 long cnt, diff, raw_cpu_freq, raw_bus_freq, tmp_bus_freq,
111 last_bus_freq, tries = 10;
112
113 // Save the via addr so the asm part can use it.
114 clock_test_data.via_addr = via_addr;
115
116 // Keep looping until it matches the last try.
117 bus_freq_num = 0;
118 do {
119 last_bus_freq = bus_freq_num;
120
121 // The the asm part to do the real work.
122 pe_run_clock_test((void *)&clock_test_data);
123
124 // First find the pll mode. Allow any integer times two.
125 cpu_pll = 10000000 / clock_test_data.dec_ticks;
126 cpu_pll = (cpu_pll / 2) + (cpu_pll & 1);
127
128 // Using 64 bit math figure out the raw bus speed.
129 // 0xBF401675E5DULL is 1 / 1.27655us times 2 ^ 24.
130 raw_bus_freq = ((0xBF401675E5DULL * clock_test_data.dec_ticks) /
131 clock_test_data.via_ticks) >> 22;
132
133 // use the pll mode and the raw bus speed to find the raw cpu speed.
134 raw_cpu_freq = raw_bus_freq * cpu_pll / 2;
135
136 // Look to see if the bus speed is close to one of the
137 // speeds in the table.
138 for (cnt = 0; cnt < num_speeds; cnt++) {
139 bus_freq_num = speed_list[cnt * 2];
140 bus_freq_den = speed_list[cnt * 2 + 1];
141 diff = bus_freq_num - raw_bus_freq * bus_freq_den;
142 if (diff < 0) diff = -diff;
143
144 if (diff < kMaxFreqDiff * bus_freq_den) break;
145 }
146 if (cnt != num_speeds) continue;
147
148 // Look to see if the bus speed is close to n * 0.5 MHz
149 tmp_bus_freq = ((raw_bus_freq + 250000) / 500000) * 500000;
150
151 diff = tmp_bus_freq - raw_bus_freq;
152 if (diff < 0) diff = -diff;
153
154 if (diff < kMaxFreqDiff) {
155 bus_freq_num = tmp_bus_freq;
156 bus_freq_den = 1;
157 continue;
158 }
159
160 // Look to see if the bus speed is close to n * 50/3 MHz
161 tmp_bus_freq = ((raw_bus_freq * 3 + 25000000) / 50000000) * 50000000;
162
163 diff = tmp_bus_freq - raw_bus_freq * 3;
164 if (diff < 0) diff = -diff;
165
166 if (diff < kMaxFreqDiff * 3) {
167 bus_freq_num = tmp_bus_freq;
168 bus_freq_den = 3;
169 continue;
170 }
171
172 // Since all else failed return the raw bus speed
173 bus_freq_num = raw_bus_freq;
174 bus_freq_den = 1;
175 } while ((bus_freq_num != last_bus_freq) && tries--);
176}