blob: ab8cfad5a9b01f060769ad41bab7297867316a46 [file] [log] [blame]
Ben Skeggs02c30ca2010-09-16 16:17:35 +10001/*
2 * Copyright 2010 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 "drmP.h"
26#include "nouveau_drv.h"
Ben Skeggsaee582d2010-09-27 10:13:23 +100027#include "nouveau_bios.h"
Ben Skeggsf3fbaf32011-10-26 09:11:02 +100028#include "nouveau_hw.h"
Ben Skeggs02c30ca2010-09-16 16:17:35 +100029#include "nouveau_pm.h"
30
Ben Skeggsf3fbaf32011-10-26 09:11:02 +100031enum clk_src {
32 clk_src_crystal,
33 clk_src_href,
34 clk_src_hclk,
35 clk_src_hclkm3,
36 clk_src_hclkm3d2,
37 clk_src_host,
38 clk_src_nvclk,
39 clk_src_sclk,
40 clk_src_mclk,
41 clk_src_vdec,
42 clk_src_dom6
Ben Skeggs02c30ca2010-09-16 16:17:35 +100043};
44
Ben Skeggsf3fbaf32011-10-26 09:11:02 +100045static u32 read_clk(struct drm_device *, enum clk_src);
46
47static u32
48read_div(struct drm_device *dev)
Ben Skeggs02c30ca2010-09-16 16:17:35 +100049{
Ben Skeggsf3fbaf32011-10-26 09:11:02 +100050 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs02c30ca2010-09-16 16:17:35 +100051
Ben Skeggsf3fbaf32011-10-26 09:11:02 +100052 switch (dev_priv->chipset) {
53 case 0x50: /* it exists, but only has bit 31, not the dividers.. */
54 case 0x84:
55 case 0x86:
56 case 0x98:
57 case 0xa0:
58 return nv_rd32(dev, 0x004700);
59 case 0x92:
60 case 0x94:
61 case 0x96:
62 return nv_rd32(dev, 0x004800);
63 default:
64 return 0x00000000;
65 }
66}
Ben Skeggs02c30ca2010-09-16 16:17:35 +100067
Ben Skeggsf3fbaf32011-10-26 09:11:02 +100068static u32
69read_pll_ref(struct drm_device *dev, u32 base)
70{
71 struct drm_nouveau_private *dev_priv = dev->dev_private;
72 u32 coef, ref = read_clk(dev, clk_src_crystal);
73 u32 rsel = nv_rd32(dev, 0x00e18c);
74 int P, N, M, id;
Emil Velikov619d4f72011-04-11 20:43:23 +010075
Ben Skeggsf3fbaf32011-10-26 09:11:02 +100076 switch (dev_priv->chipset) {
77 case 0x50:
78 case 0xa0:
79 switch (base) {
80 case 0x4020:
81 case 0x4028: id = !!(rsel & 0x00000004); break;
82 case 0x4008: id = !!(rsel & 0x00000008); break;
83 case 0x4030: id = 0; break;
84 default:
85 NV_ERROR(dev, "ref: bad pll 0x%06x\n", base);
86 return 0;
87 }
88
89 coef = nv_rd32(dev, 0x00e81c + (id * 0x0c));
90 ref *= (coef & 0x01000000) ? 2 : 4;
91 P = (coef & 0x00070000) >> 16;
92 N = ((coef & 0x0000ff00) >> 8) + 1;
93 M = ((coef & 0x000000ff) >> 0) + 1;
94 break;
95 case 0x84:
96 case 0x86:
97 case 0x92:
98 coef = nv_rd32(dev, 0x00e81c);
99 P = (coef & 0x00070000) >> 16;
100 N = (coef & 0x0000ff00) >> 8;
101 M = (coef & 0x000000ff) >> 0;
102 break;
103 case 0x94:
104 case 0x96:
105 case 0x98:
106 rsel = nv_rd32(dev, 0x00c050);
107 switch (base) {
108 case 0x4020: rsel = (rsel & 0x00000003) >> 0; break;
109 case 0x4008: rsel = (rsel & 0x0000000c) >> 2; break;
110 case 0x4028: rsel = (rsel & 0x00001800) >> 11; break;
111 case 0x4030: rsel = 3; break;
112 default:
113 NV_ERROR(dev, "ref: bad pll 0x%06x\n", base);
114 return 0;
115 }
116
117 switch (rsel) {
118 case 0: id = 1; break;
119 case 1: return read_clk(dev, clk_src_crystal);
120 case 2: return read_clk(dev, clk_src_href);
121 case 3: id = 0; break;
122 }
123
124 coef = nv_rd32(dev, 0x00e81c + (id * 0x28));
125 P = (nv_rd32(dev, 0x00e824 + (id * 0x28)) >> 16) & 7;
126 P += (coef & 0x00070000) >> 16;
127 N = (coef & 0x0000ff00) >> 8;
128 M = (coef & 0x000000ff) >> 0;
129 break;
130 default:
131 BUG_ON(1);
132 }
133
134 if (M)
135 return (ref * N / M) >> P;
136 return 0;
137}
138
139static u32
140read_pll(struct drm_device *dev, u32 base)
141{
142 struct drm_nouveau_private *dev_priv = dev->dev_private;
143 u32 mast = nv_rd32(dev, 0x00c040);
144 u32 src = 0, ref = 0, clk = 0;
145 u32 ctrl, coef;
146 int N1, N2, M1, M2;
147
148 switch (base) {
149 case 0x004028:
150 if (mast & 0x00100000) {
151 /* wtf, appears to only disable post-divider on nva0 */
152 if (dev_priv->chipset != 0xa0)
153 return read_clk(dev, clk_src_dom6);
154 }
155 src = !!(mast & 0x00200000);
156 break;
157 case 0x004020:
158 src = !!(mast & 0x00400000);
159 break;
160 case 0x004008:
161 src = !!(mast & 0x00010000);
162 break;
163 case 0x004030:
164 src = !!(mast & 0x02000000);
165 break;
166 case 0x00e810:
167 ref = read_clk(dev, clk_src_crystal);
168 break;
169 default:
170 NV_ERROR(dev, "bad pll 0x%06x\n", base);
171 return 0;
172 }
173
174 if (ref == 0) {
175 if (src)
176 ref = read_clk(dev, clk_src_href);
177 else
178 ref = read_pll_ref(dev, base);
179 }
180
181 ctrl = nv_rd32(dev, base + 0);
182 coef = nv_rd32(dev, base + 4);
183
184 N2 = (coef & 0xff000000) >> 24;
185 M2 = (coef & 0x00ff0000) >> 16;
186 N1 = (coef & 0x0000ff00) >> 8;
187 M1 = (coef & 0x000000ff);
188 if ((ctrl & 0x80000000) && M1) {
189 clk = ref * N1 / M1;
190 if ((ctrl & 0x40000100) == 0x40000000) {
191 if (M2)
192 clk = clk * N2 / M2;
193 else
194 clk = 0;
Emil Velikov619d4f72011-04-11 20:43:23 +0100195 }
196 }
197
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000198 return clk;
199}
Ben Skeggs02c30ca2010-09-16 16:17:35 +1000200
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000201static u32
202read_clk(struct drm_device *dev, enum clk_src src)
203{
204 struct drm_nouveau_private *dev_priv = dev->dev_private;
205 u32 mast = nv_rd32(dev, 0x00c040);
206 u32 P = 0;
207
208 switch (src) {
209 case clk_src_crystal:
210 return dev_priv->crystal;
211 case clk_src_href:
212 return 100000; /* PCIE reference clock */
213 case clk_src_hclk:
214 return read_clk(dev, clk_src_href) * 27778 / 10000;
215 case clk_src_hclkm3:
216 return read_clk(dev, clk_src_hclk) * 3;
217 case clk_src_hclkm3d2:
218 return read_clk(dev, clk_src_hclk) * 3 / 2;
219 case clk_src_host:
220 switch (mast & 0x30000000) {
221 case 0x00000000: return read_clk(dev, clk_src_href);
222 case 0x10000000: break;
223 case 0x20000000: /* !0x50 */
224 case 0x30000000: return read_clk(dev, clk_src_hclk);
225 }
226 break;
227 case clk_src_nvclk:
228 if (!(mast & 0x00100000))
229 P = (nv_rd32(dev, 0x004028) & 0x00070000) >> 16;
230 switch (mast & 0x00000003) {
231 case 0x00000000: return read_clk(dev, clk_src_crystal) >> P;
232 case 0x00000001: return read_clk(dev, clk_src_dom6);
233 case 0x00000002: return read_pll(dev, 0x004020) >> P;
234 case 0x00000003: return read_pll(dev, 0x004028) >> P;
235 }
236 break;
237 case clk_src_sclk:
238 P = (nv_rd32(dev, 0x004020) & 0x00070000) >> 16;
239 switch (mast & 0x00000030) {
240 case 0x00000000:
241 if (mast & 0x00000080)
242 return read_clk(dev, clk_src_host) >> P;
243 return read_clk(dev, clk_src_crystal) >> P;
244 case 0x00000010: break;
245 case 0x00000020: return read_pll(dev, 0x004028) >> P;
246 case 0x00000030: return read_pll(dev, 0x004020) >> P;
247 }
248 break;
249 case clk_src_mclk:
250 P = (nv_rd32(dev, 0x004008) & 0x00070000) >> 16;
251 if (nv_rd32(dev, 0x004008) & 0x00000200) {
252 switch (mast & 0x0000c000) {
253 case 0x00000000:
254 return read_clk(dev, clk_src_crystal) >> P;
255 case 0x00008000:
256 case 0x0000c000:
257 return read_clk(dev, clk_src_href) >> P;
258 }
259 } else {
260 return read_pll(dev, 0x004008) >> P;
261 }
262 break;
263 case clk_src_vdec:
264 P = (read_div(dev) & 0x00000700) >> 8;
265 switch (dev_priv->chipset) {
266 case 0x84:
267 case 0x86:
268 case 0x92:
269 case 0x94:
270 case 0x96:
271 case 0xa0:
272 switch (mast & 0x00000c00) {
273 case 0x00000000:
274 if (dev_priv->chipset == 0xa0) /* wtf?? */
275 return read_clk(dev, clk_src_nvclk) >> P;
276 return read_clk(dev, clk_src_crystal) >> P;
277 case 0x00000400:
278 return 0;
279 case 0x00000800:
280 if (mast & 0x01000000)
281 return read_pll(dev, 0x004028) >> P;
282 return read_pll(dev, 0x004030) >> P;
283 case 0x00000c00:
284 return read_clk(dev, clk_src_nvclk) >> P;
285 }
286 break;
287 case 0x98:
288 switch (mast & 0x00000c00) {
289 case 0x00000000:
290 return read_clk(dev, clk_src_nvclk) >> P;
291 case 0x00000400:
292 return 0;
293 case 0x00000800:
294 return read_clk(dev, clk_src_hclkm3d2) >> P;
295 case 0x00000c00:
296 return read_pll(dev, clk_src_mclk) >> P;
297 }
298 break;
299 }
300 break;
301 case clk_src_dom6:
302 switch (dev_priv->chipset) {
303 case 0x50:
304 case 0xa0:
305 return read_pll(dev, 0x00e810) >> 2;
306 case 0x84:
307 case 0x86:
308 case 0x92:
309 case 0x94:
310 case 0x96:
311 case 0x98:
312 P = (read_div(dev) & 0x00000007) >> 0;
313 switch (mast & 0x0c000000) {
314 case 0x00000000: return read_clk(dev, clk_src_href);
315 case 0x04000000: break;
316 case 0x08000000: return read_clk(dev, clk_src_hclk);
317 case 0x0c000000:
318 return read_clk(dev, clk_src_hclkm3) >> P;
319 }
320 break;
321 default:
322 break;
323 }
324 default:
325 break;
326 }
327
328 NV_DEBUG(dev, "unknown clock source %d 0x%08x\n", src, mast);
329 return 0;
330}
331
332int
333nv50_pm_clocks_get(struct drm_device *dev, struct nouveau_pm_level *perflvl)
334{
335 struct drm_nouveau_private *dev_priv = dev->dev_private;
336 if (dev_priv->chipset == 0xaa ||
337 dev_priv->chipset == 0xac)
338 return 0;
339
340 perflvl->core = read_clk(dev, clk_src_nvclk);
341 perflvl->shader = read_clk(dev, clk_src_sclk);
342 perflvl->memory = read_clk(dev, clk_src_mclk);
343 if (dev_priv->chipset != 0x50) {
344 perflvl->vdec = read_clk(dev, clk_src_vdec);
345 perflvl->dom6 = read_clk(dev, clk_src_dom6);
346 }
347
348 return 0;
349}
350
351struct nv50_pm_state {
352 u32 emast;
353 u32 nctrl;
354 u32 ncoef;
355 u32 sctrl;
356 u32 scoef;
357
358 u32 amast;
359 u32 pdivs;
360
361 u32 mscript;
362 u32 mctrl;
363 u32 mcoef;
364};
365
366static u32
367calc_pll(struct drm_device *dev, u32 reg, struct pll_lims *pll,
368 u32 clk, int *N1, int *M1, int *log2P)
369{
370 struct nouveau_pll_vals coef;
371 int ret;
372
373 ret = get_pll_limits(dev, reg, pll);
374 if (ret)
375 return 0;
376
377 pll->vco2.maxfreq = 0;
378 pll->refclk = read_pll_ref(dev, reg);
379 if (!pll->refclk)
380 return 0;
381
382 ret = nouveau_calc_pll_mnp(dev, pll, clk, &coef);
383 if (ret == 0)
384 return 0;
385
386 *N1 = coef.N1;
387 *M1 = coef.M1;
388 *log2P = coef.log2P;
389 return ret;
390}
391
392static inline u32
393calc_div(u32 src, u32 target, int *div)
394{
395 u32 clk0 = src, clk1 = src;
396 for (*div = 0; *div <= 7; (*div)++) {
397 if (clk0 <= target) {
398 clk1 = clk0 << (*div ? 1 : 0);
399 break;
400 }
401 clk0 >>= 1;
402 }
403
404 if (target - clk0 <= clk1 - target)
405 return clk0;
406 (*div)--;
407 return clk1;
408}
409
410static inline u32
411clk_same(u32 a, u32 b)
412{
413 return ((a / 1000) == (b / 1000));
Ben Skeggs02c30ca2010-09-16 16:17:35 +1000414}
415
416void *
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000417nv50_pm_clocks_pre(struct drm_device *dev, struct nouveau_pm_level *perflvl)
Ben Skeggs02c30ca2010-09-16 16:17:35 +1000418{
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000419 struct drm_nouveau_private *dev_priv = dev->dev_private;
420 struct nv50_pm_state *info;
421 struct pll_lims pll;
422 int ret = -EINVAL;
423 int N, M, P1, P2;
424 u32 clk, out;
Ben Skeggs02c30ca2010-09-16 16:17:35 +1000425
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000426 if (dev_priv->chipset == 0xaa ||
427 dev_priv->chipset == 0xac)
428 return ERR_PTR(-ENODEV);
429
430 info = kmalloc(sizeof(*info), GFP_KERNEL);
431 if (!info)
Ben Skeggs02c30ca2010-09-16 16:17:35 +1000432 return ERR_PTR(-ENOMEM);
Ben Skeggs02c30ca2010-09-16 16:17:35 +1000433
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000434 /* core: for the moment at least, always use nvpll */
435 clk = calc_pll(dev, 0x4028, &pll, perflvl->core, &N, &M, &P1);
436 if (clk == 0)
437 goto error;
438
439 info->emast = 0x00000003;
440 info->nctrl = 0x80000000 | (P1 << 19) | (P1 << 16);
441 info->ncoef = (N << 8) | M;
442
443 /* shader: tie to nvclk if possible, otherwise use spll. have to be
444 * very careful that the shader clock is at least twice the core, or
445 * some chipsets will be very unhappy. i expect most or all of these
446 * cases will be handled by tying to nvclk, but it's possible there's
447 * corners
448 */
449 if (P1-- && perflvl->shader == (perflvl->core << 1)) {
450 info->emast |= 0x00000020;
451 info->sctrl = 0x00000000 | (P1 << 19) | (P1 << 16);
452 info->scoef = nv_rd32(dev, 0x004024);
453 } else {
454 clk = calc_pll(dev, 0x4020, &pll, perflvl->shader, &N, &M, &P1);
455 if (clk == 0)
456 goto error;
457
458 info->emast |= 0x00000030;
459 info->sctrl = 0x80000000 | (P1 << 19) | (P1 << 16);
460 info->scoef = (N << 8) | M;
Ben Skeggs02c30ca2010-09-16 16:17:35 +1000461 }
462
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000463 /* memory: use pcie refclock if possible, otherwise use mpll */
464 info->mscript = perflvl->memscript;
465 if (clk_same(perflvl->memory, read_clk(dev, clk_src_href))) {
466 info->mctrl = nv_rd32(dev, 0x4008) | 0x00000200;
467 info->mcoef = nv_rd32(dev, 0x400c);
468 } else
469 if (perflvl->memory) {
470 clk = calc_pll(dev, 0x4008, &pll, perflvl->memory,
471 &N, &M, &P1);
472 if (clk == 0)
473 goto error;
474
475 info->mctrl = 0x80000000 | (P1 << 22) | (P1 << 16);
476 info->mctrl |= pll.log2p_bias << 19;
477 info->mcoef = (N << 8) | M;
478 } else {
479 info->mctrl = 0x00000000;
Ben Skeggs02c30ca2010-09-16 16:17:35 +1000480 }
481
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000482 /* vdec: avoid modifying xpll until we know exactly how the other
483 * clock domains work, i suspect at least some of them can also be
484 * tied to xpll...
485 */
486 info->amast = info->pdivs = 0;
487 if (perflvl->vdec) {
488 /* see how close we can get using nvclk as a source */
489 clk = calc_div(perflvl->core, perflvl->vdec, &P1);
490
491 /* see how close we can get using xpll/hclk as a source */
492 if (dev_priv->chipset != 0x98)
493 out = read_pll(dev, 0x004030);
494 else
495 out = read_clk(dev, clk_src_hclkm3d2);
496 out = calc_div(out, perflvl->vdec, &P2);
497
498 /* select whichever gets us closest */
499 if (abs((int)perflvl->vdec - clk) <=
500 abs((int)perflvl->vdec - out)) {
501 if (dev_priv->chipset != 0x98)
502 info->amast |= 0x00000c00;
503 else
504 info->amast |= 0x00000000;
505 info->pdivs |= P1 << 8;
506 } else {
507 info->amast |= 0x00000800;
508 info->pdivs |= P2 << 8;
509 }
510 }
511
512 /* dom6: nfi what this is, but we're limited to various combinations
513 * of the host clock frequency
514 */
515 if (clk_same(perflvl->dom6, read_clk(dev, clk_src_href))) {
516 info->amast |= 0x00000000;
517 info->pdivs |= read_div(dev) & 0x00000007;
518 } else
519 if (clk_same(perflvl->dom6, read_clk(dev, clk_src_hclk))) {
520 info->amast |= 0x08000000;
521 info->pdivs |= read_div(dev) & 0x00000007;
522 } else
523 if (perflvl->dom6) {
524 clk = read_clk(dev, clk_src_hclk) * 3;
525 clk = calc_div(clk, perflvl->dom6, &P1);
526
527 info->amast |= 0x0c000000;
528 info->pdivs |= P1;
529 }
530
531
532 return info;
533error:
534 kfree(info);
535 return ERR_PTR(ret);
Ben Skeggs02c30ca2010-09-16 16:17:35 +1000536}
537
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000538int
539nv50_pm_clocks_set(struct drm_device *dev, void *data)
Ben Skeggs02c30ca2010-09-16 16:17:35 +1000540{
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000541 struct drm_nouveau_private *dev_priv = dev->dev_private;
542 struct nv50_pm_state *info = data;
543 struct bit_entry M;
544 int ret = 0;
Ben Skeggs02c30ca2010-09-16 16:17:35 +1000545
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000546 /* halt and idle execution engines */
547 nv_mask(dev, 0x002504, 0x00000001, 0x00000001);
548 if (!nv_wait(dev, 0x002504, 0x00000010, 0x00000010))
549 goto error;
Ben Skeggsaee582d2010-09-27 10:13:23 +1000550
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000551 /* reclock vdec/dom6 */
552 nv_mask(dev, 0x00c040, 0x00000c00, 0x00000000);
553 switch (dev_priv->chipset) {
554 case 0x92:
555 case 0x94:
556 case 0x96:
557 nv_mask(dev, 0x004800, 0x00000707, info->pdivs);
558 break;
559 default:
560 nv_mask(dev, 0x004700, 0x00000707, info->pdivs);
561 break;
562 }
563 nv_mask(dev, 0x00c040, 0x0c000c00, info->amast);
564
Ben Skeggs68059792011-10-30 23:04:31 +1000565 /* core/shader: make sure sclk/nvclk are disconnected from their
566 * plls (nvclk to dom6, sclk to hclk), modify the plls, and
567 * reconnect sclk/nvclk to their new clock source
568 */
569 if (dev_priv->chipset < 0x92)
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000570 nv_mask(dev, 0x00c040, 0x001000b0, 0x00100080); /* grrr! */
571 else
572 nv_mask(dev, 0x00c040, 0x000000b3, 0x00000081);
573 nv_mask(dev, 0x004020, 0xc03f0100, info->sctrl);
574 nv_wr32(dev, 0x004024, info->scoef);
575 nv_mask(dev, 0x004028, 0xc03f0100, info->nctrl);
576 nv_wr32(dev, 0x00402c, info->ncoef);
577 nv_mask(dev, 0x00c040, 0x00100033, info->emast);
578
579 /* memory */
580 if (!info->mctrl)
581 goto resume;
582
583 /* execute some scripts that do ??? from the vbios.. */
584 if (!bit_table(dev, 'M', &M) && M.version == 1) {
585 if (M.length >= 6)
586 nouveau_bios_init_exec(dev, ROM16(M.data[5]));
587 if (M.length >= 8)
588 nouveau_bios_init_exec(dev, ROM16(M.data[7]));
589 if (M.length >= 10)
590 nouveau_bios_init_exec(dev, ROM16(M.data[9]));
591 nouveau_bios_init_exec(dev, info->mscript);
Ben Skeggsaee582d2010-09-27 10:13:23 +1000592 }
593
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000594 /* disable display */
595 nv_wr32(dev, 0x611200, 0x00003300);
596 udelay(100);
Ben Skeggs02c30ca2010-09-16 16:17:35 +1000597
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000598 /* prepare ram for reclocking */
599 nv_wr32(dev, 0x1002d4, 0x00000001); /* precharge */
600 nv_wr32(dev, 0x1002d0, 0x00000001); /* refresh */
601 nv_wr32(dev, 0x1002d0, 0x00000001); /* refresh */
602 nv_mask(dev, 0x100210, 0x80000000, 0x00000000); /* no auto-refresh */
603 nv_wr32(dev, 0x1002dc, 0x00000001); /* enable self-refresh */
Ben Skeggs02c30ca2010-09-16 16:17:35 +1000604
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000605 /* modify mpll */
606 nv_mask(dev, 0x00c040, 0x0000c000, 0x0000c000);
607 nv_mask(dev, 0x004008, 0x81ff0200, 0x00000200 | info->mctrl);
608 nv_wr32(dev, 0x00400c, info->mcoef);
609 udelay(100);
610 nv_mask(dev, 0x004008, 0x81ff0200, info->mctrl);
Ben Skeggs02c30ca2010-09-16 16:17:35 +1000611
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000612 /* re-enable normal operation of memory controller */
613 nv_wr32(dev, 0x1002dc, 0x00000000);
614 nv_mask(dev, 0x100210, 0x80000000, 0x80000000);
615 udelay(100);
616
617 /* re-enable display */
618 nv_wr32(dev, 0x611200, 0x00003330);
619
620 goto resume;
621error:
622 ret = -EBUSY;
623resume:
624 nv_mask(dev, 0x002504, 0x00000001, 0x00000000);
Ben Skeggs19fa2242011-10-28 22:10:15 +1000625 kfree(info);
Ben Skeggsf3fbaf32011-10-26 09:11:02 +1000626 return ret;
Ben Skeggs02c30ca2010-09-16 16:17:35 +1000627}
628
Ben Skeggscb9fa622011-08-14 12:43:47 +1000629static int
Ben Skeggs5a4267a2011-09-17 02:01:24 +1000630pwm_info(struct drm_device *dev, struct dcb_gpio_entry *gpio,
631 int *ctrl, int *line, int *indx)
Ben Skeggscb9fa622011-08-14 12:43:47 +1000632{
Ben Skeggs5a4267a2011-09-17 02:01:24 +1000633 if (gpio->line == 0x04) {
634 *ctrl = 0x00e100;
635 *line = 4;
636 *indx = 0;
637 } else
638 if (gpio->line == 0x09) {
639 *ctrl = 0x00e100;
640 *line = 9;
641 *indx = 1;
642 } else
643 if (gpio->line == 0x10) {
644 *ctrl = 0x00e28c;
645 *line = 0;
646 *indx = 0;
647 } else {
648 NV_ERROR(dev, "unknown pwm ctrl for gpio %d\n", gpio->line);
649 return -ENODEV;
Ben Skeggscb9fa622011-08-14 12:43:47 +1000650 }
651
Ben Skeggs5a4267a2011-09-17 02:01:24 +1000652 return 0;
Ben Skeggscb9fa622011-08-14 12:43:47 +1000653}
654
655int
Ben Skeggs5a4267a2011-09-17 02:01:24 +1000656nv50_pm_pwm_get(struct drm_device *dev, struct dcb_gpio_entry *gpio,
657 u32 *divs, u32 *duty)
Ben Skeggscb9fa622011-08-14 12:43:47 +1000658{
Ben Skeggs5a4267a2011-09-17 02:01:24 +1000659 int ctrl, line, id, ret = pwm_info(dev, gpio, &ctrl, &line, &id);
Ben Skeggscb9fa622011-08-14 12:43:47 +1000660 if (ret)
661 return ret;
662
Ben Skeggs5a4267a2011-09-17 02:01:24 +1000663 if (nv_rd32(dev, ctrl) & (1 << line)) {
664 *divs = nv_rd32(dev, 0x00e114 + (id * 8));
665 *duty = nv_rd32(dev, 0x00e118 + (id * 8));
Ben Skeggscb9fa622011-08-14 12:43:47 +1000666 return 0;
667 }
668
Ben Skeggs5a4267a2011-09-17 02:01:24 +1000669 return -EINVAL;
Ben Skeggscb9fa622011-08-14 12:43:47 +1000670}
671
672int
Ben Skeggs5a4267a2011-09-17 02:01:24 +1000673nv50_pm_pwm_set(struct drm_device *dev, struct dcb_gpio_entry *gpio,
674 u32 divs, u32 duty)
Ben Skeggscb9fa622011-08-14 12:43:47 +1000675{
Ben Skeggs5a4267a2011-09-17 02:01:24 +1000676 int ctrl, line, id, ret = pwm_info(dev, gpio, &ctrl, &line, &id);
Ben Skeggscb9fa622011-08-14 12:43:47 +1000677 if (ret)
678 return ret;
679
Ben Skeggs5a4267a2011-09-17 02:01:24 +1000680 nv_mask(dev, ctrl, 0x00010001 << line, 0x00000001 << line);
681 nv_wr32(dev, 0x00e114 + (id * 8), divs);
682 nv_wr32(dev, 0x00e118 + (id * 8), duty | 0x80000000);
Ben Skeggscb9fa622011-08-14 12:43:47 +1000683 return 0;
684}