Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
pllnv04.c
Go to the documentation of this file.
1 /*
2  * Copyright 1993-2003 NVIDIA, Corporation
3  * Copyright 2007-2009 Stuart Bennett
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
20  * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 
24 #include <subdev/clock.h>
25 #include <subdev/bios.h>
26 #include <subdev/bios/pll.h>
27 
28 #include "pll.h"
29 
30 static int
31 getMNP_single(struct nouveau_clock *clock, struct nvbios_pll *info, int clk,
32  int *pN, int *pM, int *pP)
33 {
34  /* Find M, N and P for a single stage PLL
35  *
36  * Note that some bioses (NV3x) have lookup tables of precomputed MNP
37  * values, but we're too lazy to use those atm
38  *
39  * "clk" parameter in kHz
40  * returns calculated clock
41  */
42  int cv = nouveau_bios(clock)->version.chip;
43  int minvco = info->vco1.min_freq, maxvco = info->vco1.max_freq;
44  int minM = info->vco1.min_m, maxM = info->vco1.max_m;
45  int minN = info->vco1.min_n, maxN = info->vco1.max_n;
46  int minU = info->vco1.min_inputfreq;
47  int maxU = info->vco1.max_inputfreq;
48  int minP = info->min_p;
49  int maxP = info->max_p_usable;
50  int crystal = info->refclk;
51  int M, N, thisP, P;
52  int clkP, calcclk;
53  int delta, bestdelta = INT_MAX;
54  int bestclk = 0;
55 
56  /* this division verified for nv20, nv18, nv28 (Haiku), and nv34 */
57  /* possibly correlated with introduction of 27MHz crystal */
58  if (cv < 0x17 || cv == 0x1a || cv == 0x20) {
59  if (clk > 250000)
60  maxM = 6;
61  if (clk > 340000)
62  maxM = 2;
63  } else if (cv < 0x40) {
64  if (clk > 150000)
65  maxM = 6;
66  if (clk > 200000)
67  maxM = 4;
68  if (clk > 340000)
69  maxM = 2;
70  }
71 
72  P = 1 << maxP;
73  if ((clk * P) < minvco) {
74  minvco = clk * maxP;
75  maxvco = minvco * 2;
76  }
77 
78  if (clk + clk/200 > maxvco) /* +0.5% */
79  maxvco = clk + clk/200;
80 
81  /* NV34 goes maxlog2P->0, NV20 goes 0->maxlog2P */
82  for (thisP = minP; thisP <= maxP; thisP++) {
83  P = 1 << thisP;
84  clkP = clk * P;
85 
86  if (clkP < minvco)
87  continue;
88  if (clkP > maxvco)
89  return bestclk;
90 
91  for (M = minM; M <= maxM; M++) {
92  if (crystal/M < minU)
93  return bestclk;
94  if (crystal/M > maxU)
95  continue;
96 
97  /* add crystal/2 to round better */
98  N = (clkP * M + crystal/2) / crystal;
99 
100  if (N < minN)
101  continue;
102  if (N > maxN)
103  break;
104 
105  /* more rounding additions */
106  calcclk = ((N * crystal + P/2) / P + M/2) / M;
107  delta = abs(calcclk - clk);
108  /* we do an exhaustive search rather than terminating
109  * on an optimality condition...
110  */
111  if (delta < bestdelta) {
112  bestdelta = delta;
113  bestclk = calcclk;
114  *pN = N;
115  *pM = M;
116  *pP = thisP;
117  if (delta == 0) /* except this one */
118  return bestclk;
119  }
120  }
121  }
122 
123  return bestclk;
124 }
125 
126 static int
127 getMNP_double(struct nouveau_clock *clock, struct nvbios_pll *info, int clk,
128  int *pN1, int *pM1, int *pN2, int *pM2, int *pP)
129 {
130  /* Find M, N and P for a two stage PLL
131  *
132  * Note that some bioses (NV30+) have lookup tables of precomputed MNP
133  * values, but we're too lazy to use those atm
134  *
135  * "clk" parameter in kHz
136  * returns calculated clock
137  */
138  int chip_version = nouveau_bios(clock)->version.chip;
139  int minvco1 = info->vco1.min_freq, maxvco1 = info->vco1.max_freq;
140  int minvco2 = info->vco2.min_freq, maxvco2 = info->vco2.max_freq;
141  int minU1 = info->vco1.min_inputfreq, minU2 = info->vco2.min_inputfreq;
142  int maxU1 = info->vco1.max_inputfreq, maxU2 = info->vco2.max_inputfreq;
143  int minM1 = info->vco1.min_m, maxM1 = info->vco1.max_m;
144  int minN1 = info->vco1.min_n, maxN1 = info->vco1.max_n;
145  int minM2 = info->vco2.min_m, maxM2 = info->vco2.max_m;
146  int minN2 = info->vco2.min_n, maxN2 = info->vco2.max_n;
147  int maxlog2P = info->max_p_usable;
148  int crystal = info->refclk;
149  bool fixedgain2 = (minM2 == maxM2 && minN2 == maxN2);
150  int M1, N1, M2, N2, log2P;
151  int clkP, calcclk1, calcclk2, calcclkout;
152  int delta, bestdelta = INT_MAX;
153  int bestclk = 0;
154 
155  int vco2 = (maxvco2 - maxvco2/200) / 2;
156  for (log2P = 0; clk && log2P < maxlog2P && clk <= (vco2 >> log2P); log2P++)
157  ;
158  clkP = clk << log2P;
159 
160  if (maxvco2 < clk + clk/200) /* +0.5% */
161  maxvco2 = clk + clk/200;
162 
163  for (M1 = minM1; M1 <= maxM1; M1++) {
164  if (crystal/M1 < minU1)
165  return bestclk;
166  if (crystal/M1 > maxU1)
167  continue;
168 
169  for (N1 = minN1; N1 <= maxN1; N1++) {
170  calcclk1 = crystal * N1 / M1;
171  if (calcclk1 < minvco1)
172  continue;
173  if (calcclk1 > maxvco1)
174  break;
175 
176  for (M2 = minM2; M2 <= maxM2; M2++) {
177  if (calcclk1/M2 < minU2)
178  break;
179  if (calcclk1/M2 > maxU2)
180  continue;
181 
182  /* add calcclk1/2 to round better */
183  N2 = (clkP * M2 + calcclk1/2) / calcclk1;
184  if (N2 < minN2)
185  continue;
186  if (N2 > maxN2)
187  break;
188 
189  if (!fixedgain2) {
190  if (chip_version < 0x60)
191  if (N2/M2 < 4 || N2/M2 > 10)
192  continue;
193 
194  calcclk2 = calcclk1 * N2 / M2;
195  if (calcclk2 < minvco2)
196  break;
197  if (calcclk2 > maxvco2)
198  continue;
199  } else
200  calcclk2 = calcclk1;
201 
202  calcclkout = calcclk2 >> log2P;
203  delta = abs(calcclkout - clk);
204  /* we do an exhaustive search rather than terminating
205  * on an optimality condition...
206  */
207  if (delta < bestdelta) {
208  bestdelta = delta;
209  bestclk = calcclkout;
210  *pN1 = N1;
211  *pM1 = M1;
212  *pN2 = N2;
213  *pM2 = M2;
214  *pP = log2P;
215  if (delta == 0) /* except this one */
216  return bestclk;
217  }
218  }
219  }
220  }
221 
222  return bestclk;
223 }
224 
225 int
226 nv04_pll_calc(struct nouveau_clock *clk, struct nvbios_pll *info, u32 freq,
227  int *N1, int *M1, int *N2, int *M2, int *P)
228 {
229  int ret;
230 
231  if (!info->vco2.max_freq) {
232  ret = getMNP_single(clk, info, freq, N1, M1, P);
233  *N2 = 1;
234  *M2 = 1;
235  } else {
236  ret = getMNP_double(clk, info, freq, N1, M1, N2, M2, P);
237  }
238 
239  if (!ret)
240  nv_error(clk, "unable to compute acceptable pll values\n");
241  return ret;
242 }