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