]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/nouveau/core/subdev/clock/nva3.c
Merge remote-tracking branch 'regmap/fix/core' into regmap-linus
[karo-tx-linux.git] / drivers / gpu / drm / nouveau / core / subdev / clock / nva3.c
1 /*
2  * Copyright 2012 Red Hat Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: Ben Skeggs
23  */
24
25 #include <subdev/bios.h>
26 #include <subdev/bios/pll.h>
27 #include <subdev/timer.h>
28
29 #include "pll.h"
30
31 #include "nva3.h"
32
33 struct nva3_clock_priv {
34         struct nouveau_clock base;
35         struct nva3_clock_info eng[nv_clk_src_max];
36 };
37
38 static u32 read_clk(struct nva3_clock_priv *, int, bool);
39 static u32 read_pll(struct nva3_clock_priv *, int, u32);
40
41 static u32
42 read_vco(struct nva3_clock_priv *priv, int clk)
43 {
44         u32 sctl = nv_rd32(priv, 0x4120 + (clk * 4));
45         if ((sctl & 0x00000030) != 0x00000030)
46                 return read_pll(priv, 0x41, 0x00e820);
47         return read_pll(priv, 0x42, 0x00e8a0);
48 }
49
50 static u32
51 read_clk(struct nva3_clock_priv *priv, int clk, bool ignore_en)
52 {
53         u32 sctl, sdiv, sclk;
54
55         /* refclk for the 0xe8xx plls is a fixed frequency */
56         if (clk >= 0x40) {
57                 if (nv_device(priv)->chipset == 0xaf) {
58                         /* no joke.. seriously.. sigh.. */
59                         return nv_rd32(priv, 0x00471c) * 1000;
60                 }
61
62                 return nv_device(priv)->crystal;
63         }
64
65         sctl = nv_rd32(priv, 0x4120 + (clk * 4));
66         if (!ignore_en && !(sctl & 0x00000100))
67                 return 0;
68
69         switch (sctl & 0x00003000) {
70         case 0x00000000:
71                 return nv_device(priv)->crystal;
72         case 0x00002000:
73                 if (sctl & 0x00000040)
74                         return 108000;
75                 return 100000;
76         case 0x00003000:
77                 sclk = read_vco(priv, clk);
78                 sdiv = ((sctl & 0x003f0000) >> 16) + 2;
79                 return (sclk * 2) / sdiv;
80         default:
81                 return 0;
82         }
83 }
84
85 static u32
86 read_pll(struct nva3_clock_priv *priv, int clk, u32 pll)
87 {
88         u32 ctrl = nv_rd32(priv, pll + 0);
89         u32 sclk = 0, P = 1, N = 1, M = 1;
90
91         if (!(ctrl & 0x00000008)) {
92                 if (ctrl & 0x00000001) {
93                         u32 coef = nv_rd32(priv, pll + 4);
94                         M = (coef & 0x000000ff) >> 0;
95                         N = (coef & 0x0000ff00) >> 8;
96                         P = (coef & 0x003f0000) >> 16;
97
98                         /* no post-divider on these.. */
99                         if ((pll & 0x00ff00) == 0x00e800)
100                                 P = 1;
101
102                         sclk = read_clk(priv, 0x00 + clk, false);
103                 }
104         } else {
105                 sclk = read_clk(priv, 0x10 + clk, false);
106         }
107
108         if (M * P)
109                 return sclk * N / (M * P);
110         return 0;
111 }
112
113 static int
114 nva3_clock_read(struct nouveau_clock *clk, enum nv_clk_src src)
115 {
116         struct nva3_clock_priv *priv = (void *)clk;
117
118         switch (src) {
119         case nv_clk_src_crystal:
120                 return nv_device(priv)->crystal;
121         case nv_clk_src_href:
122                 return 100000;
123         case nv_clk_src_core:
124                 return read_pll(priv, 0x00, 0x4200);
125         case nv_clk_src_shader:
126                 return read_pll(priv, 0x01, 0x4220);
127         case nv_clk_src_mem:
128                 return read_pll(priv, 0x02, 0x4000);
129         case nv_clk_src_disp:
130                 return read_clk(priv, 0x20, false);
131         case nv_clk_src_vdec:
132                 return read_clk(priv, 0x21, false);
133         case nv_clk_src_daemon:
134                 return read_clk(priv, 0x25, false);
135         default:
136                 nv_error(clk, "invalid clock source %d\n", src);
137                 return -EINVAL;
138         }
139 }
140
141 int
142 nva3_clock_info(struct nouveau_clock *clock, int clk, u32 pll, u32 khz,
143                 struct nva3_clock_info *info)
144 {
145         struct nouveau_bios *bios = nouveau_bios(clock);
146         struct nva3_clock_priv *priv = (void *)clock;
147         struct nvbios_pll limits;
148         u32 oclk, sclk, sdiv;
149         int P, N, M, diff;
150         int ret;
151
152         info->pll = 0;
153         info->clk = 0;
154
155         switch (khz) {
156         case 27000:
157                 info->clk = 0x00000100;
158                 return khz;
159         case 100000:
160                 info->clk = 0x00002100;
161                 return khz;
162         case 108000:
163                 info->clk = 0x00002140;
164                 return khz;
165         default:
166                 sclk = read_vco(priv, clk);
167                 sdiv = min((sclk * 2) / (khz - 2999), (u32)65);
168                 /* if the clock has a PLL attached, and we can get a within
169                  * [-2, 3) MHz of a divider, we'll disable the PLL and use
170                  * the divider instead.
171                  *
172                  * divider can go as low as 2, limited here because NVIDIA
173                  * and the VBIOS on my NVA8 seem to prefer using the PLL
174                  * for 810MHz - is there a good reason?
175                  */
176                 if (sdiv > 4) {
177                         oclk = (sclk * 2) / sdiv;
178                         diff = khz - oclk;
179                         if (!pll || (diff >= -2000 && diff < 3000)) {
180                                 info->clk = (((sdiv - 2) << 16) | 0x00003100);
181                                 return oclk;
182                         }
183                 }
184
185                 if (!pll)
186                         return -ERANGE;
187                 break;
188         }
189
190         ret = nvbios_pll_parse(bios, pll, &limits);
191         if (ret)
192                 return ret;
193
194         limits.refclk = read_clk(priv, clk - 0x10, true);
195         if (!limits.refclk)
196                 return -EINVAL;
197
198         ret = nva3_pll_calc(nv_subdev(priv), &limits, khz, &N, NULL, &M, &P);
199         if (ret >= 0) {
200                 info->clk = nv_rd32(priv, 0x4120 + (clk * 4));
201                 info->pll = (P << 16) | (N << 8) | M;
202         }
203
204         return ret ? ret : -ERANGE;
205 }
206
207 static int
208 calc_clk(struct nva3_clock_priv *priv, struct nouveau_cstate *cstate,
209          int clk, u32 pll, int idx)
210 {
211         int ret = nva3_clock_info(&priv->base, clk, pll, cstate->domain[idx],
212                                   &priv->eng[idx]);
213         if (ret >= 0)
214                 return 0;
215         return ret;
216 }
217
218 static void
219 prog_pll(struct nva3_clock_priv *priv, int clk, u32 pll, int idx)
220 {
221         struct nva3_clock_info *info = &priv->eng[idx];
222         const u32 src0 = 0x004120 + (clk * 4);
223         const u32 src1 = 0x004160 + (clk * 4);
224         const u32 ctrl = pll + 0;
225         const u32 coef = pll + 4;
226
227         if (info->pll) {
228                 nv_mask(priv, src0, 0x00000101, 0x00000101);
229                 nv_wr32(priv, coef, info->pll);
230                 nv_mask(priv, ctrl, 0x00000015, 0x00000015);
231                 nv_mask(priv, ctrl, 0x00000010, 0x00000000);
232                 nv_wait(priv, ctrl, 0x00020000, 0x00020000);
233                 nv_mask(priv, ctrl, 0x00000010, 0x00000010);
234                 nv_mask(priv, ctrl, 0x00000008, 0x00000000);
235                 nv_mask(priv, src1, 0x00000100, 0x00000000);
236                 nv_mask(priv, src1, 0x00000001, 0x00000000);
237         } else {
238                 nv_mask(priv, src1, 0x003f3141, 0x00000101 | info->clk);
239                 nv_mask(priv, ctrl, 0x00000018, 0x00000018);
240                 udelay(20);
241                 nv_mask(priv, ctrl, 0x00000001, 0x00000000);
242                 nv_mask(priv, src0, 0x00000100, 0x00000000);
243                 nv_mask(priv, src0, 0x00000001, 0x00000000);
244         }
245 }
246
247 static void
248 prog_clk(struct nva3_clock_priv *priv, int clk, int idx)
249 {
250         struct nva3_clock_info *info = &priv->eng[idx];
251         nv_mask(priv, 0x004120 + (clk * 4), 0x003f3141, 0x00000101 | info->clk);
252 }
253
254 static int
255 nva3_clock_calc(struct nouveau_clock *clk, struct nouveau_cstate *cstate)
256 {
257         struct nva3_clock_priv *priv = (void *)clk;
258         int ret;
259
260         if ((ret = calc_clk(priv, cstate, 0x10, 0x4200, nv_clk_src_core)) ||
261             (ret = calc_clk(priv, cstate, 0x11, 0x4220, nv_clk_src_shader)) ||
262             (ret = calc_clk(priv, cstate, 0x20, 0x0000, nv_clk_src_disp)) ||
263             (ret = calc_clk(priv, cstate, 0x21, 0x0000, nv_clk_src_vdec)))
264                 return ret;
265
266         return 0;
267 }
268
269 static int
270 nva3_clock_prog(struct nouveau_clock *clk)
271 {
272         struct nva3_clock_priv *priv = (void *)clk;
273         prog_pll(priv, 0x00, 0x004200, nv_clk_src_core);
274         prog_pll(priv, 0x01, 0x004220, nv_clk_src_shader);
275         prog_clk(priv, 0x20, nv_clk_src_disp);
276         prog_clk(priv, 0x21, nv_clk_src_vdec);
277         return 0;
278 }
279
280 static void
281 nva3_clock_tidy(struct nouveau_clock *clk)
282 {
283 }
284
285 static struct nouveau_clocks
286 nva3_domain[] = {
287         { nv_clk_src_crystal, 0xff },
288         { nv_clk_src_href   , 0xff },
289         { nv_clk_src_core   , 0x00, 0, "core", 1000 },
290         { nv_clk_src_shader , 0x01, 0, "shader", 1000 },
291         { nv_clk_src_mem    , 0x02, 0, "memory", 1000 },
292         { nv_clk_src_vdec   , 0x03 },
293         { nv_clk_src_disp   , 0x04 },
294         { nv_clk_src_max }
295 };
296
297 static int
298 nva3_clock_ctor(struct nouveau_object *parent, struct nouveau_object *engine,
299                 struct nouveau_oclass *oclass, void *data, u32 size,
300                 struct nouveau_object **pobject)
301 {
302         struct nva3_clock_priv *priv;
303         int ret;
304
305         ret = nouveau_clock_create(parent, engine, oclass, nva3_domain, &priv);
306         *pobject = nv_object(priv);
307         if (ret)
308                 return ret;
309
310         priv->base.read = nva3_clock_read;
311         priv->base.calc = nva3_clock_calc;
312         priv->base.prog = nva3_clock_prog;
313         priv->base.tidy = nva3_clock_tidy;
314         return 0;
315 }
316
317 struct nouveau_oclass
318 nva3_clock_oclass = {
319         .handle = NV_SUBDEV(CLOCK, 0xa3),
320         .ofuncs = &(struct nouveau_ofuncs) {
321                 .ctor = nva3_clock_ctor,
322                 .dtor = _nouveau_clock_dtor,
323                 .init = _nouveau_clock_init,
324                 .fini = _nouveau_clock_fini,
325         },
326 };