OSDN Git Service

drm/nouveau: pass generic subdev to calculation routines
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / drivers / gpu / drm / nouveau / core / subdev / clock / pllnv04.c
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/bios.h>
25 #include <subdev/bios/pll.h>
26
27 #include "pll.h"
28
29 static int
30 getMNP_single(struct nouveau_subdev *subdev, struct nvbios_pll *info, int clk,
31               int *pN, int *pM, int *pP)
32 {
33         /* Find M, N and P for a single stage PLL
34          *
35          * Note that some bioses (NV3x) have lookup tables of precomputed MNP
36          * values, but we're too lazy to use those atm
37          *
38          * "clk" parameter in kHz
39          * returns calculated clock
40          */
41         int cv = nouveau_bios(subdev)->version.chip;
42         int minvco = info->vco1.min_freq, maxvco = info->vco1.max_freq;
43         int minM = info->vco1.min_m, maxM = info->vco1.max_m;
44         int minN = info->vco1.min_n, maxN = info->vco1.max_n;
45         int minU = info->vco1.min_inputfreq;
46         int maxU = info->vco1.max_inputfreq;
47         int minP = info->min_p;
48         int maxP = info->max_p_usable;
49         int crystal = info->refclk;
50         int M, N, thisP, P;
51         int clkP, calcclk;
52         int delta, bestdelta = INT_MAX;
53         int bestclk = 0;
54
55         /* this division verified for nv20, nv18, nv28 (Haiku), and nv34 */
56         /* possibly correlated with introduction of 27MHz crystal */
57         if (cv < 0x17 || cv == 0x1a || cv == 0x20) {
58                 if (clk > 250000)
59                         maxM = 6;
60                 if (clk > 340000)
61                         maxM = 2;
62         } else if (cv < 0x40) {
63                 if (clk > 150000)
64                         maxM = 6;
65                 if (clk > 200000)
66                         maxM = 4;
67                 if (clk > 340000)
68                         maxM = 2;
69         }
70
71         P = 1 << maxP;
72         if ((clk * P) < minvco) {
73                 minvco = clk * maxP;
74                 maxvco = minvco * 2;
75         }
76
77         if (clk + clk/200 > maxvco)     /* +0.5% */
78                 maxvco = clk + clk/200;
79
80         /* NV34 goes maxlog2P->0, NV20 goes 0->maxlog2P */
81         for (thisP = minP; thisP <= maxP; thisP++) {
82                 P = 1 << thisP;
83                 clkP = clk * P;
84
85                 if (clkP < minvco)
86                         continue;
87                 if (clkP > maxvco)
88                         return bestclk;
89
90                 for (M = minM; M <= maxM; M++) {
91                         if (crystal/M < minU)
92                                 return bestclk;
93                         if (crystal/M > maxU)
94                                 continue;
95
96                         /* add crystal/2 to round better */
97                         N = (clkP * M + crystal/2) / crystal;
98
99                         if (N < minN)
100                                 continue;
101                         if (N > maxN)
102                                 break;
103
104                         /* more rounding additions */
105                         calcclk = ((N * crystal + P/2) / P + M/2) / M;
106                         delta = abs(calcclk - clk);
107                         /* we do an exhaustive search rather than terminating
108                          * on an optimality condition...
109                          */
110                         if (delta < bestdelta) {
111                                 bestdelta = delta;
112                                 bestclk = calcclk;
113                                 *pN = N;
114                                 *pM = M;
115                                 *pP = thisP;
116                                 if (delta == 0) /* except this one */
117                                         return bestclk;
118                         }
119                 }
120         }
121
122         return bestclk;
123 }
124
125 static int
126 getMNP_double(struct nouveau_subdev *subdev, struct nvbios_pll *info, int clk,
127               int *pN1, int *pM1, int *pN2, int *pM2, int *pP)
128 {
129         /* Find M, N and P for a two stage PLL
130          *
131          * Note that some bioses (NV30+) have lookup tables of precomputed MNP
132          * values, but we're too lazy to use those atm
133          *
134          * "clk" parameter in kHz
135          * returns calculated clock
136          */
137         int chip_version = nouveau_bios(subdev)->version.chip;
138         int minvco1 = info->vco1.min_freq, maxvco1 = info->vco1.max_freq;
139         int minvco2 = info->vco2.min_freq, maxvco2 = info->vco2.max_freq;
140         int minU1 = info->vco1.min_inputfreq, minU2 = info->vco2.min_inputfreq;
141         int maxU1 = info->vco1.max_inputfreq, maxU2 = info->vco2.max_inputfreq;
142         int minM1 = info->vco1.min_m, maxM1 = info->vco1.max_m;
143         int minN1 = info->vco1.min_n, maxN1 = info->vco1.max_n;
144         int minM2 = info->vco2.min_m, maxM2 = info->vco2.max_m;
145         int minN2 = info->vco2.min_n, maxN2 = info->vco2.max_n;
146         int maxlog2P = info->max_p_usable;
147         int crystal = info->refclk;
148         bool fixedgain2 = (minM2 == maxM2 && minN2 == maxN2);
149         int M1, N1, M2, N2, log2P;
150         int clkP, calcclk1, calcclk2, calcclkout;
151         int delta, bestdelta = INT_MAX;
152         int bestclk = 0;
153
154         int vco2 = (maxvco2 - maxvco2/200) / 2;
155         for (log2P = 0; clk && log2P < maxlog2P && clk <= (vco2 >> log2P); log2P++)
156                 ;
157         clkP = clk << log2P;
158
159         if (maxvco2 < clk + clk/200)    /* +0.5% */
160                 maxvco2 = clk + clk/200;
161
162         for (M1 = minM1; M1 <= maxM1; M1++) {
163                 if (crystal/M1 < minU1)
164                         return bestclk;
165                 if (crystal/M1 > maxU1)
166                         continue;
167
168                 for (N1 = minN1; N1 <= maxN1; N1++) {
169                         calcclk1 = crystal * N1 / M1;
170                         if (calcclk1 < minvco1)
171                                 continue;
172                         if (calcclk1 > maxvco1)
173                                 break;
174
175                         for (M2 = minM2; M2 <= maxM2; M2++) {
176                                 if (calcclk1/M2 < minU2)
177                                         break;
178                                 if (calcclk1/M2 > maxU2)
179                                         continue;
180
181                                 /* add calcclk1/2 to round better */
182                                 N2 = (clkP * M2 + calcclk1/2) / calcclk1;
183                                 if (N2 < minN2)
184                                         continue;
185                                 if (N2 > maxN2)
186                                         break;
187
188                                 if (!fixedgain2) {
189                                         if (chip_version < 0x60)
190                                                 if (N2/M2 < 4 || N2/M2 > 10)
191                                                         continue;
192
193                                         calcclk2 = calcclk1 * N2 / M2;
194                                         if (calcclk2 < minvco2)
195                                                 break;
196                                         if (calcclk2 > maxvco2)
197                                                 continue;
198                                 } else
199                                         calcclk2 = calcclk1;
200
201                                 calcclkout = calcclk2 >> log2P;
202                                 delta = abs(calcclkout - clk);
203                                 /* we do an exhaustive search rather than terminating
204                                  * on an optimality condition...
205                                  */
206                                 if (delta < bestdelta) {
207                                         bestdelta = delta;
208                                         bestclk = calcclkout;
209                                         *pN1 = N1;
210                                         *pM1 = M1;
211                                         *pN2 = N2;
212                                         *pM2 = M2;
213                                         *pP = log2P;
214                                         if (delta == 0) /* except this one */
215                                                 return bestclk;
216                                 }
217                         }
218                 }
219         }
220
221         return bestclk;
222 }
223
224 int
225 nv04_pll_calc(struct nouveau_subdev *subdev, struct nvbios_pll *info, u32 freq,
226               int *N1, int *M1, int *N2, int *M2, int *P)
227 {
228         int ret;
229
230         if (!info->vco2.max_freq) {
231                 ret = getMNP_single(subdev, info, freq, N1, M1, P);
232                 *N2 = 1;
233                 *M2 = 1;
234         } else {
235                 ret = getMNP_double(subdev, info, freq, N1, M1, N2, M2, P);
236         }
237
238         if (!ret)
239                 nv_error(subdev, "unable to compute acceptable pll values\n");
240         return ret;
241 }